~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

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

Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

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 (NULL,
68
 
                                 _("Configuration system error"),
69
 
                                 (*error)->message);
70
 
                g_error_free (*error);
71
 
                *error = NULL;
72
 
 
73
 
                return TRUE;
74
 
        }
75
 
 
76
 
        return FALSE;
77
 
}
78
 
 
79
 
void
80
 
eel_gconf_set_boolean (const char *key,
81
 
                            gboolean boolean_value)
82
 
{
83
 
        GConfClient *client;
84
 
        GError *error = NULL;
85
 
        
86
 
        g_return_if_fail (key != NULL);
87
 
 
88
 
        client = eel_gconf_client_get_global ();
89
 
        g_return_if_fail (client != NULL);
90
 
        
91
 
        gconf_client_set_bool (client, key, boolean_value, &error);
92
 
        eel_gconf_handle_error (&error);
93
 
}
94
 
 
95
 
gboolean
96
 
eel_gconf_get_boolean (const char *key)
97
 
{
98
 
        gboolean result;
99
 
        GConfClient *client;
100
 
        GError *error = NULL;
101
 
        
102
 
        g_return_val_if_fail (key != NULL, FALSE);
103
 
        
104
 
        client = eel_gconf_client_get_global ();
105
 
        g_return_val_if_fail (client != NULL, FALSE);
106
 
        
107
 
        result = gconf_client_get_bool (client, key, &error);
108
 
        
109
 
        if (eel_gconf_handle_error (&error)) {
110
 
                result = FALSE;
111
 
        }
112
 
        
113
 
        return result;
114
 
}
115
 
 
116
 
void
117
 
eel_gconf_set_integer (const char *key,
118
 
                            int int_value)
119
 
{
120
 
        GConfClient *client;
121
 
        GError *error = NULL;
122
 
 
123
 
        g_return_if_fail (key != NULL);
124
 
 
125
 
        client = eel_gconf_client_get_global ();
126
 
        g_return_if_fail (client != NULL);
127
 
 
128
 
        gconf_client_set_int (client, key, int_value, &error);
129
 
        eel_gconf_handle_error (&error);
130
 
}
131
 
 
132
 
int
133
 
eel_gconf_get_integer (const char *key)
134
 
{
135
 
        int result;
136
 
        GConfClient *client;
137
 
        GError *error = NULL;
138
 
        
139
 
        g_return_val_if_fail (key != NULL, 0);
140
 
        
141
 
        client = eel_gconf_client_get_global ();
142
 
        g_return_val_if_fail (client != NULL, 0);
143
 
        
144
 
        result = gconf_client_get_int (client, key, &error);
145
 
 
146
 
        if (eel_gconf_handle_error (&error)) {
147
 
                result = 0;
148
 
        }
149
 
 
150
 
        return result;
151
 
}
152
 
 
153
 
void
154
 
eel_gconf_set_float (const char *key,
155
 
                            gfloat float_value)
156
 
{
157
 
        GConfClient *client;
158
 
        GError *error = NULL;
159
 
 
160
 
        g_return_if_fail (key != NULL);
161
 
 
162
 
        client = eel_gconf_client_get_global ();
163
 
        g_return_if_fail (client != NULL);
164
 
 
165
 
        gconf_client_set_float (client, key, float_value, &error);
166
 
        eel_gconf_handle_error (&error);
167
 
}
168
 
 
169
 
gfloat
170
 
eel_gconf_get_float (const char *key)
171
 
{
172
 
        gfloat result;
173
 
        GConfClient *client;
174
 
        GError *error = NULL;
175
 
        
176
 
        g_return_val_if_fail (key != NULL, 0);
177
 
        
178
 
        client = eel_gconf_client_get_global ();
179
 
        g_return_val_if_fail (client != NULL, 0);
180
 
        
181
 
        result = gconf_client_get_float (client, key, &error);
182
 
 
183
 
        if (eel_gconf_handle_error (&error)) {
184
 
                result = 0;
185
 
        }
186
 
 
187
 
        return result;
188
 
}
189
 
 
190
 
void
191
 
eel_gconf_set_string (const char *key,
192
 
                           const char *string_value)
193
 
{
194
 
        GConfClient *client;
195
 
        GError *error = NULL;
196
 
 
197
 
        g_return_if_fail (key != NULL);
198
 
        g_return_if_fail (string_value != NULL);
199
 
 
200
 
        client = eel_gconf_client_get_global ();
201
 
        g_return_if_fail (client != NULL);
202
 
        
203
 
        gconf_client_set_string (client, key, string_value, &error);
204
 
        eel_gconf_handle_error (&error);
205
 
}
206
 
 
207
 
char *
208
 
eel_gconf_get_string (const char *key)
209
 
