Add option to save album cover in album directory
This commit is contained in:
@@ -51,6 +51,8 @@
|
||||
#include "core/application.h"
|
||||
|
||||
#include "collection/collectionbackend.h"
|
||||
#include "settings/collectionsettingspage.h"
|
||||
#include "organise/organiseformat.h"
|
||||
#include "albumcoverchoicecontroller.h"
|
||||
#include "albumcoverfetcher.h"
|
||||
#include "albumcoverloader.h"
|
||||
@@ -70,7 +72,13 @@ AlbumCoverChoiceController::AlbumCoverChoiceController(QWidget *parent) :
|
||||
cover_searcher_(nullptr),
|
||||
cover_fetcher_(nullptr),
|
||||
save_file_dialog_(nullptr),
|
||||
cover_from_url_dialog_(nullptr) {
|
||||
cover_from_url_dialog_(nullptr),
|
||||
cover_album_dir_(false),
|
||||
cover_filename_(CollectionSettingsPage::SaveCover_Hash),
|
||||
cover_overwrite_(false),
|
||||
cover_lowercase_(true),
|
||||
cover_replace_spaces_(true)
|
||||
{
|
||||
|
||||
cover_from_file_ = new QAction(IconLoader::Load("document-open"), tr("Load cover from disk..."), this);
|
||||
cover_to_file_ = new QAction(IconLoader::Load("document-save"), tr("Save cover to disk..."), this);
|
||||
@@ -90,6 +98,20 @@ AlbumCoverChoiceController::AlbumCoverChoiceController(QWidget *parent) :
|
||||
|
||||
AlbumCoverChoiceController::~AlbumCoverChoiceController() {}
|
||||
|
||||
void AlbumCoverChoiceController::ReloadSettings() {
|
||||
|
||||
QSettings s;
|
||||
s.beginGroup(CollectionSettingsPage::kSettingsGroup);
|
||||
cover_album_dir_ = s.value("cover_album_dir", false).toBool();
|
||||
cover_filename_ = CollectionSettingsPage::SaveCover(s.value("cover_filename", CollectionSettingsPage::SaveCover_Hash).toInt());
|
||||
cover_pattern_ = s.value("cover_pattern", "%albumartist-%album").toString();
|
||||
cover_overwrite_ = s.value("cover_overwrite", false).toBool();
|
||||
cover_lowercase_ = s.value("cover_lowercase", false).toBool();
|
||||
cover_replace_spaces_ = s.value("cover_replace_spaces", false).toBool();
|
||||
s.endGroup();
|
||||
|
||||
}
|
||||
|
||||
void AlbumCoverChoiceController::SetApplication(Application *app) {
|
||||
|
||||
app_ = app;
|
||||
@@ -125,9 +147,17 @@ QString AlbumCoverChoiceController::LoadCoverFromFile(Song *song) {
|
||||
|
||||
}
|
||||
|
||||
void AlbumCoverChoiceController::SaveCoverToFile(const Song &song, const QImage &image) {
|
||||
void AlbumCoverChoiceController::SaveCoverToFileManual(const Song &song, const QImage &image) {
|
||||
|
||||
QString initial_file_name = "/" + (song.effective_album().isEmpty() ? tr("Unknown") : song.effective_album()) + ".jpg";
|
||||
QString initial_file_name = "/";
|
||||
|
||||
if (!song.effective_albumartist().isEmpty()) {
|
||||
initial_file_name = initial_file_name + song.effective_albumartist();
|
||||
}
|
||||
initial_file_name = initial_file_name + "-" + (song.effective_album().isEmpty() ? tr("unknown") : song.effective_album()) + ".jpg";
|
||||
initial_file_name = initial_file_name.toLower();
|
||||
initial_file_name.replace(QRegExp("\\s"), "-");
|
||||
initial_file_name.remove(OrganiseFormat::kValidFatCharacters);
|
||||
|
||||
QString save_filename = QFileDialog::getSaveFileName(this, tr("Save album cover"), GetInitialPathForFileDialog(song, initial_file_name), tr(kSaveImageFileFilter) + ";;" + tr(kAllFilesFilter));
|
||||
|
||||
@@ -168,9 +198,9 @@ QString AlbumCoverChoiceController::LoadCoverFromURL(Song *song) {
|
||||
QImage image = cover_from_url_dialog_->Exec();
|
||||
|
||||
if (!image.isNull()) {
|
||||
QString cover = SaveCoverInCache(song->artist(), song->album(), image);
|
||||
QString cover = SaveCoverToFileAutomatic(song, image);
|
||||
if (cover.isEmpty()) return QString();
|
||||
SaveCover(song, cover);
|
||||
|
||||
return cover;
|
||||
}
|
||||
else { return QString(); }
|
||||
@@ -187,7 +217,8 @@ QString AlbumCoverChoiceController::SearchForCover(Song *song) {
|
||||
QImage image = cover_searcher_->Exec(song->effective_albumartist(), album);
|
||||
|
||||
if (!image.isNull()) {
|
||||
QString cover = SaveCoverInCache(song->artist(), song->album(), image);
|
||||
QString cover = SaveCoverToFileAutomatic(song, image);
|
||||
if (cover.isEmpty()) return QString();
|
||||
SaveCover(song, cover);
|
||||
|
||||
return cover;
|
||||
@@ -282,7 +313,8 @@ void AlbumCoverChoiceController::AlbumCoverFetched(quint64 id, const QImage &ima
|
||||
}
|
||||
|
||||
if (!image.isNull()) {
|
||||
QString cover = SaveCoverInCache(song.artist(), song.album(), image);
|
||||
QString cover = SaveCoverToFileAutomatic(&song, image);
|
||||
if (cover.isEmpty()) return;
|
||||
SaveCover(&song, cover);
|
||||
}
|
||||
|
||||
@@ -303,23 +335,71 @@ void AlbumCoverChoiceController::SaveCover(Song *song, const QString &cover) {
|
||||
|
||||
}
|
||||
|
||||
QString AlbumCoverChoiceController::SaveCoverInCache(const QString &artist, const QString &album, const QImage &image) {
|
||||
QString AlbumCoverChoiceController::SaveCoverToFileAutomatic(const Song *song, const QImage &image) {
|
||||
|
||||
QString album2(album);
|
||||
album2.remove(Song::kAlbumRemoveDisc);
|
||||
QString albumartist(song->effective_albumartist());
|
||||
QString artist(song->artist());
|
||||
QString album(song->effective_album());
|
||||
album.remove(Song::kAlbumRemoveDisc);
|
||||
|
||||
// Hash the artist and album into a filename for the image
|
||||
QString filename(Utilities::Sha1CoverHash(artist, album2).toHex() + ".jpg");
|
||||
QString path(AlbumCoverLoader::ImageCacheDir() + "/" + filename);
|
||||
return SaveCoverToFileAutomatic(albumartist, artist, album, song->url().adjusted(QUrl::RemoveFilename).path(), image);
|
||||
|
||||
}
|
||||
|
||||
QString AlbumCoverChoiceController::SaveCoverToFileAutomatic(const QString &albumartist, const QString &artist, const QString &album, const QString &album_dir, const QImage &image) {
|
||||
|
||||
QString album_new(album);
|
||||
album_new.remove(Song::kAlbumRemoveDisc);
|
||||
|
||||
QString path;
|
||||
QString filename;
|
||||
if (cover_album_dir_) {
|
||||
path = album_dir;
|
||||
}
|
||||
else {
|
||||
path = AlbumCoverLoader::ImageCacheDir();
|
||||
}
|
||||
|
||||
if (path.right(1) == QDir::separator()) {
|
||||
path.chop(1);
|
||||
}
|
||||
|
||||
// Make sure this directory exists first
|
||||
QDir dir;
|
||||
dir.mkdir(AlbumCoverLoader::ImageCacheDir());
|
||||
if (!dir.mkpath(path)) {
|
||||
qLog(Error) << "Unable to create directory" << path;
|
||||
return QString();
|
||||
}
|
||||
|
||||
// Save the image to disk
|
||||
image.save(path, "JPG");
|
||||
if (cover_album_dir_ && cover_filename_ == CollectionSettingsPage::SaveCover_Pattern && !cover_pattern_.isEmpty()) {
|
||||
filename = CreateCoverFilename(albumartist, artist, album_new) + ".jpg";
|
||||
filename.remove(OrganiseFormat::kValidFatCharacters);
|
||||
if (cover_lowercase_) filename = filename.toLower();
|
||||
if (cover_replace_spaces_) filename.replace(QRegExp("\\s"), "-");
|
||||
}
|
||||
else {
|
||||
filename = Utilities::Sha1CoverHash(albumartist, album_new).toHex() + ".jpg";
|
||||
}
|
||||
|
||||
return path;
|
||||
QString filepath(path + "/" + filename);
|
||||
|
||||
// Don't overwrite when saving in album dir if the filename is set to pattern unless the "cover_overwrite" is set.
|
||||
if (QFile::exists(filepath) && !cover_overwrite_ && cover_album_dir_ && cover_filename_ == CollectionSettingsPage::SaveCover_Pattern) {
|
||||
return filepath;
|
||||
}
|
||||
|
||||
image.save(filepath, "JPG");
|
||||
|
||||
return filepath;
|
||||
|
||||
}
|
||||
|
||||
QString AlbumCoverChoiceController::CreateCoverFilename(const QString &albumartist, const QString &artist, const QString &album) {
|
||||
|
||||
QString filename(cover_pattern_);
|
||||
filename.replace("%albumartist", albumartist);
|
||||
filename.replace("%artist", artist);
|
||||
filename.replace("%album", album);
|
||||
return filename;
|
||||
|
||||
}
|
||||
|
||||
@@ -363,7 +443,8 @@ QString AlbumCoverChoiceController::SaveCover(Song *song, const QDropEvent *e) {
|
||||
if (e->mimeData()->hasImage()) {
|
||||
QImage image = qvariant_cast<QImage>(e->mimeData()->imageData());
|
||||
if (!image.isNull()) {
|
||||
QString cover_path = SaveCoverInCache(song->artist(), song->album(), image);
|
||||
QString cover_path = SaveCoverToFileAutomatic(song, image);
|
||||
if (cover_path.isEmpty()) return QString();
|
||||
SaveCover(song, cover_path);
|
||||
return cover_path;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
#include <QFileDialog>
|
||||
#include <QtEvents>
|
||||
|
||||
#include "settings/collectionsettingspage.h"
|
||||
|
||||
class Song;
|
||||
class Application;
|
||||
class AlbumCoverFetcher;
|
||||
@@ -57,6 +59,7 @@ class AlbumCoverChoiceController : public QWidget {
|
||||
~AlbumCoverChoiceController();
|
||||
|
||||
void SetApplication(Application *app);
|
||||
void ReloadSettings();
|
||||
|
||||
// Getters for all QActions implemented by this controller.
|
||||
|
||||
@@ -86,7 +89,7 @@ class AlbumCoverChoiceController : public QWidget {
|
||||
|
||||
// Shows a dialog that allows user to save the given image on disk.
|
||||
// The image is supposed to be the cover of the given song's album.
|
||||
void SaveCoverToFile(const Song &song, const QImage &image);
|
||||
void SaveCoverToFileManual(const Song &song, const QImage &image);
|
||||
|
||||
// Downloads the cover from an URL given by user.
|
||||
// This returns the downloaded image or null image if something went wrong for example when user cancelled the dialog.
|
||||
@@ -113,8 +116,10 @@ class AlbumCoverChoiceController : public QWidget {
|
||||
// Saves the cover that the user picked through a drag and drop operation.
|
||||
QString SaveCover(Song *song, const QDropEvent *e);
|
||||
|
||||
// Saves the given image in cache as a cover for 'artist' - 'album'. The method returns path of the cached image.
|
||||
QString SaveCoverInCache(const QString &artist, const QString &album, const QImage &image);
|
||||
// Saves the given image in album directory or cache as a cover for 'album artist' - 'album'. The method returns path of the image.
|
||||
QString SaveCoverToFileAutomatic(const QString &albumartist, const QString &artist, const QString &album, const QString &album_dir, const QImage &image);
|
||||
QString SaveCoverToFileAutomatic(const Song *song, const QImage &image);
|
||||
QString CreateCoverFilename(const QString &albumartist, const QString &artist, const QString &album);
|
||||
|
||||
static bool CanAcceptDrag(const QDragEnterEvent *e);
|
||||
|
||||
@@ -147,6 +152,14 @@ signals:
|
||||
QAction *search_cover_auto_;
|
||||
|
||||
QMap<quint64, Song> cover_fetching_tasks_;
|
||||
|
||||
bool cover_album_dir_;
|
||||
CollectionSettingsPage::SaveCover cover_filename_;
|
||||
QString cover_pattern_;
|
||||
bool cover_overwrite_;
|
||||
bool cover_lowercase_;
|
||||
bool cover_replace_spaces_;
|
||||
|
||||
};
|
||||
|
||||
#endif // ALBUMCOVERCHOICECONTROLLER_H
|
||||
|
||||
@@ -138,6 +138,8 @@ AlbumCoverManager::AlbumCoverManager(Application *app, CollectionBackend *collec
|
||||
|
||||
EnableCoversButtons();
|
||||
|
||||
ReloadSettings();
|
||||
|
||||
}
|
||||
|
||||
AlbumCoverManager::~AlbumCoverManager() {
|
||||
@@ -145,6 +147,10 @@ AlbumCoverManager::~AlbumCoverManager() {
|
||||
delete ui_;
|
||||
}
|
||||
|
||||
void AlbumCoverManager::ReloadSettings() {
|
||||
album_cover_choice_controller_->ReloadSettings();
|
||||
}
|
||||
|
||||
CollectionBackend *AlbumCoverManager::backend() const {
|
||||
return collection_backend_;
|
||||
}
|
||||
@@ -629,7 +635,7 @@ void AlbumCoverManager::SaveCoverToFile() {
|
||||
}
|
||||
}
|
||||
|
||||
album_cover_choice_controller_->SaveCoverToFile(song, image);
|
||||
album_cover_choice_controller_->SaveCoverToFileManual(song, image);
|
||||
|
||||
}
|
||||
|
||||
@@ -777,8 +783,10 @@ void AlbumCoverManager::SaveAndSetCover(QListWidgetItem *item, const QImage &ima
|
||||
const QString artist = item->data(Role_ArtistName).toString();
|
||||
const QString albumartist = item->data(Role_AlbumArtistName).toString();
|
||||
const QString album = item->data(Role_AlbumName).toString();
|
||||
const QUrl url = item->data(Role_FirstUrl).toUrl();
|
||||
|
||||
QString path = album_cover_choice_controller_->SaveCoverInCache(artist, album, image);
|
||||
QString path = album_cover_choice_controller_->SaveCoverToFileAutomatic((!albumartist.isEmpty() ? albumartist : artist), artist, album, url.adjusted(QUrl::RemoveFilename).path(), image);
|
||||
if (path.isEmpty()) return;
|
||||
|
||||
// Save the image in the database
|
||||
collection_backend_->UpdateManualAlbumArtAsync(artist, albumartist, album, path);
|
||||
|
||||
@@ -72,6 +72,7 @@ class AlbumCoverManager : public QMainWindow {
|
||||
|
||||
void Reset();
|
||||
void Init();
|
||||
void ReloadSettings();
|
||||
|
||||
void EnableCoversButtons();
|
||||
void DisableCoversButtons();
|
||||
|
||||
Reference in New Issue
Block a user