summaryrefslogtreecommitdiff
path: root/xfconf/xfconf.c
blob: cb9470863d62df263b7b90c9515c1dadfaa99bb6 (plain)
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
/*
 *  xfconf
 *
 *  Copyright (c) 2007 Brian Tarricone <bjt23@cornell.edu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License ONLY.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#include <dbus/dbus-glib.h>

#include "xfconf.h"
#include "xfconf-marshal.h"
#include "xfconf-private.h"
#include "common/xfconf-alias.h"

static guint xfconf_refcnt = 0;
static DBusGConnection *dbus_conn = NULL;
static DBusGProxy *dbus_proxy = NULL;
static GHashTable *named_structs = NULL;


/* private api */

DBusGConnection *
_xfconf_get_dbus_g_connection(void)
{
    if(!xfconf_refcnt) {
        g_critical("xfconf_init() must be called before attempting to use libxfconf!");
        return NULL;
    }

    return dbus_conn;
}

DBusGProxy *
_xfconf_get_dbus_g_proxy(void)
{
    if(!xfconf_refcnt) {
        g_critical("xfconf_init() must be called before attempting to use libxfconf!");
        return NULL;
    }

    return dbus_proxy;
}

XfconfNamedStruct *
_xfconf_named_struct_lookup(const gchar *struct_name)
{
    return named_structs ? g_hash_table_lookup(named_structs, struct_name) : NULL;
}

static void
_xfconf_named_struct_free(XfconfNamedStruct *ns)
{
    g_free(ns->member_types);
    g_slice_free(XfconfNamedStruct, ns);
}



static void
xfconf_static_dbus_init(void)
{
    static gboolean static_dbus_inited = FALSE;

    if(!static_dbus_inited) {
        dbus_g_error_domain_register(XFCONF_ERROR, "org.xfce.Xfconf.Error",
                                     XFCONF_TYPE_ERROR);

        dbus_g_object_register_marshaller(_xfconf_marshal_VOID__STRING_STRING_BOXED,
                                          G_TYPE_NONE,
                                          G_TYPE_STRING,
                                          G_TYPE_STRING,
                                          G_TYPE_VALUE,
                                          G_TYPE_INVALID);
        dbus_g_object_register_marshaller(_xfconf_marshal_VOID__STRING_STRING,
                                          G_TYPE_NONE,
                                          G_TYPE_STRING,
                                          G_TYPE_STRING,
                                          G_TYPE_INVALID);

        static_dbus_inited = TRUE;
    }
}



/* public api */

/**
 * xfconf_init:
 * @error: An error return.
 *
 * Initializes the Xfconf library.  Can be called multiple times with no
 * adverse effects.
 *
 * Returns: %TRUE if the library was initialized succesfully, %FALSE on
 *          error.  If there is an error @error will be set.
 **/
gboolean
xfconf_init(GError **error)
{
    if(xfconf_refcnt) {
        ++xfconf_refcnt;
        return TRUE;
    }

    g_type_init();

    xfconf_static_dbus_init();

    dbus_conn = dbus_g_bus_get(DBUS_BUS_SESSION, error);
    if(!dbus_conn)
        return FALSE;

    dbus_proxy = dbus_g_proxy_new_for_name(dbus_conn,
                                           "org.xfce.Xfconf",
                                           "/org/xfce/Xfconf",
                                           "org.xfce.Xfconf");

    dbus_g_proxy_add_signal(dbus_proxy, "PropertyChanged",
                            G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VALUE,
                            G_TYPE_INVALID);
    dbus_g_proxy_add_signal(dbus_proxy, "PropertyRemoved",
                            G_TYPE_STRING, G_TYPE_STRING,
                            G_TYPE_INVALID);

    _xfconf_g_bindings_init();

    ++xfconf_refcnt;
    return TRUE;
}

/**
 * xfconf_shutdown:
 *
 * Shuts down and frees any resources consumed by the Xfconf library.
 * If xfconf_init() is called multiple times, xfconf_shutdown() must be
 * called an equal number of times to shut down the library.
 **/
void
xfconf_shutdown(void)
{
    if(xfconf_refcnt <= 0) {
        return;
    }

    if(xfconf_refcnt > 1) {
        --xfconf_refcnt;
        return;
    }

    _xfconf_g_bindings_shutdown();
    _xfconf_channel_shutdown();

    if(named_structs) {
        g_hash_table_destroy(named_structs);
        named_structs = NULL;
    }

    g_object_unref(G_OBJECT(dbus_proxy));
    dbus_proxy = NULL;

    dbus_g_connection_unref(dbus_conn);
    dbus_conn = NULL;

    --xfconf_refcnt;
}

