5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
29 #include <dbus/dbus.h>
33 #define METHOD_CALL_TIMEOUT (300 * 1000)
37 DBusConnection *dbus_conn;
41 GPtrArray *match_rules;
42 DBusPendingCall *pending_call;
43 GDBusWatchFunction connect_func;
45 GDBusWatchFunction disconn_func;
47 GDBusMessageFunction signal_func;
49 GDBusProxyFunction proxy_added;
50 GDBusProxyFunction proxy_removed;
51 GDBusPropertyFunction property_changed;
61 GHashTable *prop_list;
63 GDBusPropertyFunction prop_func;
73 static void modify_match_reply(DBusPendingCall *call, void *user_data)
75 DBusMessage *reply = dbus_pending_call_steal_reply(call);
78 dbus_error_init(&error);
80 if (dbus_set_error_from_message(&error, reply) == TRUE)
81 dbus_error_free(&error);
83 dbus_message_unref(reply);
86 static gboolean modify_match(DBusConnection *conn, const char *member,
90 DBusPendingCall *call;
92 msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
93 DBUS_INTERFACE_DBUS, member);
97 dbus_message_append_args(msg, DBUS_TYPE_STRING, &rule,
100 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
101 dbus_message_unref(msg);
105 dbus_pending_call_set_notify(call, modify_match_reply, NULL, NULL);
106 dbus_pending_call_unref(call);
108 dbus_message_unref(msg);
113 static void iter_append_iter(DBusMessageIter *base, DBusMessageIter *iter)
117 type = dbus_message_iter_get_arg_type(iter);
119 if (dbus_type_is_basic(type)) {
122 dbus_message_iter_get_basic(iter, &value);
123 dbus_message_iter_append_basic(base, type, &value);
124 } else if (dbus_type_is_container(type)) {
125 DBusMessageIter iter_sub, base_sub;
128 dbus_message_iter_recurse(iter, &iter_sub);
131 case DBUS_TYPE_ARRAY:
132 case DBUS_TYPE_VARIANT:
133 sig = dbus_message_iter_get_signature(&iter_sub);
140 dbus_message_iter_open_container(base, type, sig, &base_sub);
145 while (dbus_message_iter_get_arg_type(&iter_sub) !=
147 iter_append_iter(&base_sub, &iter_sub);
148 dbus_message_iter_next(&iter_sub);
151 dbus_message_iter_close_container(base, &base_sub);
155 static void prop_entry_update(struct prop_entry *prop, DBusMessageIter *iter)
158 DBusMessageIter base;
160 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN);
164 dbus_message_iter_init_append(msg, &base);
165 iter_append_iter(&base, iter);
167 if (prop->msg != NULL)
168 dbus_message_unref(prop->msg);
170 prop->msg = dbus_message_copy(msg);
171 dbus_message_unref(msg);
174 static struct prop_entry *prop_entry_new(const char *name,
175 DBusMessageIter *iter)
177 struct prop_entry *prop;
179 prop = g_try_new0(struct prop_entry, 1);
183 prop->name = g_strdup(name);
184 prop->type = dbus_message_iter_get_arg_type(iter);
186 prop_entry_update(prop, iter);
191 static void prop_entry_free(gpointer data)
193 struct prop_entry *prop = data;
195 if (prop->msg != NULL)
196 dbus_message_unref(prop->msg);
203 static void add_property(GDBusProxy *proxy, const char *name,
204 DBusMessageIter *iter)
206 DBusMessageIter value;
207 struct prop_entry *prop;
209 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT)
212 dbus_message_iter_recurse(iter, &value);
214 prop = g_hash_table_lookup(proxy->prop_list, name);
216 GDBusClient *client = proxy->client;
218 prop_entry_update(prop, &value);
220 if (proxy->prop_func)
221 proxy->prop_func(proxy, name, &value, proxy->prop_data);
226 if (client->property_changed)
227 client->property_changed(proxy, name, &value,
232 prop = prop_entry_new(name, &value);
236 g_hash_table_replace(proxy->prop_list, prop->name, prop);
238 if (proxy->prop_func)
239 proxy->prop_func(proxy, name, &value, proxy->prop_data);
242 static void update_properties(GDBusProxy *proxy, DBusMessageIter *iter)
244 DBusMessageIter dict;
246 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
249 dbus_message_iter_recurse(iter, &dict);
251 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
252 DBusMessageIter entry;
255 dbus_message_iter_recurse(&dict, &entry);
257 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
260 dbus_message_iter_get_basic(&entry, &name);
261 dbus_message_iter_next(&entry);
263 add_property(proxy, name, &entry);
265 dbus_message_iter_next(&dict);
269 static void get_all_properties_reply(DBusPendingCall *call, void *user_data)
271 GDBusProxy *proxy = user_data;
272 GDBusClient *client = proxy->client;
273 DBusMessage *reply = dbus_pending_call_steal_reply(call);
274 DBusMessageIter iter;
277 dbus_error_init(&error);
279 if (dbus_set_error_from_message(&error, reply) == TRUE) {
280 dbus_error_free(&error);
284 dbus_message_iter_init(reply, &iter);
286 update_properties(proxy, &iter);
289 if (client->proxy_added)
290 client->proxy_added(proxy, client->user_data);
292 client->proxy_list = g_list_append(client->proxy_list, proxy);
294 dbus_message_unref(reply);
296 g_dbus_client_unref(client);
299 static void get_all_properties(GDBusProxy *proxy)
301 GDBusClient *client = proxy->client;
302 const char *service_name = client->service_name;
304 DBusPendingCall *call;
306 msg = dbus_message_new_method_call(service_name, proxy->obj_path,
307 DBUS_INTERFACE_PROPERTIES, "GetAll");
311 dbus_message_append_args(msg, DBUS_TYPE_STRING, &proxy->interface,
314 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
315 &call, -1) == FALSE) {
316 dbus_message_unref(msg);
320 g_dbus_client_ref(client);
322 dbus_pending_call_set_notify(call, get_all_properties_reply,
324 dbus_pending_call_unref(call);
326 dbus_message_unref(msg);
329 static GDBusProxy *proxy_lookup(GDBusClient *client, const char *path,
330 const char *interface)
334 for (list = g_list_first(client->proxy_list); list;
335 list = g_list_next(list)) {
336 GDBusProxy *proxy = list->data;
338 if (g_str_equal(proxy->interface, interface) == TRUE &&
339 g_str_equal(proxy->obj_path, path) == TRUE)
346 static GDBusProxy *proxy_new(GDBusClient *client, const char *path,
347 const char *interface)
351 proxy = g_try_new0(GDBusProxy, 1);
355 proxy->client = client;
356 proxy->obj_path = g_strdup(path);
357 proxy->interface = g_strdup(interface);
359 proxy->prop_list = g_hash_table_new_full(g_str_hash, g_str_equal,
360 NULL, prop_entry_free);
362 proxy->match_rule = g_strdup_printf("type='signal',"
363 "sender='%s',path='%s',interface='%s',"
364 "member='PropertiesChanged',arg0='%s'",
365 client->service_name, proxy->obj_path,
366 DBUS_INTERFACE_PROPERTIES, proxy->interface);
368 modify_match(client->dbus_conn, "AddMatch", proxy->match_rule);
370 return g_dbus_proxy_ref(proxy);
373 static void proxy_free(gpointer data)
375 GDBusProxy *proxy = data;
378 GDBusClient *client = proxy->client;
380 if (client->proxy_removed)
381 client->proxy_removed(proxy, client->user_data);
383 modify_match(client->dbus_conn, "RemoveMatch",
386 g_free(proxy->match_rule);
387 proxy->match_rule = NULL;
389 g_hash_table_remove_all(proxy->prop_list);
391 proxy->client = NULL;
394 g_dbus_proxy_unref(proxy);
397 static void proxy_remove(GDBusClient *client, const char *path,
398 const char *interface)
402 for (list = g_list_first(client->proxy_list); list;
403 list = g_list_next(list)) {
404 GDBusProxy *proxy = list->data;
406 if (g_str_equal(proxy->interface, interface) == TRUE &&
407 g_str_equal(proxy->obj_path, path) == TRUE) {
409 g_list_delete_link(client->proxy_list, list);
416 GDBusProxy *g_dbus_proxy_new(GDBusClient *client, const char *path,
417 const char *interface)
424 proxy = proxy_lookup(client, path, interface);
426 return g_dbus_proxy_ref(proxy);
428 proxy = proxy_new(client, path, interface);
432 get_all_properties(proxy);
434 return g_dbus_proxy_ref(proxy);
437 GDBusProxy *g_dbus_proxy_ref(GDBusProxy *proxy)
442 g_atomic_int_inc(&proxy->ref_count);
447 void g_dbus_proxy_unref(GDBusProxy *proxy)
452 if (g_atomic_int_dec_and_test(&proxy->ref_count) == FALSE)
455 g_hash_table_destroy(proxy->prop_list);
457 g_free(proxy->obj_path);
458 g_free(proxy->interface);
463 const char *g_dbus_proxy_get_path(GDBusProxy *proxy)
468 return proxy->obj_path;
471 const char *g_dbus_proxy_get_interface(GDBusProxy *proxy)
476 return proxy->interface;
479 gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,
480 DBusMessageIter *iter)
482 struct prop_entry *prop;
484 if (proxy == NULL || name == NULL)
487 prop = g_hash_table_lookup(proxy->prop_list, name);
491 if (prop->msg == NULL)
494 if (dbus_message_iter_init(prop->msg, iter) == FALSE)
500 struct refresh_property_data {
505 static void refresh_property_free(gpointer user_data)
507 struct refresh_property_data *data = user_data;
513 static void refresh_property_reply(DBusPendingCall *call, void *user_data)
515 struct refresh_property_data *data = user_data;
516 DBusMessage *reply = dbus_pending_call_steal_reply(call);
519 dbus_error_init(&error);
521 if (dbus_set_error_from_message(&error, reply) == FALSE) {
522 DBusMessageIter iter;
524 dbus_message_iter_init(reply, &iter);
526 add_property(data->proxy, data->name, &iter);
528 dbus_error_free(&error);
530 dbus_message_unref(reply);
533 gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name)
535 struct refresh_property_data *data;
538 DBusMessageIter iter;
539 DBusPendingCall *call;
541 if (proxy == NULL || name == NULL)
544 client = proxy->client;
548 data = g_try_new0(struct refresh_property_data, 1);
553 data->name = g_strdup(name);
555 msg = dbus_message_new_method_call(client->service_name,
556 proxy->obj_path, DBUS_INTERFACE_PROPERTIES, "Get");
558 refresh_property_free(data);
562 dbus_message_iter_init_append(msg, &iter);
563 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
565 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &name);
567 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
568 &call, -1) == FALSE) {
569 dbus_message_unref(msg);
570 refresh_property_free(data);
574 dbus_pending_call_set_notify(call, refresh_property_reply,
575 data, refresh_property_free);
576 dbus_pending_call_unref(call);
578 dbus_message_unref(msg);
583 struct set_property_data {
584 GDBusResultFunction function;
586 GDBusDestroyFunction destroy;
589 static void set_property_reply(DBusPendingCall *call, void *user_data)
591 struct set_property_data *data = user_data;
592 DBusMessage *reply = dbus_pending_call_steal_reply(call);
595 dbus_error_init(&error);
597 dbus_set_error_from_message(&error, reply);
600 data->function(&error, data->user_data);
603 data->destroy(data->user_data);
605 dbus_error_free(&error);
607 dbus_message_unref(reply);
610 gboolean g_dbus_proxy_set_property_basic(GDBusProxy *proxy,
611 const char *name, int type, const void *value,
612 GDBusResultFunction function, void *user_data,
613 GDBusDestroyFunction destroy)
615 struct set_property_data *data;
618 DBusMessageIter iter, variant;
619 DBusPendingCall *call;
622 if (proxy == NULL || name == NULL || value == NULL)
625 if (dbus_type_is_basic(type) == FALSE)
628 client = proxy->client;
632 data = g_try_new0(struct set_property_data, 1);
636 data->function = function;
637 data->user_data = user_data;
638 data->destroy = destroy;
640 msg = dbus_message_new_method_call(client->service_name,
641 proxy->obj_path, DBUS_INTERFACE_PROPERTIES, "Set");
647 type_as_str[0] = (char) type;
648 type_as_str[1] = '\0';
650 dbus_message_iter_init_append(msg, &iter);
651 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
653 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &name);
655 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
656 type_as_str, &variant);
657 dbus_message_iter_append_basic(&variant, type, value);
658 dbus_message_iter_close_container(&iter, &variant);
660 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
661 &call, -1) == FALSE) {
662 dbus_message_unref(msg);
667 dbus_pending_call_set_notify(call, set_property_reply, data, g_free);
668 dbus_pending_call_unref(call);
670 dbus_message_unref(msg);
675 struct method_call_data {
676 GDBusReturnFunction function;
678 GDBusDestroyFunction destroy;
681 static void method_call_reply(DBusPendingCall *call, void *user_data)
683 struct method_call_data *data = user_data;
684 DBusMessage *reply = dbus_pending_call_steal_reply(call);
687 data->function(reply, data->user_data);
690 data->destroy(data->user_data);
692 dbus_message_unref(reply);
695 gboolean g_dbus_proxy_method_call(GDBusProxy *proxy, const char *method,
696 GDBusSetupFunction setup,
697 GDBusReturnFunction function, void *user_data,
698 GDBusDestroyFunction destroy)
700 struct method_call_data *data;
703 DBusPendingCall *call;
705 if (proxy == NULL || method == NULL)
708 client = proxy->client;
712 data = g_try_new0(struct method_call_data, 1);
716 data->function = function;
717 data->user_data = user_data;
718 data->destroy = destroy;
720 msg = dbus_message_new_method_call(client->service_name,
721 proxy->obj_path, proxy->interface, method);
728 DBusMessageIter iter;
730 dbus_message_iter_init_append(msg, &iter);
731 setup(&iter, data->user_data);
734 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
735 &call, METHOD_CALL_TIMEOUT) == FALSE) {
736 dbus_message_unref(msg);
741 dbus_pending_call_set_notify(call, method_call_reply, data, g_free);
742 dbus_pending_call_unref(call);
744 dbus_message_unref(msg);
749 gboolean g_dbus_proxy_set_property_watch(GDBusProxy *proxy,
750 GDBusPropertyFunction function, void *user_data)
755 proxy->prop_func = function;
756 proxy->prop_data = user_data;
761 static void properties_changed(GDBusClient *client, const char *path,
764 GDBusProxy *proxy = NULL;
765 DBusMessageIter iter, entry;
766 const char *interface;
769 if (dbus_message_iter_init(msg, &iter) == FALSE)
772 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
775 dbus_message_iter_get_basic(&iter, &interface);
776 dbus_message_iter_next(&iter);
778 for (list = g_list_first(client->proxy_list); list;
779 list = g_list_next(list)) {
780 GDBusProxy *data = list->data;
782 if (g_str_equal(data->interface, interface) == TRUE &&
783 g_str_equal(data->obj_path, path) == TRUE) {
792 update_properties(proxy, &iter);
794 dbus_message_iter_next(&iter);
796 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
799 dbus_message_iter_recurse(&iter, &entry);
801 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
804 dbus_message_iter_get_basic(&entry, &name);
806 g_hash_table_remove(proxy->prop_list, name);
808 if (proxy->prop_func)
809 proxy->prop_func(proxy, name, NULL, proxy->prop_data);
811 if (client->property_changed)
812 client->property_changed(proxy, name, NULL,
815 dbus_message_iter_next(&entry);
819 static void parse_properties(GDBusClient *client, const char *path,
820 const char *interface, DBusMessageIter *iter)
824 if (g_str_equal(interface, DBUS_INTERFACE_INTROSPECTABLE) == TRUE)
827 if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE)
830 proxy = proxy_lookup(client, path, interface);
832 update_properties(proxy, iter);
836 proxy = proxy_new(client, path, interface);
840 update_properties(proxy, iter);
842 if (client->proxy_added)
843 client->proxy_added(proxy, client->user_data);
845 client->proxy_list = g_list_append(client->proxy_list, proxy);
848 static void parse_interfaces(GDBusClient *client, const char *path,
849 DBusMessageIter *iter)
851 DBusMessageIter dict;
853 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
856 dbus_message_iter_recurse(iter, &dict);
858 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
859 DBusMessageIter entry;
860 const char *interface;
862 dbus_message_iter_recurse(&dict, &entry);
864 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
867 dbus_message_iter_get_basic(&entry, &interface);
868 dbus_message_iter_next(&entry);
870 parse_properties(client, path, interface, &entry);
872 dbus_message_iter_next(&dict);
876 static void interfaces_added(GDBusClient *client, DBusMessage *msg)
878 DBusMessageIter iter;
881 if (dbus_message_iter_init(msg, &iter) == FALSE)
884 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
887 dbus_message_iter_get_basic(&iter, &path);
888 dbus_message_iter_next(&iter);
890 g_dbus_client_ref(client);
892 parse_interfaces(client, path, &iter);
894 g_dbus_client_unref(client);
897 static void interfaces_removed(GDBusClient *client, DBusMessage *msg)
899 DBusMessageIter iter, entry;
902 if (dbus_message_iter_init(msg, &iter) == FALSE)
905 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
908 dbus_message_iter_get_basic(&iter, &path);
909 dbus_message_iter_next(&iter);
911 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
914 dbus_message_iter_recurse(&iter, &entry);
916 g_dbus_client_ref(client);
918 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
919 const char *interface;
921 dbus_message_iter_get_basic(&entry, &interface);
922 proxy_remove(client, path, interface);
923 dbus_message_iter_next(&entry);
926 g_dbus_client_unref(client);
929 static void parse_managed_objects(GDBusClient *client, DBusMessage *msg)
931 DBusMessageIter iter, dict;
933 if (dbus_message_iter_init(msg, &iter) == FALSE)
936 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
939 dbus_message_iter_recurse(&iter, &dict);
941 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
942 DBusMessageIter entry;
945 dbus_message_iter_recurse(&dict, &entry);
947 if (dbus_message_iter_get_arg_type(&entry) !=
948 DBUS_TYPE_OBJECT_PATH)
951 dbus_message_iter_get_basic(&entry, &path);
952 dbus_message_iter_next(&entry);
954 parse_interfaces(client, path, &entry);
956 dbus_message_iter_next(&dict);
960 static void get_managed_objects_reply(DBusPendingCall *call, void *user_data)
962 GDBusClient *client = user_data;
963 DBusMessage *reply = dbus_pending_call_steal_reply(call);
966 dbus_error_init(&error);
968 if (dbus_set_error_from_message(&error, reply) == TRUE) {
969 dbus_error_free(&error);
973 parse_managed_objects(client, reply);
976 dbus_message_unref(reply);
978 g_dbus_client_unref(client);
981 static void get_managed_objects(GDBusClient *client)
984 DBusPendingCall *call;
986 if (!client->proxy_added && !client->proxy_removed)
989 msg = dbus_message_new_method_call(client->service_name, "/",
990 DBUS_INTERFACE_DBUS ".ObjectManager",
991 "GetManagedObjects");
995 dbus_message_append_args(msg, DBUS_TYPE_INVALID);
997 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
998 &call, -1) == FALSE) {
999 dbus_message_unref(msg);
1003 g_dbus_client_ref(client);
1005 dbus_pending_call_set_notify(call, get_managed_objects_reply,
1007 dbus_pending_call_unref(call);
1009 dbus_message_unref(msg);
1012 static void get_name_owner_reply(DBusPendingCall *call, void *user_data)
1014 GDBusClient *client = user_data;
1015 DBusMessage *reply = dbus_pending_call_steal_reply(call);
1019 dbus_error_init(&error);
1021 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1022 dbus_error_free(&error);
1026 if (dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &name,
1027 DBUS_TYPE_INVALID) == FALSE)
1030 if (client->unique_name == NULL) {
1031 client->unique_name = g_strdup(name);
1033 if (client->connect_func)
1034 client->connect_func(client->dbus_conn,
1035 client->connect_data);
1039 dbus_message_unref(reply);
1041 dbus_pending_call_unref(client->pending_call);
1042 client->pending_call = NULL;
1045 static void get_name_owner(GDBusClient *client, const char *name)
1049 msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1050 DBUS_INTERFACE_DBUS, "GetNameOwner");
1054 dbus_message_append_args(msg, DBUS_TYPE_STRING, &name,
1057 if (dbus_connection_send_with_reply(client->dbus_conn, msg,
1058 &client->pending_call, -1) == FALSE) {
1059 dbus_message_unref(msg);
1063 dbus_pending_call_set_notify(client->pending_call,
1064 get_name_owner_reply, client, NULL);
1066 dbus_message_unref(msg);
1069 static DBusHandlerResult message_filter(DBusConnection *connection,
1070 DBusMessage *message, void *user_data)
1072 GDBusClient *client = user_data;
1075 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
1076 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1078 sender = dbus_message_get_sender(message);
1080 if (g_str_equal(sender, DBUS_SERVICE_DBUS) == TRUE) {
1081 const char *interface, *member;
1082 const char *name, *old, *new;
1084 interface = dbus_message_get_interface(message);
1086 if (g_str_equal(interface, DBUS_INTERFACE_DBUS) == FALSE)
1087 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1089 member = dbus_message_get_member(message);
1091 if (g_str_equal(member, "NameOwnerChanged") == FALSE)
1092 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1094 if (dbus_message_get_args(message, NULL,
1095 DBUS_TYPE_STRING, &name,
1096 DBUS_TYPE_STRING, &old,
1097 DBUS_TYPE_STRING, &new,
1098 DBUS_TYPE_INVALID) == FALSE)
1099 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1101 if (g_str_equal(name, client->service_name) == FALSE)
1102 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1104 if (*new == '\0' && client->unique_name != NULL &&
1105 g_str_equal(old, client->unique_name) == TRUE) {
1106 if (client->disconn_func)
1107 client->disconn_func(client->dbus_conn,
1108 client->disconn_data);
1110 g_free(client->unique_name);
1111 client->unique_name = NULL;
1112 } else if (*old == '\0' && client->unique_name == NULL) {
1113 client->unique_name = g_strdup(new);
1115 if (client->connect_func)
1116 client->connect_func(client->dbus_conn,
1117 client->connect_data);
1119 get_managed_objects(client);
1122 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1125 if (client->unique_name == NULL)
1126 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1128 if (g_str_equal(sender, client->unique_name) == TRUE) {
1129 const char *path, *interface, *member;
1131 path = dbus_message_get_path(message);
1132 interface = dbus_message_get_interface(message);
1133 member = dbus_message_get_member(message);
1135 if (g_str_equal(path, "/") == TRUE) {
1136 if (g_str_equal(interface, DBUS_INTERFACE_DBUS
1137 ".ObjectManager") == FALSE)
1138 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1140 if (g_str_equal(member, "InterfacesAdded") == TRUE) {
1141 interfaces_added(client, message);
1142 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1145 if (g_str_equal(member, "InterfacesRemoved") == TRUE) {
1146 interfaces_removed(client, message);
1147 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1150 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1153 if (g_str_has_prefix(path, client->base_path) == FALSE)
1154 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1156 if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE) {
1157 if (g_str_equal(member, "PropertiesChanged") == TRUE)
1158 properties_changed(client, path, message);
1160 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1163 if (client->signal_func)
1164 client->signal_func(client->dbus_conn,
1165 message, client->signal_data);
1168 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1171 GDBusClient *g_dbus_client_new(DBusConnection *connection,
1172 const char *service, const char *path)
1174 GDBusClient *client;
1177 if (connection == NULL)
1180 client = g_try_new0(GDBusClient, 1);
1184 if (dbus_connection_add_filter(connection, message_filter,
1185 client, NULL) == FALSE) {
1190 client->dbus_conn = dbus_connection_ref(connection);
1191 client->service_name = g_strdup(service);
1192 client->base_path = g_strdup(path);
1194 get_name_owner(client, client->service_name);
1196 client->match_rules = g_ptr_array_sized_new(4);
1197 g_ptr_array_set_free_func(client->match_rules, g_free);
1199 g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
1200 "sender='%s',path='%s',interface='%s',"
1201 "member='NameOwnerChanged',arg0='%s'",
1202 DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
1203 DBUS_INTERFACE_DBUS, client->service_name));
1204 g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
1206 "path='/',interface='%s.ObjectManager',"
1207 "member='InterfacesAdded'",
1208 client->service_name, DBUS_INTERFACE_DBUS));
1209 g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
1211 "path='/',interface='%s.ObjectManager',"
1212 "member='InterfacesRemoved'",
1213 client->service_name, DBUS_INTERFACE_DBUS));
1214 g_ptr_array_add(client->match_rules, g_strdup_printf("type='signal',"
1215 "sender='%s',path_namespace='%s'",
1216 client->service_name, client->base_path));
1218 for (i = 0; i < client->match_rules->len; i++) {
1219 modify_match(client->dbus_conn, "AddMatch",
1220 g_ptr_array_index(client->match_rules, i));
1223 return g_dbus_client_ref(client);
1226 GDBusClient *g_dbus_client_ref(GDBusClient *client)
1231 g_atomic_int_inc(&client->ref_count);
1236 void g_dbus_client_unref(GDBusClient *client)
1243 if (g_atomic_int_dec_and_test(&client->ref_count) == FALSE)
1246 if (client->pending_call != NULL) {
1247 dbus_pending_call_cancel(client->pending_call);
1248 dbus_pending_call_unref(client->pending_call);
1251 for (i = 0; i < client->match_rules->len; i++) {
1252 modify_match(client->dbus_conn, "RemoveMatch",
1253 g_ptr_array_index(client->match_rules, i));
1256 g_ptr_array_free(client->match_rules, TRUE);
1258 dbus_connection_remove_filter(client->dbus_conn,
1259 message_filter, client);
1261 g_list_free_full(client->proxy_list, proxy_free);
1263 if (client->disconn_func)
1264 client->disconn_func(client->dbus_conn, client->disconn_data);
1266 dbus_connection_unref(client->dbus_conn);
1268 g_free(client->service_name);
1269 g_free(client->unique_name);
1270 g_free(client->base_path);
1275 gboolean g_dbus_client_set_connect_watch(GDBusClient *client,
1276 GDBusWatchFunction function, void *user_data)
1281 client->connect_func = function;
1282 client->connect_data = user_data;
1287 gboolean g_dbus_client_set_disconnect_watch(GDBusClient *client,
1288 GDBusWatchFunction function, void *user_data)
1293 client->disconn_func = function;
1294 client->disconn_data = user_data;
1299 gboolean g_dbus_client_set_signal_watch(GDBusClient *client,
1300 GDBusMessageFunction function, void *user_data)
1305 client->signal_func = function;
1306 client->signal_data = user_data;
1311 gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
1312 GDBusProxyFunction proxy_added,
1313 GDBusProxyFunction proxy_removed,
1314 GDBusPropertyFunction property_changed,
1320 client->proxy_added = proxy_added;
1321 client->proxy_removed = proxy_removed;
1322 client->property_changed = property_changed;
1323 client->user_data = user_data;
1325 get_managed_objects(client);