1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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"
47 * @short_description: Base class for proxies
50 * #GDBusProxy is a base class used for proxies to access a D-Bus
51 * interface on a remote object. A #GDBusProxy can only be constructed
52 * for unique name bus and does not track whether the name
53 * vanishes. Use g_bus_watch_proxy() to construct #GDBusProxy proxies
54 * for owners of a well-known names.
57 struct _GDBusProxyPrivate
59 GDBusConnection *connection;
60 GDBusProxyFlags flags;
61 gchar *unique_bus_name;
63 gchar *interface_name;
66 /* gchar* -> GVariant* */
67 GHashTable *properties;
69 GDBusInterfaceInfo *expected_interface;
71 guint properties_changed_subscriber_id;
72 guint signals_subscriber_id;
79 PROP_G_UNIQUE_BUS_NAME,
82 PROP_G_INTERFACE_NAME,
83 PROP_G_DEFAULT_TIMEOUT,
89 PROPERTIES_CHANGED_SIGNAL,
94 static void g_dbus_proxy_constructed (GObject *object);
96 guint signals[LAST_SIGNAL] = {0};
98 static void initable_iface_init (GInitableIface *initable_iface);
99 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
101 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
102 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
103 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
107 g_dbus_proxy_finalize (GObject *object)
109 GDBusProxy *proxy = G_DBUS_PROXY (object);
111 if (proxy->priv->properties_changed_subscriber_id > 0)
113 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
114 proxy->priv->properties_changed_subscriber_id);
117 if (proxy->priv->signals_subscriber_id > 0)
119 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
120 proxy->priv->signals_subscriber_id);
123 g_object_unref (proxy->priv->connection);
124 g_free (proxy->priv->unique_bus_name);
125 g_free (proxy->priv->object_path);
126 g_free (proxy->priv->interface_name);
127 if (proxy->priv->properties != NULL)
128 g_hash_table_unref (proxy->priv->properties);
130 if (proxy->priv->expected_interface != NULL)
131 g_dbus_interface_info_unref (proxy->priv->expected_interface);
133 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize != NULL)
134 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
138 g_dbus_proxy_get_property (GObject *object,
143 GDBusProxy *proxy = G_DBUS_PROXY (object);
147 case PROP_G_CONNECTION:
148 g_value_set_object (value, proxy->priv->connection);
152 g_value_set_flags (value, proxy->priv->flags);
155 case PROP_G_UNIQUE_BUS_NAME:
156 g_value_set_string (value, proxy->priv->unique_bus_name);
159 case PROP_G_OBJECT_PATH:
160 g_value_set_string (value, proxy->priv->object_path);
163 case PROP_G_INTERFACE_NAME:
164 g_value_set_string (value, proxy->priv->interface_name);
167 case PROP_G_DEFAULT_TIMEOUT:
168 g_value_set_int (value, proxy->priv->timeout_msec);
171 case PROP_G_INTERFACE_INFO:
172 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 g_dbus_proxy_set_property (GObject *object,
187 GDBusProxy *proxy = G_DBUS_PROXY (object);
191 case PROP_G_CONNECTION:
192 proxy->priv->connection = g_value_dup_object (value);
196 proxy->priv->flags = g_value_get_flags (value);
199 case PROP_G_UNIQUE_BUS_NAME:
200 proxy->priv->unique_bus_name = g_value_dup_string (value);
203 case PROP_G_OBJECT_PATH:
204 proxy->priv->object_path = g_value_dup_string (value);
207 case PROP_G_INTERFACE_NAME:
208 proxy->priv->interface_name = g_value_dup_string (value);
211 case PROP_G_DEFAULT_TIMEOUT:
212 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
215 case PROP_G_INTERFACE_INFO:
216 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 g_dbus_proxy_class_init (GDBusProxyClass *klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
230 gobject_class->finalize = g_dbus_proxy_finalize;
231 gobject_class->set_property = g_dbus_proxy_set_property;
232 gobject_class->get_property = g_dbus_proxy_get_property;
233 gobject_class->constructed = g_dbus_proxy_constructed;
235 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
236 * in derived classes */
239 * GDBusProxy:g-interface-info:
241 * Ensure that interactions with this proxy conform to the given
242 * interface. For example, when completing a method call, if the
243 * type signature of the message isn't what's expected, the given
244 * #GError is set. Signals that have a type signature mismatch are
249 g_object_class_install_property (gobject_class,
250 PROP_G_INTERFACE_INFO,
251 g_param_spec_boxed ("g-interface-info",
252 _("Interface Information"),
253 _("Interface Information"),
254 G_TYPE_DBUS_INTERFACE_INFO,
257 G_PARAM_STATIC_NAME |
258 G_PARAM_STATIC_BLURB |
259 G_PARAM_STATIC_NICK));
262 * GDBusProxy:g-connection:
264 * The #GDBusConnection the proxy is for.
268 g_object_class_install_property (gobject_class,
270 g_param_spec_object ("g-connection",
272 _("The connection the proxy is for"),
273 G_TYPE_DBUS_CONNECTION,
276 G_PARAM_CONSTRUCT_ONLY |
277 G_PARAM_STATIC_NAME |
278 G_PARAM_STATIC_BLURB |
279 G_PARAM_STATIC_NICK));
282 * GDBusProxy:g-flags:
284 * Flags from the #GDBusProxyFlags enumeration.
288 g_object_class_install_property (gobject_class,
290 g_param_spec_flags ("g-flags",
292 _("Flags for the proxy"),
293 G_TYPE_DBUS_PROXY_FLAGS,
294 G_DBUS_PROXY_FLAGS_NONE,
297 G_PARAM_CONSTRUCT_ONLY |
298 G_PARAM_STATIC_NAME |
299 G_PARAM_STATIC_BLURB |
300 G_PARAM_STATIC_NICK));
303 * GDBusProxy:g-unique-bus-name:
305 * The unique bus name the proxy is for.
309 g_object_class_install_property (gobject_class,
310 PROP_G_UNIQUE_BUS_NAME,
311 g_param_spec_string ("g-unique-bus-name",
312 _("g-unique-bus-name"),
313 _("The unique bus name the proxy is for"),
317 G_PARAM_CONSTRUCT_ONLY |
318 G_PARAM_STATIC_NAME |
319 G_PARAM_STATIC_BLURB |
320 G_PARAM_STATIC_NICK));
323 * GDBusProxy:g-object-path:
325 * The object path the proxy is for.
329 g_object_class_install_property (gobject_class,
331 g_param_spec_string ("g-object-path",
333 _("The object path the proxy is for"),
337 G_PARAM_CONSTRUCT_ONLY |
338 G_PARAM_STATIC_NAME |
339 G_PARAM_STATIC_BLURB |
340 G_PARAM_STATIC_NICK));
343 * GDBusProxy:g-interface-name:
345 * The D-Bus interface name the proxy is for.
349 g_object_class_install_property (gobject_class,
350 PROP_G_INTERFACE_NAME,
351 g_param_spec_string ("g-interface-name",
352 _("g-interface-name"),
353 _("The D-Bus interface name the proxy is for"),
357 G_PARAM_CONSTRUCT_ONLY |
358 G_PARAM_STATIC_NAME |
359 G_PARAM_STATIC_BLURB |
360 G_PARAM_STATIC_NICK));
363 * GDBusProxy:g-default-timeout:
365 * The timeout to use if -1 (specifying default timeout) is passed
366 * as @timeout_msec in the g_dbus_proxy_invoke_method() and
367 * g_dbus_proxy_invoke_method_sync() functions.
369 * This allows applications to set a proxy-wide timeout for all
370 * remote method invocations on the proxy. If this property is -1,
371 * the default timeout (typically 25 seconds) is used. If set to
372 * %G_MAXINT, then no timeout is used.
376 g_object_class_install_property (gobject_class,
377 PROP_G_DEFAULT_TIMEOUT,
378 g_param_spec_int ("g-default-timeout",
379 _("Default Timeout"),
380 _("Timeout for remote method invocation"),
387 G_PARAM_STATIC_NAME |
388 G_PARAM_STATIC_BLURB |
389 G_PARAM_STATIC_NICK));
392 * GDBusProxy::g-properties-changed:
393 * @proxy: The #GDBusProxy emitting the signal.
394 * @changed_properties: A #GHashTable containing the properties that changed.
396 * Emitted when one or more D-Bus properties on @proxy changes. The cached properties
397 * are already replaced when this signal fires.
401 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
404 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
407 g_cclosure_marshal_VOID__BOXED,
413 * GDBusProxy::g-signal:
414 * @proxy: The #GDBusProxy emitting the signal.
415 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
416 * @signal_name: The name of the signal.
417 * @parameters: A #GVariant tuple with parameters for the signal.
419 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
423 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
426 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
429 _gio_marshal_VOID__STRING_STRING_BOXED,
437 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
441 g_dbus_proxy_init (GDBusProxy *proxy)
443 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
446 /* ---------------------------------------------------------------------------------------------------- */
449 * g_dbus_proxy_get_cached_property_names:
450 * @proxy: A #GDBusProxy.
451 * @error: Return location for error or %NULL.
453 * Gets the names of all cached properties on @proxy.
455 * Returns: A %NULL-terminated array of strings or %NULL if @error is set. Free with
461 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy,
469 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
470 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
474 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
479 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
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))
488 g_ptr_array_add (p, g_strdup (key));
490 g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
491 g_ptr_array_add (p, NULL);
493 names = (gchar **) g_ptr_array_free (p, FALSE);
500 * g_dbus_proxy_get_cached_property:
501 * @proxy: A #GDBusProxy.
502 * @property_name: Property name.
503 * @error: Return location for error or %NULL.
505 * Looks up the value for a property from the cache. This call does no blocking IO.
507 * Normally you will not need to modify the returned variant since it is updated automatically
508 * in response to <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
509 * D-Bus signals (which also causes #GDBusProxy::g-properties-changed to be emitted).
511 * However, for properties for which said D-Bus signal is not emitted, you
512 * can catch other signals and modify the returned variant accordingly (remember to emit
513 * #GDBusProxy::g-properties-changed yourself).
515 * Returns: A reference to the #GVariant instance that holds the value for @property_name or
516 * %NULL if @error is set. Free the reference with g_variant_unref().
521 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
522 const gchar *property_name,
527 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
528 g_return_val_if_fail (property_name != NULL, NULL);
532 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
537 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
541 value = g_hash_table_lookup (proxy->priv->properties, property_name);
547 _("No property with name %s"),
552 g_variant_ref (value);
559 /* ---------------------------------------------------------------------------------------------------- */
562 on_signal_received (GDBusConnection *connection,
563 const gchar *sender_name,
564 const gchar *object_path,
565 const gchar *interface_name,
566 const gchar *signal_name,
567 GVariant *parameters,
570 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
572 g_signal_emit (proxy,
573 signals[SIGNAL_SIGNAL],
580 /* ---------------------------------------------------------------------------------------------------- */
583 on_properties_changed (GDBusConnection *connection,
584 const gchar *sender_name,
585 const gchar *object_path,
586 const gchar *interface_name,
587 const gchar *signal_name,
588 GVariant *parameters,
591 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
593 const gchar *interface_name_for_signal;
596 GHashTable *changed_properties;
602 /* Ignore this signal if properties are not yet available
604 * (can happen in the window between subscribing to PropertiesChanged() and until
605 * org.freedesktop.DBus.Properties.GetAll() returns)
607 if (!proxy->priv->properties_available)
611 if (strcmp (g_variant_get_type_string (parameters), "(sa{sv})") != 0)
613 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv})'",
614 g_variant_get_type_string (parameters));
618 g_variant_get (parameters,
620 &interface_name_for_signal,
623 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
626 changed_properties = g_hash_table_new_full (g_str_hash,
629 (GDestroyNotify) g_variant_unref);
631 while ((item = g_variant_iter_next_value (iter)))
641 g_hash_table_insert (proxy->priv->properties,
643 value); /* steals value */
645 g_hash_table_insert (changed_properties,
647 g_variant_ref (value));
652 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL], 0, changed_properties);
654 g_hash_table_unref (changed_properties);
658 g_variant_iter_free (iter);
661 /* ---------------------------------------------------------------------------------------------------- */
664 g_dbus_proxy_constructed (GObject *object)
666 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed != NULL)
667 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed (object);
670 /* ---------------------------------------------------------------------------------------------------- */
673 subscribe_to_signals (GDBusProxy *proxy)
675 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
677 /* subscribe to PropertiesChanged() */
678 proxy->priv->properties_changed_subscriber_id =
679 g_dbus_connection_signal_subscribe (proxy->priv->connection,
680 proxy->priv->unique_bus_name,
681 "org.freedesktop.DBus.Properties",
683 proxy->priv->object_path,
684 proxy->priv->interface_name,
685 on_properties_changed,
690 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
692 /* subscribe to all signals for the object */
693 proxy->priv->signals_subscriber_id =
694 g_dbus_connection_signal_subscribe (proxy->priv->connection,
695 proxy->priv->unique_bus_name,
696 proxy->priv->interface_name,
698 proxy->priv->object_path,
706 /* ---------------------------------------------------------------------------------------------------- */
709 process_get_all_reply (GDBusProxy *proxy,
715 if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
717 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
718 g_variant_get_type_string (result));
722 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
725 (GDestroyNotify) g_variant_unref);
727 g_variant_iter_init (&iter, g_variant_get_child_value (result, 0));
728 while ((item = g_variant_iter_next_value (&iter)) != NULL)
737 //g_print ("got %s -> %s\n", key, g_variant_markup_print (value, FALSE, 0, 0));
739 g_hash_table_insert (proxy->priv->properties,
741 value); /* steals value */
748 initable_init (GInitable *initable,
749 GCancellable *cancellable,
752 GDBusProxy *proxy = G_DBUS_PROXY (initable);
758 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
760 /* load all properties synchronously */
761 result = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
762 proxy->priv->unique_bus_name,
763 proxy->priv->object_path,
764 "org.freedesktop.DBus.Properties",
766 g_variant_new ("(s)", proxy->priv->interface_name),
767 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
774 process_get_all_reply (proxy, result);
776 g_variant_unref (result);
779 subscribe_to_signals (proxy);
788 initable_iface_init (GInitableIface *initable_iface)
790 initable_iface->init = initable_init;
793 /* ---------------------------------------------------------------------------------------------------- */
796 get_all_cb (GDBusConnection *connection,
800 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
805 result = g_dbus_connection_invoke_method_finish (connection,
810 g_simple_async_result_set_from_error (simple, error);
811 g_error_free (error);
815 g_simple_async_result_set_op_res_gpointer (simple,
817 (GDestroyNotify) g_variant_unref);
820 g_simple_async_result_complete_in_idle (simple);
821 g_object_unref (simple);
825 async_initable_init_async (GAsyncInitable *initable,
827 GCancellable *cancellable,
828 GAsyncReadyCallback callback,
831 GDBusProxy *proxy = G_DBUS_PROXY (initable);
832 GSimpleAsyncResult *simple;
834 simple = g_simple_async_result_new (G_OBJECT (proxy),
839 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
841 /* load all properties asynchronously */
842 g_dbus_connection_invoke_method (proxy->priv->connection,
843 proxy->priv->unique_bus_name,
844 proxy->priv->object_path,
845 "org.freedesktop.DBus.Properties",
847 g_variant_new ("(s)", proxy->priv->interface_name),
848 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
851 (GAsyncReadyCallback) get_all_cb,
856 g_simple_async_result_complete_in_idle (simple);
857 g_object_unref (simple);
862 async_initable_init_finish (GAsyncInitable *initable,
866 GDBusProxy *proxy = G_DBUS_PROXY (initable);
867 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
873 result = g_simple_async_result_get_op_res_gpointer (simple);
876 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
878 g_simple_async_result_propagate_error (simple, error);
884 process_get_all_reply (proxy, result);
887 subscribe_to_signals (proxy);
896 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
898 async_initable_iface->init_async = async_initable_init_async;
899 async_initable_iface->init_finish = async_initable_init_finish;
902 /* ---------------------------------------------------------------------------------------------------- */
906 * @connection: A #GDBusConnection.
907 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
908 * @flags: Flags used when constructing the proxy.
909 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
910 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
911 * @object_path: An object path.
912 * @interface_name: A D-Bus interface name.
913 * @cancellable: A #GCancellable or %NULL.
914 * @callback: Callback function to invoke when the proxy is ready.
915 * @user_data: User data to pass to @callback.
917 * Creates a proxy for accessing @interface_name on the remote object at @object_path
918 * owned by @unique_bus_name at @connection and asynchronously loads D-Bus properties unless the
919 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to the
920 * #GDBusProxy::g-properties-changed signal to get notified about property changes.
922 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
923 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
924 * to handle signals from the remote object.
926 * This is a failable asynchronous constructor - when the proxy is
927 * ready, @callback will be invoked and you can use
928 * g_dbus_proxy_new_finish() to get the result.
930 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
935 g_dbus_proxy_new (GDBusConnection *connection,
937 GDBusProxyFlags flags,
938 GDBusInterfaceInfo *info,
939 const gchar *unique_bus_name,
940 const gchar *object_path,
941 const gchar *interface_name,
942 GCancellable *cancellable,
943 GAsyncReadyCallback callback,
946 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
947 g_return_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY));
948 g_return_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
949 g_dbus_is_unique_name (unique_bus_name));
950 g_return_if_fail (g_variant_is_object_path (object_path));
951 g_return_if_fail (g_dbus_is_interface_name (interface_name));
953 g_async_initable_new_async (object_type,
959 "g-interface-info", info,
960 "g-unique-bus-name", unique_bus_name,
961 "g-connection", connection,
962 "g-object-path", object_path,
963 "g-interface-name", interface_name,
968 * g_dbus_proxy_new_finish:
969 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
970 * @error: Return location for error or %NULL.
972 * Finishes creating a #GDBusProxy.
974 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
979 g_dbus_proxy_new_finish (GAsyncResult *res,
983 GObject *source_object;
985 source_object = g_async_result_get_source_object (res);
986 g_assert (source_object != NULL);
988 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
991 g_object_unref (source_object);
994 return G_DBUS_PROXY (object);
1000 /* ---------------------------------------------------------------------------------------------------- */
1003 * g_dbus_proxy_new_sync:
1004 * @connection: A #GDBusConnection.
1005 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
1006 * @flags: Flags used when constructing the proxy.
1007 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1008 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
1009 * @object_path: An object path.
1010 * @interface_name: A D-Bus interface name.
1011 * @cancellable: A #GCancellable or %NULL.
1012 * @error: Return location for error or %NULL.
1014 * Creates a proxy for accessing @interface_name on the remote object at @object_path
1015 * owned by @unique_bus_name at @connection and synchronously loads D-Bus properties unless the
1016 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1018 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1019 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1020 * to handle signals from the remote object.
1022 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1023 * and g_dbus_proxy_new_finish() for the asynchronous version.
1025 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1030 g_dbus_proxy_new_sync (GDBusConnection *connection,
1032 GDBusProxyFlags flags,
1033 GDBusInterfaceInfo *info,
1034 const gchar *unique_bus_name,
1035 const gchar *object_path,
1036 const gchar *interface_name,
1037 GCancellable *cancellable,
1040 GInitable *initable;
1042 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1043 g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY), NULL);
1044 g_return_val_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1045 g_dbus_is_unique_name (unique_bus_name), NULL);
1046 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1047 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1049 initable = g_initable_new (object_type,
1053 "g-interface-info", info,
1054 "g-unique-bus-name", unique_bus_name,
1055 "g-connection", connection,
1056 "g-object-path", object_path,
1057 "g-interface-name", interface_name,
1059 if (initable != NULL)
1060 return G_DBUS_PROXY (initable);
1065 /* ---------------------------------------------------------------------------------------------------- */
1068 * g_dbus_proxy_get_connection:
1069 * @proxy: A #GDBusProxy.
1071 * Gets the connection @proxy is for.
1073 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1078 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1080 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1081 return proxy->priv->connection;
1085 * g_dbus_proxy_get_flags:
1086 * @proxy: A #GDBusProxy.
1088 * Gets the flags that @proxy was constructed with.
1090 * Returns: Flags from the #GDBusProxyFlags enumeration.
1095 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1097 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1098 return proxy->priv->flags;
1102 * g_dbus_proxy_get_unique_bus_name:
1103 * @proxy: A #GDBusProxy.
1105 * Gets the unique bus name @proxy is for.
1107 * Returns: A string owned by @proxy. Do not free.
1112 g_dbus_proxy_get_unique_bus_name (GDBusProxy *proxy)
1114 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1115 return proxy->priv->unique_bus_name;
1119 * g_dbus_proxy_get_object_path:
1120 * @proxy: A #GDBusProxy.
1122 * Gets the object path @proxy is for.
1124 * Returns: A string owned by @proxy. Do not free.
1129 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1131 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1132 return proxy->priv->object_path;
1136 * g_dbus_proxy_get_interface_name:
1137 * @proxy: A #GDBusProxy.
1139 * Gets the D-Bus interface name @proxy is for.
1141 * Returns: A string owned by @proxy. Do not free.
1146 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1148 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1149 return proxy->priv->interface_name;
1153 * g_dbus_proxy_get_default_timeout:
1154 * @proxy: A #GDBusProxy.
1156 * Gets the timeout to use if -1 (specifying default timeout) is
1157 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1158 * g_dbus_proxy_invoke_method_sync() functions.
1160 * See the #GDBusProxy:g-default-timeout property for more details.
1162 * Returns: Timeout to use for @proxy.
1167 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1169 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1170 return proxy->priv->timeout_msec;
1174 * g_dbus_proxy_set_default_timeout:
1175 * @proxy: A #GDBusProxy.
1176 * @timeout_msec: Timeout in milliseconds.
1178 * Sets the timeout to use if -1 (specifying default timeout) is
1179 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1180 * g_dbus_proxy_invoke_method_sync() functions.
1182 * See the #GDBusProxy:g-default-timeout property for more details.
1187 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1190 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1191 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1193 /* TODO: locking? */
1194 if (proxy->priv->timeout_msec != timeout_msec)
1196 proxy->priv->timeout_msec = timeout_msec;
1197 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
1202 * g_dbus_proxy_get_interface_info:
1203 * @proxy: A #GDBusProxy
1205 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
1206 * interface that @proxy conforms to.
1208 * See the #GDBusProxy:g-interface-info property for more details.
1210 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
1211 * object, it is owned by @proxy.
1215 GDBusInterfaceInfo *
1216 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
1218 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1219 return proxy->priv->expected_interface;
1223 * g_dbus_proxy_set_interface_info:
1224 * @proxy: A #GDBusProxy
1225 * @info: Minimum interface this proxy conforms to or %NULL to unset.
1227 * Ensure that interactions with @proxy conform to the given
1228 * interface. For example, when completing a method call, if the type
1229 * signature of the message isn't what's expected, the given #GError
1230 * is set. Signals that have a type signature mismatch are simply
1233 * See the #GDBusProxy:g-interface-info property for more details.
1238 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
1239 GDBusInterfaceInfo *info)
1241 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1242 if (proxy->priv->expected_interface != NULL)
1243 g_dbus_interface_info_unref (proxy->priv->expected_interface);
1244 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
1247 /* ---------------------------------------------------------------------------------------------------- */
1250 maybe_split_method_name (const gchar *method_name,
1251 gchar **out_interface_name,
1252 const gchar **out_method_name)
1257 g_assert (out_interface_name != NULL);
1258 g_assert (out_method_name != NULL);
1259 *out_interface_name = NULL;
1260 *out_method_name = NULL;
1262 if (strchr (method_name, '.') != NULL)
1267 p = g_strdup (method_name);
1268 last_dot = strrchr (p, '.');
1271 *out_interface_name = p;
1272 *out_method_name = last_dot + 1;
1282 reply_cb (GDBusConnection *connection,
1286 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1291 value = g_dbus_connection_invoke_method_finish (connection,
1296 g_simple_async_result_set_from_error (simple,
1298 g_error_free (error);
1302 g_simple_async_result_set_op_res_gpointer (simple,
1304 (GDestroyNotify) g_variant_unref);
1307 /* no need to complete in idle since the method GDBusConnection already does */
1308 g_simple_async_result_complete (simple);
1311 static const GDBusMethodInfo *
1312 lookup_method_info_or_warn (GDBusProxy *proxy,
1313 const char *method_name)
1315 const GDBusMethodInfo *info;
1317 if (!proxy->priv->expected_interface)
1320 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1322 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1323 method_name, proxy->priv->expected_interface->name);
1329 validate_method_return (const char *method_name,
1331 const GDBusMethodInfo *expected_method_info,
1334 const gchar *type_string;
1341 if (value == NULL || expected_method_info == NULL)
1344 /* Shouldn't happen... */
1345 if (g_variant_classify (value) != G_VARIANT_CLASS_TUPLE)
1348 type_string = g_variant_get_type_string (value);
1349 signature = _g_dbus_compute_complete_signature (expected_method_info->out_args, TRUE);
1350 if (g_strcmp0 (type_string, signature) != 0)
1354 G_IO_ERROR_INVALID_ARGUMENT,
1355 _("Method `%s' returned signature `%s', but expected `%s'"),
1368 * g_dbus_proxy_invoke_method:
1369 * @proxy: A #GDBusProxy.
1370 * @method_name: Name of method to invoke.
1371 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1372 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1373 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1374 * @cancellable: A #GCancellable or %NULL.
1375 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1376 * care about the result of the method invocation.
1377 * @user_data: The data to pass to @callback.
1379 * Asynchronously invokes the @method_name method on @proxy.
1381 * If @method_name contains any dots, then @name is split into interface and
1382 * method name parts. This allows using @proxy for invoking methods on
1385 * If the #GDBusConnection associated with @proxy is closed then
1386 * the operation will fail with %G_IO_ERROR_CLOSED. If
1387 * @cancellable is canceled, the operation will fail with
1388 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1389 * compatible with the D-Bus protocol, the operation fails with
1390 * %G_IO_ERROR_INVALID_ARGUMENT.
1392 * This is an asynchronous method. When the operation is finished, @callback will be invoked
1393 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
1394 * of the thread you are calling this method from. You can then call
1395 * g_dbus_proxy_invoke_method_finish() to get the result of the operation.
1396 * See g_dbus_proxy_invoke_method_sync() for the
1397 * synchronous version of this method.
1402 g_dbus_proxy_invoke_method (GDBusProxy *proxy,
1403 const gchar *method_name,
1404 GVariant *parameters,
1405 GDBusInvokeMethodFlags flags,
1407 GCancellable *cancellable,
1408 GAsyncReadyCallback callback,
1411 GSimpleAsyncResult *simple;
1413 gchar *split_interface_name;
1414 const gchar *split_method_name;
1415 const GDBusMethodInfo *expected_method_info;
1416 const gchar *target_method_name;
1417 const gchar *target_interface_name;
1419 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1420 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
1421 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
1422 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1424 simple = g_simple_async_result_new (G_OBJECT (proxy),
1427 g_dbus_proxy_invoke_method);
1429 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1430 target_method_name = was_split ? split_method_name : method_name;
1431 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1433 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
1435 /* Just warn here */
1436 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
1438 g_dbus_connection_invoke_method (proxy->priv->connection,
1439 proxy->priv->unique_bus_name,
1440 proxy->priv->object_path,
1441 target_interface_name,
1445 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1447 (GAsyncReadyCallback) reply_cb,
1450 g_free (split_interface_name);
1454 * g_dbus_proxy_invoke_method_finish:
1455 * @proxy: A #GDBusProxy.
1456 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_invoke_method().
1457 * @error: Return location for error or %NULL.
1459 * Finishes an operation started with g_dbus_proxy_invoke_method().
1461 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1462 * return values. Free with g_variant_unref().
1467 g_dbus_proxy_invoke_method_finish (GDBusProxy *proxy,
1471 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1473 const char *method_name;
1474 const GDBusMethodInfo *expected_method_info;
1476 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1477 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1478 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1480 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_invoke_method);
1484 if (g_simple_async_result_propagate_error (simple, error))
1487 value = g_simple_async_result_get_op_res_gpointer (simple);
1488 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
1490 /* We may not have a method name for internally-generated proxy calls like GetAll */
1491 if (value && method_name && proxy->priv->expected_interface)
1493 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1494 if (!validate_method_return (method_name, value, expected_method_info, error))
1496 g_variant_unref (value);
1506 * g_dbus_proxy_invoke_method_sync:
1507 * @proxy: A #GDBusProxy.
1508 * @method_name: Name of method to invoke.
1509 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1510 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1511 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1512 * @cancellable: A #GCancellable or %NULL.
1513 * @error: Return location for error or %NULL.
1515 * Synchronously invokes the @method_name method on @proxy.
1517 * If @method_name contains any dots, then @name is split into interface and
1518 * method name parts. This allows using @proxy for invoking methods on
1521 * If the #GDBusConnection associated with @proxy is disconnected then
1522 * the operation will fail with %G_IO_ERROR_CLOSED. If
1523 * @cancellable is canceled, the operation will fail with
1524 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1525 * compatible with the D-Bus protocol, the operation fails with
1526 * %G_IO_ERROR_INVALID_ARGUMENT.
1528 * The calling thread is blocked until a reply is received. See
1529 * g_dbus_proxy_invoke_method() for the asynchronous version of this
1532 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1533 * return values. Free with g_variant_unref().
1538 g_dbus_proxy_invoke_method_sync (GDBusProxy *proxy,
1539 const gchar *method_name,
1540 GVariant *parameters,
1541 GDBusInvokeMethodFlags flags,
1543 GCancellable *cancellable,
1548 gchar *split_interface_name;
1549 const gchar *split_method_name;
1550 const GDBusMethodInfo *expected_method_info;
1551 const char *target_method_name;
1552 const char *target_interface_name;
1554 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1555 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
1556 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
1557 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
1558 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1560 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1561 target_method_name = was_split ? split_method_name : method_name;
1562 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1564 if (proxy->priv->expected_interface)
1566 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
1567 if (!expected_method_info)
1568 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1569 target_method_name, target_interface_name);
1573 expected_method_info = NULL;
1576 ret = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
1577 proxy->priv->unique_bus_name,
1578 proxy->priv->object_path,
1579 target_interface_name,
1583 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1586 if (!validate_method_return (target_method_name, ret, expected_method_info, error))
1588 g_variant_unref (ret);
1592 g_free (split_interface_name);
1597 /* ---------------------------------------------------------------------------------------------------- */