~ubuntu-branches/ubuntu/utopic/rhythmbox/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/eel-gconf-extensions.c

Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
 
 
3
 
/* arch-tag: Implementation of stuff to make GConf easier to use.
4
 
 
5
 
   Copyright (C) 2000, 2001 Eazel, Inc.
6
 
 
7
 
   The Gnome Library is free software; you can redistribute it and/or
8
 
   modify it under the terms of the GNU Library General Public License as
9
 
   published by the Free Software Foundation; either version 2 of the
10
 
   License, or (at your option) any later version.
11
 
 
12
 
   The Gnome Library is distributed in the hope that it will be useful,
13
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
   Library General Public License for more details.
16
 
 
17
 
   You should have received a copy of the GNU Library General Public
18
 
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
19
 
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
   Boston, MA 02111-1307, USA.
21
 
 
22
 
   Authors: Ramiro Estrugo <ramiro@eazel.com>
23
 
*/
24
 
 
25
 
#include <config.h>
26
 
#include <stdlib.h>
27
 
#include <gconf/gconf-client.h>
28
 
#include <gconf/gconf.h>
29
 
#include <libgnome/gnome-i18n.h>
30
 
#include <libgnomeui/gnome-dialog.h>
31
 
#include <libgnomeui/gnome-dialog-util.h>
32
 
 
33
 
#include "eel-gconf-extensions.h"
34
 
#include "rb-dialog.h"
35
 
 
36
 
static GConfClient *global_gconf_client = NULL;
37
 
 
38
 
static void
39
 
global_client_free (void)
40
 
{
41
 
        if (global_gconf_client == NULL) {
42
 
                return;
43
 
        }
44
 
 
45
 
        g_object_unref (G_OBJECT (global_gconf_client));
46
 
        global_gconf_client = NULL;
47
 
}
48
 
 
49
 
/* Public */
50
 
GConfClient *
51
 
eel_gconf_client_get_global (void)
52
 
{
53
 
        if (global_gconf_client == NULL) {
54
 
                global_gconf_client = gconf_client_get_default ();
55
 
                g_atexit (global_client_free);
56
 
        }
57
 
        
58
 
        return global_gconf_client;
59
 
}
60
 
 
61
 
gboolean
62
 
eel_gconf_handle_error (GError **error)
63
 
{
64
 
        g_return_val_if_fail (error != NULL, FALSE);
65
 
 
66
 
        if (*error != NULL) {
67
 
                rb_error_dialog (_("GConf error:\n  %s"), (*error)->message);
68
 
                g_error_free (*error);
69
 
                *error = NULL;
70
 
 
71
 
                return TRUE;
72
 
        }
73
 
 
74
 
        return FALSE;
75
 
}
76
 
 
77
 
void
78
 
eel_gconf_set_boolean (const char *key,
79
 
                            gboolean boolean_value)
80
 
{
81
 
        GConfClient *client;
82
 
        GError *error = NULL;
83
 
        
84
 
        g_return_if_fail (key != NULL);
85
 
 
86
 
        client = eel_gconf_client_get_global ();
87
 
        g_return_if_fail (client != NULL);
88
 
        
89
 
        gconf_client_set_bool (client, key, boolean_value, &error);
90
 
        eel_gconf_handle_error (&error);
91
 
}
92
 
 
93
 
gboolean
94
 
eel_gconf_get_boolean (const char *key)
95
 
{
96
 
        gboolean result;
97
 
        GConfClient *client;
98
 
        GError *error = NULL;
99
 
        
100
 
        g_return_val_if_fail (key != NULL, FALSE);
101
 
        
102
 
        client = eel_gconf_client_get_global ();
103
 
        g_return_val_if_fail (client != NULL, FALSE);
104
 
        
105
 
        result = gconf_client_get_bool (client, key, &error);
106
 
        
107
 
        if (eel_gconf_handle_error (&error)) {
108
 
                result = FALSE;
109
 
        }
110
 
        
111
 
        return result;
112
 
}
113
 
 
114
 
void
115
 
eel_gconf_set_integer (const char *key,
116
 
                            int int_value)
117
 
{
118
 
        GConfClient *client;
119
 
        GError *error = NULL;
120
 
 
121
 
        g_return_if_fail (key != NULL);
122
 
 
123
 
        client = eel_gconf_client_get_global ();
124
 
        g_return_if_fail (client != NULL);
125
 
 
126
 
        gconf_client_set_int (client, key, int_value, &error);
127
 
        eel_gconf_handle_error (&error);
128
 
}
129
 
 
130
 
