Skip to content

Commit

Permalink
Show the correct dialog when an error occurs while opening a file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbjeukendrup committed Jul 5, 2023
1 parent 2ceeae7 commit 8420350
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
34 changes: 17 additions & 17 deletions src/engraving/infrastructure/mscreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -309,7 +309,7 @@ StringList MscReader::ZipFileReader::fileList() const
StringList files;
std::vector<ZipReader::FileInfo> fileInfoList = m_zip->fileInfoList();
if (m_zip->hasError()) {
LOGD() << "failed read meta";
LOGE() << "failed read meta";
}

for (const ZipReader::FileInfo& fi : fileInfoList) {
Expand Down Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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();
}

Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/project/internal/notationproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectFileInfoProvider>(this));

SettingsCompat settingsCompat;
Ret ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode);
ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode);
if (!ret) {
return ret;
}
Expand Down
34 changes: 31 additions & 3 deletions src/project/internal/projectactionscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.");
Expand All @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/project/internal/projectactionscontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 8420350

Please sign in to comment.