Code

From trunk
[inkscape.git] / src / preferences.cpp
1 /** @file
2  * @brief  Singleton class to access the preferences file - implementation
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 #include "preferences.h"
13 #include "preferences-skeleton.h"
14 #include "inkscape.h"
15 #include "xml/repr.h"
16 #include "xml/node-observer.h"
17 #include "xml/node-iterators.h"
18 #include "xml/attribute-record.h"
19 #include <cstring>
20 #include <vector>
21 #include <glibmm/fileutils.h>
22 #include <glibmm/i18n.h>
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <gtkmm/messagedialog.h>
27 #define PREFERENCES_FILE_NAME "preferences.xml"
29 namespace Inkscape {
31 // private inner class definition
33 /**
34  * @brief XML - prefs observer bridge
35  *
36  * This is an XML node observer that watches for changes in the XML document storing the preferences.
37  * It is used to implement preference observers.
38  */
39 class Preferences::PrefNodeObserver : public XML::NodeObserver {
40 public:
41     PrefNodeObserver(Observer &o, Glib::ustring const &filter) :
42         _observer(o),
43         _filter(filter)
44     {}
45     virtual ~PrefNodeObserver() {}
46     virtual void notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char>);
47 private:
48     Observer &_observer;
49     Glib::ustring const _filter;
50 };
53 Preferences::Preferences() :
54     _prefs_basename(PREFERENCES_FILE_NAME),
55     _prefs_dir(""),
56     _prefs_filename(""),
57     _writable(false),
58     _prefs_doc(NULL)
59 {
60     // profile_path essentailly returns the argument prefixed by the profile directory.
61     gchar *path = profile_path(NULL);
62     _prefs_dir = path;
63     g_free(path);
64     
65     path = profile_path(_prefs_basename.data());
66     _prefs_filename = path;
67     g_free(path);
68     
69     _load();
70 }
72 Preferences::~Preferences()
73 {
74     // when the preferences are unloaded, save them
75     save();
76     
77     // delete all PrefNodeObservers
78     for (_ObsMap::iterator i = _observer_map.begin(); i != _observer_map.end(); ) {
79         delete (*i++).second; // avoids reference to a deleted key
80     }
81     // unref XML document
82     Inkscape::GC::release(_prefs_doc);
83 }
85 /**
86  * @brief Load internal defaults
87  *
88  * In the future this will try to load the system-wide file before falling
89  * back to the internal defaults.
90  */
91 void Preferences::_loadDefaults()
92 {
93     _prefs_doc = sp_repr_read_mem(preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL);
94 }
96 /**
97  * @brief Load the user's customized preferences
98  *
99  * Tries to load the user's preferences.xml file. If there is none, creates it.
100  * Displays dialog boxes on any errors.
101  */
102 void Preferences::_load()
104     _loadDefaults();
105     
106     Glib::ustring const not_saved = _("Inkscape will run with default settings, "
107                                 "and new settings will not be saved. ");
108     
109     // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
110     
111     // 1. Does the file exist?
112     if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_EXISTS)) {
113         // No - we need to create one.
114         // Does the profile directory exist?
115         if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_EXISTS)) {
116             // No - create the profile directory
117             if (g_mkdir(_prefs_dir.data(), 0755)) {
118                 // the creation failed
119                 //_errorDialog(Glib::ustring::compose(_("Cannot create profile directory %1."),
120                 //    Glib::filename_to_utf8(_prefs_dir)), not_saved);
121                 gchar *msg = g_strdup_printf(_("Cannot create profile directory %s."),
122                     Glib::filename_to_utf8(_prefs_dir).data());
123                 _errorDialog(msg, not_saved);
124                 g_free(msg);
125                 return;
126             }
127             // create some subdirectories for user stuff
128             char const *user_dirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
129             for(int i=0; user_dirs[i]; ++i) {
130                 char *dir = profile_path(user_dirs[i]);
131                 g_mkdir(dir, 0755);
132                 g_free(dir);
133             }
134             
135         } else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) {
136             // The profile dir is not actually a directory
137             //_errorDialog(Glib::ustring::compose(_("%1 is not a valid directory."),
138             //    Glib::filename_to_utf8(_prefs_dir)), not_saved);
139             gchar *msg = g_strdup_printf(_("%s is not a valid directory."),
140                 Glib::filename_to_utf8(_prefs_dir).data());
141             _errorDialog(msg, not_saved);
142             g_free(msg);
143             return;
144         }
145         // The profile dir exists and is valid.
146         if (!g_file_set_contents(_prefs_filename.data(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) {
147             // The write failed.
148             //_errorDialog(Glib::ustring::compose(_("Failed to create the preferences file %1."),
149             //    Glib::filename_to_utf8(_prefs_filename)), not_saved);
150             gchar *msg = g_strdup_printf(_("Failed to create the preferences file %s."),
151                 Glib::filename_to_utf8(_prefs_filename).data());
152             _errorDialog(msg, not_saved);
153             g_free(msg);
154             return;
155         }
156         
157         // The prefs file was just created.
158         // We can return now and skip the rest of the load process.
159         _writable = true;
160         return;
161     }
162     
163     // Yes, the pref file exists.
164     // 2. Is it a regular file?
165     if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) {
166         //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a regular file."),
167         //    Glib::filename_to_utf8(_prefs_filename)), not_saved);
168         gchar *msg = g_strdup_printf(_("The preferences file %s is not a regular file."),
169             Glib::filename_to_utf8(_prefs_filename).data());
170         _errorDialog(msg, not_saved);
171         g_free(msg);
172         return;
173     }
174     
175     // 3. Is the file readable?
176     gchar *prefs_xml = NULL; gsize len = 0;
177     if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) {
178         //_errorDialog(Glib::ustring::compose(_("The preferences file %1 could not be read."),
179         //    Glib::filename_to_utf8(_prefs_filename)), not_saved);
180         gchar *msg = g_strdup_printf(_("The preferences file %s could not be read."),
181             Glib::filename_to_utf8(_prefs_filename).data());
182         _errorDialog(msg, not_saved);
183         g_free(msg);
184         return;
185     }
186     // 4. Is it valid XML?
187     Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL);
188     g_free(prefs_xml);
189     if (!prefs_read) {
190         //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."),
191         //    Glib::filename_to_utf8(_prefs_filename)), not_saved);
192         gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."),
193             Glib::filename_to_utf8(_prefs_filename).data());
194         _errorDialog(msg, not_saved);
195         g_free(msg);
196         return;
197     }
198     // 5. Basic sanity check: does the root element have a correct name?
199     if (strcmp(prefs_read->root()->name(), "inkscape")) {
200         //_errorDialog(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."),
201         //    Glib::filename_to_utf8(_prefs_filename)), not_saved);
202         gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."),
203             Glib::filename_to_utf8(_prefs_filename).data());
204         _errorDialog(msg, not_saved);
205         g_free(msg);
206         Inkscape::GC::release(prefs_read);
207         return;
208     }
209     
210     // Merge the loaded prefs with defaults.
211     _prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
212     Inkscape::GC::release(prefs_read);
213     _writable = true;
216 /**
217  * @brief Flush all pref changes to the XML file
218  */
219 void Preferences::save()
221     if (!_writable) return; // no-op if the prefs file is not writable
222     
223     // sp_repr_save_file uses utf-8 instead of the glib filename encoding.
224     // I don't know why filenames are kept in utf-8 in Inkscape and then
225     // converted to filename encoding when necessary through special functions
226     // - wouldn't it be easier to keep things in the encoding they are supposed
227     // to be in?
228     Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename);
229     if (utf8name.empty()) return;
230     sp_repr_save_file(_prefs_doc, utf8name.data());
234 // Now for the meat.
236 /**
237  * @brief Get names of all entries in the specified path
238  * @param path Preference path to query
239  * @return A vector containing all entries in the given directory
240  */
241 std::vector<Preferences::Entry> Preferences::getAllEntries(Glib::ustring const &path)
243     std::vector<Entry> temp;
244     Inkscape::XML::Node *node = _getNode(path, false);
245     if (!node) return temp;
246     
247     // argh - purge this Util::List nonsense from XML classes fast
248     Inkscape::Util::List<Inkscape::XML::AttributeRecord const> alist = node->attributeList();
249     for (; alist; ++alist)
250         temp.push_back( Entry(path + '/' + g_quark_to_string(alist->key), static_cast<void const*>(alist->value.pointer())) );
251     return temp;
254 /**
255  * @brief Get the paths to all subdirectories of the specified path
256  * @param path Preference path to query
257  * @return A vector containing absolute paths to all subdirectories in the given path
258  */
259 std::vector<Glib::ustring> Preferences::getAllDirs(Glib::ustring const &path)
261     std::vector<Glib::ustring> temp;
262     Inkscape::XML::Node *node = _getNode(path, false);
263     if (!node) return temp;
264     
265     for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
266         temp.push_back(path + '/' + i->attribute("id"));
267     }
268     return temp;
271 // getter methods
273 Preferences::Entry const Preferences::getEntry(Glib::ustring const &pref_path)
275     gchar const *v;
276     _getRawValue(pref_path, v);
277     return Entry(pref_path, v);
280 // setter methods
282 /**
283  * @brief Set a boolean attribute of a preference
284  * @param pref_path Path of the preference to modify
285  * @param value The new value of the pref attribute
286  */
287 void Preferences::setBool(Glib::ustring const &pref_path, bool value)
289     /// @todo Boolean values should be stored as "true" and "false",
290     /// but this is not possible due to an interaction with event contexts.
291     /// Investigate this in depth.
292     _setRawValue(pref_path, ( value ? "1" : "0" ));
295 /**
296  * @brief Set an integer attribute of a preference
297  * @param pref_path Path of the preference to modify
298  * @param value The new value of the pref attribute
299  */
300 void Preferences::setInt(Glib::ustring const &pref_path, int value)
302     gchar intstr[32];
303     g_snprintf(intstr, 32, "%d", value);
304     _setRawValue(pref_path, intstr);
307 /**
308  * @brief Set a floating point attribute of a preference
309  * @param pref_path Path of the preference to modify
310  * @param value The new value of the pref attribute
311  */
312 void Preferences::setDouble(Glib::ustring const &pref_path, double value)
314     gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
315     g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, value);
316     _setRawValue(pref_path, buf);
319 /**
320  * @brief Set a string attribute of a preference
321  * @param pref_path Path of the preference to modify
322  * @param value The new value of the pref attribute
323  */
324 void Preferences::setString(Glib::ustring const &pref_path, Glib::ustring const &value)
326     _setRawValue(pref_path, value.data());
329 void Preferences::setStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
331     gchar *css_str = sp_repr_css_write_string(style);
332     _setRawValue(pref_path, css_str);
333     g_free(css_str);
336 void Preferences::mergeStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
338     SPCSSAttr *current = getStyle(pref_path);
339     sp_repr_css_merge(current, style);
340     gchar *css_str = sp_repr_css_write_string(current);
341     _setRawValue(pref_path, css_str);
342     g_free(css_str);
343     sp_repr_css_attr_unref(current);
347 // Observer stuff
348 namespace {
350 /**
351  * @brief Structure that holds additional information for registered Observers
352  */
353 struct _ObserverData {
354     Inkscape::XML::Node *_node; ///< Node at which the wrapping PrefNodeObserver is registered
355     bool _is_attr; ///< Whether this Observer watches a single attribute
356 };
358 } // anonymous namespace
360 Preferences::Observer::Observer(Glib::ustring const &path) :
361     observed_path(path)
365 Preferences::Observer::~Observer()
367     // on destruction remove observer to prevent invalid references
368     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
369     prefs->removeObserver(*this);
372 void Preferences::PrefNodeObserver::notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char> new_value)
374     // filter out attributes we don't watch
375     gchar const *attr_name = g_quark_to_string(name);
376     if ( !_filter.empty() && _filter != attr_name ) return;
377     
378     _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
379     Glib::ustring notify_path = _observer.observed_path;
380     
381     if (!d->_is_attr) {
382         std::vector<gchar const *> path_fragments;
383         notify_path.reserve(256); // this will make appending operations faster
384         
385         // walk the XML tree, saving each of the id attributes in a vector
386         // we terminate when we hit the observer's attachment node, because the path to this node
387         // is already stored in notify_path
388         for (XML::NodeParentIterator n = &node; static_cast<XML::Node*>(n) != d->_node; ++n)
389             path_fragments.push_back(n->attribute("id"));
390         // assemble the elements into a path
391         for (std::vector<gchar const *>::reverse_iterator i = path_fragments.rbegin(); i != path_fragments.rend(); ++i) {
392             notify_path.push_back('/');
393             notify_path.append(*i);
394         }
396         // append attribute name
397         notify_path.push_back('/');
398         notify_path.append(attr_name);
399     }
400     
401     Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
402     _observer.notify(val);
405 /**
406  * @brief Find the XML node to observe
407  */
408 XML::Node *Preferences::_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create)
410     // first assume that the last path element is an entry.
411     _keySplit(pref_path, node_key, attr_key);
412     
413     // find the node corresponding to the "directory".
414     Inkscape::XML::Node *node = _getNode(node_key, create), *child;
415     for (child = node->firstChild(); child; child = child->next()) {
416         // If there is a node with id corresponding to the attr key,
417         // this means that the last part of the path is actually a key (folder).
418         // Change values accordingly.
419         if (attr_key == child->attribute("id")) {
420             node = child;
421             attr_key = "";
422             node_key = pref_path;
423             break;
424         }
425     }
426     return node;
429 void Preferences::addObserver(Observer &o)
431     // prevent adding the same observer twice
432     if ( _observer_map.find(&o) != _observer_map.end() ) return;
433     
434     Glib::ustring node_key, attr_key;
435     Inkscape::XML::Node *node;
436     node = _findObserverNode(o.observed_path, node_key, attr_key, false);
437     if (!node) return;
438     
439     // set additional data
440     _ObserverData *d = new _ObserverData;
441     d->_node = node;
442     d->_is_attr = !attr_key.empty();
443     o._data = static_cast<void*>(d);
444     
445     _observer_map[&o] = new PrefNodeObserver(o, attr_key);
446     
447     // if we watch a single pref, we want to receive notifications only for a single node
448     if (d->_is_attr) {
449         node->addObserver( *(_observer_map[&o]) );
450     } else {
451         node->addSubtreeObserver( *(_observer_map[&o]) );
452     }
455 void Preferences::removeObserver(Observer &o)
457     // prevent removing an observer which was not added
458     if ( _observer_map.find(&o) == _observer_map.end() ) return;
459     Inkscape::XML::Node *node = static_cast<_ObserverData*>(o._data)->_node;
460     delete static_cast<_ObserverData*>(o._data);
461     o._data = NULL;
462     
463     node->removeSubtreeObserver( *(_observer_map[&o]) );
464     delete _observer_map[&o];
465     _observer_map.erase(&o);
469 /**
470  * @brief Get the XML node corresponding to the given pref key
471  * @param pref_key Preference key (path) to get
472  * @param create Whether to create the corresponding node if it doesn't exist
473  * @param separator The character used to separate parts of the pref key
474  * @return XML node corresponding to the specified key
475  *
476  * Derived from former inkscape_get_repr(). Private because it assumes that the backend is
477  * a flat XML file, which may not be the case e.g. if we are using GConf (in future).
478  */
479 Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool create)
481     // verify path
482     g_assert( pref_key.at(0) == '/' );
483     g_assert( pref_key.find('.') == Glib::ustring::npos );
485     Inkscape::XML::Node *node = _prefs_doc->root(), *child = NULL;
486     gchar **splits = g_strsplit(pref_key.data(), "/", 0);
487     
488     if ( splits == NULL ) return node;
489     
490     for (int part_i = 0; splits[part_i]; ++part_i) {
491         // skip empty path segments
492         if (!splits[part_i][0]) continue;
493         
494         for (child = node->firstChild(); child; child = child->next())
495             if (!strcmp(splits[part_i], child->attribute("id"))) break;
496         
497         // If the previous loop found a matching key, child now contains the node
498         // matching the processed key part. If no node was found then it is NULL.
499         if (!child) {
500             if (create) {
501                 // create the rest of the key
502                 while(splits[part_i]) {
503                     child = node->document()->createElement("group");
504                     child->setAttribute("id", splits[part_i]);
505                     node->appendChild(child);
506                     
507                     ++part_i;
508                     node = child;
509                 }
510                 g_strfreev(splits);
511                 return node;
512             } else {
513                 return NULL;
514             }
515         }
517         node = child;
518     }
519     g_strfreev(splits);
520     return node;
523 void Preferences::_getRawValue(Glib::ustring const &path, gchar const *&result)
525     // create node and attribute keys
526     Glib::ustring node_key, attr_key;
527     _keySplit(path, node_key, attr_key);
528     
529     // retrieve the attribute
530     Inkscape::XML::Node *node = _getNode(node_key, false);
531     if ( node == NULL ) {
532         result = NULL;
533     } else {
534         gchar const *attr = node->attribute(attr_key.data());
535         if ( attr == NULL ) {
536             result = NULL;
537         } else {
538             result = attr;
539         }
540     }
543 void Preferences::_setRawValue(Glib::ustring const &path, gchar const *value)
545     // create node and attribute keys
546     Glib::ustring node_key, attr_key;
547     _keySplit(path, node_key, attr_key);
548     
549     // set the attribute
550     Inkscape::XML::Node *node = _getNode(node_key, true);
551     node->setAttribute(attr_key.data(), value);
554 // The _extract* methods are where the actual wrok is done - they define how preferences are stored
555 // in the XML file.
557 bool Preferences::_extractBool(Entry const &v)
559     gchar const *s = static_cast<gchar const *>(v._value);
560     if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) return false;
561     return true;
564 int Preferences::_extractInt(Entry const &v)
566     gchar const *s = static_cast<gchar const *>(v._value);
567     if ( !strcmp(s, "true") ) return true;
568     if ( !strcmp(s, "false") ) return false;
569     return atoi(s);
572 double Preferences::_extractDouble(Entry const &v)
574     gchar const *s = static_cast<gchar const *>(v._value);
575     return g_ascii_strtod(s, NULL);
578 Glib::ustring Preferences::_extractString(Entry const &v)
580     return Glib::ustring(static_cast<gchar const *>(v._value));
583 SPCSSAttr *Preferences::_extractStyle(Entry const &v)
585     SPCSSAttr *style = sp_repr_css_attr_new();
586     sp_repr_css_attr_add_from_string(style, static_cast<gchar const*>(v._value));
587     return style;
590 SPCSSAttr *Preferences::_extractInheritedStyle(Entry const &v)
592     // This is the dirtiest extraction method. Generally we ignore whatever was in v._value
593     // and just get the style using sp_repr_css_attr_inherited. To implement this in GConf,
594     // we'll have to walk up the tree and call sp_repr_css_attr_add_from_string
595     Glib::ustring node_key, attr_key;
596     _keySplit(v._pref_path, node_key, attr_key);
597     
598     Inkscape::XML::Node *node = _getNode(node_key, false);
599     return sp_repr_css_attr_inherited(node, attr_key.data());
602 // XML backend helper: Split the path into a node key and an attribute key.
603 void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key)
605     // everything after the last slash
606     attr_key = pref_path.substr(pref_path.rfind('/') + 1, Glib::ustring::npos);
607     // everything before the last slash
608     node_key = pref_path.substr(0, pref_path.rfind('/'));
611 void Preferences::_errorDialog(Glib::ustring const &msg, Glib::ustring const &secondary)
613     if (Preferences::use_gui) {
614         Gtk::MessageDialog err(
615             msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
616         err.set_secondary_text(secondary);
617         err.run();
618     } else {
619         g_message("%s", msg.data());
620         g_message("%s", secondary.data());
621     }
624 Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &path, void const *ptr)
626     return Entry(path, ptr);
629 bool Preferences::use_gui = true;
630 Preferences *Preferences::_instance = NULL;
633 } // namespace Inkscape
634  
635 /*
636   Local Variables:
637   mode:c++
638   c-file-style:"stroustrup"
639   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
640   indent-tabs-mode:nil
641   fill-column:99
642   End:
643 */
644 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :