[glib/gdbus] Use generic message send API in GBusNameOwner and GBusNameWatcher



commit a49aaa5a9e2068072c941562db6a47ebcc26ddd0
Author: David Zeuthen <davidz redhat com>
Date:   Wed Apr 22 15:02:32 2009 -0400

    Use generic message send API in GBusNameOwner and GBusNameWatcher
---
 gdbus/gbusnameowner.c    |  459 ++++++++-------------------------------------
 gdbus/gbusnamewatcher.c  |  443 ++++++++------------------------------------
 gdbus/gdbusconnection.c  |   15 ++-
 gdbus/tests/connection.c |    1 +
 4 files changed, 173 insertions(+), 745 deletions(-)

diff --git a/gdbus/gbusnameowner.c b/gdbus/gbusnameowner.c
index c816c6e..c074984 100644
--- a/gdbus/gbusnameowner.c
+++ b/gdbus/gbusnameowner.c
@@ -86,29 +86,6 @@ filter_function (DBusConnection *dbus_1_connection,
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-static void bus_request_name (GDBusConnection       *connection,
-                              const gchar           *name,
-                              guint                  flags,
-                              GAsyncReadyCallback    callback,
-                              gpointer               user_data);
-static guint bus_request_name_finish (GDBusConnection  *bus,
-                                      GAsyncResult     *res,
-                                      GError          **error);
-
-static guint bus_release_name_sync (GDBusConnection       *connection,
-                                    const gchar           *name,
-                                    GError               **error);
-
-#if 0
-static void bus_release_name (GDBusConnection       *connection,
-                              const gchar           *name,
-                              GAsyncReadyCallback    callback,
-                              gpointer               user_data);
-static guint bus_release_name_finish (GDBusConnection  *bus,
-                                      GAsyncResult     *res,
-                                      GError          **error);
-#endif
-
 static void on_connection_opened (GDBusConnection *connection,
                                   gpointer         user_data);
 
@@ -142,27 +119,18 @@ g_bus_name_owner_finalize (GObject *object)
       /* release the name if we own it */
       if (owner->priv->owns_name)
         {
-          /* Gah, even though there's a ReleaseName() method in flight,
-           * we can't use RequestName() until it is processed.. This must
-           * be a bug in the message bus daemon.
-           *
-           * So do ReleaseName() synchronously for now.
-           */
-          GError *error;
-          guint reply;
-          error = NULL;
-          reply = bus_release_name_sync (owner->priv->connection,
-                                         owner->priv->name,
-                                         &error);
-          if (error != NULL)
-            {
-              g_warning ("Error doing ReleaseName(): %s", error->message);
-              g_error_free (error);
-            }
-          else if (reply != DBUS_RELEASE_NAME_REPLY_RELEASED)
-            {
-              g_warning ("Expected DBUS_RELEASE_NAME_REPLY_RELEASED but got %d", reply);
-            }
+          DBusMessage *message;
+          if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                                       DBUS_PATH_DBUS,
+                                                       DBUS_INTERFACE_DBUS,
+                                                       "ReleaseName")) == NULL)
+            _g_dbus_oom ();
+          if (!dbus_message_append_args (message,
+                                         DBUS_TYPE_STRING, &owner->priv->name,
+                                         DBUS_TYPE_INVALID))
+            _g_dbus_oom ();
+          g_dbus_connection_send_dbus_1_message (owner->priv->connection, message);
+          dbus_message_unref (message);
         }
 
       g_signal_handlers_disconnect_by_func (owner->priv->connection, on_connection_opened, owner);
