aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-config-listener.c
blob: ed57bf8cdcc05af31e50fac7d332491c185ccf24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Configuration component listener
 *
 * Author:
 *   Rodrigo Moya <rodrigo@ximian.com>
 *
 * Copyright 2002, Ximian, Inc.
 */

#include <gconf/gconf-client.h>
#include "e-config-listener.h"

#define PARENT_TYPE G_TYPE_OBJECT

typedef struct {
    EConfigListener *cl;
    guint lid;
    char *key;
    GConfValueType type;
    union {
        gboolean v_bool;
        float v_float;
        long v_long;
        char *v_str;
    } value;
    gboolean used_default;
} KeyData;

struct _EConfigListenerPrivate {
    GConfClient *db;
    GHashTable *keys;
};

static void e_config_listener_class_init (EConfigListenerClass *klass);
static void e_config_listener_init       (EConfigListener *cl, EConfigListenerClass *klass);
static void e_config_listener_finalize   (GObject *object);

static GObjectClass *parent_class = NULL;

enum {
    KEY_CHANGED,
    LAST_SIGNAL
};

static guint config_listener_signals[LAST_SIGNAL];

static void
e_config_listener_class_init (EConfigListenerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = e_config_listener_finalize;
    klass->key_changed = NULL;

    config_listener_signals[KEY_CHANGED] =
        g_signal_new ("key_changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EConfigListenerClass, key_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__STRING,
                  G_TYPE_NONE, 1, G_TYPE_STRING);

    /* make sure GConf is initialized */
    if (!gconf_is_initialized ())
        gconf_init (0, NULL, NULL);
}

static void
e_config_listener_init (EConfigListener *cl, EConfigListenerClass *klass)
{
    /* allocate internal structure */
    cl->priv = g_new0 (EConfigListenerPrivate, 1);

    cl->priv->keys = g_hash_table_new (g_str_hash, g_str_equal);
    cl->priv->db = gconf_client_get_default ();
}

static void
free_key_hash (gpointer key, gpointer value, gpointer user_data)
{
    KeyData *kd = (KeyData *) value;

    g_return_if_fail (kd != NULL);

    gconf_client_notify_remove (kd->cl->priv->db, kd->lid);

    g_free (kd->key);
    switch (kd->type) {
    case GCONF_VALUE_STRING :
        g_free (kd->value.v_str);
        break;
    default :
        break;
    }

    g_free (kd);
}

static void
e_config_listener_finalize (GObject *object)
{
    EConfigListener *cl = (EConfigListener *) object;

    g_return_if_fail (E_IS_CONFIG_LISTENER (cl));

    g_hash_table_foreach (cl->priv->keys, (GHFunc) free_key_hash, NULL);
    g_hash_table_destroy (cl->priv->keys);
    cl->priv->keys = NULL;

    if (cl->priv->db != NULL) {
        g_object_unref (G_OBJECT (cl->priv->db));
        cl->priv->db = NULL;
    }

    g_free (cl->priv);
    cl->priv = NULL;

    if (G_OBJECT_CLASS (parent_class)->finalize)
        (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

GType
e_config_listener_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
                        sizeof (EConfigListenerClass),
                        (GBaseInitFunc) NULL,
                        (GBaseFinalizeFunc) NULL,
                        (GClassInitFunc) e_config_listener_class_init,
                        NULL,
                        NULL,
                        sizeof (EConfigListener),
                        0,
                        (GInstanceInitFunc) e_config_listener_init
                };
                type = g_type_register_static (PARENT_TYPE, "EConfigListener", &info, 0);
    }

    return type;
}

/**
 * e_config_listener_new
 *
 * Create a new configuration listener, which is an object which
 * allows to listen for changes in the configuration database. It keeps
 * an updated copy of all requested configuration entries, so that
 * access is much quicker and instantaneous.
 *
 * Returns: the newly created listener.
 */
EConfigListener *
e_config_listener_new (void)
{
    EConfigListener *cl;

    cl = g_object_new (E_CONFIG_LISTENER_TYPE, NULL);
    return cl;
}

