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