2013-05-06 21:22:05 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-26 13:34:53 +01:00
|
|
|
Copyright 2013, Andreas <asfa194@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2013-05-06 21:22:05 +02: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 14:57:02 +01:00
|
|
|
#include "coverexportrunnable.h"
|
2013-05-06 21:22:05 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2014-12-26 14:57:02 +01:00
|
|
|
#include "albumcoverexporter.h"
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/tagreaderclient.h"
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
CoverExportRunnable::CoverExportRunnable(
|
|
|
|
const AlbumCoverExport::DialogResult& dialog_result, const Song& song)
|
|
|
|
: dialog_result_(dialog_result), song_(song) {}
|
2013-05-06 21:22:05 +02:00
|
|
|
|
|
|
|
void CoverExportRunnable::run() {
|
|
|
|
QString cover_path = GetCoverPath();
|
|
|
|
|
|
|
|
// manually unset?
|
2014-02-07 16:34:20 +01:00
|
|
|
if (cover_path.isEmpty()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (dialog_result_.RequiresCoverProcessing()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
ProcessAndExportCover();
|
|
|
|
} else {
|
|
|
|
ExportCover();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CoverExportRunnable::GetCoverPath() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song_.has_manually_unset_cover()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
return QString();
|
2014-02-07 16:34:20 +01:00
|
|
|
// Export downloaded covers?
|
|
|
|
} else if (!song_.art_manual().isEmpty() &&
|
|
|
|
dialog_result_.export_downloaded_) {
|
2013-05-06 21:22:05 +02:00
|
|
|
return song_.art_manual();
|
2014-02-07 16:34:20 +01:00
|
|
|
// Export embedded covers?
|
|
|
|
} else if (!song_.art_automatic().isEmpty() &&
|
|
|
|
song_.art_automatic() == Song::kEmbeddedCover &&
|
|
|
|
dialog_result_.export_embedded_) {
|
2013-05-06 21:22:05 +02:00
|
|
|
return song_.art_automatic();
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports a single album cover using a "save QImage to file" approach.
|
|
|
|
// For performance reasons this method will be invoked only if loading
|
|
|
|
// and in memory processing of images is necessary for current settings
|
|
|
|
// which means that:
|
|
|
|
// - either the force size flag is being used
|
|
|
|
// - or the "overwrite smaller" mode is used
|
|
|
|
// In all other cases, the faster ExportCover() method will be used.
|
|
|
|
void CoverExportRunnable::ProcessAndExportCover() {
|
|
|
|
QString cover_path = GetCoverPath();
|
|
|
|
|
|
|
|
// either embedded or disk - the one we'll export for the current album
|
|
|
|
QImage cover;
|
|
|
|
|
|
|
|
QImage embedded_cover;
|
|
|
|
QImage disk_cover;
|
|
|
|
|
|
|
|
// load embedded cover if any
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song_.has_embedded_cover()) {
|
|
|
|
embedded_cover = TagReaderClient::Instance()->LoadEmbeddedArtBlocking(
|
|
|
|
song_.url().toLocalFile());
|
2013-05-06 21:22:05 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (embedded_cover.isNull()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cover = embedded_cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load a file cover which iss mandatory if there's no embedded cover
|
|
|
|
disk_cover.load(cover_path);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (embedded_cover.isNull()) {
|
|
|
|
if (disk_cover.isNull()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cover = disk_cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rescale if necessary
|
2014-02-07 16:34:20 +01:00
|
|
|
if (dialog_result_.IsSizeForced()) {
|
|
|
|
cover = cover.scaled(QSize(dialog_result_.width_, dialog_result_.height_),
|
|
|
|
Qt::IgnoreAspectRatio);
|
2013-05-06 21:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString dir = song_.url().toLocalFile().section('/', 0, -2);
|
|
|
|
QString extension = cover_path.section('.', -1);
|
|
|
|
|
|
|
|
QString new_file = dir + '/' + dialog_result_.fileName_ + '.' +
|
2014-02-07 16:34:20 +01:00
|
|
|
(cover_path == Song::kEmbeddedCover ? "jpg" : extension);
|
2013-05-06 21:22:05 +02:00
|
|
|
|
|
|
|
// If the file exists, do not override!
|
2014-12-26 13:34:53 +01:00
|
|
|
if (dialog_result_.overwrite_ == AlbumCoverExport::OverwriteMode_None && QFile::exists(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// we're handling overwrite as remove + copy so we need to delete the old file
|
|
|
|
// first
|
2014-12-26 13:34:53 +01:00
|
|
|
if (QFile::exists(new_file) && dialog_result_.overwrite_ != AlbumCoverExport::OverwriteMode_None) {
|
2013-05-06 21:22:05 +02:00
|
|
|
// if the mode is "overwrite smaller" then skip the cover if a bigger one
|
|
|
|
// is already available in the folder
|
2014-02-07 16:34:20 +01:00
|
|
|
if (dialog_result_.overwrite_ == AlbumCoverExport::OverwriteMode_Smaller) {
|
2013-05-06 21:22:05 +02:00
|
|
|
QImage existing;
|
|
|
|
existing.load(new_file);
|
|
|
|
|
2014-12-26 13:34:53 +01:00
|
|
|
if (existing.isNull() || existing.size().height() >= cover.size().height() ||
|
2014-02-07 16:34:20 +01:00
|
|
|
existing.size().width() >= cover.size().width()) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!QFile::remove(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (cover.save(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverExported();
|
|
|
|
} else {
|
|
|
|
EmitCoverSkipped();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports a single album cover using a "copy file" approach.
|
|
|
|
void CoverExportRunnable::ExportCover() {
|
|
|
|
QString cover_path = GetCoverPath();
|
|
|
|
|
|
|
|
QString dir = song_.url().toLocalFile().section('/', 0, -2);
|
|
|
|
QString extension = cover_path.section('.', -1);
|
|
|
|
|
|
|
|
QString new_file = dir + '/' + dialog_result_.fileName_ + '.' +
|
2014-02-07 16:34:20 +01:00
|
|
|
(cover_path == Song::kEmbeddedCover ? "jpg" : extension);
|
2013-05-06 21:22:05 +02:00
|
|
|
|
|
|
|
// If the file exists, do not override!
|
2014-02-07 16:34:20 +01:00
|
|
|
if (dialog_result_.overwrite_ == AlbumCoverExport::OverwriteMode_None &&
|
|
|
|
QFile::exists(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// we're handling overwrite as remove + copy so we need to delete the old file
|
|
|
|
// first
|
|
|
|
if (dialog_result_.overwrite_ != AlbumCoverExport::OverwriteMode_None &&
|
|
|
|
QFile::exists(new_file)) {
|
|
|
|
if (!QFile::remove(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (cover_path == Song::kEmbeddedCover) {
|
2013-05-06 21:22:05 +02:00
|
|
|
// an embedded cover
|
2014-02-07 16:34:20 +01:00
|
|
|
QImage embedded = TagReaderClient::Instance()->LoadEmbeddedArtBlocking(
|
|
|
|
song_.url().toLocalFile());
|
|
|
|
if (!embedded.save(new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// automatic or manual cover, available in an image file
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!QFile::copy(cover_path, new_file)) {
|
2013-05-06 21:22:05 +02:00
|
|
|
EmitCoverSkipped();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EmitCoverExported();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void CoverExportRunnable::EmitCoverExported() { emit CoverExported(); }
|
2013-05-06 21:22:05 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void CoverExportRunnable::EmitCoverSkipped() { emit CoverSkipped(); }
|