Code

Refactored preferences handling into a new version of
[inkscape.git] / src / preferences.h
1 /** @file
2  * @brief  Singleton class to access the preferences file in a convenient way.
3  *
4  * Authors:
5  *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
6  *
7  * Copyright (C) 2008 Authors
8  *
9  * Released under GNU GPL.  Read the file 'COPYING' for more information.
10  */
12 #ifndef INKSCAPE_PREFSTORE_H
13 #define INKSCAPE_PREFSTORE_H
15 #include <glibmm/ustring.h>
16 #include <string>
17 #include <climits>
18 #include <cfloat>
20 namespace Inkscape {
21 namespace XML {
22     class Node;
23     class Document;
24     class NodeObserver;
25 } // namespace XML
27 /**
28  * @brief Preference storage class
29  *
30  * This is a singleton that allows one to access the user preferences stored in
31  * the preferences.xml file. The preferences are stored in a tree hierarchy.
32  * Each preference is identified by its key, and sections of the key
33  * corresponding to the levels of the hierarchy are delimited with dots.
34  * Preferences are generally typeless - it's up to the programmer to ensure
35  * that a given preference is always accessed as the correct type.
36  *
37  * All preferences are loaded when the first singleton pointer is requested,
38  * or when the static load() method is called. Before loading, the static
39  * variable @c use_gui should be set accordingly. To save the preferences,
40  * the method save() or the static function unload() can be used.
41  *
42  * In future, this could be a virtual base from which specific backends
43  * derive (e.g. GConf, Windows registry, flat XML file...)
44  */
45 class Preferences {
46 public:
47     // utility methods
48     void save();
49     bool isWritable() { return _writable; }
50     
51     // some helpers
52     bool exists(Glib::ustring const &pref_key);
53     unsigned int childCount(Glib::ustring const &pref_key);
54     Glib::ustring getNthChild(Glib::ustring const &father_path, unsigned int n);
55     bool create(Glib::ustring const &pref_key);
56     
57     // getter methods
58     // Note that default values supplied as arguments are last-chance,
59     // and are overridden by those in preferences-defaults.h
60     bool getBool(Glib::ustring const &pref_path, Glib::ustring const &attr, bool def=false);
61     int getInt(Glib::ustring const &pref_path, Glib::ustring const &attr, int def=0);
62     int getIntLimited(Glib::ustring const &pref_path, Glib::ustring const &attr, int def=0, int min=INT_MIN, int max=INT_MAX);
63     double getDouble(Glib::ustring const &pref_path, Glib::ustring const &attr, double def=0.0);
64     double getDoubleLimited(Glib::ustring const &pref_path, Glib::ustring const &attr, double def=0.0, double min=DBL_MIN, double max=DBL_MAX);
65     Glib::ustring getString(Glib::ustring const &pref_path, Glib::ustring const &attr);
66     
67     // setter methods
68     void setBool(Glib::ustring const &pref_path, Glib::ustring const &attr, bool value);
69     void setInt(Glib::ustring const &pref_path, Glib::ustring const &attr, int value);
70     void setDouble(Glib::ustring const &pref_path, Glib::ustring const &attr, double value);
71     void setString(Glib::ustring const &pref_path, Glib::ustring const &attr, Glib::ustring const &value);
72     
73     // do not use this - it is only temporarily public to ease porting old code to this class
74     XML::Node *_getNode(Glib::ustring const &pref_path, bool create=false);
75     
76     // used for some obscure purpose in sp_desktop_widget_init
77     void addPrefsObserver(XML::NodeObserver *observer);
79     // singleton accessor
80     static Preferences *get() {
81         if (!_instance) _instance = new Preferences();
82         return _instance;
83     }
84     static void load() {
85         if (!_instance) _instance = new Preferences();
86     }
87     static void unload() {
88         if(_instance)
89         {
90             delete _instance;
91             _instance = NULL;
92         }
93     }
94     // this is a static member to reduce dependency bloat for this class
95     static bool use_gui; ///< Whether to use GUI error notifications
96     
97 private:
98     Preferences();
99     ~Preferences();
100     void _load();
101     void _loadDefaults();
102     void _errorDialog(Glib::ustring const &, Glib::ustring const &);
103     
104     // disable copying
105     Preferences(Preferences const &);
106     Preferences operator=(Preferences const &);
107     
108     std::string _prefs_basename; ///< Basename of the prefs file
109     std::string _prefs_dir; ///< Directory in which to look for the prefs file
110     std::string _prefs_filename; ///< Full filename (with directory) of the prefs file
111     bool _writable; ///< Will the preferences be saved at exit?
112     XML::Document *_prefs_doc; ///< XML document storing all the preferences
113     
114     static Preferences *_instance;
115 };
117 } // namespace Inkscape
119 #endif // INKSCAPE_PREFSTORE_H
121 /*
122   Local Variables:
123   mode:c++
124   c-file-style:"stroustrup"
125   c-file-offsets:((innamespace . 0)(inline-open . 0))
126   indent-tabs-mode:nil
127   fill-column:75
128   End:
129 */
130 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :