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 <gobject/gvaluecollector.h>
30 #include "gdbusutils.h"
31 #include "gdbusproxy.h"
32 #include "gioenumtypes.h"
33 #include "gdbusconnection.h"
34 #include "gdbuserror.h"
35 #include "gdbusprivate.h"
36 #include "gio-marshal.h"
37 #include "ginitable.h"
38 #include "gasyncinitable.h"
40 #include "gasyncresult.h"
41 #include "gsimpleasyncresult.h"
48 * @short_description: Base class for proxies
51 * #GDBusProxy is a base class used for proxies to access a D-Bus
52 * interface on a remote object. A #GDBusProxy can only be constructed
53 * for unique name bus and does not track whether the name
54 * vanishes. Use g_bus_watch_proxy() to construct #GDBusProxy proxies
55 * for owners of a well-known names.
58 struct _GDBusProxyPrivate
60 GDBusConnection *connection;
61 GDBusProxyFlags flags;
62 gchar *unique_bus_name;
64 gchar *interface_name;
67 /* gchar* -> GVariant* */
68 GHashTable *properties;
70 GDBusInterfaceInfo *expected_interface;
72 guint properties_changed_subscriber_id;
73 guint signals_subscriber_id;
82 PROP_G_UNIQUE_BUS_NAME,
85 PROP_G_INTERFACE_NAME,
86 PROP_G_DEFAULT_TIMEOUT,
92 PROPERTIES_CHANGED_SIGNAL,
97 static void g_dbus_proxy_constructed (GObject *object);
99 guint signals[LAST_SIGNAL] = {0};
101 static void initable_iface_init (GInitableIface *initable_iface);
102 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
104 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
105 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
106 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
110 g_dbus_proxy_finalize (GObject *object)
112 GDBusProxy *proxy = G_DBUS_PROXY (object);
114 if (proxy->priv->properties_changed_subscriber_id > 0)
115 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
116 proxy->priv->properties_changed_subscriber_id);
118 if (proxy->priv->signals_subscriber_id > 0)
119 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
120 proxy->priv->signals_subscriber_id);
122 g_object_unref (proxy->priv->connection);
123 g_free (proxy->priv->unique_bus_name);
124 g_free (proxy->priv->object_path);
125 g_free (proxy->priv->interface_name);
126 if (proxy->priv->properties != NULL)
127 g_hash_table_unref (proxy->priv->properties);
129 if (proxy->priv->expected_interface != NULL)
130 g_dbus_interface_info_unref (proxy->priv->expected_interface);
132 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
136 g_dbus_proxy_get_property (GObject *object,
141 GDBusProxy *proxy = G_DBUS_PROXY (object);
145 case PROP_G_CONNECTION:
146 g_value_set_object (value, proxy->priv->connection);
150 g_value_set_flags (value, proxy->priv->flags);
153 case PROP_G_UNIQUE_BUS_NAME:
154 g_value_set_string (value, proxy->priv->unique_bus_name);
157 case PROP_G_OBJECT_PATH:
158 g_value_set_string (value, proxy->priv->object_path);
161 case PROP_G_INTERFACE_NAME:
162 g_value_set_string (value, proxy->priv->interface_name);
165 case PROP_G_DEFAULT_TIMEOUT:
166 g_value_set_int (value, proxy->priv->timeout_msec);
169 case PROP_G_INTERFACE_INFO:
170 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 g_dbus_proxy_set_property (GObject *object,
185 GDBusProxy *proxy = G_DBUS_PROXY (object);
189 case PROP_G_CONNECTION:
190 proxy->priv->connection = g_value_dup_object (value);
194 proxy->priv->flags = g_value_get_flags (value);
197 case PROP_G_UNIQUE_BUS_NAME:
198 proxy->priv->unique_bus_name = g_value_dup_string (value);
201 case PROP_G_OBJECT_PATH:
202 proxy->priv->object_path = g_value_dup_string (value);
205 case PROP_G_INTERFACE_NAME:
206 proxy->priv->interface_name = g_value_dup_string (value);
209 case PROP_G_DEFAULT_TIMEOUT:
210 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
213 case PROP_G_INTERFACE_INFO:
214 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 g_dbus_proxy_class_init (GDBusProxyClass *klass)
226 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->finalize = g_dbus_proxy_finalize;
229 gobject_class->set_property = g_dbus_proxy_set_property;
230 gobject_class->get_property = g_dbus_proxy_get_property;
231 gobject_class->constructed = g_dbus_proxy_constructed;
233 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
234 * in derived classes */
237 * GDBusProxy:g-interface-info:
239 * Ensure that interactions with this proxy conform to the given
240 * interface. For example, when completing a method call, if the
241 * type signature of the message isn't what's expected, the given
242 * #GError is set. Signals that have a type signature mismatch are
247 g_object_class_install_property (gobject_class,
248 PROP_G_INTERFACE_INFO,
249 g_param_spec_boxed ("g-interface-info",
250 P_("Interface Information"),
251 P_("Interface Information"),
252 G_TYPE_DBUS_INTERFACE_INFO,
255 G_PARAM_STATIC_NAME |
256 G_PARAM_STATIC_BLURB |
257 G_PARAM_STATIC_NICK));
260 * GDBusProxy:g-connection:
262 * The #GDBusConnection the proxy is for.
266 g_object_class_install_property (gobject_class,
268 g_param_spec_object ("g-connection",
270 P_("The connection the proxy is for"),
271 G_TYPE_DBUS_CONNECTION,
274 G_PARAM_CONSTRUCT_ONLY |
275 G_PARAM_STATIC_NAME |
276 G_PARAM_STATIC_BLURB |
277 G_PARAM_STATIC_NICK));
280 * GDBusProxy:g-flags:
282 * Flags from the #GDBusProxyFlags enumeration.
286 g_object_class_install_property (gobject_class,
288 g_param_spec_flags ("g-flags",
290 P_("Flags for the proxy"),
291 G_TYPE_DBUS_PROXY_FLAGS,
292 G_DBUS_PROXY_FLAGS_NONE,
295 G_PARAM_CONSTRUCT_ONLY |
296 G_PARAM_STATIC_NAME |
297 G_PARAM_STATIC_BLURB |
298 G_PARAM_STATIC_NICK));
301 * GDBusProxy:g-unique-bus-name:
303 * The unique bus name the proxy is for.
307 g_object_class_install_property (gobject_class,
308 PROP_G_UNIQUE_BUS_NAME,
309 g_param_spec_string ("g-unique-bus-name",
310 P_("g-unique-bus-name"),
311 P_("The unique bus name the proxy is for"),
315 G_PARAM_CONSTRUCT_ONLY |
316 G_PARAM_STATIC_NAME |
317 G_PARAM_STATIC_BLURB |
318 G_PARAM_STATIC_NICK));
321 * GDBusProxy:g-object-path:
323 * The object path the proxy is for.
327 g_object_class_install_property (gobject_class,
329 g_param_spec_string ("g-object-path",
331 P_("The object path the proxy is for"),
335 G_PARAM_CONSTRUCT_ONLY |
336 G_PARAM_STATIC_NAME |
337 G_PARAM_STATIC_BLURB |
338 G_PARAM_STATIC_NICK));
341 * GDBusProxy:g-interface-name:
343 * The D-Bus interface name the proxy is for.
347 g_object_class_install_property (gobject_class,
348 PROP_G_INTERFACE_NAME,
349 g_param_spec_string ("g-interface-name",
350 P_("g-interface-name"),
351 P_("The D-Bus interface name the proxy is for"),
355 G_PARAM_CONSTRUCT_ONLY |
356 G_PARAM_STATIC_NAME |
357 G_PARAM_STATIC_BLURB |
358 G_PARAM_STATIC_NICK));
361 * GDBusProxy:g-default-timeout:
363 * The timeout to use if -1 (specifying default timeout) is passed
364 * as @timeout_msec in the g_dbus_proxy_call() and
365 * g_dbus_proxy_call_sync() functions.
367 * This allows applications to set a proxy-wide timeout for all
368 * remote method invocations on the proxy. If this property is -1,
369 * the default timeout (typically 25 seconds) is used. If set to
370 * %G_MAXINT, then no timeout is used.
374 g_object_class_install_property (gobject_class,
375 PROP_G_DEFAULT_TIMEOUT,
376 g_param_spec_int ("g-default-timeout",
377 P_("Default Timeout"),
378 P_("Timeout for remote method invocation"),
385 G_PARAM_STATIC_NAME |
386 G_PARAM_STATIC_BLURB |
387 G_PARAM_STATIC_NICK));
390 * GDBusProxy::g-properties-changed:
391 * @proxy: The #GDBusProxy emitting the signal.
392 * @changed_properties: A #GVariant containing the properties that changed
393 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
395 * Emitted when one or more D-Bus properties on @proxy changes. The
396 * local cache has already been updated when this signal fires. Note
397 * that both @changed_properties and @invalidated_properties are
398 * guaranteed to never be %NULL (either may be empty though).
400 * This signal corresponds to the
401 * <literal>PropertiesChanged</literal> D-Bus signal on the
402 * <literal>org.freedesktop.DBus.Properties</literal> interface.
406 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
409 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
412 _gio_marshal_VOID__BOXED_BOXED,
416 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
419 * GDBusProxy::g-signal:
420 * @proxy: The #GDBusProxy emitting the signal.
421 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
422 * @signal_name: The name of the signal.
423 * @parameters: A #GVariant tuple with parameters for the signal.
425 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
429 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
432 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
435 _gio_marshal_VOID__STRING_STRING_BOXED,
443 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
447 g_dbus_proxy_init (GDBusProxy *proxy)
449 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
450 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
453 (GDestroyNotify) g_variant_unref);
456 /* ---------------------------------------------------------------------------------------------------- */
459 * g_dbus_proxy_get_cached_property_names:
460 * @proxy: A #GDBusProxy.
462 * Gets the names of all cached properties on @proxy.
464 * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
465 * no cached properties. Free the returned array with g_strfreev().
470 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
477 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
480 if (g_hash_table_size (proxy->priv->properties) == 0)
483 p = g_ptr_array_new ();
485 g_hash_table_iter_init (&iter, proxy->priv->properties);
486 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
487 g_ptr_array_add (p, g_strdup (key));
488 g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
489 g_ptr_array_add (p, NULL);
491 names = (gchar **) g_ptr_array_free (p, FALSE);
497 static const GDBusPropertyInfo *
498 lookup_property_info_or_warn (GDBusProxy *proxy,
499 const gchar *property_name)
501 const GDBusPropertyInfo *info;
503 if (proxy->priv->expected_interface == NULL)
506 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
509 g_warning ("Trying to lookup property %s which isn't in expected interface %s",
511 proxy->priv->expected_interface->name);
518 * g_dbus_proxy_get_cached_property:
519 * @proxy: A #GDBusProxy.
520 * @property_name: Property name.
522 * Looks up the value for a property from the cache. This call does no
525 * If @proxy has an expected interface (see
526 * #GDBusProxy:g-interface-info), then @property_name (for existence)
527 * is checked against it.
529 * Returns: A reference to the #GVariant instance that holds the value
530 * for @property_name or %NULL if the value is not in the cache. The
531 * returned reference must be freed with g_variant_unref().
536 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
537 const gchar *property_name)
541 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
542 g_return_val_if_fail (property_name != NULL, NULL);
544 value = g_hash_table_lookup (proxy->priv->properties, property_name);
547 const GDBusPropertyInfo *info;
548 info = lookup_property_info_or_warn (proxy, property_name);
553 g_variant_ref (value);
560 * g_dbus_proxy_set_cached_property:
561 * @proxy: A #GDBusProxy
562 * @property_name: Property name.
563 * @value: Value for the property or %NULL to remove it from the cache.
565 * If @value is not %NULL, sets the cached value for the property with
566 * name @property_name to the value in @value.
568 * If @value is %NULL, then the cached value is removed from the
571 * If @proxy has an expected interface (see
572 * #GDBusProxy:g-interface-info), then @property_name (for existence)
573 * and @value (for the type) is checked against it.
575 * If the @value #GVariant is floating, it is consumed. This allows
576 * convenient 'inline' use of g_variant_new(), e.g.
578 * g_dbus_proxy_set_cached_property (proxy,
580 * g_variant_new ("(si)",
585 * Normally you will not need to use this method since @proxy is
586 * tracking changes using the
587 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
588 * D-Bus signal. However, for performance reasons an object may decide
589 * to not use this signal for some properties and instead use a
590 * proprietary out-of-band mechanism to transmit changes.
592 * As a concrete example, consider an object with a property
593 * <literal>ChatroomParticipants</literal> which is an array of
594 * strings. Instead of transmitting the same (long) array every time
595 * the property changes, it is more efficient to only transmit the
596 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
597 * name)</literal> and <literal>ChatroomParticipantParted(String
603 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
604 const gchar *property_name,
607 const GDBusPropertyInfo *info;
609 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
610 g_return_if_fail (property_name != NULL);
614 info = lookup_property_info_or_warn (proxy, property_name);
617 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
619 g_warning (_("Trying to set property %s of type %s but according to the expected "
620 "interface the type is %s"),
622 g_variant_get_type_string (value),
627 g_hash_table_insert (proxy->priv->properties,
628 g_strdup (property_name),
629 g_variant_ref_sink (value));
633 g_hash_table_remove (proxy->priv->properties, property_name);
640 /* ---------------------------------------------------------------------------------------------------- */
643 on_signal_received (GDBusConnection *connection,
644 const gchar *sender_name,
645 const gchar *object_path,
646 const gchar *interface_name,
647 const gchar *signal_name,
648 GVariant *parameters,
651 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
653 if (!proxy->priv->initialized)
656 g_signal_emit (proxy,
657 signals[SIGNAL_SIGNAL],
666 /* ---------------------------------------------------------------------------------------------------- */
669 on_properties_changed (GDBusConnection *connection,
670 const gchar *sender_name,
671 const gchar *object_path,
672 const gchar *interface_name,
673 const gchar *signal_name,
674 GVariant *parameters,
677 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
679 const gchar *interface_name_for_signal;
680 GVariant *changed_properties;
681 gchar **invalidated_properties;
687 changed_properties = NULL;
688 invalidated_properties = NULL;
690 if (!proxy->priv->initialized)
693 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
695 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
696 g_variant_get_type_string (parameters));
700 g_variant_get (parameters,
702 &interface_name_for_signal,
704 &invalidated_properties);
706 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
709 g_variant_iter_init (&iter, changed_properties);
710 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
712 g_hash_table_insert (proxy->priv->properties,
713 key, /* adopts string */
714 value); /* adopts value */
718 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
721 invalidated_properties);
724 if (changed_properties != NULL)
725 g_variant_unref (changed_properties);
726 g_free (invalidated_properties);
729 /* ---------------------------------------------------------------------------------------------------- */
732 g_dbus_proxy_constructed (GObject *object)
734 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed != NULL)
735 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed (object);
738 /* ---------------------------------------------------------------------------------------------------- */
741 subscribe_to_signals (GDBusProxy *proxy)
743 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
745 /* subscribe to PropertiesChanged() */
746 proxy->priv->properties_changed_subscriber_id =
747 g_dbus_connection_signal_subscribe (proxy->priv->connection,
748 proxy->priv->unique_bus_name,
749 "org.freedesktop.DBus.Properties",
751 proxy->priv->object_path,
752 proxy->priv->interface_name,
753 on_properties_changed,
758 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
760 /* subscribe to all signals for the object */
761 proxy->priv->signals_subscriber_id =
762 g_dbus_connection_signal_subscribe (proxy->priv->connection,
763 proxy->priv->unique_bus_name,
764 proxy->priv->interface_name,
766 proxy->priv->object_path,
774 /* ---------------------------------------------------------------------------------------------------- */
777 process_get_all_reply (GDBusProxy *proxy,
783 if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
785 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
786 g_variant_get_type_string (result));
790 g_variant_iter_init (&iter, g_variant_get_child_value (result, 0));
791 while ((item = g_variant_iter_next_value (&iter)) != NULL)
800 //g_print ("got %s -> %s\n", key, g_variant_markup_print (value, FALSE, 0, 0));
802 g_hash_table_insert (proxy->priv->properties,
804 value); /* steals value */
811 initable_init (GInitable *initable,
812 GCancellable *cancellable,
815 GDBusProxy *proxy = G_DBUS_PROXY (initable);
821 subscribe_to_signals (proxy);
823 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
825 /* load all properties synchronously */
826 result = g_dbus_connection_call_sync (proxy->priv->connection,
827 proxy->priv->unique_bus_name,
828 proxy->priv->object_path,
829 "org.freedesktop.DBus.Properties",
831 g_variant_new ("(s)", proxy->priv->interface_name),
832 G_DBUS_CALL_FLAGS_NONE,
839 process_get_all_reply (proxy, result);
841 g_variant_unref (result);
847 proxy->priv->initialized = TRUE;
852 initable_iface_init (GInitableIface *initable_iface)
854 initable_iface->init = initable_init;
857 /* ---------------------------------------------------------------------------------------------------- */
860 get_all_cb (GDBusConnection *connection,
864 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
869 result = g_dbus_connection_call_finish (connection,
874 g_simple_async_result_set_from_error (simple, error);
875 g_error_free (error);
879 g_simple_async_result_set_op_res_gpointer (simple,
881 (GDestroyNotify) g_variant_unref);
884 g_simple_async_result_complete_in_idle (simple);
885 g_object_unref (simple);
889 async_initable_init_async (GAsyncInitable *initable,
891 GCancellable *cancellable,
892 GAsyncReadyCallback callback,
895 GDBusProxy *proxy = G_DBUS_PROXY (initable);
896 GSimpleAsyncResult *simple;
898 simple = g_simple_async_result_new (G_OBJECT (proxy),
903 subscribe_to_signals (proxy);
905 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
907 /* load all properties asynchronously */
908 g_dbus_connection_call (proxy->priv->connection,
909 proxy->priv->unique_bus_name,
910 proxy->priv->object_path,
911 "org.freedesktop.DBus.Properties",
913 g_variant_new ("(s)", proxy->priv->interface_name),
914 G_DBUS_CALL_FLAGS_NONE,
917 (GAsyncReadyCallback) get_all_cb,
922 g_simple_async_result_complete_in_idle (simple);
923 g_object_unref (simple);
928 async_initable_init_finish (GAsyncInitable *initable,
932 GDBusProxy *proxy = G_DBUS_PROXY (initable);
933 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
939 result = g_simple_async_result_get_op_res_gpointer (simple);
942 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
944 g_simple_async_result_propagate_error (simple, error);
950 process_get_all_reply (proxy, result);
956 proxy->priv->initialized = TRUE;
961 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
963 async_initable_iface->init_async = async_initable_init_async;
964 async_initable_iface->init_finish = async_initable_init_finish;
967 /* ---------------------------------------------------------------------------------------------------- */
971 * @connection: A #GDBusConnection.
972 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
973 * @flags: Flags used when constructing the proxy.
974 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
975 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
976 * @object_path: An object path.
977 * @interface_name: A D-Bus interface name.
978 * @cancellable: A #GCancellable or %NULL.
979 * @callback: Callback function to invoke when the proxy is ready.
980 * @user_data: User data to pass to @callback.
982 * Creates a proxy for accessing @interface_name on the remote object at @object_path
983 * owned by @unique_bus_name at @connection and asynchronously loads D-Bus properties unless the
984 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to the
985 * #GDBusProxy::g-properties-changed signal to get notified about property changes.
987 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
988 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
989 * to handle signals from the remote object.
991 * This is a failable asynchronous constructor - when the proxy is
992 * ready, @callback will be invoked and you can use
993 * g_dbus_proxy_new_finish() to get the result.
995 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1000 g_dbus_proxy_new (GDBusConnection *connection,
1002 GDBusProxyFlags flags,
1003 GDBusInterfaceInfo *info,
1004 const gchar *unique_bus_name,
1005 const gchar *object_path,
1006 const gchar *interface_name,
1007 GCancellable *cancellable,
1008 GAsyncReadyCallback callback,
1011 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1012 g_return_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY));
1013 g_return_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1014 g_dbus_is_unique_name (unique_bus_name));
1015 g_return_if_fail (g_variant_is_object_path (object_path));
1016 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1018 g_async_initable_new_async (object_type,
1024 "g-interface-info", info,
1025 "g-unique-bus-name", unique_bus_name,
1026 "g-connection", connection,
1027 "g-object-path", object_path,
1028 "g-interface-name", interface_name,
1033 * g_dbus_proxy_new_finish:
1034 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1035 * @error: Return location for error or %NULL.
1037 * Finishes creating a #GDBusProxy.
1039 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1044 g_dbus_proxy_new_finish (GAsyncResult *res,
1048 GObject *source_object;
1050 source_object = g_async_result_get_source_object (res);
1051 g_assert (source_object != NULL);
1053 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1056 g_object_unref (source_object);
1059 return G_DBUS_PROXY (object);
1065 /* ---------------------------------------------------------------------------------------------------- */
1068 * g_dbus_proxy_new_sync:
1069 * @connection: A #GDBusConnection.
1070 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
1071 * @flags: Flags used when constructing the proxy.
1072 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1073 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
1074 * @object_path: An object path.
1075 * @interface_name: A D-Bus interface name.
1076 * @cancellable: A #GCancellable or %NULL.
1077 * @error: Return location for error or %NULL.
1079 * Creates a proxy for accessing @interface_name on the remote object at @object_path
1080 * owned by @unique_bus_name at @connection and synchronously loads D-Bus properties unless the
1081 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1083 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1084 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1085 * to handle signals from the remote object.
1087 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1088 * and g_dbus_proxy_new_finish() for the asynchronous version.
1090 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1095 g_dbus_proxy_new_sync (GDBusConnection *connection,
1097 GDBusProxyFlags flags,
1098 GDBusInterfaceInfo *info,
1099 const gchar *unique_bus_name,
1100 const gchar *object_path,
1101 const gchar *interface_name,
1102 GCancellable *cancellable,
1105 GInitable *initable;
1107 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1108 g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY), NULL);
1109 g_return_val_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1110 g_dbus_is_unique_name (unique_bus_name), NULL);
1111 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1112 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1114 initable = g_initable_new (object_type,
1118 "g-interface-info", info,
1119 "g-unique-bus-name", unique_bus_name,
1120 "g-connection", connection,
1121 "g-object-path", object_path,
1122 "g-interface-name", interface_name,
1124 if (initable != NULL)
1125 return G_DBUS_PROXY (initable);
1130 /* ---------------------------------------------------------------------------------------------------- */
1133 * g_dbus_proxy_get_connection:
1134 * @proxy: A #GDBusProxy.
1136 * Gets the connection @proxy is for.
1138 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1143 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1145 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1146 return proxy->priv->connection;
1150 * g_dbus_proxy_get_flags:
1151 * @proxy: A #GDBusProxy.
1153 * Gets the flags that @proxy was constructed with.
1155 * Returns: Flags from the #GDBusProxyFlags enumeration.
1160 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1162 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1163 return proxy->priv->flags;
1167 * g_dbus_proxy_get_unique_bus_name:
1168 * @proxy: A #GDBusProxy.
1170 * Gets the unique bus name @proxy is for.
1172 * Returns: A string owned by @proxy. Do not free.
1177 g_dbus_proxy_get_unique_bus_name (GDBusProxy *proxy)
1179 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1180 return proxy->priv->unique_bus_name;
1184 * g_dbus_proxy_get_object_path:
1185 * @proxy: A #GDBusProxy.
1187 * Gets the object path @proxy is for.
1189 * Returns: A string owned by @proxy. Do not free.
1194 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1196 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1197 return proxy->priv->object_path;
1201 * g_dbus_proxy_get_interface_name:
1202 * @proxy: A #GDBusProxy.
1204 * Gets the D-Bus interface name @proxy is for.
1206 * Returns: A string owned by @proxy. Do not free.
1211 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1213 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1214 return proxy->priv->interface_name;
1218 * g_dbus_proxy_get_default_timeout:
1219 * @proxy: A #GDBusProxy.
1221 * Gets the timeout to use if -1 (specifying default timeout) is
1222 * passed as @timeout_msec in the g_dbus_proxy_call() and
1223 * g_dbus_proxy_call_sync() functions.
1225 * See the #GDBusProxy:g-default-timeout property for more details.
1227 * Returns: Timeout to use for @proxy.
1232 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1234 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1235 return proxy->priv->timeout_msec;
1239 * g_dbus_proxy_set_default_timeout:
1240 * @proxy: A #GDBusProxy.
1241 * @timeout_msec: Timeout in milliseconds.
1243 * Sets the timeout to use if -1 (specifying default timeout) is
1244 * passed as @timeout_msec in the g_dbus_proxy_call() and
1245 * g_dbus_proxy_call_sync() functions.
1247 * See the #GDBusProxy:g-default-timeout property for more details.
1252 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1255 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1256 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1258 /* TODO: locking? */
1259 if (proxy->priv->timeout_msec != timeout_msec)
1261 proxy->priv->timeout_msec = timeout_msec;
1262 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
1267 * g_dbus_proxy_get_interface_info:
1268 * @proxy: A #GDBusProxy
1270 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
1271 * interface that @proxy conforms to.
1273 * See the #GDBusProxy:g-interface-info property for more details.
1275 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
1276 * object, it is owned by @proxy.
1280 GDBusInterfaceInfo *
1281 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
1283 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1284 return proxy->priv->expected_interface;
1288 * g_dbus_proxy_set_interface_info:
1289 * @proxy: A #GDBusProxy
1290 * @info: Minimum interface this proxy conforms to or %NULL to unset.
1292 * Ensure that interactions with @proxy conform to the given
1293 * interface. For example, when completing a method call, if the type
1294 * signature of the message isn't what's expected, the given #GError
1295 * is set. Signals that have a type signature mismatch are simply
1298 * See the #GDBusProxy:g-interface-info property for more details.
1303 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
1304 GDBusInterfaceInfo *info)
1306 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1307 if (proxy->priv->expected_interface != NULL)
1308 g_dbus_interface_info_unref (proxy->priv->expected_interface);
1309 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
1312 /* ---------------------------------------------------------------------------------------------------- */
1315 maybe_split_method_name (const gchar *method_name,
1316 gchar **out_interface_name,
1317 const gchar **out_method_name)
1322 g_assert (out_interface_name != NULL);
1323 g_assert (out_method_name != NULL);
1324 *out_interface_name = NULL;
1325 *out_method_name = NULL;
1327 if (strchr (method_name, '.') != NULL)
1332 p = g_strdup (method_name);
1333 last_dot = strrchr (p, '.');
1336 *out_interface_name = p;
1337 *out_method_name = last_dot + 1;
1347 reply_cb (GDBusConnection *connection,
1351 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1356 value = g_dbus_connection_call_finish (connection,
1361 g_simple_async_result_set_from_error (simple,
1363 g_error_free (error);
1367 g_simple_async_result_set_op_res_gpointer (simple,
1369 (GDestroyNotify) g_variant_unref);
1372 /* no need to complete in idle since the method GDBusConnection already does */
1373 g_simple_async_result_complete (simple);
1376 static const GDBusMethodInfo *
1377 lookup_method_info_or_warn (GDBusProxy *proxy,
1378 const gchar *method_name)
1380 const GDBusMethodInfo *info;
1382 if (proxy->priv->expected_interface == NULL)
1385 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1388 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1389 method_name, proxy->priv->expected_interface->name);
1396 validate_method_return (const char *method_name,
1398 const GDBusMethodInfo *expected_method_info,
1401 const gchar *type_string;
1408 if (value == NULL || expected_method_info == NULL)
1411 /* Shouldn't happen... */
1412 if (g_variant_classify (value) != G_VARIANT_CLASS_TUPLE)
1415 type_string = g_variant_get_type_string (value);
1416 signature = _g_dbus_compute_complete_signature (expected_method_info->out_args, TRUE);
1417 if (g_strcmp0 (type_string, signature) != 0)
1421 G_IO_ERROR_INVALID_ARGUMENT,
1422 _("Method `%s' returned signature `%s', but expected `%s'"),
1435 * g_dbus_proxy_call:
1436 * @proxy: A #GDBusProxy.
1437 * @method_name: Name of method to invoke.
1438 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1439 * @flags: Flags from the #GDBusCallFlags enumeration.
1440 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1441 * @cancellable: A #GCancellable or %NULL.
1442 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1443 * care about the result of the method invocation.
1444 * @user_data: The data to pass to @callback.
1446 * Asynchronously invokes the @method_name method on @proxy.
1448 * If @method_name contains any dots, then @name is split into interface and
1449 * method name parts. This allows using @proxy for invoking methods on
1452 * If the #GDBusConnection associated with @proxy is closed then
1453 * the operation will fail with %G_IO_ERROR_CLOSED. If
1454 * @cancellable is canceled, the operation will fail with
1455 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1456 * compatible with the D-Bus protocol, the operation fails with
1457 * %G_IO_ERROR_INVALID_ARGUMENT.
1459 * If the @parameters #GVariant is floating, it is consumed. This allows
1460 * convenient 'inline' use of g_variant_new(), e.g.:
1462 * g_dbus_proxy_call (proxy,
1464 * g_variant_new ("(ss)",
1467 * G_DBUS_CALL_FLAGS_NONE,
1470 * (GAsyncReadyCallback) two_strings_done,
1474 * This is an asynchronous method. When the operation is finished,
1475 * @callback will be invoked in the
1476 * <link linkend="g-main-context-push-thread-default">thread-default
1477 * main loop</link> of the thread you are calling this method from.
1478 * You can then call g_dbus_proxy_call_finish() to get the result of
1479 * the operation. See g_dbus_proxy_call_sync() for the synchronous
1480 * version of this method.
1485 g_dbus_proxy_call (GDBusProxy *proxy,
1486 const gchar *method_name,
1487 GVariant *parameters,
1488 GDBusCallFlags flags,
1490 GCancellable *cancellable,
1491 GAsyncReadyCallback callback,
1494 GSimpleAsyncResult *simple;
1496 gchar *split_interface_name;
1497 const gchar *split_method_name;
1498 const GDBusMethodInfo *expected_method_info;
1499 const gchar *target_method_name;
1500 const gchar *target_interface_name;
1502 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1503 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
1504 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
1505 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1507 simple = g_simple_async_result_new (G_OBJECT (proxy),
1512 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1513 target_method_name = was_split ? split_method_name : method_name;
1514 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1516 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
1518 /* Just warn here */
1519 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
1521 g_dbus_connection_call (proxy->priv->connection,
1522 proxy->priv->unique_bus_name,
1523 proxy->priv->object_path,
1524 target_interface_name,
1528 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1530 (GAsyncReadyCallback) reply_cb,
1533 g_free (split_interface_name);
1537 * g_dbus_proxy_call_finish:
1538 * @proxy: A #GDBusProxy.
1539 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
1540 * @error: Return location for error or %NULL.
1542 * Finishes an operation started with g_dbus_proxy_call().
1544 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1545 * return values. Free with g_variant_unref().
1550 g_dbus_proxy_call_finish (GDBusProxy *proxy,
1554 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1556 const char *method_name;
1557 const GDBusMethodInfo *expected_method_info;
1559 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1560 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1561 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1563 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
1567 if (g_simple_async_result_propagate_error (simple, error))
1570 value = g_simple_async_result_get_op_res_gpointer (simple);
1571 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
1573 /* We may not have a method name for internally-generated proxy calls like GetAll */
1574 if (value && method_name && proxy->priv->expected_interface)
1576 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1577 if (!validate_method_return (method_name, value, expected_method_info, error))
1579 g_variant_unref (value);
1589 * g_dbus_proxy_call_sync:
1590 * @proxy: A #GDBusProxy.
1591 * @method_name: Name of method to invoke.
1592 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1593 * @flags: Flags from the #GDBusCallFlags enumeration.
1594 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1595 * @cancellable: A #GCancellable or %NULL.
1596 * @error: Return location for error or %NULL.
1598 * Synchronously invokes the @method_name method on @proxy.
1600 * If @method_name contains any dots, then @name is split into interface and
1601 * method name parts. This allows using @proxy for invoking methods on
1604 * If the #GDBusConnection associated with @proxy is disconnected then
1605 * the operation will fail with %G_IO_ERROR_CLOSED. If
1606 * @cancellable is canceled, the operation will fail with
1607 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1608 * compatible with the D-Bus protocol, the operation fails with
1609 * %G_IO_ERROR_INVALID_ARGUMENT.
1611 * If the @parameters #GVariant is floating, it is consumed. This allows
1612 * convenient 'inline' use of g_variant_new(), e.g.:
1614 * g_dbus_proxy_call_sync (proxy,
1616 * g_variant_new ("(ss)",
1619 * G_DBUS_CALL_FLAGS_NONE,
1625 * The calling thread is blocked until a reply is received. See
1626 * g_dbus_proxy_call() for the asynchronous version of this
1629 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1630 * return values. Free with g_variant_unref().
1635 g_dbus_proxy_call_sync (GDBusProxy *proxy,
1636 const gchar *method_name,
1637 GVariant *parameters,
1638 GDBusCallFlags flags,
1640 GCancellable *cancellable,
1645 gchar *split_interface_name;
1646 const gchar *split_method_name;
1647 const GDBusMethodInfo *expected_method_info;
1648 const gchar *target_method_name;
1649 const gchar *target_interface_name;
1651 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1652 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
1653 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
1654 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
1655 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1657 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1658 target_method_name = was_split ? split_method_name : method_name;
1659 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1661 if (proxy->priv->expected_interface)
1663 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
1664 if (expected_method_info == NULL)
1666 g_warning ("Trying to invoke method `%s' which isn't in expected interface `%s'",
1668 target_interface_name);
1673 expected_method_info = NULL;
1676 ret = g_dbus_connection_call_sync (proxy->priv->connection,
1677 proxy->priv->unique_bus_name,
1678 proxy->priv->object_path,
1679 target_interface_name,
1683 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1686 if (!validate_method_return (target_method_name, ret, expected_method_info, error))
1688 g_variant_unref (ret);
1692 g_free (split_interface_name);
1697 /* ---------------------------------------------------------------------------------------------------- */
1699 #define __G_DBUS_PROXY_C__
1700 #include "gioaliasdef.c"