summary refs log tree commit diff
path: root/src/dialogs/PreviewUploadOverlay.cpp
blob: 31d0121406645a992c9a96f24333927f751d31c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * 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 <QFile>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QMimeDatabase>
#include <QVBoxLayout>

#include "dialogs/PreviewUploadOverlay.h"

#include "Config.h"
#include "Logging.h"
#include "MainWindow.h"
#include "Utils.h"

using namespace dialogs;

constexpr const char *DEFAULT = "Upload %1?";
constexpr const char *ERR_MSG = "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->addStretch(1);
        hlayout->addWidget(&cancel_);
        hlayout->addWidget(&upload_);
        hlayout->setMargin(0);

        auto vlayout = new QVBoxLayout{this};
        vlayout->addWidget(&titleLabel_);
        vlayout->addWidget(&infoLabel_);
        vlayout->addWidget(&fileName_);
        vlayout->addLayout(hlayout);
        vlayout->setSpacing(conf::modals::WIDGET_SPACING);
        vlayout->setMargin(conf::modals::WIDGET_MARGIN);

        upload_.setDefault(true);
        connect(&upload_, &QPushButton::clicked, [this]() {
                emit confirmUpload(data_, mediaType_, fileName_.text());
                close();
        });
        connect(&cancel_, &QPushButton::clicked, this, &PreviewUploadOverlay::close);
}

void
PreviewUploadOverlay::init()
{
        QSize winsize;
        QPoint center;

        auto window = MainWindow::instance();
        if (window) {
                winsize = window->frameGeometry().size();
                center  = window->frameGeometry().center();
        } else {
                nhlog::ui()->warn("unable to retrieve MainWindow's size");
        }

        fileName_.setText(QFileInfo{filePath_}.fileName());

        setAutoFillBackground(true);
        setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::WindowModal);

        QFont font;
        font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO);

        titleLabel_.setFont(font);
        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);

        if (isImage_) {
                infoLabel_.setAlignment(Qt::AlignCenter);

                const auto maxWidth  = winsize.width() * 0.8;
                const auto maxHeight = winsize.height() * 0.8;

                // Scale image preview to fit into the application window.
                infoLabel_.setPixmap(utils::scaleDown(maxWidth, maxHeight, 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, uint64_t upload_size)
{
        if (mediaType_ == "image") {
                if (!image_.loadFromData(data_)) {
                        titleLabel_.setText(QString{tr(ERR_MSG)}.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 QImage &src, const QString &mime)
{
        auto const &split = mime.split('/');
        auto const &type  = split[1];

        QBuffer buffer(&data_);
        buffer.open(QIODevice::WriteOnly);
        if (src.save(&buffer, type.toStdString().c_str()))
                titleLabel_.setText(QString{tr(DEFAULT)}.arg("image"));
        else
                titleLabel_.setText(QString{tr(ERR_MSG)}.arg(type));

        mediaType_ = split[0];
        filePath_  = "clipboard." + type;
        image_.convertFromImage(src);
        isImage_ = true;

        titleLabel_.setText(QString{tr(DEFAULT)}.arg("image"));
        init();
}

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)) {
                nhlog::ui()->warn("Failed to open file ({}): {}",
                                  path.toStdString(),
                                  file.errorString().toStdString());
                close();
                return;
        }

        QMimeDatabase db;
        auto mime = db.mimeTypeForFileNameAndData(path, &file);

        if ((data_ = file.readAll()).isEmpty()) {
                nhlog::ui()->warn("Failed to read media: {}", file.errorString().toStdString());
                close();
                return;
        }

        auto const &split = mime.name().split('/');

        mediaType_ = split[0];
        filePath_  = file.fileName();
        isImage_   = false;

        setLabels(split[1], mime.name(), data_.size());
        init();
}