Disable preferences menu if the config can't be written.
authorCarl Hetherington <cth@carlh.net>
Thu, 27 Sep 2018 14:26:05 +0000 (15:26 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 27 Sep 2018 14:26:05 +0000 (15:26 +0100)
src/lib/config.cc
src/lib/config.h
src/tools/dcpomatic_player.cc

index d7fd74449e4ab59430cd68793b832b5f29ddcfd4..e1d5b958d1af3da9413247aabd4ab4fe4412b0a2 100644 (file)
@@ -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;
+}
index bcc46f8fa4535e26e7e84d60d4622d19c99247e9..80bb48d32cfb3a15d68acfb786721fca71ba7143 100644 (file)
@@ -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<const Film> film, std::string name) const;
        bool existing_template (std::string name) const;
index 7313daf0fab9cd64cfb0f1f76af5ecb3426b1eac..0f1ed00b32f901fb544662978d3c2dbd9aca703c 100644 (file)
@@ -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);
                }