gdbus: Simplify generated introspection
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <dbus/dbus.h>
33
34 #include "gdbus.h"
35
36 #define info(fmt...)
37 #define error(fmt...)
38 #define debug(fmt...)
39
40 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
41
42 #ifndef DBUS_ERROR_UNKNOWN_PROPERTY
43 #define DBUS_ERROR_UNKNOWN_PROPERTY "org.freedesktop.DBus.Error.UnknownProperty"
44 #endif
45
46 #ifndef DBUS_ERROR_PROPERTY_READ_ONLY
47 #define DBUS_ERROR_PROPERTY_READ_ONLY "org.freedesktop.DBus.Error.PropertyReadOnly"
48 #endif
49
50 struct generic_data {
51         unsigned int refcount;
52         DBusConnection *conn;
53         char *path;
54         GSList *interfaces;
55         GSList *objects;
56         GSList *added;
57         GSList *removed;
58         guint process_id;
59         gboolean pending_prop;
60         char *introspect;
61         struct generic_data *parent;
62 };
63
64 struct interface_data {
65         char *name;
66         const GDBusMethodTable *methods;
67         const GDBusSignalTable *signals;
68         const GDBusPropertyTable *properties;
69         GSList *pending_prop;
70         void *user_data;
71         GDBusDestroyFunction destroy;
72 };
73
74 struct security_data {
75         GDBusPendingReply pending;
76         DBusMessage *message;
77         const GDBusMethodTable *method;
78         void *iface_user_data;
79 };
80
81 struct property_data {
82         DBusConnection *conn;
83         GDBusPendingPropertySet id;
84         DBusMessage *message;
85 };
86
87 static int global_flags = 0;
88 static struct generic_data *root;
89
90 static gboolean process_changes(gpointer user_data);
91 static void process_properties_from_interface(struct generic_data *data,
92                                                 struct interface_data *iface);
93 static void process_property_changes(struct generic_data *data);
94
95 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
96                                                 const char *direction)
97 {
98         for (; args && args->name; args++) {
99                 g_string_append_printf(gstr,
100                                         "<arg name=\"%s\" type=\"%s\"",
101                                         args->name, args->signature);
102
103                 if (direction)
104                         g_string_append_printf(gstr,
105                                         " direction=\"%s\"/>\n", direction);
106                 else
107                         g_string_append_printf(gstr, "/>\n");
108
109         }
110 }
111
112 #define G_DBUS_ANNOTATE(name_, value_)                          \
113         "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
114         "value=\"" value_ "\"/>"
115
116 #define G_DBUS_ANNOTATE_DEPRECATED \
117         G_DBUS_ANNOTATE("Deprecated", "true")
118
119 #define G_DBUS_ANNOTATE_NOREPLY \
120         G_DBUS_ANNOTATE("Method.NoReply", "true")
121
122 static gboolean check_experimental(int flags, int flag)
123 {
124         if (!(flags & flag))
125                 return FALSE;
126
127         return !(global_flags & G_DBUS_FLAG_ENABLE_EXPERIMENTAL);
128 }
129
130 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
131 {
132         const GDBusMethodTable *method;
133         const GDBusSignalTable *signal;
134         const GDBusPropertyTable *property;
135
136         for (method = iface->methods; method && method->name; method++) {
137                 if (check_experimental(method->flags,
138                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
139                         continue;
140
141                 g_string_append_printf(gstr, "<method name=\"%s\">",
142                                                                 method->name);
143                 print_arguments(gstr, method->in_args, "in");
144                 print_arguments(gstr, method->out_args, "out");
145
146                 if (method->flags & G_DBUS_METHOD_FLAG_DEPRECATED)
147                         g_string_append_printf(gstr,
148                                                 G_DBUS_ANNOTATE_DEPRECATED);
149
150                 if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY)
151                         g_string_append_printf(gstr, G_DBUS_ANNOTATE_NOREPLY);
152
153                 g_string_append_printf(gstr, "</method>");
154         }
155
156         for (signal = iface->signals; signal && signal->name; signal++) {
157                 if (check_experimental(signal->flags,
158                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
159                         continue;
160
161                 g_string_append_printf(gstr, "<signal name=\"%s\">",
162                                                                 signal->name);
163                 print_arguments(gstr, signal->args, NULL);
164
165                 if (signal->flags & G_DBUS_SIGNAL_FLAG_DEPRECATED)
166                         g_string_append_printf(gstr,
167                                                 G_DBUS_ANNOTATE_DEPRECATED);
168
169                 g_string_append_printf(gstr, "</signal>\n");
170         }
171
172         for (property = iface->properties; property && property->name;
173                                                                 property++) {
174                 if (check_experimental(property->flags,
175                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
176                         continue;
177
178                 g_string_append_printf(gstr, "<property name=\"%s\""
179                                         " type=\"%s\" access=\"%s%s\">",
180                                         property->name, property->type,
181                                         property->get ? "read" : "",
182                                         property->set ? "write" : "");
183
184                 if (property->flags & G_DBUS_PROPERTY_FLAG_DEPRECATED)
185                         g_string_append_printf(gstr,
186                                                 G_DBUS_ANNOTATE_DEPRECATED);
187
188                 g_string_append_printf(gstr, "</property>");
189         }
190 }
191
192 static void generate_introspection_xml(DBusConnection *conn,
193                                 struct generic_data *data, const char *path)
194 {
195         GSList *list;
196         GString *gstr;
197         char **children;
198         int i;
199
200         g_free(data->introspect);
201
202         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
203
204         g_string_append_printf(gstr, "<node>");
205
206         for (list = data->interfaces; list; list = list->next) {
207                 struct interface_data *iface = list->data;
208
209                 g_string_append_printf(gstr, "<interface name=\"%s\">",
210                                                                 iface->name);
211
212                 generate_interface_xml(gstr, iface);
213
214                 g_string_append_printf(gstr, "</interface>");
215         }
216
217         if (!dbus_connection_list_registered(conn, path, &children))
218                 goto done;
219
220         for (i = 0; children[i]; i++)
221                 g_string_append_printf(gstr, "<node name=\"%s\"/>",
222                                                                 children[i]);
223
224         dbus_free_string_array(children);
225
226 done:
227         g_string_append_printf(gstr, "</node>");
228
229         data->introspect = g_string_free(gstr, FALSE);
230 }
231
232 static DBusMessage *introspect(DBusConnection *connection,
233                                 DBusMessage *message, void *user_data)
234 {
235         struct generic_data *data = user_data;
236         DBusMessage *reply;
237
238         if (data->introspect == NULL)
239                 generate_introspection_xml(connection, data,
240                                                 dbus_message_get_path(message));
241
242         reply = dbus_message_new_method_return(message);
243         if (reply == NULL)
244                 return NULL;
245
246         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
247                                         DBUS_TYPE_INVALID);
248
249         return reply;
250 }
251
252 static DBusHandlerResult process_message(DBusConnection *connection,
253                         DBusMessage *message, const GDBusMethodTable *method,
254                                                         void *iface_user_data)
255 {
256         DBusMessage *reply;
257
258         reply = method->function(connection, message, iface_user_data);
259
260         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
261                 if (reply != NULL)
262                         dbus_message_unref(reply);
263                 return DBUS_HANDLER_RESULT_HANDLED;
264         }
265
266         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
267                 if (reply == NULL)
268                         return DBUS_HANDLER_RESULT_HANDLED;
269         }
270
271         if (reply == NULL)
272                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
273
274         dbus_connection_send(connection, reply, NULL);
275         dbus_message_unref(reply);
276
277         return DBUS_HANDLER_RESULT_HANDLED;
278 }
279
280 static GDBusPendingReply next_pending = 1;
281 static GSList *pending_security = NULL;
282
283 static const GDBusSecurityTable *security_table = NULL;
284
285 void g_dbus_pending_success(DBusConnection *connection,
286                                         GDBusPendingReply pending)
287 {
288         GSList *list;
289
290         for (list = pending_security; list; list = list->next) {
291                 struct security_data *secdata = list->data;
292
293                 if (secdata->pending != pending)
294                         continue;
295
296                 pending_security = g_slist_remove(pending_security, secdata);
297
298                 process_message(connection, secdata->message,
299                                 secdata->method, secdata->iface_user_data);
300
301                 dbus_message_unref(secdata->message);
302                 g_free(secdata);
303                 return;
304         }
305 }
306
307 void g_dbus_pending_error_valist(DBusConnection *connection,
308                                 GDBusPendingReply pending, const char *name,
309                                         const char *format, va_list args)
310 {
311         GSList *list;
312
313         for (list = pending_security; list; list = list->next) {
314                 struct security_data *secdata = list->data;
315                 DBusMessage *reply;
316
317                 if (secdata->pending != pending)
318                         continue;
319
320                 pending_security = g_slist_remove(pending_security, secdata);
321
322                 reply = g_dbus_create_error_valist(secdata->message,
323                                                         name, format, args);
324                 if (reply != NULL) {
325                         dbus_connection_send(connection, reply, NULL);
326                         dbus_message_unref(reply);
327                 }
328
329                 dbus_message_unref(secdata->message);
330                 g_free(secdata);
331                 return;
332         }
333 }
334
335 void g_dbus_pending_error(DBusConnection *connection,
336                                 GDBusPendingReply pending,
337                                 const char *name, const char *format, ...)
338 {
339         va_list args;
340
341         va_start(args, format);
342
343         g_dbus_pending_error_valist(connection, pending, name, format, args);
344
345         va_end(args);
346 }
347
348 int polkit_check_authorization(DBusConnection *conn,
349                                 const char *action, gboolean interaction,
350                                 void (*function) (dbus_bool_t authorized,
351                                                         void *user_data),
352                                                 void *user_data, int timeout);
353
354 struct builtin_security_data {
355         DBusConnection *conn;
356         GDBusPendingReply pending;
357 };
358
359 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
360 {
361         struct builtin_security_data *data = user_data;
362
363         if (authorized == TRUE)
364                 g_dbus_pending_success(data->conn, data->pending);
365         else
366                 g_dbus_pending_error(data->conn, data->pending,
367                                                 DBUS_ERROR_AUTH_FAILED, NULL);
368
369         g_free(data);
370 }
371
372 static void builtin_security_function(DBusConnection *conn,
373                                                 const char *action,
374                                                 gboolean interaction,
375                                                 GDBusPendingReply pending)
376 {
377         struct builtin_security_data *data;
378
379         data = g_new0(struct builtin_security_data, 1);
380         data->conn = conn;
381         data->pending = pending;
382
383         if (polkit_check_authorization(conn, action, interaction,
384                                 builtin_security_result, data, 30000) < 0)
385                 g_dbus_pending_error(conn, pending, NULL, NULL);
386 }
387
388 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
389                         const GDBusMethodTable *method, void *iface_user_data)
390 {
391         const GDBusSecurityTable *security;
392
393         for (security = security_table; security && security->privilege;
394                                                                 security++) {
395                 struct security_data *secdata;
396                 gboolean interaction;
397
398                 if (security->privilege != method->privilege)
399                         continue;
400
401                 secdata = g_new(struct security_data, 1);
402                 secdata->pending = next_pending++;
403                 secdata->message = dbus_message_ref(msg);
404                 secdata->method = method;
405                 secdata->iface_user_data = iface_user_data;
406
407                 pending_security = g_slist_prepend(pending_security, secdata);
408
409                 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
410                         interaction = TRUE;
411                 else
412                         interaction = FALSE;
413
414                 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
415                                                         security->function)
416                         security->function(conn, security->action,
417                                                 interaction, secdata->pending);
418                 else
419                         builtin_security_function(conn, security->action,
420                                                 interaction, secdata->pending);
421
422                 return TRUE;
423         }
424
425         return FALSE;
426 }
427
428 static GDBusPendingPropertySet next_pending_property = 1;
429 static GSList *pending_property_set;
430
431 static struct property_data *remove_pending_property_data(
432                                                 GDBusPendingPropertySet id)
433 {
434         struct property_data *propdata;
435         GSList *l;
436
437         for (l = pending_property_set; l != NULL; l = l->next) {
438                 propdata = l->data;
439                 if (propdata->id != id)
440                         continue;
441
442                 break;
443         }
444
445         if (l == NULL)
446                 return NULL;
447
448         pending_property_set = g_slist_delete_link(pending_property_set, l);
449
450         return propdata;
451 }
452
453 void g_dbus_pending_property_success(GDBusPendingPropertySet id)
454 {
455         struct property_data *propdata;
456
457         propdata = remove_pending_property_data(id);
458         if (propdata == NULL)
459                 return;
460
461         g_dbus_send_reply(propdata->conn, propdata->message,
462                                                         DBUS_TYPE_INVALID);
463         dbus_message_unref(propdata->message);
464         g_free(propdata);
465 }
466
467 void g_dbus_pending_property_error_valist(GDBusPendingReply id,
468                                         const char *name, const char *format,
469                                         va_list args)
470 {
471         struct property_data *propdata;
472         DBusMessage *reply;
473
474         propdata = remove_pending_property_data(id);
475         if (propdata == NULL)
476                 return;
477
478         reply = g_dbus_create_error_valist(propdata->message, name, format,
479                                                                         args);
480         if (reply != NULL) {
481                 dbus_connection_send(propdata->conn, reply, NULL);
482                 dbus_message_unref(reply);
483         }
484
485         dbus_message_unref(propdata->message);
486         g_free(propdata);
487 }
488
489 void g_dbus_pending_property_error(GDBusPendingReply id, const char *name,
490                                                 const char *format, ...)
491 {
492         va_list args;
493
494         va_start(args, format);
495
496         g_dbus_pending_property_error_valist(id, name, format, args);
497
498         va_end(args);
499 }
500
501 static void reset_parent(gpointer data, gpointer user_data)
502 {
503         struct generic_data *child = data;
504         struct generic_data *parent = user_data;
505
506         child->parent = parent;
507 }
508
509 static void append_property(struct interface_data *iface,
510                         const GDBusPropertyTable *p, DBusMessageIter *dict)
511 {
512         DBusMessageIter entry, value;
513
514         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, NULL,
515                                                                 &entry);
516         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &p->name);
517         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, p->type,
518                                                                 &value);
519
520         p->get(p, &value, iface->user_data);
521
522         dbus_message_iter_close_container(&entry, &value);
523         dbus_message_iter_close_container(dict, &entry);
524 }
525
526 static void append_properties(struct interface_data *data,
527                                                         DBusMessageIter *iter)
528 {
529         DBusMessageIter dict;
530         const GDBusPropertyTable *p;
531
532         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
533                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
534                                 DBUS_TYPE_STRING_AS_STRING
535                                 DBUS_TYPE_VARIANT_AS_STRING
536                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
537
538         for (p = data->properties; p && p->name; p++) {
539                 if (check_experimental(p->flags,
540                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
541                         continue;
542
543                 if (p->get == NULL)
544                         continue;
545
546                 if (p->exists != NULL && !p->exists(p, data->user_data))
547                         continue;
548
549                 append_property(data, p, &dict);
550         }
551
552         dbus_message_iter_close_container(iter, &dict);
553 }
554
555 static void append_interface(gpointer data, gpointer user_data)
556 {
557         struct interface_data *iface = data;
558         DBusMessageIter *array = user_data;
559         DBusMessageIter entry;
560
561         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
562                                                                 &entry);
563         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
564         append_properties(data, &entry);
565         dbus_message_iter_close_container(array, &entry);
566 }
567
568 static void emit_interfaces_added(struct generic_data *data)
569 {
570         DBusMessage *signal;
571         DBusMessageIter iter, array;
572
573         if (root == NULL || data == root)
574                 return;
575
576         signal = dbus_message_new_signal(root->path,
577                                         DBUS_INTERFACE_OBJECT_MANAGER,
578                                         "InterfacesAdded");
579         if (signal == NULL)
580                 return;
581
582         dbus_message_iter_init_append(signal, &iter);
583         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
584                                                                 &data->path);
585
586         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
587                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
588                                 DBUS_TYPE_STRING_AS_STRING
589                                 DBUS_TYPE_ARRAY_AS_STRING
590                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
591                                 DBUS_TYPE_STRING_AS_STRING
592                                 DBUS_TYPE_VARIANT_AS_STRING
593                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
594                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
595
596         g_slist_foreach(data->added, append_interface, &array);
597         g_slist_free(data->added);
598         data->added = NULL;
599
600         dbus_message_iter_close_container(&iter, &array);
601
602         g_dbus_send_message(data->conn, signal);
603 }
604
605 static struct interface_data *find_interface(GSList *interfaces,
606                                                 const char *name)
607 {
608         GSList *list;
609
610         if (name == NULL)
611                 return NULL;
612
613         for (list = interfaces; list; list = list->next) {
614                 struct interface_data *iface = list->data;
615                 if (!strcmp(name, iface->name))
616                         return iface;
617         }
618
619         return NULL;
620 }
621
622 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
623                                                         DBusMessage *message)
624 {
625         const char *sig = dbus_message_get_signature(message);
626         const char *p = NULL;
627
628         for (; args && args->signature && *sig; args++) {
629                 p = args->signature;
630
631                 for (; *sig && *p; sig++, p++) {
632                         if (*p != *sig)
633                                 return FALSE;
634                 }
635         }
636
637         if (*sig || (p && *p) || (args && args->signature))
638                 return FALSE;
639
640         return TRUE;
641 }
642
643 static gboolean remove_interface(struct generic_data *data, const char *name)
644 {
645         struct interface_data *iface;
646
647         iface = find_interface(data->interfaces, name);
648         if (iface == NULL)
649                 return FALSE;
650
651         process_properties_from_interface(data, iface);
652
653         data->interfaces = g_slist_remove(data->interfaces, iface);
654
655         if (iface->destroy) {
656                 iface->destroy(iface->user_data);
657                 iface->user_data = NULL;
658         }
659
660         /*
661          * Interface being removed was just added, on the same mainloop
662          * iteration? Don't send any signal
663          */
664         if (g_slist_find(data->added, iface)) {
665                 data->added = g_slist_remove(data->added, iface);
666                 g_free(iface->name);
667                 g_free(iface);
668                 return TRUE;
669         }
670
671         if (data->parent == NULL) {
672                 g_free(iface->name);
673                 g_free(iface);
674                 return TRUE;
675         }
676
677         data->removed = g_slist_prepend(data->removed, iface->name);
678         g_free(iface);
679
680         if (data->process_id > 0)
681                 return TRUE;
682
683         data->process_id = g_idle_add(process_changes, data);
684
685         return TRUE;
686 }
687
688 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
689                                                 const char *child_path)
690 {
691         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
692         char *parent_path, *slash;
693
694         parent_path = g_strdup(child_path);
695         slash = strrchr(parent_path, '/');
696         if (slash == NULL)
697                 goto done;
698
699         if (slash == parent_path && parent_path[1] != '\0')
700                 parent_path[1] = '\0';
701         else
702                 *slash = '\0';
703
704         if (!strlen(parent_path))
705                 goto done;
706
707         if (dbus_connection_get_object_path_data(conn, parent_path,
708                                                         (void *) &data) == FALSE) {
709                 goto done;
710         }
711
712         parent = invalidate_parent_data(conn, parent_path);
713
714         if (data == NULL) {
715                 data = parent;
716                 if (data == NULL)
717                         goto done;
718         }
719
720         g_free(data->introspect);
721         data->introspect = NULL;
722
723         if (!dbus_connection_get_object_path_data(conn, child_path,
724                                                         (void *) &child))
725                 goto done;
726
727         if (child == NULL || g_slist_find(data->objects, child) != NULL)
728                 goto done;
729
730         data->objects = g_slist_prepend(data->objects, child);
731         child->parent = data;
732
733 done:
734         g_free(parent_path);
735         return data;
736 }
737
738 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
739                                                         const char *name)
740 {
741         const GDBusPropertyTable *p;
742
743         for (p = properties; p && p->name; p++) {
744                 if (strcmp(name, p->name) != 0)
745                         continue;
746
747                 if (check_experimental(p->flags,
748                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
749                         break;
750
751                 return p;
752         }
753
754         return NULL;
755 }
756
757 static DBusMessage *properties_get(DBusConnection *connection,
758                                         DBusMessage *message, void *user_data)
759 {
760         struct generic_data *data = user_data;
761         struct interface_data *iface;
762         const GDBusPropertyTable *property;
763         const char *interface, *name;
764         DBusMessageIter iter, value;
765         DBusMessage *reply;
766
767         if (!dbus_message_get_args(message, NULL,
768                                         DBUS_TYPE_STRING, &interface,
769                                         DBUS_TYPE_STRING, &name,
770                                         DBUS_TYPE_INVALID))
771                 return NULL;
772
773         iface = find_interface(data->interfaces, interface);
774         if (iface == NULL)
775                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
776                                 "No such interface '%s'", interface);
777
778         property = find_property(iface->properties, name);
779         if (property == NULL)
780                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
781                                 "No such property '%s'", name);
782
783         if (property->exists != NULL &&
784                         !property->exists(property, iface->user_data))
785                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
786                                         "No such property '%s'", name);
787
788         if (property->get == NULL)
789                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
790                                 "Property '%s' is not readable", name);
791
792         reply = dbus_message_new_method_return(message);
793         if (reply == NULL)
794                 return NULL;
795
796         dbus_message_iter_init_append(reply, &iter);
797         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
798                                                 property->type, &value);
799
800         if (!property->get(property, &value, iface->user_data)) {
801                 dbus_message_unref(reply);
802                 return NULL;
803         }
804
805         dbus_message_iter_close_container(&iter, &value);
806
807         return reply;
808 }
809
810 static DBusMessage *properties_get_all(DBusConnection *connection,
811                                         DBusMessage *message, void *user_data)
812 {
813         struct generic_data *data = user_data;
814         struct interface_data *iface;
815         const char *interface;
816         DBusMessageIter iter;
817         DBusMessage *reply;
818
819         if (!dbus_message_get_args(message, NULL,
820                                         DBUS_TYPE_STRING, &interface,
821                                         DBUS_TYPE_INVALID))
822                 return NULL;
823
824         iface = find_interface(data->interfaces, interface);
825         if (iface == NULL)
826                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
827                                         "No such interface '%s'", interface);
828
829         reply = dbus_message_new_method_return(message);
830         if (reply == NULL)
831                 return NULL;
832
833         dbus_message_iter_init_append(reply, &iter);
834
835         append_properties(iface, &iter);
836
837         return reply;
838 }
839
840 static DBusMessage *properties_set(DBusConnection *connection,
841                                         DBusMessage *message, void *user_data)
842 {
843         struct generic_data *data = user_data;
844         DBusMessageIter iter, sub;
845         struct interface_data *iface;
846         const GDBusPropertyTable *property;
847         const char *name, *interface;
848         struct property_data *propdata;
849
850         if (!dbus_message_iter_init(message, &iter))
851                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
852                                                         "No arguments given");
853
854         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
855                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
856                                         "Invalid argument type: '%c'",
857                                         dbus_message_iter_get_arg_type(&iter));
858
859         dbus_message_iter_get_basic(&iter, &interface);
860         dbus_message_iter_next(&iter);
861
862         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
863                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
864                                         "Invalid argument type: '%c'",
865                                         dbus_message_iter_get_arg_type(&iter));
866
867         dbus_message_iter_get_basic(&iter, &name);
868         dbus_message_iter_next(&iter);
869
870         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
871                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
872                                         "Invalid argument type: '%c'",
873                                         dbus_message_iter_get_arg_type(&iter));
874
875         dbus_message_iter_recurse(&iter, &sub);
876
877         iface = find_interface(data->interfaces, interface);
878         if (iface == NULL)
879                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
880                                         "No such interface '%s'", interface);
881
882         property = find_property(iface->properties, name);
883         if (property == NULL)
884                 return g_dbus_create_error(message,
885                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
886                                                 "No such property '%s'", name);
887
888         if (property->set == NULL)
889                 return g_dbus_create_error(message,
890                                         DBUS_ERROR_PROPERTY_READ_ONLY,
891                                         "Property '%s' is not writable", name);
892
893         if (property->exists != NULL &&
894                         !property->exists(property, iface->user_data))
895                 return g_dbus_create_error(message,
896                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
897                                                 "No such property '%s'", name);
898
899         propdata = g_new(struct property_data, 1);
900         propdata->id = next_pending_property++;
901         propdata->message = dbus_message_ref(message);
902         propdata->conn = connection;
903         pending_property_set = g_slist_prepend(pending_property_set, propdata);
904
905         property->set(property, &sub, propdata->id, iface->user_data);
906
907         return NULL;
908 }
909
910 static const GDBusMethodTable properties_methods[] = {
911         { GDBUS_METHOD("Get",
912                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
913                         GDBUS_ARGS({ "value", "v" }),
914                         properties_get) },
915         { GDBUS_ASYNC_METHOD("Set",
916                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
917                                                         { "value", "v" }),
918                         NULL,
919                         properties_set) },
920         { GDBUS_METHOD("GetAll",
921                         GDBUS_ARGS({ "interface", "s" }),
922                         GDBUS_ARGS({ "properties", "a{sv}" }),
923                         properties_get_all) },
924         { }
925 };
926
927 static const GDBusSignalTable properties_signals[] = {
928         { GDBUS_SIGNAL("PropertiesChanged",
929                         GDBUS_ARGS({ "interface", "s" },
930                                         { "changed_properties", "a{sv}" },
931                                         { "invalidated_properties", "as"})) },
932         { }
933 };
934
935 static void append_name(gpointer data, gpointer user_data)
936 {
937         char *name = data;
938         DBusMessageIter *iter = user_data;
939
940         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
941 }
942
943 static void emit_interfaces_removed(struct generic_data *data)
944 {
945         DBusMessage *signal;
946         DBusMessageIter iter, array;
947
948         if (root == NULL || data == root)
949                 return;
950
951         signal = dbus_message_new_signal(root->path,
952                                         DBUS_INTERFACE_OBJECT_MANAGER,
953                                         "InterfacesRemoved");
954         if (signal == NULL)
955                 return;
956
957         dbus_message_iter_init_append(signal, &iter);
958         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
959                                                                 &data->path);
960         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
961                                         DBUS_TYPE_STRING_AS_STRING, &array);
962
963         g_slist_foreach(data->removed, append_name, &array);
964         g_slist_free_full(data->removed, g_free);
965         data->removed = NULL;
966
967         dbus_message_iter_close_container(&iter, &array);
968
969         g_dbus_send_message(data->conn, signal);
970 }
971
972 static gboolean process_changes(gpointer user_data)
973 {
974         struct generic_data *data = user_data;
975
976         data->process_id = 0;
977
978         if (data->added != NULL)
979                 emit_interfaces_added(data);
980
981         /* Flush pending properties */
982         if (data->pending_prop == TRUE)
983                 process_property_changes(data);
984
985         if (data->removed != NULL)
986                 emit_interfaces_removed(data);
987
988         return FALSE;
989 }
990
991 static void generic_unregister(DBusConnection *connection, void *user_data)
992 {
993         struct generic_data *data = user_data;
994         struct generic_data *parent = data->parent;
995
996         if (parent != NULL)
997                 parent->objects = g_slist_remove(parent->objects, data);
998
999         if (data->process_id > 0) {
1000                 g_source_remove(data->process_id);
1001                 process_changes(data);
1002         }
1003
1004         g_slist_foreach(data->objects, reset_parent, data->parent);
1005         g_slist_free(data->objects);
1006
1007         dbus_connection_unref(data->conn);
1008         g_free(data->introspect);
1009         g_free(data->path);
1010         g_free(data);
1011 }
1012
1013 static DBusHandlerResult generic_message(DBusConnection *connection,
1014                                         DBusMessage *message, void *user_data)
1015 {
1016         struct generic_data *data = user_data;
1017         struct interface_data *iface;
1018         const GDBusMethodTable *method;
1019         const char *interface;
1020
1021         interface = dbus_message_get_interface(message);
1022
1023         iface = find_interface(data->interfaces, interface);
1024         if (iface == NULL)
1025                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1026
1027         for (method = iface->methods; method &&
1028                         method->name && method->function; method++) {
1029
1030                 if (dbus_message_is_method_call(message, iface->name,
1031                                                         method->name) == FALSE)
1032                         continue;
1033
1034                 if (check_experimental(method->flags,
1035                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1036                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1037
1038                 if (g_dbus_args_have_signature(method->in_args,
1039                                                         message) == FALSE)
1040                         continue;
1041
1042                 if (check_privilege(connection, message, method,
1043                                                 iface->user_data) == TRUE)
1044                         return DBUS_HANDLER_RESULT_HANDLED;
1045
1046                 return process_message(connection, message, method,
1047                                                         iface->user_data);
1048         }
1049
1050         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1051 }
1052
1053 static DBusObjectPathVTable generic_table = {
1054         .unregister_function    = generic_unregister,
1055         .message_function       = generic_message,
1056 };
1057
1058 static const GDBusMethodTable introspect_methods[] = {
1059         { GDBUS_METHOD("Introspect", NULL,
1060                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1061         { }
1062 };
1063
1064 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1065 {
1066         DBusMessageIter array;
1067
1068         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1069                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1070                                 DBUS_TYPE_STRING_AS_STRING
1071                                 DBUS_TYPE_ARRAY_AS_STRING
1072                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1073                                 DBUS_TYPE_STRING_AS_STRING
1074                                 DBUS_TYPE_VARIANT_AS_STRING
1075                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1076                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1077
1078         g_slist_foreach(data->interfaces, append_interface, &array);
1079
1080         dbus_message_iter_close_container(iter, &array);
1081 }
1082
1083 static void append_object(gpointer data, gpointer user_data)
1084 {
1085         struct generic_data *child = data;
1086         DBusMessageIter *array = user_data;
1087         DBusMessageIter entry;
1088
1089         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1090                                                                 &entry);
1091         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1092                                                                 &child->path);
1093         append_interfaces(child, &entry);
1094         dbus_message_iter_close_container(array, &entry);
1095
1096         g_slist_foreach(child->objects, append_object, user_data);
1097 }
1098
1099 static DBusMessage *get_objects(DBusConnection *connection,
1100                                 DBusMessage *message, void *user_data)
1101 {
1102         struct generic_data *data = user_data;
1103         DBusMessage *reply;
1104         DBusMessageIter iter;
1105         DBusMessageIter array;
1106
1107         reply = dbus_message_new_method_return(message);
1108         if (reply == NULL)
1109                 return NULL;
1110
1111         dbus_message_iter_init_append(reply, &iter);
1112
1113         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1114                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1115                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1116                                         DBUS_TYPE_ARRAY_AS_STRING
1117                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1118                                         DBUS_TYPE_STRING_AS_STRING
1119                                         DBUS_TYPE_ARRAY_AS_STRING
1120                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1121                                         DBUS_TYPE_STRING_AS_STRING
1122                                         DBUS_TYPE_VARIANT_AS_STRING
1123                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1124                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1125                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1126                                         &array);
1127
1128         g_slist_foreach(data->objects, append_object, &array);
1129
1130         dbus_message_iter_close_container(&iter, &array);
1131
1132         return reply;
1133 }
1134
1135 static const GDBusMethodTable manager_methods[] = {
1136         { GDBUS_METHOD("GetManagedObjects", NULL,
1137                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1138         { }
1139 };
1140
1141 static const GDBusSignalTable manager_signals[] = {
1142         { GDBUS_SIGNAL("InterfacesAdded",
1143                 GDBUS_ARGS({ "object", "o" },
1144                                 { "interfaces", "a{sa{sv}}" })) },
1145         { GDBUS_SIGNAL("InterfacesRemoved",
1146                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1147         { }
1148 };
1149
1150 static gboolean add_interface(struct generic_data *data,
1151                                 const char *name,
1152                                 const GDBusMethodTable *methods,
1153                                 const GDBusSignalTable *signals,
1154                                 const GDBusPropertyTable *properties,
1155                                 void *user_data,
1156                                 GDBusDestroyFunction destroy)
1157 {
1158         struct interface_data *iface;
1159         const GDBusMethodTable *method;
1160         const GDBusSignalTable *signal;
1161         const GDBusPropertyTable *property;
1162
1163         for (method = methods; method && method->name; method++) {
1164                 if (!check_experimental(method->flags,
1165                                         G_DBUS_METHOD_FLAG_EXPERIMENTAL))
1166                         goto done;
1167         }
1168
1169         for (signal = signals; signal && signal->name; signal++) {
1170                 if (!check_experimental(signal->flags,
1171                                         G_DBUS_SIGNAL_FLAG_EXPERIMENTAL))
1172                         goto done;
1173         }
1174
1175         for (property = properties; property && property->name; property++) {
1176                 if (!check_experimental(property->flags,
1177                                         G_DBUS_PROPERTY_FLAG_EXPERIMENTAL))
1178                         goto done;
1179         }
1180
1181         /* Nothing to register */
1182         return FALSE;
1183
1184 done:
1185         iface = g_new0(struct interface_data, 1);
1186         iface->name = g_strdup(name);
1187         iface->methods = methods;
1188         iface->signals = signals;
1189         iface->properties = properties;
1190         iface->user_data = user_data;
1191         iface->destroy = destroy;
1192
1193         data->interfaces = g_slist_append(data->interfaces, iface);
1194         if (data->parent == NULL)
1195                 return TRUE;
1196
1197         data->added = g_slist_append(data->added, iface);
1198         if (data->process_id > 0)
1199                 return TRUE;
1200
1201         data->process_id = g_idle_add(process_changes, data);
1202
1203         return TRUE;
1204 }
1205
1206 static struct generic_data *object_path_ref(DBusConnection *connection,
1207                                                         const char *path)
1208 {
1209         struct generic_data *data;
1210
1211         if (dbus_connection_get_object_path_data(connection, path,
1212                                                 (void *) &data) == TRUE) {
1213                 if (data != NULL) {
1214                         data->refcount++;
1215                         return data;
1216                 }
1217         }
1218
1219         data = g_new0(struct generic_data, 1);
1220         data->conn = dbus_connection_ref(connection);
1221         data->path = g_strdup(path);
1222         data->refcount = 1;
1223
1224         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1225
1226         if (!dbus_connection_register_object_path(connection, path,
1227                                                 &generic_table, data)) {
1228                 g_free(data->introspect);
1229                 g_free(data);
1230                 return NULL;
1231         }
1232
1233         invalidate_parent_data(connection, path);
1234
1235         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1236                                                 NULL, NULL, data, NULL);
1237
1238         return data;
1239 }
1240
1241 static void object_path_unref(DBusConnection *connection, const char *path)
1242 {
1243         struct generic_data *data = NULL;
1244
1245         if (dbus_connection_get_object_path_data(connection, path,
1246                                                 (void *) &data) == FALSE)
1247                 return;
1248
1249         if (data == NULL)
1250                 return;
1251
1252         data->refcount--;
1253
1254         if (data->refcount > 0)
1255                 return;
1256
1257         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1258         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1259
1260         invalidate_parent_data(data->conn, data->path);
1261
1262         dbus_connection_unregister_object_path(data->conn, data->path);
1263 }
1264
1265 static gboolean check_signal(DBusConnection *conn, const char *path,
1266                                 const char *interface, const char *name,
1267                                 const GDBusArgInfo **args)
1268 {
1269         struct generic_data *data = NULL;
1270         struct interface_data *iface;
1271         const GDBusSignalTable *signal;
1272
1273         *args = NULL;
1274         if (!dbus_connection_get_object_path_data(conn, path,
1275                                         (void *) &data) || data == NULL) {
1276                 error("dbus_connection_emit_signal: path %s isn't registered",
1277                                 path);
1278                 return FALSE;
1279         }
1280
1281         iface = find_interface(data->interfaces, interface);
1282         if (iface == NULL) {
1283                 error("dbus_connection_emit_signal: %s does not implement %s",
1284                                 path, interface);
1285                 return FALSE;
1286         }
1287
1288         for (signal = iface->signals; signal && signal->name; signal++) {
1289                 if (strcmp(signal->name, name) != 0)
1290                         continue;
1291
1292                 if (signal->flags & G_DBUS_SIGNAL_FLAG_EXPERIMENTAL) {
1293                         const char *env = g_getenv("GDBUS_EXPERIMENTAL");
1294                         if (g_strcmp0(env, "1") != 0)
1295                                 break;
1296                 }
1297
1298                 *args = signal->args;
1299                 return TRUE;
1300         }
1301
1302         error("No signal named %s on interface %s", name, interface);
1303         return FALSE;
1304 }
1305
1306 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1307                                                 const char *path,
1308                                                 const char *interface,
1309                                                 const char *name,
1310                                                 int first,
1311                                                 va_list var_args)
1312 {
1313         DBusMessage *signal;
1314         dbus_bool_t ret;
1315         const GDBusArgInfo *args;
1316
1317         if (!check_signal(conn, path, interface, name, &args))
1318                 return FALSE;
1319
1320         signal = dbus_message_new_signal(path, interface, name);
1321         if (signal == NULL) {
1322                 error("Unable to allocate new %s.%s signal", interface,  name);
1323                 return FALSE;
1324         }
1325
1326         ret = dbus_message_append_args_valist(signal, first, var_args);
1327         if (!ret)
1328                 goto fail;
1329
1330         if (g_dbus_args_have_signature(args, signal) == FALSE) {
1331                 error("%s.%s: got unexpected signature '%s'", interface, name,
1332                                         dbus_message_get_signature(signal));
1333                 ret = FALSE;
1334                 goto fail;
1335         }
1336
1337         ret = dbus_connection_send(conn, signal, NULL);
1338
1339 fail:
1340         dbus_message_unref(signal);
1341
1342         return ret;
1343 }
1344
1345 gboolean g_dbus_register_interface(DBusConnection *connection,
1346                                         const char *path, const char *name,
1347                                         const GDBusMethodTable *methods,
1348                                         const GDBusSignalTable *signals,
1349                                         const GDBusPropertyTable *properties,
1350                                         void *user_data,
1351                                         GDBusDestroyFunction destroy)
1352 {
1353         struct generic_data *data;
1354
1355         data = object_path_ref(connection, path);
1356         if (data == NULL)
1357                 return FALSE;
1358
1359         if (find_interface(data->interfaces, name)) {
1360                 object_path_unref(connection, path);
1361                 return FALSE;
1362         }
1363
1364         if (!add_interface(data, name, methods, signals, properties, user_data,
1365                                                                 destroy)) {
1366                 object_path_unref(connection, path);
1367                 return FALSE;
1368         }
1369
1370         if (properties != NULL && !find_interface(data->interfaces,
1371                                                 DBUS_INTERFACE_PROPERTIES))
1372                 add_interface(data, DBUS_INTERFACE_PROPERTIES,
1373                                 properties_methods, properties_signals, NULL,
1374                                 data, NULL);
1375
1376         g_free(data->introspect);
1377         data->introspect = NULL;
1378
1379         return TRUE;
1380 }
1381
1382 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1383                                         const char *path, const char *name)
1384 {
1385         struct generic_data *data = NULL;
1386
1387         if (path == NULL)
1388                 return FALSE;
1389
1390         if (dbus_connection_get_object_path_data(connection, path,
1391                                                 (void *) &data) == FALSE)
1392                 return FALSE;
1393
1394         if (data == NULL)
1395                 return FALSE;
1396
1397         if (remove_interface(data, name) == FALSE)
1398                 return FALSE;
1399
1400         g_free(data->introspect);
1401         data->introspect = NULL;
1402
1403         object_path_unref(connection, data->path);
1404
1405         return TRUE;
1406 }
1407
1408 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1409 {
1410         if (security_table != NULL)
1411                 return FALSE;
1412
1413         security_table = security;
1414
1415         return TRUE;
1416 }
1417
1418 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1419 {
1420         security_table = NULL;
1421
1422         return TRUE;
1423 }
1424
1425 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1426                                         const char *format, va_list args)
1427 {
1428         char str[1024];
1429
1430         vsnprintf(str, sizeof(str), format, args);
1431
1432         return dbus_message_new_error(message, name, str);
1433 }
1434
1435 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1436                                                 const char *format, ...)
1437 {
1438         va_list args;
1439         DBusMessage *reply;
1440
1441         va_start(args, format);
1442
1443         reply = g_dbus_create_error_valist(message, name, format, args);
1444
1445         va_end(args);
1446
1447         return reply;
1448 }
1449
1450 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1451                                                 int type, va_list args)
1452 {
1453         DBusMessage *reply;
1454
1455         reply = dbus_message_new_method_return(message);
1456         if (reply == NULL)
1457                 return NULL;
1458
1459         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1460                 dbus_message_unref(reply);
1461                 return NULL;
1462         }
1463
1464         return reply;
1465 }
1466
1467 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1468 {
1469         va_list args;
1470         DBusMessage *reply;
1471
1472         va_start(args, type);
1473
1474         reply = g_dbus_create_reply_valist(message, type, args);
1475
1476         va_end(args);
1477
1478         return reply;
1479 }
1480
1481 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1482 {
1483         dbus_bool_t result;
1484
1485         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1486                 dbus_message_set_no_reply(message, TRUE);
1487         else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL) {
1488                 const char *path = dbus_message_get_path(message);
1489                 const char *interface = dbus_message_get_interface(message);
1490                 const char *name = dbus_message_get_member(message);
1491                 const GDBusArgInfo *args;
1492
1493                 if (!check_signal(connection, path, interface, name, &args))
1494                         return FALSE;
1495         }
1496
1497         result = dbus_connection_send(connection, message, NULL);
1498
1499         dbus_message_unref(message);
1500
1501         return result;
1502 }
1503
1504 gboolean g_dbus_send_error_valist(DBusConnection *connection,
1505                                         DBusMessage *message, const char *name,
1506                                         const char *format, va_list args)
1507 {
1508         DBusMessage *error;
1509         char str[1024];
1510
1511         vsnprintf(str, sizeof(str), format, args);
1512
1513         error = dbus_message_new_error(message, name, str);
1514         if (error == NULL)
1515                 return FALSE;
1516
1517         return g_dbus_send_message(connection, error);
1518 }
1519
1520 gboolean g_dbus_send_error(DBusConnection *connection, DBusMessage *message,
1521                                 const char *name, const char *format, ...)
1522 {
1523         va_list args;
1524         gboolean result;
1525
1526         va_start(args, format);
1527
1528         result = g_dbus_send_error_valist(connection, message, name,
1529                                                         format, args);
1530
1531         va_end(args);
1532
1533         return result;
1534 }
1535
1536 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1537                                 DBusMessage *message, int type, va_list args)
1538 {
1539         DBusMessage *reply;
1540
1541         reply = dbus_message_new_method_return(message);
1542         if (reply == NULL)
1543                 return FALSE;
1544
1545         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1546                 dbus_message_unref(reply);
1547                 return FALSE;
1548         }
1549
1550         return g_dbus_send_message(connection, reply);
1551 }
1552
1553 gboolean g_dbus_send_reply(DBusConnection *connection,
1554                                 DBusMessage *message, int type, ...)
1555 {
1556         va_list args;
1557         gboolean result;
1558
1559         va_start(args, type);
1560
1561         result = g_dbus_send_reply_valist(connection, message, type, args);
1562
1563         va_end(args);
1564
1565         return result;
1566 }
1567
1568 gboolean g_dbus_emit_signal(DBusConnection *connection,
1569                                 const char *path, const char *interface,
1570                                 const char *name, int type, ...)
1571 {
1572         va_list args;
1573         gboolean result;
1574
1575         va_start(args, type);
1576
1577         result = emit_signal_valist(connection, path, interface,
1578                                                         name, type, args);
1579
1580         va_end(args);
1581
1582         return result;
1583 }
1584
1585 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1586                                 const char *path, const char *interface,
1587                                 const char *name, int type, va_list args)
1588 {
1589         return emit_signal_valist(connection, path, interface,
1590                                                         name, type, args);
1591 }
1592
1593 static void process_properties_from_interface(struct generic_data *data,
1594                                                 struct interface_data *iface)
1595 {
1596         GSList *l;
1597         DBusMessage *signal;
1598         DBusMessageIter iter, dict, array;
1599         GSList *invalidated;
1600
1601         if (iface->pending_prop == NULL)
1602                 return;
1603
1604         signal = dbus_message_new_signal(data->path,
1605                         DBUS_INTERFACE_PROPERTIES, "PropertiesChanged");
1606         if (signal == NULL) {
1607                 error("Unable to allocate new " DBUS_INTERFACE_PROPERTIES
1608                                                 ".PropertiesChanged signal");
1609                 return;
1610         }
1611
1612         iface->pending_prop = g_slist_reverse(iface->pending_prop);
1613
1614         dbus_message_iter_init_append(signal, &iter);
1615         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &iface->name);
1616         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1617                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1618                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
1619                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
1620
1621         invalidated = NULL;
1622
1623         for (l = iface->pending_prop; l != NULL; l = l->next) {
1624                 GDBusPropertyTable *p = l->data;
1625
1626                 if (p->get == NULL)
1627                         continue;
1628
1629                 if (p->exists != NULL && !p->exists(p, iface->user_data)) {
1630                         invalidated = g_slist_prepend(invalidated, p);
1631                         continue;
1632                 }
1633
1634                 append_property(iface, p, &dict);
1635         }
1636
1637         dbus_message_iter_close_container(&iter, &dict);
1638
1639         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1640                                 DBUS_TYPE_STRING_AS_STRING, &array);
1641         for (l = invalidated; l != NULL; l = g_slist_next(l)) {
1642                 GDBusPropertyTable *p = l->data;
1643
1644                 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
1645                                                                 &p->name);
1646         }
1647         g_slist_free(invalidated);
1648         dbus_message_iter_close_container(&iter, &array);
1649
1650         g_dbus_send_message(data->conn, signal);
1651
1652         g_slist_free(iface->pending_prop);
1653         iface->pending_prop = NULL;
1654 }
1655
1656 static void process_property_changes(struct generic_data *data)
1657 {
1658         GSList *l;
1659
1660         for (l = data->interfaces; l != NULL; l = l->next) {
1661                 struct interface_data *iface = l->data;
1662
1663                 process_properties_from_interface(data, iface);
1664         }
1665
1666         data->pending_prop = FALSE;
1667 }
1668
1669 void g_dbus_emit_property_changed(DBusConnection *connection,
1670                                 const char *path, const char *interface,
1671                                 const char *name)
1672 {
1673         const GDBusPropertyTable *property;
1674         struct generic_data *data;
1675         struct interface_data *iface;
1676
1677         if (!dbus_connection_get_object_path_data(connection, path,
1678                                         (void **) &data) || data == NULL)
1679                 return;
1680
1681         iface = find_interface(data->interfaces, interface);
1682         if (iface == NULL)
1683                 return;
1684
1685         property = find_property(iface->properties, name);
1686         if (property == NULL) {
1687                 error("Could not find property %s in %p", name,
1688                                                         iface->properties);
1689                 return;
1690         }
1691
1692         if (g_slist_find(iface->pending_prop, (void *) property) != NULL)
1693                 return;
1694
1695         data->pending_prop = TRUE;
1696         iface->pending_prop = g_slist_prepend(iface->pending_prop,
1697                                                 (void *) property);
1698
1699         if (!data->process_id) {
1700                 data->process_id = g_idle_add(process_changes, data);
1701                 return;
1702         }
1703 }
1704
1705 gboolean g_dbus_get_properties(DBusConnection *connection, const char *path,
1706                                 const char *interface, DBusMessageIter *iter)
1707 {
1708         struct generic_data *data;
1709         struct interface_data *iface;
1710
1711         if (!dbus_connection_get_object_path_data(connection, path,
1712                                         (void **) &data) || data == NULL)
1713                 return FALSE;
1714
1715         iface = find_interface(data->interfaces, interface);
1716         if (iface == NULL)
1717                 return FALSE;
1718
1719         append_properties(iface, iter);
1720
1721         return TRUE;
1722 }
1723
1724 gboolean g_dbus_attach_object_manager(DBusConnection *connection)
1725 {
1726         struct generic_data *data;
1727
1728         data = object_path_ref(connection, "/");
1729         if (data == NULL)
1730                 return FALSE;
1731
1732         add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1733                                         manager_methods, manager_signals,
1734                                         NULL, data, NULL);
1735         root = data;
1736
1737         return TRUE;
1738 }
1739
1740 gboolean g_dbus_detach_object_manager(DBusConnection *connection)
1741 {
1742         if (!g_dbus_unregister_interface(connection, "/",
1743                                         DBUS_INTERFACE_OBJECT_MANAGER))
1744                 return FALSE;
1745
1746         root = NULL;
1747
1748         return TRUE;
1749 }
1750
1751 void g_dbus_set_flags(int flags)
1752 {
1753         global_flags = flags;
1754 }