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 and
65 * calls are always sent to the current name owner. This avoids a
66 * number of race conditions when the name is lost by one owner and
67 * claimed by another. However, if no name owner currently exists,
68 * then calls will be sent to the well-known name which may result in
69 * the message bus launching an owner (unless
70 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
72 * The generic #GDBusProxy::g-properties-changed and #GDBusProxy::g-signal
73 * signals are not very convenient to work with. Therefore, the recommended
74 * way of working with proxies is to subclass #GDBusProxy, and have
75 * more natural properties and signals in your derived class.
77 * See <xref linkend="gdbus-example-proxy-subclass"/> for an example.
79 * <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>
82 struct _GDBusProxyPrivate
85 GDBusConnection *connection;
87 GDBusProxyFlags flags;
91 gchar *interface_name;
94 guint name_owner_changed_subscription_id;
96 GCancellable *get_all_cancellable;
98 /* gchar* -> GVariant* */
99 GHashTable *properties;
101 GDBusInterfaceInfo *expected_interface;
103 guint properties_changed_subscriber_id;
104 guint signals_subscriber_id;
106 gboolean initialized;
118 PROP_G_INTERFACE_NAME,
119 PROP_G_DEFAULT_TIMEOUT,
120 PROP_G_INTERFACE_INFO
125 PROPERTIES_CHANGED_SIGNAL,
130 guint signals[LAST_SIGNAL] = {0};
132 static void initable_iface_init (GInitableIface *initable_iface);
133 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
135 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
136 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
137 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
141 g_dbus_proxy_finalize (GObject *object)
143 GDBusProxy *proxy = G_DBUS_PROXY (object);
145 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
147 if (proxy->priv->name_owner_changed_subscription_id > 0)
148 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
149 proxy->priv->name_owner_changed_subscription_id);
151 if (proxy->priv->properties_changed_subscriber_id > 0)
152 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
153 proxy->priv->properties_changed_subscriber_id);
155 if (proxy->priv->signals_subscriber_id > 0)
156 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
157 proxy->priv->signals_subscriber_id);
159 g_object_unref (proxy->priv->connection);
160 g_free (proxy->priv->name);
161 g_free (proxy->priv->name_owner);
162 g_free (proxy->priv->object_path);
163 g_free (proxy->priv->interface_name);
164 if (proxy->priv->properties != NULL)
165 g_hash_table_unref (proxy->priv->properties);
167 if (proxy->priv->expected_interface != NULL)
168 g_dbus_interface_info_unref (proxy->priv->expected_interface);
170 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
174 g_dbus_proxy_get_property (GObject *object,
179 GDBusProxy *proxy = G_DBUS_PROXY (object);
183 case PROP_G_CONNECTION:
184 g_value_set_object (value, proxy->priv->connection);
188 g_value_set_flags (value, proxy->priv->flags);
192 g_value_set_string (value, proxy->priv->name);
195 case PROP_G_NAME_OWNER:
196 g_value_set_string (value, proxy->priv->name_owner);
199 case PROP_G_OBJECT_PATH:
200 g_value_set_string (value, proxy->priv->object_path);
203 case PROP_G_INTERFACE_NAME:
204 g_value_set_string (value, proxy->priv->interface_name);
207 case PROP_G_DEFAULT_TIMEOUT:
208 g_value_set_int (value, proxy->priv->timeout_msec);
211 case PROP_G_INTERFACE_INFO:
212 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 g_dbus_proxy_set_property (GObject *object,
227 GDBusProxy *proxy = G_DBUS_PROXY (object);
231 case PROP_G_CONNECTION:
232 proxy->priv->connection = g_value_dup_object (value);
236 proxy->priv->flags = g_value_get_flags (value);
240 proxy->priv->name = g_value_dup_string (value);
243 case PROP_G_OBJECT_PATH:
244 proxy->priv->object_path = g_value_dup_string (value);
247 case PROP_G_INTERFACE_NAME:
248 proxy->priv->interface_name = g_value_dup_string (value);
251 case PROP_G_DEFAULT_TIMEOUT:
252 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
255 case PROP_G_INTERFACE_INFO:
256 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
259 case PROP_G_BUS_TYPE:
260 proxy->priv->bus_type = g_value_get_enum (value);
264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 g_dbus_proxy_class_init (GDBusProxyClass *klass)
272 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
274 gobject_class->finalize = g_dbus_proxy_finalize;
275 gobject_class->set_property = g_dbus_proxy_set_property;
276 gobject_class->get_property = g_dbus_proxy_get_property;
278 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
279 * in derived classes */
282 * GDBusProxy:g-interface-info:
284 * Ensure that interactions with this proxy conform to the given
285 * interface. For example, when completing a method call, if the
286 * type signature of the message isn't what's expected, the given
287 * #GError is set. Signals that have a type signature mismatch are
292 g_object_class_install_property (gobject_class,
293 PROP_G_INTERFACE_INFO,
294 g_param_spec_boxed ("g-interface-info",
295 P_("Interface Information"),
296 P_("Interface Information"),
297 G_TYPE_DBUS_INTERFACE_INFO,
300 G_PARAM_STATIC_NAME |
301 G_PARAM_STATIC_BLURB |
302 G_PARAM_STATIC_NICK));
305 * GDBusProxy:g-connection:
307 * The #GDBusConnection the proxy is for.
311 g_object_class_install_property (gobject_class,
313 g_param_spec_object ("g-connection",
315 P_("The connection the proxy is for"),
316 G_TYPE_DBUS_CONNECTION,
319 G_PARAM_CONSTRUCT_ONLY |
320 G_PARAM_STATIC_NAME |
321 G_PARAM_STATIC_BLURB |
322 G_PARAM_STATIC_NICK));
325 * GDBusProxy:g-bus-type:
327 * If this property is not %G_BUS_TYPE_NONE, then
328 * #GDBusProxy:g-connection must be %NULL and will be set to the
329 * #GDBusConnection obtained by calling g_bus_get() with the value
334 g_object_class_install_property (gobject_class,
336 g_param_spec_enum ("g-bus-type",
338 P_("The bus to connect to, if any"),
342 G_PARAM_CONSTRUCT_ONLY |
343 G_PARAM_STATIC_NAME |
344 G_PARAM_STATIC_BLURB |
345 G_PARAM_STATIC_NICK));
348 * GDBusProxy:g-flags:
350 * Flags from the #GDBusProxyFlags enumeration.
354 g_object_class_install_property (gobject_class,
356 g_param_spec_flags ("g-flags",
358 P_("Flags for the proxy"),
359 G_TYPE_DBUS_PROXY_FLAGS,
360 G_DBUS_PROXY_FLAGS_NONE,
363 G_PARAM_CONSTRUCT_ONLY |
364 G_PARAM_STATIC_NAME |
365 G_PARAM_STATIC_BLURB |
366 G_PARAM_STATIC_NICK));
371 * The well-known or unique name that the proxy is for.
375 g_object_class_install_property (gobject_class,
377 g_param_spec_string ("g-name",
379 P_("The well-known or unique name that the proxy is for"),
383 G_PARAM_CONSTRUCT_ONLY |
384 G_PARAM_STATIC_NAME |
385 G_PARAM_STATIC_BLURB |
386 G_PARAM_STATIC_NICK));
389 * GDBusProxy:g-name-owner:
391 * The unique name that owns #GDBusProxy:name or %NULL if no-one
392 * currently owns that name. You may connect to #GObject::notify signal to
393 * track changes to this property.
397 g_object_class_install_property (gobject_class,
399 g_param_spec_string ("g-name-owner",
401 P_("The unique name for the owner"),
404 G_PARAM_STATIC_NAME |
405 G_PARAM_STATIC_BLURB |
406 G_PARAM_STATIC_NICK));
409 * GDBusProxy:g-object-path:
411 * The object path the proxy is for.
415 g_object_class_install_property (gobject_class,
417 g_param_spec_string ("g-object-path",
419 P_("The object path the proxy is for"),
423 G_PARAM_CONSTRUCT_ONLY |
424 G_PARAM_STATIC_NAME |
425 G_PARAM_STATIC_BLURB |
426 G_PARAM_STATIC_NICK));
429 * GDBusProxy:g-interface-name:
431 * The D-Bus interface name the proxy is for.
435 g_object_class_install_property (gobject_class,
436 PROP_G_INTERFACE_NAME,
437 g_param_spec_string ("g-interface-name",
438 P_("g-interface-name"),
439 P_("The D-Bus interface name the proxy is for"),
443 G_PARAM_CONSTRUCT_ONLY |
444 G_PARAM_STATIC_NAME |
445 G_PARAM_STATIC_BLURB |
446 G_PARAM_STATIC_NICK));
449 * GDBusProxy:g-default-timeout:
451 * The timeout to use if -1 (specifying default timeout) is passed
452 * as @timeout_msec in the g_dbus_proxy_call() and
453 * g_dbus_proxy_call_sync() functions.
455 * This allows applications to set a proxy-wide timeout for all
456 * remote method invocations on the proxy. If this property is -1,
457 * the default timeout (typically 25 seconds) is used. If set to
458 * %G_MAXINT, then no timeout is used.
462 g_object_class_install_property (gobject_class,
463 PROP_G_DEFAULT_TIMEOUT,
464 g_param_spec_int ("g-default-timeout",
465 P_("Default Timeout"),
466 P_("Timeout for remote method invocation"),
473 G_PARAM_STATIC_NAME |
474 G_PARAM_STATIC_BLURB |
475 G_PARAM_STATIC_NICK));
478 * GDBusProxy::g-properties-changed:
479 * @proxy: The #GDBusProxy emitting the signal.
480 * @changed_properties: A #GVariant containing the properties that changed
481 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
483 * Emitted when one or more D-Bus properties on @proxy changes. The
484 * local cache has already been updated when this signal fires. Note
485 * that both @changed_properties and @invalidated_properties are
486 * guaranteed to never be %NULL (either may be empty though).
488 * This signal corresponds to the
489 * <literal>PropertiesChanged</literal> D-Bus signal on the
490 * <literal>org.freedesktop.DBus.Properties</literal> interface.
494 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
497 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
500 _gio_marshal_VOID__VARIANT_BOXED,
504 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
507 * GDBusProxy::g-signal:
508 * @proxy: The #GDBusProxy emitting the signal.
509 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
510 * @signal_name: The name of the signal.
511 * @parameters: A #GVariant tuple with parameters for the signal.
513 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
517 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
520 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
523 _gio_marshal_VOID__STRING_STRING_VARIANT,
531 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
535 g_dbus_proxy_init (GDBusProxy *proxy)
537 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
538 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
541 (GDestroyNotify) g_variant_unref);
544 /* ---------------------------------------------------------------------------------------------------- */
547 property_name_sort_func (const gchar **a,
550 return g_strcmp0 (*a, *b);
554 * g_dbus_proxy_get_cached_property_names:
555 * @proxy: A #GDBusProxy.
557 * Gets the names of all cached properties on @proxy.
559 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
560 * no cached properties. Free the returned array with g_strfreev().
565 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
572 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
575 if (g_hash_table_size (proxy->priv->properties) == 0)
578 p = g_ptr_array_new ();
580 g_hash_table_iter_init (&iter, proxy->priv->properties);
581 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
582 g_ptr_array_add (p, g_strdup (key));
583 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
584 g_ptr_array_add (p, NULL);
586 names = (gchar **) g_ptr_array_free (p, FALSE);
592 static const GDBusPropertyInfo *
593 lookup_property_info_or_warn (GDBusProxy *proxy,
594 const gchar *property_name)
596 const GDBusPropertyInfo *info;
598 if (proxy->priv->expected_interface == NULL)
601 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
604 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
606 proxy->priv->expected_interface->name);
613 * g_dbus_proxy_get_cached_property:
614 * @proxy: A #GDBusProxy.
615 * @property_name: Property name.
617 * Looks up the value for a property from the cache. This call does no
620 * If @proxy has an expected interface (see
621 * #GDBusProxy:g-interface-info), then @property_name (for existence)
622 * is checked against it.
624 * Returns: A reference to the #GVariant instance that holds the value
625 * for @property_name or %NULL if the value is not in the cache. The
626 * returned reference must be freed with g_variant_unref().
631 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
632 const gchar *property_name)
636 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
637 g_return_val_if_fail (property_name != NULL, NULL);
639 value = g_hash_table_lookup (proxy->priv->properties, property_name);
642 const GDBusPropertyInfo *info;
643 info = lookup_property_info_or_warn (proxy, property_name);
648 g_variant_ref (value);
655 * g_dbus_proxy_set_cached_property:
656 * @proxy: A #GDBusProxy
657 * @property_name: Property name.
658 * @value: Value for the property or %NULL to remove it from the cache.
660 * If @value is not %NULL, sets the cached value for the property with
661 * name @property_name to the value in @value.
663 * If @value is %NULL, then the cached value is removed from the
666 * If @proxy has an expected interface (see
667 * #GDBusProxy:g-interface-info), then @property_name (for existence)
668 * and @value (for the type) is checked against it.
670 * If the @value #GVariant is floating, it is consumed. This allows
671 * convenient 'inline' use of g_variant_new(), e.g.
673 * g_dbus_proxy_set_cached_property (proxy,
675 * g_variant_new ("(si)",
680 * Normally you will not need to use this method since @proxy is
681 * tracking changes using the
682 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
683 * D-Bus signal. However, for performance reasons an object may decide
684 * to not use this signal for some properties and instead use a
685 * proprietary out-of-band mechanism to transmit changes.
687 * As a concrete example, consider an object with a property
688 * <literal>ChatroomParticipants</literal> which is an array of
689 * strings. Instead of transmitting the same (long) array every time
690 * the property changes, it is more efficient to only transmit the
691 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
692 * name)</literal> and <literal>ChatroomParticipantParted(String
698 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
699 const gchar *property_name,
702 const GDBusPropertyInfo *info;
704 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
705 g_return_if_fail (property_name != NULL);
709 info = lookup_property_info_or_warn (proxy, property_name);
712 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
714 g_warning (_("Trying to set property %s of type %s but according to the expected "
715 "interface the type is %s"),
717 g_variant_get_type_string (value),
722 g_hash_table_insert (proxy->priv->properties,
723 g_strdup (property_name),
724 g_variant_ref_sink (value));
728 g_hash_table_remove (proxy->priv->properties, property_name);
735 /* ---------------------------------------------------------------------------------------------------- */
738 on_signal_received (GDBusConnection *connection,
739 const gchar *sender_name,
740 const gchar *object_path,
741 const gchar *interface_name,
742 const gchar *signal_name,
743 GVariant *parameters,
746 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
748 if (!proxy->priv->initialized)
751 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
754 g_signal_emit (proxy,
755 signals[SIGNAL_SIGNAL],
764 /* ---------------------------------------------------------------------------------------------------- */
767 on_properties_changed (GDBusConnection *connection,
768 const gchar *sender_name,
769 const gchar *object_path,
770 const gchar *interface_name,
771 const gchar *signal_name,
772 GVariant *parameters,
775 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
777 const gchar *interface_name_for_signal;
778 GVariant *changed_properties;
779 gchar **invalidated_properties;
786 changed_properties = NULL;
787 invalidated_properties = NULL;
789 if (!proxy->priv->initialized)
792 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
795 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
797 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
798 g_variant_get_type_string (parameters));
802 g_variant_get (parameters,
804 &interface_name_for_signal,
806 &invalidated_properties);
808 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
811 g_variant_iter_init (&iter, changed_properties);
812 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
814 g_hash_table_insert (proxy->priv->properties,
815 key, /* adopts string */
816 value); /* adopts value */
819 for (n = 0; invalidated_properties[n] != NULL; n++)
821 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
825 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
828 invalidated_properties);
831 if (changed_properties != NULL)
832 g_variant_unref (changed_properties);
833 g_free (invalidated_properties);
836 /* ---------------------------------------------------------------------------------------------------- */
839 process_get_all_reply (GDBusProxy *proxy,
846 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
848 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
849 g_variant_get_type_string (result));
853 g_variant_get (result, "(a{sv})", &iter);
854 while (g_variant_iter_next (iter, "{sv}", &key, &value))
856 g_hash_table_insert (proxy->priv->properties,
857 key, /* adopts string */
858 value); /* adopts value */
860 g_variant_iter_free (iter);
862 /* Synthesize ::g-properties-changed changed */
863 if (g_hash_table_size (proxy->priv->properties) > 0)
865 GVariant *changed_properties;
866 const gchar *invalidated_properties[1] = {NULL};
868 g_variant_get (result,
870 &changed_properties);
871 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
874 invalidated_properties);
875 g_variant_unref (changed_properties);
885 GCancellable *cancellable;
887 } LoadPropertiesOnNameOwnerChangedData;
890 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
894 LoadPropertiesOnNameOwnerChangedData *data = user_data;
902 result = g_dbus_connection_call_finish (connection,
907 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
909 /* We just ignore if GetAll() is failing. Because this might happen
910 * if the object has no properties at all. Or if the caller is
911 * not authorized to see the properties.
913 * Either way, apps can know about this by using
914 * get_cached_property_names() or get_cached_property().
916 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
917 * fact that GetAll() failed
919 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
920 g_error_free (error);
923 /* and finally we can notify */
926 g_free (data->proxy->priv->name_owner);
927 data->proxy->priv->name_owner = data->name_owner;
928 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
930 g_hash_table_remove_all (data->proxy->priv->properties);
933 process_get_all_reply (data->proxy, result);
934 g_variant_unref (result);
937 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
940 if (data->cancellable == data->proxy->priv->get_all_cancellable)
941 data->proxy->priv->get_all_cancellable = NULL;
943 g_object_unref (data->proxy);
944 g_object_unref (data->cancellable);
945 g_free (data->name_owner);
950 on_name_owner_changed (GDBusConnection *connection,
951 const gchar *sender_name,
952 const gchar *object_path,
953 const gchar *interface_name,
954 const gchar *signal_name,
955 GVariant *parameters,
958 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
959 const gchar *old_owner;
960 const gchar *new_owner;
962 /* if we are already trying to load properties, cancel that */
963 if (proxy->priv->get_all_cancellable != NULL)
965 g_cancellable_cancel (proxy->priv->get_all_cancellable);
966 proxy->priv->get_all_cancellable = NULL;
969 g_variant_get (parameters,
975 if (strlen (new_owner) == 0)
977 g_free (proxy->priv->name_owner);
978 proxy->priv->name_owner = NULL;
980 /* Synthesize ::g-properties-changed changed */
981 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
982 g_hash_table_size (proxy->priv->properties) > 0)
984 GVariantBuilder builder;
985 GVariant *changed_properties;
986 GPtrArray *invalidated_properties;
990 /* Build changed_properties (always empty) and invalidated_properties ... */
991 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
992 changed_properties = g_variant_builder_end (&builder);
993 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
994 g_hash_table_iter_init (&iter, proxy->priv->properties);
995 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
996 g_ptr_array_add (invalidated_properties, g_strdup (key));
997 g_ptr_array_add (invalidated_properties, NULL);
999 /* ... throw out the properties ... */
1000 g_hash_table_remove_all (proxy->priv->properties);
1002 /* ... and finally emit the ::g-properties-changed signal */
1003 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1006 (const gchar* const *) invalidated_properties->pdata);
1007 g_variant_unref (changed_properties);
1008 g_ptr_array_unref (invalidated_properties);
1010 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1014 /* ignore duplicates - this can happen when activating the service */
1015 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1018 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1020 g_free (proxy->priv->name_owner);
1021 proxy->priv->name_owner = g_strdup (new_owner);
1022 g_hash_table_remove_all (proxy->priv->properties);
1023 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1027 LoadPropertiesOnNameOwnerChangedData *data;
1029 /* start loading properties.. only then emit notify::g-name-owner .. we
1030 * need to be able to cancel this in the event another NameOwnerChanged
1031 * signal suddenly happens
1034 g_assert (proxy->priv->get_all_cancellable == NULL);
1035 proxy->priv->get_all_cancellable = g_cancellable_new ();
1036 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1037 data->proxy = g_object_ref (proxy);
1038 data->cancellable = proxy->priv->get_all_cancellable;
1039 data->name_owner = g_strdup (new_owner);
1040 g_dbus_connection_call (proxy->priv->connection,
1042 proxy->priv->object_path,
1043 "org.freedesktop.DBus.Properties",
1045 g_variant_new ("(s)", proxy->priv->interface_name),
1046 G_VARIANT_TYPE ("(a{sv})"),
1047 G_DBUS_CALL_FLAGS_NONE,
1049 proxy->priv->get_all_cancellable,
1050 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1059 /* ---------------------------------------------------------------------------------------------------- */
1064 GCancellable *cancellable;
1065 GSimpleAsyncResult *simple;
1069 async_init_data_free (AsyncInitData *data)
1071 g_object_unref (data->proxy);
1072 if (data->cancellable != NULL)
1073 g_object_unref (data->cancellable);
1074 g_object_unref (data->simple);
1079 async_init_get_all_cb (GDBusConnection *connection,
1083 AsyncInitData *data = user_data;
1088 result = g_dbus_connection_call_finish (connection,
1093 /* We just ignore if GetAll() is failing. Because this might happen
1094 * if the object has no properties at all. Or if the caller is
1095 * not authorized to see the properties.
1097 * Either way, apps can know about this by using
1098 * get_cached_property_names() or get_cached_property().
1100 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1101 * fact that GetAll() failed
1103 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1104 g_error_free (error);
1108 g_simple_async_result_set_op_res_gpointer (data->simple,
1110 (GDestroyNotify) g_variant_unref);
1113 g_simple_async_result_complete_in_idle (data->simple);
1114 async_init_data_free (data);
1119 async_init_get_name_owner_cb (GDBusConnection *connection,
1123 AsyncInitData *data = user_data;
1131 result = g_dbus_connection_call_finish (connection,
1136 if (error->domain == G_DBUS_ERROR &&
1137 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1139 g_error_free (error);
1143 g_simple_async_result_set_from_error (data->simple, error);
1144 g_error_free (error);
1145 g_simple_async_result_complete_in_idle (data->simple);
1146 async_init_data_free (data);
1152 g_variant_get (result,
1154 &data->proxy->priv->name_owner);
1155 g_variant_unref (result);
1159 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1161 /* load all properties asynchronously */
1162 g_dbus_connection_call (data->proxy->priv->connection,
1163 data->proxy->priv->name_owner,
1164 data->proxy->priv->object_path,
1165 "org.freedesktop.DBus.Properties",
1167 g_variant_new ("(s)", data->proxy->priv->interface_name),
1168 G_VARIANT_TYPE ("(a{sv})"),
1169 G_DBUS_CALL_FLAGS_NONE,
1172 (GAsyncReadyCallback) async_init_get_all_cb,
1177 g_simple_async_result_complete_in_idle (data->simple);
1178 async_init_data_free (data);
1186 async_init_call_get_name_owner (AsyncInitData *data)
1188 g_dbus_connection_call (data->proxy->priv->connection,
1189 "org.freedesktop.DBus", /* name */
1190 "/org/freedesktop/DBus", /* object path */
1191 "org.freedesktop.DBus", /* interface */
1193 g_variant_new ("(s)",
1194 data->proxy->priv->name),
1195 G_VARIANT_TYPE ("(s)"),
1196 G_DBUS_CALL_FLAGS_NONE,
1199 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1204 async_init_start_service_by_name_cb (GDBusConnection *connection,
1208 AsyncInitData *data = user_data;
1213 result = g_dbus_connection_call_finish (connection,
1218 /* Errors are not unexpected; the bus will reply e.g.
1220 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1221 * was not provided by any .service files
1223 * This doesn't mean that the name doesn't have an owner, just
1224 * that it's not provided by a .service file. So just proceed to
1225 * invoke GetNameOwner() if dealing with that error.
1227 if (error->domain == G_DBUS_ERROR &&
1228 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1230 g_error_free (error);
1234 g_prefix_error (&error,
1235 _("Error calling StartServiceByName for %s: "),
1236 data->proxy->priv->name);
1242 guint32 start_service_result;
1243 g_variant_get (result,
1245 &start_service_result);
1246 g_variant_unref (result);
1247 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1248 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1250 /* continue to invoke GetNameOwner() */
1254 error = g_error_new (G_IO_ERROR,
1256 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1257 start_service_result,
1258 data->proxy->priv->name);
1263 async_init_call_get_name_owner (data);
1267 g_warn_if_fail (error != NULL);
1268 g_simple_async_result_set_from_error (data->simple, error);
1269 g_error_free (error);
1270 g_simple_async_result_complete_in_idle (data->simple);
1271 async_init_data_free (data);
1275 async_init_call_start_service_by_name (AsyncInitData *data)
1277 g_dbus_connection_call (data->proxy->priv->connection,
1278 "org.freedesktop.DBus", /* name */
1279 "/org/freedesktop/DBus", /* object path */
1280 "org.freedesktop.DBus", /* interface */
1281 "StartServiceByName",
1282 g_variant_new ("(su)",
1283 data->proxy->priv->name,
1285 G_VARIANT_TYPE ("(u)"),
1286 G_DBUS_CALL_FLAGS_NONE,
1289 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1294 async_initable_init_second_async (GAsyncInitable *initable,
1296 GCancellable *cancellable,
1297 GAsyncReadyCallback callback,
1300 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1301 AsyncInitData *data;
1303 data = g_new0 (AsyncInitData, 1);
1304 data->proxy = g_object_ref (proxy);
1305 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1306 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1311 /* Check name ownership asynchronously - possibly also start the service */
1312 if (proxy->priv->name == NULL)
1315 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1317 else if (g_dbus_is_unique_name (proxy->priv->name))
1319 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1320 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1324 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1326 async_init_call_get_name_owner (data);
1330 async_init_call_start_service_by_name (data);
1336 async_initable_init_second_finish (GAsyncInitable *initable,
1340 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1341 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1347 if (g_simple_async_result_propagate_error (simple, error))
1350 result = g_simple_async_result_get_op_res_gpointer (simple);
1353 process_get_all_reply (proxy, result);
1359 proxy->priv->initialized = TRUE;
1363 /* ---------------------------------------------------------------------------------------------------- */
1366 async_initable_init_first (GAsyncInitable *initable)
1368 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1370 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1372 /* subscribe to PropertiesChanged() */
1373 proxy->priv->properties_changed_subscriber_id =
1374 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1376 "org.freedesktop.DBus.Properties",
1377 "PropertiesChanged",
1378 proxy->priv->object_path,
1379 proxy->priv->interface_name,
1380 G_DBUS_SIGNAL_FLAGS_NONE,
1381 on_properties_changed,
1386 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1388 /* subscribe to all signals for the object */
1389 proxy->priv->signals_subscriber_id =
1390 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1392 proxy->priv->interface_name,
1394 proxy->priv->object_path,
1396 G_DBUS_SIGNAL_FLAGS_NONE,
1402 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1404 proxy->priv->name_owner_changed_subscription_id =
1405 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1406 "org.freedesktop.DBus", /* name */
1407 "org.freedesktop.DBus", /* interface */
1408 "NameOwnerChanged", /* signal name */
1409 "/org/freedesktop/DBus", /* path */
1410 proxy->priv->name, /* arg0 */
1411 G_DBUS_SIGNAL_FLAGS_NONE,
1412 on_name_owner_changed,
1418 /* ---------------------------------------------------------------------------------------------------- */
1420 /* initialization is split into two parts - the first is the
1421 * non-blocing part that requires the callers GMainContext - the
1422 * second is a blocking part async part that doesn't require the
1423 * callers GMainContext.. we do this split so the code can be reused
1424 * in the GInitable implementation below.
1426 * Note that obtaining a GDBusConnection is not shared between the two
1434 GCancellable *cancellable;
1435 GAsyncReadyCallback callback;
1437 } GetConnectionData;
1440 get_connection_cb (GObject *source_object,
1444 GetConnectionData *data = user_data;
1448 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1449 if (data->proxy->priv->connection == NULL)
1451 GSimpleAsyncResult *simple;
1452 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1456 g_simple_async_result_set_from_error (simple, error);
1457 g_error_free (error);
1458 g_simple_async_result_complete_in_idle (simple);
1459 g_object_unref (simple);
1463 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1464 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1471 if (data->cancellable != NULL)
1472 g_object_unref (data->cancellable);
1473 if (data->proxy != NULL)
1474 g_object_unref (data->proxy);
1479 async_initable_init_async (GAsyncInitable *initable,
1481 GCancellable *cancellable,
1482 GAsyncReadyCallback callback,
1485 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1487 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1489 GetConnectionData *data;
1491 g_assert (proxy->priv->connection == NULL);
1493 data = g_new0 (GetConnectionData, 1);
1494 data->proxy = g_object_ref (proxy);
1495 data->io_priority = io_priority;
1496 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1497 data->callback = callback;
1498 data->user_data = user_data;
1499 g_bus_get (proxy->priv->bus_type,
1506 async_initable_init_first (initable);
1507 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1512 async_initable_init_finish (GAsyncInitable *initable,
1516 return async_initable_init_second_finish (initable, res, error);
1520 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1522 async_initable_iface->init_async = async_initable_init_async;
1523 async_initable_iface->init_finish = async_initable_init_finish;
1526 /* ---------------------------------------------------------------------------------------------------- */
1530 GMainContext *context;
1533 } InitableAsyncInitableData;
1536 async_initable_init_async_cb (GObject *source_object,
1540 InitableAsyncInitableData *data = user_data;
1541 data->res = g_object_ref (res);
1542 g_main_loop_quit (data->loop);
1545 /* Simply reuse the GAsyncInitable implementation but run the first
1546 * part (that is non-blocking and requires the callers GMainContext)
1547 * with the callers GMainContext.. and the second with a private
1548 * GMainContext (bug 621310 is slightly related).
1550 * Note that obtaining a GDBusConnection is not shared between the two
1554 initable_init (GInitable *initable,
1555 GCancellable *cancellable,
1558 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1559 InitableAsyncInitableData *data;
1564 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1566 g_assert (proxy->priv->connection == NULL);
1567 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1570 if (proxy->priv->connection == NULL)
1574 async_initable_init_first (G_ASYNC_INITABLE (initable));
1576 data = g_new0 (InitableAsyncInitableData, 1);
1577 data->context = g_main_context_new ();
1578 data->loop = g_main_loop_new (data->context, FALSE);
1580 g_main_context_push_thread_default (data->context);
1582 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1585 async_initable_init_async_cb,
1588 g_main_loop_run (data->loop);
1590 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1594 g_main_context_pop_thread_default (data->context);
1596 g_main_context_unref (data->context);
1597 g_main_loop_unref (data->loop);
1598 g_object_unref (data->res);
1607 initable_iface_init (GInitableIface *initable_iface)
1609 initable_iface->init = initable_init;
1612 /* ---------------------------------------------------------------------------------------------------- */
1616 * @connection: A #GDBusConnection.
1617 * @flags: Flags used when constructing the proxy.
1618 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1619 * @name: A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1620 * @object_path: An object path.
1621 * @interface_name: A D-Bus interface name.
1622 * @cancellable: A #GCancellable or %NULL.
1623 * @callback: Callback function to invoke when the proxy is ready.
1624 * @user_data: User data to pass to @callback.
1626 * Creates a proxy for accessing @interface_name on the remote object
1627 * at @object_path owned by @name at @connection and asynchronously
1628 * loads D-Bus properties unless the
1629 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1630 * the #GDBusProxy::g-properties-changed signal to get notified about
1633 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1634 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1635 * to handle signals from the remote object.
1637 * If @name is a well-known name and the
1638 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1639 * owner currently exists, the message bus will be requested to launch
1640 * a name owner for the name.
1642 * This is a failable asynchronous constructor - when the proxy is
1643 * ready, @callback will be invoked and you can use
1644 * g_dbus_proxy_new_finish() to get the result.
1646 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1648 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1653 g_dbus_proxy_new (GDBusConnection *connection,
1654 GDBusProxyFlags flags,
1655 GDBusInterfaceInfo *info,
1657 const gchar *object_path,
1658 const gchar *interface_name,
1659 GCancellable *cancellable,
1660 GAsyncReadyCallback callback,
1663 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1664 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1665 g_return_if_fail (g_variant_is_object_path (object_path));
1666 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1668 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1674 "g-interface-info", info,
1676 "g-connection", connection,
1677 "g-object-path", object_path,
1678 "g-interface-name", interface_name,
1683 * g_dbus_proxy_new_finish:
1684 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1685 * @error: Return location for error or %NULL.
1687 * Finishes creating a #GDBusProxy.
1689 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1694 g_dbus_proxy_new_finish (GAsyncResult *res,
1698 GObject *source_object;
1700 source_object = g_async_result_get_source_object (res);
1701 g_assert (source_object != NULL);
1703 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1706 g_object_unref (source_object);
1709 return G_DBUS_PROXY (object);
1715 * g_dbus_proxy_new_sync:
1716 * @connection: A #GDBusConnection.
1717 * @flags: Flags used when constructing the proxy.
1718 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1719 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1720 * @object_path: An object path.
1721 * @interface_name: A D-Bus interface name.
1722 * @cancellable: (allow-none): A #GCancellable or %NULL.
1723 * @error: (allow-none): Return location for error or %NULL.
1725 * Creates a proxy for accessing @interface_name on the remote object
1726 * at @object_path owned by @name at @connection and synchronously
1727 * loads D-Bus properties unless the
1728 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1730 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1731 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1732 * to handle signals from the remote object.
1734 * If @name is a well-known name and the
1735 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1736 * owner currently exists, the message bus will be requested to launch
1737 * a name owner for the name.
1739 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1740 * and g_dbus_proxy_new_finish() for the asynchronous version.
1742 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1744 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1749 g_dbus_proxy_new_sync (GDBusConnection *connection,
1750 GDBusProxyFlags flags,
1751 GDBusInterfaceInfo *info,
1753 const gchar *object_path,
1754 const gchar *interface_name,
1755 GCancellable *cancellable,
1758 GInitable *initable;
1760 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1761 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1762 g_dbus_is_name (name), NULL);
1763 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1764 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1766 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1770 "g-interface-info", info,
1772 "g-connection", connection,
1773 "g-object-path", object_path,
1774 "g-interface-name", interface_name,
1776 if (initable != NULL)
1777 return G_DBUS_PROXY (initable);
1782 /* ---------------------------------------------------------------------------------------------------- */
1785 * g_dbus_proxy_new_for_bus:
1786 * @bus_type: A #GBusType.
1787 * @flags: Flags used when constructing the proxy.
1788 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1789 * @name: A bus name (well-known or unique).
1790 * @object_path: An object path.
1791 * @interface_name: A D-Bus interface name.
1792 * @cancellable: A #GCancellable or %NULL.
1793 * @callback: Callback function to invoke when the proxy is ready.
1794 * @user_data: User data to pass to @callback.
1796 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1798 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1803 g_dbus_proxy_new_for_bus (GBusType bus_type,
1804 GDBusProxyFlags flags,
1805 GDBusInterfaceInfo *info,
1807 const gchar *object_path,
1808 const gchar *interface_name,
1809 GCancellable *cancellable,
1810 GAsyncReadyCallback callback,
1813 g_return_if_fail (g_dbus_is_name (name));
1814 g_return_if_fail (g_variant_is_object_path (object_path));
1815 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1817 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1823 "g-interface-info", info,
1825 "g-bus-type", bus_type,
1826 "g-object-path", object_path,
1827 "g-interface-name", interface_name,
1832 * g_dbus_proxy_new_for_bus_finish:
1833 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1834 * @error: Return location for error or %NULL.
1836 * Finishes creating a #GDBusProxy.
1838 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1843 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1846 return g_dbus_proxy_new_finish (res, error);
1850 * g_dbus_proxy_new_for_bus_sync:
1851 * @bus_type: A #GBusType.
1852 * @flags: Flags used when constructing the proxy.
1853 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1854 * @name: A bus name (well-known or unique).
1855 * @object_path: An object path.
1856 * @interface_name: A D-Bus interface name.
1857 * @cancellable: A #GCancellable or %NULL.
1858 * @error: Return location for error or %NULL.
1860 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1862 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1864 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1869 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1870 GDBusProxyFlags flags,
1871 GDBusInterfaceInfo *info,
1873 const gchar *object_path,
1874 const gchar *interface_name,
1875 GCancellable *cancellable,
1878 GInitable *initable;
1880 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1881 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1882 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1884 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1888 "g-interface-info", info,
1890 "g-bus-type", bus_type,
1891 "g-object-path", object_path,
1892 "g-interface-name", interface_name,
1894 if (initable != NULL)
1895 return G_DBUS_PROXY (initable);
1900 /* ---------------------------------------------------------------------------------------------------- */
1903 * g_dbus_proxy_get_connection:
1904 * @proxy: A #GDBusProxy.
1906 * Gets the connection @proxy is for.
1908 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
1913 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1915 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1916 return proxy->priv->connection;
1920 * g_dbus_proxy_get_flags:
1921 * @proxy: A #GDBusProxy.
1923 * Gets the flags that @proxy was constructed with.
1925 * Returns: Flags from the #GDBusProxyFlags enumeration.
1930 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1932 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1933 return proxy->priv->flags;
1937 * g_dbus_proxy_get_name:
1938 * @proxy: A #GDBusProxy.
1940 * Gets the name that @proxy was constructed for.
1942 * Returns: A string owned by @proxy. Do not free.
1947 g_dbus_proxy_get_name (GDBusProxy *proxy)
1949 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1950 return proxy->priv->name;
1954 * g_dbus_proxy_get_name_owner:
1955 * @proxy: A #GDBusProxy.
1957 * The unique name that owns the name that @proxy is for or %NULL if
1958 * no-one currently owns that name. You may connect to the
1959 * #GObject::notify signal to track changes to the
1960 * #GDBusProxy:g-name-owner property.
1962 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
1967 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
1969 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1970 return g_strdup (proxy->priv->name_owner);
1974 * g_dbus_proxy_get_object_path:
1975 * @proxy: A #GDBusProxy.
1977 * Gets the object path @proxy is for.
1979 * Returns: A string owned by @proxy. Do not free.
1984 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1986 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1987 return proxy->priv->object_path;
1991 * g_dbus_proxy_get_interface_name:
1992 * @proxy: A #GDBusProxy.
1994 * Gets the D-Bus interface name @proxy is for.
1996 * Returns: A string owned by @proxy. Do not free.
2001 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2003 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2004 return proxy->priv->interface_name;
2008 * g_dbus_proxy_get_default_timeout:
2009 * @proxy: A #GDBusProxy.
2011 * Gets the timeout to use if -1 (specifying default timeout) is
2012 * passed as @timeout_msec in the g_dbus_proxy_call() and
2013 * g_dbus_proxy_call_sync() functions.
2015 * See the #GDBusProxy:g-default-timeout property for more details.
2017 * Returns: Timeout to use for @proxy.
2022 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2024 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2025 return proxy->priv->timeout_msec;
2029 * g_dbus_proxy_set_default_timeout:
2030 * @proxy: A #GDBusProxy.
2031 * @timeout_msec: Timeout in milliseconds.
2033 * Sets the timeout to use if -1 (specifying default timeout) is
2034 * passed as @timeout_msec in the g_dbus_proxy_call() and
2035 * g_dbus_proxy_call_sync() functions.
2037 * See the #GDBusProxy:g-default-timeout property for more details.
2042 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2045 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2046 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2048 /* TODO: locking? */
2049 if (proxy->priv->timeout_msec != timeout_msec)
2051 proxy->priv->timeout_msec = timeout_msec;
2052 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2057 * g_dbus_proxy_get_interface_info:
2058 * @proxy: A #GDBusProxy
2060 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2061 * interface that @proxy conforms to.
2063 * See the #GDBusProxy:g-interface-info property for more details.
2065 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2066 * object, it is owned by @proxy.
2070 GDBusInterfaceInfo *
2071 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2073 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2074 return proxy->priv->expected_interface;
2078 * g_dbus_proxy_set_interface_info:
2079 * @proxy: A #GDBusProxy
2080 * @info: Minimum interface this proxy conforms to or %NULL to unset.
2082 * Ensure that interactions with @proxy conform to the given
2083 * interface. For example, when completing a method call, if the type
2084 * signature of the message isn't what's expected, the given #GError
2085 * is set. Signals that have a type signature mismatch are simply
2088 * See the #GDBusProxy:g-interface-info property for more details.
2093 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2094 GDBusInterfaceInfo *info)
2096 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2097 if (proxy->priv->expected_interface != NULL)
2098 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2099 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2102 /* ---------------------------------------------------------------------------------------------------- */
2105 maybe_split_method_name (const gchar *method_name,
2106 gchar **out_interface_name,
2107 const gchar **out_method_name)
2112 g_assert (out_interface_name != NULL);
2113 g_assert (out_method_name != NULL);
2114 *out_interface_name = NULL;
2115 *out_method_name = NULL;
2117 if (strchr (method_name, '.') != NULL)
2122 p = g_strdup (method_name);
2123 last_dot = strrchr (p, '.');
2126 *out_interface_name = p;
2127 *out_method_name = last_dot + 1;
2137 reply_cb (GDBusConnection *connection,
2141 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2146 value = g_dbus_connection_call_finish (connection,
2151 g_simple_async_result_set_from_error (simple,
2153 g_error_free (error);
2157 g_simple_async_result_set_op_res_gpointer (simple,
2159 (GDestroyNotify) g_variant_unref);
2162 /* no need to complete in idle since the method GDBusConnection already does */
2163 g_simple_async_result_complete (simple);
2164 g_object_unref (simple);
2167 static const GDBusMethodInfo *
2168 lookup_method_info_or_warn (GDBusProxy *proxy,
2169 const gchar *method_name)
2171 const GDBusMethodInfo *info;
2173 if (proxy->priv->expected_interface == NULL)
2176 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2179 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2180 method_name, proxy->priv->expected_interface->name);
2186 static const gchar *
2187 get_destination_for_call (GDBusProxy *proxy)
2193 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2194 * is never NULL and always the same as proxy->priv->name. We use this
2195 * knowledge to avoid checking if proxy->priv->name is a unique or
2198 ret = proxy->priv->name_owner;
2202 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2205 ret = proxy->priv->name;
2212 * g_dbus_proxy_call:
2213 * @proxy: A #GDBusProxy.
2214 * @method_name: Name of method to invoke.
2215 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2216 * @flags: Flags from the #GDBusCallFlags enumeration.
2217 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2218 * @cancellable: A #GCancellable or %NULL.
2219 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2220 * care about the result of the method invocation.
2221 * @user_data: The data to pass to @callback.
2223 * Asynchronously invokes the @method_name method on @proxy.
2225 * If @method_name contains any dots, then @name is split into interface and
2226 * method name parts. This allows using @proxy for invoking methods on
2229 * If the #GDBusConnection associated with @proxy is closed then
2230 * the operation will fail with %G_IO_ERROR_CLOSED. If
2231 * @cancellable is canceled, the operation will fail with
2232 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2233 * compatible with the D-Bus protocol, the operation fails with
2234 * %G_IO_ERROR_INVALID_ARGUMENT.
2236 * If the @parameters #GVariant is floating, it is consumed. This allows
2237 * convenient 'inline' use of g_variant_new(), e.g.:
2239 * g_dbus_proxy_call (proxy,
2241 * g_variant_new ("(ss)",
2244 * G_DBUS_CALL_FLAGS_NONE,
2247 * (GAsyncReadyCallback) two_strings_done,
2251 * This is an asynchronous method. When the operation is finished,
2252 * @callback will be invoked in the
2253 * <link linkend="g-main-context-push-thread-default">thread-default
2254 * main loop</link> of the thread you are calling this method from.
2255 * You can then call g_dbus_proxy_call_finish() to get the result of
2256 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2257 * version of this method.
2262 g_dbus_proxy_call (GDBusProxy *proxy,
2263 const gchar *method_name,
2264 GVariant *parameters,
2265 GDBusCallFlags flags,
2267 GCancellable *cancellable,
2268 GAsyncReadyCallback callback,
2271 GSimpleAsyncResult *simple;
2273 gchar *split_interface_name;
2274 const gchar *split_method_name;
2275 const gchar *target_method_name;
2276 const gchar *target_interface_name;
2277 const gchar *destination;
2278 GVariantType *reply_type;
2280 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2281 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2282 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2283 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2286 split_interface_name = NULL;
2288 simple = g_simple_async_result_new (G_OBJECT (proxy),
2293 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2294 target_method_name = was_split ? split_method_name : method_name;
2295 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2297 /* Warn if method is unexpected (cf. :g-interface-info) */
2300 const GDBusMethodInfo *expected_method_info;
2301 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2302 if (expected_method_info != NULL)
2303 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2307 if (proxy->priv->name != NULL)
2309 destination = get_destination_for_call (proxy);
2310 if (destination == NULL)
2312 g_simple_async_result_set_error (simple,
2315 _("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"));
2320 g_dbus_connection_call (proxy->priv->connection,
2322 proxy->priv->object_path,
2323 target_interface_name,
2328 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2330 (GAsyncReadyCallback) reply_cb,
2334 if (reply_type != NULL)
2335 g_variant_type_free (reply_type);
2337 g_free (split_interface_name);
2341 * g_dbus_proxy_call_finish:
2342 * @proxy: A #GDBusProxy.
2343 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2344 * @error: Return location for error or %NULL.
2346 * Finishes an operation started with g_dbus_proxy_call().
2348 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2349 * return values. Free with g_variant_unref().
2354 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2358 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2361 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2362 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2363 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2365 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2369 if (g_simple_async_result_propagate_error (simple, error))
2372 value = g_variant_ref (g_simple_async_result_get_op_res_gpointer (simple));
2379 * g_dbus_proxy_call_sync:
2380 * @proxy: A #GDBusProxy.
2381 * @method_name: Name of method to invoke.
2382 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2383 * @flags: Flags from the #GDBusCallFlags enumeration.
2384 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
2385 * @cancellable: A #GCancellable or %NULL.
2386 * @error: Return location for error or %NULL.
2388 * Synchronously invokes the @method_name method on @proxy.
2390 * If @method_name contains any dots, then @name is split into interface and
2391 * method name parts. This allows using @proxy for invoking methods on
2394 * If the #GDBusConnection associated with @proxy is disconnected then
2395 * the operation will fail with %G_IO_ERROR_CLOSED. If
2396 * @cancellable is canceled, the operation will fail with
2397 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2398 * compatible with the D-Bus protocol, the operation fails with
2399 * %G_IO_ERROR_INVALID_ARGUMENT.
2401 * If the @parameters #GVariant is floating, it is consumed. This allows
2402 * convenient 'inline' use of g_variant_new(), e.g.:
2404 * g_dbus_proxy_call_sync (proxy,
2406 * g_variant_new ("(ss)",
2409 * G_DBUS_CALL_FLAGS_NONE,
2415 * The calling thread is blocked until a reply is received. See
2416 * g_dbus_proxy_call() for the asynchronous version of this
2419 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2420 * return values. Free with g_variant_unref().
2425 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2426 const gchar *method_name,
2427 GVariant *parameters,
2428 GDBusCallFlags flags,
2430 GCancellable *cancellable,
2435 gchar *split_interface_name;
2436 const gchar *split_method_name;
2437 const gchar *target_method_name;
2438 const gchar *target_interface_name;
2439 const gchar *destination;
2440 GVariantType *reply_type;
2442 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2443 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2444 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2445 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2446 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2450 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2451 target_method_name = was_split ? split_method_name : method_name;
2452 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2454 /* Warn if method is unexpected (cf. :g-interface-info) */
2457 const GDBusMethodInfo *expected_method_info;
2458 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2459 if (expected_method_info != NULL)
2460 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2464 if (proxy->priv->name != NULL)
2466 destination = get_destination_for_call (proxy);
2467 if (destination == NULL)
2469 g_set_error_literal (error,
2472 _("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"));
2478 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2480 proxy->priv->object_path,
2481 target_interface_name,
2486 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2491 if (reply_type != NULL)
2492 g_variant_type_free (reply_type);
2494 g_free (split_interface_name);
2499 /* ---------------------------------------------------------------------------------------------------- */