Cockatrice 2025-11-30-Development-2.11.0-beta.38
A cross-platform virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
card_picture_loader_worker.h
Go to the documentation of this file.
1#ifndef PICTURE_LOADER_WORKER_H
2#define PICTURE_LOADER_WORKER_H
3
7
8#include <QLoggingCategory>
9#include <QMutex>
10#include <QNetworkAccessManager>
11#include <QNetworkDiskCache>
12#include <QObject>
13#include <QQueue>
14#include <QTimer>
17
18#define REDIRECT_HEADER_NAME "redirects"
19#define REDIRECT_ORIGINAL_URL "original"
20#define REDIRECT_URL "redirect"
21#define REDIRECT_TIMESTAMP "timestamp"
22#define REDIRECT_CACHE_FILENAME "cache.ini"
23
24inline Q_LOGGING_CATEGORY(CardPictureLoaderWorkerLog, "card_picture_loader.worker")
25
27
40class CardPictureLoaderWorker : public QObject
41{
42 Q_OBJECT
43
44public:
50 explicit CardPictureLoaderWorker();
51
52 ~CardPictureLoaderWorker() override;
53
60 void enqueueImageLoad(const ExactCard &card);
61
67 void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker);
68
70 void clearNetworkCache();
71
72public slots:
79 QNetworkReply *makeRequest(const QUrl &url, CardPictureLoaderWorkerWork *workThread);
80
83
89
95 void handleImageLoaded(const ExactCard &card, const QImage &image);
96
98 void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl);
99
101 void removedCachedUrl(const QUrl &url);
102
103private:
105 QNetworkAccessManager *networkManager;
106 QNetworkDiskCache *cache;
107 QHash<QUrl, QPair<QUrl, QDateTime>> redirectCache;
109 static constexpr int CacheTTLInDays = 30;
111 QQueue<QPair<QUrl, CardPictureLoaderWorkerWork *>> requestLoadQueue;
112
115
117 QSet<QString> currentlyLoading;
118
120 [[nodiscard]] QUrl getCachedRedirect(const QUrl &originalUrl) const;
121
123 void loadRedirectCache();
124
126 void saveRedirectCache() const;
127
129 void cleanStaleEntries();
130
131private slots:
133 void resetRequestQuota();
134
136 void handleImageLoadEnqueued(const ExactCard &card);
137
138signals:
140 void imageLoadEnqueued(const ExactCard &card);
141
143 void imageLoaded(const ExactCard &card, const QImage &image);
144
146 void imageRequestQueued(const QUrl &url, const ExactCard &card, const QString &setName);
147
149 void imageRequestSucceeded(const QUrl &url);
150};
151
152#endif // PICTURE_LOADER_WORKER_H
Q_LOGGING_CATEGORY(CardPictureLoaderWorkerLog, "card_picture_loader.worker") class CardPictureLoaderWorkerWork
Handles searching for and loading card images from local and custom image folders.
Definition card_picture_loader_local.h:23
Handles downloading a single card image from network or local sources.
Definition card_picture_loader_worker_work.h:31
QQueue< QPair< QUrl, CardPictureLoaderWorkerWork * > > requestLoadQueue
Queue of pending network requests.
Definition card_picture_loader_worker.h:111
QNetworkDiskCache * cache
Disk cache for downloaded images.
Definition card_picture_loader_worker.h:106
void clearNetworkCache()
Clears the network cache and redirect cache.
Definition card_picture_loader_worker.cpp:226
void cleanStaleEntries()
Removes stale redirect entries older than TTL.
Definition card_picture_loader_worker.cpp:212
int requestQuota
Remaining requests allowed per second.
Definition card_picture_loader_worker.h:113
void imageRequestQueued(const QUrl &url, const ExactCard &card, const QString &setName)
Emitted when a request is added to the network queue.
void saveRedirectCache() const
Saves redirect cache to disk.
Definition card_picture_loader_worker.cpp:197
void imageRequestSucceeded(const QUrl &url)
Emitted when a network request successfully completes.
QString cacheFilePath
Path to persistent redirect cache file.
Definition card_picture_loader_worker.h:108
QSet< QString > currentlyLoading
Deduplication: contains pixmapCacheKey currently being loaded.
Definition card_picture_loader_worker.h:117
void resetRequestQuota()
Resets the request quota for rate-limiting.
Definition card_picture_loader_worker.cpp:101
void processQueuedRequests()
Processes all queued requests respecting the request quota.
Definition card_picture_loader_worker.cpp:107
CardPictureLoaderLocal * localLoader
Loader for local images.
Definition card_picture_loader_worker.h:116
QUrl getCachedRedirect(const QUrl &originalUrl) const
Returns cached redirect URL for the given original URL, if available.
Definition card_picture_loader_worker.cpp:170
void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker)
Queues a network request for a given URL and worker thread.
Definition card_picture_loader_worker.cpp:64
QThread * pictureLoaderThread
Thread for executing worker tasks.
Definition card_picture_loader_worker.h:104
bool picDownload
Whether downloading images from network is enabled.
Definition card_picture_loader_worker.h:110
void removedCachedUrl(const QUrl &url)
Removes a URL from the network cache.
Definition card_picture_loader_worker.cpp:165
static constexpr int CacheTTLInDays
Time-to-live for redirect cache entries (days)
Definition card_picture_loader_worker.h:109
void imageLoadEnqueued(const ExactCard &card)
Emitted when an image load is enqueued.
bool processSingleRequest()
Processes a single queued request.
Definition card_picture_loader_worker.cpp:114
void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl)
Caches a redirect mapping between original and redirected URL.
Definition card_picture_loader_worker.cpp:159
void handleImageLoadEnqueued(const ExactCard &card)
Handles image load requests enqueued on this worker.
Definition card_picture_loader_worker.cpp:130
QTimer requestTimer
Timer to reset the request quota.
Definition card_picture_loader_worker.h:114
QNetworkAccessManager * networkManager
Network manager for HTTP requests.
Definition card_picture_loader_worker.h:105
void enqueueImageLoad(const ExactCard &card)
Enqueues an ExactCard for loading.
Definition card_picture_loader_worker.cpp:124
CardPictureLoaderWorker()
Constructs a CardPictureLoaderWorker.
Definition card_picture_loader_worker.cpp:16
void loadRedirectCache()
Loads redirect cache from disk.
Definition card_picture_loader_worker.cpp:178
QHash< QUrl, QPair< QUrl, QDateTime > > redirectCache
Maps original URLs to redirects with timestamp.
Definition card_picture_loader_worker.h:107
QNetworkReply * makeRequest(const QUrl &url, CardPictureLoaderWorkerWork *workThread)
Makes a network request for the given URL using the specified worker.
Definition card_picture_loader_worker.cpp:79
void imageLoaded(const ExactCard &card, const QImage &image)
Emitted when an image has finished loading.
void handleImageLoaded(const ExactCard &card, const QImage &image)
Handles an image that has finished loading.
Definition card_picture_loader_worker.cpp:153
Represents a specific card instance, defined by its CardInfo and a particular printing.
Definition exact_card.h:19