From c5c7a07c12c15429398dbd91b594291a3e60849e Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 12 May 2020 19:48:37 +0200 Subject: [PATCH] Add QImageReader::imageFormatsForMimeType replacement function --- src/core/utilities.cpp | 10 ++++++++++ src/core/utilities.h | 2 ++ src/subsonic/subsonicrequest.cpp | 11 +++++++++-- src/tidal/tidalrequest.cpp | 11 +++++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/utilities.cpp b/src/core/utilities.cpp index 476418292..2c12be4f9 100644 --- a/src/core/utilities.cpp +++ b/src/core/utilities.cpp @@ -989,6 +989,16 @@ bool IsColorDark(const QColor &color) { return ((30 * color.red() + 59 * color.green() + 11 * color.blue()) / 100) <= 130; } +QList ImageFormatsForMimeType(const QByteArray &mimetype) { + + if (mimetype == "image/bmp") return QList() << "BMP"; + else if (mimetype == "image/gif") return QList() << "GIF"; + else if (mimetype == "image/jpeg") return QList() << "JPG"; + else if (mimetype == "image/png") return QList() << "PNG"; + else return QList(); + +} + } // namespace Utilities ScopedWCharArray::ScopedWCharArray(const QString &str) diff --git a/src/core/utilities.h b/src/core/utilities.h index f60dccdee..ea2727ba2 100644 --- a/src/core/utilities.h +++ b/src/core/utilities.h @@ -163,6 +163,8 @@ QString ReplaceVariable(const QString &variable, const Song &song, const QString bool IsColorDark(const QColor &color); +QList ImageFormatsForMimeType(const QByteArray &mimetype); + } // namespace class ScopedWCharArray { diff --git a/src/subsonic/subsonicrequest.cpp b/src/subsonic/subsonicrequest.cpp index cd14b5762..cf0263e7d 100644 --- a/src/subsonic/subsonicrequest.cpp +++ b/src/subsonic/subsonicrequest.cpp @@ -44,6 +44,7 @@ #include "core/logging.h" #include "core/song.h" #include "core/timeconstants.h" +#include "core/utilities.h" #include "covermanager/albumcoverloader.h" #include "subsonicservice.h" #include "subsonicurlhandler.h" @@ -769,15 +770,21 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb AlbumCoverFinishCheck(); return; } - QByteArray format = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()).first(); + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) + QList format_list = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()); +#else + QList format_list = Utilities::ImageFormatsForMimeType(mimetype.toUtf8()); +#endif QByteArray data = reply->readAll(); - if (data.isEmpty()) { + if (format_list.isEmpty() || data.isEmpty()) { Error(QString("Received empty image data for %1").arg(url.toString())); if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); AlbumCoverFinishCheck(); return; } + QByteArray format = format_list.first(); QString fullfilename = filename + "." + format.toLower(); QImage image; diff --git a/src/tidal/tidalrequest.cpp b/src/tidal/tidalrequest.cpp index 28fa685a6..991c646a4 100644 --- a/src/tidal/tidalrequest.cpp +++ b/src/tidal/tidalrequest.cpp @@ -37,6 +37,7 @@ #include "core/song.h" #include "core/timeconstants.h" #include "core/application.h" +#include "core/utilities.h" #include "covermanager/albumcoverloader.h" #include "tidalservice.h" #include "tidalurlhandler.h" @@ -1166,15 +1167,21 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album AlbumCoverFinishCheck(); return; } - QByteArray format = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()).first(); + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) + QList format_list = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()); +#else + QList format_list = Utilities::ImageFormatsForMimeType(mimetype.toUtf8()); +#endif QByteArray data = reply->readAll(); - if (data.isEmpty()) { + if (format_list.isEmpty() || data.isEmpty()) { Error(QString("Received empty image data for %1").arg(url.toString())); if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); AlbumCoverFinishCheck(); return; } + QByteArray format = format_list.first(); QImage image; if (image.loadFromData(data, format)) {