1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3
/* eel-gconf-extensions.c - 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., 59 Temple Place - Suite 330,
20
Boston, MA 02111-1307, USA.
22
Authors: Ramiro Estrugo <ramiro@eazel.com>
27
#include "eel-gconf-extensions.h"
29
#include <gconf/gconf-client.h>
30
#include <gconf/gconf.h>
32
#include <glib/gi18n.h>
34
static GConfClient *global_gconf_client = NULL;
37
global_client_free(void)
39
if (global_gconf_client == NULL) {
43
g_object_unref(G_OBJECT(global_gconf_client));
44
global_gconf_client = NULL;
49
eel_gconf_client_get_global(void)
51
/* Initialize gconf if needed */
52
if (!gconf_is_initialized()) {
53
char *argv[] = { "eel-preferences", NULL };
57
if (!gconf_init(1, argv, &error)) {
58
if (eel_gconf_handle_error(&error)) {
63
/* check if gconf schemas are working */
64
teststr = gconf_client_get_string(eel_gconf_client_get_global(),
65
"/apps/gpdf/gconf_test", NULL);
69
dialog = gtk_message_dialog_new(NULL,
73
_("Cannot find a schema for gpdf preferences. \n"
74
"Check your gconf setup, look at gpdf FAQ for \n"
76
gtk_dialog_run(GTK_DIALOG(dialog));
84
if (global_gconf_client == NULL) {
85
global_gconf_client = gconf_client_get_default();
86
g_atexit(global_client_free);
89
return global_gconf_client;
93
eel_gconf_handle_error(GError **error)
95
g_return_val_if_fail(error != NULL, FALSE);
98
g_warning("GConf error: %s\n", (*error)->message);
109
eel_gconf_set_boolean(const char *key,
110
gboolean boolean_value)
113
GError *error = NULL;
115
g_return_if_fail(key != NULL);
117
client = eel_gconf_client_get_global();
118
g_return_if_fail(client != NULL);
120
gconf_client_set_bool(client, key, boolean_value, &error);
121
eel_gconf_handle_error(&error);
125
eel_gconf_get_boolean(const char *key)
129
GError *error = NULL;
131
g_return_val_if_fail(key != NULL, FALSE);
133
client = eel_gconf_client_get_global();
134
g_return_val_if_fail(client != NULL, FALSE);
136
result = gconf_client_get_bool(client, key, &error);
138
if (eel_gconf_handle_error(&error)) {
146
eel_gconf_set_integer(const char *key,
150
GError *error = NULL;
152
g_return_if_fail(key != NULL);
154
client = eel_gconf_client_get_global();
155
g_return_if_fail(client != NULL);
157
gconf_client_set_int(client, key, int_value, &error);
158
eel_gconf_handle_error(&error);
162
eel_gconf_get_integer(const char *key)
166
GError *error = NULL;
168
g_return_val_if_fail(key != NULL, 0);
170
client = eel_gconf_client_get_global();
171
g_return_val_if_fail(client != NULL, 0);
173
result = gconf_client_get_int(client, key, &error);
175
if (eel_gconf_handle_error(&error)) {
183
eel_gconf_set_float(const char *key,
187
GError *error = NULL;
189
g_return_if_fail(key != NULL);
191
client = eel_gconf_client_get_global();
192
g_return_if_fail(client != NULL);
194
gconf_client_set_float(client, key, float_value, &error);
195
eel_gconf_handle_error(&error);
199
eel_gconf_get_float(const char *key)
203
GError *error = NULL;
205
g_return_val_if_fail(key != NULL, 0);
207
client = eel_gconf_client_get_global();
208
g_return_val_if_fail(client != NULL, 0);
210
result = gconf_client_get_float(client, key, &error);
212
if (eel_gconf_handle_error(&error)) {
220
eel_gconf_set_string(const char *key,
221
const char *string_value)
224
GError *error = NULL;
226
g_return_if_fail(key != NULL);
227
g_return_if_fail(string_value != NULL);
229
client = eel_gconf_client_get_global();
230
g_return_if_fail(client != NULL);
232
gconf_client_set_string(client, key, string_value, &error);
233
eel_gconf_handle_error(&error);
237
eel_gconf_unset(const char *key)
240
GError *error = NULL;
242
g_return_if_fail(key != NULL);
244
client = eel_gconf_client_get_global();
245
g_return_if_fail(client != NULL);
247
gconf_client_unset(client, key, &error);
248
eel_gconf_handle_error(&error);
252
eel_gconf_get_string(const char *key)
256
GError *error = NULL;
258
g_return_val_if_fail(key != NULL, NULL);
260
client = eel_gconf_client_get_global();
261
g_return_val_if_fail(client != NULL, NULL);
263
result = gconf_client_get_string(client, key, &error);
265
if (eel_gconf_handle_error(&error)) {
266
result = g_strdup("");
273
eel_gconf_set_string_list(const char *key,
279
g_return_if_fail(key != NULL);
281
client = eel_gconf_client_get_global();
282
g_return_if_fail(client != NULL);
285
gconf_client_set_list(client, key, GCONF_VALUE_STRING,
286
/* Need cast cause of GConf api bug */
289
eel_gconf_handle_error(&error);
293
eel_gconf_get_string_list(const char *key)
299
g_return_val_if_fail(key != NULL, NULL);
301
client = eel_gconf_client_get_global();
302
g_return_val_if_fail(client != NULL, NULL);
305
slist = gconf_client_get_list(client, key, GCONF_VALUE_STRING, &error);
307
if (eel_gconf_handle_error(&error)) {
314
/* This code wasn't part of the original eel-gconf-extensions.c */
316
eel_gconf_set_integer_list(const char *key,
322
g_return_if_fail(key != NULL);
324
client = eel_gconf_client_get_global();
325
g_return_if_fail(client != NULL);
328
gconf_client_set_list(client, key, GCONF_VALUE_INT,
329
/* Need cast cause of GConf api bug */
332
eel_gconf_handle_error(&error);
336
eel_gconf_get_integer_list(const char *key)
342
g_return_val_if_fail(key != NULL, NULL);
344
client = eel_gconf_client_get_global();
345
g_return_val_if_fail(client != NULL, NULL);
348
slist = gconf_client_get_list(client, key, GCONF_VALUE_INT, &error);
350
if (eel_gconf_handle_error(&error)) {
356
/* End of added code */
359
eel_gconf_is_default(const char *key)
363
GError *error = NULL;
365
g_return_val_if_fail(key != NULL, FALSE);
367
value = gconf_client_get_without_default(eel_gconf_client_get_global(), key, &error);
369
if (eel_gconf_handle_error(&error)) {
371
gconf_value_free(value);
377
result = (value == NULL);
380
gconf_value_free(value);
388
eel_gconf_monitor_add(const char *directory)
390
GError *error = NULL;
393
g_return_val_if_fail(directory != NULL, FALSE);
395
client = eel_gconf_client_get_global();
396
g_return_val_if_fail(client != NULL, FALSE);
398
gconf_client_add_dir(client,
400
GCONF_CLIENT_PRELOAD_NONE,
403
if (eel_gconf_handle_error(&error)) {
411
eel_gconf_monitor_remove(const char *directory)
413
GError *error = NULL;
416
if (directory == NULL) {
420
client = eel_gconf_client_get_global();
421
g_return_val_if_fail(client != NULL, FALSE);
423
gconf_client_remove_dir(client,
427
if (eel_gconf_handle_error(&error)) {
435
eel_gconf_suggest_sync(void)
438
GError *error = NULL;
440
client = eel_gconf_client_get_global();
441
g_return_if_fail(client != NULL);
443
gconf_client_suggest_sync(client, &error);
444
eel_gconf_handle_error(&error);
448
eel_gconf_get_value(const char *key)
450
GConfValue *value = NULL;
452
GError *error = NULL;
454
g_return_val_if_fail(key != NULL, NULL);
456
client = eel_gconf_client_get_global();
457
g_return_val_if_fail(client != NULL, NULL);
459
value = gconf_client_get(client, key, &error);
461
if (eel_gconf_handle_error(&error)) {
463
gconf_value_free(value);
472
eel_gconf_set_value(const char *key, const GConfValue *value)
475
GError *error = NULL;
477
g_return_if_fail(key != NULL);
479
client = eel_gconf_client_get_global();
480
g_return_if_fail(client != NULL);
482
gconf_client_set(client, key, value, &error);
484
if (eel_gconf_handle_error(&error)) {
490
eel_gconf_key_exists(const char *key)
492
GConfValue *value = NULL;
494
GError *error = NULL;
495
gboolean error_occurred;
496
gboolean value_found;
498
g_return_val_if_fail(key != NULL, FALSE);
500
client = eel_gconf_client_get_global();
501
g_return_val_if_fail(client != NULL, FALSE);
503
value = gconf_client_get(client, key, &error);
505
value_found = (value != NULL);
506
error_occurred = (error != NULL);
508
eel_gconf_value_free(value);
514
return (!error_occurred && value_found);
518
eel_gconf_value_free(GConfValue *value)
524
gconf_value_free(value);
528
eel_gconf_notification_add(const char *key,
529
GConfClientNotifyFunc notification_callback,
530
gpointer callback_data)
532
guint notification_id;
534
GError *error = NULL;
536
g_return_val_if_fail(key != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
537
g_return_val_if_fail(notification_callback != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
539
client = eel_gconf_client_get_global();
540
g_return_val_if_fail(client != NULL, EEL_GCONF_UNDEFINED_CONNECTION);
542
notification_id = gconf_client_notify_add(client,
544
notification_callback,
549
if (eel_gconf_handle_error(&error)) {
550
if (notification_id != EEL_GCONF_UNDEFINED_CONNECTION) {
551
gconf_client_notify_remove(client, notification_id);
552
notification_id = EEL_GCONF_UNDEFINED_CONNECTION;
556
return notification_id;
560
eel_gconf_notification_remove(guint notification_id)
564
if (notification_id == EEL_GCONF_UNDEFINED_CONNECTION) {
568
client = eel_gconf_client_get_global();
569
g_return_if_fail(client != NULL);
571
gconf_client_notify_remove(client, notification_id);
574
/* Simple wrapper for eel_gconf_notifier_add which
575
* adds the notifier id to the GList given as argument
576
* so that a call to gpdf_notification_free can remove the notifiers
579
gpdf_notification_add(const char *key,
580
GConfClientNotifyFunc notification_callback,
581
gpointer callback_data,
586
id = eel_gconf_notification_add(key,
587
notification_callback,
590
if (notifiers != NULL) {
591
*notifiers = g_list_append(*notifiers,
592
GINT_TO_POINTER(id));
596
/* Removes all the notifiers listed in notifiers */
597
/* Frees the notifiers list */
599
gpdf_notification_remove(GList **notifiers)
601
g_list_foreach(*notifiers,
602
(GFunc) eel_gconf_notification_remove,
604
g_list_free(*notifiers);