Skip to content

Commit

Permalink
DlgTagFetcher: cache fetched covers in QMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Nov 15, 2023
1 parent 607c171 commit cf6c689
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/library/dlgcoverartfullsize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ void DlgCoverArtFullSize::init(TrackPointer pTrack) {
slotLoadTrack(pTrack);
}

void DlgCoverArtFullSize::initFetchedCoverArt(const QByteArray& fetchedCoverArtBytes) {
m_pixmap.loadFromData(fetchedCoverArtBytes);
void DlgCoverArtFullSize::initFetchedCoverArt(const QPixmap& pixmap) {
m_pixmap = pixmap;

// The real size will be calculated later by adjustImageAndDialogSize().
resize(100, 100);
Expand Down
2 changes: 1 addition & 1 deletion src/library/dlgcoverartfullsize.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DlgCoverArtFullSize
~DlgCoverArtFullSize() override = default;

void init(TrackPointer pTrack);
void initFetchedCoverArt(const QByteArray& fetchedCoverArtBytes);
void initFetchedCoverArt(const QPixmap& fetchedCoverArtBytes);
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* ) override;
void mouseMoveEvent(QMouseEvent* ) override;
Expand Down
78 changes: 65 additions & 13 deletions src/library/dlgtagfetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void DlgTagFetcher::init() {
connect(&m_tagFetcher,
&TagFetcher::coverArtImageFetchAvailable,
this,
&DlgTagFetcher::slotLoadBytesToLabel);
&DlgTagFetcher::slotLoadFetchedCoverArt);

connect(&m_tagFetcher,
&TagFetcher::coverArtLinkNotFound,
Expand Down Expand Up @@ -231,6 +231,11 @@ void DlgTagFetcher::loadTrack(const TrackPointer& pTrack) {

m_pWFetchedCoverArtLabel->setCoverArt(CoverInfo{}, QPixmap{});

qWarning() << " .";
qWarning() << " load track, clear cache";
m_coverCache.clear();
qWarning() << " .";

m_pTrack = pTrack;
if (!m_pTrack) {
return;
Expand Down Expand Up @@ -527,13 +532,31 @@ void DlgTagFetcher::tagSelected() {
m_data.m_selectedTag = tagIndex;

m_fetchedCoverArtByteArrays.clear();
m_pWFetchedCoverArtLabel->loadData(QByteArray());
m_pWFetchedCoverArtLabel->setCoverArt(CoverInfo{},
QPixmap(CoverArtUtils::defaultCoverLocation()));
btnApplyCover->setDisabled(true);

const mixxx::musicbrainz::TrackRelease& trackRelease = m_data.m_tags[tagIndex];
QUuid selectedTagAlbumId = trackRelease.albumReleaseId;

// Check if we already fetched the cover for this release earlier
QString cacheKey = selectedTagAlbumId.toString();
auto it = m_coverCache.find(cacheKey);
if (it != m_coverCache.constEnd()) {

Check failure on line 545 in src/library/dlgtagfetcher.cpp

View workflow job for this annotation

GitHub Actions / clazy

Mixing iterators with const_iterators [-Wclazy-strict-iterators]
qWarning() << " .";
qWarning() << " found cached cover for release" << cacheKey;
QPixmap pix = it.value();
qWarning() << " pix is null?" << pix.isNull() << pix.size();
qWarning() << " load";
loadPixmapToLabel(pix);
qWarning() << " .";
return;
} else {
qWarning() << " .";
qWarning() << " no cache hit, request cover links";
qWarning() << " .";
}

statusMessage->setVisible(false);

loadingProgressBar->setFormat(tr("Looking for cover art"));
Expand All @@ -555,7 +578,8 @@ void DlgTagFetcher::slotCoverFound(
}
}

void DlgTagFetcher::slotStartFetchCoverArt(const QList<QString>& allUrls) {
void DlgTagFetcher::slotStartFetchCoverArt(const QUuid& albumReleaseId,
const QList<QString>& allUrls) {
const int fetcherQuality = m_pConfig->getValue(
mixxx::library::prefs::kCoverArtFetcherQualityConfigKey,
static_cast<int>(DlgPrefLibrary::CoverArtFetcherQuality::Medium));
Expand All @@ -578,35 +602,63 @@ void DlgTagFetcher::slotStartFetchCoverArt(const QList<QString>& allUrls) {
return;
}

// TODO(ronso0) Check if we already fetched the cover for the preferred resolution

QString coverUrl;
if (allUrls.size() > fetcherQuality) {
getCoverArt(allUrls.at(fetcherQuality));
coverUrl = allUrls.at(fetcherQuality);
} else {
getCoverArt(allUrls.last());
coverUrl = allUrls.last();
}
getCoverArt(albumReleaseId, coverUrl);
}

void DlgTagFetcher::slotLoadBytesToLabel(const QByteArray& data) {
void DlgTagFetcher::slotLoadFetchedCoverArt(const QUuid& albumReleaseId,
const QByteArray& data) {
QPixmap fetchedCoverArtPixmap;
fetchedCoverArtPixmap.loadFromData(data);

// Bytes to be eventually applied as track cover art
m_fetchedCoverArtByteArrays = data;

// Cache the fetched cover image
qWarning() << " .";
qWarning() << " cache size:" << m_coverCache.size();
QString cacheKey = albumReleaseId.toString();
qWarning() << " cache cover for release" << cacheKey;
m_coverCache.insert(cacheKey, fetchedCoverArtPixmap);
qWarning() << " cache size:" << m_coverCache.size();
// Test
auto it = m_coverCache.find(cacheKey);
qWarning() << " TEST";
if (it != m_coverCache.constEnd()) {

Check failure on line 634 in src/library/dlgtagfetcher.cpp

View workflow job for this annotation

GitHub Actions / clazy

Mixing iterators with const_iterators [-Wclazy-strict-iterators]
qWarning() << " .";
qWarning() << " found cached cover for release" << cacheKey;
QPixmap pix = it.value();
qWarning() << " pix is okay:" << !pix.isNull() << pix.size();
qWarning() << " .";
}
loadPixmapToLabel(fetchedCoverArtPixmap);
}

void DlgTagFetcher::loadPixmapToLabel(const QPixmap& pixmap) {
CoverInfo coverInfo;
coverInfo.type = CoverInfo::NONE;
coverInfo.source = CoverInfo::USER_SELECTED;

loadingProgressBar->setVisible(false);
statusMessage->clear();
statusMessage->setVisible(true);

m_fetchedCoverArtByteArrays = data;
m_pWFetchedCoverArtLabel->loadData(
m_fetchedCoverArtByteArrays); // This data loaded because for full size.
m_pWFetchedCoverArtLabel->setCoverArt(coverInfo, fetchedCoverArtPixmap);
m_pWFetchedCoverArtLabel->setCoverArt(coverInfo, pixmap);

btnApplyCover->setDisabled(data.isNull());
btnApplyCover->setDisabled(pixmap.isNull());
}

void DlgTagFetcher::getCoverArt(const QString& url) {
void DlgTagFetcher::getCoverArt(const QUuid& albumReleaseId, const QString& url) {
loadingProgressBar->setFormat(tr("Cover art found, receiving image."));
loadingProgressBar->setValue(kPercentForCoverArtImageTask);
m_tagFetcher.startFetchCoverArtImage(url);
m_tagFetcher.startFetchCoverArtImage(albumReleaseId, url);
}

void DlgTagFetcher::slotCoverArtLinkNotFound() {
Expand Down
12 changes: 9 additions & 3 deletions src/library/dlgtagfetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class DlgTagFetcher : public QDialog, public Ui::DlgTagFetcher {
const QObject* pRequester,
const CoverInfo& coverInfo,
const QPixmap& pixmap);
void slotStartFetchCoverArt(const QList<QString>& allUrls);
void slotLoadBytesToLabel(const QByteArray& data);
void slotStartFetchCoverArt(const QUuid& albumReleaseId,
const QList<QString>& allUrls);
void slotLoadFetchedCoverArt(const QUuid& albumReleaseId,
const QByteArray& data);
void slotCoverArtLinkNotFound();
void slotWorkerStarted();
void slotWorkerAskOverwrite(const QString& coverArtAbsolutePath,
Expand All @@ -74,9 +76,11 @@ class DlgTagFetcher : public QDialog, public Ui::DlgTagFetcher {
// Called on population or changed via buttons Next&Prev.
void loadTrackInternal(const TrackPointer& pTrack);
void addDivider(const QString& text, QTreeWidget* pParent) const;
void getCoverArt(const QString& url);
void getCoverArt(const QUuid& albumReleaseId, const QString& url);
void loadCurrentTrackCover();

void loadPixmapToLabel(const QPixmap& pPixmap);

UserSettingsPointer m_pConfig;

const TrackModel* const m_pTrackModel;
Expand Down Expand Up @@ -107,5 +111,7 @@ class DlgTagFetcher : public QDialog, public Ui::DlgTagFetcher {

QByteArray m_fetchedCoverArtByteArrays;

QMap<QString, QPixmap> m_coverCache;

QScopedPointer<CoverArtCopyWorker> m_pWorker;
};
14 changes: 9 additions & 5 deletions src/musicbrainz/tagfetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void TagFetcher::slotCoverArtArchiveLinksTaskFailed(
-1);
}

void TagFetcher::slotCoverArtArchiveLinksTaskSucceeded(
void TagFetcher::slotCoverArtArchiveLinksTaskSucceeded(const QUuid& albumReleaseId,
const QList<QString>& allUrls) {
DEBUG_ASSERT_QOBJECT_THREAD_AFFINITY(this);
if (m_pCoverArtArchiveLinksTask.get() != sender()) {
Expand All @@ -393,14 +393,16 @@ void TagFetcher::slotCoverArtArchiveLinksTaskSucceeded(
auto pTrack = std::move(m_pTrack);
terminate();

emit coverArtArchiveLinksAvailable(std::move(allUrls));
emit coverArtArchiveLinksAvailable(std::move(albumReleaseId),
std::move(allUrls));
}

void TagFetcher::startFetchCoverArtImage(
void TagFetcher::startFetchCoverArtImage(const QUuid& albumReleaseId,
const QString& coverArtUrl) {
m_pCoverArtArchiveImageTask = make_parented<mixxx::CoverArtArchiveImageTask>(
&m_network,
coverArtUrl,
albumReleaseId,
this);

connect(m_pCoverArtArchiveImageTask,
Expand All @@ -424,7 +426,8 @@ void TagFetcher::startFetchCoverArtImage(
kCoverArtArchiveImageTimeoutMilis);
}

void TagFetcher::slotCoverArtArchiveImageTaskSucceeded(const QByteArray& coverArtBytes) {
void TagFetcher::slotCoverArtArchiveImageTaskSucceeded(const QUuid& albumReleaseId,
const QByteArray& coverArtBytes) {
if (m_pCoverArtArchiveImageTask.get() != sender()) {
// stray call from an already aborted try
return;
Expand All @@ -433,7 +436,8 @@ void TagFetcher::slotCoverArtArchiveImageTaskSucceeded(const QByteArray& coverAr
auto pTrack = std::move(m_pTrack);
terminate();

emit coverArtImageFetchAvailable(coverArtBytes);
emit coverArtImageFetchAvailable(std::move(albumReleaseId),
coverArtBytes);
}

void TagFetcher::slotCoverArtArchiveImageTaskAborted() {
Expand Down
14 changes: 9 additions & 5 deletions src/musicbrainz/tagfetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TagFetcher : public QObject {
// Link provided from preference option.
// After a success task, related label updated with cover art.
// If user presses apply, cover art downloaded and applied to the song.
void startFetchCoverArtImage(
void startFetchCoverArtImage(const QUuid& albumReleaseId,
const QString& coverArtUrl);

public slots:
Expand All @@ -58,8 +58,10 @@ class TagFetcher : public QObject {
const QString& message,
int code);
void fetchedCoverUpdate(const QByteArray& coverInfo);
void coverArtImageFetchAvailable(const QByteArray& coverArtBytes);
void coverArtArchiveLinksAvailable(const QList<QString>& allUrls);
void coverArtImageFetchAvailable(const QUuid& albumReleaseId,
const QByteArray& coverArtBytes);
void coverArtArchiveLinksAvailable(const QUuid& albumReleaseId,
const QList<QString>& allUrls);
void coverArtLinkNotFound();

private slots:
Expand Down Expand Up @@ -87,7 +89,8 @@ class TagFetcher : public QObject {
const QString& errorString,
const mixxx::network::WebResponseWithContent& responseWithContent);

void slotCoverArtArchiveLinksTaskSucceeded(const QList<QString>& allUrls);
void slotCoverArtArchiveLinksTaskSucceeded(const QUuid& albumReleaseId,
const QList<QString>& allUrls);
void slotCoverArtArchiveLinksTaskFailed(
const mixxx::network::JsonWebResponse& response);
void slotCoverArtArchiveLinksTaskAborted();
Expand All @@ -96,7 +99,8 @@ class TagFetcher : public QObject {
const QString& errorString,
const mixxx::network::WebResponseWithContent& responseWithContent);

void slotCoverArtArchiveImageTaskSucceeded(const QByteArray& coverArtBytes);
void slotCoverArtArchiveImageTaskSucceeded(const QUuid& albumReleaseId,
const QByteArray& coverArtBytes);
void slotCoverArtArchiveImageTaskFailed(
const mixxx::network::WebResponse& response,
int errorCode,
Expand Down
6 changes: 4 additions & 2 deletions src/musicbrainz/web/coverartarchiveimagetask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ QNetworkRequest createNetworkRequest(const QString& coverArtUrl) {
CoverArtArchiveImageTask::CoverArtArchiveImageTask(
QNetworkAccessManager* pNetworkAccessManager,
const QString& coverArtLink,
const QUuid& albumReleaseId,
QObject* pParent)
: network::WebTask(
pNetworkAccessManager,
pParent),
m_coverArtUrl(coverArtLink) {
m_coverArtUrl(coverArtLink),
m_albumReleaseId(albumReleaseId) {
}

QNetworkReply* CoverArtArchiveImageTask::doStartNetworkRequest(
Expand Down Expand Up @@ -83,7 +85,7 @@ void CoverArtArchiveImageTask::emitSucceeded(
deleteLater();
return;
}
emit succeeded(coverArtImageBytes);
emit succeeded(m_albumReleaseId, coverArtImageBytes);
}

void CoverArtArchiveImageTask::emitFailed(
Expand Down
8 changes: 6 additions & 2 deletions src/musicbrainz/web/coverartarchiveimagetask.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QUuid>

#include "network/webtask.h"

namespace mixxx {
Expand All @@ -11,11 +13,12 @@ class CoverArtArchiveImageTask : public network::WebTask {
CoverArtArchiveImageTask(
QNetworkAccessManager* pNetworkAccessManager,
const QString& coverArtLink,
const QUuid& albumReleaseId,
QObject* pParent = nullptr);
~CoverArtArchiveImageTask() override = default;

signals:
void succeeded(
void succeeded(const QUuid& albumReleaseId,
const QByteArray& coverArtImageBytes);

void failed(
Expand All @@ -39,7 +42,8 @@ class CoverArtArchiveImageTask : public network::WebTask {
int errorCode,
const QString& errorMessage);

QString m_coverArtUrl;
const QString m_coverArtUrl;
const QUuid m_albumReleaseId;

QByteArray coverArtImageBytes;
};
Expand Down
2 changes: 1 addition & 1 deletion src/musicbrainz/web/coverartarchivelinkstask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void CoverArtArchiveLinksTask::emitSucceeded(
deleteLater();
return;
}
emit succeeded(allUrls);
emit succeeded(m_albumReleaseId, allUrls);
}

} // namespace mixxx
2 changes: 1 addition & 1 deletion src/musicbrainz/web/coverartarchivelinkstask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CoverArtArchiveLinksTask : public network::JsonWebTask {
~CoverArtArchiveLinksTask() override = default;

signals:
void succeeded(
void succeeded(const QUuid& albumReleaseId,
const QList<QString>& allUrls);

private:
Expand Down
10 changes: 4 additions & 6 deletions src/widget/wcoverartlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ void WCoverArtLabel::setCoverArt(const CoverInfo& coverInfo,
}
if (px.isNull()) {
m_loadedCover = px;
m_fullSizeCover = px;
setPixmap(m_defaultCover);
} else {
m_loadedCover = scaleCoverLabel(this, px);
m_fullSizeCover = px;
setPixmap(m_loadedCover);
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
Expand Down Expand Up @@ -83,10 +85,6 @@ void WCoverArtLabel::loadTrack(TrackPointer pTrack) {
m_pLoadedTrack = pTrack;
}

void WCoverArtLabel::loadData(const QByteArray& data) {
m_Data = data;
}

void WCoverArtLabel::mousePressEvent(QMouseEvent* event) {
if (m_pCoverMenu != nullptr && m_pCoverMenu->isVisible()) {
return;
Expand All @@ -98,8 +96,8 @@ void WCoverArtLabel::mousePressEvent(QMouseEvent* event) {
} else {
if (m_loadedCover.isNull()) {
return;
} else if (!m_pLoadedTrack && !m_Data.isNull()) {
m_pDlgFullSize->initFetchedCoverArt(m_Data);
} else if (!m_pLoadedTrack && !m_fullSizeCover.isNull()) {
m_pDlgFullSize->initFetchedCoverArt(m_fullSizeCover);
} else {
m_pDlgFullSize->init(m_pLoadedTrack);
}
Expand Down
Loading

0 comments on commit cf6c689

Please sign in to comment.