diff --git a/sailfish/qml/Notification.qml b/sailfish/qml/Notification.qml index 34acb0a..014243d 100644 --- a/sailfish/qml/Notification.qml +++ b/sailfish/qml/Notification.qml @@ -28,6 +28,7 @@ Notification { maxContentLines: 10 function show(bodyText, summaryText, clickedHandler) { + //console.log("show: " + bodyText + " " + summaryText) if (!bodyText || bodyText.length === 0) return diff --git a/sailfish/qml/SettingsPage.qml b/sailfish/qml/SettingsPage.qml index de0eef3..1297914 100644 --- a/sailfish/qml/SettingsPage.qml +++ b/sailfish/qml/SettingsPage.qml @@ -899,47 +899,6 @@ Page { } } - /*SectionHeader { - text: qsTr("Experiments") - } - - Column { - x: Theme.horizontalPageMargin - spacing: Theme.paddingMedium - - Row { - spacing: Theme.paddingMedium - - Image { - source: "image://icons/icon-m-good" - height: Theme.iconSizeSmall - width: Theme.iconSizeSmall - anchors.verticalCenter: parent.verticalCenter - } - - Label { - text: ai.evaluationCount(1) - anchors.verticalCenter: parent.verticalCenter - } - } - - Row { - spacing: Theme.paddingMedium - - Image { - source: "image://icons/icon-m-bad" - height: Theme.iconSizeSmall - width: Theme.iconSizeSmall - anchors.verticalCenter: parent.verticalCenter - } - - Label { - text: ai.evaluationCount(-1) - anchors.verticalCenter: parent.verticalCenter - } - } - }*/ - SectionHeader { text: qsTr("Other") } diff --git a/sailfish/qml/main.qml b/sailfish/qml/main.qml index e27178e..dcb1080 100644 --- a/sailfish/qml/main.qml +++ b/sailfish/qml/main.qml @@ -325,7 +325,7 @@ ApplicationWindow { return; } } else if (code === 800) { - notification.show(qsTr("Cannot save the image file")); + notification.show(qsTr("Cannot save image")); } else if (code === 801) { notification.show(qsTr("Image file already exists")); } else { @@ -402,6 +402,7 @@ ApplicationWindow { } function fetcherImageSaved(filename) { + //console.log("fetcherImageSaved: " + filename) notification.show(qsTr("Image saved as \"" + filename + "\"")); } diff --git a/sailfish/src/fetcher.cpp b/sailfish/src/fetcher.cpp index 5aeee26..cc6b675 100644 --- a/sailfish/src/fetcher.cpp +++ b/sailfish/src/fetcher.cpp @@ -24,10 +24,12 @@ #include #include #include -#include #include #include #include +#include +#include +#include #include #else #include "parser.h" @@ -434,26 +436,42 @@ void Fetcher::taskEnd() setBusy(false); } -void Fetcher::addExtension(const QString &contentType, QString &path) +void Fetcher::copyImage(const QString &path, const QString &contentType) { - auto orig_ext = QFileInfo(path).suffix(); + QString dpath = QDir(Settings::instance()->getImagesDir()) + .absoluteFilePath("kaktus_" + QFileInfo(path).fileName()); + + Utils::addExtension(contentType, dpath); + + if (QFile::exists(dpath)) { + emit error(801); // image already exists + return; + } - QString new_ext; if (contentType == "image/jpeg") { - new_ext = "jpg"; - } else if (contentType == "image/png") { - new_ext = "png"; - } else if (contentType == "image/gif") { - new_ext = "gif"; - } else if (contentType == "image/svg+xml") { - new_ext = "svg"; - } else { - new_ext = orig_ext; + qWarning() << "JPEG file, so removing EXIF metadata"; + QImageReader ir(path); + QImage img = ir.read(); + if (img.isNull()) { + qWarning() << "Cannot read image:" << path << ir.errorString(); + } else { + QImageWriter iw(dpath); + iw.setFormat(ir.format()); + if (iw.write(img)) { + qDebug() << "Image saved successfully"; + emit imageSaved(QFileInfo(dpath).fileName()); + return; + } else { + qWarning() << "Cannot write image:" << dpath << iw.errorString(); + } + } + } else if (QFile::copy(path, dpath)) { + qDebug() << "Image saved successfully"; + emit imageSaved(QFileInfo(dpath).fileName()); + return; } - if (new_ext != orig_ext) { - path.append("." + new_ext); - } + emit error(800); // image save error } void Fetcher::saveImage(const QString &url) @@ -463,26 +481,13 @@ void Fetcher::saveImage(const QString &url) QString path, contentType; if (CacheServer::getPathAndContentTypeByUrl(url, path, contentType)) { qDebug() << "Image already in cache"; - //qDebug() << "path:" << path; - //qDebug() << "contentType:" << contentType; - QString dpath = QDir(QStandardPaths::writableLocation( - QStandardPaths::PicturesLocation)) - .absoluteFilePath(QFileInfo(path).fileName()); - addExtension(contentType, dpath); - //qDebug() << "dpath:" << dpath; - if (QFile::exists(dpath)) { - emit error(801); // image already exists - } else if (QFile::copy(path, dpath)) { - emit imageSaved(QFileInfo(dpath).fileName()); - } else { - emit error(800); // image save error - } + copyImage(path, contentType); } else { qDebug() << "Image not cached, so downloading"; DatabaseManager::CacheItem item; item.origUrl = url; item.finalUrl = url; - //item.type = "entry-image"; + item.type = "entry-image"; emit addDownload(item); auto dm = DownloadManager::instance(); @@ -494,18 +499,7 @@ void Fetcher::saveImage(const QString &url) //qDebug() << "Download finished:" << url << path << contentType; Q_UNUSED(url); disconnect(*conn1); disconnect(*conn2); - QString dpath = QDir(QStandardPaths::writableLocation( - QStandardPaths::PicturesLocation)) - .absoluteFilePath(QFileInfo(path).fileName()); - addExtension(contentType, dpath); - //qDebug() << "dpath:" << dpath; - if (QFile::exists(dpath)) { - emit error(801); // image already exists - } else if (QFile::copy(path, dpath)) { - emit imageSaved(QFileInfo(dpath).fileName()); - } else { - emit error(800); // image save error - } + copyImage(path, contentType); }); *conn2 = connect(dm, &DownloadManager::downloadFailed, [this, conn1, conn2](const QString &url) { diff --git a/sailfish/src/fetcher.h b/sailfish/src/fetcher.h index 0fcf2be..ab52197 100644 --- a/sailfish/src/fetcher.h +++ b/sailfish/src/fetcher.h @@ -165,7 +165,7 @@ private: DatabaseManager::ActionsTypes typeUnset, DatabaseManager::ActionsTypes typeSetList, DatabaseManager::ActionsTypes typeUnsetList); - void addExtension(const QString &contentType, QString &path); + void copyImage(const QString &path, const QString &contentType); }; #endif // FETCHER_H diff --git a/sailfish/src/settings.cpp b/sailfish/src/settings.cpp index 0a96b9e..d2a580f 100644 --- a/sailfish/src/settings.cpp +++ b/sailfish/src/settings.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -939,3 +940,23 @@ bool Settings::getIgnoreSslErrors() { return settings.value("ignoresslerrors", false).toBool(); } + +void Settings::setImagesDir(const QString &value) +{ + if (getImagesDir() != value) { + settings.setValue("imagesdir", value); + emit imagesDirChanged(); + } +} + +QString Settings::getImagesDir() +{ + auto dir = settings.value("imagesdir", "").toString(); + QFileInfo d(dir); + if (d.exists() && d.isDir()) { + return dir; + } + + // default is Pictures dir + return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); +} diff --git a/sailfish/src/settings.h b/sailfish/src/settings.h index b1e5b44..d29981d 100644 --- a/sailfish/src/settings.h +++ b/sailfish/src/settings.h @@ -83,7 +83,7 @@ class Settings: public QObject Q_PROPERTY (QString pocketTags READ getPocketTags WRITE setPocketTags NOTIFY pocketTagsChanged) Q_PROPERTY (QString pocketTagsHistory READ getPocketTagsHistory WRITE setPocketTagsHistory NOTIFY pocketTagsHistoryChanged) Q_PROPERTY (bool ignoreSslErrors READ getIgnoreSslErrors WRITE setIgnoreSslErrors NOTIFY ignoreSslErrorsChanged) - + Q_PROPERTY (QString imagesDir READ getImagesDir WRITE setImagesDir NOTIFY imagesDirChanged) public: static Settings* instance(); Fetcher* fetcher; @@ -168,6 +168,9 @@ public: void setOffsetLimit(int value); int getOffsetLimit(); + void setImagesDir(const QString &value); + QString getImagesDir(); + /* View modes: 0 - Tabs->Feeds->Entries @@ -323,6 +326,7 @@ signals: void pocketFavoriteChanged(); void pocketQuickAddChanged(); void ignoreSslErrorsChanged(); + void imagesDirChanged(); /* 501 - Unable create settings dir diff --git a/sailfish/src/utils.cpp b/sailfish/src/utils.cpp index 895546c..c1cacad 100644 --- a/sailfish/src/utils.cpp +++ b/sailfish/src/utils.cpp @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #ifdef SAILFISH #include @@ -825,3 +828,29 @@ void Utils::resetFetcher(int type) #endif } +QString Utils::nameFromPath(const QString &path) +{ + return QFileInfo(path).fileName(); +} + +void Utils::addExtension(const QString &contentType, QString &path) +{ + auto orig_ext = QFileInfo(path).suffix(); + + QString new_ext; + if (contentType == "image/jpeg") { + new_ext = "jpg"; + } else if (contentType == "image/png") { + new_ext = "png"; + } else if (contentType == "image/gif") { + new_ext = "gif"; + } else if (contentType == "image/svg+xml") { + new_ext = "svg"; + } else { + new_ext = orig_ext; + } + + if (new_ext != orig_ext) { + path.append("." + new_ext); + } +} diff --git a/sailfish/src/utils.h b/sailfish/src/utils.h index ed001ad..11bb883 100644 --- a/sailfish/src/utils.h +++ b/sailfish/src/utils.h @@ -23,10 +23,6 @@ #include #include #include -#include -#include -#include -#include #include #ifdef ANDROID @@ -71,11 +67,13 @@ public: //Q_INVOKABLE bool isOnline(); Q_INVOKABLE QString formatHtml(const QString &data, bool offline, const QString &style = QString()); Q_INVOKABLE QString readAsset(const QString &path); + Q_INVOKABLE QString nameFromPath(const QString &path); static QString hash(const QString &url); static int monthsTo(const QDate &from, const QDate &to); static int yearsTo(const QDate &from, const QDate &to); static bool isSameWeek(const QDate &date1, const QDate &date2); + static void addExtension(const QString &contentType, QString &path); Q_INVOKABLE static void log(const QString &data); #ifdef ANDROID