GnuCash  5.6-150-g038405b370+
gnc-prefs.c
1 /*
2  * gnc-prefs.c:
3  *
4  * Copyright (C) 2006 Chris Shoemaker <c.shoemaker@cox.net>
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 #include <stdlib.h>
25 #include <glib.h>
26 #include <config.h>
27 #include "gnc-prefs.h"
28 #include "gnc-prefs-p.h"
29 #include "gnc-version.h"
30 
31 static gchar *namespace_regexp = NULL;
32 static gboolean is_debugging = FALSE;
33 static gboolean extras_enabled = FALSE;
34 static gboolean use_compression = TRUE; // This is also the default in the prefs backend
35 static gint file_retention_policy = 1; // 1 = "days", the default in the prefs backend
36 static gint file_retention_days = 30; // This is also the default in the prefs backend
37 
38 
39 /* Global variables used to remove the preference registered callbacks
40  * that were setup via a g_once. */
41 static gulong reg_auto_raise_lists_id;
42 static gulong reg_negative_color_pref_id;
43 
44 PrefsBackend *prefsbackend = NULL;
45 
46 const gchar *
47 gnc_prefs_get_namespace_regexp(void)
48 {
49  return namespace_regexp;
50 }
51 
52 void
53 gnc_prefs_set_namespace_regexp(const gchar *str)
54 {
55  if (namespace_regexp)
56  g_free(namespace_regexp);
57 
58  if (str)
59  namespace_regexp = g_strdup(str);
60 }
61 
62 gboolean
63 gnc_prefs_is_debugging_enabled(void)
64 {
65  return is_debugging;
66 }
67 
68 void
69 gnc_prefs_set_debugging(gboolean d)
70 {
71  is_debugging = d;
72 }
73 
74 gboolean
75 gnc_prefs_is_extra_enabled(void)
76 {
77  return extras_enabled;
78 }
79 
80 void
81 gnc_prefs_set_extra(gboolean enabled)
82 {
83  extras_enabled = enabled;
84 }
85 
86 gboolean
87 gnc_prefs_get_file_save_compressed(void)
88 {
89  return use_compression;
90 }
91 
92 void
93 gnc_prefs_set_file_save_compressed(gboolean compressed)
94 {
95  use_compression = compressed;
96 }
97 
98 gint
99 gnc_prefs_get_file_retention_policy(void)
100 {
101  return file_retention_policy;
102 }
103 
104 void
105 gnc_prefs_set_file_retention_policy(gint policy)
106 {
107  file_retention_policy = policy;
108 }
109 
110 gint
111 gnc_prefs_get_file_retention_days(void)
112 {
113  return file_retention_days;
114 }
115 
116 void
117 gnc_prefs_set_file_retention_days(gint days)
118 {
119  file_retention_days = days;
120 }
121 
122 guint
123 gnc_prefs_get_long_version()
124 {
125  return PROJECT_VERSION_MAJOR * 1000000 + PROJECT_VERSION_MINOR;
126 }
127 
128 gulong gnc_prefs_register_cb (const char *group,
129  const gchar *pref_name,
130  gpointer func,
131  gpointer user_data)
132 {
133  if (prefsbackend && prefsbackend->register_cb)
134  return (prefsbackend->register_cb) (group, pref_name, func, user_data);
135  else
136  {
137  g_warning ("no preferences backend loaded, or the backend doesn't define register_cb, returning 0");
138  return 0;
139  }
140 }
141 
142 
143 void gnc_prefs_remove_cb_by_func (const gchar *group,
144  const gchar *pref_name,
145  gpointer func,
146  gpointer user_data)
147 {
148  if (prefsbackend && prefsbackend->remove_cb_by_func)
149  (prefsbackend->remove_cb_by_func) (group, pref_name, func, user_data);
150 }
151 
152 
153 void gnc_prefs_remove_cb_by_id (const gchar *group,
154  guint id)
155 {
156  if (prefsbackend && prefsbackend->remove_cb_by_id)
157  (prefsbackend->remove_cb_by_id) (group, id);
158 }
159 
160 
161 guint gnc_prefs_register_group_cb (const gchar *group,
162  gpointer func,
163  gpointer user_data)
164 {
165  if (prefsbackend && prefsbackend->register_group_cb)
166  return (prefsbackend->register_group_cb) (group, func, user_data);
167  else
168  return 0;
169 }
170 
171 
172 void gnc_prefs_remove_group_cb_by_func (const gchar *group,
173  gpointer func,
174  gpointer user_data)
175 {
176  if (prefsbackend && prefsbackend->remove_group_cb_by_func)
177  (prefsbackend->remove_group_cb_by_func) (group, func, user_data);
178 }
179 
180 
181 void gnc_prefs_bind (const gchar *group,
182  /*@ null @*/ const gchar *pref_name,
183  /*@ null @*/ const gchar *pref_value,
184  gpointer object,
185  const gchar *property)
186 {
187  if (prefsbackend && prefsbackend->bind)
188  (prefsbackend->bind) (group, pref_name, pref_value, object, property);
189 }
190 
191 
192 gboolean gnc_prefs_get_bool (const gchar *group,
193  /*@ null @*/ const gchar *pref_name)
194 {
195  if (prefsbackend && prefsbackend->get_bool)
196  return (prefsbackend->get_bool) (group, pref_name);
197  else
198  return FALSE;
199 }
200 
201 
202 gint gnc_prefs_get_int (const gchar *group,
203  const gchar *pref_name)
204 {
205  if (prefsbackend && prefsbackend->get_int)
206  return (prefsbackend->get_int) (group, pref_name);
207  else
208  return 0;
209 }
210 
211 
212 gint64 gnc_prefs_get_int64 (const gchar *group,
213  const gchar *pref_name)
214 {
215  gint64 result = 0;
216  GVariant *var = gnc_prefs_get_value(group, pref_name);
217  result = g_variant_get_int64 (var);
218  g_variant_unref (var);
219  return result;
220 }
221 
222 
223 gdouble gnc_prefs_get_float (const gchar *group,
224  const gchar *pref_name)
225 {
226  if (prefsbackend && prefsbackend->get_float)
227  return (prefsbackend->get_float) (group, pref_name);
228  else
229  return 0.0;
230 }
231 
232 
233 gchar *gnc_prefs_get_string (const gchar *group,
234  const gchar *pref_name)
235 {
236  if (prefsbackend && prefsbackend->get_string)
237  return (prefsbackend->get_string) (group, pref_name);
238  else
239  return NULL;
240 }
241 
242 
243 gint gnc_prefs_get_enum (const gchar *group,
244  const gchar *pref_name)
245 {
246  if (prefsbackend && prefsbackend->get_enum)
247  return (prefsbackend->get_enum) (group, pref_name);
248  else
249  return 0;
250 }
251 
252 void
253 gnc_prefs_get_coords (const gchar *group,
254  const gchar *pref_name,
255  gdouble *x, gdouble *y)
256 {
257  GVariant *coords = gnc_prefs_get_value (group, pref_name);
258 
259  *x = 0;
260  *y = 0;
261 
262  if (g_variant_is_of_type (coords, (const GVariantType *) "(dd)") )
263  g_variant_get (coords, "(dd)", x, y);
264  g_variant_unref (coords);
265 }
266 
267 
268 GVariant *gnc_prefs_get_value (const gchar *group,
269  const gchar *pref_name)
270 {
271  if (prefsbackend && prefsbackend->get_value)
272  return (prefsbackend->get_value) (group,pref_name);
273  else
274  return NULL;
275 }
276 
277 
278 gboolean gnc_prefs_set_bool (const gchar *group,
279  const gchar *pref_name,
280  gboolean value)
281 {
282  if (prefsbackend && prefsbackend->set_bool)
283  return (prefsbackend->set_bool) (group, pref_name, value);
284  else
285  return FALSE;
286 }
287 
288 
289 gboolean gnc_prefs_set_int (const gchar *group,
290  const gchar *pref_name,
291  gint value)
292 {
293  if (prefsbackend && prefsbackend->set_int)
294  return (prefsbackend->set_int) (group, pref_name, value);
295  else
296  return FALSE;
297 }
298 
299 
300 gboolean gnc_prefs_set_int64 (const gchar *group,
301  const gchar *pref_name,
302  gint64 value)
303 {
304  GVariant *var = g_variant_new ("x",value);
305  return gnc_prefs_set_value (group, pref_name, var);
306 }
307 
308 
309 gboolean gnc_prefs_set_float (const gchar *group,
310  const gchar *pref_name,
311  gdouble value)
312 {
313  if (prefsbackend && prefsbackend->set_float)
314  return (prefsbackend->set_float) (group, pref_name, value);
315  else
316  return FALSE;
317 }
318 
319 
320 gboolean gnc_prefs_set_string (const gchar *group,
321  const gchar *pref_name,
322  const gchar *value)
323 {
324  if (prefsbackend && prefsbackend->set_string)
325  return (prefsbackend->set_string) (group, pref_name, value);
326  else
327  return FALSE;
328 }
329 
330 
331 gboolean gnc_prefs_set_enum (const gchar *group,
332  const gchar *pref_name,
333  gint value)
334 {
335  if (prefsbackend && prefsbackend->set_enum)
336  return (prefsbackend->set_enum) (group, pref_name, value);
337  else
338  return FALSE;
339 }
340 
341 
342 gboolean gnc_prefs_set_coords (const gchar *group,
343  const gchar *pref_name,
344  gdouble x, gdouble y)
345 {
346  GVariant *var = g_variant_new ("(dd)",x, y);
347  return gnc_prefs_set_value (group, pref_name, var);
348 }
349 
350 
351 gboolean gnc_prefs_set_value (const gchar *group,
352  const gchar *pref_name,
353  GVariant *value)
354 {
355  if (prefsbackend && prefsbackend->set_value)
356  return (prefsbackend->set_value) (group, pref_name, value);
357  else
358  return FALSE;
359 }
360 
361 
362 void gnc_prefs_reset (const gchar *group,
363  const gchar *pref_name)
364 {
365  if (prefsbackend && prefsbackend->reset)
366  (prefsbackend->reset) (group, pref_name);
367 }
368 
369 void gnc_prefs_reset_group (const gchar *group)
370 {
371  if (prefsbackend && prefsbackend->reset_group)
372  (prefsbackend->reset_group) (group);
373 }
374 
375 gboolean gnc_prefs_is_set_up (void)
376 {
377  return (prefsbackend !=NULL);
378 }
379 
381 {
382  if (prefsbackend && prefsbackend->block_all)
383  (prefsbackend->block_all) ();
384 }
385 
387 {
388  if (prefsbackend && prefsbackend->unblock_all)
389  (prefsbackend->unblock_all) ();
390 }
391 
393 {
394  return reg_auto_raise_lists_id;
395 }
396 
397 void gnc_prefs_set_reg_auto_raise_lists_id (gulong id)
398 {
399  reg_auto_raise_lists_id = id;
400 }
401 
403 {
404  return reg_negative_color_pref_id;
405 }
406 
407 void gnc_prefs_set_reg_negative_color_pref_id (gulong id)
408 {
409  reg_negative_color_pref_id = id;
410 }
411 
void gnc_prefs_reset_group(const gchar *group)
Reset all preferences in a group to their default values in the preferences backend.
Definition: gnc-prefs.c:369
gboolean gnc_prefs_set_value(const gchar *group, const gchar *pref_name, GVariant *value)
Store an arbitrary combination of values into the preferences backend.
Definition: gnc-prefs.c:351
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
Definition: gnc-prefs.c:233
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
functions to query various version related strings that were set at build time.
gulong gnc_prefs_get_reg_negative_color_pref_id(void)
Get and Set registered preference id for register negative_color_pref.
Definition: gnc-prefs.c:402
gboolean gnc_prefs_set_int(const gchar *group, const gchar *pref_name, gint value)
Store an integer value into the preferences backend.
Definition: gnc-prefs.c:289
void gnc_prefs_reset(const gchar *group, const gchar *pref_name)
Reset a preference to its default value in the preferences backend.
Definition: gnc-prefs.c:362
gboolean gnc_prefs_set_string(const gchar *group, const gchar *pref_name, const gchar *value)
Store a string into the preferences backend.
Definition: gnc-prefs.c:320
gint64 gnc_prefs_get_int64(const gchar *group, const gchar *pref_name)
Get an 64 bit integer value from the preferences backend.
Definition: gnc-prefs.c:212
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
Definition: gnc-prefs.c:202
void gnc_prefs_remove_cb_by_id(const gchar *group, guint id)
Remove a function that was registered for a callback when a specific preference in the settings group...
Definition: gnc-prefs.c:153
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:278
gboolean gnc_prefs_set_enum(const gchar *group, const gchar *pref_name, gint value)
Store an enum value into the preferences backend.
Definition: gnc-prefs.c:331
gint gnc_prefs_get_enum(const gchar *group, const gchar *pref_name)
Get an enum value from the preferences backend.
Definition: gnc-prefs.c:243
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:309
gboolean gnc_prefs_set_coords(const gchar *group, const gchar *pref_name, gdouble x, gdouble y)
Store coordinates into the preferences backend.
Definition: gnc-prefs.c:342
void gnc_prefs_get_coords(const gchar *group, const gchar *pref_name, gdouble *x, gdouble *y)
Get a pair of coordinates from the preferences backend.
Definition: gnc-prefs.c:253
void gnc_prefs_bind(const gchar *group, const gchar *pref_name, const gchar *pref_value, gpointer object, const gchar *property)
Bind a setting to a g_object property.
Definition: gnc-prefs.c:181
void gnc_prefs_remove_group_cb_by_func(const gchar *group, gpointer func, gpointer user_data)
Remove a function that was registered for a callback when any preference in the given settings group ...
Definition: gnc-prefs.c:172
GVariant * gnc_prefs_get_value(const gchar *group, const gchar *pref_name)
Get an arbitrary combination of values from the preferences backend.
Definition: gnc-prefs.c:268
Generic api to store and retrieve preferences.
guint gnc_prefs_register_group_cb(const gchar *group, gpointer func, gpointer user_data)
Register a callback for when any preference in the settings group is changed.
Definition: gnc-prefs.c:161
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
Definition: gnc-prefs.c:192
gulong gnc_prefs_get_reg_auto_raise_lists_id(void)
Get and Set registered preference id for register auto_raise_lists.
Definition: gnc-prefs.c:392
void gnc_prefs_block_all(void)
Block all preference callbacks.
Definition: gnc-prefs.c:380
void gnc_prefs_unblock_all(void)
Unblock all preferences callbacks.
Definition: gnc-prefs.c:386
gboolean gnc_prefs_is_set_up(void)
Test if preferences backend is set up.
Definition: gnc-prefs.c:375
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.
Definition: gnc-prefs.c:223
gboolean gnc_prefs_set_int64(const gchar *group, const gchar *pref_name, gint64 value)
Store a 64 bit integer value into the preferences backend.
Definition: gnc-prefs.c:300