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);
65 path = profile_path(_prefs_basename.data());
66 _prefs_filename = path;
67 g_free(path);
69 _load();
70 }
72 Preferences::~Preferences()
73 {
74 // when the preferences are unloaded, save them
75 save();
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()
103 {
104 _loadDefaults();
106 Glib::ustring const not_saved = _("Inkscape will run with default settings, "
107 "and new settings will not be saved. ");
109 // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
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 }
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 }
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 }
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 }
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 }
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;
214 }
216 /**
217 * @brief Flush all pref changes to the XML file
218 */
219 void Preferences::save()
220 {
221 if (!_writable) return; // no-op if the prefs file is not writable
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());
231 }
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)
242 {
243 std::vector<Entry> temp;
244 Inkscape::XML::Node *node = _getNode(path, false);
245 if (!node) return temp;
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;
252 }
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)
260 {
261 std::vector<Glib::ustring> temp;
262 Inkscape::XML::Node *node = _getNode(path, false);
263 if (!node) return temp;
265 for (Inkscape::XML::NodeSiblingIterator i = node->firstChild(); i; ++i) {
266 temp.push_back(path + '/' + i->attribute("id"));
267 }
268 return temp;
269 }
271 // getter methods
273 Preferences::Entry const Preferences::getEntry(Glib::ustring const &pref_path)
274 {
275 gchar const *v;
276 _getRawValue(pref_path, v);
277 return Entry(pref_path, v);
278 }
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)
288 {
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" ));
293 }
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)
301 {
302 gchar intstr[32];
303 g_snprintf(intstr, 32, "%d", value);
304 _setRawValue(pref_path, intstr);
305 }
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)
313 {
314 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
315 g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, value);
316 _setRawValue(pref_path, buf);
317 }
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)
325 {
326 _setRawValue(pref_path, value.data());
327 }
329 void Preferences::setStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
330 {
331 gchar *css_str = sp_repr_css_write_string(style);
332 _setRawValue(pref_path, css_str);
333 g_free(css_str);
334 }
336 void Preferences::mergeStyle(Glib::ustring const &pref_path, SPCSSAttr *style)
337 {
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);
344 }
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)
362 {
363 }
365 Preferences::Observer::~Observer()
366 {
367 // on destruction remove observer to prevent invalid references
368 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
369 prefs->removeObserver(*this);
370 }
372 void Preferences::PrefNodeObserver::notifyAttributeChanged(XML::Node &node, GQuark name, Util::ptr_shared<char>, Util::ptr_shared<char> new_value)
373 {
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;
378 _ObserverData *d = static_cast<_ObserverData*>(Preferences::_get_pref_observer_data(_observer));
379 Glib::ustring notify_path = _observer.observed_path;
381 if (!d->_is_attr) {
382 std::vector<gchar const *> path_fragments;
383 notify_path.reserve(256); // this will make appending operations faster
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 }
401 Entry const val = Preferences::_create_pref_value(notify_path, static_cast<void const*>(new_value.pointer()));
402 _observer.notify(val);
403 }
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)
409 {
410 // first assume that the last path element is an entry.
411 _keySplit(pref_path, node_key, attr_key);
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;
427 }
429 void Preferences::addObserver(Observer &o)
430 {
431 // prevent adding the same observer twice
432 if ( _observer_map.find(&o) != _observer_map.end() ) return;
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;
439 // set additional data
440 _ObserverData *priv_data = new _ObserverData;
441 priv_data->_node = node;
442 priv_data->_is_attr = !attr_key.empty();
443 o._data = static_cast<void*>(priv_data);
445 _observer_map[&o] = new PrefNodeObserver(o, attr_key);
447 // if we watch a single pref, we want to receive notifications only for a single node
448 if (priv_data->_is_attr) {
449 node->addObserver( *(_observer_map[&o]) );
450 } else {
451 node->addSubtreeObserver( *(_observer_map[&o]) );
452 }
453 }
455 void Preferences::removeObserver(Observer &o)
456 {
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 _ObserverData *priv_data = static_cast<_ObserverData*>(o._data);
461 o._data = NULL;
463 if (priv_data->_is_attr)
464 node->removeObserver( *(_observer_map[&o]) );
465 else
466 node->removeSubtreeObserver( *(_observer_map[&o]) );
468 delete priv_data;
469 delete _observer_map[&o];
470 _observer_map.erase(&o);
471 }
474 /**
475 * @brief Get the XML node corresponding to the given pref key
476 * @param pref_key Preference key (path) to get
477 * @param create Whether to create the corresponding node if it doesn't exist
478 * @param separator The character used to separate parts of the pref key
479 * @return XML node corresponding to the specified key
480 *
481 * Derived from former inkscape_get_repr(). Private because it assumes that the backend is
482 * a flat XML file, which may not be the case e.g. if we are using GConf (in future).
483 */
484 Inkscape::XML::Node *Preferences::_getNode(Glib::ustring const &pref_key, bool create)
485 {
486 // verify path
487 g_assert( pref_key.at(0) == '/' );
488 g_assert( pref_key.find('.') == Glib::ustring::npos );
490 Inkscape::XML::Node *node = _prefs_doc->root(), *child = NULL;
491 gchar **splits = g_strsplit(pref_key.data(), "/", 0);
493 if ( splits == NULL ) return node;
495 for (int part_i = 0; splits[part_i]; ++part_i) {
496 // skip empty path segments
497 if (!splits[part_i][0]) continue;
499 for (child = node->firstChild(); child; child = child->next())
500 if (!strcmp(splits[part_i], child->attribute("id"))) break;
502 // If the previous loop found a matching key, child now contains the node
503 // matching the processed key part. If no node was found then it is NULL.
504 if (!child) {
505 if (create) {
506 // create the rest of the key
507 while(splits[part_i]) {
508 child = node->document()->createElement("group");
509 child->setAttribute("id", splits[part_i]);
510 node->appendChild(child);
512 ++part_i;
513 node = child;
514 }
515 g_strfreev(splits);
516 return node;
517 } else {
518 return NULL;
519 }
520 }
522 node = child;
523 }
524 g_strfreev(splits);
525 return node;
526 }
528 void Preferences::_getRawValue(Glib::ustring const &path, gchar const *&result)
529 {
530 // create node and attribute keys
531 Glib::ustring node_key, attr_key;
532 _keySplit(path, node_key, attr_key);
534 // retrieve the attribute
535 Inkscape::XML::Node *node = _getNode(node_key, false);
536 if ( node == NULL ) {
537 result = NULL;
538 } else {
539 gchar const *attr = node->attribute(attr_key.data());
540 if ( attr == NULL ) {
541 result = NULL;
542 } else {
543 result = attr;
544 }
545 }
546 }
548 void Preferences::_setRawValue(Glib::ustring const &path, gchar const *value)
549 {
550 // create node and attribute keys
551 Glib::ustring node_key, attr_key;
552 _keySplit(path, node_key, attr_key);
554 // set the attribute
555 Inkscape::XML::Node *node = _getNode(node_key, true);
556 node->setAttribute(attr_key.data(), value);
557 }
559 // The _extract* methods are where the actual wrok is done - they define how preferences are stored
560 // in the XML file.
562 bool Preferences::_extractBool(Entry const &v)
563 {
564 gchar const *s = static_cast<gchar const *>(v._value);
565 if ( !s[0] || !strcmp(s, "0") || !strcmp(s, "false") ) return false;
566 return true;
567 }
569 int Preferences::_extractInt(Entry const &v)
570 {
571 gchar const *s = static_cast<gchar const *>(v._value);
572 if ( !strcmp(s, "true") ) return true;
573 if ( !strcmp(s, "false") ) return false;
574 return atoi(s);
575 }
577 double Preferences::_extractDouble(Entry const &v)
578 {
579 gchar const *s = static_cast<gchar const *>(v._value);
580 return g_ascii_strtod(s, NULL);
581 }
583 Glib::ustring Preferences::_extractString(Entry const &v)
584 {
585 return Glib::ustring(static_cast<gchar const *>(v._value));
586 }
588 SPCSSAttr *Preferences::_extractStyle(Entry const &v)
589 {
590 SPCSSAttr *style = sp_repr_css_attr_new();
591 sp_repr_css_attr_add_from_string(style, static_cast<gchar const*>(v._value));
592 return style;
593 }
595 SPCSSAttr *Preferences::_extractInheritedStyle(Entry const &v)
596 {
597 // This is the dirtiest extraction method. Generally we ignore whatever was in v._value
598 // and just get the style using sp_repr_css_attr_inherited. To implement this in GConf,
599 // we'll have to walk up the tree and call sp_repr_css_attr_add_from_string
600 Glib::ustring node_key, attr_key;
601 _keySplit(v._pref_path, node_key, attr_key);
603 Inkscape::XML::Node *node = _getNode(node_key, false);
604 return sp_repr_css_attr_inherited(node, attr_key.data());
605 }
607 // XML backend helper: Split the path into a node key and an attribute key.
608 void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key)
609 {
610 // everything after the last slash
611 attr_key = pref_path.substr(pref_path.rfind('/') + 1, Glib::ustring::npos);
612 // everything before the last slash
613 node_key = pref_path.substr(0, pref_path.rfind('/'));
614 }
616 void Preferences::_errorDialog(Glib::ustring const &msg, Glib::ustring const &secondary)
617 {
618 if (Preferences::use_gui) {
619 Gtk::MessageDialog err(
620 msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
621 err.set_secondary_text(secondary);
622 err.run();
623 } else {
624 g_message("%s", msg.data());
625 g_message("%s", secondary.data());
626 }
627 }
629 Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &path, void const *ptr)
630 {
631 return Entry(path, ptr);
632 }
634 bool Preferences::use_gui = true;
635 Preferences *Preferences::_instance = NULL;
638 } // namespace Inkscape
640 /*
641 Local Variables:
642 mode:c++
643 c-file-style:"stroustrup"
644 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
645 indent-tabs-mode:nil
646 fill-column:99
647 End:
648 */
649 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :