a bit more work

* added open screenshot folder button
* removed history dialog
* moved history dialog to main window
* added filename to history
* removed many actions from menu bar
This commit is contained in:
Niklas 2019-05-08 21:07:06 +02:00
parent 2242f3f188
commit 8c0e961dc0
20 changed files with 250 additions and 234 deletions

View File

@ -8,11 +8,12 @@
QNetworkAccessManager ioutils::networkManager;
void addLogEntry(QNetworkReply *reply, QByteArray data) {
void addLogEntry(QNetworkReply *reply, QByteArray data, QString filename) {
requestlogging::RequestContext ctx;
ctx.reply = reply;
ctx.response = data;
ctx.filename = filename;
requestlogging::addEntry(ctx);
}
@ -30,6 +31,7 @@ void removeTask() {
void ioutils::postMultipart(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
@ -37,10 +39,10 @@ void ioutils::postMultipart(QUrl target,
}
QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(QJsonDocument::fromJson(data), data, reply);
delete reply;
});
@ -49,6 +51,7 @@ void ioutils::postMultipart(QUrl target,
void ioutils::postMultipartData(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
QString filename,
std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
@ -56,10 +59,10 @@ void ioutils::postMultipartData(QUrl target,
}
QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(data, reply);
delete reply;
});
@ -67,6 +70,7 @@ void ioutils::postMultipartData(QUrl target,
void ioutils::getJson(QUrl target,
QList<QPair<QString, QString>> headers,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
@ -74,10 +78,10 @@ void ioutils::getJson(QUrl target,
}
QNetworkReply *reply = networkManager.get(req);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(QJsonDocument::fromJson(data), data, reply);
reply->deleteLater();
});
@ -86,6 +90,7 @@ void ioutils::getJson(QUrl target,
void ioutils::postJson(QUrl target,
QList<QPair<QString, QString>> headers,
QByteArray body,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
@ -93,26 +98,26 @@ void ioutils::postJson(QUrl target,
}
QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(QJsonDocument::fromJson(data), data, reply);
delete reply;
});
}
void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, std::function<void(QByteArray, QNetworkReply *)> callback) {
void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.get(req);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(data, reply);
delete reply;
});
@ -121,6 +126,7 @@ void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, std::
void ioutils::postData(QUrl target,
QList<QPair<QString, QString>> headers,
QByteArray body,
QString filename,
std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
@ -128,10 +134,10 @@ void ioutils::postData(QUrl target,
}
QNetworkReply *reply = networkManager.post(req, body);
addTask();
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QObject::connect(reply, &QNetworkReply::finished, [reply, callback, filename] {
removeTask();
QByteArray data = reply->readAll();
addLogEntry(reply, data);
addLogEntry(reply, data, filename);
callback(data, reply);
delete reply;
});

View File

@ -11,20 +11,24 @@ namespace ioutils {
extern QNetworkAccessManager networkManager;
void getJson(QUrl target,
QList<QPair<QString, QString>> headers,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
void postJson(QUrl target,
QList<QPair<QString, QString>> headers,
QByteArray body,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
void getData(QUrl target, QList<QPair<QString, QString>> headers, std::function<void(QByteArray, QNetworkReply *)> callback);
void postData(QUrl target, QList<QPair<QString, QString>> headers, QByteArray body, std::function<void(QByteArray, QNetworkReply *)> callback);
void getData(QUrl target, QList<QPair<QString, QString>> headers, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback);
void postData(QUrl target, QList<QPair<QString, QString>> headers, QByteArray body, QString filename, std::function<void(QByteArray, QNetworkReply *)> callback);
void postMultipart(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
QString filename,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);
void postMultipartData(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
QString filename,
std::function<void(QByteArray, QNetworkReply *)> callback);
QString methodString(QNetworkAccessManager::Operation operation);
} // namespace ioutils

View File

@ -1,37 +0,0 @@
#include "historydialog.hpp"
#include "requestlogging.hpp"
#include "ui_historydialog.h"
#include <monospacetextdialog.hpp>
using requestlogging::LoggedRequest;
HistoryDialog::HistoryDialog(QWidget *parent) : QDialog(parent), ui(new Ui::HistoryDialog) {
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
ui->treeWidget->setColumnWidth(0, 50);
ui->treeWidget->setColumnWidth(1, 150);
ui->treeWidget->setColumnWidth(2, 50);
ui->treeWidget->setColumnWidth(3, 100);
QList<LoggedRequest> requests = requestlogging::getRequests();
for (LoggedRequest req : requests) {
ui->treeWidget->addTopLevelItem(
new QTreeWidgetItem({ req.getType(), req.getUrl(), QString::number(req.getResponseCode()), req.getTime() + " UTC" }));
}
}
HistoryDialog::~HistoryDialog() {
delete ui;
}
void HistoryDialog::on_treeWidget_doubleClicked(const QModelIndex &) {
QString file = ui->treeWidget->currentItem()->text(3);
file = settings::dir().absoluteFilePath("responses/" + file.left(file.length() - 4));
QFile dataFile(file);
if (!dataFile.open(QIODevice::ReadOnly)) return;
MonospaceTextDialog *dialog = new MonospaceTextDialog(file, dataFile.readAll());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}

View File

@ -1,24 +0,0 @@
#ifndef HISTORYDIALOG_H
#define HISTORYDIALOG_H
#include <QDialog>
namespace Ui {
class HistoryDialog;
}
class HistoryDialog : public QDialog {
Q_OBJECT
public:
explicit HistoryDialog(QWidget *parent = 0);
~HistoryDialog();
private slots:
void on_treeWidget_doubleClicked(const QModelIndex &);
private:
Ui::HistoryDialog *ui;
};
#endif // HISTORYDIALOG_H

View File

@ -1,88 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>HistoryDialog</class>
<widget class="QDialog" name="HistoryDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Request History</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string>Type</string>
</property>
</column>
<column>
<property name="text">
<string>URL</string>
</property>
</column>
<column>
<property name="text">
<string>Status</string>
</property>
</column>
<column>
<property name="text">
<string>Time</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>HistoryDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>HistoryDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -4,6 +4,9 @@
#include <io/ioutils.hpp>
#include <utils.hpp>
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
// $type $url $status $time
// $type = GET POST PATCH DELETE etc
// $url = request target
@ -37,6 +40,7 @@ void requestlogging::addEntry(RequestContext context) {
QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type
<< context.reply->url().toString().replace(" ", "%20") << " " // $url
<< context.filename.replace(" ", "_") << " " // $filename
<< context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << " " // $status
<< timeNow.replace(" ", "_") << endl
<< flush; // $time
@ -57,9 +61,11 @@ QList<LoggedRequest> requestlogging::getRequests() {
QTextStream stream(&line);
stream >> r.type;
stream >> r.url;
stream >> r.filename;
stream >> r.responseCode;
stream >> r.time;
r.time = r.time.replace("_", " ");
r.filename = r.filename.replace("_", " ");
ret.append(r);
}

View File

@ -3,6 +3,7 @@
#include <QByteArray>
#include <QNetworkReply>
#include <QString>
#include <settings.hpp>
@ -10,6 +11,7 @@ namespace requestlogging {
struct RequestContext {
QByteArray response;
QNetworkReply *reply;
QString filename;
};
class LoggedRequest {
@ -19,6 +21,9 @@ namespace requestlogging {
QString getUrl() {
return url;
}
QString getFilename() {
return filename;
}
QString getType() {
return type;
}
@ -34,6 +39,7 @@ namespace requestlogging {
private:
QString url;
QString filename;
QString type;
QString time;
int responseCode;

View File

@ -10,14 +10,22 @@
#include <formats.hpp>
#include <hotkeying.hpp>
#include <logger.hpp>
#include <logs/historydialog.hpp>
#include <platformbackend.hpp>
#include <recording/recordingformats.hpp>
#include <settings.hpp>
#include <uploaders/uploadersingleton.hpp>
#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QStandardPaths>
#include <QDesktopServices>
#include <logs/requestlogging.hpp>
#include <monospacetextdialog.hpp>
MainWindow *MainWindow::instance;
using requestlogging::LoggedRequest;
void MainWindow::rec() {
if (controller->isRunning()) return;
auto f = static_cast<formats::Recording>(
@ -94,7 +102,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
connect(recoff, &QAction::triggered, controller, &RecordingController::end);
connect(recabort, &QAction::triggered, controller, &RecordingController::abort);
connect(about, &QAction::triggered, this, &MainWindow::on_actionAbout_triggered);
connect(ui->settings, &QPushButton::clicked, this, &MainWindow::on_actionSettings_triggered);
connect(ui->settingsButton, &QPushButton::clicked, this, &MainWindow::on_actionSettings_triggered);
connect(ui->fullscreenButton, &QPushButton::clicked, this, [] { screenshotter::fullscreenDelayed(); });
connect(ui->areaButton, &QPushButton::clicked, this, [] { screenshotter::areaDelayed(); });
connect(ui->aboutButton, &QPushButton::clicked, this, &MainWindow::on_actionAbout_triggered);
connect(ui->screenshotFolderButton, &QPushButton::clicked, this, &MainWindow::openScreenshotFolder);
connect(ui->colorPickerButton, &QPushButton::clicked, this, [] { ColorPickerScene::showPicker(); });
tray->setContextMenu(menu);
@ -110,6 +124,12 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
for (auto err : errors) ui->logBox->addItem(QString("ERROR: ") + err.what());
setWindowTitle("KShare v" + QApplication::applicationVersion());
val = true;
QList<LoggedRequest> requests = requestlogging::getRequests();
for (LoggedRequest req : requests) {
ui->treeWidget->addTopLevelItem(
new QTreeWidgetItem({ QString::number(req.getResponseCode()), req.getFilename(), req.getUrl(), req.getTime() + " UTC" }));
}
}
MainWindow::~MainWindow() {
@ -189,11 +209,43 @@ void MainWindow::on_actionAbort_triggered() {
controller->abort();
}
void MainWindow::on_history_clicked() {
HistoryDialog *dialog = new HistoryDialog;
void MainWindow::on_treeWidget_doubleClicked(const QModelIndex &) {
QString file = ui->treeWidget->currentItem()->text(3);
file = settings::dir().absoluteFilePath("responses/" + file.left(file.length() - 4));
QFile dataFile(file);
if (!dataFile.open(QIODevice::ReadOnly)) return;
MonospaceTextDialog *dialog = new MonospaceTextDialog(file, dataFile.readAll());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}
void MainWindow::openScreenshotFolder() {
QDir saveDir;
switch (settings::settings().value("saveLocation", 1).toInt()) {
case 0:
saveDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (QStandardPaths::writableLocation(QStandardPaths::PicturesLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location for pictures").toLocal8Bit().constData());
}
break;
case 1:
if (QStandardPaths::writableLocation(QStandardPaths::HomeLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location of your home directory").toLocal8Bit().constData());
}
saveDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/Screenshots";
break;
default:
qFatal("%s", tr("Invalid config [saveLocation not int or is not in range]").toLocal8Bit().constData());
return;
case 2:
// Do not Save images
return;
}
QDesktopServices::openUrl(QUrl::fromLocalFile(saveDir.absolutePath()));
}
void MainWindow::setTrayIcon(QIcon icon) {
tray->setIcon(icon);
}

View File

@ -30,7 +30,8 @@ private slots:
void on_actionAbout_triggered();
void on_actionActive_window_triggered();
void on_actionAbort_triggered();
void on_history_clicked();
void openScreenshotFolder();
void on_treeWidget_doubleClicked(const QModelIndex &);
public:
static MainWindow *inst();

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>513</width>
<height>328</height>
<width>760</width>
<height>363</height>
</rect>
</property>
<property name="windowTitle">
@ -25,30 +25,131 @@
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="settings">
<item row="0" column="3" rowspan="3">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="2" rowspan="2">
<layout class="QGridLayout" name="gridLayout_3">
<item row="2" column="0">
<widget class="QPushButton" name="clipboardButton">
<property name="text">
<string>Copy Clipboard</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="settingsButton">
<property name="text">
<string>Settings</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Log</string>
<item row="1" column="0">
<widget class="QPushButton" name="areaButton">
<property name="text">
<string>Area</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="colorPickerButton">
<property name="text">
<string>Open color picker</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QPushButton" name="aboutButton">
<property name="text">
<string>About</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QListWidget" name="logBox"/>
<widget class="QPushButton" name="fullscreenButton">
<property name="text">
<string>Fullscreen</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="screenshotFolderButton">
<property name="text">
<string>Open Screenshot Folder</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="4">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_1">
<attribute name="title">
<string>Request History</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QTreeWidget" name="treeWidget">
<property name="rootIsDecorated">
<bool>true</bool>
</property>
<property name="uniformRowHeights">
<bool>false</bool>
</property>
<property name="expandsOnDoubleClick">
<bool>false</bool>
</property>
<column>
<property name="text">
<string>Status</string>
</property>
</column>
<column>
<property name="text">
<string>Filename</string>
</property>
</column>
<column>
<property name="text">
<string>URL</string>
</property>
</column>
<column>
<property name="text">
<string>Time</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="history">
<property name="text">
<string>Open request history</string>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Log</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QListWidget" name="logBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
@ -59,7 +160,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>513</width>
<width>760</width>
<height>23</height>
</rect>
</property>
@ -72,20 +173,6 @@
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<widget class="QMenu" name="menuScreenshot">
<property name="title">
<string>Scree&amp;nshot</string>
</property>
<addaction name="actionFullscreen"/>
<addaction name="actionArea"/>
<addaction name="actionActive_window"/>
</widget>
<widget class="QMenu" name="menuUtilities">
<property name="title">
<string>&amp;Utilities</string>
</property>
<addaction name="actionColor_Picker"/>
</widget>
<widget class="QMenu" name="menuRecording">
<property name="title">
<string>&amp;Recording</string>
@ -95,8 +182,6 @@
<addaction name="actionAbort"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuScreenshot"/>
<addaction name="menuUtilities"/>
<addaction name="menuRecording"/>
</widget>
<action name="actionQuit">
@ -156,6 +241,7 @@
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="icon.qrc"/>
<include location="sounds.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -67,7 +67,6 @@ SOURCES += main.cpp\
uploaders/default/imgursettingsdialog.cpp \
filenamevalidator.cpp \
logs/requestlogging.cpp \
logs/historydialog.cpp \
monospacetextdialog.cpp \
cropeditor/selectionrectangle.cpp \
screenoverlay/screenoverlayview.cpp \
@ -119,7 +118,6 @@ HEADERS += mainwindow.hpp \
uploaders/default/imgursettingsdialog.hpp \
filenamevalidator.hpp \
logs/requestlogging.hpp \
logs/historydialog.hpp \
monospacetextdialog.hpp \
cropeditor/selectionrectangle.hpp \
screenoverlay/screenoverlayview.hpp \
@ -185,7 +183,6 @@ FORMS += mainwindow.ui \
aboutbox.ui \
hotkeyinputdialog.ui \
uploaders/default/imgursettingsdialog.ui \
logs/historydialog.ui \
monospacetextdialog.ui \
screenoverlay/screenoverlaysettings.ui

View File

@ -266,7 +266,7 @@ QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString forma
return o;
}
void CustomUploader::doUpload(QByteArray imgData, QString format) {
void CustomUploader::doUpload(QByteArray imgData, QString format, QString filename) {
auto h = getHeaders(headers, format, this->rFormat);
QByteArray data;
if (base64) imgData = imgData.toBase64(QByteArray::Base64UrlEncoding);
@ -337,7 +337,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
switch (method) {
case HttpMethod::POST:
if (returnPathspec == "|") {
ioutils::postMultipartData(target, h, multipart,
ioutils::postMultipartData(target, h, multipart, filename,
[&, buffersToDelete, arraysToDelete](QByteArray result, QNetworkReply *) {
QApplication::clipboard()->setText(QString::fromUtf8(result));
for (auto buffer : buffersToDelete) buffer->deleteLater();
@ -347,7 +347,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
tr("Copied upload result to clipboard!"));
});
} else {
ioutils::postMultipart(target, h, multipart,
ioutils::postMultipart(target, h, multipart, filename,
[&, buffersToDelete, arraysToDelete](QJsonDocument result, QByteArray data, QNetworkReply *) {
for (auto buffer : buffersToDelete) buffer->deleteLater();
for (auto arr : arraysToDelete) delete arr;
@ -367,13 +367,13 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
switch (method) {
case HttpMethod::POST:
if (returnPathspec == "|") {
ioutils::postData(target, h, data, [&](QByteArray result, QNetworkReply *) {
ioutils::postData(target, h, data, filename, [&](QByteArray result, QNetworkReply *) {
QApplication::clipboard()->setText(QString::fromUtf8(result));
playSuccessSound();
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
});
} else {
ioutils::postJson(target, h, data, [&](QJsonDocument result, QByteArray data, QNetworkReply *) {
ioutils::postJson(target, h, data, filename, [&](QJsonDocument result, QByteArray data, QNetworkReply *) {
parseResult(result, data, returnPathspec, name());
});
}

View File

@ -18,7 +18,7 @@ public:
CustomUploader(QString absFilePath);
QString name();
QString description();
void doUpload(QByteArray imgData, QString format);
void doUpload(QByteArray imgData, QString format, QString filename);
private:
double limit = -1;

View File

@ -5,8 +5,9 @@
#include <QMimeData>
#include <formats.hpp>
#include <notifications.hpp>
#include <QString>
void ClipboardUploader::doUpload(QByteArray imgData, QString format) {
void ClipboardUploader::doUpload(QByteArray imgData, QString format, QString filename) {
auto f = formats::recordingFormatFromName(format);
if (f != formats::Recording::None) {
auto data = new QMimeData();

View File

@ -15,7 +15,7 @@ public:
return "Copies the image to clipboard";
}
void doUpload(QByteArray imgData, QString format);
void doUpload(QByteArray imgData, QString format, QString filename);
};
#endif // CLIPBOARDUPLOADER_HPP

View File

@ -45,6 +45,7 @@ void ImgurSettingsDialog::on_authorize_clicked() {
ioutils::postJson(QUrl("https://api.imgur.com/oauth2/token"),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
QJsonDocument::fromVariant(object.toVariantMap()).toJson(),
"",
[&](QJsonDocument response, QByteArray, QNetworkReply *r) {
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
ui->buttonBox->setEnabled(true);

View File

@ -5,6 +5,7 @@
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkReply>
#include <QString>
#include <formats.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
@ -25,15 +26,15 @@ struct SegfaultWorkaround { // I'm a scrub for doing this
ioutils::postJson(
QUrl("https://api.imgur.com/oauth2/token"),
QList<QPair<QString, QString>>({ QPair<QString, QString>("Content-Type", "applicaton/json") }),
QJsonDocument::fromVariant(object.toVariantMap()).toJson(), [&](QJsonDocument response, QByteArray, QNetworkReply *r) {
QJsonDocument::fromVariant(object.toVariantMap()).toJson(), "", [&](QJsonDocument response, QByteArray, QNetworkReply *r) {
qDebug() << response;
if (r->error() != QNetworkReply::NoError || !response.isObject()) {
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray, "");
return;
}
QJsonObject res = response.object();
if (res.value("success").toBool()) {
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
dis->handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray, "");
return;
}
@ -42,7 +43,7 @@ struct SegfaultWorkaround { // I'm a scrub for doing this
settings::settings().setValue("imgur/refresh", res["refresh_token"].toString());
settings::settings().setValue("imgur/access", token);
dis->handleSend(token.prepend("Bearer "), mime, byteArray);
dis->handleSend(token.prepend("Bearer "), mime, byteArray, "");
QScopedPointer<SegfaultWorkaround>(this);
});
}
@ -53,7 +54,7 @@ private:
QString mime;
}; // I feel terrible for making this. I am sorry, reader
void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
void ImgurUploader::doUpload(QByteArray byteArray, QString format, QString filename) {
if (byteArray.size() > 1e+7) {
notifications::notify(tr("KShare imgur Uploader"), tr("Failed upload! Image too big"));
return;
@ -70,20 +71,20 @@ void ImgurUploader::doUpload(QByteArray byteArray, QString format) {
if (QDateTime::currentDateTimeUtc() > expireTime) {
new SegfaultWorkaround(byteArray, this, mime);
} else
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray);
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray, filename);
} else
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray);
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray, filename);
}
void ImgurUploader::showSettings() {
(new ImgurSettingsDialog())->show();
}
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray, QString filename) {
ioutils::postJson(QUrl("https://api.imgur.com/3/image"),
QList<QPair<QString, QString>>() << QPair<QString, QString>("Content-Type", mime.toUtf8())
<< QPair<QString, QString>("Authorization", auth),
byteArray, [byteArray, this, mime](QJsonDocument res, QByteArray, QNetworkReply *r) {
byteArray, filename, [byteArray, this, mime](QJsonDocument res, QByteArray, QNetworkReply *r) {
QString result = res.object()["data"].toObject()["link"].toString();
if (r->error() == QNetworkReply::ContentAccessDenied) {
new SegfaultWorkaround(byteArray, this, mime);

View File

@ -15,11 +15,11 @@ public:
QString description() override {
return "imgur.com uploader";
}
void doUpload(QByteArray byteArray, QString) override;
void doUpload(QByteArray byteArray, QString, QString filename) override;
void showSettings() override;
private:
void handleSend(QString auth, QString mime, QByteArray byteArray);
void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename);
void playSuccessSound();
void playErrorSound();
};

View File

@ -6,7 +6,7 @@
class Uploader {
public:
virtual void doUpload(QByteArray imgData, QString format) = 0;
virtual void doUpload(QByteArray imgData, QString format, QString filename) = 0;
virtual QString name() = 0;
virtual QString description() = 0;
virtual void showSettings() {

View File

@ -5,6 +5,7 @@
#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QTemporaryFile>
#include <formats.hpp>
@ -72,7 +73,8 @@ void UploaderSingleton::upload(QPixmap pixmap) {
playSound();
pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
file->seek(0);
u->doUpload(file->readAll(), format);
QFileInfo fileInfo(file->fileName());
u->doUpload(file->readAll(), format, fileInfo.fileName());
} else
notifications::notify(tr("KShare - Failed to save picture"), file->errorString(), QSystemTrayIcon::Warning);
delete file;
@ -94,8 +96,9 @@ void UploaderSingleton::upload(QByteArray img, QString format) {
file->write(img);
file->close();
}
QFileInfo fileInfo(file->fileName());
delete file;
uploaders.value(uploader)->doUpload(img, format);
uploaders.value(uploader)->doUpload(img, format, fileInfo.fileName());
}
void UploaderSingleton::upload(QFile &img, QString format) {
@ -105,8 +108,9 @@ void UploaderSingleton::upload(QFile &img, QString format) {
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) {
playSound();
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), format);
uploaders.value(uploader)->doUpload(img.readAll(), format, fileInfo.fileName());
else
notifications::notify(tr("KShare - Failed to save picture"), img.errorString(), QSystemTrayIcon::Warning);
} else