From 573423742a77816699f219817ba462c7f6f5d33a Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Wed, 27 Aug 2025 22:13:21 +1000 Subject: plugin: make thread safe --- plugin/src/Caelestia/cachingimagemanager.cpp | 41 ++++++++++++++-------------- plugin/src/Caelestia/cutils.cpp | 39 +++++++++++++------------- plugin/src/Caelestia/cutils.hpp | 12 ++++---- 3 files changed, 46 insertions(+), 46 deletions(-) (limited to 'plugin') diff --git a/plugin/src/Caelestia/cachingimagemanager.cpp b/plugin/src/Caelestia/cachingimagemanager.cpp index d549e8c..d95af27 100644 --- a/plugin/src/Caelestia/cachingimagemanager.cpp +++ b/plugin/src/Caelestia/cachingimagemanager.cpp @@ -70,34 +70,33 @@ void CachingImageManager::setPath(const QString& path) { if (!path.isEmpty()) { QThreadPool::globalInstance()->start([path, this] { - const QString filename = QString("%1@%2x%3.png") - .arg(sha256sum(path)) - .arg(effectiveWidth()) - .arg(effectiveHeight()); + const QString sha = sha256sum(path); - m_cachePath = m_cacheDir.resolved(QUrl(filename)); - emit cachePathChanged(); + QMetaObject::invokeMethod(this, [path, sha, this]() { + const QString filename = QString("%1@%2x%3.png") + .arg(sha) + .arg(effectiveWidth()) + .arg(effectiveHeight()); - if (!m_cachePath.isLocalFile()) { - qWarning() << "CachingImageManager::setPath: cachePath" << m_cachePath << "is not a local file"; - return; - } + m_cachePath = m_cacheDir.resolved(QUrl(filename)); + emit cachePathChanged(); - if (QFile::exists(m_cachePath.toLocalFile())) { - QMetaObject::invokeMethod(m_item, [this]() { - m_item->setProperty("source", m_cachePath); - }, Qt::QueuedConnection); + if (!m_cachePath.isLocalFile()) { + qWarning() << "CachingImageManager::setPath: cachePath" << m_cachePath << "is not a local file"; + return; + } - m_usingCache = true; - emit usingCacheChanged(); - } else { - QMetaObject::invokeMethod(m_item, [path, this]() { + bool cacheExists = QFile::exists(m_cachePath.toLocalFile()); + + if (cacheExists) { + m_item->setProperty("source", m_cachePath); + } else { m_item->setProperty("source", QUrl::fromLocalFile(path)); - }, Qt::QueuedConnection); + } - m_usingCache = false; + m_usingCache = cacheExists; emit usingCacheChanged(); - } + }); }); } } diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index 2415f80..dfe5ee8 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -7,27 +7,27 @@ #include #include -void CUtils::saveItem(QQuickItem* target, const QUrl& path) const { +void CUtils::saveItem(QQuickItem* target, const QUrl& path) { this->saveItem(target, path, QRect(), QJSValue(), QJSValue()); } -void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const { +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, QJSValue onSaved) const { +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, QJSValue onFailed) const { +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, const QRect& rect, QJSValue onSaved) const { +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, QJSValue onFailed) const { +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; @@ -40,10 +40,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q QSharedPointer grabResult = target->grabToImage(); - QObject::connect( - grabResult.data(), - &QQuickItemGrabResult::ready, - this, + 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(); @@ -54,16 +51,20 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q 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)) }); + bool success = QDir().mkpath(parent) && image.save(file); + + QMetaObject::invokeMethod(this, [file, success, path, onSaved, onFailed, this]() { + if (success) { + 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)) }); + } } - } else { - qWarning() << "CUtils::saveItem: failed to save" << path; - if (onFailed.isCallable()) { - onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); - } - } + }, Qt::QueuedConnection); }); } ); diff --git a/plugin/src/Caelestia/cutils.hpp b/plugin/src/Caelestia/cutils.hpp index 76beb2b..c796e8b 100644 --- a/plugin/src/Caelestia/cutils.hpp +++ b/plugin/src/Caelestia/cutils.hpp @@ -10,12 +10,12 @@ class CUtils : public QObject { QML_SINGLETON; public: - 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; + 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 bool copyFile(const QUrl& source, const QUrl& target) const; Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const; -- cgit v1.2.3-freya