4a321f7393893f993aee9ba1d77364ca930a8d85
[platform/upstream/glib.git] / gio / gdbusproxy.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gdbusutils.h"
29 #include "gdbusproxy.h"
30 #include "gioenumtypes.h"
31 #include "gdbusconnection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
34 #include "ginitable.h"
35 #include "gasyncinitable.h"
36 #include "gioerror.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "gcancellable.h"
40 #include "gdbusinterface.h"
41
42 #ifdef G_OS_UNIX
43 #include "gunixfdlist.h"
44 #endif
45
46 #include "glibintl.h"
47
48 /**
49  * SECTION:gdbusproxy
50  * @short_description: Client-side D-Bus interface proxy
51  * @include: gio/gio.h
52  *
53  * #GDBusProxy is a base class used for proxies to access a D-Bus
54  * interface on a remote object. A #GDBusProxy can be constructed for
55  * both well-known and unique names.
56  *
57  * By default, #GDBusProxy will cache all properties (and listen to
58  * changes) of the remote object, and proxy all signals that gets
59  * emitted. This behaviour can be changed by passing suitable
60  * #GDBusProxyFlags when the proxy is created. If the proxy is for a
61  * well-known name, the property cache is flushed when the name owner
62  * vanishes and reloaded when a name owner appears.
63  *
64  * If a #GDBusProxy is used for a well-known name, the owner of the
65  * name is tracked and can be read from
66  * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
67  * get notified of changes. Additionally, only signals and property
68  * changes emitted from the current name owner are considered and
69  * calls are always sent to the current name owner. This avoids a
70  * number of race conditions when the name is lost by one owner and
71  * claimed by another. However, if no name owner currently exists,
72  * then calls will be sent to the well-known name which may result in
73  * the message bus launching an owner (unless
74  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
75  *
76  * The generic #GDBusProxy::g-properties-changed and
77  * #GDBusProxy::g-signal signals are not very convenient to work
78  * with. Therefore, the recommended way of working with proxies is to
79  * subclass #GDBusProxy, and have more natural properties and signals
80  * in your derived class. See <xref linkend="gdbus-example-gdbus-codegen"/>
81  * for how this can easily be done using the
82  * <command><link linkend="gdbus-codegen">gdbus-codegen</link></command>
83  * tool.
84  *
85  * A #GDBusProxy instance can be used from multiple threads but note
86  * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
87  * and #GObject::notify) are emitted in the
88  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
89  * of the thread where the instance was constructed.
90  *
91  * <example id="gdbus-wellknown-proxy"><title>GDBusProxy for a well-known-name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-proxy.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
92  */
93
94 /* lock protecting the properties GHashTable */
95 G_LOCK_DEFINE_STATIC (properties_lock);
96
97 /* ---------------------------------------------------------------------------------------------------- */
98
99 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
100
101 typedef struct
102 {
103   volatile gint ref_count;
104   GDBusProxy *proxy;
105 } SignalSubscriptionData;
106
107 static SignalSubscriptionData *
108 signal_subscription_ref (SignalSubscriptionData *data)
109 {
110   g_atomic_int_inc (&data->ref_count);
111   return data;
112 }
113
114 static void
115 signal_subscription_unref (SignalSubscriptionData *data)
116 {
117   if (g_atomic_int_dec_and_test (&data->ref_count))
118     {
119       g_slice_free (SignalSubscriptionData, data);
120     }
121 }
122
123 /* ---------------------------------------------------------------------------------------------------- */
124
125 struct _GDBusProxyPrivate
126 {
127   GBusType bus_type;
128   GDBusProxyFlags flags;
129   GDBusConnection *connection;
130
131   gchar *name;
132   gchar *name_owner;
133   gchar *object_path;
134   gchar *interface_name;
135   gint timeout_msec;
136
137   guint name_owner_changed_subscription_id;
138
139   GCancellable *get_all_cancellable;
140
141   /* gchar* -> GVariant* */
142   GHashTable *properties;
143
144   GDBusInterfaceInfo *expected_interface;
145
146   guint properties_changed_subscription_id;
147   guint signals_subscription_id;
148
149   gboolean initialized;
150
151   GDBusObject *object;
152
153   SignalSubscriptionData *signal_subscription_data;
154 };
155
156 enum
157 {
158   PROP_0,
159   PROP_G_CONNECTION,
160   PROP_G_BUS_TYPE,
161   PROP_G_NAME,
162   PROP_G_NAME_OWNER,
163   PROP_G_FLAGS,
164   PROP_G_OBJECT_PATH,
165   PROP_G_INTERFACE_NAME,
166   PROP_G_DEFAULT_TIMEOUT,
167   PROP_G_INTERFACE_INFO
168 };
169
170 enum
171 {
172   PROPERTIES_CHANGED_SIGNAL,
173   SIGNAL_SIGNAL,
174   LAST_SIGNAL,
175 };
176
177 guint signals[LAST_SIGNAL] = {0};
178
179 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
180 static void initable_iface_init       (GInitableIface *initable_iface);
181 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
182
183 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
184                          G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
185                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
186                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
187                          );
188
189 static void
190 g_dbus_proxy_dispose (GObject *object)
191 {
192   GDBusProxy *proxy = G_DBUS_PROXY (object);
193   G_LOCK (signal_subscription_lock);
194   if (proxy->priv->signal_subscription_data != NULL)
195     {
196       proxy->priv->signal_subscription_data->proxy = NULL;
197       signal_subscription_unref (proxy->priv->signal_subscription_data);
198       proxy->priv->signal_subscription_data = NULL;
199     }
200   G_UNLOCK (signal_subscription_lock);
201
202   G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
203 }
204
205 static void
206 g_dbus_proxy_finalize (GObject *object)
207 {
208   GDBusProxy *proxy = G_DBUS_PROXY (object);
209
210   g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
211
212   if (proxy->priv->name_owner_changed_subscription_id > 0)
213     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
214                                           proxy->priv->name_owner_changed_subscription_id);
215
216   if (proxy->priv->properties_changed_subscription_id > 0)
217     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
218                                           proxy->priv->properties_changed_subscription_id);
219
220   if (proxy->priv->signals_subscription_id > 0)
221     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
222                                           proxy->priv->signals_subscription_id);
223
224   if (proxy->priv->connection != NULL)
225     g_object_unref (proxy->priv->connection);
226   g_free (proxy->priv->name);
227   g_free (proxy->priv->name_owner);
228   g_free (proxy->priv->object_path);
229   g_free (proxy->priv->interface_name);
230   if (proxy->priv->properties != NULL)
231     g_hash_table_unref (proxy->priv->properties);
232
233   if (proxy->priv->expected_interface != NULL)
234     {
235       g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
236       g_dbus_interface_info_unref (proxy->priv->expected_interface);
237     }
238
239   if (proxy->priv->object != NULL)
240     g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
241
242   G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
243 }
244
245 static void
246 g_dbus_proxy_get_property (GObject    *object,
247                            guint       prop_id,
248                            GValue     *value,
249                            GParamSpec *pspec)
250 {
251   GDBusProxy *proxy = G_DBUS_PROXY (object);
252
253   switch (prop_id)
254     {
255     case PROP_G_CONNECTION:
256       g_value_set_object (value, proxy->priv->connection);
257       break;
258
259     case PROP_G_FLAGS:
260       g_value_set_flags (value, proxy->priv->flags);
261       break;
262
263     case PROP_G_NAME:
264       g_value_set_string (value, proxy->priv->name);
265       break;
266
267     case PROP_G_NAME_OWNER:
268       g_value_set_string (value, proxy->priv->name_owner);
269       break;
270
271     case PROP_G_OBJECT_PATH:
272       g_value_set_string (value, proxy->priv->object_path);
273       break;
274
275     case PROP_G_INTERFACE_NAME:
276       g_value_set_string (value, proxy->priv->interface_name);
277       break;
278
279     case PROP_G_DEFAULT_TIMEOUT:
280       g_value_set_int (value, proxy->priv->timeout_msec);
281       break;
282
283     case PROP_G_INTERFACE_INFO:
284       g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
285       break;
286
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290     }
291 }
292
293 static void
294 g_dbus_proxy_set_property (GObject      *object,
295                            guint         prop_id,
296                            const GValue *value,
297                            GParamSpec   *pspec)
298 {
299   GDBusProxy *proxy = G_DBUS_PROXY (object);
300
301   switch (prop_id)
302     {
303     case PROP_G_CONNECTION:
304       proxy->priv->connection = g_value_dup_object (value);
305       break;
306
307     case PROP_G_FLAGS:
308       proxy->priv->flags = g_value_get_flags (value);
309       break;
310
311     case PROP_G_NAME:
312       proxy->priv->name = g_value_dup_string (value);
313       break;
314
315     case PROP_G_OBJECT_PATH:
316       proxy->priv->object_path = g_value_dup_string (value);
317       break;
318
319     case PROP_G_INTERFACE_NAME:
320       proxy->priv->interface_name = g_value_dup_string (value);
321       break;
322
323     case PROP_G_DEFAULT_TIMEOUT:
324       g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
325       break;
326
327     case PROP_G_INTERFACE_INFO:
328       g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
329       break;
330
331     case PROP_G_BUS_TYPE:
332       proxy->priv->bus_type = g_value_get_enum (value);
333       break;
334
335     default:
336       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337       break;
338     }
339 }
340
341 static void
342 g_dbus_proxy_class_init (GDBusProxyClass *klass)
343 {
344   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
345
346   gobject_class->dispose      = g_dbus_proxy_dispose;
347   gobject_class->finalize     = g_dbus_proxy_finalize;
348   gobject_class->set_property = g_dbus_proxy_set_property;
349   gobject_class->get_property = g_dbus_proxy_get_property;
350
351   /* Note that all property names are prefixed to avoid collisions with D-Bus property names
352    * in derived classes */
353
354   /**
355    * GDBusProxy:g-interface-info:
356    *
357    * Ensure that interactions with this proxy conform to the given
358    * interface.  For example, when completing a method call, if the
359    * type signature of the message isn't what's expected, the given
360    * #GError is set.  Signals that have a type signature mismatch are
361    * simply dropped.
362    *
363    * Since: 2.26
364    */
365   g_object_class_install_property (gobject_class,
366                                    PROP_G_INTERFACE_INFO,
367                                    g_param_spec_boxed ("g-interface-info",
368                                                        P_("Interface Information"),
369                                                        P_("Interface Information"),
370                                                        G_TYPE_DBUS_INTERFACE_INFO,
371                                                        G_PARAM_READABLE |
372                                                        G_PARAM_WRITABLE |
373                                                        G_PARAM_STATIC_NAME |
374                                                        G_PARAM_STATIC_BLURB |
375                                                        G_PARAM_STATIC_NICK));
376
377   /**
378    * GDBusProxy:g-connection:
379    *
380    * The #GDBusConnection the proxy is for.
381    *
382    * Since: 2.26
383    */
384   g_object_class_install_property (gobject_class,
385                                    PROP_G_CONNECTION,
386                                    g_param_spec_object ("g-connection",
387                                                         P_("g-connection"),
388                                                         P_("The connection the proxy is for"),
389                                                         G_TYPE_DBUS_CONNECTION,
390                                                         G_PARAM_READABLE |
391                                                         G_PARAM_WRITABLE |
392                                                         G_PARAM_CONSTRUCT_ONLY |
393                                                         G_PARAM_STATIC_NAME |
394                                                         G_PARAM_STATIC_BLURB |
395                                                         G_PARAM_STATIC_NICK));
396
397   /**
398    * GDBusProxy:g-bus-type:
399    *
400    * If this property is not %G_BUS_TYPE_NONE, then
401    * #GDBusProxy:g-connection must be %NULL and will be set to the
402    * #GDBusConnection obtained by calling g_bus_get() with the value
403    * of this property.
404    *
405    * Since: 2.26
406    */
407   g_object_class_install_property (gobject_class,
408                                    PROP_G_BUS_TYPE,
409                                    g_param_spec_enum ("g-bus-type",
410                                                       P_("Bus Type"),
411                                                       P_("The bus to connect to, if any"),
412                                                       G_TYPE_BUS_TYPE,
413                                                       G_BUS_TYPE_NONE,
414                                                       G_PARAM_WRITABLE |
415                                                       G_PARAM_CONSTRUCT_ONLY |
416                                                       G_PARAM_STATIC_NAME |
417                                                       G_PARAM_STATIC_BLURB |
418                                                       G_PARAM_STATIC_NICK));
419
420   /**
421    * GDBusProxy:g-flags:
422    *
423    * Flags from the #GDBusProxyFlags enumeration.
424    *
425    * Since: 2.26
426    */
427   g_object_class_install_property (gobject_class,
428                                    PROP_G_FLAGS,
429                                    g_param_spec_flags ("g-flags",
430                                                        P_("g-flags"),
431                                                        P_("Flags for the proxy"),
432                                                        G_TYPE_DBUS_PROXY_FLAGS,
433                                                        G_DBUS_PROXY_FLAGS_NONE,
434                                                        G_PARAM_READABLE |
435                                                        G_PARAM_WRITABLE |
436                                                        G_PARAM_CONSTRUCT_ONLY |
437                                                        G_PARAM_STATIC_NAME |
438                                                        G_PARAM_STATIC_BLURB |
439                                                        G_PARAM_STATIC_NICK));
440
441   /**
442    * GDBusProxy:g-name:
443    *
444    * The well-known or unique name that the proxy is for.
445    *
446    * Since: 2.26
447    */
448   g_object_class_install_property (gobject_class,
449                                    PROP_G_NAME,
450                                    g_param_spec_string ("g-name",
451                                                         P_("g-name"),
452                                                         P_("The well-known or unique name that the proxy is for"),
453                                                         NULL,
454                                                         G_PARAM_READABLE |
455                                                         G_PARAM_WRITABLE |
456                                                         G_PARAM_CONSTRUCT_ONLY |
457                                                         G_PARAM_STATIC_NAME |
458                                                         G_PARAM_STATIC_BLURB |
459                                                         G_PARAM_STATIC_NICK));
460
461   /**
462    * GDBusProxy:g-name-owner:
463    *
464    * The unique name that owns #GDBusProxy:name or %NULL if no-one
465    * currently owns that name. You may connect to #GObject::notify signal to
466    * track changes to this property.
467    *
468    * Since: 2.26
469    */
470   g_object_class_install_property (gobject_class,
471                                    PROP_G_NAME_OWNER,
472                                    g_param_spec_string ("g-name-owner",
473                                                         P_("g-name-owner"),
474                                                         P_("The unique name for the owner"),
475                                                         NULL,
476                                                         G_PARAM_READABLE |
477                                                         G_PARAM_STATIC_NAME |
478                                                         G_PARAM_STATIC_BLURB |
479                                                         G_PARAM_STATIC_NICK));
480
481   /**
482    * GDBusProxy:g-object-path:
483    *
484    * The object path the proxy is for.
485    *
486    * Since: 2.26
487    */
488   g_object_class_install_property (gobject_class,
489                                    PROP_G_OBJECT_PATH,
490                                    g_param_spec_string ("g-object-path",
491                                                         P_("g-object-path"),
492                                                         P_("The object path the proxy is for"),
493                                                         NULL,
494                                                         G_PARAM_READABLE |
495                                                         G_PARAM_WRITABLE |
496                                                         G_PARAM_CONSTRUCT_ONLY |
497                                                         G_PARAM_STATIC_NAME |
498                                                         G_PARAM_STATIC_BLURB |
499                                                         G_PARAM_STATIC_NICK));
500
501   /**
502    * GDBusProxy:g-interface-name:
503    *
504    * The D-Bus interface name the proxy is for.
505    *
506    * Since: 2.26
507    */
508   g_object_class_install_property (gobject_class,
509                                    PROP_G_INTERFACE_NAME,
510                                    g_param_spec_string ("g-interface-name",
511                                                         P_("g-interface-name"),
512                                                         P_("The D-Bus interface name the proxy is for"),
513                                                         NULL,
514                                                         G_PARAM_READABLE |
515                                                         G_PARAM_WRITABLE |
516                                                         G_PARAM_CONSTRUCT_ONLY |
517                                                         G_PARAM_STATIC_NAME |
518                                                         G_PARAM_STATIC_BLURB |
519                                                         G_PARAM_STATIC_NICK));
520
521   /**
522    * GDBusProxy:g-default-timeout:
523    *
524    * The timeout to use if -1 (specifying default timeout) is passed
525    * as @timeout_msec in the g_dbus_proxy_call() and
526    * g_dbus_proxy_call_sync() functions.
527    *
528    * This allows applications to set a proxy-wide timeout for all
529    * remote method invocations on the proxy. If this property is -1,
530    * the default timeout (typically 25 seconds) is used. If set to
531    * %G_MAXINT, then no timeout is used.
532    *
533    * Since: 2.26
534    */
535   g_object_class_install_property (gobject_class,
536                                    PROP_G_DEFAULT_TIMEOUT,
537                                    g_param_spec_int ("g-default-timeout",
538                                                      P_("Default Timeout"),
539                                                      P_("Timeout for remote method invocation"),
540                                                      -1,
541                                                      G_MAXINT,
542                                                      -1,
543                                                      G_PARAM_READABLE |
544                                                      G_PARAM_WRITABLE |
545                                                      G_PARAM_CONSTRUCT |
546                                                      G_PARAM_STATIC_NAME |
547                                                      G_PARAM_STATIC_BLURB |
548                                                      G_PARAM_STATIC_NICK));
549
550   /**
551    * GDBusProxy::g-properties-changed:
552    * @proxy: The #GDBusProxy emitting the signal.
553    * @changed_properties: A #GVariant containing the properties that changed
554    * @invalidated_properties: A %NULL terminated array of properties that was invalidated
555    *
556    * Emitted when one or more D-Bus properties on @proxy changes. The
557    * local cache has already been updated when this signal fires. Note
558    * that both @changed_properties and @invalidated_properties are
559    * guaranteed to never be %NULL (either may be empty though).
560    *
561    * This signal corresponds to the
562    * <literal>PropertiesChanged</literal> D-Bus signal on the
563    * <literal>org.freedesktop.DBus.Properties</literal> interface.
564    *
565    * Since: 2.26
566    */
567   signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
568                                                      G_TYPE_DBUS_PROXY,
569                                                      G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
570                                                      G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
571                                                      NULL,
572                                                      NULL,
573                                                      NULL,
574                                                      G_TYPE_NONE,
575                                                      2,
576                                                      G_TYPE_VARIANT,
577                                                      G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
578
579   /**
580    * GDBusProxy::g-signal:
581    * @proxy: The #GDBusProxy emitting the signal.
582    * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
583    * @signal_name: The name of the signal.
584    * @parameters: A #GVariant tuple with parameters for the signal.
585    *
586    * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
587    *
588    * Since: 2.26
589    */
590   signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
591                                          G_TYPE_DBUS_PROXY,
592                                          G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
593                                          G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
594                                          NULL,
595                                          NULL,
596                                          NULL,
597                                          G_TYPE_NONE,
598                                          3,
599                                          G_TYPE_STRING,
600                                          G_TYPE_STRING,
601                                          G_TYPE_VARIANT);
602
603
604   g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
605 }
606
607 static void
608 g_dbus_proxy_init (GDBusProxy *proxy)
609 {
610   proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
611   proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
612   proxy->priv->signal_subscription_data->ref_count = 1;
613   proxy->priv->signal_subscription_data->proxy = proxy;
614   proxy->priv->properties = g_hash_table_new_full (g_str_hash,
615                                                    g_str_equal,
616                                                    g_free,
617                                                    (GDestroyNotify) g_variant_unref);
618 }
619
620 /* ---------------------------------------------------------------------------------------------------- */
621
622 static gint
623 property_name_sort_func (const gchar **a,
624                          const gchar **b)
625 {
626   return g_strcmp0 (*a, *b);
627 }
628
629 /**
630  * g_dbus_proxy_get_cached_property_names:
631  * @proxy: A #GDBusProxy.
632  *
633  * Gets the names of all cached properties on @proxy.
634  *
635  * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
636  *          @proxy has no cached properties. Free the returned array with
637  *          g_strfreev().
638  *
639  * Since: 2.26
640  */
641 gchar **
642 g_dbus_proxy_get_cached_property_names (GDBusProxy  *proxy)
643 {
644   gchar **names;
645   GPtrArray *p;
646   GHashTableIter iter;
647   const gchar *key;
648
649   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
650
651   G_LOCK (properties_lock);
652
653   names = NULL;
654   if (g_hash_table_size (proxy->priv->properties) == 0)
655     goto out;
656
657   p = g_ptr_array_new ();
658
659   g_hash_table_iter_init (&iter, proxy->priv->properties);
660   while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
661     g_ptr_array_add (p, g_strdup (key));
662   g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
663   g_ptr_array_add (p, NULL);
664
665   names = (gchar **) g_ptr_array_free (p, FALSE);
666
667  out:
668   G_UNLOCK (properties_lock);
669   return names;
670 }
671
672 static const GDBusPropertyInfo *
673 lookup_property_info_or_warn (GDBusProxy  *proxy,
674                               const gchar *property_name)
675 {
676   const GDBusPropertyInfo *info;
677
678   if (proxy->priv->expected_interface == NULL)
679     return NULL;
680
681   info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
682   if (info == NULL)
683     {
684       g_warning ("Trying to lookup property %s which isn't in expected interface %s",
685                  property_name,
686                  proxy->priv->expected_interface->name);
687     }
688
689   return info;
690 }
691
692 /**
693  * g_dbus_proxy_get_cached_property:
694  * @proxy: A #GDBusProxy.
695  * @property_name: Property name.
696  *
697  * Looks up the value for a property from the cache. This call does no
698  * blocking IO.
699  *
700  * If @proxy has an expected interface (see
701  * #GDBusProxy:g-interface-info), then @property_name (for existence)
702  * is checked against it.
703  *
704  * Returns: A reference to the #GVariant instance that holds the value
705  * for @property_name or %NULL if the value is not in the cache. The
706  * returned reference must be freed with g_variant_unref().
707  *
708  * Since: 2.26
709  */
710 GVariant *
711 g_dbus_proxy_get_cached_property (GDBusProxy   *proxy,
712                                   const gchar  *property_name)
713 {
714   GVariant *value;
715
716   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
717   g_return_val_if_fail (property_name != NULL, NULL);
718
719   G_LOCK (properties_lock);
720
721   value = g_hash_table_lookup (proxy->priv->properties, property_name);
722   if (value == NULL)
723     {
724       lookup_property_info_or_warn (proxy, property_name);
725       /* no difference */
726       goto out;
727     }
728
729   g_variant_ref (value);
730
731  out:
732   G_UNLOCK (properties_lock);
733   return value;
734 }
735
736 /**
737  * g_dbus_proxy_set_cached_property:
738  * @proxy: A #GDBusProxy
739  * @property_name: Property name.
740  * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
741  *
742  * If @value is not %NULL, sets the cached value for the property with
743  * name @property_name to the value in @value.
744  *
745  * If @value is %NULL, then the cached value is removed from the
746  * property cache.
747  *
748  * If @proxy has an expected interface (see
749  * #GDBusProxy:g-interface-info), then @property_name (for existence)
750  * and @value (for the type) is checked against it.
751  *
752  * If the @value #GVariant is floating, it is consumed. This allows
753  * convenient 'inline' use of g_variant_new(), e.g.
754  * |[
755  *  g_dbus_proxy_set_cached_property (proxy,
756  *                                    "SomeProperty",
757  *                                    g_variant_new ("(si)",
758  *                                                  "A String",
759  *                                                  42));
760  * ]|
761  *
762  * Normally you will not need to use this method since @proxy is
763  * tracking changes using the
764  * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
765  * D-Bus signal. However, for performance reasons an object may decide
766  * to not use this signal for some properties and instead use a
767  * proprietary out-of-band mechanism to transmit changes.
768  *
769  * As a concrete example, consider an object with a property
770  * <literal>ChatroomParticipants</literal> which is an array of
771  * strings. Instead of transmitting the same (long) array every time
772  * the property changes, it is more efficient to only transmit the
773  * delta using e.g. signals <literal>ChatroomParticipantJoined(String
774  * name)</literal> and <literal>ChatroomParticipantParted(String
775  * name)</literal>.
776  *
777  * Since: 2.26
778  */
779 void
780 g_dbus_proxy_set_cached_property (GDBusProxy   *proxy,
781                                   const gchar  *property_name,
782                                   GVariant     *value)
783 {
784   const GDBusPropertyInfo *info;
785
786   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
787   g_return_if_fail (property_name != NULL);
788
789   G_LOCK (properties_lock);
790
791   if (value != NULL)
792     {
793       info = lookup_property_info_or_warn (proxy, property_name);
794       if (info != NULL)
795         {
796           if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
797             {
798               g_warning ("Trying to set property %s of type %s but according to the expected "
799                          "interface the type is %s",
800                          property_name,
801                          g_variant_get_type_string (value),
802                          info->signature);
803               goto out;
804             }
805         }
806       g_hash_table_insert (proxy->priv->properties,
807                            g_strdup (property_name),
808                            g_variant_ref_sink (value));
809     }
810   else
811     {
812       g_hash_table_remove (proxy->priv->properties, property_name);
813     }
814
815  out:
816   G_UNLOCK (properties_lock);
817 }
818
819 /* ---------------------------------------------------------------------------------------------------- */
820
821 static void
822 on_signal_received (GDBusConnection *connection,
823                     const gchar     *sender_name,
824                     const gchar     *object_path,
825                     const gchar     *interface_name,
826                     const gchar     *signal_name,
827                     GVariant        *parameters,
828                     gpointer         user_data)
829 {
830   SignalSubscriptionData *data = user_data;
831   GDBusProxy *proxy;
832
833   G_LOCK (signal_subscription_lock);
834   proxy = data->proxy;
835   if (proxy == NULL)
836     {
837       G_UNLOCK (signal_subscription_lock);
838       goto out;
839     }
840   else
841     {
842       g_object_ref (proxy);
843       G_UNLOCK (signal_subscription_lock);
844     }
845
846   if (!proxy->priv->initialized)
847     goto out;
848
849   if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
850     goto out;
851
852   if (proxy->priv->expected_interface != NULL)
853     {
854       const GDBusSignalInfo *info;
855       GVariantType *expected_type;
856       info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
857       if (info == NULL)
858         goto out;
859       expected_type = _g_dbus_compute_complete_signature (info->args);
860       if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
861         {
862           g_variant_type_free (expected_type);
863           goto out;
864         }
865       g_variant_type_free (expected_type);
866     }
867
868   g_signal_emit (proxy,
869                  signals[SIGNAL_SIGNAL],
870                  0,
871                  sender_name,
872                  signal_name,
873                  parameters);
874  out:
875   if (proxy != NULL)
876     g_object_unref (proxy);
877 }
878
879 /* ---------------------------------------------------------------------------------------------------- */
880
881 /* must hold properties_lock */
882 static void
883 insert_property_checked (GDBusProxy  *proxy,
884                          gchar *property_name,
885                          GVariant *value)
886 {
887   if (proxy->priv->expected_interface != NULL)
888     {
889       const GDBusPropertyInfo *info;
890
891       info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
892       /* Ignore unknown properties */
893       if (info == NULL)
894         goto invalid;
895
896       /* Ignore properties with the wrong type */
897       if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
898         goto invalid;
899     }
900
901   g_hash_table_insert (proxy->priv->properties,
902                        property_name, /* adopts string */
903                        value); /* adopts value */
904
905   return;
906
907  invalid:
908   g_variant_unref (value);
909   g_free (property_name);
910 }
911
912 static void
913 on_properties_changed (GDBusConnection *connection,
914                        const gchar     *sender_name,
915                        const gchar     *object_path,
916                        const gchar     *interface_name,
917                        const gchar     *signal_name,
918                        GVariant        *parameters,
919                        gpointer         user_data)
920 {
921   SignalSubscriptionData *data = user_data;
922   GDBusProxy *proxy;
923   const gchar *interface_name_for_signal;
924   GVariant *changed_properties;
925   gchar **invalidated_properties;
926   GVariantIter iter;
927   gchar *key;
928   GVariant *value;
929   guint n;
930
931   G_LOCK (signal_subscription_lock);
932   proxy = data->proxy;
933   if (proxy == NULL)
934     {
935       G_UNLOCK (signal_subscription_lock);
936       goto out;
937     }
938   else
939     {
940       g_object_ref (proxy);
941       G_UNLOCK (signal_subscription_lock);
942     }
943
944   changed_properties = NULL;
945   invalidated_properties = NULL;
946
947   if (!proxy->priv->initialized)
948     goto out;
949
950   if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
951     goto out;
952
953   if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
954     {
955       g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
956                  g_variant_get_type_string (parameters));
957       goto out;
958     }
959
960   g_variant_get (parameters,
961                  "(&s@a{sv}^a&s)",
962                  &interface_name_for_signal,
963                  &changed_properties,
964                  &invalidated_properties);
965
966   if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
967     goto out;
968
969   G_LOCK (properties_lock);
970
971   g_variant_iter_init (&iter, changed_properties);
972   while (g_variant_iter_next (&iter, "{sv}", &key, &value))
973     {
974       insert_property_checked (proxy,
975                                key, /* adopts string */
976                                value); /* adopts value */
977     }
978
979   for (n = 0; invalidated_properties[n] != NULL; n++)
980     {
981       g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
982     }
983
984   G_UNLOCK (properties_lock);
985
986   /* emit signal */
987   g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
988                  0,
989                  changed_properties,
990                  invalidated_properties);
991
992  out:
993   if (changed_properties != NULL)
994     g_variant_unref (changed_properties);
995   g_free (invalidated_properties);
996   if (proxy != NULL)
997     g_object_unref (proxy);
998 }
999
1000 /* ---------------------------------------------------------------------------------------------------- */
1001
1002 static void
1003 process_get_all_reply (GDBusProxy *proxy,
1004                        GVariant   *result)
1005 {
1006   GVariantIter *iter;
1007   gchar *key;
1008   GVariant *value;
1009   guint num_properties;
1010
1011   if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1012     {
1013       g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1014                  g_variant_get_type_string (result));
1015       goto out;
1016     }
1017
1018   G_LOCK (properties_lock);
1019
1020   g_variant_get (result, "(a{sv})", &iter);
1021   while (g_variant_iter_next (iter, "{sv}", &key, &value))
1022     {
1023       insert_property_checked (proxy,
1024                                key, /* adopts string */
1025                                value); /* adopts value */
1026     }
1027   g_variant_iter_free (iter);
1028
1029   num_properties = g_hash_table_size (proxy->priv->properties);
1030   G_UNLOCK (properties_lock);
1031
1032   /* Synthesize ::g-properties-changed changed */
1033   if (num_properties > 0)
1034     {
1035       GVariant *changed_properties;
1036       const gchar *invalidated_properties[1] = {NULL};
1037
1038       g_variant_get (result,
1039                      "(@a{sv})",
1040                      &changed_properties);
1041       g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1042                      0,
1043                      changed_properties,
1044                      invalidated_properties);
1045       g_variant_unref (changed_properties);
1046     }
1047
1048  out:
1049   ;
1050 }
1051
1052 typedef struct
1053 {
1054   GDBusProxy *proxy;
1055   GCancellable *cancellable;
1056   gchar *name_owner;
1057 } LoadPropertiesOnNameOwnerChangedData;
1058
1059 static void
1060 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1061                                   GAsyncResult    *res,
1062                                   gpointer         user_data)
1063 {
1064   LoadPropertiesOnNameOwnerChangedData *data = user_data;
1065   GVariant *result;
1066   GError *error;
1067   gboolean cancelled;
1068
1069   cancelled = FALSE;
1070
1071   error = NULL;
1072   result = g_dbus_connection_call_finish (connection,
1073                                           res,
1074                                           &error);
1075   if (result == NULL)
1076     {
1077       if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1078         cancelled = TRUE;
1079       /* We just ignore if GetAll() is failing. Because this might happen
1080        * if the object has no properties at all. Or if the caller is
1081        * not authorized to see the properties.
1082        *
1083        * Either way, apps can know about this by using
1084        * get_cached_property_names() or get_cached_property().
1085        *
1086        * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1087        * fact that GetAll() failed
1088        */
1089       //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1090       g_error_free (error);
1091     }
1092
1093   /* and finally we can notify */
1094   if (!cancelled)
1095     {
1096       g_free (data->proxy->priv->name_owner);
1097       data->proxy->priv->name_owner = data->name_owner;
1098       data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1099
1100       G_LOCK (properties_lock);
1101       g_hash_table_remove_all (data->proxy->priv->properties);
1102       G_UNLOCK (properties_lock);
1103       if (result != NULL)
1104         {
1105           process_get_all_reply (data->proxy, result);
1106           g_variant_unref (result);
1107         }
1108
1109       g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1110     }
1111
1112   if (data->cancellable == data->proxy->priv->get_all_cancellable)
1113     data->proxy->priv->get_all_cancellable = NULL;
1114
1115   g_object_unref (data->proxy);
1116   g_object_unref (data->cancellable);
1117   g_free (data->name_owner);
1118   g_free (data);
1119 }
1120
1121 static void
1122 on_name_owner_changed (GDBusConnection *connection,
1123                        const gchar      *sender_name,
1124                        const gchar      *object_path,
1125                        const gchar      *interface_name,
1126                        const gchar      *signal_name,
1127                        GVariant         *parameters,
1128                        gpointer          user_data)
1129 {
1130   SignalSubscriptionData *data = user_data;
1131   GDBusProxy *proxy;
1132   const gchar *old_owner;
1133   const gchar *new_owner;
1134
1135   G_LOCK (signal_subscription_lock);
1136   proxy = data->proxy;
1137   if (proxy == NULL)
1138     {
1139       G_UNLOCK (signal_subscription_lock);
1140       goto out;
1141     }
1142   else
1143     {
1144       g_object_ref (proxy);
1145       G_UNLOCK (signal_subscription_lock);
1146     }
1147
1148   /* if we are already trying to load properties, cancel that */
1149   if (proxy->priv->get_all_cancellable != NULL)
1150     {
1151       g_cancellable_cancel (proxy->priv->get_all_cancellable);
1152       proxy->priv->get_all_cancellable = NULL;
1153     }
1154
1155   g_variant_get (parameters,
1156                  "(&s&s&s)",
1157                  NULL,
1158                  &old_owner,
1159                  &new_owner);
1160
1161   if (strlen (new_owner) == 0)
1162     {
1163       g_free (proxy->priv->name_owner);
1164       proxy->priv->name_owner = NULL;
1165
1166       G_LOCK (properties_lock);
1167
1168       /* Synthesize ::g-properties-changed changed */
1169       if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1170           g_hash_table_size (proxy->priv->properties) > 0)
1171         {
1172           GVariantBuilder builder;
1173           GPtrArray *invalidated_properties;
1174           GHashTableIter iter;
1175           const gchar *key;
1176
1177           /* Build changed_properties (always empty) and invalidated_properties ... */
1178           g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1179
1180           invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1181           g_hash_table_iter_init (&iter, proxy->priv->properties);
1182           while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1183             g_ptr_array_add (invalidated_properties, g_strdup (key));
1184           g_ptr_array_add (invalidated_properties, NULL);
1185
1186           /* ... throw out the properties ... */
1187           g_hash_table_remove_all (proxy->priv->properties);
1188
1189           G_UNLOCK (properties_lock);
1190
1191           /* ... and finally emit the ::g-properties-changed signal */
1192           g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1193                          0,
1194                          g_variant_builder_end (&builder) /* consumed */,
1195                          (const gchar* const *) invalidated_properties->pdata);
1196           g_ptr_array_unref (invalidated_properties);
1197         }
1198       else
1199         {
1200           G_UNLOCK (properties_lock);
1201         }
1202       g_object_notify (G_OBJECT (proxy), "g-name-owner");
1203     }
1204   else
1205     {
1206       /* ignore duplicates - this can happen when activating the service */
1207       if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1208         goto out;
1209
1210       if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1211         {
1212           g_free (proxy->priv->name_owner);
1213           proxy->priv->name_owner = g_strdup (new_owner);
1214           G_LOCK (properties_lock);
1215           g_hash_table_remove_all (proxy->priv->properties);
1216           G_UNLOCK (properties_lock);
1217           g_object_notify (G_OBJECT (proxy), "g-name-owner");
1218         }
1219       else
1220         {
1221           LoadPropertiesOnNameOwnerChangedData *data;
1222
1223           /* start loading properties.. only then emit notify::g-name-owner .. we
1224            * need to be able to cancel this in the event another NameOwnerChanged
1225            * signal suddenly happens
1226            */
1227
1228           g_assert (proxy->priv->get_all_cancellable == NULL);
1229           proxy->priv->get_all_cancellable = g_cancellable_new ();
1230           data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1231           data->proxy = g_object_ref (proxy);
1232           data->cancellable = proxy->priv->get_all_cancellable;
1233           data->name_owner = g_strdup (new_owner);
1234           g_dbus_connection_call (proxy->priv->connection,
1235                                   data->name_owner,
1236                                   proxy->priv->object_path,
1237                                   "org.freedesktop.DBus.Properties",
1238                                   "GetAll",
1239                                   g_variant_new ("(s)", proxy->priv->interface_name),
1240                                   G_VARIANT_TYPE ("(a{sv})"),
1241                                   G_DBUS_CALL_FLAGS_NONE,
1242                                   -1,           /* timeout */
1243                                   proxy->priv->get_all_cancellable,
1244                                   (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1245                                   data);
1246         }
1247     }
1248
1249  out:
1250   if (proxy != NULL)
1251     g_object_unref (proxy);
1252 }
1253
1254 /* ---------------------------------------------------------------------------------------------------- */
1255
1256 typedef struct
1257 {
1258   GDBusProxy *proxy;
1259   GCancellable *cancellable;
1260   GSimpleAsyncResult *simple;
1261 } AsyncInitData;
1262
1263 static void
1264 async_init_data_free (AsyncInitData *data)
1265 {
1266   g_object_unref (data->proxy);
1267   if (data->cancellable != NULL)
1268     g_object_unref (data->cancellable);
1269   g_object_unref (data->simple);
1270   g_free (data);
1271 }
1272
1273 static void
1274 async_init_get_all_cb (GDBusConnection *connection,
1275                        GAsyncResult    *res,
1276                        gpointer         user_data)
1277 {
1278   AsyncInitData *data = user_data;
1279   GVariant *result;
1280   GError *error;
1281
1282   error = NULL;
1283   result = g_dbus_connection_call_finish (connection,
1284                                           res,
1285                                           &error);
1286   if (result == NULL)
1287     {
1288       /* We just ignore if GetAll() is failing. Because this might happen
1289        * if the object has no properties at all. Or if the caller is
1290        * not authorized to see the properties.
1291        *
1292        * Either way, apps can know about this by using
1293        * get_cached_property_names() or get_cached_property().
1294        *
1295        * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1296        * fact that GetAll() failed
1297        */
1298       //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1299       g_error_free (error);
1300     }
1301   else
1302     {
1303       g_simple_async_result_set_op_res_gpointer (data->simple,
1304                                                  result,
1305                                                  (GDestroyNotify) g_variant_unref);
1306     }
1307
1308   g_simple_async_result_complete_in_idle (data->simple);
1309   async_init_data_free (data);
1310 }
1311
1312
1313 static void
1314 async_init_get_name_owner_cb (GDBusConnection *connection,
1315                               GAsyncResult    *res,
1316                               gpointer         user_data)
1317 {
1318   AsyncInitData *data = user_data;
1319
1320   if (res != NULL)
1321     {
1322       GError *error;
1323       GVariant *result;
1324
1325       error = NULL;
1326       result = g_dbus_connection_call_finish (connection,
1327                                               res,
1328                                               &error);
1329       if (result == NULL)
1330         {
1331           if (error->domain == G_DBUS_ERROR &&
1332               error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1333             {
1334               g_error_free (error);
1335             }
1336           else
1337             {
1338               g_simple_async_result_take_error (data->simple, error);
1339               g_simple_async_result_complete_in_idle (data->simple);
1340               async_init_data_free (data);
1341               goto out;
1342             }
1343         }
1344       else
1345         {
1346           g_variant_get (result,
1347                          "(s)",
1348                          &data->proxy->priv->name_owner);
1349           g_variant_unref (result);
1350         }
1351     }
1352
1353   if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1354     {
1355       /* load all properties asynchronously */
1356       g_dbus_connection_call (data->proxy->priv->connection,
1357                               data->proxy->priv->name_owner,
1358                               data->proxy->priv->object_path,
1359                               "org.freedesktop.DBus.Properties",
1360                               "GetAll",
1361                               g_variant_new ("(s)", data->proxy->priv->interface_name),
1362                               G_VARIANT_TYPE ("(a{sv})"),
1363                               G_DBUS_CALL_FLAGS_NONE,
1364                               -1,           /* timeout */
1365                               data->cancellable,
1366                               (GAsyncReadyCallback) async_init_get_all_cb,
1367                               data);
1368     }
1369   else
1370     {
1371       g_simple_async_result_complete_in_idle (data->simple);
1372       async_init_data_free (data);
1373     }
1374
1375  out:
1376   ;
1377 }
1378
1379 static void
1380 async_init_call_get_name_owner (AsyncInitData *data)
1381 {
1382   g_dbus_connection_call (data->proxy->priv->connection,
1383                           "org.freedesktop.DBus",  /* name */
1384                           "/org/freedesktop/DBus", /* object path */
1385                           "org.freedesktop.DBus",  /* interface */
1386                           "GetNameOwner",
1387                           g_variant_new ("(s)",
1388                                          data->proxy->priv->name),
1389                           G_VARIANT_TYPE ("(s)"),
1390                           G_DBUS_CALL_FLAGS_NONE,
1391                           -1,           /* timeout */
1392                           data->cancellable,
1393                           (GAsyncReadyCallback) async_init_get_name_owner_cb,
1394                           data);
1395 }
1396
1397 static void
1398 async_init_start_service_by_name_cb (GDBusConnection *connection,
1399                                      GAsyncResult    *res,
1400                                      gpointer         user_data)
1401 {
1402   AsyncInitData *data = user_data;
1403   GError *error;
1404   GVariant *result;
1405
1406   error = NULL;
1407   result = g_dbus_connection_call_finish (connection,
1408                                           res,
1409                                           &error);
1410   if (result == NULL)
1411     {
1412       /* Errors are not unexpected; the bus will reply e.g.
1413        *
1414        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1415        *   was not provided by any .service files
1416        *
1417        * This doesn't mean that the name doesn't have an owner, just
1418        * that it's not provided by a .service file. So just proceed to
1419        * invoke GetNameOwner() if dealing with that error.
1420        */
1421       if (error->domain == G_DBUS_ERROR &&
1422           error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1423         {
1424           g_error_free (error);
1425         }
1426       else
1427         {
1428           g_prefix_error (&error,
1429                           _("Error calling StartServiceByName for %s: "),
1430                           data->proxy->priv->name);
1431           goto failed;
1432         }
1433     }
1434   else
1435     {
1436       guint32 start_service_result;
1437       g_variant_get (result,
1438                      "(u)",
1439                      &start_service_result);
1440       g_variant_unref (result);
1441       if (start_service_result == 1 ||  /* DBUS_START_REPLY_SUCCESS */
1442           start_service_result == 2)    /* DBUS_START_REPLY_ALREADY_RUNNING */
1443         {
1444           /* continue to invoke GetNameOwner() */
1445         }
1446       else
1447         {
1448           error = g_error_new (G_IO_ERROR,
1449                                G_IO_ERROR_FAILED,
1450                                _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1451                                start_service_result,
1452                                data->proxy->priv->name);
1453           goto failed;
1454         }
1455     }
1456
1457   async_init_call_get_name_owner (data);
1458   return;
1459
1460  failed:
1461   g_warn_if_fail (error != NULL);
1462   g_simple_async_result_take_error (data->simple, error);
1463   g_simple_async_result_complete_in_idle (data->simple);
1464   async_init_data_free (data);
1465 }
1466
1467 static void
1468 async_init_call_start_service_by_name (AsyncInitData *data)
1469 {
1470   g_dbus_connection_call (data->proxy->priv->connection,
1471                           "org.freedesktop.DBus",  /* name */
1472                           "/org/freedesktop/DBus", /* object path */
1473                           "org.freedesktop.DBus",  /* interface */
1474                           "StartServiceByName",
1475                           g_variant_new ("(su)",
1476                                          data->proxy->priv->name,
1477                                          0),
1478                           G_VARIANT_TYPE ("(u)"),
1479                           G_DBUS_CALL_FLAGS_NONE,
1480                           -1,           /* timeout */
1481                           data->cancellable,
1482                           (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1483                           data);
1484 }
1485
1486 static void
1487 async_initable_init_second_async (GAsyncInitable      *initable,
1488                                   gint                 io_priority,
1489                                   GCancellable        *cancellable,
1490                                   GAsyncReadyCallback  callback,
1491                                   gpointer             user_data)
1492 {
1493   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1494   AsyncInitData *data;
1495
1496   data = g_new0 (AsyncInitData, 1);
1497   data->proxy = g_object_ref (proxy);
1498   data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1499   data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1500                                             callback,
1501                                             user_data,
1502                                             NULL);
1503
1504   /* Check name ownership asynchronously - possibly also start the service */
1505   if (proxy->priv->name == NULL)
1506     {
1507       /* Do nothing */
1508       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1509     }
1510   else if (g_dbus_is_unique_name (proxy->priv->name))
1511     {
1512       proxy->priv->name_owner = g_strdup (proxy->priv->name);
1513       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1514     }
1515   else
1516     {
1517       if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1518         {
1519           async_init_call_get_name_owner (data);
1520         }
1521       else
1522         {
1523           async_init_call_start_service_by_name (data);
1524         }
1525     }
1526 }
1527
1528 static gboolean
1529 async_initable_init_second_finish (GAsyncInitable  *initable,
1530                                    GAsyncResult    *res,
1531                                    GError         **error)
1532 {
1533   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1534   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1535   GVariant *result;
1536   gboolean ret;
1537
1538   ret = FALSE;
1539
1540   if (g_simple_async_result_propagate_error (simple, error))
1541     goto out;
1542
1543   result = g_simple_async_result_get_op_res_gpointer (simple);
1544   if (result != NULL)
1545     {
1546       process_get_all_reply (proxy, result);
1547     }
1548
1549   ret = TRUE;
1550
1551  out:
1552   proxy->priv->initialized = TRUE;
1553   return ret;
1554 }
1555
1556 /* ---------------------------------------------------------------------------------------------------- */
1557
1558 static void
1559 async_initable_init_first (GAsyncInitable *initable)
1560 {
1561   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1562
1563   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1564     {
1565       /* subscribe to PropertiesChanged() */
1566       proxy->priv->properties_changed_subscription_id =
1567         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1568                                             proxy->priv->name,
1569                                             "org.freedesktop.DBus.Properties",
1570                                             "PropertiesChanged",
1571                                             proxy->priv->object_path,
1572                                             proxy->priv->interface_name,
1573                                             G_DBUS_SIGNAL_FLAGS_NONE,
1574                                             on_properties_changed,
1575                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1576                                             (GDestroyNotify) signal_subscription_unref);
1577     }
1578
1579   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1580     {
1581       /* subscribe to all signals for the object */
1582       proxy->priv->signals_subscription_id =
1583         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1584                                             proxy->priv->name,
1585                                             proxy->priv->interface_name,
1586                                             NULL,                        /* member */
1587                                             proxy->priv->object_path,
1588                                             NULL,                        /* arg0 */
1589                                             G_DBUS_SIGNAL_FLAGS_NONE,
1590                                             on_signal_received,
1591                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1592                                             (GDestroyNotify) signal_subscription_unref);
1593     }
1594
1595   if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1596     {
1597       proxy->priv->name_owner_changed_subscription_id =
1598         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1599                                             "org.freedesktop.DBus",  /* name */
1600                                             "org.freedesktop.DBus",  /* interface */
1601                                             "NameOwnerChanged",      /* signal name */
1602                                             "/org/freedesktop/DBus", /* path */
1603                                             proxy->priv->name,       /* arg0 */
1604                                             G_DBUS_SIGNAL_FLAGS_NONE,
1605                                             on_name_owner_changed,
1606                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1607                                             (GDestroyNotify) signal_subscription_unref);
1608     }
1609 }
1610
1611 /* ---------------------------------------------------------------------------------------------------- */
1612
1613 /* initialization is split into two parts - the first is the
1614  * non-blocing part that requires the callers GMainContext - the
1615  * second is a blocking part async part that doesn't require the
1616  * callers GMainContext.. we do this split so the code can be reused
1617  * in the GInitable implementation below.
1618  *
1619  * Note that obtaining a GDBusConnection is not shared between the two
1620  * paths.
1621  */
1622
1623 typedef struct
1624 {
1625   GDBusProxy          *proxy;
1626   gint                 io_priority;
1627   GCancellable        *cancellable;
1628   GAsyncReadyCallback  callback;
1629   gpointer             user_data;
1630 } GetConnectionData;
1631
1632 static void
1633 get_connection_cb (GObject       *source_object,
1634                    GAsyncResult  *res,
1635                    gpointer       user_data)
1636 {
1637   GetConnectionData *data = user_data;
1638   GError *error;
1639
1640   error = NULL;
1641   data->proxy->priv->connection = g_bus_get_finish (res, &error);
1642   if (data->proxy->priv->connection == NULL)
1643     {
1644       GSimpleAsyncResult *simple;
1645       simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1646                                           data->callback,
1647                                           data->user_data,
1648                                           NULL);
1649       g_simple_async_result_take_error (simple, error);
1650       g_simple_async_result_complete_in_idle (simple);
1651       g_object_unref (simple);
1652     }
1653   else
1654     {
1655       async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1656       async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1657                                         data->io_priority,
1658                                         data->cancellable,
1659                                         data->callback,
1660                                         data->user_data);
1661     }
1662
1663   if (data->cancellable != NULL)
1664     g_object_unref (data->cancellable);
1665
1666   g_object_unref (data->proxy);
1667   g_free (data);
1668 }
1669
1670 static void
1671 async_initable_init_async (GAsyncInitable      *initable,
1672                            gint                 io_priority,
1673                            GCancellable        *cancellable,
1674                            GAsyncReadyCallback  callback,
1675                            gpointer             user_data)
1676 {
1677   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1678
1679   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1680     {
1681       GetConnectionData *data;
1682
1683       g_assert (proxy->priv->connection == NULL);
1684
1685       data = g_new0 (GetConnectionData, 1);
1686       data->proxy = g_object_ref (proxy);
1687       data->io_priority = io_priority;
1688       data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1689       data->callback = callback;
1690       data->user_data = user_data;
1691       g_bus_get (proxy->priv->bus_type,
1692                  cancellable,
1693                  get_connection_cb,
1694                  data);
1695     }
1696   else
1697     {
1698       async_initable_init_first (initable);
1699       async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1700     }
1701 }
1702
1703 static gboolean
1704 async_initable_init_finish (GAsyncInitable  *initable,
1705                             GAsyncResult    *res,
1706                             GError         **error)
1707 {
1708   return async_initable_init_second_finish (initable, res, error);
1709 }
1710
1711 static void
1712 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1713 {
1714   async_initable_iface->init_async = async_initable_init_async;
1715   async_initable_iface->init_finish = async_initable_init_finish;
1716 }
1717
1718 /* ---------------------------------------------------------------------------------------------------- */
1719
1720 typedef struct
1721 {
1722   GMainContext *context;
1723   GMainLoop *loop;
1724   GAsyncResult *res;
1725 } InitableAsyncInitableData;
1726
1727 static void
1728 async_initable_init_async_cb (GObject      *source_object,
1729                               GAsyncResult *res,
1730                               gpointer      user_data)
1731 {
1732   InitableAsyncInitableData *data = user_data;
1733   data->res = g_object_ref (res);
1734   g_main_loop_quit (data->loop);
1735 }
1736
1737 /* Simply reuse the GAsyncInitable implementation but run the first
1738  * part (that is non-blocking and requires the callers GMainContext)
1739  * with the callers GMainContext.. and the second with a private
1740  * GMainContext (bug 621310 is slightly related).
1741  *
1742  * Note that obtaining a GDBusConnection is not shared between the two
1743  * paths.
1744  */
1745 static gboolean
1746 initable_init (GInitable     *initable,
1747                GCancellable  *cancellable,
1748                GError       **error)
1749 {
1750   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1751   InitableAsyncInitableData *data;
1752   gboolean ret;
1753
1754   ret = FALSE;
1755
1756   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1757     {
1758       g_assert (proxy->priv->connection == NULL);
1759       proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1760                                                 cancellable,
1761                                                 error);
1762       if (proxy->priv->connection == NULL)
1763         goto out;
1764     }
1765
1766   async_initable_init_first (G_ASYNC_INITABLE (initable));
1767
1768   data = g_new0 (InitableAsyncInitableData, 1);
1769   data->context = g_main_context_new ();
1770   data->loop = g_main_loop_new (data->context, FALSE);
1771
1772   g_main_context_push_thread_default (data->context);
1773
1774   async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1775                                     G_PRIORITY_DEFAULT,
1776                                     cancellable,
1777                                     async_initable_init_async_cb,
1778                                     data);
1779
1780   g_main_loop_run (data->loop);
1781
1782   ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1783                                            data->res,
1784                                            error);
1785
1786   g_main_context_pop_thread_default (data->context);
1787
1788   g_main_context_unref (data->context);
1789   g_main_loop_unref (data->loop);
1790   g_object_unref (data->res);
1791   g_free (data);
1792
1793  out:
1794
1795   return ret;
1796 }
1797
1798 static void
1799 initable_iface_init (GInitableIface *initable_iface)
1800 {
1801   initable_iface->init = initable_init;
1802 }
1803
1804 /* ---------------------------------------------------------------------------------------------------- */
1805
1806 /**
1807  * g_dbus_proxy_new:
1808  * @connection: A #GDBusConnection.
1809  * @flags: Flags used when constructing the proxy.
1810  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1811  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1812  * @object_path: An object path.
1813  * @interface_name: A D-Bus interface name.
1814  * @cancellable: A #GCancellable or %NULL.
1815  * @callback: Callback function to invoke when the proxy is ready.
1816  * @user_data: User data to pass to @callback.
1817  *
1818  * Creates a proxy for accessing @interface_name on the remote object
1819  * at @object_path owned by @name at @connection and asynchronously
1820  * loads D-Bus properties unless the
1821  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1822  * the #GDBusProxy::g-properties-changed signal to get notified about
1823  * property changes.
1824  *
1825  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1826  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1827  * to handle signals from the remote object.
1828  *
1829  * If @name is a well-known name and the
1830  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1831  * owner currently exists, the message bus will be requested to launch
1832  * a name owner for the name.
1833  *
1834  * This is a failable asynchronous constructor - when the proxy is
1835  * ready, @callback will be invoked and you can use
1836  * g_dbus_proxy_new_finish() to get the result.
1837  *
1838  * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1839  *
1840  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1841  *
1842  * Since: 2.26
1843  */
1844 void
1845 g_dbus_proxy_new (GDBusConnection     *connection,
1846                   GDBusProxyFlags      flags,
1847                   GDBusInterfaceInfo  *info,
1848                   const gchar         *name,
1849                   const gchar         *object_path,
1850                   const gchar         *interface_name,
1851                   GCancellable        *cancellable,
1852                   GAsyncReadyCallback  callback,
1853                   gpointer             user_data)
1854 {
1855   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1856   g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1857   g_return_if_fail (g_variant_is_object_path (object_path));
1858   g_return_if_fail (g_dbus_is_interface_name (interface_name));
1859
1860   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1861                               G_PRIORITY_DEFAULT,
1862                               cancellable,
1863                               callback,
1864                               user_data,
1865                               "g-flags", flags,
1866                               "g-interface-info", info,
1867                               "g-name", name,
1868                               "g-connection", connection,
1869                               "g-object-path", object_path,
1870                               "g-interface-name", interface_name,
1871                               NULL);
1872 }
1873
1874 /**
1875  * g_dbus_proxy_new_finish:
1876  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1877  * @error: Return location for error or %NULL.
1878  *
1879  * Finishes creating a #GDBusProxy.
1880  *
1881  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1882  *
1883  * Since: 2.26
1884  */
1885 GDBusProxy *
1886 g_dbus_proxy_new_finish (GAsyncResult  *res,
1887                          GError       **error)
1888 {
1889   GObject *object;
1890   GObject *source_object;
1891
1892   source_object = g_async_result_get_source_object (res);
1893   g_assert (source_object != NULL);
1894
1895   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1896                                         res,
1897                                         error);
1898   g_object_unref (source_object);
1899
1900   if (object != NULL)
1901     return G_DBUS_PROXY (object);
1902   else
1903     return NULL;
1904 }
1905
1906 /**
1907  * g_dbus_proxy_new_sync:
1908  * @connection: A #GDBusConnection.
1909  * @flags: Flags used when constructing the proxy.
1910  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1911  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1912  * @object_path: An object path.
1913  * @interface_name: A D-Bus interface name.
1914  * @cancellable: (allow-none): A #GCancellable or %NULL.
1915  * @error: (allow-none): Return location for error or %NULL.
1916  *
1917  * Creates a proxy for accessing @interface_name on the remote object
1918  * at @object_path owned by @name at @connection and synchronously
1919  * loads D-Bus properties unless the
1920  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1921  *
1922  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1923  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1924  * to handle signals from the remote object.
1925  *
1926  * If @name is a well-known name and the
1927  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1928  * owner currently exists, the message bus will be requested to launch
1929  * a name owner for the name.
1930  *
1931  * This is a synchronous failable constructor. See g_dbus_proxy_new()
1932  * and g_dbus_proxy_new_finish() for the asynchronous version.
1933  *
1934  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1935  *
1936  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1937  *
1938  * Since: 2.26
1939  */
1940 GDBusProxy *
1941 g_dbus_proxy_new_sync (GDBusConnection     *connection,
1942                        GDBusProxyFlags      flags,
1943                        GDBusInterfaceInfo  *info,
1944                        const gchar         *name,
1945                        const gchar         *object_path,
1946                        const gchar         *interface_name,
1947                        GCancellable        *cancellable,
1948                        GError             **error)
1949 {
1950   GInitable *initable;
1951
1952   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1953   g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1954                         g_dbus_is_name (name), NULL);
1955   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1956   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1957
1958   initable = g_initable_new (G_TYPE_DBUS_PROXY,
1959                              cancellable,
1960                              error,
1961                              "g-flags", flags,
1962                              "g-interface-info", info,
1963                              "g-name", name,
1964                              "g-connection", connection,
1965                              "g-object-path", object_path,
1966                              "g-interface-name", interface_name,
1967                              NULL);
1968   if (initable != NULL)
1969     return G_DBUS_PROXY (initable);
1970   else
1971     return NULL;
1972 }
1973
1974 /* ---------------------------------------------------------------------------------------------------- */
1975
1976 /**
1977  * g_dbus_proxy_new_for_bus:
1978  * @bus_type: A #GBusType.
1979  * @flags: Flags used when constructing the proxy.
1980  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1981  * @name: A bus name (well-known or unique).
1982  * @object_path: An object path.
1983  * @interface_name: A D-Bus interface name.
1984  * @cancellable: A #GCancellable or %NULL.
1985  * @callback: Callback function to invoke when the proxy is ready.
1986  * @user_data: User data to pass to @callback.
1987  *
1988  * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1989  *
1990  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1991  *
1992  * Since: 2.26
1993  */
1994 void
1995 g_dbus_proxy_new_for_bus (GBusType             bus_type,
1996                           GDBusProxyFlags      flags,
1997                           GDBusInterfaceInfo  *info,
1998                           const gchar         *name,
1999                           const gchar         *object_path,
2000                           const gchar         *interface_name,
2001                           GCancellable        *cancellable,
2002                           GAsyncReadyCallback  callback,
2003                           gpointer             user_data)
2004 {
2005   g_return_if_fail (g_dbus_is_name (name));
2006   g_return_if_fail (g_variant_is_object_path (object_path));
2007   g_return_if_fail (g_dbus_is_interface_name (interface_name));
2008
2009   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2010                               G_PRIORITY_DEFAULT,
2011                               cancellable,
2012                               callback,
2013                               user_data,
2014                               "g-flags", flags,
2015                               "g-interface-info", info,
2016                               "g-name", name,
2017                               "g-bus-type", bus_type,
2018                               "g-object-path", object_path,
2019                               "g-interface-name", interface_name,
2020                               NULL);
2021 }
2022
2023 /**
2024  * g_dbus_proxy_new_for_bus_finish:
2025  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2026  * @error: Return location for error or %NULL.
2027  *
2028  * Finishes creating a #GDBusProxy.
2029  *
2030  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2031  *
2032  * Since: 2.26
2033  */
2034 GDBusProxy *
2035 g_dbus_proxy_new_for_bus_finish (GAsyncResult  *res,
2036                                  GError       **error)
2037 {
2038   return g_dbus_proxy_new_finish (res, error);
2039 }
2040
2041 /**
2042  * g_dbus_proxy_new_for_bus_sync:
2043  * @bus_type: A #GBusType.
2044  * @flags: Flags used when constructing the proxy.
2045  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2046  *        that @proxy conforms to or %NULL.
2047  * @name: A bus name (well-known or unique).
2048  * @object_path: An object path.
2049  * @interface_name: A D-Bus interface name.
2050  * @cancellable: A #GCancellable or %NULL.
2051  * @error: Return location for error or %NULL.
2052  *
2053  * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2054  *
2055  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2056  *
2057  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2058  *
2059  * Since: 2.26
2060  */
2061 GDBusProxy *
2062 g_dbus_proxy_new_for_bus_sync (GBusType             bus_type,
2063                                GDBusProxyFlags      flags,
2064                                GDBusInterfaceInfo  *info,
2065                                const gchar         *name,
2066                                const gchar         *object_path,
2067                                const gchar         *interface_name,
2068                                GCancellable        *cancellable,
2069                                GError             **error)
2070 {
2071   GInitable *initable;
2072
2073   g_return_val_if_fail (g_dbus_is_name (name), NULL);
2074   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2075   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2076
2077   initable = g_initable_new (G_TYPE_DBUS_PROXY,
2078                              cancellable,
2079                              error,
2080                              "g-flags", flags,
2081                              "g-interface-info", info,
2082                              "g-name", name,
2083                              "g-bus-type", bus_type,
2084                              "g-object-path", object_path,
2085                              "g-interface-name", interface_name,
2086                              NULL);
2087   if (initable != NULL)
2088     return G_DBUS_PROXY (initable);
2089   else
2090     return NULL;
2091 }
2092
2093 /* ---------------------------------------------------------------------------------------------------- */
2094
2095 /**
2096  * g_dbus_proxy_get_connection:
2097  * @proxy: A #GDBusProxy.
2098  *
2099  * Gets the connection @proxy is for.
2100  *
2101  * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2102  *
2103  * Since: 2.26
2104  */
2105 GDBusConnection *
2106 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2107 {
2108   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2109   return proxy->priv->connection;
2110 }
2111
2112 /**
2113  * g_dbus_proxy_get_flags:
2114  * @proxy: A #GDBusProxy.
2115  *
2116  * Gets the flags that @proxy was constructed with.
2117  *
2118  * Returns: Flags from the #GDBusProxyFlags enumeration.
2119  *
2120  * Since: 2.26
2121  */
2122 GDBusProxyFlags
2123 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2124 {
2125   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2126   return proxy->priv->flags;
2127 }
2128
2129 /**
2130  * g_dbus_proxy_get_name:
2131  * @proxy: A #GDBusProxy.
2132  *
2133  * Gets the name that @proxy was constructed for.
2134  *
2135  * Returns: A string owned by @proxy. Do not free.
2136  *
2137  * Since: 2.26
2138  */
2139 const gchar *
2140 g_dbus_proxy_get_name (GDBusProxy *proxy)
2141 {
2142   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2143   return proxy->priv->name;
2144 }
2145
2146 /**
2147  * g_dbus_proxy_get_name_owner:
2148  * @proxy: A #GDBusProxy.
2149  *
2150  * The unique name that owns the name that @proxy is for or %NULL if
2151  * no-one currently owns that name. You may connect to the
2152  * #GObject::notify signal to track changes to the
2153  * #GDBusProxy:g-name-owner property.
2154  *
2155  * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2156  *
2157  * Since: 2.26
2158  */
2159 gchar *
2160 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2161 {
2162   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2163   return g_strdup (proxy->priv->name_owner);
2164 }
2165
2166 /**
2167  * g_dbus_proxy_get_object_path:
2168  * @proxy: A #GDBusProxy.
2169  *
2170  * Gets the object path @proxy is for.
2171  *
2172  * Returns: A string owned by @proxy. Do not free.
2173  *
2174  * Since: 2.26
2175  */
2176 const gchar *
2177 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2178 {
2179   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2180   return proxy->priv->object_path;
2181 }
2182
2183 /**
2184  * g_dbus_proxy_get_interface_name:
2185  * @proxy: A #GDBusProxy.
2186  *
2187  * Gets the D-Bus interface name @proxy is for.
2188  *
2189  * Returns: A string owned by @proxy. Do not free.
2190  *
2191  * Since: 2.26
2192  */
2193 const gchar *
2194 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2195 {
2196   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2197   return proxy->priv->interface_name;
2198 }
2199
2200 /**
2201  * g_dbus_proxy_get_default_timeout:
2202  * @proxy: A #GDBusProxy.
2203  *
2204  * Gets the timeout to use if -1 (specifying default timeout) is
2205  * passed as @timeout_msec in the g_dbus_proxy_call() and
2206  * g_dbus_proxy_call_sync() functions.
2207  *
2208  * See the #GDBusProxy:g-default-timeout property for more details.
2209  *
2210  * Returns: Timeout to use for @proxy.
2211  *
2212  * Since: 2.26
2213  */
2214 gint
2215 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2216 {
2217   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2218   return proxy->priv->timeout_msec;
2219 }
2220
2221 /**
2222  * g_dbus_proxy_set_default_timeout:
2223  * @proxy: A #GDBusProxy.
2224  * @timeout_msec: Timeout in milliseconds.
2225  *
2226  * Sets the timeout to use if -1 (specifying default timeout) is
2227  * passed as @timeout_msec in the g_dbus_proxy_call() and
2228  * g_dbus_proxy_call_sync() functions.
2229  *
2230  * See the #GDBusProxy:g-default-timeout property for more details.
2231  *
2232  * Since: 2.26
2233  */
2234 void
2235 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2236                                   gint        timeout_msec)
2237 {
2238   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2239   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2240
2241   /* TODO: locking? */
2242   if (proxy->priv->timeout_msec != timeout_msec)
2243     {
2244       proxy->priv->timeout_msec = timeout_msec;
2245       g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2246     }
2247 }
2248
2249 /**
2250  * g_dbus_proxy_get_interface_info:
2251  * @proxy: A #GDBusProxy
2252  *
2253  * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2254  * interface that @proxy conforms to.
2255  *
2256  * See the #GDBusProxy:g-interface-info property for more details.
2257  *
2258  * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2259  * object, it is owned by @proxy.
2260  *
2261  * Since: 2.26
2262  */
2263 GDBusInterfaceInfo *
2264 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2265 {
2266   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2267   return proxy->priv->expected_interface;
2268 }
2269
2270 /**
2271  * g_dbus_proxy_set_interface_info:
2272  * @proxy: A #GDBusProxy
2273  * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2274  *
2275  * Ensure that interactions with @proxy conform to the given
2276  * interface.  For example, when completing a method call, if the type
2277  * signature of the message isn't what's expected, the given #GError
2278  * is set.  Signals that have a type signature mismatch are simply
2279  * dropped.
2280  *
2281  * See the #GDBusProxy:g-interface-info property for more details.
2282  *
2283  * Since: 2.26
2284  */
2285 void
2286 g_dbus_proxy_set_interface_info (GDBusProxy         *proxy,
2287                                  GDBusInterfaceInfo *info)
2288 {
2289   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2290   if (proxy->priv->expected_interface != NULL)
2291     {
2292       g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2293       g_dbus_interface_info_unref (proxy->priv->expected_interface);
2294     }
2295   proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2296   if (proxy->priv->expected_interface != NULL)
2297     g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2298 }
2299
2300 /* ---------------------------------------------------------------------------------------------------- */
2301
2302 static gboolean
2303 maybe_split_method_name (const gchar  *method_name,
2304                          gchar       **out_interface_name,
2305                          const gchar **out_method_name)
2306 {
2307   gboolean was_split;
2308
2309   was_split = FALSE;
2310   g_assert (out_interface_name != NULL);
2311   g_assert (out_method_name != NULL);
2312   *out_interface_name = NULL;
2313   *out_method_name = NULL;
2314
2315   if (strchr (method_name, '.') != NULL)
2316     {
2317       gchar *p;
2318       gchar *last_dot;
2319
2320       p = g_strdup (method_name);
2321       last_dot = strrchr (p, '.');
2322       *last_dot = '\0';
2323
2324       *out_interface_name = p;
2325       *out_method_name = last_dot + 1;
2326
2327       was_split = TRUE;
2328     }
2329
2330   return was_split;
2331 }
2332
2333 typedef struct
2334 {
2335   GVariant *value;
2336 #ifdef G_OS_UNIX
2337   GUnixFDList *fd_list;
2338 #endif
2339 } ReplyData;
2340
2341 static void
2342 reply_data_free (ReplyData *data)
2343 {
2344   g_variant_unref (data->value);
2345 #ifdef G_OS_UNIX
2346   if (data->fd_list != NULL)
2347     g_object_unref (data->fd_list);
2348 #endif
2349   g_slice_free (ReplyData, data);
2350 }
2351
2352 static void
2353 reply_cb (GDBusConnection *connection,
2354           GAsyncResult    *res,
2355           gpointer         user_data)
2356 {
2357   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2358   GVariant *value;
2359   GError *error;
2360 #ifdef G_OS_UNIX
2361   GUnixFDList *fd_list;
2362 #endif
2363
2364   error = NULL;
2365 #ifdef G_OS_UNIX
2366   value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2367                                                            &fd_list,
2368                                                            res,
2369                                                            &error);
2370 #else
2371   value = g_dbus_connection_call_finish (connection,
2372                                          res,
2373                                          &error);
2374 #endif
2375   if (error != NULL)
2376     {
2377       g_simple_async_result_take_error (simple, error);
2378     }
2379   else
2380     {
2381       ReplyData *data;
2382       data = g_slice_new0 (ReplyData);
2383       data->value = value;
2384 #ifdef G_OS_UNIX
2385       data->fd_list = fd_list;
2386 #endif
2387       g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2388     }
2389
2390   /* no need to complete in idle since the method GDBusConnection already does */
2391   g_simple_async_result_complete (simple);
2392   g_object_unref (simple);
2393 }
2394
2395 static const GDBusMethodInfo *
2396 lookup_method_info_or_warn (GDBusProxy  *proxy,
2397                             const gchar *method_name)
2398 {
2399   const GDBusMethodInfo *info;
2400
2401   if (proxy->priv->expected_interface == NULL)
2402     return NULL;
2403
2404   info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2405   if (info == NULL)
2406     {
2407       g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2408                  method_name, proxy->priv->expected_interface->name);
2409     }
2410
2411   return info;
2412 }
2413
2414 static const gchar *
2415 get_destination_for_call (GDBusProxy *proxy)
2416 {
2417   const gchar *ret;
2418
2419   ret = NULL;
2420
2421   /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2422    * is never NULL and always the same as proxy->priv->name. We use this
2423    * knowledge to avoid checking if proxy->priv->name is a unique or
2424    * well-known name.
2425    */
2426   ret = proxy->priv->name_owner;
2427   if (ret != NULL)
2428     goto out;
2429
2430   if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2431     goto out;
2432
2433   ret = proxy->priv->name;
2434
2435  out:
2436   return ret;
2437 }
2438
2439 /* ---------------------------------------------------------------------------------------------------- */
2440
2441 static void
2442 g_dbus_proxy_call_internal (GDBusProxy          *proxy,
2443                             const gchar         *method_name,
2444                             GVariant            *parameters,
2445                             GDBusCallFlags       flags,
2446                             gint                 timeout_msec,
2447                             GUnixFDList         *fd_list,
2448                             GCancellable        *cancellable,
2449                             GAsyncReadyCallback  callback,
2450                             gpointer             user_data)
2451 {
2452   GSimpleAsyncResult *simple;
2453   gboolean was_split;
2454   gchar *split_interface_name;
2455   const gchar *split_method_name;
2456   const gchar *target_method_name;
2457   const gchar *target_interface_name;
2458   const gchar *destination;
2459   GVariantType *reply_type;
2460
2461   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2462   g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2463   g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2464   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2465 #ifdef G_OS_UNIX
2466   g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2467 #else
2468   g_return_if_fail (fd_list == NULL);
2469 #endif
2470
2471   reply_type = NULL;
2472   split_interface_name = NULL;
2473
2474   simple = g_simple_async_result_new (G_OBJECT (proxy),
2475                                       callback,
2476                                       user_data,
2477                                       g_dbus_proxy_call_internal);
2478
2479   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2480   target_method_name = was_split ? split_method_name : method_name;
2481   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2482
2483   /* Warn if method is unexpected (cf. :g-interface-info) */
2484   if (!was_split)
2485     {
2486       const GDBusMethodInfo *expected_method_info;
2487       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2488       if (expected_method_info != NULL)
2489         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2490     }
2491
2492   destination = NULL;
2493   if (proxy->priv->name != NULL)
2494     {
2495       destination = get_destination_for_call (proxy);
2496       if (destination == NULL)
2497         {
2498           g_simple_async_result_set_error (simple,
2499                                            G_IO_ERROR,
2500                                            G_IO_ERROR_FAILED,
2501                                            _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2502           goto out;
2503         }
2504     }
2505
2506 #ifdef G_OS_UNIX
2507   g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2508                                             destination,
2509                                             proxy->priv->object_path,
2510                                             target_interface_name,
2511                                             target_method_name,
2512                                             parameters,
2513                                             reply_type,
2514                                             flags,
2515                                             timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2516                                             fd_list,
2517                                             cancellable,
2518                                             (GAsyncReadyCallback) reply_cb,
2519                                             simple);
2520 #else
2521   g_dbus_connection_call (proxy->priv->connection,
2522                           destination,
2523                           proxy->priv->object_path,
2524                           target_interface_name,
2525                           target_method_name,
2526                           parameters,
2527                           reply_type,
2528                           flags,
2529                           timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2530                           cancellable,
2531                           (GAsyncReadyCallback) reply_cb,
2532                           simple);
2533 #endif
2534
2535  out:
2536   if (reply_type != NULL)
2537     g_variant_type_free (reply_type);
2538
2539   g_free (split_interface_name);
2540 }
2541
2542 static GVariant *
2543 g_dbus_proxy_call_finish_internal (GDBusProxy    *proxy,
2544                                    GUnixFDList  **out_fd_list,
2545                                    GAsyncResult  *res,
2546                                    GError       **error)
2547 {
2548   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2549   GVariant *value;
2550   ReplyData *data;
2551
2552   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2553   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2554   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2555
2556   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2557
2558   value = NULL;
2559
2560   if (g_simple_async_result_propagate_error (simple, error))
2561     goto out;
2562
2563   data = g_simple_async_result_get_op_res_gpointer (simple);
2564   value = g_variant_ref (data->value);
2565 #ifdef G_OS_UNIX
2566   if (out_fd_list != NULL)
2567     *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2568 #endif
2569
2570  out:
2571   return value;
2572 }
2573
2574 static GVariant *
2575 g_dbus_proxy_call_sync_internal (GDBusProxy      *proxy,
2576                                  const gchar     *method_name,
2577                                  GVariant        *parameters,
2578                                  GDBusCallFlags   flags,
2579                                  gint             timeout_msec,
2580                                  GUnixFDList     *fd_list,
2581                                  GUnixFDList    **out_fd_list,
2582                                  GCancellable    *cancellable,
2583                                  GError         **error)
2584 {
2585   GVariant *ret;
2586   gboolean was_split;
2587   gchar *split_interface_name;
2588   const gchar *split_method_name;
2589   const gchar *target_method_name;
2590   const gchar *target_interface_name;
2591   const gchar *destination;
2592   GVariantType *reply_type;
2593
2594   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2595   g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2596   g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2597   g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2598 #ifdef G_OS_UNIX
2599   g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2600 #else
2601   g_return_val_if_fail (fd_list == NULL, NULL);
2602 #endif
2603   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2604
2605   reply_type = NULL;
2606
2607   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2608   target_method_name = was_split ? split_method_name : method_name;
2609   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2610
2611   /* Warn if method is unexpected (cf. :g-interface-info) */
2612   if (!was_split)
2613     {
2614       const GDBusMethodInfo *expected_method_info;
2615       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2616       if (expected_method_info != NULL)
2617         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2618     }
2619
2620   destination = NULL;
2621   if (proxy->priv->name != NULL)
2622     {
2623       destination = get_destination_for_call (proxy);
2624       if (destination == NULL)
2625         {
2626           g_set_error_literal (error,
2627                                G_IO_ERROR,
2628                                G_IO_ERROR_FAILED,
2629                                _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2630           ret = NULL;
2631           goto out;
2632         }
2633     }
2634
2635 #ifdef G_OS_UNIX
2636   ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2637                                                        destination,
2638                                                        proxy->priv->object_path,
2639                                                        target_interface_name,
2640                                                        target_method_name,
2641                                                        parameters,
2642                                                        reply_type,
2643                                                        flags,
2644                                                        timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2645                                                        fd_list,
2646                                                        out_fd_list,
2647                                                        cancellable,
2648                                                        error);
2649 #else
2650   ret = g_dbus_connection_call_sync (proxy->priv->connection,
2651                                      destination,
2652                                      proxy->priv->object_path,
2653                                      target_interface_name,
2654                                      target_method_name,
2655                                      parameters,
2656                                      reply_type,
2657                                      flags,
2658                                      timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2659                                      cancellable,
2660                                      error);
2661 #endif
2662
2663  out:
2664   if (reply_type != NULL)
2665     g_variant_type_free (reply_type);
2666
2667   g_free (split_interface_name);
2668
2669   return ret;
2670 }
2671
2672 /* ---------------------------------------------------------------------------------------------------- */
2673
2674 /**
2675  * g_dbus_proxy_call:
2676  * @proxy: A #GDBusProxy.
2677  * @method_name: Name of method to invoke.
2678  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2679  * @flags: Flags from the #GDBusCallFlags enumeration.
2680  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2681  *                "infinite") or -1 to use the proxy default timeout.
2682  * @cancellable: A #GCancellable or %NULL.
2683  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2684  * care about the result of the method invocation.
2685  * @user_data: The data to pass to @callback.
2686  *
2687  * Asynchronously invokes the @method_name method on @proxy.
2688  *
2689  * If @method_name contains any dots, then @name is split into interface and
2690  * method name parts. This allows using @proxy for invoking methods on
2691  * other interfaces.
2692  *
2693  * If the #GDBusConnection associated with @proxy is closed then
2694  * the operation will fail with %G_IO_ERROR_CLOSED. If
2695  * @cancellable is canceled, the operation will fail with
2696  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2697  * compatible with the D-Bus protocol, the operation fails with
2698  * %G_IO_ERROR_INVALID_ARGUMENT.
2699  *
2700  * If the @parameters #GVariant is floating, it is consumed. This allows
2701  * convenient 'inline' use of g_variant_new(), e.g.:
2702  * |[
2703  *  g_dbus_proxy_call (proxy,
2704  *                     "TwoStrings",
2705  *                     g_variant_new ("(ss)",
2706  *                                    "Thing One",
2707  *                                    "Thing Two"),
2708  *                     G_DBUS_CALL_FLAGS_NONE,
2709  *                     -1,
2710  *                     NULL,
2711  *                     (GAsyncReadyCallback) two_strings_done,
2712  *                     &amp;data);
2713  * ]|
2714  *
2715  * This is an asynchronous method. When the operation is finished,
2716  * @callback will be invoked in the
2717  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2718  * of the thread you are calling this method from.
2719  * You can then call g_dbus_proxy_call_finish() to get the result of
2720  * the operation. See g_dbus_proxy_call_sync() for the synchronous
2721  * version of this method.
2722  *
2723  * Since: 2.26
2724  */
2725 void
2726 g_dbus_proxy_call (GDBusProxy          *proxy,
2727                    const gchar         *method_name,
2728                    GVariant            *parameters,
2729                    GDBusCallFlags       flags,
2730                    gint                 timeout_msec,
2731                    GCancellable        *cancellable,
2732                    GAsyncReadyCallback  callback,
2733                    gpointer             user_data)
2734 {
2735   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2736 }
2737
2738 /**
2739  * g_dbus_proxy_call_finish:
2740  * @proxy: A #GDBusProxy.
2741  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2742  * @error: Return location for error or %NULL.
2743  *
2744  * Finishes an operation started with g_dbus_proxy_call().
2745  *
2746  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2747  * return values. Free with g_variant_unref().
2748  *
2749  * Since: 2.26
2750  */
2751 GVariant *
2752 g_dbus_proxy_call_finish (GDBusProxy    *proxy,
2753                           GAsyncResult  *res,
2754                           GError       **error)
2755 {
2756   return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2757 }
2758
2759 /**
2760  * g_dbus_proxy_call_sync:
2761  * @proxy: A #GDBusProxy.
2762  * @method_name: Name of method to invoke.
2763  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2764  *              or %NULL if not passing parameters.
2765  * @flags: Flags from the #GDBusCallFlags enumeration.
2766  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2767  *                "infinite") or -1 to use the proxy default timeout.
2768  * @cancellable: A #GCancellable or %NULL.
2769  * @error: Return location for error or %NULL.
2770  *
2771  * Synchronously invokes the @method_name method on @proxy.
2772  *
2773  * If @method_name contains any dots, then @name is split into interface and
2774  * method name parts. This allows using @proxy for invoking methods on
2775  * other interfaces.
2776  *
2777  * If the #GDBusConnection associated with @proxy is disconnected then
2778  * the operation will fail with %G_IO_ERROR_CLOSED. If
2779  * @cancellable is canceled, the operation will fail with
2780  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2781  * compatible with the D-Bus protocol, the operation fails with
2782  * %G_IO_ERROR_INVALID_ARGUMENT.
2783  *
2784  * If the @parameters #GVariant is floating, it is consumed. This allows
2785  * convenient 'inline' use of g_variant_new(), e.g.:
2786  * |[
2787  *  g_dbus_proxy_call_sync (proxy,
2788  *                          "TwoStrings",
2789  *                          g_variant_new ("(ss)",
2790  *                                         "Thing One",
2791  *                                         "Thing Two"),
2792  *                          G_DBUS_CALL_FLAGS_NONE,
2793  *                          -1,
2794  *                          NULL,
2795  *                          &amp;error);
2796  * ]|
2797  *
2798  * The calling thread is blocked until a reply is received. See
2799  * g_dbus_proxy_call() for the asynchronous version of this
2800  * method.
2801  *
2802  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2803  * return values. Free with g_variant_unref().
2804  *
2805  * Since: 2.26
2806  */
2807 GVariant *
2808 g_dbus_proxy_call_sync (GDBusProxy      *proxy,
2809                         const gchar     *method_name,
2810                         GVariant        *parameters,
2811                         GDBusCallFlags   flags,
2812                         gint             timeout_msec,
2813                         GCancellable    *cancellable,
2814                         GError         **error)
2815 {
2816   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2817 }
2818
2819 /* ---------------------------------------------------------------------------------------------------- */
2820
2821 #ifdef G_OS_UNIX
2822
2823 /**
2824  * g_dbus_proxy_call_with_unix_fd_list:
2825  * @proxy: A #GDBusProxy.
2826  * @method_name: Name of method to invoke.
2827  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2828  * @flags: Flags from the #GDBusCallFlags enumeration.
2829  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2830  *                "infinite") or -1 to use the proxy default timeout.
2831  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2832  * @cancellable: A #GCancellable or %NULL.
2833  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2834  * care about the result of the method invocation.
2835  * @user_data: The data to pass to @callback.
2836  *
2837  * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2838  *
2839  * This method is only available on UNIX.
2840  *
2841  * Since: 2.30
2842  */
2843 void
2844 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy          *proxy,
2845                                      const gchar         *method_name,
2846                                      GVariant            *parameters,
2847                                      GDBusCallFlags       flags,
2848                                      gint                 timeout_msec,
2849                                      GUnixFDList         *fd_list,
2850                                      GCancellable        *cancellable,
2851                                      GAsyncReadyCallback  callback,
2852                                      gpointer             user_data)
2853 {
2854   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2855 }
2856
2857 /**
2858  * g_dbus_proxy_call_with_unix_fd_list_finish:
2859  * @proxy: A #GDBusProxy.
2860  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2861  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2862  * @error: Return location for error or %NULL.
2863  *
2864  * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2865  *
2866  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2867  * return values. Free with g_variant_unref().
2868  *
2869  * Since: 2.30
2870  */
2871 GVariant *
2872 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy    *proxy,
2873                                             GUnixFDList  **out_fd_list,
2874                                             GAsyncResult  *res,
2875                                             GError       **error)
2876 {
2877   return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2878 }
2879
2880 /**
2881  * g_dbus_proxy_call_with_unix_fd_list_sync:
2882  * @proxy: A #GDBusProxy.
2883  * @method_name: Name of method to invoke.
2884  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2885  *              or %NULL if not passing parameters.
2886  * @flags: Flags from the #GDBusCallFlags enumeration.
2887  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2888  *                "infinite") or -1 to use the proxy default timeout.
2889  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2890  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2891  * @cancellable: A #GCancellable or %NULL.
2892  * @error: Return location for error or %NULL.
2893  *
2894  * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2895  *
2896  * This method is only available on UNIX.
2897  *
2898  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2899  * return values. Free with g_variant_unref().
2900  *
2901  * Since: 2.30
2902  */
2903 GVariant *
2904 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy      *proxy,
2905                                           const gchar     *method_name,
2906                                           GVariant        *parameters,
2907                                           GDBusCallFlags   flags,
2908                                           gint             timeout_msec,
2909                                           GUnixFDList     *fd_list,
2910                                           GUnixFDList    **out_fd_list,
2911                                           GCancellable    *cancellable,
2912                                           GError         **error)
2913 {
2914   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2915 }
2916
2917 #endif /* G_OS_UNIX */
2918
2919 /* ---------------------------------------------------------------------------------------------------- */
2920
2921 static GDBusInterfaceInfo *
2922 _g_dbus_proxy_get_info (GDBusInterface *interface)
2923 {
2924   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2925   return g_dbus_proxy_get_interface_info (proxy);
2926 }
2927
2928 static GDBusObject *
2929 _g_dbus_proxy_get_object (GDBusInterface *interface)
2930 {
2931   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2932   return proxy->priv->object;
2933 }
2934
2935 static void
2936 _g_dbus_proxy_set_object (GDBusInterface *interface,
2937                           GDBusObject    *object)
2938 {
2939   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2940   if (proxy->priv->object != NULL)
2941     g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2942   proxy->priv->object = object;
2943   if (proxy->priv->object != NULL)
2944     g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2945 }
2946
2947 static void
2948 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2949 {
2950   dbus_interface_iface->get_info   = _g_dbus_proxy_get_info;
2951   dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2952   dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2953 }
2954
2955 /* ---------------------------------------------------------------------------------------------------- */