summaryrefslogtreecommitdiff
path: root/xfconf/xfconf.c
blob: 578bf30ffa594255121f3f18ecd595167a06b6d0 (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
/*
 *  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 <dbus/dbus-glib.h>

#include "xfconf.h"
#include "xfconf-marshal.h"
#include "xfconf-private.h"

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


/* private api */

DBusGConnection *
_xfconf_get_dbus_g_connection()
{
    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()
{
    if(!xfconf_refcnt) {
        g_critical("xfconf_init() must be called before attempting to use libxfconf!");
        return NULL;
    }

    return dbus_proxy;
}

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

    return gui_dbus_proxy;
}

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

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



/* 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();

    dbus_g_error_domain_register(XFCONF_ERROR, "org.xfce.Xfconf.Error",
                                 XFCONF_TYPE_ERROR);

    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_object_register_marshaller((GClosureMarshal)xfconf_marshal_VOID__STRING_STRING,
                                      G_TYPE_NONE,
                                      G_TYPE_STRING,
                                      G_TYPE_STRING,
                                      G_TYPE_INVALID);
    dbus_g_proxy_add_signal(dbus_proxy, "PropertyChanged",
                            G_TYPE_STRING, G_TYPE_STRING,
                            G_TYPE_INVALID);

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

    named_structs = g_hash_table_new_full(g_str_hash, g_str_equal,
                                          (GDestroyNotify)g_free,
                                          (GDestroyNotify)_xfconf_named_struct_free);

    ++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()
{
    if(--xfconf_refcnt)
        return;

    g_hash_table_destroy(named_structs);
    named_structs = NULL;

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

    g_object_unref(G_OBJECT(gui_dbus_proxy));
    gui_dbus_proxy = NULL;

    dbus_g_connection_unref(dbus_conn);
    dbus_conn = NULL;
}

/**
 * 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
                     && !g_hash_table_lookup(named_structs, struct_name));

    ns = g_new(XfconfNamedStruct, 1);
    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);
}