Add uploading indicator. Fix #73

This commit is contained in:
ArsenArsen 2018-05-09 20:40:57 +02:00
parent 4a9e28b84c
commit 81cfe1584e
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
13 changed files with 69 additions and 80 deletions

View File

@ -18,5 +18,6 @@
<file>icons/crop.png</file> <file>icons/crop.png</file>
<file>icons/circle.png</file> <file>icons/circle.png</file>
<file>icons/highlighter.png</file> <file>icons/highlighter.png</file>
<file>icons/infinity.png</file>
</qresource> </qresource>
</RCC> </RCC>

BIN
src/icons/infinity.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

@ -3,6 +3,7 @@
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <thread>
#include <logs/requestlogging.hpp> #include <logs/requestlogging.hpp>
QNetworkAccessManager ioutils::networkManager; QNetworkAccessManager ioutils::networkManager;
@ -16,6 +17,16 @@ void addLogEntry(QNetworkReply *reply, QByteArray data) {
requestlogging::addEntry(ctx); requestlogging::addEntry(ctx);
} }
int tasks = 0;
void addTask() {
requestlogging::indicator::show(++tasks);
}
void removeTask() {
requestlogging::indicator::show(--tasks);
}
void ioutils::postMultipart(QUrl target, void ioutils::postMultipart(QUrl target,
QList<QPair<QString, QString>> headers, QList<QPair<QString, QString>> headers,
QHttpMultiPart *body, QHttpMultiPart *body,
@ -25,7 +36,9 @@ void ioutils::postMultipart(QUrl target,
if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.post(req, body); QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(QJsonDocument::fromJson(data), data, reply); callback(QJsonDocument::fromJson(data), data, reply);
@ -42,7 +55,9 @@ void ioutils::postMultipartData(QUrl target,
if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.post(req, body); QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(data, reply); callback(data, reply);
@ -58,7 +73,9 @@ void ioutils::getJson(QUrl target,
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.get(req); QNetworkReply *reply = networkManager.get(req);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(QJsonDocument::fromJson(data), data, reply); callback(QJsonDocument::fromJson(data), data, reply);
@ -75,7 +92,9 @@ void ioutils::postJson(QUrl target,
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.post(req, body); QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(QJsonDocument::fromJson(data), data, reply); callback(QJsonDocument::fromJson(data), data, reply);
@ -89,7 +108,9 @@ void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, std::
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.get(req); QNetworkReply *reply = networkManager.get(req);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(data, reply); callback(data, reply);
@ -106,7 +127,9 @@ void ioutils::postData(QUrl target,
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8()); req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
} }
QNetworkReply *reply = networkManager.post(req, body); QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] { QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
removeTask();
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
addLogEntry(reply, data); addLogEntry(reply, data);
callback(data, reply); callback(data, reply);
@ -128,7 +151,6 @@ QString ioutils::methodString(QNetworkAccessManager::Operation operation) {
case QNetworkAccessManager::HeadOperation: case QNetworkAccessManager::HeadOperation:
return "HEAD"; return "HEAD";
default: default:
// return "Dunno";
return "Unknown"; return "Unknown";
} }
} }

View File

@ -1,6 +1,8 @@
#include "requestlogging.hpp" #include "requestlogging.hpp"
#include <QDateTime> #include <QDateTime>
#include <mainwindow.hpp>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <utils.hpp>
// $type $url $status $time // $type $url $status $time
// $type = GET POST PATCH DELETE etc // $type = GET POST PATCH DELETE etc
@ -63,3 +65,7 @@ QList<LoggedRequest> requestlogging::getRequests() {
return ret; return ret;
} }
void requestlogging::indicator::show(int count) {
MainWindow::inst()->setTrayIcon(utils::getTrayIcon(count));
}

View File

@ -41,6 +41,10 @@ namespace requestlogging {
QList<LoggedRequest> getRequests(); QList<LoggedRequest> getRequests();
void addEntry(RequestContext context); void addEntry(RequestContext context);
namespace indicator {
void show(int count);
} // namespace indicator
} // namespace requestlogging } // namespace requestlogging
#endif // REQUESTLOGGING_HPP #endif // REQUESTLOGGING_HPP

View File

@ -187,3 +187,7 @@ void MainWindow::on_history_clicked() {
HistoryDialog *dialog = new HistoryDialog; HistoryDialog *dialog = new HistoryDialog;
dialog->show(); dialog->show();
} }
void MainWindow::setTrayIcon(QIcon icon) {
tray->setIcon(icon);
}

View File

@ -37,10 +37,11 @@ public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
bool valid(); bool valid();
Ui::MainWindow *ui; void setTrayIcon(QIcon icon);
RecordingController *controller = new RecordingController; RecordingController *controller = new RecordingController;
QSystemTrayIcon *tray; QSystemTrayIcon *tray;
Ui::MainWindow *ui;
public slots: public slots:
void rec(); void rec();

View File

@ -65,7 +65,6 @@ SOURCES += main.cpp\
hotkeyinputdialog.cpp \ hotkeyinputdialog.cpp \
cropeditor/drawing/arrowitem.cpp \ cropeditor/drawing/arrowitem.cpp \
uploaders/default/imgursettingsdialog.cpp \ uploaders/default/imgursettingsdialog.cpp \
uploaders/default/imgplusuploader.cpp \
filenamevalidator.cpp \ filenamevalidator.cpp \
logs/requestlogging.cpp \ logs/requestlogging.cpp \
logs/historydialog.cpp \ logs/historydialog.cpp \
@ -118,7 +117,6 @@ HEADERS += mainwindow.hpp \
hotkeyinputdialog.hpp \ hotkeyinputdialog.hpp \
cropeditor/drawing/arrowitem.hpp \ cropeditor/drawing/arrowitem.hpp \
uploaders/default/imgursettingsdialog.hpp \ uploaders/default/imgursettingsdialog.hpp \
uploaders/default/imgplusuploader.hpp \
filenamevalidator.hpp \ filenamevalidator.hpp \
logs/requestlogging.hpp \ logs/requestlogging.hpp \
logs/historydialog.hpp \ logs/historydialog.hpp \

