Browse Source

Add uploading indicator. Fix #73

tags/v5.0.0^2
ArsenArsen 4 years ago
parent
commit
81cfe1584e
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
  1. 1
      src/icon.qrc
  2. BIN
      src/icons/infinity.png
  3. 24
      src/io/ioutils.cpp
  4. 6
      src/logs/requestlogging.cpp
  5. 4
      src/logs/requestlogging.hpp
  6. 4
      src/mainwindow.cpp
  7. 3
      src/mainwindow.hpp
  8. 2
      src/src.pro
  9. 53
      src/uploaders/default/imgplusuploader.cpp
  10. 21
      src/uploaders/default/imgplusuploader.hpp
  11. 2
      src/uploaders/uploadersingleton.cpp
  12. 27
      src/utils.cpp
  13. 2
      src/utils.hpp

1
src/icon.qrc

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

24
src/io/ioutils.cpp

@ -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";
}
}

6
src/logs/requestlogging.cpp

@ -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));
}

4
src/logs/requestlogging.hpp

@ -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

4
src/mainwindow.cpp

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

3
src/mainwindow.hpp

@ -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();

2
src/src.pro

@ -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 \

53
src/uploaders/default/imgplusuploader.cpp

@ -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;
}

21
src/uploaders/default/imgplusuploader.hpp

@ -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

2
src/uploaders/uploadersingleton.cpp

@ -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);
// ---------

27
src/utils.cpp

@ -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();
}
}

2
src/utils.hpp

@ -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…
Cancel
Save