2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2019-07-07 21:14:24 +02:00
|
|
|
* Copyright 2019, 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>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QQueue>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMap>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QString>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
|
|
|
#include "core/song.h"
|
2019-07-07 21:14:24 +02:00
|
|
|
#include "settings/collectionsettingspage.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "albumcoverloaderoptions.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
class QThread;
|
2018-05-01 00:41:33 +02:00
|
|
|
class Song;
|
2018-02-27 18:06:05 +01:00
|
|
|
class NetworkAccessManager;
|
|
|
|
|
|
|
|
class AlbumCoverLoader : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AlbumCoverLoader(QObject *parent = nullptr);
|
|
|
|
|
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; }
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
static QString ImageCacheDir(const Song::Source source);
|
|
|
|
QString CreateCoverFilename(const QString &artist, const QString &album);
|
|
|
|
QString CoverFilePath(const Song::Source source, const QString &artist, QString album, const QString &album_id, const QString &album_dir, const QUrl &cover_url);
|
|
|
|
QString AlbumCoverFileName(QString artist, QString album);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const Song &song);
|
2019-07-07 21:14:24 +02:00
|
|
|
virtual quint64 LoadImageAsync(const AlbumCoverLoaderOptions &options, const QUrl &art_automatic, const QUrl &art_manual, const QString &song_filename = QString(), const QImage &embedded_image = QImage());
|
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-09 01:14:58 +02:00
|
|
|
static QPixmap TryLoadPixmap(const QUrl &automatic, const QUrl &manual, const QUrl &url = QUrl());
|
2018-02-27 18:06:05 +01:00
|
|
|
static QImage ScaleAndPad(const AlbumCoverLoaderOptions &options, const QImage &image);
|
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
signals:
|
|
|
|
void ExitFinished();
|
2019-07-07 21:14:24 +02:00
|
|
|
void ImageLoaded(const quint64 id, const QUrl &cover_url, const QImage &image);
|
|
|
|
void ImageLoaded(const quint64 id, const QUrl &cover_url, const QImage &scaled, const QImage &original);
|
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
|
|
|
|
|
|
|
protected:
|
|
|
|
enum State {
|
|
|
|
State_TryingManual,
|
|
|
|
State_TryingAuto,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Task {
|
|
|
|
Task() : redirects(0) {}
|
|
|
|
|
|
|
|
AlbumCoverLoaderOptions options;
|
|
|
|
|
|
|
|
quint64 id;
|
2019-07-07 21:14:24 +02:00
|
|
|
QUrl art_automatic;
|
|
|
|
QUrl art_manual;
|
2018-02-27 18:06:05 +01:00
|
|
|
QString song_filename;
|
|
|
|
QImage embedded_image;
|
|
|
|
State state;
|
|
|
|
int redirects;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TryLoadResult {
|
2019-07-07 21:14:24 +02:00
|
|
|
TryLoadResult(bool async, bool success, const QUrl &_cover_url, const QImage &_image) : started_async(async), loaded_success(success), cover_url(_cover_url), image(_image) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool started_async;
|
|
|
|
bool loaded_success;
|
2019-07-07 21:14:24 +02:00
|
|
|
|
|
|
|
QUrl cover_url;
|
2018-02-27 18:06:05 +01:00
|
|
|
QImage image;
|
|
|
|
};
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
void ProcessTask(Task *task);
|
|
|
|
void NextState(Task *task);
|
2018-02-27 18:06:05 +01:00
|
|
|
TryLoadResult TryLoadImage(const Task &task);
|
|
|
|
|
|
|
|
bool stop_requested_;
|
|
|
|
|
|
|
|
QMutex mutex_;
|
|
|
|
QQueue<Task> tasks_;
|
|
|
|
QMap<QNetworkReply *, Task> remote_tasks_;
|
|
|
|
quint64 next_id_;
|
|
|
|
|
|
|
|
NetworkAccessManager *network_;
|
|
|
|
|
|
|
|
static const int kMaxRedirects = 3;
|
2019-07-07 21:14:24 +02:00
|
|
|
|
|
|
|
bool cover_album_dir_;
|
|
|
|
CollectionSettingsPage::SaveCover cover_filename_;
|
|
|
|
QString cover_pattern_;
|
|
|
|
bool cover_overwrite_;
|
|
|
|
bool cover_lowercase_;
|
|
|
|
bool cover_replace_spaces_;
|
|
|
|
|
2019-07-24 23:29:09 +02:00
|
|
|
QThread *original_thread_;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ALBUMCOVERLOADER_H
|
|
|
|
|