2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.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/>.
|
|
|
|
*/
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
#include "albumcoverloader.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2010-02-28 20:25:52 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QCoreApplication>
|
2010-05-10 15:15:52 +02:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QNetworkReply>
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2012-10-12 12:38:12 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/tagreaderclient.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "internet/internetmodel.h"
|
2013-09-17 16:49:29 +02:00
|
|
|
#include "internet/spotifyservice.h"
|
2012-10-12 12:38:12 +02:00
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
AlbumCoverLoader::AlbumCoverLoader(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
stop_requested_(false),
|
|
|
|
next_id_(1),
|
|
|
|
network_(new NetworkAccessManager(this)),
|
|
|
|
connected_spotify_(false) {}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-02-28 20:25:52 +01:00
|
|
|
QString AlbumCoverLoader::ImageCacheDir() {
|
2010-12-09 14:06:00 +01:00
|
|
|
return Utilities::GetConfigPath(Utilities::Path_AlbumCovers);
|
2010-02-28 20:25:52 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
void AlbumCoverLoader::CancelTask(quint64 id) {
|
2010-02-28 19:04:50 +01:00
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (QQueue<Task>::iterator it = tasks_.begin(); it != tasks_.end(); ++it) {
|
2012-02-13 21:44:04 +01:00
|
|
|
if (it->id == id) {
|
|
|
|
tasks_.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
void AlbumCoverLoader::CancelTasks(const QSet<quint64>& ids) {
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (QQueue<Task>::iterator it = tasks_.begin(); it != tasks_.end();) {
|
2012-02-13 21:44:04 +01:00
|
|
|
if (ids.contains(it->id)) {
|
|
|
|
it = tasks_.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 AlbumCoverLoader::LoadImageAsync(const AlbumCoverLoaderOptions& options,
|
|
|
|
const QString& art_automatic,
|
2010-07-19 19:55:02 +02:00
|
|
|
const QString& art_manual,
|
|
|
|
const QString& song_filename,
|
|
|
|
const QImage& embedded_image) {
|
2010-02-28 19:04:50 +01:00
|
|
|
Task task;
|
2012-02-13 21:44:04 +01:00
|
|
|
task.options = options;
|
2010-02-28 19:04:50 +01:00
|
|
|
task.art_automatic = art_automatic;
|
|
|
|
task.art_manual = art_manual;
|
2010-07-19 19:55:02 +02:00
|
|
|
task.song_filename = song_filename;
|
|
|
|
task.embedded_image = embedded_image;
|
2010-05-10 15:15:52 +02:00
|
|
|
task.state = State_TryingManual;
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
task.id = next_id_++;
|
2010-02-28 19:04:50 +01:00
|
|
|
tasks_.enqueue(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
metaObject()->invokeMethod(this, "ProcessTasks", Qt::QueuedConnection);
|
|
|
|
|
|
|
|
return task.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverLoader::ProcessTasks() {
|
2010-03-25 15:47:41 +01:00
|
|
|
while (!stop_requested_) {
|
2010-02-28 19:04:50 +01:00
|
|
|
// Get the next task
|
|
|
|
Task task;
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (tasks_.isEmpty()) return;
|
2010-02-28 19:04:50 +01:00
|
|
|
task = tasks_.dequeue();
|
|
|
|
}
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
ProcessTask(&task);
|
|
|
|
}
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverLoader::ProcessTask(Task* task) {
|
2010-05-10 15:15:52 +02:00
|
|
|
TryLoadResult result = TryLoadImage(*task);
|
|
|
|
if (result.started_async) {
|
|
|
|
// The image is being loaded from a remote URL, we'll carry on later
|
|
|
|
// when it's done
|
|
|
|
return;
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
if (result.loaded_success) {
|
2012-02-13 21:44:04 +01:00
|
|
|
QImage scaled = ScaleAndPad(task->options, result.image);
|
2011-01-31 21:53:38 +01:00
|
|
|
emit ImageLoaded(task->id, scaled);
|
|
|
|
emit ImageLoaded(task->id, scaled, result.image);
|
2010-05-10 15:15:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
NextState(task);
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
void AlbumCoverLoader::NextState(Task* task) {
|
|
|
|
if (task->state == State_TryingManual) {
|
|
|
|
// Try the automatic one next
|
|
|
|
task->state = State_TryingAuto;
|
|
|
|
ProcessTask(task);
|
|
|
|
} else {
|
|
|
|
// Give up
|
2012-02-13 21:44:04 +01:00
|
|
|
emit ImageLoaded(task->id, task->options.default_output_image_);
|
|
|
|
emit ImageLoaded(task->id, task->options.default_output_image_,
|
|
|
|
task->options.default_output_image_);
|
2010-05-10 15:15:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AlbumCoverLoader::TryLoadResult AlbumCoverLoader::TryLoadImage(
|
|
|
|
const Task& task) {
|
2010-07-19 19:55:02 +02:00
|
|
|
// An image embedded in the song itself takes priority
|
|
|
|
if (!task.embedded_image.isNull())
|
2014-02-07 16:34:20 +01:00
|
|
|
return TryLoadResult(false, true,
|
|
|
|
ScaleAndPad(task.options, task.embedded_image));
|
2010-07-19 19:55:02 +02:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
QString filename;
|
|
|
|
switch (task.state) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case State_TryingAuto:
|
|
|
|
filename = task.art_automatic;
|
|
|
|
break;
|
|
|
|
case State_TryingManual:
|
|
|
|
filename = task.art_manual;
|
|
|
|
break;
|
2010-05-10 15:15:52 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 22:01:08 +01:00
|
|
|
if (filename == Song::kManuallyUnsetCover)
|
2012-02-13 21:44:04 +01:00
|
|
|
return TryLoadResult(false, true, task.options.default_output_image_);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2011-02-02 22:01:08 +01:00
|
|
|
if (filename == Song::kEmbeddedCover && !task.song_filename.isEmpty()) {
|
2012-01-06 22:27:02 +01:00
|
|
|
const QImage taglib_image =
|
2014-02-07 16:34:20 +01:00
|
|
|
TagReaderClient::Instance()->LoadEmbeddedArtBlocking(
|
|
|
|
task.song_filename);
|
2012-01-06 22:27:02 +01:00
|
|
|
|
2010-07-19 19:55:02 +02:00
|
|
|
if (!taglib_image.isNull())
|
2014-02-07 16:34:20 +01:00
|
|
|
return TryLoadResult(false, true,
|
|
|
|
ScaleAndPad(task.options, taglib_image));
|
2010-07-19 19:55:02 +02:00
|
|
|
}
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
if (filename.toLower().startsWith("http://")) {
|
2012-09-26 17:09:13 +02:00
|
|
|
QUrl url(filename);
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
2012-10-12 12:38:12 +02:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(RemoteFetchFinished(QNetworkReply*)), reply);
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
remote_tasks_.insert(reply, task);
|
2010-05-10 15:15:52 +02:00
|
|
|
return TryLoadResult(true, false, QImage());
|
2011-04-29 15:41:42 +02:00
|
|
|
} else if (filename.toLower().startsWith("spotify://image/")) {
|
|
|
|
// HACK: we should add generic image URL handlers
|
2013-09-17 16:49:29 +02:00
|
|
|
SpotifyService* spotify = InternetModel::Service<SpotifyService>();
|
|
|
|
|
|
|
|
if (!connected_spotify_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(spotify, SIGNAL(ImageLoaded(QString, QImage)),
|
|
|
|
SLOT(SpotifyImageLoaded(QString, QImage)));
|
2013-09-17 16:49:29 +02:00
|
|
|
connected_spotify_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString id = QUrl(filename).path();
|
|
|
|
if (id.startsWith('/')) {
|
|
|
|
id.remove(0, 1);
|
|
|
|
}
|
|
|
|
remote_spotify_tasks_.insert(id, task);
|
|
|
|
|
|
|
|
// Need to schedule this in the spotify service's thread
|
|
|
|
QMetaObject::invokeMethod(spotify, "LoadImage", Qt::QueuedConnection,
|
|
|
|
Q_ARG(QString, id));
|
|
|
|
return TryLoadResult(true, false, QImage());
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
2010-05-10 15:15:52 +02:00
|
|
|
|
|
|
|
QImage image(filename);
|
2014-02-07 16:34:20 +01:00
|
|
|
return TryLoadResult(
|
|
|
|
false, !image.isNull(),
|
|
|
|
image.isNull() ? task.options.default_output_image_ : image);
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
2010-03-03 15:29:53 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AlbumCoverLoader::SpotifyImageLoaded(const QString& id,
|
|
|
|
const QImage& image) {
|
|
|
|
if (!remote_spotify_tasks_.contains(id)) return;
|
2011-04-29 15:41:42 +02:00
|
|
|
|
2011-11-29 11:20:23 +01:00
|
|
|
Task task = remote_spotify_tasks_.take(id);
|
2012-02-13 21:44:04 +01:00
|
|
|
QImage scaled = ScaleAndPad(task.options, image);
|
2011-04-29 15:41:42 +02:00
|
|
|
emit ImageLoaded(task.id, scaled);
|
|
|
|
emit ImageLoaded(task.id, scaled, image);
|
|
|
|
}
|
|
|
|
|
2012-10-12 12:38:12 +02:00
|
|
|
void AlbumCoverLoader::RemoteFetchFinished(QNetworkReply* reply) {
|
2010-11-29 14:34:30 +01:00
|
|
|
reply->deleteLater();
|
2010-10-16 19:20:54 +02:00
|
|
|
|
|
|
|
Task task = remote_tasks_.take(reply);
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2010-11-29 14:34:30 +01:00
|
|
|
// Handle redirects.
|
2014-02-07 16:34:20 +01:00
|
|
|
QVariant redirect =
|
|
|
|
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
2010-11-29 14:34:30 +01:00
|
|
|
if (redirect.isValid()) {
|
|
|
|
if (++task.redirects > kMaxRedirects) {
|
|
|
|
return; // Give up.
|
|
|
|
}
|
|
|
|
QNetworkRequest request = reply->request();
|
|
|
|
request.setUrl(redirect.toUrl());
|
|
|
|
QNetworkReply* redirected_reply = network_->get(request);
|
2012-10-12 12:38:12 +02:00
|
|
|
NewClosure(redirected_reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(RemoteFetchFinished(QNetworkReply*)), redirected_reply);
|
2010-11-29 14:34:30 +01:00
|
|
|
|
|
|
|
remote_tasks_.insert(redirected_reply, task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
// Try to load the image
|
|
|
|
QImage image;
|
|
|
|
if (image.load(reply, 0)) {
|
2012-02-13 21:44:04 +01:00
|
|
|
QImage scaled = ScaleAndPad(task.options, image);
|
2011-01-31 21:53:38 +01:00
|
|
|
emit ImageLoaded(task.id, scaled);
|
|
|
|
emit ImageLoaded(task.id, scaled, image);
|
2010-05-10 15:15:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NextState(&task);
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
QImage AlbumCoverLoader::ScaleAndPad(const AlbumCoverLoaderOptions& options,
|
|
|
|
const QImage& image) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (image.isNull()) return image;
|
2010-05-10 15:15:52 +02:00
|
|
|
|
|
|
|
// Scale the image down
|
2010-12-18 20:42:06 +01:00
|
|
|
QImage copy;
|
2012-02-13 21:44:04 +01:00
|
|
|
if (options.scale_output_image_) {
|
|
|
|
copy = image.scaled(QSize(options.desired_height_, options.desired_height_),
|
2010-12-18 20:42:06 +01:00
|
|
|
Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
} else {
|
|
|
|
copy = image;
|
|
|
|
}
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!options.pad_output_image_) return copy;
|
2010-05-17 00:41:01 +02:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
// Pad the image to height_ x height_
|
2012-02-13 21:44:04 +01:00
|
|
|
QImage padded_image(options.desired_height_, options.desired_height_,
|
|
|
|
QImage::Format_ARGB32);
|
2010-05-17 00:41:01 +02:00
|
|
|
padded_image.fill(0);
|
2010-05-10 15:15:52 +02:00
|
|
|
|
2010-05-17 00:41:01 +02:00
|
|
|
QPainter p(&padded_image);
|
2012-02-13 21:44:04 +01:00
|
|
|
p.drawImage((options.desired_height_ - copy.width()) / 2,
|
2014-02-07 16:34:20 +01:00
|
|
|
(options.desired_height_ - copy.height()) / 2, copy);
|
2010-05-10 15:15:52 +02:00
|
|
|
p.end();
|
|
|
|
|
2010-05-17 00:41:01 +02:00
|
|
|
return padded_image;
|
2010-03-03 15:29:53 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 23:20:54 +01:00
|
|
|
QPixmap AlbumCoverLoader::TryLoadPixmap(const QString& automatic,
|
|
|
|
const QString& manual,
|
|
|
|
const QString& filename) {
|
2010-03-03 15:29:53 +01:00
|
|
|
QPixmap ret;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (manual == Song::kManuallyUnsetCover) return ret;
|
|
|
|
if (!manual.isEmpty()) ret.load(manual);
|
2010-12-28 23:20:54 +01:00
|
|
|
if (ret.isNull()) {
|
2011-02-02 22:01:08 +01:00
|
|
|
if (automatic == Song::kEmbeddedCover && !filename.isNull())
|
2012-01-06 22:27:02 +01:00
|
|
|
ret = QPixmap::fromImage(
|
2014-02-07 16:34:20 +01:00
|
|
|
TagReaderClient::Instance()->LoadEmbeddedArtBlocking(filename));
|
2010-12-28 23:20:54 +01:00
|
|
|
else if (!automatic.isEmpty())
|
|
|
|
ret.load(automatic);
|
|
|
|
}
|
2010-03-03 15:29:53 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2010-05-17 00:53:42 +02:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
quint64 AlbumCoverLoader::LoadImageAsync(const AlbumCoverLoaderOptions& options,
|
2014-02-07 16:34:20 +01:00
|
|
|
const Song& song) {
|
|
|
|
return LoadImageAsync(options, song.art_automatic(), song.art_manual(),
|
2011-04-28 14:27:53 +02:00
|
|
|
song.url().toLocalFile(), song.image());
|
2010-07-19 19:55:02 +02:00
|
|
|
}
|