From 09a9330f3eb2832f2873009b22f3eea6a93f59ce Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 19 Sep 2021 19:31:34 +0200 Subject: [PATCH] Show error when reading or saving album covers --- src/core/mainwindow.cpp | 2 + .../albumcoverchoicecontroller.cpp | 38 +++++++++++++++---- src/covermanager/albumcoverchoicecontroller.h | 1 + src/covermanager/albumcovermanager.cpp | 1 + src/covermanager/albumcovermanager.h | 1 + src/dialogs/edittagdialog.cpp | 1 + 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index bc48e8abe..4522d9c17 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -253,6 +253,7 @@ MainWindow::MainWindow(Application *app, std::shared_ptr tray_ic cover_manager->Init(); // Cover manager connections + QObject::connect(cover_manager, &AlbumCoverManager::Error, this, &MainWindow::ShowErrorDialog); QObject::connect(cover_manager, &AlbumCoverManager::AddToPlaylist, this, &MainWindow::AddToPlaylist); return cover_manager; }), @@ -621,6 +622,7 @@ MainWindow::MainWindow(Application *app, std::shared_ptr tray_ic QObject::connect(app_->task_manager(), &TaskManager::ResumeCollectionWatchers, app_->collection(), &SCollection::ResumeWatcher); QObject::connect(app_->current_albumcover_loader(), &CurrentAlbumCoverLoader::AlbumCoverLoaded, this, &MainWindow::AlbumCoverLoaded); + QObject::connect(album_cover_choice_controller_, &AlbumCoverChoiceController::Error, this, &MainWindow::ShowErrorDialog); QObject::connect(album_cover_choice_controller_->cover_from_file_action(), &QAction::triggered, this, &MainWindow::LoadCoverFromFile); QObject::connect(album_cover_choice_controller_->cover_to_file_action(), &QAction::triggered, this, &MainWindow::SaveCoverToFile); QObject::connect(album_cover_choice_controller_->cover_from_url_action(), &QAction::triggered, this, &MainWindow::LoadCoverFromURL); diff --git a/src/covermanager/albumcoverchoicecontroller.cpp b/src/covermanager/albumcoverchoicecontroller.cpp index a8063eb3b..cf556638a 100644 --- a/src/covermanager/albumcoverchoicecontroller.cpp +++ b/src/covermanager/albumcoverchoicecontroller.cpp @@ -182,7 +182,11 @@ AlbumCoverImageResult AlbumCoverChoiceController::LoadImageFromFile(Song *song) if (file.open(QIODevice::ReadOnly)) { result.image_data = file.readAll(); file.close(); - if (!result.image_data.isEmpty()) { + if (result.image_data.isEmpty()) { + qLog(Error) << "Cover file" << cover_file << "is empty."; + emit Error(tr("Cover file %1 is empty.").arg(cover_file)); + } + else { result.mime_type = Utilities::MimeTypeFromData(result.image_data); result.image.loadFromData(result.image_data); result.cover_url = QUrl::fromLocalFile(cover_file); @@ -190,6 +194,7 @@ AlbumCoverImageResult AlbumCoverChoiceController::LoadImageFromFile(Song *song) } else { qLog(Error) << "Failed to open cover file" << cover_file << "for reading:" << file.errorString(); + emit Error(tr("Failed to open cover file %1 for reading: %2").arg(cover_file, file.errorString())); } return result; @@ -257,15 +262,20 @@ void AlbumCoverChoiceController::SaveCoverToFileManual(const Song &song, const A if (file.open(QIODevice::WriteOnly)) { if (file.write(result.image_data) <= 0) { qLog(Error) << "Failed writing cover to file" << save_filename << file.errorString(); + emit Error(tr("Failed writing cover to file %1: %2").arg(save_filename, file.errorString())); } file.close(); } else { qLog(Error) << "Failed to open cover file" << save_filename << "for writing:" << file.errorString(); + emit Error(tr("Failed to open cover file %1 for writing: %2").arg(save_filename, file.errorString())); } } else { - result.image.save(save_filename); + if (!result.image.save(save_filename)) { + qLog(Error) << "Failed writing cover to file" << save_filename; + emit Error(tr("Failed writing cover to file %1.").arg(save_filename)); + } } } @@ -383,24 +393,34 @@ bool AlbumCoverChoiceController::DeleteCover(Song *song, const bool manually_uns bool success = true; if (!art_automatic.isEmpty()) { - if (QFile::exists(art_automatic)) { - if (QFile::remove(art_automatic)) { + QFile file(art_automatic); + if (file.exists()) { + if (file.remove()) { song->clear_art_automatic(); if (art_automatic == art_manual) song->clear_art_manual(); } - else success = false; + else { + success = false; + qLog(Error) << "Failed to delete cover file" << art_automatic << file.errorString(); + emit Error(tr("Failed to delete cover file %1: %2").arg(art_automatic, file.errorString())); + } } else song->clear_art_automatic(); } else song->clear_art_automatic(); if (!art_manual.isEmpty()) { - if (QFile::exists(art_manual)) { - if (QFile::remove(art_manual)) { + QFile file(art_manual); + if (file.exists()) { + if (file.remove()) { song->clear_art_manual(); if (art_automatic == art_manual) song->clear_art_automatic(); } - else success = false; + else { + success = false; + qLog(Error) << "Failed to delete cover file" << art_manual << file.errorString(); + emit Error(tr("Failed to delete cover file %1: %2").arg(art_manual, file.errorString())); + } } else song->clear_art_manual(); } @@ -609,11 +629,13 @@ QUrl AlbumCoverChoiceController::SaveCoverToFileAutomatic(const Song::Source sou } else { qLog(Error) << "Failed to write cover to file" << file.fileName() << file.errorString(); + emit Error(tr("Failed to write cover to file %1: %2").arg(file.fileName(), file.errorString())); } file.close(); } else { qLog(Error) << "Failed to open cover file" << file.fileName() << "for writing:" << file.errorString(); + emit Error(tr("Failed to open cover file %1 for writing: %2").arg(file.fileName(), file.errorString())); } } else { diff --git a/src/covermanager/albumcoverchoicecontroller.h b/src/covermanager/albumcoverchoicecontroller.h index 05aa7cf01..5807e2e77 100644 --- a/src/covermanager/albumcoverchoicecontroller.h +++ b/src/covermanager/albumcoverchoicecontroller.h @@ -157,6 +157,7 @@ class AlbumCoverChoiceController : public QWidget { void SaveEmbeddedCoverAsyncFinished(quint64 id, const bool success, const bool cleared); signals: + void Error(QString); void AutomaticCoverSearchDone(); private: diff --git a/src/covermanager/albumcovermanager.cpp b/src/covermanager/albumcovermanager.cpp index f4adfb9b7..2d559ec7e 100644 --- a/src/covermanager/albumcovermanager.cpp +++ b/src/covermanager/albumcovermanager.cpp @@ -189,6 +189,7 @@ void AlbumCoverManager::Init() { QList actions = album_cover_choice_controller_->GetAllActions(); + QObject::connect(album_cover_choice_controller_, &AlbumCoverChoiceController::Error, this, &AlbumCoverManager::Error); QObject::connect(album_cover_choice_controller_->cover_from_file_action(), &QAction::triggered, this, &AlbumCoverManager::LoadCoverFromFile); QObject::connect(album_cover_choice_controller_->cover_to_file_action(), &QAction::triggered, this, &AlbumCoverManager::SaveCoverToFile); QObject::connect(album_cover_choice_controller_->cover_from_url_action(), &QAction::triggered, this, &AlbumCoverManager::LoadCoverFromURL); diff --git a/src/covermanager/albumcovermanager.h b/src/covermanager/albumcovermanager.h index c3cfe575b..edc726d55 100644 --- a/src/covermanager/albumcovermanager.h +++ b/src/covermanager/albumcovermanager.h @@ -147,6 +147,7 @@ class AlbumCoverManager : public QMainWindow { bool ItemHasCover(const AlbumItem &item) const; signals: + void Error(QString); void AddToPlaylist(QMimeData *data); private slots: diff --git a/src/dialogs/edittagdialog.cpp b/src/dialogs/edittagdialog.cpp index 55414fe2e..b81f8d971 100644 --- a/src/dialogs/edittagdialog.cpp +++ b/src/dialogs/edittagdialog.cpp @@ -193,6 +193,7 @@ EditTagDialog::EditTagDialog(Application *app, QWidget *parent) QList actions = album_cover_choice_controller_->GetAllActions(); + QObject::connect(album_cover_choice_controller_, &AlbumCoverChoiceController::Error, this, &EditTagDialog::Error); QObject::connect(album_cover_choice_controller_->cover_from_file_action(), &QAction::triggered, this, &EditTagDialog::LoadCoverFromFile); QObject::connect(album_cover_choice_controller_->cover_to_file_action(), &QAction::triggered, this, &EditTagDialog::SaveCoverToFile); QObject::connect(album_cover_choice_controller_->cover_from_url_action(), &QAction::triggered, this, &EditTagDialog::LoadCoverFromURL);