int
131
 
eel_gconf_get_integer (const char *key)
132
 
{
133
 
        int result;
134
 
        GConfClient *client;
135
 
        GError *error = NULL;
136
 
        
137
 
        g_return_val_if_fail (key != NULL, 0);
138
 
        
139
 
        client = eel_gconf_client_get_global ();
140
 
        g_return_val_if_fail (client != NULL, 0);
141
 
        
142
 
        result = gconf_client_get_int (client, key, &error);
143
 
 
144
 
        if (eel_gconf_handle_error (&error)) {
145
 
                result = 0;
146
 
        }
147
 
 
148
 
        return result;
149
 
}
150
 
 
151
 
void
152
 
eel_gconf_set_float (const char *key,
153
 
                            gfloat float_value)
154
 
{
155
 
        GConfClient *client;
156
 
        GError *error = NULL;
157
 
 
158
 
        g_return_if_fail (key != NULL);
159
 
 
160
 
        client = eel_gconf_client_get_global ();
161
 
        g_return_if_fail (client != NULL);
162
 
 
163
 
        gconf_client_set_float (client, key, float_value, &error);
164
 
        eel_gconf_handle_error (&error);
165
 
}
166
 
 
167
 
gfloat
168
 
eel_gconf_get_float (const char *key)
169
 
{
170
 
        gfloat result;
171
 
        GConfClient *client;
172
 
        GError *error = NULL;
173
 
        
174
 
        g_return_val_if_fail (key != NULL, 0);
175
 
        
176
 
        client = eel_gconf_client_get_global ();
177
 
        g_return_val_if_fail (client != NULL, 0);
178
 
        
179
 
        result = gconf_client_get_float (client, key, &error);
180
 
 
181
 
        if (eel_gconf_handle_error (&error)) {
182
 
                result = 0;
183
 
        }
184
 
 
185
 
        return result;
186
 
}
187
 
 
188
 
void
189
 
eel_gconf_set_string (const char *key,
190
 
                           const char *string_value)
191
 
{
192
 
        GConfClient *client;
193
 
        GError *error = NULL;
194
 
 
195
 
        g_return_if_fail (key != NULL);
196
 
        g_return_if_fail (string_value != NULL);
197
 
 
198
 
        client = eel_gconf_client_get_global ();
199
 
        g_return_if_fail (client != NULL);
200
 
        
201
 
        gconf_client_set_string (client, key, string_value, &error);
202
 
        eel_gconf_handle_error (&error);
203
 
}
204
 
 
205
 
char *
206
 
eel_gconf_get_string (const char *key)
207
 
{
208
 
        char *result;
209
 
        GConfClient *client;
210
 
        GError *error = NULL;
211
 
        
212
 
        g_return_val_if_fail (key != NULL, NULL);
213
 
        
214
 
        client = eel_gconf_client_get_global ();
215
 
        g_return_val_if_fail (client != NULL, NULL);
216
 
        
217
 
        result = gconf_client_get_string (client, key, &error);
218
 
        
219
 
        if (eel_gconf_handle_error (&error)) {
220
 
                result = g_strdup ("");
221
 
        }
222
 
        
223
 
        return result;
224
 
}
225
 
 
226
 
void
227
 
eel_gconf_set_string_list (const char *key,
228
 
                                const GSList *slist)
229
 
{
230
 
        GConfClient *client;
231
 
        GError *error;
232
 
 
233
 
        g_return_if_fail (key != NULL);
234
 
 
235
 
        client = eel_gconf_client_get_global ();
236
 
        g_return_if_fail (client != NULL);
237
 
 
238
 
        error = NULL;
239
 
        gconf_client_set_list (client, key, GCONF_VALUE_STRING,
240
 
                               /* Need cast cause of GConf api bug */
241
 
                               (GSList *) slist,
242
 
                               &error);
243
 
        eel_gconf_handle_error (&error);
244
 
}
245
 
 
246
 
GSList *
247
 
eel_gconf_get_string_list (const char *key)
248
 
