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_take_string (value, g_dbus_proxy_get_name_owner (proxy));
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, g_dbus_proxy_get_default_timeout (proxy));
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 changed_properties = NULL;
932 invalidated_properties = NULL;
934 G_LOCK (signal_subscription_lock);
938 G_UNLOCK (signal_subscription_lock);
943 g_object_ref (proxy);
944 G_UNLOCK (signal_subscription_lock);
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;
1327 result = g_dbus_connection_call_finish (connection,
1332 if (error->domain == G_DBUS_ERROR &&
1333 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1335 g_error_free (error);
1339 g_simple_async_result_take_error (data->simple, error);
1340 g_simple_async_result_complete_in_idle (data->simple);
1341 async_init_data_free (data);
1347 g_variant_get (result,
1349 &data->proxy->priv->name_owner);
1350 g_variant_unref (result);
1356 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1358 /* Don't load properties if the API user doesn't want them */
1361 else if (data->proxy->priv->name_owner == NULL &&
1362 data->proxy->priv->name != NULL)
1364 /* Don't attempt to load properties if the name_owner is NULL (which
1365 * usually means the name isn't owned), unless name is also NULL (which
1366 * means we actually wanted to talk to the directly-connected process -
1367 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1374 /* load all properties asynchronously */
1375 g_dbus_connection_call (data->proxy->priv->connection,
1376 data->proxy->priv->name_owner,
1377 data->proxy->priv->object_path,
1378 "org.freedesktop.DBus.Properties",
1380 g_variant_new ("(s)", data->proxy->priv->interface_name),
1381 G_VARIANT_TYPE ("(a{sv})"),
1382 G_DBUS_CALL_FLAGS_NONE,
1385 (GAsyncReadyCallback) async_init_get_all_cb,
1390 g_simple_async_result_complete_in_idle (data->simple);
1391 async_init_data_free (data);
1399 async_init_call_get_name_owner (AsyncInitData *data)
1401 g_dbus_connection_call (data->proxy->priv->connection,
1402 "org.freedesktop.DBus", /* name */
1403 "/org/freedesktop/DBus", /* object path */
1404 "org.freedesktop.DBus", /* interface */
1406 g_variant_new ("(s)",
1407 data->proxy->priv->name),
1408 G_VARIANT_TYPE ("(s)"),
1409 G_DBUS_CALL_FLAGS_NONE,
1412 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1417 async_init_start_service_by_name_cb (GDBusConnection *connection,
1421 AsyncInitData *data = user_data;
1426 result = g_dbus_connection_call_finish (connection,
1431 /* Errors are not unexpected; the bus will reply e.g.
1433 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1434 * was not provided by any .service files
1436 * This doesn't mean that the name doesn't have an owner, just
1437 * that it's not provided by a .service file. So just proceed to
1438 * invoke GetNameOwner() if dealing with that error.
1440 if (error->domain == G_DBUS_ERROR &&
1441 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1443 g_error_free (error);
1447 g_prefix_error (&error,
1448 _("Error calling StartServiceByName for %s: "),
1449 data->proxy->priv->name);
1455 guint32 start_service_result;
1456 g_variant_get (result,
1458 &start_service_result);
1459 g_variant_unref (result);
1460 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1461 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1463 /* continue to invoke GetNameOwner() */
1467 error = g_error_new (G_IO_ERROR,
1469 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1470 start_service_result,
1471 data->proxy->priv->name);
1476 async_init_call_get_name_owner (data);
1480 g_warn_if_fail (error != NULL);
1481 g_simple_async_result_take_error (data->simple, error);
1482 g_simple_async_result_complete_in_idle (data->simple);
1483 async_init_data_free (data);
1487 async_init_call_start_service_by_name (AsyncInitData *data)
1489 g_dbus_connection_call (data->proxy->priv->connection,
1490 "org.freedesktop.DBus", /* name */
1491 "/org/freedesktop/DBus", /* object path */
1492 "org.freedesktop.DBus", /* interface */
1493 "StartServiceByName",
1494 g_variant_new ("(su)",
1495 data->proxy->priv->name,
1497 G_VARIANT_TYPE ("(u)"),
1498 G_DBUS_CALL_FLAGS_NONE,
1501 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1506 async_initable_init_second_async (GAsyncInitable *initable,
1508 GCancellable *cancellable,
1509 GAsyncReadyCallback callback,
1512 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1513 AsyncInitData *data;
1515 data = g_new0 (AsyncInitData, 1);
1516 data->proxy = g_object_ref (proxy);
1517 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1518 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1523 /* Check name ownership asynchronously - possibly also start the service */
1524 if (proxy->priv->name == NULL)
1527 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1529 else if (g_dbus_is_unique_name (proxy->priv->name))
1531 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1532 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1536 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1538 async_init_call_get_name_owner (data);
1542 async_init_call_start_service_by_name (data);
1548 async_initable_init_second_finish (GAsyncInitable *initable,
1552 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1553 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1559 if (g_simple_async_result_propagate_error (simple, error))
1562 result = g_simple_async_result_get_op_res_gpointer (simple);
1565 process_get_all_reply (proxy, result);
1571 proxy->priv->initialized = TRUE;
1575 /* ---------------------------------------------------------------------------------------------------- */
1578 async_initable_init_first (GAsyncInitable *initable)
1580 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1582 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1584 /* subscribe to PropertiesChanged() */
1585 proxy->priv->properties_changed_subscription_id =
1586 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1588 "org.freedesktop.DBus.Properties",
1589 "PropertiesChanged",
1590 proxy->priv->object_path,
1591 proxy->priv->interface_name,
1592 G_DBUS_SIGNAL_FLAGS_NONE,
1593 on_properties_changed,
1594 signal_subscription_ref (proxy->priv->signal_subscription_data),
1595 (GDestroyNotify) signal_subscription_unref);
1598 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1600 /* subscribe to all signals for the object */
1601 proxy->priv->signals_subscription_id =
1602 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1604 proxy->priv->interface_name,
1606 proxy->priv->object_path,
1608 G_DBUS_SIGNAL_FLAGS_NONE,
1610 signal_subscription_ref (proxy->priv->signal_subscription_data),
1611 (GDestroyNotify) signal_subscription_unref);
1614 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1616 proxy->priv->name_owner_changed_subscription_id =
1617 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1618 "org.freedesktop.DBus", /* name */
1619 "org.freedesktop.DBus", /* interface */
1620 "NameOwnerChanged", /* signal name */
1621 "/org/freedesktop/DBus", /* path */
1622 proxy->priv->name, /* arg0 */
1623 G_DBUS_SIGNAL_FLAGS_NONE,
1624 on_name_owner_changed,
1625 signal_subscription_ref (proxy->priv->signal_subscription_data),
1626 (GDestroyNotify) signal_subscription_unref);
1630 /* ---------------------------------------------------------------------------------------------------- */
1632 /* initialization is split into two parts - the first is the
1633 * non-blocing part that requires the callers GMainContext - the
1634 * second is a blocking part async part that doesn't require the
1635 * callers GMainContext.. we do this split so the code can be reused
1636 * in the GInitable implementation below.
1638 * Note that obtaining a GDBusConnection is not shared between the two
1646 GCancellable *cancellable;
1647 GAsyncReadyCallback callback;
1649 } GetConnectionData;
1652 get_connection_cb (GObject *source_object,
1656 GetConnectionData *data = user_data;
1660 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1661 if (data->proxy->priv->connection == NULL)
1663 GSimpleAsyncResult *simple;
1664 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1668 g_simple_async_result_take_error (simple, error);
1669 g_simple_async_result_complete_in_idle (simple);
1670 g_object_unref (simple);
1674 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1675 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1682 if (data->cancellable != NULL)
1683 g_object_unref (data->cancellable);
1685 g_object_unref (data->proxy);
1690 async_initable_init_async (GAsyncInitable *initable,
1692 GCancellable *cancellable,
1693 GAsyncReadyCallback callback,
1696 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1698 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1700 GetConnectionData *data;
1702 g_assert (proxy->priv->connection == NULL);
1704 data = g_new0 (GetConnectionData, 1);
1705 data->proxy = g_object_ref (proxy);
1706 data->io_priority = io_priority;
1707 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1708 data->callback = callback;
1709 data->user_data = user_data;
1710 g_bus_get (proxy->priv->bus_type,
1717 async_initable_init_first (initable);
1718 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1723 async_initable_init_finish (GAsyncInitable *initable,
1727 return async_initable_init_second_finish (initable, res, error);
1731 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1733 async_initable_iface->init_async = async_initable_init_async;
1734 async_initable_iface->init_finish = async_initable_init_finish;
1737 /* ---------------------------------------------------------------------------------------------------- */
1741 GMainContext *context;
1744 } InitableAsyncInitableData;
1747 async_initable_init_async_cb (GObject *source_object,
1751 InitableAsyncInitableData *data = user_data;
1752 data->res = g_object_ref (res);
1753 g_main_loop_quit (data->loop);
1756 /* Simply reuse the GAsyncInitable implementation but run the first
1757 * part (that is non-blocking and requires the callers GMainContext)
1758 * with the callers GMainContext.. and the second with a private
1759 * GMainContext (bug 621310 is slightly related).
1761 * Note that obtaining a GDBusConnection is not shared between the two
1765 initable_init (GInitable *initable,
1766 GCancellable *cancellable,
1769 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1770 InitableAsyncInitableData *data;
1775 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1777 g_assert (proxy->priv->connection == NULL);
1778 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1781 if (proxy->priv->connection == NULL)
1785 async_initable_init_first (G_ASYNC_INITABLE (initable));
1787 data = g_new0 (InitableAsyncInitableData, 1);
1788 data->context = g_main_context_new ();
1789 data->loop = g_main_loop_new (data->context, FALSE);
1791 g_main_context_push_thread_default (data->context);
1793 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1796 async_initable_init_async_cb,
1799 g_main_loop_run (data->loop);
1801 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1805 g_main_context_pop_thread_default (data->context);
1807 g_main_context_unref (data->context);
1808 g_main_loop_unref (data->loop);
1809 g_object_unref (data->res);
1818 initable_iface_init (GInitableIface *initable_iface)
1820 initable_iface->init = initable_init;
1823 /* ---------------------------------------------------------------------------------------------------- */
1827 * @connection: A #GDBusConnection.
1828 * @flags: Flags used when constructing the proxy.
1829 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1830 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1831 * @object_path: An object path.
1832 * @interface_name: A D-Bus interface name.
1833 * @cancellable: A #GCancellable or %NULL.
1834 * @callback: Callback function to invoke when the proxy is ready.
1835 * @user_data: User data to pass to @callback.
1837 * Creates a proxy for accessing @interface_name on the remote object
1838 * at @object_path owned by @name at @connection and asynchronously
1839 * loads D-Bus properties unless the
1840 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1841 * the #GDBusProxy::g-properties-changed signal to get notified about
1844 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1845 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1846 * to handle signals from the remote object.
1848 * If @name is a well-known name and the
1849 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1850 * owner currently exists, the message bus will be requested to launch
1851 * a name owner for the name.
1853 * This is a failable asynchronous constructor - when the proxy is
1854 * ready, @callback will be invoked and you can use
1855 * g_dbus_proxy_new_finish() to get the result.
1857 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1859 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1864 g_dbus_proxy_new (GDBusConnection *connection,
1865 GDBusProxyFlags flags,
1866 GDBusInterfaceInfo *info,
1868 const gchar *object_path,
1869 const gchar *interface_name,
1870 GCancellable *cancellable,
1871 GAsyncReadyCallback callback,
1874 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1875 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1876 g_return_if_fail (g_variant_is_object_path (object_path));
1877 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1879 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1885 "g-interface-info", info,
1887 "g-connection", connection,
1888 "g-object-path", object_path,
1889 "g-interface-name", interface_name,
1894 * g_dbus_proxy_new_finish:
1895 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1896 * @error: Return location for error or %NULL.
1898 * Finishes creating a #GDBusProxy.
1900 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1905 g_dbus_proxy_new_finish (GAsyncResult *res,
1909 GObject *source_object;
1911 source_object = g_async_result_get_source_object (res);
1912 g_assert (source_object != NULL);
1914 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1917 g_object_unref (source_object);
1920 return G_DBUS_PROXY (object);
1926 * g_dbus_proxy_new_sync:
1927 * @connection: A #GDBusConnection.
1928 * @flags: Flags used when constructing the proxy.
1929 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1930 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1931 * @object_path: An object path.
1932 * @interface_name: A D-Bus interface name.
1933 * @cancellable: (allow-none): A #GCancellable or %NULL.
1934 * @error: (allow-none): Return location for error or %NULL.
1936 * Creates a proxy for accessing @interface_name on the remote object
1937 * at @object_path owned by @name at @connection and synchronously
1938 * loads D-Bus properties unless the
1939 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1941 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1942 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1943 * to handle signals from the remote object.
1945 * If @name is a well-known name and the
1946 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1947 * owner currently exists, the message bus will be requested to launch
1948 * a name owner for the name.
1950 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1951 * and g_dbus_proxy_new_finish() for the asynchronous version.
1953 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1955 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1960 g_dbus_proxy_new_sync (GDBusConnection *connection,
1961 GDBusProxyFlags flags,
1962 GDBusInterfaceInfo *info,
1964 const gchar *object_path,
1965 const gchar *interface_name,
1966 GCancellable *cancellable,
1969 GInitable *initable;
1971 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1972 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1973 g_dbus_is_name (name), NULL);
1974 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1975 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1977 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1981 "g-interface-info", info,
1983 "g-connection", connection,
1984 "g-object-path", object_path,
1985 "g-interface-name", interface_name,
1987 if (initable != NULL)
1988 return G_DBUS_PROXY (initable);
1993 /* ---------------------------------------------------------------------------------------------------- */
1996 * g_dbus_proxy_new_for_bus:
1997 * @bus_type: A #GBusType.
1998 * @flags: Flags used when constructing the proxy.
1999 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2000 * @name: A bus name (well-known or unique).
2001 * @object_path: An object path.
2002 * @interface_name: A D-Bus interface name.
2003 * @cancellable: A #GCancellable or %NULL.
2004 * @callback: Callback function to invoke when the proxy is ready.
2005 * @user_data: User data to pass to @callback.
2007 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2009 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2014 g_dbus_proxy_new_for_bus (GBusType bus_type,
2015 GDBusProxyFlags flags,
2016 GDBusInterfaceInfo *info,
2018 const gchar *object_path,
2019 const gchar *interface_name,
2020 GCancellable *cancellable,
2021 GAsyncReadyCallback callback,
2024 g_return_if_fail (g_dbus_is_name (name));
2025 g_return_if_fail (g_variant_is_object_path (object_path));
2026 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2028 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2034 "g-interface-info", info,
2036 "g-bus-type", bus_type,
2037 "g-object-path", object_path,
2038 "g-interface-name", interface_name,
2043 * g_dbus_proxy_new_for_bus_finish:
2044 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2045 * @error: Return location for error or %NULL.
2047 * Finishes creating a #GDBusProxy.
2049 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2054 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2057 return g_dbus_proxy_new_finish (res, error);
2061 * g_dbus_proxy_new_for_bus_sync:
2062 * @bus_type: A #GBusType.
2063 * @flags: Flags used when constructing the proxy.
2064 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2065 * that @proxy conforms to or %NULL.
2066 * @name: A bus name (well-known or unique).
2067 * @object_path: An object path.
2068 * @interface_name: A D-Bus interface name.
2069 * @cancellable: A #GCancellable or %NULL.
2070 * @error: Return location for error or %NULL.
2072 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2074 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2076 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2081 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2082 GDBusProxyFlags flags,
2083 GDBusInterfaceInfo *info,
2085 const gchar *object_path,
2086 const gchar *interface_name,
2087 GCancellable *cancellable,
2090 GInitable *initable;
2092 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2093 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2094 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2096 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2100 "g-interface-info", info,
2102 "g-bus-type", bus_type,
2103 "g-object-path", object_path,
2104 "g-interface-name", interface_name,
2106 if (initable != NULL)
2107 return G_DBUS_PROXY (initable);
2112 /* ---------------------------------------------------------------------------------------------------- */
2115 * g_dbus_proxy_get_connection:
2116 * @proxy: A #GDBusProxy.
2118 * Gets the connection @proxy is for.
2120 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2125 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2127 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2128 return proxy->priv->connection;
2132 * g_dbus_proxy_get_flags:
2133 * @proxy: A #GDBusProxy.
2135 * Gets the flags that @proxy was constructed with.
2137 * Returns: Flags from the #GDBusProxyFlags enumeration.
2142 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2144 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2145 return proxy->priv->flags;
2149 * g_dbus_proxy_get_name:
2150 * @proxy: A #GDBusProxy.
2152 * Gets the name that @proxy was constructed for.
2154 * Returns: A string owned by @proxy. Do not free.
2159 g_dbus_proxy_get_name (GDBusProxy *proxy)
2161 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2162 return proxy->priv->name;
2166 * g_dbus_proxy_get_name_owner:
2167 * @proxy: A #GDBusProxy.
2169 * The unique name that owns the name that @proxy is for or %NULL if
2170 * no-one currently owns that name. You may connect to the
2171 * #GObject::notify signal to track changes to the
2172 * #GDBusProxy:g-name-owner property.
2174 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2179 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2181 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2182 return g_strdup (proxy->priv->name_owner);
2186 * g_dbus_proxy_get_object_path:
2187 * @proxy: A #GDBusProxy.
2189 * Gets the object path @proxy is for.
2191 * Returns: A string owned by @proxy. Do not free.
2196 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2198 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2199 return proxy->priv->object_path;
2203 * g_dbus_proxy_get_interface_name:
2204 * @proxy: A #GDBusProxy.
2206 * Gets the D-Bus interface name @proxy is for.
2208 * Returns: A string owned by @proxy. Do not free.
2213 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2215 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2216 return proxy->priv->interface_name;
2220 * g_dbus_proxy_get_default_timeout:
2221 * @proxy: A #GDBusProxy.
2223 * Gets the timeout to use if -1 (specifying default timeout) is
2224 * passed as @timeout_msec in the g_dbus_proxy_call() and
2225 * g_dbus_proxy_call_sync() functions.
2227 * See the #GDBusProxy:g-default-timeout property for more details.
2229 * Returns: Timeout to use for @proxy.
2234 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2236 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2237 return proxy->priv->timeout_msec;
2241 * g_dbus_proxy_set_default_timeout:
2242 * @proxy: A #GDBusProxy.
2243 * @timeout_msec: Timeout in milliseconds.
2245 * Sets the timeout to use if -1 (specifying default timeout) is
2246 * passed as @timeout_msec in the g_dbus_proxy_call() and
2247 * g_dbus_proxy_call_sync() functions.
2249 * See the #GDBusProxy:g-default-timeout property for more details.
2254 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2257 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2258 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2260 /* TODO: locking? */
2261 if (proxy->priv->timeout_msec != timeout_msec)
2263 proxy->priv->timeout_msec = timeout_msec;
2264 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2269 * g_dbus_proxy_get_interface_info:
2270 * @proxy: A #GDBusProxy
2272 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2273 * interface that @proxy conforms to.
2275 * See the #GDBusProxy:g-interface-info property for more details.
2277 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2278 * object, it is owned by @proxy.
2282 GDBusInterfaceInfo *
2283 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2285 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2286 return proxy->priv->expected_interface;
2290 * g_dbus_proxy_set_interface_info:
2291 * @proxy: A #GDBusProxy
2292 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2294 * Ensure that interactions with @proxy conform to the given
2295 * interface. For example, when completing a method call, if the type
2296 * signature of the message isn't what's expected, the given #GError
2297 * is set. Signals that have a type signature mismatch are simply
2300 * See the #GDBusProxy:g-interface-info property for more details.
2305 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2306 GDBusInterfaceInfo *info)
2308 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2309 if (proxy->priv->expected_interface != NULL)
2311 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2312 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2314 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2315 if (proxy->priv->expected_interface != NULL)
2316 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2319 /* ---------------------------------------------------------------------------------------------------- */
2322 maybe_split_method_name (const gchar *method_name,
2323 gchar **out_interface_name,
2324 const gchar **out_method_name)
2329 g_assert (out_interface_name != NULL);
2330 g_assert (out_method_name != NULL);
2331 *out_interface_name = NULL;
2332 *out_method_name = NULL;
2334 if (strchr (method_name, '.') != NULL)
2339 p = g_strdup (method_name);
2340 last_dot = strrchr (p, '.');
2343 *out_interface_name = p;
2344 *out_method_name = last_dot + 1;
2356 GUnixFDList *fd_list;
2361 reply_data_free (ReplyData *data)
2363 g_variant_unref (data->value);
2365 if (data->fd_list != NULL)
2366 g_object_unref (data->fd_list);
2368 g_slice_free (ReplyData, data);
2372 reply_cb (GDBusConnection *connection,
2376 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2380 GUnixFDList *fd_list;
2385 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2390 value = g_dbus_connection_call_finish (connection,
2396 g_simple_async_result_take_error (simple, error);
2401 data = g_slice_new0 (ReplyData);
2402 data->value = value;
2404 data->fd_list = fd_list;
2406 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2409 /* no need to complete in idle since the method GDBusConnection already does */
2410 g_simple_async_result_complete (simple);
2411 g_object_unref (simple);
2414 static const GDBusMethodInfo *
2415 lookup_method_info_or_warn (GDBusProxy *proxy,
2416 const gchar *method_name)
2418 const GDBusMethodInfo *info;
2420 if (proxy->priv->expected_interface == NULL)
2423 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2426 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2427 method_name, proxy->priv->expected_interface->name);
2433 static const gchar *
2434 get_destination_for_call (GDBusProxy *proxy)
2440 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2441 * is never NULL and always the same as proxy->priv->name. We use this
2442 * knowledge to avoid checking if proxy->priv->name is a unique or
2445 ret = proxy->priv->name_owner;
2449 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2452 ret = proxy->priv->name;
2458 /* ---------------------------------------------------------------------------------------------------- */
2461 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2462 const gchar *method_name,
2463 GVariant *parameters,
2464 GDBusCallFlags flags,
2466 GUnixFDList *fd_list,
2467 GCancellable *cancellable,
2468 GAsyncReadyCallback callback,
2471 GSimpleAsyncResult *simple;
2473 gchar *split_interface_name;
2474 const gchar *split_method_name;
2475 const gchar *target_method_name;
2476 const gchar *target_interface_name;
2477 const gchar *destination;
2478 GVariantType *reply_type;
2480 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2481 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2482 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2483 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2485 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2487 g_return_if_fail (fd_list == NULL);
2491 split_interface_name = NULL;
2493 simple = g_simple_async_result_new (G_OBJECT (proxy),
2496 g_dbus_proxy_call_internal);
2498 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2499 target_method_name = was_split ? split_method_name : method_name;
2500 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2502 /* Warn if method is unexpected (cf. :g-interface-info) */
2505 const GDBusMethodInfo *expected_method_info;
2506 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2507 if (expected_method_info != NULL)
2508 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2512 if (proxy->priv->name != NULL)
2514 destination = get_destination_for_call (proxy);
2515 if (destination == NULL)
2517 g_simple_async_result_set_error (simple,
2520 _("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"));
2526 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2528 proxy->priv->object_path,
2529 target_interface_name,
2534 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2537 (GAsyncReadyCallback) reply_cb,
2540 g_dbus_connection_call (proxy->priv->connection,
2542 proxy->priv->object_path,
2543 target_interface_name,
2548 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2550 (GAsyncReadyCallback) reply_cb,
2555 if (reply_type != NULL)
2556 g_variant_type_free (reply_type);
2558 g_free (split_interface_name);
2562 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2563 GUnixFDList **out_fd_list,
2567 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2571 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2572 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2573 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2575 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2579 if (g_simple_async_result_propagate_error (simple, error))
2582 data = g_simple_async_result_get_op_res_gpointer (simple);
2583 value = g_variant_ref (data->value);
2585 if (out_fd_list != NULL)
2586 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2594 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2595 const gchar *method_name,
2596 GVariant *parameters,
2597 GDBusCallFlags flags,
2599 GUnixFDList *fd_list,
2600 GUnixFDList **out_fd_list,
2601 GCancellable *cancellable,
2606 gchar *split_interface_name;
2607 const gchar *split_method_name;
2608 const gchar *target_method_name;
2609 const gchar *target_interface_name;
2610 const gchar *destination;
2611 GVariantType *reply_type;
2613 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2614 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2615 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2616 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2618 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2620 g_return_val_if_fail (fd_list == NULL, NULL);
2622 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2626 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2627 target_method_name = was_split ? split_method_name : method_name;
2628 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2630 /* Warn if method is unexpected (cf. :g-interface-info) */
2633 const GDBusMethodInfo *expected_method_info;
2634 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2635 if (expected_method_info != NULL)
2636 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2640 if (proxy->priv->name != NULL)
2642 destination = get_destination_for_call (proxy);
2643 if (destination == NULL)
2645 g_set_error_literal (error,
2648 _("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"));
2655 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2657 proxy->priv->object_path,
2658 target_interface_name,
2663 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2669 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2671 proxy->priv->object_path,
2672 target_interface_name,
2677 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2683 if (reply_type != NULL)
2684 g_variant_type_free (reply_type);
2686 g_free (split_interface_name);
2691 /* ---------------------------------------------------------------------------------------------------- */
2694 * g_dbus_proxy_call:
2695 * @proxy: A #GDBusProxy.
2696 * @method_name: Name of method to invoke.
2697 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2698 * @flags: Flags from the #GDBusCallFlags enumeration.
2699 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2700 * "infinite") or -1 to use the proxy default timeout.
2701 * @cancellable: A #GCancellable or %NULL.
2702 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2703 * care about the result of the method invocation.
2704 * @user_data: The data to pass to @callback.
2706 * Asynchronously invokes the @method_name method on @proxy.
2708 * If @method_name contains any dots, then @name is split into interface and
2709 * method name parts. This allows using @proxy for invoking methods on
2712 * If the #GDBusConnection associated with @proxy is closed then
2713 * the operation will fail with %G_IO_ERROR_CLOSED. If
2714 * @cancellable is canceled, the operation will fail with
2715 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2716 * compatible with the D-Bus protocol, the operation fails with
2717 * %G_IO_ERROR_INVALID_ARGUMENT.
2719 * If the @parameters #GVariant is floating, it is consumed. This allows
2720 * convenient 'inline' use of g_variant_new(), e.g.:
2722 * g_dbus_proxy_call (proxy,
2724 * g_variant_new ("(ss)",
2727 * G_DBUS_CALL_FLAGS_NONE,
2730 * (GAsyncReadyCallback) two_strings_done,
2734 * This is an asynchronous method. When the operation is finished,
2735 * @callback will be invoked in the
2736 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2737 * of the thread you are calling this method from.
2738 * You can then call g_dbus_proxy_call_finish() to get the result of
2739 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2740 * version of this method.
2745 g_dbus_proxy_call (GDBusProxy *proxy,
2746 const gchar *method_name,
2747 GVariant *parameters,
2748 GDBusCallFlags flags,
2750 GCancellable *cancellable,
2751 GAsyncReadyCallback callback,
2754 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2758 * g_dbus_proxy_call_finish:
2759 * @proxy: A #GDBusProxy.
2760 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2761 * @error: Return location for error or %NULL.
2763 * Finishes an operation started with g_dbus_proxy_call().
2765 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2766 * return values. Free with g_variant_unref().
2771 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2775 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2779 * g_dbus_proxy_call_sync:
2780 * @proxy: A #GDBusProxy.
2781 * @method_name: Name of method to invoke.
2782 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2783 * or %NULL if not passing parameters.
2784 * @flags: Flags from the #GDBusCallFlags enumeration.
2785 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2786 * "infinite") or -1 to use the proxy default timeout.
2787 * @cancellable: A #GCancellable or %NULL.
2788 * @error: Return location for error or %NULL.
2790 * Synchronously invokes the @method_name method on @proxy.
2792 * If @method_name contains any dots, then @name is split into interface and
2793 * method name parts. This allows using @proxy for invoking methods on
2796 * If the #GDBusConnection associated with @proxy is disconnected then
2797 * the operation will fail with %G_IO_ERROR_CLOSED. If
2798 * @cancellable is canceled, the operation will fail with
2799 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2800 * compatible with the D-Bus protocol, the operation fails with
2801 * %G_IO_ERROR_INVALID_ARGUMENT.
2803 * If the @parameters #GVariant is floating, it is consumed. This allows
2804 * convenient 'inline' use of g_variant_new(), e.g.:
2806 * g_dbus_proxy_call_sync (proxy,
2808 * g_variant_new ("(ss)",
2811 * G_DBUS_CALL_FLAGS_NONE,
2817 * The calling thread is blocked until a reply is received. See
2818 * g_dbus_proxy_call() for the asynchronous version of this
2821 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2822 * return values. Free with g_variant_unref().
2827 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2828 const gchar *method_name,
2829 GVariant *parameters,
2830 GDBusCallFlags flags,
2832 GCancellable *cancellable,
2835 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2838 /* ---------------------------------------------------------------------------------------------------- */
2843 * g_dbus_proxy_call_with_unix_fd_list:
2844 * @proxy: A #GDBusProxy.
2845 * @method_name: Name of method to invoke.
2846 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2847 * @flags: Flags from the #GDBusCallFlags enumeration.
2848 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2849 * "infinite") or -1 to use the proxy default timeout.
2850 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2851 * @cancellable: A #GCancellable or %NULL.
2852 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2853 * care about the result of the method invocation.
2854 * @user_data: The data to pass to @callback.
2856 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2858 * This method is only available on UNIX.
2863 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
2864 const gchar *method_name,
2865 GVariant *parameters,
2866 GDBusCallFlags flags,
2868 GUnixFDList *fd_list,
2869 GCancellable *cancellable,
2870 GAsyncReadyCallback callback,
2873 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2877 * g_dbus_proxy_call_with_unix_fd_list_finish:
2878 * @proxy: A #GDBusProxy.
2879 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2880 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2881 * @error: Return location for error or %NULL.
2883 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2885 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2886 * return values. Free with g_variant_unref().
2891 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
2892 GUnixFDList **out_fd_list,
2896 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2900 * g_dbus_proxy_call_with_unix_fd_list_sync:
2901 * @proxy: A #GDBusProxy.
2902 * @method_name: Name of method to invoke.
2903 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2904 * or %NULL if not passing parameters.
2905 * @flags: Flags from the #GDBusCallFlags enumeration.
2906 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2907 * "infinite") or -1 to use the proxy default timeout.
2908 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2909 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2910 * @cancellable: A #GCancellable or %NULL.
2911 * @error: Return location for error or %NULL.
2913 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2915 * This method is only available on UNIX.
2917 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2918 * return values. Free with g_variant_unref().
2923 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
2924 const gchar *method_name,
2925 GVariant *parameters,
2926 GDBusCallFlags flags,
2928 GUnixFDList *fd_list,
2929 GUnixFDList **out_fd_list,
2930 GCancellable *cancellable,
2933 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2936 #endif /* G_OS_UNIX */
2938 /* ---------------------------------------------------------------------------------------------------- */
2940 static GDBusInterfaceInfo *
2941 _g_dbus_proxy_get_info (GDBusInterface *interface)
2943 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2944 return g_dbus_proxy_get_interface_info (proxy);
2947 static GDBusObject *
2948 _g_dbus_proxy_get_object (GDBusInterface *interface)
2950 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2951 return proxy->priv->object;
2955 _g_dbus_proxy_set_object (GDBusInterface *interface,
2956 GDBusObject *object)
2958 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2959 if (proxy->priv->object != NULL)
2960 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2961 proxy->priv->object = object;
2962 if (proxy->priv->object != NULL)
2963 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2967 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2969 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
2970 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2971 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2974 /* ---------------------------------------------------------------------------------------------------- */