From: joncruz Date: Mon, 4 May 2009 03:58:23 +0000 (+0000) Subject: Fixed broken preferences singleton and removed GUI encapsulation leakage. Fixes bug... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b50bd4043d21611cc8301b731f60b4b0cd27bfb8;p=inkscape.git Fixed broken preferences singleton and removed GUI encapsulation leakage. Fixes bug #367752. --- diff --git a/src/inkscape.cpp b/src/inkscape.cpp index 25b3cc5a9..5d9e217a8 100644 --- a/src/inkscape.cpp +++ b/src/inkscape.cpp @@ -41,6 +41,7 @@ using Inkscape::Extension::Internal::PrintWin32; #include #include #include +#include #include #include #include "application/application.h" @@ -742,6 +743,28 @@ inkscape_crash_handler (int /*signum*/) } +class InkErrorHandler : public Inkscape::ErrorReporter { +public: + InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(), + _useGui(useGui) + {} + virtual ~InkErrorHandler() {} + + virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const + { + if (_useGui) { + Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); + err.set_secondary_text(secondary); + err.run(); + } else { + g_message("%s", primary.data()); + g_message("%s", secondary.data()); + } + } + +private: + bool _useGui; +}; void inkscape_application_init (const gchar *argv0, gboolean use_gui) @@ -762,7 +785,9 @@ inkscape_application_init (const gchar *argv0, gboolean use_gui) /* Load the preferences and menus */ Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - prefs->load(use_gui, false); + InkErrorHandler* handler = new InkErrorHandler(use_gui); + prefs->setErrorHandler(handler); + inkscape_load_menus(inkscape); sp_input_load_from_preferences(); diff --git a/src/inkview.cpp b/src/inkview.cpp index bb675f0ee..cd9d5524b 100644 --- a/src/inkview.cpp +++ b/src/inkview.cpp @@ -214,7 +214,6 @@ main (int argc, const char **argv) Inkscape::GC::init(); Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - prefs->load(false, true); // keep quiet about any failures gtk_init (&argc, (char ***) &argv); diff --git a/src/preferences.cpp b/src/preferences.cpp index 16b775b34..a6409b5ba 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -3,26 +3,24 @@ */ /* Authors: * Krzysztof Kosiński + * Jon A. Cruz * - * Copyright (C) 2008 Authors + * Copyright (C) 2008,2009 Authors * * Released under GNU GPL. Read the file 'COPYING' for more information. */ +#include +#include +#include +#include +#include #include "preferences.h" #include "preferences-skeleton.h" #include "inkscape.h" -#include "xml/repr.h" #include "xml/node-observer.h" #include "xml/node-iterators.h" #include "xml/attribute-record.h" -#include -#include -#include -#include -#include -#include -#include #define PREFERENCES_FILE_NAME "preferences.xml" @@ -54,10 +52,8 @@ Preferences::Preferences() : _prefs_basename(PREFERENCES_FILE_NAME), _prefs_dir(""), _prefs_filename(""), - _prefs_doc(NULL), - _use_gui(true), - _quiet(false), - _loaded(false), + _prefs_doc(0), + _errorHandler(0), _writable(false) { // profile_path essentailly returns the argument prefixed by the profile directory. @@ -70,6 +66,7 @@ Preferences::Preferences() : g_free(path); _loadDefaults(); + _load(); } Preferences::~Preferences() @@ -97,16 +94,12 @@ void Preferences::_loadDefaults() * @brief Load the user's customized preferences * * Tries to load the user's preferences.xml file. If there is none, creates it. - * Displays dialog boxes on any errors. */ -void Preferences::load(bool use_gui, bool quiet) +void Preferences::_load() { Glib::ustring const not_saved = _("Inkscape will run with default settings, " - "and new settings will not be saved. "); - _use_gui = use_gui; - _quiet = quiet; - _loaded = true; - + "and new settings will not be saved. "); + // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose // 1. Does the file exist? @@ -117,11 +110,11 @@ void Preferences::load(bool use_gui, bool quiet) // No - create the profile directory if (g_mkdir(_prefs_dir.data(), 0755)) { // the creation failed - //_errorDialog(Glib::ustring::compose(_("Cannot create profile directory %1."), + //_reportError(Glib::ustring::compose(_("Cannot create profile directory %1."), // Glib::filename_to_utf8(_prefs_dir)), not_saved); gchar *msg = g_strdup_printf(_("Cannot create profile directory %s."), Glib::filename_to_utf8(_prefs_dir).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } @@ -135,22 +128,22 @@ void Preferences::load(bool use_gui, bool quiet) } else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) { // The profile dir is not actually a directory - //_errorDialog(Glib::ustring::compose(_("%1 is not a valid directory."), + //_reportError(Glib::ustring::compose(_("%1 is not a valid directory."), // Glib::filename_to_utf8(_prefs_dir)), not_saved); gchar *msg = g_strdup_printf(_("%s is not a valid directory."), Glib::filename_to_utf8(_prefs_dir).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } // The profile dir exists and is valid. if (!g_file_set_contents(_prefs_filename.data(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) { // The write failed. - //_errorDialog(Glib::ustring::compose(_("Failed to create the preferences file %1."), + //_reportError(Glib::ustring::compose(_("Failed to create the preferences file %1."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("Failed to create the preferences file %s."), Glib::filename_to_utf8(_prefs_filename).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } @@ -164,11 +157,11 @@ void Preferences::load(bool use_gui, bool quiet) // Yes, the pref file exists. // 2. Is it a regular file? if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) { - //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a regular file."), + //_reportError(Glib::ustring::compose(_("The preferences file %1 is not a regular file."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s is not a regular file."), Glib::filename_to_utf8(_prefs_filename).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } @@ -176,11 +169,11 @@ void Preferences::load(bool use_gui, bool quiet) // 3. Is the file readable? gchar *prefs_xml = NULL; gsize len = 0; if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) { - //_errorDialog(Glib::ustring::compose(_("The preferences file %1 could not be read."), + //_reportError(Glib::ustring::compose(_("The preferences file %1 could not be read."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s could not be read."), Glib::filename_to_utf8(_prefs_filename).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } @@ -188,21 +181,21 @@ void Preferences::load(bool use_gui, bool quiet) Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL); g_free(prefs_xml); if (!prefs_read) { - //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."), + //_reportError(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."), Glib::filename_to_utf8(_prefs_filename).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); return; } // 5. Basic sanity check: does the root element have a correct name? if (strcmp(prefs_read->root()->name(), "inkscape")) { - //_errorDialog(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."), + //_reportError(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."), Glib::filename_to_utf8(_prefs_filename).data()); - _errorDialog(msg, not_saved); + _reportError(msg, not_saved); g_free(msg); Inkscape::GC::release(prefs_read); return; @@ -615,17 +608,10 @@ void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_ node_key = pref_path.substr(0, pref_path.rfind('/')); } -void Preferences::_errorDialog(Glib::ustring const &msg, Glib::ustring const &secondary) +void Preferences::_reportError(Glib::ustring const &msg, Glib::ustring const &secondary) { - if (_quiet) return; - if (_use_gui) { - Gtk::MessageDialog err( - msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); - err.set_secondary_text(secondary); - err.run(); - } else { - g_message("%s", msg.data()); - g_message("%s", secondary.data()); + if (_errorHandler) { + _errorHandler->handleError(msg, secondary); } } @@ -634,6 +620,11 @@ Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &pa return Entry(path, ptr); } +void Preferences::setErrorHandler(ErrorReporter* handler) +{ + _errorHandler = handler; +} + void Preferences::unload(bool save) { if(_instance) diff --git a/src/preferences.h b/src/preferences.h index fcdade816..3c25a520f 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -3,8 +3,9 @@ */ /* Authors: * Krzysztof Kosiński + * Jon A. Cruz * - * Copyright (C) 2008 Authors + * Copyright (C) 2008,2009 Authors * * Released under GNU GPL. Read the file 'COPYING' for more information. */ @@ -25,8 +26,13 @@ class SPCSSAttr; namespace Inkscape { +class ErrorReporter { +public: + virtual void handleError(Glib::ustring const& primary, Glib::ustring const& secondary ) const = 0; +}; + /** - * @brief Preference storage class + * @brief Preference storage class. * * This is a singleton that allows one to access the user preferences stored in * the preferences.xml file. The preferences are stored in a file system-like @@ -39,10 +45,9 @@ namespace Inkscape { * the path must start with a slash, and not contain a trailing slash. * An example of a correct path would be "/options/some_group/some_option". * - * All preferences are loaded when the first singleton pointer is requested, - * or when the static load() method is called. Before loading, the static - * variable @c use_gui should be set accordingly. To save the preferences, - * the method save() or the static function unload() can be used. + * All preferences are loaded when the first singleton pointer is requested. + * To save the preferences, the method save() or the static function unload() + * can be used. * * In future, this will be a virtual base from which specific backends * derive (e.g. GConf, flat XML file...) @@ -208,26 +213,6 @@ public: // utility methods - /** - * @name Load stored preferences and save them to the disk. - * @{ - */ - - /** - * @brief Load the preferences from the default location. - * - * Loads the stored user preferences and enables saving them. If there's - * no preferences file in the expected location, it creates it. Any changes - * made to the preferences before loading will be overridden by the stored - * prefs. Not calling load() is sometimes useful, e.g. for testing. - * - * @param use_gui Whether to use dialogs to notify about errors when - * loading the preferences. Set to false in console mode. - * @param quiet Whether to output any messages about preference loading. - * If this is true, the use_gui parameter is ignored. - */ - void load(bool use_gui=true, bool quiet=false); - /** * @brief Save all preferences to the hard disk. * @@ -428,6 +413,8 @@ public: return _instance; } + void setErrorHandler(ErrorReporter* handler); + /** * @brief Unload all preferences * @param save Whether to save the preferences; defaults to true @@ -456,9 +443,10 @@ private: Preferences(); ~Preferences(); void _loadDefaults(); + void _load(); void _getRawValue(Glib::ustring const &path, gchar const *&result); void _setRawValue(Glib::ustring const &path, gchar const *value); - void _errorDialog(Glib::ustring const &, Glib::ustring const &); + void _reportError(Glib::ustring const &, Glib::ustring const &); void _keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key); XML::Node *_getNode(Glib::ustring const &pref_path, bool create=false); XML::Node *_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create); @@ -471,9 +459,7 @@ private: std::string _prefs_dir; ///< Directory in which to look for the prefs file std::string _prefs_filename; ///< Full filename (with directory) of the prefs file XML::Document *_prefs_doc; ///< XML document storing all the preferences - bool _use_gui; ///< Use GUI error notifications? - bool _quiet; ///< Display any messages about loading? - bool _loaded; ///< Was a load attempt made? + ErrorReporter* _errorHandler; ///< Pointer to object reporting errors. bool _writable; ///< Will the preferences be saved at exit? /// Wrapper class for XML node observers