Add status bar, customuploader cleanup

This commit is contained in:
ArsenArsen 2017-04-25 16:04:46 +02:00
parent 0101b61651
commit 90fbb80f50
5 changed files with 39 additions and 23 deletions

View File

@ -7,9 +7,9 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QMenu> #include <QMenu>
#include <QStatusBar>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QTimer> #include <QTimer>
#include <QUrl>
#include <settings.hpp> #include <settings.hpp>
#include <uploaders/uploadersingleton.hpp> #include <uploaders/uploadersingleton.hpp>
@ -51,6 +51,12 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
setScheme(settings::settings().value("fileFormat").toString()); setScheme(settings::settings().value("fileFormat").toString());
else else
setScheme("Screenshot %(yyyy-MM-dd HH:mm:ss)date"); setScheme("Screenshot %(yyyy-MM-dd HH:mm:ss)date");
auto errors = UploaderSingleton::inst().errors();
if (errors.length() == 1)
statusBar()->showMessage(errors.at(0).what());
else
statusBar()->showMessage(QString("Errors visible in console. Count: " + errors.size()));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()

View File

@ -79,6 +79,7 @@
<addaction name="menuFile"/> <addaction name="menuFile"/>
<addaction name="menuScreenshot"/> <addaction name="menuScreenshot"/>
</widget> </widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionQuit"> <action name="actionQuit">
<property name="text"> <property name="text">
<string>&amp;Quit</string> <string>&amp;Quit</string>

View File