{
210
 
        char *result;
211
 
        GConfClient *client;
212
 
        GError *error = NULL;
213
 
        
214
 
        g_return_val_if_fail (key != NULL, NULL);
215
 
        
216
 
        client = eel_gconf_client_get_global ();
217
 
        g_return_val_if_fail (client != NULL, NULL);
218
 
        
219
 
        result = gconf_client_get_string (client, key, &error);
220
 
        
221
 
        if (eel_gconf_handle_error (&error)) {
222
 
                result = g_strdup ("");
223
 
        }
224
 
        
225
 
        return result;
226
 
}
227
 
 
228
 
void
229
 
eel_gconf_set_string_list (const char *key,
230
 
                                const GSList *slist)
231
 
{
232
 
        GConfClient *client;
233
 
        GError *error;
234
 
 
235
 
        g_return_if_fail (key != NULL);
236
 
 
237
 
        client = eel_gconf_client_get_global ();
238
 
        g_return_if_fail (client != NULL);
239
 
 
240
 
        error = NULL;
241
 
        gconf_client_set_list (client, key, GCONF_VALUE_STRING,
242
 
                               /* Need cast cause of GConf api bug */
243
 
                               (GSList *) slist,
244
 
                               &error);
245
 
        eel_gconf_handle_error (&error);
246
 
}
247
 
 
248
 
GSList *
249
 
eel_gconf_get_string_list (const char *key)
250
 
{
251
 
        GSList *slist;
252
 
        GConfClient *client;
253
 
        GError *error;
254
 
        
255
 
        g_return_val_if_fail (key != NULL, NULL);
256
 
        
257
 
        client = eel_gconf_client_get_global ();
258
 
        g_return_val_if_fail (client != NULL, NULL);
259
 
        
260
 
        error = NULL;
261
 
        slist = gconf_client_get_list (client, key, GCONF_VALUE_STRING, &error);
262
 
        if (eel_gconf_handle_error (&error)) {
263
 
                slist = NULL;
264
 
        }
265
 
 
266
 
        return slist;
267
 
}
268
 
 
269
 
/* This code wasn't part of the original eel-gconf-extensions.c */
270
 
void
271
 
eel_gconf_set_integer_list (const char *key,
272
 
                        const GSList *slist)
273
 
{
274
 
        GConfClient *client;
275
 
        GError *error;
276
 
 
277
 
        g_return_if_fail (key != NULL);
278
 
 
279
 
        client = eel_gconf_client_get_global ();
280
 
        g_return_if_fail (client != NULL);
281
 
 
282
 
        error = NULL;
283
 
        gconf_client_set_list (client, key, GCONF_VALUE_INT,
284
 
                               /* Need cast cause of GConf api bug */
285
 
                               (GSList *) slist,
286
 
                               &error);
287
 
        eel_gconf_handle_error (&error);
288
 
}
289
 
 
290
 
GSList *
291
 
eel_gconf_get_integer_list (const char *key)
292
 
{
293
 
        GSList *slist;
294
 
        GConfClient *client;
295
 
        GError *error;
296
 
        
297
 
        g_return_val_if_fail (key != NULL, NULL);
298
 
        
299
 
        client = eel_gconf_client_get_global ();
300
 
        g_return_val_if_fail (client != NULL, NULL);
301
 
        
302
 
        error = NULL;
303
 
        slist = gconf_client_get_list (client, key, GCONF_VALUE_INT, &error);
304
 
        if (eel_gconf_handle_error (&error)) {
305
 
                slist = NULL;
306
 
        }
307
 
 
308
 
        return slist;
309
 
}
310
 
/* End of added code */
311
 
 
312
 
gboolean
313
 
eel_gconf_is_default (const char *key)
314
 
{
315
 
        gboolean result;
316
 
        GConfValue *value;
317
 
        GError *error = NULL;
318
 
        
319
 
        g_return_val_if_fail (key != NULL, FALSE);
320
 
        
321
 
        value = gconf_client_get_without_default  (eel_gconf_client_get_global (), key, &error);
322
 
 
323
 
        if (eel_gconf_handle_error (&error)) {
324
 
                if (value != NULL) {
325
 
                        gconf_value_free (value);
326
 
                }
327
 
                return FALSE;
328
 
        }
329
 
 
330
 
        result = (value == NULL);
331
 
 
332
 
        if (value != NULL) {
333
 
                gconf_value_free (value);
334
 
        }
335
 
 
336
 
        
337
 
        return result;
338
 
}
339
 
 
340
 
void 
341
 
eel_gconf_unset (const char *key)
342
 
{
343
 
        GConfClient *client;
344
 
        GError *error = NULL;
345
 
 
346
 
        g_return_if_fail (key != NULL);
347
 
 
348
 
        client = eel_gconf_client_get_global ();
349
 
        g_return_if_fail (client != NULL);
350
 
        
351
 
        gconf_client_unset (client, key, &error);
352
 
        eel_gconf_handle_error (&error);
353
 
}
354
 
 
355
 
gboolean
356
 
eel_gconf_monitor_add (const char *directory)
357
 
