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>
|
|
|
|
|
2017-04-25 21:27:29 +02:00
|
|
|
UploaderSingleton::UploaderSingleton() : QObject()
|
2017-04-23 15:05:48 +02:00
|
|
|
{
|
2017-04-24 23:14:01 +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)
|
|
|
|
{
|
2017-04-25 16:04:46 +02:00
|
|
|
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);
|
|
|
|
// ---------
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploaderSingleton::registerUploader(Uploader *uploader)
|
|
|
|
{
|
|
|
|
uploaders.insert(uploader->name(), uploader);
|
2017-04-25 21:27:29 +02:00
|
|
|
emit newUploader(uploader);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UploaderSingleton::upload(QPixmap *pixmap)
|
|
|
|
{
|
2017-04-23 20:29:24 +02:00
|
|
|
if (settings::settings().contains("fileFormat"))
|
|
|
|
{
|
|
|
|
QString format = settings::settings().value("fileFormat").toString();
|
|
|
|
if (!format.isEmpty())
|
|
|
|
{
|
|
|
|
pixmap->save(QDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)).absoluteFilePath(formatter::format(format) + ".png"),
|
|
|
|
"PNG");
|
|
|
|
}
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
uploaders.value(uploader)->doUpload(pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Uploader *> UploaderSingleton::uploaderList()
|
|
|
|
{
|
|
|
|
return uploaders.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploaderSingleton::set(QString uploader)
|
|
|
|
{
|
|
|
|
if (uploaders.contains(uploader))
|
|
|
|
{
|
|
|
|
this->uploader = uploader;
|
|
|
|
settings::settings().setValue("uploader", uploader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString UploaderSingleton::selectedUploader()
|
|
|
|
{
|
|
|
|
return uploader;
|
|
|
|
}
|
2017-04-25 16:04:46 +02:00
|
|
|
|
|
|
|
QList<std::runtime_error> UploaderSingleton::errors()
|
|
|
|
{
|
|
|
|
return errs;
|
|
|
|
}
|