Notifications

This commit is contained in:
ArsenArsen 2017-04-25 22:17:36 +02:00
parent 895960c1b0
commit 9a90814891
9 changed files with 68 additions and 14 deletions

View File

@ -36,7 +36,8 @@ SOURCES += main.cpp\
settings.cpp \ settings.cpp \
uploaders/default/clipboarduploader.cpp \ uploaders/default/clipboarduploader.cpp \
formatter.cpp \ formatter.cpp \
uploaders/customuploader.cpp uploaders/customuploader.cpp \
notifications.cpp
HEADERS += mainwindow.hpp \ HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \ cropeditor/cropeditor.hpp \
@ -51,7 +52,8 @@ HEADERS += mainwindow.hpp \
settings.hpp \ settings.hpp \
uploaders/default/clipboarduploader.hpp \ uploaders/default/clipboarduploader.hpp \
formatter.hpp \ formatter.hpp \
uploaders/customuploader.hpp uploaders/customuploader.hpp \
notifications.hpp
FORMS += mainwindow.ui FORMS += mainwindow.ui

View File

@ -13,8 +13,11 @@
#include <settings.hpp> #include <settings.hpp>
#include <uploaders/uploadersingleton.hpp> #include <uploaders/uploadersingleton.hpp>
MainWindow *MainWindow::instance;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ {
instance = this;
ui->setupUi(this); ui->setupUi(this);
setWindowIcon(QIcon(":/icons/icon.jpg")); setWindowIcon(QIcon(":/icons/icon.jpg"));
tray = new QSystemTrayIcon(windowIcon(), this); tray = new QSystemTrayIcon(windowIcon(), this);
@ -51,7 +54,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
if (errors.length() == 1) if (errors.length() == 1)
statusBar()->showMessage(errors.at(0).what()); statusBar()->showMessage(errors.at(0).what());
else else
statusBar()->showMessage(QString("Errors visible in console. Count: " + errors.size())); statusBar()->showMessage(QString("Errors visible in console (if present). Count: " + QString::number(errors.size())));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -64,6 +67,11 @@ void MainWindow::setScheme(QString scheme)
ui->nameScheme->setText(scheme); ui->nameScheme->setText(scheme);
} }
MainWindow *MainWindow::inst()
{
return instance;
}
void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::closeEvent(QCloseEvent *event)
{ {
event->ignore(); event->ignore();

View File

@ -29,11 +29,14 @@ class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
QSystemTrayIcon *tray;
void setScheme(QString scheme); void setScheme(QString scheme);
static MainWindow *inst();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QSystemTrayIcon *tray; static MainWindow *instance;
protected: protected:
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>312</width> <width>470</width>
<height>317</height> <height>315</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -59,7 +59,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>312</width> <width>470</width>
<height>24</height> <height>24</height>
</rect> </rect>
</property> </property>

10
notifications.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "notifications.hpp"
#include "mainwindow.hpp"
#include <QStatusBar>
void notifications::notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon)
{
MainWindow::inst()->tray->showMessage(title, body, icon, 5000);
MainWindow::inst()->statusBar()->showMessage(title + ": " + body);
}

12
notifications.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef NOTIFICATIONS_HPP
#define NOTIFICATIONS_HPP
#include <QString>
#include <QSystemTrayIcon>
namespace notifications
{
void notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
}
#endif // NOTIFICATIONS_HPP

View File

@ -8,6 +8,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QNetworkReply> #include <QNetworkReply>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <notifications.hpp>
using std::runtime_error; using std::runtime_error;
@ -257,6 +258,21 @@ QString parsePathspec(QJsonDocument &response, QString &pathspec)
return ""; return "";
} }
void parseResult(QJsonDocument result, QString returnPathspec, QString name)
{
if (result.isObject())
{
QString url = parsePathspec(result, returnPathspec);
if (!url.isEmpty())
{
QApplication::clipboard()->setText(url);
notifications::notify("KShare Custom Uploader " + name, "Copied upload link to clipboard!");
}
else
notifications::notify("KShare Custom Uploader " + name, "Upload done, but result empty!");
}
}
void CustomUploader::doUpload(QPixmap *pixmap) void CustomUploader::doUpload(QPixmap *pixmap)
{ {
auto h = getHeaders(headers, getFormatString(false), types, this->format); auto h = getHeaders(headers, getFormatString(false), types, this->format);
@ -329,16 +345,13 @@ void CustomUploader::doUpload(QPixmap *pixmap)
{ {
ioutils::postData(target, h, data, [&](QByteArray result, QNetworkReply *) { ioutils::postData(target, h, data, [&](QByteArray result, QNetworkReply *) {
QApplication::clipboard()->setText(QString::fromUtf8(result)); QApplication::clipboard()->setText(QString::fromUtf8(result));
notifications::notify("KShare Custom Uploader " + name(), "Copied upload result to clipboard!");
}); });
} }
else else
{ {
ioutils::postJson(target, h, data, [&](QJsonDocument result, QNetworkReply *) { ioutils::postJson(target, h, data,
if (result.isObject()) [&](QJsonDocument result, QNetworkReply *) { parseResult(result, returnPathspec, name()); });
{
QApplication::clipboard()->setText(parsePathspec(result, returnPathspec));
}
});
} }
break; break;
} }

View File

@ -2,8 +2,10 @@
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <notifications.hpp>
void ClipboardUploader::doUpload(QPixmap *pixmap) void ClipboardUploader::doUpload(QPixmap *pixmap)
{ {
QApplication::clipboard()->setImage(pixmap->toImage()); QApplication::clipboard()->setImage(pixmap->toImage());
notifications::notify("KShare", "Copied to clipboard!");
} }

View File

@ -4,6 +4,7 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <notifications.hpp>
#include <screenshotutil.hpp> #include <screenshotutil.hpp>
void ImgurUploader::doUpload(QPixmap *pixmap) void ImgurUploader::doUpload(QPixmap *pixmap)
@ -16,6 +17,9 @@ void ImgurUploader::doUpload(QPixmap *pixmap)
<< QPair<QString, QString>("Content-Type", "application/x-www-form-urlencoded") << QPair<QString, QString>("Content-Type", "application/x-www-form-urlencoded")
<< QPair<QString, QString>("Authorization", "Client-ID 8a98f183fc895da"), << QPair<QString, QString>("Authorization", "Client-ID 8a98f183fc895da"),
byteArray, [](QJsonDocument res, QNetworkReply *) { byteArray, [](QJsonDocument res, QNetworkReply *) {
screenshotutil::toClipboard(res.object()["data"].toObject()["link"].toString()); QString result = res.object()["data"].toObject()["link"].toString();
screenshotutil::toClipboard(result);
notifications::notify("KShare imgur Uploader ",
result.isEmpty() ? "Failed upload!" : "Upload done, but result empty!");
}); });
} }