From bc36ddea65fda2088f7e8fa98390e3feac07df84 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 11 Mar 2020 20:23:11 +0000 Subject: [PATCH] Give a better error when opening a DCP with File -> Open by mistake (#1723). --- src/lib/exceptions.cc | 7 +++++++ src/lib/exceptions.h | 16 ++++++++++++++++ src/lib/film.cc | 4 ++++ src/tools/dcpomatic.cc | 19 +++++++++++++++---- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/lib/exceptions.cc b/src/lib/exceptions.cc index 99d4c3979..ba3d4a05c 100644 --- a/src/lib/exceptions.cc +++ b/src/lib/exceptions.cc @@ -40,6 +40,13 @@ OpenFileError::OpenFileError (boost::filesystem::path f, int error, Mode mode) } +FileNotFoundError::FileNotFoundError (boost::filesystem::path f) + : runtime_error(String::compose("File %1 not found", f.string())) + , _file (f) +{ + +} + ReadFileError::ReadFileError (boost::filesystem::path f, int e) : FileError (String::compose (_("could not read from file %1 (%2)"), f.string(), strerror (e)), f) { diff --git a/src/lib/exceptions.h b/src/lib/exceptions.h index 391258bd5..73b8cc85a 100644 --- a/src/lib/exceptions.h +++ b/src/lib/exceptions.h @@ -117,6 +117,22 @@ public: OpenFileError (boost::filesystem::path f, int error, Mode mode); }; +class FileNotFoundError : public std::runtime_error +{ +public: + FileNotFoundError (boost::filesystem::path f); + virtual ~FileNotFoundError () throw () {} + + /** @return name of the file that this exception concerns */ + boost::filesystem::path file () const { + return _file; + } + +private: + /** name of the file that this exception concerns */ + boost::filesystem::path _file; +}; + /** @class ReadFileError. * @brief Indicates that some error occurred when trying to read from a file */ diff --git a/src/lib/film.cc b/src/lib/film.cc index 90b18ea70..691ef58da 100644 --- a/src/lib/film.cc +++ b/src/lib/film.cc @@ -475,6 +475,10 @@ Film::read_metadata (optional path) path = file (metadata_file); } + if (!boost::filesystem::exists(*path)) { + throw FileNotFoundError(*path); + } + cxml::Document f ("Metadata"); f.read_file (path.get ()); diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index ea3cfbe32..aea058d80 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -447,10 +447,21 @@ public: JobManager::instance()->add(shared_ptr(new CheckContentChangeJob(film))); } - catch (std::exception& e) { - wxString p = std_to_wx (file.string ()); - wxCharBuffer b = p.ToUTF8 (); - error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx (e.what())); + catch (FileNotFoundError& e) { + boost::filesystem::path const dir = e.file().parent_path(); + if (boost::filesystem::exists(dir / "ASSETMAP") || boost::filesystem::exists(dir / "ASSETMAP.xml")) { + error_dialog ( + this, _("Could not open this folder as a DCP-o-matic project."), + _("It looks like you are trying to open a DCP. File -> Open is for loading DCP-o-matic projects, not DCPs. To import a DCP, create a new project with File -> New and then click the \"Add DCP...\" button.") + ); + } else { + wxString const p = std_to_wx(file.string ()); + error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what())); + } + + } catch (std::exception& e) { + wxString const p = std_to_wx (file.string()); + error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what())); } void set_film (shared_ptr film) -- 2.30.2