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