Add uploading indicator. Fix #73
This commit is contained in:
parent
4a9e28b84c
commit
81cfe1584e
@ -18,5 +18,6 @@
|
||||
<file>icons/crop.png</file>
|
||||
<file>icons/circle.png</file>
|
||||
<file>icons/highlighter.png</file>
|
||||
<file>icons/infinity.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
BIN
src/icons/infinity.png
Normal file
BIN
src/icons/infinity.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 47 KiB |
@ -3,6 +3,7 @@
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <thread>
|
||||
#include <logs/requestlogging.hpp>
|
||||
|
||||
QNetworkAccessManager ioutils::networkManager;
|
||||
@ -16,6 +17,16 @@ void addLogEntry(QNetworkReply *reply, QByteArray data) {
|
||||
requestlogging::addEntry(ctx);
|
||||
}
|
||||
|
||||
int tasks = 0;
|
||||
|
||||
void addTask() {
|
||||
requestlogging::indicator::show(++tasks);
|
||||
}
|
||||
|
||||
void removeTask() {
|
||||
requestlogging::indicator::show(--tasks);
|
||||
}
|
||||
|
||||
void ioutils::postMultipart(QUrl target,
|
||||
QList<QPair<QString, QString>> headers,
|
||||
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());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
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());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
callback(data, reply);
|
||||
@ -58,7 +73,9 @@ void ioutils::getJson(QUrl target,
|
||||
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.get(req);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
callback(QJsonDocument::fromJson(data), data, reply);
|
||||
@ -75,7 +92,9 @@ void ioutils::postJson(QUrl target,
|
||||
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
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());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.get(req);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
callback(data, reply);
|
||||
@ -106,7 +127,9 @@ void ioutils::postData(QUrl target,
|
||||
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
|
||||
}
|
||||
QNetworkReply *reply = networkManager.post(req, body);
|
||||
addTask();
|
||||
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
|
||||
removeTask();
|
||||
QByteArray data = reply->readAll();
|
||||
addLogEntry(reply, data);
|
||||
callback(data, reply);
|
||||
@ -128,7 +151,6 @@ QString ioutils::methodString(QNetworkAccessManager::Operation operation) {
|
||||
case QNetworkAccessManager::HeadOperation:
|
||||
return "HEAD";
|
||||
default:
|
||||
// return "Dunno";
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include "requestlogging.hpp"
|
||||
#include <QDateTime>
|
||||
#include <mainwindow.hpp>
|
||||
#include <io/ioutils.hpp>
|
||||
#include <utils.hpp>
|
||||
|
||||
// $type $url $status $time
|
||||
// $type = GET POST PATCH DELETE etc
|
||||
@ -63,3 +65,7 @@ QList<LoggedRequest> requestlogging::getRequests() {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void requestlogging::indicator::show(int count) {
|
||||
MainWindow::inst()->setTrayIcon(utils::getTrayIcon(count));
|
||||
}
|
||||
|
@ -41,6 +41,10 @@ namespace requestlogging {
|
||||
|
||||
QList<LoggedRequest> getRequests();
|
||||
void addEntry(RequestContext context);
|
||||
|
||||
namespace indicator {
|
||||
void show(int count);
|
||||
} // namespace indicator
|
||||
} // namespace requestlogging
|
||||
|
||||
#endif // REQUESTLOGGING_HPP
|
||||
|
@ -187,3 +187,7 @@ void MainWindow::on_history_clicked() {
|
||||
HistoryDialog *dialog = new HistoryDialog;
|
||||
dialog->show();
|
||||
}
|
||||
|
||||
void MainWindow::setTrayIcon(QIcon icon) {
|
||||
tray->setIcon(icon);
|
||||
}
|
||||
|
@ -37,10 +37,11 @@ public:
|
||||
explicit MainWindow(QWidget *parent = 0);
|
||||
~MainWindow();
|
||||
bool valid();
|
||||
Ui::MainWindow *ui;
|
||||
void setTrayIcon(QIcon icon);
|
||||
RecordingController *controller = new RecordingController;
|
||||
|
||||
QSystemTrayIcon *tray;
|
||||
Ui::MainWindow *ui;
|
||||
public slots:
|
||||
void rec();
|
||||
|
||||
|
@ -65,7 +65,6 @@ SOURCES += main.cpp\
|
||||
hotkeyinputdialog.cpp \
|
||||
cropeditor/drawing/arrowitem.cpp \
|
||||
uploaders/default/imgursettingsdialog.cpp \
|
||||
uploaders/default/imgplusuploader.cpp \
|
||||
filenamevalidator.cpp \
|
||||
logs/requestlogging.cpp \
|
||||
logs/historydialog.cpp \
|
||||
@ -118,7 +117,6 @@ HEADERS += mainwindow.hpp \
|
||||
hotkeyinputdialog.hpp \
|
||||
cropeditor/drawing/arrowitem.hpp \
|
||||
uploaders/default/imgursettingsdialog.hpp \
|
||||
uploaders/default/imgplusuploader.hpp \
|
||||
filenamevalidator.hpp \
|
||||
logs/requestlogging.hpp \
|
||||
logs/historydialog.hpp \
|
||||
|
@ -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;
|
||||
}
|
@ -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
|
@ -12,7 +12,6 @@
|
||||
#include <logger.hpp>
|
||||
#include <notifications.hpp>
|
||||
#include <settings.hpp>
|
||||
#include <uploaders/default/imgplusuploader.hpp>
|
||||
|
||||
UploaderSingleton::UploaderSingleton() : QObject() {
|
||||
updateSaveSettings();
|
||||
@ -31,7 +30,6 @@ UploaderSingleton::UploaderSingleton() : QObject() {
|
||||
|
||||
// UPLOADERS
|
||||
registerUploader(new ImgurUploader);
|
||||
registerUploader(new ImgplusUploader);
|
||||
registerUploader(new ClipboardUploader);
|
||||
// ---------
|
||||
|
||||
|
@ -183,3 +183,30 @@ void utils::externalScreenshotActive(std::function<void(QPixmap)> callback) {
|
||||
});
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <QApplication>
|
||||
#include <QPixmap>
|
||||
#include <QIcon>
|
||||
#include <QScreen>
|
||||
#include <QWidget>
|
||||
#include <functional>
|
||||
@ -20,6 +21,7 @@ namespace utils {
|
||||
void externalScreenshot(std::function<void(QPixmap)> callback);
|
||||
void externalScreenshotActive(std::function<void(QPixmap)> callback);
|
||||
QString randomString(int length);
|
||||
QIcon getTrayIcon(int num);
|
||||
} // namespace utils
|
||||
|
||||
#endif // UTILS_HPP
|
||||
|
Loading…
Reference in New Issue
Block a user