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 ++++++++++++++-------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'plugin/src/Caelestia/cachingimagemanager.cpp') 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(); - } + }); }); } } -- cgit v1.2.3-freya