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