@ -11,6 +11,11 @@
using std::runtime_error; using std::runtime_error;
void error(QString absFilePath, QString err)
{
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": " + err).toStdString());
}
CustomUploader::CustomUploader(QString absFilePath) CustomUploader::CustomUploader(QString absFilePath)
{ {
types.insert("PNG", "image/png"); // Is a list of supported formats, too types.insert("PNG", "image/png"); // Is a list of supported formats, too
@ -21,21 +26,21 @@ CustomUploader::CustomUploader(QString absFilePath)
types.insert("WEBM", "video/mp4"); types.insert("WEBM", "video/mp4");
// Let's go // Let's go
QFile file(absFilePath); QFile file(absFilePath);
file.open(QIODevice::ReadOnly | QIODevice::Text); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) error(absFilePath, file.errorString());
QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
if (!doc.isObject()) if (!doc.isObject())
{ {
throw runtime_error(QString("Invalid file: ").append(absFilePath).toStdString()); error(absFilePath, "Root not an object");
} }
QJsonObject obj = doc.object(); QJsonObject obj = doc.object();
if (!obj["name"].isString()) if (!obj["name"].isString())
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": name is not a string").toStdString()); error(absFilePath, "name is not a string");
else else
uName = obj["name"].toString(); uName = obj["name"].toString();
if (!obj.contains("desc")) if (!obj.contains("desc"))
{ {
if (!obj["desc"].isString()) if (!obj["desc"].isString())
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": desc not a string, or nonexisting").toStdString()); /*t*/ error(absFilePath, "desc not a string");
else else
desc = obj["desc"].toString(); desc = obj["desc"].toString();
} }
@ -44,22 +49,20 @@ CustomUploader::CustomUploader(QString absFilePath)
QJsonValue m = obj["method"]; QJsonValue m = obj["method"];
if (!m.isUndefined() && !m.isNull()) if (!m.isUndefined() && !m.isNull())
{ {
if (!m.isString()) if (!m.isString()) error(absFilePath, "method not a string");
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": method not string").toStdString());
QString toCheck = m.toString().toLower(); QString toCheck = m.toString().toLower();
if (toCheck == "post") if (toCheck == "post")
method = HttpMethod::POST; method = HttpMethod::POST;
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": Bad method").toStdString()); error(absFilePath, "method invalid");
} }
QJsonValue url = obj["target"]; QJsonValue url = obj["target"];
if (!url.isString()) if (!url.isString())
{ {
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": target missing").toStdString()); error(absFilePath, "target missing");
} }
QUrl target(url.toString()); QUrl target(url.toString());
if (!target.isValid()) if (!target.isValid()) error(absFilePath, "target not URL");
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": target not URL").toStdString());
this->target = target; this->target = target;
QJsonValue formatValue = obj["format"]; QJsonValue formatValue = obj["format"];
if (!formatValue.isUndefined() && !formatValue.isNull()) if (!formatValue.isUndefined() && !formatValue.isNull())
@ -74,15 +77,15 @@ CustomUploader::CustomUploader(QString absFilePath)
else if (formatString == "plain") else if (formatString == "plain")
format = RequestFormat::PLAIN; format = RequestFormat::PLAIN;
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": format invalid").toStdString()); error(absFilePath, "format invalid");
} }
} }
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": format provided but not string").toStdString()); error(absFilePath, "format provided but not string");
QJsonValue imageValue = obj["imageformat"]; QJsonValue imageValue = obj["imageformat"];
if (!imageValue.isString()) if (!imageValue.isString())
{ {
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": imageformat invalid/missing").toStdString()); error(absFilePath, "imageformat not string");
} }
QString imageFormat = imageValue.toString(); QString imageFormat = imageValue.toString();
if (imageFormat == "base64" || QRegExp("base64\\([^+]+\\+[^+]+)").exactMatch(imageFormat) if (imageFormat == "base64" || QRegExp("base64\\([^+]+\\+[^+]+)").exactMatch(imageFormat)
@ -91,16 +94,15 @@ CustomUploader::CustomUploader(QString absFilePath)
this->iFormat = imageFormat; this->iFormat = imageFormat;
} }
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": imageformat bad").toStdString()); error(absFilePath, "imageformat invalid");
QJsonValue bodyValue = obj["body"]; QJsonValue bodyValue = obj["body"];
if (format != RequestFormat::PLAIN) if (format != RequestFormat::PLAIN)
{ {
if (bodyValue.isUndefined()) if (bodyValue.isUndefined()) error(absFilePath, "body not set");
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": body unset").toStdString());
if (bodyValue.isObject()) if (bodyValue.isObject())
body = bodyValue; body = bodyValue;
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": body must be object").toStdString()); error(absFilePath, "body not object");
} }
else else
{ {
@ -109,13 +111,12 @@ CustomUploader::CustomUploader(QString absFilePath)
body = bodyValue; body = bodyValue;
} }
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": body must be string (due to PLAIN)").toStdString()); error(absFilePath, "body not string (reason: format: PLAIN)");
} }
QJsonValue headerVal = obj["headers"]; QJsonValue headerVal = obj["headers"];
if (!(headerVal.isUndefined() || headerVal.isNull())) if (!(headerVal.isUndefined() || headerVal.isNull()))
{ {
if (!headerVal.isObject()) if (!headerVal.isObject()) error(absFilePath, "headers must be object");
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": headers must be object").toStdString());
headers = headerVal.toObject(); headers = headerVal.toObject();
} }
else else
@ -126,7 +127,7 @@ CustomUploader::CustomUploader(QString absFilePath)
returnPathspec = returnPsVal.toString(); returnPathspec = returnPsVal.toString();
} }
else else
throw runtime_error((QString("Invalid file: ").append(absFilePath) + ": return invalid").toStdString()); error(absFilePath, "return invalid");
} }
QString CustomUploader::name() QString CustomUploader::name()

View File

@ -22,7 +22,8 @@ UploaderSingleton::UploaderSingleton()
} }
catch (std::runtime_error e) catch (std::runtime_error e)
{ {
qWarning() << e.what(); // u wot m8 qWarning() << e.what();
errs << e;
} }
} }
@ -79,3 +80,8 @@ QString UploaderSingleton::selectedUploader()
{ {
return uploader; return uploader;
} }
QList<std::runtime_error> UploaderSingleton::errors()
{
return errs;
}

View File

@ -17,11 +17,13 @@ class UploaderSingleton
QList<Uploader *> uploaderList(); QList<Uploader *> uploaderList();
void set(QString uploader); void set(QString uploader);
QString selectedUploader(); QString selectedUploader();
QList<std::runtime_error> errors();
private: private:
UploaderSingleton(); UploaderSingleton();
QMap<QString, Uploader *> uploaders; QMap<QString, Uploader *> uploaders;
QString uploader = "imgur"; QString uploader = "imgur";
QList<std::runtime_error> errs;
}; };
#endif // UPLOADERSINGLETON_HPP #endif // UPLOADERSINGLETON_HPP