Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / gdbus / client.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 <glib.h>
30 #include <dbus/dbus.h>
31
32 #include "gdbus.h"
33
34 #define METHOD_CALL_TIMEOUT (300 * 1000)
35
36 #ifndef DBUS_INTERFACE_OBJECT_MANAGER
37 #define DBUS_INTERFACE_OBJECT_MANAGER DBUS_INTERFACE_DBUS ".ObjectManager"
38 #endif
39
40 struct GDBusClient {
41         int ref_count;
42         DBusConnection *dbus_conn;
43         char *service_name;
44         char *base_path;
45         char *root_path;
46         guint watch;
47         guint added_watch;
48         guint removed_watch;
49         GPtrArray *match_rules;
50         DBusPendingCall *pending_call;
51         DBusPendingCall *get_objects_call;
52         GDBusWatchFunction connect_func;
53         void *connect_data;
54         GDBusWatchFunction disconn_func;
55         gboolean connected;
56         void *disconn_data;
57         GDBusMessageFunction signal_func;
58         void *signal_data;
59         GDBusProxyFunction proxy_added;
60         GDBusProxyFunction proxy_removed;
61         GDBusClientFunction ready;
62         void *ready_data;
63         GDBusPropertyFunction property_changed;
64         void *user_data;
65         GList *proxy_list;
66 };
67
68 struct GDBusProxy {
69         int ref_count;
70         GDBusClient *client;
71         char *obj_path;
72         char *interface;
73         GHashTable *prop_list;
74         guint watch;
75         GDBusPropertyFunction prop_func;
76         void *prop_data;
77         GDBusProxyFunction removed_func;
78         void *removed_data;
79 };
80
81 struct prop_entry {
82         char *name;
83         int type;
84         DBusMessage *msg;
85 };
86
87 static void modify_match_reply(DBusPendingCall *call, void *user_data)
88 {
89         DBusMessage *reply = dbus_pending_call_steal_reply(call);
90         DBusError error;
91
92         dbus_error_init(&error);
93
94         if (dbus_set_error_from_message(&error, reply) == TRUE)
95                 dbus_error_free(&error);
96
97         dbus_message_unref(reply);
98 }
99
100 static gboolean modify_match(DBusConnection *conn, const char *member,
101                                                         const char *rule)
102 {
103         DBusMessage *msg;
104         DBusPendingCall *call;
105
106         msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
107                                         DBUS_INTERFACE_DBUS, member);
108         if (msg == NULL)
109                 return FALSE;
110
111         dbus_message_append_args(msg, DBUS_TYPE_STRING, &rule,
112                                                 DBUS_TYPE_INVALID);
113
114         if (g_dbus_send_message_with_reply(conn, msg, &call, -1) == FALSE) {
115                 dbus_message_unref(msg);
116                 return FALSE;
117         }
118
119         dbus_pending_call_set_notify(call, modify_match_reply, NULL, NULL);
120         dbus_pending_call_unref(call);
121
122         dbus_message_unref(msg);
123
124         return TRUE;
125 }
126
127 static void iter_append_iter(DBusMessageIter *base, DBusMessageIter *iter)
128 {
129         int type;
130
131         type = dbus_message_iter_get_arg_type(iter);
132
133         if (dbus_type_is_basic(type)) {
134                 const void *value;
135
136                 dbus_message_iter_get_basic(iter, &value);
137                 dbus_message_iter_append_basic(base, type, &value);
138         } else if (dbus_type_is_container(type)) {
139                 DBusMessageIter iter_sub, base_sub;
140                 char *sig;
141
142                 dbus_message_iter_recurse(iter, &iter_sub);
143
144                 switch (type) {
145                 case DBUS_TYPE_ARRAY:
146                 case DBUS_TYPE_VARIANT:
147                         sig = dbus_message_iter_get_signature(&iter_sub);
148                         break;
149                 default:
150                         sig = NULL;
151                         break;
152                 }
153
154                 dbus_message_iter_open_container(base, type, sig, &base_sub);
155
156                 if (sig != NULL)
157                         dbus_free(sig);
158
159                 while (dbus_message_iter_get_arg_type(&iter_sub) !=
160                                                         DBUS_TYPE_INVALID) {
161                         iter_append_iter(&base_sub, &iter_sub);
162                         dbus_message_iter_next(&iter_sub);
163                 }
164
165                 dbus_message_iter_close_container(base, &base_sub);
166         }
167 }
168
169 static void prop_entry_update(struct prop_entry *prop, DBusMessageIter *iter)
170 {
171         DBusMessage *msg;
172         DBusMessageIter base;
173
174         msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN);
175         if (msg == NULL)
176                 return;
177
178         dbus_message_iter_init_append(msg, &base);
179         iter_append_iter(&base, iter);
180
181         if (prop->msg != NULL)
182                 dbus_message_unref(prop->msg);
183
184         prop->msg = dbus_message_copy(msg);
185         dbus_message_unref(msg);
186 }
187
188 static struct prop_entry *prop_entry_new(const char *name,
189                                                 DBusMessageIter *iter)
190 {
191         struct prop_entry *prop;
192
193         prop = g_try_new0(struct prop_entry, 1);
194         if (prop == NULL)
195                 return NULL;
196
197         prop->name = g_strdup(name);
198         prop->type = dbus_message_iter_get_arg_type(iter);
199
200         prop_entry_update(prop, iter);
201
202         return prop;
203 }
204
205 static void prop_entry_free(gpointer data)
206 {
207         struct prop_entry *prop = data;
208
209         if (prop->msg != NULL)
210                 dbus_message_unref(prop->msg);
211
212         g_free(prop->name);
213
214         g_free(prop);
215 }
216
217 static void add_property(GDBusProxy *proxy, const char *name,
218                                 DBusMessageIter *iter, gboolean send_changed)
219 {
220         GDBusClient *client = proxy->client;
221         DBusMessageIter value;
222         struct prop_entry *prop;
223
224         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT)
225                 return;
226
227         dbus_message_iter_recurse(iter, &value);
228
229         prop = g_hash_table_lookup(proxy->prop_list, name);
230         if (prop != NULL) {
231                 prop_entry_update(prop, &value);
232                 goto done;
233         }
234
235         prop = prop_entry_new(name, &value);
236         if (prop == NULL)
237                 return;
238
239         g_hash_table_replace(proxy->prop_list, prop->name, prop);
240
241 done:
242         if (proxy->prop_func)
243                 proxy->prop_func(proxy, name, &value, proxy->prop_data);
244
245         if (client == NULL || send_changed == FALSE)
246                 return;
247
248         if (client->property_changed)
249                 client->property_changed(proxy, name, &value,
250                                                         client->user_data);
251 }
252
253 static void update_properties(GDBusProxy *proxy, DBusMessageIter *iter,
254                                                         gboolean send_changed)
255 {
256         DBusMessageIter dict;
257
258         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
259                 return;
260
261         dbus_message_iter_recurse(iter, &dict);
262
263         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
264                 DBusMessageIter entry;
265                 const char *name;
266
267                 dbus_message_iter_recurse(&dict, &entry);
268
269                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
270                         break;
271
272                 dbus_message_iter_get_basic(&entry, &name);
273                 dbus_message_iter_next(&entry);
274
275                 add_property(proxy, name, &entry, send_changed);
276
277                 dbus_message_iter_next(&dict);
278         }
279 }
280
281 static void get_all_properties_reply(DBusPendingCall *call, void *user_data)
282 {
283         GDBusProxy *proxy = user_data;
284         GDBusClient *client = proxy->client;
285         DBusMessage *reply = dbus_pending_call_steal_reply(call);
286         DBusMessageIter iter;
287         DBusError error;
288
289         dbus_error_init(&error);
290
291         if (dbus_set_error_from_message(&error, reply) == TRUE) {
292                 dbus_error_free(&error);
293                 goto done;
294         }
295
296         dbus_message_iter_init(reply, &iter);
297
298         update_properties(proxy, &iter, FALSE);
299
300 done:
301         if (g_list_find(client->proxy_list, proxy) == NULL) {
302                 if (client->proxy_added)
303                         client->proxy_added(proxy, client->user_data);
304
305                 client->proxy_list = g_list_append(client->proxy_list, proxy);
306         }
307
308         dbus_message_unref(reply);
309
310         g_dbus_client_unref(client);
311 }
312
313 static void get_all_properties(GDBusProxy *proxy)
314 {
315         GDBusClient *client = proxy->client;
316         const char *service_name = client->service_name;
317         DBusMessage *msg;
318         DBusPendingCall *call;
319
320         msg = dbus_message_new_method_call(service_name, proxy->obj_path,
321                                         DBUS_INTERFACE_PROPERTIES, "GetAll");
322         if (msg == NULL)
323                 return;
324
325         dbus_message_append_args(msg, DBUS_TYPE_STRING, &proxy->interface,
326                                                         DBUS_TYPE_INVALID);
327
328         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
329                                                         &call, -1) == FALSE) {
330                 dbus_message_unref(msg);
331                 return;
332         }
333
334         g_dbus_client_ref(client);
335
336         dbus_pending_call_set_notify(call, get_all_properties_reply,
337                                                         proxy, NULL);
338         dbus_pending_call_unref(call);
339
340         dbus_message_unref(msg);
341 }
342
343 static GDBusProxy *proxy_lookup(GDBusClient *client, const char *path,
344                                                 const char *interface)
345 {
346         GList *list;
347
348         for (list = g_list_first(client->proxy_list); list;
349                                                 list = g_list_next(list)) {
350                 GDBusProxy *proxy = list->data;
351
352                 if (g_str_equal(proxy->interface, interface) == TRUE &&
353                                 g_str_equal(proxy->obj_path, path) == TRUE)
354                         return proxy;
355         }
356
357         return NULL;
358 }
359
360 static gboolean properties_changed(DBusConnection *conn, DBusMessage *msg,
361                                                         void *user_data)
362 {
363         GDBusProxy *proxy = user_data;
364         GDBusClient *client = proxy->client;
365         DBusMessageIter iter, entry;
366         const char *interface;
367
368         if (dbus_message_iter_init(msg, &iter) == FALSE)
369                 return TRUE;
370
371         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
372                 return TRUE;
373
374         dbus_message_iter_get_basic(&iter, &interface);
375         dbus_message_iter_next(&iter);
376
377         update_properties(proxy, &iter, TRUE);
378
379         dbus_message_iter_next(&iter);
380
381         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
382                 return TRUE;
383
384         dbus_message_iter_recurse(&iter, &entry);
385
386         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
387                 const char *name;
388
389                 dbus_message_iter_get_basic(&entry, &name);
390
391                 g_hash_table_remove(proxy->prop_list, name);
392
393                 if (proxy->prop_func)
394                         proxy->prop_func(proxy, name, NULL, proxy->prop_data);
395
396                 if (client->property_changed)
397                         client->property_changed(proxy, name, NULL,
398                                                         client->user_data);
399
400                 dbus_message_iter_next(&entry);
401         }
402
403         return TRUE;
404 }
405
406 static GDBusProxy *proxy_new(GDBusClient *client, const char *path,
407                                                 const char *interface)
408 {
409         GDBusProxy *proxy;
410
411         proxy = g_try_new0(GDBusProxy, 1);
412         if (proxy == NULL)
413                 return NULL;
414
415         proxy->client = client;
416         proxy->obj_path = g_strdup(path);
417         proxy->interface = g_strdup(interface);
418
419         proxy->prop_list = g_hash_table_new_full(g_str_hash, g_str_equal,
420                                                         NULL, prop_entry_free);
421         proxy->watch = g_dbus_add_properties_watch(client->dbus_conn,
422                                                         client->service_name,
423                                                         proxy->obj_path,
424                                                         proxy->interface,
425                                                         properties_changed,
426                                                         proxy, NULL);
427
428         return g_dbus_proxy_ref(proxy);
429 }
430
431 static void proxy_free(gpointer data)
432 {
433         GDBusProxy *proxy = data;
434
435         if (proxy->client) {
436                 GDBusClient *client = proxy->client;
437
438                 if (client->proxy_removed)
439                         client->proxy_removed(proxy, client->user_data);
440
441                 g_dbus_remove_watch(client->dbus_conn, proxy->watch);
442
443                 g_hash_table_remove_all(proxy->prop_list);
444
445                 proxy->client = NULL;
446         }
447
448         if (proxy->removed_func)
449                 proxy->removed_func(proxy, proxy->removed_data);
450
451         g_dbus_proxy_unref(proxy);
452 }
453
454 static void proxy_remove(GDBusClient *client, const char *path,
455                                                 const char *interface)
456 {
457         GList *list;
458
459         for (list = g_list_first(client->proxy_list); list;
460                                                 list = g_list_next(list)) {
461                 GDBusProxy *proxy = list->data;
462
463                 if (g_str_equal(proxy->interface, interface) == TRUE &&
464                                 g_str_equal(proxy->obj_path, path) == TRUE) {
465                         client->proxy_list =
466                                 g_list_delete_link(client->proxy_list, list);
467                         proxy_free(proxy);
468                         break;
469                 }
470         }
471 }
472
473 GDBusProxy *g_dbus_proxy_new(GDBusClient *client, const char *path,
474                                                         const char *interface)
475 {
476         GDBusProxy *proxy;
477
478         if (client == NULL)
479                 return NULL;
480
481         proxy = proxy_lookup(client, path, interface);
482         if (proxy)
483                 return g_dbus_proxy_ref(proxy);
484
485         proxy = proxy_new(client, path, interface);
486         if (proxy == NULL)
487                 return NULL;
488
489         get_all_properties(proxy);
490
491         return g_dbus_proxy_ref(proxy);
492 }
493
494 GDBusProxy *g_dbus_proxy_ref(GDBusProxy *proxy)
495 {
496         if (proxy == NULL)
497                 return NULL;
498
499         __sync_fetch_and_add(&proxy->ref_count, 1);
500
501         return proxy;
502 }
503
504 void g_dbus_proxy_unref(GDBusProxy *proxy)
505 {
506         if (proxy == NULL)
507                 return;
508
509         if (__sync_sub_and_fetch(&proxy->ref_count, 1) > 0)
510                 return;
511
512         g_hash_table_destroy(proxy->prop_list);
513
514         g_free(proxy->obj_path);
515         g_free(proxy->interface);
516
517         g_free(proxy);
518 }
519
520 const char *g_dbus_proxy_get_path(GDBusProxy *proxy)
521 {
522         if (proxy == NULL)
523                 return NULL;
524
525         return proxy->obj_path;
526 }
527
528 const char *g_dbus_proxy_get_interface(GDBusProxy *proxy)
529 {
530         if (proxy == NULL)
531                 return NULL;
532
533         return proxy->interface;
534 }
535
536 gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,
537                                                         DBusMessageIter *iter)
538 {
539         struct prop_entry *prop;
540
541         if (proxy == NULL || name == NULL)
542                 return FALSE;
543
544         prop = g_hash_table_lookup(proxy->prop_list, name);
545         if (prop == NULL)
546                 return FALSE;
547
548         if (prop->msg == NULL)
549                 return FALSE;
550
551         if (dbus_message_iter_init(prop->msg, iter) == FALSE)
552                 return FALSE;
553
554         return TRUE;
555 }
556
557 struct refresh_property_data {
558         GDBusProxy *proxy;
559         char *name;
560 };
561
562 static void refresh_property_free(gpointer user_data)
563 {
564         struct refresh_property_data *data = user_data;
565
566         g_free(data->name);
567         g_free(data);
568 }
569
570 static void refresh_property_reply(DBusPendingCall *call, void *user_data)
571 {
572         struct refresh_property_data *data = user_data;
573         DBusMessage *reply = dbus_pending_call_steal_reply(call);
574         DBusError error;
575
576         dbus_error_init(&error);
577
578         if (dbus_set_error_from_message(&error, reply) == FALSE) {
579                 DBusMessageIter iter;
580
581                 dbus_message_iter_init(reply, &iter);
582
583                 add_property(data->proxy, data->name, &iter, TRUE);
584         } else
585                 dbus_error_free(&error);
586
587         dbus_message_unref(reply);
588 }
589
590 gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name)
591 {
592         struct refresh_property_data *data;
593         GDBusClient *client;
594         DBusMessage *msg;
595         DBusMessageIter iter;
596         DBusPendingCall *call;
597
598         if (proxy == NULL || name == NULL)
599                 return FALSE;
600
601         client = proxy->client;
602         if (client == NULL)
603                 return FALSE;
604
605         data = g_try_new0(struct refresh_property_data, 1);
606         if (data == NULL)
607                 return FALSE;
608
609         data->proxy = proxy;
610         data->name = g_strdup(name);
611
612         msg = dbus_message_new_method_call(client->service_name,
613                         proxy->obj_path, DBUS_INTERFACE_PROPERTIES, "Get");
614         if (msg == NULL) {
615                 refresh_property_free(data);
616                 return FALSE;
617         }
618
619         dbus_message_iter_init_append(msg, &iter);
620         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
621                                                         &proxy->interface);
622         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &name);
623
624         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
625                                                         &call, -1) == FALSE) {
626                 dbus_message_unref(msg);
627                 refresh_property_free(data);
628                 return FALSE;
629         }
630
631         dbus_pending_call_set_notify(call, refresh_property_reply,
632                                                 data, refresh_property_free);
633         dbus_pending_call_unref(call);
634
635         dbus_message_unref(msg);
636
637         return TRUE;
638 }
639
640 struct set_property_data {
641         GDBusResultFunction function;
642         void *user_data;
643         GDBusDestroyFunction destroy;
644 };
645
646 static void set_property_reply(DBusPendingCall *call, void *user_data)
647 {
648         struct set_property_data *data = user_data;
649         DBusMessage *reply = dbus_pending_call_steal_reply(call);
650         DBusError error;
651
652         dbus_error_init(&error);
653
654         dbus_set_error_from_message(&error, reply);
655
656         if (data->function)
657                 data->function(&error, data->user_data);
658
659         if (data->destroy)
660                 data->destroy(data->user_data);
661
662         dbus_error_free(&error);
663
664         dbus_message_unref(reply);
665 }
666
667 gboolean g_dbus_proxy_set_property_basic(GDBusProxy *proxy,
668                                 const char *name, int type, const void *value,
669                                 GDBusResultFunction function, void *user_data,
670                                 GDBusDestroyFunction destroy)
671 {
672         struct set_property_data *data;
673         GDBusClient *client;
674         DBusMessage *msg;
675         DBusMessageIter iter, variant;
676         DBusPendingCall *call;
677         char type_as_str[2];
678
679         if (proxy == NULL || name == NULL || value == NULL)
680                 return FALSE;
681
682         if (dbus_type_is_basic(type) == FALSE)
683                 return FALSE;
684
685         client = proxy->client;
686         if (client == NULL)
687                 return FALSE;
688
689         data = g_try_new0(struct set_property_data, 1);
690         if (data == NULL)
691                 return FALSE;
692
693         data->function = function;
694         data->user_data = user_data;
695         data->destroy = destroy;
696
697         msg = dbus_message_new_method_call(client->service_name,
698                         proxy->obj_path, DBUS_INTERFACE_PROPERTIES, "Set");
699         if (msg == NULL) {
700                 g_free(data);
701                 return FALSE;
702         }
703
704         type_as_str[0] = (char) type;
705         type_as_str[1] = '\0';
706
707         dbus_message_iter_init_append(msg, &iter);
708         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
709                                                         &proxy->interface);
710         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &name);
711
712         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
713                                                 type_as_str, &variant);
714         dbus_message_iter_append_basic(&variant, type, value);
715         dbus_message_iter_close_container(&iter, &variant);
716
717         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
718                                                         &call, -1) == FALSE) {
719                 dbus_message_unref(msg);
720                 g_free(data);
721                 return FALSE;
722         }
723
724         dbus_pending_call_set_notify(call, set_property_reply, data, g_free);
725         dbus_pending_call_unref(call);
726
727         dbus_message_unref(msg);
728
729         return TRUE;
730 }
731
732 gboolean g_dbus_proxy_set_property_array(GDBusProxy *proxy,
733                                 const char *name, int type, const void *value,
734                                 size_t size, GDBusResultFunction function,
735                                 void *user_data, GDBusDestroyFunction destroy)
736 {
737         struct set_property_data *data;
738         GDBusClient *client;
739         DBusMessage *msg;
740         DBusMessageIter iter, variant, array;
741         DBusPendingCall *call;
742         char array_sig[3];
743         char type_sig[2];
744
745         if (!proxy || !name || !value)
746                 return FALSE;
747
748         if (!dbus_type_is_basic(type))
749                 return FALSE;
750
751         client = proxy->client;
752         if (!client)
753                 return FALSE;
754
755         data = g_try_new0(struct set_property_data, 1);
756         if (!data)
757                 return FALSE;
758
759         data->function = function;
760         data->user_data = user_data;
761         data->destroy = destroy;
762
763         msg = dbus_message_new_method_call(client->service_name,
764                                                 proxy->obj_path,
765                                                 DBUS_INTERFACE_PROPERTIES,
766                                                 "Set");
767         if (!msg) {
768                 g_free(data);
769                 return FALSE;
770         }
771
772         array_sig[0] = DBUS_TYPE_ARRAY;
773         array_sig[1] = (char) type;
774         array_sig[2] = '\0';
775
776         type_sig[0] = (char) type;
777         type_sig[1] = '\0';
778
779         dbus_message_iter_init_append(msg, &iter);
780         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
781                                                         &proxy->interface);
782         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &name);
783
784         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
785                                                         array_sig, &variant);
786
787         dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
788                                                         type_sig, &array);
789
790         if (dbus_type_is_fixed(type))
791                 dbus_message_iter_append_fixed_array(&array, type, &value,
792                                                                         size);
793         else if (type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) {
794                 const char **str = (const char **) value;
795                 size_t i;
796
797                 for (i = 0; i < size; i++)
798                         dbus_message_iter_append_basic(&array, type, &str[i]);
799         }
800
801         dbus_message_iter_close_container(&variant, &array);
802         dbus_message_iter_close_container(&iter, &variant);
803
804         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
805                                                         &call, -1) == FALSE) {
806                 dbus_message_unref(msg);
807                 g_free(data);
808                 return FALSE;
809         }
810
811         dbus_pending_call_set_notify(call, set_property_reply, data, g_free);
812         dbus_pending_call_unref(call);
813
814         dbus_message_unref(msg);
815
816         return TRUE;
817 }
818
819 struct method_call_data {
820         GDBusReturnFunction function;
821         void *user_data;
822         GDBusDestroyFunction destroy;
823 };
824
825 static void method_call_reply(DBusPendingCall *call, void *user_data)
826 {
827         struct method_call_data *data = user_data;
828         DBusMessage *reply = dbus_pending_call_steal_reply(call);
829
830         if (data->function)
831                 data->function(reply, data->user_data);
832
833         if (data->destroy)
834                 data->destroy(data->user_data);
835
836         dbus_message_unref(reply);
837 }
838
839 gboolean g_dbus_proxy_method_call(GDBusProxy *proxy, const char *method,
840                                 GDBusSetupFunction setup,
841                                 GDBusReturnFunction function, void *user_data,
842                                 GDBusDestroyFunction destroy)
843 {
844         struct method_call_data *data;
845         GDBusClient *client;
846         DBusMessage *msg;
847         DBusPendingCall *call;
848
849         if (proxy == NULL || method == NULL)
850                 return FALSE;
851
852         client = proxy->client;
853         if (client == NULL)
854                 return FALSE;
855
856         data = g_try_new0(struct method_call_data, 1);
857         if (data == NULL)
858                 return FALSE;
859
860         data->function = function;
861         data->user_data = user_data;
862         data->destroy = destroy;
863
864         msg = dbus_message_new_method_call(client->service_name,
865                                 proxy->obj_path, proxy->interface, method);
866         if (msg == NULL) {
867                 g_free(data);
868                 return FALSE;
869         }
870
871         if (setup) {
872                 DBusMessageIter iter;
873
874                 dbus_message_iter_init_append(msg, &iter);
875                 setup(&iter, data->user_data);
876         }
877
878         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
879                                         &call, METHOD_CALL_TIMEOUT) == FALSE) {
880                 dbus_message_unref(msg);
881                 g_free(data);
882                 return FALSE;
883         }
884
885         dbus_pending_call_set_notify(call, method_call_reply, data, g_free);
886         dbus_pending_call_unref(call);
887
888         dbus_message_unref(msg);
889
890         return TRUE;
891 }
892
893 gboolean g_dbus_proxy_set_property_watch(GDBusProxy *proxy,
894                         GDBusPropertyFunction function, void *user_data)
895 {
896         if (proxy == NULL)
897                 return FALSE;
898
899         proxy->prop_func = function;
900         proxy->prop_data = user_data;
901
902         return TRUE;
903 }
904
905 gboolean g_dbus_proxy_set_removed_watch(GDBusProxy *proxy,
906                                 GDBusProxyFunction function, void *user_data)
907 {
908         if (proxy == NULL)
909                 return FALSE;
910
911         proxy->removed_func = function;
912         proxy->removed_data = user_data;
913
914         return TRUE;
915 }
916
917 static void refresh_properties(GDBusClient *client)
918 {
919         GList *list;
920
921         for (list = g_list_first(client->proxy_list); list;
922                                                 list = g_list_next(list)) {
923                 GDBusProxy *proxy = list->data;
924
925                 get_all_properties(proxy);
926         }
927 }
928
929 static void parse_properties(GDBusClient *client, const char *path,
930                                 const char *interface, DBusMessageIter *iter)
931 {
932         GDBusProxy *proxy;
933
934         if (g_str_equal(interface, DBUS_INTERFACE_INTROSPECTABLE) == TRUE)
935                 return;
936
937         if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE)
938                 return;
939
940         proxy = proxy_lookup(client, path, interface);
941         if (proxy) {
942                 update_properties(proxy, iter, FALSE);
943                 return;
944         }
945
946         proxy = proxy_new(client, path, interface);
947         if (proxy == NULL)
948                 return;
949
950         update_properties(proxy, iter, FALSE);
951
952         if (client->proxy_added)
953                 client->proxy_added(proxy, client->user_data);
954
955         client->proxy_list = g_list_append(client->proxy_list, proxy);
956 }
957
958 static void parse_interfaces(GDBusClient *client, const char *path,
959                                                 DBusMessageIter *iter)
960 {
961         DBusMessageIter dict;
962
963         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
964                 return;
965
966         dbus_message_iter_recurse(iter, &dict);
967
968         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
969                 DBusMessageIter entry;
970                 const char *interface;
971
972                 dbus_message_iter_recurse(&dict, &entry);
973
974                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
975                         break;
976
977                 dbus_message_iter_get_basic(&entry, &interface);
978                 dbus_message_iter_next(&entry);
979
980                 parse_properties(client, path, interface, &entry);
981
982                 dbus_message_iter_next(&dict);
983         }
984 }
985
986 static gboolean interfaces_added(DBusConnection *conn, DBusMessage *msg,
987                                                         void *user_data)
988 {
989         GDBusClient *client = user_data;
990         DBusMessageIter iter;
991         const char *path;
992
993         if (dbus_message_iter_init(msg, &iter) == FALSE)
994                 return TRUE;
995
996         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
997                 return TRUE;
998
999         dbus_message_iter_get_basic(&iter, &path);
1000         dbus_message_iter_next(&iter);
1001
1002         g_dbus_client_ref(client);
1003
1004         parse_interfaces(client, path, &iter);
1005
1006         g_dbus_client_unref(client);
1007
1008         return TRUE;
1009 }
1010
1011 static gboolean interfaces_removed(DBusConnection *conn, DBusMessage *msg,
1012                                                         void *user_data)
1013 {
1014         GDBusClient *client = user_data;
1015         DBusMessageIter iter, entry;
1016         const char *path;
1017
1018         if (dbus_message_iter_init(msg, &iter) == FALSE)
1019                 return TRUE;
1020
1021         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
1022                 return TRUE;
1023
1024         dbus_message_iter_get_basic(&iter, &path);
1025         dbus_message_iter_next(&iter);
1026
1027         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
1028                 return TRUE;
1029
1030         dbus_message_iter_recurse(&iter, &entry);
1031
1032         g_dbus_client_ref(client);
1033
1034         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1035                 const char *interface;
1036
1037                 dbus_message_iter_get_basic(&entry, &interface);
1038                 proxy_remove(client, path, interface);
1039                 dbus_message_iter_next(&entry);
1040         }
1041
1042         g_dbus_client_unref(client);
1043
1044         return TRUE;
1045 }
1046
1047 static void parse_managed_objects(GDBusClient *client, DBusMessage *msg)
1048 {
1049         DBusMessageIter iter, dict;
1050
1051         if (dbus_message_iter_init(msg, &iter) == FALSE)
1052                 return;
1053
1054         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
1055                 return;
1056
1057         dbus_message_iter_recurse(&iter, &dict);
1058
1059         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1060                 DBusMessageIter entry;
1061                 const char *path;
1062
1063                 dbus_message_iter_recurse(&dict, &entry);
1064
1065                 if (dbus_message_iter_get_arg_type(&entry) !=
1066                                                         DBUS_TYPE_OBJECT_PATH)
1067                         break;
1068
1069                 dbus_message_iter_get_basic(&entry, &path);
1070                 dbus_message_iter_next(&entry);
1071
1072                 parse_interfaces(client, path, &entry);
1073
1074                 dbus_message_iter_next(&dict);
1075         }
1076
1077         if (client->ready)
1078                 client->ready(client, client->ready_data);
1079 }
1080
1081 static void get_managed_objects_reply(DBusPendingCall *call, void *user_data)
1082 {
1083         GDBusClient *client = user_data;
1084         DBusMessage *reply = dbus_pending_call_steal_reply(call);
1085         DBusError error;
1086
1087         g_dbus_client_ref(client);
1088
1089         dbus_error_init(&error);
1090
1091         if (dbus_set_error_from_message(&error, reply) == TRUE) {
1092                 dbus_error_free(&error);
1093                 goto done;
1094         }
1095
1096         parse_managed_objects(client, reply);
1097
1098 done:
1099         dbus_message_unref(reply);
1100
1101         dbus_pending_call_unref(client->get_objects_call);
1102         client->get_objects_call = NULL;
1103
1104         g_dbus_client_unref(client);
1105 }
1106
1107 static void get_managed_objects(GDBusClient *client)
1108 {
1109         DBusMessage *msg;
1110
1111         if (!client->connected)
1112                 return;
1113
1114         if ((!client->proxy_added && !client->proxy_removed) ||
1115                                                         !client->root_path) {
1116                 refresh_properties(client);
1117                 return;
1118         }
1119
1120         if (client->get_objects_call != NULL)
1121                 return;
1122
1123         msg = dbus_message_new_method_call(client->service_name,
1124                                                 client->root_path,
1125                                                 DBUS_INTERFACE_OBJECT_MANAGER,
1126                                                 "GetManagedObjects");
1127         if (msg == NULL)
1128                 return;
1129
1130         dbus_message_append_args(msg, DBUS_TYPE_INVALID);
1131
1132         if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
1133                                 &client->get_objects_call, -1) == FALSE) {
1134                 dbus_message_unref(msg);
1135                 return;
1136         }
1137
1138         dbus_pending_call_set_notify(client->get_objects_call,
1139                                                 get_managed_objects_reply,
1140                                                 client, NULL);
1141
1142         dbus_message_unref(msg);
1143 }
1144
1145 static void service_connect(DBusConnection *conn, void *user_data)
1146 {
1147         GDBusClient *client = user_data;
1148
1149         g_dbus_client_ref(client);
1150
1151         client->connected = TRUE;
1152
1153         if (client->connect_func)
1154                 client->connect_func(conn, client->connect_data);
1155
1156         get_managed_objects(client);
1157
1158         g_dbus_client_unref(client);
1159 }
1160
1161 static void service_disconnect(DBusConnection *conn, void *user_data)
1162 {
1163         GDBusClient *client = user_data;
1164
1165         client->connected = FALSE;
1166
1167         g_list_free_full(client->proxy_list, proxy_free);
1168         client->proxy_list = NULL;
1169
1170         if (client->disconn_func)
1171                 client->disconn_func(conn, client->disconn_data);
1172 }
1173
1174 static DBusHandlerResult message_filter(DBusConnection *connection,
1175                                         DBusMessage *message, void *user_data)
1176 {
1177         GDBusClient *client = user_data;
1178         const char *sender, *path, *interface;
1179
1180         if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
1181                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1182
1183         sender = dbus_message_get_sender(message);
1184         if (sender == NULL)
1185                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1186
1187         path = dbus_message_get_path(message);
1188         interface = dbus_message_get_interface(message);
1189
1190         if (g_str_has_prefix(path, client->base_path) == FALSE)
1191                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1192
1193         if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE)
1194                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1195
1196         if (client->signal_func)
1197                 client->signal_func(connection, message, client->signal_data);
1198
1199         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1200 }
1201
1202 GDBusClient *g_dbus_client_new(DBusConnection *connection,
1203                                         const char *service, const char *path)
1204 {
1205         return g_dbus_client_new_full(connection, service, path, "/");
1206 }
1207
1208 GDBusClient *g_dbus_client_new_full(DBusConnection *connection,
1209                                                         const char *service,
1210                                                         const char *path,
1211                                                         const char *root_path)
1212 {
1213         GDBusClient *client;
1214         unsigned int i;
1215
1216         if (!connection || !service)
1217                 return NULL;
1218
1219         client = g_try_new0(GDBusClient, 1);
1220         if (client == NULL)
1221                 return NULL;
1222
1223         if (dbus_connection_add_filter(connection, message_filter,
1224                                                 client, NULL) == FALSE) {
1225                 g_free(client);
1226                 return NULL;
1227         }
1228
1229         client->dbus_conn = dbus_connection_ref(connection);
1230         client->service_name = g_strdup(service);
1231         client->base_path = g_strdup(path);
1232         client->root_path = g_strdup(root_path);
1233         client->connected = FALSE;
1234
1235         client->match_rules = g_ptr_array_sized_new(1);
1236         g_ptr_array_set_free_func(client->match_rules, g_free);
1237
1238         client->watch = g_dbus_add_service_watch(connection, service,
1239                                                 service_connect,
1240                                                 service_disconnect,
1241                                                 client, NULL);
1242
1243         if (!root_path)
1244                 return g_dbus_client_ref(client);
1245
1246         client->added_watch = g_dbus_add_signal_watch(connection, service,
1247                                                 client->root_path,
1248                                                 DBUS_INTERFACE_OBJECT_MANAGER,
1249                                                 "InterfacesAdded",
1250                                                 interfaces_added,
1251                                                 client, NULL);
1252         client->removed_watch = g_dbus_add_signal_watch(connection, service,
1253                                                 client->root_path,
1254                                                 DBUS_INTERFACE_OBJECT_MANAGER,
1255                                                 "InterfacesRemoved",
1256                                                 interfaces_removed,
1257                                                 client, NULL);
1258         g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
1259                                 "sender='%s',path_namespace='%s'",
1260                                 client->service_name, client->base_path));
1261
1262         for (i = 0; i < client->match_rules->len; i++) {
1263                 modify_match(client->dbus_conn, "AddMatch",
1264                                 g_ptr_array_index(client->match_rules, i));
1265         }
1266
1267         return g_dbus_client_ref(client);
1268 }
1269
1270 GDBusClient *g_dbus_client_ref(GDBusClient *client)
1271 {
1272         if (client == NULL)
1273                 return NULL;
1274
1275         __sync_fetch_and_add(&client->ref_count, 1);
1276
1277         return client;
1278 }
1279
1280 void g_dbus_client_unref(GDBusClient *client)
1281 {
1282         unsigned int i;
1283
1284         if (client == NULL)
1285                 return;
1286
1287         if (__sync_sub_and_fetch(&client->ref_count, 1) > 0)
1288                 return;
1289
1290         if (client->pending_call != NULL) {
1291                 dbus_pending_call_cancel(client->pending_call);
1292                 dbus_pending_call_unref(client->pending_call);
1293         }
1294
1295         if (client->get_objects_call != NULL) {
1296                 dbus_pending_call_cancel(client->get_objects_call);
1297                 dbus_pending_call_unref(client->get_objects_call);
1298         }
1299
1300         for (i = 0; i < client->match_rules->len; i++) {
1301                 modify_match(client->dbus_conn, "RemoveMatch",
1302                                 g_ptr_array_index(client->match_rules, i));
1303         }
1304
1305         g_ptr_array_free(client->match_rules, TRUE);
1306
1307         dbus_connection_remove_filter(client->dbus_conn,
1308                                                 message_filter, client);
1309
1310         g_list_free_full(client->proxy_list, proxy_free);
1311
1312         /*
1313          * Don't call disconn_func twice if disconnection
1314          * was previously reported.
1315          */
1316         if (client->disconn_func && client->connected)
1317                 client->disconn_func(client->dbus_conn, client->disconn_data);
1318
1319         g_dbus_remove_watch(client->dbus_conn, client->watch);
1320         g_dbus_remove_watch(client->dbus_conn, client->added_watch);
1321         g_dbus_remove_watch(client->dbus_conn, client->removed_watch);
1322
1323         dbus_connection_unref(client->dbus_conn);
1324
1325         g_free(client->service_name);
1326         g_free(client->base_path);
1327         g_free(client->root_path);
1328
1329         g_free(client);
1330 }
1331
1332 gboolean g_dbus_client_set_connect_watch(GDBusClient *client,
1333                                 GDBusWatchFunction function, void *user_data)
1334 {
1335         if (client == NULL)
1336                 return FALSE;
1337
1338         client->connect_func = function;
1339         client->connect_data = user_data;
1340
1341         return TRUE;
1342 }
1343
1344 gboolean g_dbus_client_set_disconnect_watch(GDBusClient *client,
1345                                 GDBusWatchFunction function, void *user_data)
1346 {
1347         if (client == NULL)
1348                 return FALSE;
1349
1350         client->disconn_func = function;
1351         client->disconn_data = user_data;
1352
1353         return TRUE;
1354 }
1355
1356 gboolean g_dbus_client_set_signal_watch(GDBusClient *client,
1357                                 GDBusMessageFunction function, void *user_data)
1358 {
1359         if (client == NULL)
1360                 return FALSE;
1361
1362         client->signal_func = function;
1363         client->signal_data = user_data;
1364
1365         return TRUE;
1366 }
1367
1368 gboolean g_dbus_client_set_ready_watch(GDBusClient *client,
1369                                 GDBusClientFunction ready, void *user_data)
1370 {
1371         if (client == NULL)
1372                 return FALSE;
1373
1374         client->ready = ready;
1375         client->ready_data = user_data;
1376
1377         return TRUE;
1378 }
1379
1380 gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
1381                                         GDBusProxyFunction proxy_added,
1382                                         GDBusProxyFunction proxy_removed,
1383                                         GDBusPropertyFunction property_changed,
1384                                         void *user_data)
1385 {
1386         if (client == NULL)
1387                 return FALSE;
1388
1389         client->proxy_added = proxy_added;
1390         client->proxy_removed = proxy_removed;
1391         client->property_changed = property_changed;
1392         client->user_data = user_data;
1393
1394         if (proxy_added || proxy_removed || property_changed)
1395                 get_managed_objects(client);
1396
1397         return TRUE;
1398 }