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"
41 #include "gdbusinterface.h"
47 * @short_description: Client-side D-Bus interface proxy
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 and
66 * calls are always sent to the current name owner. This avoids a
67 * number of race conditions when the name is lost by one owner and
68 * claimed by another. However, if no name owner currently exists,
69 * then calls will be sent to the well-known name which may result in
70 * the message bus launching an owner (unless
71 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
73 * The generic #GDBusProxy::g-properties-changed and #GDBusProxy::g-signal
74 * signals are not very convenient to work with. Therefore, the recommended
75 * way of working with proxies is to subclass #GDBusProxy, and have
76 * more natural properties and signals in your derived class.
78 * See <xref linkend="gdbus-example-proxy-subclass"/> for an example.
80 * <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>
83 struct _GDBusProxyPrivate
86 GDBusConnection *connection;
88 GDBusProxyFlags flags;
92 gchar *interface_name;
95 guint name_owner_changed_subscription_id;
97 GCancellable *get_all_cancellable;
99 /* gchar* -> GVariant* */
100 GHashTable *properties;
102 GDBusInterfaceInfo *expected_interface;
104 guint properties_changed_subscriber_id;
105 guint signals_subscriber_id;
107 gboolean initialized;
119 PROP_G_INTERFACE_NAME,
120 PROP_G_DEFAULT_TIMEOUT,
121 PROP_G_INTERFACE_INFO
126 PROPERTIES_CHANGED_SIGNAL,
131 guint signals[LAST_SIGNAL] = {0};
133 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
134 static void initable_iface_init (GInitableIface *initable_iface);
135 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
137 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
138 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
139 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
140 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
144 g_dbus_proxy_finalize (GObject *object)
146 GDBusProxy *proxy = G_DBUS_PROXY (object);
148 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
150 if (proxy->priv->name_owner_changed_subscription_id > 0)
151 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
152 proxy->priv->name_owner_changed_subscription_id);
154 if (proxy->priv->properties_changed_subscriber_id > 0)
155 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
156 proxy->priv->properties_changed_subscriber_id);
158 if (proxy->priv->signals_subscriber_id > 0)
159 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
160 proxy->priv->signals_subscriber_id);
162 if (proxy->priv->connection != NULL)
163 g_object_unref (proxy->priv->connection);
164 g_free (proxy->priv->name);
165 g_free (proxy->priv->name_owner);
166 g_free (proxy->priv->object_path);
167 g_free (proxy->priv->interface_name);
168 if (proxy->priv->properties != NULL)
169 g_hash_table_unref (proxy->priv->properties);
171 if (proxy->priv->expected_interface != NULL)
173 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
174 g_dbus_interface_info_unref (proxy->priv->expected_interface);
177 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
181 g_dbus_proxy_get_property (GObject *object,
186 GDBusProxy *proxy = G_DBUS_PROXY (object);
190 case PROP_G_CONNECTION:
191 g_value_set_object (value, proxy->priv->connection);
195 g_value_set_flags (value, proxy->priv->flags);
199 g_value_set_string (value, proxy->priv->name);
202 case PROP_G_NAME_OWNER:
203 g_value_set_string (value, proxy->priv->name_owner);
206 case PROP_G_OBJECT_PATH:
207 g_value_set_string (value, proxy->priv->object_path);
210 case PROP_G_INTERFACE_NAME:
211 g_value_set_string (value, proxy->priv->interface_name);
214 case PROP_G_DEFAULT_TIMEOUT:
215 g_value_set_int (value, proxy->priv->timeout_msec);
218 case PROP_G_INTERFACE_INFO:
219 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 g_dbus_proxy_set_property (GObject *object,
234 GDBusProxy *proxy = G_DBUS_PROXY (object);
238 case PROP_G_CONNECTION:
239 proxy->priv->connection = g_value_dup_object (value);
243 proxy->priv->flags = g_value_get_flags (value);
247 proxy->priv->name = g_value_dup_string (value);
250 case PROP_G_OBJECT_PATH:
251 proxy->priv->object_path = g_value_dup_string (value);
254 case PROP_G_INTERFACE_NAME:
255 proxy->priv->interface_name = g_value_dup_string (value);
258 case PROP_G_DEFAULT_TIMEOUT:
259 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
262 case PROP_G_INTERFACE_INFO:
263 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
266 case PROP_G_BUS_TYPE:
267 proxy->priv->bus_type = g_value_get_enum (value);
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277 g_dbus_proxy_class_init (GDBusProxyClass *klass)
279 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281 gobject_class->finalize = g_dbus_proxy_finalize;
282 gobject_class->set_property = g_dbus_proxy_set_property;
283 gobject_class->get_property = g_dbus_proxy_get_property;
285 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
286 * in derived classes */
289 * GDBusProxy:g-interface-info:
291 * Ensure that interactions with this proxy conform to the given
292 * interface. For example, when completing a method call, if the
293 * type signature of the message isn't what's expected, the given
294 * #GError is set. Signals that have a type signature mismatch are
299 g_object_class_install_property (gobject_class,
300 PROP_G_INTERFACE_INFO,
301 g_param_spec_boxed ("g-interface-info",
302 P_("Interface Information"),
303 P_("Interface Information"),
304 G_TYPE_DBUS_INTERFACE_INFO,
307 G_PARAM_STATIC_NAME |
308 G_PARAM_STATIC_BLURB |
309 G_PARAM_STATIC_NICK));
312 * GDBusProxy:g-connection:
314 * The #GDBusConnection the proxy is for.
318 g_object_class_install_property (gobject_class,
320 g_param_spec_object ("g-connection",
322 P_("The connection the proxy is for"),
323 G_TYPE_DBUS_CONNECTION,
326 G_PARAM_CONSTRUCT_ONLY |
327 G_PARAM_STATIC_NAME |
328 G_PARAM_STATIC_BLURB |
329 G_PARAM_STATIC_NICK));
332 * GDBusProxy:g-bus-type:
334 * If this property is not %G_BUS_TYPE_NONE, then
335 * #GDBusProxy:g-connection must be %NULL and will be set to the
336 * #GDBusConnection obtained by calling g_bus_get() with the value
341 g_object_class_install_property (gobject_class,
343 g_param_spec_enum ("g-bus-type",
345 P_("The bus to connect to, if any"),
349 G_PARAM_CONSTRUCT_ONLY |
350 G_PARAM_STATIC_NAME |
351 G_PARAM_STATIC_BLURB |
352 G_PARAM_STATIC_NICK));
355 * GDBusProxy:g-flags:
357 * Flags from the #GDBusProxyFlags enumeration.
361 g_object_class_install_property (gobject_class,
363 g_param_spec_flags ("g-flags",
365 P_("Flags for the proxy"),
366 G_TYPE_DBUS_PROXY_FLAGS,
367 G_DBUS_PROXY_FLAGS_NONE,
370 G_PARAM_CONSTRUCT_ONLY |
371 G_PARAM_STATIC_NAME |
372 G_PARAM_STATIC_BLURB |
373 G_PARAM_STATIC_NICK));
378 * The well-known or unique name that the proxy is for.
382 g_object_class_install_property (gobject_class,
384 g_param_spec_string ("g-name",
386 P_("The well-known or unique name that the proxy is for"),
390 G_PARAM_CONSTRUCT_ONLY |
391 G_PARAM_STATIC_NAME |
392 G_PARAM_STATIC_BLURB |
393 G_PARAM_STATIC_NICK));
396 * GDBusProxy:g-name-owner:
398 * The unique name that owns #GDBusProxy:name or %NULL if no-one
399 * currently owns that name. You may connect to #GObject::notify signal to
400 * track changes to this property.
404 g_object_class_install_property (gobject_class,
406 g_param_spec_string ("g-name-owner",
408 P_("The unique name for the owner"),
411 G_PARAM_STATIC_NAME |
412 G_PARAM_STATIC_BLURB |
413 G_PARAM_STATIC_NICK));
416 * GDBusProxy:g-object-path:
418 * The object path the proxy is for.
422 g_object_class_install_property (gobject_class,
424 g_param_spec_string ("g-object-path",
426 P_("The object path the proxy is for"),
430 G_PARAM_CONSTRUCT_ONLY |
431 G_PARAM_STATIC_NAME |
432 G_PARAM_STATIC_BLURB |
433 G_PARAM_STATIC_NICK));
436 * GDBusProxy:g-interface-name:
438 * The D-Bus interface name the proxy is for.
442 g_object_class_install_property (gobject_class,
443 PROP_G_INTERFACE_NAME,
444 g_param_spec_string ("g-interface-name",
445 P_("g-interface-name"),
446 P_("The D-Bus interface name the proxy is for"),
450 G_PARAM_CONSTRUCT_ONLY |
451 G_PARAM_STATIC_NAME |
452 G_PARAM_STATIC_BLURB |
453 G_PARAM_STATIC_NICK));
456 * GDBusProxy:g-default-timeout:
458 * The timeout to use if -1 (specifying default timeout) is passed
459 * as @timeout_msec in the g_dbus_proxy_call() and
460 * g_dbus_proxy_call_sync() functions.
462 * This allows applications to set a proxy-wide timeout for all
463 * remote method invocations on the proxy. If this property is -1,
464 * the default timeout (typically 25 seconds) is used. If set to
465 * %G_MAXINT, then no timeout is used.
469 g_object_class_install_property (gobject_class,
470 PROP_G_DEFAULT_TIMEOUT,
471 g_param_spec_int ("g-default-timeout",
472 P_("Default Timeout"),
473 P_("Timeout for remote method invocation"),
480 G_PARAM_STATIC_NAME |
481 G_PARAM_STATIC_BLURB |
482 G_PARAM_STATIC_NICK));
485 * GDBusProxy::g-properties-changed:
486 * @proxy: The #GDBusProxy emitting the signal.
487 * @changed_properties: A #GVariant containing the properties that changed
488 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
490 * Emitted when one or more D-Bus properties on @proxy changes. The
491 * local cache has already been updated when this signal fires. Note
492 * that both @changed_properties and @invalidated_properties are
493 * guaranteed to never be %NULL (either may be empty though).
495 * This signal corresponds to the
496 * <literal>PropertiesChanged</literal> D-Bus signal on the
497 * <literal>org.freedesktop.DBus.Properties</literal> interface.
501 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
504 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
507 _gio_marshal_VOID__VARIANT_BOXED,
511 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
514 * GDBusProxy::g-signal:
515 * @proxy: The #GDBusProxy emitting the signal.
516 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
517 * @signal_name: The name of the signal.
518 * @parameters: A #GVariant tuple with parameters for the signal.
520 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
524 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
527 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
530 _gio_marshal_VOID__STRING_STRING_VARIANT,
538 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
542 g_dbus_proxy_init (GDBusProxy *proxy)
544 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
545 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
548 (GDestroyNotify) g_variant_unref);
551 /* ---------------------------------------------------------------------------------------------------- */
554 property_name_sort_func (const gchar **a,
557 return g_strcmp0 (*a, *b);
561 * g_dbus_proxy_get_cached_property_names:
562 * @proxy: A #GDBusProxy.
564 * Gets the names of all cached properties on @proxy.
566 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
567 * no cached properties. Free the returned array with g_strfreev().
572 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
579 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
582 if (g_hash_table_size (proxy->priv->properties) == 0)
585 p = g_ptr_array_new ();
587 g_hash_table_iter_init (&iter, proxy->priv->properties);
588 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
589 g_ptr_array_add (p, g_strdup (key));
590 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
591 g_ptr_array_add (p, NULL);
593 names = (gchar **) g_ptr_array_free (p, FALSE);
599 static const GDBusPropertyInfo *
600 lookup_property_info_or_warn (GDBusProxy *proxy,
601 const gchar *property_name)
603 const GDBusPropertyInfo *info;
605 if (proxy->priv->expected_interface == NULL)
608 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
611 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
613 proxy->priv->expected_interface->name);
620 * g_dbus_proxy_get_cached_property:
621 * @proxy: A #GDBusProxy.
622 * @property_name: Property name.
624 * Looks up the value for a property from the cache. This call does no
627 * If @proxy has an expected interface (see
628 * #GDBusProxy:g-interface-info), then @property_name (for existence)
629 * is checked against it.
631 * Returns: A reference to the #GVariant instance that holds the value
632 * for @property_name or %NULL if the value is not in the cache. The
633 * returned reference must be freed with g_variant_unref().
638 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
639 const gchar *property_name)
643 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
644 g_return_val_if_fail (property_name != NULL, NULL);
646 value = g_hash_table_lookup (proxy->priv->properties, property_name);
649 lookup_property_info_or_warn (proxy, property_name);
654 g_variant_ref (value);
661 * g_dbus_proxy_set_cached_property:
662 * @proxy: A #GDBusProxy
663 * @property_name: Property name.
664 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
666 * If @value is not %NULL, sets the cached value for the property with
667 * name @property_name to the value in @value.
669 * If @value is %NULL, then the cached value is removed from the
672 * If @proxy has an expected interface (see
673 * #GDBusProxy:g-interface-info), then @property_name (for existence)
674 * and @value (for the type) is checked against it.
676 * If the @value #GVariant is floating, it is consumed. This allows
677 * convenient 'inline' use of g_variant_new(), e.g.
679 * g_dbus_proxy_set_cached_property (proxy,
681 * g_variant_new ("(si)",
686 * Normally you will not need to use this method since @proxy is
687 * tracking changes using the
688 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
689 * D-Bus signal. However, for performance reasons an object may decide
690 * to not use this signal for some properties and instead use a
691 * proprietary out-of-band mechanism to transmit changes.
693 * As a concrete example, consider an object with a property
694 * <literal>ChatroomParticipants</literal> which is an array of
695 * strings. Instead of transmitting the same (long) array every time
696 * the property changes, it is more efficient to only transmit the
697 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
698 * name)</literal> and <literal>ChatroomParticipantParted(String
704 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
705 const gchar *property_name,
708 const GDBusPropertyInfo *info;
710 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
711 g_return_if_fail (property_name != NULL);
715 info = lookup_property_info_or_warn (proxy, property_name);
718 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
720 g_warning (_("Trying to set property %s of type %s but according to the expected "
721 "interface the type is %s"),
723 g_variant_get_type_string (value),
728 g_hash_table_insert (proxy->priv->properties,
729 g_strdup (property_name),
730 g_variant_ref_sink (value));
734 g_hash_table_remove (proxy->priv->properties, property_name);
741 /* ---------------------------------------------------------------------------------------------------- */
744 on_signal_received (GDBusConnection *connection,
745 const gchar *sender_name,
746 const gchar *object_path,
747 const gchar *interface_name,
748 const gchar *signal_name,
749 GVariant *parameters,
752 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
754 if (!proxy->priv->initialized)
757 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
760 if (proxy->priv->expected_interface != NULL)
762 const GDBusSignalInfo *info;
763 GVariantType *expected_type;
764 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
767 expected_type = _g_dbus_compute_complete_signature (info->args);
768 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
770 g_variant_type_free (expected_type);
773 g_variant_type_free (expected_type);
776 g_signal_emit (proxy,
777 signals[SIGNAL_SIGNAL],
786 /* ---------------------------------------------------------------------------------------------------- */
789 insert_property_checked (GDBusProxy *proxy,
790 gchar *property_name,
793 if (proxy->priv->expected_interface != NULL)
795 const GDBusPropertyInfo *info;
797 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
798 /* Ignore unknown properties */
802 /* Ignore properties with the wrong type */
803 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
807 g_hash_table_insert (proxy->priv->properties,
808 property_name, /* adopts string */
809 value); /* adopts value */
814 g_variant_unref (value);
815 g_free (property_name);
819 on_properties_changed (GDBusConnection *connection,
820 const gchar *sender_name,
821 const gchar *object_path,
822 const gchar *interface_name,
823 const gchar *signal_name,
824 GVariant *parameters,
827 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
828 const gchar *interface_name_for_signal;
829 GVariant *changed_properties;
830 gchar **invalidated_properties;
836 changed_properties = NULL;
837 invalidated_properties = NULL;
839 if (!proxy->priv->initialized)
842 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
845 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
847 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
848 g_variant_get_type_string (parameters));
852 g_variant_get (parameters,
854 &interface_name_for_signal,
856 &invalidated_properties);
858 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
861 g_variant_iter_init (&iter, changed_properties);
862 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
864 insert_property_checked (proxy,
865 key, /* adopts string */
866 value); /* adopts value */
869 for (n = 0; invalidated_properties[n] != NULL; n++)
871 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
875 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
878 invalidated_properties);
881 if (changed_properties != NULL)
882 g_variant_unref (changed_properties);
883 g_free (invalidated_properties);
886 /* ---------------------------------------------------------------------------------------------------- */
889 process_get_all_reply (GDBusProxy *proxy,
896 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
898 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
899 g_variant_get_type_string (result));
903 g_variant_get (result, "(a{sv})", &iter);
904 while (g_variant_iter_next (iter, "{sv}", &key, &value))
906 insert_property_checked (proxy,
907 key, /* adopts string */
908 value); /* adopts value */
910 g_variant_iter_free (iter);
912 /* Synthesize ::g-properties-changed changed */
913 if (g_hash_table_size (proxy->priv->properties) > 0)
915 GVariant *changed_properties;
916 const gchar *invalidated_properties[1] = {NULL};
918 g_variant_get (result,
920 &changed_properties);
921 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
924 invalidated_properties);
925 g_variant_unref (changed_properties);
935 GCancellable *cancellable;
937 } LoadPropertiesOnNameOwnerChangedData;
940 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
944 LoadPropertiesOnNameOwnerChangedData *data = user_data;
952 result = g_dbus_connection_call_finish (connection,
957 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
959 /* We just ignore if GetAll() is failing. Because this might happen
960 * if the object has no properties at all. Or if the caller is
961 * not authorized to see the properties.
963 * Either way, apps can know about this by using
964 * get_cached_property_names() or get_cached_property().
966 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
967 * fact that GetAll() failed
969 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
970 g_error_free (error);
973 /* and finally we can notify */
976 g_free (data->proxy->priv->name_owner);
977 data->proxy->priv->name_owner = data->name_owner;
978 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
980 g_hash_table_remove_all (data->proxy->priv->properties);
983 process_get_all_reply (data->proxy, result);
984 g_variant_unref (result);
987 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
990 if (data->cancellable == data->proxy->priv->get_all_cancellable)
991 data->proxy->priv->get_all_cancellable = NULL;
993 g_object_unref (data->proxy);
994 g_object_unref (data->cancellable);
995 g_free (data->name_owner);
1000 on_name_owner_changed (GDBusConnection *connection,
1001 const gchar *sender_name,
1002 const gchar *object_path,
1003 const gchar *interface_name,
1004 const gchar *signal_name,
1005 GVariant *parameters,
1008 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
1009 const gchar *old_owner;
1010 const gchar *new_owner;
1012 /* if we are already trying to load properties, cancel that */
1013 if (proxy->priv->get_all_cancellable != NULL)
1015 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1016 proxy->priv->get_all_cancellable = NULL;
1019 g_variant_get (parameters,
1025 if (strlen (new_owner) == 0)
1027 g_free (proxy->priv->name_owner);
1028 proxy->priv->name_owner = NULL;
1030 /* Synthesize ::g-properties-changed changed */
1031 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1032 g_hash_table_size (proxy->priv->properties) > 0)
1034 GVariantBuilder builder;
1035 GVariant *changed_properties;
1036 GPtrArray *invalidated_properties;
1037 GHashTableIter iter;
1040 /* Build changed_properties (always empty) and invalidated_properties ... */
1041 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1042 changed_properties = g_variant_builder_end (&builder);
1043 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1044 g_hash_table_iter_init (&iter, proxy->priv->properties);
1045 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1046 g_ptr_array_add (invalidated_properties, g_strdup (key));
1047 g_ptr_array_add (invalidated_properties, NULL);
1049 /* ... throw out the properties ... */
1050 g_hash_table_remove_all (proxy->priv->properties);
1052 /* ... and finally emit the ::g-properties-changed signal */
1053 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1056 (const gchar* const *) invalidated_properties->pdata);
1057 g_variant_unref (changed_properties);
1058 g_ptr_array_unref (invalidated_properties);
1060 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1064 /* ignore duplicates - this can happen when activating the service */
1065 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1068 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1070 g_free (proxy->priv->name_owner);
1071 proxy->priv->name_owner = g_strdup (new_owner);
1072 g_hash_table_remove_all (proxy->priv->properties);
1073 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1077 LoadPropertiesOnNameOwnerChangedData *data;
1079 /* start loading properties.. only then emit notify::g-name-owner .. we
1080 * need to be able to cancel this in the event another NameOwnerChanged
1081 * signal suddenly happens
1084 g_assert (proxy->priv->get_all_cancellable == NULL);
1085 proxy->priv->get_all_cancellable = g_cancellable_new ();
1086 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1087 data->proxy = g_object_ref (proxy);
1088 data->cancellable = proxy->priv->get_all_cancellable;
1089 data->name_owner = g_strdup (new_owner);
1090 g_dbus_connection_call (proxy->priv->connection,
1092 proxy->priv->object_path,
1093 "org.freedesktop.DBus.Properties",
1095 g_variant_new ("(s)", proxy->priv->interface_name),
1096 G_VARIANT_TYPE ("(a{sv})"),
1097 G_DBUS_CALL_FLAGS_NONE,
1099 proxy->priv->get_all_cancellable,
1100 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1109 /* ---------------------------------------------------------------------------------------------------- */
1114 GCancellable *cancellable;
1115 GSimpleAsyncResult *simple;
1119 async_init_data_free (AsyncInitData *data)
1121 g_object_unref (data->proxy);
1122 if (data->cancellable != NULL)
1123 g_object_unref (data->cancellable);
1124 g_object_unref (data->simple);
1129 async_init_get_all_cb (GDBusConnection *connection,
1133 AsyncInitData *data = user_data;
1138 result = g_dbus_connection_call_finish (connection,
1143 /* We just ignore if GetAll() is failing. Because this might happen
1144 * if the object has no properties at all. Or if the caller is
1145 * not authorized to see the properties.
1147 * Either way, apps can know about this by using
1148 * get_cached_property_names() or get_cached_property().
1150 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1151 * fact that GetAll() failed
1153 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1154 g_error_free (error);
1158 g_simple_async_result_set_op_res_gpointer (data->simple,
1160 (GDestroyNotify) g_variant_unref);
1163 g_simple_async_result_complete_in_idle (data->simple);
1164 async_init_data_free (data);
1169 async_init_get_name_owner_cb (GDBusConnection *connection,
1173 AsyncInitData *data = user_data;
1181 result = g_dbus_connection_call_finish (connection,
1186 if (error->domain == G_DBUS_ERROR &&
1187 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1189 g_error_free (error);
1193 g_simple_async_result_take_error (data->simple, error);
1194 g_simple_async_result_complete_in_idle (data->simple);
1195 async_init_data_free (data);
1201 g_variant_get (result,
1203 &data->proxy->priv->name_owner);
1204 g_variant_unref (result);
1208 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1210 /* load all properties asynchronously */
1211 g_dbus_connection_call (data->proxy->priv->connection,
1212 data->proxy->priv->name_owner,
1213 data->proxy->priv->object_path,
1214 "org.freedesktop.DBus.Properties",
1216 g_variant_new ("(s)", data->proxy->priv->interface_name),
1217 G_VARIANT_TYPE ("(a{sv})"),
1218 G_DBUS_CALL_FLAGS_NONE,
1221 (GAsyncReadyCallback) async_init_get_all_cb,
1226 g_simple_async_result_complete_in_idle (data->simple);
1227 async_init_data_free (data);
1235 async_init_call_get_name_owner (AsyncInitData *data)
1237 g_dbus_connection_call (data->proxy->priv->connection,
1238 "org.freedesktop.DBus", /* name */
1239 "/org/freedesktop/DBus", /* object path */
1240 "org.freedesktop.DBus", /* interface */
1242 g_variant_new ("(s)",
1243 data->proxy->priv->name),
1244 G_VARIANT_TYPE ("(s)"),
1245 G_DBUS_CALL_FLAGS_NONE,
1248 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1253 async_init_start_service_by_name_cb (GDBusConnection *connection,
1257 AsyncInitData *data = user_data;
1262 result = g_dbus_connection_call_finish (connection,
1267 /* Errors are not unexpected; the bus will reply e.g.
1269 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1270 * was not provided by any .service files
1272 * This doesn't mean that the name doesn't have an owner, just
1273 * that it's not provided by a .service file. So just proceed to
1274 * invoke GetNameOwner() if dealing with that error.
1276 if (error->domain == G_DBUS_ERROR &&
1277 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1279 g_error_free (error);
1283 g_prefix_error (&error,
1284 _("Error calling StartServiceByName for %s: "),
1285 data->proxy->priv->name);
1291 guint32 start_service_result;
1292 g_variant_get (result,
1294 &start_service_result);
1295 g_variant_unref (result);
1296 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1297 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1299 /* continue to invoke GetNameOwner() */
1303 error = g_error_new (G_IO_ERROR,
1305 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1306 start_service_result,
1307 data->proxy->priv->name);
1312 async_init_call_get_name_owner (data);
1316 g_warn_if_fail (error != NULL);
1317 g_simple_async_result_take_error (data->simple, error);
1318 g_simple_async_result_complete_in_idle (data->simple);
1319 async_init_data_free (data);
1323 async_init_call_start_service_by_name (AsyncInitData *data)
1325 g_dbus_connection_call (data->proxy->priv->connection,
1326 "org.freedesktop.DBus", /* name */
1327 "/org/freedesktop/DBus", /* object path */
1328 "org.freedesktop.DBus", /* interface */
1329 "StartServiceByName",
1330 g_variant_new ("(su)",
1331 data->proxy->priv->name,
1333 G_VARIANT_TYPE ("(u)"),
1334 G_DBUS_CALL_FLAGS_NONE,
1337 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1342 async_initable_init_second_async (GAsyncInitable *initable,
1344 GCancellable *cancellable,
1345 GAsyncReadyCallback callback,
1348 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1349 AsyncInitData *data;
1351 data = g_new0 (AsyncInitData, 1);
1352 data->proxy = g_object_ref (proxy);
1353 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1354 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1359 /* Check name ownership asynchronously - possibly also start the service */
1360 if (proxy->priv->name == NULL)
1363 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1365 else if (g_dbus_is_unique_name (proxy->priv->name))
1367 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1368 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1372 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1374 async_init_call_get_name_owner (data);
1378 async_init_call_start_service_by_name (data);
1384 async_initable_init_second_finish (GAsyncInitable *initable,
1388 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1389 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1395 if (g_simple_async_result_propagate_error (simple, error))
1398 result = g_simple_async_result_get_op_res_gpointer (simple);
1401 process_get_all_reply (proxy, result);
1407 proxy->priv->initialized = TRUE;
1411 /* ---------------------------------------------------------------------------------------------------- */
1414 async_initable_init_first (GAsyncInitable *initable)
1416 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1418 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1420 /* subscribe to PropertiesChanged() */
1421 proxy->priv->properties_changed_subscriber_id =
1422 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1424 "org.freedesktop.DBus.Properties",
1425 "PropertiesChanged",
1426 proxy->priv->object_path,
1427 proxy->priv->interface_name,
1428 G_DBUS_SIGNAL_FLAGS_NONE,
1429 on_properties_changed,
1434 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1436 /* subscribe to all signals for the object */
1437 proxy->priv->signals_subscriber_id =
1438 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1440 proxy->priv->interface_name,
1442 proxy->priv->object_path,
1444 G_DBUS_SIGNAL_FLAGS_NONE,
1450 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1452 proxy->priv->name_owner_changed_subscription_id =
1453 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1454 "org.freedesktop.DBus", /* name */
1455 "org.freedesktop.DBus", /* interface */
1456 "NameOwnerChanged", /* signal name */
1457 "/org/freedesktop/DBus", /* path */
1458 proxy->priv->name, /* arg0 */
1459 G_DBUS_SIGNAL_FLAGS_NONE,
1460 on_name_owner_changed,
1466 /* ---------------------------------------------------------------------------------------------------- */
1468 /* initialization is split into two parts - the first is the
1469 * non-blocing part that requires the callers GMainContext - the
1470 * second is a blocking part async part that doesn't require the
1471 * callers GMainContext.. we do this split so the code can be reused
1472 * in the GInitable implementation below.
1474 * Note that obtaining a GDBusConnection is not shared between the two
1482 GCancellable *cancellable;
1483 GAsyncReadyCallback callback;
1485 } GetConnectionData;
1488 get_connection_cb (GObject *source_object,
1492 GetConnectionData *data = user_data;
1496 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1497 if (data->proxy->priv->connection == NULL)
1499 GSimpleAsyncResult *simple;
1500 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1504 g_simple_async_result_take_error (simple, error);
1505 g_simple_async_result_complete_in_idle (simple);
1506 g_object_unref (simple);
1510 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1511 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1518 if (data->cancellable != NULL)
1519 g_object_unref (data->cancellable);
1520 if (data->proxy != NULL)
1521 g_object_unref (data->proxy);
1526 async_initable_init_async (GAsyncInitable *initable,
1528 GCancellable *cancellable,
1529 GAsyncReadyCallback callback,
1532 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1534 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1536 GetConnectionData *data;
1538 g_assert (proxy->priv->connection == NULL);
1540 data = g_new0 (GetConnectionData, 1);
1541 data->proxy = g_object_ref (proxy);
1542 data->io_priority = io_priority;
1543 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1544 data->callback = callback;
1545 data->user_data = user_data;
1546 g_bus_get (proxy->priv->bus_type,
1553 async_initable_init_first (initable);
1554 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1559 async_initable_init_finish (GAsyncInitable *initable,
1563 return async_initable_init_second_finish (initable, res, error);
1567 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1569 async_initable_iface->init_async = async_initable_init_async;
1570 async_initable_iface->init_finish = async_initable_init_finish;
1573 /* ---------------------------------------------------------------------------------------------------- */
1577 GMainContext *context;
1580 } InitableAsyncInitableData;
1583 async_initable_init_async_cb (GObject *source_object,
1587 InitableAsyncInitableData *data = user_data;
1588 data->res = g_object_ref (res);
1589 g_main_loop_quit (data->loop);
1592 /* Simply reuse the GAsyncInitable implementation but run the first
1593 * part (that is non-blocking and requires the callers GMainContext)
1594 * with the callers GMainContext.. and the second with a private
1595 * GMainContext (bug 621310 is slightly related).
1597 * Note that obtaining a GDBusConnection is not shared between the two
1601 initable_init (GInitable *initable,
1602 GCancellable *cancellable,
1605 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1606 InitableAsyncInitableData *data;
1611 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1613 g_assert (proxy->priv->connection == NULL);
1614 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1617 if (proxy->priv->connection == NULL)
1621 async_initable_init_first (G_ASYNC_INITABLE (initable));
1623 data = g_new0 (InitableAsyncInitableData, 1);
1624 data->context = g_main_context_new ();
1625 data->loop = g_main_loop_new (data->context, FALSE);
1627 g_main_context_push_thread_default (data->context);
1629 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1632 async_initable_init_async_cb,
1635 g_main_loop_run (data->loop);
1637 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1641 g_main_context_pop_thread_default (data->context);
1643 g_main_context_unref (data->context);
1644 g_main_loop_unref (data->loop);
1645 g_object_unref (data->res);
1654 initable_iface_init (GInitableIface *initable_iface)
1656 initable_iface->init = initable_init;
1659 /* ---------------------------------------------------------------------------------------------------- */
1663 * @connection: A #GDBusConnection.
1664 * @flags: Flags used when constructing the proxy.
1665 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1666 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1667 * @object_path: An object path.
1668 * @interface_name: A D-Bus interface name.
1669 * @cancellable: A #GCancellable or %NULL.
1670 * @callback: Callback function to invoke when the proxy is ready.
1671 * @user_data: User data to pass to @callback.
1673 * Creates a proxy for accessing @interface_name on the remote object
1674 * at @object_path owned by @name at @connection and asynchronously
1675 * loads D-Bus properties unless the
1676 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1677 * the #GDBusProxy::g-properties-changed signal to get notified about
1680 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1681 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1682 * to handle signals from the remote object.
1684 * If @name is a well-known name and the
1685 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1686 * owner currently exists, the message bus will be requested to launch
1687 * a name owner for the name.
1689 * This is a failable asynchronous constructor - when the proxy is
1690 * ready, @callback will be invoked and you can use
1691 * g_dbus_proxy_new_finish() to get the result.
1693 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1695 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1700 g_dbus_proxy_new (GDBusConnection *connection,
1701 GDBusProxyFlags flags,
1702 GDBusInterfaceInfo *info,
1704 const gchar *object_path,
1705 const gchar *interface_name,
1706 GCancellable *cancellable,
1707 GAsyncReadyCallback callback,
1710 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1711 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1712 g_return_if_fail (g_variant_is_object_path (object_path));
1713 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1715 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1721 "g-interface-info", info,
1723 "g-connection", connection,
1724 "g-object-path", object_path,
1725 "g-interface-name", interface_name,
1730 * g_dbus_proxy_new_finish:
1731 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1732 * @error: Return location for error or %NULL.
1734 * Finishes creating a #GDBusProxy.
1736 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1741 g_dbus_proxy_new_finish (GAsyncResult *res,
1745 GObject *source_object;
1747 source_object = g_async_result_get_source_object (res);
1748 g_assert (source_object != NULL);
1750 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1753 g_object_unref (source_object);
1756 return G_DBUS_PROXY (object);
1762 * g_dbus_proxy_new_sync:
1763 * @connection: A #GDBusConnection.
1764 * @flags: Flags used when constructing the proxy.
1765 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1766 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1767 * @object_path: An object path.
1768 * @interface_name: A D-Bus interface name.
1769 * @cancellable: (allow-none): A #GCancellable or %NULL.
1770 * @error: (allow-none): Return location for error or %NULL.
1772 * Creates a proxy for accessing @interface_name on the remote object
1773 * at @object_path owned by @name at @connection and synchronously
1774 * loads D-Bus properties unless the
1775 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1777 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1778 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1779 * to handle signals from the remote object.
1781 * If @name is a well-known name and the
1782 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1783 * owner currently exists, the message bus will be requested to launch
1784 * a name owner for the name.
1786 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1787 * and g_dbus_proxy_new_finish() for the asynchronous version.
1789 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1791 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1796 g_dbus_proxy_new_sync (GDBusConnection *connection,
1797 GDBusProxyFlags flags,
1798 GDBusInterfaceInfo *info,
1800 const gchar *object_path,
1801 const gchar *interface_name,
1802 GCancellable *cancellable,
1805 GInitable *initable;
1807 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1808 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1809 g_dbus_is_name (name), NULL);
1810 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1811 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1813 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1817 "g-interface-info", info,
1819 "g-connection", connection,
1820 "g-object-path", object_path,
1821 "g-interface-name", interface_name,
1823 if (initable != NULL)
1824 return G_DBUS_PROXY (initable);
1829 /* ---------------------------------------------------------------------------------------------------- */
1832 * g_dbus_proxy_new_for_bus:
1833 * @bus_type: A #GBusType.
1834 * @flags: Flags used when constructing the proxy.
1835 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1836 * @name: A bus name (well-known or unique).
1837 * @object_path: An object path.
1838 * @interface_name: A D-Bus interface name.
1839 * @cancellable: A #GCancellable or %NULL.
1840 * @callback: Callback function to invoke when the proxy is ready.
1841 * @user_data: User data to pass to @callback.
1843 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1845 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1850 g_dbus_proxy_new_for_bus (GBusType bus_type,
1851 GDBusProxyFlags flags,
1852 GDBusInterfaceInfo *info,
1854 const gchar *object_path,
1855 const gchar *interface_name,
1856 GCancellable *cancellable,
1857 GAsyncReadyCallback callback,
1860 g_return_if_fail (g_dbus_is_name (name));
1861 g_return_if_fail (g_variant_is_object_path (object_path));
1862 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1864 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1870 "g-interface-info", info,
1872 "g-bus-type", bus_type,
1873 "g-object-path", object_path,
1874 "g-interface-name", interface_name,
1879 * g_dbus_proxy_new_for_bus_finish:
1880 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1881 * @error: Return location for error or %NULL.
1883 * Finishes creating a #GDBusProxy.
1885 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1890 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1893 return g_dbus_proxy_new_finish (res, error);
1897 * g_dbus_proxy_new_for_bus_sync:
1898 * @bus_type: A #GBusType.
1899 * @flags: Flags used when constructing the proxy.
1900 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
1901 * that @proxy conforms to or %NULL.
1902 * @name: A bus name (well-known or unique).
1903 * @object_path: An object path.
1904 * @interface_name: A D-Bus interface name.
1905 * @cancellable: A #GCancellable or %NULL.
1906 * @error: Return location for error or %NULL.
1908 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1910 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1912 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1917 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1918 GDBusProxyFlags flags,
1919 GDBusInterfaceInfo *info,
1921 const gchar *object_path,
1922 const gchar *interface_name,
1923 GCancellable *cancellable,
1926 GInitable *initable;
1928 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1929 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1930 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1932 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1936 "g-interface-info", info,
1938 "g-bus-type", bus_type,
1939 "g-object-path", object_path,
1940 "g-interface-name", interface_name,
1942 if (initable != NULL)
1943 return G_DBUS_PROXY (initable);
1948 /* ---------------------------------------------------------------------------------------------------- */
1951 * g_dbus_proxy_get_connection:
1952 * @proxy: A #GDBusProxy.
1954 * Gets the connection @proxy is for.
1956 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
1961 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1963 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1964 return proxy->priv->connection;
1968 * g_dbus_proxy_get_flags:
1969 * @proxy: A #GDBusProxy.
1971 * Gets the flags that @proxy was constructed with.
1973 * Returns: Flags from the #GDBusProxyFlags enumeration.
1978 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1980 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1981 return proxy->priv->flags;
1985 * g_dbus_proxy_get_name:
1986 * @proxy: A #GDBusProxy.
1988 * Gets the name that @proxy was constructed for.
1990 * Returns: A string owned by @proxy. Do not free.
1995 g_dbus_proxy_get_name (GDBusProxy *proxy)
1997 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1998 return proxy->priv->name;
2002 * g_dbus_proxy_get_name_owner:
2003 * @proxy: A #GDBusProxy.
2005 * The unique name that owns the name that @proxy is for or %NULL if
2006 * no-one currently owns that name. You may connect to the
2007 * #GObject::notify signal to track changes to the
2008 * #GDBusProxy:g-name-owner property.
2010 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2015 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2017 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2018 return g_strdup (proxy->priv->name_owner);
2022 * g_dbus_proxy_get_object_path:
2023 * @proxy: A #GDBusProxy.
2025 * Gets the object path @proxy is for.
2027 * Returns: A string owned by @proxy. Do not free.
2032 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2034 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2035 return proxy->priv->object_path;
2039 * g_dbus_proxy_get_interface_name:
2040 * @proxy: A #GDBusProxy.
2042 * Gets the D-Bus interface name @proxy is for.
2044 * Returns: A string owned by @proxy. Do not free.
2049 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2051 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2052 return proxy->priv->interface_name;
2056 * g_dbus_proxy_get_default_timeout:
2057 * @proxy: A #GDBusProxy.
2059 * Gets the timeout to use if -1 (specifying default timeout) is
2060 * passed as @timeout_msec in the g_dbus_proxy_call() and
2061 * g_dbus_proxy_call_sync() functions.
2063 * See the #GDBusProxy:g-default-timeout property for more details.
2065 * Returns: Timeout to use for @proxy.
2070 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2072 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2073 return proxy->priv->timeout_msec;
2077 * g_dbus_proxy_set_default_timeout:
2078 * @proxy: A #GDBusProxy.
2079 * @timeout_msec: Timeout in milliseconds.
2081 * Sets the timeout to use if -1 (specifying default timeout) is
2082 * passed as @timeout_msec in the g_dbus_proxy_call() and
2083 * g_dbus_proxy_call_sync() functions.
2085 * See the #GDBusProxy:g-default-timeout property for more details.
2090 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2093 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2094 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2096 /* TODO: locking? */
2097 if (proxy->priv->timeout_msec != timeout_msec)
2099 proxy->priv->timeout_msec = timeout_msec;
2100 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2105 * g_dbus_proxy_get_interface_info:
2106 * @proxy: A #GDBusProxy
2108 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2109 * interface that @proxy conforms to.
2111 * See the #GDBusProxy:g-interface-info property for more details.
2113 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2114 * object, it is owned by @proxy.
2118 GDBusInterfaceInfo *
2119 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2121 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2122 return proxy->priv->expected_interface;
2126 * g_dbus_proxy_set_interface_info:
2127 * @proxy: A #GDBusProxy
2128 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2130 * Ensure that interactions with @proxy conform to the given
2131 * interface. For example, when completing a method call, if the type
2132 * signature of the message isn't what's expected, the given #GError
2133 * is set. Signals that have a type signature mismatch are simply
2136 * See the #GDBusProxy:g-interface-info property for more details.
2141 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2142 GDBusInterfaceInfo *info)
2144 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2145 if (proxy->priv->expected_interface != NULL)
2147 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2148 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2150 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2151 if (proxy->priv->expected_interface != NULL)
2152 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2155 /* ---------------------------------------------------------------------------------------------------- */
2158 maybe_split_method_name (const gchar *method_name,
2159 gchar **out_interface_name,
2160 const gchar **out_method_name)
2165 g_assert (out_interface_name != NULL);
2166 g_assert (out_method_name != NULL);
2167 *out_interface_name = NULL;
2168 *out_method_name = NULL;
2170 if (strchr (method_name, '.') != NULL)
2175 p = g_strdup (method_name);
2176 last_dot = strrchr (p, '.');
2179 *out_interface_name = p;
2180 *out_method_name = last_dot + 1;
2190 reply_cb (GDBusConnection *connection,
2194 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2199 value = g_dbus_connection_call_finish (connection,
2204 g_simple_async_result_take_error (simple, error);
2208 g_simple_async_result_set_op_res_gpointer (simple,
2210 (GDestroyNotify) g_variant_unref);
2213 /* no need to complete in idle since the method GDBusConnection already does */
2214 g_simple_async_result_complete (simple);
2215 g_object_unref (simple);
2218 static const GDBusMethodInfo *
2219 lookup_method_info_or_warn (GDBusProxy *proxy,
2220 const gchar *method_name)
2222 const GDBusMethodInfo *info;
2224 if (proxy->priv->expected_interface == NULL)
2227 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2230 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2231 method_name, proxy->priv->expected_interface->name);
2237 static const gchar *
2238 get_destination_for_call (GDBusProxy *proxy)
2244 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2245 * is never NULL and always the same as proxy->priv->name. We use this
2246 * knowledge to avoid checking if proxy->priv->name is a unique or
2249 ret = proxy->priv->name_owner;
2253 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2256 ret = proxy->priv->name;
2263 * g_dbus_proxy_call:
2264 * @proxy: A #GDBusProxy.
2265 * @method_name: Name of method to invoke.
2266 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2267 * @flags: Flags from the #GDBusCallFlags enumeration.
2268 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2269 * "infinite") or -1 to use the proxy default timeout.
2270 * @cancellable: A #GCancellable or %NULL.
2271 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2272 * care about the result of the method invocation.
2273 * @user_data: The data to pass to @callback.
2275 * Asynchronously invokes the @method_name method on @proxy.
2277 * If @method_name contains any dots, then @name is split into interface and
2278 * method name parts. This allows using @proxy for invoking methods on
2281 * If the #GDBusConnection associated with @proxy is closed then
2282 * the operation will fail with %G_IO_ERROR_CLOSED. If
2283 * @cancellable is canceled, the operation will fail with
2284 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2285 * compatible with the D-Bus protocol, the operation fails with
2286 * %G_IO_ERROR_INVALID_ARGUMENT.
2288 * If the @parameters #GVariant is floating, it is consumed. This allows
2289 * convenient 'inline' use of g_variant_new(), e.g.:
2291 * g_dbus_proxy_call (proxy,
2293 * g_variant_new ("(ss)",
2296 * G_DBUS_CALL_FLAGS_NONE,
2299 * (GAsyncReadyCallback) two_strings_done,
2303 * This is an asynchronous method. When the operation is finished,
2304 * @callback will be invoked in the
2305 * <link linkend="g-main-context-push-thread-default">thread-default
2306 * main loop</link> of the thread you are calling this method from.
2307 * You can then call g_dbus_proxy_call_finish() to get the result of
2308 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2309 * version of this method.
2314 g_dbus_proxy_call (GDBusProxy *proxy,
2315 const gchar *method_name,
2316 GVariant *parameters,
2317 GDBusCallFlags flags,
2319 GCancellable *cancellable,
2320 GAsyncReadyCallback callback,
2323 GSimpleAsyncResult *simple;
2325 gchar *split_interface_name;
2326 const gchar *split_method_name;
2327 const gchar *target_method_name;
2328 const gchar *target_interface_name;
2329 const gchar *destination;
2330 GVariantType *reply_type;
2332 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2333 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2334 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2335 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2338 split_interface_name = NULL;
2340 simple = g_simple_async_result_new (G_OBJECT (proxy),
2345 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2346 target_method_name = was_split ? split_method_name : method_name;
2347 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2349 /* Warn if method is unexpected (cf. :g-interface-info) */
2352 const GDBusMethodInfo *expected_method_info;
2353 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2354 if (expected_method_info != NULL)
2355 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2359 if (proxy->priv->name != NULL)
2361 destination = get_destination_for_call (proxy);
2362 if (destination == NULL)
2364 g_simple_async_result_set_error (simple,
2367 _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2372 g_dbus_connection_call (proxy->priv->connection,
2374 proxy->priv->object_path,
2375 target_interface_name,
2380 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2382 (GAsyncReadyCallback) reply_cb,
2386 if (reply_type != NULL)
2387 g_variant_type_free (reply_type);
2389 g_free (split_interface_name);
2393 * g_dbus_proxy_call_finish:
2394 * @proxy: A #GDBusProxy.
2395 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2396 * @error: Return location for error or %NULL.
2398 * Finishes an operation started with g_dbus_proxy_call().
2400 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2401 * return values. Free with g_variant_unref().
2406 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2410 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2413 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2415 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2417 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2421 if (g_simple_async_result_propagate_error (simple, error))
2424 value = g_variant_ref (g_simple_async_result_get_op_res_gpointer (simple));
2431 * g_dbus_proxy_call_sync:
2432 * @proxy: A #GDBusProxy.
2433 * @method_name: Name of method to invoke.
2434 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2435 * or %NULL if not passing parameters.
2436 * @flags: Flags from the #GDBusCallFlags enumeration.
2437 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2438 * "infinite") or -1 to use the proxy default timeout.
2439 * @cancellable: A #GCancellable or %NULL.
2440 * @error: Return location for error or %NULL.
2442 * Synchronously invokes the @method_name method on @proxy.
2444 * If @method_name contains any dots, then @name is split into interface and
2445 * method name parts. This allows using @proxy for invoking methods on
2448 * If the #GDBusConnection associated with @proxy is disconnected then
2449 * the operation will fail with %G_IO_ERROR_CLOSED. If
2450 * @cancellable is canceled, the operation will fail with
2451 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2452 * compatible with the D-Bus protocol, the operation fails with
2453 * %G_IO_ERROR_INVALID_ARGUMENT.
2455 * If the @parameters #GVariant is floating, it is consumed. This allows
2456 * convenient 'inline' use of g_variant_new(), e.g.:
2458 * g_dbus_proxy_call_sync (proxy,
2460 * g_variant_new ("(ss)",
2463 * G_DBUS_CALL_FLAGS_NONE,
2469 * The calling thread is blocked until a reply is received. See
2470 * g_dbus_proxy_call() for the asynchronous version of this
2473 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2474 * return values. Free with g_variant_unref().
2479 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2480 const gchar *method_name,
2481 GVariant *parameters,
2482 GDBusCallFlags flags,
2484 GCancellable *cancellable,
2489 gchar *split_interface_name;
2490 const gchar *split_method_name;
2491 const gchar *target_method_name;
2492 const gchar *target_interface_name;
2493 const gchar *destination;
2494 GVariantType *reply_type;
2496 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2497 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2498 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2499 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2500 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2504 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2505 target_method_name = was_split ? split_method_name : method_name;
2506 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2508 /* Warn if method is unexpected (cf. :g-interface-info) */
2511 const GDBusMethodInfo *expected_method_info;
2512 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2513 if (expected_method_info != NULL)
2514 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2518 if (proxy->priv->name != NULL)
2520 destination = get_destination_for_call (proxy);
2521 if (destination == NULL)
2523 g_set_error_literal (error,
2526 _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2532 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2534 proxy->priv->object_path,
2535 target_interface_name,
2540 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2545 if (reply_type != NULL)
2546 g_variant_type_free (reply_type);
2548 g_free (split_interface_name);
2553 /* ---------------------------------------------------------------------------------------------------- */
2554 /* Hack until this is merged into libgio (extending types at run-time isn't really safe in any way) */
2556 static GDBusInterfaceInfo *
2557 _g_dbus_proxy_get_info (GDBusInterface *interface)
2559 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2560 return g_dbus_proxy_get_interface_info (proxy);
2563 static GDBusObject *
2564 _g_dbus_proxy_get_object (GDBusInterface *interface)
2567 return g_object_get_data (G_OBJECT (interface), "-x-gdbus-binding-tool-object");
2571 _g_dbus_proxy_set_object (GDBusInterface *interface,
2572 GDBusObject *object)
2575 g_object_set_data (G_OBJECT (interface), "-x-gdbus-binding-tool-object", object);
2579 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2581 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
2582 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2583 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2586 /* ---------------------------------------------------------------------------------------------------- */