summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6d6d2ee)
raw | patch | inline | side by side (parent: 6d6d2ee)
author | joncruz <joncruz@users.sourceforge.net> | |
Tue, 5 May 2009 07:33:52 +0000 (07:33 +0000) | ||
committer | joncruz <joncruz@users.sourceforge.net> | |
Tue, 5 May 2009 07:33:52 +0000 (07:33 +0000) |
src/preferences.cpp | patch | blob | history | |
src/preferences.h | patch | blob | history |
diff --git a/src/preferences.cpp b/src/preferences.cpp
index 071dbf91e2ec69119572c586027b065696c1feb5..cb71e03b24d2b3e21c588b68cff1d3888ac974c2 100644 (file)
--- a/src/preferences.cpp
+++ b/src/preferences.cpp
gchar *path = profile_path(NULL);
_prefs_dir = path;
g_free(path);
-
+
path = profile_path(_prefs_basename.data());
_prefs_filename = path;
g_free(path);
-
+
_loadDefaults();
_load();
}
Preferences::~Preferences()
-{
+{
// delete all PrefNodeObservers
for (_ObsMap::iterator i = _observer_map.begin(); i != _observer_map.end(); ) {
delete (*i++).second; // avoids reference to a deleted key
* Tries to load the user's preferences.xml file. If there is none, creates it.
*/
void Preferences::_load()
-{
+{
Glib::ustring const not_saved = _("Inkscape will run with default settings, "
"and new settings will not be saved. ");
// NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
-
+
// 1. Does the file exist?
if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_EXISTS)) {
// No - we need to create one.
}
// create some subdirectories for user stuff
char const *user_dirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
- for(int i=0; user_dirs[i]; ++i) {
+ for (int i=0; user_dirs[i]; ++i) {
char *dir = profile_path(user_dirs[i]);
g_mkdir(dir, 0755);
g_free(dir);
}
-
+
} else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) {
// The profile dir is not actually a directory
//_reportError(Glib::ustring::compose(_("%1 is not a valid directory."),
g_free(msg);
return;
}
-
+
// The prefs file was just created.
// We can return now and skip the rest of the load process.
_writable = true;
return;
}
-
+
// Yes, the pref file exists.
// 2. Is it a regular file?
if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) {
g_free(msg);
return;
}
-
+
// 3. Is the file readable?
gchar *prefs_xml = NULL; gsize len = 0;
if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) {
g_free(msg);
return;
}
+
// 4. Is it valid XML?
Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL);
g_free(prefs_xml);
g_free(msg);
return;
}
+
// 5. Basic sanity check: does the root element have a correct name?
if (strcmp(prefs_read->root()->name(), "inkscape")) {
//_reportError(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."),
Inkscape::GC::release(prefs_read);
return;
}
-
+
// Merge the loaded prefs with defaults.
_prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
Inkscape::GC::release(prefs_read);
*/
void Preferences::save()
{
- if (!_writable) return; // no-op if the prefs file is not writable
-
- // sp_repr_save_file uses utf-8 instead of the glib filename encoding.
- // I don't know why filenames are kept in utf-8 in Inkscape and then
- // converted to filename encoding when necessary through special functions
- // - wouldn't it be easier to keep things in the encoding they are supposed
- // to be in?
- Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename);
- if (utf8name.empty()) return;
- sp_repr_save_file(_prefs_doc, utf8name.data());
+ // no-op if the prefs file is not writable
+ if (_writable) {
+ // sp_repr_save_file uses utf-8 instead of the glib filename encoding.
+ // I don't know why filenames are kept in utf-8 in Inkscape and then
+ // converted to filename encoding when necessary through special functions
+ // - wouldn't it be easier to keep things in the encoding they are supposed
+ // to be in?
+
+ // No, it would not. There are many reasons, one key reason being that the
+ // rest of GTK+ is explicitly UTF-8. From an engineering standpoint, keeping
+ // the filesystem encoding would change things from a one-to-many problem to
+ // instead be a many-to-many problem. Also filesystem encoding can change
+ // from one run of the program to the next, so can not be stored.
+ // There are many other factors, so ask if you would like to learn them. - JAC
+ Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename);
+ if (!utf8name.empty()) {
+ sp_repr_save_file(_prefs_doc, utf8name.data());
+ }
+ }
}
bool Preferences::getLastError( Glib::ustring& primary, Glib::ustring& secondary )
@@ -252,12 +263,13 @@ std::vector<Preferences::Entry> Preferences::getAllEntries(Glib::ustring const &
{
std::vector<Entry> temp;
Inkscape::XML::Node *node = _getNode(path, false);
- if (!node) return temp;
-
- // argh - purge this Util::List nonsense from XML classes fast
- Inkscape::Util::List<Inkscape::XML::AttributeRecord const> alist = node->attributeList();
- for (; alist; ++alist)
- temp.push_back( Entry(path + '/' + g_quark_to_string(alist->key), static_cast<void const*>(alist->value.pointer())) );
+ if (node) {
+ // argh - purge this Util::List nonsense from XML classes fast
+ Inkscape::Util::List<Inkscape::XML::AttributeRecord const> alist = node->attributeList();
+ for (; alist; ++alist) {
+ temp.push_back( Entry(path + '/' + g_quark_to_string(alist->key), static_cast<void const*>(alist->value.pointer())) );
+ }
+ }
return temp;
}
{
std::vector<Glib::ustring> temp;
Inkscape::XML::Node *node = _getNode(path, false);
- if (!node) return temp;
-
- for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
- temp.push_back(path + '/' + i->attribute("id"));
+ if (node) {
+ for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
+ temp.push_back(path + '/' + i->attribute("id"));
+ }
}
return temp;
}
@@ -383,33 +395,34 @@ void Preferences::PrefNodeObserver::notifyAttributeChanged(XML::Node &node, GQua
{
// filter out attributes we don't watch
gchar const *attr_name = g_quark_to_string(name);
- if ( !_filter.empty() && _filter != attr_name ) return;
-
- _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
- Glib::ustring notify_path = _observer.observed_path;
-
- if (!d->_is_attr) {
- std::vector<gchar const *> path_fragments;
- notify_path.reserve(256); // this will make appending operations faster
-
- // walk the XML tree, saving each of the id attributes in a vector
- // we terminate when we hit the observer's attachment node, because the path to this node
- // is already stored in notify_path
- for (XML::NodeParentIterator n = &node; static_cast<XML::Node*>(n) != d->_node; ++n)
- path_fragments.push_back(n->attribute("id"));
- // assemble the elements into a path
- for (std::vector<gchar const *>::reverse_iterator i = path_fragments.rbegin(); i != path_fragments.rend(); ++i) {
+ if ( _filter.empty() || (_filter == attr_name) ) {
+ _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
+ Glib::ustring notify_path = _observer.observed_path;
+
+ if (!d->_is_attr) {
+ std::vector<gchar const *> path_fragments;
+ notify_path.reserve(256); // this will make appending operations faster
+
+ // walk the XML tree, saving each of the id attributes in a vector
+ // we terminate when we hit the observer's attachment node, because the path to this node
+ // is already stored in notify_path
+ for (XML::NodeParentIterator n = &node; static_cast<XML::Node*>(n) != d->_node; ++n) {
+ path_fragments.push_back(n->attribute("id"));
+ }
+ // assemble the elements into a path
+ for (std::vector<gchar const *>::reverse_iterator i = path_fragments.rbegin(); i != path_fragments.rend(); ++i) {
+ notify_path.push_back('/');
+ notify_path.append(*i);
+ }
+
+ // append attribute name
notify_path.push_back('/');
- notify_path.append(*i);
+ notify_path.append(attr_name);
}
- // append attribute name
- notify_path.push_back('/');
- notify_path.append(attr_name);
+ Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
+ _observer.notify(val);
}
-
- Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
- _observer.notify(val);
}
/**
@@ -419,7 +432,7 @@ XML::Node *Preferences::_findObserverNode(Glib::ustring const &pref_path, Glib::
{
// first assume that the last path element is an entry.
_keySplit(pref_path, node_key, attr_key);
-
+
// find the node corresponding to the "directory".
Inkscape::XML::Node *node = _getNode(node_key, create), *child;
for (child = node->firstChild(); child; child = child->next()) {
@@ -439,45 +452,47 @@ XML::Node *Preferences::_findObserverNode(Glib::ustring const &pref_path, Glib::
void Preferences::addObserver(Observer &o)
{
// prevent adding the same observer twice
- if ( _observer_map.find(&o) != _observer_map.end() ) return;
-
- Glib::ustring node_key, attr_key;
- Inkscape::XML::Node *node;
- node = _findObserverNode(o.observed_path, node_key, attr_key, false);
- if (!node) return;
-
- // set additional data
- _ObserverData *priv_data = new _ObserverData;
- priv_data->_node = node;
- priv_data->_is_attr = !attr_key.empty();
- o._data = static_cast<void*>(priv_data);
-
- _observer_map[&o] = new PrefNodeObserver(o, attr_key);
-
- // if we watch a single pref, we want to receive notifications only for a single node
- if (priv_data->_is_attr) {
- node->addObserver( *(_observer_map[&o]) );
- } else {
- node->addSubtreeObserver( *(_observer_map[&o]) );
+ if ( _observer_map.find(&o) == _observer_map.end() ) {
+ Glib::ustring node_key, attr_key;
+ Inkscape::XML::Node *node;
+ node = _findObserverNode(o.observed_path, node_key, attr_key, false);
+ if (node) {
+ // set additional data
+ _ObserverData *priv_data = new _ObserverData;
+ priv_data->_node = node;
+ priv_data->_is_attr = !attr_key.empty();
+ o._data = static_cast<void*>(priv_data);
+
+ _observer_map[&o] = new PrefNodeObserver(o, attr_key);
+
+ // if we watch a single pref, we want to receive notifications only for a single node
+ if (priv_data->_is_attr) {
+ node->addObserver( *(_observer_map[&o]) );
+ } else {
+ node->addSubtreeObserver( *(_observer_map[&o]) );
+ }
+ }
}
}
void Preferences::removeObserver(Observer &o)
{
// prevent removing an observer which was not added
- if ( _observer_map.find(&o) == _observer_map.end() ) return;
- Inkscape::XML::Node *node = static_cast<_ObserverData*>(o._data)->_node;
- _ObserverData *priv_data = static_cast<_ObserverData*>(o._data);
- o._data = NULL;
-
- if (priv_data->_is_attr)
- node->removeObserver( *(_observer_map[&o]) );
- else
- node->removeSubtreeObserver( *(_observer_map[&o]) );
+ if ( _observer_map.find(&o) != _observer_map.end() ) {
+ Inkscape::XML::Node *node = static_cast<_ObserverData*>(o._data)->_node;
+ _ObserverData *priv_data = static_cast<_ObserverData*>(o._data);
+ o._data = NULL;
+
+ if (priv_data->_is_attr) {
+ node->removeObserver( *(_observer_map[&o]) );
+ } else {
+ node->removeSubtreeObserver( *(_observer_map[&o]) );
+ }
- delete priv_data;
- delete _observer_map[&o];
- _observer_map.erase(&o);
+ delete priv_data;
+ delete _observer_map[&o];
+ _observer_map.erase(&o);
+ }
}
@@ -498,41 +513,47 @@ Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool c
// No longer necessary, can cause problems with input devices which have a dot in the name
// g_assert( pref_key.find('.') == Glib::ustring::npos );
- Inkscape::XML::Node *node = _prefs_doc->root(), *child = NULL;
+ Inkscape::XML::Node *node = _prefs_doc->root();
+ Inkscape::XML::Node *child = NULL;
gchar **splits = g_strsplit(pref_key.data(), "/", 0);
-
- if ( splits == NULL ) return node;
-
- for (int part_i = 0; splits[part_i]; ++part_i) {
- // skip empty path segments
- if (!splits[part_i][0]) continue;
-
- for (child = node->firstChild(); child; child = child->next())
- if (!strcmp(splits[part_i], child->attribute("id"))) break;
-
- // If the previous loop found a matching key, child now contains the node
- // matching the processed key part. If no node was found then it is NULL.
- if (!child) {
- if (create) {
- // create the rest of the key
- while(splits[part_i]) {
- child = node->document()->createElement("group");
- child->setAttribute("id", splits[part_i]);
- node->appendChild(child);
-
- ++part_i;
- node = child;
+
+ if ( splits ) {
+ for (int part_i = 0; splits[part_i]; ++part_i) {
+ // skip empty path segments
+ if (!splits[part_i][0]) {
+ continue;
+ }
+
+ for (child = node->firstChild(); child; child = child->next()) {
+ if (!strcmp(splits[part_i], child->attribute("id"))) {
+ break;
+ }
+ }
+
+ // If the previous loop found a matching key, child now contains the node
+ // matching the processed key part. If no node was found then it is NULL.
+ if (!child) {
+ if (create) {
+ // create the rest of the key
+ while(splits[part_i]) {
+ child = node->document()->createElement("group");
+ child->setAttribute("id", splits[part_i]);
+ node->appendChild(child);
+
+ ++part_i;
+ node = child;
+ }
+ g_strfreev(splits);
+ return node;
+ } else {
+ return NULL;
}
- g_strfreev(splits);
- return node;
- } else {
- return NULL;
}
- }
- node = child;
+ node = child;
+ }
+ g_strfreev(splits);
}
- g_strfreev(splits);
return node;
}
// create node and attribute keys
Glib::ustring node_key, attr_key;
_keySplit(path, node_key, attr_key);
-
+
// retrieve the attribute
Inkscape::XML::Node *node = _getNode(node_key, false);
if ( node == NULL ) {
// create node and attribute keys
Glib::ustring node_key, attr_key;
_keySplit(path, node_key, attr_key);
-
+
// set the attribute
Inkscape::XML::Node *node = _getNode(node_key, true);
node->setAttribute(attr_key.data(), value);
bool Preferences::_extractBool(Entry const &v)
{
gchar const *s = static_cast<gchar const *>(v._value);
- if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) return false;
- return true;
+ if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) {
+ return false;
+ } else {
+ return true;
+ }
}
int Preferences::_extractInt(Entry const &v)
{
gchar const *s = static_cast<gchar const *>(v._value);
- if ( !strcmp(s, "true") ) return true;
- if ( !strcmp(s, "false") ) return false;
- return atoi(s);
+ if ( !strcmp(s, "true") ) {
+ return true;
+ } else if ( !strcmp(s, "false") ) {
+ return false;
+ } else {
+ return atoi(s);
+ }
}
double Preferences::_extractDouble(Entry const &v)
// we'll have to walk up the tree and call sp_repr_css_attr_add_from_string
Glib::ustring node_key, attr_key;
_keySplit(v._pref_path, node_key, attr_key);
-
+
Inkscape::XML::Node *node = _getNode(node_key, false);
return sp_repr_css_attr_inherited(node, attr_key.data());
}
void Preferences::unload(bool save)
{
- if(_instance)
+ if (_instance)
{
- if (save) _instance->save();
+ if (save) {
+ _instance->save();
+ }
delete _instance;
_instance = NULL;
}
} // namespace Inkscape
-
+
/*
Local Variables:
mode:c++
diff --git a/src/preferences.h b/src/preferences.h
index 7aded5a03cb82603c3c65400bdd30b2776c25232..4e3d0732c00fb6b359ad26cc3f2d94f217d92bed 100644 (file)
--- a/src/preferences.h
+++ b/src/preferences.h
// #############################
// ## 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.
*/
* 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
*
* @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
* @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 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
-
+
/**
* @brief Save all preferences to the hard disk.
*
*/
std::vector<Glib::ustring> getAllDirs(Glib::ustring const &path);
/*@}*/
-
+
/**
* @name Retrieve data from the preference storage.
* @{
*/
Entry const getEntry(Glib::ustring const &pref_path);
/*@}*/
-
+
/**
* @name Update preference values.
* @{
* @name Access and manipulate the Preferences object.
* @{
*/
-
+
/**
* @brief Access the singleton Preferences object.
*/
static Preferences *get() {
- if (!_instance) _instance = new Preferences();
+ if (!_instance) {
+ _instance = new Preferences();
+ }
return _instance;
}
*/
static void unload(bool save=true);
/*@}*/
-
+
protected:
/* helper methods used by Entry
* This will enable using the same Entry class with different backends.
Glib::ustring _extractString(Entry const &v);
SPCSSAttr *_extractStyle(Entry const &v);
SPCSSAttr *_extractInheritedStyle(Entry const &v);
-
+
private:
Preferences();
~Preferences();
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
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;
};
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 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