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 GDBusProxyFlags flags;
87 GDBusConnection *connection;
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;
121 PROP_G_INTERFACE_NAME,
122 PROP_G_DEFAULT_TIMEOUT,
123 PROP_G_INTERFACE_INFO
128 PROPERTIES_CHANGED_SIGNAL,
133 guint signals[LAST_SIGNAL] = {0};
135 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
136 static void initable_iface_init (GInitableIface *initable_iface);
137 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
139 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
140 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
141 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
142 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
146 g_dbus_proxy_finalize (GObject *object)
148 GDBusProxy *proxy = G_DBUS_PROXY (object);
150 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
152 if (proxy->priv->name_owner_changed_subscription_id > 0)
153 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
154 proxy->priv->name_owner_changed_subscription_id);
156 if (proxy->priv->properties_changed_subscriber_id > 0)
157 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
158 proxy->priv->properties_changed_subscriber_id);
160 if (proxy->priv->signals_subscriber_id > 0)
161 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
162 proxy->priv->signals_subscriber_id);
164 if (proxy->priv->connection != NULL)
165 g_object_unref (proxy->priv->connection);
166 g_free (proxy->priv->name);
167 g_free (proxy->priv->name_owner);
168 g_free (proxy->priv->object_path);
169 g_free (proxy->priv->interface_name);
170 if (proxy->priv->properties != NULL)
171 g_hash_table_unref (proxy->priv->properties);
173 if (proxy->priv->expected_interface != NULL)
175 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
176 g_dbus_interface_info_unref (proxy->priv->expected_interface);
179 if (proxy->priv->object != NULL)
180 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
182 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
186 g_dbus_proxy_get_property (GObject *object,
191 GDBusProxy *proxy = G_DBUS_PROXY (object);
195 case PROP_G_CONNECTION:
196 g_value_set_object (value, proxy->priv->connection);
200 g_value_set_flags (value, proxy->priv->flags);
204 g_value_set_string (value, proxy->priv->name);
207 case PROP_G_NAME_OWNER:
208 g_value_set_string (value, proxy->priv->name_owner);
211 case PROP_G_OBJECT_PATH:
212 g_value_set_string (value, proxy->priv->object_path);
215 case PROP_G_INTERFACE_NAME:
216 g_value_set_string (value, proxy->priv->interface_name);
219 case PROP_G_DEFAULT_TIMEOUT:
220 g_value_set_int (value, proxy->priv->timeout_msec);
223 case PROP_G_INTERFACE_INFO:
224 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 g_dbus_proxy_set_property (GObject *object,
239 GDBusProxy *proxy = G_DBUS_PROXY (object);
243 case PROP_G_CONNECTION:
244 proxy->priv->connection = g_value_dup_object (value);
248 proxy->priv->flags = g_value_get_flags (value);
252 proxy->priv->name = g_value_dup_string (value);
255 case PROP_G_OBJECT_PATH:
256 proxy->priv->object_path = g_value_dup_string (value);
259 case PROP_G_INTERFACE_NAME:
260 proxy->priv->interface_name = g_value_dup_string (value);
263 case PROP_G_DEFAULT_TIMEOUT:
264 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
267 case PROP_G_INTERFACE_INFO:
268 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
271 case PROP_G_BUS_TYPE:
272 proxy->priv->bus_type = g_value_get_enum (value);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 g_dbus_proxy_class_init (GDBusProxyClass *klass)
284 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
286 gobject_class->finalize = g_dbus_proxy_finalize;
287 gobject_class->set_property = g_dbus_proxy_set_property;
288 gobject_class->get_property = g_dbus_proxy_get_property;
290 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
291 * in derived classes */
294 * GDBusProxy:g-interface-info:
296 * Ensure that interactions with this proxy conform to the given
297 * interface. For example, when completing a method call, if the
298 * type signature of the message isn't what's expected, the given
299 * #GError is set. Signals that have a type signature mismatch are
304 g_object_class_install_property (gobject_class,
305 PROP_G_INTERFACE_INFO,
306 g_param_spec_boxed ("g-interface-info",
307 P_("Interface Information"),
308 P_("Interface Information"),
309 G_TYPE_DBUS_INTERFACE_INFO,
312 G_PARAM_STATIC_NAME |
313 G_PARAM_STATIC_BLURB |
314 G_PARAM_STATIC_NICK));
317 * GDBusProxy:g-connection:
319 * The #GDBusConnection the proxy is for.
323 g_object_class_install_property (gobject_class,
325 g_param_spec_object ("g-connection",
327 P_("The connection the proxy is for"),
328 G_TYPE_DBUS_CONNECTION,
331 G_PARAM_CONSTRUCT_ONLY |
332 G_PARAM_STATIC_NAME |
333 G_PARAM_STATIC_BLURB |
334 G_PARAM_STATIC_NICK));
337 * GDBusProxy:g-bus-type:
339 * If this property is not %G_BUS_TYPE_NONE, then
340 * #GDBusProxy:g-connection must be %NULL and will be set to the
341 * #GDBusConnection obtained by calling g_bus_get() with the value
346 g_object_class_install_property (gobject_class,
348 g_param_spec_enum ("g-bus-type",
350 P_("The bus to connect to, if any"),
354 G_PARAM_CONSTRUCT_ONLY |
355 G_PARAM_STATIC_NAME |
356 G_PARAM_STATIC_BLURB |
357 G_PARAM_STATIC_NICK));
360 * GDBusProxy:g-flags:
362 * Flags from the #GDBusProxyFlags enumeration.
366 g_object_class_install_property (gobject_class,
368 g_param_spec_flags ("g-flags",
370 P_("Flags for the proxy"),
371 G_TYPE_DBUS_PROXY_FLAGS,
372 G_DBUS_PROXY_FLAGS_NONE,
375 G_PARAM_CONSTRUCT_ONLY |
376 G_PARAM_STATIC_NAME |
377 G_PARAM_STATIC_BLURB |
378 G_PARAM_STATIC_NICK));
383 * The well-known or unique name that the proxy is for.
387 g_object_class_install_property (gobject_class,
389 g_param_spec_string ("g-name",
391 P_("The well-known or unique name that the proxy is for"),
395 G_PARAM_CONSTRUCT_ONLY |
396 G_PARAM_STATIC_NAME |
397 G_PARAM_STATIC_BLURB |
398 G_PARAM_STATIC_NICK));
401 * GDBusProxy:g-name-owner:
403 * The unique name that owns #GDBusProxy:name or %NULL if no-one
404 * currently owns that name. You may connect to #GObject::notify signal to
405 * track changes to this property.
409 g_object_class_install_property (gobject_class,
411 g_param_spec_string ("g-name-owner",
413 P_("The unique name for the owner"),
416 G_PARAM_STATIC_NAME |
417 G_PARAM_STATIC_BLURB |
418 G_PARAM_STATIC_NICK));
421 * GDBusProxy:g-object-path:
423 * The object path the proxy is for.
427 g_object_class_install_property (gobject_class,
429 g_param_spec_string ("g-object-path",
431 P_("The object path the proxy is for"),
435 G_PARAM_CONSTRUCT_ONLY |
436 G_PARAM_STATIC_NAME |
437 G_PARAM_STATIC_BLURB |
438 G_PARAM_STATIC_NICK));
441 * GDBusProxy:g-interface-name:
443 * The D-Bus interface name the proxy is for.
447 g_object_class_install_property (gobject_class,
448 PROP_G_INTERFACE_NAME,
449 g_param_spec_string ("g-interface-name",
450 P_("g-interface-name"),
451 P_("The D-Bus interface name the proxy is for"),
455 G_PARAM_CONSTRUCT_ONLY |
456 G_PARAM_STATIC_NAME |
457 G_PARAM_STATIC_BLURB |
458 G_PARAM_STATIC_NICK));
461 * GDBusProxy:g-default-timeout:
463 * The timeout to use if -1 (specifying default timeout) is passed
464 * as @timeout_msec in the g_dbus_proxy_call() and
465 * g_dbus_proxy_call_sync() functions.
467 * This allows applications to set a proxy-wide timeout for all
468 * remote method invocations on the proxy. If this property is -1,
469 * the default timeout (typically 25 seconds) is used. If set to
470 * %G_MAXINT, then no timeout is used.
474 g_object_class_install_property (gobject_class,
475 PROP_G_DEFAULT_TIMEOUT,
476 g_param_spec_int ("g-default-timeout",
477 P_("Default Timeout"),
478 P_("Timeout for remote method invocation"),
485 G_PARAM_STATIC_NAME |
486 G_PARAM_STATIC_BLURB |
487 G_PARAM_STATIC_NICK));
490 * GDBusProxy::g-properties-changed:
491 * @proxy: The #GDBusProxy emitting the signal.
492 * @changed_properties: A #GVariant containing the properties that changed
493 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
495 * Emitted when one or more D-Bus properties on @proxy changes. The
496 * local cache has already been updated when this signal fires. Note
497 * that both @changed_properties and @invalidated_properties are
498 * guaranteed to never be %NULL (either may be empty though).
500 * This signal corresponds to the
501 * <literal>PropertiesChanged</literal> D-Bus signal on the
502 * <literal>org.freedesktop.DBus.Properties</literal> interface.
506 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
508 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
509 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
512 _gio_marshal_VOID__VARIANT_BOXED,
516 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
519 * GDBusProxy::g-signal:
520 * @proxy: The #GDBusProxy emitting the signal.
521 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
522 * @signal_name: The name of the signal.
523 * @parameters: A #GVariant tuple with parameters for the signal.
525 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
529 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
531 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
532 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
535 _gio_marshal_VOID__STRING_STRING_VARIANT,
543 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
547 g_dbus_proxy_init (GDBusProxy *proxy)
549 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
550 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
553 (GDestroyNotify) g_variant_unref);
556 /* ---------------------------------------------------------------------------------------------------- */
559 property_name_sort_func (const gchar **a,
562 return g_strcmp0 (*a, *b);
566 * g_dbus_proxy_get_cached_property_names:
567 * @proxy: A #GDBusProxy.
569 * Gets the names of all cached properties on @proxy.
571 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
572 * no cached properties. Free the returned array with g_strfreev().
577 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
584 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
587 if (g_hash_table_size (proxy->priv->properties) == 0)
590 p = g_ptr_array_new ();
592 g_hash_table_iter_init (&iter, proxy->priv->properties);
593 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
594 g_ptr_array_add (p, g_strdup (key));
595 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
596 g_ptr_array_add (p, NULL);
598 names = (gchar **) g_ptr_array_free (p, FALSE);
604 static const GDBusPropertyInfo *
605 lookup_property_info_or_warn (GDBusProxy *proxy,
606 const gchar *property_name)
608 const GDBusPropertyInfo *info;
610 if (proxy->priv->expected_interface == NULL)
613 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
616 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
618 proxy->priv->expected_interface->name);
625 * g_dbus_proxy_get_cached_property:
626 * @proxy: A #GDBusProxy.
627 * @property_name: Property name.
629 * Looks up the value for a property from the cache. This call does no
632 * If @proxy has an expected interface (see
633 * #GDBusProxy:g-interface-info), then @property_name (for existence)
634 * is checked against it.
636 * Returns: A reference to the #GVariant instance that holds the value
637 * for @property_name or %NULL if the value is not in the cache. The
638 * returned reference must be freed with g_variant_unref().
643 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
644 const gchar *property_name)
648 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
649 g_return_val_if_fail (property_name != NULL, NULL);
651 value = g_hash_table_lookup (proxy->priv->properties, property_name);
654 lookup_property_info_or_warn (proxy, property_name);
659 g_variant_ref (value);
666 * g_dbus_proxy_set_cached_property:
667 * @proxy: A #GDBusProxy
668 * @property_name: Property name.
669 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
671 * If @value is not %NULL, sets the cached value for the property with
672 * name @property_name to the value in @value.
674 * If @value is %NULL, then the cached value is removed from the
677 * If @proxy has an expected interface (see
678 * #GDBusProxy:g-interface-info), then @property_name (for existence)
679 * and @value (for the type) is checked against it.
681 * If the @value #GVariant is floating, it is consumed. This allows
682 * convenient 'inline' use of g_variant_new(), e.g.
684 * g_dbus_proxy_set_cached_property (proxy,
686 * g_variant_new ("(si)",
691 * Normally you will not need to use this method since @proxy is
692 * tracking changes using the
693 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
694 * D-Bus signal. However, for performance reasons an object may decide
695 * to not use this signal for some properties and instead use a
696 * proprietary out-of-band mechanism to transmit changes.
698 * As a concrete example, consider an object with a property
699 * <literal>ChatroomParticipants</literal> which is an array of
700 * strings. Instead of transmitting the same (long) array every time
701 * the property changes, it is more efficient to only transmit the
702 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
703 * name)</literal> and <literal>ChatroomParticipantParted(String
709 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
710 const gchar *property_name,
713 const GDBusPropertyInfo *info;
715 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
716 g_return_if_fail (property_name != NULL);
720 info = lookup_property_info_or_warn (proxy, property_name);
723 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
725 g_warning ("Trying to set property %s of type %s but according to the expected "
726 "interface the type is %s",
728 g_variant_get_type_string (value),
733 g_hash_table_insert (proxy->priv->properties,
734 g_strdup (property_name),
735 g_variant_ref_sink (value));
739 g_hash_table_remove (proxy->priv->properties, property_name);
746 /* ---------------------------------------------------------------------------------------------------- */
749 on_signal_received (GDBusConnection *connection,
750 const gchar *sender_name,
751 const gchar *object_path,
752 const gchar *interface_name,
753 const gchar *signal_name,
754 GVariant *parameters,
757 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
759 if (!proxy->priv->initialized)
762 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
765 if (proxy->priv->expected_interface != NULL)
767 const GDBusSignalInfo *info;
768 GVariantType *expected_type;
769 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
772 expected_type = _g_dbus_compute_complete_signature (info->args);
773 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
775 g_variant_type_free (expected_type);
778 g_variant_type_free (expected_type);
781 g_signal_emit (proxy,
782 signals[SIGNAL_SIGNAL],
791 /* ---------------------------------------------------------------------------------------------------- */
794 insert_property_checked (GDBusProxy *proxy,
795 gchar *property_name,
798 if (proxy->priv->expected_interface != NULL)
800 const GDBusPropertyInfo *info;
802 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
803 /* Ignore unknown properties */
807 /* Ignore properties with the wrong type */
808 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
812 g_hash_table_insert (proxy->priv->properties,
813 property_name, /* adopts string */
814 value); /* adopts value */
819 g_variant_unref (value);
820 g_free (property_name);
824 on_properties_changed (GDBusConnection *connection,
825 const gchar *sender_name,
826 const gchar *object_path,
827 const gchar *interface_name,
828 const gchar *signal_name,
829 GVariant *parameters,
832 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
833 const gchar *interface_name_for_signal;
834 GVariant *changed_properties;
835 gchar **invalidated_properties;
841 changed_properties = NULL;
842 invalidated_properties = NULL;
844 if (!proxy->priv->initialized)
847 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
850 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
852 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
853 g_variant_get_type_string (parameters));
857 g_variant_get (parameters,
859 &interface_name_for_signal,
861 &invalidated_properties);
863 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
866 g_variant_iter_init (&iter, changed_properties);
867 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
869 insert_property_checked (proxy,
870 key, /* adopts string */
871 value); /* adopts value */
874 for (n = 0; invalidated_properties[n] != NULL; n++)
876 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
880 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
883 invalidated_properties);
886 if (changed_properties != NULL)
887 g_variant_unref (changed_properties);
888 g_free (invalidated_properties);
891 /* ---------------------------------------------------------------------------------------------------- */
894 process_get_all_reply (GDBusProxy *proxy,
901 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
903 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
904 g_variant_get_type_string (result));
908 g_variant_get (result, "(a{sv})", &iter);
909 while (g_variant_iter_next (iter, "{sv}", &key, &value))
911 insert_property_checked (proxy,
912 key, /* adopts string */
913 value); /* adopts value */
915 g_variant_iter_free (iter);
917 /* Synthesize ::g-properties-changed changed */
918 if (g_hash_table_size (proxy->priv->properties) > 0)
920 GVariant *changed_properties;
921 const gchar *invalidated_properties[1] = {NULL};
923 g_variant_get (result,
925 &changed_properties);
926 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
929 invalidated_properties);
930 g_variant_unref (changed_properties);
940 GCancellable *cancellable;
942 } LoadPropertiesOnNameOwnerChangedData;
945 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
949 LoadPropertiesOnNameOwnerChangedData *data = user_data;
957 result = g_dbus_connection_call_finish (connection,
962 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
964 /* We just ignore if GetAll() is failing. Because this might happen
965 * if the object has no properties at all. Or if the caller is
966 * not authorized to see the properties.
968 * Either way, apps can know about this by using
969 * get_cached_property_names() or get_cached_property().
971 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
972 * fact that GetAll() failed
974 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
975 g_error_free (error);
978 /* and finally we can notify */
981 g_free (data->proxy->priv->name_owner);
982 data->proxy->priv->name_owner = data->name_owner;
983 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
985 g_hash_table_remove_all (data->proxy->priv->properties);
988 process_get_all_reply (data->proxy, result);
989 g_variant_unref (result);
992 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
995 if (data->cancellable == data->proxy->priv->get_all_cancellable)
996 data->proxy->priv->get_all_cancellable = NULL;
998 g_object_unref (data->proxy);
999 g_object_unref (data->cancellable);
1000 g_free (data->name_owner);
1005 on_name_owner_changed (GDBusConnection *connection,
1006 const gchar *sender_name,
1007 const gchar *object_path,
1008 const gchar *interface_name,
1009 const gchar *signal_name,
1010 GVariant *parameters,
1013 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
1014 const gchar *old_owner;
1015 const gchar *new_owner;
1017 /* if we are already trying to load properties, cancel that */
1018 if (proxy->priv->get_all_cancellable != NULL)
1020 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1021 proxy->priv->get_all_cancellable = NULL;
1024 g_variant_get (parameters,
1030 if (strlen (new_owner) == 0)
1032 g_free (proxy->priv->name_owner);
1033 proxy->priv->name_owner = NULL;
1035 /* Synthesize ::g-properties-changed changed */
1036 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1037 g_hash_table_size (proxy->priv->properties) > 0)
1039 GVariantBuilder builder;
1040 GPtrArray *invalidated_properties;
1041 GHashTableIter iter;
1044 /* Build changed_properties (always empty) and invalidated_properties ... */
1045 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1047 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1048 g_hash_table_iter_init (&iter, proxy->priv->properties);
1049 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1050 g_ptr_array_add (invalidated_properties, g_strdup (key));
1051 g_ptr_array_add (invalidated_properties, NULL);
1053 /* ... throw out the properties ... */
1054 g_hash_table_remove_all (proxy->priv->properties);
1056 /* ... and finally emit the ::g-properties-changed signal */
1057 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1059 g_variant_builder_end (&builder) /* consumed */,
1060 (const gchar* const *) invalidated_properties->pdata);
1061 g_ptr_array_unref (invalidated_properties);
1063 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1067 /* ignore duplicates - this can happen when activating the service */
1068 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1071 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1073 g_free (proxy->priv->name_owner);
1074 proxy->priv->name_owner = g_strdup (new_owner);
1075 g_hash_table_remove_all (proxy->priv->properties);
1076 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1080 LoadPropertiesOnNameOwnerChangedData *data;
1082 /* start loading properties.. only then emit notify::g-name-owner .. we
1083 * need to be able to cancel this in the event another NameOwnerChanged
1084 * signal suddenly happens
1087 g_assert (proxy->priv->get_all_cancellable == NULL);
1088 proxy->priv->get_all_cancellable = g_cancellable_new ();
1089 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1090 data->proxy = g_object_ref (proxy);
1091 data->cancellable = proxy->priv->get_all_cancellable;
1092 data->name_owner = g_strdup (new_owner);
1093 g_dbus_connection_call (proxy->priv->connection,
1095 proxy->priv->object_path,
1096 "org.freedesktop.DBus.Properties",
1098 g_variant_new ("(s)", proxy->priv->interface_name),
1099 G_VARIANT_TYPE ("(a{sv})"),
1100 G_DBUS_CALL_FLAGS_NONE,
1102 proxy->priv->get_all_cancellable,
1103 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1112 /* ---------------------------------------------------------------------------------------------------- */
1117 GCancellable *cancellable;
1118 GSimpleAsyncResult *simple;
1122 async_init_data_free (AsyncInitData *data)
1124 g_object_unref (data->proxy);
1125 if (data->cancellable != NULL)
1126 g_object_unref (data->cancellable);
1127 g_object_unref (data->simple);
1132 async_init_get_all_cb (GDBusConnection *connection,
1136 AsyncInitData *data = user_data;
1141 result = g_dbus_connection_call_finish (connection,
1146 /* We just ignore if GetAll() is failing. Because this might happen
1147 * if the object has no properties at all. Or if the caller is
1148 * not authorized to see the properties.
1150 * Either way, apps can know about this by using
1151 * get_cached_property_names() or get_cached_property().
1153 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1154 * fact that GetAll() failed
1156 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1157 g_error_free (error);
1161 g_simple_async_result_set_op_res_gpointer (data->simple,
1163 (GDestroyNotify) g_variant_unref);
1166 g_simple_async_result_complete_in_idle (data->simple);
1167 async_init_data_free (data);
1172 async_init_get_name_owner_cb (GDBusConnection *connection,
1176 AsyncInitData *data = user_data;
1184 result = g_dbus_connection_call_finish (connection,
1189 if (error->domain == G_DBUS_ERROR &&
1190 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1192 g_error_free (error);
1196 g_simple_async_result_take_error (data->simple, error);
1197 g_simple_async_result_complete_in_idle (data->simple);
1198 async_init_data_free (data);
1204 g_variant_get (result,
1206 &data->proxy->priv->name_owner);
1207 g_variant_unref (result);
1211 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1213 /* load all properties asynchronously */
1214 g_dbus_connection_call (data->proxy->priv->connection,
1215 data->proxy->priv->name_owner,
1216 data->proxy->priv->object_path,
1217 "org.freedesktop.DBus.Properties",
1219 g_variant_new ("(s)", data->proxy->priv->interface_name),
1220 G_VARIANT_TYPE ("(a{sv})"),
1221 G_DBUS_CALL_FLAGS_NONE,
1224 (GAsyncReadyCallback) async_init_get_all_cb,
1229 g_simple_async_result_complete_in_idle (data->simple);
1230 async_init_data_free (data);
1238 async_init_call_get_name_owner (AsyncInitData *data)
1240 g_dbus_connection_call (data->proxy->priv->connection,
1241 "org.freedesktop.DBus", /* name */
1242 "/org/freedesktop/DBus", /* object path */
1243 "org.freedesktop.DBus", /* interface */
1245 g_variant_new ("(s)",
1246 data->proxy->priv->name),
1247 G_VARIANT_TYPE ("(s)"),
1248 G_DBUS_CALL_FLAGS_NONE,
1251 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1256 async_init_start_service_by_name_cb (GDBusConnection *connection,
1260 AsyncInitData *data = user_data;
1265 result = g_dbus_connection_call_finish (connection,
1270 /* Errors are not unexpected; the bus will reply e.g.
1272 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1273 * was not provided by any .service files
1275 * This doesn't mean that the name doesn't have an owner, just
1276 * that it's not provided by a .service file. So just proceed to
1277 * invoke GetNameOwner() if dealing with that error.
1279 if (error->domain == G_DBUS_ERROR &&
1280 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1282 g_error_free (error);
1286 g_prefix_error (&error,
1287 _("Error calling StartServiceByName for %s: "),
1288 data->proxy->priv->name);
1294 guint32 start_service_result;
1295 g_variant_get (result,
1297 &start_service_result);
1298 g_variant_unref (result);
1299 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1300 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1302 /* continue to invoke GetNameOwner() */
1306 error = g_error_new (G_IO_ERROR,
1308 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1309 start_service_result,
1310 data->proxy->priv->name);
1315 async_init_call_get_name_owner (data);
1319 g_warn_if_fail (error != NULL);
1320 g_simple_async_result_take_error (data->simple, error);
1321 g_simple_async_result_complete_in_idle (data->simple);
1322 async_init_data_free (data);
1326 async_init_call_start_service_by_name (AsyncInitData *data)
1328 g_dbus_connection_call (data->proxy->priv->connection,
1329 "org.freedesktop.DBus", /* name */
1330 "/org/freedesktop/DBus", /* object path */
1331 "org.freedesktop.DBus", /* interface */
1332 "StartServiceByName",
1333 g_variant_new ("(su)",
1334 data->proxy->priv->name,
1336 G_VARIANT_TYPE ("(u)"),
1337 G_DBUS_CALL_FLAGS_NONE,
1340 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1345 async_initable_init_second_async (GAsyncInitable *initable,
1347 GCancellable *cancellable,
1348 GAsyncReadyCallback callback,
1351 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1352 AsyncInitData *data;
1354 data = g_new0 (AsyncInitData, 1);
1355 data->proxy = g_object_ref (proxy);
1356 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1357 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1362 /* Check name ownership asynchronously - possibly also start the service */
1363 if (proxy->priv->name == NULL)
1366 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1368 else if (g_dbus_is_unique_name (proxy->priv->name))
1370 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1371 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1375 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1377 async_init_call_get_name_owner (data);
1381 async_init_call_start_service_by_name (data);
1387 async_initable_init_second_finish (GAsyncInitable *initable,
1391 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1392 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1398 if (g_simple_async_result_propagate_error (simple, error))
1401 result = g_simple_async_result_get_op_res_gpointer (simple);
1404 process_get_all_reply (proxy, result);
1410 proxy->priv->initialized = TRUE;
1414 /* ---------------------------------------------------------------------------------------------------- */
1417 async_initable_init_first (GAsyncInitable *initable)
1419 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1421 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1423 /* subscribe to PropertiesChanged() */
1424 proxy->priv->properties_changed_subscriber_id =
1425 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1427 "org.freedesktop.DBus.Properties",
1428 "PropertiesChanged",
1429 proxy->priv->object_path,
1430 proxy->priv->interface_name,
1431 G_DBUS_SIGNAL_FLAGS_NONE,
1432 on_properties_changed,
1437 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1439 /* subscribe to all signals for the object */
1440 proxy->priv->signals_subscriber_id =
1441 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1443 proxy->priv->interface_name,
1445 proxy->priv->object_path,
1447 G_DBUS_SIGNAL_FLAGS_NONE,
1453 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1455 proxy->priv->name_owner_changed_subscription_id =
1456 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1457 "org.freedesktop.DBus", /* name */
1458 "org.freedesktop.DBus", /* interface */
1459 "NameOwnerChanged", /* signal name */
1460 "/org/freedesktop/DBus", /* path */
1461 proxy->priv->name, /* arg0 */
1462 G_DBUS_SIGNAL_FLAGS_NONE,
1463 on_name_owner_changed,
1469 /* ---------------------------------------------------------------------------------------------------- */
1471 /* initialization is split into two parts - the first is the
1472 * non-blocing part that requires the callers GMainContext - the
1473 * second is a blocking part async part that doesn't require the
1474 * callers GMainContext.. we do this split so the code can be reused
1475 * in the GInitable implementation below.
1477 * Note that obtaining a GDBusConnection is not shared between the two
1485 GCancellable *cancellable;
1486 GAsyncReadyCallback callback;
1488 } GetConnectionData;
1491 get_connection_cb (GObject *source_object,
1495 GetConnectionData *data = user_data;
1499 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1500 if (data->proxy->priv->connection == NULL)
1502 GSimpleAsyncResult *simple;
1503 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1507 g_simple_async_result_take_error (simple, error);
1508 g_simple_async_result_complete_in_idle (simple);
1509 g_object_unref (simple);
1513 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1514 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1521 if (data->cancellable != NULL)
1522 g_object_unref (data->cancellable);
1523 if (data->proxy != NULL)
1524 g_object_unref (data->proxy);
1529 async_initable_init_async (GAsyncInitable *initable,
1531 GCancellable *cancellable,
1532 GAsyncReadyCallback callback,
1535 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1537 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1539 GetConnectionData *data;
1541 g_assert (proxy->priv->connection == NULL);
1543 data = g_new0 (GetConnectionData, 1);
1544 data->proxy = g_object_ref (proxy);
1545 data->io_priority = io_priority;
1546 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1547 data->callback = callback;
1548 data->user_data = user_data;
1549 g_bus_get (proxy->priv->bus_type,
1556 async_initable_init_first (initable);
1557 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1562 async_initable_init_finish (GAsyncInitable *initable,
1566 return async_initable_init_second_finish (initable, res, error);
1570 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1572 async_initable_iface->init_async = async_initable_init_async;
1573 async_initable_iface->init_finish = async_initable_init_finish;
1576 /* ---------------------------------------------------------------------------------------------------- */
1580 GMainContext *context;
1583 } InitableAsyncInitableData;
1586 async_initable_init_async_cb (GObject *source_object,
1590 InitableAsyncInitableData *data = user_data;
1591 data->res = g_object_ref (res);
1592 g_main_loop_quit (data->loop);
1595 /* Simply reuse the GAsyncInitable implementation but run the first
1596 * part (that is non-blocking and requires the callers GMainContext)
1597 * with the callers GMainContext.. and the second with a private
1598 * GMainContext (bug 621310 is slightly related).
1600 * Note that obtaining a GDBusConnection is not shared between the two
1604 initable_init (GInitable *initable,
1605 GCancellable *cancellable,
1608 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1609 InitableAsyncInitableData *data;
1614 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1616 g_assert (proxy->priv->connection == NULL);
1617 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1620 if (proxy->priv->connection == NULL)
1624 async_initable_init_first (G_ASYNC_INITABLE (initable));
1626 data = g_new0 (InitableAsyncInitableData, 1);
1627 data->context = g_main_context_new ();
1628 data->loop = g_main_loop_new (data->context, FALSE);
1630 g_main_context_push_thread_default (data->context);
1632 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1635 async_initable_init_async_cb,
1638 g_main_loop_run (data->loop);
1640 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1644 g_main_context_pop_thread_default (data->context);
1646 g_main_context_unref (data->context);
1647 g_main_loop_unref (data->loop);
1648 g_object_unref (data->res);
1657 initable_iface_init (GInitableIface *initable_iface)
1659 initable_iface->init = initable_init;
1662 /* ---------------------------------------------------------------------------------------------------- */
1666 * @connection: A #GDBusConnection.
1667 * @flags: Flags used when constructing the proxy.
1668 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1669 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1670 * @object_path: An object path.
1671 * @interface_name: A D-Bus interface name.
1672 * @cancellable: A #GCancellable or %NULL.
1673 * @callback: Callback function to invoke when the proxy is ready.
1674 * @user_data: User data to pass to @callback.
1676 * Creates a proxy for accessing @interface_name on the remote object
1677 * at @object_path owned by @name at @connection and asynchronously
1678 * loads D-Bus properties unless the
1679 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1680 * the #GDBusProxy::g-properties-changed signal to get notified about
1683 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1684 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1685 * to handle signals from the remote object.
1687 * If @name is a well-known name and the
1688 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1689 * owner currently exists, the message bus will be requested to launch
1690 * a name owner for the name.
1692 * This is a failable asynchronous constructor - when the proxy is
1693 * ready, @callback will be invoked and you can use
1694 * g_dbus_proxy_new_finish() to get the result.
1696 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1698 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1703 g_dbus_proxy_new (GDBusConnection *connection,
1704 GDBusProxyFlags flags,
1705 GDBusInterfaceInfo *info,
1707 const gchar *object_path,
1708 const gchar *interface_name,
1709 GCancellable *cancellable,
1710 GAsyncReadyCallback callback,
1713 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1714 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1715 g_return_if_fail (g_variant_is_object_path (object_path));
1716 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1718 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1724 "g-interface-info", info,
1726 "g-connection", connection,
1727 "g-object-path", object_path,
1728 "g-interface-name", interface_name,
1733 * g_dbus_proxy_new_finish:
1734 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1735 * @error: Return location for error or %NULL.
1737 * Finishes creating a #GDBusProxy.
1739 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1744 g_dbus_proxy_new_finish (GAsyncResult *res,
1748 GObject *source_object;
1750 source_object = g_async_result_get_source_object (res);
1751 g_assert (source_object != NULL);
1753 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1756 g_object_unref (source_object);
1759 return G_DBUS_PROXY (object);
1765 * g_dbus_proxy_new_sync:
1766 * @connection: A #GDBusConnection.
1767 * @flags: Flags used when constructing the proxy.
1768 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1769 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1770 * @object_path: An object path.
1771 * @interface_name: A D-Bus interface name.
1772 * @cancellable: (allow-none): A #GCancellable or %NULL.
1773 * @error: (allow-none): Return location for error or %NULL.
1775 * Creates a proxy for accessing @interface_name on the remote object
1776 * at @object_path owned by @name at @connection and synchronously
1777 * loads D-Bus properties unless the
1778 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1780 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1781 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1782 * to handle signals from the remote object.
1784 * If @name is a well-known name and the
1785 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1786 * owner currently exists, the message bus will be requested to launch
1787 * a name owner for the name.
1789 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1790 * and g_dbus_proxy_new_finish() for the asynchronous version.
1792 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1794 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1799 g_dbus_proxy_new_sync (GDBusConnection *connection,
1800 GDBusProxyFlags flags,
1801 GDBusInterfaceInfo *info,
1803 const gchar *object_path,
1804 const gchar *interface_name,
1805 GCancellable *cancellable,
1808 GInitable *initable;
1810 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1811 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1812 g_dbus_is_name (name), NULL);
1813 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1814 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1816 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1820 "g-interface-info", info,
1822 "g-connection", connection,
1823 "g-object-path", object_path,
1824 "g-interface-name", interface_name,
1826 if (initable != NULL)
1827 return G_DBUS_PROXY (initable);
1832 /* ---------------------------------------------------------------------------------------------------- */
1835 * g_dbus_proxy_new_for_bus:
1836 * @bus_type: A #GBusType.
1837 * @flags: Flags used when constructing the proxy.
1838 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1839 * @name: A bus name (well-known or unique).
1840 * @object_path: An object path.
1841 * @interface_name: A D-Bus interface name.
1842 * @cancellable: A #GCancellable or %NULL.
1843 * @callback: Callback function to invoke when the proxy is ready.
1844 * @user_data: User data to pass to @callback.
1846 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1848 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1853 g_dbus_proxy_new_for_bus (GBusType bus_type,
1854 GDBusProxyFlags flags,
1855 GDBusInterfaceInfo *info,
1857 const gchar *object_path,
1858 const gchar *interface_name,
1859 GCancellable *cancellable,
1860 GAsyncReadyCallback callback,
1863 g_return_if_fail (g_dbus_is_name (name));
1864 g_return_if_fail (g_variant_is_object_path (object_path));
1865 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1867 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1873 "g-interface-info", info,
1875 "g-bus-type", bus_type,
1876 "g-object-path", object_path,
1877 "g-interface-name", interface_name,
1882 * g_dbus_proxy_new_for_bus_finish:
1883 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1884 * @error: Return location for error or %NULL.
1886 * Finishes creating a #GDBusProxy.
1888 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1893 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1896 return g_dbus_proxy_new_finish (res, error);
1900 * g_dbus_proxy_new_for_bus_sync:
1901 * @bus_type: A #GBusType.
1902 * @flags: Flags used when constructing the proxy.
1903 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
1904 * that @proxy conforms to or %NULL.
1905 * @name: A bus name (well-known or unique).
1906 * @object_path: An object path.
1907 * @interface_name: A D-Bus interface name.
1908 * @cancellable: A #GCancellable or %NULL.
1909 * @error: Return location for error or %NULL.
1911 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1913 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1915 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1920 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1921 GDBusProxyFlags flags,
1922 GDBusInterfaceInfo *info,
1924 const gchar *object_path,
1925 const gchar *interface_name,
1926 GCancellable *cancellable,
1929 GInitable *initable;
1931 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1932 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1933 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1935 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1939 "g-interface-info", info,
1941 "g-bus-type", bus_type,
1942 "g-object-path", object_path,
1943 "g-interface-name", interface_name,
1945 if (initable != NULL)
1946 return G_DBUS_PROXY (initable);
1951 /* ---------------------------------------------------------------------------------------------------- */
1954 * g_dbus_proxy_get_connection:
1955 * @proxy: A #GDBusProxy.
1957 * Gets the connection @proxy is for.
1959 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
1964 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1966 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1967 return proxy->priv->connection;
1971 * g_dbus_proxy_get_flags:
1972 * @proxy: A #GDBusProxy.
1974 * Gets the flags that @proxy was constructed with.
1976 * Returns: Flags from the #GDBusProxyFlags enumeration.
1981 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1983 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1984 return proxy->priv->flags;
1988 * g_dbus_proxy_get_name:
1989 * @proxy: A #GDBusProxy.
1991 * Gets the name that @proxy was constructed for.
1993 * Returns: A string owned by @proxy. Do not free.
1998 g_dbus_proxy_get_name (GDBusProxy *proxy)
2000 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2001 return proxy->priv->name;
2005 * g_dbus_proxy_get_name_owner:
2006 * @proxy: A #GDBusProxy.
2008 * The unique name that owns the name that @proxy is for or %NULL if
2009 * no-one currently owns that name. You may connect to the
2010 * #GObject::notify signal to track changes to the
2011 * #GDBusProxy:g-name-owner property.
2013 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2018 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2020 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2021 return g_strdup (proxy->priv->name_owner);
2025 * g_dbus_proxy_get_object_path:
2026 * @proxy: A #GDBusProxy.
2028 * Gets the object path @proxy is for.
2030 * Returns: A string owned by @proxy. Do not free.
2035 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2037 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2038 return proxy->priv->object_path;
2042 * g_dbus_proxy_get_interface_name:
2043 * @proxy: A #GDBusProxy.
2045 * Gets the D-Bus interface name @proxy is for.
2047 * Returns: A string owned by @proxy. Do not free.
2052 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2054 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2055 return proxy->priv->interface_name;
2059 * g_dbus_proxy_get_default_timeout:
2060 * @proxy: A #GDBusProxy.
2062 * Gets the timeout to use if -1 (specifying default timeout) is
2063 * passed as @timeout_msec in the g_dbus_proxy_call() and
2064 * g_dbus_proxy_call_sync() functions.
2066 * See the #GDBusProxy:g-default-timeout property for more details.
2068 * Returns: Timeout to use for @proxy.
2073 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2075 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2076 return proxy->priv->timeout_msec;
2080 * g_dbus_proxy_set_default_timeout:
2081 * @proxy: A #GDBusProxy.
2082 * @timeout_msec: Timeout in milliseconds.
2084 * Sets the timeout to use if -1 (specifying default timeout) is
2085 * passed as @timeout_msec in the g_dbus_proxy_call() and
2086 * g_dbus_proxy_call_sync() functions.
2088 * See the #GDBusProxy:g-default-timeout property for more details.
2093 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2096 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2097 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2099 /* TODO: locking? */
2100 if (proxy->priv->timeout_msec != timeout_msec)
2102 proxy->priv->timeout_msec = timeout_msec;
2103 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2108 * g_dbus_proxy_get_interface_info:
2109 * @proxy: A #GDBusProxy
2111 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2112 * interface that @proxy conforms to.
2114 * See the #GDBusProxy:g-interface-info property for more details.
2116 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2117 * object, it is owned by @proxy.
2121 GDBusInterfaceInfo *
2122 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2124 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2125 return proxy->priv->expected_interface;
2129 * g_dbus_proxy_set_interface_info:
2130 * @proxy: A #GDBusProxy
2131 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2133 * Ensure that interactions with @proxy conform to the given
2134 * interface. For example, when completing a method call, if the type
2135 * signature of the message isn't what's expected, the given #GError
2136 * is set. Signals that have a type signature mismatch are simply
2139 * See the #GDBusProxy:g-interface-info property for more details.
2144 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2145 GDBusInterfaceInfo *info)
2147 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2148 if (proxy->priv->expected_interface != NULL)
2150 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2151 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2153 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2154 if (proxy->priv->expected_interface != NULL)
2155 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2158 /* ---------------------------------------------------------------------------------------------------- */
2161 maybe_split_method_name (const gchar *method_name,
2162 gchar **out_interface_name,
2163 const gchar **out_method_name)
2168 g_assert (out_interface_name != NULL);
2169 g_assert (out_method_name != NULL);
2170 *out_interface_name = NULL;
2171 *out_method_name = NULL;
2173 if (strchr (method_name, '.') != NULL)
2178 p = g_strdup (method_name);
2179 last_dot = strrchr (p, '.');
2182 *out_interface_name = p;
2183 *out_method_name = last_dot + 1;
2193 reply_cb (GDBusConnection *connection,
2197 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2202 value = g_dbus_connection_call_finish (connection,
2207 g_simple_async_result_take_error (simple, error);
2211 g_simple_async_result_set_op_res_gpointer (simple,
2213 (GDestroyNotify) g_variant_unref);
2216 /* no need to complete in idle since the method GDBusConnection already does */
2217 g_simple_async_result_complete (simple);
2218 g_object_unref (simple);
2221 static const GDBusMethodInfo *
2222 lookup_method_info_or_warn (GDBusProxy *proxy,
2223 const gchar *method_name)
2225 const GDBusMethodInfo *info;
2227 if (proxy->priv->expected_interface == NULL)
2230 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2233 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2234 method_name, proxy->priv->expected_interface->name);
2240 static const gchar *
2241 get_destination_for_call (GDBusProxy *proxy)
2247 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2248 * is never NULL and always the same as proxy->priv->name. We use this
2249 * knowledge to avoid checking if proxy->priv->name is a unique or
2252 ret = proxy->priv->name_owner;
2256 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2259 ret = proxy->priv->name;
2266 * g_dbus_proxy_call:
2267 * @proxy: A #GDBusProxy.
2268 * @method_name: Name of method to invoke.
2269 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2270 * @flags: Flags from the #GDBusCallFlags enumeration.
2271 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2272 * "infinite") or -1 to use the proxy default timeout.
2273 * @cancellable: A #GCancellable or %NULL.
2274 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2275 * care about the result of the method invocation.
2276 * @user_data: The data to pass to @callback.
2278 * Asynchronously invokes the @method_name method on @proxy.
2280 * If @method_name contains any dots, then @name is split into interface and
2281 * method name parts. This allows using @proxy for invoking methods on
2284 * If the #GDBusConnection associated with @proxy is closed then
2285 * the operation will fail with %G_IO_ERROR_CLOSED. If
2286 * @cancellable is canceled, the operation will fail with
2287 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2288 * compatible with the D-Bus protocol, the operation fails with
2289 * %G_IO_ERROR_INVALID_ARGUMENT.
2291 * If the @parameters #GVariant is floating, it is consumed. This allows
2292 * convenient 'inline' use of g_variant_new(), e.g.:
2294 * g_dbus_proxy_call (proxy,
2296 * g_variant_new ("(ss)",
2299 * G_DBUS_CALL_FLAGS_NONE,
2302 * (GAsyncReadyCallback) two_strings_done,
2306 * This is an asynchronous method. When the operation is finished,
2307 * @callback will be invoked in the
2308 * <link linkend="g-main-context-push-thread-default">thread-default
2309 * main loop</link> of the thread you are calling this method from.
2310 * You can then call g_dbus_proxy_call_finish() to get the result of
2311 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2312 * version of this method.
2317 g_dbus_proxy_call (GDBusProxy *proxy,
2318 const gchar *method_name,
2319 GVariant *parameters,
2320 GDBusCallFlags flags,
2322 GCancellable *cancellable,
2323 GAsyncReadyCallback callback,
2326 GSimpleAsyncResult *simple;
2328 gchar *split_interface_name;
2329 const gchar *split_method_name;
2330 const gchar *target_method_name;
2331 const gchar *target_interface_name;
2332 const gchar *destination;
2333 GVariantType *reply_type;
2335 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2336 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2337 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2338 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2341 split_interface_name = NULL;
2343 simple = g_simple_async_result_new (G_OBJECT (proxy),
2348 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2349 target_method_name = was_split ? split_method_name : method_name;
2350 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2352 /* Warn if method is unexpected (cf. :g-interface-info) */
2355 const GDBusMethodInfo *expected_method_info;
2356 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2357 if (expected_method_info != NULL)
2358 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2362 if (proxy->priv->name != NULL)
2364 destination = get_destination_for_call (proxy);
2365 if (destination == NULL)
2367 g_simple_async_result_set_error (simple,
2370 _("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"));
2375 g_dbus_connection_call (proxy->priv->connection,
2377 proxy->priv->object_path,
2378 target_interface_name,
2383 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2385 (GAsyncReadyCallback) reply_cb,
2389 if (reply_type != NULL)
2390 g_variant_type_free (reply_type);
2392 g_free (split_interface_name);
2396 * g_dbus_proxy_call_finish:
2397 * @proxy: A #GDBusProxy.
2398 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2399 * @error: Return location for error or %NULL.
2401 * Finishes an operation started with g_dbus_proxy_call().
2403 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2404 * return values. Free with g_variant_unref().
2409 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2413 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2416 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2417 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2418 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2420 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2424 if (g_simple_async_result_propagate_error (simple, error))
2427 value = g_variant_ref (g_simple_async_result_get_op_res_gpointer (simple));
2434 * g_dbus_proxy_call_sync:
2435 * @proxy: A #GDBusProxy.
2436 * @method_name: Name of method to invoke.
2437 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2438 * or %NULL if not passing parameters.
2439 * @flags: Flags from the #GDBusCallFlags enumeration.
2440 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2441 * "infinite") or -1 to use the proxy default timeout.
2442 * @cancellable: A #GCancellable or %NULL.
2443 * @error: Return location for error or %NULL.
2445 * Synchronously invokes the @method_name method on @proxy.
2447 * If @method_name contains any dots, then @name is split into interface and
2448 * method name parts. This allows using @proxy for invoking methods on
2451 * If the #GDBusConnection associated with @proxy is disconnected then
2452 * the operation will fail with %G_IO_ERROR_CLOSED. If
2453 * @cancellable is canceled, the operation will fail with
2454 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2455 * compatible with the D-Bus protocol, the operation fails with
2456 * %G_IO_ERROR_INVALID_ARGUMENT.
2458 * If the @parameters #GVariant is floating, it is consumed. This allows
2459 * convenient 'inline' use of g_variant_new(), e.g.:
2461 * g_dbus_proxy_call_sync (proxy,
2463 * g_variant_new ("(ss)",
2466 * G_DBUS_CALL_FLAGS_NONE,
2472 * The calling thread is blocked until a reply is received. See
2473 * g_dbus_proxy_call() for the asynchronous version of this
2476 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2477 * return values. Free with g_variant_unref().
2482 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2483 const gchar *method_name,
2484 GVariant *parameters,
2485 GDBusCallFlags flags,
2487 GCancellable *cancellable,
2492 gchar *split_interface_name;
2493 const gchar *split_method_name;
2494 const gchar *target_method_name;
2495 const gchar *target_interface_name;
2496 const gchar *destination;
2497 GVariantType *reply_type;
2499 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2500 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2501 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2502 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2503 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2507 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2508 target_method_name = was_split ? split_method_name : method_name;
2509 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2511 /* Warn if method is unexpected (cf. :g-interface-info) */
2514 const GDBusMethodInfo *expected_method_info;
2515 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2516 if (expected_method_info != NULL)
2517 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2521 if (proxy->priv->name != NULL)
2523 destination = get_destination_for_call (proxy);
2524 if (destination == NULL)
2526 g_set_error_literal (error,
2529 _("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"));
2535 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2537 proxy->priv->object_path,
2538 target_interface_name,
2543 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2548 if (reply_type != NULL)
2549 g_variant_type_free (reply_type);
2551 g_free (split_interface_name);
2556 /* ---------------------------------------------------------------------------------------------------- */
2558 static GDBusInterfaceInfo *
2559 _g_dbus_proxy_get_info (GDBusInterface *interface)
2561 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2562 return g_dbus_proxy_get_interface_info (proxy);
2565 static GDBusObject *
2566 _g_dbus_proxy_get_object (GDBusInterface *interface)
2568 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2569 return proxy->priv->object;
2573 _g_dbus_proxy_set_object (GDBusInterface *interface,
2574 GDBusObject *object)
2576 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2577 if (proxy->priv->object != NULL)
2578 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2579 proxy->priv->object = object;
2580 if (proxy->priv->object != NULL)
2581 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2585 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2587 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
2588 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2589 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2592 /* ---------------------------------------------------------------------------------------------------- */