Add option to save album cover in album directory

This commit is contained in:
Jonas Kvinge 2019-03-11 23:07:11 +01:00
parent 242137a50c
commit 2211716d04
9 changed files with 308 additions and 55 deletions

View File

@ -860,6 +860,7 @@ void MainWindow::ReloadAllSettings() {
osd_->ReloadSettings();
collection_view_->ReloadSettings();
ui_->playlist->view()->ReloadSettings();
if (cover_manager_.get()) cover_manager_->ReloadSettings();
#ifdef HAVE_STREAM_TIDAL
tidal_search_view_->ReloadSettings();
#endif
@ -2326,7 +2327,7 @@ void MainWindow::SearchForCover() {
}
void MainWindow::SaveCoverToFile() {
album_cover_choice_controller_->SaveCoverToFile(song_, image_original_);
album_cover_choice_controller_->SaveCoverToFileManual(song_, image_original_);
}
void MainWindow::UnsetCover() {

View File

@ -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;
}

View File

@ -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

View File

@ -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);

View File

@ -72,6 +72,7 @@ class AlbumCoverManager : public QMainWindow {
void Reset();
void Init();
void ReloadSettings();
void EnableCoversButtons();
void DisableCoversButtons();

View File

@ -634,7 +634,7 @@ void EditTagDialog::SaveCoverToFile() {
Song *song = GetFirstSelected();
if (!song) return;
album_cover_choice_controller_->SaveCoverToFile(*song, original_);
album_cover_choice_controller_->SaveCoverToFileManual(*song, original_);
}

View File

@ -43,10 +43,12 @@
const char *CollectionSettingsPage::kSettingsGroup = "Collection";
CollectionSettingsPage::CollectionSettingsPage(SettingsDialog* dialog)
CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog)
: SettingsPage(dialog),
ui_(new Ui_CollectionSettingsPage),
initialised_model_(false) {
initialised_model_(false)
{
ui_->setupUi(this);
ui_->list->setItemDelegate(new NativeSeparatorsDelegate(this));
@ -56,6 +58,11 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog* dialog)
connect(ui_->add, SIGNAL(clicked()), SLOT(Add()));
connect(ui_->remove, SIGNAL(clicked()), SLOT(Remove()));
connect(ui_->checkbox_cover_album_dir, SIGNAL(toggled(bool)), SLOT(CoverSaveInAlbumDirChanged()));
connect(ui_->radiobutton_cover_hash, SIGNAL(toggled(bool)), SLOT(CoverSaveInAlbumDirChanged()));
connect(ui_->radiobutton_cover_pattern, SIGNAL(toggled(bool)), SLOT(CoverSaveInAlbumDirChanged()));
}
CollectionSettingsPage::~CollectionSettingsPage() { delete ui_; }
@ -83,25 +90,6 @@ void CollectionSettingsPage::CurrentRowChanged(const QModelIndex& index) {
ui_->remove->setEnabled(index.isValid());
}
void CollectionSettingsPage::Save() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("auto_open", ui_->auto_open->isChecked());
s.setValue("pretty_covers", ui_->pretty_covers->isChecked());
s.setValue("show_dividers", ui_->show_dividers->isChecked());
s.setValue("startup_scan", ui_->startup_scan->isChecked());
s.setValue("monitor", ui_->monitor->isChecked());
QString filter_text = ui_->cover_art_patterns->text();
QStringList filters = filter_text.split(',', QString::SkipEmptyParts);
s.setValue("cover_art_patterns", filters);
s.endGroup();
}
void CollectionSettingsPage::Load() {
if (!initialised_model_) {
@ -127,5 +115,74 @@ void CollectionSettingsPage::Load() {
QStringList filters = s.value("cover_art_patterns", QStringList() << "front" << "cover").toStringList();
ui_->cover_art_patterns->setText(filters.join(","));
ui_->checkbox_cover_album_dir->setChecked(s.value("cover_album_dir", false).toBool());
SaveCover save_cover = SaveCover(s.value("cover_filename", SaveCover_Hash).toInt());
switch (save_cover) {
case SaveCover_Hash: ui_->radiobutton_cover_hash->setChecked(true); break;
case SaveCover_Pattern: ui_->radiobutton_cover_pattern->setChecked(true); break;
}
QString cover_pattern = s.value("cover_pattern").toString();
if (!cover_pattern.isEmpty()) ui_->lineedit_cover_pattern->setText(cover_pattern);
ui_->checkbox_cover_overwrite->setChecked(s.value("cover_overwrite", false).toBool());
ui_->checkbox_cover_lowercase->setChecked(s.value("cover_lowercase", true).toBool());
ui_->checkbox_cover_replace_spaces->setChecked(s.value("cover_replace_spaces", true).toBool());
s.endGroup();
}
void CollectionSettingsPage::Save() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("auto_open", ui_->auto_open->isChecked());
s.setValue("pretty_covers", ui_->pretty_covers->isChecked());
s.setValue("show_dividers", ui_->show_dividers->isChecked());
s.setValue("startup_scan", ui_->startup_scan->isChecked());
s.setValue("monitor", ui_->monitor->isChecked());
QString filter_text = ui_->cover_art_patterns->text();
QStringList filters = filter_text.split(',', QString::SkipEmptyParts);
s.setValue("cover_art_patterns", filters);
s.setValue("cover_album_dir", ui_->checkbox_cover_album_dir->isChecked());
SaveCover save_cover = SaveCover_Hash;
if (ui_->radiobutton_cover_hash->isChecked()) save_cover = SaveCover_Hash;
if (ui_->radiobutton_cover_pattern->isChecked()) save_cover = SaveCover_Pattern;
s.setValue("cover_filename", int(save_cover));
s.setValue("cover_pattern", ui_->lineedit_cover_pattern->text());
s.setValue("cover_overwrite", ui_->checkbox_cover_overwrite->isChecked());
s.setValue("cover_lowercase", ui_->checkbox_cover_lowercase->isChecked());
s.setValue("cover_replace_spaces", ui_->checkbox_cover_replace_spaces->isChecked());
s.endGroup();
}
void CollectionSettingsPage::CoverSaveInAlbumDirChanged() {
if (ui_->checkbox_cover_album_dir->isChecked()) {
if (!ui_->groupbox_cover_filename->isEnabled()) {
ui_->groupbox_cover_filename->setEnabled(true);
}
if (ui_->radiobutton_cover_pattern->isChecked()) {
if (!ui_->lineedit_cover_pattern->isEnabled()) ui_->lineedit_cover_pattern->setEnabled(true);
if (!ui_->checkbox_cover_overwrite->isEnabled()) ui_->checkbox_cover_overwrite->setEnabled(true);
if (!ui_->checkbox_cover_lowercase->isEnabled()) ui_->checkbox_cover_lowercase->setEnabled(true);
if (!ui_->checkbox_cover_replace_spaces->isEnabled()) ui_->checkbox_cover_replace_spaces->setEnabled(true);
}
else {
if (ui_->lineedit_cover_pattern->isEnabled()) ui_->lineedit_cover_pattern->setEnabled(false);
if (ui_->checkbox_cover_overwrite->isEnabled()) ui_->checkbox_cover_overwrite->setEnabled(false);
if (ui_->checkbox_cover_lowercase->isEnabled()) ui_->checkbox_cover_lowercase->setEnabled(false);
if (ui_->checkbox_cover_replace_spaces->isEnabled()) ui_->checkbox_cover_replace_spaces->setEnabled(false);
}
}
else {
if (ui_->groupbox_cover_filename->isEnabled()) {
ui_->groupbox_cover_filename->setEnabled(false);
}
}
}

View File

@ -43,6 +43,11 @@ public:
static const char *kSettingsGroup;
enum SaveCover {
SaveCover_Hash = 1,
SaveCover_Pattern = 2
};
void Load();
void Save();
@ -51,6 +56,7 @@ private slots:
void Remove();
void CurrentRowChanged(const QModelIndex &index);
void CoverSaveInAlbumDirChanged();
private:
Ui_CollectionSettingsPage *ui_;

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>509</width>
<height>452</height>
<height>695</height>
</rect>
</property>
<property name="windowTitle">
@ -15,14 +15,14 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="label_collection_folders">
<property name="text">
<string>These folders will be scanned for music to make up your collection</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="layout_collection_folders">
<item>
<widget class="QListView" name="list">
<property name="iconSize">
@ -37,7 +37,7 @@
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="layout_collection_folder_buttons">
<item>
<widget class="QPushButton" name="add">
<property name="text">
@ -56,7 +56,7 @@
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<spacer name="spacer_collection_buttons">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
@ -73,7 +73,7 @@
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<widget class="QGroupBox" name="groupbox_updating">
<property name="title">
<string>Automatic updating</string>
</property>
@ -93,7 +93,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="label_preferred_cover_filenames">
<property name="text">
<string>Preferred album art filenames (comma separated)</string>
</property>
@ -111,7 +111,7 @@ If there are no matches then it will use the largest image in the directory.</st
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_1">
<widget class="QGroupBox" name="groupbox_display">
<property name="title">
<string>Display options</string>
</property>
@ -140,6 +140,92 @@ If there are no matches then it will use the largest image in the directory.</st
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupbox_albumcovers">
<property name="title">
<string>Saving album covers</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QCheckBox" name="checkbox_cover_album_dir">
<property name="text">
<string>Save album covers in album directory</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupbox_cover_filename">
<property name="title">
<string>Filename:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QWidget" name="groupbox_buttons" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QRadioButton" name="radiobutton_cover_hash">
<property name="text">
<string>Use hash</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radiobutton_cover_pattern">
<property name="text">
<string>Use pattern</string>
</property>
</widget>
</item>
<item>
<spacer name="spacer_cover_filename_bttons">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineedit_cover_pattern">
<property name="text">
<string>%albumartist-%album</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkbox_cover_overwrite">
<property name="text">
<string>Overwrite existing file</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkbox_cover_lowercase">
<property name="text">
<string>Lowercase filename</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkbox_cover_replace_spaces">
<property name="text">
<string>Replace spaces with dashes</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<tabstops>