1 /** @file
2 * @brief Singleton class to access the preferences file - implementation
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
6 * Jon A. Cruz <jon@joncruz.org>
7 *
8 * Copyright (C) 2008,2009 Authors
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information.
11 */
13 #include <cstring>
14 #include <glibmm/fileutils.h>
15 #include <glibmm/i18n.h>
16 #include <glib.h>
17 #include <glib/gstdio.h>
18 #include "preferences.h"
19 #include "preferences-skeleton.h"
20 #include "inkscape.h"
21 #include "xml/node-observer.h"
22 #include "xml/node-iterators.h"
23 #include "xml/attribute-record.h"
25 #define PREFERENCES_FILE_NAME "preferences.xml"
27 namespace Inkscape {
29 // private inner class definition
31 /**
32 * @brief XML - prefs observer bridge
33 *
34 * This is an XML node observer that watches for changes in the XML document storing the preferences.
35 * It is used to implement preference observers.
36 */
37 class Preferences::PrefNodeObserver : public XML::NodeObserver {
38 public:
39 PrefNodeObserver(Observer &o, Glib::ustring const &filter) :
40 _observer(o),
41 _filter(filter)
42 {}
43 virtual ~PrefNodeObserver() {}
44 virtual void notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char>);
45 private:
46 Observer &_observer;
47 Glib::ustring const _filter;
48 };
51 Preferences::Preferences() :
52 _prefs_basename(PREFERENCES_FILE_NAME),
53 _prefs_dir(""),
54 _prefs_filename(""),
55 _prefs_doc(0),
56 _errorHandler(0),
57 _writable(false),
58 _hasError(false)
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);
65 path = profile_path(_prefs_basename.data());
66 _prefs_filename = path;
67 g_free(path);
69 _loadDefaults();
70 _load();
71 }
73 Preferences::~Preferences()
74 {
75 // delete all PrefNodeObservers
76 for (_ObsMap::iterator i = _observer_map.begin(); i != _observer_map.end(); ) {
77 delete (*i++).second; // avoids reference to a deleted key
78 }
79 // unref XML document
80 Inkscape::GC::release(_prefs_doc);
81 }
83 /**
84 * @brief Load internal defaults
85 *
86 * In the future this will try to load the system-wide file before falling
87 * back to the internal defaults.
88 */
89 void Preferences::_loadDefaults()
90 {
91 _prefs_doc = sp_repr_read_mem(preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL);
92 }
94 /**
95 * @brief Load the user's customized preferences
96 *
97 * Tries to load the user's preferences.xml file. If there is none, creates it.
98 */
99 void Preferences::_load()
100 {
101 Glib::ustring const not_saved = _("Inkscape will run with default settings, "
102 "and new settings will not be saved. ");
104 // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
106 // 1. Does the file exist?
107 if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_EXISTS)) {
108 // No - we need to create one.
109 // Does the profile directory exist?
110 if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_EXISTS)) {
111 // No - create the profile directory
112 if (g_mkdir(_prefs_dir.data(), 0755)) {
113 // the creation failed
114 //_reportError(Glib::ustring::compose(_("Cannot create profile directory %1."),
115 // Glib::filename_to_utf8(_prefs_dir)), not_saved);
116 gchar *msg = g_strdup_printf(_("Cannot create profile directory %s."),
117 Glib::filename_to_utf8(_prefs_dir).data());
118 _reportError(msg, not_saved);
119 g_free(msg);
120 return;
121 }
122 // create some subdirectories for user stuff
123 char const *user_dirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
124 for (int i=0; user_dirs[i]; ++i) {
125 char *dir = profile_path(user_dirs[i]);
126 g_mkdir(dir, 0755);
127 g_free(dir);
128 }
130 } else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) {
131 // The profile dir is not actually a directory
132 //_reportError(Glib::ustring::compose(_("%1 is not a valid directory."),
133 // Glib::filename_to_utf8(_prefs_dir)), not_saved);
134 gchar *msg = g_strdup_printf(_("%s is not a valid directory."),
135 Glib::filename_to_utf8(_prefs_dir).data());
136 _reportError(msg, not_saved);
137 g_free(msg);
138 return;
139 }
140 // The profile dir exists and is valid.
141 if (!g_file_set_contents(_prefs_filename.data(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) {
142 // The write failed.
143 //_reportError(Glib::ustring::compose(_("Failed to create the preferences file %1."),
144 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
145 gchar *msg = g_strdup_printf(_("Failed to create the preferences file %s."),
146 Glib::filename_to_utf8(_prefs_filename).data());
147 _reportError(msg, not_saved);
148 g_free(msg);
149 return;
150 }
152 // The prefs file was just created.
153 // We can return now and skip the rest of the load process.
154 _writable = true;
155 return;
156 }
158 // Yes, the pref file exists.
159 // 2. Is it a regular file?
160 if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) {
161 //_reportError(Glib::ustring::compose(_("The preferences file %1 is not a regular file."),
162 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
163 gchar *msg = g_strdup_printf(_("The preferences file %s is not a regular file."),
164 Glib::filename_to_utf8(_prefs_filename).data());
165 _reportError(msg, not_saved);
166 g_free(msg);
167 return;
168 }
170 // 3. Is the file readable?
171 gchar *prefs_xml = NULL; gsize len = 0;
172 if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) {
173 //_reportError(Glib::ustring::compose(_("The preferences file %1 could not be read."),
174 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
175 gchar *msg = g_strdup_printf(_("The preferences file %s could not be read."),
176 Glib::filename_to_utf8(_prefs_filename).data());
177 _reportError(msg, not_saved);
178 g_free(msg);
179 return;
180 }
182 // 4. Is it valid XML?
183 Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL);
184 g_free(prefs_xml);
185 if (!prefs_read) {
186 //_reportError(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."),
187 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
188 gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."),
189 Glib::filename_to_utf8(_prefs_filename).data());
190 _reportError(msg, not_saved);
191 g_free(msg);
192 return;
193 }
195 // 5. Basic sanity check: does the root element have a correct name?
196 if (strcmp(prefs_read->root()->name(), "inkscape")) {
197 //_reportError(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."),
198 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
199 gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."),
200 Glib::filename_to_utf8(_prefs_filename).data());
201 _reportError(msg, not_saved);
202 g_free(msg);
203 Inkscape::GC::release(prefs_read);
204 return;
205 }
207 // Merge the loaded prefs with defaults.
208 _prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
209 Inkscape::GC::release(prefs_read);
210 _writable = true;
211 }
213 /**
214 * @brief Flush all pref changes to the XML file
215 */
216 void Preferences::save()
217 {
218 // no-op if the prefs file is not writable
219 if (_writable) {
220 // sp_repr_save_file uses utf-8 instead of the glib filename encoding.
221 // I don't know why filenames are kept in utf-8 in Inkscape and then
222 // converted to filename encoding when necessary through special functions
223 // - wouldn't it be easier to keep things in the encoding they are supposed
224 // to be in?
226 // No, it would not. There are many reasons, one key reason being that the
227 // rest of GTK+ is explicitly UTF-8. From an engineering standpoint, keeping
228 // the filesystem encoding would change things from a one-to-many problem to
229 // instead be a many-to-many problem. Also filesystem encoding can change
230 // from one run of the program to the next, so can not be stored.
231 // There are many other factors, so ask if you would like to learn them. - JAC
232 Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename);
233 if (!utf8name.empty()) {
234 sp_repr_save_file(_prefs_doc, utf8name.data());
235 }
236 }
237 }
239 bool Preferences::getLastError( Glib::ustring& primary, Glib::ustring& secondary )
240 {
241 bool result = _hasError;
242 if ( _hasError ) {
243 primary = _lastErrPrimary;
244 secondary = _lastErrSecondary;
245 _hasError = false;
246 _lastErrPrimary.clear();
247 _lastErrSecondary.clear();
248 } else {
249 primary.clear();
250 secondary.clear();
251 }
252 return result;
253 }
255 // Now for the meat.
257 /**
258 * @brief Get names of all entries in the specified path
259 * @param path Preference path to query
260 * @return A vector containing all entries in the given directory
261 */
262 std::vector<Preferences::Entry> Preferences::getAllEntries(Glib::ustring const &path)
263 {
264 std::vector<Entry> temp;
265 Inkscape::XML::Node *node = _getNode(path, false);
266 if (node) {
267 // argh - purge this Util::List nonsense from XML classes fast
268 Inkscape::Util::List<Inkscape::XML::AttributeRecord const> alist = node->attributeList();
269 for (; alist; ++alist) {
270 temp.push_back( Entry(path + '/' + g_quark_to_string(alist->key), static_cast<void const*>(alist->value.pointer())) );
271 }
272 }
273 return temp;
274 }
276 /**
277 * @brief Get the paths to all subdirectories of the specified path
278 * @param path Preference path to query
279 * @return A vector containing absolute paths to all subdirectories in the given path
280 */
281 std::vector<Glib::ustring> Preferences::getAllDirs(Glib::ustring const &path)
282 {
283 std::vector<Glib::ustring> temp;
284 Inkscape::XML::Node *node = _getNode(path, false);
285 if (node) {
286 for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
287 temp.push_back(path + '/' + i->attribute("id"));
288 }
289 }
290 return temp;
291 }
293 // getter methods
295 Preferences::Entry const Preferences::getEntry(Glib::ustring const &pref_path)
296 {
297 gchar const *v;
298 _getRawValue(pref_path, v);
299 return Entry(pref_path, v);
300 }
302 // setter methods
304 /**
305 * @brief Set a boolean attribute of a preference
306 * @param pref_path Path of the preference to modify
307 * @param value The new value of the pref attribute
308 */
309 void Preferences::setBool(Glib::ustring const &pref_path, bool value)
310 {
311 /// @todo Boolean values should be stored as "true" and "false",
312 /// but this is not possible due to an interaction with event contexts.
313 /// Investigate this in depth.
314 _setRawValue(pref_path, ( value ? "1" : "0" ));
315 }
317 /**
318 * @brief Set an integer attribute of a preference
319 * @param pref_path Path of the preference to modify
320 * @param value The new value of the pref attribute
321 */
322 void Preferences::setInt(Glib::ustring const &pref_path, int value)
323 {
324 gchar intstr[32];
325 g_snprintf(intstr, 32, "%d", value);
326 _setRawValue(pref_path, intstr);
327 }
329 /**
330 * @brief Set a floating point attribute of a preference
331 * @param pref_path Path of the preference to modify
332 * @param value The new value of the pref attribute
333 */
334 void Preferences::setDouble(Glib::ustring const &pref_path, double value)
335 {
336 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
337 g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, value);
338 _setRawValue(pref_path, buf);
339 }
341 /**
342 * @brief Set a string attribute of a preference
343 * @param pref_path Path of the preference to modify
344 * @param value The new value of the pref attribute
345 */
346 void Preferences::setString(Glib::ustring const &pref_path, Glib::ustring const &value)
347 {
348 _setRawValue(pref_path, value.data());
349 }
351 void Preferences::setStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
352 {
353 gchar *css_str = sp_repr_css_write_string(style);
354 _setRawValue(pref_path, css_str);
355 g_free(css_str);
356 }
358 void Preferences::mergeStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
359 {
360 SPCSSAttr *current = getStyle(pref_path);
361 sp_repr_css_merge(current, style);
362 gchar *css_str = sp_repr_css_write_string(current);
363 _setRawValue(pref_path, css_str);
364 g_free(css_str);
365 sp_repr_css_attr_unref(current);
366 }
369 // Observer stuff
370 namespace {
372 /**
373 * @brief Structure that holds additional information for registered Observers
374 */
375 struct _ObserverData {
376 Inkscape::XML::Node *_node; ///< Node at which the wrapping PrefNodeObserver is registered
377 bool _is_attr; ///< Whether this Observer watches a single attribute
378 };
380 } // anonymous namespace
382 Preferences::Observer::Observer(Glib::ustring const &path) :
383 observed_path(path)
384 {
385 }
387 Preferences::Observer::~Observer()
388 {
389 // on destruction remove observer to prevent invalid references
390 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
391 prefs->removeObserver(*this);
392 }
394 void Preferences::PrefNodeObserver::notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char> new_value)
395 {
396 // filter out attributes we don't watch
397 gchar const *attr_name = g_quark_to_string(name);
398 if ( _filter.empty() || (_filter == attr_name) ) {
399 _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
400 Glib::ustring notify_path = _observer.observed_path;
402 if (!d->_is_attr) {
403 std::vector<gchar const *> path_fragments;
404 notify_path.reserve(256); // this will make appending operations faster
406 // walk the XML tree, saving each of the id attributes in a vector
407 // we terminate when we hit the observer's attachment node, because the path to this node
408 // is already stored in notify_path
409 for (XML::NodeParentIterator n = &node; static_cast<XML::Node*>(n) != d->_node; ++n) {
410 path_fragments.push_back(n->attribute("id"));
411 }
412 // assemble the elements into a path
413 for (std::vector<gchar const *>::reverse_iterator i = path_fragments.rbegin(); i != path_fragments.rend(); ++i) {
414 notify_path.push_back('/');
415 notify_path.append(*i);
416 }
418 // append attribute name
419 notify_path.push_back('/');
420 notify_path.append(attr_name);
421 }
423 Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
424 _observer.notify(val);
425 }
426 }
428 /**
429 * @brief Find the XML node to observe
430 */
431 XML::Node *Preferences::_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create)
432 {
433 // first assume that the last path element is an entry.
434 _keySplit(pref_path, node_key, attr_key);
436 // find the node corresponding to the "directory".
437 Inkscape::XML::Node *node = _getNode(node_key, create), *child;
438 for (child = node->firstChild(); child; child = child->next()) {
439 // If there is a node with id corresponding to the attr key,
440 // this means that the last part of the path is actually a key (folder).
441 // Change values accordingly.
442 if (attr_key == child->attribute("id")) {
443 node = child;
444 attr_key = "";
445 node_key = pref_path;
446 break;
447 }
448 }
449 return node;
450 }
452 void Preferences::addObserver(Observer &o)
453 {
454 // prevent adding the same observer twice
455 if ( _observer_map.find(&o) == _observer_map.end() ) {
456 Glib::ustring node_key, attr_key;
457 Inkscape::XML::Node *node;
458 node = _findObserverNode(o.observed_path, node_key, attr_key, false);
459 if (node) {
460 // set additional data
461 _ObserverData *priv_data = new _ObserverData;
462 priv_data->_node = node;
463 priv_data->_is_attr = !attr_key.empty();
464 o._data = static_cast<void*>(priv_data);
466 _observer_map[&o] = new PrefNodeObserver(o, attr_key);
468 // if we watch a single pref, we want to receive notifications only for a single node
469 if (priv_data->_is_attr) {
470 node->addObserver( *(_observer_map[&o]) );
471 } else {
472 node->addSubtreeObserver( *(_observer_map[&o]) );
473 }
474 }
475 }
476 }
478 void Preferences::removeObserver(Observer &o)
479 {
480 // prevent removing an observer which was not added
481 if ( _observer_map.find(&o) != _observer_map.end() ) {
482 Inkscape::XML::Node *node = static_cast<_ObserverData*>(o._data)->_node;
483 _ObserverData *priv_data = static_cast<_ObserverData*>(o._data);
484 o._data = NULL;
486 if (priv_data->_is_attr) {
487 node->removeObserver( *(_observer_map[&o]) );
488 } else {
489 node->removeSubtreeObserver( *(_observer_map[&o]) );
490 }
492 delete priv_data;
493 delete _observer_map[&o];
494 _observer_map.erase(&o);
495 }
496 }
499 /**
500 * @brief Get the XML node corresponding to the given pref key
501 * @param pref_key Preference key (path) to get
502 * @param create Whether to create the corresponding node if it doesn't exist
503 * @param separator The character used to separate parts of the pref key
504 * @return XML node corresponding to the specified key
505 *
506 * Derived from former inkscape_get_repr(). Private because it assumes that the backend is
507 * a flat XML file, which may not be the case e.g. if we are using GConf (in future).
508 */
509 Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool create)
510 {
511 // verify path
512 g_assert( pref_key.at(0) == '/' );
513 // No longer necessary, can cause problems with input devices which have a dot in the name
514 // g_assert( pref_key.find('.') == Glib::ustring::npos );
516 Inkscape::XML::Node *node = _prefs_doc->root();
517 Inkscape::XML::Node *child = NULL;
518 gchar **splits = g_strsplit(pref_key.data(), "/", 0);
520 if ( splits ) {
521 for (int part_i = 0; splits[part_i]; ++part_i) {
522 // skip empty path segments
523 if (!splits[part_i][0]) {
524 continue;
525 }
527 for (child = node->firstChild(); child; child = child->next()) {
528 if (!strcmp(splits[part_i], child->attribute("id"))) {
529 break;
530 }
531 }
533 // If the previous loop found a matching key, child now contains the node
534 // matching the processed key part. If no node was found then it is NULL.
535 if (!child) {
536 if (create) {
537 // create the rest of the key
538 while(splits[part_i]) {
539 child = node->document()->createElement("group");
540 child->setAttribute("id", splits[part_i]);
541 node->appendChild(child);
543 ++part_i;
544 node = child;
545 }
546 g_strfreev(splits);
547 return node;
548 } else {
549 return NULL;
550 }
551 }
553 node = child;
554 }
555 g_strfreev(splits);
556 }
557 return node;
558 }
560 void Preferences::_getRawValue(Glib::ustring const &path, gchar const *&result)
561 {
562 // create node and attribute keys
563 Glib::ustring node_key, attr_key;
564 _keySplit(path, node_key, attr_key);
566 // retrieve the attribute
567 Inkscape::XML::Node *node = _getNode(node_key, false);
568 if ( node == NULL ) {
569 result = NULL;
570 } else {
571 gchar const *attr = node->attribute(attr_key.data());
572 if ( attr == NULL ) {
573 result = NULL;
574 } else {
575 result = attr;
576 }
577 }
578 }
580 void Preferences::_setRawValue(Glib::ustring const &path, gchar const *value)
581 {
582 // create node and attribute keys
583 Glib::ustring node_key, attr_key;
584 _keySplit(path, node_key, attr_key);
586 // set the attribute
587 Inkscape::XML::Node *node = _getNode(node_key, true);
588 node->setAttribute(attr_key.data(), value);
589 }
591 // The _extract* methods are where the actual wrok is done - they define how preferences are stored
592 // in the XML file.
594 bool Preferences::_extractBool(Entry const &v)
595 {
596 gchar const *s = static_cast<gchar const *>(v._value);
597 if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) {
598 return false;
599 } else {
600 return true;
601 }
602 }
604 int Preferences::_extractInt(Entry const &v)
605 {
606 gchar const *s = static_cast<gchar const *>(v._value);
607 if ( !strcmp(s, "true") ) {
608 return true;
609 } else if ( !strcmp(s, "false") ) {
610 return false;
611 } else {
612 return atoi(s);
613 }
614 }
616 double Preferences::_extractDouble(Entry const &v)
617 {
618 gchar const *s = static_cast<gchar const *>(v._value);
619 return g_ascii_strtod(s, NULL);
620 }
622 Glib::ustring Preferences::_extractString(Entry const &v)
623 {
624 return Glib::ustring(static_cast<gchar const *>(v._value));
625 }
627 SPCSSAttr *Preferences::_extractStyle(Entry const &v)
628 {
629 SPCSSAttr *style = sp_repr_css_attr_new();
630 sp_repr_css_attr_add_from_string(style, static_cast<gchar const*>(v._value));
631 return style;
632 }
634 SPCSSAttr *Preferences::_extractInheritedStyle(Entry const &v)
635 {
636 // This is the dirtiest extraction method. Generally we ignore whatever was in v._value
637 // and just get the style using sp_repr_css_attr_inherited. To implement this in GConf,
638 // we'll have to walk up the tree and call sp_repr_css_attr_add_from_string
639 Glib::ustring node_key, attr_key;
640 _keySplit(v._pref_path, node_key, attr_key);
642 Inkscape::XML::Node *node = _getNode(node_key, false);
643 return sp_repr_css_attr_inherited(node, attr_key.data());
644 }
646 // XML backend helper: Split the path into a node key and an attribute key.
647 void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key)
648 {
649 // everything after the last slash
650 attr_key = pref_path.substr(pref_path.rfind('/') + 1, Glib::ustring::npos);
651 // everything before the last slash
652 node_key = pref_path.substr(0, pref_path.rfind('/'));
653 }
655 void Preferences::_reportError(Glib::ustring const &msg, Glib::ustring const &secondary)
656 {
657 _hasError = true;
658 _lastErrPrimary = msg;
659 _lastErrSecondary = secondary;
660 if (_errorHandler) {
661 _errorHandler->handleError(msg, secondary);
662 }
663 }
665 Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &path, void const *ptr)
666 {
667 return Entry(path, ptr);
668 }
670 void Preferences::setErrorHandler(ErrorReporter* handler)
671 {
672 _errorHandler = handler;
673 }
675 void Preferences::unload(bool save)
676 {
677 if (_instance)
678 {
679 if (save) {
680 _instance->save();
681 }
682 delete _instance;
683 _instance = NULL;
684 }
685 }
687 Preferences *Preferences::_instance = NULL;
690 } // namespace Inkscape
692 /*
693 Local Variables:
694 mode:c++
695 c-file-style:"stroustrup"
696 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
697 indent-tabs-mode:nil
698 fill-column:99
699 End:
700 */
701 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :