From eca74114a27d1208e9ee4bcb8dcd0be5941f9f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Fri, 22 Nov 2024 18:33:15 +0100 Subject: [PATCH] Load and store redirects properly. --- cockatrice/src/client/ui/picture_loader.cpp | 8 ++++++-- cockatrice/src/settings/cache_settings.cpp | 1 + cockatrice/src/settings/cache_settings.h | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/client/ui/picture_loader.cpp b/cockatrice/src/client/ui/picture_loader.cpp index 873556747..ba37a8448 100644 --- a/cockatrice/src/client/ui/picture_loader.cpp +++ b/cockatrice/src/client/ui/picture_loader.cpp @@ -145,10 +145,13 @@ PictureLoaderWorker::PictureLoaderWorker() networkManager->setRedirectPolicy(QNetworkRequest::ManualRedirectPolicy); connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *))); - cacheFilePath = "redirectCache.ini"; + cacheFilePath = SettingsCache::instance().getRedirectCachePath() + "cacheSettings.ini"; loadRedirectCache(); cleanStaleEntries(); + connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, + &PictureLoaderWorker::saveRedirectCache); + pictureLoaderThread = new QThread; pictureLoaderThread->start(QThread::LowPriority); moveToThread(pictureLoaderThread); @@ -156,7 +159,6 @@ PictureLoaderWorker::PictureLoaderWorker() PictureLoaderWorker::~PictureLoaderWorker() { - saveRedirectCache(); pictureLoaderThread->deleteLater(); } @@ -489,6 +491,7 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url) void PictureLoaderWorker::cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl) { redirectCache[originalUrl] = qMakePair(redirectUrl, QDateTime::currentDateTimeUtc()); + saveRedirectCache(); } QUrl PictureLoaderWorker::getCachedRedirect(const QUrl &originalUrl) const @@ -520,6 +523,7 @@ void PictureLoaderWorker::loadRedirectCache() void PictureLoaderWorker::saveRedirectCache() const { + qDebug() << "Saving redirect cache"; QSettings settings(cacheFilePath, QSettings::IniFormat); settings.beginWriteArray("redirects", redirectCache.size()); diff --git a/cockatrice/src/settings/cache_settings.cpp b/cockatrice/src/settings/cache_settings.cpp index 05de9fd48..176f74f9b 100644 --- a/cockatrice/src/settings/cache_settings.cpp +++ b/cockatrice/src/settings/cache_settings.cpp @@ -1030,6 +1030,7 @@ void SettingsCache::loadPaths() replaysPath = getSafeConfigPath("paths/replays", dataPath + "/replays/"); themesPath = getSafeConfigPath("paths/themes", dataPath + "/themes/"); picsPath = getSafeConfigPath("paths/pics", dataPath + "/pics/"); + redirectCachePath = getSafeConfigPath("paths/redirects", dataPath + "/pics/redirects/"); // this has never been exposed as an user-configurable setting if (picsPath.endsWith("/")) { customPicsPath = getSafeConfigPath("paths/custompics", picsPath + "CUSTOM/"); diff --git a/cockatrice/src/settings/cache_settings.h b/cockatrice/src/settings/cache_settings.h index e0bf8b9ed..61607fdf6 100644 --- a/cockatrice/src/settings/cache_settings.h +++ b/cockatrice/src/settings/cache_settings.h @@ -73,7 +73,7 @@ private: QByteArray tokenDialogGeometry; QByteArray setsDialogGeometry; QString lang; - QString deckPath, replaysPath, picsPath, customPicsPath, cardDatabasePath, customCardDatabasePath, themesPath, + QString deckPath, replaysPath, picsPath, redirectCachePath, customPicsPath, cardDatabasePath, customCardDatabasePath, themesPath, spoilerDatabasePath, tokenDatabasePath, themeName; bool notifyAboutUpdates; bool notifyAboutNewVersion; @@ -183,6 +183,10 @@ public: { return picsPath; } + QString getRedirectCachePath() const + { + return redirectCachePath; + } QString getCustomPicsPath() const { return customPicsPath;