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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2021-06-21 15:40:25 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QDir>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QImage>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
|
|
|
|
#include "core/application.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "core/song.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "playlist/playlistmanager.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "albumcoverloader.h"
|
2020-04-20 18:03:18 +02:00
|
|
|
#include "albumcoverloaderresult.h"
|
2019-07-07 21:14:24 +02:00
|
|
|
#include "currentalbumcoverloader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
using std::make_unique;
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
CurrentAlbumCoverLoader::CurrentAlbumCoverLoader(Application *app, QObject *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: QObject(parent),
|
|
|
|
app_(app),
|
2024-04-11 02:56:01 +02:00
|
|
|
temp_file_pattern_(QDir::tempPath() + QStringLiteral("/strawberry-cover-XXXXXX.jpg")),
|
2021-07-11 07:40:57 +02:00
|
|
|
id_(0) {
|
2019-02-09 14:51:12 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
options_.options = AlbumCoverLoaderOptions::Option::RawImageData | AlbumCoverLoaderOptions::Option::OriginalImage | AlbumCoverLoaderOptions::Option::ScaledImage;
|
|
|
|
options_.desired_scaled_size = QSize(120, 120);
|
2024-04-09 23:20:26 +02:00
|
|
|
options_.default_cover = QStringLiteral(":/pictures/cdcase.png");
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
QObject::connect(&*app_->playlist_manager(), &PlaylistManager::CurrentSongChanged, this, &CurrentAlbumCoverLoader::LoadAlbumCover);
|
|
|
|
QObject::connect(&*app_->album_cover_loader(), &AlbumCoverLoader::AlbumCoverLoaded, this, &CurrentAlbumCoverLoader::AlbumCoverReady);
|
2019-02-09 14:51:12 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
ReloadSettingsAsync();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
CurrentAlbumCoverLoader::~CurrentAlbumCoverLoader() {
|
2020-04-20 18:03:18 +02:00
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
if (temp_cover_) temp_cover_->remove();
|
|
|
|
if (temp_cover_thumbnail_) temp_cover_thumbnail_->remove();
|
2020-04-20 18:03:18 +02:00
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
void CurrentAlbumCoverLoader::ReloadSettingsAsync() {
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, &CurrentAlbumCoverLoader::ReloadSettings);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentAlbumCoverLoader::ReloadSettings() {
|
|
|
|
|
|
|
|
options_.types = AlbumCoverLoaderOptions::LoadTypes();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-07 21:14:24 +02:00
|
|
|
void CurrentAlbumCoverLoader::LoadAlbumCover(const Song &song) {
|
2020-04-20 18:03:18 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
last_song_ = song;
|
|
|
|
id_ = app_->album_cover_loader()->LoadImageAsync(options_, last_song_);
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-06-29 00:44:00 +02:00
|
|
|
void CurrentAlbumCoverLoader::AlbumCoverReady(const quint64 id, AlbumCoverLoaderResult result) {
|
2019-09-15 20:27:32 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (id != id_) return;
|
|
|
|
id_ = 0;
|
|
|
|
|
2023-04-09 22:26:17 +02:00
|
|
|
if (!result.album_cover.image.isNull()) {
|
2023-07-21 05:55:24 +02:00
|
|
|
temp_cover_ = make_unique<QTemporaryFile>(temp_file_pattern_);
|
2019-07-07 21:14:24 +02:00
|
|
|
temp_cover_->setAutoRemove(true);
|
2020-04-20 18:03:18 +02:00
|
|
|
if (temp_cover_->open()) {
|
2023-04-09 22:26:17 +02:00
|
|
|
if (result.album_cover.image.save(temp_cover_->fileName(), "JPEG")) {
|
|
|
|
result.temp_cover_url = QUrl::fromLocalFile(temp_cover_->fileName());
|
2020-04-20 18:03:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-08-09 23:32:26 +02:00
|
|
|
qLog(Error) << "Failed to save cover image to" << temp_cover_->fileName() << temp_cover_->errorString();
|
2020-04-20 18:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-08-09 23:32:26 +02:00
|
|
|
qLog(Error) << "Failed to open" << temp_cover_->fileName() << temp_cover_->errorString();
|
2020-04-20 18:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
QUrl thumbnail_url;
|
2023-05-14 11:34:55 +02:00
|
|
|
if (!result.image_scaled.isNull()) {
|
2023-07-21 05:55:24 +02:00
|
|
|
temp_cover_thumbnail_ = make_unique<QTemporaryFile>(temp_file_pattern_);
|
2019-07-07 21:14:24 +02:00
|
|
|
temp_cover_thumbnail_->setAutoRemove(true);
|
2020-04-20 18:03:18 +02:00
|
|
|
if (temp_cover_thumbnail_->open()) {
|
2023-05-14 11:34:55 +02:00
|
|
|
if (result.image_scaled.save(temp_cover_thumbnail_->fileName(), "JPEG")) {
|
2020-04-20 18:03:18 +02:00
|
|
|
thumbnail_url = QUrl::fromLocalFile(temp_cover_thumbnail_->fileName());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Error) << "Unable to save cover thumbnail image to" << temp_cover_thumbnail_->fileName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Error) << "Unable to open" << temp_cover_thumbnail_->fileName();
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 21:14:24 +02:00
|
|
|
|
2023-06-29 00:44:00 +02:00
|
|
|
if (result.art_manual_updated.isValid()) {
|
|
|
|
last_song_.set_art_manual(result.art_manual_updated);
|
|
|
|
}
|
|
|
|
if (result.art_automatic_updated.isValid()) {
|
|
|
|
last_song_.set_art_automatic(result.art_automatic_updated);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:03:18 +02:00
|
|
|
emit AlbumCoverLoaded(last_song_, result);
|
2023-05-14 11:34:55 +02:00
|
|
|
emit ThumbnailLoaded(last_song_, thumbnail_url, result.image_scaled);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|