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>
28 #include "gdbusutils.h"
29 #include "gdbusproxy.h"
30 #include "gioenumtypes.h"
31 #include "gdbusconnection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
34 #include "gio-marshal.h"
35 #include "ginitable.h"
36 #include "gasyncinitable.h"
38 #include "gasyncresult.h"
39 #include "gsimpleasyncresult.h"
40 #include "gcancellable.h"
47 * @short_description: Client-side proxies
50 * #GDBusProxy is a base class used for proxies to access a D-Bus
51 * interface on a remote object. A #GDBusProxy can be constructed for
52 * both well-known and unique names.
54 * By default, #GDBusProxy will cache all properties (and listen to
55 * changes) of the remote object, and proxy all signals that gets
56 * emitted. This behaviour can be changed by passing suitable
57 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
58 * well-known name, the property cache is flushed when the name owner
59 * vanishes and reloaded when a name owner appears.
61 * If a #GDBusProxy is used for a well-known name, the owner of the
62 * name is tracked and can be read from
63 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
64 * get notified of changes. Additionally, only signals and property
65 * changes emitted from the current name owner are considered. This
66 * avoids a number of race conditions when the name is lost by one
67 * owner and claimed by another.
69 * The generic #GDBusProxy::g-properties-changed and #GDBusProxy::g-signal
70 * signals are not very convenient to work with. Therefore, the recommended
71 * way of working with proxies is to subclass #GDBusProxy, and have
72 * more natural properties and signals in your derived class.
74 * See <xref linkend="gdbus-example-proxy-subclass"/> for an example.
76 * <example id="gdbus-wellknown-proxy"><title>GDBusProxy for a well-known-name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-proxy.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
79 struct _GDBusProxyPrivate
82 GDBusConnection *connection;
84 GDBusProxyFlags flags;
88 gchar *interface_name;
91 guint name_owner_changed_subscription_id;
93 GCancellable *get_all_cancellable;
95 /* gchar* -> GVariant* */
96 GHashTable *properties;
98 GDBusInterfaceInfo *expected_interface;
100 guint properties_changed_subscriber_id;
101 guint signals_subscriber_id;
103 gboolean initialized;
115 PROP_G_INTERFACE_NAME,
116 PROP_G_DEFAULT_TIMEOUT,
117 PROP_G_INTERFACE_INFO
122 PROPERTIES_CHANGED_SIGNAL,
127 guint signals[LAST_SIGNAL] = {0};
129 static void initable_iface_init (GInitableIface *initable_iface);
130 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
132 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
133 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
134 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
138 g_dbus_proxy_finalize (GObject *object)
140 GDBusProxy *proxy = G_DBUS_PROXY (object);
142 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
144 if (proxy->priv->name_owner_changed_subscription_id > 0)
145 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
146 proxy->priv->name_owner_changed_subscription_id);
148 if (proxy->priv->properties_changed_subscriber_id > 0)
149 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
150 proxy->priv->properties_changed_subscriber_id);
152 if (proxy->priv->signals_subscriber_id > 0)
153 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
154 proxy->priv->signals_subscriber_id);
156 g_object_unref (proxy->priv->connection);
157 g_free (proxy->priv->name);
158 g_free (proxy->priv->name_owner);
159 g_free (proxy->priv->object_path);
160 g_free (proxy->priv->interface_name);
161 if (proxy->priv->properties != NULL)
162 g_hash_table_unref (proxy->priv->properties);
164 if (proxy->priv->expected_interface != NULL)
165 g_dbus_interface_info_unref (proxy->priv->expected_interface);
167 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
171 g_dbus_proxy_get_property (GObject *object,
176 GDBusProxy *proxy = G_DBUS_PROXY (object);
180 case PROP_G_CONNECTION:
181 g_value_set_object (value, proxy->priv->connection);
185 g_value_set_flags (value, proxy->priv->flags);
189 g_value_set_string (value, proxy->priv->name);
192 case PROP_G_NAME_OWNER:
193 g_value_set_string (value, proxy->priv->name_owner);
196 case PROP_G_OBJECT_PATH:
197 g_value_set_string (value, proxy->priv->object_path);
200 case PROP_G_INTERFACE_NAME:
201 g_value_set_string (value, proxy->priv->interface_name);
204 case PROP_G_DEFAULT_TIMEOUT:
205 g_value_set_int (value, proxy->priv->timeout_msec);
208 case PROP_G_INTERFACE_INFO:
209 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 g_dbus_proxy_set_property (GObject *object,
224 GDBusProxy *proxy = G_DBUS_PROXY (object);
228 case PROP_G_CONNECTION:
229 proxy->priv->connection = g_value_dup_object (value);
233 proxy->priv->flags = g_value_get_flags (value);
237 proxy->priv->name = g_value_dup_string (value);
240 case PROP_G_OBJECT_PATH:
241 proxy->priv->object_path = g_value_dup_string (value);
244 case PROP_G_INTERFACE_NAME:
245 proxy->priv->interface_name = g_value_dup_string (value);
248 case PROP_G_DEFAULT_TIMEOUT:
249 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
252 case PROP_G_INTERFACE_INFO:
253 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
256 case PROP_G_BUS_TYPE:
257 proxy->priv->bus_type = g_value_get_enum (value);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 g_dbus_proxy_class_init (GDBusProxyClass *klass)
269 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
271 gobject_class->finalize = g_dbus_proxy_finalize;
272 gobject_class->set_property = g_dbus_proxy_set_property;
273 gobject_class->get_property = g_dbus_proxy_get_property;
275 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
276 * in derived classes */
279 * GDBusProxy:g-interface-info:
281 * Ensure that interactions with this proxy conform to the given
282 * interface. For example, when completing a method call, if the
283 * type signature of the message isn't what's expected, the given
284 * #GError is set. Signals that have a type signature mismatch are
289 g_object_class_install_property (gobject_class,
290 PROP_G_INTERFACE_INFO,
291 g_param_spec_boxed ("g-interface-info",
292 P_("Interface Information"),
293 P_("Interface Information"),
294 G_TYPE_DBUS_INTERFACE_INFO,
297 G_PARAM_STATIC_NAME |
298 G_PARAM_STATIC_BLURB |
299 G_PARAM_STATIC_NICK));
302 * GDBusProxy:g-connection:
304 * The #GDBusConnection the proxy is for.
308 g_object_class_install_property (gobject_class,
310 g_param_spec_object ("g-connection",
312 P_("The connection the proxy is for"),
313 G_TYPE_DBUS_CONNECTION,
316 G_PARAM_CONSTRUCT_ONLY |
317 G_PARAM_STATIC_NAME |
318 G_PARAM_STATIC_BLURB |
319 G_PARAM_STATIC_NICK));
322 * GDBusProxy:g-bus-type:
324 * If this property is not %G_BUS_TYPE_NONE, then
325 * #GDBusProxy:g-connection must be %NULL and will be set to the
326 * #GDBusConnection obtained by calling g_bus_get() with the value
331 g_object_class_install_property (gobject_class,
333 g_param_spec_enum ("g-bus-type",
335 P_("The bus to connect to, if any"),
339 G_PARAM_CONSTRUCT_ONLY |
340 G_PARAM_STATIC_NAME |
341 G_PARAM_STATIC_BLURB |
342 G_PARAM_STATIC_NICK));
345 * GDBusProxy:g-flags:
347 * Flags from the #GDBusProxyFlags enumeration.
351 g_object_class_install_property (gobject_class,
353 g_param_spec_flags ("g-flags",
355 P_("Flags for the proxy"),
356 G_TYPE_DBUS_PROXY_FLAGS,
357 G_DBUS_PROXY_FLAGS_NONE,
360 G_PARAM_CONSTRUCT_ONLY |
361 G_PARAM_STATIC_NAME |
362 G_PARAM_STATIC_BLURB |
363 G_PARAM_STATIC_NICK));
368 * The well-known or unique name that the proxy is for.
372 g_object_class_install_property (gobject_class,
374 g_param_spec_string ("g-name",
376 P_("The well-known or unique name that the proxy is for"),
380 G_PARAM_CONSTRUCT_ONLY |
381 G_PARAM_STATIC_NAME |
382 G_PARAM_STATIC_BLURB |
383 G_PARAM_STATIC_NICK));
386 * GDBusProxy:g-name-owner:
388 * The unique name that owns #GDBusProxy:name or %NULL if no-one
389 * currently owns that name. You may connect to #GObject::notify signal to
390 * track changes to this property.
394 g_object_class_install_property (gobject_class,
396 g_param_spec_string ("g-name-owner",
398 P_("The unique name for the owner"),
401 G_PARAM_STATIC_NAME |
402 G_PARAM_STATIC_BLURB |
403 G_PARAM_STATIC_NICK));
406 * GDBusProxy:g-object-path:
408 * The object path the proxy is for.
412 g_object_class_install_property (gobject_class,
414 g_param_spec_string ("g-object-path",
416 P_("The object path the proxy is for"),
420 G_PARAM_CONSTRUCT_ONLY |
421 G_PARAM_STATIC_NAME |
422 G_PARAM_STATIC_BLURB |
423 G_PARAM_STATIC_NICK));
426 * GDBusProxy:g-interface-name:
428 * The D-Bus interface name the proxy is for.
432 g_object_class_install_property (gobject_class,
433 PROP_G_INTERFACE_NAME,
434 g_param_spec_string ("g-interface-name",
435 P_("g-interface-name"),
436 P_("The D-Bus interface name the proxy is for"),
440 G_PARAM_CONSTRUCT_ONLY |
441 G_PARAM_STATIC_NAME |
442 G_PARAM_STATIC_BLURB |
443 G_PARAM_STATIC_NICK));
446 * GDBusProxy:g-default-timeout:
448 * The timeout to use if -1 (specifying default timeout) is passed
449 * as @timeout_msec in the g_dbus_proxy_call() and
450 * g_dbus_proxy_call_sync() functions.
452 * This allows applications to set a proxy-wide timeout for all
453 * remote method invocations on the proxy. If this property is -1,
454 * the default timeout (typically 25 seconds) is used. If set to
455 * %G_MAXINT, then no timeout is used.
459 g_object_class_install_property (gobject_class,
460 PROP_G_DEFAULT_TIMEOUT,
461 g_param_spec_int ("g-default-timeout",
462 P_("Default Timeout"),
463 P_("Timeout for remote method invocation"),
470 G_PARAM_STATIC_NAME |
471 G_PARAM_STATIC_BLURB |
472 G_PARAM_STATIC_NICK));
475 * GDBusProxy::g-properties-changed:
476 * @proxy: The #GDBusProxy emitting the signal.
477 * @changed_properties: A #GVariant containing the properties that changed
478 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
480 * Emitted when one or more D-Bus properties on @proxy changes. The
481 * local cache has already been updated when this signal fires. Note
482 * that both @changed_properties and @invalidated_properties are
483 * guaranteed to never be %NULL (either may be empty though).
485 * This signal corresponds to the
486 * <literal>PropertiesChanged</literal> D-Bus signal on the
487 * <literal>org.freedesktop.DBus.Properties</literal> interface.
491 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
494 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
497 _gio_marshal_VOID__BOXED_BOXED,
501 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
504 * GDBusProxy::g-signal:
505 * @proxy: The #GDBusProxy emitting the signal.
506 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
507 * @signal_name: The name of the signal.
508 * @parameters: A #GVariant tuple with parameters for the signal.
510 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
514 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
517 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
520 _gio_marshal_VOID__STRING_STRING_BOXED,
528 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
532 g_dbus_proxy_init (GDBusProxy *proxy)
534 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
535 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
538 (GDestroyNotify) g_variant_unref);
541 /* ---------------------------------------------------------------------------------------------------- */
544 * g_dbus_proxy_get_cached_property_names:
545 * @proxy: A #GDBusProxy.
547 * Gets the names of all cached properties on @proxy.
549 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
550 * no cached properties. Free the returned array with g_strfreev().
555 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
562 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
565 if (g_hash_table_size (proxy->priv->properties) == 0)
568 p = g_ptr_array_new ();
570 g_hash_table_iter_init (&iter, proxy->priv->properties);
571 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
572 g_ptr_array_add (p, g_strdup (key));
573 g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
574 g_ptr_array_add (p, NULL);
576 names = (gchar **) g_ptr_array_free (p, FALSE);
582 static const GDBusPropertyInfo *
583 lookup_property_info_or_warn (GDBusProxy *proxy,
584 const gchar *property_name)
586 const GDBusPropertyInfo *info;
588 if (proxy->priv->expected_interface == NULL)
591 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
594 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
596 proxy->priv->expected_interface->name);
603 * g_dbus_proxy_get_cached_property:
604 * @proxy: A #GDBusProxy.
605 * @property_name: Property name.
607 * Looks up the value for a property from the cache. This call does no
610 * If @proxy has an expected interface (see
611 * #GDBusProxy:g-interface-info), then @property_name (for existence)
612 * is checked against it.
614 * Returns: A reference to the #GVariant instance that holds the value
615 * for @property_name or %NULL if the value is not in the cache. The
616 * returned reference must be freed with g_variant_unref().
621 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
622 const gchar *property_name)
626 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
627 g_return_val_if_fail (property_name != NULL, NULL);
629 value = g_hash_table_lookup (proxy->priv->properties, property_name);
632 const GDBusPropertyInfo *info;
633 info = lookup_property_info_or_warn (proxy, property_name);
638 g_variant_ref (value);
645 * g_dbus_proxy_set_cached_property:
646 * @proxy: A #GDBusProxy
647 * @property_name: Property name.
648 * @value: Value for the property or %NULL to remove it from the cache.
650 * If @value is not %NULL, sets the cached value for the property with
651 * name @property_name to the value in @value.
653 * If @value is %NULL, then the cached value is removed from the
656 * If @proxy has an expected interface (see
657 * #GDBusProxy:g-interface-info), then @property_name (for existence)
658 * and @value (for the type) is checked against it.
660 * If the @value #GVariant is floating, it is consumed. This allows
661 * convenient 'inline' use of g_variant_new(), e.g.
663 * g_dbus_proxy_set_cached_property (proxy,
665 * g_variant_new ("(si)",
670 * Normally you will not need to use this method since @proxy is
671 * tracking changes using the
672 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
673 * D-Bus signal. However, for performance reasons an object may decide
674 * to not use this signal for some properties and instead use a
675 * proprietary out-of-band mechanism to transmit changes.
677 * As a concrete example, consider an object with a property
678 * <literal>ChatroomParticipants</literal> which is an array of
679 * strings. Instead of transmitting the same (long) array every time
680 * the property changes, it is more efficient to only transmit the
681 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
682 * name)</literal> and <literal>ChatroomParticipantParted(String
688 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
689 const gchar *property_name,
692 const GDBusPropertyInfo *info;
694 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
695 g_return_if_fail (property_name != NULL);
699 info = lookup_property_info_or_warn (proxy, property_name);
702 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
704 g_warning (_("Trying to set property %s of type %s but according to the expected "
705 "interface the type is %s"),
707 g_variant_get_type_string (value),
712 g_hash_table_insert (proxy->priv->properties,
713 g_strdup (property_name),
714 g_variant_ref_sink (value));
718 g_hash_table_remove (proxy->priv->properties, property_name);
725 /* ---------------------------------------------------------------------------------------------------- */
728 on_signal_received (GDBusConnection *connection,
729 const gchar *sender_name,
730 const gchar *object_path,
731 const gchar *interface_name,
732 const gchar *signal_name,
733 GVariant *parameters,
736 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
738 if (!proxy->priv->initialized)
741 if (g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
744 g_signal_emit (proxy,
745 signals[SIGNAL_SIGNAL],
754 /* ---------------------------------------------------------------------------------------------------- */
757 on_properties_changed (GDBusConnection *connection,
758 const gchar *sender_name,
759 const gchar *object_path,
760 const gchar *interface_name,
761 const gchar *signal_name,
762 GVariant *parameters,
765 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
767 const gchar *interface_name_for_signal;
768 GVariant *changed_properties;
769 gchar **invalidated_properties;
776 changed_properties = NULL;
777 invalidated_properties = NULL;
779 if (!proxy->priv->initialized)
782 if (g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
785 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
787 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
788 g_variant_get_type_string (parameters));
792 g_variant_get (parameters,
794 &interface_name_for_signal,
796 &invalidated_properties);
798 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
801 g_variant_iter_init (&iter, changed_properties);
802 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
804 g_hash_table_insert (proxy->priv->properties,
805 key, /* adopts string */
806 value); /* adopts value */
809 for (n = 0; invalidated_properties[n] != NULL; n++)
811 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
815 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
818 invalidated_properties);
821 if (changed_properties != NULL)
822 g_variant_unref (changed_properties);
823 g_free (invalidated_properties);
826 /* ---------------------------------------------------------------------------------------------------- */
829 process_get_all_reply (GDBusProxy *proxy,
836 if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
838 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
839 g_variant_get_type_string (result));
843 g_variant_get (result, "(a{sv})", &iter);
844 while (g_variant_iter_next (iter, "{sv}", &key, &value))
846 /*g_print ("got %s -> %s\n", key, g_variant_print (value, FALSE));*/
848 g_hash_table_insert (proxy->priv->properties,
849 key, /* adopts string */
850 value); /* adopts value */
852 g_variant_iter_free (iter);
861 GCancellable *cancellable;
863 } LoadPropertiesOnNameOwnerChangedData;
866 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
870 LoadPropertiesOnNameOwnerChangedData *data = user_data;
878 result = g_dbus_connection_call_finish (connection,
883 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
885 /* We just ignore if GetAll() is failing. Because this might happen
886 * if the object has no properties at all. Or if the caller is
887 * not authorized to see the properties.
889 * Either way, apps can know about this by using
890 * get_cached_property_names() or get_cached_property().
892 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
893 * fact that GetAll() failed
895 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
896 g_error_free (error);
899 /* and finally we can notify */
902 g_free (data->proxy->priv->name_owner);
903 data->proxy->priv->name_owner = data->name_owner;
904 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
906 g_hash_table_remove_all (data->proxy->priv->properties);
909 process_get_all_reply (data->proxy, result);
910 g_variant_unref (result);
913 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
916 if (data->cancellable == data->proxy->priv->get_all_cancellable)
917 data->proxy->priv->get_all_cancellable = NULL;
919 g_object_unref (data->proxy);
920 g_object_unref (data->cancellable);
921 g_free (data->name_owner);
926 on_name_owner_changed (GDBusConnection *connection,
927 const gchar *sender_name,
928 const gchar *object_path,
929 const gchar *interface_name,
930 const gchar *signal_name,
931 GVariant *parameters,
934 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
936 const gchar *old_owner;
937 const gchar *new_owner;
939 /* if we are already trying to load properties, cancel that */
940 if (proxy->priv->get_all_cancellable != NULL)
942 g_cancellable_cancel (proxy->priv->get_all_cancellable);
943 proxy->priv->get_all_cancellable = NULL;
946 g_variant_get (parameters,
952 /* we only care about a specific name */
953 if (g_strcmp0 (name, proxy->priv->name) != 0)
956 if (strlen (new_owner) == 0)
958 g_free (proxy->priv->name_owner);
959 proxy->priv->name_owner = NULL;
960 g_hash_table_remove_all (proxy->priv->properties);
961 g_object_notify (G_OBJECT (proxy), "g-name-owner");
965 /* ignore duplicates - this can happen when activating the service */
966 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
969 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
971 g_free (proxy->priv->name_owner);
972 proxy->priv->name_owner = g_strdup (new_owner);
973 g_hash_table_remove_all (proxy->priv->properties);
974 g_object_notify (G_OBJECT (proxy), "g-name-owner");
978 LoadPropertiesOnNameOwnerChangedData *data;
980 /* start loading properties.. only then emit notify::g-name-owner .. we
981 * need to be able to cancel this in the event another NameOwnerChanged
982 * signal suddenly happens
985 g_assert (proxy->priv->get_all_cancellable == NULL);
986 proxy->priv->get_all_cancellable = g_cancellable_new ();
987 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
988 data->proxy = g_object_ref (proxy);
989 data->cancellable = proxy->priv->get_all_cancellable;
990 data->name_owner = g_strdup (new_owner);
991 g_dbus_connection_call (proxy->priv->connection,
993 proxy->priv->object_path,
994 "org.freedesktop.DBus.Properties",
996 g_variant_new ("(s)", proxy->priv->interface_name),
997 G_VARIANT_TYPE ("(a{sv})"),
998 G_DBUS_CALL_FLAGS_NONE,
1000 proxy->priv->get_all_cancellable,
1001 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1010 /* ---------------------------------------------------------------------------------------------------- */
1015 GCancellable *cancellable;
1016 GSimpleAsyncResult *simple;
1020 async_init_data_free (AsyncInitData *data)
1022 g_object_unref (data->proxy);
1023 if (data->cancellable != NULL)
1024 g_object_unref (data->cancellable);
1025 g_object_unref (data->simple);
1030 async_init_get_all_cb (GDBusConnection *connection,
1034 AsyncInitData *data = user_data;
1039 result = g_dbus_connection_call_finish (connection,
1044 /* We just ignore if GetAll() is failing. Because this might happen
1045 * if the object has no properties at all. Or if the caller is
1046 * not authorized to see the properties.
1048 * Either way, apps can know about this by using
1049 * get_cached_property_names() or get_cached_property().
1051 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1052 * fact that GetAll() failed
1054 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1055 g_error_free (error);
1059 g_simple_async_result_set_op_res_gpointer (data->simple,
1061 (GDestroyNotify) g_variant_unref);
1064 g_simple_async_result_complete_in_idle (data->simple);
1065 async_init_data_free (data);
1070 async_init_get_name_owner_cb (GDBusConnection *connection,
1074 AsyncInitData *data = user_data;
1082 result = g_dbus_connection_call_finish (connection,
1087 if (error->domain == G_DBUS_ERROR &&
1088 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1090 g_error_free (error);
1094 g_simple_async_result_set_from_error (data->simple, error);
1095 g_error_free (error);
1096 g_simple_async_result_complete_in_idle (data->simple);
1097 async_init_data_free (data);
1103 g_variant_get (result,
1105 &data->proxy->priv->name_owner);
1106 g_variant_unref (result);
1110 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1112 /* load all properties asynchronously */
1113 g_dbus_connection_call (data->proxy->priv->connection,
1114 data->proxy->priv->name_owner,
1115 data->proxy->priv->object_path,
1116 "org.freedesktop.DBus.Properties",
1118 g_variant_new ("(s)", data->proxy->priv->interface_name),
1119 G_VARIANT_TYPE ("(a{sv})"),
1120 G_DBUS_CALL_FLAGS_NONE,
1123 (GAsyncReadyCallback) async_init_get_all_cb,
1128 g_simple_async_result_complete_in_idle (data->simple);
1129 async_init_data_free (data);
1137 async_init_call_get_name_owner (AsyncInitData *data)
1139 g_dbus_connection_call (data->proxy->priv->connection,
1140 "org.freedesktop.DBus", /* name */
1141 "/org/freedesktop/DBus", /* object path */
1142 "org.freedesktop.DBus", /* interface */
1144 g_variant_new ("(s)",
1145 data->proxy->priv->name),
1146 G_VARIANT_TYPE ("(s)"),
1147 G_DBUS_CALL_FLAGS_NONE,
1150 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1155 async_init_start_service_by_name_cb (GDBusConnection *connection,
1159 AsyncInitData *data = user_data;
1164 result = g_dbus_connection_call_finish (connection,
1169 /* Errors are not unexpected; the bus will reply e.g.
1171 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1172 * was not provided by any .service files
1174 * This doesn't mean that the name doesn't have an owner, just
1175 * that it's not provided by a .service file. So just proceed to
1176 * invoke GetNameOwner() if dealing with that error.
1178 if (error->domain == G_DBUS_ERROR &&
1179 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1181 g_error_free (error);
1185 g_prefix_error (&error,
1186 _("Error calling StartServiceByName for %s: "),
1187 data->proxy->priv->name);
1193 guint32 start_service_result;
1194 g_variant_get (result,
1196 &start_service_result);
1197 g_variant_unref (result);
1198 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1199 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1201 /* continue to invoke GetNameOwner() */
1205 error = g_error_new (G_IO_ERROR,
1207 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1208 start_service_result,
1209 data->proxy->priv->name);
1214 async_init_call_get_name_owner (data);
1218 g_warn_if_fail (error != NULL);
1219 g_simple_async_result_set_from_error (data->simple, error);
1220 g_error_free (error);
1221 g_simple_async_result_complete_in_idle (data->simple);
1222 async_init_data_free (data);
1226 async_init_call_start_service_by_name (AsyncInitData *data)
1228 g_dbus_connection_call (data->proxy->priv->connection,
1229 "org.freedesktop.DBus", /* name */
1230 "/org/freedesktop/DBus", /* object path */
1231 "org.freedesktop.DBus", /* interface */
1232 "StartServiceByName",
1233 g_variant_new ("(su)",
1234 data->proxy->priv->name,
1236 G_VARIANT_TYPE ("(u)"),
1237 G_DBUS_CALL_FLAGS_NONE,
1240 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1245 async_initable_init_second_async (GAsyncInitable *initable,
1247 GCancellable *cancellable,
1248 GAsyncReadyCallback callback,
1251 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1252 AsyncInitData *data;
1254 data = g_new0 (AsyncInitData, 1);
1255 data->proxy = g_object_ref (proxy);
1256 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1257 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1262 /* Check name ownership asynchronously - possibly also start the service */
1263 if (proxy->priv->name == NULL)
1266 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1268 else if (g_dbus_is_unique_name (proxy->priv->name))
1270 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1271 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1275 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1277 async_init_call_get_name_owner (data);
1281 async_init_call_start_service_by_name (data);
1287 async_initable_init_second_finish (GAsyncInitable *initable,
1291 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1292 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1298 if (g_simple_async_result_propagate_error (simple, error))
1301 result = g_simple_async_result_get_op_res_gpointer (simple);
1304 process_get_all_reply (proxy, result);
1310 proxy->priv->initialized = TRUE;
1314 /* ---------------------------------------------------------------------------------------------------- */
1317 async_initable_init_first (GAsyncInitable *initable)
1319 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1321 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1323 /* subscribe to PropertiesChanged() */
1324 proxy->priv->properties_changed_subscriber_id =
1325 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1327 "org.freedesktop.DBus.Properties",
1328 "PropertiesChanged",
1329 proxy->priv->object_path,
1330 proxy->priv->interface_name,
1331 on_properties_changed,
1336 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1338 /* subscribe to all signals for the object */
1339 proxy->priv->signals_subscriber_id =
1340 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1342 proxy->priv->interface_name,
1344 proxy->priv->object_path,
1351 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1353 proxy->priv->name_owner_changed_subscription_id =
1354 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1355 "org.freedesktop.DBus", /* name */
1356 "org.freedesktop.DBus", /* interface */
1357 "NameOwnerChanged", /* signal name */
1358 "/org/freedesktop/DBus", /* path */
1359 proxy->priv->name, /* arg0 */
1360 on_name_owner_changed,
1366 /* ---------------------------------------------------------------------------------------------------- */
1368 /* initialization is split into two parts - the first is the
1369 * non-blocing part that requires the callers GMainContext - the
1370 * second is a blocking part async part that doesn't require the
1371 * callers GMainContext.. we do this split so the code can be reused
1372 * in the GInitable implementation below.
1374 * Note that obtaining a GDBusConnection is not shared between the two
1382 GCancellable *cancellable;
1383 GAsyncReadyCallback callback;
1385 } GetConnectionData;
1388 get_connection_cb (GObject *source_object,
1392 GetConnectionData *data = user_data;
1396 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1397 if (data->proxy->priv->connection == NULL)
1399 GSimpleAsyncResult *simple;
1400 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1404 g_simple_async_result_set_from_error (simple, error);
1405 g_error_free (error);
1406 g_simple_async_result_complete_in_idle (simple);
1407 g_object_unref (simple);
1411 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1412 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1419 if (data->cancellable != NULL)
1420 g_object_unref (data->cancellable);
1425 async_initable_init_async (GAsyncInitable *initable,
1427 GCancellable *cancellable,
1428 GAsyncReadyCallback callback,
1431 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1433 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1435 GetConnectionData *data;
1437 g_assert (proxy->priv->connection == NULL);
1439 data = g_new0 (GetConnectionData, 1);
1440 data->proxy = proxy;
1441 data->io_priority = io_priority;
1442 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1443 data->callback = callback;
1444 data->user_data = user_data;
1445 g_bus_get (proxy->priv->bus_type,
1452 async_initable_init_first (initable);
1453 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1458 async_initable_init_finish (GAsyncInitable *initable,
1462 return async_initable_init_second_finish (initable, res, error);
1466 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1468 async_initable_iface->init_async = async_initable_init_async;
1469 async_initable_iface->init_finish = async_initable_init_finish;
1472 /* ---------------------------------------------------------------------------------------------------- */
1476 GMainContext *context;
1479 } InitableAsyncInitableData;
1482 async_initable_init_async_cb (GObject *source_object,
1486 InitableAsyncInitableData *data = user_data;
1487 data->res = g_object_ref (res);
1488 g_main_loop_quit (data->loop);
1491 /* Simply reuse the GAsyncInitable implementation but run the first
1492 * part (that is non-blocking and requires the callers GMainContext)
1493 * with the callers GMainContext.. and the second with a private
1494 * GMainContext (bug 621310 is slightly related).
1496 * Note that obtaining a GDBusConnection is not shared between the two
1500 initable_init (GInitable *initable,
1501 GCancellable *cancellable,
1504 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1505 InitableAsyncInitableData *data;
1510 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1512 g_assert (proxy->priv->connection == NULL);
1513 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1516 if (proxy->priv->connection == NULL)
1520 async_initable_init_first (G_ASYNC_INITABLE (initable));
1522 data = g_new0 (InitableAsyncInitableData, 1);
1523 data->context = g_main_context_new ();
1524 data->loop = g_main_loop_new (data->context, FALSE);
1526 g_main_context_push_thread_default (data->context);
1528 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1531 async_initable_init_async_cb,
1534 g_main_loop_run (data->loop);
1536 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1540 g_main_context_pop_thread_default (data->context);
1542 g_main_context_unref (data->context);
1543 g_main_loop_unref (data->loop);
1544 g_object_unref (data->res);
1553 initable_iface_init (GInitableIface *initable_iface)
1555 initable_iface->init = initable_init;
1558 /* ---------------------------------------------------------------------------------------------------- */
1562 * @connection: A #GDBusConnection.
1563 * @flags: Flags used when constructing the proxy.
1564 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1565 * @name: A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1566 * @object_path: An object path.
1567 * @interface_name: A D-Bus interface name.
1568 * @cancellable: A #GCancellable or %NULL.
1569 * @callback: Callback function to invoke when the proxy is ready.
1570 * @user_data: User data to pass to @callback.
1572 * Creates a proxy for accessing @interface_name on the remote object
1573 * at @object_path owned by @name at @connection and asynchronously
1574 * loads D-Bus properties unless the
1575 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1576 * the #GDBusProxy::g-properties-changed signal to get notified about
1579 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1580 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1581 * to handle signals from the remote object.
1583 * If @name is a well-known name and the
1584 * #G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1585 * owner currently exists, the message bus will be requested to launch
1586 * a name owner for the name.
1588 * This is a failable asynchronous constructor - when the proxy is
1589 * ready, @callback will be invoked and you can use
1590 * g_dbus_proxy_new_finish() to get the result.
1592 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1594 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1599 g_dbus_proxy_new (GDBusConnection *connection,
1600 GDBusProxyFlags flags,
1601 GDBusInterfaceInfo *info,
1603 const gchar *object_path,
1604 const gchar *interface_name,
1605 GCancellable *cancellable,
1606 GAsyncReadyCallback callback,
1609 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1610 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1611 g_return_if_fail (g_variant_is_object_path (object_path));
1612 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1614 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1620 "g-interface-info", info,
1622 "g-connection", connection,
1623 "g-object-path", object_path,
1624 "g-interface-name", interface_name,
1629 * g_dbus_proxy_new_finish:
1630 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1631 * @error: Return location for error or %NULL.
1633 * Finishes creating a #GDBusProxy.
1635 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1640 g_dbus_proxy_new_finish (GAsyncResult *res,
1644 GObject *source_object;
1646 source_object = g_async_result_get_source_object (res);
1647 g_assert (source_object != NULL);
1649 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1652 g_object_unref (source_object);
1655 return G_DBUS_PROXY (object);
1661 * g_dbus_proxy_new_sync:
1662 * @connection: A #GDBusConnection.
1663 * @flags: Flags used when constructing the proxy.
1664 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1665 * @name: A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1666 * @object_path: An object path.
1667 * @interface_name: A D-Bus interface name.
1668 * @cancellable: A #GCancellable or %NULL.
1669 * @error: Return location for error or %NULL.
1671 * Creates a proxy for accessing @interface_name on the remote object
1672 * at @object_path owned by @name at @connection and synchronously
1673 * loads D-Bus properties unless the
1674 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1676 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1677 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1678 * to handle signals from the remote object.
1680 * If @name is a well-known name and the
1681 * #G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1682 * owner currently exists, the message bus will be requested to launch
1683 * a name owner for the name.
1685 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1686 * and g_dbus_proxy_new_finish() for the asynchronous version.
1688 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1690 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1695 g_dbus_proxy_new_sync (GDBusConnection *connection,
1696 GDBusProxyFlags flags,
1697 GDBusInterfaceInfo *info,
1699 const gchar *object_path,
1700 const gchar *interface_name,
1701 GCancellable *cancellable,
1704 GInitable *initable;
1706 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1707 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1708 g_dbus_is_name (name), NULL);
1709 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1710 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1712 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1716 "g-interface-info", info,
1718 "g-connection", connection,
1719 "g-object-path", object_path,
1720 "g-interface-name", interface_name,
1722 if (initable != NULL)
1723 return G_DBUS_PROXY (initable);
1728 /* ---------------------------------------------------------------------------------------------------- */
1731 * g_dbus_proxy_new_for_bus:
1732 * @bus_type: A #GBusType.
1733 * @flags: Flags used when constructing the proxy.
1734 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1735 * @name: A bus name (well-known or unique).
1736 * @object_path: An object path.
1737 * @interface_name: A D-Bus interface name.
1738 * @cancellable: A #GCancellable or %NULL.
1739 * @callback: Callback function to invoke when the proxy is ready.
1740 * @user_data: User data to pass to @callback.
1742 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1744 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1749 g_dbus_proxy_new_for_bus (GBusType bus_type,
1750 GDBusProxyFlags flags,
1751 GDBusInterfaceInfo *info,
1753 const gchar *object_path,
1754 const gchar *interface_name,
1755 GCancellable *cancellable,
1756 GAsyncReadyCallback callback,
1759 g_return_if_fail (g_dbus_is_name (name));
1760 g_return_if_fail (g_variant_is_object_path (object_path));
1761 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1763 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1769 "g-interface-info", info,
1771 "g-bus-type", bus_type,
1772 "g-object-path", object_path,
1773 "g-interface-name", interface_name,
1778 * g_dbus_proxy_new_for_bus_finish:
1779 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1780 * @error: Return location for error or %NULL.
1782 * Finishes creating a #GDBusProxy.
1784 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1789 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1792 return g_dbus_proxy_new_finish (res, error);
1796 * g_dbus_proxy_new_for_bus_sync:
1797 * @bus_type: A #GBusType.
1798 * @flags: Flags used when constructing the proxy.
1799 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1800 * @name: A bus name (well-known or unique).
1801 * @object_path: An object path.
1802 * @interface_name: A D-Bus interface name.
1803 * @cancellable: A #GCancellable or %NULL.
1804 * @error: Return location for error or %NULL.
1806 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1808 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1810 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1815 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1816 GDBusProxyFlags flags,
1817 GDBusInterfaceInfo *info,
1819 const gchar *object_path,
1820 const gchar *interface_name,
1821 GCancellable *cancellable,
1824 GInitable *initable;
1826 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1827 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1828 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1830 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1834 "g-interface-info", info,
1836 "g-bus-type", bus_type,
1837 "g-object-path", object_path,
1838 "g-interface-name", interface_name,
1840 if (initable != NULL)
1841 return G_DBUS_PROXY (initable);
1846 /* ---------------------------------------------------------------------------------------------------- */
1849 * g_dbus_proxy_get_connection:
1850 * @proxy: A #GDBusProxy.
1852 * Gets the connection @proxy is for.
1854 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1859 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1861 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1862 return proxy->priv->connection;
1866 * g_dbus_proxy_get_flags:
1867 * @proxy: A #GDBusProxy.
1869 * Gets the flags that @proxy was constructed with.
1871 * Returns: Flags from the #GDBusProxyFlags enumeration.
1876 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1878 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1879 return proxy->priv->flags;
1883 * g_dbus_proxy_get_name:
1884 * @proxy: A #GDBusProxy.
1886 * Gets the name that @proxy was constructed for.
1888 * Returns: A string owned by @proxy. Do not free.
1893 g_dbus_proxy_get_name (GDBusProxy *proxy)
1895 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1896 return proxy->priv->name;
1900 * g_dbus_proxy_get_name_owner:
1901 * @proxy: A #GDBusProxy.
1903 * The unique name that owns the name that @proxy is for or %NULL if
1904 * no-one currently owns that name. You may connect to the
1905 * #GObject::notify signal to track changes to the
1906 * #GDBusProxy:g-name-owner property.
1908 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
1913 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
1915 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1916 return g_strdup (proxy->priv->name_owner);
1920 * g_dbus_proxy_get_object_path:
1921 * @proxy: A #GDBusProxy.
1923 * Gets the object path @proxy is for.
1925 * Returns: A string owned by @proxy. Do not free.
1930 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1932 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1933 return proxy->priv->object_path;
1937 * g_dbus_proxy_get_interface_name:
1938 * @proxy: A #GDBusProxy.
1940 * Gets the D-Bus interface name @proxy is for.
1942 * Returns: A string owned by @proxy. Do not free.
1947 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1949 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1950 return proxy->priv->interface_name;
1954 * g_dbus_proxy_get_default_timeout:
1955 * @proxy: A #GDBusProxy.
1957 * Gets the timeout to use if -1 (specifying default timeout) is
1958 * passed as @timeout_msec in the g_dbus_proxy_call() and
1959 * g_dbus_proxy_call_sync() functions.
1961 * See the #GDBusProxy:g-default-timeout property for more details.
1963 * Returns: Timeout to use for @proxy.
1968 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1970 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1971 return proxy->priv->timeout_msec;
1975 * g_dbus_proxy_set_default_timeout:
1976 * @proxy: A #GDBusProxy.
1977 * @timeout_msec: Timeout in milliseconds.
1979 * Sets the timeout to use if -1 (specifying default timeout) is
1980 * passed as @timeout_msec in the g_dbus_proxy_call() and
1981 * g_dbus_proxy_call_sync() functions.
1983 * See the #GDBusProxy:g-default-timeout property for more details.
1988 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1991 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1992 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1994 /* TODO: locking? */
1995 if (proxy->priv->timeout_msec != timeout_msec)
1997 proxy->priv->timeout_msec = timeout_msec;
1998 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2003 * g_dbus_proxy_get_interface_info:
2004 * @proxy: A #GDBusProxy
2006 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2007 * interface that @proxy conforms to.
2009 * See the #GDBusProxy:g-interface-info property for more details.
2011 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2012 * object, it is owned by @proxy.
2016 GDBusInterfaceInfo *
2017 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2019 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2020 return proxy->priv->expected_interface;
2024 * g_dbus_proxy_set_interface_info:
2025 * @proxy: A #GDBusProxy
2026 * @info: Minimum interface this proxy conforms to or %NULL to unset.
2028 * Ensure that interactions with @proxy conform to the given
2029 * interface. For example, when completing a method call, if the type
2030 * signature of the message isn't what's expected, the given #GError
2031 * is set. Signals that have a type signature mismatch are simply
2034 * See the #GDBusProxy:g-interface-info property for more details.
2039 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2040 GDBusInterfaceInfo *info)
2042 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2043 if (proxy->priv->expected_interface != NULL)
2044 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2045 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2048 /* ---------------------------------------------------------------------------------------------------- */
2051 maybe_split_method_name (const gchar *method_name,
2052 gchar **out_interface_name,
2053 const gchar **out_method_name)
2058 g_assert (out_interface_name != NULL);
2059 g_assert (out_method_name != NULL);
2060 *out_interface_name = NULL;
2061 *out_method_name = NULL;
2063 if (strchr (method_name, '.') != NULL)
2068 p = g_strdup (method_name);
2069 last_dot = strrchr (p, '.');
2072 *out_interface_name = p;
2073 *out_method_name = last_dot + 1;
2083 reply_cb (GDBusConnection *connection,
2087 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2092 value = g_dbus_connection_call_finish (connection,
2097 g_simple_async_result_set_from_error (simple,
2099 g_error_free (error);
2103 g_simple_async_result_set_op_res_gpointer (simple,
2105 (GDestroyNotify) g_variant_unref);
2108 /* no need to complete in idle since the method GDBusConnection already does */
2109 g_simple_async_result_complete (simple);
2112 static const GDBusMethodInfo *
2113 lookup_method_info_or_warn (GDBusProxy *proxy,
2114 const gchar *method_name)
2116 const GDBusMethodInfo *info;
2118 if (proxy->priv->expected_interface == NULL)
2121 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2124 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2125 method_name, proxy->priv->expected_interface->name);
2132 * g_dbus_proxy_call:
2133 * @proxy: A #GDBusProxy.
2134 * @method_name: Name of method to invoke.
2135 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2136 * @flags: Flags from the #GDBusCallFlags enumeration.
2137 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2138 * @cancellable: A #GCancellable or %NULL.
2139 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2140 * care about the result of the method invocation.
2141 * @user_data: The data to pass to @callback.
2143 * Asynchronously invokes the @method_name method on @proxy.
2145 * If @method_name contains any dots, then @name is split into interface and
2146 * method name parts. This allows using @proxy for invoking methods on
2149 * If the #GDBusConnection associated with @proxy is closed then
2150 * the operation will fail with %G_IO_ERROR_CLOSED. If
2151 * @cancellable is canceled, the operation will fail with
2152 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2153 * compatible with the D-Bus protocol, the operation fails with
2154 * %G_IO_ERROR_INVALID_ARGUMENT.
2156 * If the @parameters #GVariant is floating, it is consumed. This allows
2157 * convenient 'inline' use of g_variant_new(), e.g.:
2159 * g_dbus_proxy_call (proxy,
2161 * g_variant_new ("(ss)",
2164 * G_DBUS_CALL_FLAGS_NONE,
2167 * (GAsyncReadyCallback) two_strings_done,
2171 * This is an asynchronous method. When the operation is finished,
2172 * @callback will be invoked in the
2173 * <link linkend="g-main-context-push-thread-default">thread-default
2174 * main loop</link> of the thread you are calling this method from.
2175 * You can then call g_dbus_proxy_call_finish() to get the result of
2176 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2177 * version of this method.
2182 g_dbus_proxy_call (GDBusProxy *proxy,
2183 const gchar *method_name,
2184 GVariant *parameters,
2185 GDBusCallFlags flags,
2187 GCancellable *cancellable,
2188 GAsyncReadyCallback callback,
2191 GSimpleAsyncResult *simple;
2193 gchar *split_interface_name;
2194 const gchar *split_method_name;
2195 const GDBusMethodInfo *expected_method_info;
2196 const gchar *target_method_name;
2197 const gchar *target_interface_name;
2198 GVariantType *reply_type;
2200 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2201 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2202 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2203 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2205 simple = g_simple_async_result_new (G_OBJECT (proxy),
2210 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2211 target_method_name = was_split ? split_method_name : method_name;
2212 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2214 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
2216 /* Just warn here */
2217 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2219 if (expected_method_info)
2220 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2224 g_dbus_connection_call (proxy->priv->connection,
2225 proxy->priv->name_owner,
2226 proxy->priv->object_path,
2227 target_interface_name,
2232 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2234 (GAsyncReadyCallback) reply_cb,
2237 if (reply_type != NULL)
2238 g_variant_type_free (reply_type);
2240 g_free (split_interface_name);
2244 * g_dbus_proxy_call_finish:
2245 * @proxy: A #GDBusProxy.
2246 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2247 * @error: Return location for error or %NULL.
2249 * Finishes an operation started with g_dbus_proxy_call().
2251 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2252 * return values. Free with g_variant_unref().
2257 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2261 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2263 const char *method_name;
2265 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2266 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2267 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2269 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2273 if (g_simple_async_result_propagate_error (simple, error))
2276 value = g_simple_async_result_get_op_res_gpointer (simple);
2277 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
2284 * g_dbus_proxy_call_sync:
2285 * @proxy: A #GDBusProxy.
2286 * @method_name: Name of method to invoke.
2287 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2288 * @flags: Flags from the #GDBusCallFlags enumeration.
2289 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2290 * @cancellable: A #GCancellable or %NULL.
2291 * @error: Return location for error or %NULL.
2293 * Synchronously invokes the @method_name method on @proxy.
2295 * If @method_name contains any dots, then @name is split into interface and
2296 * method name parts. This allows using @proxy for invoking methods on
2299 * If the #GDBusConnection associated with @proxy is disconnected then
2300 * the operation will fail with %G_IO_ERROR_CLOSED. If
2301 * @cancellable is canceled, the operation will fail with
2302 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2303 * compatible with the D-Bus protocol, the operation fails with
2304 * %G_IO_ERROR_INVALID_ARGUMENT.
2306 * If the @parameters #GVariant is floating, it is consumed. This allows
2307 * convenient 'inline' use of g_variant_new(), e.g.:
2309 * g_dbus_proxy_call_sync (proxy,
2311 * g_variant_new ("(ss)",
2314 * G_DBUS_CALL_FLAGS_NONE,
2320 * The calling thread is blocked until a reply is received. See
2321 * g_dbus_proxy_call() for the asynchronous version of this
2324 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2325 * return values. Free with g_variant_unref().
2330 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2331 const gchar *method_name,
2332 GVariant *parameters,
2333 GDBusCallFlags flags,
2335 GCancellable *cancellable,
2340 gchar *split_interface_name;
2341 const gchar *split_method_name;
2342 const GDBusMethodInfo *expected_method_info;
2343 const gchar *target_method_name;
2344 const gchar *target_interface_name;
2345 GVariantType *reply_type;
2347 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2348 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2349 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2350 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2351 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2353 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2354 target_method_name = was_split ? split_method_name : method_name;
2355 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2357 if (proxy->priv->expected_interface)
2359 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
2360 if (expected_method_info == NULL)
2362 g_warning ("Trying to invoke method `%s' which isn't in expected interface `%s'",
2364 target_interface_name);
2369 expected_method_info = NULL;
2372 if (expected_method_info)
2373 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2377 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2378 proxy->priv->name_owner,
2379 proxy->priv->object_path,
2380 target_interface_name,
2385 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2389 if (reply_type != NULL)
2390 g_variant_type_free (reply_type);
2392 g_free (split_interface_name);
2397 /* ---------------------------------------------------------------------------------------------------- */
2399 #define __G_DBUS_PROXY_C__
2400 #include "gioaliasdef.c"