regex: unicode: Update to Unicode 6.1.0
[glib.git] / gio / gdbusnameowning.c
blob207fa60b9061ddc36ba802a213adc612dcf67034
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
23 #include "config.h"
25 #include <stdlib.h>
27 #include "gdbusutils.h"
28 #include "gdbusnameowning.h"
29 #include "gdbuserror.h"
30 #include "gdbusprivate.h"
31 #include "gdbusconnection.h"
33 #include "glibintl.h"
35 /**
36 * SECTION:gdbusnameowning
37 * @title: Owning Bus Names
38 * @short_description: Simple API for owning bus names
39 * @include: gio/gio.h
41 * Convenience API for owning bus names.
43 * <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>
46 G_LOCK_DEFINE_STATIC (lock);
48 /* ---------------------------------------------------------------------------------------------------- */
50 typedef enum
52 PREVIOUS_CALL_NONE = 0,
53 PREVIOUS_CALL_ACQUIRED,
54 PREVIOUS_CALL_LOST,
55 } PreviousCall;
57 typedef struct
59 volatile gint ref_count;
60 guint id;
61 GBusNameOwnerFlags flags;
62 gchar *name;
63 GBusAcquiredCallback bus_acquired_handler;
64 GBusNameAcquiredCallback name_acquired_handler;
65 GBusNameLostCallback name_lost_handler;
66 gpointer user_data;
67 GDestroyNotify user_data_free_func;
68 GMainContext *main_context;
70 PreviousCall previous_call;
72 GDBusConnection *connection;
73 gulong disconnected_signal_handler_id;
74 guint name_acquired_subscription_id;
75 guint name_lost_subscription_id;
77 volatile gboolean cancelled; /* must hold lock when reading or modifying */
79 gboolean needs_release;
80 } Client;
82 static guint next_global_id = 1;
83 static GHashTable *map_id_to_client = NULL;
86 static Client *
87 client_ref (Client *client)
89 g_atomic_int_inc (&client->ref_count);
90 return client;
93 static void
94 client_unref (Client *client)
96 if (g_atomic_int_dec_and_test (&client->ref_count))
98 if (client->connection != NULL)
100 if (client->disconnected_signal_handler_id > 0)
101 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
102 if (client->name_acquired_subscription_id > 0)
103 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
104 if (client->name_lost_subscription_id > 0)
105 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
106 g_object_unref (client->connection);
108 g_main_context_unref (client->main_context);
109 g_free (client->name);
110 if (client->user_data_free_func != NULL)
111 client->user_data_free_func (client->user_data);
112 g_free (client);
116 /* ---------------------------------------------------------------------------------------------------- */
119 typedef enum
121 CALL_TYPE_NAME_ACQUIRED,
122 CALL_TYPE_NAME_LOST
123 } CallType;
125 typedef struct
127 Client *client;
129 /* keep this separate because client->connection may
130 * be set to NULL after scheduling the call
132 GDBusConnection *connection;
134 /* set to TRUE to call acquired */
135 CallType call_type;
136 } CallHandlerData;
138 static void
139 call_handler_data_free (CallHandlerData *data)
141 if (data->connection != NULL)
142 g_object_unref (data->connection);
143 client_unref (data->client);
144 g_free (data);
147 static void
148 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
150 switch (call_type)
152 case CALL_TYPE_NAME_ACQUIRED:
153 if (client->name_acquired_handler != NULL)
155 client->name_acquired_handler (connection,
156 client->name,
157 client->user_data);
159 break;
161 case CALL_TYPE_NAME_LOST:
162 if (client->name_lost_handler != NULL)
164 client->name_lost_handler (connection,
165 client->name,
166 client->user_data);
168 break;
170 default:
171 g_assert_not_reached ();
172 break;
176 static gboolean
177 call_in_idle_cb (gpointer _data)
179 CallHandlerData *data = _data;
180 actually_do_call (data->client, data->connection, data->call_type);
181 return FALSE;
184 static void
185 schedule_call_in_idle (Client *client, CallType call_type)
187 CallHandlerData *data;
188 GSource *idle_source;
190 data = g_new0 (CallHandlerData, 1);
191 data->client = client_ref (client);
192 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
193 data->call_type = call_type;
195 idle_source = g_idle_source_new ();
196 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
197 g_source_set_callback (idle_source,
198 call_in_idle_cb,
199 data,
200 (GDestroyNotify) call_handler_data_free);
201 g_source_attach (idle_source, client->main_context);
202 g_source_unref (idle_source);
205 static void
206 do_call (Client *client, CallType call_type)
208 GMainContext *current_context;
210 /* only schedule in idle if we're not in the right thread */
211 current_context = g_main_context_ref_thread_default ();
212 if (current_context != client->main_context)
213 schedule_call_in_idle (client, call_type);
214 else
215 actually_do_call (client, client->connection, call_type);
216 g_main_context_unref (current_context);
219 static void
220 call_acquired_handler (Client *client)
222 G_LOCK (lock);
223 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
225 client->previous_call = PREVIOUS_CALL_ACQUIRED;
226 if (!client->cancelled)
228 G_UNLOCK (lock);
229 do_call (client, CALL_TYPE_NAME_ACQUIRED);
230 goto out;
233 G_UNLOCK (lock);
234 out:
238 static void
239 call_lost_handler (Client *client)
241 G_LOCK (lock);
242 if (client->previous_call != PREVIOUS_CALL_LOST)
244 client->previous_call = PREVIOUS_CALL_LOST;
245 if (!client->cancelled)
247 G_UNLOCK (lock);
248 do_call (client, CALL_TYPE_NAME_LOST);
249 goto out;
252 G_UNLOCK (lock);
253 out:
257 /* ---------------------------------------------------------------------------------------------------- */
259 static void
260 on_name_lost_or_acquired (GDBusConnection *connection,
261 const gchar *sender_name,
262 const gchar *object_path,
263 const gchar *interface_name,
264 const gchar *signal_name,
265 GVariant *parameters,
266 gpointer user_data)
268 Client *client = user_data;
269 const gchar *name;
271 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
272 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
273 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
274 goto out;
276 if (g_strcmp0 (signal_name, "NameLost") == 0)
278 g_variant_get (parameters, "(&s)", &name);
279 if (g_strcmp0 (name, client->name) == 0)
281 call_lost_handler (client);
284 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
286 g_variant_get (parameters, "(&s)", &name);
287 if (g_strcmp0 (name, client->name) == 0)
289 call_acquired_handler (client);
292 out:
296 /* ---------------------------------------------------------------------------------------------------- */
298 static void
299 request_name_cb (GObject *source_object,
300 GAsyncResult *res,
301 gpointer user_data)
303 Client *client = user_data;
304 GVariant *result;
305 guint32 request_name_reply;
306 gboolean subscribe;
308 request_name_reply = 0;
309 result = NULL;
311 /* don't use client->connection - it may be NULL already */
312 result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
313 res,
314 NULL);
315 if (result != NULL)
317 g_variant_get (result, "(u)", &request_name_reply);
318 g_variant_unref (result);
321 subscribe = FALSE;
323 switch (request_name_reply)
325 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
326 /* We got the name - now listen for NameLost and NameAcquired */
327 call_acquired_handler (client);
328 subscribe = TRUE;
329 client->needs_release = TRUE;
330 break;
332 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
333 /* Waiting in line - listen for NameLost and NameAcquired */
334 call_lost_handler (client);
335 subscribe = TRUE;
336 client->needs_release = TRUE;
337 break;
339 default:
340 /* assume we couldn't get the name - explicit fallthrough */
341 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
342 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
343 /* Some other part of the process is already owning the name */
344 call_lost_handler (client);
345 break;
349 if (subscribe)
351 GDBusConnection *connection = NULL;
353 /* if cancelled, there is no point in subscribing to signals - if not, make sure
354 * we use a known good Connection object since it may be set to NULL at any point
355 * after being cancelled
357 G_LOCK (lock);
358 if (!client->cancelled)
359 connection = g_object_ref (client->connection);
360 G_UNLOCK (lock);
362 /* start listening to NameLost and NameAcquired messages */
363 if (connection != NULL)
365 client->name_lost_subscription_id =
366 g_dbus_connection_signal_subscribe (connection,
367 "org.freedesktop.DBus",
368 "org.freedesktop.DBus",
369 "NameLost",
370 "/org/freedesktop/DBus",
371 client->name,
372 G_DBUS_SIGNAL_FLAGS_NONE,
373 on_name_lost_or_acquired,
374 client,
375 NULL);
376 client->name_acquired_subscription_id =
377 g_dbus_connection_signal_subscribe (connection,
378 "org.freedesktop.DBus",
379 "org.freedesktop.DBus",
380 "NameAcquired",
381 "/org/freedesktop/DBus",
382 client->name,
383 G_DBUS_SIGNAL_FLAGS_NONE,
384 on_name_lost_or_acquired,
385 client,
386 NULL);
387 g_object_unref (connection);
391 client_unref (client);
394 /* ---------------------------------------------------------------------------------------------------- */
396 static void
397 on_connection_disconnected (GDBusConnection *connection,
398 gboolean remote_peer_vanished,
399 GError *error,
400 gpointer user_data)
402 Client *client = user_data;
404 if (client->disconnected_signal_handler_id > 0)
405 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
406 if (client->name_acquired_subscription_id > 0)
407 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
408 if (client->name_lost_subscription_id > 0)
409 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
410 g_object_unref (client->connection);
411 client->disconnected_signal_handler_id = 0;
412 client->name_acquired_subscription_id = 0;
413 client->name_lost_subscription_id = 0;
414 client->connection = NULL;
416 call_lost_handler (client);
419 /* ---------------------------------------------------------------------------------------------------- */
421 static void
422 has_connection (Client *client)
424 /* listen for disconnection */
425 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
426 "closed",
427 G_CALLBACK (on_connection_disconnected),
428 client);
430 /* attempt to acquire the name */
431 g_dbus_connection_call (client->connection,
432 "org.freedesktop.DBus", /* bus name */
433 "/org/freedesktop/DBus", /* object path */
434 "org.freedesktop.DBus", /* interface name */
435 "RequestName", /* method name */
436 g_variant_new ("(su)",
437 client->name,
438 client->flags),
439 G_VARIANT_TYPE ("(u)"),
440 G_DBUS_CALL_FLAGS_NONE,
442 NULL,
443 (GAsyncReadyCallback) request_name_cb,
444 client_ref (client));
448 static void
449 connection_get_cb (GObject *source_object,
450 GAsyncResult *res,
451 gpointer user_data)
453 Client *client = user_data;
455 /* must not do anything if already cancelled */
456 G_LOCK (lock);
457 if (client->cancelled)
459 G_UNLOCK (lock);
460 goto out;
462 G_UNLOCK (lock);
464 client->connection = g_bus_get_finish (res, NULL);
465 if (client->connection == NULL)
467 call_lost_handler (client);
468 goto out;
471 /* No need to schedule this in idle as we're already in the thread
472 * that the user called g_bus_own_name() from. This is because
473 * g_bus_get() guarantees that.
475 * Also, we need to ensure that the handler is invoked *before*
476 * we call RequestName(). Otherwise there is a race.
478 if (client->bus_acquired_handler != NULL)
480 client->bus_acquired_handler (client->connection,
481 client->name,
482 client->user_data);
485 has_connection (client);
487 out:
488 client_unref (client);
491 /* ---------------------------------------------------------------------------------------------------- */
494 * g_bus_own_name_on_connection:
495 * @connection: A #GDBusConnection.
496 * @name: The well-known name to own.
497 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
498 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
499 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
500 * @user_data: User data to pass to handlers.
501 * @user_data_free_func: Function for freeing @user_data or %NULL.
503 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
504 * #GBusType.
506 * Returns: An identifier (never 0) that an be used with
507 * g_bus_unown_name() to stop owning the name.
509 * Since: 2.26
511 guint
512 g_bus_own_name_on_connection (GDBusConnection *connection,
513 const gchar *name,
514 GBusNameOwnerFlags flags,
515 GBusNameAcquiredCallback name_acquired_handler,
516 GBusNameLostCallback name_lost_handler,
517 gpointer user_data,
518 GDestroyNotify user_data_free_func)
520 Client *client;
522 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
523 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
525 G_LOCK (lock);
527 client = g_new0 (Client, 1);
528 client->ref_count = 1;
529 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
530 client->name = g_strdup (name);
531 client->flags = flags;
532 client->name_acquired_handler = name_acquired_handler;
533 client->name_lost_handler = name_lost_handler;
534 client->user_data = user_data;
535 client->user_data_free_func = user_data_free_func;
536 client->main_context = g_main_context_ref_thread_default ();
538 client->connection = g_object_ref (connection);
540 if (map_id_to_client == NULL)
542 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
544 g_hash_table_insert (map_id_to_client,
545 GUINT_TO_POINTER (client->id),
546 client);
548 G_UNLOCK (lock);
550 has_connection (client);
552 return client->id;
556 * g_bus_own_name:
557 * @bus_type: The type of bus to own a name on.
558 * @name: The well-known name to own.
559 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
560 * @bus_acquired_handler: Handler to invoke when connected to the bus of type @bus_type or %NULL.
561 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
562 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
563 * @user_data: User data to pass to handlers.
564 * @user_data_free_func: Function for freeing @user_data or %NULL.
566 * Starts acquiring @name on the bus specified by @bus_type and calls
567 * @name_acquired_handler and @name_lost_handler when the name is
568 * acquired respectively lost. Callbacks will be invoked in the <link
569 * linkend="g-main-context-push-thread-default">thread-default main
570 * loop</link> of the thread you are calling this function from.
572 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
573 * callbacks will be invoked after calling this function - there are three
574 * possible cases:
575 * <itemizedlist>
576 * <listitem><para>
577 * @name_lost_handler with a %NULL connection (if a connection to the bus can't be made).
578 * </para></listitem>
579 * <listitem><para>
580 * @bus_acquired_handler then @name_lost_handler (if the name can't be obtained)
581 * </para></listitem>
582 * <listitem><para>
583 * @bus_acquired_handler then @name_acquired_handler (if the name was obtained).
584 * </para></listitem>
585 * </itemizedlist>
586 * When you are done owning the name, just call g_bus_unown_name()
587 * with the owner id this function returns.
589 * If the name is acquired or lost (for example another application
590 * could acquire the name if you allow replacement or the application
591 * currently owning the name exits), the handlers are also invoked. If the
592 * #GDBusConnection that is used for attempting to own the name
593 * closes, then @name_lost_handler is invoked since it is no
594 * longer possible for other processes to access the process.
596 * You cannot use g_bus_own_name() several times for the same name (unless
597 * interleaved with calls to g_bus_unown_name()) - only the first call
598 * will work.
600 * Another guarantee is that invocations of @name_acquired_handler
601 * and @name_lost_handler are guaranteed to alternate; that
602 * is, if @name_acquired_handler is invoked then you are
603 * guaranteed that the next time one of the handlers is invoked, it
604 * will be @name_lost_handler. The reverse is also true.
606 * If you plan on exporting objects (using e.g.
607 * g_dbus_connection_register_object()), note that it is generally too late
608 * to export the objects in @name_acquired_handler. Instead, you can do this
609 * in @bus_acquired_handler since you are guaranteed that this will run
610 * before @name is requested from the bus.
612 * This behavior makes it very simple to write applications that wants
613 * to own names and export objects, see <xref linkend="gdbus-owning-names"/>.
614 * Simply register objects to be exported in @bus_acquired_handler and
615 * unregister the objects (if any) in @name_lost_handler.
617 * Returns: An identifier (never 0) that an be used with
618 * g_bus_unown_name() to stop owning the name.
620 * Since: 2.26
622 guint
623 g_bus_own_name (GBusType bus_type,
624 const gchar *name,
625 GBusNameOwnerFlags flags,
626 GBusAcquiredCallback bus_acquired_handler,
627 GBusNameAcquiredCallback name_acquired_handler,
628 GBusNameLostCallback name_lost_handler,
629 gpointer user_data,
630 GDestroyNotify user_data_free_func)
632 Client *client;
634 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
636 G_LOCK (lock);
638 client = g_new0 (Client, 1);
639 client->ref_count = 1;
640 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
641 client->name = g_strdup (name);
642 client->flags = flags;
643 client->bus_acquired_handler = bus_acquired_handler;
644 client->name_acquired_handler = name_acquired_handler;
645 client->name_lost_handler = name_lost_handler;
646 client->user_data = user_data;
647 client->user_data_free_func = user_data_free_func;
648 client->main_context = g_main_context_ref_thread_default ();
650 if (map_id_to_client == NULL)
652 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
654 g_hash_table_insert (map_id_to_client,
655 GUINT_TO_POINTER (client->id),
656 client);
658 g_bus_get (bus_type,
659 NULL,
660 connection_get_cb,
661 client_ref (client));
663 G_UNLOCK (lock);
665 return client->id;
668 typedef struct {
669 GClosure *bus_acquired_closure;
670 GClosure *name_acquired_closure;
671 GClosure *name_lost_closure;
672 } OwnNameData;
674 static OwnNameData *
675 own_name_data_new (GClosure *bus_acquired_closure,
676 GClosure *name_acquired_closure,
677 GClosure *name_lost_closure)
679 OwnNameData *data;
681 data = g_new0 (OwnNameData, 1);
683 if (bus_acquired_closure != NULL)
685 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
686 g_closure_sink (bus_acquired_closure);
687 if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
688 g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
691 if (name_acquired_closure != NULL)
693 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
694 g_closure_sink (name_acquired_closure);
695 if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
696 g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
699 if (name_lost_closure != NULL)
701 data->name_lost_closure = g_closure_ref (name_lost_closure);
702 g_closure_sink (name_lost_closure);
703 if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
704 g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
707 return data;
710 static void
711 own_with_closures_on_bus_acquired (GDBusConnection *connection,
712 const gchar *name,
713 gpointer user_data)
715 OwnNameData *data = user_data;
716 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
718 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
719 g_value_set_object (&params[0], connection);
721 g_value_init (&params[1], G_TYPE_STRING);
722 g_value_set_string (&params[1], name);
724 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
726 g_value_unset (params + 0);
727 g_value_unset (params + 1);
730 static void
731 own_with_closures_on_name_acquired (GDBusConnection *connection,
732 const gchar *name,
733 gpointer user_data)
735 OwnNameData *data = user_data;
736 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
738 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
739 g_value_set_object (&params[0], connection);
741 g_value_init (&params[1], G_TYPE_STRING);
742 g_value_set_string (&params[1], name);
744 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
746 g_value_unset (params + 0);
747 g_value_unset (params + 1);
750 static void
751 own_with_closures_on_name_lost (GDBusConnection *connection,
752 const gchar *name,
753 gpointer user_data)
755 OwnNameData *data = user_data;
756 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
758 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
759 g_value_set_object (&params[0], connection);
761 g_value_init (&params[1], G_TYPE_STRING);
762 g_value_set_string (&params[1], name);
764 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
766 g_value_unset (params + 0);
767 g_value_unset (params + 1);
770 static void
771 bus_own_name_free_func (gpointer user_data)
773 OwnNameData *data = user_data;
775 if (data->bus_acquired_closure != NULL)
776 g_closure_unref (data->bus_acquired_closure);
778 if (data->name_acquired_closure != NULL)
779 g_closure_unref (data->name_acquired_closure);
781 if (data->name_lost_closure != NULL)
782 g_closure_unref (data->name_lost_closure);
784 g_free (data);
788 * g_bus_own_name_with_closures:
789 * @bus_type: The type of bus to own a name on.
790 * @name: The well-known name to own.
791 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
792 * @bus_acquired_closure: (allow-none): #GClosure to invoke when connected to
793 * the bus of type @bus_type or %NULL.
794 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
795 * acquired or %NULL.
796 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
797 * %NULL.
799 * Version of g_bus_own_name() using closures instead of callbacks for
800 * easier binding in other languages.
802 * Returns: An identifier (never 0) that an be used with
803 * g_bus_unown_name() to stop owning the name.
805 * Rename to: g_bus_own_name
807 * Since: 2.26
809 guint
810 g_bus_own_name_with_closures (GBusType bus_type,
811 const gchar *name,
812 GBusNameOwnerFlags flags,
813 GClosure *bus_acquired_closure,
814 GClosure *name_acquired_closure,
815 GClosure *name_lost_closure)
817 return g_bus_own_name (bus_type,
818 name,
819 flags,
820 bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
821 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
822 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
823 own_name_data_new (bus_acquired_closure,
824 name_acquired_closure,
825 name_lost_closure),
826 bus_own_name_free_func);
830 * g_bus_own_name_on_connection_with_closures:
831 * @connection: A #GDBusConnection.
832 * @name: The well-known name to own.
833 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
834 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
835 * acquired or %NULL.
836 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
837 * %NULL.
839 * Version of g_bus_own_name_on_connection() using closures instead of callbacks for
840 * easier binding in other languages.
842 * Returns: An identifier (never 0) that an be used with
843 * g_bus_unown_name() to stop owning the name.
845 * Rename to: g_bus_own_name_on_connection
847 * Since: 2.26
849 guint
850 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
851 const gchar *name,
852 GBusNameOwnerFlags flags,
853 GClosure *name_acquired_closure,
854 GClosure *name_lost_closure)
856 return g_bus_own_name_on_connection (connection,
857 name,
858 flags,
859 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
860 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
861 own_name_data_new (NULL,
862 name_acquired_closure,
863 name_lost_closure),
864 bus_own_name_free_func);
868 * g_bus_unown_name:
869 * @owner_id: An identifier obtained from g_bus_own_name()
871 * Stops owning a name.
873 * Since: 2.26
875 void
876 g_bus_unown_name (guint owner_id)
878 Client *client;
880 g_return_if_fail (owner_id > 0);
882 client = NULL;
884 G_LOCK (lock);
885 if (owner_id == 0 || map_id_to_client == NULL ||
886 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
888 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
889 goto out;
892 client->cancelled = TRUE;
893 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
895 out:
896 G_UNLOCK (lock);
898 /* do callback without holding lock */
899 if (client != NULL)
901 /* Release the name if needed */
902 if (client->needs_release && client->connection != NULL)
904 GVariant *result;
905 GError *error;
906 guint32 release_name_reply;
908 /* TODO: it kinda sucks having to do a sync call to release the name - but if
909 * we don't, then a subsequent grab of the name will make the bus daemon return
910 * IN_QUEUE which will trigger name_lost().
912 * I believe this is a bug in the bus daemon.
914 error = NULL;
915 result = g_dbus_connection_call_sync (client->connection,
916 "org.freedesktop.DBus", /* bus name */
917 "/org/freedesktop/DBus", /* object path */
918 "org.freedesktop.DBus", /* interface name */
919 "ReleaseName", /* method name */
920 g_variant_new ("(s)", client->name),
921 G_VARIANT_TYPE ("(u)"),
922 G_DBUS_CALL_FLAGS_NONE,
924 NULL,
925 &error);
926 if (result == NULL)
928 g_warning ("Error releasing name %s: %s", client->name, error->message);
929 g_error_free (error);
931 else
933 g_variant_get (result, "(u)", &release_name_reply);
934 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
936 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
938 g_variant_unref (result);
942 if (client->disconnected_signal_handler_id > 0)
943 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
944 if (client->name_acquired_subscription_id > 0)
945 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
946 if (client->name_lost_subscription_id > 0)
947 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
948 client->disconnected_signal_handler_id = 0;
949 client->name_acquired_subscription_id = 0;
950 client->name_lost_subscription_id = 0;
951 if (client->connection != NULL)
953 g_object_unref (client->connection);
954 client->connection = NULL;
957 client_unref (client);