{
358
 
        GError *error = NULL;
359
 
        GConfClient *client;
360
 
 
361
 
        g_return_val_if_fail (directory != NULL, FALSE);
362
 
 
363
 
        client = eel_gconf_client_get_global ();
364
 
        g_return_val_if_fail (client != NULL, FALSE);
365
 
 
366
 
        gconf_client_add_dir (client,
367
 
                              directory,
368
 
                              GCONF_CLIENT_PRELOAD_NONE,
369
 
                              &error);
370
 
        
371
 
        if (eel_gconf_handle_error (&error)) {
372
 
                return FALSE;
373
 
        }
374
 
 
375
 
        return TRUE;
376
 
}
377
 
 
378
 
gboolean
379
 
eel_gconf_monitor_remove (const char *directory)
380
 
{
381
 
        GError *error = NULL;
382
 
        GConfClient *client;
383
 
 
384
 
        if (directory == NULL) {
385
 
                return FALSE;
386
 
        }
387
 
 
388
 
        client = eel_gconf_client_get_global ();
389
 
        g_return_val_if_fail (client != NULL, FALSE);
390
 
        
391
 
        gconf_client_remove_dir (client,
392
 
                                 directory,
393
 
                                 &error);
394
 
        
395
 
        if (eel_gconf_handle_error (&error)) {
396
 
                return FALSE;
397
 
        }
398
 
        
399
 
        return TRUE;
400
 
}
401
 
 
402
 
void
403
 
eel_gconf_suggest_sync (void)
404
 
{
405
 
        GConfClient *client;
406
 
        GError *error = NULL;
407
 
 
408
 
        client = eel_gconf_client_get_global ();
409
 
        g_return_if_fail (client != NULL);
410
 
        
411
 
        gconf_client_suggest_sync (client, &error);
412
 
        eel_gconf_handle_error (&error);
413
 
}
414
 
 
415
 
GConfValue*
416
 
eel_gconf_get_value (const char *key)
417
 
{
418
 
        GConfValue *value = NULL;
419
 
        GConfClient *client;
420
 
        GError *error = NULL;
421
 
 
422
 
        g_return_val_if_fail (key != NULL, NULL);
423
 
 
424
 
        client = eel_gconf_client_get_global ();
425
 
        g_return_val_if_fail (client != NULL, NULL);
426
 
 
427
 
        value = gconf_client_get (client, key, &error);
428
 
        
429
 
        if (eel_gconf_handle_error (&error)) {
430
 
                if (value != NULL) {
431
 
                        gconf_value_free (value);
432
 
                        value = NULL;
433
 
                }
434
 
        }
435
 
 
436
 
        return value;
437
 
}
438
 
 
439
 
void
440
 
eel_gconf_set_value (const char *key, GConfValue *value)
441
 
{
442
 
        GConfClient *client;
443
 
        GError *error = NULL;
444
 
 
445
 
        g_return_if_fail (key != NULL);
446
 
 
447
 
        client = eel_gconf_client_get_global ();
448
 
        g_return_if_fail (client != NULL);
449
 
 
450
 
        gconf_client_set (client, key, value, &error);
451
 
        
452
 
        if (eel_gconf_handle_error (&error)) {
453
 
                return;
454
 
        }
455
 
}
456
 
 
457
 
void
458
 
eel_gconf_value_free (GConfValue *value)
459
 
{
460
 
        if (value == NULL) {
461
 
                return;
462
 
        }
463
 
        
464
 
        gconf_value_free (value);
465
 
}
466
 
 
467
 
guint
468
 
eel_gconf_notification_add (const char *key,
469
 
                                 GConfClientNotifyFunc notification_callback,
470
 
                                 gpointer callback_data)
471
 
{
472
 
        guint notification_id;
473
 
        GConfClient *client;
474
 
        GError *error = NULL;
475
 
        
476
 
        g_return_val_if_fail (key != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
477
 
        g_return_val_if_fail (notification_callback != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
478
 
 
479
 
        client = eel_gconf_client_get_global ();
480
 
        g_return_val_if_fail (client != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
481
 
        
482
 
        notification_id = gconf_client_notify_add (client,
483
 
                                                   key,
484
 
                                                   notification_callback,
485
 
                                                   callback_data,
486
 
                                                   NULL,
487
 
                                                   &error);
488
 
        
489
 
        if (eel_gconf_handle_error (&error)) {
490
 
                if (notification_id != EEL_GCONF_UNDEFINED_CONNECTION) {
491
 
                        gconf_client_notify_remove (client, notification_id);
492
 
                        notification_id = EEL_GCONF_UNDEFINED_CONNECTION;
493
 
                }
494
 
        }
495
 
        
496
 
        return notification_id;
497
 
}
498
 
 
499
 
void
500
 
eel_gconf_notification_remove (guint notification_id)
501
 
{
502
 
        GConfClient *client;
503
 
 
504
 
        if (notification_id == EEL_GCONF_UNDEFINED_CONNECTION) {
505
 
                return;
506
 
        }
507
 
        
508
 
        client = eel_gconf_client_get_global ();
509
 
        g_return_if_fail (client != NULL);
510
 
 
511
 
        gconf_client_notify_remove (client, notification_id);
512
 
}