2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-12-26 13:34:53 +01:00
|
|
|
Copyright 2010-2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2010-2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2011, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-12-26 13:34:53 +01:00
|
|
|
#ifndef COVERS_ALBUMCOVERLOADER_H_
|
|
|
|
#define COVERS_ALBUMCOVERLOADER_H_
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
#include "albumcoverloaderoptions.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
#include "core/song.h"
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QMutex>
|
2011-11-28 14:51:35 +01:00
|
|
|
#include <QObject>
|
2010-02-28 19:04:50 +01:00
|
|
|
#include <QQueue>
|
2011-11-28 14:51:35 +01:00
|
|
|
#include <QUrl>
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
class NetworkAccessManager;
|
|
|
|
class QNetworkReply;
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
class AlbumCoverLoader : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2014-12-26 13:34:53 +01:00
|
|
|
explicit AlbumCoverLoader(QObject* parent = nullptr);
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2010-03-25 15:33:09 +01:00
|
|
|
void Stop() { stop_requested_ = true; }
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-02-28 20:25:52 +01:00
|
|
|
static QString ImageCacheDir();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
quint64 LoadImageAsync(const AlbumCoverLoaderOptions& options,
|
|
|
|
const Song& song);
|
|
|
|
virtual quint64 LoadImageAsync(const AlbumCoverLoaderOptions& options,
|
|
|
|
const QString& art_automatic,
|
|
|
|
const QString& art_manual,
|
|
|
|
const QString& song_filename = QString(),
|
|
|
|
const QImage& embedded_image = QImage());
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
void CancelTask(quint64 id);
|
|
|
|
void CancelTasks(const QSet<quint64>& ids);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-12-28 23:20:54 +01:00
|
|
|
static QPixmap TryLoadPixmap(const QString& automatic, const QString& manual,
|
|
|
|
const QString& filename = QString());
|
2014-02-07 16:34:20 +01:00
|
|
|
static QImage ScaleAndPad(const AlbumCoverLoaderOptions& options,
|
|
|
|
const QImage& image);
|
2010-03-03 15:29:53 +01:00
|
|
|
|
2014-12-26 13:34:53 +01:00
|
|
|
signals:
|
2010-02-28 19:04:50 +01:00
|
|
|
void ImageLoaded(quint64 id, const QImage& image);
|
2011-01-31 21:53:38 +01:00
|
|
|
void ImageLoaded(quint64 id, const QImage& scaled, const QImage& original);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-12-10 15:26:29 +01:00
|
|
|
protected slots:
|
2010-02-28 19:04:50 +01:00
|
|
|
void ProcessTasks();
|
2012-10-12 12:38:12 +02:00
|
|
|
void RemoteFetchFinished(QNetworkReply* reply);
|
2011-11-29 11:20:23 +01:00
|
|
|
void SpotifyImageLoaded(const QString& url, const QImage& image);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-12-10 15:26:29 +01:00
|
|
|
protected:
|
2014-02-07 16:34:20 +01:00
|
|
|
enum State { State_TryingManual, State_TryingAuto, };
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
struct Task {
|
2010-11-29 14:34:30 +01:00
|
|
|
Task() : redirects(0) {}
|
2012-02-13 21:44:04 +01:00
|
|
|
|
|
|
|
AlbumCoverLoaderOptions options;
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
quint64 id;
|
|
|
|
QString art_automatic;
|
|
|
|
QString art_manual;
|
2010-07-19 19:55:02 +02:00
|
|
|
QString song_filename;
|
|
|
|
QImage embedded_image;
|
2010-05-10 15:15:52 +02:00
|
|
|
State state;
|
2010-11-29 14:34:30 +01:00
|
|
|
int redirects;
|
2010-02-28 19:04:50 +01:00
|
|
|
};
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
struct TryLoadResult {
|
2010-07-19 19:55:02 +02:00
|
|
|
TryLoadResult(bool async, bool success, const QImage& i)
|
2014-02-07 16:34:20 +01:00
|
|
|
: started_async(async), loaded_success(success), image(i) {}
|
2010-05-10 15:15:52 +02:00
|
|
|
|
|
|
|
bool started_async;
|
|
|
|
bool loaded_success;
|
|
|
|
QImage image;
|
|
|
|
};
|
|
|
|
|
|
|
|
void ProcessTask(Task* task);
|
|
|
|
void NextState(Task* task);
|
|
|
|
TryLoadResult TryLoadImage(const Task& task);
|
|
|
|
|
2010-03-25 15:33:09 +01:00
|
|
|
bool stop_requested_;
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
QMutex mutex_;
|
|
|
|
QQueue<Task> tasks_;
|
2010-10-16 19:20:54 +02:00
|
|
|
QMap<QNetworkReply*, Task> remote_tasks_;
|
2011-11-29 11:20:23 +01:00
|
|
|
QMap<QString, Task> remote_spotify_tasks_;
|
2010-02-28 19:04:50 +01:00
|
|
|
quint64 next_id_;
|
2010-05-10 15:15:52 +02:00
|
|
|
|
|
|
|
NetworkAccessManager* network_;
|
2010-11-29 14:34:30 +01:00
|
|
|
|
2011-04-29 15:41:42 +02:00
|
|
|
bool connected_spotify_;
|
|
|
|
|
2010-11-29 14:34:30 +01:00
|
|
|
static const int kMaxRedirects = 3;
|
2010-02-28 19:04:50 +01:00
|
|
|
};
|
|
|
|
|
2014-12-26 13:34:53 +01:00
|
|
|
#endif // COVERS_ALBUMCOVERLOADER_H_
|