@@ -373,6 +341,8 @@ on_connection_opened (GDBusConnection *connection,
                       gpointer         user_data)
 {
   GBusNameOwner *owner = G_BUS_NAME_OWNER (user_data);
+  DBusMessage *message;
+  dbus_uint32_t flags;
 
   /* set up a filter function for listening on NameLost and NameAcquired messages on the DBusConnection */
   if (!dbus_connection_add_filter (g_dbus_connection_get_dbus_1_connection (owner->priv->connection),
@@ -391,11 +361,19 @@ on_connection_opened (GDBusConnection *connection,
   /* we don't care about the reply.. we'll know soon enough, via a NameAcquired
    * signal, if we managed to claim the name
    */
-  bus_request_name (owner->priv->connection,
-                    owner->priv->name,
-                    get_request_name_flags (owner->priv->flags),
-                    NULL,
-                    NULL);
+  flags = get_request_name_flags (owner->priv->flags);
+  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS,
+                                               "RequestName")) == NULL)
+    _g_dbus_oom ();
+  if (!dbus_message_append_args (message,
+                                 DBUS_TYPE_STRING, &owner->priv->name,
+                                 DBUS_TYPE_UINT32, &flags,
+                                 DBUS_TYPE_INVALID))
+    _g_dbus_oom ();
+  g_dbus_connection_send_dbus_1_message (owner->priv->connection, message);
+  dbus_message_unref (message);
 }
 
 static void
@@ -559,202 +537,6 @@ g_bus_name_owner_init (GBusNameOwner *owner)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-#if 0
-
-static void
-bus_release_name_cb (DBusPendingCall *pending,
-                     void            *user_data)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
-  DBusMessage *reply;
-  DBusError dbus_error;
-  GError *error;
-  dbus_uint32_t release_name_reply;
-
-  reply = dbus_pending_call_steal_reply (pending);
-  g_assert (reply != NULL);
-
-  dbus_error_init (&dbus_error);
-  if (!dbus_set_error_from_message (reply, &dbus_error))
-    {
-      if (dbus_message_get_args (reply,
-                                 &dbus_error,
-                                 DBUS_TYPE_UINT32, &release_name_reply,
-                                 DBUS_TYPE_INVALID))
-        {
-          g_simple_async_result_set_op_res_gpointer (simple, GINT_TO_POINTER (release_name_reply), NULL);
-          goto done;
-        }
-    }
-  error = g_dbus_error_new_for_dbus_error (&dbus_error,
-                                           NULL,
-                                           NULL);
-  g_simple_async_result_set_from_error (simple, error);
-  g_error_free (error);
-  dbus_error_free (&dbus_error);
-
- done:
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
-  dbus_message_unref (reply);
-}
-
-static void
-bus_release_name (GDBusConnection       *connection,
-                  const gchar           *name,
-                  GAsyncReadyCallback    callback,
-                  gpointer               user_data)
-{
-  GSimpleAsyncResult *simple;
-  DBusMessage *message;
-  DBusPendingCall *pending_call;
-
-  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  g_return_if_fail (name != NULL);
-
-  simple = g_simple_async_result_new (G_OBJECT (connection),
-                                      callback,
-                                      user_data,
-                                      bus_release_name);
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_simple_async_result_set_error (simple,
-                                       G_DBUS_ERROR,
-                                       G_DBUS_ERROR_FAILED,
-                                       _("Not connected to message bus"));
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "ReleaseName")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &name,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  if (!dbus_connection_send_with_reply (g_dbus_connection_get_dbus_1_connection (connection),
-                                        message,
-                                        &pending_call,
-                                        -1))
-    _g_dbus_oom ();
-
-  dbus_message_unref (message);
-
-  dbus_pending_call_set_notify (pending_call,
-                                bus_release_name_cb,
-                                simple,
-                                NULL);
-
- out:
-  ;
-}
-
-static guint
-bus_release_name_finish (GDBusConnection  *bus,
-                         GAsyncResult     *res,
-                         GError          **error)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-  guint ret;
-
-  ret = 0;
-
-  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == bus_release_name);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    goto out;
-
-  ret = GPOINTER_TO_INT (g_simple_async_result_get_op_res_gpointer (simple));
-
- out:
-  return ret;
-}
-#endif
-
-static guint
-bus_release_name_sync (GDBusConnection       *connection,
-                       const gchar           *name,
-                       GError               **error)
-{
-  DBusMessage *message;
-  DBusMessage *reply;
-  dbus_uint32_t release_name_reply;
-  DBusError dbus_error;
-
-  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
-  g_return_val_if_fail (name != NULL, 0);
-
-  message = NULL;
-  reply = NULL;
-  release_name_reply = 0;
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_set_error (error,
-                   G_DBUS_ERROR,
-                   G_DBUS_ERROR_FAILED,
-                   _("Not connected to message bus"));
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "ReleaseName")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &name,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  dbus_error_init (&dbus_error);
-  reply = dbus_connection_send_with_reply_and_block (g_dbus_connection_get_dbus_1_connection (connection),
-                                                     message,
-                                                     -1,
-                                                     &dbus_error);
-  if (reply == NULL)
-    {
-      g_dbus_error_set_dbus_error (error,
-                                   &dbus_error,
-                                   NULL,
-                                   NULL);
-      dbus_error_free (&dbus_error);
-      goto out;
-    }
-
-  if (!dbus_set_error_from_message (&dbus_error, reply))
-    {
-      if (dbus_message_get_args (reply,
-                                 &dbus_error,
-                                 DBUS_TYPE_UINT32, &release_name_reply,
-                                 DBUS_TYPE_INVALID))
-        {
-          goto out;
-        }
-    }
-  g_dbus_error_set_dbus_error (error,
-                               &dbus_error,
-                               NULL,
-                               NULL);
-  dbus_error_free (&dbus_error);
-
- out:
-  if (message != NULL)
-    dbus_message_unref (message);
-  if (reply != NULL)
-    dbus_message_unref (reply);
-
-  return release_name_reply;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
-
 static guint
 get_request_name_flags (GBusNameOwnerFlags flags)
 {
@@ -769,123 +551,6 @@ get_request_name_flags (GBusNameOwnerFlags flags)
   return request_name_flags;
 }
 
