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-06-04 22:58:29 +02:00
|
|
|
#include <QBuffer>
|
2017-04-24 23:14:01 +02:00
|
|
|
#include <QDebug>
|
2017-04-23 15:05:48 +02:00
|
|
|
#include <QDir>
|
2017-06-06 17:05:34 +02:00
|
|
|
#include <QFile>
|
2017-04-23 15:05:48 +02:00
|
|
|
#include <QStandardPaths>
|
2017-06-06 17:05:34 +02:00
|
|
|
#include <formats.hpp>
|
2017-04-23 20:29:24 +02:00
|
|
|
#include <formatter.hpp>
|
2017-06-06 17:05:34 +02:00
|
|
|
#include <notifications.hpp>
|
2017-04-23 15:05:48 +02:00
|
|
|
#include <settings.hpp>
|
2017-06-30 16:49:12 +02:00
|
|
|
#include <uploaders/default/imgplusuploader.hpp>
|
2017-04-23 15:05:48 +02:00
|
|
|
|
2017-07-02 22:31:27 +02:00
|
|
|
UploaderSingleton::UploaderSingleton() : QObject() {
|
|
|
|
switch (settings::settings().value("saveLocation", 1).toInt()) {
|
|
|
|
case 0:
|
|
|
|
saveDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
|
|
|
if (QStandardPaths::writableLocation(QStandardPaths::PicturesLocation).isEmpty()) {
|
|
|
|
qFatal("Cannot determine location for pictures");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (QStandardPaths::writableLocation(QStandardPaths::HomeLocation).isEmpty()) {
|
|
|
|
qFatal("Cannot determine location of your home directory");
|
|
|
|
}
|
|
|
|
saveDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/Screenshots";
|
2017-07-03 12:07:49 +02:00
|
|
|
break;
|
2017-07-02 22:31:27 +02:00
|
|
|
default:
|
2017-07-02 22:33:44 +02:00
|
|
|
qFatal("Invalid config [saveLocation not int or is not in range]");
|
2017-07-02 22:31:27 +02:00
|
|
|
break;
|
2017-05-21 12:08:30 +02:00
|
|
|
}
|
2017-07-02 22:31:27 +02:00
|
|
|
|
2017-05-21 12:08:30 +02:00
|
|
|
if (!saveDir.exists()) {
|
2017-05-22 15:56:47 +02:00
|
|
|
if (!saveDir.mkpath(".")) {
|
2017-05-22 18:23:45 +02:00
|
|
|
qFatal("Could not create the path %s to store images in!", saveDir.absolutePath().toLocal8Bit().constData());
|
2017-05-22 15:56:47 +02:00
|
|
|
}
|
2017-05-21 12:08:30 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +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)));
|
2017-06-30 15:05:37 +02:00
|
|
|
} catch (std::runtime_error &e) {
|
2017-05-09 17:26:00 +02:00
|
|
|
qWarning() << e.what();
|
|
|
|
errs << e;
|
|
|
|
}
|
2017-04-24 23:14:01 +02:00
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
// UPLOADERS
|
|
|
|
registerUploader(new ImgurUploader);
|
2017-06-30 16:49:12 +02:00
|
|
|
registerUploader(new ImgplusUploader);
|
2017-05-09 17:26:00 +02:00
|
|
|
registerUploader(new ClipboardUploader);
|
|
|
|
// ---------
|
2017-04-23 15:05:48 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
if (settings::settings().contains("uploader"))
|
|
|
|
uploader = settings::settings().value("uploader").toString();
|
|
|
|
else
|
|
|
|
settings::settings().setValue("uploader", uploader);
|
|
|
|
if (!uploaders.contains(uploader)) {
|
|
|
|
uploader = "imgur";
|
2017-06-30 15:05:37 +02:00
|
|
|
settings::settings().setValue("uploader", uploader);
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
void UploaderSingleton::registerUploader(Uploader *uploader) {
|
2017-06-30 15:05:37 +02:00
|
|
|
if (uploaders.contains(uploader->name()))
|
2017-05-09 17:26:00 +02:00
|
|
|
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-07-02 20:51:15 +02:00
|
|
|
void UploaderSingleton::upload(QPixmap pixmap) {
|
2017-06-04 22:58:29 +02:00
|
|
|
auto u = uploaders.value(uploader);
|
2017-06-30 15:05:37 +02:00
|
|
|
if (!u->validate()) {
|
|
|
|
u = uploaders.value("imgur");
|
|
|
|
set("imgur");
|
|
|
|
qWarning() << "Currently selected uploader is not set up properly! Falling back to imgur";
|
|
|
|
}
|
2017-06-06 17:05:34 +02:00
|
|
|
QString format = settings::settings().value("captureformat", "PNG").toString();
|
2017-06-22 23:07:09 +02:00
|
|
|
QFile file(saveDir.absoluteFilePath(
|
2017-07-04 22:35:34 +02:00
|
|
|
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
|
2017-06-22 23:07:09 +02:00
|
|
|
format.toLower())));
|
2017-06-06 17:05:34 +02:00
|
|
|
|
|
|
|
if (file.open(QFile::ReadWrite)) {
|
2017-07-02 20:51:15 +02:00
|
|
|
pixmap.save(&file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
|
2017-06-06 17:05:34 +02:00
|
|
|
file.seek(0);
|
|
|
|
u->doUpload(file.readAll(), format);
|
|
|
|
} else
|
|
|
|
notifications::notify("KShare - Failed to save picture", file.errorString(), QSystemTrayIcon::Warning);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:05:34 +02:00
|
|
|
void UploaderSingleton::upload(QByteArray img, QString format) {
|
2017-06-13 17:02:35 +02:00
|
|
|
if (img.isEmpty()) return;
|
2017-06-06 23:58:02 +02:00
|
|
|
QFile file(saveDir.absoluteFilePath(formatter::format(settings::settings().value("fileFormat").toString(), format.toLower())));
|
|
|
|
if (file.open(QFile::WriteOnly)) {
|
|
|
|
file.write(img);
|
|
|
|
file.close();
|
|
|
|
}
|
2017-06-06 17:05:34 +02:00
|
|
|
uploaders.value(uploader)->doUpload(img, format);
|
2017-06-04 22:58:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 22:34:11 +02:00
|
|
|
void UploaderSingleton::upload(QFile &img, QString format) {
|
|
|
|
if (img.size() <= 0) return;
|
|
|
|
if (img.rename(
|
|
|
|
saveDir.absoluteFilePath(formatter::format(settings::settings().value("fileFormat").toString(), format.toLower())))) {
|
|
|
|
if (img.open(QFile::ReadWrite))
|
|
|
|
uploaders.value(uploader)->doUpload(img.readAll(), format);
|
|
|
|
else
|
|
|
|
notifications::notify("KShare - Failed to save picture", img.errorString(), QSystemTrayIcon::Warning);
|
|
|
|
} else
|
|
|
|
notifications::notify("KShare - Failed to save picture", img.errorString(), QSystemTrayIcon::Warning);
|
2017-06-04 22:58:29 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 15:05:37 +02:00
|
|
|
void UploaderSingleton::showSettings() {
|
|
|
|
uploaders.value(uploader)->showSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UploaderSingleton::validate() {
|
|
|
|
return uploaders.value(uploader)->validate();
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
QList<Uploader *> UploaderSingleton::uploaderList() {
|
2017-05-09 17:26:00 +02:00
|
|
|
return uploaders.values();
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:21:12 +02:00
|
|
|
void UploaderSingleton::set(QString uploader) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (uploaders.contains(uploader)) {
|
|
|
|
this->uploader = uploader;
|
|
|
|
settings::settings().setValue("uploader", uploader);
|
2017-06-30 15:05:37 +02:00
|
|
|
emit uploaderChanged(uploader);
|
2017-05-09 17:26:00 +02:00
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QString UploaderSingleton::selectedUploader() {
|
|
|
|
return uploader;
|
|
|
|
}
|
2017-04-25 16:04:46 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QList<std::runtime_error> UploaderSingleton::errors() {
|
|
|
|
return errs;
|
|
|
|
}
|
2017-06-23 14:39:51 +02:00
|
|
|
|
|
|
|
QString UploaderSingleton::currentUploader() {
|
|
|
|
return uploader;
|
|
|
|
}
|