Port gdbus to use g_dbus_add_signal_watch
[framework/connectivity/connman.git] / gdbus / mainloop.c
index 9e49326..994a832 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  D-Bus helper library
  *
- *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
+ *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -64,19 +64,16 @@ struct disconnect_data {
        void *user_data;
 };
 
-static DBusHandlerResult disconnect_filter(DBusConnection *conn,
+static gboolean disconnected_signal(DBusConnection *conn,
                                                DBusMessage *msg, void *data)
 {
        struct disconnect_data *dc_data = data;
 
-       if (dbus_message_is_signal(msg,
-                       DBUS_INTERFACE_LOCAL, "Disconnected") == TRUE) {
-               error("Got disconnected from the system message bus");
-               dc_data->disconnect_cb(conn, dc_data->user_data);
-               dbus_connection_unref(conn);
-       }
+       error("Got disconnected from the system message bus");
+       dc_data->disconnect_cb(conn, dc_data->user_data);
+       dbus_connection_unref(conn);
 
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       return TRUE;
 }
 
 static gboolean message_dispatch_cb(void *data)
@@ -183,7 +180,11 @@ static void timeout_handler_free(void *data)
        if (!handler)
                return;
 
-       g_source_remove(handler->id);
+       if (handler->id > 0) {
+               g_source_remove(handler->id);
+               handler->id = 0;
+       }
+
        g_free(handler);
 }
 
@@ -207,6 +208,17 @@ static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
 
 static void remove_timeout(DBusTimeout *timeout, void *data)
 {
+        timeout_handler_t *handler;
+
+        handler = dbus_timeout_get_data(timeout);
+
+        if (!handler)
+                return;
+
+        if (handler->id > 0) {
+                g_source_remove(handler->id);
+                handler->id = 0;
+        }
 }
 
 static void timeout_toggled(DBusTimeout *timeout, void *data)
@@ -272,6 +284,9 @@ DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
 
        setup_dbus_with_main_loop(conn);
 
+       if (dbus_connection_get_dispatch_status(conn) == DBUS_DISPATCH_DATA_REMAINS)
+               g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, conn);
+
        return conn;
 }
 
@@ -281,49 +296,6 @@ gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
        return TRUE;
 }
 
-gboolean g_dbus_check_service(DBusConnection *connection, const char *name)
-{
-       DBusMessage *message, *reply;
-       const char **names;
-       int i, count;
-       gboolean result = FALSE;
-
-       message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
-                       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "ListNames");
-       if (message == NULL) {
-               error("Can't allocate new message");
-               return FALSE;
-       }
-
-       reply = dbus_connection_send_with_reply_and_block(connection,
-                                                       message, -1, NULL);
-
-       dbus_message_unref(message);
-
-       if (reply == NULL) {
-               error("Failed to execute method call");
-               return FALSE;
-       }
-
-       if (dbus_message_get_args(reply, NULL,
-                               DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
-                               &names, &count, DBUS_TYPE_INVALID) == FALSE) {
-               error("Failed to read name list");
-               goto done;
-       }
-
-       for (i = 0; i < count; i++)
-               if (g_str_equal(names[i], name) == TRUE) {
-                       result = TRUE;
-                       break;
-               }
-
-done:
-       dbus_message_unref(reply);
-
-       return result;
-}
-
 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
                                GDBusWatchFunction function,
                                void *user_data, DBusFreeFunction destroy)
@@ -337,9 +309,11 @@ gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
 
        dbus_connection_set_exit_on_disconnect(connection, FALSE);
 
-       if (dbus_connection_add_filter(connection, disconnect_filter,
-                                               dc_data, g_free) == FALSE) {
-               error("Can't add D-Bus disconnect filter");
+       if (g_dbus_add_signal_watch(connection, NULL, NULL,
+                               DBUS_INTERFACE_LOCAL, "Disconnected",
+                               disconnected_signal, dc_data,
+                               g_free) == 0) {
+               error("Can't add watch for D-Bus Disconnected signal\n");
                g_free(dc_data);
                return FALSE;
        }