Skip to content

Commit 8420350

Browse files
committed
Show the correct dialog when an error occurs while opening a file
1 parent 2ceeae7 commit 8420350

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

src/engraving/infrastructure/mscreader.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,25 @@ Ret MscReader::ZipFileReader::open(IODevice* device, const path_t& filePath)
258258
{
259259
m_device = device;
260260
if (!m_device) {
261-
m_device = new File(filePath);
262-
m_selfDeviceOwner = true;
263-
264261
if (!FileInfo::exists(filePath)) {
265-
LOGD() << "not exists path: " << filePath;
262+
LOGE() << "path does not exist: " << filePath;
266263
return make_ret(Err::FileNotFound, filePath);
267264
}
265+
266+
m_device = new File(filePath);
267+
m_selfDeviceOwner = true;
268268
}
269269

270270
if (!m_device->isOpen()) {
271271
if (!m_device->open(IODevice::ReadOnly)) {
272-
LOGD() << "failed open file: " << filePath;
272+
LOGE() << "failed open file: " << filePath;
273273
return make_ret(Err::FileOpenError, filePath);
274274
}
275275
}
276276

277277
m_zip = new ZipReader(m_device);
278278

279-
return make_ret(Err::NoError, filePath);
279+
return true;
280280
}
281281

282282
void MscReader::ZipFileReader::close()
@@ -309,7 +309,7 @@ StringList MscReader::ZipFileReader::fileList() const
309309
StringList files;
310310
std::vector<ZipReader::FileInfo> fileInfoList = m_zip->fileInfoList();
311311
if (m_zip->hasError()) {
312-
LOGD() << "failed read meta";
312+
LOGE() << "failed read meta";
313313
}
314314

315315
for (const ZipReader::FileInfo& fi : fileInfoList) {
@@ -338,7 +338,7 @@ ByteArray MscReader::ZipFileReader::fileData(const String& fileName) const
338338

339339
ByteArray data = m_zip->fileData(fileName.toStdString());
340340
if (m_zip->hasError()) {
341-
LOGD() << "failed read data";
341+
LOGE() << "failed read data for filename " << fileName;
342342
return ByteArray();
343343
}
344344
return data;
@@ -352,13 +352,13 @@ Ret MscReader::DirReader::open(IODevice* device, const path_t& filePath)
352352
}
353353

354354
if (!FileInfo::exists(filePath)) {
355-
LOGD() << "not exists path: " << filePath;
355+
LOGE() << "path does not exist: " << filePath;
356356
return make_ret(Err::FileNotFound, filePath);
357357
}
358358

359359
m_rootPath = containerPath(filePath);
360360

361-
return make_ret(Err::NoError, filePath);
361+
return make_ok();
362362
}
363363

364364
void MscReader::DirReader::close()
@@ -406,7 +406,7 @@ ByteArray MscReader::DirReader::fileData(const String& fileName) const
406406
io::path_t filePath = m_rootPath + "/" + fileName;
407407
File file(filePath);
408408
if (!file.open(IODevice::ReadOnly)) {
409-
LOGD() << "failed open file: " << filePath;
409+
LOGE() << "failed open file: " << filePath;
410410
return ByteArray();
411411
}
412412

@@ -417,23 +417,23 @@ Ret MscReader::XmlFileReader::open(IODevice* device, const path_t& filePath)
417417
{
418418
m_device = device;
419419
if (!m_device) {
420-
m_device = new File(filePath);
421-
m_selfDeviceOwner = true;
422-
423420
if (!FileInfo::exists(filePath)) {
424-
LOGD() << "not exists path: " << filePath;
421+
LOGE() << "path does not exist: " << filePath;
425422
return make_ret(Err::FileNotFound, filePath);
426423
}
424+
425+
m_device = new File(filePath);
426+
m_selfDeviceOwner = true;
427427
}
428428

429429
if (!m_device->isOpen()) {
430430
if (!m_device->open(IODevice::ReadOnly)) {
431-
LOGD() << "failed open file: " << filePath;
431+
LOGE() << "failed open file: " << filePath;
432432
return make_ret(Err::FileOpenError, filePath);
433433
}
434434
}
435435

436-
return make_ret(Err::NoError, filePath);
436+
return make_ok();
437437
}
438438

439439
void MscReader::XmlFileReader::close()

src/project/internal/notationproject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,16 @@ mu::Ret NotationProject::doLoad(const io::path_t& path, const io::path_t& styleP
184184
}
185185

