2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, 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>
|
2020-04-20 18:03:18 +02:00
|
|
|
#include <QPair>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QSet>
|
2021-07-12 08:28:52 +02:00
|
|
|
#include <QHash>
|
2021-02-26 21:03:51 +01:00
|
|
|
#include <QMultiMap>
|
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>
|
|
|
|
#include <QPixmap>
|
|
|
|
|
|
|
|
#include "core/song.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "core/tagreaderclient.h"
|
2019-07-07 21:14:24 +02:00
|
|
|
#include "settings/collectionsettingspage.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#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:
|
|
|
|
explicit AlbumCoverLoader(QObject *parent = nullptr);
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
enum State {
|
|
|
|
State_None,
|
|
|
|
State_Manual,
|
|
|
|
State_Automatic,
|
|
|
|
};
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
void ReloadSettings();
|
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
void ExitAsync();
|
2018-02-27 18:06:05 +01:00
|
|
|
void Stop() { stop_requested_ = true; }
|
|
|
|
|
2020-05-12 18:50:57 +02:00
|
|
|
static QString AlbumCoverFilename(QString artist, QString album, const QString &extension);
|
2020-04-20 18:03:18 +02:00
|
|
|
|
2021-06-22 13:41:38 +02:00
|
|
|
static QString CoverFilenameFromSource(const Song::Source source, const QUrl &cover_url, const QString &artist, const QString &album, const QString &album_id, const QString &extension);
|
2020-05-12 18:50:57 +02:00
|
|
|
QString CoverFilenameFromVariable(const QString &artist, const QString &album, const QString &extension = QString());
|
|
|
|
QString CoverFilePath(const Song &song, const QString &album_dir, const QUrl &cover_url, const QString &extension = QString());
|
|
|
|
|
|
|
|
QString CoverFilePath(const Song::Source source, const QString &artist, QString album, const QString &album_id, const QString &album_dir, const QUrl &cover_url, const QString &extension = QString());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const Song &song);
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const QUrl &art_automatic, const QUrl &art_manual, const QUrl &song_url = QUrl(), const Song::Source song_source = Song::Source_Unknown);
|
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const AlbumCoverImageResult &album_cover);
|
|
|
|
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);
|
|
|
|
|
2021-09-12 21:24:22 +02:00
|
|
|
quint64 SaveEmbeddedCoverAsync(const QString &song_filename, const QString &cover_filename);
|
|
|
|
quint64 SaveEmbeddedCoverAsync(const QString &song_filename, const QImage &image);
|
|
|
|
quint64 SaveEmbeddedCoverAsync(const QString &song_filename, const QByteArray &image_data);
|
|
|
|
quint64 SaveEmbeddedCoverAsync(const QList<QUrl> &urls, const QString &cover_filename);
|
|
|
|
quint64 SaveEmbeddedCoverAsync(const QList<QUrl> &urls, const QImage &image);
|
|
|
|
quint64 SaveEmbeddedCoverAsync(const QList<QUrl> &urls, const QByteArray &image_data);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
signals:
|
|
|
|
void ExitFinished();
|
2020-04-20 18:03:18 +02:00
|
|
|
void AlbumCoverLoaded(quint64 id, AlbumCoverLoaderResult result);
|
2021-03-07 05:43:56 +01:00
|
|
|
void SaveEmbeddedCoverAsyncFinished(quint64 id, bool success, bool cleared);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
protected slots:
|
2019-07-24 23:29:09 +02:00
|
|
|
void Exit();
|
2018-02-27 18:06:05 +01:00
|
|
|
void ProcessTasks();
|
2019-07-07 21:14:24 +02:00
|
|
|
void RemoteFetchFinished(QNetworkReply *reply, const QUrl &cover_url);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void SaveEmbeddedCover(const qint64 id, const QString &song_filename, const QString &cover_filename);
|
|
|
|
void SaveEmbeddedCover(const qint64 id, const QString &song_filename, const QImage &image);
|
|
|
|
void SaveEmbeddedCover(const qint64 id, const QString &song_filename, const QByteArray &image_data);
|
|
|
|
void SaveEmbeddedCover(const qint64 id, const QList<QUrl> &urls, const QImage &image);
|
|
|
|
void SaveEmbeddedCover(const qint64 id, const QList<QUrl> &urls, const QString &cover_filename);
|
|
|
|
void SaveEmbeddedCover(const qint64 id, const QList<QUrl> &urls, const QByteArray &image_data);
|
2021-02-26 21:03:51 +01:00
|
|
|
|
2021-03-07 05:43:56 +01:00
|
|
|
void SaveEmbeddedArtFinished(const qint64 id, TagReaderReply *reply, const bool cleared);
|
2021-02-26 21:03:51 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
protected:
|
|
|
|
|
|
|
|
struct Task {
|
2020-04-20 18:03:18 +02:00
|
|
|
explicit Task() : id(0), state(State_None), type(AlbumCoverLoaderResult::Type_None), art_updated(false), redirects(0) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
AlbumCoverLoaderOptions options;
|
|
|
|
|
|
|
|
quint64 id;
|
2020-04-20 18:03:18 +02:00
|
|
|
Song song;
|
2021-02-26 21:03:51 +01:00
|
|
|
AlbumCoverImageResult album_cover;
|
2018-02-27 18:06:05 +01:00
|
|
|
State state;
|
2020-04-20 18:03:18 +02:00
|
|
|
AlbumCoverLoaderResult::Type type;
|
|
|
|
bool art_updated;
|
2018-02-27 18:06:05 +01:00
|
|
|
int redirects;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TryLoadResult {
|
2021-02-26 21:03:51 +01:00
|
|
|
explicit TryLoadResult(const bool _started_async = false,
|
|
|
|
const bool _loaded_success = false,
|
|
|
|
const AlbumCoverLoaderResult::Type _type = AlbumCoverLoaderResult::Type_None,
|
|
|
|
const AlbumCoverImageResult &_album_cover = AlbumCoverImageResult()) :
|
|
|
|
started_async(_started_async),
|
|
|
|
loaded_success(_loaded_success),
|
|
|
|
type(_type),
|
|
|
|
album_cover(_album_cover) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool started_async;
|
|
|
|
bool loaded_success;
|
2019-07-07 21:14:24 +02:00
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
AlbumCoverLoaderResult::Type type;
|
2021-02-26 21:03:51 +01:00
|
|
|
AlbumCoverImageResult album_cover;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 EnqueueTask(Task &task);
|
2018-05-01 00:41:33 +02:00
|
|
|
void ProcessTask(Task *task);
|
|
|
|
void NextState(Task *task);
|
2020-04-20 18:03:18 +02:00
|
|
|
TryLoadResult TryLoadImage(Task *task);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool stop_requested_;
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
QMutex mutex_load_image_async_;
|
|
|
|
QMutex mutex_save_image_async_;
|
2018-02-27 18:06:05 +01:00
|
|
|
QQueue<Task> tasks_;
|
2021-07-12 08:28:52 +02:00
|
|
|
QHash<QNetworkReply*, Task> remote_tasks_;
|
2021-02-26 21:03:51 +01:00
|
|
|
quint64 load_image_async_id_;
|
|
|
|
quint64 save_image_async_id_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
NetworkAccessManager *network_;
|
|
|
|
|
|
|
|
static const int kMaxRedirects = 3;
|
2019-07-07 21:14:24 +02:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
CollectionSettingsPage::SaveCoverType save_cover_type_;
|
|
|
|
CollectionSettingsPage::SaveCoverFilename save_cover_filename_;
|
2019-07-07 21:14:24 +02:00
|
|
|
QString cover_pattern_;
|
|
|
|
bool cover_overwrite_;
|
|
|
|
bool cover_lowercase_;
|
|
|
|
bool cover_replace_spaces_;
|
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
QThread *original_thread_;
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
QMultiMap<qint64, TagReaderReply*> tagreader_save_embedded_art_requests_;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ALBUMCOVERLOADER_H
|