GnuCash  5.6-150-g038405b370+
gnc-gsettings.cpp
1 /********************************************************************\
2  * gnc-gsettings.c -- utility functions for storing/retrieving *
3  * data in the GSettings database for GnuCash *
4  * Copyright (C) 2013 Geert Janssens <geert@kobaltwit.be> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22  * *
23 \********************************************************************/
24 
25 #include <config.h>
26 
27 
28 #include <gio/gio.h>
29 #include <glib.h>
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include "gnc-gsettings.h"
34 #include "gnc-path.h"
35 #include "qof.h"
36 #include "gnc-prefs-p.h"
37 
38 #include <boost/property_tree/ptree.hpp>
39 #include <boost/property_tree/xml_parser.hpp>
40 #include <fstream>
41 #include <iostream>
42 #include <unordered_map>
43 
44 namespace bpt = boost::property_tree;
45 
46 #define GSET_SCHEMA_PREFIX "org.gnucash.GnuCash"
47 #define GSET_SCHEMA_OLD_PREFIX "org.gnucash"
48 
50 {
51  void operator()(GSettings* gsp)
52  {
53  g_object_unref(gsp);
54  }
55 };
56 
57 static GSettingsDeleter g_settings_deleter;
58 
59 using GSettingsPtr = std::unique_ptr<GSettings, GSettingsDeleter>;
60 
61 static std::unordered_map<std::string,GSettingsPtr> schema_hash;
62 
63 /* This static indicates the debugging module that this .o belongs to. */
64 static QofLogModule log_module = "gnc.app-utils.gsettings";
65 
66 /************************************************************/
67 /* Internal helper functions */
68 /************************************************************/
69 static bool gnc_gsettings_is_valid_key(GSettings *settings, const gchar *key)
70 {
71  // Check if the key is valid key within settings
72  if (!G_IS_SETTINGS(settings))
73  return false;
74 
75  GSettingsSchema *schema;
76  g_object_get (settings, "settings-schema", &schema, nullptr);
77  if (!schema)
78  return false;
79 
80  auto keys = g_settings_schema_list_keys (schema);
81  auto found = (keys && g_strv_contains(keys, key));
82  g_strfreev (keys);
83  g_settings_schema_unref (schema);
84 
85  return found;
86 }
87 
88 static std::string
89 normalize_schema_name (const gchar *name)
90 {
91  if (!name)
92  return GSET_SCHEMA_PREFIX;
93 
94  if (g_str_has_prefix (name, GSET_SCHEMA_PREFIX) ||
95  (g_str_has_prefix (name, GSET_SCHEMA_OLD_PREFIX)))
96  return name;
97 
98  return std::string{GSET_SCHEMA_PREFIX} + '.' + name;
99 }
100 
101 static GSettings * gnc_gsettings_get_settings_obj (const gchar *schema_str)
102 {
103  ENTER("");
104 
105  auto full_name_str = normalize_schema_name (schema_str);
106  auto full_name = full_name_str.c_str();
107  auto schema_source {g_settings_schema_source_get_default()};
108  auto schema {g_settings_schema_source_lookup(schema_source, full_name, true)};
109  auto gset = g_settings_new_full (schema, nullptr, nullptr);
110  DEBUG ("Created gsettings object %p for schema %s", gset, full_name);
111 
112  if (!G_IS_SETTINGS(gset))
113  PWARN ("Ignoring attempt to access unknown gsettings schema %s", full_name);
114 
115  LEAVE("");
116  g_settings_schema_unref (schema);
117  return gset;
118 }
119 
120 static GSettings*
121 schema_to_gsettings (const char *schema, bool can_retrieve)
122 {
123  auto full_name = normalize_schema_name (schema);
124  auto iter = schema_hash.find (full_name);
125  if (iter != schema_hash.end())
126  return iter->second.get();
127 
128  if (!can_retrieve)
129  return nullptr;
130 
131  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
132  if (!G_IS_SETTINGS (gs_obj))
133  {
134  PWARN ("Ignoring attempt to access unknown gsettings schema %s", full_name.c_str());
135  return nullptr;
136  }
137 
138  schema_hash[full_name] = GSettingsPtr (gs_obj, g_settings_deleter);
139  return gs_obj;
140 }
141 
142 /************************************************************/
143 /* GSettings Utilities */
144 /************************************************************/
145 
146 const gchar *
148 {
149  return GSET_SCHEMA_PREFIX;
150 }
151 
152 /************************************************************/
153 /* Change notification */
154 /************************************************************/
155 
156 gulong
157 gnc_gsettings_register_cb (const gchar *schema, const gchar *key,
158  gpointer func,
159  gpointer user_data)
160 {
161  ENTER("");
162  g_return_val_if_fail (func, 0);
163 
164  auto gs_obj = schema_to_gsettings (schema, true);
165  g_return_val_if_fail (G_IS_SETTINGS (gs_obj), 0);
166 
167  auto signal = static_cast<char *> (nullptr);
168  if (!(key && *key))
169  signal = g_strdup ("changed");
170  else if (gnc_gsettings_is_valid_key(gs_obj, key))
171  signal = g_strconcat ("changed::", key, nullptr);
172 
173  auto handlerid = g_signal_connect (gs_obj, signal, G_CALLBACK (func), user_data);
174  if (handlerid)
175  {
176  g_object_ref (gs_obj);
177 
178  PINFO("schema: %s, key: %s, gs_obj: %p, handler_id: %ld",
179  schema, key, gs_obj, handlerid);
180  }
181  g_free (signal);
182 
183  LEAVE("");
184  return handlerid;
185 }
186 
187 
188 static void
189 gnc_gsettings_remove_cb_by_id_internal (GSettings *gs_obj, guint handlerid)
190 {
191  ENTER ();
192  g_return_if_fail (G_IS_SETTINGS (gs_obj));
193 
194  g_signal_handler_disconnect (gs_obj, handlerid);
195  g_object_unref (gs_obj);
196 
197  LEAVE ("Schema: %p, handlerid: %d - removed for handler",
198  gs_obj, handlerid);
199 }
200 
201 
202 void
203 gnc_gsettings_remove_cb_by_func (const gchar *schema, const gchar *key,
204  gpointer func, gpointer user_data)
205 {
206  ENTER ();
207  g_return_if_fail (func);
208 
209  auto gs_obj = schema_to_gsettings (schema, false);
210 
211  if (!G_IS_SETTINGS (gs_obj))
212  {
213  LEAVE ("No valid GSettings object retrieved from hash table");
214  return;
215  }
216 
217  auto match_type = static_cast<GSignalMatchType> (G_SIGNAL_MATCH_DETAIL | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA);
218  auto changed_signal = g_signal_lookup ("changed", G_TYPE_SETTINGS); /* signal_id */
219  auto quark = g_quark_from_string (key); /* signal_detail */
220 
221  auto matched = 0;
222  guint handler_id = 0;
223  do
224  {
225  handler_id = g_signal_handler_find (gs_obj, match_type,
226  changed_signal, quark, nullptr,
227  func, user_data);
228  if (handler_id)
229  {
230  matched ++;
231  gnc_gsettings_remove_cb_by_id_internal (gs_obj, handler_id);
232 
233  // Previous function will invalidate object if there is only one handler
234  if (!G_IS_SETTINGS (gs_obj))
235  handler_id = 0;
236  }
237  } while (handler_id);
238 
239  LEAVE ("Schema: %s, key: %s - removed %d handlers for 'changed' signal",
240  schema, key, matched);
241 }
242 
243 
244 void
245 gnc_gsettings_remove_cb_by_id (const gchar *schema, guint handlerid)
246 {
247  ENTER ();
248 
249  auto gs_obj = schema_to_gsettings (schema, false);
250 
251  if (!G_IS_SETTINGS (gs_obj))
252  {
253  LEAVE ("No valid GSettings object retrieved from hash table");
254  return;
255  }
256 
257  gnc_gsettings_remove_cb_by_id_internal (gs_obj, handlerid);
258 
259  LEAVE ("Schema: %p, handlerid: %d - removed for handler",
260  gs_obj, handlerid);
261 }
262 
263 
264 guint
265 gnc_gsettings_register_any_cb (const gchar *schema,
266  gpointer func,
267  gpointer user_data)
268 {
269  return gnc_gsettings_register_cb (schema, nullptr, func, user_data);
270 }
271 
272 
273 void
275  gpointer func,
276  gpointer user_data)
277 {
278  gnc_gsettings_remove_cb_by_func (schema, nullptr, func, user_data);
279 }
280 
281 
282 static gboolean gnc_gsettings_enum_bool_mapping_get (GValue *value,
283  GVariant *variant,
284  gpointer user_data)
285 {
286  g_value_set_boolean (value,
287  !g_strcmp0 ((const gchar *)user_data,
288  g_variant_get_string (variant, nullptr)));
289 
290  return true;
291 }
292 
293 static GVariant* gnc_gsettings_enum_bool_mapping_set (const GValue *value,
294  const GVariantType *expected_type,
295  gpointer user_data)
296 {
297  if (g_value_get_boolean (value))
298  {
299  return g_variant_new_string ((const gchar *)user_data);
300  }
301  else
302  {
303  /* GtkRadioButtons will set the value to false when another option is
304  * selected, just ignore this. */
305  return nullptr;
306  }
307 }
308 
309 void gnc_gsettings_bind (const gchar *schema,
310  /*@ null @*/ const gchar *key,
311  /*@ null @*/ const gchar *value,
312  gpointer object,
313  const gchar *property)
314 {
315  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
316  g_return_if_fail (G_IS_SETTINGS (gs_obj));
317 
318  if (gnc_gsettings_is_valid_key (gs_obj, key))
319  {
320  if (value)
321  {
322  g_settings_bind_with_mapping (gs_obj, key, object, property,
323  G_SETTINGS_BIND_DEFAULT,
324  gnc_gsettings_enum_bool_mapping_get,
325  gnc_gsettings_enum_bool_mapping_set,
326  g_strdup (value), g_free);
327  }
328  else
329  {
330  g_settings_bind (gs_obj, key, object, property, G_SETTINGS_BIND_DEFAULT);
331  }
332  }
333  else
334  {
335  PERR ("Invalid key %s for schema %s", key, schema);
336  }
337 }
338 
339 
340 static void
341 gs_obj_block_handlers ([[maybe_unused]] gpointer key, gpointer gs_obj,
342  [[maybe_unused]] gpointer pointer)
343 {
344  g_signal_handlers_block_matched (gs_obj, G_SIGNAL_MATCH_CLOSURE, 0, 0, nullptr, nullptr, nullptr);
345  PINFO("Block all handlers for GSettings object %p", gs_obj);
346 }
347 
348 static void
349 gs_obj_unblock_handlers ([[maybe_unused]] gpointer key, gpointer gs_obj,
350  [[maybe_unused]] gpointer pointer)
351 {
352  g_signal_handlers_unblock_matched (gs_obj, G_SIGNAL_MATCH_CLOSURE, 0, 0, nullptr, nullptr, nullptr);
353  PINFO("Unblock all handlers for GSettings object %p", gs_obj);
354 }
355 
357 {
358  ENTER ();
359  for (const auto& it : schema_hash)
360  gs_obj_block_handlers (nullptr, it.second.get(), nullptr);
361  LEAVE();
362 }
363 
364 
366 {
367  ENTER ();
368  for (const auto& it : schema_hash)
369  gs_obj_unblock_handlers (nullptr, it.second.get(), nullptr);
370  LEAVE();
371 }
372 
373 
374 /************************************************************/
375 /* Getters */
376 /************************************************************/
377 template<typename T>
378 T gnc_gsettings_get(const char *schema, const char *key,
379  auto getter(GSettings*, const char *)->T, T default_val)
380 {
381  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
382  g_return_val_if_fail (G_IS_SETTINGS (gs_obj), default_val);
383 
384  T val = default_val;
385  if (gnc_gsettings_is_valid_key (gs_obj, key))
386  val = getter (gs_obj, key);
387  else
388  PERR ("Invalid key %s for schema %s", key, schema);
389 
390  g_object_unref (gs_obj);
391  return val;
392 }
393 
394 gboolean
395 gnc_gsettings_get_bool (const gchar *schema, const gchar *key)
396 {
397  return gnc_gsettings_get (schema, key, g_settings_get_boolean,
398  static_cast<gboolean>(false));
399 }
400 
401 gint
402 gnc_gsettings_get_int (const gchar *schema, const gchar *key)
403 {
404  return gnc_gsettings_get (schema, key, g_settings_get_int, 0);
405 }
406 
407 gdouble
408 gnc_gsettings_get_float (const gchar *schema, const gchar *key)
409 {
410  return gnc_gsettings_get (schema, key, g_settings_get_double, 0.0);
411 }
412 
413 gchar *
414 gnc_gsettings_get_string (const gchar *schema, const gchar *key)
415 {
416  return gnc_gsettings_get (schema, key, g_settings_get_string,
417  static_cast<gchar *> (nullptr));
418 }
419 
420 gint
421 gnc_gsettings_get_enum (const gchar *schema, const gchar *key)
422 {
423  return gnc_gsettings_get (schema, key, g_settings_get_enum, 0);
424 }
425 
426 GVariant *
427 gnc_gsettings_get_value (const gchar *schema, const gchar *key)
428 {
429  return gnc_gsettings_get (schema, key, g_settings_get_value,
430  static_cast<GVariant *> (nullptr));
431 }
432 
433 /************************************************************/
434 /* Setters */
435 /************************************************************/
436 template<typename T> gboolean
437 gnc_gsettings_set (const gchar *schema,
438  const gchar *key,
439  T value,
440  gboolean setter(GSettings*, const char *, T))
441 {
442  ENTER("schema: %s, key: %s", schema, key);
443 
444  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
445  g_return_val_if_fail (G_IS_SETTINGS (gs_obj), false);
446 
447  auto result = false;
448  if (gnc_gsettings_is_valid_key (gs_obj, key))
449  {
450  result = setter (gs_obj, key, value);
451  if (!result)
452  PERR ("Unable to set value for key %s in schema %s", key, schema);
453  }
454  else
455  PERR ("Invalid key %s for schema %s", key, schema);
456 
457  g_object_unref (gs_obj);
458  LEAVE("result %i", result);
459  return result;
460 }
461 
462 gboolean
463 gnc_gsettings_set_bool (const gchar *schema, const gchar *key, gboolean value)
464 {
465  return gnc_gsettings_set (schema, key, value, g_settings_set_boolean);
466 }
467 
468 gboolean
469 gnc_gsettings_set_int (const gchar *schema, const gchar *key, gint value)
470 {
471  return gnc_gsettings_set (schema, key, value, g_settings_set_int);
472 }
473 
474 gboolean
475 gnc_gsettings_set_float (const gchar *schema, const gchar *key, gdouble value)
476 {
477  return gnc_gsettings_set (schema, key, value, g_settings_set_double);
478 }
479 
480 gboolean
481 gnc_gsettings_set_string (const gchar *schema, const gchar *key, const gchar *value)
482 {
483  return gnc_gsettings_set (schema, key, value, g_settings_set_string);
484 }
485 
486 gboolean
487 gnc_gsettings_set_enum (const gchar *schema, const gchar *key, gint value)
488 {
489  return gnc_gsettings_set (schema, key, value, g_settings_set_enum);
490 }
491 
492 gboolean
493 gnc_gsettings_set_value (const gchar *schema, const gchar *key, GVariant *value)
494 {
495  return gnc_gsettings_set (schema, key, value, g_settings_set_value);
496 }
497 
498 void
499 gnc_gsettings_reset (const gchar *schema,
500  const gchar *key)
501 {
502  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
503  g_return_if_fail (G_IS_SETTINGS (gs_obj));
504 
505  if (gnc_gsettings_is_valid_key (gs_obj, key))
506  g_settings_reset (gs_obj, key);
507  else
508  PERR ("Invalid key %s for schema %s", key, schema);
509 
510  g_object_unref (gs_obj);
511 }
512 
513 void
514 gnc_gsettings_reset_schema (const gchar *schema_str)
515 {
516  auto gs_obj = gnc_gsettings_get_settings_obj (schema_str);
517 
518  if (!gs_obj)
519  return;
520 
521  GSettingsSchema *schema;
522  g_object_get (gs_obj, "settings-schema", &schema, nullptr);
523  if (!schema)
524  {
525  g_object_unref (gs_obj);
526  return;
527  }
528 
529  auto keys = g_settings_schema_list_keys (schema);
530  if (keys)
531  {
532  auto fkeys = keys;
533  for (auto key = *fkeys; key; key = *++fkeys)
534  gnc_gsettings_reset (schema_str, key);
535  }
536 
537  g_object_unref (gs_obj);
538  g_settings_schema_unref (schema);
539  g_strfreev (keys);
540 }
541 
542 static void
543 gnc_settings_dump_schema_paths (void)
544 {
545  gchar **non_relocatable;
546 
547  auto schema_source {g_settings_schema_source_get_default()};
548  g_settings_schema_source_list_schemas (schema_source, true,
549  &non_relocatable, nullptr);
550 
551  for (gint i = 0; non_relocatable[i] != nullptr; i++)
552  PINFO("Schema entry %d is '%s'", i, non_relocatable[i]);
553 
554  g_strfreev (non_relocatable);
555 }
556 
558 {
559  ENTER("");
560 
561  /* The gsettings backend only works in an installed environment.
562  * When called from the source environment (for testing purposes)
563  * simply return.
564  */
565  if (g_strcmp0 (g_getenv ("GNC_UNINSTALLED"), "1") == 0)
566  return;
567 
568  g_free (prefsbackend);
569  prefsbackend = g_new0 (PrefsBackend, 1);
570 
571  prefsbackend->register_cb = gnc_gsettings_register_cb;
572  prefsbackend->remove_cb_by_func = gnc_gsettings_remove_cb_by_func;
573  prefsbackend->remove_cb_by_id = gnc_gsettings_remove_cb_by_id;
574  prefsbackend->register_group_cb = gnc_gsettings_register_any_cb;
575  prefsbackend->remove_group_cb_by_func = gnc_gsettings_remove_any_cb_by_func;
576  prefsbackend->bind = gnc_gsettings_bind;
577  prefsbackend->get_bool = gnc_gsettings_get_bool;
578  prefsbackend->get_int = gnc_gsettings_get_int;
579  prefsbackend->get_float = gnc_gsettings_get_float;
580  prefsbackend->get_string = gnc_gsettings_get_string;
581  prefsbackend->get_enum = gnc_gsettings_get_enum;
582  prefsbackend->get_value = gnc_gsettings_get_value;
583  prefsbackend->set_bool = gnc_gsettings_set_bool;
584  prefsbackend->set_int = gnc_gsettings_set_int;
585  prefsbackend->set_float = gnc_gsettings_set_float;
586  prefsbackend->set_string = gnc_gsettings_set_string;
587  prefsbackend->set_enum = gnc_gsettings_set_enum;
588  prefsbackend->set_value = gnc_gsettings_set_value;
589  prefsbackend->reset = gnc_gsettings_reset;
590  prefsbackend->reset_group = gnc_gsettings_reset_schema;
591  prefsbackend->block_all = gnc_gsettings_block_all;
592  prefsbackend->unblock_all = gnc_gsettings_unblock_all;
593 
594  if (qof_log_check (log_module, QOF_LOG_DEBUG))
595  gnc_settings_dump_schema_paths ();
596 
597  /* Run any data model changes for the backend before it's used
598  * by anyone */
600 
601  LEAVE("Prefsbackend bind = %p", prefsbackend->bind);
602 }
603 
604 void
606 {
607  schema_hash.clear();
608  g_free (prefsbackend);
609 }
610 
611 
612 static GVariant *
613 gnc_gsettings_get_user_value (const gchar *schema,
614  const gchar *key)
615 {
616  auto gs_obj = gnc_gsettings_get_settings_obj (schema);
617  g_return_val_if_fail (G_IS_SETTINGS (gs_obj), nullptr);
618 
619  auto val = static_cast<GVariant *> (nullptr);
620  if (gnc_gsettings_is_valid_key (gs_obj, key))
621  val = g_settings_get_user_value (gs_obj, key);
622  else
623  PERR ("Invalid key %s for schema %s", key, schema);
624 
625  g_object_unref (gs_obj);
626  return val;
627 }
628 
629 using opt_str_vec = boost::optional<std::string>;
630 
631 static void
632 deprecate_one_key (const opt_str_vec &oldpath, const opt_str_vec &oldkey)
633 {
634  if (!oldpath || !oldkey )
635  {
636  DEBUG ("Skipping <deprecate> node - missing attribute (old-path or old-key)");
637  return;
638  }
639 
640  PINFO ("'%s:%s' has been marked deprecated", oldpath->c_str(), oldkey->c_str());
641  /* This does nothing really, but is a reminder for future maintainers
642  * to mark this pref as obsolete in the next major release. */
643 }
644 
645 static void
646 migrate_one_key (const opt_str_vec &oldpath, const opt_str_vec &oldkey,
647  const opt_str_vec &newpath, const opt_str_vec &newkey)
648 {
649  if (!oldpath || !oldkey || !newpath || !newkey)
650  {
651  DEBUG ("Skipping <migrate> node - missing attribute (old-path, old-key, new-path or new-key)");
652  return;
653  }
654 
655  PINFO ("Migrating '%s:%s' to '%s:%s'", oldpath->c_str(), oldkey->c_str(),
656  newpath->c_str(), newkey->c_str());
657 
658  auto user_value = gnc_gsettings_get_user_value (oldpath->c_str(), oldkey->c_str());
659  if (user_value)
660  gnc_gsettings_set_value (newpath->c_str(), newkey->c_str(), user_value);
661 }
662 
663 static void
664 obsolete_one_key (const opt_str_vec &oldpath, const opt_str_vec &oldkey)
665 {
666  if (!oldpath || !oldkey )
667  {
668  DEBUG ("Skipping <obsolete> node - missing attribute (old-path or old-key)");
669  return;
670  }
671 
672  PINFO ("Resetting obsolete '%s:%s'", oldpath->c_str(), oldkey->c_str());
673  gnc_gsettings_reset (oldpath->c_str(), oldkey->c_str());
674 }
675 
676 static void
677 parse_one_release_node (bpt::ptree &pt)
678 {
679  /* loop over top-level property tree */
680  std::for_each (pt.begin(), pt.end(),
681  [] (std::pair<bpt::ptree::key_type, bpt::ptree> node)
682  {
683  if (node.first == "<xmlattr>")
684  return;
685  else if (node.first == "deprecate")
686  deprecate_one_key (node.second.get_optional<std::string> ("<xmlattr>.old-path"),
687  node.second.get_optional<std::string> ("<xmlattr>.old-key"));
688  else if (node.first == "migrate")
689  migrate_one_key (node.second.get_optional<std::string> ("<xmlattr>.old-path"),
690  node.second.get_optional<std::string> ("<xmlattr>.old-key"),
691  node.second.get_optional<std::string> ("<xmlattr>.new-path"),
692  node.second.get_optional<std::string> ("<xmlattr>.new-key"));
693  else if (node.first == "obsolete")
694  obsolete_one_key (node.second.get_optional<std::string> ("<xmlattr>.old-path"),
695  node.second.get_optional<std::string> ("<xmlattr>.old-key"));
696  else
697  {
698  DEBUG ("Skipping unknown node <%s>", node.first.c_str());
699  return;
700  }
701  });
702 }
703 
704 static void
705 transform_settings (int old_maj_min, int cur_maj_min)
706 {
707  bpt::ptree pt;
708 
709  auto pkg_data_dir = gnc_path_get_pkgdatadir();
710  auto transform_file = std::string (pkg_data_dir) + "/pref_transformations.xml";
711  g_free (pkg_data_dir);
712 
713  std::ifstream transform_stream {transform_file};
714  if (!transform_stream.is_open())
715  {
716  PWARN("Failed to load preferences transformation file '%s'", transform_file.c_str());
717  return;
718  }
719 
720  try
721  {
722  bpt::read_xml (transform_stream, pt);
723  }
724  catch (bpt::xml_parser_error &e) {
725  PWARN ("Failed to parse GnuCash preferences transformation file.\n");
726  PWARN ("Error message:\n");
727  PWARN ("%s\n", e.what());
728  return;
729  }
730  catch (...) {
731  PWARN ("Unknown error while parsing GnuCash preferences transformation file.\n");
732  return;
733  }
734 
735  /* loop over top-level property tree */
736  std::for_each (pt.begin(), pt.end(),
737  [&old_maj_min, &cur_maj_min] (std::pair<bpt::ptree::key_type, bpt::ptree> node)
738  {
739  if (node.first != "release")
740  {
741  DEBUG ("Skipping non-<release> node <%s>", node.first.c_str());
742  return;
743  }
744  auto version = node.second.get_optional<int> ("<xmlattr>.version");
745  if (!version)
746  {
747  DEBUG ("Skipping <release> node - no version attribute found");
748  return;
749  }
750  if (*version <= old_maj_min)
751  {
752  DEBUG ("Skipping <release> node - version %i is less than current compatibility level %i", *version, old_maj_min);
753  return;
754  }
755  if (*version > cur_maj_min)
756  {
757  DEBUG ("Skipping <release> node - version %i is greater than current version level %i", *version, cur_maj_min);
758  return;
759  }
760  DEBUG ("Retrieved version value '%i'", *version);
761 
762  parse_one_release_node (node.second);
763  });
764 }
765 
767 {
768  /* This routine will conditionally execute conversion rules from
769  * prefs_transformations.xml to adapt the user's existing preferences to
770  * the current preferences schema. The rules in this file are versioned and
771  * only rules still relevant to the user's existing preferences and for
772  * this version of GnuCash will be executed.
773  *
774  * Starting with GnuCash 4.7 the code expects all preferences to be stored
775  * under prefix org.gnucash.GnuCash instead of org.gnucash, including our
776  * GNC_PREF_VERSION setting.
777  * As the logic to determine whether or not settings conversions are needed
778  * depends on this preference, we have to test for its value in two
779  * locations:
780  * - if GNC_PREF_VERSION is not set under old nor new prefix
781  * => GnuCash has never run before so no conversion run necessary
782  * - if GNC_PREF_VERSION is set under old prefix and not new prefix
783  * => user's preferences weren't moved yet from old to new prefix. Use old
784  * prefix GNC_PREF_VERSION to determine which conversions may be needed
785  * - if GNC_PREF_VERSION is set under both prefixes
786  * => ignore old prefix and use new prefix GNC_PREF_VERSION to determine
787  * which conversions may be needed.
788  * Sometime in the future (GnuCash 6.0) the old prefix will be fully removed
789  * and the test will be simplified to only check in the new prefix.
790  */
791  ENTER("Start of settings transform routine.");
792 
793  auto ogG_maj_min = gnc_gsettings_get_user_value (GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION);
794  auto og_maj_min = gnc_gsettings_get_user_value (GSET_SCHEMA_OLD_PREFIX "." GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION);
795 
796  auto cur_maj_min = PROJECT_VERSION_MAJOR * 1000 + PROJECT_VERSION_MINOR;
797 
798  if (!ogG_maj_min && !og_maj_min) // new install
799  {
800  gnc_gsettings_set_int (GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION, cur_maj_min);
801  LEAVE ("Setting Previous compatibility level to current version: %i", cur_maj_min);
802  return;
803  }
804 
805  auto old_maj_min = 0;
806  if (!ogG_maj_min) // old preference location
807  old_maj_min = gnc_gsettings_get_int (GSET_SCHEMA_OLD_PREFIX "." GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION);
808  else // new preference location
809  {
810  g_variant_unref (ogG_maj_min);
811  old_maj_min = gnc_gsettings_get_int (GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION);
812  }
813  if (og_maj_min)
814  g_variant_unref (og_maj_min);
815 
816  PINFO ("Previous setting compatibility level: %i, Current version: %i", old_maj_min, cur_maj_min);
817 
818  transform_settings (old_maj_min, cur_maj_min);
819 
820  /* Only write current version if it's more recent than what was set */
821  if (cur_maj_min > old_maj_min)
822  gnc_gsettings_set_int (GNC_PREFS_GROUP_GENERAL, GNC_PREF_VERSION, cur_maj_min);
823 
824  LEAVE("");
825 }
gboolean gnc_gsettings_set_int(const gchar *schema, const gchar *key, gint value)
Store an integer value into GSettings.
gboolean gnc_gsettings_set_float(const gchar *schema, const gchar *key, gdouble value)
Store a float value into GSettings.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
gint gnc_gsettings_get_int(const gchar *schema, const gchar *key)
Get an integer value from GSettings.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
void gnc_gsettings_remove_any_cb_by_func(const gchar *schema, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when any key in the given settings schema change...
gboolean gnc_gsettings_set_enum(const gchar *schema, const gchar *key, gint value)
Store an enum value into GSettings.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
gboolean qof_log_check(QofLogModule domain, QofLogLevel level)
Check to see if the given log_module is configured to log at the given log_level. ...
Definition: qoflog.cpp:324
GVariant * gnc_gsettings_get_value(const gchar *schema, const gchar *key)
Get an arbitrary combination of values from GSettings.
guint gnc_gsettings_register_any_cb(const gchar *schema, gpointer func, gpointer user_data)
Register a callback for when any key in the settings schema is changed.
void gnc_gsettings_unblock_all(void)
UnBlock all prefs callbacks, used while preference dialog is loaded.
void gnc_gsettings_reset_schema(const gchar *schema_str)
Reset all keys in a schema to their default values in GSettings.
void gnc_gsettings_shutdown(void)
Free the GSettings resources.
gdouble gnc_gsettings_get_float(const gchar *schema, const gchar *key)
Get an float value from GSettings.
void gnc_gsettings_reset(const gchar *schema, const gchar *key)
Reset a key to its default value in GSettings.
gchar * gnc_gsettings_get_string(const gchar *schema, const gchar *key)
Get a string value from GSettings.
void gnc_gsettings_bind(const gchar *schema, const gchar *key, const gchar *value, gpointer object, const gchar *property)
Bind a setting to a g_object property.
gint gnc_gsettings_get_enum(const gchar *schema, const gchar *key)
Get an enum value from GSettings.
gboolean gnc_gsettings_set_bool(const gchar *schema, const gchar *key, gboolean value)
Store a boolean value into GSettings.
void gnc_gsettings_load_backend(void)
Configure gsettings as the backend for the gnucash preferences api.
void gnc_gsettings_version_upgrade(void)
Check whether we need to adjust the user settings to a newer version.
void gnc_gsettings_block_all(void)
Block all prefs callbacks, used while preference dialog is loaded.
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
gboolean gnc_gsettings_get_bool(const gchar *schema, const gchar *key)
Get a boolean value from GSettings.
const gchar * gnc_gsettings_get_prefix(void)
Get the default gsettings schema prefix.
void gnc_gsettings_remove_cb_by_id(const gchar *schema, guint handlerid)
Remove a function that was registered for a callback when a specific key in the settings schema chang...
gboolean gnc_gsettings_set_value(const gchar *schema, const gchar *key, GVariant *value)
Store an arbitrary combination of values into GSettings.
gulong gnc_gsettings_register_cb(const char *schema, const gchar *key, gpointer func, gpointer user_data)
Register a callback for when a specific key in the settings schema is changed.
GSettings helper routines.
gboolean gnc_gsettings_set_string(const gchar *schema, const gchar *key, const gchar *value)
Store a string into GSettings.
void gnc_gsettings_remove_cb_by_func(const gchar *schema, const gchar *key, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when a specific key in the settings schema chang...