static void
property_change_cb (GConfEngine *engine,
            guint cnxn_id,
            GConfEntry *entry,
            gpointer user_data)
{
    KeyData *kd = (KeyData *) user_data;

    g_return_if_fail (entry != NULL);
    g_return_if_fail (kd != NULL);

    /* free previous value */
    if (kd->type == GCONF_VALUE_STRING)
        g_free (kd->value.v_str);

    /* set new value */
    if (entry->value->type == GCONF_VALUE_BOOL) {
        kd->type = GCONF_VALUE_BOOL;
        kd->value.v_bool = gconf_value_get_bool (entry->value);
    } else if (entry->value->type == GCONF_VALUE_FLOAT) {
        kd->type = GCONF_VALUE_FLOAT;
        kd->value.v_float = gconf_value_get_float (entry->value);
    } else if (entry->value->type == GCONF_VALUE_INT) {
        kd->type = GCONF_VALUE_INT;
        kd->value.v_long = gconf_value_get_int (entry->value);
    } else if (entry->value->type == GCONF_VALUE_STRING) {
        kd->type = GCONF_VALUE_STRING;
        kd->value.v_str = g_strdup (gconf_value_get_string (entry->value));
    } else
        return;

    g_signal_emit (G_OBJECT (kd->cl), config_listener_signals[KEY_CHANGED], 0, kd->key);
}

static KeyData *
add_key (EConfigListener *cl, const char *key, GConfValueType type,
     gpointer value, gboolean used_default)
{
    KeyData *kd;

    /* add the key to our hash table */
    kd = g_new0 (KeyData, 1);
    kd->cl = cl;
    kd->key = g_strdup (key);
    kd->type = type;
    switch (type) {
    case GCONF_VALUE_BOOL :
        memcpy (&kd->value.v_bool, value, sizeof (gboolean));
        break;
    case GCONF_VALUE_FLOAT :
        memcpy (&kd->value.v_float, value, sizeof (float));
        break;
    case GCONF_VALUE_INT :
        memcpy (&kd->value.v_long, value, sizeof (long));
        break;
    case GCONF_VALUE_STRING :
        kd->value.v_str = g_strdup ((const char *) value);
        break;
    default :
        break;
    }

    kd->used_default = used_default;

    /* add the listener for changes */
    gconf_client_add_dir (cl->priv->db, key, GCONF_CLIENT_PRELOAD_NONE, NULL);
    kd->lid = gconf_client_notify_add (cl->priv->db, key,
                       (GConfClientNotifyFunc) property_change_cb,
                       kd, NULL, NULL);

    g_hash_table_insert (cl->priv->keys, kd->key, kd);

    return kd;
}

gboolean
e_config_listener_get_boolean (EConfigListener *cl, const char *key)
{
    return e_config_listener_get_boolean_with_default (cl, key, FALSE, NULL);
}

gboolean
e_config_listener_get_boolean_with_default (EConfigListener *cl,
                        const char *key,
                        gboolean def,
                        gboolean *used_default)
{
    GConfValue *conf_value;
    gboolean value;
    KeyData *kd;
    gpointer orig_key, orig_value;

    g_return_val_if_fail (E_IS_CONFIG_LISTENER (cl), FALSE);
    g_return_val_if_fail (key != NULL, FALSE);

    /* search for the key in our hash table */
    if (!g_hash_table_lookup_extended (cl->priv->keys, key, &orig_key, &orig_value)) {
        /* not found, so retrieve it from the configuration database */
        conf_value = gconf_client_get (cl->priv->db, key, NULL);
        if (conf_value) {
            value = gconf_value_get_bool (conf_value);
            kd = add_key (cl, key, GCONF_VALUE_BOOL, &value, FALSE);
            gconf_value_free (conf_value);

            if (used_default != NULL)
                *used_default = FALSE;
        } else {
            value = def;
            kd = add_key (cl, key, GCONF_VALUE_BOOL, &def, TRUE);

            if (used_default != NULL)
                *used_default = TRUE;
        }
    } else {
        kd = (KeyData *) orig_value;
        g_assert (kd != NULL);

        if (kd->type == GCONF_VALUE_BOOL) {
            value = kd->value.v_bool;
            if (used_default != NULL)
                *used_default = kd->used_default;
        } else
            return FALSE;
    }

    return value;
}

