Add an option to not save SS. Fixes #65

This commit is contained in:
ArsenArsen 2018-03-18 11:52:30 +01:00
parent 2b89307d84
commit fdbb43a19f
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
3 changed files with 67 additions and 38 deletions

View File

@ -128,6 +128,11 @@
<string>Screenshots folder (In your user folder)</string> <string>Screenshots folder (In your user folder)</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>Do not save</string>
</property>
</item>
</widget> </widget>
</item> </item>
<item row="8" column="2"> <item row="8" column="2">

View File

@ -6,6 +6,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
#include <QTemporaryFile>
#include <formats.hpp> #include <formats.hpp>
#include <formatter.hpp> #include <formatter.hpp>
#include <logger.hpp> #include <logger.hpp>
@ -14,28 +15,7 @@
#include <uploaders/default/imgplusuploader.hpp> #include <uploaders/default/imgplusuploader.hpp>
UploaderSingleton::UploaderSingleton() : QObject() { UploaderSingleton::UploaderSingleton() : QObject() {
switch (settings::settings().value("saveLocation", 1).toInt()) { updateSaveSettings();
case 0:
saveDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (QStandardPaths::writableLocation(QStandardPaths::PicturesLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location for pictures").toLocal8Bit().constData());
}
break;
case 1:
if (QStandardPaths::writableLocation(QStandardPaths::HomeLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location of your home directory").toLocal8Bit().constData());
}
saveDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/Screenshots";
break;
default:
qFatal("%s", tr("Invalid config [saveLocation not int or is not in range]").toLocal8Bit().constData());
}
if (!saveDir.exists()) {
if (!saveDir.mkpath(".")) {
qFatal("Could not create the path %s to store images in!", saveDir.absolutePath().toLocal8Bit().constData());
}
}
QDir configDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)); QDir configDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
configDir.mkpath("KShare/uploaders"); configDir.mkpath("KShare/uploaders");
configDir.cd("KShare/uploaders"); configDir.cd("KShare/uploaders");
@ -73,6 +53,7 @@ void UploaderSingleton::registerUploader(Uploader *uploader) {
} }
void UploaderSingleton::upload(QPixmap pixmap) { void UploaderSingleton::upload(QPixmap pixmap) {
updateSaveSettings();
auto u = uploaders.value(uploader); auto u = uploaders.value(uploader);
if (!u->validate()) { if (!u->validate()) {
u = uploaders.value("imgur"); u = uploaders.value("imgur");
@ -80,33 +61,46 @@ void UploaderSingleton::upload(QPixmap pixmap) {
logger::warn(tr("Currently selected uploader is not set up properly! Falling back to imgur")); logger::warn(tr("Currently selected uploader is not set up properly! Falling back to imgur"));
} }
QString format = settings::settings().value("captureformat", "PNG").toString(); QString format = settings::settings().value("captureformat", "PNG").toString();
QFile file(saveDir.absoluteFilePath( QFile *file = nullptr;
if (saveImages) {
file = new QFile(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(), formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower()))); format.toLower())));
} else {
if (file.open(QFile::ReadWrite)) { file = new QTemporaryFile();
pixmap.save(&file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt()); }
file.seek(0); if (file->open(QFile::ReadWrite)) {
u->doUpload(file.readAll(), format); pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
file->seek(0);
u->doUpload(file->readAll(), format);
} else } else
notifications::notify(tr("KShare - Failed to save picture"), file.errorString(), QSystemTrayIcon::Warning); notifications::notify(tr("KShare - Failed to save picture"), file->errorString(), QSystemTrayIcon::Warning);
delete file;
} }
void UploaderSingleton::upload(QByteArray img, QString format) { void UploaderSingleton::upload(QByteArray img, QString format) {
updateSaveSettings();
if (img.isEmpty()) return; if (img.isEmpty()) return;
QFile file(saveDir.absoluteFilePath( QFile *file = nullptr;
if (saveImages) {
file = new QFile(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(), formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower()))); format.toLower())));
if (file.open(QFile::WriteOnly)) { } else {
file.write(img); file = new QTemporaryFile();
file.close();
} }
if (file->open(QFile::WriteOnly)) {
file->write(img);
file->close();
}
delete file;
uploaders.value(uploader)->doUpload(img, format); uploaders.value(uploader)->doUpload(img, format);
} }
void UploaderSingleton::upload(QFile &img, QString format) { void UploaderSingleton::upload(QFile &img, QString format) {
updateSaveSettings();
if (img.size() <= 0) return; if (img.size() <= 0) return;
if (img.rename(saveDir.absoluteFilePath( if (!saveImages || img.rename(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(), formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) { format.toLower())))) {
if (img.open(QFile::ReadWrite)) if (img.open(QFile::ReadWrite))
@ -148,3 +142,31 @@ QList<std::runtime_error> UploaderSingleton::errors() {
QString UploaderSingleton::currentUploader() { QString UploaderSingleton::currentUploader() {
return uploader; return uploader;
} }
void UploaderSingleton::updateSaveSettings() {
switch (settings::settings().value("saveLocation", 1).toInt()) {
case 0:
saveDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (QStandardPaths::writableLocation(QStandardPaths::PicturesLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location for pictures").toLocal8Bit().constData());
}
break;
case 1:
if (QStandardPaths::writableLocation(QStandardPaths::HomeLocation).isEmpty()) {
qFatal("%s", tr("Cannot determine location of your home directory").toLocal8Bit().constData());
}
saveDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/Screenshots";
break;
default:
qFatal("%s", tr("Invalid config [saveLocation not int or is not in range]").toLocal8Bit().constData());
case 2:
saveImages = false;
break;
}
if (!saveDir.exists()) {
if (!saveDir.mkpath(".")) {
qFatal("Could not create the path %s to store images in!", saveDir.absolutePath().toLocal8Bit().constData());
}
}
}

View File

@ -29,7 +29,9 @@ signals:
void uploaderChanged(QString newName); void uploaderChanged(QString newName);
private: private:
void updateSaveSettings();
QDir saveDir; QDir saveDir;
bool saveImages = true;
QMap<QString, Uploader *> uploaders; QMap<QString, Uploader *> uploaders;
QString uploader = "imgur"; QString uploader = "imgur";
QList<std::runtime_error> errs; QList<std::runtime_error> errs;