GnuCash  5.6-150-g038405b370+
gnc-prefs.cpp
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 <string>
25 
26 #include <stdlib.h>
27 #include <glib.h>
28 #include <config.h>
29 #include "gnc-prefs.h"
30 #include "gnc-prefs-p.h"
31 #include "gnc-version.h"
32 
33 static std::string namespace_regexp;
34 static gboolean is_debugging = FALSE;
35 static gboolean extras_enabled = FALSE;
36 static gboolean use_compression = TRUE; // This is also the default in the prefs backend
37 static gint file_retention_policy = 1; // 1 = "days", the default in the prefs backend
38 static gint file_retention_days = 30; // This is also the default in the prefs backend
39 
40 
41 /* Global variables used to remove the preference registered callbacks
42  * that were setup via a g_once. */
43 static gulong reg_auto_raise_lists_id;
44 static gulong reg_negative_color_pref_id;
45 
46 PrefsBackend *prefsbackend = NULL;
47 
48 const gchar *
49 gnc_prefs_get_namespace_regexp(void)
50 {
51  return namespace_regexp.c_str();
52 }
53 
54 void
55 gnc_prefs_set_namespace_regexp(const gchar *str)
56 {
57  if (str)
58  namespace_regexp = str;
59 }
60 
61 gboolean
62 gnc_prefs_is_debugging_enabled(void)
63 {
64  return is_debugging;
65 }
66 
67 void
68 gnc_prefs_set_debugging(gboolean d)
69 {
70  is_debugging = d;
71 }
72 
73 gboolean
74 gnc_prefs_is_extra_enabled(void)
75 {
76  return extras_enabled;
77 }
78 
79 void
80 gnc_prefs_set_extra(gboolean enabled)
81 {
82  extras_enabled = enabled;
83 }
84 
85 gboolean
86 gnc_prefs_get_file_save_compressed(void)
87 {
88  return use_compression;
89 }
90 
91 void
92 gnc_prefs_set_file_save_compressed(gboolean compressed)
93 {
94  use_compression = compressed;
95 }
96 
97 gint
98 gnc_prefs_get_file_retention_policy(void)
99 {
100  return file_retention_policy;
101 }
102 
103 void
104 gnc_prefs_set_file_retention_policy(gint policy)
105 {
106  file_retention_policy = policy;
107 }
108 
109 gint
110 gnc_prefs_get_file_retention_days(void)
111 {
112  return file_retention_days;
113 }
114 
115 void
116 gnc_prefs_set_file_retention_days(gint days)
117 {
118  file_retention_days = days;
119 }
120 
121 guint
122 gnc_prefs_get_long_version()
123 {
124  return PROJECT_VERSION_MAJOR * 1000000 + PROJECT_VERSION_MINOR;
125 }
126 
127 gulong gnc_prefs_register_cb (const char *group,
128  const gchar *pref_name,
129  gpointer func,
130  gpointer user_data)
131 {
132  if (prefsbackend && prefsbackend->register_cb)
133  return (prefsbackend->register_cb) (group, pref_name, func, user_data);
134  else
135  {
136  g_warning ("no preferences backend loaded, or the backend doesn't define register_cb, returning 0");
137  return 0;
138  }
139 }
140 
141 
142 void gnc_prefs_remove_cb_by_func (const gchar *group,
143  const gchar *pref_name,
144  gpointer func,
145  gpointer user_data)
146 {
147  if (prefsbackend && prefsbackend->remove_cb_by_func)
148  (prefsbackend->remove_cb_by_func) (group, pref_name, func, user_data);
149 }
150 
151 
152 void gnc_prefs_remove_cb_by_id (const gchar *group,
153  guint id)
154 {
155  if (prefsbackend && prefsbackend->remove_cb_by_id)
156  (prefsbackend->remove_cb_by_id) (group, id);
157 }
158 
159 
160 guint gnc_prefs_register_group_cb (const gchar *group,
161  gpointer func,
162  gpointer user_data)
163 {
164  if (prefsbackend && prefsbackend->register_group_cb)
165  return (prefsbackend->register_group_cb) (group, func, user_data);
166  else
167  return 0;
168 }
169 
170 
171 void gnc_prefs_remove_group_cb_by_func (const gchar *group,
172  gpointer func,
173  gpointer user_data)
174 {
175  if (prefsbackend && prefsbackend->remove_group_cb_by_func)
176  (prefsbackend->remove_group_cb_by_func) (group, func, user_data);
177 }
178 
179 
180 void gnc_prefs_bind (const gchar *group,
181  /*@ null @*/ const gchar *pref_name,
182  /*@ null @*/ const gchar *pref_value,
183  gpointer object,
184  const gchar *property)
185 {
186  if (prefsbackend && prefsbackend->bind)
187  (prefsbackend->bind) (group, pref_name, pref_value, 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.cpp: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.cpp:350
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
Definition: gnc-prefs.cpp: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.cpp:127
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.cpp: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.cpp: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.cpp: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.cpp: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.cpp:211
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
Definition: gnc-prefs.cpp: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.cpp:152
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.cpp: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.cpp:330
gint gnc_prefs_get_enum(const gchar *group, const gchar *pref_name)
Get an enum value from the preferences backend.
Definition: gnc-prefs.cpp: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.cpp: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.cpp: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.cpp:252
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.cpp:180
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.cpp:171
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.cpp: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.cpp:160
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
Definition: gnc-prefs.cpp: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.cpp:391
void gnc_prefs_block_all(void)
Block all preference callbacks.
Definition: gnc-prefs.cpp:379
void gnc_prefs_unblock_all(void)
Unblock all preferences callbacks.
Definition: gnc-prefs.cpp:385
gboolean gnc_prefs_is_set_up(void)
Test if preferences backend is set up.
Definition: gnc-prefs.cpp: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.cpp:142
gdouble gnc_prefs_get_float(const gchar *group, const gchar *pref_name)
Get an float value from the preferences backend.
Definition: gnc-prefs.cpp: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.cpp:299