summaryrefslogtreecommitdiff
path: root/xfconfd/xfconf-daemon.c
blob: 3a42e62de02a9e9a5ca306bb1b73b249644d7540 (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
/*
 *  xfconfd
 *
 *  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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

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

#include <dbus/dbus-glib-lowlevel.h>
#include <libxfce4util/libxfce4util.h>

#include "xfconf-daemon.h"
#include "xfconf-backend-factory.h"
#include "xfconf-backend.h"
#include "xfconf-marshal.h"


static gboolean xfconf_set_property(XfconfDaemon *xfconfd,
                                    const gchar *channel,
                                    const gchar *property,
                                    const GValue *value,
                                    GError **error);
static gboolean xfconf_get_property(XfconfDaemon *xfconfd,
                                    const gchar *channel,
                                    const gchar *property,
                                    GValue *value,
                                    GError **error);
static gboolean xfconf_get_all_properties(XfconfDaemon *xfconfd,
                                          const gchar *channel,
                                          GHashTable **properties,
                                          GError **error);
static gboolean xfconf_property_exists(XfconfDaemon *xfconfd,
                                       const gchar *channel,
                                       const gchar *property,
                                       gboolean *exists,
                                       GError **error);
static gboolean xfconf_remove_property(XfconfDaemon *xfconfd,
                                       const gchar *channel,
                                       const gchar *property,
                                       GError **error);
static gboolean xfconf_remove_channel(XfconfDaemon *xfconfd,
                                      const gchar *channel,
                                      GError **error);
static gboolean xfconf_gui_show_list(XfconfDaemon *xfconfd,
                                     const gchar *display,
                                     GError **error);
static gboolean xfconf_gui_show_plugin(XfconfDaemon *xfconfd,
                                       const gchar *display,
                                       const gchar *name,
                                       GError **error);

#include "xfconf-dbus-server.h"

struct _XfconfDaemon
{
    GObject parent;
    
    DBusGConnection *dbus_conn;
    
    XfconfBackend *backend;
};

typedef struct _XfconfDaemonClass
{
    GObjectClass parent;
} XfconfDaemonClass;

static void xfconf_daemon_class_init(XfconfDaemonClass *klass);

static void xfconf_daemon_init(XfconfDaemon *instance);
static void xfconf_daemon_finalize(GObject *obj);


G_DEFINE_TYPE(XfconfDaemon, xfconf_daemon, G_TYPE_OBJECT)


static void
xfconf_daemon_class_init(XfconfDaemonClass *klass)
{
    GObjectClass *object_class = (GObjectClass *)klass;
    
    object_class->finalize = xfconf_daemon_finalize;
    
    g_signal_new("changed", XFCONF_TYPE_DAEMON, G_SIGNAL_RUN_LAST, 0, NULL,
                 NULL, xfconf_marshal_VOID__STRING_STRING, G_TYPE_NONE, 2,
                 G_TYPE_STRING, G_TYPE_STRING);
    
    dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(klass),
                                    &dbus_glib_xfconf_object_info);
}

static void
xfconf_daemon_init(XfconfDaemon *instance)
{
    
}

static void
xfconf_daemon_finalize(GObject *obj)
{
    XfconfDaemon *xfconfd = XFCONF_DAEMON(obj);
    
    if(xfconfd->backend) {
        xfconf_backend_flush(xfconfd->backend, NULL);
        g_object_unref(G_OBJECT(xfconfd->backend));
    }
    
    if(xfconfd->dbus_conn)
        dbus_g_connection_unref(xfconfd->dbus_conn);
    
    G_OBJECT_CLASS(xfconf_daemon_parent_class)->finalize(obj);
}



static gboolean
xfconf_set_property(XfconfDaemon *xfconfd,
                    const gchar *channel,
                    const gchar *property,
                    const GValue *value,
                    GError **error)
{
    return xfconf_backend_set(xfconfd->backend, channel, property, value, error);
}

static gboolean
xfconf_get_property(XfconfDaemon *xfconfd,
                    const gchar *channel,
                    const gchar *property,
                    GValue *value,
                    GError **error)
{
    /* FIXME: presumably, |value| leaks.  how do we fix this?  perhaps
     * using the org.freedesktop.DBus.GLib.Async annotation? */
    return xfconf_backend_get(xfconfd->backend, channel, property, value, error);
}

