diff options
| author | Krzysztof Kosi??ski <tweenk.pl@gmail.com> | 2008-09-16 17:15:22 +0000 |
|---|---|---|
| committer | tweenk <tweenk@users.sourceforge.net> | 2008-09-16 17:15:22 +0000 |
| commit | 9d87d30b72145fdee954992a9dc70f8c60174d7d (patch) | |
| tree | 194a94ece6ed668ad7dc529de2cdd09b7565c6fe /src/preferences.h | |
| parent | fix leak of the arena and arenaitem (diff) | |
| download | inkscape-9d87d30b72145fdee954992a9dc70f8c60174d7d.tar.gz inkscape-9d87d30b72145fdee954992a9dc70f8c60174d7d.zip | |
Refactored preferences handling into a new version of
the Inkscape::Preferences class. Removed all use of
prefs_get_string_attribute(), pref_path_get_nth_child() and
create_pref() in favor of the new API. Replaced some "0 or 1" integer
preferences with booleans.
(bzr r6823)
Diffstat (limited to 'src/preferences.h')
| -rw-r--r-- | src/preferences.h | 126 |
1 files changed, 106 insertions, 20 deletions
diff --git a/src/preferences.h b/src/preferences.h index 4a7659586..90b2f518e 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -1,44 +1,130 @@ -/** \file - * \brief Static class where all preferences handling happens. +/** @file + * @brief Singleton class to access the preferences file in a convenient way. * * Authors: - * Ralf Stephan <ralf@ark.in-berlin.de> + * Krzysztof KosiĆski <tweenk.pl@gmail.com> * - * Copyright (C) 2005 Authors + * Copyright (C) 2008 Authors * * Released under GNU GPL. Read the file 'COPYING' for more information. */ -#ifndef INKSCAPE_PREFERENCES_H -#define INKSCAPE_PREFERENCES_H +#ifndef INKSCAPE_PREFSTORE_H +#define INKSCAPE_PREFSTORE_H + +#include <glibmm/ustring.h> +#include <string> +#include <climits> +#include <cfloat> namespace Inkscape { - namespace XML { - class Document; - } +namespace XML { + class Node; + class Document; + class NodeObserver; +} // namespace XML -class Preferences -{ +/** + * @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 tree hierarchy. + * Each preference is identified by its key, and sections of the key + * corresponding to the levels of the hierarchy are delimited with dots. + * Preferences are generally typeless - it's up to the programmer to ensure + * that a given preference is always accessed as the correct type. + * + * 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. + * + * In future, this could be a virtual base from which specific backends + * derive (e.g. GConf, Windows registry, flat XML file...) + */ +class Preferences { public: - static XML::Document *get(); - static void load(); - static void save(); - static void loadSkeleton(); - + // utility methods + void save(); + bool isWritable() { return _writable; } + + // some helpers + bool exists(Glib::ustring const &pref_key); + unsigned int childCount(Glib::ustring const &pref_key); + Glib::ustring getNthChild(Glib::ustring const &father_path, unsigned int n); + bool create(Glib::ustring const &pref_key); + + // getter methods + // Note that default values supplied as arguments are last-chance, + // and are overridden by those in preferences-defaults.h + bool getBool(Glib::ustring const &pref_path, Glib::ustring const &attr, bool def=false); + int getInt(Glib::ustring const &pref_path, Glib::ustring const &attr, int def=0); + int getIntLimited(Glib::ustring const &pref_path, Glib::ustring const &attr, int def=0, int min=INT_MIN, int max=INT_MAX); + double getDouble(Glib::ustring const &pref_path, Glib::ustring const &attr, double def=0.0); + double getDoubleLimited(Glib::ustring const &pref_path, Glib::ustring const &attr, double def=0.0, double min=DBL_MIN, double max=DBL_MAX); + Glib::ustring getString(Glib::ustring const &pref_path, Glib::ustring const &attr); + + // setter methods + void setBool(Glib::ustring const &pref_path, Glib::ustring const &attr, bool value); + void setInt(Glib::ustring const &pref_path, Glib::ustring const &attr, int value); + void setDouble(Glib::ustring const &pref_path, Glib::ustring const &attr, double value); + void setString(Glib::ustring const &pref_path, Glib::ustring const &attr, Glib::ustring const &value); + + // do not use this - it is only temporarily public to ease porting old code to this class + XML::Node *_getNode(Glib::ustring const &pref_path, bool create=false); + + // used for some obscure purpose in sp_desktop_widget_init + void addPrefsObserver(XML::NodeObserver *observer); + + // singleton accessor + static Preferences *get() { + if (!_instance) _instance = new Preferences(); + return _instance; + } + static void load() { + if (!_instance) _instance = new Preferences(); + } + static void unload() { + if(_instance) + { + delete _instance; + _instance = NULL; + } + } + // this is a static member to reduce dependency bloat for this class + static bool use_gui; ///< Whether to use GUI error notifications + private: + Preferences(); + ~Preferences(); + void _load(); + void _loadDefaults(); + void _errorDialog(Glib::ustring const &, Glib::ustring const &); + + // 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 + bool _writable; ///< Will the preferences be saved at exit? + XML::Document *_prefs_doc; ///< XML document storing all the preferences + + static Preferences *_instance; }; } // namespace Inkscape -#endif // INKSCAPE_PREFERENCES_H +#endif // INKSCAPE_PREFSTORE_H /* Local Variables: mode:c++ c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + c-file-offsets:((innamespace . 0)(inline-open . 0)) indent-tabs-mode:nil - fill-column:99 + fill-column:75 End: */ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : |
