KShare/uploaders/uploadersingleton.cpp

85 lines
2.7 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "uploadersingleton.hpp"
2017-04-24 23:14:01 +02:00
#include "customuploader.hpp"
2017-04-23 15:05:48 +02:00
#include "default/clipboarduploader.hpp"
#include "default/imguruploader.hpp"
2017-04-24 23:14:01 +02:00
#include <QDebug>
2017-04-23 15:05:48 +02:00
#include <QDir>
#include <QStandardPaths>
2017-04-23 20:29:24 +02:00
#include <formatter.hpp>
2017-04-23 15:05:48 +02:00
#include <settings.hpp>
UploaderSingleton::UploaderSingleton() : QObject(), saveDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)) {
if (QStandardPaths::writableLocation(QStandardPaths::PicturesLocation).isEmpty()) {
2017-05-22 15:56:47 +02:00
qFatal("Cannot determine location for pictures");
}
if (!saveDir.exists()) {
2017-05-22 15:56:47 +02:00
if (!saveDir.mkpath(".")) {
qFatal("Could not create the path %s to store images in!", saveDir.absolutePath().toLocal8Bit().constData());
2017-05-22 15:56:47 +02:00
}
}
QDir configDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
configDir.mkpath("KShare/uploaders");
configDir.cd("KShare/uploaders");
configDir.setNameFilters({ "*.uploader" });
for (QString file : configDir.entryList()) {
try {
registerUploader(new CustomUploader(configDir.absoluteFilePath(file)));
} catch (std::runtime_error e) {
qWarning() << e.what();
errs << e;
}
2017-04-24 23:14:01 +02:00
}
2017-04-23 15:05:48 +02:00
// UPLOADERS
registerUploader(new ImgurUploader);
registerUploader(new ClipboardUploader);
// ---------
2017-04-23 15:05:48 +02:00
if (settings::settings().contains("uploader"))
uploader = settings::settings().value("uploader").toString();
else
settings::settings().setValue("uploader", uploader);
if (!uploaders.contains(uploader)) {
settings::settings().setValue("uploader", uploader);
uploader = "imgur";
}
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +02:00
void UploaderSingleton::registerUploader(Uploader *uploader) {
if (uploaders.contains(uploader->name())) {
throw std::runtime_error(("Ambigious uploader " + uploader->name()).toStdString());
}
uploaders.insert(uploader->name(), uploader);
emit newUploader(uploader);
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +02:00
void UploaderSingleton::upload(QPixmap *pixmap) {
if (settings::settings().contains("fileFormat")) {
QString format = settings::settings().value("fileFormat").toString();
if (!format.isEmpty()) {
pixmap->save(saveDir.absoluteFilePath(formatter::format(format) + ".png"), "PNG");
}
2017-04-23 20:29:24 +02:00
}
uploaders.value(uploader)->doUpload(pixmap);
delete pixmap;
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +02:00
QList<Uploader *> UploaderSingleton::uploaderList() {
return uploaders.values();
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +02:00
void UploaderSingleton::set(QString uploader) {
if (uploaders.contains(uploader)) {
this->uploader = uploader;
settings::settings().setValue("uploader", uploader);
}
2017-04-23 15:05:48 +02:00
}
QString UploaderSingleton::selectedUploader() {
return uploader;
}
2017-04-25 16:04:46 +02:00
QList<std::runtime_error> UploaderSingleton::errors() {
return errs;
}