From a78a254715fb410d4c2f7fd274894e51d74bc9bb Mon Sep 17 00:00:00 2001 From: Jason Conti Date: Tue, 8 May 2012 14:01:57 -0400 Subject: [PATCH] Add function ibus_bus_new_async to create a new IBusBus object and asynchronously connect to the IBus daemon. BUG=http://code.google.com/p/ibus/issues/detail?id=1452 TEST=Manually Review URL: https://codereview.appspot.com/6159047 Patch from Jason Conti . --- src/ibusbus.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- src/ibusbus.h | 10 +++++++ 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/ibusbus.c b/src/ibusbus.c index ea18841..3aaf819 100644 --- a/src/ibusbus.c +++ b/src/ibusbus.c @@ -258,7 +258,7 @@ _connection_closed_cb (GDBusConnection *connection, } static void -ibus_bus_connect (IBusBus *bus) +ibus_bus_disconnect (IBusBus *bus) { /* unref the old connection at first */ if (bus->priv->connection != NULL) { @@ -268,15 +268,11 @@ ibus_bus_connect (IBusBus *bus) g_object_unref (bus->priv->connection); bus->priv->connection = NULL; } +} - if (ibus_get_address () != NULL) { - bus->priv->connection = - g_dbus_connection_new_for_address_sync (ibus_get_address (), - G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | - G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, - NULL, NULL, NULL); - } - +static void +ibus_bus_connect_finish (IBusBus *bus) +{ if (bus->priv->connection) { /* FIXME */ ibus_bus_hello (bus); @@ -297,6 +293,63 @@ ibus_bus_connect (IBusBus *bus) } static void +_bus_connect_async_cb (GObject *source_object, + GAsyncResult *res, + gpointer user_data) +{ + g_return_if_fail (user_data != NULL); + g_return_if_fail (IBUS_IS_BUS (user_data)); + + IBusBus *bus = IBUS_BUS (user_data); + GError *error = NULL; + + bus->priv->connection = g_dbus_connection_new_for_address_finish (res, &error); + + if (error != NULL) { + g_warning ("Unable to connect to ibus: %s", error->message); + g_error_free (error); + error = NULL; + } + + if (bus->priv->connection) + ibus_bus_connect_finish (bus); + + /* unref the ref from ibus_bus_connect */ + g_object_unref (bus); +} + +static void +ibus_bus_connect_async (IBusBus *bus) +{ + ibus_bus_disconnect (bus); + + if (ibus_get_address () != NULL) { + g_object_ref (bus); + g_dbus_connection_new_for_address (ibus_get_address (), + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | + G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, + NULL, NULL, + _bus_connect_async_cb, bus); + } +} + +static void +ibus_bus_connect (IBusBus *bus) +{ + ibus_bus_disconnect (bus); + + if (ibus_get_address () != NULL) { + bus->priv->connection = + g_dbus_connection_new_for_address_sync (ibus_get_address (), + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | + G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, + NULL, NULL, NULL); + } + + ibus_bus_connect_finish (bus); +} + +static void _changed_cb (GFileMonitor *monitor, GFile *file, GFile *other_file, @@ -343,8 +396,6 @@ ibus_bus_init (IBusBus *bus) } } - ibus_bus_connect (bus); - file = g_file_new_for_path (ibus_get_socket_path ()); bus->priv->monitor = g_file_monitor_file (file, 0, NULL, NULL); @@ -482,9 +533,24 @@ ibus_bus_new (void) { IBusBus *bus = IBUS_BUS (g_object_new (IBUS_TYPE_BUS, NULL)); + if (!ibus_bus_is_connected(bus)) + ibus_bus_connect (bus); + return bus; } +IBusBus * +ibus_bus_new_async (void) +{ + IBusBus *bus = IBUS_BUS (g_object_new (IBUS_TYPE_BUS, NULL)); + + if (!ibus_bus_is_connected(bus)) + ibus_bus_connect_async (bus); + + return bus; +} + + gboolean ibus_bus_is_connected (IBusBus *bus) diff --git a/src/ibusbus.h b/src/ibusbus.h index 337091b..8fe0036 100644 --- a/src/ibusbus.h +++ b/src/ibusbus.h @@ -91,6 +91,16 @@ GType ibus_bus_get_type (void); IBusBus *ibus_bus_new (void); /** + * ibus_bus_new_async: + * @returns: A newly allocated #IBusBus instance, and the instance is not floating. + * + * New an #IBusBus instance. The instance will asynchronously connect to the IBus + * daemon. + */ +IBusBus *ibus_bus_new_async (void); + + +/** * ibus_bus_is_connected: * @bus: An #IBusBus. * @returns: %TRUE if @bus is connected, %FALSE otherwise. -- 2.7.4