/**
 * xfconf_named_struct_register:
 * @struct_name: The unique name of the struct to register.
 * @n_members: The number of data members in the struct.
 * @member_types: An array of the #GType<!-- -->s of the struct members.
 *
 * Registers a named struct for use with xfconf_channel_get_named_struct()
 * and xfconf_channel_set_named_struct().
 **/
void
xfconf_named_struct_register(const gchar *struct_name,
                             guint n_members,
                             const GType *member_types)
{
    XfconfNamedStruct *ns;

    g_return_if_fail(struct_name && *struct_name && n_members && member_types);

    /* lazy initialize the hash table */
    if(named_structs == NULL)
        named_structs = g_hash_table_new_full(g_str_hash, g_str_equal,
                                              (GDestroyNotify)g_free,
                                              (GDestroyNotify)_xfconf_named_struct_free);

    if(G_UNLIKELY(g_hash_table_lookup(named_structs, struct_name)))
        g_critical("The struct '%s' is already registered", struct_name);
    else {
        ns = g_slice_new(XfconfNamedStruct);
        ns->n_members = n_members;
        ns->member_types = g_new(GType, n_members);
        memcpy(ns->member_types, member_types, sizeof(GType) * n_members);

        g_hash_table_insert(named_structs, g_strdup(struct_name), ns);
    }
}

/**
 * xfconf_array_values_from_gvalue:
 * @value: A #GValue containing a #GPtrArray
 * @member_index: the index of the first item to retrieve from the array
 * @...: variable arguments
 *
 * Convenience function to retrieve array values from a #GValue returned
 * by Xfconf.  The variable arguments should start with a pointer to a
 * location to store the value at index @member_index.  Further vararg
 * pairs should be (index, pointer location).  Terminate the argument
 * list with G_MAXUINT.
 *
 * Returns: %TRUE if all values were copied, %FALSE on error.
 **/
gboolean
xfconf_array_values_from_gvalue(const GValue *value,
                                gint member_index,
                                ...)
{
    va_list var_args;
    GPtrArray *arr;
    guint cur_i;
    GValue *val_arr;

    arr = g_value_get_boxed(value);
    if(!arr)
        return FALSE;

    va_start(var_args, member_index);

    for(cur_i = member_index; cur_i != G_MAXUINT; cur_i = va_arg(var_args,
                                                                 guint))
    {
        gchar *errstr = NULL;

        if(cur_i >= arr->len) {
            va_end(var_args);
            return FALSE;
        }

        val_arr = g_ptr_array_index(arr, cur_i);
        G_VALUE_LCOPY(val_arr, var_args, 0, &errstr);
        if(errstr) {
            g_warning("Unable to convert value at position %d: %s",
                      cur_i, errstr);
            g_free(errstr);
            va_end(var_args);
            return FALSE;
        }
    }

    va_end(var_args);

    return TRUE;
}

#if 0
/**
 * xfconf_array_new:
 * @n_preallocs: Number of entries to preallocate space for.
 *
 * Convenience function to greate a new #GArray to hold
 * #GValue<!-- -->s.  Normal #GArray functions may be used on
 * the returned array.  For convenience, see also xfconf_array_free().
 *
 * Returns: A new #GArray.
 **/
GArray *
xfconf_array_new(gint n_preallocs)
{
    return g_array_sized_new(FALSE, TRUE, sizeof(GValue), n_preallocs);
}
#endif

/**
 * xfconf_array_free:
 * @arr: A #GPtrArray of #GValue<!-- -->s.
 *
 * Properly frees a #GPtrArray structure containing a list of
 * #GValue<!-- -->s.  This will also cause the contents of the
 * values to be freed as well.
 **/
void
xfconf_array_free(GPtrArray *arr)
{
    guint i;
    
    if(!arr)
        return;
    
    for(i = 0; i < arr->len; ++i) {
        GValue *val = g_ptr_array_index(arr, i);
        g_value_unset(val);
        g_free(val);
    }
    
    g_ptr_array_free(arr, TRUE);
}



#define __XFCONF_C__
#include "common/xfconf-aliasdef.c"