View File

@ -1,53 +0,0 @@
#include "imgplusuploader.hpp"
#include <QApplication>
#include <QBuffer>
#include <QClipboard>
#include <QHttpMultiPart>
#include <QInputDialog>
#include <formats.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <settings.hpp>
void ImgplusUploader::doUpload(QByteArray byteArray, QString format) {
QString mime;
if (formats::normalFormatFromName(format) != formats::Normal::None)
mime = formats::normalFormatMIME(formats::normalFormatFromName(format));
else
mime = formats::recordingFormatMIME(formats::recordingFormatFromName(format));
QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"file\"; filename=\"image." + format + "\"");
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, mime);
QByteArray *data = new QByteArray;
data->swap(byteArray);
QBuffer *buff = new QBuffer(data);
buff->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(buff);
multipart->append(imagePart);
QHttpPart keyPart;
keyPart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"api_key\"");
keyPart.setBody(settings::settings().value("imgplus/apikey").toString().toUtf8());
multipart->append(keyPart);
ioutils::postMultipartData(QUrl("http://imgpl.us/api/upload"), { QPair<QString, QString>("Accept", "application/json") },
multipart, [](QByteArray link, QNetworkReply *) {
QApplication::clipboard()->setText(QString::fromUtf8(link));
if (!link.startsWith("http"))
qCritical() << QObject::tr("Failed to upload! Copied the response to clipboard");
else
notifications::notify(QObject::tr("KShare imgplus Uploader"),
QObject::tr("Uploaded to ImagePlus!"));
});
}
void ImgplusUploader::showSettings() {
QString text = QInputDialog::getText(0, tr("imgplus API key"), tr("Enter the imgpl.us API key (Found in Settings)"),
QLineEdit::Normal, settings::settings().value("imgplus/apikey").toString());
if (!text.isNull()) settings::settings().setValue("imgplus/apikey", text);
}
bool ImgplusUploader::validate() {
return settings::settings().value("imgplus/apikey").userType() == QMetaType::QString;
}

View File

@ -1,21 +0,0 @@
#ifndef IMGPLUSUPLOADER_HPP
#define IMGPLUSUPLOADER_HPP
#include <QApplication>
#include <uploaders/uploader.hpp>
class ImgplusUploader : public Uploader {
Q_DECLARE_TR_FUNCTIONS(ImgplusUploader)
public:
QString name() override {
return "ImagePlus";
}
QString description() override {
return "imgpl.us uploader";
}
void doUpload(QByteArray byteArray, QString format) override;
void showSettings() override;
bool validate() override;
};
#endif // IMGPLUSUPLOADER_HPP

View File

@ -12,7 +12,6 @@
#include <logger.hpp> #include <logger.hpp>
#include <notifications.hpp> #include <notifications.hpp>
#include <settings.hpp> #include <settings.hpp>
#include <uploaders/default/imgplusuploader.hpp>
UploaderSingleton::UploaderSingleton() : QObject() { UploaderSingleton::UploaderSingleton() : QObject() {
updateSaveSettings(); updateSaveSettings();
@ -31,7 +30,6 @@ UploaderSingleton::UploaderSingleton() : QObject() {
// UPLOADERS // UPLOADERS
registerUploader(new ImgurUploader); registerUploader(new ImgurUploader);
registerUploader(new ImgplusUploader);
registerUploader(new ClipboardUploader); registerUploader(new ClipboardUploader);
// --------- // ---------

View File

@ -183,3 +183,30 @@ void utils::externalScreenshotActive(std::function<void(QPixmap)> callback) {
}); });
process->start(args.takeFirst(), args); process->start(args.takeFirst(), args);
} }
QIcon defaultIcon() {
static QIcon icon = QIcon(":/icons/icon.png");
return icon;
}
QIcon infinity() {
static QIcon icon = QIcon(":/icons/infinity.png");
return icon;
}
QIcon utils::getTrayIcon(int num) {
if (!num) {
return defaultIcon();
} else if (num < 100) {
QPixmap unscaled = utils::renderText(QString::number(num), 0, Qt::lightGray, Qt::black);
int dim = qMax(unscaled.width(), unscaled.height());
QPixmap scaled(dim, dim);
scaled.fill(Qt::lightGray);
QPainter *painter = new QPainter(&scaled);
painter->drawPixmap((dim / 2) - (unscaled.width() / 2), 0, unscaled);
delete painter;
return scaled;
} else {
return infinity();
}
}

View File

@ -3,6 +3,7 @@
#include <QApplication> #include <QApplication>
#include <QPixmap> #include <QPixmap>
#include <QIcon>
#include <QScreen> #include <QScreen>
#include <QWidget> #include <QWidget>
#include <functional> #include <functional>
@ -20,6 +21,7 @@ namespace utils {
void externalScreenshot(std::function<void(QPixmap)> callback); void externalScreenshot(std::function<void(QPixmap)> callback);
void externalScreenshotActive(std::function<void(QPixmap)> callback); void externalScreenshotActive(std::function<void(QPixmap)> callback);
QString randomString(int length); QString randomString(int length);
QIcon getTrayIcon(int num);
} // namespace utils } // namespace utils
#endif // UTILS_HPP #endif // UTILS_HPP