~ubuntu-branches/ubuntu/quantal/gnumeric/quantal

« back to all changes in this revision

Viewing changes to src/gnm-conf-gconf.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2009-06-07 11:10:47 UTC
  • mfrom: (1.1.19 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090607111047-l3rtbzfjxvmi1kx0
Tags: 1.9.8-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Promoted gnumeric-doc to Recommends in gnumeric package for help to be
    installed automatically
  - gnumeric-gtk is a transitional package
  - gnumeric conflicts with gnumeric-gtk << 1.8.3-3ubuntu1
  - call initltool-update in po*
  - remove psiconv support (psiconv is in universe):
    o debian/control: remove B-D on libpsiconv-dev
    o debian/rules: don't pass --with-psiconv to ./configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
 
3
 
#include <gconf/gconf-client.h>
4
 
 
5
 
struct _GOConfNode {
6
 
        gchar *path;
7
 
        gboolean root;
8
 
};
9
 
 
10
 
static GConfClient *gconf_client = NULL;
11
 
 
12
 
static void
13
 
go_conf_init (void)
14
 
{
15
 
        if (!gconf_client)
16
 
                gconf_client = gconf_client_get_default ();
17
 
}
18
 
 
19
 
static void
20
 
go_conf_shutdown (void)
21
 
{
22
 
        if (gconf_client) {
23
 
                g_object_unref (G_OBJECT (gconf_client));
24
 
                gconf_client = NULL;
25
 
        }
26
 
}
27
 
 
28
 
static gchar *
29
 
go_conf_get_real_key (GOConfNode const *key, gchar const *subkey)
30
 
{
31
 
        return key ? g_strconcat ((key)->path, "/", subkey, NULL) :
32
 
                     g_strdup (subkey);
33
 
}
34
 
 
35
 
GOConfNode *
36
 
go_conf_get_node (GOConfNode *parent, gchar const *key)
37
 
{
38
 
        GOConfNode *node;
39
 
 
40
 
        node = g_new (GOConfNode, 1);
41
 
        gconf_client = gconf_client;
42
 
        node->root = !parent;
43
 
        if (node->root) {
44
 
                node->path = g_strconcat ("/apps/", key, NULL);
45
 
                gconf_client_add_dir (gconf_client, node->path,
46
 
                                      GCONF_CLIENT_PRELOAD_RECURSIVE,
47
 
                                      NULL);
48
 
        }
49
 
        else
50
 
                node->path = go_conf_get_real_key (parent, key);
51
 
 
52
 
        return node;
53
 
}
54
 
 
55
 
void
56
 
go_conf_free_node (GOConfNode *node)
57
 
{
58
 
        if (node) {
59
 
                if (node->root)
60
 
                        gconf_client_remove_dir (gconf_client, node->path, NULL);
61
 
                g_free (node->path);
62
 
                g_free (node);
63
 
        }
64
 
}
65
 
 
66
 
void
67
 
go_conf_sync (GOConfNode *node)
68
 
{
69
 
        gconf_client_suggest_sync (gconf_client, NULL);
70
 
}
71
 
 
72
 
void
73
 
go_conf_set_bool (GOConfNode *node, gchar const *key, gboolean val)
74
 
{
75
 
        gchar *real_key = go_conf_get_real_key (node, key);
76
 
        gconf_client_set_bool (gconf_client, real_key, val, NULL);
77
 
        g_free (real_key);
78
 
}
79
 
 
80
 
void
81
 
go_conf_set_int (GOConfNode *node, gchar const *key, gint val)
82
 
{
83
 
        gchar *real_key = go_conf_get_real_key (node, key);
84
 
        gconf_client_set_int (gconf_client, real_key, val, NULL);
85
 
        g_free (real_key);
86
 
}
87
 
 
88
 
void
89
 
go_conf_set_double (GOConfNode *node, gchar const *key, gnm_float val)
90
 
{
91
 
        gchar *real_key = go_conf_get_real_key (node, key);
92
 
        gconf_client_set_float (gconf_client, real_key, val, NULL);
93
 
        g_free (real_key);
94
 
}
95
 
 
96
 
void
97
 
go_conf_set_string (GOConfNode *node, gchar const *key, gchar const *str)
98
 
{
99
 
        gchar *real_key = go_conf_get_real_key (node, key);
100
 
        gconf_client_set_string (gconf_client, real_key, str, NULL);
101
 
        g_free (real_key);
102
 
}
103
 
 
104
 
void
105
 
go_conf_set_str_list (GOConfNode *node, gchar const *key, GSList *list)
106
 
{
107
 
        gchar *real_key = go_conf_get_real_key (node, key);
108
 
        gconf_client_set_list (gconf_client, real_key,
109
 
                GCONF_VALUE_STRING, list, NULL);
110
 
        g_free (real_key);
111
 
}
112
 
 
113
 
static GConfValue *
114
 
go_conf_get (GOConfNode *node, gchar const *key, GConfValueType t)
115
 
{
116
 
        GError *err = NULL;
117
 
        GConfValue *val;
118
 
        gchar *real_key;
119
 
 
120
 
        real_key = go_conf_get_real_key (node, key);
121
 
        val = gconf_client_get (gconf_client, real_key, &err);
122
 
 
123
 
        if (err != NULL) {
124
 
                d (g_warning ("Unable to load key '%s' : because %s",
125
 
                              real_key, err->message));
126
 
                g_free (real_key);
127
 
                g_error_free (err);
128
 
                return NULL;
129
 
        }
130
 
        if (val == NULL) {
131
 
                d (g_warning ("Unable to load key '%s'", real_key));
132
 
                g_free (real_key);
133
 
                return NULL;
134
 
        }
135
 
 
136
 
        if (val->type != t) {
137
 
#if 1 /* gconf_value_type_to_string is internal */
138
 
                g_warning ("Expected `%d' got `%d' for key %s",
139
 
                           t, val->type, real_key);
140
 
#else
141
 
                g_warning ("Expected `%s' got `%s' for key %s",
142
 
                           gconf_value_type_to_string (t),
143
 
                           gconf_value_type_to_string (val->type),
144
 
                           real_key);
145
 
#endif
146
 
                g_free (real_key);
147
 
                gconf_value_free (val);
148
 
                return NULL;
149
 
        }
150
 
        g_free (real_key);
151
 
 
152
 
        return val;
153
 
}
154
 
 
155
 
gboolean
156
 
go_conf_load_bool (GOConfNode *node, gchar const *key, gboolean default_val)
157
 
{
158
 
        gboolean res;
159
 
        GConfValue *val = go_conf_get (node, key, GCONF_VALUE_BOOL);
160
 
 
161
 
        if (val != NULL) {
162
 
                res = gconf_value_get_bool (val);
163
 
                gconf_value_free (val);
164
 
        } else {
165
 
                d (g_warning ("Using default value '%s'", default_val ? "true" : "false"));
166
 
                return default_val;
167
 
        }
168
 
        return res;
169
 
}
170
 
 
171
 
gint
172
 
go_conf_load_int (GOConfNode *node, gchar const *key, gint minima, gint maxima, gint default_val)
173
 
{
174
 
        gint res = -1;
175
 
        GConfValue *val = go_conf_get (node, key, GCONF_VALUE_INT);
176
 
 
177
 
        if (val != NULL) {
178
 
                res = gconf_value_get_int (val);
179
 
                gconf_value_free (val);
180
 
                if (res < minima || maxima < res) {
181
 
                        g_warning ("Invalid value '%d' for %s.  If should be >= %d and <= %d",
182
 
                                   res, key, minima, maxima);
183
 
                        val = NULL;
184
 
                }
185
 
        }
186
 
        if (val == NULL) {
187
 
                d (g_warning ("Using default value '%d'", default_val));
188
 
                return default_val;
189
 
        }
190
 
        return res;
191
 
}
192
 
 
193
 
gdouble
194
 
go_conf_load_double (GOConfNode *node, gchar const *key,
195
 
                     gdouble minima, gdouble maxima, gdouble default_val)
196
 
{
197
 
        gdouble res = -1;
198
 
        GConfValue *val = go_conf_get (node, key, GCONF_VALUE_FLOAT);
199
 
 
200
 
        if (val != NULL) {
201
 
                res = gconf_value_get_float (val);
202
 
                gconf_value_free (val);
203
 
                if (res < minima || maxima < res) {
204
 
                        g_warning ("Invalid value '%g' for %s.  If should be >= %g and <= %g",
205
 
                                   res, key, minima, maxima);
206
 
                        val = NULL;
207
 
                }
208
 
        }
209
 
        if (val == NULL) {
210
 
                d (g_warning ("Using default value '%g'", default_val));
211
 
                return default_val;
212
 
        }
213
 
        return res;
214
 
}
215
 
 
216
 
gchar *
217
 
go_conf_load_string (GOConfNode *node, gchar const *key)
218
 
{
219
 
        gchar *val;
220
 
 
221
 
        gchar *real_key = go_conf_get_real_key (node, key);
222
 
        val = gconf_client_get_string (gconf_client, real_key, NULL);
223
 
        g_free (real_key);
224
 
 
225
 
        return val;
226
 
}
227
 
 
228
 
GSList *
229
 
go_conf_load_str_list (GOConfNode *node, gchar const *key)
230
 
{
231
 
        GSList *list;
232
 
 
233
 
        gchar *real_key = go_conf_get_real_key (node, key);
234
 
        list = gconf_client_get_list (gconf_client, real_key,
235
 
                                      GCONF_VALUE_STRING, NULL);
236
 
        g_free (real_key);
237
 
 
238
 
        return list;
239
 
}
240
 
 
241
 
static GConfSchema *
242
 
get_schema (GOConfNode *node, gchar const *key)
243
 
{
244
 
        gchar *schema_key = g_strconcat (
245
 
                "/schemas", node->path, "/", key, NULL);
246
 
        GConfSchema *schema = gconf_client_get_schema (
247
 
                gconf_client, schema_key, NULL);
248
 
        g_free (schema_key);
249
 
        return schema;
250
 
}
251
 
 
252
 
gchar *
253
 
go_conf_get_short_desc (GOConfNode *node, gchar const *key)
254
 
{
255
 
        GConfSchema *schema = get_schema (node, key);
256
 
 
257
 
        if (schema != NULL) {
258
 
                gchar *desc = g_strdup (gconf_schema_get_short_desc (schema));
259
 
                gconf_schema_free (schema);
260
 
                return desc;
261
 
        }
262
 
        return NULL;
263
 
}
264
 
 
265
 
gchar *
266
 
go_conf_get_long_desc  (GOConfNode *node, gchar const *key)
267
 
{
268
 
        GConfSchema *schema = get_schema (node, key);
269
 
 
270
 
        if (schema != NULL) {
271
 
                gchar *desc =  g_strdup (gconf_schema_get_long_desc (schema));
272
 
                gconf_schema_free (schema);
273
 
                return desc;
274
 
        }
275
 
        return NULL;
276
 
}
277
 
 
278
 
GType
279
 
go_conf_get_type (GOConfNode *node, gchar const *key)
280
 
{
281
 
        GConfSchema *schema = get_schema (node, key);
282
 
        GType t;
283
 
 
284
 
        switch (gconf_schema_get_type (schema)) {
285
 
        case GCONF_VALUE_STRING: t = G_TYPE_STRING; break;
286
 
        case GCONF_VALUE_FLOAT: t = G_TYPE_FLOAT; break;
287
 
        case GCONF_VALUE_INT: t = G_TYPE_INT; break;
288
 
        case GCONF_VALUE_BOOL: t = G_TYPE_BOOLEAN; break;
289
 
        default :
290
 
                t = G_TYPE_NONE;
291
 
        }
292
 
 
293
 
        if (schema != NULL)
294
 
                gconf_schema_free (schema);
295
 
        return t;
296
 
}
297
 
 
298
 
gchar *
299
 
go_conf_get_value_as_str (GOConfNode *node, gchar const *key)
300
 
{
301
 
        gchar *value_string;
302
 
 
303
 
        switch (go_conf_get_type (node, key)) {
304
 
        case G_TYPE_STRING:
305
 
                value_string = go_conf_get_string (node, key);
306
 
                break;
307
 
        case G_TYPE_INT:
308
 
                value_string = g_strdup_printf ("%i", go_conf_get_int (node, key));
309
 
                break;
310
 
        case G_TYPE_FLOAT:
311
 
                value_string = g_strdup_printf ("%f", go_conf_get_double (node, key));
312
 
                break;
313
 
        case G_TYPE_BOOLEAN:
314
 
                value_string = g_strdup (go_locale_boolean_name (go_conf_get_bool (node, key)));
315
 
                break;
316
 
        default:
317
 
                value_string = g_strdup ("ERROR FIXME");
318
 
        }
319
 
 
320
 
        return value_string;
321
 
}
322
 
 
323
 
gboolean
324
 
go_conf_get_bool (GOConfNode *node, gchar const *key)
325
 
{
326
 
        gboolean val;
327
 
        gchar *real_key;
328
 
 
329
 
        real_key = go_conf_get_real_key (node, key);
330
 
        val = gconf_client_get_bool (gconf_client, real_key, NULL);
331
 
        g_free (real_key);
332
 
 
333
 
        return val;
334
 
}
335
 
 
336
 
gint
337
 
go_conf_get_int (GOConfNode *node, gchar const *key)
338
 
{
339
 
        gint val;
340
 
        gchar *real_key;
341
 
 
342
 
        real_key = go_conf_get_real_key (node, key);
343
 
        val = gconf_client_get_int (gconf_client, real_key, NULL);
344
 
        g_free (real_key);
345
 
 
346
 
        return val;
347
 
}
348
 
 
349
 
gdouble
350
 
go_conf_get_double (GOConfNode *node, gchar const *key)
351
 
{
352
 
        gdouble val;
353
 
        gchar *real_key;
354
 
 
355
 
        real_key = go_conf_get_real_key (node, key);
356
 
        val = gconf_client_get_float (gconf_client, real_key, NULL);
357
 
        g_free (real_key);
358
 
 
359
 
        return val;
360
 
}
361
 
 
362
 
gchar *
363
 
go_conf_get_string (GOConfNode *node, gchar const *key)
364
 
{
365
 
        gchar *real_key = go_conf_get_real_key (node, key);
366
 
        gchar *res = gconf_client_get_string (gconf_client, real_key, NULL);
367
 
        g_free (real_key);
368
 
        return res;
369
 
}
370
 
 
371
 
 
372
 
GSList *
373
 
go_conf_get_str_list (GOConfNode *node, gchar const *key)
374
 
{
375
 
        return go_conf_load_str_list (node, key);
376
 
}
377
 
 
378
 
gboolean
379
 
go_conf_set_value_from_str (GOConfNode *node, gchar const *key, gchar const *val_str)
380
 
{
381
 
        switch (go_conf_get_type (node, key)) {
382
 
        case G_TYPE_STRING:
383
 
                go_conf_set_string (node, key, val_str);
384
 
                break;
385
 
        case G_TYPE_FLOAT: {
386
 
                GODateConventions const *conv = NULL;  /* workbook_date_conv (state->wb); */
387
 
                GnmValue *value = format_match_number (val_str, NULL, conv);
388
 
                if (value != NULL) {
389
 
                        gnm_float the_float = value_get_as_float (value);
390
 
                        go_conf_set_double (node, key, the_float);
391
 
                }
392
 
                if (value)
393
 
                        value_release (value);
394
 
                break;
395
 
        }
396
 
        case G_TYPE_INT: {
397
 
                GODateConventions const *conv = NULL;  /* workbook_date_conv (state->wb); */
398
 
                GnmValue *value = format_match_number (val_str, NULL, conv);
399
 
                if (value != NULL) {
400
 
                        gint the_int = value_get_as_int (value);
401
 
                        go_conf_set_int (node, key, the_int);
402
 
                }
403
 
                if (value)
404
 
                        value_release (value);
405
 
                break;
406
 
        }
407
 
        case G_TYPE_BOOLEAN: {
408
 
                GODateConventions const *conv = NULL;  /* workbook_date_conv (state->wb); */
409
 
                GnmValue *value = format_match_number (val_str, NULL, conv);
410
 
                gboolean err, the_bool;
411
 
                if (value != NULL) {
412
 
                        err = FALSE;
413
 
                        the_bool =  value_get_as_bool (value, &err);
414
 
                        go_conf_set_bool (node, key, the_bool);
415
 
                }
416
 
                if (value)
417
 
                        value_release (value);
418
 
                break;
419
 
        }
420
 
        default:
421
 
                g_warning ("Unsupported gconf type in preference dialog");
422
 
        }
423
 
 
424
 
        return TRUE;
425
 
}
426
 
 
427
 
void
428
 
go_conf_remove_monitor (guint monitor_id)
429
 
{
430
 
        gconf_client_notify_remove (gconf_client,
431
 
                GPOINTER_TO_INT (monitor_id));
432
 
}
433
 
 
434
 
typedef struct {
435
 
        GOConfMonitorFunc monitor;
436
 
        gpointer data;
437
 
} GOConfClosure;
438
 
 
439
 
static void
440
 
cb_key_changed (GConfClient *client, guint cnxn_id,
441
 
                GConfEntry *entry, GOConfClosure *close)
442
 
{
443
 
        close->monitor (NULL, gconf_entry_get_key (entry), close->data);
444
 
}
445
 
 
446
 
guint
447
 
go_conf_add_monitor (GOConfNode *node, gchar const *key,
448
 
                     GOConfMonitorFunc monitor, gpointer data)
449
 
{
450
 
        guint ret;
451
 
        GOConfClosure *close = g_new0 (GOConfClosure, 1);
452
 
        gchar *real_key;
453
 
 
454
 
        close->monitor = monitor;
455
 
        close->data = data;
456
 
        real_key = go_conf_get_real_key (node, key);
457
 
        ret = gconf_client_notify_add (gconf_client, real_key,
458
 
                (GConfClientNotifyFunc) cb_key_changed, close, g_free, NULL);
459
 
        g_free (real_key);
460
 
 
461
 
        return ret;
462
 
}