Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / gdbus / client.c
old mode 100644 (file)
new mode 100755 (executable)
index 3bf883a..48711ae
@@ -42,6 +42,7 @@ struct GDBusClient {
        DBusConnection *dbus_conn;
        char *service_name;
        char *base_path;
+       char *root_path;
        guint watch;
        guint added_watch;
        guint removed_watch;
@@ -51,6 +52,7 @@ struct GDBusClient {
        GDBusWatchFunction connect_func;
        void *connect_data;
        GDBusWatchFunction disconn_func;
+       gboolean connected;
        void *disconn_data;
        GDBusMessageFunction signal_func;
        void *signal_data;
@@ -1106,7 +1108,11 @@ static void get_managed_objects(GDBusClient *client)
 {
        DBusMessage *msg;
 
-       if (!client->proxy_added && !client->proxy_removed) {
+       if (!client->connected)
+               return;
+
+       if ((!client->proxy_added && !client->proxy_removed) ||
+                                                       !client->root_path) {
                refresh_properties(client);
                return;
        }
@@ -1114,9 +1120,10 @@ static void get_managed_objects(GDBusClient *client)
        if (client->get_objects_call != NULL)
                return;
 
-       msg = dbus_message_new_method_call(client->service_name, "/",
-                                       DBUS_INTERFACE_DBUS ".ObjectManager",
-                                                       "GetManagedObjects");
+       msg = dbus_message_new_method_call(client->service_name,
+                                               client->root_path,
+                                               DBUS_INTERFACE_OBJECT_MANAGER,
+                                               "GetManagedObjects");
        if (msg == NULL)
                return;
 
@@ -1141,6 +1148,8 @@ static void service_connect(DBusConnection *conn, void *user_data)
 
        g_dbus_client_ref(client);
 
+       client->connected = TRUE;
+
        if (client->connect_func)
                client->connect_func(conn, client->connect_data);
 
@@ -1153,6 +1162,8 @@ static void service_disconnect(DBusConnection *conn, void *user_data)
 {
        GDBusClient *client = user_data;
 
+       client->connected = FALSE;
+
        g_list_free_full(client->proxy_list, proxy_free);
        client->proxy_list = NULL;
 
@@ -1191,10 +1202,18 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
 GDBusClient *g_dbus_client_new(DBusConnection *connection,
                                        const char *service, const char *path)
 {
+       return g_dbus_client_new_full(connection, service, path, "/");
+}
+
+GDBusClient *g_dbus_client_new_full(DBusConnection *connection,
+                                                       const char *service,
+                                                       const char *path,
+                                                       const char *root_path)
+{
        GDBusClient *client;
        unsigned int i;
 
-       if (connection == NULL)
+       if (!connection || !service)
                return NULL;
 
        client = g_try_new0(GDBusClient, 1);
@@ -1210,6 +1229,8 @@ GDBusClient *g_dbus_client_new(DBusConnection *connection,
        client->dbus_conn = dbus_connection_ref(connection);
        client->service_name = g_strdup(service);
        client->base_path = g_strdup(path);
+       client->root_path = g_strdup(root_path);
+       client->connected = FALSE;
 
        client->match_rules = g_ptr_array_sized_new(1);
        g_ptr_array_set_free_func(client->match_rules, g_free);
@@ -1218,14 +1239,18 @@ GDBusClient *g_dbus_client_new(DBusConnection *connection,
                                                service_connect,
                                                service_disconnect,
                                                client, NULL);
+
+       if (!root_path)
+               return g_dbus_client_ref(client);
+
        client->added_watch = g_dbus_add_signal_watch(connection, service,
-                                               "/",
+                                               client->root_path,
                                                DBUS_INTERFACE_OBJECT_MANAGER,
                                                "InterfacesAdded",
                                                interfaces_added,
                                                client, NULL);
        client->removed_watch = g_dbus_add_signal_watch(connection, service,
-                                               "/",
+                                               client->root_path,
                                                DBUS_INTERFACE_OBJECT_MANAGER,
                                                "InterfacesRemoved",
                                                interfaces_removed,
@@ -1284,7 +1309,11 @@ void g_dbus_client_unref(GDBusClient *client)
 
        g_list_free_full(client->proxy_list, proxy_free);
 
-       if (client->disconn_func)
+       /*
+        * Don't call disconn_func twice if disconnection
+        * was previously reported.
+        */
+       if (client->disconn_func && client->connected)
                client->disconn_func(client->dbus_conn, client->disconn_data);
 
        g_dbus_remove_watch(client->dbus_conn, client->watch);
@@ -1295,6 +1324,7 @@ void g_dbus_client_unref(GDBusClient *client)
 
        g_free(client->service_name);
        g_free(client->base_path);
+       g_free(client->root_path);
 
        g_free(client);
 }
@@ -1361,7 +1391,8 @@ gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
        client->property_changed = property_changed;
        client->user_data = user_data;
 
-       get_managed_objects(client);
+       if (proxy_added || proxy_removed || property_changed)
+               get_managed_objects(client);
 
        return TRUE;
 }