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