/** @file
* Singleton class to access the preferences file in a convenient way.
*/
/* Authors:
* Krzysztof Kosi_ski <tweenk.pl@gmail.com>
* Jon A. Cruz <jon@joncruz.org>
*
* 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 <climits>
#include <cfloat>
#include <map>
#include <vector>
typedef unsigned int guint32;
virtual ~ErrorReporter() {}
};
/**
* 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
* hierarchy. They are generally typeless - it's up to the programmer to ensure
* that a given preference is always accessed as the correct type. The backend
* is not guaranteed to be tolerant to type mismatches.
*
* Preferences are identified by paths similar to file system paths. Components
* of the path are separated by a slash (/). As an additional requirement,
* 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.
* 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...)
*/
// #############################
// ## inner class definitions ##
// #############################
/**
* 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.
*/
/**
* Constructor.
*
* Since each Observer is assigned to a single path, the base
* constructor takes this path as an argument. This prevents one from
* adding a single observer to multiple paths, but this is intentional
* to simplify the implementation of observers and notifications.
*
* After you add the object with Preferences::addObserver(), you will
* receive notifications for everything below the attachment point.
* You can also specify a single preference as the watch point.
* For example, watching the directory "/foo" will give you notifications
* about "/foo/some_pref" as well as "/foo/some_dir/other_pref".
* Watching the preference "/options/some_group/some_option" will only
* generate notifications when this single preference changes.
*
* @param path Preference path the observer should watch.
*/
/**
* Notification about a preference change.
*
* @param new_val Entry object containing information about
* the modified preference.
*/
};
/**
* Data type representing a typeless value of a preference.
*
* This is passed to the observer in the notify() method.
* To retrieve useful data from it, use its member functions. Setting
* any preference using the Preferences class invalidates this object,
* so use its get methods before doing so.
*/
~Entry() {}
/**
* Check whether the received entry is valid.
*
* @return If false, the default value will be returned by the getters.
*/
/**
* Interpret the preference as a Boolean value.
*
* @param def Default value if the preference is not set.
*/
/**
* Interpret the preference as an integer.
*
* @param def Default value if the preference is not set.
*/
/**
* Interpret the preference as a limited integer.
*
* This method will return the default value if the interpreted value is
* larger than @c max or smaller than @c min. Do not use to store
* Boolean values as integers.
*
* @param def Default value if the preference is not set.
* @param min Minimum value allowed to return.
* @param max Maximum value allowed to return.
*/
/**
* Interpret the preference as a floating point value.
*
* @param def Default value if the preference is not set.
* @param unit Specifies the unit of the returned result. Will be ignored when equal to "". If the preference has no unit set, the default unit will be assumed.
*/
/**
* Interpret the preference as a limited floating point value.
*
* This method will return the default value if the interpreted value is
* larger than @c max or smaller than @c min.
*
* @param def Default value if the preference is not set.
* @param min Minimum value allowed to return.
* @param max Maximum value allowed to return.
* @param unit Specifies the unit of the returned result. Will be ignored when equal to "". If the preference has no unit set, the default unit will be assumed.
*/
inline double getDoubleLimited(double def=0.0, double min=DBL_MIN, double max=DBL_MAX, Glib::ustring const &unit = "") const;
/**
* Interpret the preference as an UTF-8 string.
*
* To store a filename, convert it using Glib::filename_to_utf8().
*/
/**
* Interpret the preference as a number followed by a unit (without space), and return this unit string.
*/
/**
* Interpret the preference as an RGBA color value.
*/
/**
* Interpret the preference as a CSS style.
*
* @return A CSS style that has to be unrefed when no longer necessary. Never NULL.
*/
/**
* Interpret the preference as a CSS style with directory-based
* inheritance.
*
* This function will look up the preferences with the same entry name
* in ancestor directories and return the inherited CSS style.
*
* @return Inherited CSS style that has to be unrefed after use. Never NULL.
*/
inline SPCSSAttr *getInheritedStyle() const;
/**
* Get the full path of the preference described by this Entry.
*/
/**
* Get the last component of the preference's path.
*
* E.g. For "/options/some_group/some_option" it will return "some_option".
*/
void const *_value;
};
// utility methods
/**
* Save all preferences to the hard disk.
*
* For some backends, the preferences may be saved as they are modified.
* Not calling this method doesn't guarantee the preferences are unmodified
* the next time Inkscape runs.
*/
void save();
/**
* Check whether saving the preferences will have any effect.
*/
/*@}*/
/**
* 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.
*/
/**
* @name Iterate over directories and entries.
* @{
*/
/**
* Get all entries from the specified directory.
*
* This method will return a vector populated with preference entries
* from the specified directory. Subdirectories will not be represented.
*/
/**
* Get all subdirectories of the specified directory.
*
* This will return a vector populated with full paths to the subdirectories
* present in the specified @c path.
*/
/*@}*/
/**
* @name Retrieve data from the preference storage.
* @{
*/
/**
* Retrieve a Boolean value.
*
* @param pref_path Path to the retrieved preference.
* @param def The default value to return if the preference is not set.
*/
}
/**
* Retrieve an integer.
*
* @param pref_path Path to the retrieved preference.
* @param def The default value to return if the preference is not set.
*/
}
/**
* Retrieve a limited integer.
*
* The default value is returned if the actual value is larger than @c max
* or smaller than @c min. Do not use to store Boolean values.
*
* @param pref_path Path to the retrieved preference.
* @param def The default value to return if the preference is not set.
* @param min Minimum value to return.
* @param max Maximum value to return.
*/
}
}
/**
* Retrieve a limited floating point value.
*
* The default value is returned if the actual value is larger than @c max
* or smaller than @c min.
*
* @param pref_path Path to the retrieved preference.
* @param def The default value to return if the preference is not set.
* @param min Minimum value to return.
* @param max Maximum value to return.
* @param unit Specifies the unit of the returned result. Will be ignored when equal to "". If the preference has no unit set, the default unit will be assumed.
*/
double getDoubleLimited(Glib::ustring const &pref_path, double def=0.0, double min=DBL_MIN, double max=DBL_MAX, Glib::ustring const &unit = "") {
}
/**
* Retrieve an UTF-8 string.
*
* @param pref_path Path to the retrieved preference.
*/
}
/**
* Retrieve the unit string.
*
* @param pref_path Path to the retrieved preference.
*/
}
}
/**
* Retrieve a CSS style.
*
* @param pref_path Path to the retrieved preference.
* @return A CSS style that has to be unrefed after use.
*/
}
/**
* Retrieve an inherited CSS style.
*
* This method will look up preferences with the same entry name in ancestor
* directories and return a style obtained by inheriting properties from
* ancestor styles.
*
* @param pref_path Path to the retrieved preference.
* @return An inherited CSS style that has to be unrefed after use.
*/
}
/**
* Retrieve a preference entry without specifying its type.
*/
/*@}*/
return _prefs_filename;
}
/**
* @name Update preference values.
* @{
*/
/**
* Set a Boolean value.
*/
/**
* Set an integer value.
*/
/**
* Set a floating point value.
*/
/**
* Set a floating point value with unit.
*/
/**
* Set an UTF-8 string value.
*/
/**
* Set an RGBA color value.
*/
/**
* Set a CSS style.
*/
/**
* Merge a CSS style with the current preference value.
*
* This method is similar to setStyle(), except that it merges the style
* rather than replacing it. This means that if @c style doesn't have
* a property set, it is left unchanged in the style stored in
* the preferences.
*/
/*@}*/
/**
* @name Receive notifications about preference changes.
* @{
*/
/**
* Register a preference observer.
*/
void addObserver(Observer &);
/**
* Remove an observer an prevent further notifications to it.
*/
void removeObserver(Observer &);
/*@}*/
/**
* @name Access and manipulate the Preferences object.
* @{
*/
/**
* Copies values from old location to new.
*/
/**
* Access the singleton Preferences object.
*/
if (!_instance) {
}
return _instance;
}
/**
* Unload all preferences.
*
* @param save Whether to save the preferences; defaults to true.
*
* 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.
*/
/*@}*/
/**
* Remove a node from prefs
* @param pref_path Path to entry
*
*/
/* helper methods used by Entry
* This will enable using the same Entry class with different backends.
* For now, however, those methods are not virtual. These methods assume
* that v._value is not NULL
*/
bool _extractBool(Entry const &v);
int _extractInt(Entry const &v);
double _extractDouble(Entry const &v);
Preferences();
~Preferences();
void _loadDefaults();
void _load();
XML::Node *_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create);
// disable copying
Preferences(Preferences const &);
/// Wrapper class for XML node observers
/// Map that keeps track of wrappers assigned to PrefObservers
// privilege escalation methods for PrefNodeObserver
};
/* Trivial inline Preferences::Entry functions.
* In fact only the _extract* methods do something, the rest is delegation
* to avoid duplication of code. There should be no performance hit if
* compiled with -finline-functions.
*/
{
return def;
} else {
}
}
{
return def;
} else {
}
}
{
return def;
} else {
}
}
{
return def;
} else {
}
}
inline double Preferences::Entry::getDoubleLimited(double def, double min, double max, Glib::ustring const &unit) const
{
return def;
} else {
} else {
}
}
}
{
return "";
} else {
}
}
{
return "";
} else {
}
}
{
return def;
} else {
}
}
{
return sp_repr_css_attr_new();
} else {
}
}
{
return sp_repr_css_attr_new();
} else {
}
}
{
return path_base;
}
} // namespace Inkscape
#endif // INKSCAPE_PREFSTORE_H
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0))
indent-tabs-mode:nil
fill-column:75
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :