From 8420350afb31b094c8ecc4661d028cdbd37eaff7 Mon Sep 17 00:00:00 2001 From: Casper Jeukendrup <48658420+cbjeukendrup@users.noreply.github.com> Date: Tue, 4 Jul 2023 00:09:58 +0200 Subject: [PATCH] Show the correct dialog when an error occurs while opening a file --- src/engraving/infrastructure/mscreader.cpp | 34 +++++++++---------- src/project/internal/notationproject.cpp | 8 ++--- .../internal/projectactionscontroller.cpp | 34 +++++++++++++++++-- .../internal/projectactionscontroller.h | 3 +- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/engraving/infrastructure/mscreader.cpp b/src/engraving/infrastructure/mscreader.cpp index 65dcce86aff0a..1837fa92cb22f 100644 --- a/src/engraving/infrastructure/mscreader.cpp +++ b/src/engraving/infrastructure/mscreader.cpp @@ -258,25 +258,25 @@ Ret MscReader::ZipFileReader::open(IODevice* device, const path_t& filePath) { m_device = device; if (!m_device) { - m_device = new File(filePath); - m_selfDeviceOwner = true; - if (!FileInfo::exists(filePath)) { - LOGD() << "not exists path: " << filePath; + LOGE() << "path does not exist: " << filePath; return make_ret(Err::FileNotFound, filePath); } + + m_device = new File(filePath); + m_selfDeviceOwner = true; } if (!m_device->isOpen()) { if (!m_device->open(IODevice::ReadOnly)) { - LOGD() << "failed open file: " << filePath; + LOGE() << "failed open file: " << filePath; return make_ret(Err::FileOpenError, filePath); } } m_zip = new ZipReader(m_device); - return make_ret(Err::NoError, filePath); + return true; } void MscReader::ZipFileReader::close() @@ -309,7 +309,7 @@ StringList MscReader::ZipFileReader::fileList() const StringList files; std::vector fileInfoList = m_zip->fileInfoList(); if (m_zip->hasError()) { - LOGD() << "failed read meta"; + LOGE() << "failed read meta"; } for (const ZipReader::FileInfo& fi : fileInfoList) { @@ -338,7 +338,7 @@ ByteArray MscReader::ZipFileReader::fileData(const String& fileName) const ByteArray data = m_zip->fileData(fileName.toStdString()); if (m_zip->hasError()) { - LOGD() << "failed read data"; + LOGE() << "failed read data for filename " << fileName; return ByteArray(); } return data; @@ -352,13 +352,13 @@ Ret MscReader::DirReader::open(IODevice* device, const path_t& filePath) } if (!FileInfo::exists(filePath)) { - LOGD() << "not exists path: " << filePath; + LOGE() << "path does not exist: " << filePath; return make_ret(Err::FileNotFound, filePath); } m_rootPath = containerPath(filePath); - return make_ret(Err::NoError, filePath); + return make_ok(); } void MscReader::DirReader::close() @@ -406,7 +406,7 @@ ByteArray MscReader::DirReader::fileData(const String& fileName) const io::path_t filePath = m_rootPath + "/" + fileName; File file(filePath); if (!file.open(IODevice::ReadOnly)) { - LOGD() << "failed open file: " << filePath; + LOGE() << "failed open file: " << filePath; return ByteArray(); } @@ -417,23 +417,23 @@ Ret MscReader::XmlFileReader::open(IODevice* device, const path_t& filePath) { m_device = device; if (!m_device) { - m_device = new File(filePath); - m_selfDeviceOwner = true; - if (!FileInfo::exists(filePath)) { - LOGD() << "not exists path: " << filePath; + LOGE() << "path does not exist: " << filePath; return make_ret(Err::FileNotFound, filePath); } + + m_device = new File(filePath); + m_selfDeviceOwner = true; } if (!m_device->isOpen()) { if (!m_device->open(IODevice::ReadOnly)) { - LOGD() << "failed open file: " << filePath; + LOGE() << "failed open file: " << filePath; return make_ret(Err::FileOpenError, filePath); } } - return make_ret(Err::NoError, filePath); + return make_ok(); } void MscReader::XmlFileReader::close() diff --git a/src/project/internal/notationproject.cpp b/src/project/internal/notationproject.cpp index 75507a45aa67a..b73b5ffedafa2 100644 --- a/src/project/internal/notationproject.cpp +++ b/src/project/internal/notationproject.cpp @@ -184,16 +184,16 @@ mu::Ret NotationProject::doLoad(const io::path_t& path, const io::path_t& styleP } MscReader reader(params); - Ret openResult = reader.open(); - if (openResult != make_ret(mu::engraving::Err::NoError)) { - return openResult; + Ret ret = reader.open(); + if (!ret) { + return ret; } // Load engraving project m_engravingProject->setFileInfoProvider(std::make_shared(this)); SettingsCompat settingsCompat; - Ret ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode); + ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode); if (!ret) { return ret; } diff --git a/src/project/internal/projectactionscontroller.cpp b/src/project/internal/projectactionscontroller.cpp index ff4c6ca273e0b..1b3b98c517cc7 100644 --- a/src/project/internal/projectactionscontroller.cpp +++ b/src/project/internal/projectactionscontroller.cpp @@ -1390,10 +1390,15 @@ bool ProjectActionsController::checkCanIgnoreError(const Ret& ret, const io::pat return false; case engraving::Err::FileCorrupted: return askIfUserAgreesToOpenCorruptedProject(io::filename(filepath).toString(), ret.text()); - default: - warnProjectCannotBeOpened(io::filename(filepath).toString(), ret.text()); + case engraving::Err::FileCriticallyCorrupted: + warnProjectCriticallyCorrupted(io::filename(filepath).toString(), ret.text()); return false; + default: + break; } + + warnProjectCannotBeOpened(ret, filepath); + return false; } bool ProjectActionsController::askIfUserAgreesToOpenProjectWithIncompatibleVersion(const std::string& errorText) @@ -1430,7 +1435,7 @@ bool ProjectActionsController::askIfUserAgreesToOpenCorruptedProject(const Strin return btn == openAnywayBtn.btn; } -void ProjectActionsController::warnProjectCannotBeOpened(const String& projectName, const std::string& errorText) +void ProjectActionsController::warnProjectCriticallyCorrupted(const String& projectName, const std::string& errorText) { std::string title = mtrc("project", "File “%1” is corrupted and cannot be opened").arg(projectName).toStdString(); std::string body = trc("project", "Get help for this issue on musescore.org."); @@ -1447,6 +1452,29 @@ void ProjectActionsController::warnProjectCannotBeOpened(const String& projectNa } } +void ProjectActionsController::warnProjectCannotBeOpened(const Ret& ret, const io::path_t& filepath) +{ + std::string title = mtrc("project", "Cannot read file %1").arg(io::toNativeSeparators(filepath).toString()).toStdString(); + std::string body; + + switch (ret.code()) { + case int(engraving::Err::FileNotFound): + body = trc("project", "This file does not exist or cannot be accessed at the moment."); + break; + case int(engraving::Err::FileOpenError): + body = trc("project", "This file could not be opened. Please make sure that MuseScore has permission to read this file."); + break; + default: + if (!ret.text().empty()) { + body = ret.text(); + } else { + body = trc("project", "An error occurred while reading this file."); + } + } + + interactive()->error(title, body); +} + void ProjectActionsController::importPdf() { interactive()->openUrl("https://musescore.com/import"); diff --git a/src/project/internal/projectactionscontroller.h b/src/project/internal/projectactionscontroller.h index c1bccef43c538..25bedd01fbc18 100644 --- a/src/project/internal/projectactionscontroller.h +++ b/src/project/internal/projectactionscontroller.h @@ -107,7 +107,8 @@ class ProjectActionsController : public IProjectFilesController, public QObject, bool askIfUserAgreesToOpenProjectWithIncompatibleVersion(const std::string& errorText); void warnFileTooNew(const io::path_t& filepath); bool askIfUserAgreesToOpenCorruptedProject(const String& projectName, const std::string& errorText); - void warnProjectCannotBeOpened(const String& projectName, const std::string& errorText); + void warnProjectCriticallyCorrupted(const String& projectName, const std::string& errorText); + void warnProjectCannotBeOpened(const Ret& ret, const io::path_t& filepath); framework::IInteractive::Button askAboutSavingScore(INotationProjectPtr project);