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;
80 PROP_G_UNIQUE_BUS_NAME,
83 PROP_G_INTERFACE_NAME,
84 PROP_G_DEFAULT_TIMEOUT,
90 PROPERTIES_CHANGED_SIGNAL,
95 static void g_dbus_proxy_constructed (GObject *object);
97 guint signals[LAST_SIGNAL] = {0};
99 static void initable_iface_init (GInitableIface *initable_iface);
100 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
102 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
103 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
104 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
108 g_dbus_proxy_finalize (GObject *object)
110 GDBusProxy *proxy = G_DBUS_PROXY (object);
112 if (proxy->priv->properties_changed_subscriber_id > 0)
114 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
115 proxy->priv->properties_changed_subscriber_id);
118 if (proxy->priv->signals_subscriber_id > 0)
120 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
121 proxy->priv->signals_subscriber_id);
124 g_object_unref (proxy->priv->connection);
125 g_free (proxy->priv->unique_bus_name);
126 g_free (proxy->priv->object_path);
127 g_free (proxy->priv->interface_name);
128 if (proxy->priv->properties != NULL)
129 g_hash_table_unref (proxy->priv->properties);
131 if (proxy->priv->expected_interface != NULL)
132 g_dbus_interface_info_unref (proxy->priv->expected_interface);
134 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize != NULL)
135 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
139 g_dbus_proxy_get_property (GObject *object,
144 GDBusProxy *proxy = G_DBUS_PROXY (object);
148 case PROP_G_CONNECTION:
149 g_value_set_object (value, proxy->priv->connection);
153 g_value_set_flags (value, proxy->priv->flags);
156 case PROP_G_UNIQUE_BUS_NAME:
157 g_value_set_string (value, proxy->priv->unique_bus_name);
160 case PROP_G_OBJECT_PATH:
161 g_value_set_string (value, proxy->priv->object_path);
164 case PROP_G_INTERFACE_NAME:
165 g_value_set_string (value, proxy->priv->interface_name);
168 case PROP_G_DEFAULT_TIMEOUT:
169 g_value_set_int (value, proxy->priv->timeout_msec);
172 case PROP_G_INTERFACE_INFO:
173 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 g_dbus_proxy_set_property (GObject *object,
188 GDBusProxy *proxy = G_DBUS_PROXY (object);
192 case PROP_G_CONNECTION:
193 proxy->priv->connection = g_value_dup_object (value);
197 proxy->priv->flags = g_value_get_flags (value);
200 case PROP_G_UNIQUE_BUS_NAME:
201 proxy->priv->unique_bus_name = g_value_dup_string (value);
204 case PROP_G_OBJECT_PATH:
205 proxy->priv->object_path = g_value_dup_string (value);
208 case PROP_G_INTERFACE_NAME:
209 proxy->priv->interface_name = g_value_dup_string (value);
212 case PROP_G_DEFAULT_TIMEOUT:
213 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
216 case PROP_G_INTERFACE_INFO:
217 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 g_dbus_proxy_class_init (GDBusProxyClass *klass)
229 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231 gobject_class->finalize = g_dbus_proxy_finalize;
232 gobject_class->set_property = g_dbus_proxy_set_property;
233 gobject_class->get_property = g_dbus_proxy_get_property;
234 gobject_class->constructed = g_dbus_proxy_constructed;
236 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
237 * in derived classes */
240 * GDBusProxy:g-interface-info:
242 * Ensure that interactions with this proxy conform to the given
243 * interface. For example, when completing a method call, if the
244 * type signature of the message isn't what's expected, the given
245 * #GError is set. Signals that have a type signature mismatch are
250 g_object_class_install_property (gobject_class,
251 PROP_G_INTERFACE_INFO,
252 g_param_spec_boxed ("g-interface-info",
253 _("Interface Information"),
254 _("Interface Information"),
255 G_TYPE_DBUS_INTERFACE_INFO,
258 G_PARAM_STATIC_NAME |
259 G_PARAM_STATIC_BLURB |
260 G_PARAM_STATIC_NICK));
263 * GDBusProxy:g-connection:
265 * The #GDBusConnection the proxy is for.
269 g_object_class_install_property (gobject_class,
271 g_param_spec_object ("g-connection",
273 _("The connection the proxy is for"),
274 G_TYPE_DBUS_CONNECTION,
277 G_PARAM_CONSTRUCT_ONLY |
278 G_PARAM_STATIC_NAME |
279 G_PARAM_STATIC_BLURB |
280 G_PARAM_STATIC_NICK));
283 * GDBusProxy:g-flags:
285 * Flags from the #GDBusProxyFlags enumeration.
289 g_object_class_install_property (gobject_class,
291 g_param_spec_flags ("g-flags",
293 _("Flags for the proxy"),
294 G_TYPE_DBUS_PROXY_FLAGS,
295 G_DBUS_PROXY_FLAGS_NONE,
298 G_PARAM_CONSTRUCT_ONLY |
299 G_PARAM_STATIC_NAME |
300 G_PARAM_STATIC_BLURB |
301 G_PARAM_STATIC_NICK));
304 * GDBusProxy:g-unique-bus-name:
306 * The unique bus name the proxy is for.
310 g_object_class_install_property (gobject_class,
311 PROP_G_UNIQUE_BUS_NAME,
312 g_param_spec_string ("g-unique-bus-name",
313 _("g-unique-bus-name"),
314 _("The unique bus name the proxy is for"),
318 G_PARAM_CONSTRUCT_ONLY |
319 G_PARAM_STATIC_NAME |
320 G_PARAM_STATIC_BLURB |
321 G_PARAM_STATIC_NICK));
324 * GDBusProxy:g-object-path:
326 * The object path the proxy is for.
330 g_object_class_install_property (gobject_class,
332 g_param_spec_string ("g-object-path",
334 _("The object path the proxy is for"),
338 G_PARAM_CONSTRUCT_ONLY |
339 G_PARAM_STATIC_NAME |
340 G_PARAM_STATIC_BLURB |
341 G_PARAM_STATIC_NICK));
344 * GDBusProxy:g-interface-name:
346 * The D-Bus interface name the proxy is for.
350 g_object_class_install_property (gobject_class,
351 PROP_G_INTERFACE_NAME,
352 g_param_spec_string ("g-interface-name",
353 _("g-interface-name"),
354 _("The D-Bus interface name the proxy is for"),
358 G_PARAM_CONSTRUCT_ONLY |
359 G_PARAM_STATIC_NAME |
360 G_PARAM_STATIC_BLURB |
361 G_PARAM_STATIC_NICK));
364 * GDBusProxy:g-default-timeout:
366 * The timeout to use if -1 (specifying default timeout) is passed
367 * as @timeout_msec in the g_dbus_proxy_invoke_method() and
368 * g_dbus_proxy_invoke_method_sync() functions.
370 * This allows applications to set a proxy-wide timeout for all
371 * remote method invocations on the proxy. If this property is -1,
372 * the default timeout (typically 25 seconds) is used. If set to
373 * %G_MAXINT, then no timeout is used.
377 g_object_class_install_property (gobject_class,
378 PROP_G_DEFAULT_TIMEOUT,
379 g_param_spec_int ("g-default-timeout",
380 _("Default Timeout"),
381 _("Timeout for remote method invocation"),
388 G_PARAM_STATIC_NAME |
389 G_PARAM_STATIC_BLURB |
390 G_PARAM_STATIC_NICK));
393 * GDBusProxy::g-properties-changed:
394 * @proxy: The #GDBusProxy emitting the signal.
395 * @changed_properties: A #GHashTable containing the properties that changed.
397 * Emitted when one or more D-Bus properties on @proxy changes. The cached properties
398 * are already replaced when this signal fires.
402 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
405 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
408 g_cclosure_marshal_VOID__BOXED,
414 * GDBusProxy::g-signal:
415 * @proxy: The #GDBusProxy emitting the signal.
416 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
417 * @signal_name: The name of the signal.
418 * @parameters: A #GVariant tuple with parameters for the signal.
420 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
424 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
427 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
430 _gio_marshal_VOID__STRING_STRING_BOXED,
438 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
442 g_dbus_proxy_init (GDBusProxy *proxy)
444 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
447 /* ---------------------------------------------------------------------------------------------------- */
450 * g_dbus_proxy_get_cached_property_names:
451 * @proxy: A #GDBusProxy.
452 * @error: Return location for error or %NULL.
454 * Gets the names of all cached properties on @proxy.
456 * Returns: A %NULL-terminated array of strings or %NULL if @error is set. Free with
462 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy,
470 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
471 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
475 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
480 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
484 p = g_ptr_array_new ();
486 g_hash_table_iter_init (&iter, proxy->priv->properties);
487 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
489 g_ptr_array_add (p, g_strdup (key));
491 g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
492 g_ptr_array_add (p, NULL);
494 names = (gchar **) g_ptr_array_free (p, FALSE);
501 * g_dbus_proxy_get_cached_property:
502 * @proxy: A #GDBusProxy.
503 * @property_name: Property name.
504 * @error: Return location for error or %NULL.
506 * Looks up the value for a property from the cache. This call does no blocking IO.
508 * Normally you will not need to modify the returned variant since it is updated automatically
509 * in response to <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
510 * D-Bus signals (which also causes #GDBusProxy::g-properties-changed to be emitted).
512 * However, for properties for which said D-Bus signal is not emitted, you
513 * can catch other signals and modify the returned variant accordingly (remember to emit
514 * #GDBusProxy::g-properties-changed yourself).
516 * Returns: A reference to the #GVariant instance that holds the value for @property_name or
517 * %NULL if @error is set. Free the reference with g_variant_unref().
522 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
523 const gchar *property_name,
528 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
529 g_return_val_if_fail (property_name != NULL, NULL);
533 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
538 _("Properties are not available (proxy created with G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)"));
542 value = g_hash_table_lookup (proxy->priv->properties, property_name);
548 _("No property with name %s"),
553 g_variant_ref (value);
560 /* ---------------------------------------------------------------------------------------------------- */
563 on_signal_received (GDBusConnection *connection,
564 const gchar *sender_name,
565 const gchar *object_path,
566 const gchar *interface_name,
567 const gchar *signal_name,
568 GVariant *parameters,
571 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
573 g_signal_emit (proxy,
574 signals[SIGNAL_SIGNAL],
581 /* ---------------------------------------------------------------------------------------------------- */
584 on_properties_changed (GDBusConnection *connection,
585 const gchar *sender_name,
586 const gchar *object_path,
587 const gchar *interface_name,
588 const gchar *signal_name,
589 GVariant *parameters,
592 GDBusProxy *proxy = G_DBUS_PROXY (user_data);
594 const gchar *interface_name_for_signal;
597 GHashTable *changed_properties;
603 /* Ignore this signal if properties are not yet available
605 * (can happen in the window between subscribing to PropertiesChanged() and until
606 * org.freedesktop.DBus.Properties.GetAll() returns)
608 if (!proxy->priv->properties_available)
612 if (strcmp (g_variant_get_type_string (parameters), "(sa{sv})") != 0)
614 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv})'",
615 g_variant_get_type_string (parameters));
619 g_variant_get (parameters,
621 &interface_name_for_signal,
624 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
627 changed_properties = g_hash_table_new_full (g_str_hash,
630 (GDestroyNotify) g_variant_unref);
632 while ((item = g_variant_iter_next_value (iter)))
642 g_hash_table_insert (proxy->priv->properties,
644 value); /* steals value */
646 g_hash_table_insert (changed_properties,
648 g_variant_ref (value));
653 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL], 0, changed_properties);
655 g_hash_table_unref (changed_properties);
659 g_variant_iter_free (iter);
662 /* ---------------------------------------------------------------------------------------------------- */
665 g_dbus_proxy_constructed (GObject *object)
667 if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed != NULL)
668 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed (object);
671 /* ---------------------------------------------------------------------------------------------------- */
674 subscribe_to_signals (GDBusProxy *proxy)
676 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
678 /* subscribe to PropertiesChanged() */
679 proxy->priv->properties_changed_subscriber_id =
680 g_dbus_connection_signal_subscribe (proxy->priv->connection,
681 proxy->priv->unique_bus_name,
682 "org.freedesktop.DBus.Properties",
684 proxy->priv->object_path,
685 proxy->priv->interface_name,
686 on_properties_changed,
691 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
693 /* subscribe to all signals for the object */
694 proxy->priv->signals_subscriber_id =
695 g_dbus_connection_signal_subscribe (proxy->priv->connection,
696 proxy->priv->unique_bus_name,
697 proxy->priv->interface_name,
699 proxy->priv->object_path,
707 /* ---------------------------------------------------------------------------------------------------- */
710 process_get_all_reply (GDBusProxy *proxy,
716 if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
718 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
719 g_variant_get_type_string (result));
723 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
726 (GDestroyNotify) g_variant_unref);
728 g_variant_iter_init (&iter, g_variant_get_child_value (result, 0));
729 while ((item = g_variant_iter_next_value (&iter)) != NULL)
738 //g_print ("got %s -> %s\n", key, g_variant_markup_print (value, FALSE, 0, 0));
740 g_hash_table_insert (proxy->priv->properties,
742 value); /* steals value */
749 initable_init (GInitable *initable,
750 GCancellable *cancellable,
753 GDBusProxy *proxy = G_DBUS_PROXY (initable);
759 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
761 /* load all properties synchronously */
762 result = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
763 proxy->priv->unique_bus_name,
764 proxy->priv->object_path,
765 "org.freedesktop.DBus.Properties",
767 g_variant_new ("(s)", proxy->priv->interface_name),
768 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
775 process_get_all_reply (proxy, result);
777 g_variant_unref (result);
780 subscribe_to_signals (proxy);
789 initable_iface_init (GInitableIface *initable_iface)
791 initable_iface->init = initable_init;
794 /* ---------------------------------------------------------------------------------------------------- */
797 get_all_cb (GDBusConnection *connection,
801 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
806 result = g_dbus_connection_invoke_method_finish (connection,
811 g_simple_async_result_set_from_error (simple, error);
812 g_error_free (error);
816 g_simple_async_result_set_op_res_gpointer (simple,
818 (GDestroyNotify) g_variant_unref);
821 g_simple_async_result_complete_in_idle (simple);
822 g_object_unref (simple);
826 async_initable_init_async (GAsyncInitable *initable,
828 GCancellable *cancellable,
829 GAsyncReadyCallback callback,
832 GDBusProxy *proxy = G_DBUS_PROXY (initable);
833 GSimpleAsyncResult *simple;
835 simple = g_simple_async_result_new (G_OBJECT (proxy),
840 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
842 /* load all properties asynchronously */
843 g_dbus_connection_invoke_method (proxy->priv->connection,
844 proxy->priv->unique_bus_name,
845 proxy->priv->object_path,
846 "org.freedesktop.DBus.Properties",
848 g_variant_new ("(s)", proxy->priv->interface_name),
849 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
852 (GAsyncReadyCallback) get_all_cb,
857 g_simple_async_result_complete_in_idle (simple);
858 g_object_unref (simple);
863 async_initable_init_finish (GAsyncInitable *initable,
867 GDBusProxy *proxy = G_DBUS_PROXY (initable);
868 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
874 result = g_simple_async_result_get_op_res_gpointer (simple);
877 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
879 g_simple_async_result_propagate_error (simple, error);
885 process_get_all_reply (proxy, result);
888 subscribe_to_signals (proxy);
897 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
899 async_initable_iface->init_async = async_initable_init_async;
900 async_initable_iface->init_finish = async_initable_init_finish;
903 /* ---------------------------------------------------------------------------------------------------- */
907 * @connection: A #GDBusConnection.
908 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
909 * @flags: Flags used when constructing the proxy.
910 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
911 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
912 * @object_path: An object path.
913 * @interface_name: A D-Bus interface name.
914 * @cancellable: A #GCancellable or %NULL.
915 * @callback: Callback function to invoke when the proxy is ready.
916 * @user_data: User data to pass to @callback.
918 * Creates a proxy for accessing @interface_name on the remote object at @object_path
919 * owned by @unique_bus_name at @connection and asynchronously loads D-Bus properties unless the
920 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to the
921 * #GDBusProxy::g-properties-changed signal to get notified about property changes.
923 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
924 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
925 * to handle signals from the remote object.
927 * This is a failable asynchronous constructor - when the proxy is
928 * ready, @callback will be invoked and you can use
929 * g_dbus_proxy_new_finish() to get the result.
931 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
936 g_dbus_proxy_new (GDBusConnection *connection,
938 GDBusProxyFlags flags,
939 GDBusInterfaceInfo *info,
940 const gchar *unique_bus_name,
941 const gchar *object_path,
942 const gchar *interface_name,
943 GCancellable *cancellable,
944 GAsyncReadyCallback callback,
947 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
948 g_return_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY));
949 g_return_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
950 g_dbus_is_unique_name (unique_bus_name));
951 g_return_if_fail (g_variant_is_object_path (object_path));
952 g_return_if_fail (g_dbus_is_interface_name (interface_name));
954 g_async_initable_new_async (object_type,
960 "g-interface-info", info,
961 "g-unique-bus-name", unique_bus_name,
962 "g-connection", connection,
963 "g-object-path", object_path,
964 "g-interface-name", interface_name,
969 * g_dbus_proxy_new_finish:
970 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
971 * @error: Return location for error or %NULL.
973 * Finishes creating a #GDBusProxy.
975 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
980 g_dbus_proxy_new_finish (GAsyncResult *res,
984 GObject *source_object;
986 source_object = g_async_result_get_source_object (res);
987 g_assert (source_object != NULL);
989 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
992 g_object_unref (source_object);
995 return G_DBUS_PROXY (object);
1001 /* ---------------------------------------------------------------------------------------------------- */
1004 * g_dbus_proxy_new_sync:
1005 * @connection: A #GDBusConnection.
1006 * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
1007 * @flags: Flags used when constructing the proxy.
1008 * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1009 * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
1010 * @object_path: An object path.
1011 * @interface_name: A D-Bus interface name.
1012 * @cancellable: A #GCancellable or %NULL.
1013 * @error: Return location for error or %NULL.
1015 * Creates a proxy for accessing @interface_name on the remote object at @object_path
1016 * owned by @unique_bus_name at @connection and synchronously loads D-Bus properties unless the
1017 * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1019 * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1020 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1021 * to handle signals from the remote object.
1023 * This is a synchronous failable constructor. See g_dbus_proxy_new()
1024 * and g_dbus_proxy_new_finish() for the asynchronous version.
1026 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1031 g_dbus_proxy_new_sync (GDBusConnection *connection,
1033 GDBusProxyFlags flags,
1034 GDBusInterfaceInfo *info,
1035 const gchar *unique_bus_name,
1036 const gchar *object_path,
1037 const gchar *interface_name,
1038 GCancellable *cancellable,
1041 GInitable *initable;
1043 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1044 g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY), NULL);
1045 g_return_val_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1046 g_dbus_is_unique_name (unique_bus_name), NULL);
1047 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1048 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1050 initable = g_initable_new (object_type,
1054 "g-interface-info", info,
1055 "g-unique-bus-name", unique_bus_name,
1056 "g-connection", connection,
1057 "g-object-path", object_path,
1058 "g-interface-name", interface_name,
1060 if (initable != NULL)
1061 return G_DBUS_PROXY (initable);
1066 /* ---------------------------------------------------------------------------------------------------- */
1069 * g_dbus_proxy_get_connection:
1070 * @proxy: A #GDBusProxy.
1072 * Gets the connection @proxy is for.
1074 * Returns: A #GDBusConnection owned by @proxy. Do not free.
1079 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1081 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1082 return proxy->priv->connection;
1086 * g_dbus_proxy_get_flags:
1087 * @proxy: A #GDBusProxy.
1089 * Gets the flags that @proxy was constructed with.
1091 * Returns: Flags from the #GDBusProxyFlags enumeration.
1096 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1098 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1099 return proxy->priv->flags;
1103 * g_dbus_proxy_get_unique_bus_name:
1104 * @proxy: A #GDBusProxy.
1106 * Gets the unique bus name @proxy is for.
1108 * Returns: A string owned by @proxy. Do not free.
1113 g_dbus_proxy_get_unique_bus_name (GDBusProxy *proxy)
1115 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1116 return proxy->priv->unique_bus_name;
1120 * g_dbus_proxy_get_object_path:
1121 * @proxy: A #GDBusProxy.
1123 * Gets the object path @proxy is for.
1125 * Returns: A string owned by @proxy. Do not free.
1130 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1132 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1133 return proxy->priv->object_path;
1137 * g_dbus_proxy_get_interface_name:
1138 * @proxy: A #GDBusProxy.
1140 * Gets the D-Bus interface name @proxy is for.
1142 * Returns: A string owned by @proxy. Do not free.
1147 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1149 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1150 return proxy->priv->interface_name;
1154 * g_dbus_proxy_get_default_timeout:
1155 * @proxy: A #GDBusProxy.
1157 * Gets the timeout to use if -1 (specifying default timeout) is
1158 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1159 * g_dbus_proxy_invoke_method_sync() functions.
1161 * See the #GDBusProxy:g-default-timeout property for more details.
1163 * Returns: Timeout to use for @proxy.
1168 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1170 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1171 return proxy->priv->timeout_msec;
1175 * g_dbus_proxy_set_default_timeout:
1176 * @proxy: A #GDBusProxy.
1177 * @timeout_msec: Timeout in milliseconds.
1179 * Sets the timeout to use if -1 (specifying default timeout) is
1180 * passed as @timeout_msec in the g_dbus_proxy_invoke_method() and
1181 * g_dbus_proxy_invoke_method_sync() functions.
1183 * See the #GDBusProxy:g-default-timeout property for more details.
1188 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1191 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1192 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1194 /* TODO: locking? */
1195 if (proxy->priv->timeout_msec != timeout_msec)
1197 proxy->priv->timeout_msec = timeout_msec;
1198 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
1203 * g_dbus_proxy_get_interface_info:
1204 * @proxy: A #GDBusProxy
1206 * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
1207 * interface that @proxy conforms to.
1209 * See the #GDBusProxy:g-interface-info property for more details.
1211 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
1212 * object, it is owned by @proxy.
1216 GDBusInterfaceInfo *
1217 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
1219 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1220 return proxy->priv->expected_interface;
1224 * g_dbus_proxy_set_interface_info:
1225 * @proxy: A #GDBusProxy
1226 * @info: Minimum interface this proxy conforms to or %NULL to unset.
1228 * Ensure that interactions with @proxy conform to the given
1229 * interface. For example, when completing a method call, if the type
1230 * signature of the message isn't what's expected, the given #GError
1231 * is set. Signals that have a type signature mismatch are simply
1234 * See the #GDBusProxy:g-interface-info property for more details.
1239 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
1240 GDBusInterfaceInfo *info)
1242 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1243 if (proxy->priv->expected_interface != NULL)
1244 g_dbus_interface_info_unref (proxy->priv->expected_interface);
1245 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
1248 /* ---------------------------------------------------------------------------------------------------- */
1251 maybe_split_method_name (const gchar *method_name,
1252 gchar **out_interface_name,
1253 const gchar **out_method_name)
1258 g_assert (out_interface_name != NULL);
1259 g_assert (out_method_name != NULL);
1260 *out_interface_name = NULL;
1261 *out_method_name = NULL;
1263 if (strchr (method_name, '.') != NULL)
1268 p = g_strdup (method_name);
1269 last_dot = strrchr (p, '.');
1272 *out_interface_name = p;
1273 *out_method_name = last_dot + 1;
1283 reply_cb (GDBusConnection *connection,
1287 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1292 value = g_dbus_connection_invoke_method_finish (connection,
1297 g_simple_async_result_set_from_error (simple,
1299 g_error_free (error);
1303 g_simple_async_result_set_op_res_gpointer (simple,
1305 (GDestroyNotify) g_variant_unref);
1308 /* no need to complete in idle since the method GDBusConnection already does */
1309 g_simple_async_result_complete (simple);
1312 static const GDBusMethodInfo *
1313 lookup_method_info_or_warn (GDBusProxy *proxy,
1314 const char *method_name)
1316 const GDBusMethodInfo *info;
1318 if (!proxy->priv->expected_interface)
1321 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1323 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1324 method_name, proxy->priv->expected_interface->name);
1330 validate_method_return (const char *method_name,
1332 const GDBusMethodInfo *expected_method_info,
1335 const gchar *type_string;
1342 if (value == NULL || expected_method_info == NULL)
1345 /* Shouldn't happen... */
1346 if (g_variant_classify (value) != G_VARIANT_CLASS_TUPLE)
1349 type_string = g_variant_get_type_string (value);
1350 signature = _g_dbus_compute_complete_signature (expected_method_info->out_args, TRUE);
1351 if (g_strcmp0 (type_string, signature) != 0)
1355 G_IO_ERROR_INVALID_ARGUMENT,
1356 _("Method `%s' returned signature `%s', but expected `%s'"),
1369 * g_dbus_proxy_invoke_method:
1370 * @proxy: A #GDBusProxy.
1371 * @method_name: Name of method to invoke.
1372 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1373 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1374 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1375 * @cancellable: A #GCancellable or %NULL.
1376 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1377 * care about the result of the method invocation.
1378 * @user_data: The data to pass to @callback.
1380 * Asynchronously invokes the @method_name method on @proxy.
1382 * If @method_name contains any dots, then @name is split into interface and
1383 * method name parts. This allows using @proxy for invoking methods on
1386 * If the #GDBusConnection associated with @proxy is closed then
1387 * the operation will fail with %G_IO_ERROR_CLOSED. If
1388 * @cancellable is canceled, the operation will fail with
1389 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1390 * compatible with the D-Bus protocol, the operation fails with
1391 * %G_IO_ERROR_INVALID_ARGUMENT.
1393 * This is an asynchronous method. When the operation is finished, @callback will be invoked
1394 * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
1395 * of the thread you are calling this method from. You can then call
1396 * g_dbus_proxy_invoke_method_finish() to get the result of the operation.
1397 * See g_dbus_proxy_invoke_method_sync() for the
1398 * synchronous version of this method.
1403 g_dbus_proxy_invoke_method (GDBusProxy *proxy,
1404 const gchar *method_name,
1405 GVariant *parameters,
1406 GDBusInvokeMethodFlags flags,
1408 GCancellable *cancellable,
1409 GAsyncReadyCallback callback,
1412 GSimpleAsyncResult *simple;
1414 gchar *split_interface_name;
1415 const gchar *split_method_name;
1416 const GDBusMethodInfo *expected_method_info;
1417 const gchar *target_method_name;
1418 const gchar *target_interface_name;
1420 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1421 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
1422 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
1423 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1425 simple = g_simple_async_result_new (G_OBJECT (proxy),
1428 g_dbus_proxy_invoke_method);
1430 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1431 target_method_name = was_split ? split_method_name : method_name;
1432 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1434 g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
1436 /* Just warn here */
1437 expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
1439 g_dbus_connection_invoke_method (proxy->priv->connection,
1440 proxy->priv->unique_bus_name,
1441 proxy->priv->object_path,
1442 target_interface_name,
1446 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1448 (GAsyncReadyCallback) reply_cb,
1451 g_free (split_interface_name);
1455 * g_dbus_proxy_invoke_method_finish:
1456 * @proxy: A #GDBusProxy.
1457 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_invoke_method().
1458 * @error: Return location for error or %NULL.
1460 * Finishes an operation started with g_dbus_proxy_invoke_method().
1462 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1463 * return values. Free with g_variant_unref().
1468 g_dbus_proxy_invoke_method_finish (GDBusProxy *proxy,
1472 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1474 const char *method_name;
1475 const GDBusMethodInfo *expected_method_info;
1477 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1478 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1479 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1481 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_invoke_method);
1485 if (g_simple_async_result_propagate_error (simple, error))
1488 value = g_simple_async_result_get_op_res_gpointer (simple);
1489 method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
1491 /* We may not have a method name for internally-generated proxy calls like GetAll */
1492 if (value && method_name && proxy->priv->expected_interface)
1494 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1495 if (!validate_method_return (method_name, value, expected_method_info, error))
1497 g_variant_unref (value);
1507 * g_dbus_proxy_invoke_method_sync:
1508 * @proxy: A #GDBusProxy.
1509 * @method_name: Name of method to invoke.
1510 * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1511 * @flags: Flags from the #GDBusInvokeMethodFlags enumeration.
1512 * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1513 * @cancellable: A #GCancellable or %NULL.
1514 * @error: Return location for error or %NULL.
1516 * Synchronously invokes the @method_name method on @proxy.
1518 * If @method_name contains any dots, then @name is split into interface and
1519 * method name parts. This allows using @proxy for invoking methods on
1522 * If the #GDBusConnection associated with @proxy is disconnected then
1523 * the operation will fail with %G_IO_ERROR_CLOSED. If
1524 * @cancellable is canceled, the operation will fail with
1525 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1526 * compatible with the D-Bus protocol, the operation fails with
1527 * %G_IO_ERROR_INVALID_ARGUMENT.
1529 * The calling thread is blocked until a reply is received. See
1530 * g_dbus_proxy_invoke_method() for the asynchronous version of this
1533 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1534 * return values. Free with g_variant_unref().
1539 g_dbus_proxy_invoke_method_sync (GDBusProxy *proxy,
1540 const gchar *method_name,
1541 GVariant *parameters,
1542 GDBusInvokeMethodFlags flags,
1544 GCancellable *cancellable,
1549 gchar *split_interface_name;
1550 const gchar *split_method_name;
1551 const GDBusMethodInfo *expected_method_info;
1552 const char *target_method_name;
1553 const char *target_interface_name;
1555 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1556 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
1557 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
1558 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
1559 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1561 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1562 target_method_name = was_split ? split_method_name : method_name;
1563 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1565 if (proxy->priv->expected_interface)
1567 expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
1568 if (!expected_method_info)
1569 g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1570 target_method_name, target_interface_name);
1574 expected_method_info = NULL;
1577 ret = g_dbus_connection_invoke_method_sync (proxy->priv->connection,
1578 proxy->priv->unique_bus_name,
1579 proxy->priv->object_path,
1580 target_interface_name,
1584 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1587 if (!validate_method_return (target_method_name, ret, expected_method_info, error))
1589 g_variant_unref (ret);
1593 g_free (split_interface_name);
1598 /* ---------------------------------------------------------------------------------------------------- */
1600 #define __G_DBUS_PROXY_C__
1601 #include "gioaliasdef.c"