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 if (proxy->priv->connection != NULL)
160 g_object_unref (proxy->priv->connection);
161 g_free (proxy->priv->name);
162 g_free (proxy->priv->name_owner);
163 g_free (proxy->priv->object_path);
164 g_free (proxy->priv->interface_name);
165 if (proxy->priv->properties != NULL)
166 g_hash_table_unref (proxy->priv->properties);
168 if (proxy->priv->expected_interface != NULL)
169 g_dbus_interface_info_unref (proxy->priv->expected_interface);
171 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
175 g_dbus_proxy_get_property (GObject *object,
180 GDBusProxy *proxy = G_DBUS_PROXY (object);
184 case PROP_G_CONNECTION:
185 g_value_set_object (value, proxy->priv->connection);
189 g_value_set_flags (value, proxy->priv->flags);
193 g_value_set_string (value, proxy->priv->name);
196 case PROP_G_NAME_OWNER:
197 g_value_set_string (value, proxy->priv->name_owner);
200 case PROP_G_OBJECT_PATH:
201 g_value_set_string (value, proxy->priv->object_path);
204 case PROP_G_INTERFACE_NAME:
205 g_value_set_string (value, proxy->priv->interface_name);
208 case PROP_G_DEFAULT_TIMEOUT:
209 g_value_set_int (value, proxy->priv->timeout_msec);
212 case PROP_G_INTERFACE_INFO:
213 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 g_dbus_proxy_set_property (GObject *object,
228 GDBusProxy *proxy = G_DBUS_PROXY (object);
232 case PROP_G_CONNECTION:
233 proxy->priv->connection = g_value_dup_object (value);
237 proxy->priv->flags = g_value_get_flags (value);
241 proxy->priv->name = g_value_dup_string (value);
244 case PROP_G_OBJECT_PATH:
245 proxy->priv->object_path = g_value_dup_string (value);
248 case PROP_G_INTERFACE_NAME:
249 proxy->priv->interface_name = g_value_dup_string (value);
252 case PROP_G_DEFAULT_TIMEOUT:
253 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
256 case PROP_G_INTERFACE_INFO:
257 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
260 case PROP_G_BUS_TYPE:
261 proxy->priv->bus_type = g_value_get_enum (value);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 g_dbus_proxy_class_init (GDBusProxyClass *klass)
273 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
275 gobject_class->finalize = g_dbus_proxy_finalize;
276 gobject_class->set_property = g_dbus_proxy_set_property;
277 gobject_class->get_property = g_dbus_proxy_get_property;
279 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
280 * in derived classes */
283 * GDBusProxy:g-interface-info:
285 * Ensure that interactions with this proxy conform to the given
286 * interface. For example, when completing a method call, if the
287 * type signature of the message isn't what's expected, the given
288 * #GError is set. Signals that have a type signature mismatch are
293 g_object_class_install_property (gobject_class,
294 PROP_G_INTERFACE_INFO,
295 g_param_spec_boxed ("g-interface-info",
296 P_("Interface Information"),
297 P_("Interface Information"),
298 G_TYPE_DBUS_INTERFACE_INFO,
301 G_PARAM_STATIC_NAME |
302 G_PARAM_STATIC_BLURB |
303 G_PARAM_STATIC_NICK));
306 * GDBusProxy:g-connection:
308 * The #GDBusConnection the proxy is for.
312 g_object_class_install_property (gobject_class,
314 g_param_spec_object ("g-connection",
316 P_("The connection the proxy is for"),
317 G_TYPE_DBUS_CONNECTION,
320 G_PARAM_CONSTRUCT_ONLY |
321 G_PARAM_STATIC_NAME |
322 G_PARAM_STATIC_BLURB |
323 G_PARAM_STATIC_NICK));
326 * GDBusProxy:g-bus-type:
328 * If this property is not %G_BUS_TYPE_NONE, then
329 * #GDBusProxy:g-connection must be %NULL and will be set to the
330 * #GDBusConnection obtained by calling g_bus_get() with the value
335 g_object_class_install_property (gobject_class,
337 g_param_spec_enum ("g-bus-type",
339 P_("The bus to connect to, if any"),
343 G_PARAM_CONSTRUCT_ONLY |
344 G_PARAM_STATIC_NAME |
345 G_PARAM_STATIC_BLURB |
346 G_PARAM_STATIC_NICK));
349 * GDBusProxy:g-flags:
351 * Flags from the #GDBusProxyFlags enumeration.
355 g_object_class_install_property (gobject_class,
357 g_param_spec_flags ("g-flags",
359 P_("Flags for the proxy"),
360 G_TYPE_DBUS_PROXY_FLAGS,
361 G_DBUS_PROXY_FLAGS_NONE,
364 G_PARAM_CONSTRUCT_ONLY |
365 G_PARAM_STATIC_NAME |
366 G_PARAM_STATIC_BLURB |
367 G_PARAM_STATIC_NICK));
372 * The well-known or unique name that the proxy is for.
376 g_object_class_install_property (gobject_class,
378 g_param_spec_string ("g-name",
380 P_("The well-known or unique name that the proxy is for"),
384 G_PARAM_CONSTRUCT_ONLY |
385 G_PARAM_STATIC_NAME |
386 G_PARAM_STATIC_BLURB |
387 G_PARAM_STATIC_NICK));
390 * GDBusProxy:g-name-owner:
392 * The unique name that owns #GDBusProxy:name or %NULL if no-one
393 * currently owns that name. You may connect to #GObject::notify signal to
394 * track changes to this property.
398 g_object_class_install_property (gobject_class,
400 g_param_spec_string ("g-name-owner",
402 P_("The unique name for the owner"),
405 G_PARAM_STATIC_NAME |
406 G_PARAM_STATIC_BLURB |
407 G_PARAM_STATIC_NICK));
410 * GDBusProxy:g-object-path:
412 * The object path the proxy is for.
416 g_object_class_install_property (gobject_class,
418 g_param_spec_string ("g-object-path",
420 P_("The object path the proxy is for"),
424 G_PARAM_CONSTRUCT_ONLY |
425 G_PARAM_STATIC_NAME |
426 G_PARAM_STATIC_BLURB |
427 G_PARAM_STATIC_NICK));
430 * GDBusProxy:g-interface-name:
432 * The D-Bus interface name the proxy is for.
436 g_object_class_install_property (gobject_class,
437 PROP_G_INTERFACE_NAME,
438 g_param_spec_string ("g-interface-name",
439 P_("g-interface-name"),
440 P_("The D-Bus interface name the proxy is for"),
444 G_PARAM_CONSTRUCT_ONLY |
445 G_PARAM_STATIC_NAME |
446 G_PARAM_STATIC_BLURB |
447 G_PARAM_STATIC_NICK));
450 * GDBusProxy:g-default-timeout:
452 * The timeout to use if -1 (specifying default timeout) is passed
453 * as @timeout_msec in the g_dbus_proxy_call() and
454 * g_dbus_proxy_call_sync() functions.
456 * This allows applications to set a proxy-wide timeout for all
457 * remote method invocations on the proxy. If this property is -1,
458 * the default timeout (typically 25 seconds) is used. If set to
459 * %G_MAXINT, then no timeout is used.
463 g_object_class_install_property (gobject_class,
464 PROP_G_DEFAULT_TIMEOUT,
465 g_param_spec_int ("g-default-timeout",
466 P_("Default Timeout"),
467 P_("Timeout for remote method invocation"),
474 G_PARAM_STATIC_NAME |
475 G_PARAM_STATIC_BLURB |
476 G_PARAM_STATIC_NICK));
479 * GDBusProxy::g-properties-changed:
480 * @proxy: The #GDBusProxy emitting the signal.
481 * @changed_properties: A #GVariant containing the properties that changed
482 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
484 * Emitted when one or more D-Bus properties on @proxy changes. The
485 * local cache has already been updated when this signal fires. Note
486 * that both @changed_properties and @invalidated_properties are
487 * guaranteed to never be %NULL (either may be empty though).
489 * This signal corresponds to the
490 * <literal>PropertiesChanged</literal> D-Bus signal on the
491 * <literal>org.freedesktop.DBus.Properties</literal> interface.
495 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
498 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
501 _gio_marshal_VOID__VARIANT_BOXED,
505 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
508 * GDBusProxy::g-signal:
509 * @proxy: The #GDBusProxy emitting the signal.
510 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
511 * @signal_name: The name of the signal.
512 * @parameters: A #GVariant tuple with parameters for the signal.
514 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
518 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
521 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
524 _gio_marshal_VOID__STRING_STRING_VARIANT,
532 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
536 g_dbus_proxy_init (GDBusProxy *proxy)
538 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
539 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
542 (GDestroyNotify) g_variant_unref);
545 /* ---------------------------------------------------------------------------------------------------- */
548 property_name_sort_func (const gchar **a,
551 return g_strcmp0 (*a, *b);
555 * g_dbus_proxy_get_cached_property_names:
556 * @proxy: A #GDBusProxy.
558 * Gets the names of all cached properties on @proxy.
560 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
561 * no cached properties. Free the returned array with g_strfreev().
566 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
573 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
576 if (g_hash_table_size (proxy->priv->properties) == 0)
579 p = g_ptr_array_new ();
581 g_hash_table_iter_init (&iter, proxy->priv->properties);
582 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
583 g_ptr_array_add (p, g_strdup (key));
584 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
585 g_ptr_array_add (p, NULL);
587 names = (gchar **) g_ptr_array_free (p, FALSE);
593 static const GDBusPropertyInfo *
594 lookup_property_info_or_warn (GDBusProxy *proxy,
595 const gchar *property_name)
597 const GDBusPropertyInfo *info;
599 if (proxy->priv->expected_interface == NULL)
602 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
605 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
607 proxy->priv->expected_interface->name);
614 * g_dbus_proxy_get_cached_property:
615 * @proxy: A #GDBusProxy.
616 * @property_name: Property name.
618 * Looks up the value for a property from the cache. This call does no
621 * If @proxy has an expected interface (see
622 * #GDBusProxy:g-interface-info), then @property_name (for existence)
623 * is checked against it.
625 * Returns: A reference to the #GVariant instance that holds the value
626 * for @property_name or %NULL if the value is not in the cache. The
627 * returned reference must be freed with g_variant_unref().
632 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
633 const gchar *property_name)
637 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
638 g_return_val_if_fail (property_name != NULL, NULL);
640 value = g_hash_table_lookup (proxy->priv->properties, property_name);
643 const GDBusPropertyInfo *info;
644 info = lookup_property_info_or_warn (proxy, property_name);
649 g_variant_ref (value);
656 * g_dbus_proxy_set_cached_property:
657 * @proxy: A #GDBusProxy
658 * @property_name: Property name.
659 * @value: Value for the property or %NULL to remove it from the cache.
661 * If @value is not %NULL, sets the cached value for the property with
662 * name @property_name to the value in @value.
664 * If @value is %NULL, then the cached value is removed from the
667 * If @proxy has an expected interface (see
668 * #GDBusProxy:g-interface-info), then @property_name (for existence)
669 * and @value (for the type) is checked against it.
671 * If the @value #GVariant is floating, it is consumed. This allows
672 * convenient 'inline' use of g_variant_new(), e.g.
674 * g_dbus_proxy_set_cached_property (proxy,
676 * g_variant_new ("(si)",
681 * Normally you will not need to use this method since @proxy is
682 * tracking changes using the
683 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
684 * D-Bus signal. However, for performance reasons an object may decide
685 * to not use this signal for some properties and instead use a
686 * proprietary out-of-band mechanism to transmit changes.
688 * As a concrete example, consider an object with a property
689 * <literal>ChatroomParticipants</literal> which is an array of
690 * strings. Instead of transmitting the same (long) array every time
691 * the property changes, it is more efficient to only transmit the
692 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
693 * name)</literal> and <literal>ChatroomParticipantParted(String
699 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
700 const gchar *property_name,
703 const GDBusPropertyInfo *info;
705 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
706 g_return_if_fail (property_name != NULL);
710 info = lookup_property_info_or_warn (proxy, property_name);
713 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
715 g_warning (_("Trying to set property %s of type %s but according to the expected "
716 "interface the type is %s"),
718 g_variant_get_type_string (value),
723 g_hash_table_insert (proxy->priv->properties,
724 g_strdup (property_name),
725 g_variant_ref_sink (value));
729 g_hash_table_remove (proxy->priv->properties, property_name);
736 /* ---------------------------------------------------------------------------------------------------- */
739 on_signal_received (GDBusConnection *connection,
740 const gchar *sender_name,
741 const gchar *object_path,
742 const gchar *interface_name,
743 const gchar *signal_name,
744 GVariant *parameters,
747 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
749 if (!proxy->priv->initialized)
752 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
755 g_signal_emit (proxy,
756 signals[SIGNAL_SIGNAL],
765 /* ---------------------------------------------------------------------------------------------------- */
768 on_properties_changed (GDBusConnection *connection,
769 const gchar *sender_name,
770 const gchar *object_path,
771 const gchar *interface_name,
772 const gchar *signal_name,
773 GVariant *parameters,
776 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
778 const gchar *interface_name_for_signal;
779 GVariant *changed_properties;
780 gchar **invalidated_properties;
787 changed_properties = NULL;
788 invalidated_properties = NULL;
790 if (!proxy->priv->initialized)
793 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
796 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
798 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
799 g_variant_get_type_string (parameters));
803 g_variant_get (parameters,
805 &interface_name_for_signal,
807 &invalidated_properties);
809 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
812 g_variant_iter_init (&iter, changed_properties);
813 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
815 g_hash_table_insert (proxy->priv->properties,
816 key, /* adopts string */
817 value); /* adopts value */
820 for (n = 0; invalidated_properties[n] != NULL; n++)
822 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
826 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
829 invalidated_properties);
832 if (changed_properties != NULL)
833 g_variant_unref (changed_properties);
834 g_free (invalidated_properties);
837 /* ---------------------------------------------------------------------------------------------------- */
840 process_get_all_reply (GDBusProxy *proxy,
847 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
849 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
850 g_variant_get_type_string (result));
854 g_variant_get (result, "(a{sv})", &iter);
855 while (g_variant_iter_next (iter, "{sv}", &key, &value))
857 g_hash_table_insert (proxy->priv->properties,
858 key, /* adopts string */
859 value); /* adopts value */
861 g_variant_iter_free (iter);
863 /* Synthesize ::g-properties-changed changed */
864 if (g_hash_table_size (proxy->priv->properties) > 0)
866 GVariant *changed_properties;
867 const gchar *invalidated_properties[1] = {NULL};
869 g_variant_get (result,
871 &changed_properties);
872 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
875 invalidated_properties);
876 g_variant_unref (changed_properties);
886 GCancellable *cancellable;
888 } LoadPropertiesOnNameOwnerChangedData;
891 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
895 LoadPropertiesOnNameOwnerChangedData *data = user_data;
903 result = g_dbus_connection_call_finish (connection,
908 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
910 /* We just ignore if GetAll() is failing. Because this might happen
911 * if the object has no properties at all. Or if the caller is
912 * not authorized to see the properties.
914 * Either way, apps can know about this by using
915 * get_cached_property_names() or get_cached_property().
917 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
918 * fact that GetAll() failed
920 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
921 g_error_free (error);
924 /* and finally we can notify */
927 g_free (data->proxy->priv->name_owner);
928 data->proxy->priv->name_owner = data->name_owner;
929 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
931 g_hash_table_remove_all (data->proxy->priv->properties);
934 process_get_all_reply (data->proxy, result);
935 g_variant_unref (result);
938 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
941 if (data->cancellable == data->proxy->priv->get_all_cancellable)
942 data->proxy->priv->get_all_cancellable = NULL;
944 g_object_unref (data->proxy);
945 g_object_unref (data->cancellable);
946 g_free (data->name_owner);
951 on_name_owner_changed (GDBusConnection *connection,
952 const gchar *sender_name,
953 const gchar *object_path,
954 const gchar *interface_name,
955 const gchar *signal_name,
956 GVariant *parameters,
959 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
960 const gchar *old_owner;
961 const gchar *new_owner;
963 /* if we are already trying to load properties, cancel that */
964 if (proxy->priv->get_all_cancellable != NULL)
966 g_cancellable_cancel (proxy->priv->get_all_cancellable);
967 proxy->priv->get_all_cancellable = NULL;
970 g_variant_get (parameters,
976 if (strlen (new_owner) == 0)
978 g_free (proxy->priv->name_owner);
979 proxy->priv->name_owner = NULL;
981 /* Synthesize ::g-properties-changed changed */
982 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
983 g_hash_table_size (proxy->priv->properties) > 0)
985 GVariantBuilder builder;
986 GVariant *changed_properties;
987 GPtrArray *invalidated_properties;
991 /* Build changed_properties (always empty) and invalidated_properties ... */
992 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
993 changed_properties = g_variant_builder_end (&builder);
994 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
995 g_hash_table_iter_init (&iter, proxy->priv->properties);
996 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
997 g_ptr_array_add (invalidated_properties, g_strdup (key));
998 g_ptr_array_add (invalidated_properties, NULL);
1000 /* ... throw out the properties ... */
1001 g_hash_table_remove_all (proxy->priv->properties);
1003 /* ... and finally emit the ::g-properties-changed signal */
1004 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1007 (const gchar* const *) invalidated_properties->pdata);
1008 g_variant_unref (changed_properties);
1009 g_ptr_array_unref (invalidated_properties);
1011 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1015 /* ignore duplicates - this can happen when activating the service */
1016 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1019 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1021 g_free (proxy->priv->name_owner);
1022 proxy->priv->name_owner = g_strdup (new_owner);
1023 g_hash_table_remove_all (proxy->priv->properties);
1024 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1028 LoadPropertiesOnNameOwnerChangedData *data;
1030 /* start loading properties.. only then emit notify::g-name-owner .. we
1031 * need to be able to cancel this in the event another NameOwnerChanged
1032 * signal suddenly happens
1035 g_assert (proxy->priv->get_all_cancellable == NULL);
1036 proxy->priv->get_all_cancellable = g_cancellable_new ();
1037 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1038 data->proxy = g_object_ref (proxy);
1039 data->cancellable = proxy->priv->get_all_cancellable;
1040 data->name_owner = g_strdup (new_owner);
1041 g_dbus_connection_call (proxy->priv->connection,
1043 proxy->priv->object_path,
1044 "org.freedesktop.DBus.Properties",
1046 g_variant_new ("(s)", proxy->priv->interface_name),
1047 G_VARIANT_TYPE ("(a{sv})"),
1048 G_DBUS_CALL_FLAGS_NONE,
1050 proxy->priv->get_all_cancellable,
1051 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1060 /* ---------------------------------------------------------------------------------------------------- */
1065 GCancellable *cancellable;
1066 GSimpleAsyncResult *simple;
1070 async_init_data_free (AsyncInitData *data)
1072 g_object_unref (data->proxy);
1073 if (data->cancellable != NULL)
1074 g_object_unref (data->cancellable);
1075 g_object_unref (data->simple);
1080 async_init_get_all_cb (GDBusConnection *connection,
1084 AsyncInitData *data = user_data;
1089 result = g_dbus_connection_call_finish (connection,
1094 /* We just ignore if GetAll() is failing. Because this might happen
1095 * if the object has no properties at all. Or if the caller is
1096 * not authorized to see the properties.
1098 * Either way, apps can know about this by using
1099 * get_cached_property_names() or get_cached_property().
1101 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1102 * fact that GetAll() failed
1104 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1105 g_error_free (error);
1109 g_simple_async_result_set_op_res_gpointer (data->simple,
1111 (GDestroyNotify) g_variant_unref);
1114 g_simple_async_result_complete_in_idle (data->simple);
1115 async_init_data_free (data);
1120 async_init_get_name_owner_cb (GDBusConnection *connection,
1124 AsyncInitData *data = user_data;
1132 result = g_dbus_connection_call_finish (connection,
1137 if (error->domain == G_DBUS_ERROR &&
1138 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1140 g_error_free (error);
1144 g_simple_async_result_take_error (data->simple, 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_take_error (data->simple, error);
1269 g_simple_async_result_complete_in_idle (data->simple);
1270 async_init_data_free (data);
1274 async_init_call_start_service_by_name (AsyncInitData *data)
1276 g_dbus_connection_call (data->proxy->priv->connection,
1277 "org.freedesktop.DBus", /* name */
1278 "/org/freedesktop/DBus", /* object path */
1279 "org.freedesktop.DBus", /* interface */
1280 "StartServiceByName",
1281 g_variant_new ("(su)",
1282 data->proxy->priv->name,
1284 G_VARIANT_TYPE ("(u)"),
1285 G_DBUS_CALL_FLAGS_NONE,
1288 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1293 async_initable_init_second_async (GAsyncInitable *initable,
1295 GCancellable *cancellable,
1296 GAsyncReadyCallback callback,
1299 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1300 AsyncInitData *data;
1302 data = g_new0 (AsyncInitData, 1);
1303 data->proxy = g_object_ref (proxy);
1304 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1305 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1310 /* Check name ownership asynchronously - possibly also start the service */
1311 if (proxy->priv->name == NULL)
1314 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1316 else if (g_dbus_is_unique_name (proxy->priv->name))
1318 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1319 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1323 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1325 async_init_call_get_name_owner (data);
1329 async_init_call_start_service_by_name (data);
1335 async_initable_init_second_finish (GAsyncInitable *initable,
1339 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1340 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1346 if (g_simple_async_result_propagate_error (simple, error))
1349 result = g_simple_async_result_get_op_res_gpointer (simple);
1352 process_get_all_reply (proxy, result);
1358 proxy->priv->initialized = TRUE;
1362 /* ---------------------------------------------------------------------------------------------------- */
1365 async_initable_init_first (GAsyncInitable *initable)
1367 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1369 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1371 /* subscribe to PropertiesChanged() */
1372 proxy->priv->properties_changed_subscriber_id =
1373 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1375 "org.freedesktop.DBus.Properties",
1376 "PropertiesChanged",
1377 proxy->priv->object_path,
1378 proxy->priv->interface_name,
1379 G_DBUS_SIGNAL_FLAGS_NONE,
1380 on_properties_changed,
1385 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1387 /* subscribe to all signals for the object */
1388 proxy->priv->signals_subscriber_id =
1389 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1391 proxy->priv->interface_name,
1393 proxy->priv->object_path,
1395 G_DBUS_SIGNAL_FLAGS_NONE,
1401 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1403 proxy->priv->name_owner_changed_subscription_id =
1404 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1405 "org.freedesktop.DBus", /* name */
1406 "org.freedesktop.DBus", /* interface */
1407 "NameOwnerChanged", /* signal name */
1408 "/org/freedesktop/DBus", /* path */
1409 proxy->priv->name, /* arg0 */
1410 G_DBUS_SIGNAL_FLAGS_NONE,
1411 on_name_owner_changed,
1417 /* ---------------------------------------------------------------------------------------------------- */
1419 /* initialization is split into two parts - the first is the
1420 * non-blocing part that requires the callers GMainContext - the
1421 * second is a blocking part async part that doesn't require the
1422 * callers GMainContext.. we do this split so the code can be reused
1423 * in the GInitable implementation below.
1425 * Note that obtaining a GDBusConnection is not shared between the two
1433 GCancellable *cancellable;
1434 GAsyncReadyCallback callback;
1436 } GetConnectionData;
1439 get_connection_cb (GObject *source_object,
1443 GetConnectionData *data = user_data;
1447 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1448 if (data->proxy->priv->connection == NULL)
1450 GSimpleAsyncResult *simple;
1451 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1455 g_simple_async_result_take_error (simple, error);
1456 g_simple_async_result_complete_in_idle (simple);
1457 g_object_unref (simple);
1461 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1462 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1469 if (data->cancellable != NULL)
1470 g_object_unref (data->cancellable);
1471 if (data->proxy != NULL)
1472 g_object_unref (data->proxy);
1477 async_initable_init_async (GAsyncInitable *initable,
1479 GCancellable *cancellable,
1480 GAsyncReadyCallback callback,
1483 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1485 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1487 GetConnectionData *data;
1489 g_assert (proxy->priv->connection == NULL);
1491 data = g_new0 (GetConnectionData, 1);
1492 data->proxy = g_object_ref (proxy);
1493 data->io_priority = io_priority;
1494 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1495 data->callback = callback;
1496 data->user_data = user_data;
1497 g_bus_get (proxy->priv->bus_type,
1504 async_initable_init_first (initable);
1505 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1510 async_initable_init_finish (GAsyncInitable *initable,
1514 return async_initable_init_second_finish (initable, res, error);
1518 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1520 async_initable_iface->init_async = async_initable_init_async;
1521 async_initable_iface->init_finish = async_initable_init_finish;
1524 /* ---------------------------------------------------------------------------------------------------- */
1528 GMainContext *context;
1531 } InitableAsyncInitableData;
1534 async_initable_init_async_cb (GObject *source_object,
1538 InitableAsyncInitableData *data = user_data;
1539 data->res = g_object_ref (res);
1540 g_main_loop_quit (data->loop);
1543 /* Simply reuse the GAsyncInitable implementation but run the first
1544 * part (that is non-blocking and requires the callers GMainContext)
1545 * with the callers GMainContext.. and the second with a private
1546 * GMainContext (bug 621310 is slightly related).
1548 * Note that obtaining a GDBusConnection is not shared between the two
1552 initable_init (GInitable *initable,
1553 GCancellable *cancellable,
1556 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1557 InitableAsyncInitableData *data;
1562 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1564 g_assert (proxy->priv->connection == NULL);
1565 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1568 if (proxy->priv->connection == NULL)
1572 async_initable_init_first (G_ASYNC_INITABLE (initable));
1574 data = g_new0 (InitableAsyncInitableData, 1);
1575 data->context = g_main_context_new ();
1576 data->loop = g_main_loop_new (data->context, FALSE);
1578 g_main_context_push_thread_default (data->context);
1580 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1583 async_initable_init_async_cb,
1586 g_main_loop_run (data->loop);
1588 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1592 g_main_context_pop_thread_default (data->context);
1594 g_main_context_unref (data->context);
1595 g_main_loop_unref (data->loop);
1596 g_object_unref (data->res);
1605 initable_iface_init (GInitableIface *initable_iface)
1607 initable_iface->init = initable_init;
1610 /* ---------------------------------------------------------------------------------------------------- */
1614 * @connection: A #GDBusConnection.
1615 * @flags: Flags used when constructing the proxy.
1616 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1617 * @name: A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1618 * @object_path: An object path.
1619 * @interface_name: A D-Bus interface name.
1620 * @cancellable: A #GCancellable or %NULL.
1621 * @callback: Callback function to invoke when the proxy is ready.
1622 * @user_data: User data to pass to @callback.
1624 * Creates a proxy for accessing @interface_name on the remote object
1625 * at @object_path owned by @name at @connection and asynchronously
1626 * loads D-Bus properties unless the
1627 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1628 * the #GDBusProxy::g-properties-changed signal to get notified about
1631 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1632 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1633 * to handle signals from the remote object.
1635 * If @name is a well-known name and the
1636 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1637 * owner currently exists, the message bus will be requested to launch
1638 * a name owner for the name.
1640 * This is a failable asynchronous constructor - when the proxy is
1641 * ready, @callback will be invoked and you can use
1642 * g_dbus_proxy_new_finish() to get the result.
1644 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1646 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1651 g_dbus_proxy_new (GDBusConnection *connection,
1652 GDBusProxyFlags flags,
1653 GDBusInterfaceInfo *info,
1655 const gchar *object_path,
1656 const gchar *interface_name,
1657 GCancellable *cancellable,
1658 GAsyncReadyCallback callback,
1661 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1662 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1663 g_return_if_fail (g_variant_is_object_path (object_path));
1664 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1666 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1672 "g-interface-info", info,
1674 "g-connection", connection,
1675 "g-object-path", object_path,
1676 "g-interface-name", interface_name,
1681 * g_dbus_proxy_new_finish:
1682 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1683 * @error: Return location for error or %NULL.
1685 * Finishes creating a #GDBusProxy.
1687 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1692 g_dbus_proxy_new_finish (GAsyncResult *res,
1696 GObject *source_object;
1698 source_object = g_async_result_get_source_object (res);
1699 g_assert (source_object != NULL);
1701 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1704 g_object_unref (source_object);
1707 return G_DBUS_PROXY (object);
1713 * g_dbus_proxy_new_sync:
1714 * @connection: A #GDBusConnection.
1715 * @flags: Flags used when constructing the proxy.
1716 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1717 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1718 * @object_path: An object path.
1719 * @interface_name: A D-Bus interface name.
1720 * @cancellable: (allow-none): A #GCancellable or %NULL.
1721 * @error: (allow-none): Return location for error or %NULL.
1723 * Creates a proxy for accessing @interface_name on the remote object
1724 * at @object_path owned by @name at @connection and synchronously
1725 * loads D-Bus properties unless the
1726 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1728 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1729 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1730 * to handle signals from the remote object.
1732 * If @name is a well-known name and the
1733 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1734 * owner currently exists, the message bus will be requested to launch
1735 * a name owner for the name.
1737 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1738 * and g_dbus_proxy_new_finish() for the asynchronous version.
1740 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1742 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1747 g_dbus_proxy_new_sync (GDBusConnection *connection,
1748 GDBusProxyFlags flags,
1749 GDBusInterfaceInfo *info,
1751 const gchar *object_path,
1752 const gchar *interface_name,
1753 GCancellable *cancellable,
1756 GInitable *initable;
1758 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1759 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1760 g_dbus_is_name (name), NULL);
1761 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1762 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1764 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1768 "g-interface-info", info,
1770 "g-connection", connection,
1771 "g-object-path", object_path,
1772 "g-interface-name", interface_name,
1774 if (initable != NULL)
1775 return G_DBUS_PROXY (initable);
1780 /* ---------------------------------------------------------------------------------------------------- */
1783 * g_dbus_proxy_new_for_bus:
1784 * @bus_type: A #GBusType.
1785 * @flags: Flags used when constructing the proxy.
1786 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1787 * @name: A bus name (well-known or unique).
1788 * @object_path: An object path.
1789 * @interface_name: A D-Bus interface name.
1790 * @cancellable: A #GCancellable or %NULL.
1791 * @callback: Callback function to invoke when the proxy is ready.
1792 * @user_data: User data to pass to @callback.
1794 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1796 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1801 g_dbus_proxy_new_for_bus (GBusType bus_type,
1802 GDBusProxyFlags flags,
1803 GDBusInterfaceInfo *info,
1805 const gchar *object_path,
1806 const gchar *interface_name,
1807 GCancellable *cancellable,
1808 GAsyncReadyCallback callback,
1811 g_return_if_fail (g_dbus_is_name (name));
1812 g_return_if_fail (g_variant_is_object_path (object_path));
1813 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1815 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1821 "g-interface-info", info,
1823 "g-bus-type", bus_type,
1824 "g-object-path", object_path,
1825 "g-interface-name", interface_name,
1830 * g_dbus_proxy_new_for_bus_finish:
1831 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
1832 * @error: Return location for error or %NULL.
1834 * Finishes creating a #GDBusProxy.
1836 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1841 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
1844 return g_dbus_proxy_new_finish (res, error);
1848 * g_dbus_proxy_new_for_bus_sync:
1849 * @bus_type: A #GBusType.
1850 * @flags: Flags used when constructing the proxy.
1851 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1852 * @name: A bus name (well-known or unique).
1853 * @object_path: An object path.
1854 * @interface_name: A D-Bus interface name.
1855 * @cancellable: A #GCancellable or %NULL.
1856 * @error: Return location for error or %NULL.
1858 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
1860 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1862 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1867 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
1868 GDBusProxyFlags flags,
1869 GDBusInterfaceInfo *info,
1871 const gchar *object_path,
1872 const gchar *interface_name,
1873 GCancellable *cancellable,
1876 GInitable *initable;
1878 g_return_val_if_fail (g_dbus_is_name (name), NULL);
1879 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1880 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1882 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1886 "g-interface-info", info,
1888 "g-bus-type", bus_type,
1889 "g-object-path", object_path,
1890 "g-interface-name", interface_name,
1892 if (initable != NULL)
1893 return G_DBUS_PROXY (initable);
1898 /* ---------------------------------------------------------------------------------------------------- */
1901 * g_dbus_proxy_get_connection:
1902 * @proxy: A #GDBusProxy.
1904 * Gets the connection @proxy is for.
1906 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
1911 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1913 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1914 return proxy->priv->connection;
1918 * g_dbus_proxy_get_flags:
1919 * @proxy: A #GDBusProxy.
1921 * Gets the flags that @proxy was constructed with.
1923 * Returns: Flags from the #GDBusProxyFlags enumeration.
1928 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1930 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1931 return proxy->priv->flags;
1935 * g_dbus_proxy_get_name:
1936 * @proxy: A #GDBusProxy.
1938 * Gets the name that @proxy was constructed for.
1940 * Returns: A string owned by @proxy. Do not free.
1945 g_dbus_proxy_get_name (GDBusProxy *proxy)
1947 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1948 return proxy->priv->name;
1952 * g_dbus_proxy_get_name_owner:
1953 * @proxy: A #GDBusProxy.
1955 * The unique name that owns the name that @proxy is for or %NULL if
1956 * no-one currently owns that name. You may connect to the
1957 * #GObject::notify signal to track changes to the
1958 * #GDBusProxy:g-name-owner property.
1960 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
1965 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
1967 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1968 return g_strdup (proxy->priv->name_owner);
1972 * g_dbus_proxy_get_object_path:
1973 * @proxy: A #GDBusProxy.
1975 * Gets the object path @proxy is for.
1977 * Returns: A string owned by @proxy. Do not free.
1982 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1984 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1985 return proxy->priv->object_path;
1989 * g_dbus_proxy_get_interface_name:
1990 * @proxy: A #GDBusProxy.
1992 * Gets the D-Bus interface name @proxy is for.
1994 * Returns: A string owned by @proxy. Do not free.
1999 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2001 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2002 return proxy->priv->interface_name;
2006 * g_dbus_proxy_get_default_timeout:
2007 * @proxy: A #GDBusProxy.
2009 * Gets the timeout to use if -1 (specifying default timeout) is
2010 * passed as @timeout_msec in the g_dbus_proxy_call() and
2011 * g_dbus_proxy_call_sync() functions.
2013 * See the #GDBusProxy:g-default-timeout property for more details.
2015 * Returns: Timeout to use for @proxy.
2020 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2022 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2023 return proxy->priv->timeout_msec;
2027 * g_dbus_proxy_set_default_timeout:
2028 * @proxy: A #GDBusProxy.
2029 * @timeout_msec: Timeout in milliseconds.
2031 * Sets the timeout to use if -1 (specifying default timeout) is
2032 * passed as @timeout_msec in the g_dbus_proxy_call() and
2033 * g_dbus_proxy_call_sync() functions.
2035 * See the #GDBusProxy:g-default-timeout property for more details.
2040 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2043 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2044 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2046 /* TODO: locking? */
2047 if (proxy->priv->timeout_msec != timeout_msec)
2049 proxy->priv->timeout_msec = timeout_msec;
2050 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2055 * g_dbus_proxy_get_interface_info:
2056 * @proxy: A #GDBusProxy
2058 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2059 * interface that @proxy conforms to.
2061 * See the #GDBusProxy:g-interface-info property for more details.
2063 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2064 * object, it is owned by @proxy.
2068 GDBusInterfaceInfo *
2069 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2071 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2072 return proxy->priv->expected_interface;
2076 * g_dbus_proxy_set_interface_info:
2077 * @proxy: A #GDBusProxy
2078 * @info: Minimum interface this proxy conforms to or %NULL to unset.
2080 * Ensure that interactions with @proxy conform to the given
2081 * interface. For example, when completing a method call, if the type
2082 * signature of the message isn't what's expected, the given #GError
2083 * is set. Signals that have a type signature mismatch are simply
2086 * See the #GDBusProxy:g-interface-info property for more details.
2091 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2092 GDBusInterfaceInfo *info)
2094 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2095 if (proxy->priv->expected_interface != NULL)
2096 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2097 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2100 /* ---------------------------------------------------------------------------------------------------- */
2103 maybe_split_method_name (const gchar *method_name,
2104 gchar **out_interface_name,
2105 const gchar **out_method_name)
2110 g_assert (out_interface_name != NULL);
2111 g_assert (out_method_name != NULL);
2112 *out_interface_name = NULL;
2113 *out_method_name = NULL;
2115 if (strchr (method_name, '.') != NULL)
2120 p = g_strdup (method_name);
2121 last_dot = strrchr (p, '.');
2124 *out_interface_name = p;
2125 *out_method_name = last_dot + 1;
2135 reply_cb (GDBusConnection *connection,
2139 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2144 value = g_dbus_connection_call_finish (connection,
2149 g_simple_async_result_take_error (simple, error);
2153 g_simple_async_result_set_op_res_gpointer (simple,
2155 (GDestroyNotify) g_variant_unref);
2158 /* no need to complete in idle since the method GDBusConnection already does */
2159 g_simple_async_result_complete (simple);
2160 g_object_unref (simple);
2163 static const GDBusMethodInfo *
2164 lookup_method_info_or_warn (GDBusProxy *proxy,
2165 const gchar *method_name)
2167 const GDBusMethodInfo *info;
2169 if (proxy->priv->expected_interface == NULL)
2172 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2175 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2176 method_name, proxy->priv->expected_interface->name);
2182 static const gchar *
2183 get_destination_for_call (GDBusProxy *proxy)
2189 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2190 * is never NULL and always the same as proxy->priv->name. We use this
2191 * knowledge to avoid checking if proxy->priv->name is a unique or
2194 ret = proxy->priv->name_owner;
2198 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2201 ret = proxy->priv->name;
2208 * g_dbus_proxy_call:
2209 * @proxy: A #GDBusProxy.
2210 * @method_name: Name of method to invoke.
2211 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2212 * @flags: Flags from the #GDBusCallFlags enumeration.
2213 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2214 * "infinite") or -1 to use the proxy default timeout.
2215 * @cancellable: A #GCancellable or %NULL.
2216 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2217 * care about the result of the method invocation.
2218 * @user_data: The data to pass to @callback.
2220 * Asynchronously invokes the @method_name method on @proxy.
2222 * If @method_name contains any dots, then @name is split into interface and
2223 * method name parts. This allows using @proxy for invoking methods on
2226 * If the #GDBusConnection associated with @proxy is closed then
2227 * the operation will fail with %G_IO_ERROR_CLOSED. If
2228 * @cancellable is canceled, the operation will fail with
2229 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2230 * compatible with the D-Bus protocol, the operation fails with
2231 * %G_IO_ERROR_INVALID_ARGUMENT.
2233 * If the @parameters #GVariant is floating, it is consumed. This allows
2234 * convenient 'inline' use of g_variant_new(), e.g.:
2236 * g_dbus_proxy_call (proxy,
2238 * g_variant_new ("(ss)",
2241 * G_DBUS_CALL_FLAGS_NONE,
2244 * (GAsyncReadyCallback) two_strings_done,
2248 * This is an asynchronous method. When the operation is finished,
2249 * @callback will be invoked in the
2250 * <link linkend="g-main-context-push-thread-default">thread-default
2251 * main loop</link> of the thread you are calling this method from.
2252 * You can then call g_dbus_proxy_call_finish() to get the result of
2253 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2254 * version of this method.
2259 g_dbus_proxy_call (GDBusProxy *proxy,
2260 const gchar *method_name,
2261 GVariant *parameters,
2262 GDBusCallFlags flags,
2264 GCancellable *cancellable,
2265 GAsyncReadyCallback callback,
2268 GSimpleAsyncResult *simple;
2270 gchar *split_interface_name;
2271 const gchar *split_method_name;
2272 const gchar *target_method_name;
2273 const gchar *target_interface_name;
2274 const gchar *destination;
2275 GVariantType *reply_type;
2277 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2278 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2279 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2280 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2283 split_interface_name = NULL;
2285 simple = g_simple_async_result_new (G_OBJECT (proxy),
2290 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2291 target_method_name = was_split ? split_method_name : method_name;
2292 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2294 /* Warn if method is unexpected (cf. :g-interface-info) */
2297 const GDBusMethodInfo *expected_method_info;
2298 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2299 if (expected_method_info != NULL)
2300 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2304 if (proxy->priv->name != NULL)
2306 destination = get_destination_for_call (proxy);
2307 if (destination == NULL)
2309 g_simple_async_result_set_error (simple,
2312 _("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"));
2317 g_dbus_connection_call (proxy->priv->connection,
2319 proxy->priv->object_path,
2320 target_interface_name,
2325 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2327 (GAsyncReadyCallback) reply_cb,
2331 if (reply_type != NULL)
2332 g_variant_type_free (reply_type);
2334 g_free (split_interface_name);
2338 * g_dbus_proxy_call_finish:
2339 * @proxy: A #GDBusProxy.
2340 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2341 * @error: Return location for error or %NULL.
2343 * Finishes an operation started with g_dbus_proxy_call().
2345 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2346 * return values. Free with g_variant_unref().
2351 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2355 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2358 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2359 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2360 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2362 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
2366 if (g_simple_async_result_propagate_error (simple, error))
2369 value = g_variant_ref (g_simple_async_result_get_op_res_gpointer (simple));
2376 * g_dbus_proxy_call_sync:
2377 * @proxy: A #GDBusProxy.
2378 * @method_name: Name of method to invoke.
2379 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2380 * @flags: Flags from the #GDBusCallFlags enumeration.
2381 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2382 * "infinite") or -1 to use the proxy default timeout.
2383 * @cancellable: A #GCancellable or %NULL.
2384 * @error: Return location for error or %NULL.
2386 * Synchronously invokes the @method_name method on @proxy.
2388 * If @method_name contains any dots, then @name is split into interface and
2389 * method name parts. This allows using @proxy for invoking methods on
2392 * If the #GDBusConnection associated with @proxy is disconnected then
2393 * the operation will fail with %G_IO_ERROR_CLOSED. If
2394 * @cancellable is canceled, the operation will fail with
2395 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2396 * compatible with the D-Bus protocol, the operation fails with
2397 * %G_IO_ERROR_INVALID_ARGUMENT.
2399 * If the @parameters #GVariant is floating, it is consumed. This allows
2400 * convenient 'inline' use of g_variant_new(), e.g.:
2402 * g_dbus_proxy_call_sync (proxy,
2404 * g_variant_new ("(ss)",
2407 * G_DBUS_CALL_FLAGS_NONE,
2413 * The calling thread is blocked until a reply is received. See
2414 * g_dbus_proxy_call() for the asynchronous version of this
2417 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2418 * return values. Free with g_variant_unref().
2423 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2424 const gchar *method_name,
2425 GVariant *parameters,
2426 GDBusCallFlags flags,
2428 GCancellable *cancellable,
2433 gchar *split_interface_name;
2434 const gchar *split_method_name;
2435 const gchar *target_method_name;
2436 const gchar *target_interface_name;
2437 const gchar *destination;
2438 GVariantType *reply_type;
2440 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2441 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2442 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2443 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2444 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2448 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2449 target_method_name = was_split ? split_method_name : method_name;
2450 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2452 /* Warn if method is unexpected (cf. :g-interface-info) */
2455 const GDBusMethodInfo *expected_method_info;
2456 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2457 if (expected_method_info != NULL)
2458 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2462 if (proxy->priv->name != NULL)
2464 destination = get_destination_for_call (proxy);
2465 if (destination == NULL)
2467 g_set_error_literal (error,
2470 _("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"));
2476 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2478 proxy->priv->object_path,
2479 target_interface_name,
2484 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2489 if (reply_type != NULL)
2490 g_variant_type_free (reply_type);
2492 g_free (split_interface_name);
2497 /* ---------------------------------------------------------------------------------------------------- */