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