186186
MscReader reader(params);
187-
Ret openResult = reader.open();
188-
if (openResult != make_ret(mu::engraving::Err::NoError)) {
189-
return openResult;
187+
Ret ret = reader.open();
188+
if (!ret) {
189+
return ret;
190190
}
191191

192192
// Load engraving project
193193
m_engravingProject->setFileInfoProvider(std::make_shared<ProjectFileInfoProvider>(this));
194194

195195
SettingsCompat settingsCompat;
196-
Ret ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode);
196+
ret = m_engravingProject->loadMscz(reader, settingsCompat, forceMode);
197197
if (!ret) {
198198
return ret;
199199
}

src/project/internal/projectactionscontroller.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,15 @@ bool ProjectActionsController::checkCanIgnoreError(const Ret& ret, const io::pat
13901390
return false;
13911391
case engraving::Err::FileCorrupted:
13921392
return askIfUserAgreesToOpenCorruptedProject(io::filename(filepath).toString(), ret.text());
1393-
default:
1394-
warnProjectCannotBeOpened(io::filename(filepath).toString(), ret.text());
1393+
case engraving::Err::FileCriticallyCorrupted:
1394+
warnProjectCriticallyCorrupted(io::filename(filepath).toString(), ret.text());
13951395
return false;
1396+
default:
1397+
break;
13961398
}
1399+
1400+
warnProjectCannotBeOpened(ret, filepath);
1401+
return false;
13971402
}
13981403

13991404
bool ProjectActionsController::askIfUserAgreesToOpenProjectWithIncompatibleVersion(const std::string& errorText)
@@ -1430,7 +1435,7 @@ bool ProjectActionsController::askIfUserAgreesToOpenCorruptedProject(const Strin
14301435
return btn == openAnywayBtn.btn;
14311436
}
14321437

1433-
void ProjectActionsController::warnProjectCannotBeOpened(const String& projectName, const std::string& errorText)
1438+
void ProjectActionsController::warnProjectCriticallyCorrupted(const String& projectName, const std::string& errorText)
14341439
{
14351440
std::string title = mtrc("project", "File “%1” is corrupted and cannot be opened").arg(projectName).toStdString();
14361441
std::string body = trc("project", "Get help for this issue on musescore.org.");
@@ -1447,6 +1452,29 @@ void ProjectActionsController::warnProjectCannotBeOpened(const String& projectNa
14471452
}
14481453
}
14491454

1455+
void ProjectActionsController::warnProjectCannotBeOpened(const Ret& ret, const io::path_t& filepath)
1456+
{
1457+
std::string title = mtrc("project", "Cannot read file %1").arg(io::toNativeSeparators(filepath).toString()).toStdString();
1458+
std::string body;
1459+
1460+
switch (ret.code()) {
1461+
case int(engraving::Err::FileNotFound):
1462+
body = trc("project", "This file does not exist or cannot be accessed at the moment.");
1463+
break;
1464+
case int(engraving::Err::FileOpenError):
1465+
body = trc("project", "This file could not be opened. Please make sure that MuseScore has permission to read this file.");
1466+
break;
1467+
default:
1468+
if (!ret.text().empty()) {
1469+
body = ret.text();
1470+
} else {
1471+
body = trc("project", "An error occurred while reading this file.");
1472+
}
1473+
}
1474+
1475+
interactive()->error(title, body);
1476+
}
1477+
14501478
void ProjectActionsController::importPdf()
14511479
{
14521480
interactive()->openUrl("https://musescore.com/import");

src/project/internal/projectactionscontroller.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class ProjectActionsController : public IProjectFilesController, public QObject,
107107
bool askIfUserAgreesToOpenProjectWithIncompatibleVersion(const std::string& errorText);
108108
void warnFileTooNew(const io::path_t& filepath);
109109
bool askIfUserAgreesToOpenCorruptedProject(const String& projectName, const std::string& errorText);
110-
void warnProjectCannotBeOpened(const String& projectName, const std::string& errorText);
110+
void warnProjectCriticallyCorrupted(const String& projectName, const std::string& errorText);
111+
void warnProjectCannotBeOpened(const Ret& ret, const io::path_t& filepath);
111112

112113
framework::IInteractive::Button askAboutSavingScore(INotationProjectPtr project);
113114

0 commit comments

Comments
 (0)