float
e_config_listener_get_float (EConfigListener *cl, const char *key)
{
    return e_config_listener_get_float_with_default (cl, key, 0.0, NULL);
}

float
e_config_listener_get_float_with_default (EConfigListener *cl,
                      const char *key,
                      float def,
                      gboolean *used_default)
{
    GConfValue *conf_value;
    float value;
    KeyData *kd;
    gpointer orig_key, orig_value;

    g_return_val_if_fail (E_IS_CONFIG_LISTENER (cl), -1);
    g_return_val_if_fail (key != NULL, -1);

    /* search for the key in our hash table */
    if (!g_hash_table_lookup_extended (cl->priv->keys, key, &orig_key, &orig_value)) {
        /* not found, so retrieve it from the configuration database */
        conf_value = gconf_client_get (cl->priv->db, key, NULL);
        if (conf_value) {
            value = gconf_value_get_float (conf_value);
            kd = add_key (cl, key, GCONF_VALUE_FLOAT, &value, FALSE);
            gconf_value_free (conf_value);

            if (used_default != NULL)
                *used_default = FALSE;
        } else {
            value = def;
            kd = add_key (cl, key, GCONF_VALUE_FLOAT, &def, TRUE);

            if (used_default != NULL)
                *used_default = TRUE;
        }
    } else {
        kd = (KeyData *) orig_value;
        g_assert (kd != NULL);

        if (kd->type == GCONF_VALUE_FLOAT) {
            value = kd->value.v_float;
            if (used_default != NULL)
                *used_default = kd->used_default;
        } else
            return -1;
    }

    return value;
}

long
e_config_listener_get_long (EConfigListener *cl, const char *key)
{
    return e_config_listener_get_long_with_default (cl, key, 0, NULL);
}

long
e_config_listener_get_long_with_default (EConfigListener *cl,
                     const char *key,
                     long def,
                     gboolean *used_default)
{
    GConfValue *conf_value;
    long value;
    KeyData *kd;
    gpointer orig_key, orig_value;

    g_return_val_if_fail (E_IS_CONFIG_LISTENER (cl), -1);
    g_return_val_if_fail (key != NULL, -1);

    /* search for the key in our hash table */
    if (!g_hash_table_lookup_extended (cl->priv->keys, key, &orig_key, &orig_value)) {
        /* not found, so retrieve it from the configuration database */
        conf_value = gconf_client_get (cl->priv->db, key, NULL);
        if (conf_value) {
            value = gconf_value_get_int (conf_value);
            kd = add_key (cl, key, GCONF_VALUE_INT, &value, FALSE);
            gconf_value_free (conf_value);

            if (used_default != NULL)
                *used_default = FALSE;
        } else {
            value = def;
            kd = add_key (cl, key, GCONF_VALUE_INT, &def, TRUE);

            if (used_default != NULL)
                *used_default = TRUE;
        }
    } else {
        kd = (KeyData *) orig_value;
        g_assert (kd != NULL);

        if (kd->type == GCONF_VALUE_INT) {
            value = kd->value.v_long;
            if (used_default != NULL)
                *used_default = kd->used_default;
        } else
            return -1;
    }

    return value;
}

char *
e_config_listener_get_string (EConfigListener *cl, const char *key)
{
    return e_config_listener_get_string_with_default (cl, key, NULL, NULL);
}