-static void
-bus_request_name_cb (DBusPendingCall *pending,
-                     void            *user_data)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
-  DBusMessage *reply;
-  DBusError dbus_error;
-  GError *error;
-  dbus_uint32_t request_name_reply;
-
-  reply = dbus_pending_call_steal_reply (pending);
-  g_assert (reply != NULL);
-
-  dbus_error_init (&dbus_error);
-  if (!dbus_set_error_from_message (&dbus_error, reply))
-    {
-      if (dbus_message_get_args (reply,
-                                 &dbus_error,
-                                 DBUS_TYPE_UINT32, &request_name_reply,
-                                 DBUS_TYPE_INVALID))
-        {
-          g_simple_async_result_set_op_res_gpointer (simple, GINT_TO_POINTER (request_name_reply), NULL);
-          goto done;
-        }
-    }
-  error = g_dbus_error_new_for_dbus_error (&dbus_error,
-                                           NULL,
-                                           NULL);
-  g_simple_async_result_set_from_error (simple, error);
-  g_error_free (error);
-  dbus_error_free (&dbus_error);
-
- done:
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
-  dbus_message_unref (reply);
-}
-
-static void
-bus_request_name (GDBusConnection       *connection,
-                  const gchar           *name,
-                  guint                  flags,
-                  GAsyncReadyCallback    callback,
-                  gpointer               user_data)
-{
-  GSimpleAsyncResult *simple;
-  DBusMessage *message;
-  DBusPendingCall *pending_call;
-
-  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  g_return_if_fail (name != NULL);
-
-  simple = g_simple_async_result_new (G_OBJECT (connection),
-                                      callback,
-                                      user_data,
-                                      bus_request_name);
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_simple_async_result_set_error (simple,
-                                       G_DBUS_ERROR,
-                                       G_DBUS_ERROR_FAILED,
-                                       _("Not connected to message bus"));
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "RequestName")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &name,
-                                 DBUS_TYPE_UINT32, &flags,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  if (!dbus_connection_send_with_reply (g_dbus_connection_get_dbus_1_connection (connection),
-                                        message,
-                                        &pending_call,
-                                        -1))
-    _g_dbus_oom ();
-
-  dbus_message_unref (message);
-
-  dbus_pending_call_set_notify (pending_call,
-                                bus_request_name_cb,
-                                simple,
-                                NULL);
-
- out:
-  ;
-}
-
-static guint
-bus_request_name_finish (GDBusConnection  *bus,
-                         GAsyncResult     *res,
-                         GError          **error)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-  guint ret;
-
-  ret = 0;
-
-  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == bus_request_name);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    goto out;
-
-  ret = GPOINTER_TO_INT (g_simple_async_result_get_op_res_gpointer (simple));
-
- out:
-  return ret;
-}
-
 /* ---------------------------------------------------------------------------------------------------- */
 /* routines used for singleton handling */
 
