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 "ginitable.h"
35 #include "gasyncinitable.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "gcancellable.h"
40 #include "gdbusinterface.h"
43 #include "gunixfdlist.h"
50 * @short_description: Client-side D-Bus interface proxy
53 * #GDBusProxy is a base class used for proxies to access a D-Bus
54 * interface on a remote object. A #GDBusProxy can be constructed for
55 * both well-known and unique names.
57 * By default, #GDBusProxy will cache all properties (and listen to
58 * changes) of the remote object, and proxy all signals that gets
59 * emitted. This behaviour can be changed by passing suitable
60 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
61 * well-known name, the property cache is flushed when the name owner
62 * vanishes and reloaded when a name owner appears.
64 * If a #GDBusProxy is used for a well-known name, the owner of the
65 * name is tracked and can be read from
66 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
67 * get notified of changes. Additionally, only signals and property
68 * changes emitted from the current name owner are considered and
69 * calls are always sent to the current name owner. This avoids a
70 * number of race conditions when the name is lost by one owner and
71 * claimed by another. However, if no name owner currently exists,
72 * then calls will be sent to the well-known name which may result in
73 * the message bus launching an owner (unless
74 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
76 * The generic #GDBusProxy::g-properties-changed and
77 * #GDBusProxy::g-signal signals are not very convenient to work
78 * with. Therefore, the recommended way of working with proxies is to
79 * subclass #GDBusProxy, and have more natural properties and signals
80 * in your derived class. See <xref linkend="gdbus-example-gdbus-codegen"/>
81 * for how this can easily be done using the
82 * <command><link linkend="gdbus-codegen">gdbus-codegen</link></command>
85 * A #GDBusProxy instance can be used from multiple threads but note
86 * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
87 * and #GObject::notify) are emitted in the
88 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
89 * of the thread where the instance was constructed.
91 * <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>
94 /* lock protecting the properties GHashTable */
95 G_LOCK_DEFINE_STATIC (properties_lock);
97 /* ---------------------------------------------------------------------------------------------------- */
99 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
103 volatile gint ref_count;
105 } SignalSubscriptionData;
107 static SignalSubscriptionData *
108 signal_subscription_ref (SignalSubscriptionData *data)
110 g_atomic_int_inc (&data->ref_count);
115 signal_subscription_unref (SignalSubscriptionData *data)
117 if (g_atomic_int_dec_and_test (&data->ref_count))
119 g_slice_free (SignalSubscriptionData, data);
123 /* ---------------------------------------------------------------------------------------------------- */
125 struct _GDBusProxyPrivate
128 GDBusProxyFlags flags;
129 GDBusConnection *connection;
134 gchar *interface_name;
137 guint name_owner_changed_subscription_id;
139 GCancellable *get_all_cancellable;
141 /* gchar* -> GVariant* */
142 GHashTable *properties;
144 GDBusInterfaceInfo *expected_interface;
146 guint properties_changed_subscription_id;
147 guint signals_subscription_id;
149 gboolean initialized;
153 SignalSubscriptionData *signal_subscription_data;
165 PROP_G_INTERFACE_NAME,
166 PROP_G_DEFAULT_TIMEOUT,
167 PROP_G_INTERFACE_INFO
172 PROPERTIES_CHANGED_SIGNAL,
177 guint signals[LAST_SIGNAL] = {0};
179 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
180 static void initable_iface_init (GInitableIface *initable_iface);
181 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
183 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
184 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
185 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
186 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
190 g_dbus_proxy_dispose (GObject *object)
192 GDBusProxy *proxy = G_DBUS_PROXY (object);
193 G_LOCK (signal_subscription_lock);
194 if (proxy->priv->signal_subscription_data != NULL)
196 proxy->priv->signal_subscription_data->proxy = NULL;
197 signal_subscription_unref (proxy->priv->signal_subscription_data);
198 proxy->priv->signal_subscription_data = NULL;
200 G_UNLOCK (signal_subscription_lock);
202 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
206 g_dbus_proxy_finalize (GObject *object)
208 GDBusProxy *proxy = G_DBUS_PROXY (object);
210 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
212 if (proxy->priv->name_owner_changed_subscription_id > 0)
213 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
214 proxy->priv->name_owner_changed_subscription_id);
216 if (proxy->priv->properties_changed_subscription_id > 0)
217 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
218 proxy->priv->properties_changed_subscription_id);
220 if (proxy->priv->signals_subscription_id > 0)
221 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
222 proxy->priv->signals_subscription_id);
224 if (proxy->priv->connection != NULL)
225 g_object_unref (proxy->priv->connection);
226 g_free (proxy->priv->name);
227 g_free (proxy->priv->name_owner);
228 g_free (proxy->priv->object_path);
229 g_free (proxy->priv->interface_name);
230 if (proxy->priv->properties != NULL)
231 g_hash_table_unref (proxy->priv->properties);
233 if (proxy->priv->expected_interface != NULL)
235 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
236 g_dbus_interface_info_unref (proxy->priv->expected_interface);
239 if (proxy->priv->object != NULL)
240 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
242 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
246 g_dbus_proxy_get_property (GObject *object,
251 GDBusProxy *proxy = G_DBUS_PROXY (object);
255 case PROP_G_CONNECTION:
256 g_value_set_object (value, proxy->priv->connection);
260 g_value_set_flags (value, proxy->priv->flags);
264 g_value_set_string (value, proxy->priv->name);
267 case PROP_G_NAME_OWNER:
268 g_value_set_string (value, proxy->priv->name_owner);
271 case PROP_G_OBJECT_PATH:
272 g_value_set_string (value, proxy->priv->object_path);
275 case PROP_G_INTERFACE_NAME:
276 g_value_set_string (value, proxy->priv->interface_name);
279 case PROP_G_DEFAULT_TIMEOUT:
280 g_value_set_int (value, proxy->priv->timeout_msec);
283 case PROP_G_INTERFACE_INFO:
284 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 g_dbus_proxy_set_property (GObject *object,
299 GDBusProxy *proxy = G_DBUS_PROXY (object);
303 case PROP_G_CONNECTION:
304 proxy->priv->connection = g_value_dup_object (value);
308 proxy->priv->flags = g_value_get_flags (value);
312 proxy->priv->name = g_value_dup_string (value);
315 case PROP_G_OBJECT_PATH:
316 proxy->priv->object_path = g_value_dup_string (value);
319 case PROP_G_INTERFACE_NAME:
320 proxy->priv->interface_name = g_value_dup_string (value);
323 case PROP_G_DEFAULT_TIMEOUT:
324 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
327 case PROP_G_INTERFACE_INFO:
328 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
331 case PROP_G_BUS_TYPE:
332 proxy->priv->bus_type = g_value_get_enum (value);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 g_dbus_proxy_class_init (GDBusProxyClass *klass)
344 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
346 gobject_class->dispose = g_dbus_proxy_dispose;
347 gobject_class->finalize = g_dbus_proxy_finalize;
348 gobject_class->set_property = g_dbus_proxy_set_property;
349 gobject_class->get_property = g_dbus_proxy_get_property;
351 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
352 * in derived classes */
355 * GDBusProxy:g-interface-info:
357 * Ensure that interactions with this proxy conform to the given
358 * interface. For example, when completing a method call, if the
359 * type signature of the message isn't what's expected, the given
360 * #GError is set. Signals that have a type signature mismatch are
365 g_object_class_install_property (gobject_class,
366 PROP_G_INTERFACE_INFO,
367 g_param_spec_boxed ("g-interface-info",
368 P_("Interface Information"),
369 P_("Interface Information"),
370 G_TYPE_DBUS_INTERFACE_INFO,
373 G_PARAM_STATIC_NAME |
374 G_PARAM_STATIC_BLURB |
375 G_PARAM_STATIC_NICK));
378 * GDBusProxy:g-connection:
380 * The #GDBusConnection the proxy is for.
384 g_object_class_install_property (gobject_class,
386 g_param_spec_object ("g-connection",
388 P_("The connection the proxy is for"),
389 G_TYPE_DBUS_CONNECTION,
392 G_PARAM_CONSTRUCT_ONLY |
393 G_PARAM_STATIC_NAME |
394 G_PARAM_STATIC_BLURB |
395 G_PARAM_STATIC_NICK));
398 * GDBusProxy:g-bus-type:
400 * If this property is not %G_BUS_TYPE_NONE, then
401 * #GDBusProxy:g-connection must be %NULL and will be set to the
402 * #GDBusConnection obtained by calling g_bus_get() with the value
407 g_object_class_install_property (gobject_class,
409 g_param_spec_enum ("g-bus-type",
411 P_("The bus to connect to, if any"),
415 G_PARAM_CONSTRUCT_ONLY |
416 G_PARAM_STATIC_NAME |
417 G_PARAM_STATIC_BLURB |
418 G_PARAM_STATIC_NICK));
421 * GDBusProxy:g-flags:
423 * Flags from the #GDBusProxyFlags enumeration.
427 g_object_class_install_property (gobject_class,
429 g_param_spec_flags ("g-flags",
431 P_("Flags for the proxy"),
432 G_TYPE_DBUS_PROXY_FLAGS,
433 G_DBUS_PROXY_FLAGS_NONE,
436 G_PARAM_CONSTRUCT_ONLY |
437 G_PARAM_STATIC_NAME |
438 G_PARAM_STATIC_BLURB |
439 G_PARAM_STATIC_NICK));
444 * The well-known or unique name that the proxy is for.
448 g_object_class_install_property (gobject_class,
450 g_param_spec_string ("g-name",
452 P_("The well-known or unique name that the proxy is for"),
456 G_PARAM_CONSTRUCT_ONLY |
457 G_PARAM_STATIC_NAME |
458 G_PARAM_STATIC_BLURB |
459 G_PARAM_STATIC_NICK));
462 * GDBusProxy:g-name-owner:
464 * The unique name that owns #GDBusProxy:name or %NULL if no-one
465 * currently owns that name. You may connect to #GObject::notify signal to
466 * track changes to this property.
470 g_object_class_install_property (gobject_class,
472 g_param_spec_string ("g-name-owner",
474 P_("The unique name for the owner"),
477 G_PARAM_STATIC_NAME |
478 G_PARAM_STATIC_BLURB |
479 G_PARAM_STATIC_NICK));
482 * GDBusProxy:g-object-path:
484 * The object path the proxy is for.
488 g_object_class_install_property (gobject_class,
490 g_param_spec_string ("g-object-path",
492 P_("The object path the proxy is for"),
496 G_PARAM_CONSTRUCT_ONLY |
497 G_PARAM_STATIC_NAME |
498 G_PARAM_STATIC_BLURB |
499 G_PARAM_STATIC_NICK));
502 * GDBusProxy:g-interface-name:
504 * The D-Bus interface name the proxy is for.
508 g_object_class_install_property (gobject_class,
509 PROP_G_INTERFACE_NAME,
510 g_param_spec_string ("g-interface-name",
511 P_("g-interface-name"),
512 P_("The D-Bus interface name the proxy is for"),
516 G_PARAM_CONSTRUCT_ONLY |
517 G_PARAM_STATIC_NAME |
518 G_PARAM_STATIC_BLURB |
519 G_PARAM_STATIC_NICK));
522 * GDBusProxy:g-default-timeout:
524 * The timeout to use if -1 (specifying default timeout) is passed
525 * as @timeout_msec in the g_dbus_proxy_call() and
526 * g_dbus_proxy_call_sync() functions.
528 * This allows applications to set a proxy-wide timeout for all
529 * remote method invocations on the proxy. If this property is -1,
530 * the default timeout (typically 25 seconds) is used. If set to
531 * %G_MAXINT, then no timeout is used.
535 g_object_class_install_property (gobject_class,
536 PROP_G_DEFAULT_TIMEOUT,
537 g_param_spec_int ("g-default-timeout",
538 P_("Default Timeout"),
539 P_("Timeout for remote method invocation"),
546 G_PARAM_STATIC_NAME |
547 G_PARAM_STATIC_BLURB |
548 G_PARAM_STATIC_NICK));
551 * GDBusProxy::g-properties-changed:
552 * @proxy: The #GDBusProxy emitting the signal.
553 * @changed_properties: A #GVariant containing the properties that changed
554 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
556 * Emitted when one or more D-Bus properties on @proxy changes. The
557 * local cache has already been updated when this signal fires. Note
558 * that both @changed_properties and @invalidated_properties are
559 * guaranteed to never be %NULL (either may be empty though).
561 * This signal corresponds to the
562 * <literal>PropertiesChanged</literal> D-Bus signal on the
563 * <literal>org.freedesktop.DBus.Properties</literal> interface.
567 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
569 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
570 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
577 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
580 * GDBusProxy::g-signal:
581 * @proxy: The #GDBusProxy emitting the signal.
582 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
583 * @signal_name: The name of the signal.
584 * @parameters: A #GVariant tuple with parameters for the signal.
586 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
590 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
592 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
593 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
604 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
608 g_dbus_proxy_init (GDBusProxy *proxy)
610 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
611 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
612 proxy->priv->signal_subscription_data->ref_count = 1;
613 proxy->priv->signal_subscription_data->proxy = proxy;
614 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
617 (GDestroyNotify) g_variant_unref);
620 /* ---------------------------------------------------------------------------------------------------- */
623 property_name_sort_func (const gchar **a,
626 return g_strcmp0 (*a, *b);
630 * g_dbus_proxy_get_cached_property_names:
631 * @proxy: A #GDBusProxy.
633 * Gets the names of all cached properties on @proxy.
635 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
636 * @proxy has no cached properties. Free the returned array with
642 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
649 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
651 G_LOCK (properties_lock);
654 if (g_hash_table_size (proxy->priv->properties) == 0)
657 p = g_ptr_array_new ();
659 g_hash_table_iter_init (&iter, proxy->priv->properties);
660 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
661 g_ptr_array_add (p, g_strdup (key));
662 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
663 g_ptr_array_add (p, NULL);
665 names = (gchar **) g_ptr_array_free (p, FALSE);
668 G_UNLOCK (properties_lock);
672 static const GDBusPropertyInfo *
673 lookup_property_info_or_warn (GDBusProxy *proxy,
674 const gchar *property_name)
676 const GDBusPropertyInfo *info;
678 if (proxy->priv->expected_interface == NULL)
681 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
684 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
686 proxy->priv->expected_interface->name);
693 * g_dbus_proxy_get_cached_property:
694 * @proxy: A #GDBusProxy.
695 * @property_name: Property name.
697 * Looks up the value for a property from the cache. This call does no
700 * If @proxy has an expected interface (see
701 * #GDBusProxy:g-interface-info), then @property_name (for existence)
702 * is checked against it.
704 * Returns: A reference to the #GVariant instance that holds the value
705 * for @property_name or %NULL if the value is not in the cache. The
706 * returned reference must be freed with g_variant_unref().
711 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
712 const gchar *property_name)
716 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
717 g_return_val_if_fail (property_name != NULL, NULL);
719 G_LOCK (properties_lock);
721 value = g_hash_table_lookup (proxy->priv->properties, property_name);
724 lookup_property_info_or_warn (proxy, property_name);
729 g_variant_ref (value);
732 G_UNLOCK (properties_lock);
737 * g_dbus_proxy_set_cached_property:
738 * @proxy: A #GDBusProxy
739 * @property_name: Property name.
740 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
742 * If @value is not %NULL, sets the cached value for the property with
743 * name @property_name to the value in @value.
745 * If @value is %NULL, then the cached value is removed from the
748 * If @proxy has an expected interface (see
749 * #GDBusProxy:g-interface-info), then @property_name (for existence)
750 * and @value (for the type) is checked against it.
752 * If the @value #GVariant is floating, it is consumed. This allows
753 * convenient 'inline' use of g_variant_new(), e.g.
755 * g_dbus_proxy_set_cached_property (proxy,
757 * g_variant_new ("(si)",
762 * Normally you will not need to use this method since @proxy is
763 * tracking changes using the
764 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
765 * D-Bus signal. However, for performance reasons an object may decide
766 * to not use this signal for some properties and instead use a
767 * proprietary out-of-band mechanism to transmit changes.
769 * As a concrete example, consider an object with a property
770 * <literal>ChatroomParticipants</literal> which is an array of
771 * strings. Instead of transmitting the same (long) array every time
772 * the property changes, it is more efficient to only transmit the
773 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
774 * name)</literal> and <literal>ChatroomParticipantParted(String
780 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
781 const gchar *property_name,
784 const GDBusPropertyInfo *info;
786 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
787 g_return_if_fail (property_name != NULL);
789 G_LOCK (properties_lock);
793 info = lookup_property_info_or_warn (proxy, property_name);
796 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
798 g_warning ("Trying to set property %s of type %s but according to the expected "
799 "interface the type is %s",
801 g_variant_get_type_string (value),
806 g_hash_table_insert (proxy->priv->properties,
807 g_strdup (property_name),
808 g_variant_ref_sink (value));
812 g_hash_table_remove (proxy->priv->properties, property_name);
816 G_UNLOCK (properties_lock);
819 /* ---------------------------------------------------------------------------------------------------- */
822 on_signal_received (GDBusConnection *connection,
823 const gchar *sender_name,
824 const gchar *object_path,
825 const gchar *interface_name,
826 const gchar *signal_name,
827 GVariant *parameters,
830 SignalSubscriptionData *data = user_data;
833 G_LOCK (signal_subscription_lock);
837 G_UNLOCK (signal_subscription_lock);
842 g_object_ref (proxy);
843 G_UNLOCK (signal_subscription_lock);
846 if (!proxy->priv->initialized)
849 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
852 if (proxy->priv->expected_interface != NULL)
854 const GDBusSignalInfo *info;
855 GVariantType *expected_type;
856 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
859 expected_type = _g_dbus_compute_complete_signature (info->args);
860 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
862 g_variant_type_free (expected_type);
865 g_variant_type_free (expected_type);
868 g_signal_emit (proxy,
869 signals[SIGNAL_SIGNAL],
876 g_object_unref (proxy);
879 /* ---------------------------------------------------------------------------------------------------- */
881 /* must hold properties_lock */
883 insert_property_checked (GDBusProxy *proxy,
884 gchar *property_name,
887 if (proxy->priv->expected_interface != NULL)
889 const GDBusPropertyInfo *info;
891 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
892 /* Ignore unknown properties */
896 /* Ignore properties with the wrong type */
897 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
901 g_hash_table_insert (proxy->priv->properties,
902 property_name, /* adopts string */
903 value); /* adopts value */
908 g_variant_unref (value);
909 g_free (property_name);
913 on_properties_changed (GDBusConnection *connection,
914 const gchar *sender_name,
915 const gchar *object_path,
916 const gchar *interface_name,
917 const gchar *signal_name,
918 GVariant *parameters,
921 SignalSubscriptionData *data = user_data;
923 const gchar *interface_name_for_signal;
924 GVariant *changed_properties;
925 gchar **invalidated_properties;
931 G_LOCK (signal_subscription_lock);
935 G_UNLOCK (signal_subscription_lock);
940 g_object_ref (proxy);
941 G_UNLOCK (signal_subscription_lock);
944 changed_properties = NULL;
945 invalidated_properties = NULL;
947 if (!proxy->priv->initialized)
950 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
953 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
955 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
956 g_variant_get_type_string (parameters));
960 g_variant_get (parameters,
962 &interface_name_for_signal,
964 &invalidated_properties);
966 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
969 G_LOCK (properties_lock);
971 g_variant_iter_init (&iter, changed_properties);
972 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
974 insert_property_checked (proxy,
975 key, /* adopts string */
976 value); /* adopts value */
979 for (n = 0; invalidated_properties[n] != NULL; n++)
981 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
984 G_UNLOCK (properties_lock);
987 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
990 invalidated_properties);
993 if (changed_properties != NULL)
994 g_variant_unref (changed_properties);
995 g_free (invalidated_properties);
997 g_object_unref (proxy);
1000 /* ---------------------------------------------------------------------------------------------------- */
1003 process_get_all_reply (GDBusProxy *proxy,
1009 guint num_properties;
1011 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1013 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1014 g_variant_get_type_string (result));
1018 G_LOCK (properties_lock);
1020 g_variant_get (result, "(a{sv})", &iter);
1021 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1023 insert_property_checked (proxy,
1024 key, /* adopts string */
1025 value); /* adopts value */
1027 g_variant_iter_free (iter);
1029 num_properties = g_hash_table_size (proxy->priv->properties);
1030 G_UNLOCK (properties_lock);
1032 /* Synthesize ::g-properties-changed changed */
1033 if (num_properties > 0)
1035 GVariant *changed_properties;
1036 const gchar *invalidated_properties[1] = {NULL};
1038 g_variant_get (result,
1040 &changed_properties);
1041 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1044 invalidated_properties);
1045 g_variant_unref (changed_properties);
1055 GCancellable *cancellable;
1057 } LoadPropertiesOnNameOwnerChangedData;
1060 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1064 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1072 result = g_dbus_connection_call_finish (connection,
1077 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1079 /* We just ignore if GetAll() is failing. Because this might happen
1080 * if the object has no properties at all. Or if the caller is
1081 * not authorized to see the properties.
1083 * Either way, apps can know about this by using
1084 * get_cached_property_names() or get_cached_property().
1086 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1087 * fact that GetAll() failed
1089 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1090 g_error_free (error);
1093 /* and finally we can notify */
1096 g_free (data->proxy->priv->name_owner);
1097 data->proxy->priv->name_owner = data->name_owner;
1098 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1100 G_LOCK (properties_lock);
1101 g_hash_table_remove_all (data->proxy->priv->properties);
1102 G_UNLOCK (properties_lock);
1105 process_get_all_reply (data->proxy, result);
1106 g_variant_unref (result);
1109 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1112 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1113 data->proxy->priv->get_all_cancellable = NULL;
1115 g_object_unref (data->proxy);
1116 g_object_unref (data->cancellable);
1117 g_free (data->name_owner);
1122 on_name_owner_changed (GDBusConnection *connection,
1123 const gchar *sender_name,
1124 const gchar *object_path,
1125 const gchar *interface_name,
1126 const gchar *signal_name,
1127 GVariant *parameters,
1130 SignalSubscriptionData *data = user_data;
1132 const gchar *old_owner;
1133 const gchar *new_owner;
1135 G_LOCK (signal_subscription_lock);
1136 proxy = data->proxy;
1139 G_UNLOCK (signal_subscription_lock);
1144 g_object_ref (proxy);
1145 G_UNLOCK (signal_subscription_lock);
1148 /* if we are already trying to load properties, cancel that */
1149 if (proxy->priv->get_all_cancellable != NULL)
1151 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1152 proxy->priv->get_all_cancellable = NULL;
1155 g_variant_get (parameters,
1161 if (strlen (new_owner) == 0)
1163 g_free (proxy->priv->name_owner);
1164 proxy->priv->name_owner = NULL;
1166 G_LOCK (properties_lock);
1168 /* Synthesize ::g-properties-changed changed */
1169 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1170 g_hash_table_size (proxy->priv->properties) > 0)
1172 GVariantBuilder builder;
1173 GPtrArray *invalidated_properties;
1174 GHashTableIter iter;
1177 /* Build changed_properties (always empty) and invalidated_properties ... */
1178 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1180 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1181 g_hash_table_iter_init (&iter, proxy->priv->properties);
1182 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1183 g_ptr_array_add (invalidated_properties, g_strdup (key));
1184 g_ptr_array_add (invalidated_properties, NULL);
1186 /* ... throw out the properties ... */
1187 g_hash_table_remove_all (proxy->priv->properties);
1189 G_UNLOCK (properties_lock);
1191 /* ... and finally emit the ::g-properties-changed signal */
1192 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1194 g_variant_builder_end (&builder) /* consumed */,
1195 (const gchar* const *) invalidated_properties->pdata);
1196 g_ptr_array_unref (invalidated_properties);
1200 G_UNLOCK (properties_lock);
1202 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1206 /* ignore duplicates - this can happen when activating the service */
1207 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1210 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1212 g_free (proxy->priv->name_owner);
1213 proxy->priv->name_owner = g_strdup (new_owner);
1214 G_LOCK (properties_lock);
1215 g_hash_table_remove_all (proxy->priv->properties);
1216 G_UNLOCK (properties_lock);
1217 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1221 LoadPropertiesOnNameOwnerChangedData *data;
1223 /* start loading properties.. only then emit notify::g-name-owner .. we
1224 * need to be able to cancel this in the event another NameOwnerChanged
1225 * signal suddenly happens
1228 g_assert (proxy->priv->get_all_cancellable == NULL);
1229 proxy->priv->get_all_cancellable = g_cancellable_new ();
1230 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1231 data->proxy = g_object_ref (proxy);
1232 data->cancellable = proxy->priv->get_all_cancellable;
1233 data->name_owner = g_strdup (new_owner);
1234 g_dbus_connection_call (proxy->priv->connection,
1236 proxy->priv->object_path,
1237 "org.freedesktop.DBus.Properties",
1239 g_variant_new ("(s)", proxy->priv->interface_name),
1240 G_VARIANT_TYPE ("(a{sv})"),
1241 G_DBUS_CALL_FLAGS_NONE,
1243 proxy->priv->get_all_cancellable,
1244 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1251 g_object_unref (proxy);
1254 /* ---------------------------------------------------------------------------------------------------- */
1259 GCancellable *cancellable;
1260 GSimpleAsyncResult *simple;
1264 async_init_data_free (AsyncInitData *data)
1266 g_object_unref (data->proxy);
1267 if (data->cancellable != NULL)
1268 g_object_unref (data->cancellable);
1269 g_object_unref (data->simple);
1274 async_init_get_all_cb (GDBusConnection *connection,
1278 AsyncInitData *data = user_data;
1283 result = g_dbus_connection_call_finish (connection,
1288 /* We just ignore if GetAll() is failing. Because this might happen
1289 * if the object has no properties at all. Or if the caller is
1290 * not authorized to see the properties.
1292 * Either way, apps can know about this by using
1293 * get_cached_property_names() or get_cached_property().
1295 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1296 * fact that GetAll() failed
1298 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1299 g_error_free (error);
1303 g_simple_async_result_set_op_res_gpointer (data->simple,
1305 (GDestroyNotify) g_variant_unref);
1308 g_simple_async_result_complete_in_idle (data->simple);
1309 async_init_data_free (data);
1314 async_init_get_name_owner_cb (GDBusConnection *connection,
1318 AsyncInitData *data = user_data;
1326 result = g_dbus_connection_call_finish (connection,
1331 if (error->domain == G_DBUS_ERROR &&
1332 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1334 g_error_free (error);
1338 g_simple_async_result_take_error (data->simple, error);
1339 g_simple_async_result_complete_in_idle (data->simple);
1340 async_init_data_free (data);
1346 g_variant_get (result,
1348 &data->proxy->priv->name_owner);
1349 g_variant_unref (result);
1353 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1355 /* load all properties asynchronously */
1356 g_dbus_connection_call (data->proxy->priv->connection,
1357 data->proxy->priv->name_owner,
1358 data->proxy->priv->object_path,
1359 "org.freedesktop.DBus.Properties",
1361 g_variant_new ("(s)", data->proxy->priv->interface_name),
1362 G_VARIANT_TYPE ("(a{sv})"),
1363 G_DBUS_CALL_FLAGS_NONE,
1366 (GAsyncReadyCallback) async_init_get_all_cb,
1371 g_simple_async_result_complete_in_idle (data->simple);
1372 async_init_data_free (data);
1380 async_init_call_get_name_owner (AsyncInitData *data)
1382 g_dbus_connection_call (data->proxy->priv->connection,
1383 "org.freedesktop.DBus", /* name */
1384 "/org/freedesktop/DBus", /* object path */
1385 "org.freedesktop.DBus", /* interface */
1387 g_variant_new ("(s)",
1388 data->proxy->priv->name),
1389 G_VARIANT_TYPE ("(s)"),
1390 G_DBUS_CALL_FLAGS_NONE,
1393 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1398 async_init_start_service_by_name_cb (GDBusConnection *connection,
1402 AsyncInitData *data = user_data;
1407 result = g_dbus_connection_call_finish (connection,
1412 /* Errors are not unexpected; the bus will reply e.g.
1414 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1415 * was not provided by any .service files
1417 * This doesn't mean that the name doesn't have an owner, just
1418 * that it's not provided by a .service file. So just proceed to
1419 * invoke GetNameOwner() if dealing with that error.
1421 if (error->domain == G_DBUS_ERROR &&
1422 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1424 g_error_free (error);
1428 g_prefix_error (&error,
1429 _("Error calling StartServiceByName for %s: "),
1430 data->proxy->priv->name);
1436 guint32 start_service_result;
1437 g_variant_get (result,
1439 &start_service_result);
1440 g_variant_unref (result);
1441 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1442 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1444 /* continue to invoke GetNameOwner() */
1448 error = g_error_new (G_IO_ERROR,
1450 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1451 start_service_result,
1452 data->proxy->priv->name);
1457 async_init_call_get_name_owner (data);
1461 g_warn_if_fail (error != NULL);
1462 g_simple_async_result_take_error (data->simple, error);
1463 g_simple_async_result_complete_in_idle (data->simple);
1464 async_init_data_free (data);
1468 async_init_call_start_service_by_name (AsyncInitData *data)
1470 g_dbus_connection_call (data->proxy->priv->connection,
1471 "org.freedesktop.DBus", /* name */
1472 "/org/freedesktop/DBus", /* object path */
1473 "org.freedesktop.DBus", /* interface */
1474 "StartServiceByName",
1475 g_variant_new ("(su)",
1476 data->proxy->priv->name,
1478 G_VARIANT_TYPE ("(u)"),
1479 G_DBUS_CALL_FLAGS_NONE,
1482 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1487 async_initable_init_second_async (GAsyncInitable *initable,
1489 GCancellable *cancellable,
1490 GAsyncReadyCallback callback,
1493 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1494 AsyncInitData *data;
1496 data = g_new0 (AsyncInitData, 1);
1497 data->proxy = g_object_ref (proxy);
1498 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1499 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1504 /* Check name ownership asynchronously - possibly also start the service */
1505 if (proxy->priv->name == NULL)
1508 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1510 else if (g_dbus_is_unique_name (proxy->priv->name))
1512 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1513 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1517 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1519 async_init_call_get_name_owner (data);
1523 async_init_call_start_service_by_name (data);
1529 async_initable_init_second_finish (GAsyncInitable *initable,
1533 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1534 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1540 if (g_simple_async_result_propagate_error (simple, error))
1543 result = g_simple_async_result_get_op_res_gpointer (simple);
1546 process_get_all_reply (proxy, result);
1552 proxy->priv->initialized = TRUE;
1556 /* ---------------------------------------------------------------------------------------------------- */
1559 async_initable_init_first (GAsyncInitable *initable)
1561 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1563 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1565 /* subscribe to PropertiesChanged() */
1566 proxy->priv->properties_changed_subscription_id =
1567 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1569 "org.freedesktop.DBus.Properties",
1570 "PropertiesChanged",
1571 proxy->priv->object_path,
1572 proxy->priv->interface_name,
1573 G_DBUS_SIGNAL_FLAGS_NONE,
1574 on_properties_changed,
1575 signal_subscription_ref (proxy->priv->signal_subscription_data),
1576 (GDestroyNotify) signal_subscription_unref);
1579 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1581 /* subscribe to all signals for the object */
1582 proxy->priv->signals_subscription_id =
1583 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1585 proxy->priv->interface_name,
1587 proxy->priv->object_path,
1589 G_DBUS_SIGNAL_FLAGS_NONE,
1591 signal_subscription_ref (proxy->priv->signal_subscription_data),
1592 (GDestroyNotify) signal_subscription_unref);
1595 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1597 proxy->priv->name_owner_changed_subscription_id =
1598 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1599 "org.freedesktop.DBus", /* name */
1600 "org.freedesktop.DBus", /* interface */
1601 "NameOwnerChanged", /* signal name */
1602 "/org/freedesktop/DBus", /* path */
1603 proxy->priv->name, /* arg0 */
1604 G_DBUS_SIGNAL_FLAGS_NONE,
1605 on_name_owner_changed,
1606 signal_subscription_ref (proxy->priv->signal_subscription_data),
1607 (GDestroyNotify) signal_subscription_unref);
1611 /* ---------------------------------------------------------------------------------------------------- */
1613 /* initialization is split into two parts - the first is the
1614 * non-blocing part that requires the callers GMainContext - the
1615 * second is a blocking part async part that doesn't require the
1616 * callers GMainContext.. we do this split so the code can be reused
1617 * in the GInitable implementation below.
1619 * Note that obtaining a GDBusConnection is not shared between the two
1627 GCancellable *cancellable;
1628 GAsyncReadyCallback callback;
1630 } GetConnectionData;
1633 get_connection_cb (GObject *source_object,
1637 GetConnectionData *data = user_data;
1641 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1642 if (data->proxy->priv->connection == NULL)
1644 GSimpleAsyncResult *simple;
1645 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1649 g_simple_async_result_take_error (simple, error);
1650 g_simple_async_result_complete_in_idle (simple);
1651 g_object_unref (simple);
1655 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1656 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1663 if (data->cancellable != NULL)
1664 g_object_unref (data->cancellable);
1666 g_object_unref (data->proxy);
1671 async_initable_init_async (GAsyncInitable *initable,
1673 GCancellable *cancellable,
1674 GAsyncReadyCallback callback,
1677 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1679 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1681 GetConnectionData *data;
1683 g_assert (proxy->priv->connection == NULL);
1685 data = g_new0 (GetConnectionData, 1);
1686 data->proxy = g_object_ref (proxy);
1687 data->io_priority = io_priority;
1688 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1689 data->callback = callback;
1690 data->user_data = user_data;
1691 g_bus_get (proxy->priv->bus_type,
1698 async_initable_init_first (initable);
1699 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1704 async_initable_init_finish (GAsyncInitable *initable,
1708 return async_initable_init_second_finish (initable, res, error);
1712 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1714 async_initable_iface->init_async = async_initable_init_async;
1715 async_initable_iface->init_finish = async_initable_init_finish;
1718 /* ---------------------------------------------------------------------------------------------------- */
1722 GMainContext *context;
1725 } InitableAsyncInitableData;
1728 async_initable_init_async_cb (GObject *source_object,
1732 InitableAsyncInitableData *data = user_data;
1733 data->res = g_object_ref (res);
1734 g_main_loop_quit (data->loop);
1737 /* Simply reuse the GAsyncInitable implementation but run the first
1738 * part (that is non-blocking and requires the callers GMainContext)
1739 * with the callers GMainContext.. and the second with a private
1740 * GMainContext (bug 621310 is slightly related).
1742 * Note that obtaining a GDBusConnection is not shared between the two
1746 initable_init (GInitable *initable,
1747 GCancellable *cancellable,
1750 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1751 InitableAsyncInitableData *data;
1756 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1758 g_assert (proxy->priv->connection == NULL);
1759 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1762 if (proxy->priv->connection == NULL)
1766 async_initable_init_first (G_ASYNC_INITABLE (initable));
1768 data = g_new0 (InitableAsyncInitableData, 1);
1769 data->context = g_main_context_new ();
1770 data->loop = g_main_loop_new (data->context, FALSE);
1772 g_main_context_push_thread_default (data->context);
1774 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1777 async_initable_init_async_cb,
1780 g_main_loop_run (data->loop);
1782 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1786 g_main_context_pop_thread_default (data->context);
1788 g_main_context_unref (data->context);
1789 g_main_loop_unref (data->loop);
1790 g_object_unref (data->res);
1799 initable_iface_init (GInitableIface *initable_iface)
1801 initable_iface->init = initable_init;
1804 /* ---------------------------------------------------------------------------------------------------- */
1808 * @connection: A #GDBusConnection.
1809 * @flags: Flags used when constructing the proxy.
1810 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1811 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1812 * @object_path: An object path.
1813 * @interface_name: A D-Bus interface name.
1814 * @cancellable: A #GCancellable or %NULL.
1815 * @callback: Callback function to invoke when the proxy is ready.
1816 * @user_data: User data to pass to @callback.
1818 * Creates a proxy for accessing @interface_name on the remote object
1819 * at @object_path owned by @name at @connection and asynchronously
1820 * loads D-Bus properties unless the
1821 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1822 * the #GDBusProxy::g-properties-changed signal to get notified about
1825 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1826 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1827 * to handle signals from the remote object.
1829 * If @name is a well-known name and the
1830 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1831 * owner currently exists, the message bus will be requested to launch
1832 * a name owner for the name.
1834 * This is a failable asynchronous constructor - when the proxy is
1835 * ready, @callback will be invoked and you can use
1836 * g_dbus_proxy_new_finish() to get the result.
1838 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1840 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1845 g_dbus_proxy_new (GDBusConnection *connection,
1846 GDBusProxyFlags flags,
1847 GDBusInterfaceInfo *info,
1849 const gchar *object_path,
1850 const gchar *interface_name,
1851 GCancellable *cancellable,
1852 GAsyncReadyCallback callback,
1855 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1856 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1857 g_return_if_fail (g_variant_is_object_path (object_path));
1858 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1860 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1866 "g-interface-info", info,
1868 "g-connection", connection,
1869 "g-object-path", object_path,
1870 "g-interface-name", interface_name,
1875 * g_dbus_proxy_new_finish:
1876 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1877 * @error: Return location for error or %NULL.
1879 * Finishes creating a #GDBusProxy.
1881 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1886 g_dbus_proxy_new_finish (GAsyncResult *res,
1890 GObject *source_object;
1892 source_object = g_async_result_get_source_object (res);
1893 g_assert (source_object != NULL);
1895 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1898 g_object_unref (source_object);
1901 return G_DBUS_PROXY (object);
1907 * g_dbus_proxy_new_sync:
1908 * @connection: A #GDBusConnection.
1909 * @flags: Flags used when constructing the proxy.
1910 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1911 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1912 * @object_path: An object path.
1913 * @interface_name: A D-Bus interface name.
1914 * @cancellable: (allow-none): A #GCancellable or %NULL.
1915 * @error: (allow-none): Return location for error or %NULL.
1917 * Creates a proxy for accessing @interface_name on the remote object
1918 * at @object_path owned by @name at @connection and synchronously
1919 * loads D-Bus properties unless the
1920 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1922 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1923 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1924 * to handle signals from the remote object.
1926 * If @name is a well-known name and the
1927 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1928 * owner currently exists, the message bus will be requested to launch
1929 * a name owner for the name.
1931 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1932 * and g_dbus_proxy_new_finish() for the asynchronous version.
1934 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1936 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1941 g_dbus_proxy_new_sync (GDBusConnection *connection,
1942 GDBusProxyFlags flags,
1943 GDBusInterfaceInfo *info,
1945 const gchar *object_path,
1946 const gchar *interface_name,
1947 GCancellable *cancellable,
1950 GInitable *initable;
1952 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1953 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1954 g_dbus_is_name (name), NULL);
1955 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1956 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1958 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1962 "g-interface-info", info,
1964 "g-connection", connection,
1965 "g-object-path", object_path,
1966 "g-interface-name", interface_name,
1968 if (initable != NULL)
1969 return G_DBUS_PROXY (initable);
1974 /* ---------------------------------------------------------------------------------------------------- */
1977 * g_dbus_proxy_new_for_bus:
1978 * @bus_type: A #GBusType.
1979 * @flags: Flags used when constructing the proxy.
1980 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1981 * @name: A bus name (well-known or unique).
1982 * @object_path: An object path.
1983 * @interface_name: A D-Bus interface name.
1984 * @cancellable: A #GCancellable or %NULL.
1985 * @callback: Callback function to invoke when the proxy is ready.
1986 * @user_data: User data to pass to @callback.
1988 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1990 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1995 g_dbus_proxy_new_for_bus (GBusType bus_type,
1996 GDBusProxyFlags flags,
1997 GDBusInterfaceInfo *info,
1999 const gchar *object_path,
2000 const gchar *interface_name,
2001 GCancellable *cancellable,
2002 GAsyncReadyCallback callback,
2005 g_return_if_fail (g_dbus_is_name (name));
2006 g_return_if_fail (g_variant_is_object_path (object_path));
2007 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2009 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2015 "g-interface-info", info,
2017 "g-bus-type", bus_type,
2018 "g-object-path", object_path,
2019 "g-interface-name", interface_name,
2024 * g_dbus_proxy_new_for_bus_finish:
2025 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2026 * @error: Return location for error or %NULL.
2028 * Finishes creating a #GDBusProxy.
2030 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2035 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2038 return g_dbus_proxy_new_finish (res, error);
2042 * g_dbus_proxy_new_for_bus_sync:
2043 * @bus_type: A #GBusType.
2044 * @flags: Flags used when constructing the proxy.
2045 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2046 * that @proxy conforms to or %NULL.
2047 * @name: A bus name (well-known or unique).
2048 * @object_path: An object path.
2049 * @interface_name: A D-Bus interface name.
2050 * @cancellable: A #GCancellable or %NULL.
2051 * @error: Return location for error or %NULL.
2053 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2055 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2057 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2062 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2063 GDBusProxyFlags flags,
2064 GDBusInterfaceInfo *info,
2066 const gchar *object_path,
2067 const gchar *interface_name,
2068 GCancellable *cancellable,
2071 GInitable *initable;
2073 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2074 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2075 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2077 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2081 "g-interface-info", info,
2083 "g-bus-type", bus_type,
2084 "g-object-path", object_path,
2085 "g-interface-name", interface_name,
2087 if (initable != NULL)
2088 return G_DBUS_PROXY (initable);
2093 /* ---------------------------------------------------------------------------------------------------- */
2096 * g_dbus_proxy_get_connection:
2097 * @proxy: A #GDBusProxy.
2099 * Gets the connection @proxy is for.
2101 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2106 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2108 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2109 return proxy->priv->connection;
2113 * g_dbus_proxy_get_flags:
2114 * @proxy: A #GDBusProxy.
2116 * Gets the flags that @proxy was constructed with.
2118 * Returns: Flags from the #GDBusProxyFlags enumeration.
2123 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2125 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2126 return proxy->priv->flags;
2130 * g_dbus_proxy_get_name:
2131 * @proxy: A #GDBusProxy.
2133 * Gets the name that @proxy was constructed for.
2135 * Returns: A string owned by @proxy. Do not free.
2140 g_dbus_proxy_get_name (GDBusProxy *proxy)
2142 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2143 return proxy->priv->name;
2147 * g_dbus_proxy_get_name_owner:
2148 * @proxy: A #GDBusProxy.
2150 * The unique name that owns the name that @proxy is for or %NULL if
2151 * no-one currently owns that name. You may connect to the
2152 * #GObject::notify signal to track changes to the
2153 * #GDBusProxy:g-name-owner property.
2155 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2160 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2162 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2163 return g_strdup (proxy->priv->name_owner);
2167 * g_dbus_proxy_get_object_path:
2168 * @proxy: A #GDBusProxy.
2170 * Gets the object path @proxy is for.
2172 * Returns: A string owned by @proxy. Do not free.
2177 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2179 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2180 return proxy->priv->object_path;
2184 * g_dbus_proxy_get_interface_name:
2185 * @proxy: A #GDBusProxy.
2187 * Gets the D-Bus interface name @proxy is for.
2189 * Returns: A string owned by @proxy. Do not free.
2194 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2196 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2197 return proxy->priv->interface_name;
2201 * g_dbus_proxy_get_default_timeout:
2202 * @proxy: A #GDBusProxy.
2204 * Gets the timeout to use if -1 (specifying default timeout) is
2205 * passed as @timeout_msec in the g_dbus_proxy_call() and
2206 * g_dbus_proxy_call_sync() functions.
2208 * See the #GDBusProxy:g-default-timeout property for more details.
2210 * Returns: Timeout to use for @proxy.
2215 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2217 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2218 return proxy->priv->timeout_msec;
2222 * g_dbus_proxy_set_default_timeout:
2223 * @proxy: A #GDBusProxy.
2224 * @timeout_msec: Timeout in milliseconds.
2226 * Sets the timeout to use if -1 (specifying default timeout) is
2227 * passed as @timeout_msec in the g_dbus_proxy_call() and
2228 * g_dbus_proxy_call_sync() functions.
2230 * See the #GDBusProxy:g-default-timeout property for more details.
2235 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2238 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2239 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2241 /* TODO: locking? */
2242 if (proxy->priv->timeout_msec != timeout_msec)
2244 proxy->priv->timeout_msec = timeout_msec;
2245 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2250 * g_dbus_proxy_get_interface_info:
2251 * @proxy: A #GDBusProxy
2253 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2254 * interface that @proxy conforms to.
2256 * See the #GDBusProxy:g-interface-info property for more details.
2258 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2259 * object, it is owned by @proxy.
2263 GDBusInterfaceInfo *
2264 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2266 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2267 return proxy->priv->expected_interface;
2271 * g_dbus_proxy_set_interface_info:
2272 * @proxy: A #GDBusProxy
2273 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2275 * Ensure that interactions with @proxy conform to the given
2276 * interface. For example, when completing a method call, if the type
2277 * signature of the message isn't what's expected, the given #GError
2278 * is set. Signals that have a type signature mismatch are simply
2281 * See the #GDBusProxy:g-interface-info property for more details.
2286 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2287 GDBusInterfaceInfo *info)
2289 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2290 if (proxy->priv->expected_interface != NULL)
2292 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2293 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2295 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2296 if (proxy->priv->expected_interface != NULL)
2297 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2300 /* ---------------------------------------------------------------------------------------------------- */
2303 maybe_split_method_name (const gchar *method_name,
2304 gchar **out_interface_name,
2305 const gchar **out_method_name)
2310 g_assert (out_interface_name != NULL);
2311 g_assert (out_method_name != NULL);
2312 *out_interface_name = NULL;
2313 *out_method_name = NULL;
2315 if (strchr (method_name, '.') != NULL)
2320 p = g_strdup (method_name);
2321 last_dot = strrchr (p, '.');
2324 *out_interface_name = p;
2325 *out_method_name = last_dot + 1;
2337 GUnixFDList *fd_list;
2342 reply_data_free (ReplyData *data)
2344 g_variant_unref (data->value);
2346 if (data->fd_list != NULL)
2347 g_object_unref (data->fd_list);
2349 g_slice_free (ReplyData, data);
2353 reply_cb (GDBusConnection *connection,
2357 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2361 GUnixFDList *fd_list;
2366 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2371 value = g_dbus_connection_call_finish (connection,
2377 g_simple_async_result_take_error (simple, error);
2382 data = g_slice_new0 (ReplyData);
2383 data->value = value;
2385 data->fd_list = fd_list;
2387 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2390 /* no need to complete in idle since the method GDBusConnection already does */
2391 g_simple_async_result_complete (simple);
2392 g_object_unref (simple);
2395 static const GDBusMethodInfo *
2396 lookup_method_info_or_warn (GDBusProxy *proxy,
2397 const gchar *method_name)
2399 const GDBusMethodInfo *info;
2401 if (proxy->priv->expected_interface == NULL)
2404 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2407 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2408 method_name, proxy->priv->expected_interface->name);
2414 static const gchar *
2415 get_destination_for_call (GDBusProxy *proxy)
2421 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2422 * is never NULL and always the same as proxy->priv->name. We use this
2423 * knowledge to avoid checking if proxy->priv->name is a unique or
2426 ret = proxy->priv->name_owner;
2430 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2433 ret = proxy->priv->name;
2439 /* ---------------------------------------------------------------------------------------------------- */
2442 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2443 const gchar *method_name,
2444 GVariant *parameters,
2445 GDBusCallFlags flags,
2447 GUnixFDList *fd_list,
2448 GCancellable *cancellable,
2449 GAsyncReadyCallback callback,
2452 GSimpleAsyncResult *simple;
2454 gchar *split_interface_name;
2455 const gchar *split_method_name;
2456 const gchar *target_method_name;
2457 const gchar *target_interface_name;
2458 const gchar *destination;
2459 GVariantType *reply_type;
2461 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2462 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2463 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2464 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2466 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2468 g_return_if_fail (fd_list == NULL);
2472 split_interface_name = NULL;
2474 simple = g_simple_async_result_new (G_OBJECT (proxy),
2477 g_dbus_proxy_call_internal);
2479 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2480 target_method_name = was_split ? split_method_name : method_name;
2481 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2483 /* Warn if method is unexpected (cf. :g-interface-info) */
2486 const GDBusMethodInfo *expected_method_info;
2487 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2488 if (expected_method_info != NULL)
2489 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2493 if (proxy->priv->name != NULL)
2495 destination = get_destination_for_call (proxy);
2496 if (destination == NULL)
2498 g_simple_async_result_set_error (simple,
2501 _("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"));
2507 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2509 proxy->priv->object_path,
2510 target_interface_name,
2515 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2518 (GAsyncReadyCallback) reply_cb,
2521 g_dbus_connection_call (proxy->priv->connection,
2523 proxy->priv->object_path,
2524 target_interface_name,
2529 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2531 (GAsyncReadyCallback) reply_cb,
2536 if (reply_type != NULL)
2537 g_variant_type_free (reply_type);
2539 g_free (split_interface_name);
2543 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2544 GUnixFDList **out_fd_list,
2548 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2552 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2553 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2554 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2556 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2560 if (g_simple_async_result_propagate_error (simple, error))
2563 data = g_simple_async_result_get_op_res_gpointer (simple);
2564 value = g_variant_ref (data->value);
2566 if (out_fd_list != NULL)
2567 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2575 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2576 const gchar *method_name,
2577 GVariant *parameters,
2578 GDBusCallFlags flags,
2580 GUnixFDList *fd_list,
2581 GUnixFDList **out_fd_list,
2582 GCancellable *cancellable,
2587 gchar *split_interface_name;
2588 const gchar *split_method_name;
2589 const gchar *target_method_name;
2590 const gchar *target_interface_name;
2591 const gchar *destination;
2592 GVariantType *reply_type;
2594 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2595 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2596 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2597 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2599 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2601 g_return_val_if_fail (fd_list == NULL, NULL);
2603 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2607 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2608 target_method_name = was_split ? split_method_name : method_name;
2609 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2611 /* Warn if method is unexpected (cf. :g-interface-info) */
2614 const GDBusMethodInfo *expected_method_info;
2615 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2616 if (expected_method_info != NULL)
2617 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2621 if (proxy->priv->name != NULL)
2623 destination = get_destination_for_call (proxy);
2624 if (destination == NULL)
2626 g_set_error_literal (error,
2629 _("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"));
2636 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2638 proxy->priv->object_path,
2639 target_interface_name,
2644 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2650 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2652 proxy->priv->object_path,
2653 target_interface_name,
2658 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2664 if (reply_type != NULL)
2665 g_variant_type_free (reply_type);
2667 g_free (split_interface_name);
2672 /* ---------------------------------------------------------------------------------------------------- */
2675 * g_dbus_proxy_call:
2676 * @proxy: A #GDBusProxy.
2677 * @method_name: Name of method to invoke.
2678 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2679 * @flags: Flags from the #GDBusCallFlags enumeration.
2680 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2681 * "infinite") or -1 to use the proxy default timeout.
2682 * @cancellable: A #GCancellable or %NULL.
2683 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2684 * care about the result of the method invocation.
2685 * @user_data: The data to pass to @callback.
2687 * Asynchronously invokes the @method_name method on @proxy.
2689 * If @method_name contains any dots, then @name is split into interface and
2690 * method name parts. This allows using @proxy for invoking methods on
2693 * If the #GDBusConnection associated with @proxy is closed then
2694 * the operation will fail with %G_IO_ERROR_CLOSED. If
2695 * @cancellable is canceled, the operation will fail with
2696 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2697 * compatible with the D-Bus protocol, the operation fails with
2698 * %G_IO_ERROR_INVALID_ARGUMENT.
2700 * If the @parameters #GVariant is floating, it is consumed. This allows
2701 * convenient 'inline' use of g_variant_new(), e.g.:
2703 * g_dbus_proxy_call (proxy,
2705 * g_variant_new ("(ss)",
2708 * G_DBUS_CALL_FLAGS_NONE,
2711 * (GAsyncReadyCallback) two_strings_done,
2715 * This is an asynchronous method. When the operation is finished,
2716 * @callback will be invoked in the
2717 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2718 * of the thread you are calling this method from.
2719 * You can then call g_dbus_proxy_call_finish() to get the result of
2720 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2721 * version of this method.
2726 g_dbus_proxy_call (GDBusProxy *proxy,
2727 const gchar *method_name,
2728 GVariant *parameters,
2729 GDBusCallFlags flags,
2731 GCancellable *cancellable,
2732 GAsyncReadyCallback callback,
2735 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2739 * g_dbus_proxy_call_finish:
2740 * @proxy: A #GDBusProxy.
2741 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2742 * @error: Return location for error or %NULL.
2744 * Finishes an operation started with g_dbus_proxy_call().
2746 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2747 * return values. Free with g_variant_unref().
2752 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2756 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2760 * g_dbus_proxy_call_sync:
2761 * @proxy: A #GDBusProxy.
2762 * @method_name: Name of method to invoke.
2763 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2764 * or %NULL if not passing parameters.
2765 * @flags: Flags from the #GDBusCallFlags enumeration.
2766 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2767 * "infinite") or -1 to use the proxy default timeout.
2768 * @cancellable: A #GCancellable or %NULL.
2769 * @error: Return location for error or %NULL.
2771 * Synchronously invokes the @method_name method on @proxy.
2773 * If @method_name contains any dots, then @name is split into interface and
2774 * method name parts. This allows using @proxy for invoking methods on
2777 * If the #GDBusConnection associated with @proxy is disconnected then
2778 * the operation will fail with %G_IO_ERROR_CLOSED. If
2779 * @cancellable is canceled, the operation will fail with
2780 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2781 * compatible with the D-Bus protocol, the operation fails with
2782 * %G_IO_ERROR_INVALID_ARGUMENT.
2784 * If the @parameters #GVariant is floating, it is consumed. This allows
2785 * convenient 'inline' use of g_variant_new(), e.g.:
2787 * g_dbus_proxy_call_sync (proxy,
2789 * g_variant_new ("(ss)",
2792 * G_DBUS_CALL_FLAGS_NONE,
2798 * The calling thread is blocked until a reply is received. See
2799 * g_dbus_proxy_call() for the asynchronous version of this
2802 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2803 * return values. Free with g_variant_unref().
2808 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2809 const gchar *method_name,
2810 GVariant *parameters,
2811 GDBusCallFlags flags,
2813 GCancellable *cancellable,
2816 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2819 /* ---------------------------------------------------------------------------------------------------- */
2824 * g_dbus_proxy_call_with_unix_fd_list:
2825 * @proxy: A #GDBusProxy.
2826 * @method_name: Name of method to invoke.
2827 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2828 * @flags: Flags from the #GDBusCallFlags enumeration.
2829 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2830 * "infinite") or -1 to use the proxy default timeout.
2831 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2832 * @cancellable: A #GCancellable or %NULL.
2833 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2834 * care about the result of the method invocation.
2835 * @user_data: The data to pass to @callback.
2837 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2839 * This method is only available on UNIX.
2844 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
2845 const gchar *method_name,
2846 GVariant *parameters,
2847 GDBusCallFlags flags,
2849 GUnixFDList *fd_list,
2850 GCancellable *cancellable,
2851 GAsyncReadyCallback callback,
2854 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2858 * g_dbus_proxy_call_with_unix_fd_list_finish:
2859 * @proxy: A #GDBusProxy.
2860 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2861 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2862 * @error: Return location for error or %NULL.
2864 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2866 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2867 * return values. Free with g_variant_unref().
2872 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
2873 GUnixFDList **out_fd_list,
2877 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2881 * g_dbus_proxy_call_with_unix_fd_list_sync:
2882 * @proxy: A #GDBusProxy.
2883 * @method_name: Name of method to invoke.
2884 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2885 * or %NULL if not passing parameters.
2886 * @flags: Flags from the #GDBusCallFlags enumeration.
2887 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2888 * "infinite") or -1 to use the proxy default timeout.
2889 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2890 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2891 * @cancellable: A #GCancellable or %NULL.
2892 * @error: Return location for error or %NULL.
2894 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2896 * This method is only available on UNIX.
2898 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2899 * return values. Free with g_variant_unref().
2904 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
2905 const gchar *method_name,
2906 GVariant *parameters,
2907 GDBusCallFlags flags,
2909 GUnixFDList *fd_list,
2910 GUnixFDList **out_fd_list,
2911 GCancellable *cancellable,
2914 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2917 #endif /* G_OS_UNIX */
2919 /* ---------------------------------------------------------------------------------------------------- */
2921 static GDBusInterfaceInfo *
2922 _g_dbus_proxy_get_info (GDBusInterface *interface)
2924 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2925 return g_dbus_proxy_get_interface_info (proxy);
2928 static GDBusObject *
2929 _g_dbus_proxy_get_object (GDBusInterface *interface)
2931 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2932 return proxy->priv->object;
2936 _g_dbus_proxy_set_object (GDBusInterface *interface,
2937 GDBusObject *object)
2939 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2940 if (proxy->priv->object != NULL)
2941 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2942 proxy->priv->object = object;
2943 if (proxy->priv->object != NULL)
2944 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2948 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2950 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
2951 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2952 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2955 /* ---------------------------------------------------------------------------------------------------- */