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: A %NULL-terminated array of strings or %NULL if @proxy has
636 * no cached properties. Free the returned array with g_strfreev().
641 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
648 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
650 G_LOCK (properties_lock);
653 if (g_hash_table_size (proxy->priv->properties) == 0)
656 p = g_ptr_array_new ();
658 g_hash_table_iter_init (&iter, proxy->priv->properties);
659 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
660 g_ptr_array_add (p, g_strdup (key));
661 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
662 g_ptr_array_add (p, NULL);
664 names = (gchar **) g_ptr_array_free (p, FALSE);
667 G_UNLOCK (properties_lock);
671 static const GDBusPropertyInfo *
672 lookup_property_info_or_warn (GDBusProxy *proxy,
673 const gchar *property_name)
675 const GDBusPropertyInfo *info;
677 if (proxy->priv->expected_interface == NULL)
680 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
683 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
685 proxy->priv->expected_interface->name);
692 * g_dbus_proxy_get_cached_property:
693 * @proxy: A #GDBusProxy.
694 * @property_name: Property name.
696 * Looks up the value for a property from the cache. This call does no
699 * If @proxy has an expected interface (see
700 * #GDBusProxy:g-interface-info), then @property_name (for existence)
701 * is checked against it.
703 * Returns: A reference to the #GVariant instance that holds the value
704 * for @property_name or %NULL if the value is not in the cache. The
705 * returned reference must be freed with g_variant_unref().
710 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
711 const gchar *property_name)
715 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
716 g_return_val_if_fail (property_name != NULL, NULL);
718 G_LOCK (properties_lock);
720 value = g_hash_table_lookup (proxy->priv->properties, property_name);
723 lookup_property_info_or_warn (proxy, property_name);
728 g_variant_ref (value);
731 G_UNLOCK (properties_lock);
736 * g_dbus_proxy_set_cached_property:
737 * @proxy: A #GDBusProxy
738 * @property_name: Property name.
739 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
741 * If @value is not %NULL, sets the cached value for the property with
742 * name @property_name to the value in @value.
744 * If @value is %NULL, then the cached value is removed from the
747 * If @proxy has an expected interface (see
748 * #GDBusProxy:g-interface-info), then @property_name (for existence)
749 * and @value (for the type) is checked against it.
751 * If the @value #GVariant is floating, it is consumed. This allows
752 * convenient 'inline' use of g_variant_new(), e.g.
754 * g_dbus_proxy_set_cached_property (proxy,
756 * g_variant_new ("(si)",
761 * Normally you will not need to use this method since @proxy is
762 * tracking changes using the
763 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
764 * D-Bus signal. However, for performance reasons an object may decide
765 * to not use this signal for some properties and instead use a
766 * proprietary out-of-band mechanism to transmit changes.
768 * As a concrete example, consider an object with a property
769 * <literal>ChatroomParticipants</literal> which is an array of
770 * strings. Instead of transmitting the same (long) array every time
771 * the property changes, it is more efficient to only transmit the
772 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
773 * name)</literal> and <literal>ChatroomParticipantParted(String
779 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
780 const gchar *property_name,
783 const GDBusPropertyInfo *info;
785 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
786 g_return_if_fail (property_name != NULL);
788 G_LOCK (properties_lock);
792 info = lookup_property_info_or_warn (proxy, property_name);
795 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
797 g_warning ("Trying to set property %s of type %s but according to the expected "
798 "interface the type is %s",
800 g_variant_get_type_string (value),
805 g_hash_table_insert (proxy->priv->properties,
806 g_strdup (property_name),
807 g_variant_ref_sink (value));
811 g_hash_table_remove (proxy->priv->properties, property_name);
815 G_UNLOCK (properties_lock);
818 /* ---------------------------------------------------------------------------------------------------- */
821 on_signal_received (GDBusConnection *connection,
822 const gchar *sender_name,
823 const gchar *object_path,
824 const gchar *interface_name,
825 const gchar *signal_name,
826 GVariant *parameters,
829 SignalSubscriptionData *data = user_data;
832 G_LOCK (signal_subscription_lock);
836 G_UNLOCK (signal_subscription_lock);
841 g_object_ref (proxy);
842 G_UNLOCK (signal_subscription_lock);
845 if (!proxy->priv->initialized)
848 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
851 if (proxy->priv->expected_interface != NULL)
853 const GDBusSignalInfo *info;
854 GVariantType *expected_type;
855 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
858 expected_type = _g_dbus_compute_complete_signature (info->args);
859 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
861 g_variant_type_free (expected_type);
864 g_variant_type_free (expected_type);
867 g_signal_emit (proxy,
868 signals[SIGNAL_SIGNAL],
875 g_object_unref (proxy);
878 /* ---------------------------------------------------------------------------------------------------- */
880 /* must hold properties_lock */
882 insert_property_checked (GDBusProxy *proxy,
883 gchar *property_name,
886 if (proxy->priv->expected_interface != NULL)
888 const GDBusPropertyInfo *info;
890 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
891 /* Ignore unknown properties */
895 /* Ignore properties with the wrong type */
896 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
900 g_hash_table_insert (proxy->priv->properties,
901 property_name, /* adopts string */
902 value); /* adopts value */
907 g_variant_unref (value);
908 g_free (property_name);
912 on_properties_changed (GDBusConnection *connection,
913 const gchar *sender_name,
914 const gchar *object_path,
915 const gchar *interface_name,
916 const gchar *signal_name,
917 GVariant *parameters,
920 SignalSubscriptionData *data = user_data;
922 const gchar *interface_name_for_signal;
923 GVariant *changed_properties;
924 gchar **invalidated_properties;
930 G_LOCK (signal_subscription_lock);
934 G_UNLOCK (signal_subscription_lock);
939 g_object_ref (proxy);
940 G_UNLOCK (signal_subscription_lock);
943 changed_properties = NULL;
944 invalidated_properties = NULL;
946 if (!proxy->priv->initialized)
949 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
952 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
954 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
955 g_variant_get_type_string (parameters));
959 g_variant_get (parameters,
961 &interface_name_for_signal,
963 &invalidated_properties);
965 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
968 G_LOCK (properties_lock);
970 g_variant_iter_init (&iter, changed_properties);
971 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
973 insert_property_checked (proxy,
974 key, /* adopts string */
975 value); /* adopts value */
978 for (n = 0; invalidated_properties[n] != NULL; n++)
980 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
983 G_UNLOCK (properties_lock);
986 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
989 invalidated_properties);
992 if (changed_properties != NULL)
993 g_variant_unref (changed_properties);
994 g_free (invalidated_properties);
996 g_object_unref (proxy);
999 /* ---------------------------------------------------------------------------------------------------- */
1002 process_get_all_reply (GDBusProxy *proxy,
1008 guint num_properties;
1010 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1012 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1013 g_variant_get_type_string (result));
1017 G_LOCK (properties_lock);
1019 g_variant_get (result, "(a{sv})", &iter);
1020 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1022 insert_property_checked (proxy,
1023 key, /* adopts string */
1024 value); /* adopts value */
1026 g_variant_iter_free (iter);
1028 num_properties = g_hash_table_size (proxy->priv->properties);
1029 G_UNLOCK (properties_lock);
1031 /* Synthesize ::g-properties-changed changed */
1032 if (num_properties > 0)
1034 GVariant *changed_properties;
1035 const gchar *invalidated_properties[1] = {NULL};
1037 g_variant_get (result,
1039 &changed_properties);
1040 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1043 invalidated_properties);
1044 g_variant_unref (changed_properties);
1054 GCancellable *cancellable;
1056 } LoadPropertiesOnNameOwnerChangedData;
1059 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1063 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1071 result = g_dbus_connection_call_finish (connection,
1076 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1078 /* We just ignore if GetAll() is failing. Because this might happen
1079 * if the object has no properties at all. Or if the caller is
1080 * not authorized to see the properties.
1082 * Either way, apps can know about this by using
1083 * get_cached_property_names() or get_cached_property().
1085 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1086 * fact that GetAll() failed
1088 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1089 g_error_free (error);
1092 /* and finally we can notify */
1095 g_free (data->proxy->priv->name_owner);
1096 data->proxy->priv->name_owner = data->name_owner;
1097 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1099 G_LOCK (properties_lock);
1100 g_hash_table_remove_all (data->proxy->priv->properties);
1101 G_UNLOCK (properties_lock);
1104 process_get_all_reply (data->proxy, result);
1105 g_variant_unref (result);
1108 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1111 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1112 data->proxy->priv->get_all_cancellable = NULL;
1114 g_object_unref (data->proxy);
1115 g_object_unref (data->cancellable);
1116 g_free (data->name_owner);
1121 on_name_owner_changed (GDBusConnection *connection,
1122 const gchar *sender_name,
1123 const gchar *object_path,
1124 const gchar *interface_name,
1125 const gchar *signal_name,
1126 GVariant *parameters,
1129 SignalSubscriptionData *data = user_data;
1131 const gchar *old_owner;
1132 const gchar *new_owner;
1134 G_LOCK (signal_subscription_lock);
1135 proxy = data->proxy;
1138 G_UNLOCK (signal_subscription_lock);
1143 g_object_ref (proxy);
1144 G_UNLOCK (signal_subscription_lock);
1147 /* if we are already trying to load properties, cancel that */
1148 if (proxy->priv->get_all_cancellable != NULL)
1150 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1151 proxy->priv->get_all_cancellable = NULL;
1154 g_variant_get (parameters,
1160 if (strlen (new_owner) == 0)
1162 g_free (proxy->priv->name_owner);
1163 proxy->priv->name_owner = NULL;
1165 G_LOCK (properties_lock);
1167 /* Synthesize ::g-properties-changed changed */
1168 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1169 g_hash_table_size (proxy->priv->properties) > 0)
1171 GVariantBuilder builder;
1172 GPtrArray *invalidated_properties;
1173 GHashTableIter iter;
1176 /* Build changed_properties (always empty) and invalidated_properties ... */
1177 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1179 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1180 g_hash_table_iter_init (&iter, proxy->priv->properties);
1181 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1182 g_ptr_array_add (invalidated_properties, g_strdup (key));
1183 g_ptr_array_add (invalidated_properties, NULL);
1185 /* ... throw out the properties ... */
1186 g_hash_table_remove_all (proxy->priv->properties);
1188 G_UNLOCK (properties_lock);
1190 /* ... and finally emit the ::g-properties-changed signal */
1191 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1193 g_variant_builder_end (&builder) /* consumed */,
1194 (const gchar* const *) invalidated_properties->pdata);
1195 g_ptr_array_unref (invalidated_properties);
1199 G_UNLOCK (properties_lock);
1201 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1205 /* ignore duplicates - this can happen when activating the service */
1206 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1209 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1211 g_free (proxy->priv->name_owner);
1212 proxy->priv->name_owner = g_strdup (new_owner);
1213 G_LOCK (properties_lock);
1214 g_hash_table_remove_all (proxy->priv->properties);
1215 G_UNLOCK (properties_lock);
1216 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1220 LoadPropertiesOnNameOwnerChangedData *data;
1222 /* start loading properties.. only then emit notify::g-name-owner .. we
1223 * need to be able to cancel this in the event another NameOwnerChanged
1224 * signal suddenly happens
1227 g_assert (proxy->priv->get_all_cancellable == NULL);
1228 proxy->priv->get_all_cancellable = g_cancellable_new ();
1229 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1230 data->proxy = g_object_ref (proxy);
1231 data->cancellable = proxy->priv->get_all_cancellable;
1232 data->name_owner = g_strdup (new_owner);
1233 g_dbus_connection_call (proxy->priv->connection,
1235 proxy->priv->object_path,
1236 "org.freedesktop.DBus.Properties",
1238 g_variant_new ("(s)", proxy->priv->interface_name),
1239 G_VARIANT_TYPE ("(a{sv})"),
1240 G_DBUS_CALL_FLAGS_NONE,
1242 proxy->priv->get_all_cancellable,
1243 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1250 g_object_unref (proxy);
1253 /* ---------------------------------------------------------------------------------------------------- */
1258 GCancellable *cancellable;
1259 GSimpleAsyncResult *simple;
1263 async_init_data_free (AsyncInitData *data)
1265 g_object_unref (data->proxy);
1266 if (data->cancellable != NULL)
1267 g_object_unref (data->cancellable);
1268 g_object_unref (data->simple);
1273 async_init_get_all_cb (GDBusConnection *connection,
1277 AsyncInitData *data = user_data;
1282 result = g_dbus_connection_call_finish (connection,
1287 /* We just ignore if GetAll() is failing. Because this might happen
1288 * if the object has no properties at all. Or if the caller is
1289 * not authorized to see the properties.
1291 * Either way, apps can know about this by using
1292 * get_cached_property_names() or get_cached_property().
1294 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1295 * fact that GetAll() failed
1297 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1298 g_error_free (error);
1302 g_simple_async_result_set_op_res_gpointer (data->simple,
1304 (GDestroyNotify) g_variant_unref);
1307 g_simple_async_result_complete_in_idle (data->simple);
1308 async_init_data_free (data);
1313 async_init_get_name_owner_cb (GDBusConnection *connection,
1317 AsyncInitData *data = user_data;
1325 result = g_dbus_connection_call_finish (connection,
1330 if (error->domain == G_DBUS_ERROR &&
1331 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1333 g_error_free (error);
1337 g_simple_async_result_take_error (data->simple, error);
1338 g_simple_async_result_complete_in_idle (data->simple);
1339 async_init_data_free (data);
1345 g_variant_get (result,
1347 &data->proxy->priv->name_owner);
1348 g_variant_unref (result);
1352 if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1354 /* load all properties asynchronously */
1355 g_dbus_connection_call (data->proxy->priv->connection,
1356 data->proxy->priv->name_owner,
1357 data->proxy->priv->object_path,
1358 "org.freedesktop.DBus.Properties",
1360 g_variant_new ("(s)", data->proxy->priv->interface_name),
1361 G_VARIANT_TYPE ("(a{sv})"),
1362 G_DBUS_CALL_FLAGS_NONE,
1365 (GAsyncReadyCallback) async_init_get_all_cb,
1370 g_simple_async_result_complete_in_idle (data->simple);
1371 async_init_data_free (data);
1379 async_init_call_get_name_owner (AsyncInitData *data)
1381 g_dbus_connection_call (data->proxy->priv->connection,
1382 "org.freedesktop.DBus", /* name */
1383 "/org/freedesktop/DBus", /* object path */
1384 "org.freedesktop.DBus", /* interface */
1386 g_variant_new ("(s)",
1387 data->proxy->priv->name),
1388 G_VARIANT_TYPE ("(s)"),
1389 G_DBUS_CALL_FLAGS_NONE,
1392 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1397 async_init_start_service_by_name_cb (GDBusConnection *connection,
1401 AsyncInitData *data = user_data;
1406 result = g_dbus_connection_call_finish (connection,
1411 /* Errors are not unexpected; the bus will reply e.g.
1413 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1414 * was not provided by any .service files
1416 * This doesn't mean that the name doesn't have an owner, just
1417 * that it's not provided by a .service file. So just proceed to
1418 * invoke GetNameOwner() if dealing with that error.
1420 if (error->domain == G_DBUS_ERROR &&
1421 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1423 g_error_free (error);
1427 g_prefix_error (&error,
1428 _("Error calling StartServiceByName for %s: "),
1429 data->proxy->priv->name);
1435 guint32 start_service_result;
1436 g_variant_get (result,
1438 &start_service_result);
1439 g_variant_unref (result);
1440 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1441 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1443 /* continue to invoke GetNameOwner() */
1447 error = g_error_new (G_IO_ERROR,
1449 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1450 start_service_result,
1451 data->proxy->priv->name);
1456 async_init_call_get_name_owner (data);
1460 g_warn_if_fail (error != NULL);
1461 g_simple_async_result_take_error (data->simple, error);
1462 g_simple_async_result_complete_in_idle (data->simple);
1463 async_init_data_free (data);
1467 async_init_call_start_service_by_name (AsyncInitData *data)
1469 g_dbus_connection_call (data->proxy->priv->connection,
1470 "org.freedesktop.DBus", /* name */
1471 "/org/freedesktop/DBus", /* object path */
1472 "org.freedesktop.DBus", /* interface */
1473 "StartServiceByName",
1474 g_variant_new ("(su)",
1475 data->proxy->priv->name,
1477 G_VARIANT_TYPE ("(u)"),
1478 G_DBUS_CALL_FLAGS_NONE,
1481 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1486 async_initable_init_second_async (GAsyncInitable *initable,
1488 GCancellable *cancellable,
1489 GAsyncReadyCallback callback,
1492 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1493 AsyncInitData *data;
1495 data = g_new0 (AsyncInitData, 1);
1496 data->proxy = g_object_ref (proxy);
1497 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1498 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1503 /* Check name ownership asynchronously - possibly also start the service */
1504 if (proxy->priv->name == NULL)
1507 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1509 else if (g_dbus_is_unique_name (proxy->priv->name))
1511 proxy->priv->name_owner = g_strdup (proxy->priv->name);
1512 async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1516 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1518 async_init_call_get_name_owner (data);
1522 async_init_call_start_service_by_name (data);
1528 async_initable_init_second_finish (GAsyncInitable *initable,
1532 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1533 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1539 if (g_simple_async_result_propagate_error (simple, error))
1542 result = g_simple_async_result_get_op_res_gpointer (simple);
1545 process_get_all_reply (proxy, result);
1551 proxy->priv->initialized = TRUE;
1555 /* ---------------------------------------------------------------------------------------------------- */
1558 async_initable_init_first (GAsyncInitable *initable)
1560 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1562 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1564 /* subscribe to PropertiesChanged() */
1565 proxy->priv->properties_changed_subscription_id =
1566 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1568 "org.freedesktop.DBus.Properties",
1569 "PropertiesChanged",
1570 proxy->priv->object_path,
1571 proxy->priv->interface_name,
1572 G_DBUS_SIGNAL_FLAGS_NONE,
1573 on_properties_changed,
1574 signal_subscription_ref (proxy->priv->signal_subscription_data),
1575 (GDestroyNotify) signal_subscription_unref);
1578 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1580 /* subscribe to all signals for the object */
1581 proxy->priv->signals_subscription_id =
1582 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1584 proxy->priv->interface_name,
1586 proxy->priv->object_path,
1588 G_DBUS_SIGNAL_FLAGS_NONE,
1590 signal_subscription_ref (proxy->priv->signal_subscription_data),
1591 (GDestroyNotify) signal_subscription_unref);
1594 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1596 proxy->priv->name_owner_changed_subscription_id =
1597 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1598 "org.freedesktop.DBus", /* name */
1599 "org.freedesktop.DBus", /* interface */
1600 "NameOwnerChanged", /* signal name */
1601 "/org/freedesktop/DBus", /* path */
1602 proxy->priv->name, /* arg0 */
1603 G_DBUS_SIGNAL_FLAGS_NONE,
1604 on_name_owner_changed,
1605 signal_subscription_ref (proxy->priv->signal_subscription_data),
1606 (GDestroyNotify) signal_subscription_unref);
1610 /* ---------------------------------------------------------------------------------------------------- */
1612 /* initialization is split into two parts - the first is the
1613 * non-blocing part that requires the callers GMainContext - the
1614 * second is a blocking part async part that doesn't require the
1615 * callers GMainContext.. we do this split so the code can be reused
1616 * in the GInitable implementation below.
1618 * Note that obtaining a GDBusConnection is not shared between the two
1626 GCancellable *cancellable;
1627 GAsyncReadyCallback callback;
1629 } GetConnectionData;
1632 get_connection_cb (GObject *source_object,
1636 GetConnectionData *data = user_data;
1640 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1641 if (data->proxy->priv->connection == NULL)
1643 GSimpleAsyncResult *simple;
1644 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1648 g_simple_async_result_take_error (simple, error);
1649 g_simple_async_result_complete_in_idle (simple);
1650 g_object_unref (simple);
1654 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1655 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1662 if (data->cancellable != NULL)
1663 g_object_unref (data->cancellable);
1665 g_object_unref (data->proxy);
1670 async_initable_init_async (GAsyncInitable *initable,
1672 GCancellable *cancellable,
1673 GAsyncReadyCallback callback,
1676 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1678 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1680 GetConnectionData *data;
1682 g_assert (proxy->priv->connection == NULL);
1684 data = g_new0 (GetConnectionData, 1);
1685 data->proxy = g_object_ref (proxy);
1686 data->io_priority = io_priority;
1687 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1688 data->callback = callback;
1689 data->user_data = user_data;
1690 g_bus_get (proxy->priv->bus_type,
1697 async_initable_init_first (initable);
1698 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1703 async_initable_init_finish (GAsyncInitable *initable,
1707 return async_initable_init_second_finish (initable, res, error);
1711 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1713 async_initable_iface->init_async = async_initable_init_async;
1714 async_initable_iface->init_finish = async_initable_init_finish;
1717 /* ---------------------------------------------------------------------------------------------------- */
1721 GMainContext *context;
1724 } InitableAsyncInitableData;
1727 async_initable_init_async_cb (GObject *source_object,
1731 InitableAsyncInitableData *data = user_data;
1732 data->res = g_object_ref (res);
1733 g_main_loop_quit (data->loop);
1736 /* Simply reuse the GAsyncInitable implementation but run the first
1737 * part (that is non-blocking and requires the callers GMainContext)
1738 * with the callers GMainContext.. and the second with a private
1739 * GMainContext (bug 621310 is slightly related).
1741 * Note that obtaining a GDBusConnection is not shared between the two
1745 initable_init (GInitable *initable,
1746 GCancellable *cancellable,
1749 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1750 InitableAsyncInitableData *data;
1755 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1757 g_assert (proxy->priv->connection == NULL);
1758 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1761 if (proxy->priv->connection == NULL)
1765 async_initable_init_first (G_ASYNC_INITABLE (initable));
1767 data = g_new0 (InitableAsyncInitableData, 1);
1768 data->context = g_main_context_new ();
1769 data->loop = g_main_loop_new (data->context, FALSE);
1771 g_main_context_push_thread_default (data->context);
1773 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1776 async_initable_init_async_cb,
1779 g_main_loop_run (data->loop);
1781 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1785 g_main_context_pop_thread_default (data->context);
1787 g_main_context_unref (data->context);
1788 g_main_loop_unref (data->loop);
1789 g_object_unref (data->res);
1798 initable_iface_init (GInitableIface *initable_iface)
1800 initable_iface->init = initable_init;
1803 /* ---------------------------------------------------------------------------------------------------- */
1807 * @connection: A #GDBusConnection.
1808 * @flags: Flags used when constructing the proxy.
1809 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1810 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1811 * @object_path: An object path.
1812 * @interface_name: A D-Bus interface name.
1813 * @cancellable: A #GCancellable or %NULL.
1814 * @callback: Callback function to invoke when the proxy is ready.
1815 * @user_data: User data to pass to @callback.
1817 * Creates a proxy for accessing @interface_name on the remote object
1818 * at @object_path owned by @name at @connection and asynchronously
1819 * loads D-Bus properties unless the
1820 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1821 * the #GDBusProxy::g-properties-changed signal to get notified about
1824 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1825 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1826 * to handle signals from the remote object.
1828 * If @name is a well-known name and the
1829 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1830 * owner currently exists, the message bus will be requested to launch
1831 * a name owner for the name.
1833 * This is a failable asynchronous constructor - when the proxy is
1834 * ready, @callback will be invoked and you can use
1835 * g_dbus_proxy_new_finish() to get the result.
1837 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1839 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1844 g_dbus_proxy_new (GDBusConnection *connection,
1845 GDBusProxyFlags flags,
1846 GDBusInterfaceInfo *info,
1848 const gchar *object_path,
1849 const gchar *interface_name,
1850 GCancellable *cancellable,
1851 GAsyncReadyCallback callback,
1854 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1855 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1856 g_return_if_fail (g_variant_is_object_path (object_path));
1857 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1859 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1865 "g-interface-info", info,
1867 "g-connection", connection,
1868 "g-object-path", object_path,
1869 "g-interface-name", interface_name,
1874 * g_dbus_proxy_new_finish:
1875 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1876 * @error: Return location for error or %NULL.
1878 * Finishes creating a #GDBusProxy.
1880 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1885 g_dbus_proxy_new_finish (GAsyncResult *res,
1889 GObject *source_object;
1891 source_object = g_async_result_get_source_object (res);
1892 g_assert (source_object != NULL);
1894 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1897 g_object_unref (source_object);
1900 return G_DBUS_PROXY (object);
1906 * g_dbus_proxy_new_sync:
1907 * @connection: A #GDBusConnection.
1908 * @flags: Flags used when constructing the proxy.
1909 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1910 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1911 * @object_path: An object path.
1912 * @interface_name: A D-Bus interface name.
1913 * @cancellable: (allow-none): A #GCancellable or %NULL.
1914 * @error: (allow-none): Return location for error or %NULL.
1916 * Creates a proxy for accessing @interface_name on the remote object
1917 * at @object_path owned by @name at @connection and synchronously
1918 * loads D-Bus properties unless the
1919 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1921 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1922 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1923 * to handle signals from the remote object.
1925 * If @name is a well-known name and the
1926 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1927 * owner currently exists, the message bus will be requested to launch
1928 * a name owner for the name.
1930 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1931 * and g_dbus_proxy_new_finish() for the asynchronous version.
1933 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1935 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1940 g_dbus_proxy_new_sync (GDBusConnection *connection,
1941 GDBusProxyFlags flags,
1942 GDBusInterfaceInfo *info,
1944 const gchar *object_path,
1945 const gchar *interface_name,
1946 GCancellable *cancellable,
1949 GInitable *initable;
1951 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1952 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1953 g_dbus_is_name (name), NULL);
1954 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1955 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1957 initable = g_initable_new (G_TYPE_DBUS_PROXY,
1961 "g-interface-info", info,
1963 "g-connection", connection,
1964 "g-object-path", object_path,
1965 "g-interface-name", interface_name,
1967 if (initable != NULL)
1968 return G_DBUS_PROXY (initable);
1973 /* ---------------------------------------------------------------------------------------------------- */
1976 * g_dbus_proxy_new_for_bus:
1977 * @bus_type: A #GBusType.
1978 * @flags: Flags used when constructing the proxy.
1979 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1980 * @name: A bus name (well-known or unique).
1981 * @object_path: An object path.
1982 * @interface_name: A D-Bus interface name.
1983 * @cancellable: A #GCancellable or %NULL.
1984 * @callback: Callback function to invoke when the proxy is ready.
1985 * @user_data: User data to pass to @callback.
1987 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1989 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1994 g_dbus_proxy_new_for_bus (GBusType bus_type,
1995 GDBusProxyFlags flags,
1996 GDBusInterfaceInfo *info,
1998 const gchar *object_path,
1999 const gchar *interface_name,
2000 GCancellable *cancellable,
2001 GAsyncReadyCallback callback,
2004 g_return_if_fail (g_dbus_is_name (name));
2005 g_return_if_fail (g_variant_is_object_path (object_path));
2006 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2008 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2014 "g-interface-info", info,
2016 "g-bus-type", bus_type,
2017 "g-object-path", object_path,
2018 "g-interface-name", interface_name,
2023 * g_dbus_proxy_new_for_bus_finish:
2024 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2025 * @error: Return location for error or %NULL.
2027 * Finishes creating a #GDBusProxy.
2029 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2034 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2037 return g_dbus_proxy_new_finish (res, error);
2041 * g_dbus_proxy_new_for_bus_sync:
2042 * @bus_type: A #GBusType.
2043 * @flags: Flags used when constructing the proxy.
2044 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2045 * that @proxy conforms to or %NULL.
2046 * @name: A bus name (well-known or unique).
2047 * @object_path: An object path.
2048 * @interface_name: A D-Bus interface name.
2049 * @cancellable: A #GCancellable or %NULL.
2050 * @error: Return location for error or %NULL.
2052 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2054 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2056 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2061 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2062 GDBusProxyFlags flags,
2063 GDBusInterfaceInfo *info,
2065 const gchar *object_path,
2066 const gchar *interface_name,
2067 GCancellable *cancellable,
2070 GInitable *initable;
2072 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2073 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2074 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2076 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2080 "g-interface-info", info,
2082 "g-bus-type", bus_type,
2083 "g-object-path", object_path,
2084 "g-interface-name", interface_name,
2086 if (initable != NULL)
2087 return G_DBUS_PROXY (initable);
2092 /* ---------------------------------------------------------------------------------------------------- */
2095 * g_dbus_proxy_get_connection:
2096 * @proxy: A #GDBusProxy.
2098 * Gets the connection @proxy is for.
2100 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2105 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2107 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2108 return proxy->priv->connection;
2112 * g_dbus_proxy_get_flags:
2113 * @proxy: A #GDBusProxy.
2115 * Gets the flags that @proxy was constructed with.
2117 * Returns: Flags from the #GDBusProxyFlags enumeration.
2122 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2124 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2125 return proxy->priv->flags;
2129 * g_dbus_proxy_get_name:
2130 * @proxy: A #GDBusProxy.
2132 * Gets the name that @proxy was constructed for.
2134 * Returns: A string owned by @proxy. Do not free.
2139 g_dbus_proxy_get_name (GDBusProxy *proxy)
2141 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2142 return proxy->priv->name;
2146 * g_dbus_proxy_get_name_owner:
2147 * @proxy: A #GDBusProxy.
2149 * The unique name that owns the name that @proxy is for or %NULL if
2150 * no-one currently owns that name. You may connect to the
2151 * #GObject::notify signal to track changes to the
2152 * #GDBusProxy:g-name-owner property.
2154 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2159 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2161 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2162 return g_strdup (proxy->priv->name_owner);
2166 * g_dbus_proxy_get_object_path:
2167 * @proxy: A #GDBusProxy.
2169 * Gets the object path @proxy is for.
2171 * Returns: A string owned by @proxy. Do not free.
2176 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2178 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2179 return proxy->priv->object_path;
2183 * g_dbus_proxy_get_interface_name:
2184 * @proxy: A #GDBusProxy.
2186 * Gets the D-Bus interface name @proxy is for.
2188 * Returns: A string owned by @proxy. Do not free.
2193 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2195 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2196 return proxy->priv->interface_name;
2200 * g_dbus_proxy_get_default_timeout:
2201 * @proxy: A #GDBusProxy.
2203 * Gets the timeout to use if -1 (specifying default timeout) is
2204 * passed as @timeout_msec in the g_dbus_proxy_call() and
2205 * g_dbus_proxy_call_sync() functions.
2207 * See the #GDBusProxy:g-default-timeout property for more details.
2209 * Returns: Timeout to use for @proxy.
2214 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2216 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2217 return proxy->priv->timeout_msec;
2221 * g_dbus_proxy_set_default_timeout:
2222 * @proxy: A #GDBusProxy.
2223 * @timeout_msec: Timeout in milliseconds.
2225 * Sets the timeout to use if -1 (specifying default timeout) is
2226 * passed as @timeout_msec in the g_dbus_proxy_call() and
2227 * g_dbus_proxy_call_sync() functions.
2229 * See the #GDBusProxy:g-default-timeout property for more details.
2234 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2237 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2238 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2240 /* TODO: locking? */
2241 if (proxy->priv->timeout_msec != timeout_msec)
2243 proxy->priv->timeout_msec = timeout_msec;
2244 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2249 * g_dbus_proxy_get_interface_info:
2250 * @proxy: A #GDBusProxy
2252 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2253 * interface that @proxy conforms to.
2255 * See the #GDBusProxy:g-interface-info property for more details.
2257 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2258 * object, it is owned by @proxy.
2262 GDBusInterfaceInfo *
2263 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2265 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2266 return proxy->priv->expected_interface;
2270 * g_dbus_proxy_set_interface_info:
2271 * @proxy: A #GDBusProxy
2272 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2274 * Ensure that interactions with @proxy conform to the given
2275 * interface. For example, when completing a method call, if the type
2276 * signature of the message isn't what's expected, the given #GError
2277 * is set. Signals that have a type signature mismatch are simply
2280 * See the #GDBusProxy:g-interface-info property for more details.
2285 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2286 GDBusInterfaceInfo *info)
2288 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2289 if (proxy->priv->expected_interface != NULL)
2291 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2292 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2294 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2295 if (proxy->priv->expected_interface != NULL)
2296 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2299 /* ---------------------------------------------------------------------------------------------------- */
2302 maybe_split_method_name (const gchar *method_name,
2303 gchar **out_interface_name,
2304 const gchar **out_method_name)
2309 g_assert (out_interface_name != NULL);
2310 g_assert (out_method_name != NULL);
2311 *out_interface_name = NULL;
2312 *out_method_name = NULL;
2314 if (strchr (method_name, '.') != NULL)
2319 p = g_strdup (method_name);
2320 last_dot = strrchr (p, '.');
2323 *out_interface_name = p;
2324 *out_method_name = last_dot + 1;
2336 GUnixFDList *fd_list;
2341 reply_data_free (ReplyData *data)
2343 g_variant_unref (data->value);
2345 if (data->fd_list != NULL)
2346 g_object_unref (data->fd_list);
2348 g_slice_free (ReplyData, data);
2352 reply_cb (GDBusConnection *connection,
2356 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2360 GUnixFDList *fd_list;
2365 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2370 value = g_dbus_connection_call_finish (connection,
2376 g_simple_async_result_take_error (simple, error);
2381 data = g_slice_new0 (ReplyData);
2382 data->value = value;
2384 data->fd_list = fd_list;
2386 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2389 /* no need to complete in idle since the method GDBusConnection already does */
2390 g_simple_async_result_complete (simple);
2391 g_object_unref (simple);
2394 static const GDBusMethodInfo *
2395 lookup_method_info_or_warn (GDBusProxy *proxy,
2396 const gchar *method_name)
2398 const GDBusMethodInfo *info;
2400 if (proxy->priv->expected_interface == NULL)
2403 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2406 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2407 method_name, proxy->priv->expected_interface->name);
2413 static const gchar *
2414 get_destination_for_call (GDBusProxy *proxy)
2420 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2421 * is never NULL and always the same as proxy->priv->name. We use this
2422 * knowledge to avoid checking if proxy->priv->name is a unique or
2425 ret = proxy->priv->name_owner;
2429 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2432 ret = proxy->priv->name;
2438 /* ---------------------------------------------------------------------------------------------------- */
2441 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2442 const gchar *method_name,
2443 GVariant *parameters,
2444 GDBusCallFlags flags,
2446 GUnixFDList *fd_list,
2447 GCancellable *cancellable,
2448 GAsyncReadyCallback callback,
2451 GSimpleAsyncResult *simple;
2453 gchar *split_interface_name;
2454 const gchar *split_method_name;
2455 const gchar *target_method_name;
2456 const gchar *target_interface_name;
2457 const gchar *destination;
2458 GVariantType *reply_type;
2460 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2461 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2462 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2463 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2464 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2467 split_interface_name = NULL;
2469 simple = g_simple_async_result_new (G_OBJECT (proxy),
2472 g_dbus_proxy_call_internal);
2474 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2475 target_method_name = was_split ? split_method_name : method_name;
2476 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2478 /* Warn if method is unexpected (cf. :g-interface-info) */
2481 const GDBusMethodInfo *expected_method_info;
2482 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2483 if (expected_method_info != NULL)
2484 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2488 if (proxy->priv->name != NULL)
2490 destination = get_destination_for_call (proxy);
2491 if (destination == NULL)
2493 g_simple_async_result_set_error (simple,
2496 _("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"));
2502 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2504 proxy->priv->object_path,
2505 target_interface_name,
2510 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2513 (GAsyncReadyCallback) reply_cb,
2516 g_dbus_connection_call (proxy->priv->connection,
2518 proxy->priv->object_path,
2519 target_interface_name,
2524 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2526 (GAsyncReadyCallback) reply_cb,
2531 if (reply_type != NULL)
2532 g_variant_type_free (reply_type);
2534 g_free (split_interface_name);
2538 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2539 GUnixFDList **out_fd_list,
2543 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2547 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2548 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2549 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2551 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2555 if (g_simple_async_result_propagate_error (simple, error))
2558 data = g_simple_async_result_get_op_res_gpointer (simple);
2559 value = g_variant_ref (data->value);
2561 if (out_fd_list != NULL)
2562 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2570 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2571 const gchar *method_name,
2572 GVariant *parameters,
2573 GDBusCallFlags flags,
2575 GUnixFDList *fd_list,
2576 GUnixFDList **out_fd_list,
2577 GCancellable *cancellable,
2582 gchar *split_interface_name;
2583 const gchar *split_method_name;
2584 const gchar *target_method_name;
2585 const gchar *target_interface_name;
2586 const gchar *destination;
2587 GVariantType *reply_type;
2589 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2590 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2591 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2592 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2593 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2594 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2598 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2599 target_method_name = was_split ? split_method_name : method_name;
2600 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2602 /* Warn if method is unexpected (cf. :g-interface-info) */
2605 const GDBusMethodInfo *expected_method_info;
2606 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2607 if (expected_method_info != NULL)
2608 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2612 if (proxy->priv->name != NULL)
2614 destination = get_destination_for_call (proxy);
2615 if (destination == NULL)
2617 g_set_error_literal (error,
2620 _("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"));
2627 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2629 proxy->priv->object_path,
2630 target_interface_name,
2635 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2641 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2643 proxy->priv->object_path,
2644 target_interface_name,
2649 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2655 if (reply_type != NULL)
2656 g_variant_type_free (reply_type);
2658 g_free (split_interface_name);
2663 /* ---------------------------------------------------------------------------------------------------- */
2666 * g_dbus_proxy_call:
2667 * @proxy: A #GDBusProxy.
2668 * @method_name: Name of method to invoke.
2669 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2670 * @flags: Flags from the #GDBusCallFlags enumeration.
2671 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2672 * "infinite") or -1 to use the proxy default timeout.
2673 * @cancellable: A #GCancellable or %NULL.
2674 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2675 * care about the result of the method invocation.
2676 * @user_data: The data to pass to @callback.
2678 * Asynchronously invokes the @method_name method on @proxy.
2680 * If @method_name contains any dots, then @name is split into interface and
2681 * method name parts. This allows using @proxy for invoking methods on
2684 * If the #GDBusConnection associated with @proxy is closed then
2685 * the operation will fail with %G_IO_ERROR_CLOSED. If
2686 * @cancellable is canceled, the operation will fail with
2687 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2688 * compatible with the D-Bus protocol, the operation fails with
2689 * %G_IO_ERROR_INVALID_ARGUMENT.
2691 * If the @parameters #GVariant is floating, it is consumed. This allows
2692 * convenient 'inline' use of g_variant_new(), e.g.:
2694 * g_dbus_proxy_call (proxy,
2696 * g_variant_new ("(ss)",
2699 * G_DBUS_CALL_FLAGS_NONE,
2702 * (GAsyncReadyCallback) two_strings_done,
2706 * This is an asynchronous method. When the operation is finished,
2707 * @callback will be invoked in the
2708 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2709 * of the thread you are calling this method from.
2710 * You can then call g_dbus_proxy_call_finish() to get the result of
2711 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2712 * version of this method.
2717 g_dbus_proxy_call (GDBusProxy *proxy,
2718 const gchar *method_name,
2719 GVariant *parameters,
2720 GDBusCallFlags flags,
2722 GCancellable *cancellable,
2723 GAsyncReadyCallback callback,
2726 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2730 * g_dbus_proxy_call_finish:
2731 * @proxy: A #GDBusProxy.
2732 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2733 * @error: Return location for error or %NULL.
2735 * Finishes an operation started with g_dbus_proxy_call().
2737 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2738 * return values. Free with g_variant_unref().
2743 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2747 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2751 * g_dbus_proxy_call_sync:
2752 * @proxy: A #GDBusProxy.
2753 * @method_name: Name of method to invoke.
2754 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2755 * or %NULL if not passing parameters.
2756 * @flags: Flags from the #GDBusCallFlags enumeration.
2757 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2758 * "infinite") or -1 to use the proxy default timeout.
2759 * @cancellable: A #GCancellable or %NULL.
2760 * @error: Return location for error or %NULL.
2762 * Synchronously invokes the @method_name method on @proxy.
2764 * If @method_name contains any dots, then @name is split into interface and
2765 * method name parts. This allows using @proxy for invoking methods on
2768 * If the #GDBusConnection associated with @proxy is disconnected then
2769 * the operation will fail with %G_IO_ERROR_CLOSED. If
2770 * @cancellable is canceled, the operation will fail with
2771 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2772 * compatible with the D-Bus protocol, the operation fails with
2773 * %G_IO_ERROR_INVALID_ARGUMENT.
2775 * If the @parameters #GVariant is floating, it is consumed. This allows
2776 * convenient 'inline' use of g_variant_new(), e.g.:
2778 * g_dbus_proxy_call_sync (proxy,
2780 * g_variant_new ("(ss)",
2783 * G_DBUS_CALL_FLAGS_NONE,
2789 * The calling thread is blocked until a reply is received. See
2790 * g_dbus_proxy_call() for the asynchronous version of this
2793 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2794 * return values. Free with g_variant_unref().
2799 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2800 const gchar *method_name,
2801 GVariant *parameters,
2802 GDBusCallFlags flags,
2804 GCancellable *cancellable,
2807 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2810 /* ---------------------------------------------------------------------------------------------------- */
2815 * g_dbus_proxy_call_with_unix_fd_list:
2816 * @proxy: A #GDBusProxy.
2817 * @method_name: Name of method to invoke.
2818 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2819 * @flags: Flags from the #GDBusCallFlags enumeration.
2820 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2821 * "infinite") or -1 to use the proxy default timeout.
2822 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2823 * @cancellable: A #GCancellable or %NULL.
2824 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2825 * care about the result of the method invocation.
2826 * @user_data: The data to pass to @callback.
2828 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2830 * This method is only available on UNIX.
2835 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
2836 const gchar *method_name,
2837 GVariant *parameters,
2838 GDBusCallFlags flags,
2840 GUnixFDList *fd_list,
2841 GCancellable *cancellable,
2842 GAsyncReadyCallback callback,
2845 return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2849 * g_dbus_proxy_call_with_unix_fd_list_finish:
2850 * @proxy: A #GDBusProxy.
2851 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2852 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2853 * @error: Return location for error or %NULL.
2855 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2857 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2858 * return values. Free with g_variant_unref().
2863 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
2864 GUnixFDList **out_fd_list,
2868 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2872 * g_dbus_proxy_call_with_unix_fd_list_sync:
2873 * @proxy: A #GDBusProxy.
2874 * @method_name: Name of method to invoke.
2875 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2876 * or %NULL if not passing parameters.
2877 * @flags: Flags from the #GDBusCallFlags enumeration.
2878 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2879 * "infinite") or -1 to use the proxy default timeout.
2880 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2881 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2882 * @cancellable: A #GCancellable or %NULL.
2883 * @error: Return location for error or %NULL.
2885 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2887 * This method is only available on UNIX.
2889 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2890 * return values. Free with g_variant_unref().
2895 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
2896 const gchar *method_name,
2897 GVariant *parameters,
2898 GDBusCallFlags flags,
2900 GUnixFDList *fd_list,
2901 GUnixFDList **out_fd_list,
2902 GCancellable *cancellable,
2905 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2908 #endif /* G_OS_UNIX */
2910 /* ---------------------------------------------------------------------------------------------------- */
2912 static GDBusInterfaceInfo *
2913 _g_dbus_proxy_get_info (GDBusInterface *interface)
2915 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2916 return g_dbus_proxy_get_interface_info (proxy);
2919 static GDBusObject *
2920 _g_dbus_proxy_get_object (GDBusInterface *interface)
2922 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2923 return proxy->priv->object;
2927 _g_dbus_proxy_set_object (GDBusInterface *interface,
2928 GDBusObject *object)
2930 GDBusProxy *proxy = G_DBUS_PROXY (interface);
2931 if (proxy->priv->object != NULL)
2932 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2933 proxy->priv->object = object;
2934 if (proxy->priv->object != NULL)
2935 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2939 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2941 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
2942 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2943 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2946 /* ---------------------------------------------------------------------------------------------------- */