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 mutable properties: name_owner, timeout_msec,
95 * expected_interface, and the properties hash table
97 G_LOCK_DEFINE_STATIC (properties_lock);
99 /* ---------------------------------------------------------------------------------------------------- */
101 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
105 volatile gint ref_count;
107 } SignalSubscriptionData;
109 static SignalSubscriptionData *
110 signal_subscription_ref (SignalSubscriptionData *data)
112 g_atomic_int_inc (&data->ref_count);
117 signal_subscription_unref (SignalSubscriptionData *data)
119 if (g_atomic_int_dec_and_test (&data->ref_count))
121 g_slice_free (SignalSubscriptionData, data);
125 /* ---------------------------------------------------------------------------------------------------- */
127 struct _GDBusProxyPrivate
130 GDBusProxyFlags flags;
131 GDBusConnection *connection;
134 /* mutable, protected by properties_lock */
137 gchar *interface_name;
138 /* mutable, protected by properties_lock */
141 guint name_owner_changed_subscription_id;
143 GCancellable *get_all_cancellable;
145 /* gchar* -> GVariant*, protected by properties_lock */
146 GHashTable *properties;
148 /* mutable, protected by properties_lock */
149 GDBusInterfaceInfo *expected_interface;
151 guint properties_changed_subscription_id;
152 guint signals_subscription_id;
154 gboolean initialized;
158 SignalSubscriptionData *signal_subscription_data;
170 PROP_G_INTERFACE_NAME,
171 PROP_G_DEFAULT_TIMEOUT,
172 PROP_G_INTERFACE_INFO
177 PROPERTIES_CHANGED_SIGNAL,
182 guint signals[LAST_SIGNAL] = {0};
184 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
185 static void initable_iface_init (GInitableIface *initable_iface);
186 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
188 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
189 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
190 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
191 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
195 g_dbus_proxy_dispose (GObject *object)
197 GDBusProxy *proxy = G_DBUS_PROXY (object);
198 G_LOCK (signal_subscription_lock);
199 if (proxy->priv->signal_subscription_data != NULL)
201 proxy->priv->signal_subscription_data->proxy = NULL;
202 signal_subscription_unref (proxy->priv->signal_subscription_data);
203 proxy->priv->signal_subscription_data = NULL;
205 G_UNLOCK (signal_subscription_lock);
207 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
211 g_dbus_proxy_finalize (GObject *object)
213 GDBusProxy *proxy = G_DBUS_PROXY (object);
215 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
217 if (proxy->priv->name_owner_changed_subscription_id > 0)
218 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
219 proxy->priv->name_owner_changed_subscription_id);
221 if (proxy->priv->properties_changed_subscription_id > 0)
222 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
223 proxy->priv->properties_changed_subscription_id);
225 if (proxy->priv->signals_subscription_id > 0)
226 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
227 proxy->priv->signals_subscription_id);
229 if (proxy->priv->connection != NULL)
230 g_object_unref (proxy->priv->connection);
231 g_free (proxy->priv->name);
232 g_free (proxy->priv->name_owner);
233 g_free (proxy->priv->object_path);
234 g_free (proxy->priv->interface_name);
235 if (proxy->priv->properties != NULL)
236 g_hash_table_unref (proxy->priv->properties);
238 if (proxy->priv->expected_interface != NULL)
240 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
241 g_dbus_interface_info_unref (proxy->priv->expected_interface);
244 if (proxy->priv->object != NULL)
245 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
247 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
251 g_dbus_proxy_get_property (GObject *object,
256 GDBusProxy *proxy = G_DBUS_PROXY (object);
260 case PROP_G_CONNECTION:
261 g_value_set_object (value, proxy->priv->connection);
265 g_value_set_flags (value, proxy->priv->flags);
269 g_value_set_string (value, proxy->priv->name);
272 case PROP_G_NAME_OWNER:
273 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
276 case PROP_G_OBJECT_PATH:
277 g_value_set_string (value, proxy->priv->object_path);
280 case PROP_G_INTERFACE_NAME:
281 g_value_set_string (value, proxy->priv->interface_name);
284 case PROP_G_DEFAULT_TIMEOUT:
285 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
288 case PROP_G_INTERFACE_INFO:
289 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 g_dbus_proxy_set_property (GObject *object,
304 GDBusProxy *proxy = G_DBUS_PROXY (object);
308 case PROP_G_CONNECTION:
309 proxy->priv->connection = g_value_dup_object (value);
313 proxy->priv->flags = g_value_get_flags (value);
317 proxy->priv->name = g_value_dup_string (value);
320 case PROP_G_OBJECT_PATH:
321 proxy->priv->object_path = g_value_dup_string (value);
324 case PROP_G_INTERFACE_NAME:
325 proxy->priv->interface_name = g_value_dup_string (value);
328 case PROP_G_DEFAULT_TIMEOUT:
329 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
332 case PROP_G_INTERFACE_INFO:
333 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
336 case PROP_G_BUS_TYPE:
337 proxy->priv->bus_type = g_value_get_enum (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 g_dbus_proxy_class_init (GDBusProxyClass *klass)
349 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
351 gobject_class->dispose = g_dbus_proxy_dispose;
352 gobject_class->finalize = g_dbus_proxy_finalize;
353 gobject_class->set_property = g_dbus_proxy_set_property;
354 gobject_class->get_property = g_dbus_proxy_get_property;
356 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
357 * in derived classes */
360 * GDBusProxy:g-interface-info:
362 * Ensure that interactions with this proxy conform to the given
363 * interface. For example, when completing a method call, if the
364 * type signature of the message isn't what's expected, the given
365 * #GError is set. Signals that have a type signature mismatch are
370 g_object_class_install_property (gobject_class,
371 PROP_G_INTERFACE_INFO,
372 g_param_spec_boxed ("g-interface-info",
373 P_("Interface Information"),
374 P_("Interface Information"),
375 G_TYPE_DBUS_INTERFACE_INFO,
378 G_PARAM_STATIC_NAME |
379 G_PARAM_STATIC_BLURB |
380 G_PARAM_STATIC_NICK));
383 * GDBusProxy:g-connection:
385 * The #GDBusConnection the proxy is for.
389 g_object_class_install_property (gobject_class,
391 g_param_spec_object ("g-connection",
393 P_("The connection the proxy is for"),
394 G_TYPE_DBUS_CONNECTION,
397 G_PARAM_CONSTRUCT_ONLY |
398 G_PARAM_STATIC_NAME |
399 G_PARAM_STATIC_BLURB |
400 G_PARAM_STATIC_NICK));
403 * GDBusProxy:g-bus-type:
405 * If this property is not %G_BUS_TYPE_NONE, then
406 * #GDBusProxy:g-connection must be %NULL and will be set to the
407 * #GDBusConnection obtained by calling g_bus_get() with the value
412 g_object_class_install_property (gobject_class,
414 g_param_spec_enum ("g-bus-type",
416 P_("The bus to connect to, if any"),
420 G_PARAM_CONSTRUCT_ONLY |
421 G_PARAM_STATIC_NAME |
422 G_PARAM_STATIC_BLURB |
423 G_PARAM_STATIC_NICK));
426 * GDBusProxy:g-flags:
428 * Flags from the #GDBusProxyFlags enumeration.
432 g_object_class_install_property (gobject_class,
434 g_param_spec_flags ("g-flags",
436 P_("Flags for the proxy"),
437 G_TYPE_DBUS_PROXY_FLAGS,
438 G_DBUS_PROXY_FLAGS_NONE,
441 G_PARAM_CONSTRUCT_ONLY |
442 G_PARAM_STATIC_NAME |
443 G_PARAM_STATIC_BLURB |
444 G_PARAM_STATIC_NICK));
449 * The well-known or unique name that the proxy is for.
453 g_object_class_install_property (gobject_class,
455 g_param_spec_string ("g-name",
457 P_("The well-known or unique name that the proxy is for"),
461 G_PARAM_CONSTRUCT_ONLY |
462 G_PARAM_STATIC_NAME |
463 G_PARAM_STATIC_BLURB |
464 G_PARAM_STATIC_NICK));
467 * GDBusProxy:g-name-owner:
469 * The unique name that owns #GDBusProxy:name or %NULL if no-one
470 * currently owns that name. You may connect to #GObject::notify signal to
471 * track changes to this property.
475 g_object_class_install_property (gobject_class,
477 g_param_spec_string ("g-name-owner",
479 P_("The unique name for the owner"),
482 G_PARAM_STATIC_NAME |
483 G_PARAM_STATIC_BLURB |
484 G_PARAM_STATIC_NICK));
487 * GDBusProxy:g-object-path:
489 * The object path the proxy is for.
493 g_object_class_install_property (gobject_class,
495 g_param_spec_string ("g-object-path",
497 P_("The object path the proxy is for"),
501 G_PARAM_CONSTRUCT_ONLY |
502 G_PARAM_STATIC_NAME |
503 G_PARAM_STATIC_BLURB |
504 G_PARAM_STATIC_NICK));
507 * GDBusProxy:g-interface-name:
509 * The D-Bus interface name the proxy is for.
513 g_object_class_install_property (gobject_class,
514 PROP_G_INTERFACE_NAME,
515 g_param_spec_string ("g-interface-name",
516 P_("g-interface-name"),
517 P_("The D-Bus interface name the proxy is for"),
521 G_PARAM_CONSTRUCT_ONLY |
522 G_PARAM_STATIC_NAME |
523 G_PARAM_STATIC_BLURB |
524 G_PARAM_STATIC_NICK));
527 * GDBusProxy:g-default-timeout:
529 * The timeout to use if -1 (specifying default timeout) is passed
530 * as @timeout_msec in the g_dbus_proxy_call() and
531 * g_dbus_proxy_call_sync() functions.
533 * This allows applications to set a proxy-wide timeout for all
534 * remote method invocations on the proxy. If this property is -1,
535 * the default timeout (typically 25 seconds) is used. If set to
536 * %G_MAXINT, then no timeout is used.
540 g_object_class_install_property (gobject_class,
541 PROP_G_DEFAULT_TIMEOUT,
542 g_param_spec_int ("g-default-timeout",
543 P_("Default Timeout"),
544 P_("Timeout for remote method invocation"),
551 G_PARAM_STATIC_NAME |
552 G_PARAM_STATIC_BLURB |
553 G_PARAM_STATIC_NICK));
556 * GDBusProxy::g-properties-changed:
557 * @proxy: The #GDBusProxy emitting the signal.
558 * @changed_properties: A #GVariant containing the properties that changed
559 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
561 * Emitted when one or more D-Bus properties on @proxy changes. The
562 * local cache has already been updated when this signal fires. Note
563 * that both @changed_properties and @invalidated_properties are
564 * guaranteed to never be %NULL (either may be empty though).
566 * This signal corresponds to the
567 * <literal>PropertiesChanged</literal> D-Bus signal on the
568 * <literal>org.freedesktop.DBus.Properties</literal> interface.
572 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
574 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
575 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
582 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
585 * GDBusProxy::g-signal:
586 * @proxy: The #GDBusProxy emitting the signal.
587 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
588 * @signal_name: The name of the signal.
589 * @parameters: A #GVariant tuple with parameters for the signal.
591 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
595 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
597 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
598 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
609 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
613 g_dbus_proxy_init (GDBusProxy *proxy)
615 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
616 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
617 proxy->priv->signal_subscription_data->ref_count = 1;
618 proxy->priv->signal_subscription_data->proxy = proxy;
619 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
622 (GDestroyNotify) g_variant_unref);
625 /* ---------------------------------------------------------------------------------------------------- */
628 property_name_sort_func (const gchar **a,
631 return g_strcmp0 (*a, *b);
635 * g_dbus_proxy_get_cached_property_names:
636 * @proxy: A #GDBusProxy.
638 * Gets the names of all cached properties on @proxy.
640 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
641 * @proxy has no cached properties. Free the returned array with
647 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
654 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
656 G_LOCK (properties_lock);
659 if (g_hash_table_size (proxy->priv->properties) == 0)
662 p = g_ptr_array_new ();
664 g_hash_table_iter_init (&iter, proxy->priv->properties);
665 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
666 g_ptr_array_add (p, g_strdup (key));
667 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
668 g_ptr_array_add (p, NULL);
670 names = (gchar **) g_ptr_array_free (p, FALSE);
673 G_UNLOCK (properties_lock);
677 /* properties_lock must be held for as long as you will keep the
680 static const GDBusPropertyInfo *
681 lookup_property_info_or_warn (GDBusProxy *proxy,
682 const gchar *property_name)
684 const GDBusPropertyInfo *info;
686 if (proxy->priv->expected_interface == NULL)
689 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
692 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
694 proxy->priv->expected_interface->name);
701 * g_dbus_proxy_get_cached_property:
702 * @proxy: A #GDBusProxy.
703 * @property_name: Property name.
705 * Looks up the value for a property from the cache. This call does no
708 * If @proxy has an expected interface (see
709 * #GDBusProxy:g-interface-info), then @property_name (for existence)
710 * is checked against it.
712 * Returns: A reference to the #GVariant instance that holds the value
713 * for @property_name or %NULL if the value is not in the cache. The
714 * returned reference must be freed with g_variant_unref().
719 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
720 const gchar *property_name)
724 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
725 g_return_val_if_fail (property_name != NULL, NULL);
727 G_LOCK (properties_lock);
729 value = g_hash_table_lookup (proxy->priv->properties, property_name);
732 lookup_property_info_or_warn (proxy, property_name);
737 g_variant_ref (value);
740 G_UNLOCK (properties_lock);
745 * g_dbus_proxy_set_cached_property:
746 * @proxy: A #GDBusProxy
747 * @property_name: Property name.
748 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
750 * If @value is not %NULL, sets the cached value for the property with
751 * name @property_name to the value in @value.
753 * If @value is %NULL, then the cached value is removed from the
756 * If @proxy has an expected interface (see
757 * #GDBusProxy:g-interface-info), then @property_name (for existence)
758 * and @value (for the type) is checked against it.
760 * If the @value #GVariant is floating, it is consumed. This allows
761 * convenient 'inline' use of g_variant_new(), e.g.
763 * g_dbus_proxy_set_cached_property (proxy,
765 * g_variant_new ("(si)",
770 * Normally you will not need to use this method since @proxy is
771 * tracking changes using the
772 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
773 * D-Bus signal. However, for performance reasons an object may decide
774 * to not use this signal for some properties and instead use a
775 * proprietary out-of-band mechanism to transmit changes.
777 * As a concrete example, consider an object with a property
778 * <literal>ChatroomParticipants</literal> which is an array of
779 * strings. Instead of transmitting the same (long) array every time
780 * the property changes, it is more efficient to only transmit the
781 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
782 * name)</literal> and <literal>ChatroomParticipantParted(String
788 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
789 const gchar *property_name,
792 const GDBusPropertyInfo *info;
794 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
795 g_return_if_fail (property_name != NULL);
797 G_LOCK (properties_lock);
801 info = lookup_property_info_or_warn (proxy, property_name);
804 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
806 g_warning ("Trying to set property %s of type %s but according to the expected "
807 "interface the type is %s",
809 g_variant_get_type_string (value),
814 g_hash_table_insert (proxy->priv->properties,
815 g_strdup (property_name),
816 g_variant_ref_sink (value));
820 g_hash_table_remove (proxy->priv->properties, property_name);
824 G_UNLOCK (properties_lock);
827 /* ---------------------------------------------------------------------------------------------------- */
830 on_signal_received (GDBusConnection *connection,
831 const gchar *sender_name,
832 const gchar *object_path,
833 const gchar *interface_name,
834 const gchar *signal_name,
835 GVariant *parameters,
838 SignalSubscriptionData *data = user_data;
841 G_LOCK (signal_subscription_lock);
845 G_UNLOCK (signal_subscription_lock);
850 g_object_ref (proxy);
851 G_UNLOCK (signal_subscription_lock);
854 if (!proxy->priv->initialized)
857 G_LOCK (properties_lock);
859 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
861 G_UNLOCK (properties_lock);
865 if (proxy->priv->expected_interface != NULL)
867 const GDBusSignalInfo *info;
868 GVariantType *expected_type;
869 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
872 G_UNLOCK (properties_lock);
876 expected_type = _g_dbus_compute_complete_signature (info->args);
877 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
879 g_variant_type_free (expected_type);
880 G_UNLOCK (properties_lock);
883 g_variant_type_free (expected_type);
886 G_UNLOCK (properties_lock);
888 g_signal_emit (proxy,
889 signals[SIGNAL_SIGNAL],
897 g_object_unref (proxy);
900 /* ---------------------------------------------------------------------------------------------------- */
902 /* must hold properties_lock */
904 insert_property_checked (GDBusProxy *proxy,
905 gchar *property_name,
908 if (proxy->priv->expected_interface != NULL)
910 const GDBusPropertyInfo *info;
912 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
913 /* Ignore unknown properties */
917 /* Ignore properties with the wrong type */
918 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
922 g_hash_table_insert (proxy->priv->properties,
923 property_name, /* adopts string */
924 value); /* adopts value */
929 g_variant_unref (value);
930 g_free (property_name);
934 on_properties_changed (GDBusConnection *connection,
935 const gchar *sender_name,
936 const gchar *object_path,
937 const gchar *interface_name,
938 const gchar *signal_name,
939 GVariant *parameters,
942 SignalSubscriptionData *data = user_data;
944 const gchar *interface_name_for_signal;
945 GVariant *changed_properties;
946 gchar **invalidated_properties;
952 changed_properties = NULL;
953 invalidated_properties = NULL;
955 G_LOCK (signal_subscription_lock);
959 G_UNLOCK (signal_subscription_lock);
964 g_object_ref (proxy);
965 G_UNLOCK (signal_subscription_lock);
968 if (!proxy->priv->initialized)
971 G_LOCK (properties_lock);
973 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
975 G_UNLOCK (properties_lock);
979 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
981 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
982 g_variant_get_type_string (parameters));
983 G_UNLOCK (properties_lock);
987 g_variant_get (parameters,
989 &interface_name_for_signal,
991 &invalidated_properties);
993 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
995 G_UNLOCK (properties_lock);
999 g_variant_iter_init (&iter, changed_properties);
1000 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1002 insert_property_checked (proxy,
1003 key, /* adopts string */
1004 value); /* adopts value */
1007 for (n = 0; invalidated_properties[n] != NULL; n++)
1009 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1012 G_UNLOCK (properties_lock);
1015 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1018 invalidated_properties);
1021 if (changed_properties != NULL)
1022 g_variant_unref (changed_properties);
1023 g_free (invalidated_properties);
1025 g_object_unref (proxy);
1028 /* ---------------------------------------------------------------------------------------------------- */
1031 process_get_all_reply (GDBusProxy *proxy,
1037 guint num_properties;
1039 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1041 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1042 g_variant_get_type_string (result));
1046 G_LOCK (properties_lock);
1048 g_variant_get (result, "(a{sv})", &iter);
1049 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1051 insert_property_checked (proxy,
1052 key, /* adopts string */
1053 value); /* adopts value */
1055 g_variant_iter_free (iter);
1057 num_properties = g_hash_table_size (proxy->priv->properties);
1058 G_UNLOCK (properties_lock);
1060 /* Synthesize ::g-properties-changed changed */
1061 if (num_properties > 0)
1063 GVariant *changed_properties;
1064 const gchar *invalidated_properties[1] = {NULL};
1066 g_variant_get (result,
1068 &changed_properties);
1069 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1072 invalidated_properties);
1073 g_variant_unref (changed_properties);
1083 GCancellable *cancellable;
1085 } LoadPropertiesOnNameOwnerChangedData;
1088 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1092 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1100 result = g_dbus_connection_call_finish (connection,
1105 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1107 /* We just ignore if GetAll() is failing. Because this might happen
1108 * if the object has no properties at all. Or if the caller is
1109 * not authorized to see the properties.
1111 * Either way, apps can know about this by using
1112 * get_cached_property_names() or get_cached_property().
1114 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1115 * fact that GetAll() failed
1117 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1118 g_error_free (error);
1121 /* and finally we can notify */
1124 G_LOCK (properties_lock);
1125 g_free (data->proxy->priv->name_owner);
1126 data->proxy->priv->name_owner = data->name_owner;
1127 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1128 g_hash_table_remove_all (data->proxy->priv->properties);
1129 G_UNLOCK (properties_lock);
1132 process_get_all_reply (data->proxy, result);
1133 g_variant_unref (result);
1136 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1139 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1140 data->proxy->priv->get_all_cancellable = NULL;
1142 g_object_unref (data->proxy);
1143 g_object_unref (data->cancellable);
1144 g_free (data->name_owner);
1149 on_name_owner_changed (GDBusConnection *connection,
1150 const gchar *sender_name,
1151 const gchar *object_path,
1152 const gchar *interface_name,
1153 const gchar *signal_name,
1154 GVariant *parameters,
1157 SignalSubscriptionData *data = user_data;
1159 const gchar *old_owner;
1160 const gchar *new_owner;
1162 G_LOCK (signal_subscription_lock);
1163 proxy = data->proxy;
1166 G_UNLOCK (signal_subscription_lock);
1171 g_object_ref (proxy);
1172 G_UNLOCK (signal_subscription_lock);
1175 /* if we are already trying to load properties, cancel that */
1176 if (proxy->priv->get_all_cancellable != NULL)
1178 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1179 proxy->priv->get_all_cancellable = NULL;
1182 g_variant_get (parameters,
1188 if (strlen (new_owner) == 0)
1190 G_LOCK (properties_lock);
1191 g_free (proxy->priv->name_owner);
1192 proxy->priv->name_owner = NULL;
1194 /* Synthesize ::g-properties-changed changed */
1195 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1196 g_hash_table_size (proxy->priv->properties) > 0)
1198 GVariantBuilder builder;
1199 GPtrArray *invalidated_properties;
1200 GHashTableIter iter;
1203 /* Build changed_properties (always empty) and invalidated_properties ... */
1204 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1206 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1207 g_hash_table_iter_init (&iter, proxy->priv->properties);
1208 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1209 g_ptr_array_add (invalidated_properties, g_strdup (key));
1210 g_ptr_array_add (invalidated_properties, NULL);
1212 /* ... throw out the properties ... */
1213 g_hash_table_remove_all (proxy->priv->properties);
1215 G_UNLOCK (properties_lock);
1217 /* ... and finally emit the ::g-properties-changed signal */
1218 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1220 g_variant_builder_end (&builder) /* consumed */,
1221 (const gchar* const *) invalidated_properties->pdata);
1222 g_ptr_array_unref (invalidated_properties);
1226 G_UNLOCK (properties_lock);
1228 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1232 G_LOCK (properties_lock);
1234 /* ignore duplicates - this can happen when activating the service */
1235 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1237 G_UNLOCK (properties_lock);
1241 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1243 g_free (proxy->priv->name_owner);
1244 proxy->priv->name_owner = g_strdup (new_owner);
1246 g_hash_table_remove_all (proxy->priv->properties);
1247 G_UNLOCK (properties_lock);
1248 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1252 LoadPropertiesOnNameOwnerChangedData *data;
1254 G_UNLOCK (properties_lock);
1256 /* start loading properties.. only then emit notify::g-name-owner .. we
1257 * need to be able to cancel this in the event another NameOwnerChanged
1258 * signal suddenly happens
1261 g_assert (proxy->priv->get_all_cancellable == NULL);
1262 proxy->priv->get_all_cancellable = g_cancellable_new ();
1263 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1264 data->proxy = g_object_ref (proxy);
1265 data->cancellable = proxy->priv->get_all_cancellable;
1266 data->name_owner = g_strdup (new_owner);
1267 g_dbus_connection_call (proxy->priv->connection,
1269 proxy->priv->object_path,
1270 "org.freedesktop.DBus.Properties",
1272 g_variant_new ("(s)", proxy->priv->interface_name),
1273 G_VARIANT_TYPE ("(a{sv})"),
1274 G_DBUS_CALL_FLAGS_NONE,
1276 proxy->priv->get_all_cancellable,
1277 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1284 g_object_unref (proxy);
1287 /* ---------------------------------------------------------------------------------------------------- */
1292 GCancellable *cancellable;
1293 GSimpleAsyncResult *simple;
1297 async_init_data_free (AsyncInitData *data)
1299 g_object_unref (data->proxy);
1300 if (data->cancellable != NULL)
1301 g_object_unref (data->cancellable);
1302 g_object_unref (data->simple);
1307 async_init_get_all_cb (GDBusConnection *connection,
1311 AsyncInitData *data = user_data;
1316 result = g_dbus_connection_call_finish (connection,
1321 /* We just ignore if GetAll() is failing. Because this might happen
1322 * if the object has no properties at all. Or if the caller is
1323 * not authorized to see the properties.
1325 * Either way, apps can know about this by using
1326 * get_cached_property_names() or get_cached_property().
1328 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1329 * fact that GetAll() failed
1331 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1332 g_error_free (error);
1336 g_simple_async_result_set_op_res_gpointer (data->simple,
1338 (GDestroyNotify) g_variant_unref);
1341 g_simple_async_result_complete_in_idle (data->simple);
1342 async_init_data_free (data);
1346 async_init_data_set_name_owner (AsyncInitData *data,
1347 const gchar *name_owner)
1352 if (name_owner != NULL)
1354 /* it starts as NULL anyway */
1355 G_LOCK (properties_lock);
1356 data->proxy->priv->name_owner = g_strdup (name_owner);
1357 G_UNLOCK (properties_lock);
1362 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1364 /* Don't load properties if the API user doesn't want them */
1367 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1369 /* Don't attempt to load properties if the name_owner is NULL (which
1370 * usually means the name isn't owned), unless name is also NULL (which
1371 * means we actually wanted to talk to the directly-connected process -
1372 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1379 /* load all properties asynchronously */
1380 g_dbus_connection_call (data->proxy->priv->connection,
1382 data->proxy->priv->object_path,
1383 "org.freedesktop.DBus.Properties",
1385 g_variant_new ("(s)", data->proxy->priv->interface_name),
1386 G_VARIANT_TYPE ("(a{sv})"),
1387 G_DBUS_CALL_FLAGS_NONE,
1390 (GAsyncReadyCallback) async_init_get_all_cb,
1395 g_simple_async_result_complete_in_idle (data->simple);
1396 async_init_data_free (data);
1401 async_init_get_name_owner_cb (GDBusConnection *connection,
1405 AsyncInitData *data = user_data;
1410 result = g_dbus_connection_call_finish (connection,
1415 if (error->domain == G_DBUS_ERROR &&
1416 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1418 g_error_free (error);
1419 async_init_data_set_name_owner (data, NULL);
1423 g_simple_async_result_take_error (data->simple, error);
1424 g_simple_async_result_complete_in_idle (data->simple);
1425 async_init_data_free (data);
1430 /* borrowed from result to avoid an extra copy */
1431 const gchar *name_owner;
1433 g_variant_get (result, "(&s)", &name_owner);
1434 async_init_data_set_name_owner (data, name_owner);
1435 g_variant_unref (result);
1440 async_init_call_get_name_owner (AsyncInitData *data)
1442 g_dbus_connection_call (data->proxy->priv->connection,
1443 "org.freedesktop.DBus", /* name */
1444 "/org/freedesktop/DBus", /* object path */
1445 "org.freedesktop.DBus", /* interface */
1447 g_variant_new ("(s)",
1448 data->proxy->priv->name),
1449 G_VARIANT_TYPE ("(s)"),
1450 G_DBUS_CALL_FLAGS_NONE,
1453 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1458 async_init_start_service_by_name_cb (GDBusConnection *connection,
1462 AsyncInitData *data = user_data;
1467 result = g_dbus_connection_call_finish (connection,
1472 /* Errors are not unexpected; the bus will reply e.g.
1474 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1475 * was not provided by any .service files
1477 * This doesn't mean that the name doesn't have an owner, just
1478 * that it's not provided by a .service file. So just proceed to
1479 * invoke GetNameOwner() if dealing with that error.
1481 if (error->domain == G_DBUS_ERROR &&
1482 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1484 g_error_free (error);
1488 g_prefix_error (&error,
1489 _("Error calling StartServiceByName for %s: "),
1490 data->proxy->priv->name);
1496 guint32 start_service_result;
1497 g_variant_get (result,
1499 &start_service_result);
1500 g_variant_unref (result);
1501 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1502 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1504 /* continue to invoke GetNameOwner() */
1508 error = g_error_new (G_IO_ERROR,
1510 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1511 start_service_result,
1512 data->proxy->priv->name);
1517 async_init_call_get_name_owner (data);
1521 g_warn_if_fail (error != NULL);
1522 g_simple_async_result_take_error (data->simple, error);
1523 g_simple_async_result_complete_in_idle (data->simple);
1524 async_init_data_free (data);
1528 async_init_call_start_service_by_name (AsyncInitData *data)
1530 g_dbus_connection_call (data->proxy->priv->connection,
1531 "org.freedesktop.DBus", /* name */
1532 "/org/freedesktop/DBus", /* object path */
1533 "org.freedesktop.DBus", /* interface */
1534 "StartServiceByName",
1535 g_variant_new ("(su)",
1536 data->proxy->priv->name,
1538 G_VARIANT_TYPE ("(u)"),
1539 G_DBUS_CALL_FLAGS_NONE,
1542 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1547 async_initable_init_second_async (GAsyncInitable *initable,
1549 GCancellable *cancellable,
1550 GAsyncReadyCallback callback,
1553 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1554 AsyncInitData *data;
1556 data = g_new0 (AsyncInitData, 1);
1557 data->proxy = g_object_ref (proxy);
1558 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1559 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1564 /* Check name ownership asynchronously - possibly also start the service */
1565 if (proxy->priv->name == NULL)
1568 async_init_data_set_name_owner (data, NULL);
1570 else if (g_dbus_is_unique_name (proxy->priv->name))
1572 async_init_data_set_name_owner (data, proxy->priv->name);
1576 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1578 async_init_call_get_name_owner (data);
1582 async_init_call_start_service_by_name (data);
1588 async_initable_init_second_finish (GAsyncInitable *initable,
1592 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1593 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1599 if (g_simple_async_result_propagate_error (simple, error))
1602 result = g_simple_async_result_get_op_res_gpointer (simple);
1605 process_get_all_reply (proxy, result);
1611 proxy->priv->initialized = TRUE;
1615 /* ---------------------------------------------------------------------------------------------------- */
1618 async_initable_init_first (GAsyncInitable *initable)
1620 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1622 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1624 /* subscribe to PropertiesChanged() */
1625 proxy->priv->properties_changed_subscription_id =
1626 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1628 "org.freedesktop.DBus.Properties",
1629 "PropertiesChanged",
1630 proxy->priv->object_path,
1631 proxy->priv->interface_name,
1632 G_DBUS_SIGNAL_FLAGS_NONE,
1633 on_properties_changed,
1634 signal_subscription_ref (proxy->priv->signal_subscription_data),
1635 (GDestroyNotify) signal_subscription_unref);
1638 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1640 /* subscribe to all signals for the object */
1641 proxy->priv->signals_subscription_id =
1642 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1644 proxy->priv->interface_name,
1646 proxy->priv->object_path,
1648 G_DBUS_SIGNAL_FLAGS_NONE,
1650 signal_subscription_ref (proxy->priv->signal_subscription_data),
1651 (GDestroyNotify) signal_subscription_unref);
1654 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1656 proxy->priv->name_owner_changed_subscription_id =
1657 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1658 "org.freedesktop.DBus", /* name */
1659 "org.freedesktop.DBus", /* interface */
1660 "NameOwnerChanged", /* signal name */
1661 "/org/freedesktop/DBus", /* path */
1662 proxy->priv->name, /* arg0 */
1663 G_DBUS_SIGNAL_FLAGS_NONE,
1664 on_name_owner_changed,
1665 signal_subscription_ref (proxy->priv->signal_subscription_data),
1666 (GDestroyNotify) signal_subscription_unref);
1670 /* ---------------------------------------------------------------------------------------------------- */
1672 /* initialization is split into two parts - the first is the
1673 * non-blocing part that requires the callers GMainContext - the
1674 * second is a blocking part async part that doesn't require the
1675 * callers GMainContext.. we do this split so the code can be reused
1676 * in the GInitable implementation below.
1678 * Note that obtaining a GDBusConnection is not shared between the two
1686 GCancellable *cancellable;
1687 GAsyncReadyCallback callback;
1689 } GetConnectionData;
1692 get_connection_cb (GObject *source_object,
1696 GetConnectionData *data = user_data;
1700 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1701 if (data->proxy->priv->connection == NULL)
1703 GSimpleAsyncResult *simple;
1704 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1708 g_simple_async_result_take_error (simple, error);
1709 g_simple_async_result_complete_in_idle (simple);
1710 g_object_unref (simple);
1714 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1715 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1722 if (data->cancellable != NULL)
1723 g_object_unref (data->cancellable);
1725 g_object_unref (data->proxy);
1730 async_initable_init_async (GAsyncInitable *initable,
1732 GCancellable *cancellable,
1733 GAsyncReadyCallback callback,
1736 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1738 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1740 GetConnectionData *data;
1742 g_assert (proxy->priv->connection == NULL);
1744 data = g_new0 (GetConnectionData, 1);
1745 data->proxy = g_object_ref (proxy);
1746 data->io_priority = io_priority;
1747 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1748 data->callback = callback;
1749 data->user_data = user_data;
1750 g_bus_get (proxy->priv->bus_type,
1757 async_initable_init_first (initable);
1758 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1763 async_initable_init_finish (GAsyncInitable *initable,
1767 return async_initable_init_second_finish (initable, res, error);
1771 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1773 async_initable_iface->init_async = async_initable_init_async;
1774 async_initable_iface->init_finish = async_initable_init_finish;
1777 /* ---------------------------------------------------------------------------------------------------- */
1781 GMainContext *context;
1784 } InitableAsyncInitableData;
1787 async_initable_init_async_cb (GObject *source_object,
1791 InitableAsyncInitableData *data = user_data;
1792 data->res = g_object_ref (res);
1793 g_main_loop_quit (data->loop);
1796 /* Simply reuse the GAsyncInitable implementation but run the first
1797 * part (that is non-blocking and requires the callers GMainContext)
1798 * with the callers GMainContext.. and the second with a private
1799 * GMainContext (bug 621310 is slightly related).
1801 * Note that obtaining a GDBusConnection is not shared between the two
1805 initable_init (GInitable *initable,
1806 GCancellable *cancellable,
1809 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1810 InitableAsyncInitableData *data;
1815 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1817 g_assert (proxy->priv->connection == NULL);
1818 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1821 if (proxy->priv->connection == NULL)
1825 async_initable_init_first (G_ASYNC_INITABLE (initable));
1827 data = g_new0 (InitableAsyncInitableData, 1);
1828 data->context = g_main_context_new ();
1829 data->loop = g_main_loop_new (data->context, FALSE);
1831 g_main_context_push_thread_default (data->context);
1833 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1836 async_initable_init_async_cb,
1839 g_main_loop_run (data->loop);
1841 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1845 g_main_context_pop_thread_default (data->context);
1847 g_main_context_unref (data->context);
1848 g_main_loop_unref (data->loop);
1849 g_object_unref (data->res);
1858 initable_iface_init (GInitableIface *initable_iface)
1860 initable_iface->init = initable_init;
1863 /* ---------------------------------------------------------------------------------------------------- */
1867 * @connection: A #GDBusConnection.
1868 * @flags: Flags used when constructing the proxy.
1869 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1870 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1871 * @object_path: An object path.
1872 * @interface_name: A D-Bus interface name.
1873 * @cancellable: A #GCancellable or %NULL.
1874 * @callback: Callback function to invoke when the proxy is ready.
1875 * @user_data: User data to pass to @callback.
1877 * Creates a proxy for accessing @interface_name on the remote object
1878 * at @object_path owned by @name at @connection and asynchronously
1879 * loads D-Bus properties unless the
1880 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1881 * the #GDBusProxy::g-properties-changed signal to get notified about
1884 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1885 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1886 * to handle signals from the remote object.
1888 * If @name is a well-known name and the
1889 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1890 * owner currently exists, the message bus will be requested to launch
1891 * a name owner for the name.
1893 * This is a failable asynchronous constructor - when the proxy is
1894 * ready, @callback will be invoked and you can use
1895 * g_dbus_proxy_new_finish() to get the result.
1897 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1899 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1904 g_dbus_proxy_new (GDBusConnection *connection,
1905 GDBusProxyFlags flags,
1906 GDBusInterfaceInfo *info,
1908 const gchar *object_path,
1909 const gchar *interface_name,
1910 GCancellable *cancellable,
1911 GAsyncReadyCallback callback,
1914 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1915 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1916 g_return_if_fail (g_variant_is_object_path (object_path));
1917 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1919 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1925 "g-interface-info", info,
1927 "g-connection", connection,
1928 "g-object-path", object_path,
1929 "g-interface-name", interface_name,
1934 * g_dbus_proxy_new_finish:
1935 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1936 * @error: Return location for error or %NULL.
1938 * Finishes creating a #GDBusProxy.
1940 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1945 g_dbus_proxy_new_finish (GAsyncResult *res,
1949 GObject *source_object;
1951 source_object = g_async_result_get_source_object (res);
1952 g_assert (source_object != NULL);
1954 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1957 g_object_unref (source_object);
1960 return G_DBUS_PROXY (object);
1966 * g_dbus_proxy_new_sync:
1967 * @connection: A #GDBusConnection.
1968 * @flags: Flags used when constructing the proxy.
1969 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1970 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1971 * @object_path: An object path.
1972 * @interface_name: A D-Bus interface name.
1973 * @cancellable: (allow-none): A #GCancellable or %NULL.
1974 * @error: (allow-none): Return location for error or %NULL.
1976 * Creates a proxy for accessing @interface_name on the remote object
1977 * at @object_path owned by @name at @connection and synchronously
1978 * loads D-Bus properties unless the
1979 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1981 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1982 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1983 * to handle signals from the remote object.
1985 * If @name is a well-known name and the
1986 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1987 * owner currently exists, the message bus will be requested to launch
1988 * a name owner for the name.
1990 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1991 * and g_dbus_proxy_new_finish() for the asynchronous version.
1993 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1995 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2000 g_dbus_proxy_new_sync (GDBusConnection *connection,
2001 GDBusProxyFlags flags,
2002 GDBusInterfaceInfo *info,
2004 const gchar *object_path,
2005 const gchar *interface_name,
2006 GCancellable *cancellable,
2009 GInitable *initable;
2011 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2012 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2013 g_dbus_is_name (name), NULL);
2014 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2015 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2017 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2021 "g-interface-info", info,
2023 "g-connection", connection,
2024 "g-object-path", object_path,
2025 "g-interface-name", interface_name,
2027 if (initable != NULL)
2028 return G_DBUS_PROXY (initable);
2033 /* ---------------------------------------------------------------------------------------------------- */
2036 * g_dbus_proxy_new_for_bus:
2037 * @bus_type: A #GBusType.
2038 * @flags: Flags used when constructing the proxy.
2039 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2040 * @name: A bus name (well-known or unique).
2041 * @object_path: An object path.
2042 * @interface_name: A D-Bus interface name.
2043 * @cancellable: A #GCancellable or %NULL.
2044 * @callback: Callback function to invoke when the proxy is ready.
2045 * @user_data: User data to pass to @callback.
2047 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2049 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2054 g_dbus_proxy_new_for_bus (GBusType bus_type,
2055 GDBusProxyFlags flags,
2056 GDBusInterfaceInfo *info,
2058 const gchar *object_path,
2059 const gchar *interface_name,
2060 GCancellable *cancellable,
2061 GAsyncReadyCallback callback,
2064 g_return_if_fail (g_dbus_is_name (name));
2065 g_return_if_fail (g_variant_is_object_path (object_path));
2066 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2068 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2074 "g-interface-info", info,
2076 "g-bus-type", bus_type,
2077 "g-object-path", object_path,
2078 "g-interface-name", interface_name,
2083 * g_dbus_proxy_new_for_bus_finish:
2084 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2085 * @error: Return location for error or %NULL.
2087 * Finishes creating a #GDBusProxy.
2089 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2094 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2097 return g_dbus_proxy_new_finish (res, error);
2101 * g_dbus_proxy_new_for_bus_sync:
2102 * @bus_type: A #GBusType.
2103 * @flags: Flags used when constructing the proxy.
2104 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2105 * that @proxy conforms to or %NULL.
2106 * @name: A bus name (well-known or unique).
2107 * @object_path: An object path.
2108 * @interface_name: A D-Bus interface name.
2109 * @cancellable: A #GCancellable or %NULL.
2110 * @error: Return location for error or %NULL.
2112 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2114 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2116 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2121 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2122 GDBusProxyFlags flags,
2123 GDBusInterfaceInfo *info,
2125 const gchar *object_path,
2126 const gchar *interface_name,
2127 GCancellable *cancellable,
2130 GInitable *initable;
2132 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2133 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2134 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2136 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2140 "g-interface-info", info,
2142 "g-bus-type", bus_type,
2143 "g-object-path", object_path,
2144 "g-interface-name", interface_name,
2146 if (initable != NULL)
2147 return G_DBUS_PROXY (initable);
2152 /* ---------------------------------------------------------------------------------------------------- */
2155 * g_dbus_proxy_get_connection:
2156 * @proxy: A #GDBusProxy.
2158 * Gets the connection @proxy is for.
2160 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2165 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2167 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2168 return proxy->priv->connection;
2172 * g_dbus_proxy_get_flags:
2173 * @proxy: A #GDBusProxy.
2175 * Gets the flags that @proxy was constructed with.
2177 * Returns: Flags from the #GDBusProxyFlags enumeration.
2182 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2184 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2185 return proxy->priv->flags;
2189 * g_dbus_proxy_get_name:
2190 * @proxy: A #GDBusProxy.
2192 * Gets the name that @proxy was constructed for.
2194 * Returns: A string owned by @proxy. Do not free.
2199 g_dbus_proxy_get_name (GDBusProxy *proxy)
2201 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2202 return proxy->priv->name;
2206 * g_dbus_proxy_get_name_owner:
2207 * @proxy: A #GDBusProxy.
2209 * The unique name that owns the name that @proxy is for or %NULL if
2210 * no-one currently owns that name. You may connect to the
2211 * #GObject::notify signal to track changes to the
2212 * #GDBusProxy:g-name-owner property.
2214 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2219 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2223 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2225 G_LOCK (properties_lock);
2226 ret = g_strdup (proxy->priv->name_owner);
2227 G_UNLOCK (properties_lock);
2232 * g_dbus_proxy_get_object_path:
2233 * @proxy: A #GDBusProxy.
2235 * Gets the object path @proxy is for.
2237 * Returns: A string owned by @proxy. Do not free.
2242 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2244 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2245 return proxy->priv->object_path;
2249 * g_dbus_proxy_get_interface_name:
2250 * @proxy: A #GDBusProxy.
2252 * Gets the D-Bus interface name @proxy is for.
2254 * Returns: A string owned by @proxy. Do not free.
2259 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2261 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2262 return proxy->priv->interface_name;
2266 * g_dbus_proxy_get_default_timeout:
2267 * @proxy: A #GDBusProxy.
2269 * Gets the timeout to use if -1 (specifying default timeout) is
2270 * passed as @timeout_msec in the g_dbus_proxy_call() and
2271 * g_dbus_proxy_call_sync() functions.
2273 * See the #GDBusProxy:g-default-timeout property for more details.
2275 * Returns: Timeout to use for @proxy.
2280 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2284 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2286 G_LOCK (properties_lock);
2287 ret = proxy->priv->timeout_msec;
2288 G_UNLOCK (properties_lock);
2293 * g_dbus_proxy_set_default_timeout:
2294 * @proxy: A #GDBusProxy.
2295 * @timeout_msec: Timeout in milliseconds.
2297 * Sets the timeout to use if -1 (specifying default timeout) is
2298 * passed as @timeout_msec in the g_dbus_proxy_call() and
2299 * g_dbus_proxy_call_sync() functions.
2301 * See the #GDBusProxy:g-default-timeout property for more details.
2306 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2309 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2310 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2312 G_LOCK (properties_lock);
2314 if (proxy->priv->timeout_msec != timeout_msec)
2316 proxy->priv->timeout_msec = timeout_msec;
2317 G_UNLOCK (properties_lock);
2319 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2323 G_UNLOCK (properties_lock);
2328 * g_dbus_proxy_get_interface_info:
2329 * @proxy: A #GDBusProxy
2331 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2332 * interface that @proxy conforms to.
2334 * See the #GDBusProxy:g-interface-info property for more details.
2336 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2337 * object, it is owned by @proxy.
2341 GDBusInterfaceInfo *
2342 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2344 GDBusInterfaceInfo *ret;
2346 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2348 G_LOCK (properties_lock);
2349 ret = proxy->priv->expected_interface;
2350 G_UNLOCK (properties_lock);
2351 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2352 * call g_dbus_proxy_set_interface_info() and make it invalid...
2358 * g_dbus_proxy_set_interface_info:
2359 * @proxy: A #GDBusProxy
2360 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2362 * Ensure that interactions with @proxy conform to the given
2363 * interface. For example, when completing a method call, if the type
2364 * signature of the message isn't what's expected, the given #GError
2365 * is set. Signals that have a type signature mismatch are simply
2368 * See the #GDBusProxy:g-interface-info property for more details.
2373 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2374 GDBusInterfaceInfo *info)
2376 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2377 G_LOCK (properties_lock);
2379 if (proxy->priv->expected_interface != NULL)
2381 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2382 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2384 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2385 if (proxy->priv->expected_interface != NULL)
2386 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2388 G_UNLOCK (properties_lock);
2391 /* ---------------------------------------------------------------------------------------------------- */
2394 maybe_split_method_name (const gchar *method_name,
2395 gchar **out_interface_name,
2396 const gchar **out_method_name)
2401 g_assert (out_interface_name != NULL);
2402 g_assert (out_method_name != NULL);
2403 *out_interface_name = NULL;
2404 *out_method_name = NULL;
2406 if (strchr (method_name, '.') != NULL)
2411 p = g_strdup (method_name);
2412 last_dot = strrchr (p, '.');
2415 *out_interface_name = p;
2416 *out_method_name = last_dot + 1;
2428 GUnixFDList *fd_list;
2433 reply_data_free (ReplyData *data)
2435 g_variant_unref (data->value);
2437 if (data->fd_list != NULL)
2438 g_object_unref (data->fd_list);
2440 g_slice_free (ReplyData, data);
2444 reply_cb (GDBusConnection *connection,
2448 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2452 GUnixFDList *fd_list;
2457 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2462 value = g_dbus_connection_call_finish (connection,
2468 g_simple_async_result_take_error (simple, error);
2473 data = g_slice_new0 (ReplyData);
2474 data->value = value;
2476 data->fd_list = fd_list;
2478 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2481 /* no need to complete in idle since the method GDBusConnection already does */
2482 g_simple_async_result_complete (simple);
2483 g_object_unref (simple);
2486 /* properties_lock must be held for as long as you will keep the
2489 static const GDBusMethodInfo *
2490 lookup_method_info_or_warn (GDBusProxy *proxy,
2491 const gchar *method_name)
2493 const GDBusMethodInfo *info;
2495 if (proxy->priv->expected_interface == NULL)
2498 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2501 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2502 method_name, proxy->priv->expected_interface->name);
2508 /* properties_lock must be held for as long as you will keep the
2511 static const gchar *
2512 get_destination_for_call (GDBusProxy *proxy)
2518 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2519 * is never NULL and always the same as proxy->priv->name. We use this
2520 * knowledge to avoid checking if proxy->priv->name is a unique or
2523 ret = proxy->priv->name_owner;
2527 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2530 ret = proxy->priv->name;
2536 /* ---------------------------------------------------------------------------------------------------- */
2539 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2540 const gchar *method_name,
2541 GVariant *parameters,
2542 GDBusCallFlags flags,
2544 GUnixFDList *fd_list,
2545 GCancellable *cancellable,
2546 GAsyncReadyCallback callback,
2549 GSimpleAsyncResult *simple;
2551 gchar *split_interface_name;
2552 const gchar *split_method_name;
2553 const gchar *target_method_name;
2554 const gchar *target_interface_name;
2556 GVariantType *reply_type;
2558 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2559 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2560 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2561 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2563 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2565 g_return_if_fail (fd_list == NULL);
2569 split_interface_name = NULL;
2571 simple = g_simple_async_result_new (G_OBJECT (proxy),
2574 g_dbus_proxy_call_internal);
2576 G_LOCK (properties_lock);
2578 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2579 target_method_name = was_split ? split_method_name : method_name;
2580 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2582 /* Warn if method is unexpected (cf. :g-interface-info) */
2585 const GDBusMethodInfo *expected_method_info;
2586 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2587 if (expected_method_info != NULL)
2588 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2592 if (proxy->priv->name != NULL)
2594 destination = g_strdup (get_destination_for_call (proxy));
2595 if (destination == NULL)
2597 g_simple_async_result_set_error (simple,
2600 _("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"));
2601 G_UNLOCK (properties_lock);
2606 G_UNLOCK (properties_lock);
2609 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2611 proxy->priv->object_path,
2612 target_interface_name,
2617 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2620 (GAsyncReadyCallback) reply_cb,
2623 g_dbus_connection_call (proxy->priv->connection,
2625 proxy->priv->object_path,
2626 target_interface_name,
2631 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2633 (GAsyncReadyCallback) reply_cb,
2638 if (reply_type != NULL)
2639 g_variant_type_free (reply_type);
2641 g_free (destination);
2642 g_free (split_interface_name);
2646 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2647 GUnixFDList **out_fd_list,
2651 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2655 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2656 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2657 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2659 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2663 if (g_simple_async_result_propagate_error (simple, error))
2666 data = g_simple_async_result_get_op_res_gpointer (simple);
2667 value = g_variant_ref (data->value);
2669 if (out_fd_list != NULL)
2670 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2678 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2679 const gchar *method_name,
2680 GVariant *parameters,
2681 GDBusCallFlags flags,
2683 GUnixFDList *fd_list,
2684 GUnixFDList **out_fd_list,
2685 GCancellable *cancellable,
2690 gchar *split_interface_name;
2691 const gchar *split_method_name;
2692 const gchar *target_method_name;
2693 const gchar *target_interface_name;
2695 GVariantType *reply_type;
2697 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2698 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2699 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2700 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2702 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2704 g_return_val_if_fail (fd_list == NULL, NULL);
2706 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2710 G_LOCK (properties_lock);
2712 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2713 target_method_name = was_split ? split_method_name : method_name;
2714 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2716 /* Warn if method is unexpected (cf. :g-interface-info) */
2719 const GDBusMethodInfo *expected_method_info;
2720 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2721 if (expected_method_info != NULL)
2722 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2726 if (proxy->priv->name != NULL)
2728 destination = g_strdup (get_destination_for_call (proxy));
2729 if (destination == NULL)
2731 g_set_error_literal (error,
2734 _("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"));
2736 G_UNLOCK (properties_lock);
2741 G_UNLOCK (properties_lock);
2744 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2746 proxy->priv->object_path,
2747 target_interface_name,
2752 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2758 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2760 proxy->priv->object_path,
2761 target_interface_name,
2766 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2772 if (reply_type != NULL)
2773 g_variant_type_free (reply_type);
2775 g_free (destination);
2776 g_free (split_interface_name);
2781 /* ---------------------------------------------------------------------------------------------------- */
2784 * g_dbus_proxy_call:
2785 * @proxy: A #GDBusProxy.
2786 * @method_name: Name of method to invoke.
2787 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2788 * @flags: Flags from the #GDBusCallFlags enumeration.
2789 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2790 * "infinite") or -1 to use the proxy default timeout.
2791 * @cancellable: A #GCancellable or %NULL.
2792 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2793 * care about the result of the method invocation.
2794 * @user_data: The data to pass to @callback.
2796 * Asynchronously invokes the @method_name method on @proxy.
2798 * If @method_name contains any dots, then @name is split into interface and
2799 * method name parts. This allows using @proxy for invoking methods on
2802 * If the #GDBusConnection associated with @proxy is closed then
2803 * the operation will fail with %G_IO_ERROR_CLOSED. If
2804 * @cancellable is canceled, the operation will fail with
2805 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2806 * compatible with the D-Bus protocol, the operation fails with
2807 * %G_IO_ERROR_INVALID_ARGUMENT.
2809 * If the @parameters #GVariant is floating, it is consumed. This allows
2810 * convenient 'inline' use of g_variant_new(), e.g.:
2812 * g_dbus_proxy_call (proxy,
2814 * g_variant_new ("(ss)",
2817 * G_DBUS_CALL_FLAGS_NONE,
2820 * (GAsyncReadyCallback) two_strings_done,
2824 * This is an asynchronous method. When the operation is finished,
2825 * @callback will be invoked in the
2826 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2827 * of the thread you are calling this method from.
2828 * You can then call g_dbus_proxy_call_finish() to get the result of
2829 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2830 * version of this method.
2835 g_dbus_proxy_call (GDBusProxy *proxy,
2836 const gchar *method_name,
2837 GVariant *parameters,
2838 GDBusCallFlags flags,
2840 GCancellable *cancellable,
2841 GAsyncReadyCallback callback,
2844 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2848 * g_dbus_proxy_call_finish:
2849 * @proxy: A #GDBusProxy.
2850 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2851 * @error: Return location for error or %NULL.
2853 * Finishes an operation started with g_dbus_proxy_call().
2855 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2856 * return values. Free with g_variant_unref().
2861 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2865 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2869 * g_dbus_proxy_call_sync:
2870 * @proxy: A #GDBusProxy.
2871 * @method_name: Name of method to invoke.
2872 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2873 * or %NULL if not passing parameters.
2874 * @flags: Flags from the #GDBusCallFlags enumeration.
2875 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2876 * "infinite") or -1 to use the proxy default timeout.
2877 * @cancellable: A #GCancellable or %NULL.
2878 * @error: Return location for error or %NULL.
2880 * Synchronously invokes the @method_name method on @proxy.
2882 * If @method_name contains any dots, then @name is split into interface and
2883 * method name parts. This allows using @proxy for invoking methods on
2886 * If the #GDBusConnection associated with @proxy is disconnected then
2887 * the operation will fail with %G_IO_ERROR_CLOSED. If
2888 * @cancellable is canceled, the operation will fail with
2889 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2890 * compatible with the D-Bus protocol, the operation fails with
2891 * %G_IO_ERROR_INVALID_ARGUMENT.
2893 * If the @parameters #GVariant is floating, it is consumed. This allows
2894 * convenient 'inline' use of g_variant_new(), e.g.:
2896 * g_dbus_proxy_call_sync (proxy,
2898 * g_variant_new ("(ss)",
2901 * G_DBUS_CALL_FLAGS_NONE,
2907 * The calling thread is blocked until a reply is received. See
2908 * g_dbus_proxy_call() for the asynchronous version of this
2911 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2912 * return values. Free with g_variant_unref().
2917 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2918 const gchar *method_name,
2919 GVariant *parameters,
2920 GDBusCallFlags flags,
2922 GCancellable *cancellable,
2925 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2928 /* ---------------------------------------------------------------------------------------------------- */
2933 * g_dbus_proxy_call_with_unix_fd_list:
2934 * @proxy: A #GDBusProxy.
2935 * @method_name: Name of method to invoke.
2936 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2937 * @flags: Flags from the #GDBusCallFlags enumeration.
2938 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2939 * "infinite") or -1 to use the proxy default timeout.
2940 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2941 * @cancellable: A #GCancellable or %NULL.
2942 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2943 * care about the result of the method invocation.
2944 * @user_data: The data to pass to @callback.
2946 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2948 * This method is only available on UNIX.
2953 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
2954 const gchar *method_name,
2955 GVariant *parameters,
2956 GDBusCallFlags flags,
2958 GUnixFDList *fd_list,
2959 GCancellable *cancellable,
2960 GAsyncReadyCallback callback,
2963 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2967 * g_dbus_proxy_call_with_unix_fd_list_finish:
2968 * @proxy: A #GDBusProxy.
2969 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2970 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2971 * @error: Return location for error or %NULL.
2973 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2975 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2976 * return values. Free with g_variant_unref().
2981 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
2982 GUnixFDList **out_fd_list,
2986 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2990 * g_dbus_proxy_call_with_unix_fd_list_sync:
2991 * @proxy: A #GDBusProxy.
2992 * @method_name: Name of method to invoke.
2993 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2994 * or %NULL if not passing parameters.
2995 * @flags: Flags from the #GDBusCallFlags enumeration.
2996 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2997 * "infinite") or -1 to use the proxy default timeout.
2998 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2999 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
3000 * @cancellable: A #GCancellable or %NULL.
3001 * @error: Return location for error or %NULL.
3003 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3005 * This method is only available on UNIX.
3007 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3008 * return values. Free with g_variant_unref().
3013 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3014 const gchar *method_name,
3015 GVariant *parameters,
3016 GDBusCallFlags flags,
3018 GUnixFDList *fd_list,
3019 GUnixFDList **out_fd_list,
3020 GCancellable *cancellable,
3023 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3026 #endif /* G_OS_UNIX */
3028 /* ---------------------------------------------------------------------------------------------------- */
3030 static GDBusInterfaceInfo *
3031 _g_dbus_proxy_get_info (GDBusInterface *interface)
3033 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3034 return g_dbus_proxy_get_interface_info (proxy);
3037 static GDBusObject *
3038 _g_dbus_proxy_get_object (GDBusInterface *interface)
3040 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3041 return proxy->priv->object;
3045 _g_dbus_proxy_set_object (GDBusInterface *interface,
3046 GDBusObject *object)
3048 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3049 if (proxy->priv->object != NULL)
3050 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3051 proxy->priv->object = object;
3052 if (proxy->priv->object != NULL)
3053 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3057 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3059 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3060 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3061 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3064 /* ---------------------------------------------------------------------------------------------------- */