~ubuntu-branches/ubuntu/oneiric/rhythmbox/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Rico Tzschichholz
  • Date: 2011-07-29 16:41:38 UTC
  • mto: This revision was merged to the branch mainline in revision 191.
  • Revision ID: james.westby@ubuntu.com-20110729164138-wwicy8nqalm18ck7
Tags: upstream-2.90.1~20110802
ImportĀ upstreamĀ versionĀ 2.90.1~20110802

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