Skip to content

Commit

Permalink
Fix musescore#12028: Fix messaging when opening non-existing scores
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoithe authored and cbjeukendrup committed Jul 5, 2023
1 parent d2cec8a commit 2ceeae7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
39 changes: 25 additions & 14 deletions src/engraving/infrastructure/mscreader.cpp
Expand Up @@ -26,6 +26,7 @@
#include "io/dir.h"
#include "serialization/zipreader.h"
#include "serialization/xmlstreamreader.h"
#include "engraving/engravingerrors.h"

#include "log.h"

Expand Down Expand Up @@ -65,7 +66,7 @@ const MscReader::Params& MscReader::params() const
return m_params;
}

bool MscReader::open()
Ret MscReader::open()
{
return reader()->open(m_params.device, m_params.filePath);
}
Expand Down Expand Up @@ -253,24 +254,29 @@ MscReader::ZipFileReader::~ZipFileReader()
}
}

bool MscReader::ZipFileReader::open(IODevice* device, const path_t& filePath)
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;
return make_ret(Err::FileNotFound, filePath);
}
}

if (!m_device->isOpen()) {
if (!m_device->open(IODevice::ReadOnly)) {
LOGD() << "failed open file: " << filePath;
return false;
return make_ret(Err::FileOpenError, filePath);
}
}

m_zip = new ZipReader(m_device);

return true;
return make_ret(Err::NoError, filePath);
}

void MscReader::ZipFileReader::close()
Expand Down Expand Up @@ -338,7 +344,7 @@ ByteArray MscReader::ZipFileReader::fileData(const String& fileName) const
return data;
}

bool MscReader::DirReader::open(IODevice* device, const path_t& filePath)
Ret MscReader::DirReader::open(IODevice* device, const path_t& filePath)
{
if (device) {
NOT_SUPPORTED;
Expand All @@ -347,12 +353,12 @@ bool MscReader::DirReader::open(IODevice* device, const path_t& filePath)

if (!FileInfo::exists(filePath)) {
LOGD() << "not exists path: " << filePath;
return false;
return make_ret(Err::FileNotFound, filePath);
}

m_rootPath = containerPath(filePath);

return true;
return make_ret(Err::NoError, filePath);
}

void MscReader::DirReader::close()
Expand Down Expand Up @@ -407,22 +413,27 @@ ByteArray MscReader::DirReader::fileData(const String& fileName) const
return file.readAll();
}

bool MscReader::XmlFileReader::open(IODevice* device, const path_t& filePath)
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;
return make_ret(Err::FileNotFound, filePath);
}
}

if (!m_device->isOpen()) {
if (!m_device->open(IODevice::ReadOnly)) {
LOGD() << "failed open file: " << filePath;
return false;
return make_ret(Err::FileOpenError, filePath);
}
}

return true;
return make_ret(Err::NoError, filePath);
}

void MscReader::XmlFileReader::close()
Expand Down Expand Up @@ -453,13 +464,13 @@ StringList MscReader::XmlFileReader::fileList() const
m_device->seek(0);
XmlStreamReader xml(m_device);
while (xml.readNextStartElement()) {
if ("files" != xml.name()) {
if (xml.name() != "files") {
xml.skipCurrentElement();
continue;
}

while (xml.readNextStartElement()) {
if ("file" != xml.name()) {
if (xml.name() != "file") {
xml.skipCurrentElement();
continue;
}
Expand Down Expand Up @@ -511,13 +522,13 @@ ByteArray MscReader::XmlFileReader::fileData(const String& fileName) const
m_device->seek(0);
XmlStreamReader xml(m_device);
while (xml.readNextStartElement()) {
if ("files" != xml.name()) {
if (xml.name() != "files") {
xml.skipCurrentElement();
continue;
}

while (xml.readNextStartElement()) {
if ("file" != xml.name()) {
if (xml.name() != "file") {
xml.skipCurrentElement();
continue;
}
Expand Down
11 changes: 6 additions & 5 deletions src/engraving/infrastructure/mscreader.h
Expand Up @@ -22,6 +22,7 @@
#ifndef MU_ENGRAVING_MSCREADER_H
#define MU_ENGRAVING_MSCREADER_H

#include "types/ret.h"
#include "types/string.h"
#include "io/path.h"
#include "io/iodevice.h"
Expand Down Expand Up @@ -51,7 +52,7 @@ class MscReader
void setParams(const Params& params);
const Params& params() const;

bool open();
Ret open();
void close();
bool isOpened() const;

Expand All @@ -77,7 +78,7 @@ class MscReader
struct IReader {
virtual ~IReader() = default;

virtual bool open(io::IODevice* device, const io::path_t& filePath) = 0;
virtual Ret open(io::IODevice* device, const io::path_t& filePath) = 0;
virtual void close() = 0;
virtual bool isOpened() const = 0;
//! NOTE In the case of reading from a directory,
Expand All @@ -92,7 +93,7 @@ class MscReader
struct ZipFileReader : public IReader
{
~ZipFileReader() override;
bool open(io::IODevice* device, const io::path_t& filePath) override;
Ret open(io::IODevice* device, const io::path_t& filePath) override;
void close() override;
bool isOpened() const override;
bool isContainer() const override;
Expand All @@ -107,7 +108,7 @@ class MscReader

struct DirReader : public IReader
{
bool open(io::IODevice* device, const io::path_t& filePath) override;
Ret open(io::IODevice* device, const io::path_t& filePath) override;
void close() override;
bool isOpened() const override;
bool isContainer() const override;
Expand All @@ -120,7 +121,7 @@ class MscReader

struct XmlFileReader : public IReader
{
bool open(io::IODevice* device, const io::path_t& filePath) override;
Ret open(io::IODevice* device, const io::path_t& filePath) override;
void close() override;
bool isOpened() const override;
bool isContainer() const override;
Expand Down
5 changes: 3 additions & 2 deletions src/project/internal/notationproject.cpp
Expand Up @@ -184,8 +184,9 @@ mu::Ret NotationProject::doLoad(const io::path_t& path, const io::path_t& styleP
}

MscReader reader(params);
if (!reader.open()) {
return make_ret(engraving::Err::FileOpenError);
Ret openResult = reader.open();
if (openResult != make_ret(mu::engraving::Err::NoError)) {
return openResult;
}

// Load engraving project
Expand Down

0 comments on commit 2ceeae7

Please sign in to comment.