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"
46 * @short_description: Client-side proxies
49 * #GDBusProxy is a base class used for proxies to access a D-Bus
50 * interface on a remote object. A #GDBusProxy can be constructed for
51 * both well-known and unique names.
53 * By default, #GDBusProxy will cache all properties (and listen to
54 * changes) of the remote object, and proxy all signals that gets
55 * emitted. This behaviour can be changed by passing suitable
56 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
57 * well-known name, the property cache is flushed when the name owner
58 * vanishes and reloaded when a name owner appears.
60 * If a #GDBusProxy is used for a well-known name, the owner of the
61 * name is tracked and can be read from
62 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
63 * get notified of changes. Additionally, only signals and property
64 * changes emitted from the current name owner are considered. This
65 * avoids a number of race conditions when the name is lost by one
66 * owner and claimed by another.
68 * The generic #GDBusProxy::g-properties-changed and #GDBusProxy::g-signal
69 * signals are not very convenient to work with. Therefore, the recommended
70 * way of working with proxies is to subclass #GDBusProxy, and have
71 * more natural properties and signals in your derived class.
73 * See <xref linkend="gdbus-example-proxy-subclass"/> for an example.
75 * <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>
78 struct _GDBusProxyPrivate
81 GDBusConnection *connection;
83 GDBusProxyFlags flags;
87 gchar *interface_name;
90 guint name_owner_changed_subscription_id;
92 GCancellable *get_all_cancellable;
94 /* gchar* -> GVariant* */
95 GHashTable *properties;
97 GDBusInterfaceInfo *expected_interface;
99 guint properties_changed_subscriber_id;
100 guint signals_subscriber_id;
102 gboolean initialized;
114 PROP_G_INTERFACE_NAME,
115 PROP_G_DEFAULT_TIMEOUT,
116 PROP_G_INTERFACE_INFO
121 PROPERTIES_CHANGED_SIGNAL,
126 guint signals[LAST_SIGNAL] = {0};
128 static void initable_iface_init (GInitableIface *initable_iface);
129 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
131 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
132 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
133 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
137 g_dbus_proxy_finalize (GObject *object)
139 GDBusProxy *proxy = G_DBUS_PROXY (object);
141 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
143 if (proxy->priv->name_owner_changed_subscription_id > 0)
144 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
145 proxy->priv->name_owner_changed_subscription_id);
147 if (proxy->priv->properties_changed_subscriber_id > 0)
148 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
149 proxy->priv->properties_changed_subscriber_id);
151 if (proxy->priv->signals_subscriber_id > 0)
152 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
153 proxy->priv->signals_subscriber_id);
155 g_object_unref (proxy->priv->connection);
156 g_free (proxy->priv->name);
157 g_free (proxy->priv->name_owner);
158 g_free (proxy->priv->object_path);
159 g_free (proxy->priv->interface_name);
160 if (proxy->priv->properties != NULL)
161 g_hash_table_unref (proxy->priv->properties);
163 if (proxy->priv->expected_interface != NULL)
164 g_dbus_interface_info_unref (proxy->priv->expected_interface);
166 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
170 g_dbus_proxy_get_property (GObject *object,
175 GDBusProxy *proxy = G_DBUS_PROXY (object);
179 case PROP_G_CONNECTION:
180 g_value_set_object (value, proxy->priv->connection);
184 g_value_set_flags (value, proxy->priv->flags);
188 g_value_set_string (value, proxy->priv->name);
191 case PROP_G_NAME_OWNER:
192 g_value_set_string (value, proxy->priv->name_owner);
195 case PROP_G_OBJECT_PATH:
196 g_value_set_string (value, proxy->priv->object_path);
199 case PROP_G_INTERFACE_NAME:
200 g_value_set_string (value, proxy->priv->interface_name);
203 case PROP_G_DEFAULT_TIMEOUT:
204 g_value_set_int (value, proxy->priv->timeout_msec);
207 case PROP_G_INTERFACE_INFO:
208 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 g_dbus_proxy_set_property (GObject *object,
223 GDBusProxy *proxy = G_DBUS_PROXY (object);
227 case PROP_G_CONNECTION:
228 proxy->priv->connection = g_value_dup_object (value);
232 proxy->priv->flags = g_value_get_flags (value);
236 proxy->priv->name = g_value_dup_string (value);
239 case PROP_G_OBJECT_PATH:
240 proxy->priv->object_path = g_value_dup_string (value);
243 case PROP_G_INTERFACE_NAME:
244 proxy->priv->interface_name = g_value_dup_string (value);
247 case PROP_G_DEFAULT_TIMEOUT:
248 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
251 case PROP_G_INTERFACE_INFO:
252 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
255 case PROP_G_BUS_TYPE:
256 proxy->priv->bus_type = g_value_get_enum (value);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 g_dbus_proxy_class_init (GDBusProxyClass *klass)
268 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
270 gobject_class->finalize = g_dbus_proxy_finalize;
271 gobject_class->set_property = g_dbus_proxy_set_property;
272 gobject_class->get_property = g_dbus_proxy_get_property;
274 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
275 * in derived classes */
278 * GDBusProxy:g-interface-info:
280 * Ensure that interactions with this proxy conform to the given
281 * interface. For example, when completing a method call, if the
282 * type signature of the message isn't what's expected, the given
283 * #GError is set. Signals that have a type signature mismatch are
288 g_object_class_install_property (gobject_class,
289 PROP_G_INTERFACE_INFO,
290 g_param_spec_boxed ("g-interface-info",
291 P_("Interface Information"),
292 P_("Interface Information"),
293 G_TYPE_DBUS_INTERFACE_INFO,
296 G_PARAM_STATIC_NAME |
297 G_PARAM_STATIC_BLURB |
298 G_PARAM_STATIC_NICK));
301 * GDBusProxy:g-connection:
303 * The #GDBusConnection the proxy is for.
307 g_object_class_install_property (gobject_class,
309 g_param_spec_object ("g-connection",
311 P_("The connection the proxy is for"),
312 G_TYPE_DBUS_CONNECTION,
315 G_PARAM_CONSTRUCT_ONLY |
316 G_PARAM_STATIC_NAME |
317 G_PARAM_STATIC_BLURB |
318 G_PARAM_STATIC_NICK));
321 * GDBusProxy:g-bus-type:
323 * If this property is not %G_BUS_TYPE_NONE, then
324 * #GDBusProxy:g-connection must be %NULL and will be set to the
325 * #GDBusConnection obtained by calling g_bus_get() with the value
330 g_object_class_install_property (gobject_class,
332 g_param_spec_enum ("g-bus-type",
334 P_("The bus to connect to, if any"),
338 G_PARAM_CONSTRUCT_ONLY |
339 G_PARAM_STATIC_NAME |
340 G_PARAM_STATIC_BLURB |
341 G_PARAM_STATIC_NICK));
344 * GDBusProxy:g-flags:
346 * Flags from the #GDBusProxyFlags enumeration.
350 g_object_class_install_property (gobject_class,
352 g_param_spec_flags ("g-flags",
354 P_("Flags for the proxy"),
355 G_TYPE_DBUS_PROXY_FLAGS,
356 G_DBUS_PROXY_FLAGS_NONE,
359 G_PARAM_CONSTRUCT_ONLY |
360 G_PARAM_STATIC_NAME |
361 G_PARAM_STATIC_BLURB |
362 G_PARAM_STATIC_NICK));
367 * The well-known or unique name that the proxy is for.
371 g_object_class_install_property (gobject_class,
373 g_param_spec_string ("g-name",
375 P_("The well-known or unique name that the proxy is for"),
379 G_PARAM_CONSTRUCT_ONLY |
380 G_PARAM_STATIC_NAME |
381 G_PARAM_STATIC_BLURB |
382 G_PARAM_STATIC_NICK));
385 * GDBusProxy:g-name-owner:
387 * The unique name that owns #GDBusProxy:name or %NULL if no-one
388 * currently owns that name. You may connect to #GObject::notify signal to
389 * track changes to this property.
393 g_object_class_install_property (gobject_class,
395 g_param_spec_string ("g-name-owner",
397 P_("The unique name for the owner"),
400 G_PARAM_STATIC_NAME |
401 G_PARAM_STATIC_BLURB |
402 G_PARAM_STATIC_NICK));
405 * GDBusProxy:g-object-path:
407 * The object path the proxy is for.
411 g_object_class_install_property (gobject_class,
413 g_param_spec_string ("g-object-path",
415 P_("The object path the proxy is for"),
419 G_PARAM_CONSTRUCT_ONLY |
420 G_PARAM_STATIC_NAME |
421 G_PARAM_STATIC_BLURB |
422 G_PARAM_STATIC_NICK));
425 * GDBusProxy:g-interface-name:
427 * The D-Bus interface name the proxy is for.
431 g_object_class_install_property (gobject_class,
432 PROP_G_INTERFACE_NAME,
433 g_param_spec_string ("g-interface-name",
434 P_("g-interface-name"),
435 P_("The D-Bus interface name the proxy is for"),
439 G_PARAM_CONSTRUCT_ONLY |
440 G_PARAM_STATIC_NAME |
441 G_PARAM_STATIC_BLURB |
442 G_PARAM_STATIC_NICK));
445 * GDBusProxy:g-default-timeout:
447 * The timeout to use if -1 (specifying default timeout) is passed
448 * as @timeout_msec in the g_dbus_proxy_call() and
449 * g_dbus_proxy_call_sync() functions.
451 * This allows applications to set a proxy-wide timeout for all
452 * remote method invocations on the proxy. If this property is -1,
453 * the default timeout (typically 25 seconds) is used. If set to
454 * %G_MAXINT, then no timeout is used.
458 g_object_class_install_property (gobject_class,
459 PROP_G_DEFAULT_TIMEOUT,
460 g_param_spec_int ("g-default-timeout",
461 P_("Default Timeout"),
462 P_("Timeout for remote method invocation"),
469 G_PARAM_STATIC_NAME |
470 G_PARAM_STATIC_BLURB |
471 G_PARAM_STATIC_NICK));
474 * GDBusProxy::g-properties-changed:
475 * @proxy: The #GDBusProxy emitting the signal.
476 * @changed_properties: A #GVariant containing the properties that changed
477 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
479 * Emitted when one or more D-Bus properties on @proxy changes. The
480 * local cache has already been updated when this signal fires. Note
481 * that both @changed_properties and @invalidated_properties are
482 * guaranteed to never be %NULL (either may be empty though).
484 * This signal corresponds to the
485 * <literal>PropertiesChanged</literal> D-Bus signal on the
486 * <literal>org.freedesktop.DBus.Properties</literal> interface.
490 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
493 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
496 _gio_marshal_VOID__VARIANT_BOXED,
500 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
503 * GDBusProxy::g-signal:
504 * @proxy: The #GDBusProxy emitting the signal.
505 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
506 * @signal_name: The name of the signal.
507 * @parameters: A #GVariant tuple with parameters for the signal.
509 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
513 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
516 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
519 _gio_marshal_VOID__STRING_STRING_VARIANT,
527 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
531 g_dbus_proxy_init (GDBusProxy *proxy)
533 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
534 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
537 (GDestroyNotify) g_variant_unref);
540 /* ---------------------------------------------------------------------------------------------------- */
543 property_name_sort_func (const gchar **a,
546 return g_strcmp0 (*a, *b);
550 * g_dbus_proxy_get_cached_property_names:
551 * @proxy: A #GDBusProxy.
553 * Gets the names of all cached properties on @proxy.
555 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
556 * no cached properties. Free the returned array with g_strfreev().
561 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
568 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
571 if (g_hash_table_size (proxy->priv->properties) == 0)
574 p = g_ptr_array_new ();
576 g_hash_table_iter_init (&iter, proxy->priv->properties);
577 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
578 g_ptr_array_add (p, g_strdup (key));
579 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
580 g_ptr_array_add (p, NULL);
582 names = (gchar **) g_ptr_array_free (p, FALSE);
588 static const GDBusPropertyInfo *
589 lookup_property_info_or_warn (GDBusProxy *proxy,
590 const gchar *property_name)
592 const GDBusPropertyInfo *info;
594 if (proxy->priv->expected_interface == NULL)
597 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
600 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
602 proxy->priv->expected_interface->name);
609 * g_dbus_proxy_get_cached_property:
610 * @proxy: A #GDBusProxy.
611 * @property_name: Property name.
613 * Looks up the value for a property from the cache. This call does no
616 * If @proxy has an expected interface (see
617 * #GDBusProxy:g-interface-info), then @property_name (for existence)
618 * is checked against it.
620 * Returns: A reference to the #GVariant instance that holds the value
621 * for @property_name or %NULL if the value is not in the cache. The
622 * returned reference must be freed with g_variant_unref().
627 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
628 const gchar *property_name)
632 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
633 g_return_val_if_fail (property_name != NULL, NULL);
635 value = g_hash_table_lookup (proxy->priv->properties, property_name);
638 const GDBusPropertyInfo *info;
639 info = lookup_property_info_or_warn (proxy, property_name);
644 g_variant_ref (value);
651 * g_dbus_proxy_set_cached_property:
652 * @proxy: A #GDBusProxy
653 * @property_name: Property name.
654 * @value: Value for the property or %NULL to remove it from the cache.
656 * If @value is not %NULL, sets the cached value for the property with
657 * name @property_name to the value in @value.
659 * If @value is %NULL, then the cached value is removed from the
662 * If @proxy has an expected interface (see
663 * #GDBusProxy:g-interface-info), then @property_name (for existence)
664 * and @value (for the type) is checked against it.
666 * If the @value #GVariant is floating, it is consumed. This allows
667 * convenient 'inline' use of g_variant_new(), e.g.
669 * g_dbus_proxy_set_cached_property (proxy,
671 * g_variant_new ("(si)",
676 * Normally you will not need to use this method since @proxy is
677 * tracking changes using the
678 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
679 * D-Bus signal. However, for performance reasons an object may decide
680 * to not use this signal for some properties and instead use a
681 * proprietary out-of-band mechanism to transmit changes.
683 * As a concrete example, consider an object with a property
684 * <literal>ChatroomParticipants</literal> which is an array of
685 * strings. Instead of transmitting the same (long) array every time
686 * the property changes, it is more efficient to only transmit the
687 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
688 * name)</literal> and <literal>ChatroomParticipantParted(String
694 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
695 const gchar *property_name,
698 const GDBusPropertyInfo *info;
700 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
701 g_return_if_fail (property_name != NULL);
705 info = lookup_property_info_or_warn (proxy, property_name);
708 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
710 g_warning (_("Trying to set property %s of type %s but according to the expected "
711 "interface the type is %s"),
713 g_variant_get_type_string (value),
718 g_hash_table_insert (proxy->priv->properties,
719 g_strdup (property_name),
720 g_variant_ref_sink (value));
724 g_hash_table_remove (proxy->priv->properties, property_name);
731 /* ---------------------------------------------------------------------------------------------------- */
734 on_signal_received (GDBusConnection *connection,
735 const gchar *sender_name,
736 const gchar *object_path,
737 const gchar *interface_name,
738 const gchar *signal_name,
739 GVariant *parameters,
742 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
744 if (!proxy->priv->initialized)
747 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
750 g_signal_emit (proxy,
751 signals[SIGNAL_SIGNAL],
760 /* ---------------------------------------------------------------------------------------------------- */
763 on_properties_changed (GDBusConnection *connection,
764 const gchar *sender_name,
765 const gchar *object_path,
766 const gchar *interface_name,
767 const gchar *signal_name,
768 GVariant *parameters,
771 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
773 const gchar *interface_name_for_signal;
774 GVariant *changed_properties;
775 gchar **invalidated_properties;
782 changed_properties = NULL;
783 invalidated_properties = NULL;
785 if (!proxy->priv->initialized)
788 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
791 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
793 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
794 g_variant_get_type_string (parameters));
798 g_variant_get (parameters,
800 &interface_name_for_signal,
802 &invalidated_properties);
804 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
807 g_variant_iter_init (&iter, changed_properties);
808 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
810 g_hash_table_insert (proxy->priv->properties,
811 key, /* adopts string */
812 value); /* adopts value */
815 for (n = 0; invalidated_properties[n] != NULL; n++)
817 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
821 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
824 invalidated_properties);
827 if (changed_properties != NULL)
828 g_variant_unref (changed_properties);
829 g_free (invalidated_properties);
832 /* ---------------------------------------------------------------------------------------------------- */
835 process_get_all_reply (GDBusProxy *proxy,
842 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
844 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
845 g_variant_get_type_string (result));
849 g_variant_get (result, "(a{sv})", &iter);
850 while (g_variant_iter_next (iter, "{sv}", &key, &value))
852 g_hash_table_insert (proxy->priv->properties,
853 key, /* adopts string */
854 value); /* adopts value */
856 g_variant_iter_free (iter);
858 /* Synthesize ::g-properties-changed changed */
859 if (g_hash_table_size (proxy->priv->properties) > 0)
861 GVariant *changed_properties;
862 const gchar *invalidated_properties[1] = {NULL};
864 g_variant_get (result,
866 &changed_properties);
867 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
870 invalidated_properties);
871 g_variant_unref (changed_properties);
881 GCancellable *cancellable;
883 } LoadPropertiesOnNameOwnerChangedData;
886 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
890 LoadPropertiesOnNameOwnerChangedData *data = user_data;
898 result = g_dbus_connection_call_finish (connection,
903 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
905 /* We just ignore if GetAll() is failing. Because this might happen
906 * if the object has no properties at all. Or if the caller is
907 * not authorized to see the properties.
909 * Either way, apps can know about this by using
910 * get_cached_property_names() or get_cached_property().
912 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
913 * fact that GetAll() failed
915 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
916 g_error_free (error);
919 /* and finally we can notify */
922 g_free (data->proxy->priv->name_owner);
923 data->proxy->priv->name_owner = data->name_owner;
924 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
926 g_hash_table_remove_all (data->proxy->priv->properties);
929 process_get_all_reply (data->proxy, result);
930 g_variant_unref (result);
933 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
936 if (data->cancellable == data->proxy->priv->get_all_cancellable)
937 data->proxy->priv->get_all_cancellable = NULL;
939 g_object_unref (data->proxy);
940 g_object_unref (data->cancellable);
941 g_free (data->name_owner);
946 on_name_owner_changed (GDBusConnection *connection,
947 const gchar *sender_name,
948 const gchar *object_path,
949 const gchar *interface_name,
950 const gchar *signal_name,
951 GVariant *parameters,
954 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
955 const gchar *old_owner;
956 const gchar *new_owner;
958 /* if we are already trying to load properties, cancel that */
959 if (proxy->priv->get_all_cancellable != NULL)
961 g_cancellable_cancel (proxy->priv->get_all_cancellable);
962 proxy->priv->get_all_cancellable = NULL;
965 g_variant_get (parameters,
971 if (strlen (new_owner) == 0)
973 g_free (proxy->priv->name_owner);
974 proxy->priv->name_owner = NULL;
976 /* Synthesize ::g-properties-changed changed */
977 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
978 g_hash_table_size (proxy->priv->properties) > 0)
980 GVariantBuilder builder;
981 GVariant *changed_properties;
982 GPtrArray *invalidated_properties;
986 /* Build changed_properties (always empty) and invalidated_properties ... */
987 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
988 changed_properties = g_variant_builder_end (&builder);
989 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
990 g_hash_table_iter_init (&iter, proxy->priv->properties);
991 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
992 g_ptr_array_add (invalidated_properties, g_strdup (key));
993 g_ptr_array_add (invalidated_properties, NULL);
995 /* ... throw out the properties ... */
996 g_hash_table_remove_all (proxy->priv->properties);
998 /* ... and finally emit the ::g-properties-changed signal */
999 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1002 (const gchar* const *) invalidated_properties->pdata);
1003 g_variant_unref (changed_properties);
1004 g_ptr_array_unref (invalidated_properties);
1006 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1010 /* ignore duplicates - this can happen when activating the service */
1011 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1014 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1016 g_free (proxy->priv->name_owner);
1017 proxy->priv->name_owner = g_strdup (new_owner);
1018 g_hash_table_remove_all (proxy->priv->properties);
1019 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1023 LoadPropertiesOnNameOwnerChangedData *data;
1025 /* start loading properties.. only then emit notify::g-name-owner .. we
1026 * need to be able to cancel this in the event another NameOwnerChanged
1027 * signal suddenly happens
1030 g_assert (proxy->priv->get_all_cancellable == NULL);
1031 proxy->priv->get_all_cancellable = g_cancellable_new ();
1032 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1033 data->proxy = g_object_ref (proxy);
1034 data->cancellable = proxy->priv->get_all_cancellable;
1035 data->name_owner = g_strdup (new_owner);
1036 g_dbus_connection_call (proxy->priv->connection,
1038 proxy->priv->object_path,
1039 "org.freedesktop.DBus.Properties",
1041 g_variant_new ("(s)", proxy->priv->interface_name),
1042 G_VARIANT_TYPE ("(a{sv})"),
1043 G_DBUS_CALL_FLAGS_NONE,
1045 proxy->priv->get_all_cancellable,
1046 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1055 /* ---------------------------------------------------------------------------------------------------- */
1060 GCancellable *cancellable;
1061 GSimpleAsyncResult *simple;
1065 async_init_data_free (AsyncInitData *data)
1067 g_object_unref (data->proxy);
1068 if (data->cancellable != NULL)
1069 g_object_unref (data->cancellable);
1070 g_object_unref (data->simple);
1075 async_init_get_all_cb (GDBusConnection *connection,
1079 AsyncInitData *data = user_data;
1084 result = g_dbus_connection_call_finish (connection,
1089 /* We just ignore if GetAll() is failing. Because this might happen
1090 * if the object has no properties at all. Or if the caller is
1091 * not authorized to see the properties.
1093 * Either way, apps can know about this by using
1094 * get_cached_property_names() or get_cached_property().
1096 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1097 * fact that GetAll() failed
1099 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1100 g_error_free (error);
1104 g_simple_async_result_set_op_res_gpointer (data->simple,
1106 (GDestroyNotify) g_variant_unref);
1109 g_simple_async_result_complete_in_idle (data->simple);
1110 async_init_data_free (data);
1115 async_init_get_name_owner_cb (GDBusConnection *connection,
1119 AsyncInitData *data = user_data;
1127 result = g_dbus_connection_call_finish (connection,
1132 if (error->domain == G_DBUS_ERROR &&
1133 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1135 g_error_free (error);
1139 g_simple_async_result_set_from_error (data->simple, error);
1140 g_error_free (error);
1141 g_simple_async_result_complete_in_idle (data->simple);
1142 async_init_data_free (data);
1148 g_variant_get (result,
1150 &data->proxy->priv->name_owner);
1151 g_variant_unref (result);
1155 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1157 /* load all properties asynchronously */
1158 g_dbus_connection_call (data->proxy->priv->connection,
1159 data->proxy->priv->name_owner,
1160 data->proxy->priv->object_path,
1161 "org.freedesktop.DBus.Properties",
1163 g_variant_new ("(s)", data->proxy->priv->interface_name),
1164 G_VARIANT_TYPE ("(a{sv})"),
1165 G_DBUS_CALL_FLAGS_NONE,
1168 (GAsyncReadyCallback) async_init_get_all_cb,
1173 g_simple_async_result_complete_in_idle (data->simple);
1174 async_init_data_free (data);
1182 async_init_call_get_name_owner (AsyncInitData *data)
1184 g_dbus_connection_call (data->proxy->priv->connection,
1185 "org.freedesktop.DBus", /* name */
1186 "/org/freedesktop/DBus", /* object path */
1187 "org.freedesktop.DBus", /* interface */
1189 g_variant_new ("(s)",
1190 data->proxy->priv->name),
1191 G_VARIANT_TYPE ("(s)"),
1192 G_DBUS_CALL_FLAGS_NONE,
1195 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1200 async_init_start_service_by_name_cb (GDBusConnection *connection,
1204 AsyncInitData *data = user_data;
1209 result = g_dbus_connection_call_finish (connection,
1214 /* Errors are not unexpected; the bus will reply e.g.
1216 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1217 * was not provided by any .service files
1219 * This doesn't mean that the name doesn't have an owner, just
1220 * that it's not provided by a .service file. So just proceed to
1221 * invoke GetNameOwner() if dealing with that error.
1223 if (error->domain == G_DBUS_ERROR &&
1224 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1226 g_error_free (error);
1230 g_prefix_error (&error,
1231 _("Error calling StartServiceByName for %s: "),
1232 data->proxy->priv->name);
1238 guint32 start_service_result;
1239 g_variant_get (result,
1241 &start_service_result);
1242 g_variant_unref (result);
1243 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1244 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1246 /* continue to invoke GetNameOwner() */
1250 error = g_error_new (G_IO_ERROR,
1252 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1253 start_service_result,
1254 data->proxy->priv->name);
1259 async_init_call_get_name_owner (data);
1263 g_warn_if_fail (error != NULL);
1264 g_simple_async_result_set_from_error (data->simple, error);
1265 g_error_free (error);
1266 g_simple_async_result_complete_in_idle (data->simple);
1267 async_init_data_free (data);
1271 async_init_call_start_service_by_name (AsyncInitData *data)
1273 g_dbus_connection_call (data->proxy->priv->connection,
1274 "org.freedesktop.DBus", /* name */
1275 "/org/freedesktop/DBus", /* object path */
1276 "org.freedesktop.DBus", /* interface */
1277 "StartServiceByName",
1278 g_variant_new ("(su)",
1279 data->proxy->priv->name,
1281 G_VARIANT_TYPE ("(u)"),
1282 G_DBUS_CALL_FLAGS_NONE,
1285 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1290 async_initable_init_second_async (GAsyncInitable *initable,
1292 GCancellable *cancellable,
1293 GAsyncReadyCallback callback,
1296 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1297 AsyncInitData *data;
1299 data = g_new0 (AsyncInitData, 1);
1300 data->proxy = g_object_ref (proxy);
1301 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1302 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1307 /* Check name ownership asynchronously - possibly also start the service */
1308 if (proxy->priv->name == NULL)
1311 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1313 else if (g_dbus_is_unique_name (proxy->priv->name))
1315 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1316 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1320 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1322 async_init_call_get_name_owner (data);
1326 async_init_call_start_service_by_name (data);
1332 async_initable_init_second_finish (GAsyncInitable *initable,
1336 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1337 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1343 if (g_simple_async_result_propagate_error (simple, error))
1346 result = g_simple_async_result_get_op_res_gpointer (simple);
1349 process_get_all_reply (proxy, result);
1355 proxy->priv->initialized = TRUE;
1359 /* ---------------------------------------------------------------------------------------------------- */
1362 async_initable_init_first (GAsyncInitable *initable)
1364 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1366 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1368 /* subscribe to PropertiesChanged() */
1369 proxy->priv->properties_changed_subscriber_id =
1370 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1372 "org.freedesktop.DBus.Properties",
1373 "PropertiesChanged",
1374 proxy->priv->object_path,
1375 proxy->priv->interface_name,
1376 G_DBUS_SIGNAL_FLAGS_NONE,
1377 on_properties_changed,
1382 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1384 /* subscribe to all signals for the object */
1385 proxy->priv->signals_subscriber_id =
1386 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1388 proxy->priv->interface_name,
1390 proxy->priv->object_path,
1392 G_DBUS_SIGNAL_FLAGS_NONE,
1398 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1400 proxy->priv->name_owner_changed_subscription_id =
1401 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1402 "org.freedesktop.DBus", /* name */
1403 "org.freedesktop.DBus", /* interface */
1404 "NameOwnerChanged", /* signal name */
1405 "/org/freedesktop/DBus", /* path */
1406 proxy->priv->name, /* arg0 */
1407 G_DBUS_SIGNAL_FLAGS_NONE,
1408 on_name_owner_changed,
1414 /* ---------------------------------------------------------------------------------------------------- */
1416 /* initialization is split into two parts - the first is the
1417 * non-blocing part that requires the callers GMainContext - the
1418 * second is a blocking part async part that doesn't require the
1419 * callers GMainContext.. we do this split so the code can be reused
1420 * in the GInitable implementation below.
1422 * Note that obtaining a GDBusConnection is not shared between the two
1430 GCancellable *cancellable;
1431 GAsyncReadyCallback callback;
1433 } GetConnectionData;
1436 get_connection_cb (GObject *source_object,
1440 GetConnectionData *data = user_data;
1444 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1445 if (data->proxy->priv->connection == NULL)
1447 GSimpleAsyncResult *simple;
1448 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1452 g_simple_async_result_set_from_error (simple, error);
1453 g_error_free (error);
1454 g_simple_async_result_complete_in_idle (simple);
1455 g_object_unref (simple);
1459 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1460 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1467 if (data->cancellable != NULL)
1468 g_object_unref (data->cancellable);
1469 if (data->proxy != NULL)
1470 g_object_unref (data->proxy);
1475 async_initable_init_async (GAsyncInitable *initable,
1477 GCancellable *cancellable,
1478 GAsyncReadyCallback callback,
1481 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1483 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1485 GetConnectionData *data;
1487 g_assert (proxy->priv->connection == NULL);
1489 data = g_new0 (GetConnectionData, 1);
1490 data->proxy = g_object_ref (proxy);
1491 data->io_priority = io_priority;
1492 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1493 data->callback = callback;
1494 data->user_data = user_data;
1495 g_bus_get (proxy->priv->bus_type,
1502 async_initable_init_first (initable);
1503 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1508 async_initable_init_finish (GAsyncInitable *initable,
1512 return async_initable_init_second_finish (initable, res, error);
1516 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1518 async_initable_iface->init_async = async_initable_init_async;
1519 async_initable_iface->init_finish = async_initable_init_finish;
1522 /* ---------------------------------------------------------------------------------------------------- */
1526 GMainContext *context;
1529 } InitableAsyncInitableData;
1532 async_initable_init_async_cb (GObject *source_object,
1536 InitableAsyncInitableData *data = user_data;
1537 data->res = g_object_ref (res);
1538 g_main_loop_quit (data->loop);
1541 /* Simply reuse the GAsyncInitable implementation but run the first
1542 * part (that is non-blocking and requires the callers GMainContext)
1543 * with the callers GMainContext.. and the second with a private
1544 * GMainContext (bug 621310 is slightly related).
1546 * Note that obtaining a GDBusConnection is not shared between the two
1550 initable_init (GInitable *initable,
1551 GCancellable *cancellable,
1554 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1555 InitableAsyncInitableData *data;
1560 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1562 g_assert (proxy->priv->connection == NULL);
1563 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1566 if (proxy->priv->connection == NULL)
1570 async_initable_init_first (G_ASYNC_INITABLE (initable));
1572 data = g_new0 (InitableAsyncInitableData, 1);
1573 data->context = g_main_context_new ();
1574 data->loop = g_main_loop_new (data->context, FALSE);
1576 g_main_context_push_thread_default (data->context);
1578 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1581 async_initable_init_async_cb,
1584 g_main_loop_run (data->loop);
1586 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1590 g_main_context_pop_thread_default (data->context);
1592 g_main_context_unref (data->context);
1593 g_main_loop_unref (data->loop);
1594 g_object_unref (data->res);
1603 initable_iface_init (GInitableIface *initable_iface)
1605 initable_iface->init = initable_init;
1608 /* ---------------------------------------------------------------------------------------------------- */
1612 * @connection: A #GDBusConnection.
1613 * @flags: Flags used when constructing the proxy.
1614 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1615 * @name: A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1616 * @object_path: An object path.
1617 * @interface_name: A D-Bus interface name.
1618 * @cancellable: A #GCancellable or %NULL.
1619 * @callback: Callback function to invoke when the proxy is ready.
1620 * @user_data: User data to pass to @callback.
1622 * Creates a proxy for accessing @interface_name on the remote object
1623 * at @object_path owned by @name at @connection and asynchronously
1624 * loads D-Bus properties unless the
1625 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1626 * the #GDBusProxy::g-properties-changed signal to get notified about
1629 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1630 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1631 * to handle signals from the remote object.
1633 * If @name is a well-known name and the
1634 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1635 * owner currently exists, the message bus will be requested to launch
1636 * a name owner for the name.
1638 * This is a failable asynchronous constructor - when the proxy is
1639 * ready, @callback will be invoked and you can use
1640 * g_dbus_proxy_new_finish() to get the result.
1642 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1644 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1649 g_dbus_proxy_new (GDBusConnection *connection,
1650 GDBusProxyFlags flags,
1651 GDBusInterfaceInfo *info,
1653 const gchar *object_path,
1654 const gchar *interface_name,
1655 GCancellable *cancellable,
1656 GAsyncReadyCallback callback,
1659 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1660 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1661 g_return_if_fail (g_variant_is_object_path (object_path));
1662 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1664 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1670 "g-interface-info", info,
1672 "g-connection", connection,
1673 "g-object-path", object_path,
1674 "g-interface-name", interface_name,
1679 * g_dbus_proxy_new_finish:
1680 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1681 * @error: Return location for error or %NULL.
1683 * Finishes creating a #GDBusProxy.
1685 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1690 g_dbus_proxy_new_finish (GAsyncResult *res,
1694 GObject *source_object;
1696 source_object = g_async_result_get_source_object (res);
1697 g_assert (source_object != NULL);
1699 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1702 g_object_unref (source_object);
1705 return G_DBUS_PROXY (object);
1711 * g_dbus_proxy_new_sync:
1712 * @connection: A #GDBusConnection.
1713 * @flags: Flags used when constructing the proxy.
1714 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1715 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1716 * @object_path: An object path.
1717 * @interface_name: A D-Bus interface name.
1718 * @cancellable: (allow-none): A #GCancellable or %NULL.
1719 * @error: (allow-none): Return location for error or %NULL.
1721 * Creates a proxy for accessing @interface_name on the remote object
1722 * at @object_path owned by @name at @connection and synchronously
1723 * loads D-Bus properties unless the
1724 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1726 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1727 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1728 * to handle signals from the remote object.
1730 * If @name is a well-known name and the
1731 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1732 * owner currently exists, the message bus will be requested to launch
1733 * a name owner for the name.
1735 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1736 * and g_dbus_proxy_new_finish() for the asynchronous version.
1738 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1740 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1745 g_dbus_proxy_new_sync (GDBusConnection *connection,
1746 GDBusProxyFlags flags,
1747 GDBusInterfaceInfo *info,
1749 const gchar *object_path,
1750 const gchar *interface_name,
1751 GCancellable *cancellable,
1754 GInitable *initable;
1756 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1757 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1758 g_dbus_is_name (name), NULL);
1759 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1760 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1762 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1766 "g-interface-info", info,
1768 "g-connection", connection,
1769 "g-object-path", object_path,
1770 "g-interface-name", interface_name,
1772 if (initable != NULL)
1773 return G_DBUS_PROXY (initable);
1778 /* ---------------------------------------------------------------------------------------------------- */
1781 * g_dbus_proxy_new_for_bus:
1782 * @bus_type: A #GBusType.
1783 * @flags: Flags used when constructing the proxy.
1784 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1785 * @name: A bus name (well-known or unique).
1786 * @object_path: An object path.
1787 * @interface_name: A D-Bus interface name.
1788 * @cancellable: A #GCancellable or %NULL.
1789 * @callback: Callback function to invoke when the proxy is ready.
1790 * @user_data: User data to pass to @callback.
1792 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1794 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1799 g_dbus_proxy_new_for_bus (GBusType bus_type,
1800 GDBusProxyFlags flags,
1801 GDBusInterfaceInfo *info,
1803 const gchar *object_path,
1804 const gchar *interface_name,
1805 GCancellable *cancellable,
1806 GAsyncReadyCallback callback,
1809 g_return_if_fail (g_dbus_is_name (name));
1810 g_return_if_fail (g_variant_is_object_path (object_path));
1811 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1813 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1819 "g-interface-info", info,
1821 "g-bus-type", bus_type,
1822 "g-object-path", object_path,
1823 "g-interface-name", interface_name,
1828 * g_dbus_proxy_new_for_bus_finish:
1829 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1830 * @error: Return location for error or %NULL.
1832 * Finishes creating a #GDBusProxy.
1834 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1839 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1842 return g_dbus_proxy_new_finish (res, error);
1846 * g_dbus_proxy_new_for_bus_sync:
1847 * @bus_type: A #GBusType.
1848 * @flags: Flags used when constructing the proxy.
1849 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1850 * @name: A bus name (well-known or unique).
1851 * @object_path: An object path.
1852 * @interface_name: A D-Bus interface name.
1853 * @cancellable: A #GCancellable or %NULL.
1854 * @error: Return location for error or %NULL.
1856 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1858 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1860 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1865 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1866 GDBusProxyFlags flags,
1867 GDBusInterfaceInfo *info,
1869 const gchar *object_path,
1870 const gchar *interface_name,
1871 GCancellable *cancellable,
1874 GInitable *initable;
1876 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1877 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1878 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1880 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1884 "g-interface-info", info,
1886 "g-bus-type", bus_type,
1887 "g-object-path", object_path,
1888 "g-interface-name", interface_name,
1890 if (initable != NULL)
1891 return G_DBUS_PROXY (initable);
1896 /* ---------------------------------------------------------------------------------------------------- */
1899 * g_dbus_proxy_get_connection:
1900 * @proxy: A #GDBusProxy.
1902 * Gets the connection @proxy is for.
1904 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1909 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1911 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1912 return proxy->priv->connection;
1916 * g_dbus_proxy_get_flags:
1917 * @proxy: A #GDBusProxy.
1919 * Gets the flags that @proxy was constructed with.
1921 * Returns: Flags from the #GDBusProxyFlags enumeration.
1926 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1928 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1929 return proxy->priv->flags;
1933 * g_dbus_proxy_get_name:
1934 * @proxy: A #GDBusProxy.
1936 * Gets the name that @proxy was constructed for.
1938 * Returns: A string owned by @proxy. Do not free.
1943 g_dbus_proxy_get_name (GDBusProxy *proxy)
1945 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1946 return proxy->priv->name;
1950 * g_dbus_proxy_get_name_owner:
1951 * @proxy: A #GDBusProxy.
1953 * The unique name that owns the name that @proxy is for or %NULL if
1954 * no-one currently owns that name. You may connect to the
1955 * #GObject::notify signal to track changes to the
1956 * #GDBusProxy:g-name-owner property.
1958 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
1963 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
1965 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1966 return g_strdup (proxy->priv->name_owner);
1970 * g_dbus_proxy_get_object_path:
1971 * @proxy: A #GDBusProxy.
1973 * Gets the object path @proxy is for.
1975 * Returns: A string owned by @proxy. Do not free.
1980 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1982 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1983 return proxy->priv->object_path;
1987 * g_dbus_proxy_get_interface_name:
1988 * @proxy: A #GDBusProxy.
1990 * Gets the D-Bus interface name @proxy is for.
1992 * Returns: A string owned by @proxy. Do not free.
1997 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1999 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2000 return proxy->priv->interface_name;
2004 * g_dbus_proxy_get_default_timeout:
2005 * @proxy: A #GDBusProxy.
2007 * Gets the timeout to use if -1 (specifying default timeout) is
2008 * passed as @timeout_msec in the g_dbus_proxy_call() and
2009 * g_dbus_proxy_call_sync() functions.
2011 * See the #GDBusProxy:g-default-timeout property for more details.
2013 * Returns: Timeout to use for @proxy.
2018 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2020 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2021 return proxy->priv->timeout_msec;
2025 * g_dbus_proxy_set_default_timeout:
2026 * @proxy: A #GDBusProxy.
2027 * @timeout_msec: Timeout in milliseconds.
2029 * Sets the timeout to use if -1 (specifying default timeout) is
2030 * passed as @timeout_msec in the g_dbus_proxy_call() and
2031 * g_dbus_proxy_call_sync() functions.
2033 * See the #GDBusProxy:g-default-timeout property for more details.
2038 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2041 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2042 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2044 /* TODO: locking? */
2045 if (proxy->priv->timeout_msec != timeout_msec)
2047 proxy->priv->timeout_msec = timeout_msec;
2048 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2053 * g_dbus_proxy_get_interface_info:
2054 * @proxy: A #GDBusProxy
2056 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2057 * interface that @proxy conforms to.
2059 * See the #GDBusProxy:g-interface-info property for more details.
2061 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2062 * object, it is owned by @proxy.
2066 GDBusInterfaceInfo *
2067 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2069 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2070 return proxy->priv->expected_interface;
2074 * g_dbus_proxy_set_interface_info:
2075 * @proxy: A #GDBusProxy
2076 * @info: Minimum interface this proxy conforms to or %NULL to unset.
2078 * Ensure that interactions with @proxy conform to the given
2079 * interface. For example, when completing a method call, if the type
2080 * signature of the message isn't what's expected, the given #GError
2081 * is set. Signals that have a type signature mismatch are simply
2084 * See the #GDBusProxy:g-interface-info property for more details.
2089 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2090 GDBusInterfaceInfo *info)
2092 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2093 if (proxy->priv->expected_interface != NULL)
2094 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2095 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2098 /* ---------------------------------------------------------------------------------------------------- */
2101 maybe_split_method_name (const gchar *method_name,
2102 gchar **out_interface_name,
2103 const gchar **out_method_name)
2108 g_assert (out_interface_name != NULL);
2109 g_assert (out_method_name != NULL);
2110 *out_interface_name = NULL;
2111 *out_method_name = NULL;
2113 if (strchr (method_name, '.') != NULL)
2118 p = g_strdup (method_name);
2119 last_dot = strrchr (p, '.');
2122 *out_interface_name = p;
2123 *out_method_name = last_dot + 1;
2133 reply_cb (GDBusConnection *connection,
2137 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2142 value = g_dbus_connection_call_finish (connection,
2147 g_simple_async_result_set_from_error (simple,
2149 g_error_free (error);
2153 g_simple_async_result_set_op_res_gpointer (simple,
2155 (GDestroyNotify) g_variant_unref);
2158 /* no need to complete in idle since the method GDBusConnection already does */
2159 g_simple_async_result_complete (simple);
2160 g_object_unref (simple);
2163 static const GDBusMethodInfo *
2164 lookup_method_info_or_warn (GDBusProxy *proxy,
2165 const gchar *method_name)
2167 const GDBusMethodInfo *info;
2169 if (proxy->priv->expected_interface == NULL)
2172 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2175 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2176 method_name, proxy->priv->expected_interface->name);
2183 * g_dbus_proxy_call:
2184 * @proxy: A #GDBusProxy.
2185 * @method_name: Name of method to invoke.
2186 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2187 * @flags: Flags from the #GDBusCallFlags enumeration.
2188 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2189 * @cancellable: A #GCancellable or %NULL.
2190 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2191 * care about the result of the method invocation.
2192 * @user_data: The data to pass to @callback.
2194 * Asynchronously invokes the @method_name method on @proxy.
2196 * If @method_name contains any dots, then @name is split into interface and
2197 * method name parts. This allows using @proxy for invoking methods on
2200 * If the #GDBusConnection associated with @proxy is closed then
2201 * the operation will fail with %G_IO_ERROR_CLOSED. If
2202 * @cancellable is canceled, the operation will fail with
2203 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2204 * compatible with the D-Bus protocol, the operation fails with
2205 * %G_IO_ERROR_INVALID_ARGUMENT.
2207 * If the @parameters #GVariant is floating, it is consumed. This allows
2208 * convenient 'inline' use of g_variant_new(), e.g.:
2210 * g_dbus_proxy_call (proxy,
2212 * g_variant_new ("(ss)",
2215 * G_DBUS_CALL_FLAGS_NONE,
2218 * (GAsyncReadyCallback) two_strings_done,
2222 * This is an asynchronous method. When the operation is finished,
2223 * @callback will be invoked in the
2224 * <link linkend="g-main-context-push-thread-default">thread-default
2225 * main loop</link> of the thread you are calling this method from.
2226 * You can then call g_dbus_proxy_call_finish() to get the result of
2227 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2228 * version of this method.
2233 g_dbus_proxy_call (GDBusProxy *proxy,
2234 const gchar *method_name,
2235 GVariant *parameters,
2236 GDBusCallFlags flags,
2238 GCancellable *cancellable,
2239 GAsyncReadyCallback callback,
2242 GSimpleAsyncResult *simple;
2244 gchar *split_interface_name;
2245 const gchar *split_method_name;
2246 const GDBusMethodInfo *expected_method_info;
2247 const gchar *target_method_name;
2248 const gchar *target_interface_name;
2249 GVariantType *reply_type;
2251 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2252 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2253 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2254 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2256 simple = g_simple_async_result_new (G_OBJECT (proxy),
2261 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2262 target_method_name = was_split ? split_method_name : method_name;
2263 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2265 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
2267 /* Warn if method is unexpected (cf. :g-interface-info) */
2268 expected_method_info = NULL;
2270 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2272 if (expected_method_info)
2273 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2277 g_dbus_connection_call (proxy->priv->connection,
2278 proxy->priv->name_owner,
2279 proxy->priv->object_path,
2280 target_interface_name,
2285 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2287 (GAsyncReadyCallback) reply_cb,
2290 if (reply_type != NULL)
2291 g_variant_type_free (reply_type);
2293 g_free (split_interface_name);
2297 * g_dbus_proxy_call_finish:
2298 * @proxy: A #GDBusProxy.
2299 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2300 * @error: Return location for error or %NULL.
2302 * Finishes an operation started with g_dbus_proxy_call().
2304 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2305 * return values. Free with g_variant_unref().
2310 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2314 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2316 const char *method_name;
2318 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2319 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2320 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2322 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2326 if (g_simple_async_result_propagate_error (simple, error))
2329 value = g_simple_async_result_get_op_res_gpointer (simple);
2330 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
2337 * g_dbus_proxy_call_sync:
2338 * @proxy: A #GDBusProxy.
2339 * @method_name: Name of method to invoke.
2340 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2341 * @flags: Flags from the #GDBusCallFlags enumeration.
2342 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2343 * @cancellable: A #GCancellable or %NULL.
2344 * @error: Return location for error or %NULL.
2346 * Synchronously invokes the @method_name method on @proxy.
2348 * If @method_name contains any dots, then @name is split into interface and
2349 * method name parts. This allows using @proxy for invoking methods on
2352 * If the #GDBusConnection associated with @proxy is disconnected then
2353 * the operation will fail with %G_IO_ERROR_CLOSED. If
2354 * @cancellable is canceled, the operation will fail with
2355 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2356 * compatible with the D-Bus protocol, the operation fails with
2357 * %G_IO_ERROR_INVALID_ARGUMENT.
2359 * If the @parameters #GVariant is floating, it is consumed. This allows
2360 * convenient 'inline' use of g_variant_new(), e.g.:
2362 * g_dbus_proxy_call_sync (proxy,
2364 * g_variant_new ("(ss)",
2367 * G_DBUS_CALL_FLAGS_NONE,
2373 * The calling thread is blocked until a reply is received. See
2374 * g_dbus_proxy_call() for the asynchronous version of this
2377 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2378 * return values. Free with g_variant_unref().
2383 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2384 const gchar *method_name,
2385 GVariant *parameters,
2386 GDBusCallFlags flags,
2388 GCancellable *cancellable,
2393 gchar *split_interface_name;
2394 const gchar *split_method_name;
2395 const GDBusMethodInfo *expected_method_info;
2396 const gchar *target_method_name;
2397 const gchar *target_interface_name;
2398 GVariantType *reply_type;
2400 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2401 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2402 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2403 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2404 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2406 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2407 target_method_name = was_split ? split_method_name : method_name;
2408 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2410 if (proxy->priv->expected_interface)
2412 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
2413 if (expected_method_info == NULL)
2415 g_warning ("Trying to invoke method `%s' which isn't in expected interface `%s'",
2417 target_interface_name);
2422 expected_method_info = NULL;
2425 if (expected_method_info)
2426 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2430 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2431 proxy->priv->name_owner,
2432 proxy->priv->object_path,
2433 target_interface_name,
2438 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2442 if (reply_type != NULL)
2443 g_variant_type_free (reply_type);
2445 g_free (split_interface_name);
2450 /* ---------------------------------------------------------------------------------------------------- */