static gboolean
xfconf_get_all_properties(XfconfDaemon *xfconfd,
                          const gchar *channel,
                          GHashTable **properties,
                          GError **error)
{
    gboolean ret;
    
    g_return_val_if_fail(properties && !*properties, FALSE);
    
    *properties = g_hash_table_new_full(g_str_hash, g_str_equal,
                                        (GDestroyNotify)g_free,
                                        (GDestroyNotify)g_value_unset);
    
    ret = xfconf_backend_get_all(xfconfd->backend, channel, *properties, error);
    if(!ret) {
        g_hash_table_destroy(*properties);
        *properties = NULL;
    }
    
    /* FIXME: presumably, |*properties| leaks.  how do we fix this?  perhaps
     * using the org.freedesktop.DBus.GLib.Async annotation? */
    
    return ret;
}

static gboolean
xfconf_property_exists(XfconfDaemon *xfconfd,
                       const gchar *channel,
                       const gchar *property,
                       gboolean *exists,
                       GError **error)
{
    return xfconf_backend_exists(xfconfd->backend, channel, property, exists, error);
}

static gboolean
xfconf_remove_property(XfconfDaemon *xfconfd,
                       const gchar *channel,
                       const gchar *property,
                       GError **error)
{
    return xfconf_backend_remove(xfconfd->backend, channel, property, error);
}

static gboolean
xfconf_remove_channel(XfconfDaemon *xfconfd,
                      const gchar *channel,
                      GError **error)
{
    return xfconf_backend_remove_channel(xfconfd->backend, channel, error);
}

static gboolean
xfconf_gui_show_list(XfconfDaemon *xfconfd,
                     const gchar *display,
                     GError **error)
{
    gchar *command;
    gboolean ret;
    
    if(display)
        command = g_strdup_printf("env DISPLAY='%s' %s/xfconf-settings-show",
                                  display, BINDIR);
    else
        command = g_strdup(BINDIR "/xfconf-settings-show");
    
    ret = g_spawn_command_line_async(command, error);
    
    g_free(command);
    
    return ret;
}

static gboolean
xfconf_gui_show_plugin(XfconfDaemon *xfconfd,
                       const gchar *display,
                       const gchar *name,
                       GError **error)
{
    gchar *command;
    gboolean ret;
    
    g_return_val_if_fail(name, FALSE);
    
    if(display)
        command = g_strdup_printf("env DISPLAY='%s' %s/xfconf-settings-show '%s'",
                                  display, BINDIR, name);
    else
        command = g_strdup_printf("%s/xfconf-settings-show '%s'", BINDIR, name);
    
    ret = g_spawn_command_line_async(command, error);
    
    g_free(command);
    
    return ret;
}



static gboolean
xfconf_daemon_start(XfconfDaemon *xfconfd,
                    GError **error)
{
    int ret;
    DBusError derror;
    
    xfconfd->dbus_conn = dbus_g_bus_get(DBUS_BUS_SESSION, error);
    if(G_UNLIKELY(!xfconfd->dbus_conn))
        return FALSE;
    
    dbus_g_connection_register_g_object(xfconfd->dbus_conn,
                                        "/org/xfce/Xfconf",
                                        G_OBJECT(xfconfd));
    
    dbus_error_init(&derror);
    ret = dbus_bus_request_name(dbus_g_connection_get_connection(xfconfd->dbus_conn),
                                "org.xfce.Xfconf",
                                DBUS_NAME_FLAG_DO_NOT_QUEUE,
                                &derror);
    if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
        if(dbus_error_is_set(&derror)) {
            if(error)
                dbus_set_g_error(error, &derror);
            dbus_error_free(&derror);
        } else if(error) {
            g_set_error(error, DBUS_GERROR, DBUS_GERROR_FAILED,
                        _("Another Xfconf daemon is already running"));
        }
        
        return FALSE;
    }
    
    return TRUE;
}

static gboolean
xfconf_daemon_load_config(XfconfDaemon *xfconfd,
                          const gchar *backend_id,
                          GError **error)
{
    xfconfd->backend = xfconf_backend_factory_get_backend(backend_id, error);
    if(!xfconfd->backend)
        return FALSE;
    
    return TRUE;
}



XfconfDaemon *
xfconf_daemon_new_unique(const gchar *backend_id,
                         GError **error)
{
    XfconfDaemon *xfconfd;
    
    g_return_val_if_fail(backend_id && *backend_id, NULL);
    
    xfconfd = g_object_new(XFCONF_TYPE_DAEMON, NULL);
    
    if(!xfconf_daemon_start(xfconfd, error)
       || !xfconf_daemon_load_config(xfconfd, backend_id, error))
    {
        g_object_unref(G_OBJECT(xfconfd));
        return NULL;
    }
    
    return xfconfd;
}