Skip to content

Instantly share code, notes, and snippets.

@hdak1945git
Created November 12, 2018 03:40
Show Gist options
  • Save hdak1945git/ca649b34f6c6a577657d9f91b311aeb7 to your computer and use it in GitHub Desktop.
Save hdak1945git/ca649b34f6c6a577657d9f91b311aeb7 to your computer and use it in GitHub Desktop.
Save MPD artwork even if the job isn't found for it!
diff --git a/gui/covers.cpp b/gui/covers.cpp
index 7d57ad8e..a6bc6770 100644
--- a/gui/covers.cpp
+++ b/gui/covers.cpp
@@ -723,36 +723,49 @@ void CoverDownloader::downloadViaRemote(Job &job)
void CoverDownloader::mpdAlbumArt(const Song &song, const QByteArray &data)
{
- QHash<NetworkJob *, Job>::Iterator it=findJob(Job(song, QString()));
- QHash<NetworkJob *, Job>::Iterator end(jobs.end());
+ QString savedName;
- if (it!=end) {
- Covers::Image img;
- img.img= data.isEmpty() ? QImage() : QImage::fromData(data, Covers::imageFormat(data));
- Job job=it.value();
+ Covers::Image img;
+ img.img = data.isEmpty() ? QImage() : QImage::fromData(data, Covers::imageFormat(data));
- if (!img.img.isNull() && img.img.size().width()<32) {
- img.img = QImage();
+ if (!img.img.isNull() && img.img.size().width()<32) {
+ img.img = QImage();
+ } else if (!img.img.isNull()) {
+ if (img.img.size().width()>Covers::constMaxSize.width() || img.img.size().height()>Covers::constMaxSize.height()) {
+ img.img=img.img.scaled(Covers::constMaxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
- jobs.remove(it.key());
+ // TODO: refactor CoverDownloader::saveImg
+ QString mimeType=typeFromRaw(data);
+ QString extension=mimeType.isEmpty() ? constExtensions[0] : mimeType;
+ // Could not save with album, save in cache dir...
+ QString dir = Utils::cacheDir(Covers::constCoverDir+Covers::encodeName(song.albumArtist()), true);
+ if (!dir.isEmpty()) {
+ savedName=save(mimeType, extension, dir+Covers::encodeName(song.album), img.img, data);
+ }
+
+ if (!img.fileName.isEmpty()) {
+ clearScaledCache(song);
+ }
+ }
+
+ QHash<NetworkJob *, Job>::Iterator it=findJob(Job(song, QString()));
+ QHash<NetworkJob *, Job>::Iterator end(jobs.end());
+ if (it!=end) {
+ Job job=it.value();
if (img.img.isNull()) {
downloadViaHttp(job, JobHttpJpg);
} else {
- if (!img.img.isNull()) {
- if (img.img.size().width()>Covers::constMaxSize.width() || img.img.size().height()>Covers::constMaxSize.height()) {
- img.img=img.img.scaled(Covers::constMaxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- }
- img.fileName=saveImg(job, img.img, data);
- if (!img.fileName.isEmpty()) {
- clearScaledCache(job.song);
- }
- }
-
- DBUG << "got cover image" << img.fileName;
- emit cover(job.song, img.img, img.fileName);
+ jobs.remove(it.key());
}
+ } else {
+ DBUG << "CoverDownloader::mpdAlbumArt was called without pending job for:"
+ << song.file;
}
+
+ const QString actualSavedName = savedName.isEmpty() ? img.fileName : savedName;
+ DBUG << "got cover image" << actualSavedName;
+ emit cover(song, img.img, actualSavedName);
}
void CoverDownloader::remoteCallFinished()
diff --git a/mpd-interface/mpdconnection.cpp b/mpd-interface/mpdconnection.cpp
index 580b1b79..667d6e60 100644
--- a/mpd-interface/mpdconnection.cpp
+++ b/mpd-interface/mpdconnection.cpp
@@ -726,6 +726,15 @@ MPDConnection::Response MPDConnection::sendCommand(const QByteArray &command, bo
emit stateChanged(false);
emit error(tr("Failed to send command. Disconnected from %1").arg(details.description()), true);
}
+ } else if (command.startsWith("albumart ")) {
+ const auto start = command.indexOf(' ');
+ const auto end = command.lastIndexOf(' ') - start;
+ if (start > 0 && (end > 0 && (start + end) < command.length())) {
+ const QString filename = command.mid(start, end);
+ emit error(tr("MPD reported no album art for %1").arg(filename));
+ } else {
+ emit error("Album art command was malformed.");
+ }
} else if (!response.getError(command).isEmpty()) {
emit error(tr("MPD reported the following error: %1").arg(response.getError(command)));
} /*else if ("listallinfo"==command && ver>=CANTATA_MAKE_VERSION(0,18,0)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment