*
* Convenience API for owning bus names.
*
- * <example id="gdbus-owning-names"><title>Simple application owning a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-own-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * A simple example for owning a name can be found in
+ * [gdbus-example-own-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-own-name.c)
*/
G_LOCK_DEFINE_STATIC (lock);
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);
}
/* ---------------------------------------------------------------------------------------------------- */
static void
-request_name_cb (GObject *source_object,
- GAsyncResult *res,
- gpointer user_data)
+process_request_name_reply (Client *client,
+ guint32 request_name_reply)
{
- Client *client = user_data;
- GVariant *result;
- guint32 request_name_reply;
gboolean subscribe;
- request_name_reply = 0;
- result = NULL;
-
- /* don't use client->connection - it may be NULL already */
- result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
- res,
- NULL);
- if (result != NULL)
- {
- g_variant_get (result, "(u)", &request_name_reply);
- g_variant_unref (result);
- }
-
subscribe = FALSE;
switch (request_name_reply)
g_object_unref (connection);
}
}
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static void
+request_name_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ Client *client = user_data;
+ GVariant *result;
+ guint32 request_name_reply;
+
+ request_name_reply = 0;
+ result = NULL;
+
+ /* don't use client->connection - it may be NULL already */
+ result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
+ res,
+ NULL);
+ if (result != NULL)
+ {
+ g_variant_get (result, "(u)", &request_name_reply);
+ g_variant_unref (result);
+ }
+
+ process_request_name_reply (client, request_name_reply);
client_unref (client);
}
"closed",
G_CALLBACK (on_connection_disconnected),
client);
-
/* attempt to acquire the name */
- g_dbus_connection_call (client->connection,
- "org.freedesktop.DBus", /* bus name */
- "/org/freedesktop/DBus", /* object path */
- "org.freedesktop.DBus", /* interface name */
- "RequestName", /* method name */
- g_variant_new ("(su)",
- client->name,
- client->flags),
- G_VARIANT_TYPE ("(u)"),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- (GAsyncReadyCallback) request_name_cb,
- client_ref (client));
+ if (1)
+ {
+ GError *error = NULL;
+ guint32 request_name_reply;
+
+ request_name_reply = 0;
+
+ request_name_reply = g_dbus_request_name (client->connection, client->name, client->flags, &error);
+ g_assert_no_error (error);
+
+ process_request_name_reply (client, request_name_reply);
+ }
+ else
+ {
+ g_dbus_connection_call (client->connection,
+ "org.freedesktop.DBus", /* bus name */
+ "/org/freedesktop/DBus", /* object path */
+ "org.freedesktop.DBus", /* interface name */
+ "RequestName", /* method name */
+ g_variant_new ("(su)",
+ client->name,
+ client->flags),
+ G_VARIANT_TYPE ("(u)"),
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ (GAsyncReadyCallback) request_name_cb,
+ client_ref (client));
+ }
}
*
* Starts acquiring @name on the bus specified by @bus_type and calls
* @name_acquired_handler and @name_lost_handler when the name is
- * acquired respectively lost. 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.
+ * acquired respectively lost. 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 @name_acquired_handler and @name_lost_handler
* callbacks will be invoked after calling this function - there are three
* before @name is requested from the bus.
*
* This behavior makes it very simple to write applications that wants
- * to own names and export objects, see <xref linkend="gdbus-owning-names"/>.
+ * to [own names][gdbus-owning-names] and export objects.
* Simply register objects to be exported in @bus_acquired_handler and
* unregister the objects (if any) in @name_lost_handler.
*
* I believe this is a bug in the bus daemon.
*/
error = NULL;
- result = g_dbus_connection_call_sync (client->connection,
- "org.freedesktop.DBus", /* bus name */
- "/org/freedesktop/DBus", /* object path */
- "org.freedesktop.DBus", /* interface name */
- "ReleaseName", /* method name */
- g_variant_new ("(s)", client->name),
- G_VARIANT_TYPE ("(u)"),
- G_DBUS_CALL_FLAGS_NONE,
- -1,
- NULL,
- &error);
+ if (1)
+ {
+ g_dbus_release_name (client->connection, client->name, &error);
+ g_assert_no_error (error);
+
+ result = g_variant_ref_sink (g_variant_new_uint32 (1));
+ }
+ else
+ result = g_dbus_connection_call_sync (client->connection,
+ "org.freedesktop.DBus", /* bus name */
+ "/org/freedesktop/DBus", /* object path */
+ "org.freedesktop.DBus", /* interface name */
+ "ReleaseName", /* method name */
+ g_variant_new ("(s)", client->name),
+ G_VARIANT_TYPE ("(u)"),
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ &error);
if (result == NULL)
{
g_warning ("Error releasing name %s: %s", client->name, error->message);