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