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>
26 #include <glib/gi18n.h>
27 #include <gobject/gvaluecollector.h>
29 #include "gdbusutils.h"
30 #include "gdbusproxy.h"
31 #include "gioenumtypes.h"
32 #include "gdbusconnection.h"
33 #include "gdbuserror.h"
34 #include "gdbusprivate.h"
35 #include "gio-marshal.h"
36 #include "ginitable.h"
37 #include "gasyncinitable.h"
39 #include "gasyncresult.h"
40 #include "gsimpleasyncresult.h"
44 * @short_description: Base class for proxies
47 * #GDBusProxy is a base class used for proxies to access a D-Bus
48 * interface on a remote object. A #GDBusProxy can only be constructed
49 * for unique name bus and does not track whether the name
50 * vanishes. Use g_bus_watch_proxy() to construct #GDBusProxy proxies
51 * for owners of a well-known names.
54 struct _GDBusProxyPrivate
56 GDBusConnection *connection;
57 GDBusProxyFlags flags;
58 gchar *unique_bus_name;
60 gchar *interface_name;
63 /* gchar* -> GVariant* */
64 GHashTable *properties;
66 GDBusInterfaceInfo *expected_interface;
68 guint properties_changed_subscriber_id;
69 guint signals_subscriber_id;
76 PROP_G_UNIQUE_BUS_NAME,
79 PROP_G_INTERFACE_NAME,
80 PROP_G_DEFAULT_TIMEOUT,
86 PROPERTIES_CHANGED_SIGNAL,
91 static void g_dbus_proxy_constructed (GObject *object);
93 guint signals[LAST_SIGNAL] = {0};
95 static void initable_iface_init (GInitableIface *initable_iface);
96 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
98 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
99 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
100 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
104 g_dbus_proxy_finalize (GObject *object)
106 GDBusProxy *proxy = G_DBUS_PROXY (object);
108 if (proxy->priv->properties_changed_subscriber_id > 0)
110 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
111 proxy->priv->properties_changed_subscriber_id);
114 if (proxy->priv->signals_subscriber_id > 0)
116 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
117 proxy->priv->signals_subscriber_id);
120 g_object_unref (proxy->priv->connection);
121 g_free (proxy->priv->unique_bus_name);
122 g_free (proxy->priv->object_path);
123 g_free (proxy->priv->interface_name);
124 if (proxy->priv->properties != NULL)
125 g_hash_table_unref (proxy->priv->properties);
127 if (proxy->priv->expected_interface != NULL)
128 g_dbus_interface_info_unref (proxy->priv->expected_interface);
130 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize != NULL)
131 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
135 g_dbus_proxy_get_property (GObject *object,
140 GDBusProxy *proxy = G_DBUS_PROXY (object);
144 case PROP_G_CONNECTION:
145 g_value_set_object (value, proxy->priv->connection);
149 g_value_set_flags (value, proxy->priv->flags);
152 case PROP_G_UNIQUE_BUS_NAME:
153 g_value_set_string (value, proxy->priv->unique_bus_name);
156 case PROP_G_OBJECT_PATH:
157 g_value_set_string (value, proxy->priv->object_path);
160 case PROP_G_INTERFACE_NAME:
161 g_value_set_string (value, proxy->priv->interface_name);
164 case PROP_G_DEFAULT_TIMEOUT:
165 g_value_set_int (value, proxy->priv->timeout_msec);
168 case PROP_G_INTERFACE_INFO:
169 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 g_dbus_proxy_set_property (GObject *object,
184 GDBusProxy *proxy = G_DBUS_PROXY (object);
188 case PROP_G_CONNECTION:
189 proxy->priv->connection = g_value_dup_object (value);
193 proxy->priv->flags = g_value_get_flags (value);
196 case PROP_G_UNIQUE_BUS_NAME:
197 proxy->priv->unique_bus_name = g_value_dup_string (value);
200 case PROP_G_OBJECT_PATH:
201 proxy->priv->object_path = g_value_dup_string (value);
204 case PROP_G_INTERFACE_NAME:
205 proxy->priv->interface_name = g_value_dup_string (value);
208 case PROP_G_DEFAULT_TIMEOUT:
209 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
212 case PROP_G_INTERFACE_INFO:
213 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 g_dbus_proxy_class_init (GDBusProxyClass *klass)
225 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
227 gobject_class->finalize = g_dbus_proxy_finalize;
228 gobject_class->set_property = g_dbus_proxy_set_property;
229 gobject_class->get_property = g_dbus_proxy_get_property;
230 gobject_class->constructed = g_dbus_proxy_constructed;
232 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
233 * in derived classes */
236 * GDBusProxy:g-interface-info:
238 * Ensure that interactions with this proxy conform to the given
239 * interface. For example, when completing a method call, if the
240 * type signature of the message isn't what's expected, the given
241 * #GError is set. Signals that have a type signature mismatch are
246 g_object_class_install_property (gobject_class,
247 PROP_G_INTERFACE_INFO,
248 g_param_spec_boxed ("g-interface-info",
249 _("Interface Information"),
250 _("Interface Information"),
251 G_TYPE_DBUS_INTERFACE_INFO,
254 G_PARAM_STATIC_NAME |
255 G_PARAM_STATIC_BLURB |
256 G_PARAM_STATIC_NICK));
259 * GDBusProxy:g-connection:
261 * The #GDBusConnection the proxy is for.
265 g_object_class_install_property (gobject_class,
267 g_param_spec_object ("g-connection",
269 _("The connection the proxy is for"),
270 G_TYPE_DBUS_CONNECTION,
273 G_PARAM_CONSTRUCT_ONLY |
274 G_PARAM_STATIC_NAME |
275 G_PARAM_STATIC_BLURB |
276 G_PARAM_STATIC_NICK));
279 * GDBusProxy:g-flags:
281 * Flags from the #GDBusProxyFlags enumeration.
285 g_object_class_install_property (gobject_class,
287 g_param_spec_flags ("g-flags",
289 _("Flags for the proxy"),
290 G_TYPE_DBUS_PROXY_FLAGS,
291 G_DBUS_PROXY_FLAGS_NONE,
294 G_PARAM_CONSTRUCT_ONLY |
295 G_PARAM_STATIC_NAME |
296 G_PARAM_STATIC_BLURB |
297 G_PARAM_STATIC_NICK));
300 * GDBusProxy:g-unique-bus-name:
302 * The unique bus name the proxy is for.
306 g_object_class_install_property (gobject_class,
307 PROP_G_UNIQUE_BUS_NAME,
308 g_param_spec_string ("g-unique-bus-name",
309 _("g-unique-bus-name"),
310 _("The unique bus name the proxy is for"),
314 G_PARAM_CONSTRUCT_ONLY |
315 G_PARAM_STATIC_NAME |
316 G_PARAM_STATIC_BLURB |
317 G_PARAM_STATIC_NICK));
320 * GDBusProxy:g-object-path:
322 * The object path the proxy is for.
326 g_object_class_install_property (gobject_class,
328 g_param_spec_string ("g-object-path",
330 _("The object path the proxy is for"),
334 G_PARAM_CONSTRUCT_ONLY |
335 G_PARAM_STATIC_NAME |
336 G_PARAM_STATIC_BLURB |
337 G_PARAM_STATIC_NICK));
340 * GDBusProxy:g-interface-name:
342 * The D-Bus interface name the proxy is for.
346 g_object_class_install_property (gobject_class,
347 PROP_G_INTERFACE_NAME,
348 g_param_spec_string ("g-interface-name",
349 _("g-interface-name"),
350 _("The D-Bus interface name the proxy is for"),
354 G_PARAM_CONSTRUCT_ONLY |
355 G_PARAM_STATIC_NAME |
356 G_PARAM_STATIC_BLURB |
357 G_PARAM_STATIC_NICK));
360 * GDBusProxy:g-default-timeout:
362 * The timeout to use if -1 (specifying default timeout) is passed
363 * as @timeout_msec in the g_dbus_proxy_invoke_method() and
364 * g_dbus_proxy_invoke_method_sync() functions.
366 * This allows applications to set a proxy-wide timeout for all
367 * remote method invocations on the proxy. If this property is -1,
368 * the default timeout (typically 25 seconds) is used. If set to
369 * %G_MAXINT, then no timeout is used.
373 g_object_class_install_property (gobject_class,
374 PROP_G_DEFAULT_TIMEOUT,
375 g_param_spec_int ("g-default-timeout",
376 _("Default Timeout"),
377 _("Timeout for remote method invocation"),
384 G_PARAM_STATIC_NAME |
385 G_PARAM_STATIC_BLURB |
386 G_PARAM_STATIC_NICK));
389 * GDBusProxy::g-properties-changed:
390 * @proxy: The #GDBusProxy emitting the signal.
391 * @changed_properties: A #GHashTable containing the properties that changed.
393 * Emitted when one or more D-Bus properties on @proxy changes. The cached properties
394 * are already replaced when this signal fires.
398 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
401 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
404 g_cclosure_marshal_VOID__BOXED,
410 * GDBusProxy::g-signal:
411 * @proxy: The #GDBusProxy emitting the signal.
412 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
413 * @signal_name: The name of the signal.
414 * @parameters: A #GVariant tuple with parameters for the signal.
416 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
420 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
423 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
426 _gio_marshal_VOID__STRING_STRING_BOXED,
434 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
438 g_dbus_proxy_init (GDBusProxy *proxy)
440 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
443 /* ---------------------------------------------------------------------------------------------------- */
446 * g_dbus_proxy_get_cached_property_names:
447 * @proxy: A #GDBusProxy.
448 * @error: Return location for error or %NULL.
450 * Gets the names of all cached properties on @proxy.
452 * Returns: A %NULL-terminated array of strings or %NULL if @error is set. Free with
458 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy,
466 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
467 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
471 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
476 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
480 p = g_ptr_array_new ();
482 g_hash_table_iter_init (&iter, proxy->priv->properties);
483 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
485 g_ptr_array_add (p, g_strdup (key));
487 g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
488 g_ptr_array_add (p, NULL);
490 names = (gchar **) g_ptr_array_free (p, FALSE);
497 * g_dbus_proxy_get_cached_property:
498 * @proxy: A #GDBusProxy.
499 * @property_name: Property name.
500 * @error: Return location for error or %NULL.
502 * Looks up the value for a property from the cache. This call does no blocking IO.
504 * Normally you will not need to modify the returned variant since it is updated automatically
505 * in response to <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
506 * D-Bus signals (which also causes #GDBusProxy::g-properties-changed to be emitted).
508 * However, for properties for which said D-Bus signal is not emitted, you
509 * can catch other signals and modify the returned variant accordingly (remember to emit
510 * #GDBusProxy::g-properties-changed yourself).
512 * Returns: A reference to the #GVariant instance that holds the value for @property_name or
513 * %NULL if @error is set. Free the reference with g_variant_unref().
518 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
519 const gchar *property_name,
524 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
525 g_return_val_if_fail (property_name != NULL, NULL);
529 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
534 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
538 value = g_hash_table_lookup (proxy->priv->properties, property_name);
544 _("No property with name %s"),
549 g_variant_ref (value);
556 /* ---------------------------------------------------------------------------------------------------- */
559 on_signal_received (GDBusConnection *connection,
560 const gchar *sender_name,
561 const gchar *object_path,
562 const gchar *interface_name,
563 const gchar *signal_name,
564 GVariant *parameters,
567 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
569 g_signal_emit (proxy,
570 signals[SIGNAL_SIGNAL],
577 /* ---------------------------------------------------------------------------------------------------- */
580 on_properties_changed (GDBusConnection *connection,
581 const gchar *sender_name,
582 const gchar *object_path,
583 const gchar *interface_name,
584 const gchar *signal_name,
585 GVariant *parameters,
588 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
590 const gchar *interface_name_for_signal;
593 GHashTable *changed_properties;
599 /* Ignore this signal if properties are not yet available
601 * (can happen in the window between subscribing to PropertiesChanged() and until
602 * org.freedesktop.DBus.Properties.GetAll() returns)
604 if (!proxy->priv->properties_available)
608 if (strcmp (g_variant_get_type_string (parameters), "(sa{sv})") != 0)
610 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv})'",
611 g_variant_get_type_string (parameters));
615 g_variant_get (parameters,
617 &interface_name_for_signal,
620 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
623 changed_properties = g_hash_table_new_full (g_str_hash,
626 (GDestroyNotify) g_variant_unref);
628 while ((item = g_variant_iter_next_value (iter)))
638 g_hash_table_insert (proxy->priv->properties,
640 value); /* steals value */
642 g_hash_table_insert (changed_properties,
644 g_variant_ref (value));
649 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL], 0, changed_properties);
651 g_hash_table_unref (changed_properties);
655 g_variant_iter_free (iter);
658 /* ---------------------------------------------------------------------------------------------------- */
661 g_dbus_proxy_constructed (GObject *object)
663 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed != NULL)
664 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed (object);
667 /* ---------------------------------------------------------------------------------------------------- */
670 subscribe_to_signals (GDBusProxy *proxy)
672 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
674 /* subscribe to PropertiesChanged() */
675 proxy->priv->properties_changed_subscriber_id =
676 g_dbus_connection_signal_subscribe (proxy->priv->connection,
677 proxy->priv->unique_bus_name,
678 "org.freedesktop.DBus.Properties",
680 proxy->priv->object_path,
681 proxy->priv->interface_name,
682 on_properties_changed,
687 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
689 /* subscribe to all signals for the object */
690 proxy->priv->signals_subscriber_id =
691 g_dbus_connection_signal_subscribe (proxy->priv->connection,
692 proxy->priv->unique_bus_name,
693 proxy->priv->interface_name,
695 proxy->priv->object_path,
703 /* ---------------------------------------------------------------------------------------------------- */
706 process_get_all_reply (GDBusProxy *proxy,
712 if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
714 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
715 g_variant_get_type_string (result));
719 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
722 (GDestroyNotify) g_variant_unref);
724 g_variant_iter_init (&iter, g_variant_get_child_value (result, 0));
725 while ((item = g_variant_iter_next_value (&iter)) != NULL)
734 //g_print ("got %s -> %s\n", key, g_variant_markup_print (value, FALSE, 0, 0));
736 g_hash_table_insert (proxy->priv->properties,
738 value); /* steals value */
745 initable_init (GInitable *initable,
746 GCancellable *cancellable,
749 GDBusProxy *proxy = G_DBUS_PROXY (initable);
755 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
757 /* load all properties synchronously */
758 result = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
759 proxy->priv->unique_bus_name,
760 proxy->priv->object_path,
761 "org.freedesktop.DBus.Properties",
763 g_variant_new ("(s)", proxy->priv->interface_name),
764 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
771 process_get_all_reply (proxy, result);
773 g_variant_unref (result);
776 subscribe_to_signals (proxy);
785 initable_iface_init (GInitableIface *initable_iface)
787 initable_iface->init = initable_init;
790 /* ---------------------------------------------------------------------------------------------------- */
793 get_all_cb (GDBusConnection *connection,
797 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
802 result = g_dbus_connection_invoke_method_finish (connection,
807 g_simple_async_result_set_from_error (simple, error);
808 g_error_free (error);
812 g_simple_async_result_set_op_res_gpointer (simple,
814 (GDestroyNotify) g_variant_unref);
817 g_simple_async_result_complete_in_idle (simple);
818 g_object_unref (simple);
822 async_initable_init_async (GAsyncInitable *initable,
824 GCancellable *cancellable,
825 GAsyncReadyCallback callback,
828 GDBusProxy *proxy = G_DBUS_PROXY (initable);
829 GSimpleAsyncResult *simple;
831 simple = g_simple_async_result_new (G_OBJECT (proxy),
836 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
838 /* load all properties asynchronously */
839 g_dbus_connection_invoke_method (proxy->priv->connection,
840 proxy->priv->unique_bus_name,
841 proxy->priv->object_path,
842 "org.freedesktop.DBus.Properties",
844 g_variant_new ("(s)", proxy->priv->interface_name),
845 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
848 (GAsyncReadyCallback) get_all_cb,
853 g_simple_async_result_complete_in_idle (simple);
854 g_object_unref (simple);
859 async_initable_init_finish (GAsyncInitable *initable,
863 GDBusProxy *proxy = G_DBUS_PROXY (initable);
864 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
870 result = g_simple_async_result_get_op_res_gpointer (simple);
873 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
875 g_simple_async_result_propagate_error (simple, error);
881 process_get_all_reply (proxy, result);
884 subscribe_to_signals (proxy);
893 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
895 async_initable_iface->init_async = async_initable_init_async;
896 async_initable_iface->init_finish = async_initable_init_finish;
899 /* ---------------------------------------------------------------------------------------------------- */
903 * @connection: A #GDBusConnection.
904 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
905 * @flags: Flags used when constructing the proxy.
906 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
907 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
908 * @object_path: An object path.
909 * @interface_name: A D-Bus interface name.
910 * @cancellable: A #GCancellable or %NULL.
911 * @callback: Callback function to invoke when the proxy is ready.
912 * @user_data: User data to pass to @callback.
914 * Creates a proxy for accessing @interface_name on the remote object at @object_path
915 * owned by @unique_bus_name at @connection and asynchronously loads D-Bus properties unless the
916 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to the
917 * #GDBusProxy::g-properties-changed signal to get notified about property changes.
919 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
920 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
921 * to handle signals from the remote object.
923 * This is a failable asynchronous constructor - when the proxy is
924 * ready, @callback will be invoked and you can use
925 * g_dbus_proxy_new_finish() to get the result.
927 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
932 g_dbus_proxy_new (GDBusConnection *connection,
934 GDBusProxyFlags flags,
935 GDBusInterfaceInfo *info,
936 const gchar *unique_bus_name,
937 const gchar *object_path,
938 const gchar *interface_name,
939 GCancellable *cancellable,
940 GAsyncReadyCallback callback,
943 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
944 g_return_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY));
945 g_return_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
946 g_dbus_is_unique_name (unique_bus_name));
947 g_return_if_fail (g_variant_is_object_path (object_path));
948 g_return_if_fail (g_dbus_is_interface_name (interface_name));
950 g_async_initable_new_async (object_type,
956 "g-interface-info", info,
957 "g-unique-bus-name", unique_bus_name,
958 "g-connection", connection,
959 "g-object-path", object_path,
960 "g-interface-name", interface_name,
965 * g_dbus_proxy_new_finish:
966 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
967 * @error: Return location for error or %NULL.
969 * Finishes creating a #GDBusProxy.
971 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
976 g_dbus_proxy_new_finish (GAsyncResult *res,
980 GObject *source_object;
982 source_object = g_async_result_get_source_object (res);
983 g_assert (source_object != NULL);
985 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
988 g_object_unref (source_object);
991 return G_DBUS_PROXY (object);
997 /* ---------------------------------------------------------------------------------------------------- */
1000 * g_dbus_proxy_new_sync:
1001 * @connection: A #GDBusConnection.
1002 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
1003 * @flags: Flags used when constructing the proxy.
1004 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1005 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
1006 * @object_path: An object path.
1007 * @interface_name: A D-Bus interface name.
1008 * @cancellable: A #GCancellable or %NULL.
1009 * @error: Return location for error or %NULL.
1011 * Creates a proxy for accessing @interface_name on the remote object at @object_path
1012 * owned by @unique_bus_name at @connection and synchronously loads D-Bus properties unless the
1013 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1015 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1016 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1017 * to handle signals from the remote object.
1019 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1020 * and g_dbus_proxy_new_finish() for the asynchronous version.
1022 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1027 g_dbus_proxy_new_sync (GDBusConnection *connection,
1029 GDBusProxyFlags flags,
1030 GDBusInterfaceInfo *info,
1031 const gchar *unique_bus_name,
1032 const gchar *object_path,
1033 const gchar *interface_name,
1034 GCancellable *cancellable,
1037 GInitable *initable;
1039 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1040 g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY), NULL);
1041 g_return_val_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1042 g_dbus_is_unique_name (unique_bus_name), NULL);
1043 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1044 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1046 initable = g_initable_new (object_type,
1050 "g-interface-info", info,
1051 "g-unique-bus-name", unique_bus_name,
1052 "g-connection", connection,
1053 "g-object-path", object_path,
1054 "g-interface-name", interface_name,
1056 if (initable != NULL)
1057 return G_DBUS_PROXY (initable);
1062 /* ---------------------------------------------------------------------------------------------------- */
1065 * g_dbus_proxy_get_connection:
1066 * @proxy: A #GDBusProxy.
1068 * Gets the connection @proxy is for.
1070 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1075 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1077 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1078 return proxy->priv->connection;
1082 * g_dbus_proxy_get_flags:
1083 * @proxy: A #GDBusProxy.
1085 * Gets the flags that @proxy was constructed with.
1087 * Returns: Flags from the #GDBusProxyFlags enumeration.
1092 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1094 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1095 return proxy->priv->flags;
1099 * g_dbus_proxy_get_unique_bus_name:
1100 * @proxy: A #GDBusProxy.
1102 * Gets the unique bus name @proxy is for.
1104 * Returns: A string owned by @proxy. Do not free.
1109 g_dbus_proxy_get_unique_bus_name (GDBusProxy *proxy)
1111 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1112 return proxy->priv->unique_bus_name;
1116 * g_dbus_proxy_get_object_path:
1117 * @proxy: A #GDBusProxy.
1119 * Gets the object path @proxy is for.
1121 * Returns: A string owned by @proxy. Do not free.
1126 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1128 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1129 return proxy->priv->object_path;
1133 * g_dbus_proxy_get_interface_name:
1134 * @proxy: A #GDBusProxy.
1136 * Gets the D-Bus interface name @proxy is for.
1138 * Returns: A string owned by @proxy. Do not free.
1143 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1145 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1146 return proxy->priv->interface_name;
1150 * g_dbus_proxy_get_default_timeout:
1151 * @proxy: A #GDBusProxy.
1153 * Gets the timeout to use if -1 (specifying default timeout) is
1154 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1155 * g_dbus_proxy_invoke_method_sync() functions.
1157 * See the #GDBusProxy:g-default-timeout property for more details.
1159 * Returns: Timeout to use for @proxy.
1164 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1166 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1167 return proxy->priv->timeout_msec;
1171 * g_dbus_proxy_set_default_timeout:
1172 * @proxy: A #GDBusProxy.
1173 * @timeout_msec: Timeout in milliseconds.
1175 * Sets the timeout to use if -1 (specifying default timeout) is
1176 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1177 * g_dbus_proxy_invoke_method_sync() functions.
1179 * See the #GDBusProxy:g-default-timeout property for more details.
1184 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1187 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1188 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1190 /* TODO: locking? */
1191 if (proxy->priv->timeout_msec != timeout_msec)
1193 proxy->priv->timeout_msec = timeout_msec;
1194 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
1199 * g_dbus_proxy_get_interface_info:
1200 * @proxy: A #GDBusProxy
1202 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
1203 * interface that @proxy conforms to.
1205 * See the #GDBusProxy:g-interface-info property for more details.
1207 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
1208 * object, it is owned by @proxy.
1212 GDBusInterfaceInfo *
1213 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
1215 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1216 return proxy->priv->expected_interface;
1220 * g_dbus_proxy_set_interface_info:
1221 * @proxy: A #GDBusProxy
1222 * @info: Minimum interface this proxy conforms to or %NULL to unset.
1224 * Ensure that interactions with @proxy conform to the given
1225 * interface. For example, when completing a method call, if the type
1226 * signature of the message isn't what's expected, the given #GError
1227 * is set. Signals that have a type signature mismatch are simply
1230 * See the #GDBusProxy:g-interface-info property for more details.
1235 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
1236 GDBusInterfaceInfo *info)
1238 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1239 if (proxy->priv->expected_interface != NULL)
1240 g_dbus_interface_info_unref (proxy->priv->expected_interface);
1241 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
1244 /* ---------------------------------------------------------------------------------------------------- */
1247 maybe_split_method_name (const gchar *method_name,
1248 gchar **out_interface_name,
1249 const gchar **out_method_name)
1254 g_assert (out_interface_name != NULL);
1255 g_assert (out_method_name != NULL);
1256 *out_interface_name = NULL;
1257 *out_method_name = NULL;
1259 if (strchr (method_name, '.') != NULL)
1264 p = g_strdup (method_name);
1265 last_dot = strrchr (p, '.');
1268 *out_interface_name = p;
1269 *out_method_name = last_dot + 1;
1279 reply_cb (GDBusConnection *connection,
1283 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1288 value = g_dbus_connection_invoke_method_finish (connection,
1293 g_simple_async_result_set_from_error (simple,
1295 g_error_free (error);
1299 g_simple_async_result_set_op_res_gpointer (simple,
1301 (GDestroyNotify) g_variant_unref);
1304 /* no need to complete in idle since the method GDBusConnection already does */
1305 g_simple_async_result_complete (simple);
1308 static const GDBusMethodInfo *
1309 lookup_method_info_or_warn (GDBusProxy *proxy,
1310 const char *method_name)
1312 const GDBusMethodInfo *info;
1314 if (!proxy->priv->expected_interface)
1317 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1319 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1320 method_name, proxy->priv->expected_interface->name);
1326 validate_method_return (const char *method_name,
1328 const GDBusMethodInfo *expected_method_info,
1331 const gchar *type_string;
1338 if (value == NULL || expected_method_info == NULL)
1341 /* Shouldn't happen... */
1342 if (g_variant_classify (value) != G_VARIANT_CLASS_TUPLE)
1345 type_string = g_variant_get_type_string (value);
1346 signature = _g_dbus_compute_complete_signature (expected_method_info->out_args, TRUE);
1347 if (g_strcmp0 (type_string, signature) != 0)
1351 G_IO_ERROR_INVALID_ARGUMENT,
1352 _("Method `%s' returned signature `%s', but expected `%s'"),
1365 * g_dbus_proxy_invoke_method:
1366 * @proxy: A #GDBusProxy.
1367 * @method_name: Name of method to invoke.
1368 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1369 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1370 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1371 * @cancellable: A #GCancellable or %NULL.
1372 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1373 * care about the result of the method invocation.
1374 * @user_data: The data to pass to @callback.
1376 * Asynchronously invokes the @method_name method on @proxy.
1378 * If @method_name contains any dots, then @name is split into interface and
1379 * method name parts. This allows using @proxy for invoking methods on
1382 * If the #GDBusConnection associated with @proxy is closed then
1383 * the operation will fail with %G_IO_ERROR_CLOSED. If
1384 * @cancellable is canceled, the operation will fail with
1385 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1386 * compatible with the D-Bus protocol, the operation fails with
1387 * %G_IO_ERROR_INVALID_ARGUMENT.
1389 * This is an asynchronous method. When the operation is finished, @callback will be invoked
1390 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
1391 * of the thread you are calling this method from. You can then call
1392 * g_dbus_proxy_invoke_method_finish() to get the result of the operation.
1393 * See g_dbus_proxy_invoke_method_sync() for the
1394 * synchronous version of this method.
1399 g_dbus_proxy_invoke_method (GDBusProxy *proxy,
1400 const gchar *method_name,
1401 GVariant *parameters,
1402 GDBusInvokeMethodFlags flags,
1404 GCancellable *cancellable,
1405 GAsyncReadyCallback callback,
1408 GSimpleAsyncResult *simple;
1410 gchar *split_interface_name;
1411 const gchar *split_method_name;
1412 const GDBusMethodInfo *expected_method_info;
1413 const gchar *target_method_name;
1414 const gchar *target_interface_name;
1416 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1417 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
1418 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
1419 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1421 simple = g_simple_async_result_new (G_OBJECT (proxy),
1424 g_dbus_proxy_invoke_method);
1426 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1427 target_method_name = was_split ? split_method_name : method_name;
1428 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1430 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
1432 /* Just warn here */
1433 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
1435 g_dbus_connection_invoke_method (proxy->priv->connection,
1436 proxy->priv->unique_bus_name,
1437 proxy->priv->object_path,
1438 target_interface_name,
1442 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1444 (GAsyncReadyCallback) reply_cb,
1447 g_free (split_interface_name);
1451 * g_dbus_proxy_invoke_method_finish:
1452 * @proxy: A #GDBusProxy.
1453 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_invoke_method().
1454 * @error: Return location for error or %NULL.
1456 * Finishes an operation started with g_dbus_proxy_invoke_method().
1458 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1459 * return values. Free with g_variant_unref().
1464 g_dbus_proxy_invoke_method_finish (GDBusProxy *proxy,
1468 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1470 const char *method_name;
1471 const GDBusMethodInfo *expected_method_info;
1473 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1474 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1475 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1477 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_invoke_method);
1481 if (g_simple_async_result_propagate_error (simple, error))
1484 value = g_simple_async_result_get_op_res_gpointer (simple);
1485 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
1487 /* We may not have a method name for internally-generated proxy calls like GetAll */
1488 if (value && method_name && proxy->priv->expected_interface)
1490 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1491 if (!validate_method_return (method_name, value, expected_method_info, error))
1493 g_variant_unref (value);
1503 * g_dbus_proxy_invoke_method_sync:
1504 * @proxy: A #GDBusProxy.
1505 * @method_name: Name of method to invoke.
1506 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1507 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1508 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1509 * @cancellable: A #GCancellable or %NULL.
1510 * @error: Return location for error or %NULL.
1512 * Synchronously invokes the @method_name method on @proxy.
1514 * If @method_name contains any dots, then @name is split into interface and
1515 * method name parts. This allows using @proxy for invoking methods on
1518 * If the #GDBusConnection associated with @proxy is disconnected then
1519 * the operation will fail with %G_IO_ERROR_CLOSED. If
1520 * @cancellable is canceled, the operation will fail with
1521 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1522 * compatible with the D-Bus protocol, the operation fails with
1523 * %G_IO_ERROR_INVALID_ARGUMENT.
1525 * The calling thread is blocked until a reply is received. See
1526 * g_dbus_proxy_invoke_method() for the asynchronous version of this
1529 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1530 * return values. Free with g_variant_unref().
1535 g_dbus_proxy_invoke_method_sync (GDBusProxy *proxy,
1536 const gchar *method_name,
1537 GVariant *parameters,
1538 GDBusInvokeMethodFlags flags,
1540 GCancellable *cancellable,
1545 gchar *split_interface_name;
1546 const gchar *split_method_name;
1547 const GDBusMethodInfo *expected_method_info;
1548 const char *target_method_name;
1549 const char *target_interface_name;
1551 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1552 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
1553 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
1554 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
1555 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1557 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1558 target_method_name = was_split ? split_method_name : method_name;
1559 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1561 if (proxy->priv->expected_interface)
1563 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
1564 if (!expected_method_info)
1565 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1566 target_method_name, target_interface_name);
1570 expected_method_info = NULL;
1573 ret = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
1574 proxy->priv->unique_bus_name,
1575 proxy->priv->object_path,
1576 target_interface_name,
1580 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1583 if (!validate_method_return (target_method_name, ret, expected_method_info, error))
1585 g_variant_unref (ret);
1589 g_free (split_interface_name);
1594 /* ---------------------------------------------------------------------------------------------------- */