GnuCash  4.11-243-g1cac132214+
gnc-prefs-utils.c
1 /********************************************************************\
2  * gnc-prefs-utils.c -- utility functions for preferences management*
3  * Copyright (C) 2013 Geert Janssens <geert@kobaltwit.be> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
23 
24 #include <config.h>
25 
26 #include "gnc-gsettings.h"
27 #include "gnc-prefs-utils.h"
28 #include "gnc-prefs.h"
29 #include "xml/gnc-backend-xml.h"
30 
31 static QofLogModule log_module = G_LOG_DOMAIN;
32 
33 /* Keys used for core preferences */
34 #define GNC_PREF_FILE_COMPRESSION "file-compression"
35 #define GNC_PREF_RETAIN_TYPE_NEVER "retain-type-never"
36 #define GNC_PREF_RETAIN_TYPE_DAYS "retain-type-days"
37 #define GNC_PREF_RETAIN_TYPE_FOREVER "retain-type-forever"
38 #define GNC_PREF_RETAIN_DAYS "retain-days"
39 
40 /***************************************************************
41  * Initialization *
42  ***************************************************************/
43 static void
44 file_retain_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
45 {
46  if (gnc_prefs_is_set_up())
47  {
48  gint days = (int)gnc_prefs_get_float(GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS);
49  gnc_prefs_set_file_retention_days (days);
50  }
51 }
52 
53 static void
54 file_retain_type_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
55 {
56  XMLFileRetentionType type = XML_RETAIN_ALL;
57 
58  if (gnc_prefs_is_set_up())
59  {
60  if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER))
61  type = XML_RETAIN_NONE;
62  else if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS))
63  type = XML_RETAIN_DAYS;
64  else if (!gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER))
65  PWARN("no file retention policy was set, assuming conservative policy 'forever'");
66 
67  gnc_prefs_set_file_retention_policy (type);
68  }
69 }
70 
71 static void
72 file_compression_changed_cb(gpointer gsettings, gchar *key, gpointer user_data)
73 {
74  if (gnc_prefs_is_set_up())
75  {
76  gboolean file_compression = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION);
77  gnc_prefs_set_file_save_compressed (file_compression);
78  }
79 }
80 
81 
82 void gnc_prefs_init (void)
83 {
85 
86  /* Initialize the core preferences by reading their values from the loaded backend.
87  * Note: if no backend was loaded, these functions will return sane default values.
88  */
89  file_retain_changed_cb (NULL, NULL, NULL);
90  file_retain_type_changed_cb (NULL, NULL, NULL);
91  file_compression_changed_cb (NULL, NULL, NULL);
92 
93  /* Check for invalid retain_type (days)/retain_days (0) combo.
94  * This can happen either because a user changed the preferences
95  * manually outside of GnuCash, or because the user upgraded from
96  * gnucash version 2.3.15 or older. Back then, 0 retain_days meant
97  * "keep forever". From 2.3.15 on this is controlled via a multiple
98  * choice ("retain_type").
99  * So if we find a 0 retain_days value with a "days" retain_type,
100  * we will silently and conservatively interpret is as meaning
101  * retain forever ("forever" retain_type).
102  */
103  if ( (gnc_prefs_get_file_retention_policy () == XML_RETAIN_DAYS) &&
104  (gnc_prefs_get_file_retention_days () == 0 ) )
105  {
106  gnc_prefs_set_file_retention_policy (XML_RETAIN_ALL);
107  gnc_prefs_set_file_retention_days (30);
108  gnc_prefs_set_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER, TRUE);
109  gnc_prefs_set_float (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS, 30);
110  PWARN("retain 0 days policy was set, but this is probably not what the user wanted,\n"
111  "assuming conservative policy 'forever'");
112  }
113 
114  /* Add hooks to update core preferences whenever the associated preference changes */
115  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS,
116  file_retain_changed_cb, NULL);
117  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER,
118  file_retain_type_changed_cb, NULL);
119  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS,
120  file_retain_type_changed_cb, NULL);
121  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER,
122  file_retain_type_changed_cb, NULL);
123  gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION,
124  file_compression_changed_cb, NULL);
125 
126 }
127 
128 void
130 {
131  // remove the registered pref call backs above
132  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_DAYS,
133  file_retain_changed_cb, NULL);
134  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_NEVER,
135  file_retain_type_changed_cb, NULL);
136  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_DAYS,
137  file_retain_type_changed_cb, NULL);
138  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_RETAIN_TYPE_FOREVER,
139  file_retain_type_changed_cb, NULL);
140  gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_FILE_COMPRESSION,
141  file_compression_changed_cb, NULL);
142 }
gulong gnc_prefs_register_cb(const char *group, const gchar *pref_name, gpointer func, gpointer user_data)
Register a callback that gets triggered when the given preference changes.
Definition: gnc-prefs.c:128
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
gboolean gnc_prefs_set_bool(const gchar *group, const gchar *pref_name, gboolean value)
Store a boolean value into the preferences backend.
Definition: gnc-prefs.c:277
Preferences initialization function.
gboolean gnc_prefs_set_float(const gchar *group, const gchar *pref_name, gdouble value)
Store a float value into the preferences backend.
Definition: gnc-prefs.c:308
void gnc_prefs_remove_registered(void)
This function is called to remove the registered preference call backs setup in this file...
Generic api to store and retrieve preferences.
void gnc_prefs_init(void)
This function is called early in the load process to preload a number of preferences from the setting...
void gnc_gsettings_load_backend(void)
Configure gsettings as the backend for the gnucash preferences api.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
load and save data to files
GSettings helper routines.
gboolean gnc_prefs_is_set_up(void)
Test if preferences backend is set up.
Definition: gnc-prefs.c:374
void gnc_prefs_remove_cb_by_func(const gchar *group, const gchar *pref_name, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when the given preference changed.
Definition: gnc-prefs.c:143
gdouble gnc_prefs_get_float(const gchar *group, const gchar *pref_name)
Get an float value from the preferences backend.