1#ifndef PICTURE_LOADER_WORKER_H
2#define PICTURE_LOADER_WORKER_H
10#include <QLoggingCategory>
12#include <QNetworkAccessManager>
13#include <QNetworkDiskCache>
20#define REDIRECT_HEADER_NAME "redirects"
21#define REDIRECT_ORIGINAL_URL "original"
22#define REDIRECT_URL "redirect"
23#define REDIRECT_TIMESTAMP "timestamp"
24#define REDIRECT_CACHE_FILENAME "cache.ini"
106 void cacheRedirect(
const QUrl &originalUrl,
const QUrl &redirectUrl);
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:34
QQueue< QPair< QUrl, CardPictureLoaderWorkerWork * > > requestLoadQueue
Queue of pending network requests.
Definition card_picture_loader_worker.h:119
QNetworkDiskCache * cache
Disk cache for downloaded images.
Definition card_picture_loader_worker.h:114
void clearNetworkCache()
Clears the network cache and redirect cache.
Definition card_picture_loader_worker.cpp:276
void cleanStaleEntries()
Removes stale redirect entries older than TTL.
Definition card_picture_loader_worker.cpp:262
QHash< QString, int > hostRequestQuota
Sustained per-host request allowance.
Definition card_picture_loader_worker.h:123
int requestQuota
Remaining requests allowed per second.
Definition card_picture_loader_worker.h:121
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:247
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:116
QSet< QString > currentlyLoading
Deduplication: contains pixmapCacheKey currently being loaded.
Definition card_picture_loader_worker.h:128
void resetRequestQuota()
Resets the request quota for rate-limiting.
Definition card_picture_loader_worker.cpp:126
void processQueuedRequests()
Processes all queued requests respecting the request quota.
Definition card_picture_loader_worker.cpp:145
CardPictureLoaderLocal * localLoader
Loader for local images.
Definition card_picture_loader_worker.h:127
QUrl getCachedRedirect(const QUrl &originalUrl) const
Returns cached redirect URL for the given original URL, if available.
Definition card_picture_loader_worker.cpp:220
void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker)
Queues a network request for a given URL and worker thread.
Definition card_picture_loader_worker.cpp:76
void onHostRateLimited(const QString &host)
Handles a server returning HTTP 429 by reducing that host's request quota.
Definition card_picture_loader_worker.cpp:168
QThread * pictureLoaderThread
Thread for executing worker tasks.
Definition card_picture_loader_worker.h:112
QHash< QString, int > hostQuotaRemaining
Per-host allowance left in the current second.
Definition card_picture_loader_worker.h:124
QHash< QString, QDateTime > hostLast429
When each host was last rate limited.
Definition card_picture_loader_worker.h:125
bool picDownload
Whether downloading images from network is enabled.
Definition card_picture_loader_worker.h:118
void removedCachedUrl(const QUrl &url)
Removes a URL from the network cache.
Definition card_picture_loader_worker.cpp:215
static constexpr int CacheTTLInDays
Time-to-live for redirect cache entries (days).
Definition card_picture_loader_worker.h:117
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:152
void cacheRedirect(const QUrl &originalUrl, const QUrl &redirectUrl)
Caches a redirect mapping between original and redirected URL.
Definition card_picture_loader_worker.cpp:209
void handleImageLoadEnqueued(const ExactCard &card)
Handles image load requests enqueued on this worker.
Definition card_picture_loader_worker.cpp:180
QTimer requestTimer
Timer to reset the request quota.
Definition card_picture_loader_worker.h:122
QNetworkAccessManager * networkManager
Network manager for HTTP requests.
Definition card_picture_loader_worker.h:113
void enqueueImageLoad(const ExactCard &card)
Enqueues an ExactCard for loading.
Definition card_picture_loader_worker.cpp:174
CardPictureLoaderWorker()
Constructs a CardPictureLoaderWorker.
Definition card_picture_loader_worker.cpp:23
void loadRedirectCache()
Loads redirect cache from disk.
Definition card_picture_loader_worker.cpp:228
QHash< QUrl, QPair< QUrl, QDateTime > > redirectCache
Maps original URLs to redirects with timestamp.
Definition card_picture_loader_worker.h:115
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:97
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:203
Represents a specific card instance, defined by its CardInfo and a particular printing.
Definition exact_card.h:19