[multimedia/kasts] src: Fix chapter images when images are embedded

Bart De Vries <[email protected]> Tue, 4 Aug 2026 12:39:01 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit ba644130b6326ff034e23bb15304cee6225163d7 by Bart De Vries.
Committed on 04/08/2026 at 12:07.
Pushed by bdevries into branch 'master'.

Fix chapter images when images are embedded

Also, refactor part of the StorageManager to align the cache paths.

M  +0    -1    src/chapter.cpp
M  +6    -8    src/main.cpp
M  +4    -5    src/models/chaptermodel.cpp
M  +16   -1    src/utils/storagemanager.cpp

https://invent.kde.org/multimedia/kasts/-/commit/ba644130b6326ff034e23bb15304cee6225163d7

diff --git a/src/chapter.cpp b/src/chapter.cpp
index 3128562e..f493bb4f 100644
--- a/src/chapter.cpp
+++ b/src/chapter.cpp
@@ -6,7 +6,6 @@
 
 #include "chapter.h"
 
-#include "fetcher.h"
 #include "objectslogging.h"
 
 Chapter::Chapter(Entry *entry, const QString &title, const QString &link, const QString &image, const int &start, QObject *parent)
diff --git a/src/main.cpp b/src/main.cpp
index 7785fae1..6e1c7955 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -71,14 +71,12 @@ class NetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory
 public:
     QNetworkAccessManager *create(QObject *parent) override
     {
-        static QNetworkAccessManager *manager = nullptr;
-        if (!manager) {
-            manager = new QNetworkAccessManager(parent);
-            auto cache = new QNetworkDiskCache(manager);
-            QString directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1StringView("/cacheDir/");
-            cache->setCacheDirectory(directory);
-            manager->setCache(cache);
-        }
+        QNetworkAccessManager *manager = new QNetworkAccessManager(parent);
+        auto cache = new QNetworkDiskCache(manager);
+        QString directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1StringView("/cacheDir/");
+        cache->setCacheDirectory(directory);
+        cache->setMaximumCacheSize(500 * 1024 * 1024);
+        manager->setCache(cache);
         return manager;
     }
 };
diff --git a/src/models/chaptermodel.cpp b/src/models/chaptermodel.cpp
index c50ebfdd..4e12a09c 100644
--- a/src/models/chaptermodel.cpp
+++ b/src/models/chaptermodel.cpp
@@ -193,10 +193,9 @@ void ChapterModel::loadMPEGChapters()
         auto chapterFrame = dynamic_cast<TagLib::ID3v2::ChapterFrame *>(frame);
 
         const auto &apicList = chapterFrame->embeddedFrameListMap()["APIC"];
-        QString image = QStringLiteral("%1,%2").arg(m_entry->id()).arg(chapterFrame->startTime());
-        // TODO: get hashed filename from a method in Fetcher
-        auto hash = QString::fromLatin1(QCryptographicHash::hash(image.toLatin1(), QCryptographicHash::Md5).toHex());
-        auto path = QStringLiteral("%1/images/%2").arg(StorageManager::instance().storagePath(), hash);
+        QString imageName = QStringLiteral("%1,%2").arg(m_entry->id()).arg(chapterFrame->startTime());
+        QString path = StorageManager::instance().imagePath(imageName);
+        QString image = QUrl::fromLocalFile(path).toString();
         if (!apicList.isEmpty()) {
             if (!QFileInfo::exists(path)) {
                 QFile file(path);
@@ -220,7 +219,7 @@ void ChapterModel::loadMPEGChapters()
             return chapter->start() == it->start();
         });
         if (originalChapter != m_chapters.end()) {
-            (*originalChapter)->image() = chapter->image();
+            (*originalChapter)->setImage(chapter->image());
         } else {
             m_chapters << chapter;
         }
diff --git a/src/utils/storagemanager.cpp b/src/utils/storagemanager.cpp
index c58fa245..d99c4a85 100644
--- a/src/utils/storagemanager.cpp
+++ b/src/utils/storagemanager.cpp
@@ -14,6 +14,7 @@
 #include <QDir>
 #include <QFile>
 #include <QFileInfo>
+#include <QNetworkDiskCache>
 #include <QRegularExpression>
 #include <QStandardPaths>
 
@@ -24,6 +25,13 @@
 StorageManager::StorageManager()
 {
     connect(this, &StorageManager::error, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
+
+    // delete old image cache
+    // TODO: can probably be removed in a next version (i.e. after 26.12)
+    QDir oldcache = QDir(storagePath() + QStringLiteral("/images/"));
+    if (oldcache.exists()) {
+        oldcache.removeRecursively();
+    }
 }
 
 QString StorageManager::storagePath() const
@@ -107,7 +115,7 @@ void StorageManager::setStoragePath(QUrl url)
 
 QString StorageManager::imageDirPath() const
 {
-    QString path = storagePath() + QStringLiteral("/images/");
+    QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1StringView("/cacheDir/");
     // Create path if it doesn't exist yet
     QFileInfo().absoluteDir().mkpath(path);
     return path;
@@ -180,6 +188,13 @@ void StorageManager::clearImageCache()
         qCDebug(kastsStorageManager) << image;
         QFile(QDir(imageDirPath()).absoluteFilePath(image)).remove();
     }
+
+    // Also wipe the built-in QNetWorkAccessManager cache
+    QNetworkDiskCache *cache = new QNetworkDiskCache(this);
+    cache->setCacheDirectory(imageDirPath());
+    cache->clear();
+    cache->deleteLater();
+
     Q_EMIT imageDirSizeChanged();
 }