save filename with subdir in history

This commit is contained in:
Niklas 2019-05-16 00:29:05 +02:00
parent 7347c9bf42
commit 963b22d0dc
18 changed files with 130 additions and 74 deletions

View File

@ -31,7 +31,7 @@ Additionally, on Linux, you require:
* XCB
* XCB xfixes
* XCB cursor
* libnotify
* Notifications Daemon with org.freedesktop.notifications DBus support (like dunst)
Despite the name implying so, this project does not depend on the KDE API at all.

View File

@ -5,16 +5,17 @@
#include <QNetworkRequest>
#include <thread>
#include <logs/requestlogging.hpp>
#include <logs/screenshotfile.h>
QNetworkAccessManager ioutils::networkManager;
void ioutils::addLogEntry(QNetworkReply* reply, QByteArray data, QString result, QString filename) {
void ioutils::addLogEntry(QNetworkReply* reply, QByteArray data, QString result, ScreenshotFile sf) {
requestlogging::RequestContext ctx;
ctx.reply = reply;
ctx.response = data;
ctx.result = result;
ctx.filename = filename;
ctx.screenshotFile = sf;
requestlogging::addEntry(ctx);
}

View File

@ -6,10 +6,11 @@
#include <QNetworkAccessManager>
#include <QUrl>
#include <functional>
#include <logs/screenshotfile.h>
namespace ioutils {
extern QNetworkAccessManager networkManager;
void addLogEntry(QNetworkReply* reply, QByteArray data, QString result, QString filename);
void addLogEntry(QNetworkReply* reply, QByteArray data, QString result, ScreenshotFile sf);
void getJson(QUrl target,
QList<QPair<QString, QString>> headers,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback);

View File

@ -2,6 +2,7 @@
#include <QDateTime>
#include <mainwindow.hpp>
#include <io/ioutils.hpp>
#include <logs/screenshotfile.h>
#include <utils.hpp>
#include "mainwindow.hpp"
@ -37,10 +38,13 @@ void requestlogging::addEntry(RequestContext context) {
responseFile.write("\n\n" + context.response);
responseFile.close();
ScreenshotFile sf = context.screenshotFile;
QTextStream(&requestFile) << ioutils::methodString(context.reply->operation()) << " " // $type
<< context.reply->url().toString().replace(" ", "%20") << " " // $url
<< context.result.replace(" ", "%20") << " " // $result
<< context.filename.replace(" ", "_") << " " // $filename
<< sf.getSubfolder().replace(" ", "_") << " " // $subfolder
<< sf.getFilename().replace(" ", "_") << " " // $filename
<< context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << " " // $status
<< timeNow.replace(" ", "_") << endl
<< flush; // $time
@ -48,7 +52,7 @@ void requestlogging::addEntry(RequestContext context) {
MainWindow::inst()->addResponse(
context.reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
context.filename,
sf,
context.result,
context.reply->url().toString(),
timeNow.replace("_", " "));
@ -66,14 +70,18 @@ QList<LoggedRequest> requestlogging::getRequests() {
while ((line = requestFile.readLine()).size() != 0) {
LoggedRequest r;
QTextStream stream(&line);
ScreenshotFile sf;
stream >> r.type;
stream >> r.url;
stream >> r.result;
stream >> r.filename;
stream >> sf.subfolder;
stream >> sf.filename;
stream >> r.responseCode;
stream >> r.time;
r.time = r.time.replace("_", " ");
r.filename = r.filename.replace("_", " ");
sf.subfolder = sf.subfolder.replace("_", " ");
sf.filename = sf.filename.replace("_", " ");
r.screenshotFile = sf;
ret.append(r);
}

View File

@ -5,13 +5,13 @@
#include <QNetworkReply>
#include <QString>
#include <settings.hpp>
#include <logs/screenshotfile.h>
namespace requestlogging {
struct RequestContext {
QByteArray response;
QNetworkReply *reply;
QString filename;
ScreenshotFile screenshotFile;
QString result;
};
@ -22,8 +22,8 @@ namespace requestlogging {
QString getUrl() {
return url;
}
QString getFilename() {
return filename;
ScreenshotFile getScreenshotFile() {
return screenshotFile;
}
QString getType() {
return type;
@ -43,7 +43,7 @@ namespace requestlogging {
private:
QString url;
QString filename;
ScreenshotFile screenshotFile;
QString result;
QString type;
QString time;

26
src/logs/screenshotfile.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef SCREENSHOTFILE_H
#define SCREENSHOTFILE_H
#include <QByteArray>
#include <QNetworkReply>
#include <QString>
#include <settings.hpp>
#include <QFile>
class ScreenshotFile {
public:
QString getSubfolder() {
return subfolder;
}
QString getFilename() {
return filename;
}
QString subfolder;
QString filename;
};
#endif // SCREENSHOTFILE_H

View File

@ -26,6 +26,7 @@
#include "io/ioutils.hpp"
#include <monospacetextdialog.hpp>
#include <clipboard/clipboardcopy.hpp>
#include <logs/screenshotfile.h>
MainWindow *MainWindow::instance;
@ -143,7 +144,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
QList<LoggedRequest> requests = requestlogging::getRequests();
for (LoggedRequest req : requests) {
addResponse(req.getResponseCode(), req.getFilename(), req.getResult(), req.getUrl(), req.getTime());
addResponse(req.getResponseCode(), req.getScreenshotFile(), req.getResult(), req.getUrl(), req.getTime());
}
}
@ -293,9 +294,9 @@ void MainWindow::setTrayIcon(QIcon icon) {
tray->setIcon(icon);
}
void MainWindow::addResponse(int httpCode, QString filename, QString result, QString url, QString time) {
void MainWindow::addResponse(int httpCode, ScreenshotFile sf, QString result, QString url, QString time) {
QString httpStatus = ioutils::httpString(httpCode);
QTreeWidgetItem* tw = new QTreeWidgetItem({ QString::number(httpCode) + " " + httpStatus, filename, result, url, time + " UTC" });
QTreeWidgetItem* tw = new QTreeWidgetItem({ QString::number(httpCode) + " " + httpStatus, sf.getSubfolder() + QDir::separator() + sf.getFilename(), result, url, time + " UTC" });
if(httpCode >= 200 && httpCode < 300) {
tw->setIcon(0, *(new QIcon(":/icons/checked.png")));

View File

@ -9,6 +9,7 @@
#include <recording/recordingcontroller.hpp>
#include <uploaders/uploader.hpp>
#include <logs/screenshotfile.h>
namespace Ui {
class MainWindow;
@ -39,7 +40,7 @@ public:
~MainWindow();
bool valid();
void setTrayIcon(QIcon icon);
void addResponse(int httpCode, QString filename, QString result, QString url, QString time);
void addResponse(int httpCode, ScreenshotFile sf, QString result, QString url, QString time);
RecordingController *controller = new RecordingController;
QSystemTrayIcon *tray;

View File

@ -127,7 +127,8 @@ HEADERS += mainwindow.hpp \
screenoverlay/screenoverlaysettings.hpp \
logger.hpp \
clipboard/clipboardcopy.hpp \
systemnotification.h
systemnotification.h \
logs/screenshotfile.h
nopkg {
# win32 {

View File

@ -12,6 +12,7 @@
#include <formatter.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <logs/screenshotfile.h>
using formats::normalFormatFromName;
using formats::normalFormatMIME;
@ -208,7 +209,7 @@ QString parsePathspec(QJsonDocument &response, QString &pathspec) {
return "";
}
void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename) {
void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, ScreenshotFile sf) {
if (result.isObject()) {
QString url
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
@ -216,22 +217,22 @@ void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteAr
if (!url.isEmpty()) {
QApplication::clipboard()->setText(url);
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!"));
ioutils::addLogEntry(r, data, url, filename);
ioutils::addLogEntry(r, data, url, sf);
} else {
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Upload done, but result empty!"));
QApplication::clipboard()->setText(data);
ioutils::addLogEntry(r, data, "", filename);
ioutils::addLogEntry(r, data, "", sf);
}
} else {
notifications::playSound(notifications::Sound::ERROR);
notifications::notify(tr("KShare Custom Uploader ") + name,
tr("Upload done, but result is not JSON Object! Result in clipboard."));
QApplication::clipboard()->setText(data);
ioutils::addLogEntry(r, data, "", filename);
ioutils::addLogEntry(r, data, "", sf);
}
}
QByteArray substituteArgs(QByteArray arr, QString format, QString filename, QByteArray imgData = QByteArray()) {
QByteArray substituteArgs(QByteArray arr, QString format, ScreenshotFile sf, QByteArray imgData = QByteArray()) {
QString mime = normalFormatMIME(normalFormatFromName(format));
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
if (arr.startsWith("/") && arr.endsWith("/")) {
@ -241,7 +242,7 @@ QByteArray substituteArgs(QByteArray arr, QString format, QString filename, QByt
{ { "format", format.toLower() }, { "FORMAT", format }, { "contenttype", mime } })
.toUtf8();
QByteArray fA = filename.toLocal8Bit();
QByteArray fA = sf.getFilename().toLocal8Bit();
arr.replace("%filename", fA.data());
if (imgData.isNull()) return arr;
@ -251,17 +252,17 @@ QByteArray substituteArgs(QByteArray arr, QString format, QString filename, QByt
}
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString format, QString filename) {
QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString format, ScreenshotFile sf) {
QJsonObject o;
for (QString s : body.keys()) {
QJsonValue v = body[s];
if (v.isObject()) {
QJsonObject vo = v.toObject();
o.insert(s, recurseAndReplace(vo, data, format, filename));
o.insert(s, recurseAndReplace(vo, data, format, sf));
} else if (v.isString()) {
QString str = v.toString();
if (str.startsWith("/") && str.endsWith("/")) {
o.insert(s, QString::fromUtf8(substituteArgs(str.toUtf8(), format, filename, data)));
o.insert(s, QString::fromUtf8(substituteArgs(str.toUtf8(), format, sf, data)));
} else
o.insert(s, v);
} else
@ -270,7 +271,7 @@ QJsonObject recurseAndReplace(QJsonObject &body, QByteArray &data, QString forma
return o;
}
void CustomUploader::doUpload(QByteArray imgData, QString format, QString filename) {
void CustomUploader::doUpload(QByteArray imgData, QString format, ScreenshotFile sf) {
auto h = getHeaders(headers, format, this->rFormat);
QByteArray data;
if (base64) imgData = imgData.toBase64(QByteArray::Base64UrlEncoding);
@ -281,10 +282,10 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
} break;
case RequestFormat::JSON: {
if (body.isString()) {
data = substituteArgs(body.toString().toUtf8(), format, filename, imgData);
data = substituteArgs(body.toString().toUtf8(), format, sf, imgData);
} else {
QJsonObject vo = body.toObject();
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format, filename).toVariantMap()).toJson();
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, format, sf).toVariantMap()).toJson();
}
} break;
case RequestFormat::X_WWW_FORM_URLENCODED: {
@ -292,7 +293,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
for (QString key : body.keys()) {
QJsonValue val = body[key];
if (val.isString()) {
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, filename, imgData));
data.append(QUrl::toPercentEncoding(key)).append('=').append(substituteArgs(val.toString().toUtf8(), format, sf, imgData));
} else {
if (!data.isEmpty()) data.append('&');
data.append(QUrl::toPercentEncoding(key))
@ -311,7 +312,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
QHttpPart part;
QJsonValue bd = valo["body"];
if (bd.isString()) {
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, filename, imgData);
QByteArray body = substituteArgs(bd.toString().toUtf8(), format, sf, imgData);
QByteArray *bodyHeap = new QByteArray;
body.swap(*bodyHeap);
QBuffer *buffer = new QBuffer(bodyHeap);
@ -321,7 +322,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
arraysToDelete.append(bodyHeap);
} else {
auto bdo = bd.toObject();
QJsonObject result = recurseAndReplace(bdo, imgData, format, filename);
QJsonObject result = recurseAndReplace(bdo, imgData, format, sf);
part.setBody(QJsonDocument::fromVariant(result.toVariantMap()).toJson());
}
QByteArray cdh("form-data");
@ -329,11 +330,11 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
if (headerVal.startsWith("__")) {
headerVal = headerVal.mid(2);
QByteArray str = valo["__" + headerVal].toString().toUtf8();
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format, filename);
if (str.startsWith("/") && str.endsWith("/")) str = substituteArgs(str, format, sf);
part.setRawHeader(headerVal.toLatin1(), str);
} else if (headerVal != "body")
cdh += "; " + headerVal + "=\""
+ substituteArgs(valo[headerVal].toString().toUtf8(), format, filename).replace("\"", "\\\"") + "\"";
+ substituteArgs(valo[headerVal].toString().toUtf8(), format, sf).replace("\"", "\\\"") + "\"";
}
part.setHeader(QNetworkRequest::ContentDispositionHeader, cdh);
multipart->append(part);
@ -342,8 +343,8 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
case HttpMethod::POST:
if (returnPathspec == "|") {
ioutils::postMultipartData(target, h, multipart,
[&, buffersToDelete, arraysToDelete, filename](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
[&, buffersToDelete, arraysToDelete, sf](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), sf);
QApplication::clipboard()->setText(QString::fromUtf8(result));
for (auto buffer : buffersToDelete) buffer->deleteLater();
for (auto arr : arraysToDelete) delete arr;
@ -353,10 +354,10 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
});
} else {
ioutils::postMultipart(target, h, multipart,
[&, buffersToDelete, arraysToDelete, filename](QJsonDocument result, QByteArray data, QNetworkReply *r) {
[&, buffersToDelete, arraysToDelete, sf](QJsonDocument result, QByteArray data, QNetworkReply *r) {
for (auto buffer : buffersToDelete) buffer->deleteLater();
for (auto arr : arraysToDelete) delete arr;
parseResult(r, result, data, returnPathspec, name(), filename);
parseResult(r, result, data, returnPathspec, name(), sf);
});
}
break;
@ -372,15 +373,15 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
switch (method) {
case HttpMethod::POST:
if (returnPathspec == "|") {
ioutils::postData(target, h, data, [&, filename](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
ioutils::postData(target, h, data, [&, sf](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), sf);
QApplication::clipboard()->setText(QString::fromUtf8(result));
notifications::playSound(notifications::Sound::SUCCESS);
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
});
} else {
ioutils::postJson(target, h, data, [&, filename](QJsonDocument result, QByteArray data, QNetworkReply *r) {
parseResult(r, result, data, returnPathspec, name(), filename);
ioutils::postJson(target, h, data, [&, sf](QJsonDocument result, QByteArray data, QNetworkReply *r) {
parseResult(r, result, data, returnPathspec, name(), sf);
});
}
break;

View File

@ -7,6 +7,7 @@
#include <QMap>
#include <QUrl>
#include <QNetworkReply>
#include <logs/screenshotfile.h>
enum class HttpMethod { POST };
@ -19,7 +20,7 @@ public:
CustomUploader(QString absFilePath);
QString name();
QString description();
void doUpload(QByteArray imgData, QString format, QString filename);
void doUpload(QByteArray imgData, QString format, ScreenshotFile sf);
private:
double limit = -1;
@ -33,7 +34,7 @@ private:
bool base64 = false;
QString returnPathspec;
QString urlPrepend, urlAppend;
void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename);
void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, ScreenshotFile sf);
};
#endif // CUSTOMUPLOADER_HPP

View File

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

View File

@ -4,6 +4,7 @@
#include <QApplication>
#include <QPixmap>
#include <uploaders/uploader.hpp>
#include <logs/screenshotfile.h>
class ClipboardUploader : public Uploader {
Q_DECLARE_TR_FUNCTIONS(ClipboardUploader)
@ -15,7 +16,7 @@ public:
return "Copies the image to clipboard";
}
void doUpload(QByteArray imgData, QString format, QString filename);
void doUpload(QByteArray imgData, QString format, ScreenshotFile sf);
};
#endif // CLIPBOARDUPLOADER_HPP

View File

@ -11,6 +11,7 @@
#include <notifications.hpp>
#include <settings.hpp>
#include <utils.hpp>
#include <logs/screenshotfile.h>
struct SegfaultWorkaround { // I'm a scrub for doing this
SegfaultWorkaround(QByteArray a, ImgurUploader *u, QString m) : byteArray(), dis(u), mime(m) {
@ -25,14 +26,15 @@ struct SegfaultWorkaround { // I'm a scrub for doing this
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) {
ScreenshotFile sf;
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, sf);
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, sf);
return;
}
@ -41,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, sf);
QScopedPointer<SegfaultWorkaround>(this);
});
}
@ -52,7 +54,7 @@ private:
QString mime;
}; // I feel terrible for making this. I am sorry, reader
void ImgurUploader::doUpload(QByteArray byteArray, QString format, QString filename) {
void ImgurUploader::doUpload(QByteArray byteArray, QString format, ScreenshotFile sf) {
if (byteArray.size() > 1e+7) {
notifications::notify(tr("KShare imgur Uploader"), tr("Failed upload! Image too big"));
return;
@ -69,32 +71,32 @@ void ImgurUploader::doUpload(QByteArray byteArray, QString format, QString filen
if (QDateTime::currentDateTimeUtc() > expireTime) {
new SegfaultWorkaround(byteArray, this, mime);
} else
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray, filename);
handleSend("Bearer " + settings::settings().value("imgur/access").toString(), mime, byteArray, sf);
} else
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray, filename);
handleSend(QStringLiteral("Client-ID 8a98f183fc895da"), mime, byteArray, sf);
}
void ImgurUploader::showSettings() {
(new ImgurSettingsDialog())->show();
}
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray, QString filename) {
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray, ScreenshotFile sf) {
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, filename](QJsonDocument res, QByteArray data, QNetworkReply *r) {
byteArray, [byteArray, this, mime, sf](QJsonDocument res, QByteArray data, QNetworkReply *r) {
QString result = res.object()["data"].toObject()["link"].toString();
if (r->error() == QNetworkReply::ContentAccessDenied) {
new SegfaultWorkaround(byteArray, this, mime);
return;
}
if (!result.isEmpty()) {
ioutils::addLogEntry(r, data, result, filename);
ioutils::addLogEntry(r, data, result, sf);
utils::toClipboard(result);
notifications::notify(tr("KShare imgur Uploader"), tr("Uploaded to imgur!"));
notifications::playSound(notifications::Sound::SUCCESS);
} else {
ioutils::addLogEntry(r, data, result, filename);
ioutils::addLogEntry(r, data, result, sf);
notifications::notify(tr("KShare imgur Uploader "),
QString(tr("Failed upload! imgur said: HTTP %1: %2"))
.arg(r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt())
@ -103,7 +105,3 @@ void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray,
}
});
}
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
handleSend(auth, mime, byteArray);
}

View File

@ -3,6 +3,7 @@
#include "../uploader.hpp"
#include <QApplication>
#include <logs/screenshotfile.h>
class ImgurUploader : public Uploader {
Q_DECLARE_TR_FUNCTIONS(ImgurUploader)
@ -15,12 +16,11 @@ public:
QString description() override {
return "imgur.com uploader";
}
void doUpload(QByteArray byteArray, QString, QString filename) override;
void doUpload(QByteArray byteArray, QString, ScreenshotFile sf) override;
void showSettings() override;
private:
void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename);
void handleSend(QString auth, QString mime, QByteArray byteArray);
void handleSend(QString auth, QString mime, QByteArray byteArray, ScreenshotFile sf);
};
#endif // IMGURUPLOADER_HPP

View File

@ -3,10 +3,11 @@
#include <QPixmap>
#include <QString>
#include <logs/screenshotfile.h>
class Uploader {
public:
virtual void doUpload(QByteArray imgData, QString format, QString filename) = 0;
virtual void doUpload(QByteArray imgData, QString format, ScreenshotFile sf) = 0;
virtual QString name() = 0;
virtual QString description() = 0;
virtual void showSettings() {

View File

@ -14,6 +14,7 @@
#include <notifications.hpp>
#include <settings.hpp>
#include "mainwindow.hpp"
#include <logs/screenshotfile.h>
UploaderSingleton::UploaderSingleton() : QObject() {
updateSaveSettings();
@ -73,8 +74,7 @@ void UploaderSingleton::upload(QPixmap pixmap) {
notifications::playSound(notifications::Sound::CAPTURE);
pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
file->seek(0);
QFileInfo fileInfo(file->fileName());
u->doUpload(file->readAll(), format, fileInfo.fileName());
u->doUpload(file->readAll(), format, getScreenshotFile(*file));
} else
notifications::notify(tr("KShare - Failed to save picture"), file->errorString(), QSystemTrayIcon::Warning);
delete file;
@ -96,9 +96,8 @@ void UploaderSingleton::upload(QByteArray img, QString format) {
file->write(img);
file->close();
}
QFileInfo fileInfo(file->fileName());
uploaders.value(uploader)->doUpload(img, format, getScreenshotFile(*file));
delete file;
uploaders.value(uploader)->doUpload(img, format, fileInfo.fileName());
}
void UploaderSingleton::upload(QFile &img, QString format) {
@ -108,9 +107,8 @@ 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())))) {
notifications::playSound(notifications::Sound::CAPTURE);
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), format, fileInfo.fileName());
uploaders.value(uploader)->doUpload(img.readAll(), format, getScreenshotFile(img));
else
notifications::notify(tr("KShare - Failed to save picture"), img.errorString(), QSystemTrayIcon::Warning);
} else
@ -120,9 +118,8 @@ void UploaderSingleton::upload(QFile &img, QString format) {
void UploaderSingleton::upload(QFile &img) {
updateSaveSettings();
if (img.size() <= 0) return;
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), "", fileInfo.fileName());
uploaders.value(uploader)->doUpload(img.readAll(), "", getScreenshotFile(img));
else
notifications::notify(tr("KShare - Failed to open File"), img.errorString(), QSystemTrayIcon::Warning);
}
@ -159,6 +156,18 @@ QString UploaderSingleton::currentUploader() {
return uploader;
}
QString UploaderSingleton::getFormattedSubfolder() {
return formatter::format(settings::settings().value("folderFormat", "%(yyyy-MM)date").toString(), "");
}
ScreenshotFile UploaderSingleton::getScreenshotFile(QFile &f) {
ScreenshotFile sf;
sf.subfolder = getFormattedSubfolder();
QFileInfo fi(f);
sf.filename = fi.fileName();
return sf;
}
void UploaderSingleton::updateSaveSettings() {
switch (settings::settings().value("saveLocation", 1).toInt()) {
case 0:
@ -180,6 +189,8 @@ void UploaderSingleton::updateSaveSettings() {
break;
}
saveDir = QDir(saveDir.absolutePath() + QDir::separator() + getFormattedSubfolder());
if (!saveDir.exists()) {
if (!saveDir.mkpath(".")) {
qFatal("Could not create the path %s to store images in!", saveDir.absolutePath().toLocal8Bit().constData());

View File

@ -4,6 +4,7 @@
#include "uploader.hpp"
#include <QDir>
#include <QMap>
#include <logs/screenshotfile.h>
class UploaderSingleton : public QObject {
Q_OBJECT
@ -31,6 +32,8 @@ signals:
private:
void updateSaveSettings();
QString getFormattedSubfolder();
ScreenshotFile getScreenshotFile(QFile &f);
QDir saveDir;
bool saveImages = true;
QMap<QString, Uploader *> uploaders;