From 1f1661f36ffd7a5662847f0642c7a7ebdcadc0f0 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 27 Sep 2018 15:26:05 +0100 Subject: [PATCH] Disable preferences menu if the config can't be written. --- src/lib/config.cc | 12 ++++++++++++ src/lib/config.h | 1 + src/tools/dcpomatic_player.cc | 28 ++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/lib/config.cc b/src/lib/config.cc index d7fd74449..e1d5b958d 100644 --- a/src/lib/config.cc +++ b/src/lib/config.cc @@ -1201,3 +1201,15 @@ Config::copy_and_link (boost::filesystem::path new_file) const boost::filesystem::copy_file (config_file(), new_file, boost::filesystem::copy_option::overwrite_if_exists); link (new_file); } + +bool +Config::have_write_permission () const +{ + FILE* f = fopen_boost (config_file(), "r+"); + if (!f) { + return false; + } + + fclose (f); + return true; +} diff --git a/src/lib/config.h b/src/lib/config.h index bcc46f8fa..80bb48d32 100644 --- a/src/lib/config.h +++ b/src/lib/config.h @@ -1002,6 +1002,7 @@ public: void write_cinemas () const; void link (boost::filesystem::path new_file) const; void copy_and_link (boost::filesystem::path new_file) const; + bool have_write_permission () const; void save_template (boost::shared_ptr film, std::string name) const; bool existing_template (std::string name) const; diff --git a/src/tools/dcpomatic_player.cc b/src/tools/dcpomatic_player.cc index 7313daf0f..0f1ed00b3 100644 --- a/src/tools/dcpomatic_player.cc +++ b/src/tools/dcpomatic_player.cc @@ -136,7 +136,7 @@ public: #endif _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this)); - config_changed (); + update_from_config (); Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open); Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov); @@ -467,12 +467,14 @@ private: #endif #ifdef __WXOSX__ - _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P")); + wxMenuItem* prefs = _file_menu->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P")); #else wxMenu* edit = new wxMenu; - edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P")); + wxMenuItem* prefs = edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P")); #endif + prefs->Enable (Config::instance()->have_write_permission()); + _cpl_menu = new wxMenu; wxMenu* view = new wxMenu; @@ -632,6 +634,10 @@ private: void edit_preferences () { + if (!Config::instance()->have_write_permission()) { + return; + } + if (!_config_dialog) { _config_dialog = create_player_config_dialog (); } @@ -808,16 +814,26 @@ private: /* Instantly save any config changes when using the player GUI */ try { Config::instance()->write_config(); - } catch (exception& e) { + } catch (FileError& e) { error_dialog ( this, - wxString::Format ( + wxString::Format( _("Could not write to config file at %s. Your changes have not been saved."), - std_to_wx (Config::instance()->cinemas_file().string()).data() + std_to_wx(e.file().string()) ) ); + } catch (exception& e) { + error_dialog ( + this, + _("Could not write to config file. Your changes have not been saved.") + ); } + update_from_config (); + } + + void update_from_config () + { for (int i = 0; i < _history_items; ++i) { delete _file_menu->Remove (ID_file_history + i); } -- 2.30.2