@@ -991,13 +656,32 @@ request_name_cb (GDBusConnection *connection,
   GBusNameOwner *owner = G_BUS_NAME_OWNER (user_data);
   GSimpleAsyncResult *simple;
   GError *error;
-  guint reply;
+  DBusMessage *reply;
+  DBusError dbus_error;
+  dbus_uint32_t request_name_reply;
   guint n;
 
   error = NULL;
-  reply = bus_request_name_finish (connection,
-                                   result,
-                                   &error);
+  reply = g_dbus_connection_send_dbus_1_message_with_reply_finish (connection,
+                                                                   result,
+                                                                   &error);
+  if (reply != NULL)
+    {
+      dbus_error_init (&dbus_error);
+      if (!dbus_set_error_from_message (&dbus_error, reply))
+        {
+          dbus_message_get_args (reply,
+                                 &dbus_error,
+                                 DBUS_TYPE_UINT32, &request_name_reply,
+                                 DBUS_TYPE_INVALID);
+        }
+      if (dbus_error_is_set (&dbus_error))
+        {
+          error = g_dbus_error_new_for_dbus_error (&dbus_error, NULL, NULL);
+          dbus_error_free (&dbus_error);
+        }
+    }
+
   G_LOCK (owner_lock);
   if (error != NULL)
     {
@@ -1013,7 +697,7 @@ request_name_cb (GDBusConnection *connection,
     }
   else
     {
-      if (reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+      if (request_name_reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
         owner->priv->owns_name = TRUE;
 
       /* complete operations without error */
@@ -1029,6 +713,35 @@ request_name_cb (GDBusConnection *connection,
   g_ptr_array_free (owner->priv->init_phase_pending_simple_async_results, TRUE);
   owner->priv->init_phase_pending_simple_async_results = NULL;
   G_UNLOCK (owner_lock);
+
+  if (reply != NULL)
+    dbus_message_unref (reply);
+}
+
+static void
+do_request_name (GBusNameOwner *owner)
+{
+  DBusMessage *message;
+  dbus_uint32_t flags;
+
+  flags = get_request_name_flags (owner->priv->flags);
+  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS,
+                                               "RequestName")) == NULL)
+    _g_dbus_oom ();
+  if (!dbus_message_append_args (message,
+                                 DBUS_TYPE_STRING, &owner->priv->name,
+                                 DBUS_TYPE_UINT32, &flags,
+                                 DBUS_TYPE_INVALID))
+    _g_dbus_oom ();
+  g_dbus_connection_send_dbus_1_message_with_reply (owner->priv->connection,
+                                                    message,
+                                                    -1,
+                                                    NULL,
+                                                    (GAsyncReadyCallback) request_name_cb,
+                                                    owner);
+  dbus_message_unref (message);
 }
 
 static void
@@ -1039,11 +752,7 @@ on_notify_is_opening (GDBusConnection *connection,
   GBusNameOwner *owner = G_BUS_NAME_OWNER (user_data);
 
   /* connection is not opening anymore.. try to own the name */
-  bus_request_name (owner->priv->connection,
-                    owner->priv->name,
-                    get_request_name_flags (owner->priv->flags),
-                    (GAsyncReadyCallback) request_name_cb,
-                    owner);
+  do_request_name (owner);
 
   g_signal_handlers_disconnect_by_func (connection, on_notify_is_opening, owner);
 }
@@ -1142,11 +851,7 @@ g_bus_name_owner_new_internal (GBusType               bus_type,
   else
     {
       /* connection is not opening.. try to claim the name */
-      bus_request_name (owner->priv->connection,
-                        owner->priv->name,
-                        get_request_name_flags (owner->priv->flags),
-                        (GAsyncReadyCallback) request_name_cb,
-                        owner);
+      do_request_name (owner);
     }
 
 
diff --git a/gdbus/gbusnamewatcher.c b/gdbus/gbusnamewatcher.c
index 2a1a694..c046a07 100644
--- a/gdbus/gbusnamewatcher.c
+++ b/gdbus/gbusnamewatcher.c
@@ -87,34 +87,6 @@ static GBusNameWatcher *singletons_get_watcher (GDBusConnection *connection, con
 static void singletons_add_watcher (GBusNameWatcher *watcher);
 static void singletons_remove_watcher (GBusNameWatcher *watcher);
 
-static void bus_get_name_owner (GDBusConnection       *connection,
-                                const gchar           *name,
-                                GAsyncReadyCallback    callback,
-                                gpointer               user_data);
-static gchar *bus_get_name_owner_finish (GDBusConnection  *bus,
-                                         GAsyncResult     *res,
-                                         GError          **error);
-
-static void bus_add_match (GDBusConnection       *connection,
-                           const gchar           *match_rule,
-                           GAsyncReadyCallback    callback,
-                           gpointer               user_data);
-#if 0
-static gboolean bus_add_match_finish (GDBusConnection  *bus,
-                                      GAsyncResult     *res,
-                                      GError          **error);
-#endif
-
-static void bus_remove_match (GDBusConnection       *connection,
-                              const gchar           *match_rule,
-                              GAsyncReadyCallback    callback,
-                              gpointer               user_data);
-#if 0
-static gboolean bus_remove_match_finish (GDBusConnection  *bus,
-                                         GAsyncResult     *res,
-                                         GError          **error);
-#endif
-
 static void on_connection_opened (GDBusConnection *connection,
                                   gpointer         user_data);
 
@@ -148,11 +120,18 @@ g_bus_name_watcher_finalize (GObject *object)
 
       if (g_dbus_connection_get_is_open (watcher->priv->connection))
         {
-          bus_remove_match (watcher->priv->connection,
-                            watcher->priv->match_rule,
-                            NULL,
-                            NULL);
-
+          DBusMessage *message;
+          if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                                       DBUS_PATH_DBUS,
+                                                       DBUS_INTERFACE_DBUS,
+                                                       "RemoveMatch")) == NULL)
+            _g_dbus_oom ();
+          if (!dbus_message_append_args (message,
+                                         DBUS_TYPE_STRING, &watcher->priv->match_rule,
+                                         DBUS_TYPE_INVALID))
+            _g_dbus_oom ();
+          g_dbus_connection_send_dbus_1_message (watcher->priv->connection, message);
+          dbus_message_unref (message);
           dbus_connection_remove_filter (g_dbus_connection_get_dbus_1_connection (watcher->priv->connection),
                                          filter_function,
                                          watcher);
@@ -332,6 +311,7 @@ on_connection_opened (GDBusConnection *connection,
                       gpointer         user_data)
 {
   GBusNameWatcher *watcher = G_BUS_NAME_WATCHER (user_data);
+  DBusMessage *message;
 
   /* set up a filter function for listening on (some) NameOwnerChanged messages on the DBusConnection */
   if (!dbus_connection_add_filter (g_dbus_connection_get_dbus_1_connection (watcher->priv->connection),
@@ -341,10 +321,17 @@ on_connection_opened (GDBusConnection *connection,
     _g_dbus_oom ();
 
   /* set up match rule */
-  bus_add_match (watcher->priv->connection,
-                 watcher->priv->match_rule,
-                 NULL,
-                 NULL);
+  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS,
+                                               "AddMatch")) == NULL)
+    _g_dbus_oom ();
+  if (!dbus_message_append_args (message,
+                                 DBUS_TYPE_STRING, &watcher->priv->match_rule,
+                                 DBUS_TYPE_INVALID))
+    _g_dbus_oom ();
+  g_dbus_connection_send_dbus_1_message (watcher->priv->connection, message);
+  dbus_message_unref (message);
 
   /* TODO: if this is for a bus reconnection, we actually need check bus
    * name again since someone may acquire the name before our match
@@ -501,321 +488,6 @@ g_bus_name_watcher_init (GBusNameWatcher *watcher)
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
-
-static void
-bus_add_match_cb (DBusPendingCall *pending,
-                  void            *user_data)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
-  DBusMessage *reply;
-  DBusError dbus_error;
-  GError *error;
-
-  reply = dbus_pending_call_steal_reply (pending);
-  g_assert (reply != NULL);
-
-  dbus_error_init (&dbus_error);
-  if (!dbus_set_error_from_message (&dbus_error, reply))
-    goto done;
-
-  error = g_dbus_error_new_for_dbus_error (&dbus_error,
-                                           NULL,
-                                           NULL);
-  g_simple_async_result_set_from_error (simple, error);
-  g_error_free (error);
-  dbus_error_free (&dbus_error);
-
- done:
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
-  dbus_message_unref (reply);
-}
-
-static void
-bus_add_match (GDBusConnection       *connection,
-               const gchar           *match_rule,
-               GAsyncReadyCallback    callback,
-               gpointer               user_data)
-{
-  GSimpleAsyncResult *simple;
-  DBusMessage *message;
-  DBusPendingCall *pending_call;
-
-  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  g_return_if_fail (match_rule != NULL);
-
-  simple = g_simple_async_result_new (G_OBJECT (connection),
-                                      callback,
-                                      user_data,
-                                      bus_add_match);
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_simple_async_result_set_error (simple,
-                                       G_DBUS_ERROR,
-                                       G_DBUS_ERROR_FAILED,
-                                       _("Not connected to message bus"));
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "AddMatch")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &match_rule,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  if (!dbus_connection_send_with_reply (g_dbus_connection_get_dbus_1_connection (connection),
-                                        message,
-                                        &pending_call,
-                                        -1))
-    _g_dbus_oom ();
-
-  dbus_message_unref (message);
-
-  dbus_pending_call_set_notify (pending_call,
-                                bus_add_match_cb,
-                                simple,
-                                NULL);
-
- out:
-  ;
-}
-
-#if 0
-static gboolean
-bus_add_match_finish (GDBusConnection  *bus,
-                      GAsyncResult     *res,
-                      GError          **error)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == bus_add_match);
-  return !g_simple_async_result_propagate_error (simple, error);
-}
-#endif
-
-/* ---------------------------------------------------------------------------------------------------- */
-
-static void
-bus_remove_match_cb (DBusPendingCall *pending,
-                     void            *user_data)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
-  DBusMessage *reply;
-  DBusError dbus_error;
-  GError *error;
-
-  reply = dbus_pending_call_steal_reply (pending);
-  g_assert (reply != NULL);
-
-  dbus_error_init (&dbus_error);
-  if (!dbus_set_error_from_message (&dbus_error, reply))
-    goto done;
-
-  error = g_dbus_error_new_for_dbus_error (&dbus_error,
-                                           NULL,
-                                           NULL);
-  g_simple_async_result_set_from_error (simple, error);
-  g_error_free (error);
-  dbus_error_free (&dbus_error);
-
- done:
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
-  dbus_message_unref (reply);
-}
-
-static void
-bus_remove_match (GDBusConnection       *connection,
-                  const gchar           *match_rule,
-                  GAsyncReadyCallback    callback,
-                  gpointer               user_data)
-{
-  GSimpleAsyncResult *simple;
-  DBusMessage *message;
-  DBusPendingCall *pending_call;
-
-  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  g_return_if_fail (match_rule != NULL);
-
-  simple = g_simple_async_result_new (G_OBJECT (connection),
-                                      callback,
-                                      user_data,
-                                      bus_remove_match);
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_simple_async_result_set_error (simple,
-                                       G_DBUS_ERROR,
-                                       G_DBUS_ERROR_FAILED,
-                                       _("Not connected to message bus"));
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "RemoveMatch")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &match_rule,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  if (!dbus_connection_send_with_reply (g_dbus_connection_get_dbus_1_connection (connection),
-                                        message,
-                                        &pending_call,
-                                        -1))
-    _g_dbus_oom ();
-
-  dbus_message_unref (message);
-
-  dbus_pending_call_set_notify (pending_call,
-                                bus_remove_match_cb,
-                                simple,
-                                NULL);
-
- out:
-  ;
-}
-
-#if 0
-static gboolean
-bus_remove_match_finish (GDBusConnection  *bus,
-                         GAsyncResult     *res,
-                         GError          **error)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == bus_remove_match);
-  return !g_simple_async_result_propagate_error (simple, error);
-}
-#endif
-
-/* ---------------------------------------------------------------------------------------------------- */
-
-static void
-bus_get_name_owner_cb (DBusPendingCall *pending,
-                       void            *user_data)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
-  DBusMessage *reply;
-  DBusError dbus_error;
-  GError *error;
-  gchar *get_name_owner_reply;
-
-  reply = dbus_pending_call_steal_reply (pending);
-  g_assert (reply != NULL);
-
-  dbus_error_init (&dbus_error);
-  if (!dbus_set_error_from_message (&dbus_error, reply))
-    {
-      if (dbus_message_get_args (reply,
-                                 &dbus_error,
-                                 DBUS_TYPE_STRING, &get_name_owner_reply,
-                                 DBUS_TYPE_INVALID))
-        {
-          g_simple_async_result_set_op_res_gpointer (simple, g_strdup (get_name_owner_reply), g_free);
-          goto done;
-        }
-    }
-  error = g_dbus_error_new_for_dbus_error (&dbus_error,
-                                           NULL,
-                                           NULL);
-  g_simple_async_result_set_from_error (simple, error);
-  g_error_free (error);
-  dbus_error_free (&dbus_error);
-
- done:
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
-  dbus_message_unref (reply);
-}
-
-static void
-bus_get_name_owner (GDBusConnection       *connection,
-                    const gchar           *name,
-                    GAsyncReadyCallback    callback,
-                    gpointer               user_data)
-{
-  GSimpleAsyncResult *simple;
-  DBusMessage *message;
-  DBusPendingCall *pending_call;
-
-  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
-  g_return_if_fail (name != NULL);
-
-  simple = g_simple_async_result_new (G_OBJECT (connection),
-                                      callback,
-                                      user_data,
-                                      bus_get_name_owner);
-
-  if (g_dbus_connection_get_dbus_1_connection (connection) == NULL)
-    {
-      g_simple_async_result_set_error (simple,
-                                       G_DBUS_ERROR,
-                                       G_DBUS_ERROR_FAILED,
-                                       _("Not connected to message bus"));
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
-      goto out;
-    }
-
-  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                               DBUS_PATH_DBUS,
-                                               DBUS_INTERFACE_DBUS,
-                                               "GetNameOwner")) == NULL)
-    _g_dbus_oom ();
-  if (!dbus_message_append_args (message,
-                                 DBUS_TYPE_STRING, &name,
-                                 DBUS_TYPE_INVALID))
-    _g_dbus_oom ();
-
-  if (!dbus_connection_send_with_reply (g_dbus_connection_get_dbus_1_connection (connection),
-                                        message,
-                                        &pending_call,
-                                        -1))
-    _g_dbus_oom ();
-
-  dbus_message_unref (message);
-
-  dbus_pending_call_set_notify (pending_call,
-                                bus_get_name_owner_cb,
-                                simple,
-                                NULL);
-
- out:
-  ;
-}
-
-static gchar *
-bus_get_name_owner_finish (GDBusConnection  *bus,
-                           GAsyncResult     *res,
-                           GError          **error)
-{
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-  gchar *ret;
-
-  ret = NULL;
-
-  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == bus_get_name_owner);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    goto out;
-
-  ret = g_strdup (g_simple_async_result_get_op_res_gpointer (simple));
-
- out:
-  return ret;
-}
-
-/* ---------------------------------------------------------------------------------------------------- */
 /* routines used for singleton handling */
 
 /* maps from connection to GHashTable (that maps from names to GBusNameWatcher) */
