Merge branch 'gdbus-merge'
[platform/upstream/glib.git] / gio / gdbusproxy.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <gobject/gvaluecollector.h>
29
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"
39 #include "gioerror.h"
40 #include "gasyncresult.h"
41 #include "gsimpleasyncresult.h"
42
43 #include "glibintl.h"
44 #include "gioalias.h"
45
46 /**
47  * SECTION:gdbusproxy
48  * @short_description: Base class for proxies
49  * @include: gio/gio.h
50  *
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.
56  *
57  * By default, #GDBusProxy will cache all properties (and listen for
58  * their changes) of the remote object, and proxy all signals that gets
59  * emitted. This behaviour can be changed by passing suitable
60  * #GDBusProxyFlags when the proxy is created.
61  *
62  * The generic #GDBusProxy::g-properties-changed and #GDBusProxy::g-signal
63  * signals are not very convenient to work with. Therefore, the recommended
64  * way of working with proxies is to subclass #GDBusProxy, and have
65  * more natural properties and signals in your derived class. The
66  * @interface_type argument of g_bus_watch_proxy() lets you obtain
67  * instances of your derived class when using the high-level API.
68  *
69  * See <xref linkend="gdbus-example-proxy-subclass"/> for an example.
70  */
71
72 struct _GDBusProxyPrivate
73 {
74   GDBusConnection *connection;
75   GDBusProxyFlags flags;
76   gchar *unique_bus_name;
77   gchar *object_path;
78   gchar *interface_name;
79   gint timeout_msec;
80
81   /* gchar* -> GVariant* */
82   GHashTable *properties;
83
84   GDBusInterfaceInfo *expected_interface;
85
86   guint properties_changed_subscriber_id;
87   guint signals_subscriber_id;
88
89   gboolean initialized;
90 };
91
92 enum
93 {
94   PROP_0,
95   PROP_G_CONNECTION,
96   PROP_G_UNIQUE_BUS_NAME,
97   PROP_G_FLAGS,
98   PROP_G_OBJECT_PATH,
99   PROP_G_INTERFACE_NAME,
100   PROP_G_DEFAULT_TIMEOUT,
101   PROP_G_INTERFACE_INFO
102 };
103
104 enum
105 {
106   PROPERTIES_CHANGED_SIGNAL,
107   SIGNAL_SIGNAL,
108   LAST_SIGNAL,
109 };
110
111 static void g_dbus_proxy_constructed (GObject *object);
112
113 guint signals[LAST_SIGNAL] = {0};
114
115 static void initable_iface_init       (GInitableIface *initable_iface);
116 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
117
118 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
119                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
120                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
121                          );
122
123 static void
124 g_dbus_proxy_finalize (GObject *object)
125 {
126   GDBusProxy *proxy = G_DBUS_PROXY (object);
127
128   if (proxy->priv->properties_changed_subscriber_id > 0)
129     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
130                                           proxy->priv->properties_changed_subscriber_id);
131
132   if (proxy->priv->signals_subscriber_id > 0)
133     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
134                                           proxy->priv->signals_subscriber_id);
135
136   g_object_unref (proxy->priv->connection);
137   g_free (proxy->priv->unique_bus_name);
138   g_free (proxy->priv->object_path);
139   g_free (proxy->priv->interface_name);
140   if (proxy->priv->properties != NULL)
141     g_hash_table_unref (proxy->priv->properties);
142
143   if (proxy->priv->expected_interface != NULL)
144     g_dbus_interface_info_unref (proxy->priv->expected_interface);
145
146   G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
147 }
148
149 static void
150 g_dbus_proxy_get_property (GObject    *object,
151                            guint       prop_id,
152                            GValue     *value,
153                            GParamSpec *pspec)
154 {
155   GDBusProxy *proxy = G_DBUS_PROXY (object);
156
157   switch (prop_id)
158     {
159     case PROP_G_CONNECTION:
160       g_value_set_object (value, proxy->priv->connection);
161       break;
162
163     case PROP_G_FLAGS:
164       g_value_set_flags (value, proxy->priv->flags);
165       break;
166
167     case PROP_G_UNIQUE_BUS_NAME:
168       g_value_set_string (value, proxy->priv->unique_bus_name);
169       break;
170
171     case PROP_G_OBJECT_PATH:
172       g_value_set_string (value, proxy->priv->object_path);
173       break;
174
175     case PROP_G_INTERFACE_NAME:
176       g_value_set_string (value, proxy->priv->interface_name);
177       break;
178
179     case PROP_G_DEFAULT_TIMEOUT:
180       g_value_set_int (value, proxy->priv->timeout_msec);
181       break;
182
183     case PROP_G_INTERFACE_INFO:
184       g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
185       break;
186
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189       break;
190     }
191 }
192
193 static void
194 g_dbus_proxy_set_property (GObject      *object,
195                            guint         prop_id,
196                            const GValue *value,
197                            GParamSpec   *pspec)
198 {
199   GDBusProxy *proxy = G_DBUS_PROXY (object);
200
201   switch (prop_id)
202     {
203     case PROP_G_CONNECTION:
204       proxy->priv->connection = g_value_dup_object (value);
205       break;
206
207     case PROP_G_FLAGS:
208       proxy->priv->flags = g_value_get_flags (value);
209       break;
210
211     case PROP_G_UNIQUE_BUS_NAME:
212       proxy->priv->unique_bus_name = g_value_dup_string (value);
213       break;
214
215     case PROP_G_OBJECT_PATH:
216       proxy->priv->object_path = g_value_dup_string (value);
217       break;
218
219     case PROP_G_INTERFACE_NAME:
220       proxy->priv->interface_name = g_value_dup_string (value);
221       break;
222
223     case PROP_G_DEFAULT_TIMEOUT:
224       g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
225       break;
226
227     case PROP_G_INTERFACE_INFO:
228       g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
229       break;
230
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234     }
235 }
236
237 static void
238 g_dbus_proxy_class_init (GDBusProxyClass *klass)
239 {
240   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
241
242   gobject_class->finalize     = g_dbus_proxy_finalize;
243   gobject_class->set_property = g_dbus_proxy_set_property;
244   gobject_class->get_property = g_dbus_proxy_get_property;
245   gobject_class->constructed  = g_dbus_proxy_constructed;
246
247   /* Note that all property names are prefixed to avoid collisions with D-Bus property names
248    * in derived classes */
249
250   /**
251    * GDBusProxy:g-interface-info:
252    *
253    * Ensure that interactions with this proxy conform to the given
254    * interface.  For example, when completing a method call, if the
255    * type signature of the message isn't what's expected, the given
256    * #GError is set.  Signals that have a type signature mismatch are
257    * simply dropped.
258    *
259    * Since: 2.26
260    */
261   g_object_class_install_property (gobject_class,
262                                    PROP_G_INTERFACE_INFO,
263                                    g_param_spec_boxed ("g-interface-info",
264                                                        P_("Interface Information"),
265                                                        P_("Interface Information"),
266                                                        G_TYPE_DBUS_INTERFACE_INFO,
267                                                        G_PARAM_READABLE |
268                                                        G_PARAM_WRITABLE |
269                                                        G_PARAM_STATIC_NAME |
270                                                        G_PARAM_STATIC_BLURB |
271                                                        G_PARAM_STATIC_NICK));
272
273   /**
274    * GDBusProxy:g-connection:
275    *
276    * The #GDBusConnection the proxy is for.
277    *
278    * Since: 2.26
279    */
280   g_object_class_install_property (gobject_class,
281                                    PROP_G_CONNECTION,
282                                    g_param_spec_object ("g-connection",
283                                                         P_("g-connection"),
284                                                         P_("The connection the proxy is for"),
285                                                         G_TYPE_DBUS_CONNECTION,
286                                                         G_PARAM_READABLE |
287                                                         G_PARAM_WRITABLE |
288                                                         G_PARAM_CONSTRUCT_ONLY |
289                                                         G_PARAM_STATIC_NAME |
290                                                         G_PARAM_STATIC_BLURB |
291                                                         G_PARAM_STATIC_NICK));
292
293   /**
294    * GDBusProxy:g-flags:
295    *
296    * Flags from the #GDBusProxyFlags enumeration.
297    *
298    * Since: 2.26
299    */
300   g_object_class_install_property (gobject_class,
301                                    PROP_G_FLAGS,
302                                    g_param_spec_flags ("g-flags",
303                                                        P_("g-flags"),
304                                                        P_("Flags for the proxy"),
305                                                        G_TYPE_DBUS_PROXY_FLAGS,
306                                                        G_DBUS_PROXY_FLAGS_NONE,
307                                                        G_PARAM_READABLE |
308                                                        G_PARAM_WRITABLE |
309                                                        G_PARAM_CONSTRUCT_ONLY |
310                                                        G_PARAM_STATIC_NAME |
311                                                        G_PARAM_STATIC_BLURB |
312                                                        G_PARAM_STATIC_NICK));
313
314   /**
315    * GDBusProxy:g-unique-bus-name:
316    *
317    * The unique bus name the proxy is for.
318    *
319    * Since: 2.26
320    */
321   g_object_class_install_property (gobject_class,
322                                    PROP_G_UNIQUE_BUS_NAME,
323                                    g_param_spec_string ("g-unique-bus-name",
324                                                         P_("g-unique-bus-name"),
325                                                         P_("The unique bus name the proxy is for"),
326                                                         NULL,
327                                                         G_PARAM_READABLE |
328                                                         G_PARAM_WRITABLE |
329                                                         G_PARAM_CONSTRUCT_ONLY |
330                                                         G_PARAM_STATIC_NAME |
331                                                         G_PARAM_STATIC_BLURB |
332                                                         G_PARAM_STATIC_NICK));
333
334   /**
335    * GDBusProxy:g-object-path:
336    *
337    * The object path the proxy is for.
338    *
339    * Since: 2.26
340    */
341   g_object_class_install_property (gobject_class,
342                                    PROP_G_OBJECT_PATH,
343                                    g_param_spec_string ("g-object-path",
344                                                         P_("g-object-path"),
345                                                         P_("The object path the proxy is for"),
346                                                         NULL,
347                                                         G_PARAM_READABLE |
348                                                         G_PARAM_WRITABLE |
349                                                         G_PARAM_CONSTRUCT_ONLY |
350                                                         G_PARAM_STATIC_NAME |
351                                                         G_PARAM_STATIC_BLURB |
352                                                         G_PARAM_STATIC_NICK));
353
354   /**
355    * GDBusProxy:g-interface-name:
356    *
357    * The D-Bus interface name the proxy is for.
358    *
359    * Since: 2.26
360    */
361   g_object_class_install_property (gobject_class,
362                                    PROP_G_INTERFACE_NAME,
363                                    g_param_spec_string ("g-interface-name",
364                                                         P_("g-interface-name"),
365                                                         P_("The D-Bus interface name the proxy is for"),
366                                                         NULL,
367                                                         G_PARAM_READABLE |
368                                                         G_PARAM_WRITABLE |
369                                                         G_PARAM_CONSTRUCT_ONLY |
370                                                         G_PARAM_STATIC_NAME |
371                                                         G_PARAM_STATIC_BLURB |
372                                                         G_PARAM_STATIC_NICK));
373
374   /**
375    * GDBusProxy:g-default-timeout:
376    *
377    * The timeout to use if -1 (specifying default timeout) is passed
378    * as @timeout_msec in the g_dbus_proxy_call() and
379    * g_dbus_proxy_call_sync() functions.
380    *
381    * This allows applications to set a proxy-wide timeout for all
382    * remote method invocations on the proxy. If this property is -1,
383    * the default timeout (typically 25 seconds) is used. If set to
384    * %G_MAXINT, then no timeout is used.
385    *
386    * Since: 2.26
387    */
388   g_object_class_install_property (gobject_class,
389                                    PROP_G_DEFAULT_TIMEOUT,
390                                    g_param_spec_int ("g-default-timeout",
391                                                      P_("Default Timeout"),
392                                                      P_("Timeout for remote method invocation"),
393                                                      -1,
394                                                      G_MAXINT,
395                                                      -1,
396                                                      G_PARAM_READABLE |
397                                                      G_PARAM_WRITABLE |
398                                                      G_PARAM_CONSTRUCT |
399                                                      G_PARAM_STATIC_NAME |
400                                                      G_PARAM_STATIC_BLURB |
401                                                      G_PARAM_STATIC_NICK));
402
403   /**
404    * GDBusProxy::g-properties-changed:
405    * @proxy: The #GDBusProxy emitting the signal.
406    * @changed_properties: A #GVariant containing the properties that changed
407    * @invalidated_properties: A %NULL terminated array of properties that was invalidated
408    *
409    * Emitted when one or more D-Bus properties on @proxy changes. The
410    * local cache has already been updated when this signal fires. Note
411    * that both @changed_properties and @invalidated_properties are
412    * guaranteed to never be %NULL (either may be empty though).
413    *
414    * This signal corresponds to the
415    * <literal>PropertiesChanged</literal> D-Bus signal on the
416    * <literal>org.freedesktop.DBus.Properties</literal> interface.
417    *
418    * Since: 2.26
419    */
420   signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
421                                                      G_TYPE_DBUS_PROXY,
422                                                      G_SIGNAL_RUN_LAST,
423                                                      G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
424                                                      NULL,
425                                                      NULL,
426                                                      _gio_marshal_VOID__BOXED_BOXED,
427                                                      G_TYPE_NONE,
428                                                      2,
429                                                      G_TYPE_VARIANT,
430                                                      G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
431
432   /**
433    * GDBusProxy::g-signal:
434    * @proxy: The #GDBusProxy emitting the signal.
435    * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
436    * @signal_name: The name of the signal.
437    * @parameters: A #GVariant tuple with parameters for the signal.
438    *
439    * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
440    *
441    * Since: 2.26
442    */
443   signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
444                                          G_TYPE_DBUS_PROXY,
445                                          G_SIGNAL_RUN_LAST,
446                                          G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
447                                          NULL,
448                                          NULL,
449                                          _gio_marshal_VOID__STRING_STRING_BOXED,
450                                          G_TYPE_NONE,
451                                          3,
452                                          G_TYPE_STRING,
453                                          G_TYPE_STRING,
454                                          G_TYPE_VARIANT);
455
456
457   g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
458 }
459
460 static void
461 g_dbus_proxy_init (GDBusProxy *proxy)
462 {
463   proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
464   proxy->priv->properties = g_hash_table_new_full (g_str_hash,
465                                                    g_str_equal,
466                                                    g_free,
467                                                    (GDestroyNotify) g_variant_unref);
468 }
469
470 /* ---------------------------------------------------------------------------------------------------- */
471
472 /**
473  * g_dbus_proxy_get_cached_property_names:
474  * @proxy: A #GDBusProxy.
475  *
476  * Gets the names of all cached properties on @proxy.
477  *
478  * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
479  * no cached properties. Free the returned array with g_strfreev().
480  *
481  * Since: 2.26
482  */
483 gchar **
484 g_dbus_proxy_get_cached_property_names (GDBusProxy  *proxy)
485 {
486   gchar **names;
487   GPtrArray *p;
488   GHashTableIter iter;
489   const gchar *key;
490
491   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
492
493   names = NULL;
494   if (g_hash_table_size (proxy->priv->properties) == 0)
495     goto out;
496
497   p = g_ptr_array_new ();
498
499   g_hash_table_iter_init (&iter, proxy->priv->properties);
500   while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
501     g_ptr_array_add (p, g_strdup (key));
502   g_ptr_array_sort (p, (GCompareFunc) g_strcmp0);
503   g_ptr_array_add (p, NULL);
504
505   names = (gchar **) g_ptr_array_free (p, FALSE);
506
507  out:
508   return names;
509 }
510
511 static const GDBusPropertyInfo *
512 lookup_property_info_or_warn (GDBusProxy  *proxy,
513                               const gchar *property_name)
514 {
515   const GDBusPropertyInfo *info;
516
517   if (proxy->priv->expected_interface == NULL)
518     return NULL;
519
520   info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
521   if (info == NULL)
522     {
523       g_warning ("Trying to lookup property %s which isn't in expected interface %s",
524                  property_name,
525                  proxy->priv->expected_interface->name);
526     }
527
528   return info;
529 }
530
531 /**
532  * g_dbus_proxy_get_cached_property:
533  * @proxy: A #GDBusProxy.
534  * @property_name: Property name.
535  *
536  * Looks up the value for a property from the cache. This call does no
537  * blocking IO.
538  *
539  * If @proxy has an expected interface (see
540  * #GDBusProxy:g-interface-info), then @property_name (for existence)
541  * is checked against it.
542  *
543  * Returns: A reference to the #GVariant instance that holds the value
544  * for @property_name or %NULL if the value is not in the cache. The
545  * returned reference must be freed with g_variant_unref().
546  *
547  * Since: 2.26
548  */
549 GVariant *
550 g_dbus_proxy_get_cached_property (GDBusProxy   *proxy,
551                                   const gchar  *property_name)
552 {
553   GVariant *value;
554
555   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
556   g_return_val_if_fail (property_name != NULL, NULL);
557
558   value = g_hash_table_lookup (proxy->priv->properties, property_name);
559   if (value == NULL)
560     {
561       const GDBusPropertyInfo *info;
562       info = lookup_property_info_or_warn (proxy, property_name);
563       /* no difference */
564       goto out;
565     }
566
567   g_variant_ref (value);
568
569  out:
570   return value;
571 }
572
573 /**
574  * g_dbus_proxy_set_cached_property:
575  * @proxy: A #GDBusProxy
576  * @property_name: Property name.
577  * @value: Value for the property or %NULL to remove it from the cache.
578  *
579  * If @value is not %NULL, sets the cached value for the property with
580  * name @property_name to the value in @value.
581  *
582  * If @value is %NULL, then the cached value is removed from the
583  * property cache.
584  *
585  * If @proxy has an expected interface (see
586  * #GDBusProxy:g-interface-info), then @property_name (for existence)
587  * and @value (for the type) is checked against it.
588  *
589  * If the @value #GVariant is floating, it is consumed. This allows
590  * convenient 'inline' use of g_variant_new(), e.g.
591  * |[
592  *  g_dbus_proxy_set_cached_property (proxy,
593  *                                    "SomeProperty",
594  *                                    g_variant_new ("(si)",
595  *                                                  "A String",
596  *                                                  42));
597  * ]|
598  *
599  * Normally you will not need to use this method since @proxy is
600  * tracking changes using the
601  * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
602  * D-Bus signal. However, for performance reasons an object may decide
603  * to not use this signal for some properties and instead use a
604  * proprietary out-of-band mechanism to transmit changes.
605  *
606  * As a concrete example, consider an object with a property
607  * <literal>ChatroomParticipants</literal> which is an array of
608  * strings. Instead of transmitting the same (long) array every time
609  * the property changes, it is more efficient to only transmit the
610  * delta using e.g. signals <literal>ChatroomParticipantJoined(String
611  * name)</literal> and <literal>ChatroomParticipantParted(String
612  * name)</literal>.
613  *
614  * Since: 2.26
615  */
616 void
617 g_dbus_proxy_set_cached_property (GDBusProxy   *proxy,
618                                   const gchar  *property_name,
619                                   GVariant     *value)
620 {
621   const GDBusPropertyInfo *info;
622
623   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
624   g_return_if_fail (property_name != NULL);
625
626   if (value != NULL)
627     {
628       info = lookup_property_info_or_warn (proxy, property_name);
629       if (info != NULL)
630         {
631           if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
632             {
633               g_warning (_("Trying to set property %s of type %s but according to the expected "
634                            "interface the type is %s"),
635                          property_name,
636                          g_variant_get_type_string (value),
637                          info->signature);
638               goto out;
639             }
640         }
641       g_hash_table_insert (proxy->priv->properties,
642                            g_strdup (property_name),
643                            g_variant_ref_sink (value));
644     }
645   else
646     {
647       g_hash_table_remove (proxy->priv->properties, property_name);
648     }
649
650  out:
651   ;
652 }
653
654 /* ---------------------------------------------------------------------------------------------------- */
655
656 static void
657 on_signal_received (GDBusConnection *connection,
658                     const gchar     *sender_name,
659                     const gchar     *object_path,
660                     const gchar     *interface_name,
661                     const gchar     *signal_name,
662                     GVariant        *parameters,
663                     gpointer         user_data)
664 {
665   GDBusProxy *proxy = G_DBUS_PROXY (user_data);
666
667   if (!proxy->priv->initialized)
668     goto out;
669
670   g_signal_emit (proxy,
671                  signals[SIGNAL_SIGNAL],
672                  0,
673                  sender_name,
674                  signal_name,
675                  parameters);
676  out:
677   ;
678 }
679
680 /* ---------------------------------------------------------------------------------------------------- */
681
682 static void
683 on_properties_changed (GDBusConnection *connection,
684                        const gchar     *sender_name,
685                        const gchar     *object_path,
686                        const gchar     *interface_name,
687                        const gchar     *signal_name,
688                        GVariant        *parameters,
689                        gpointer         user_data)
690 {
691   GDBusProxy *proxy = G_DBUS_PROXY (user_data);
692   GError *error;
693   const gchar *interface_name_for_signal;
694   GVariant *changed_properties;
695   gchar **invalidated_properties;
696   GVariantIter iter;
697   gchar *key;
698   GVariant *value;
699
700   error = NULL;
701   changed_properties = NULL;
702   invalidated_properties = NULL;
703
704   if (!proxy->priv->initialized)
705     goto out;
706
707   if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
708     {
709       g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
710                  g_variant_get_type_string (parameters));
711       goto out;
712     }
713
714   g_variant_get (parameters,
715                  "(&s@a{sv}^a&s)",
716                  &interface_name_for_signal,
717                  &changed_properties,
718                  &invalidated_properties);
719
720   if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
721     goto out;
722
723   g_variant_iter_init (&iter, changed_properties);
724   while (g_variant_iter_next (&iter, "{sv}", &key, &value))
725     {
726       g_hash_table_insert (proxy->priv->properties,
727                            key, /* adopts string */
728                            value); /* adopts value */
729     }
730
731   /* emit signal */
732   g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
733                  0,
734                  changed_properties,
735                  invalidated_properties);
736
737  out:
738   if (changed_properties != NULL)
739     g_variant_unref (changed_properties);
740   g_free (invalidated_properties);
741 }
742
743 /* ---------------------------------------------------------------------------------------------------- */
744
745 static void
746 g_dbus_proxy_constructed (GObject *object)
747 {
748   if (G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed != NULL)
749     G_OBJECT_CLASS (g_dbus_proxy_parent_class)->constructed (object);
750 }
751
752 /* ---------------------------------------------------------------------------------------------------- */
753
754 static void
755 subscribe_to_signals (GDBusProxy *proxy)
756 {
757   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
758     {
759       /* subscribe to PropertiesChanged() */
760       proxy->priv->properties_changed_subscriber_id =
761         g_dbus_connection_signal_subscribe (proxy->priv->connection,
762                                             proxy->priv->unique_bus_name,
763                                             "org.freedesktop.DBus.Properties",
764                                             "PropertiesChanged",
765                                             proxy->priv->object_path,
766                                             proxy->priv->interface_name,
767                                             on_properties_changed,
768                                             proxy,
769                                             NULL);
770     }
771
772   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
773     {
774       /* subscribe to all signals for the object */
775       proxy->priv->signals_subscriber_id =
776         g_dbus_connection_signal_subscribe (proxy->priv->connection,
777                                             proxy->priv->unique_bus_name,
778                                             proxy->priv->interface_name,
779                                             NULL,                        /* member */
780                                             proxy->priv->object_path,
781                                             NULL,                        /* arg0 */
782                                             on_signal_received,
783                                             proxy,
784                                             NULL);
785     }
786 }
787
788 /* ---------------------------------------------------------------------------------------------------- */
789
790 static void
791 process_get_all_reply (GDBusProxy *proxy,
792                        GVariant   *result)
793 {
794   GVariantIter iter;
795   GVariant *item;
796
797   if (strcmp (g_variant_get_type_string (result), "(a{sv})") != 0)
798     {
799       g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
800                  g_variant_get_type_string (result));
801       goto out;
802     }
803
804   g_variant_iter_init (&iter, g_variant_get_child_value (result, 0));
805   while ((item = g_variant_iter_next_value (&iter)) != NULL)
806     {
807       gchar *key;
808       GVariant *value;
809
810       g_variant_get (item,
811                      "{sv}",
812                      &key,
813                      &value);
814       //g_print ("got %s -> %s\n", key, g_variant_markup_print (value, FALSE, 0, 0));
815
816       g_hash_table_insert (proxy->priv->properties,
817                            key,
818                            value); /* steals value */
819     }
820  out:
821   ;
822 }
823
824 static gboolean
825 initable_init (GInitable     *initable,
826                GCancellable  *cancellable,
827                GError       **error)
828 {
829   GDBusProxy *proxy = G_DBUS_PROXY (initable);
830   GVariant *result;
831   gboolean ret;
832
833   ret = FALSE;
834
835   subscribe_to_signals (proxy);
836
837   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
838     {
839       /* load all properties synchronously */
840       result = g_dbus_connection_call_sync (proxy->priv->connection,
841                                             proxy->priv->unique_bus_name,
842                                             proxy->priv->object_path,
843                                             "org.freedesktop.DBus.Properties",
844                                             "GetAll",
845                                             g_variant_new ("(s)", proxy->priv->interface_name),
846                                             G_DBUS_CALL_FLAGS_NONE,
847                                             -1,           /* timeout */
848                                             cancellable,
849                                             error);
850       if (result == NULL)
851         goto out;
852
853       process_get_all_reply (proxy, result);
854
855       g_variant_unref (result);
856     }
857
858   ret = TRUE;
859
860  out:
861   proxy->priv->initialized = TRUE;
862   return ret;
863 }
864
865 static void
866 initable_iface_init (GInitableIface *initable_iface)
867 {
868   initable_iface->init = initable_init;
869 }
870
871 /* ---------------------------------------------------------------------------------------------------- */
872
873 static void
874 get_all_cb (GDBusConnection *connection,
875             GAsyncResult    *res,
876             gpointer         user_data)
877 {
878   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
879   GVariant *result;
880   GError *error;
881
882   error = NULL;
883   result = g_dbus_connection_call_finish (connection,
884                                           res,
885                                           &error);
886   if (result == NULL)
887     {
888       g_simple_async_result_set_from_error (simple, error);
889       g_error_free (error);
890     }
891   else
892     {
893       g_simple_async_result_set_op_res_gpointer (simple,
894                                                  result,
895                                                  (GDestroyNotify) g_variant_unref);
896     }
897
898   g_simple_async_result_complete_in_idle (simple);
899   g_object_unref (simple);
900 }
901
902 static void
903 async_initable_init_async (GAsyncInitable      *initable,
904                            gint                 io_priority,
905                            GCancellable        *cancellable,
906                            GAsyncReadyCallback  callback,
907                            gpointer             user_data)
908 {
909   GDBusProxy *proxy = G_DBUS_PROXY (initable);
910   GSimpleAsyncResult *simple;
911
912   simple = g_simple_async_result_new (G_OBJECT (proxy),
913                                       callback,
914                                       user_data,
915                                       NULL);
916
917   subscribe_to_signals (proxy);
918
919   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
920     {
921       /* load all properties asynchronously */
922       g_dbus_connection_call (proxy->priv->connection,
923                               proxy->priv->unique_bus_name,
924                               proxy->priv->object_path,
925                               "org.freedesktop.DBus.Properties",
926                               "GetAll",
927                               g_variant_new ("(s)", proxy->priv->interface_name),
928                               G_DBUS_CALL_FLAGS_NONE,
929                               -1,           /* timeout */
930                               cancellable,
931                               (GAsyncReadyCallback) get_all_cb,
932                               simple);
933     }
934   else
935     {
936       g_simple_async_result_complete_in_idle (simple);
937       g_object_unref (simple);
938     }
939 }
940
941 static gboolean
942 async_initable_init_finish (GAsyncInitable  *initable,
943                             GAsyncResult    *res,
944                             GError         **error)
945 {
946   GDBusProxy *proxy = G_DBUS_PROXY (initable);
947   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
948   GVariant *result;
949   gboolean ret;
950
951   ret = FALSE;
952
953   result = g_simple_async_result_get_op_res_gpointer (simple);
954   if (result == NULL)
955     {
956       if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
957         {
958           g_simple_async_result_propagate_error (simple, error);
959           goto out;
960         }
961     }
962   else
963     {
964       process_get_all_reply (proxy, result);
965     }
966
967   ret = TRUE;
968
969  out:
970   proxy->priv->initialized = TRUE;
971   return ret;
972 }
973
974 static void
975 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
976 {
977   async_initable_iface->init_async = async_initable_init_async;
978   async_initable_iface->init_finish = async_initable_init_finish;
979 }
980
981 /* ---------------------------------------------------------------------------------------------------- */
982
983 /**
984  * g_dbus_proxy_new:
985  * @connection: A #GDBusConnection.
986  * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
987  * @flags: Flags used when constructing the proxy.
988  * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
989  * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
990  * @object_path: An object path.
991  * @interface_name: A D-Bus interface name.
992  * @cancellable: A #GCancellable or %NULL.
993  * @callback: Callback function to invoke when the proxy is ready.
994  * @user_data: User data to pass to @callback.
995  *
996  * Creates a proxy for accessing @interface_name on the remote object at @object_path
997  * owned by @unique_bus_name at @connection and asynchronously loads D-Bus properties unless the
998  * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to the
999  * #GDBusProxy::g-properties-changed signal to get notified about property changes.
1000  *
1001  * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1002  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1003  * to handle signals from the remote object.
1004  *
1005  * This is a failable asynchronous constructor - when the proxy is
1006  * ready, @callback will be invoked and you can use
1007  * g_dbus_proxy_new_finish() to get the result.
1008  *
1009  * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1010  *
1011  * Since: 2.26
1012  */
1013 void
1014 g_dbus_proxy_new (GDBusConnection     *connection,
1015                   GType                object_type,
1016                   GDBusProxyFlags      flags,
1017                   GDBusInterfaceInfo  *info,
1018                   const gchar         *unique_bus_name,
1019                   const gchar         *object_path,
1020                   const gchar         *interface_name,
1021                   GCancellable        *cancellable,
1022                   GAsyncReadyCallback  callback,
1023                   gpointer             user_data)
1024 {
1025   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1026   g_return_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY));
1027   g_return_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1028                     g_dbus_is_unique_name (unique_bus_name));
1029   g_return_if_fail (g_variant_is_object_path (object_path));
1030   g_return_if_fail (g_dbus_is_interface_name (interface_name));
1031
1032   g_async_initable_new_async (object_type,
1033                               G_PRIORITY_DEFAULT,
1034                               cancellable,
1035                               callback,
1036                               user_data,
1037                               "g-flags", flags,
1038                               "g-interface-info", info,
1039                               "g-unique-bus-name", unique_bus_name,
1040                               "g-connection", connection,
1041                               "g-object-path", object_path,
1042                               "g-interface-name", interface_name,
1043                               NULL);
1044 }
1045
1046 /**
1047  * g_dbus_proxy_new_finish:
1048  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1049  * @error: Return location for error or %NULL.
1050  *
1051  * Finishes creating a #GDBusProxy.
1052  *
1053  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1054  *
1055  * Since: 2.26
1056  */
1057 GDBusProxy *
1058 g_dbus_proxy_new_finish (GAsyncResult  *res,
1059                          GError       **error)
1060 {
1061   GObject *object;
1062   GObject *source_object;
1063
1064   source_object = g_async_result_get_source_object (res);
1065   g_assert (source_object != NULL);
1066
1067   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1068                                         res,
1069                                         error);
1070   g_object_unref (source_object);
1071
1072   if (object != NULL)
1073     return G_DBUS_PROXY (object);
1074   else
1075     return NULL;
1076 }
1077
1078
1079 /* ---------------------------------------------------------------------------------------------------- */
1080
1081 /**
1082  * g_dbus_proxy_new_sync:
1083  * @connection: A #GDBusConnection.
1084  * @object_type: Either #G_TYPE_DBUS_PROXY or the #GType for the #GDBusProxy<!-- -->-derived type of proxy to create.
1085  * @flags: Flags used when constructing the proxy.
1086  * @info: A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1087  * @unique_bus_name: A unique bus name or %NULL if @connection is not a message bus connection.
1088  * @object_path: An object path.
1089  * @interface_name: A D-Bus interface name.
1090  * @cancellable: A #GCancellable or %NULL.
1091  * @error: Return location for error or %NULL.
1092  *
1093  * Creates a proxy for accessing @interface_name on the remote object at @object_path
1094  * owned by @unique_bus_name at @connection and synchronously loads D-Bus properties unless the
1095  * #G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1096  *
1097  * If the #G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1098  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1099  * to handle signals from the remote object.
1100  *
1101  * This is a synchronous failable constructor. See g_dbus_proxy_new()
1102  * and g_dbus_proxy_new_finish() for the asynchronous version.
1103  *
1104  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1105  *
1106  * Since: 2.26
1107  */
1108 GDBusProxy *
1109 g_dbus_proxy_new_sync (GDBusConnection     *connection,
1110                        GType                object_type,
1111                        GDBusProxyFlags      flags,
1112                        GDBusInterfaceInfo  *info,
1113                        const gchar         *unique_bus_name,
1114                        const gchar         *object_path,
1115                        const gchar         *interface_name,
1116                        GCancellable        *cancellable,
1117                        GError             **error)
1118 {
1119   GInitable *initable;
1120
1121   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1122   g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_DBUS_PROXY), NULL);
1123   g_return_val_if_fail ((unique_bus_name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1124                         g_dbus_is_unique_name (unique_bus_name), NULL);
1125   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1126   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1127
1128   initable = g_initable_new (object_type,
1129                              cancellable,
1130                              error,
1131                              "g-flags", flags,
1132                              "g-interface-info", info,
1133                              "g-unique-bus-name", unique_bus_name,
1134                              "g-connection", connection,
1135                              "g-object-path", object_path,
1136                              "g-interface-name", interface_name,
1137                              NULL);
1138   if (initable != NULL)
1139     return G_DBUS_PROXY (initable);
1140   else
1141     return NULL;
1142 }
1143
1144 /* ---------------------------------------------------------------------------------------------------- */
1145
1146 /**
1147  * g_dbus_proxy_get_connection:
1148  * @proxy: A #GDBusProxy.
1149  *
1150  * Gets the connection @proxy is for.
1151  *
1152  * Returns: A #GDBusConnection owned by @proxy. Do not free.
1153  *
1154  * Since: 2.26
1155  */
1156 GDBusConnection *
1157 g_dbus_proxy_get_connection (GDBusProxy *proxy)
1158 {
1159   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1160   return proxy->priv->connection;
1161 }
1162
1163 /**
1164  * g_dbus_proxy_get_flags:
1165  * @proxy: A #GDBusProxy.
1166  *
1167  * Gets the flags that @proxy was constructed with.
1168  *
1169  * Returns: Flags from the #GDBusProxyFlags enumeration.
1170  *
1171  * Since: 2.26
1172  */
1173 GDBusProxyFlags
1174 g_dbus_proxy_get_flags (GDBusProxy *proxy)
1175 {
1176   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
1177   return proxy->priv->flags;
1178 }
1179
1180 /**
1181  * g_dbus_proxy_get_unique_bus_name:
1182  * @proxy: A #GDBusProxy.
1183  *
1184  * Gets the unique bus name @proxy is for.
1185  *
1186  * Returns: A string owned by @proxy. Do not free.
1187  *
1188  * Since: 2.26
1189  */
1190 const gchar *
1191 g_dbus_proxy_get_unique_bus_name (GDBusProxy *proxy)
1192 {
1193   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1194   return proxy->priv->unique_bus_name;
1195 }
1196
1197 /**
1198  * g_dbus_proxy_get_object_path:
1199  * @proxy: A #GDBusProxy.
1200  *
1201  * Gets the object path @proxy is for.
1202  *
1203  * Returns: A string owned by @proxy. Do not free.
1204  *
1205  * Since: 2.26
1206  */
1207 const gchar *
1208 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
1209 {
1210   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1211   return proxy->priv->object_path;
1212 }
1213
1214 /**
1215  * g_dbus_proxy_get_interface_name:
1216  * @proxy: A #GDBusProxy.
1217  *
1218  * Gets the D-Bus interface name @proxy is for.
1219  *
1220  * Returns: A string owned by @proxy. Do not free.
1221  *
1222  * Since: 2.26
1223  */
1224 const gchar *
1225 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
1226 {
1227   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1228   return proxy->priv->interface_name;
1229 }
1230
1231 /**
1232  * g_dbus_proxy_get_default_timeout:
1233  * @proxy: A #GDBusProxy.
1234  *
1235  * Gets the timeout to use if -1 (specifying default timeout) is
1236  * passed as @timeout_msec in the g_dbus_proxy_call() and
1237  * g_dbus_proxy_call_sync() functions.
1238  *
1239  * See the #GDBusProxy:g-default-timeout property for more details.
1240  *
1241  * Returns: Timeout to use for @proxy.
1242  *
1243  * Since: 2.26
1244  */
1245 gint
1246 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
1247 {
1248   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
1249   return proxy->priv->timeout_msec;
1250 }
1251
1252 /**
1253  * g_dbus_proxy_set_default_timeout:
1254  * @proxy: A #GDBusProxy.
1255  * @timeout_msec: Timeout in milliseconds.
1256  *
1257  * Sets the timeout to use if -1 (specifying default timeout) is
1258  * passed as @timeout_msec in the g_dbus_proxy_call() and
1259  * g_dbus_proxy_call_sync() functions.
1260  *
1261  * See the #GDBusProxy:g-default-timeout property for more details.
1262  *
1263  * Since: 2.26
1264  */
1265 void
1266 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
1267                                   gint        timeout_msec)
1268 {
1269   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1270   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1271
1272   /* TODO: locking? */
1273   if (proxy->priv->timeout_msec != timeout_msec)
1274     {
1275       proxy->priv->timeout_msec = timeout_msec;
1276       g_object_notify (G_OBJECT (proxy), "g-default-timeout");
1277     }
1278 }
1279
1280 /**
1281  * g_dbus_proxy_get_interface_info:
1282  * @proxy: A #GDBusProxy
1283  *
1284  * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
1285  * interface that @proxy conforms to.
1286  *
1287  * See the #GDBusProxy:g-interface-info property for more details.
1288  *
1289  * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
1290  * object, it is owned by @proxy.
1291  *
1292  * Since: 2.26
1293  */
1294 GDBusInterfaceInfo *
1295 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
1296 {
1297   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1298   return proxy->priv->expected_interface;
1299 }
1300
1301 /**
1302  * g_dbus_proxy_set_interface_info:
1303  * @proxy: A #GDBusProxy
1304  * @info: Minimum interface this proxy conforms to or %NULL to unset.
1305  *
1306  * Ensure that interactions with @proxy conform to the given
1307  * interface.  For example, when completing a method call, if the type
1308  * signature of the message isn't what's expected, the given #GError
1309  * is set.  Signals that have a type signature mismatch are simply
1310  * dropped.
1311  *
1312  * See the #GDBusProxy:g-interface-info property for more details.
1313  *
1314  * Since: 2.26
1315  */
1316 void
1317 g_dbus_proxy_set_interface_info (GDBusProxy         *proxy,
1318                                  GDBusInterfaceInfo *info)
1319 {
1320   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1321   if (proxy->priv->expected_interface != NULL)
1322     g_dbus_interface_info_unref (proxy->priv->expected_interface);
1323   proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
1324 }
1325
1326 /* ---------------------------------------------------------------------------------------------------- */
1327
1328 static gboolean
1329 maybe_split_method_name (const gchar  *method_name,
1330                          gchar       **out_interface_name,
1331                          const gchar **out_method_name)
1332 {
1333   gboolean was_split;
1334
1335   was_split = FALSE;
1336   g_assert (out_interface_name != NULL);
1337   g_assert (out_method_name != NULL);
1338   *out_interface_name = NULL;
1339   *out_method_name = NULL;
1340
1341   if (strchr (method_name, '.') != NULL)
1342     {
1343       gchar *p;
1344       gchar *last_dot;
1345
1346       p = g_strdup (method_name);
1347       last_dot = strrchr (p, '.');
1348       *last_dot = '\0';
1349
1350       *out_interface_name = p;
1351       *out_method_name = last_dot + 1;
1352
1353       was_split = TRUE;
1354     }
1355
1356   return was_split;
1357 }
1358
1359
1360 static void
1361 reply_cb (GDBusConnection *connection,
1362           GAsyncResult    *res,
1363           gpointer         user_data)
1364 {
1365   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1366   GVariant *value;
1367   GError *error;
1368
1369   error = NULL;
1370   value = g_dbus_connection_call_finish (connection,
1371                                          res,
1372                                          &error);
1373   if (error != NULL)
1374     {
1375       g_simple_async_result_set_from_error (simple,
1376                                             error);
1377       g_error_free (error);
1378     }
1379   else
1380     {
1381       g_simple_async_result_set_op_res_gpointer (simple,
1382                                                  value,
1383                                                  (GDestroyNotify) g_variant_unref);
1384     }
1385
1386   /* no need to complete in idle since the method GDBusConnection already does */
1387   g_simple_async_result_complete (simple);
1388 }
1389
1390 static const GDBusMethodInfo *
1391 lookup_method_info_or_warn (GDBusProxy  *proxy,
1392                             const gchar *method_name)
1393 {
1394   const GDBusMethodInfo *info;
1395
1396   if (proxy->priv->expected_interface == NULL)
1397     return NULL;
1398
1399   info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1400   if (info == NULL)
1401     {
1402       g_warning ("Trying to invoke method %s which isn't in expected interface %s",
1403                  method_name, proxy->priv->expected_interface->name);
1404     }
1405
1406   return info;
1407 }
1408
1409 static gboolean
1410 validate_method_return (const char             *method_name,
1411                         GVariant               *value,
1412                         const GDBusMethodInfo  *expected_method_info,
1413                         GError                **error)
1414 {
1415   const gchar *type_string;
1416   gchar *signature;
1417   gboolean ret;
1418
1419   ret = TRUE;
1420   signature = NULL;
1421
1422   if (value == NULL || expected_method_info == NULL)
1423     goto out;
1424
1425   /* Shouldn't happen... */
1426   if (g_variant_classify (value) != G_VARIANT_CLASS_TUPLE)
1427     goto out;
1428
1429   type_string = g_variant_get_type_string (value);
1430   signature = _g_dbus_compute_complete_signature (expected_method_info->out_args, TRUE);
1431   if (g_strcmp0 (type_string, signature) != 0)
1432     {
1433       g_set_error (error,
1434                    G_IO_ERROR,
1435                    G_IO_ERROR_INVALID_ARGUMENT,
1436                    _("Method `%s' returned signature `%s', but expected `%s'"),
1437                    method_name,
1438                    type_string,
1439                    signature);
1440       ret = FALSE;
1441     }
1442
1443  out:
1444   g_free (signature);
1445   return ret;
1446 }
1447
1448 /**
1449  * g_dbus_proxy_call:
1450  * @proxy: A #GDBusProxy.
1451  * @method_name: Name of method to invoke.
1452  * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1453  * @flags: Flags from the #GDBusCallFlags enumeration.
1454  * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1455  * @cancellable: A #GCancellable or %NULL.
1456  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
1457  * care about the result of the method invocation.
1458  * @user_data: The data to pass to @callback.
1459  *
1460  * Asynchronously invokes the @method_name method on @proxy.
1461  *
1462  * If @method_name contains any dots, then @name is split into interface and
1463  * method name parts. This allows using @proxy for invoking methods on
1464  * other interfaces.
1465  *
1466  * If the #GDBusConnection associated with @proxy is closed then
1467  * the operation will fail with %G_IO_ERROR_CLOSED. If
1468  * @cancellable is canceled, the operation will fail with
1469  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1470  * compatible with the D-Bus protocol, the operation fails with
1471  * %G_IO_ERROR_INVALID_ARGUMENT.
1472  *
1473  * If the @parameters #GVariant is floating, it is consumed. This allows
1474  * convenient 'inline' use of g_variant_new(), e.g.:
1475  * |[
1476  *  g_dbus_proxy_call (proxy,
1477  *                     "TwoStrings",
1478  *                     g_variant_new ("(ss)",
1479  *                                    "Thing One",
1480  *                                    "Thing Two"),
1481  *                     G_DBUS_CALL_FLAGS_NONE,
1482  *                     -1,
1483  *                     NULL,
1484  *                     (GAsyncReadyCallback) two_strings_done,
1485  *                     &amp;data);
1486  * ]|
1487  *
1488  * This is an asynchronous method. When the operation is finished,
1489  * @callback will be invoked in the
1490  * <link linkend="g-main-context-push-thread-default">thread-default
1491  * main loop</link> of the thread you are calling this method from.
1492  * You can then call g_dbus_proxy_call_finish() to get the result of
1493  * the operation. See g_dbus_proxy_call_sync() for the synchronous
1494  * version of this method.
1495  *
1496  * Since: 2.26
1497  */
1498 void
1499 g_dbus_proxy_call (GDBusProxy          *proxy,
1500                    const gchar         *method_name,
1501                    GVariant            *parameters,
1502                    GDBusCallFlags       flags,
1503                    gint                 timeout_msec,
1504                    GCancellable        *cancellable,
1505                    GAsyncReadyCallback  callback,
1506                    gpointer             user_data)
1507 {
1508   GSimpleAsyncResult *simple;
1509   gboolean was_split;
1510   gchar *split_interface_name;
1511   const gchar *split_method_name;
1512   const GDBusMethodInfo *expected_method_info;
1513   const gchar *target_method_name;
1514   const gchar *target_interface_name;
1515
1516   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
1517   g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
1518   g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
1519   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
1520
1521   simple = g_simple_async_result_new (G_OBJECT (proxy),
1522                                       callback,
1523                                       user_data,
1524                                       g_dbus_proxy_call);
1525
1526   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1527   target_method_name = was_split ? split_method_name : method_name;
1528   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1529
1530   g_object_set_data_full (G_OBJECT (simple), "-gdbus-proxy-method-name", g_strdup (target_method_name), g_free);
1531
1532   /* Just warn here */
1533   expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
1534
1535   g_dbus_connection_call (proxy->priv->connection,
1536                           proxy->priv->unique_bus_name,
1537                           proxy->priv->object_path,
1538                           target_interface_name,
1539                           target_method_name,
1540                           parameters,
1541                           flags,
1542                           timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1543                           cancellable,
1544                           (GAsyncReadyCallback) reply_cb,
1545                           simple);
1546
1547   g_free (split_interface_name);
1548 }
1549
1550 /**
1551  * g_dbus_proxy_call_finish:
1552  * @proxy: A #GDBusProxy.
1553  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
1554  * @error: Return location for error or %NULL.
1555  *
1556  * Finishes an operation started with g_dbus_proxy_call().
1557  *
1558  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1559  * return values. Free with g_variant_unref().
1560  *
1561  * Since: 2.26
1562  */
1563 GVariant *
1564 g_dbus_proxy_call_finish (GDBusProxy    *proxy,
1565                           GAsyncResult  *res,
1566                           GError       **error)
1567 {
1568   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1569   GVariant *value;
1570   const char *method_name;
1571   const GDBusMethodInfo *expected_method_info;
1572
1573   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1574   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1575   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1576
1577   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call);
1578
1579   value = NULL;
1580
1581   if (g_simple_async_result_propagate_error (simple, error))
1582     goto out;
1583
1584   value = g_simple_async_result_get_op_res_gpointer (simple);
1585   method_name = g_object_get_data (G_OBJECT (simple), "-gdbus-proxy-method-name");
1586
1587   /* We may not have a method name for internally-generated proxy calls like GetAll */
1588   if (value && method_name && proxy->priv->expected_interface)
1589     {
1590       expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
1591       if (!validate_method_return (method_name, value, expected_method_info, error))
1592         {
1593           g_variant_unref (value);
1594           value = NULL;
1595         }
1596     }
1597
1598  out:
1599   return value;
1600 }
1601
1602 /**
1603  * g_dbus_proxy_call_sync:
1604  * @proxy: A #GDBusProxy.
1605  * @method_name: Name of method to invoke.
1606  * @parameters: A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
1607  * @flags: Flags from the #GDBusCallFlags enumeration.
1608  * @timeout_msec: The timeout in milliseconds or -1 to use the proxy default timeout.
1609  * @cancellable: A #GCancellable or %NULL.
1610  * @error: Return location for error or %NULL.
1611  *
1612  * Synchronously invokes the @method_name method on @proxy.
1613  *
1614  * If @method_name contains any dots, then @name is split into interface and
1615  * method name parts. This allows using @proxy for invoking methods on
1616  * other interfaces.
1617  *
1618  * If the #GDBusConnection associated with @proxy is disconnected then
1619  * the operation will fail with %G_IO_ERROR_CLOSED. If
1620  * @cancellable is canceled, the operation will fail with
1621  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
1622  * compatible with the D-Bus protocol, the operation fails with
1623  * %G_IO_ERROR_INVALID_ARGUMENT.
1624  *
1625  * If the @parameters #GVariant is floating, it is consumed. This allows
1626  * convenient 'inline' use of g_variant_new(), e.g.:
1627  * |[
1628  *  g_dbus_proxy_call_sync (proxy,
1629  *                          "TwoStrings",
1630  *                          g_variant_new ("(ss)",
1631  *                                         "Thing One",
1632  *                                         "Thing Two"),
1633  *                          G_DBUS_CALL_FLAGS_NONE,
1634  *                          -1,
1635  *                          NULL,
1636  *                          &amp;error);
1637  * ]|
1638  *
1639  * The calling thread is blocked until a reply is received. See
1640  * g_dbus_proxy_call() for the asynchronous version of this
1641  * method.
1642  *
1643  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
1644  * return values. Free with g_variant_unref().
1645  *
1646  * Since: 2.26
1647  */
1648 GVariant *
1649 g_dbus_proxy_call_sync (GDBusProxy      *proxy,
1650                         const gchar     *method_name,
1651                         GVariant        *parameters,
1652                         GDBusCallFlags   flags,
1653                         gint             timeout_msec,
1654                         GCancellable    *cancellable,
1655                         GError         **error)
1656 {
1657   GVariant *ret;
1658   gboolean was_split;
1659   gchar *split_interface_name;
1660   const gchar *split_method_name;
1661   const GDBusMethodInfo *expected_method_info;
1662   const gchar *target_method_name;
1663   const gchar *target_interface_name;
1664
1665   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
1666   g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
1667   g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
1668   g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
1669   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1670
1671   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
1672   target_method_name = was_split ? split_method_name : method_name;
1673   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
1674
1675   if (proxy->priv->expected_interface)
1676     {
1677       expected_method_info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, target_method_name);
1678       if (expected_method_info == NULL)
1679         {
1680           g_warning ("Trying to invoke method `%s' which isn't in expected interface `%s'",
1681                      target_method_name,
1682                      target_interface_name);
1683         }
1684     }
1685   else
1686     {
1687       expected_method_info = NULL;
1688     }
1689
1690   ret = g_dbus_connection_call_sync (proxy->priv->connection,
1691                                      proxy->priv->unique_bus_name,
1692                                      proxy->priv->object_path,
1693                                      target_interface_name,
1694                                      target_method_name,
1695                                      parameters,
1696                                      flags,
1697                                      timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
1698                                      cancellable,
1699                                      error);
1700   if (!validate_method_return (target_method_name, ret, expected_method_info, error))
1701     {
1702       g_variant_unref (ret);
1703       ret = NULL;
1704     }
1705
1706   g_free (split_interface_name);
1707
1708   return ret;
1709 }
1710
1711 /* ---------------------------------------------------------------------------------------------------- */
1712
1713 #define __G_DBUS_PROXY_C__
1714 #include "gioaliasdef.c"