2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2023-05-14 11:34:55 +02:00
|
|
|
* Copyright 2019-2023, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ALBUMCOVERLOADER_H
|
|
|
|
#define ALBUMCOVERLOADER_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QObject>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QSet>
|
2021-07-12 08:28:52 +02:00
|
|
|
#include <QHash>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QQueue>
|
2021-02-26 21:03:51 +01:00
|
|
|
#include <QByteArray>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QImage>
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/song.h"
|
|
|
|
#include "albumcoverloaderoptions.h"
|
2020-04-20 18:03:18 +02:00
|
|
|
#include "albumcoverloaderresult.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "albumcoverimageresult.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
class QThread;
|
2020-02-09 02:29:35 +01:00
|
|
|
class QNetworkReply;
|
2018-02-27 18:06:05 +01:00
|
|
|
class NetworkAccessManager;
|
|
|
|
|
|
|
|
class AlbumCoverLoader : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2023-04-21 20:29:43 +02:00
|
|
|
explicit AlbumCoverLoader(QObject *parent = nullptr);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
void ExitAsync();
|
2018-02-27 18:06:05 +01:00
|
|
|
void Stop() { stop_requested_ = true; }
|
|
|
|
|
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const Song &song);
|
2023-05-14 11:34:55 +02:00
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const bool art_embedded, const QUrl &art_automatic, const QUrl &art_manual, const bool art_unset, const QUrl &song_url = QUrl(), const Song::Source song_source = Song::Source::Unknown);
|
2023-04-09 22:26:17 +02:00
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const AlbumCoverImageResult &album_cover);
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const QImage &image);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
void CancelTask(const quint64 id);
|
2018-02-27 18:06:05 +01:00
|
|
|
void CancelTasks(const QSet<quint64> &ids);
|
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
signals:
|
|
|
|
void ExitFinished();
|
2023-04-09 22:26:17 +02:00
|
|
|
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &result);
|
2021-02-26 21:03:51 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
private:
|
|
|
|
class Task {
|
|
|
|
public:
|
2023-06-29 00:44:00 +02:00
|
|
|
explicit Task() : id(0), success(false), art_embedded(false), art_unset(false), song_source(Song::Source::Unknown), result_type(AlbumCoverLoaderResult::Type::None), redirects(0) {}
|
2021-02-26 21:03:51 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
quint64 id;
|
|
|
|
bool success;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
AlbumCoverLoaderOptions options;
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
bool raw_image_data() const { return options.options & AlbumCoverLoaderOptions::Option::RawImageData; }
|
|
|
|
bool original_image() const { return options.options & AlbumCoverLoaderOptions::Option::OriginalImage; }
|
|
|
|
bool scaled_image() const { return options.options & AlbumCoverLoaderOptions::Option::ScaledImage; }
|
|
|
|
bool pad_scaled_image() const { return options.options & AlbumCoverLoaderOptions::Option::PadScaledImage; }
|
|
|
|
|
|
|
|
bool art_embedded;
|
|
|
|
QUrl art_automatic;
|
|
|
|
QUrl art_manual;
|
|
|
|
bool art_unset;
|
|
|
|
QUrl song_url;
|
|
|
|
Song::Source song_source;
|
2020-04-20 18:03:18 +02:00
|
|
|
Song song;
|
2023-04-09 22:26:17 +02:00
|
|
|
AlbumCoverImageResult album_cover;
|
2023-05-14 11:34:55 +02:00
|
|
|
AlbumCoverLoaderResult::Type result_type;
|
2023-06-29 00:44:00 +02:00
|
|
|
QUrl art_manual_updated;
|
|
|
|
QUrl art_automatic_updated;
|
2018-02-27 18:06:05 +01:00
|
|
|
int redirects;
|
|
|
|
};
|
2023-07-21 05:55:24 +02:00
|
|
|
using TaskPtr = SharedPtr<Task>;
|
2023-05-14 11:34:55 +02:00
|
|
|
|
|
|
|
class LoadImageResult {
|
|
|
|
public:
|
|
|
|
enum class Status {
|
|
|
|
Failure,
|
|
|
|
Async,
|
|
|
|
Success
|
|
|
|
};
|
|
|
|
explicit LoadImageResult(AlbumCoverLoaderResult::Type _type = AlbumCoverLoaderResult::Type::None, Status _status = Status::Failure) : type(_type), status(_status) {}
|
2020-04-20 18:03:18 +02:00
|
|
|
AlbumCoverLoaderResult::Type type;
|
2023-05-14 11:34:55 +02:00
|
|
|
Status status;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
private:
|
2023-04-06 23:18:10 +02:00
|
|
|
quint64 EnqueueTask(TaskPtr task);
|
|
|
|
void ProcessTask(TaskPtr task);
|
2023-05-14 11:34:55 +02:00
|
|
|
void InitArt(TaskPtr task);
|
2023-10-13 22:58:18 +02:00
|
|
|
LoadImageResult LoadImage(TaskPtr task, const AlbumCoverLoaderOptions::Type type);
|
2023-05-14 11:34:55 +02:00
|
|
|
LoadImageResult LoadEmbeddedImage(TaskPtr task);
|
|
|
|
LoadImageResult LoadUrlImage(TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
|
|
|
|
LoadImageResult LoadLocalUrlImage(TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
|
|
|
|
LoadImageResult LoadLocalFileImage(TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QString &cover_file);
|
|
|
|
LoadImageResult LoadRemoteUrlImage(TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
|
|
|
|
void FinishTask(TaskPtr task, const AlbumCoverLoaderResult::Type result_type);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void Exit();
|
|
|
|
void ProcessTasks();
|
2024-04-23 16:53:40 +02:00
|
|
|
void LoadRemoteImageFinished(QNetworkReply *reply, AlbumCoverLoader::TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
private:
|
|
|
|
static const int kMaxRedirects = 3;
|
2023-07-21 05:55:24 +02:00
|
|
|
SharedPtr<NetworkAccessManager> network_;
|
2018-02-27 18:06:05 +01:00
|
|
|
bool stop_requested_;
|
2021-02-26 21:03:51 +01:00
|
|
|
QMutex mutex_load_image_async_;
|
2023-04-06 23:18:10 +02:00
|
|
|
QQueue<TaskPtr> tasks_;
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 load_image_async_id_;
|
2019-07-24 23:29:09 +02:00
|
|
|
QThread *original_thread_;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ALBUMCOVERLOADER_H
|