{
249
 
        GSList *slist;
250
 
        GConfClient *client;
251
 
        GError *error;
252
 
        
253
 
        g_return_val_if_fail (key != NULL, NULL);
254
 
        
255
 
        client = eel_gconf_client_get_global ();
256
 
        g_return_val_if_fail (client != NULL, NULL);
257
 
        
258
 
        error = NULL;
259
 
        slist = gconf_client_get_list (client, key, GCONF_VALUE_STRING, &error);
260
 
        if (eel_gconf_handle_error (&error)) {
261
 
                slist = NULL;
262
 
        }
263
 
 
264
 
        return slist;
265
 
}
266
 
 
267
 
/* This code wasn't part of the original eel-gconf-extensions.c */
268
 
void
269
 
eel_gconf_set_integer_list (const char *key,
270
 
                        const GSList *slist)
271
 
{
272
 
        GConfClient *client;
273
 
        GError *error;
274
 
 
275
 
        g_return_if_fail (key != NULL);
276
 
 
277
 
        client = eel_gconf_client_get_global ();
278
 
        g_return_if_fail (client != NULL);
279
 
 
280
 
        error = NULL;
281
 
        gconf_client_set_list (client, key, GCONF_VALUE_INT,
282
 
                               /* Need cast cause of GConf api bug */
283
 
                               (GSList *) slist,
284
 
                               &error);
285
 
        eel_gconf_handle_error (&error);
286
 
}
287
 
 
288
 
GSList *
289
 
eel_gconf_get_integer_list (const char *key)
290
 
{
291
 
        GSList *slist;
292
 
        GConfClient *client;
293
 
        GError *error;
294
 
        
295
 
        g_return_val_if_fail (key != NULL, NULL);
296
 
        
297
 
        client = eel_gconf_client_get_global ();
298
 
        g_return_val_if_fail (client != NULL, NULL);
299
 
        
300
 
        error = NULL;
301
 
        slist = gconf_client_get_list (client, key, GCONF_VALUE_INT, &error);
302
 
        if (eel_gconf_handle_error (&error)) {
303
 
                slist = NULL;
304
 
        }
305
 
 
306
 
        return slist;
307
 
}
308
 
/* End of added code */
309
 
 
310
 
gboolean
311
 
eel_gconf_is_default (const char *key)
312
 
{
313
 
        gboolean result;
314
 
        GConfValue *value;
315
 
        GError *error = NULL;
316
 
        
317
 
        g_return_val_if_fail (key != NULL, FALSE);
318
 
        
319
 
        value = gconf_client_get_without_default  (eel_gconf_client_get_global (), key, &error);
320
 
 
321
 
        if (eel_gconf_handle_error (&error)) {
322
 
                if (value != NULL) {
323
 
                        gconf_value_free (value);
324
 
                }
325
 
                return FALSE;
326
 
        }
327
 
 
328
 
        result = (value == NULL);
329
 
 
330
 
        if (value != NULL) {
331
 
                gconf_value_free (value);
332
 
        }
333
 
 
334
 
        
335
 
        return result;
336
 
}
337
 
 
338
 
void 
339
 
eel_gconf_unset (const char *key)
340
 
{
341
 
        GConfClient *client;
342
 
        GError *error = NULL;
343
 
 
344
 
        g_return_if_fail (key != NULL);
345
 
 
346
 
        client = eel_gconf_client_get_global ();
347
 
        g_return_if_fail (client != NULL);
348
 
        
349
 
        gconf_client_unset (client, key, &error);
350
 
        eel_gconf_handle_error (&error);
351
 
}
352
 
 
353
 
gboolean
354
 
eel_gconf_monitor_add (const char *directory)
355
 
{
356
 
        GError *error = NULL;
357
 
        GConfClient *client;
358
 
 
359
 
        g_return_val_if_fail (directory != NULL, FALSE);
360
 
 
361
 
        client = eel_gconf_client_get_global ();
362
 
        g_return_val_if_fail (client != NULL, FALSE);
363
 
 
364
 
        gconf_client_add_dir (client,
365
 
                              directory,
366
 
                              GCONF_CLIENT_PRELOAD_NONE,
367
 
                              &error);
368
 
        
369
 
        if (eel_gconf_handle_error (&error)) {
370
 
                return FALSE;
371
 
        }
372
 
 
373
 
        return TRUE;
374
 
}
375
 
 
376
 
gboolean
377
 
eel_gconf_monitor_remove (const char *directory)
378
 
