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  gpointer object,
184  const gchar *property)
185 {
186  if (prefsbackend && prefsbackend->bind)
187  (prefsbackend->bind) (group, pref_name, object, property);
188 }
189 
190 
191 gboolean gnc_prefs_get_bool (const gchar *group,
192  /*@ null @*/ const gchar *pref_name)
193 {
194  if (prefsbackend && prefsbackend->get_bool)
195  return (prefsbackend->get_bool) (group, pref_name);
196  else
197  return FALSE;
198 }
199 
200 
201 gint gnc_prefs_get_int (const gchar *group,
202  const gchar *pref_name)
203 {
204  if (prefsbackend && prefsbackend->get_int)
205  return (prefsbackend->get_int) (group, pref_name);
206  else
207  return 0;
208 }
209 
210 
211 gint64 gnc_prefs_get_int64 (const gchar *group,
212  const gchar *pref_name)
213 {
214  gint64 result = 0;
215  GVariant *var = gnc_prefs_get_value(group, pref_name);
216  result = g_variant_get_int64 (var);
217  g_variant_unref (var);
218  return result;
219 }
220 
221 
222 gdouble gnc_prefs_get_float (const gchar *group,
223  const gchar *pref_name)
224 {
225  if (prefsbackend && prefsbackend->get_float)
226  return (prefsbackend->get_float) (group, pref_name);
227  else
228  return 0.0;
229 }
230 
231 
232 gchar *gnc_prefs_get_string (const gchar *group,
233  const gchar *pref_name)
234 {
235  if (prefsbackend && prefsbackend->get_string)
236  return (prefsbackend->get_string) (group, pref_name);
237  else
238  return NULL;
239 }
240 
241 
242 gint gnc_prefs_get_enum (const gchar *group,
243  const gchar *pref_name)
244 {
245  if (prefsbackend && prefsbackend->get_enum)
246  return (prefsbackend->get_enum) (group, pref_name);
247  else
248  return 0;
249 }
250 
251 void
252 gnc_prefs_get_coords (const gchar *group,
253  const gchar *pref_name,
254  gdouble *x, gdouble *y)
255 {
256  GVariant *coords = gnc_prefs_get_value (group, pref_name);
257 
258  *x = 0;
259  *y = 0;
260 
261  if (g_variant_is_of_type (coords, (const GVariantType *) "(dd)") )
262  g_variant_get (coords, "(dd)", x, y);
263  g_variant_unref (coords);
264 }
265 
266 
267 GVariant *gnc_prefs_get_value (const gchar *group,
268  const gchar *pref_name)
269 {
270  if (prefsbackend && prefsbackend->get_value)
271  return (prefsbackend->get_value) (group,pref_name);
272  else
273  return NULL;
274 }
275 
276 
277 gboolean gnc_prefs_set_bool (const gchar *group,
278  const gchar *pref_name,
279  gboolean value)
280 {
281  if (prefsbackend && prefsbackend->set_bool)
282  return (prefsbackend->set_bool) (group, pref_name, value);
283  else
284  return FALSE;
285 }
286 
287 
288 gboolean gnc_prefs_set_int (const gchar *group,
289  const gchar *pref_name,
290  gint value)
291 {
292  if (prefsbackend && prefsbackend->set_int)
293  return (prefsbackend->set_int) (group, pref_name, value);
294  else
295  return FALSE;
296 }
297 
298 
299 gboolean gnc_prefs_set_int64 (const gchar *group,
300  const gchar *pref_name,
301  gint64 value)
302 {
303  GVariant *var = g_variant_new ("x",value);
304  return gnc_prefs_set_value (group, pref_name, var);
305 }
306 
307 
308 gboolean gnc_prefs_set_float (const gchar *group,
309  const gchar *pref_name,
310  gdouble value)
311 {
312  if (prefsbackend && prefsbackend->set_float)
313  return (prefsbackend->set_float) (group, pref_name, value);
314  else
315  return FALSE;
316 }
317 
318 
319 gboolean gnc_prefs_set_string (const gchar *group,
320  const gchar *pref_name,
321  const gchar *value)
322 {
323  if (prefsbackend && prefsbackend->set_string)
324  return (prefsbackend->set_string) (group, pref_name, value);
325  else
326  return FALSE;
327 }
328 
329 
330 gboolean gnc_prefs_set_enum (const gchar *group,
331  const gchar *pref_name,
332  gint value)
333 {
334  if (prefsbackend && prefsbackend->set_enum)
335  return (prefsbackend->set_enum) (group, pref_name, value);
336  else
337  return FALSE;
338 }
339 
340 
341 gboolean gnc_prefs_set_coords (const gchar *group,
342  const gchar *pref_name,
343  gdouble x, gdouble y)
344 {
345  GVariant *var = g_variant_new ("(dd)",x, y);
346  return gnc_prefs_set_value (group, pref_name, var);
347 }
348 
349 
350 gboolean gnc_prefs_set_value (const gchar *group,
351  const gchar *pref_name,
352  GVariant *value)
353 {
354  if (prefsbackend && prefsbackend->set_value)
355  return (prefsbackend->set_value) (group, pref_name, value);
356  else
357  return FALSE;
358 }
359 
360 
361 void gnc_prefs_reset (const gchar *group,
362  const gchar *pref_name)
363 {
364  if (prefsbackend && prefsbackend->reset)
365  (prefsbackend->reset) (group, pref_name);
366 }
367 
368 void gnc_prefs_reset_group (const gchar *group)
369 {
370  if (prefsbackend && prefsbackend->reset_group)
371  (prefsbackend->reset_group) (group);
372 }
373 
374 gboolean gnc_prefs_is_set_up (void)
375 {
376  return (prefsbackend !=NULL);
377 }
378 
380 {
381  if (prefsbackend && prefsbackend->block_all)
382  (prefsbackend->block_all) ();
383 }
384 
386 {
387  if (prefsbackend && prefsbackend->unblock_all)
388  (prefsbackend->unblock_all) ();
389 }
390 
392 {
393  return reg_auto_raise_lists_id;
394 }
395 
396 void gnc_prefs_set_reg_auto_raise_lists_id (gulong id)
397 {
398  reg_auto_raise_lists_id = id;
399 }
400 
402 {
403  return reg_negative_color_pref_id;
404 }
405 
406 void gnc_prefs_set_reg_negative_color_pref_id (gulong id)
407 {
408  reg_negative_color_pref_id = id;
409 }
410 
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:368
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:350
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
Definition: gnc-prefs.c:232
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:401
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:288
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:361
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:319
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:211
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
Definition: gnc-prefs.c:201
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
void gnc_prefs_bind(const gchar *group, const gchar *pref_name, gpointer object, const gchar *property)
Bind a setting to a g_object property.
Definition: gnc-prefs.c:181
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
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:330
gint gnc_prefs_get_enum(const gchar *group, const gchar *pref_name)
Get an enum value from the preferences backend.
Definition: gnc-prefs.c:242
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
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:341
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:252
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:267
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:191
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:391
void gnc_prefs_block_all(void)
Block all preference callbacks.
Definition: gnc-prefs.c:379
void gnc_prefs_unblock_all(void)
Unblock all preferences callbacks.
Definition: gnc-prefs.c:385
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.
Definition: gnc-prefs.c:222
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:299