@@ -921,13 +593,32 @@ get_name_owner_cb (GDBusConnection *connection,
   GBusNameWatcher *watcher = G_BUS_NAME_WATCHER (user_data);
   GSimpleAsyncResult *simple;
   GError *error;
-  gchar *name_owner;
+  DBusMessage *reply;
+  DBusError dbus_error;
+  const gchar *name_owner;
   guint n;
 
   error = NULL;
-  name_owner = bus_get_name_owner_finish (connection,
-                                          result,
-                                          &error);
+  reply = g_dbus_connection_send_dbus_1_message_with_reply_finish (connection,
+                                                                   result,
+                                                                   &error);
+  name_owner = NULL;
+  if (reply != NULL)
+    {
+      dbus_error_init (&dbus_error);
+      if (!dbus_set_error_from_message (&dbus_error, reply))
+        {
+          dbus_message_get_args (reply,
+                                 &dbus_error,
+                                 DBUS_TYPE_STRING, &name_owner,
+                                 DBUS_TYPE_INVALID);
+        }
+      if (dbus_error_is_set (&dbus_error))
+        {
+          error = g_dbus_error_new_for_dbus_error (&dbus_error, NULL, NULL);
+          dbus_error_free (&dbus_error);
+        }
+    }
 
   G_LOCK (watcher_lock);
 
@@ -945,7 +636,7 @@ get_name_owner_cb (GDBusConnection *connection,
     }
   else
     {
-      watcher->priv->name_owner = name_owner; /* steals the returned string */
+      watcher->priv->name_owner = g_strdup (name_owner);
 
       /* complete operations without error */
       for (n = 0; n < watcher->priv->init_phase_pending_simple_async_results->len; n++)
@@ -960,23 +651,46 @@ get_name_owner_cb (GDBusConnection *connection,
   g_ptr_array_free (watcher->priv->init_phase_pending_simple_async_results, TRUE);
   watcher->priv->init_phase_pending_simple_async_results = NULL;
   G_UNLOCK (watcher_lock);
+
+  if (reply != NULL)
+    dbus_message_unref (reply);
 }
 
 
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
+do_get_name_owner (GBusNameWatcher *watcher)
+{
+  DBusMessage *message;
+
+  if ((message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS,
+                                               "GetNameOwner")) == NULL)
+    _g_dbus_oom ();
+  if (!dbus_message_append_args (message,
+                                 DBUS_TYPE_STRING, &watcher->priv->name,
+                                 DBUS_TYPE_INVALID))
+    _g_dbus_oom ();
+  g_dbus_connection_send_dbus_1_message_with_reply (watcher->priv->connection,
+                                                    message,
+                                                    -1,
+                                                    NULL,
+                                                    (GAsyncReadyCallback) get_name_owner_cb,
+                                                    watcher);
+  dbus_message_unref (message);
+}
+
+static void
 on_notify_is_opening (GDBusConnection *connection,
                       GParamSpec      *spec,
                       gpointer         user_data)
 {
   GBusNameWatcher *watcher = G_BUS_NAME_WATCHER (user_data);
 
-  /* connection is open.. check if the name exists */
-  bus_get_name_owner (watcher->priv->connection,
-                      watcher->priv->name,
-                      (GAsyncReadyCallback) get_name_owner_cb,
-                      watcher);
+  /* connection is not opening anymore.. get the name owner */
+  do_get_name_owner (watcher);
 
   g_signal_handlers_disconnect_by_func (connection, on_notify_is_opening, watcher);
 }
@@ -1072,10 +786,7 @@ g_bus_name_watcher_new_internal (GBusType               bus_type,
   else
     {
       /* connection is open.. check if the name exists */
-      bus_get_name_owner (watcher->priv->connection,
-                          watcher->priv->name,
-                          (GAsyncReadyCallback) get_name_owner_cb,
-                          watcher);
+      do_get_name_owner (watcher);
     }
 
  out:
diff --git a/gdbus/gdbusconnection.c b/gdbus/gdbusconnection.c
index 8a3bcb7..6d612b0 100644
--- a/gdbus/gdbusconnection.c
+++ b/gdbus/gdbusconnection.c
@@ -1083,8 +1083,19 @@ void
 g_dbus_connection_send_dbus_1_message (GDBusConnection    *connection,
                                        DBusMessage        *message)
 {
-  /* TODO: implement */
-  g_assert_not_reached ();
+  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
+  g_return_if_fail (message != NULL);
+
+  if (connection->priv->dbus_1_connection == NULL)
+    goto out;
+
+  if (!dbus_connection_send (connection->priv->dbus_1_connection,
+                             message,
+                             NULL))
+    _g_dbus_oom ();
+
+ out:
+  ;
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
diff --git a/gdbus/tests/connection.c b/gdbus/tests/connection.c
index c4840c0..9c473db 100644
--- a/gdbus/tests/connection.c
+++ b/gdbus/tests/connection.c
@@ -1310,6 +1310,7 @@ test_bus_name_watcher (void)
 
   /* now check that we get ::name-vanished when the bus goes away */
   g_signal_connect (w, "name-vanished", G_CALLBACK (bnw_on_name_vanished), NULL);
+  g_dbus_connection_set_exit_on_close (g_bus_name_watcher_get_connection (w), FALSE);
   session_bus_down ();
   g_main_loop_run (loop);
   g_assert_cmpstr (g_bus_name_watcher_get_name_owner (w), ==, NULL);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]