{
379
 
        GError *error = NULL;
380
 
        GConfClient *client;
381
 
 
382
 
        if (directory == NULL) {
383
 
                return FALSE;
384
 
        }
385
 
 
386
 
        client = eel_gconf_client_get_global ();
387
 
        g_return_val_if_fail (client != NULL, FALSE);
388
 
        
389
 
        gconf_client_remove_dir (client,
390
 
                                 directory,
391
 
                                 &error);
392
 
        
393
 
        if (eel_gconf_handle_error (&error)) {
394
 
                return FALSE;
395
 
        }
396
 
        
397
 
        return TRUE;
398
 
}
399
 
 
400
 
void
401
 
eel_gconf_suggest_sync (void)
402
 
{
403
 
        GConfClient *client;
404
 
        GError *error = NULL;
405
 
 
406
 
        client = eel_gconf_client_get_global ();
407
 
        g_return_if_fail (client != NULL);
408
 
        
409
 
        gconf_client_suggest_sync (client, &error);
410
 
        eel_gconf_handle_error (&error);
411
 
}
412
 
 
413
 
GConfValue*
414
 
eel_gconf_get_value (const char *key)
415
 
{
416
 
        GConfValue *value = NULL;
417
 
        GConfClient *client;
418
 
        GError *error = NULL;
419
 
 
420
 
        g_return_val_if_fail (key != NULL, NULL);
421
 
 
422
 
        client = eel_gconf_client_get_global ();
423
 
        g_return_val_if_fail (client != NULL, NULL);
424
 
 
425
 
        value = gconf_client_get (client, key, &error);
426
 
        
427
 
        if (eel_gconf_handle_error (&error)) {
428
 
                if (value != NULL) {
429
 
                        gconf_value_free (value);
430
 
                        value = NULL;
431
 
                }
432
 
        }
433
 
 
434
 
        return value;
435
 
}
436
 
 
437
 
void
438
 
eel_gconf_set_value (const char *key, GConfValue *value)
439
 
{
440
 
        GConfClient *client;
441
 
        GError *error = NULL;
442
 
 
443
 
        g_return_if_fail (key != NULL);
444
 
 
445
 
        client = eel_gconf_client_get_global ();
446
 
        g_return_if_fail (client != NULL);
447
 
 
448
 
        gconf_client_set (client, key, value, &error);
449
 
        
450
 
        if (eel_gconf_handle_error (&error)) {
451
 
                return;
452
 
        }
453
 
}
454
 
 
455
 
void
456
 
eel_gconf_value_free (GConfValue *value)
457
 
{
458
 
        if (value == NULL) {
459
 
                return;
460
 
        }
461
 
        
462
 
        gconf_value_free (value);
463
 
}
464
 
 
465
 
guint
466
 
eel_gconf_notification_add (const char *key,
467
 
                                 GConfClientNotifyFunc notification_callback,
468
 
                                 gpointer callback_data)
469
 
{
470
 
        guint notification_id;
471
 
        GConfClient *client;
472
 
        GError *error = NULL;
473
 
        
474
 
        g_return_val_if_fail (key != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
475
 
        g_return_val_if_fail (notification_callback != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
476
 
 
477
 
        client = eel_gconf_client_get_global ();
478
 
        g_return_val_if_fail (client != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
479
 
        
480
 
        notification_id = gconf_client_notify_add (client,
481
 
                                                   key,
482
 
                                                   notification_callback,
483
 
                                                   callback_data,
484
 
                                                   NULL,
485
 
                                                   &error);
486
 
        
487
 
        if (eel_gconf_handle_error (&error)) {
488
 
                if (notification_id != EEL_GCONF_UNDEFINED_CONNECTION) {
489
 
                        gconf_client_notify_remove (client, notification_id);
490
 
                        notification_id = EEL_GCONF_UNDEFINED_CONNECTION;
491
 
                }
492
 
        }
493
 
        
494
 
        return notification_id;
495
 
}
496
 
 
497
 
void
498
 
eel_gconf_notification_remove (guint notification_id)
499
 
{
500
 
        GConfClient *client;
501
 
 
502
 
        if (notification_id == EEL_GCONF_UNDEFINED_CONNECTION) {
503
 
                return;
504
 
        }
505
 
        
506
 
        client = eel_gconf_client_get_global ();
507
 
        g_return_if_fail (client != NULL);
508
 
 
509
 
        gconf_client_notify_remove (client, notification_id);
510
 
}