summary refs log tree commit diff
path: root/src/dialogs
diff options
context:
space:
mode:
authorchristarazi <christarazi@users.noreply.github.com>2018-02-18 12:52:31 -0800
committermujx <mujx@users.noreply.github.com>2018-02-18 22:52:31 +0200
commitcd9d1a2ec69bf4c459f9e845293d5fd7dc1398d4 (patch)
treea3669f25c8ecaf716061b3080ae9f9073134da05 /src/dialogs
parentShow loading indicator while waiting for /login & /logout (diff)
downloadnheko-cd9d1a2ec69bf4c459f9e845293d5fd7dc1398d4.tar.xz
Support audio, video, generic file for pasting (#220)
* Refactor widget items to use same interface

* Support audio, video, generic file for pasting

* Add utils function for human readable file sizes

* Set correct MIME type for media messages

This change also determines the size of the upload once from the
ContentLengthHeader, rather than seeking the QIODevice and asking for
its size. This prevents any future trouble in case the QIODevice is
sequential (cannot be seeked). The MIME type is also determined at
upload once, rather than using the QIODevice and the underlying data
inside.

* Allow for file urls to be used as fall-back

This fixes an issue on macOS which uses `text/uri-list` for copying
files to the clipboard.

fixes #228 
Diffstat (limited to 'src/dialogs')
-rw-r--r--src/dialogs/PreviewImageOverlay.cc142
-rw-r--r--src/dialogs/PreviewUploadOverlay.cc170
2 files changed, 170 insertions, 142 deletions
diff --git a/src/dialogs/PreviewImageOverlay.cc b/src/dialogs/PreviewImageOverlay.cc
deleted file mode 100644

index 31ef00ed..00000000 --- a/src/dialogs/PreviewImageOverlay.cc +++ /dev/null
@@ -1,142 +0,0 @@ -/* - * nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <QApplication> -#include <QBuffer> -#include <QDebug> -#include <QFile> -#include <QFileInfo> -#include <QHBoxLayout> -#include <QVBoxLayout> - -#include "Config.h" - -#include "dialogs/PreviewImageOverlay.h" - -using namespace dialogs; - -static constexpr const char *DEFAULT = "Upload image?"; -static constexpr const char *ERROR = "Failed to load image type '%1'. Continue upload?"; - -PreviewImageOverlay::PreviewImageOverlay(QWidget *parent) - : QWidget{parent} - , titleLabel_{tr(DEFAULT), this} - , imageLabel_{this} - , imageName_{tr("clipboard"), this} - , upload_{tr("Upload"), this} - , cancel_{tr("Cancel"), this} -{ - auto hlayout = new QHBoxLayout; - hlayout->addWidget(&upload_); - hlayout->addWidget(&cancel_); - - auto vlayout = new QVBoxLayout{this}; - vlayout->addWidget(&titleLabel_); - vlayout->addWidget(&imageLabel_); - vlayout->addWidget(&imageName_); - vlayout->addLayout(hlayout); - - connect(&upload_, &QPushButton::clicked, [&]() { - emit confirmImageUpload(imageData_, imageName_.text()); - close(); - }); - connect(&cancel_, &QPushButton::clicked, [&]() { close(); }); -} - -void -PreviewImageOverlay::init() -{ - auto window = QApplication::activeWindow(); - auto winsize = window->frameGeometry().size(); - auto center = window->frameGeometry().center(); - auto img_size = image_.size(); - - imageName_.setText(QFileInfo{imagePath_}.fileName()); - - setAutoFillBackground(true); - setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); - setWindowModality(Qt::WindowModal); - - titleLabel_.setStyleSheet( - QString{"font-weight: bold; font-size: %1px;"}.arg(conf::headerFontSize)); - titleLabel_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - titleLabel_.setAlignment(Qt::AlignCenter); - imageLabel_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); - imageLabel_.setAlignment(Qt::AlignCenter); - imageName_.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); - imageName_.setAlignment(Qt::AlignCenter); - upload_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - cancel_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - upload_.setFontSize(conf::btn::fontSize); - cancel_.setFontSize(conf::btn::fontSize); - - // Scale image preview to the size of the current window if it is larger. - if ((img_size.height() * img_size.width()) > (winsize.height() * winsize.width())) { - imageLabel_.setPixmap(image_.scaled(winsize, Qt::KeepAspectRatio)); - } else { - imageLabel_.setPixmap(image_); - move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); - } - imageLabel_.setScaledContents(false); - - raise(); -} - -void -PreviewImageOverlay::setImageAndCreate(const QByteArray data, const QString &type) -{ - imageData_ = data; - imagePath_ = "clipboard." + type; - auto loaded = image_.loadFromData(imageData_); - if (!loaded) { - titleLabel_.setText(QString{tr(ERROR)}.arg(type)); - } else { - titleLabel_.setText(tr(DEFAULT)); - } - - init(); -} - -void -PreviewImageOverlay::setImageAndCreate(const QString &path) -{ - QFile file{path}; - imagePath_ = path; - - if (!file.open(QIODevice::ReadOnly)) { - qWarning() << "Failed to open image from:" << path; - qWarning() << "Reason:" << file.errorString(); - close(); - return; - } - - if ((imageData_ = file.readAll()).isEmpty()) { - qWarning() << "Failed to read image:" << file.errorString(); - close(); - return; - } - - auto loaded = image_.loadFromData(imageData_); - if (!loaded) { - auto t = QFileInfo{path}.suffix(); - titleLabel_.setText(QString{tr(ERROR)}.arg(t)); - } else { - titleLabel_.setText(tr(DEFAULT)); - } - - init(); -} diff --git a/src/dialogs/PreviewUploadOverlay.cc b/src/dialogs/PreviewUploadOverlay.cc new file mode 100644
index 00000000..f2007011 --- /dev/null +++ b/src/dialogs/PreviewUploadOverlay.cc
@@ -0,0 +1,170 @@ +/* + * nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <QApplication> +#include <QBuffer> +#include <QDebug> +#include <QFile> +#include <QFileInfo> +#include <QHBoxLayout> +#include <QMimeDatabase> +#include <QVBoxLayout> + +#include "Config.h" +#include "Utils.h" + +#include "dialogs/PreviewUploadOverlay.h" + +using namespace dialogs; + +static constexpr const char *DEFAULT = "Upload %1?"; +static constexpr const char *ERROR = "Failed to load image type '%1'. Continue upload?"; + +PreviewUploadOverlay::PreviewUploadOverlay(QWidget *parent) + : QWidget{parent} + , titleLabel_{this} + , fileName_{this} + , upload_{tr("Upload"), this} + , cancel_{tr("Cancel"), this} +{ + auto hlayout = new QHBoxLayout; + hlayout->addWidget(&upload_); + hlayout->addWidget(&cancel_); + + auto vlayout = new QVBoxLayout{this}; + vlayout->addWidget(&titleLabel_); + vlayout->addWidget(&infoLabel_); + vlayout->addWidget(&fileName_); + vlayout->addLayout(hlayout); + + connect(&upload_, &QPushButton::clicked, [&]() { + emit confirmUpload(data_, mediaType_, fileName_.text()); + close(); + }); + connect(&cancel_, &QPushButton::clicked, this, &PreviewUploadOverlay::close); +} + +void +PreviewUploadOverlay::init() +{ + auto window = QApplication::activeWindow(); + auto winsize = window->frameGeometry().size(); + auto center = window->frameGeometry().center(); + auto img_size = image_.size(); + + fileName_.setText(QFileInfo{filePath_}.fileName()); + + setAutoFillBackground(true); + setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); + setWindowModality(Qt::WindowModal); + + titleLabel_.setStyleSheet( + QString{"font-weight: bold; font-size: %1px;"}.arg(conf::headerFontSize)); + titleLabel_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + titleLabel_.setAlignment(Qt::AlignCenter); + infoLabel_.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); + fileName_.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + fileName_.setAlignment(Qt::AlignCenter); + upload_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + cancel_.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + upload_.setFontSize(conf::btn::fontSize); + cancel_.setFontSize(conf::btn::fontSize); + + if (isImage_) { + infoLabel_.setAlignment(Qt::AlignCenter); + + // Scale image preview to the size of the current window if it is larger. + if ((img_size.height() * img_size.width()) > (winsize.height() * winsize.width())) { + infoLabel_.setPixmap(image_.scaled(winsize, Qt::KeepAspectRatio)); + } else { + infoLabel_.setPixmap(image_); + move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); + } + } else { + infoLabel_.setAlignment(Qt::AlignLeft); + } + infoLabel_.setScaledContents(false); + + show(); +} + +void +PreviewUploadOverlay::setLabels(const QString &type, const QString &mime, const int upload_size) +{ + if (mediaType_ == "image") { + if (!image_.loadFromData(data_)) { + titleLabel_.setText(QString{tr(ERROR)}.arg(type)); + } else { + titleLabel_.setText(QString{tr(DEFAULT)}.arg(mediaType_)); + } + isImage_ = true; + } else { + auto const info = QString{tr("Media type: %1\n" + "Media size: %2\n")} + .arg(mime) + .arg(utils::humanReadableFileSize(upload_size)); + + titleLabel_.setText(QString{tr(DEFAULT)}.arg("file")); + infoLabel_.setText(info); + } +} + +void +PreviewUploadOverlay::setPreview(const QByteArray data, const QString &mime) +{ + auto const &split = mime.split('/'); + auto const &type = split[1]; + + data_ = data; + mediaType_ = split[0]; + filePath_ = "clipboard." + type; + isImage_ = false; + + setLabels(type, mime, data_.size()); + init(); +} + +void +PreviewUploadOverlay::setPreview(const QString &path) +{ + QFile file{path}; + + if (!file.open(QIODevice::ReadOnly)) { + qWarning() << "Failed to open file from:" << path; + qWarning() << "Reason:" << file.errorString(); + close(); + return; + } + + QMimeDatabase db; + auto mime = db.mimeTypeForFileNameAndData(path, &file); + + if ((data_ = file.readAll()).isEmpty()) { + qWarning() << "Failed to read media:" << file.errorString(); + close(); + return; + } + + auto const &split = mime.name().split('/'); + + mediaType_ = split[0]; + filePath_ = file.fileName(); + isImage_ = false; + + setLabels(split[1], mime.name(), data_.size()); + init(); +}