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"
|
2011-04-29 15:41:42 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "core/logging.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
#include "core/network.h"
|
2012-01-06 22:27:02 +01:00
|
|
|
#include "core/tagreaderclient.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
#include "core/utilities.h"
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internet/internetmodel.h"
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
2011-04-29 16:07:30 +02:00
|
|
|
#ifdef HAVE_SPOTIFY
|
2011-07-15 15:27:50 +02:00
|
|
|
# include "internet/spotifyservice.h"
|
2011-04-29 16:07:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
AlbumCoverLoader::AlbumCoverLoader(QObject* parent)
|
|
|
|
: QObject(parent),
|
2010-03-25 15:33:09 +01:00
|
|
|
stop_requested_(false),
|
2012-02-26 16:12:39 +01:00
|
|
|
next_id_(1),
|
2011-04-29 15:41:42 +02:00
|
|
|
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_);
|
2012-02-13 21:44:04 +01:00
|
|
|
for (QQueue<Task>::iterator it = tasks_.begin() ; it != tasks_.end() ; ++it) {
|
|
|
|
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_);
|
|
|
|
for (QQueue<Task>::iterator it = tasks_.begin() ; it != tasks_.end() ; ) {
|
|
|
|
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_);
|
|
|
|
task.id = next_id_ ++;
|
|
|
|
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_);
|
|
|
|
if (tasks_.isEmpty())
|
|
|
|
return;
|
|
|
|
task = tasks_.dequeue();
|
|
|
|
}
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
ProcessTask(&task);
|
|
|
|
}
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
void AlbumCoverLoader::ProcessTask(Task *task) {
|
|
|
|
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())
|
2012-02-13 21:44:04 +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) {
|
|
|
|
case State_TryingAuto: filename = task.art_automatic; break;
|
|
|
|
case State_TryingManual: filename = task.art_manual; break;
|
|
|
|
}
|
|
|
|
|
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 =
|
|
|
|
TagReaderClient::Instance()->LoadEmbeddedArtBlocking(task.song_filename);
|
|
|
|
|
2010-07-19 19:55:02 +02:00
|
|
|
if (!taglib_image.isNull())
|
2012-02-13 21:44:04 +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://")) {
|
2010-10-16 19:20:54 +02:00
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(filename));
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(RemoteFetchFinished()));
|
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
|
|
|
|
#ifdef HAVE_SPOTIFY
|
2011-07-15 15:27:50 +02:00
|
|
|
SpotifyService* spotify = InternetModel::Service<SpotifyService>();
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
if (!connected_spotify_) {
|
2011-11-29 11:20:23 +01:00
|
|
|
connect(spotify, SIGNAL(ImageLoaded(QString,QImage)),
|
|
|
|
SLOT(SpotifyImageLoaded(QString,QImage)));
|
2011-04-29 15:41:42 +02:00
|
|
|
connected_spotify_ = true;
|
|
|
|
}
|
|
|
|
|
2011-11-29 11:20:23 +01:00
|
|
|
QString id = QUrl(filename).path();
|
|
|
|
if (id.startsWith('/')) {
|
|
|
|
id.remove(0, 1);
|
|
|
|
}
|
|
|
|
remote_spotify_tasks_.insert(id, task);
|
2011-04-29 15:41:42 +02:00
|
|
|
|
|
|
|
// Need to schedule this in the spotify service's thread
|
|
|
|
QMetaObject::invokeMethod(spotify, "LoadImage", Qt::QueuedConnection,
|
2011-11-29 11:20:23 +01:00
|
|
|
Q_ARG(QString, id));
|
2011-04-29 15:41:42 +02:00
|
|
|
return TryLoadResult(true, false, QImage());
|
|
|
|
#else
|
|
|
|
return TryLoadResult(false, false, QImage());
|
|
|
|
#endif
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
2010-05-10 15:15:52 +02:00
|
|
|
|
|
|
|
QImage image(filename);
|
2012-02-13 21:44:04 +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
|
|
|
|
2011-11-29 11:20:23 +01:00
|
|
|
void AlbumCoverLoader::SpotifyImageLoaded(const QString& id, const QImage& image) {
|
|
|
|
if (!remote_spotify_tasks_.contains(id))
|
2011-04-29 15:41:42 +02:00
|
|
|
return;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
void AlbumCoverLoader::RemoteFetchFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
if (!reply)
|
|
|
|
return;
|
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.
|
|
|
|
QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
|
|
if (redirect.isValid()) {
|
|
|
|
if (++task.redirects > kMaxRedirects) {
|
|
|
|
return; // Give up.
|
|
|
|
}
|
|
|
|
QNetworkRequest request = reply->request();
|
|
|
|
request.setUrl(redirect.toUrl());
|
|
|
|
QNetworkReply* redirected_reply = network_->get(request);
|
|
|
|
connect(redirected_reply, SIGNAL(finished()), SLOT(RemoteFetchFinished()));
|
|
|
|
|
|
|
|
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) {
|
2010-05-10 15:15:52 +02:00
|
|
|
if (image.isNull())
|
|
|
|
return image;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
if (!options.pad_output_image_)
|
2010-05-17 00:41:01 +02:00
|
|
|
return copy;
|
|
|
|
|
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,
|
|
|
|
(options.desired_height_ - copy.height()) / 2,
|
2010-05-10 15:15:52 +02:00
|
|
|
copy);
|
|
|
|
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;
|
2011-02-02 22:01:08 +01:00
|
|
|
if (manual == Song::kManuallyUnsetCover)
|
2010-03-03 15:29:53 +01:00
|
|
|
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(
|
|
|
|
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,
|
|
|
|
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
|
|
|
}
|