diff options
| -rw-r--r-- | components/images/CachingImage.qml | 4 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.cpp | 85 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.hpp | 12 | ||||
| -rw-r--r-- | utils/Paths.qml | 4 |
4 files changed, 52 insertions, 53 deletions
diff --git a/components/images/CachingImage.qml b/components/images/CachingImage.qml index 75c9126..add459a 100644 --- a/components/images/CachingImage.qml +++ b/components/images/CachingImage.qml @@ -30,10 +30,8 @@ Image { onStatusChanged: { if (source == cachePath && status === Image.Error) source = path; - else if (source == path && status === Image.Ready) { - Paths.mkdir(Paths.imagecache); + else if (source == path && status === Image.Ready) CUtils.saveItem(this, cachePath); - } } Process { diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index 1285c55..fd7fcdd 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -5,61 +5,66 @@ #include <QtQuick/QQuickItemGrabResult> #include <QThreadPool> #include <QQmlEngine> +#include <QDir> -void CUtils::saveItem(QQuickItem* target, const QUrl& path) { +void CUtils::saveItem(QQuickItem* target, const QUrl& path) const { this->saveItem(target, path, QRect(), QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) { - this->saveItem(target, path, rect, QJSValue(), QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const { + this->saveItem(target, path, rect, QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) { - this->saveItem(target, path, QRect(), onSaved, QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const { + this->saveItem(target, path, QRect(), onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) { - this->saveItem(target, path, QRect(), onSaved, onFailed); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const { + this->saveItem(target, path, QRect(), onSaved, onFailed); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) { - this->saveItem(target, path, rect, onSaved, QJSValue()); +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const { + this->saveItem(target, path, rect, onSaved, QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) { - if (!target) { - qWarning() << "CUtils::saveItem: a target is required"; - return; - } +void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const { + if (!target) { + qWarning() << "CUtils::saveItem: a target is required"; + return; + } - if (!path.isLocalFile()) { - qWarning() << "CUtils::saveItem:" << path << "is not a local file"; - return; - } + if (!path.isLocalFile()) { + qWarning() << "CUtils::saveItem:" << path << "is not a local file"; + return; + } - QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); + QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); - QObject::connect( - grabResult.data(), - &QQuickItemGrabResult::ready, - this, - [grabResult, rect, path, onSaved, onFailed, this]() { - QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { - QImage image = grabResult->image(); + QObject::connect( + grabResult.data(), + &QQuickItemGrabResult::ready, + this, + [grabResult, rect, path, onSaved, onFailed, this]() { + QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { + QImage image = grabResult->image(); - if (!rect.isEmpty()) { - image = image.copy(rect); - } + if (!rect.isEmpty()) { + image = image.copy(rect); + } - const QString file = path.toLocalFile(); - if (image.save(file)) { - if (onSaved.isCallable()) { - onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - } else if (onFailed.isCallable()) { - onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - }); - } - ); + const QString file = path.toLocalFile(); + const QString parent = QFileInfo(file).absolutePath(); + if (QDir().mkpath(parent) && image.save(file)) { + if (onSaved.isCallable()) { + onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } + } else { + qWarning() << "CUtils::saveItem: failed to save" << path; + if (onFailed.isCallable()) { + onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); + } + } + }); + } + ); } diff --git a/plugin/src/Caelestia/cutils.hpp b/plugin/src/Caelestia/cutils.hpp index bf7cb22..e50cab0 100644 --- a/plugin/src/Caelestia/cutils.hpp +++ b/plugin/src/Caelestia/cutils.hpp @@ -9,10 +9,10 @@ class CUtils : public QObject { QML_SINGLETON; public: - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved); - Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed); + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path) const; + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const; + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const; + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const; + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const; + Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const; }; diff --git a/utils/Paths.qml b/utils/Paths.qml index b45115d..71be877 100644 --- a/utils/Paths.qml +++ b/utils/Paths.qml @@ -38,10 +38,6 @@ Singleton { return stringify(path).replace("file://", ""); } - function mkdir(path: url): void { - Quickshell.execDetached(["mkdir", "-p", strip(path)]); - } - function copy(from: url, to: url): void { Quickshell.execDetached(["cp", strip(from), strip(to)]); } |