2006-10-27 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / lib / eel-gconf-extensions.c
blobcccc3a6855a0ad3ac8d7797719f23431ef59c1a5
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* arch-tag: Implementation of stuff to make GConf easier to use.
5 Copyright (C) 2000, 2001 Eazel, Inc.
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.
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.
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., 51 Franklin St, Fifth Floor,
20 Boston, MA 02110-1301 USA.
22 Authors: Ramiro Estrugo <ramiro@eazel.com>
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>
31 #include "eel-gconf-extensions.h"
32 #include "rb-dialog.h"
34 static GConfClient *global_gconf_client = NULL;
36 static void
37 global_client_free (void)
39 if (global_gconf_client == NULL) {
40 return;
43 g_object_unref (G_OBJECT (global_gconf_client));
44 global_gconf_client = NULL;
47 /* Public */
48 GConfClient *
49 eel_gconf_client_get_global (void)
51 if (global_gconf_client == NULL) {
52 global_gconf_client = gconf_client_get_default ();
53 g_atexit (global_client_free);
56 return global_gconf_client;
59 gboolean
60 eel_gconf_handle_error (GError **error)
62 g_return_val_if_fail (error != NULL, FALSE);
64 if (*error != NULL) {
65 g_warning ((*error)->message);
66 g_error_free (*error);
67 *error = NULL;
69 return TRUE;
72 return FALSE;
75 void
76 eel_gconf_set_boolean (const char *key,
77 gboolean boolean_value)
79 GConfClient *client;
80 GError *error = NULL;
82 g_return_if_fail (key != NULL);
84 client = eel_gconf_client_get_global ();
85 g_return_if_fail (client != NULL);
87 gconf_client_set_bool (client, key, boolean_value, &error);
88 eel_gconf_handle_error (&error);
91 gboolean
92 eel_gconf_get_boolean (const char *key)
94 gboolean result;
95 GConfClient *client;
96 GError *error = NULL;
98 g_return_val_if_fail (key != NULL, FALSE);
100 client = eel_gconf_client_get_global ();
101 g_return_val_if_fail (client != NULL, FALSE);
103 result = gconf_client_get_bool (client, key, &error);
105 if (eel_gconf_handle_error (&error)) {
106 result = FALSE;
109 return result;
112 void
113 eel_gconf_set_integer (const char *key,
114 int int_value)
116 GConfClient *client;
117 GError *error = NULL;
119 g_return_if_fail (key != NULL);
121 client = eel_gconf_client_get_global ();
122 g_return_if_fail (client != NULL);
124 gconf_client_set_int (client, key, int_value, &error);
125 eel_gconf_handle_error (&error);
129 eel_gconf_get_integer (const char *key)
131 int result;
132 GConfClient *client;
133 GError *error = NULL;
135 g_return_val_if_fail (key != NULL, 0);
137 client = eel_gconf_client_get_global ();
138 g_return_val_if_fail (client != NULL, 0);
140 result = gconf_client_get_int (client, key, &error);
142 if (eel_gconf_handle_error (&error)) {
143 result = 0;
146 return result;
149 void
150 eel_gconf_set_float (const char *key,
151 gfloat float_value)
153 GConfClient *client;
154 GError *error = NULL;
156 g_return_if_fail (key != NULL);
158 client = eel_gconf_client_get_global ();
159 g_return_if_fail (client != NULL);
161 gconf_client_set_float (client, key, float_value, &error);
162 eel_gconf_handle_error (&error);
165 gfloat
166 eel_gconf_get_float (const char *key)
168 gfloat result;
169 GConfClient *client;
170 GError *error = NULL;
172 g_return_val_if_fail (key != NULL, 0);
174 client = eel_gconf_client_get_global ();
175 g_return_val_if_fail (client != NULL, 0);
177 result = gconf_client_get_float (client, key, &error);
179 if (eel_gconf_handle_error (&error)) {
180 result = 0;
183 return result;
186 void
187 eel_gconf_set_string (const char *key,
188 const char *string_value)
190 GConfClient *client;
191 GError *error = NULL;
193 g_return_if_fail (key != NULL);
194 g_return_if_fail (string_value != NULL);
196 client = eel_gconf_client_get_global ();
197 g_return_if_fail (client != NULL);
199 gconf_client_set_string (client, key, string_value, &error);
200 eel_gconf_handle_error (&error);
203 char *
204 eel_gconf_get_string (const char *key)
206 char *result;
207 GConfClient *client;
208 GError *error = NULL;
210 g_return_val_if_fail (key != NULL, NULL);
212 client = eel_gconf_client_get_global ();
213 g_return_val_if_fail (client != NULL, NULL);
215 result = gconf_client_get_string (client, key, &error);
217 if (eel_gconf_handle_error (&error)) {
218 result = g_strdup ("");
221 return result;
224 void
225 eel_gconf_set_string_list (const char *key,
226 const GSList *slist)
228 GConfClient *client;
229 GError *error;
231 g_return_if_fail (key != NULL);
233 client = eel_gconf_client_get_global ();
234 g_return_if_fail (client != NULL);
236 error = NULL;
237 gconf_client_set_list (client, key, GCONF_VALUE_STRING,
238 /* Need cast cause of GConf api bug */
239 (GSList *) slist,
240 &error);
241 eel_gconf_handle_error (&error);
244 GSList *
245 eel_gconf_get_string_list (const char *key)
247 GSList *slist;
248 GConfClient *client;
249 GError *error;
251 g_return_val_if_fail (key != NULL, NULL);
253 client = eel_gconf_client_get_global ();
254 g_return_val_if_fail (client != NULL, NULL);
256 error = NULL;
257 slist = gconf_client_get_list (client, key, GCONF_VALUE_STRING, &error);
258 if (eel_gconf_handle_error (&error)) {
259 slist = NULL;
262 return slist;
265 /* This code wasn't part of the original eel-gconf-extensions.c */
266 void
267 eel_gconf_set_integer_list (const char *key,
268 const GSList *slist)
270 GConfClient *client;
271 GError *error;
273 g_return_if_fail (key != NULL);
275 client = eel_gconf_client_get_global ();
276 g_return_if_fail (client != NULL);
278 error = NULL;
279 gconf_client_set_list (client, key, GCONF_VALUE_INT,
280 /* Need cast cause of GConf api bug */
281 (GSList *) slist,
282 &error);
283 eel_gconf_handle_error (&error);
286 GSList *
287 eel_gconf_get_integer_list (const char *key)
289 GSList *slist;
290 GConfClient *client;
291 GError *error;
293 g_return_val_if_fail (key != NULL, NULL);
295 client = eel_gconf_client_get_global ();
296 g_return_val_if_fail (client != NULL, NULL);
298 error = NULL;
299 slist = gconf_client_get_list (client, key, GCONF_VALUE_INT, &error);
300 if (eel_gconf_handle_error (&error)) {
301 slist = NULL;
304 return slist;
306 /* End of added code */
308 gboolean
309 eel_gconf_is_default (const char *key)
311 gboolean result;
312 GConfValue *value;
313 GError *error = NULL;
315 g_return_val_if_fail (key != NULL, FALSE);
317 value = gconf_client_get_without_default (eel_gconf_client_get_global (), key, &error);
319 if (eel_gconf_handle_error (&error)) {
320 if (value != NULL) {
321 gconf_value_free (value);
323 return FALSE;
326 result = (value == NULL);
328 if (value != NULL) {
329 gconf_value_free (value);
333 return result;
336 void
337 eel_gconf_unset (const char *key)
339 GConfClient *client;
340 GError *error = NULL;
342 g_return_if_fail (key != NULL);
344 client = eel_gconf_client_get_global ();
345 g_return_if_fail (client != NULL);
347 gconf_client_unset (client, key, &error);
348 eel_gconf_handle_error (&error);
351 gboolean
352 eel_gconf_monitor_add (const char *directory)
354 GError *error = NULL;
355 GConfClient *client;
357 g_return_val_if_fail (directory != NULL, FALSE);
359 client = eel_gconf_client_get_global ();
360 g_return_val_if_fail (client != NULL, FALSE);
362 gconf_client_add_dir (client,
363 directory,
364 GCONF_CLIENT_PRELOAD_NONE,
365 &error);
367 if (eel_gconf_handle_error (&error)) {
368 return FALSE;
371 return TRUE;
374 gboolean
375 eel_gconf_monitor_remove (const char *directory)
377 GError *error = NULL;
378 GConfClient *client;
380 if (directory == NULL) {
381 return FALSE;
384 client = eel_gconf_client_get_global ();
385 g_return_val_if_fail (client != NULL, FALSE);
387 gconf_client_remove_dir (client,
388 directory,
389 &error);
391 if (eel_gconf_handle_error (&error)) {
392 return FALSE;
395 return TRUE;
398 void
399 eel_gconf_suggest_sync (void)
401 GConfClient *client;
402 GError *error = NULL;
404 client = eel_gconf_client_get_global ();
405 g_return_if_fail (client != NULL);
407 gconf_client_suggest_sync (client, &error);
408 eel_gconf_handle_error (&error);
411 GConfValue*
412 eel_gconf_get_value (const char *key)
414 GConfValue *value = NULL;
415 GConfClient *client;
416 GError *error = NULL;
418 g_return_val_if_fail (key != NULL, NULL);
420 client = eel_gconf_client_get_global ();
421 g_return_val_if_fail (client != NULL, NULL);
423 value = gconf_client_get (client, key, &error);
425 if (eel_gconf_handle_error (&error)) {
426 if (value != NULL) {
427 gconf_value_free (value);
428 value = NULL;
432 return value;
435 void
436 eel_gconf_set_value (const char *key, GConfValue *value)
438 GConfClient *client;
439 GError *error = NULL;
441 g_return_if_fail (key != NULL);
443 client = eel_gconf_client_get_global ();
444 g_return_if_fail (client != NULL);
446 gconf_client_set (client, key, value, &error);
448 if (eel_gconf_handle_error (&error)) {
449 return;
453 void
454 eel_gconf_value_free (GConfValue *value)
456 if (value == NULL) {
457 return;
460 gconf_value_free (value);
463 guint
464 eel_gconf_notification_add (const char *key,
465 GConfClientNotifyFunc notification_callback,
466 gpointer callback_data)
468 guint notification_id;
469 GConfClient *client;
470 GError *error = NULL;
472 g_return_val_if_fail (key != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
473 g_return_val_if_fail (notification_callback != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
475 client = eel_gconf_client_get_global ();
476 g_return_val_if_fail (client != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
478 notification_id = gconf_client_notify_add (client,
479 key,
480 notification_callback,
481 callback_data,
482 NULL,
483 &error);
485 if (eel_gconf_handle_error (&error)) {
486 if (notification_id != EEL_GCONF_UNDEFINED_CONNECTION) {
487 gconf_client_notify_remove (client, notification_id);
488 notification_id = EEL_GCONF_UNDEFINED_CONNECTION;
492 return notification_id;
495 void
496 eel_gconf_notification_remove (guint notification_id)
498 GConfClient *client;
500 if (notification_id == EEL_GCONF_UNDEFINED_CONNECTION) {
501 return;
504 client = eel_gconf_client_get_global ();
505 g_return_if_fail (client != NULL);
507 gconf_client_notify_remove (client, notification_id);