071dbf91e2ec69119572c586027b065696c1feb5
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 }
181 // 4. Is it valid XML?
182 Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL);
183 g_free(prefs_xml);
184 if (!prefs_read) {
185 //_reportError(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."),
186 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
187 gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."),
188 Glib::filename_to_utf8(_prefs_filename).data());
189 _reportError(msg, not_saved);
190 g_free(msg);
191 return;
192 }
193 // 5. Basic sanity check: does the root element have a correct name?
194 if (strcmp(prefs_read->root()->name(), "inkscape")) {
195 //_reportError(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."),
196 // Glib::filename_to_utf8(_prefs_filename)), not_saved);
197 gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."),
198 Glib::filename_to_utf8(_prefs_filename).data());
199 _reportError(msg, not_saved);
200 g_free(msg);
201 Inkscape::GC::release(prefs_read);
202 return;
203 }
205 // Merge the loaded prefs with defaults.
206 _prefs_doc->root()->mergeFrom(prefs_read->root(), "id");
207 Inkscape::GC::release(prefs_read);
208 _writable = true;
209 }
211 /**
212 * @brief Flush all pref changes to the XML file
213 */
214 void Preferences::save()
215 {
216 if (!_writable) return; // no-op if the prefs file is not writable
218 // sp_repr_save_file uses utf-8 instead of the glib filename encoding.
219 // I don't know why filenames are kept in utf-8 in Inkscape and then
220 // converted to filename encoding when necessary through special functions
221 // - wouldn't it be easier to keep things in the encoding they are supposed
222 // to be in?
223 Glib::ustring utf8name = Glib::filename_to_utf8(_prefs_filename);
224 if (utf8name.empty()) return;
225 sp_repr_save_file(_prefs_doc, utf8name.data());
226 }
228 bool Preferences::getLastError( Glib::ustring& primary, Glib::ustring& secondary )
229 {
230 bool result = _hasError;
231 if ( _hasError ) {
232 primary = _lastErrPrimary;
233 secondary = _lastErrSecondary;
234 _hasError = false;
235 _lastErrPrimary.clear();
236 _lastErrSecondary.clear();
237 } else {
238 primary.clear();
239 secondary.clear();
240 }
241 return result;
242 }
244 // Now for the meat.
246 /**
247 * @brief Get names of all entries in the specified path
248 * @param path Preference path to query
249 * @return A vector containing all entries in the given directory
250 */
251 std::vector<Preferences::Entry> Preferences::getAllEntries(Glib::ustring const &path)
252 {
253 std::vector<Entry> temp;
254 Inkscape::XML::Node *node = _getNode(path, false);
255 if (!node) return temp;
257 // argh - purge this Util::List nonsense from XML classes fast
258 Inkscape::Util::List<Inkscape::XML::AttributeRecord const> alist = node->attributeList();
259 for (; alist; ++alist)
260 temp.push_back( Entry(path + '/' + g_quark_to_string(alist->key), static_cast<void const*>(alist->value.pointer())) );
261 return temp;
262 }
264 /**
265 * @brief Get the paths to all subdirectories of the specified path
266 * @param path Preference path to query
267 * @return A vector containing absolute paths to all subdirectories in the given path
268 */
269 std::vector<Glib::ustring> Preferences::getAllDirs(Glib::ustring const &path)
270 {
271 std::vector<Glib::ustring> temp;
272 Inkscape::XML::Node *node = _getNode(path, false);
273 if (!node) return temp;
275 for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
276 temp.push_back(path + '/' + i->attribute("id"));
277 }
278 return temp;
279 }
281 // getter methods
283 Preferences::Entry const Preferences::getEntry(Glib::ustring const &pref_path)
284 {
285 gchar const *v;
286 _getRawValue(pref_path, v);
287 return Entry(pref_path, v);
288 }
290 // setter methods
292 /**
293 * @brief Set a boolean attribute of a preference
294 * @param pref_path Path of the preference to modify
295 * @param value The new value of the pref attribute
296 */
297 void Preferences::setBool(Glib::ustring const &pref_path, bool value)
298 {
299 /// @todo Boolean values should be stored as "true" and "false",
300 /// but this is not possible due to an interaction with event contexts.
301 /// Investigate this in depth.
302 _setRawValue(pref_path, ( value ? "1" : "0" ));
303 }
305 /**
306 * @brief Set an integer attribute of a preference
307 * @param pref_path Path of the preference to modify
308 * @param value The new value of the pref attribute
309 */
310 void Preferences::setInt(Glib::ustring const &pref_path, int value)
311 {
312 gchar intstr[32];
313 g_snprintf(intstr, 32, "%d", value);
314 _setRawValue(pref_path, intstr);
315 }
317 /**
318 * @brief Set a floating point 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::setDouble(Glib::ustring const &pref_path, double value)
323 {
324 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
325 g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, value);
326 _setRawValue(pref_path, buf);
327 }
329 /**
330 * @brief Set a string 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::setString(Glib::ustring const &pref_path, Glib::ustring const &value)
335 {
336 _setRawValue(pref_path, value.data());
337 }
339 void Preferences::setStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
340 {
341 gchar *css_str = sp_repr_css_write_string(style);
342 _setRawValue(pref_path, css_str);
343 g_free(css_str);
344 }
346 void Preferences::mergeStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
347 {
348 SPCSSAttr *current = getStyle(pref_path);
349 sp_repr_css_merge(current, style);
350 gchar *css_str = sp_repr_css_write_string(current);
351 _setRawValue(pref_path, css_str);
352 g_free(css_str);
353 sp_repr_css_attr_unref(current);
354 }
357 // Observer stuff
358 namespace {
360 /**
361 * @brief Structure that holds additional information for registered Observers
362 */
363 struct _ObserverData {
364 Inkscape::XML::Node *_node; ///< Node at which the wrapping PrefNodeObserver is registered
365 bool _is_attr; ///< Whether this Observer watches a single attribute
366 };
368 } // anonymous namespace
370 Preferences::Observer::Observer(Glib::ustring const &path) :
371 observed_path(path)
372 {
373 }
375 Preferences::Observer::~Observer()
376 {
377 // on destruction remove observer to prevent invalid references
378 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
379 prefs->removeObserver(*this);
380 }
382 void Preferences::PrefNodeObserver::notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char> new_value)
383 {
384 // filter out attributes we don't watch
385 gchar const *attr_name = g_quark_to_string(name);
386 if ( !_filter.empty() && _filter != attr_name ) return;
388 _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
389 Glib::ustring notify_path = _observer.observed_path;
391 if (!d->_is_attr) {
392 std::vector<gchar const *> path_fragments;
393 notify_path.reserve(256); // this will make appending operations faster
395 // walk the XML tree, saving each of the id attributes in a vector
396 // we terminate when we hit the observer's attachment node, because the path to this node
397 // is already stored in notify_path
398 for (XML::NodeParentIterator n = &node; static_cast<XML::Node*>(n) != d->_node; ++n)
399 path_fragments.push_back(n->attribute("id"));
400 // assemble the elements into a path
401 for (std::vector<gchar const *>::reverse_iterator i = path_fragments.rbegin(); i != path_fragments.rend(); ++i) {
402 notify_path.push_back('/');
403 notify_path.append(*i);
404 }
406 // append attribute name
407 notify_path.push_back('/');
408 notify_path.append(attr_name);
409 }
411 Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
412 _observer.notify(val);
413 }
415 /**
416 * @brief Find the XML node to observe
417 */
418 XML::Node *Preferences::_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create)
419 {
420 // first assume that the last path element is an entry.
421 _keySplit(pref_path, node_key, attr_key);
423 // find the node corresponding to the "directory".
424 Inkscape::XML::Node *node = _getNode(node_key, create), *child;
425 for (child = node->firstChild(); child; child = child->next()) {
426 // If there is a node with id corresponding to the attr key,
427 // this means that the last part of the path is actually a key (folder).
428 // Change values accordingly.
429 if (attr_key == child->attribute("id")) {
430 node = child;
431 attr_key = "";
432 node_key = pref_path;
433 break;
434 }
435 }
436 return node;
437 }
439 void Preferences::addObserver(Observer &o)
440 {
441 // prevent adding the same observer twice
442 if ( _observer_map.find(&o) != _observer_map.end() ) return;
444 Glib::ustring node_key, attr_key;
445 Inkscape::XML::Node *node;
446 node = _findObserverNode(o.observed_path, node_key, attr_key, false);
447 if (!node) return;
449 // set additional data
450 _ObserverData *priv_data = new _ObserverData;
451 priv_data->_node = node;
452 priv_data->_is_attr = !attr_key.empty();
453 o._data = static_cast<void*>(priv_data);
455 _observer_map[&o] = new PrefNodeObserver(o, attr_key);
457 // if we watch a single pref, we want to receive notifications only for a single node
458 if (priv_data->_is_attr) {
459 node->addObserver( *(_observer_map[&o]) );
460 } else {
461 node->addSubtreeObserver( *(_observer_map[&o]) );
462 }
463 }
465 void Preferences::removeObserver(Observer &o)
466 {
467 // prevent removing an observer which was not added
468 if ( _observer_map.find(&o) == _observer_map.end() ) return;
469 Inkscape::XML::Node *node = static_cast<_ObserverData*>(o._data)->_node;
470 _ObserverData *priv_data = static_cast<_ObserverData*>(o._data);
471 o._data = NULL;
473 if (priv_data->_is_attr)
474 node->removeObserver( *(_observer_map[&o]) );
475 else
476 node->removeSubtreeObserver( *(_observer_map[&o]) );
478 delete priv_data;
479 delete _observer_map[&o];
480 _observer_map.erase(&o);
481 }
484 /**
485 * @brief Get the XML node corresponding to the given pref key
486 * @param pref_key Preference key (path) to get
487 * @param create Whether to create the corresponding node if it doesn't exist
488 * @param separator The character used to separate parts of the pref key
489 * @return XML node corresponding to the specified key
490 *
491 * Derived from former inkscape_get_repr(). Private because it assumes that the backend is
492 * a flat XML file, which may not be the case e.g. if we are using GConf (in future).
493 */
494 Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool create)
495 {
496 // verify path
497 g_assert( pref_key.at(0) == '/' );
498 // No longer necessary, can cause problems with input devices which have a dot in the name
499 // g_assert( pref_key.find('.') == Glib::ustring::npos );
501 Inkscape::XML::Node *node = _prefs_doc->root(), *child = NULL;
502 gchar **splits = g_strsplit(pref_key.data(), "/", 0);
504 if ( splits == NULL ) return node;
506 for (int part_i = 0; splits[part_i]; ++part_i) {
507 // skip empty path segments
508 if (!splits[part_i][0]) continue;
510 for (child = node->firstChild(); child; child = child->next())
511 if (!strcmp(splits[part_i], child->attribute("id"))) break;
513 // If the previous loop found a matching key, child now contains the node
514 // matching the processed key part. If no node was found then it is NULL.
515 if (!child) {
516 if (create) {
517 // create the rest of the key
518 while(splits[part_i]) {
519 child = node->document()->createElement("group");
520 child->setAttribute("id", splits[part_i]);
521 node->appendChild(child);
523 ++part_i;
524 node = child;
525 }
526 g_strfreev(splits);
527 return node;
528 } else {
529 return NULL;
530 }
531 }
533 node = child;
534 }
535 g_strfreev(splits);
536 return node;
537 }
539 void Preferences::_getRawValue(Glib::ustring const &path, gchar const *&result)
540 {
541 // create node and attribute keys
542 Glib::ustring node_key, attr_key;
543 _keySplit(path, node_key, attr_key);
545 // retrieve the attribute
546 Inkscape::XML::Node *node = _getNode(node_key, false);
547 if ( node == NULL ) {
548 result = NULL;
549 } else {
550 gchar const *attr = node->attribute(attr_key.data());
551 if ( attr == NULL ) {
552 result = NULL;
553 } else {
554 result = attr;
555 }
556 }
557 }
559 void Preferences::_setRawValue(Glib::ustring const &path, gchar const *value)
560 {
561 // create node and attribute keys
562 Glib::ustring node_key, attr_key;
563 _keySplit(path, node_key, attr_key);
565 // set the attribute
566 Inkscape::XML::Node *node = _getNode(node_key, true);
567 node->setAttribute(attr_key.data(), value);
568 }
570 // The _extract* methods are where the actual wrok is done - they define how preferences are stored
571 // in the XML file.
573 bool Preferences::_extractBool(Entry const &v)
574 {
575 gchar const *s = static_cast<gchar const *>(v._value);
576 if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) return false;
577 return true;
578 }
580 int Preferences::_extractInt(Entry const &v)
581 {
582 gchar const *s = static_cast<gchar const *>(v._value);
583 if ( !strcmp(s, "true") ) return true;
584 if ( !strcmp(s, "false") ) return false;
585 return atoi(s);
586 }
588 double Preferences::_extractDouble(Entry const &v)
589 {
590 gchar const *s = static_cast<gchar const *>(v._value);
591 return g_ascii_strtod(s, NULL);
592 }
594 Glib::ustring Preferences::_extractString(Entry const &v)
595 {
596 return Glib::ustring(static_cast<gchar const *>(v._value));
597 }
599 SPCSSAttr *Preferences::_extractStyle(Entry const &v)
600 {
601 SPCSSAttr *style = sp_repr_css_attr_new();
602 sp_repr_css_attr_add_from_string(style, static_cast<gchar const*>(v._value));
603 return style;
604 }
606 SPCSSAttr *Preferences::_extractInheritedStyle(Entry const &v)
607 {
608 // This is the dirtiest extraction method. Generally we ignore whatever was in v._value
609 // and just get the style using sp_repr_css_attr_inherited. To implement this in GConf,
610 // we'll have to walk up the tree and call sp_repr_css_attr_add_from_string
611 Glib::ustring node_key, attr_key;
612 _keySplit(v._pref_path, node_key, attr_key);
614 Inkscape::XML::Node *node = _getNode(node_key, false);
615 return sp_repr_css_attr_inherited(node, attr_key.data());
616 }
618 // XML backend helper: Split the path into a node key and an attribute key.
619 void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key)
620 {
621 // everything after the last slash
622 attr_key = pref_path.substr(pref_path.rfind('/') + 1, Glib::ustring::npos);
623 // everything before the last slash
624 node_key = pref_path.substr(0, pref_path.rfind('/'));
625 }
627 void Preferences::_reportError(Glib::ustring const &msg, Glib::ustring const &secondary)
628 {
629 _hasError = true;
630 _lastErrPrimary = msg;
631 _lastErrSecondary = secondary;
632 if (_errorHandler) {
633 _errorHandler->handleError(msg, secondary);
634 }
635 }
637 Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &path, void const *ptr)
638 {
639 return Entry(path, ptr);
640 }
642 void Preferences::setErrorHandler(ErrorReporter* handler)
643 {
644 _errorHandler = handler;
645 }
647 void Preferences::unload(bool save)
648 {
649 if(_instance)
650 {
651 if (save) _instance->save();
652 delete _instance;
653 _instance = NULL;
654 }
655 }
657 Preferences *Preferences::_instance = NULL;
660 } // namespace Inkscape
662 /*
663 Local Variables:
664 mode:c++
665 c-file-style:"stroustrup"
666 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
667 indent-tabs-mode:nil
668 fill-column:99
669 End:
670 */
671 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :