[kdbus] Use new API insted of direct call to org.freedesktop.DBus
[platform/upstream/glib.git] / gio / gdbusnamewatching.c
index 2fd0347..2b2735f 100644 (file)
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: David Zeuthen <davidz@redhat.com>
  */
@@ -41,7 +39,8 @@
  *
  * Convenience API for watching bus names.
  *
- * <example id="gdbus-watching-names"><title>Simple application watching a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * A simple example for watching a name can be found in
+ * [gdbus-example-watch-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-name.c)
  */
 
 G_LOCK_DEFINE_STATIC (lock);
@@ -103,8 +102,7 @@ client_unref (Client *client)
         }
       g_free (client->name);
       g_free (client->name_owner);
-      if (client->main_context != NULL)
-        g_main_context_unref (client->main_context);
+      g_main_context_unref (client->main_context);
       if (client->user_data_free_func != NULL)
         client->user_data_free_func (client->user_data);
       g_free (client);
@@ -200,6 +198,7 @@ schedule_call_in_idle (Client *client, CallType call_type)
                          call_in_idle_cb,
                          data,
                          (GDestroyNotify) call_handler_data_free);
+  g_source_set_name (idle_source, "[gio] call_in_idle_cb");
   g_source_attach (idle_source, client->main_context);
   g_source_unref (idle_source);
 }
@@ -207,11 +206,15 @@ schedule_call_in_idle (Client *client, CallType call_type)
 static void
 do_call (Client *client, CallType call_type)
 {
+  GMainContext *current_context;
+
   /* only schedule in idle if we're not in the right thread */
-  if (g_main_context_get_thread_default () != client->main_context)
+  current_context = g_main_context_ref_thread_default ();
+  if (current_context != client->main_context)
     schedule_call_in_idle (client, call_type);
   else
     actually_do_call (client, client->connection, client->name_owner, call_type);
+  g_main_context_unref (current_context);
 }
 
 static void
@@ -319,6 +322,22 @@ on_name_owner_changed (GDBusConnection *connection,
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
+process_get_name_owner (Client      *client,
+                        const char  *name_owner)
+{
+  if (name_owner != NULL)
+    {
+      g_warn_if_fail (client->name_owner == NULL);
+      client->name_owner = g_strdup (name_owner);
+      call_appeared_handler (client);
+    }
+  else
+    call_vanished_handler (client, FALSE);
+
+  client->initialized = TRUE;
+}
+
+static void
 get_name_owner_cb (GObject      *source_object,
                    GAsyncResult *res,
                    gpointer      user_data)
@@ -338,18 +357,7 @@ get_name_owner_cb (GObject      *source_object,
       g_variant_get (result, "(&s)", &name_owner);
     }
 
-  if (name_owner != NULL)
-    {
-      g_warn_if_fail (client->name_owner == NULL);
-      client->name_owner = g_strdup (name_owner);
-      call_appeared_handler (client);
-    }
-  else
-    {
-      call_vanished_handler (client, FALSE);
-    }
-
-  client->initialized = TRUE;
+  process_get_name_owner (client, name_owner);
 
   if (result != NULL)
     g_variant_unref (result);
@@ -361,18 +369,31 @@ get_name_owner_cb (GObject      *source_object,
 static void
 invoke_get_name_owner (Client *client)
 {
-  g_dbus_connection_call (client->connection,
-                          "org.freedesktop.DBus",  /* bus name */
-                          "/org/freedesktop/DBus", /* object path */
-                          "org.freedesktop.DBus",  /* interface name */
-                          "GetNameOwner",          /* method name */
-                          g_variant_new ("(s)", client->name),
-                          G_VARIANT_TYPE ("(s)"),
-                          G_DBUS_CALL_FLAGS_NONE,
-                          -1,
-                          NULL,
-                          (GAsyncReadyCallback) get_name_owner_cb,
-                          client_ref (client));
+  if (_g_dbus_connection_is_kdbus (client->connection))
+    {
+      char *name_owner;
+
+      name_owner = g_dbus_get_name_owner (client->connection,
+                                          client->name,
+                                          NULL);
+      process_get_name_owner (client, name_owner);
+      g_free (name_owner);
+    }
+  else
+    {
+      g_dbus_connection_call (client->connection,
+                              "org.freedesktop.DBus",  /* bus name */
+                              "/org/freedesktop/DBus", /* object path */
+                              "org.freedesktop.DBus",  /* interface name */
+                              "GetNameOwner",          /* method name */
+                              g_variant_new ("(s)", client->name),
+                              G_VARIANT_TYPE ("(s)"),
+                              G_DBUS_CALL_FLAGS_NONE,
+                              -1,
+                              NULL,
+                              (GAsyncReadyCallback) get_name_owner_cb,
+                              client_ref (client));
+    }
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -454,18 +475,21 @@ has_connection (Client *client)
 
   if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
     {
-      g_dbus_connection_call (client->connection,
-                              "org.freedesktop.DBus",  /* bus name */
-                              "/org/freedesktop/DBus", /* object path */
-                              "org.freedesktop.DBus",  /* interface name */
-                              "StartServiceByName",    /* method name */
-                              g_variant_new ("(su)", client->name, 0),
-                              G_VARIANT_TYPE ("(u)"),
-                              G_DBUS_CALL_FLAGS_NONE,
-                              -1,
-                              NULL,
-                              (GAsyncReadyCallback) start_service_by_name_cb,
-                              client_ref (client));
+      if (_g_dbus_connection_is_kdbus (client->connection))
+        g_error ("TODO: Implement StartServiceByName\n");
+      else
+        g_dbus_connection_call (client->connection,
+                                "org.freedesktop.DBus",  /* bus name */
+                                "/org/freedesktop/DBus", /* object path */
+                                "org.freedesktop.DBus",  /* interface name */
+                                "StartServiceByName",    /* method name */
+                                g_variant_new ("(su)", client->name, 0),
+                                G_VARIANT_TYPE ("(u)"),
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                (GAsyncReadyCallback) start_service_by_name_cb,
+                                client_ref (client));
     }
   else
     {
@@ -502,17 +526,17 @@ connection_get_cb (GObject      *source_object,
  * @bus_type: The type of bus to watch a name on.
  * @name: The name (well-known or unique) to watch.
  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
- * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
- * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
+ * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
+ * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
  * @user_data: User data to pass to handlers.
- * @user_data_free_func: Function for freeing @user_data or %NULL.
+ * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
  *
  * Starts watching @name on the bus specified by @bus_type and calls
  * @name_appeared_handler and @name_vanished_handler when the name is
  * known to have a owner respectively known to lose its
- * owner. Callbacks will be invoked in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this function from.
+ * owner. Callbacks will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this function from.
  *
  * You are guaranteed that one of the handlers will be invoked after
  * calling this function. When you are done watching the name, just
@@ -531,11 +555,11 @@ connection_get_cb (GObject      *source_object,
  * guaranteed that the next time one of the handlers is invoked, it
  * will be @name_vanished_handler. The reverse is also true.
  *
- * This behavior makes it very simple to write applications that wants
- * to take action when a certain name exists, see <xref
- * linkend="gdbus-watching-names"/>. Basically, the application
- * should create object proxies in @name_appeared_handler and destroy
- * them again (if any) in @name_vanished_handler.
+ * This behavior makes it very simple to write applications that want
+ * to take action when a certain [name exists][gdbus-watching-names].
+ * Basically, the application should create object proxies in
+ * @name_appeared_handler and destroy them again (if any) in
+ * @name_vanished_handler.
  *
  * Returns: An identifier (never 0) that an be used with
  * g_bus_unwatch_name() to stop watching the name.
@@ -566,9 +590,7 @@ g_bus_watch_name (GBusType                  bus_type,
   client->name_vanished_handler = name_vanished_handler;
   client->user_data = user_data;
   client->user_data_free_func = user_data_free_func;
-  client->main_context = g_main_context_get_thread_default ();
-  if (client->main_context != NULL)
-    g_main_context_ref (client->main_context);
+  client->main_context = g_main_context_ref_thread_default ();
 
   if (map_id_to_client == NULL)
     {
@@ -593,10 +615,10 @@ g_bus_watch_name (GBusType                  bus_type,
  * @connection: A #GDBusConnection.
  * @name: The name (well-known or unique) to watch.
  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
- * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
- * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
+ * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
+ * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
  * @user_data: User data to pass to handlers.
- * @user_data_free_func: Function for freeing @user_data or %NULL.
+ * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
  *
  * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
  * #GBusType.
@@ -630,9 +652,7 @@ guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
   client->name_vanished_handler = name_vanished_handler;
   client->user_data = user_data;
   client->user_data_free_func = user_data_free_func;
-  client->main_context = g_main_context_get_thread_default ();
-  if (client->main_context != NULL)
-    g_main_context_ref (client->main_context);
+  client->main_context = g_main_context_ref_thread_default ();
 
   if (map_id_to_client == NULL)
     map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
@@ -688,7 +708,7 @@ watch_with_closures_on_name_appeared (GDBusConnection *connection,
                                       gpointer         user_data)
 {
   WatchNameData *data = user_data;
-  GValue params[3] = { { 0, }, { 0, }, { 0, } };
+  GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
 
   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
   g_value_set_object (&params[0], connection);
@@ -712,7 +732,7 @@ watch_with_closures_on_name_vanished (GDBusConnection *connection,
                                       gpointer         user_data)
 {
   WatchNameData *data = user_data;
-  GValue params[2] = { { 0, }, { 0, } };
+  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
 
   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
   g_value_set_object (&params[0], connection);