[PictureLoader] Allow saving downloaded images to local storage and not just the QNetworkManager cache (#6620)

* [PictureLoader] Allow saving downloaded images to local storage and not just the QNetworkManager cache.

Took 1 hour 11 minutes

Took 4 seconds


Took 25 seconds

* Give people options from a dropdown.

Took 1 hour 6 minutes

Took 3 seconds

* Simplify directory removal code.

Took 5 minutes

Took 8 seconds

* Merge pull request #8

* Create new category for new settings

* Split off storage settings

Took 47 minutes

Took 4 seconds

* Allow toggling between caching methods.

Took 1 hour 30 minutes

Took 9 seconds

* Adjust settings dialog.

Took 5 minutes

Took 59 seconds

Took 22 seconds

Took 6 seconds

* tr() strings

Took 1 minute

Took 6 seconds

* Readjust layout, default naming scheme.

Took 5 minutes

* Add stretch.

Took 9 minutes

* Make scrollable.

Took 2 minutes

* Add icon.

Took 7 minutes

* Change naming to be uniform.

Took 3 minutes

Took 3 seconds

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
Co-authored-by: RickyRister <42636155+RickyRister@users.noreply.github.com>
This commit is contained in:
BruebachL 2026-05-16 20:15:10 +02:00 committed by GitHub
parent 20cd7ce73d
commit f8ce5c2e39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1382 additions and 132 deletions

View file

@ -150,9 +150,10 @@ void CardPictureLoader::getPixmap(QPixmap &pixmap, const ExactCard &card, QSize
void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
{
QPixmap finalPixmap;
if (image.isNull()) {
qCDebug(CardPictureLoaderLog) << "Caching NULL pixmap for" << card.getName();
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap());
} else {
if (card.getInfo().getUiAttributes().upsideDownArt) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
@ -160,12 +161,19 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
#else
QImage mirrorImage = image.mirrored(true, true);
#endif
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap::fromImage(mirrorImage));
finalPixmap = QPixmap::fromImage(mirrorImage);
} else {
QPixmapCache::insert(card.getPixmapCacheKey(), QPixmap::fromImage(image));
finalPixmap = QPixmap::fromImage(image);
}
}
QPixmapCache::insert(card.getPixmapCacheKey(), finalPixmap);
if (SettingsCache::instance().getCardPictureLoaderCacheMethod() ==
CardPictureLoaderCacheMethod::CacheMethod::FILESYSTEM_CACHE) {
saveCardImageToLocalStorage(card, finalPixmap);
}
// imageLoaded should only be reached if the exactCard isn't already in cache.
// (plus there's a deduplication mechanism in CardPictureLoaderWorker)
// It should be safe to connect the CardInfo here without worrying about redundant connections.
@ -175,6 +183,88 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
card.emitPixmapUpdated();
}
void CardPictureLoader::saveCardImageToLocalStorage(const ExactCard &card, const QPixmap &pixmap)
{
if (pixmap.isNull() || !card) {
return;
}
const QString picsRoot = SettingsCache::instance().getPicsPath();
CardPictureLoaderLocalSchemes::NamingScheme scheme =
SettingsCache::instance().getLocalCardImageStorageNamingScheme();
QString pattern;
for (const auto &s : CardPictureLoaderLocalSchemes::exportSchemes()) {
if (s.id == scheme) {
pattern = s.pattern;
break;
}
}
if (picsRoot.isEmpty() || pattern.isEmpty()) {
return;
}
// Base directory: <picsPath>/downloadedPics
QDir baseDir(picsRoot);
if (!baseDir.exists("downloadedPics")) {
baseDir.mkpath("downloadedPics");
}
baseDir.cd("downloadedPics");
// Collect card metadata
const QString cardName = card.getInfo().getCorrectedName();
QString setName;
QString collectorNumber;
QString uuid;
PrintingInfo printing = card.getPrinting();
if (printing.getSet()) {
setName = printing.getSet()->getCorrectedShortName();
collectorNumber = printing.getProperty("num");
uuid = printing.getUuid();
}
// Build path from scheme
QString relativePath =
CardPictureLoaderLocalSchemes::expandPattern(pattern, cardName, setName, collectorNumber, uuid);
if (relativePath.isEmpty()) {
return;
}
// append extension
relativePath += ".png";
// Normalize slashes
relativePath = QDir::cleanPath(relativePath);
QFileInfo outInfo(baseDir.filePath(relativePath));
// Do not overwrite existing files
if (outInfo.exists()) {
return;
}
QDir outDir = outInfo.dir();
// Ensure directory exists
if (!outDir.exists()) {
if (!baseDir.mkpath(outDir.path())) {
qCWarning(CardPictureLoaderLog) << "Failed to create directory for downloaded card image:" << outDir.path();
return;
}
}
// Save image
QImage image = pixmap.toImage();
if (!image.save(outInfo.absoluteFilePath(), "PNG")) {
qCWarning(CardPictureLoaderLog) << "Failed to save card image to" << outInfo.absoluteFilePath();
}
}
void CardPictureLoader::clearPixmapCache()
{
QPixmapCache::clear();