Cockatrice 2026-10-19-Development-3.1.2-beta.2
A 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
4#include <QHash>
5#include <QLoggingCategory>
6#include <QObject>
7#include <QPair>
8#include <QQueue>
9#include <QSet>
10#include <QString>
11#include <QTimer>
12#include <QUrl>
13#include <qtmetamacros.h>
14
16class ExactCard;
17class QDateTime;
18class QImage;
19class QNetworkAccessManager;
20class QNetworkDiskCache;
21class QNetworkReply;
22class QThread;
23
24#define REDIRECT_HEADER_NAME "redirects"
25#define REDIRECT_ORIGINAL_URL "original"
26#define REDIRECT_URL "redirect"
27#define REDIRECT_TIMESTAMP "timestamp"
28#define REDIRECT_CACHE_FILENAME "cache.ini"
29
30inline Q_LOGGING_CATEGORY(CardPictureLoaderWorkerLog, "card_picture_loader.worker")
31
33
46class CardPictureLoaderWorker : public QObject
47{
48 Q_OBJECT
49
50public:
56 explicit CardPictureLoaderWorker();
57
58 ~CardPictureLoaderWorker() override;
59
66 void enqueueImageLoad(const ExactCard &card);
67
73 void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker);
74
79 void onHostRateLimited(const QString &host);
80
94 [[nodiscard]] bool shutdownThread();
95
102 QThread *workerThread() const;
103
104public slots:
111 void clearNetworkCache();
118 QNetworkReply *makeRequest(const QUrl &url, CardPictureLoaderWorkerWork *workThread);
119
122
125
131
137 void handleImageLoaded(const ExactCard &card, const QImage &image);
138
140 void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl);
141
143 void removedCachedUrl(const QUrl &url);
144
145private:
147 QNetworkAccessManager *networkManager;
148 QNetworkDiskCache *cache;
151 static constexpr int CacheTTLInDays = 30;
153 QQueue<QPair<QUrl, CardPictureLoaderWorkerWork *>> requestLoadQueue;
154
162
164 static constexpr int MAX_IN_FLIGHT_PER_HOST = 6;
165
167 static constexpr int MAX_REDIRECT_CHAIN_DEPTH = 10;
168
171
178 [[nodiscard]] int hostAllowanceCeiling(const QString &host) const;
179
186 [[nodiscard]] bool isUnlockedHost(const QString &host) const;
187
189 [[nodiscard]] QUrl getCachedRedirect(const QUrl &originalUrl) const;
190
193 [[nodiscard]] bool requestTouchesNetwork(const QUrl &url) const;
194
203 [[nodiscard]] QUrl resolveCachedRedirect(const QUrl &url) const;
204
206 void loadRedirectCache();
207
209 void saveRedirectCache() const;
210
212 void cleanStaleEntries();
213
215 void updateTimerState();
216
217private slots:
219 void resetRequestQuota();
220
222 void handleImageLoadEnqueued(const ExactCard &card);
223
224signals:
226 void imageLoadEnqueued(const ExactCard &card);
227
229 void imageLoaded(const ExactCard &card, const QImage &image);
230
232 void imageRequestQueued(const QUrl &url, const ExactCard &card, const QString &setName);
233
235 void imageRequestSucceeded(const QUrl &url);
236
239};
240
241#endif // PICTURE_LOADER_WORKER_H
Handles searching for and loading card images from local and custom image folders.
Definition card_picture_loader_local.h:30
Handles downloading a single card image from network or local sources.
Definition card_picture_loader_worker_work.h:34
QQueue< QPair< QUrl, CardPictureLoaderWorkerWork * > > requestLoadQueue
Queue of pending network requests.
Definition card_picture_loader_worker.h:153
QNetworkDiskCache * cache
Disk cache for downloaded images.
Definition card_picture_loader_worker.h:148
void clearNetworkCache()
Clears the network cache and redirect cache.
Definition card_picture_loader_worker.cpp:557
void cleanStaleEntries()
Removes stale redirect entries older than TTL.
Definition card_picture_loader_worker.cpp:543
QHash< QString, int > hostRequestQuota
Sustained per-host request allowance.
Definition card_picture_loader_worker.h:157
void imageRequestQueued(const QUrl &url, const ExactCard &card, const QString &setName)
Emitted when a request is added to the network queue.
QTimer dispatchTimer
Timer pacing individual network requests.
Definition card_picture_loader_worker.h:156
void dispatchQueuedRequest()
Chooses a request from the queue and starts it, respecting the quota and pacing.
Definition card_picture_loader_worker.cpp:251
void saveRedirectCache() const
Saves redirect cache to disk.
Definition card_picture_loader_worker.cpp:528
bool isUnlockedHost(const QString &host) const
Whether a host may skip dispatch pacing and per-host allowance entirely.
Definition card_picture_loader_worker.cpp:422
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:150
static constexpr int MAX_REDIRECT_CHAIN_DEPTH
Bound on how many cached-redirect hops dispatch resolution will follow.
Definition card_picture_loader_worker.h:167
QSet< QString > currentlyLoading
Deduplication: contains pixmapCacheKey currently being loaded.
Definition card_picture_loader_worker.h:170
void resetRequestQuota()
Resets the request quota for rate-limiting.
Definition card_picture_loader_worker.cpp:210
void processQueuedRequests()
Ensures the pacing and quota-reset timers reflect the current queue and recovery state.
Definition card_picture_loader_worker.cpp:240
CardPictureLoaderLocal * localLoader
Loader for local images.
Definition card_picture_loader_worker.h:169
QUrl getCachedRedirect(const QUrl &originalUrl) const
Returns cached redirect URL for the given original URL, if available.
Definition card_picture_loader_worker.cpp:485
bool shutdownThread()
Stops the worker thread and reports whether it stopped.
Definition card_picture_loader_worker.cpp:110
void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker)
Queues a network request for a given URL and worker thread.
Definition card_picture_loader_worker.cpp:137
void onHostRateLimited(const QString &host)
Handles a server returning HTTP 429 by reducing that host's request quota.
Definition card_picture_loader_worker.cpp:427
QThread * pictureLoaderThread
Thread for executing worker tasks.
Definition card_picture_loader_worker.h:146
QHash< QString, int > hostQuotaRemaining
Per-host allowance left in the current second.
Definition card_picture_loader_worker.h:159
void updateTimerState()
Starts or stops the pacing and quota-reset timers to match the queue and recovery state.
Definition card_picture_loader_worker.cpp:310
QHash< QString, QDateTime > hostLast429
When each host was last rate limited.
Definition card_picture_loader_worker.h:160
bool picDownload
Whether downloading images from network is enabled.
Definition card_picture_loader_worker.h:152
void removedCachedUrl(const QUrl &url)
Removes a URL from the network cache.
Definition card_picture_loader_worker.cpp:480
QThread * workerThread() const
Returns the worker's QThread.
Definition card_picture_loader_worker.cpp:132
static constexpr int CacheTTLInDays
Time-to-live for redirect cache entries (days).
Definition card_picture_loader_worker.h:151
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:344
bool requestTouchesNetwork(const QUrl &url) const
Whether a request for this URL would actually touch the network, rather than being served from the di...
Definition card_picture_loader_worker.cpp:403
void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl)
Caches a redirect mapping between original and redirected URL.
Definition card_picture_loader_worker.cpp:474
static constexpr int MAX_IN_FLIGHT_PER_HOST
Maximum concurrent in-flight network replies per host.
Definition card_picture_loader_worker.h:164
void handleImageLoadEnqueued(const ExactCard &card)
Handles image load requests enqueued on this worker.
Definition card_picture_loader_worker.cpp:445
void networkCacheCleared()
Emitted after clearNetworkCache() has finished clearing both caches.
QHash< QString, int > hostRequestLimits
User-set per-host request allowances.
Definition card_picture_loader_worker.h:158
QTimer requestTimer
Timer to reset the request quota.
Definition card_picture_loader_worker.h:155
QNetworkAccessManager * networkManager
Network manager for HTTP requests.
Definition card_picture_loader_worker.h:147
void enqueueImageLoad(const ExactCard &card)
Enqueues an ExactCard for loading.
Definition card_picture_loader_worker.cpp:439
CardPictureLoaderWorker()
Constructs a CardPictureLoaderWorker.
Definition card_picture_loader_worker.cpp:45
int hostAllowanceCeiling(const QString &host) const
Effective per-host allowance ceiling for a host.
Definition card_picture_loader_worker.cpp:412
void loadRedirectCache()
Loads redirect cache from disk.
Definition card_picture_loader_worker.cpp:509
QUrl resolveCachedRedirect(const QUrl &url) const
Follows the cached-redirect chain to the URL that will actually be requested.
Definition card_picture_loader_worker.cpp:493
QHash< QUrl, QPair< QUrl, QDateTime > > redirectCache
Maps original URLs to redirects with timestamp.
Definition card_picture_loader_worker.h:149
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:158
QHash< QString, int > hostInFlight
Network replies currently in flight, per host.
Definition card_picture_loader_worker.h:161
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:468
Represents a specific card instance, defined by its CardInfo and a particular printing.
Definition exact_card.h:24
Definition mana_curve_category_widget.h:11
Definition dlg_manage_sets.h:29
Q_LOGGING_CATEGORY(CrashReportLog, "crash_report")