char *
e_config_listener_get_string_with_default (EConfigListener *cl,
                       const char *key,
                       const char *def,
                       gboolean *used_default)
{
    GConfValue *conf_value;
    char *str;
    KeyData *kd;
    gpointer orig_key, orig_value;

    g_return_val_if_fail (E_IS_CONFIG_LISTENER (cl), NULL);
    g_return_val_if_fail (key != NULL, NULL);

    /* search for the key in our hash table */
    if (!g_hash_table_lookup_extended (cl->priv->keys, key, &orig_key, &orig_value)) {
        /* not found, so retrieve it from the configuration database */
        conf_value = gconf_client_get (cl->priv->db, key, NULL);
        if (conf_value) {
            str = g_strdup (gconf_value_get_string (conf_value));
            kd = add_key (cl, key, GCONF_VALUE_STRING, (gpointer) str, FALSE);
            gconf_value_free (conf_value);

            if (used_default != NULL)
                *used_default = FALSE;
        } else {
            str = g_strdup (def);
            kd = add_key (cl, key, GCONF_VALUE_STRING, (gpointer) str, TRUE);

            if (used_default != NULL)
                *used_default = TRUE;
        }
    } else {
        kd = (KeyData *) orig_value;
        g_assert (kd != NULL);

        if (kd->type == GCONF_VALUE_STRING) {
            str = g_strdup (kd->value.v_str);
            if (used_default != NULL)
                *used_default = kd->used_default;
        } else
            return NULL;
    }

    return str;
}

void
e_config_listener_set_boolean (EConfigListener *cl, const char *key, gboolean value)
{
    KeyData *kd;
    GError *err = NULL;

    g_return_if_fail (E_IS_CONFIG_LISTENER (cl));
    g_return_if_fail (key != NULL);

    /* check that the value is not the same */
    if (value == e_config_listener_get_boolean_with_default (cl, key, 0, NULL))
        return;

    gconf_client_set_bool (cl->priv->db, key, value, &err);
    if (err) {
        g_warning ("e_config_listener_set_bool: %s", err->message);
        g_error_free (err);
    } else {
        /* update the internal copy */
        kd = g_hash_table_lookup (cl->priv->keys, key);
        if (kd)
            kd->value.v_bool = value;
    }
}

void
e_config_listener_set_float (EConfigListener *cl, const char *key, float value)
{
    KeyData *kd;
    GError *err = NULL;

    g_return_if_fail (E_IS_CONFIG_LISTENER (cl));
    g_return_if_fail (key != NULL);

    /* check that the value is not the same */
    if (value == e_config_listener_get_float_with_default (cl, key, 0, NULL))
        return;

    gconf_client_set_float (cl->priv->db, key, value, &err);
    if (err) {
        g_warning ("e_config_listener_set_float: %s", err->message);
        g_error_free (err);
    } else {
        /* update the internal copy */
        kd = g_hash_table_lookup (cl->priv->keys, key);
        if (kd)
            kd->value.v_float = value;
    }
}

void
e_config_listener_set_long (EConfigListener *cl, const char *key, long value)
{
    KeyData *kd;
    GError *err = NULL;

    g_return_if_fail (E_IS_CONFIG_LISTENER (cl));
    g_return_if_fail (key != NULL);

    /* check that the value is not the same */
    if (value == e_config_listener_get_long_with_default (cl, key, 0, NULL))
        return;

    gconf_client_set_int (cl->priv->db, key, value, &err);
    if (err) {
        g_warning ("e_config_listener_set_long: %s", err->message);
        g_error_free (err);
    } else {
        /* update the internal copy */
        kd = g_hash_table_lookup (cl->priv->keys, key);
        if (kd)
            kd->value.v_long = value;
    }
}

void
e_config_listener_set_string (EConfigListener *cl, const char *key, const char *value)
{
    char *s1, *s2;
    KeyData *kd;
    GError *err = NULL;

    g_return_if_fail (E_IS_CONFIG_LISTENER (cl));
    g_return_if_fail (key != NULL);

    /* check that the value is not the same */
    s1 = (char *) value;
    s2 = e_config_listener_get_string_with_default (cl, key, NULL, NULL);
    if (!strcmp (s1 ? s1 : "", s2 ? s2 : "")) {
        g_free (s2);
        return;
    }

    g_free (s2);

    gconf_client_set_string (cl->priv->db, key, value, &err);
    if (err) {
        g_warning ("e_config_listener_set_bool: %s", err->message);
        g_error_free (err);
    } else {
        /* update the internal copy */
        kd = g_hash_table_lookup (cl->priv->keys, key);
        if (kd) {
            g_free (kd->value.v_str);
            kd->value.v_str = g_strdup (value);
        }
    }
}