Code

Use subdirectories with icon sizes.
[inkscape.git] / src / preferences.h
index a87a2771813816d9ef0722559522784868949ea4..5e1ccf9d6f7a451f48fd3d97b328e618035cc1a3 100644 (file)
@@ -2,9 +2,10 @@
  * @brief  Singleton class to access the preferences file in a convenient way.
  */
 /* Authors:
- *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
+ *   Krzysztof Kosi_ski <tweenk.pl@gmail.com>
+ *   Jon A. Cruz <jon@joncruz.org>
  *
- * Copyright (C) 2008 Authors
+ * Copyright (C) 2008,2009 Authors
  *
  * Released under GNU GPL.  Read the file 'COPYING' for more information.
  */
 #ifndef INKSCAPE_PREFSTORE_H
 #define INKSCAPE_PREFSTORE_H
 
-#include <glibmm/ustring.h>
 #include <string>
 #include <map>
 #include <vector>
 #include <climits>
 #include <cfloat>
+#include <glibmm/ustring.h>
 #include "xml/xml-forward.h"
 #include "xml/repr.h"
 
@@ -25,8 +26,14 @@ class SPCSSAttr;
 
 namespace Inkscape {
 
+class ErrorReporter {
+public:
+    virtual ~ErrorReporter() {}
+    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 +46,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...)
@@ -52,19 +58,20 @@ public:
     // #############################
     // ## inner class definitions ##
     // #############################
-    
+
     class Entry;
     class Observer;
-    
+
     /**
      * @brief Base class for preference observers
-     * 
+     *
      * If you want to watch for changes in the preferences, you'll have to
      * derive a class from this one and override the notify() method.
      */
     class Observer {
         friend class Preferences;
     public:
+
         /**
          * @brief Constructor.
          *
@@ -85,19 +92,20 @@ public:
          */
         Observer(Glib::ustring const &path);
         virtual ~Observer();
+
         /**
          * @brief Notification about a preference change
          * @param new_val  Entry object containing information about
          *                 the modified preference
          */
         virtual void notify(Preferences::Entry const &new_val) = 0;
-        
+
         Glib::ustring const observed_path; ///< Path which the observer watches
     private:
         void *_data; ///< additional data used by the implementation while the observer is active
     };
-    
-    
+
+
     /**
      * @brief Data type representing a typeless value of a preference
      *
@@ -112,22 +120,25 @@ public:
         ~Entry() {}
         Entry() : _pref_path(""), _value(NULL) {} // needed to enable use in maps
         Entry(Entry const &other) : _pref_path(other._pref_path), _value(other._value) {}
+
         /**
          * @brief Check whether the received entry is valid.
          * @return If false, the default value will be returned by the getters.
          */
         bool isValid() const { return _value != NULL; }
-        
+
         /**
          * @brief Interpret the preference as a Boolean value.
          * @param def Default value if the preference is not set
          */
         inline bool getBool(bool def=false) const;
+
         /**
          * @brief Interpret the preference as an integer.
          * @param def Default value if the preference is not set
          */
         inline int getInt(int def=0) const;
+
         /**
          * @brief Interpret the preference as a limited integer.
          *
@@ -140,11 +151,13 @@ public:
          * @param max Maximum value allowed to return
          */
         inline int getIntLimited(int def=0, int min=INT_MIN, int max=INT_MAX) const;
+
         /**
          * @brief Interpret the preference as a floating point value.
          * @param def Default value if the preference is not set
          */
         inline double getDouble(double def=0.0) const;
+
         /**
          * @brief Interpret the preference as a limited floating point value.
          *
@@ -156,17 +169,25 @@ public:
          * @param max Maximum value allowed to return
          */
         inline double getDoubleLimited(double def=0.0, double min=DBL_MIN, double max=DBL_MAX) const;
+
         /**
          * @brief Interpret the preference as an UTF-8 string.
          *
          * To store a filename, convert it using Glib::filename_to_utf8().
          */
         inline Glib::ustring getString() const;
+
+        /**
+         * @brief Interpret the preference as an RGBA color value.
+         */
+        inline guint32 getColor(guint32 def) const;
+
         /**
          * @brief Interpret the preference as a CSS style.
          * @return A CSS style that has to be unrefed when no longer necessary. Never NULL.
          */
         inline SPCSSAttr *getStyle() const;
+
         /**
          * @brief Interpret the preference as a CSS style with directory-based
          *        inheritance
@@ -177,11 +198,12 @@ public:
          * @return Inherited CSS style that has to be unrefed after use. Never NULL.
          */
         inline SPCSSAttr *getInheritedStyle() const;
-        
+
         /**
          * @brief Get the full path of the preference described by this Entry.
          */
         Glib::ustring const &getPath() const { return _pref_path; }
+
         /**
          * @brief Get the last component of the preference's path
          *
@@ -190,32 +212,13 @@ public:
         Glib::ustring getEntryName() const;
     private:
         Entry(Glib::ustring const &path, void const *v) : _pref_path(path), _value(v) {}
-        
+
         Glib::ustring _pref_path;
         void const *_value;
     };
 
     // 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.
      *
@@ -224,12 +227,27 @@ public:
      * the next time Inkscape runs.
      */
     void save();
+
     /**
      * @brief Check whether saving the preferences will have any effect.
      */
     bool isWritable() { return _writable; }
     /*@}*/
 
+    /**
+     * @brief Return details of the last encountered error, if any.
+     *
+     * This method will return true if an error has been encountered, and fill
+     * in the primary and secondary error strings of the last error. If an error
+     * had been encountered, this will reset it.
+     *
+     * @param string to set to the primary error message.
+     * @param string to set to the secondary error message.
+     *
+     * @return True if an error has occurred since last checking, false otherwise.
+     */
+    bool getLastError( Glib::ustring& primary, Glib::ustring& secondary );
+
     /**
      * @name Iterate over directories and entries.
      * @{
@@ -242,6 +260,7 @@ public:
      * from the specified directory. Subdirectories will not be represented.
      */
     std::vector<Entry> getAllEntries(Glib::ustring const &path);
+
     /**
      * @brief Get all subdirectories of the specified directory
      *
@@ -250,11 +269,12 @@ public:
      */
     std::vector<Glib::ustring> getAllDirs(Glib::ustring const &path);
     /*@}*/
-    
+
     /**
      * @name Retrieve data from the preference storage.
      * @{
      */
+
     /**
      * @brief Retrieve a Boolean value
      * @param pref_path Path to the retrieved preference
@@ -263,6 +283,7 @@ public:
     bool getBool(Glib::ustring const &pref_path, bool def=false) {
         return getEntry(pref_path).getBool(def);
     }
+
     /**
      * @brief Retrieve an integer
      * @param pref_path Path to the retrieved preference
@@ -271,6 +292,7 @@ public:
     int getInt(Glib::ustring const &pref_path, int def=0) {
         return getEntry(pref_path).getInt(def);
     }
+
     /**
      * @brief Retrieve a limited integer
      *
@@ -288,6 +310,7 @@ public:
     double getDouble(Glib::ustring const &pref_path, double def=0.0) {
         return getEntry(pref_path).getDouble(def);
     }
+
     /**
      * @brief Retrieve a limited floating point value
      *
@@ -302,6 +325,7 @@ public:
     double getDoubleLimited(Glib::ustring const &pref_path, double def=0.0, double min=DBL_MIN, double max=DBL_MAX) {
         return getEntry(pref_path).getDoubleLimited(def, min, max);
     }
+
     /**
      * @brief Retrieve an UTF-8 string
      * @param pref_path Path to the retrieved preference
@@ -309,6 +333,11 @@ public:
     Glib::ustring getString(Glib::ustring const &pref_path) {
         return getEntry(pref_path).getString();
     }
+
+    guint32 getColor(Glib::ustring const &pref_path, guint32 def=0x000000ff) {
+        return getEntry(pref_path).getColor(def);
+    }
+
     /**
      * @brief Retrieve a CSS style
      * @param pref_path Path to the retrieved preference
@@ -317,6 +346,7 @@ public:
     SPCSSAttr *getStyle(Glib::ustring const &pref_path) {
         return getEntry(pref_path).getStyle();
     }
+
     /**
      * @brief Retrieve an inherited CSS style
      *
@@ -330,12 +360,13 @@ public:
     SPCSSAttr *getInheritedStyle(Glib::ustring const &pref_path) {
         return getEntry(pref_path).getInheritedStyle();
     }
+
     /**
      * @brief Retrieve a preference entry without specifying its type
      */
     Entry const getEntry(Glib::ustring const &pref_path);
     /*@}*/
-    
+
     /**
      * @name Update preference values.
      * @{
@@ -345,22 +376,32 @@ public:
      * @brief Set a Boolean value
      */
     void setBool(Glib::ustring const &pref_path, bool value);
+
     /**
      * @brief Set an integer value
      */
     void setInt(Glib::ustring const &pref_path, int value);
+
     /**
      * @brief Set a floating point value
      */
     void setDouble(Glib::ustring const &pref_path, double value);
+
     /**
      * @brief Set an UTF-8 string value
      */
     void setString(Glib::ustring const &pref_path, Glib::ustring const &value);
+
+    /**
+     * @brief Set an RGBA color value
+     */
+    void setColor(Glib::ustring const &pref_path, guint32 value);
+
     /**
      * @brief Set a CSS style
      */
     void setStyle(Glib::ustring const &pref_path, SPCSSAttr *style);
+
     /**
      * @brief Merge a CSS style with the current preference value
      *
@@ -376,10 +417,12 @@ public:
      * @name Receive notifications about preference changes.
      * @{
      */
+
     /**
      * @brief Register a preference observer
      */
     void addObserver(Observer &);
+
     /**
      * @brief Remove an observer an prevent further notifications to it.
      */
@@ -390,28 +433,36 @@ public:
      * @name Access and manipulate the Preferences object.
      * @{
      */
-     
+
+
+    /**
+     * Copies values from old location to new.
+     */
+    static void migrate( std::string const& legacyDir, std::string const& prefdir );
+
     /**
      * @brief Access the singleton Preferences object.
      */
     static Preferences *get() {
-        if (!_instance) _instance = new Preferences();
+        if (!_instance) {
+            _instance = new Preferences();
+        }
         return _instance;
     }
+
+    void setErrorHandler(ErrorReporter* handler);
+
     /**
-     * @brief Unload all preferences and store them on the hard disk.
+     * @brief Unload all preferences
+     * @param save Whether to save the preferences; defaults to true
      *
-     * This deletes the singleton object. Calling get() after this function is
+     * This deletes the singleton object. Calling get() after this function
+     * will reinstate it, so you shouldn't. Pass false as the parameter
+     * to suppress automatic saving.
      */
-    static void unload() {
-        if(_instance)
-        {
-            delete _instance;
-            _instance = NULL;
-        }
-    }
+    static void unload(bool save=true);
     /*@}*/
-    
+
 protected:
     /* helper methods used by Entry
      * This will enable using the same Entry class with different backends.
@@ -422,46 +473,49 @@ protected:
     int _extractInt(Entry const &v);
     double _extractDouble(Entry const &v);
     Glib::ustring _extractString(Entry const &v);
+    guint32 _extractColor(Entry const &v);
     SPCSSAttr *_extractStyle(Entry const &v);
     SPCSSAttr *_extractInheritedStyle(Entry const &v);
-    
+
 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);
-    
+
     // disable copying
     Preferences(Preferences const &);
     Preferences operator=(Preferences const &);
-    
+
     std::string _prefs_basename; ///< Basename of the prefs file
     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
+    Glib::ustring _lastErrPrimary; ///< Last primary error message, if any.
+    Glib::ustring _lastErrSecondary; ///< Last secondary error message, if any.
     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?
-    
+    bool _hasError; ///< Indication that some error has occurred;
+
     /// Wrapper class for XML node observers
     class PrefNodeObserver;
-    
+
     typedef std::map<Observer *, PrefNodeObserver *> _ObsMap;
     /// Map that keeps track of wrappers assigned to PrefObservers
     _ObsMap _observer_map;
-    
+
     // privilege escalation methods for PrefNodeObserver
     static Entry const _create_pref_value(Glib::ustring const &, void const *ptr);
     static void *_get_pref_observer_data(Observer &o) { return o._data; }
-    
+
     static Preferences *_instance;
-    
+
 friend class PrefNodeObserver;
 friend class Entry;
 };
@@ -474,52 +528,85 @@ friend class Entry;
 
 inline bool Preferences::Entry::getBool(bool def) const
 {
-    if (!this->isValid()) return def;
-    return Inkscape::Preferences::get()->_extractBool(*this);
+    if (!this->isValid()) {
+        return def;
+    } else {
+        return Inkscape::Preferences::get()->_extractBool(*this);
+    }
 }
 
 inline int Preferences::Entry::getInt(int def) const
 {
-    if (!this->isValid()) return def;
-    return Inkscape::Preferences::get()->_extractInt(*this);
+    if (!this->isValid()) {
+        return def;
+    } else {
+        return Inkscape::Preferences::get()->_extractInt(*this);
+    }
 }
 
 inline int Preferences::Entry::getIntLimited(int def, int min, int max) const
 {
-    if (!this->isValid()) return def;
-    int val = Inkscape::Preferences::get()->_extractInt(*this);
-    return ( val >= min && val <= max ? val : def );
+    if (!this->isValid()) {
+        return def;
+    } else {
+        int val = Inkscape::Preferences::get()->_extractInt(*this);
+        return ( val >= min && val <= max ? val : def );
+    }
 }
 
 inline double Preferences::Entry::getDouble(double def) const
 {
-    if (!this->isValid()) return def;
-    return Inkscape::Preferences::get()->_extractDouble(*this);
+    if (!this->isValid()) {
+        return def;
+    } else {
+        return Inkscape::Preferences::get()->_extractDouble(*this);
+    }
 }
 
 inline double Preferences::Entry::getDoubleLimited(double def, double min, double max) const
 {
-    if (!this->isValid()) return def;
-    double val = Inkscape::Preferences::get()->_extractDouble(*this);
-    return ( val >= min && val <= max ? val : def );
+    if (!this->isValid()) {
+        return def;
+    } else {
+        double val = Inkscape::Preferences::get()->_extractDouble(*this);
+        return ( val >= min && val <= max ? val : def );
+    }
 }
 
 inline Glib::ustring Preferences::Entry::getString() const
 {
-    if (!this->isValid()) return "";
-    return Inkscape::Preferences::get()->_extractString(*this);
+    if (!this->isValid()) {
+        return "";
+    } else {
+        return Inkscape::Preferences::get()->_extractString(*this);
+    }
+}
+
+inline guint32 Preferences::Entry::getColor(guint32 def) const
+{
+    if (!this->isValid()) {
+        return def;
+    } else {
+        return Inkscape::Preferences::get()->_extractColor(*this);
+    }
 }
 
 inline SPCSSAttr *Preferences::Entry::getStyle() const
 {
-    if (!this->isValid()) return sp_repr_css_attr_new();
-    return Inkscape::Preferences::get()->_extractStyle(*this);
+    if (!this->isValid()) {
+        return sp_repr_css_attr_new();
+    } else {
+        return Inkscape::Preferences::get()->_extractStyle(*this);
+    }
 }
 
 inline SPCSSAttr *Preferences::Entry::getInheritedStyle() const
 {
-    if (!this->isValid()) return sp_repr_css_attr_new();
-    return Inkscape::Preferences::get()->_extractInheritedStyle(*this);
+    if (!this->isValid()) {
+        return sp_repr_css_attr_new();
+    } else {
+        return Inkscape::Preferences::get()->_extractInheritedStyle(*this);
+    }
 }
 
 inline Glib::ustring Preferences::Entry::getEntryName() const