f776662b43dafe9bd270a18e152ce9cf069e7def
[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   changed_properties = NULL;
932   invalidated_properties = NULL;
933
934   G_LOCK (signal_subscription_lock);
935   proxy = data->proxy;
936   if (proxy == NULL)
937     {
938       G_UNLOCK (signal_subscription_lock);
939       goto out;
940     }
941   else
942     {
943       g_object_ref (proxy);
944       G_UNLOCK (signal_subscription_lock);
945     }
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   gboolean get_all;
1320
1321   if (res != NULL)
1322     {
1323       GError *error;
1324       GVariant *result;
1325
1326       error = NULL;
1327       result = g_dbus_connection_call_finish (connection,
1328                                               res,
1329                                               &error);
1330       if (result == NULL)
1331         {
1332           if (error->domain == G_DBUS_ERROR &&
1333               error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1334             {
1335               g_error_free (error);
1336             }
1337           else
1338             {
1339               g_simple_async_result_take_error (data->simple, error);
1340               g_simple_async_result_complete_in_idle (data->simple);
1341               async_init_data_free (data);
1342               goto out;
1343             }
1344         }
1345       else
1346         {
1347           g_variant_get (result,
1348                          "(s)",
1349                          &data->proxy->priv->name_owner);
1350           g_variant_unref (result);
1351         }
1352     }
1353
1354   get_all = TRUE;
1355
1356   if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1357     {
1358       /* Don't load properties if the API user doesn't want them */
1359       get_all = FALSE;
1360     }
1361   else if (data->proxy->priv->name_owner == NULL &&
1362            data->proxy->priv->name != NULL)
1363     {
1364       /* Don't attempt to load properties if the name_owner is NULL (which
1365        * usually means the name isn't owned), unless name is also NULL (which
1366        * means we actually wanted to talk to the directly-connected process -
1367        * either dbus-daemon or a peer - instead of going via dbus-daemon)
1368        */
1369         get_all = FALSE;
1370     }
1371
1372   if (get_all)
1373     {
1374       /* load all properties asynchronously */
1375       g_dbus_connection_call (data->proxy->priv->connection,
1376                               data->proxy->priv->name_owner,
1377                               data->proxy->priv->object_path,
1378                               "org.freedesktop.DBus.Properties",
1379                               "GetAll",
1380                               g_variant_new ("(s)", data->proxy->priv->interface_name),
1381                               G_VARIANT_TYPE ("(a{sv})"),
1382                               G_DBUS_CALL_FLAGS_NONE,
1383                               -1,           /* timeout */
1384                               data->cancellable,
1385                               (GAsyncReadyCallback) async_init_get_all_cb,
1386                               data);
1387     }
1388   else
1389     {
1390       g_simple_async_result_complete_in_idle (data->simple);
1391       async_init_data_free (data);
1392     }
1393
1394  out:
1395   ;
1396 }
1397
1398 static void
1399 async_init_call_get_name_owner (AsyncInitData *data)
1400 {
1401   g_dbus_connection_call (data->proxy->priv->connection,
1402                           "org.freedesktop.DBus",  /* name */
1403                           "/org/freedesktop/DBus", /* object path */
1404                           "org.freedesktop.DBus",  /* interface */
1405                           "GetNameOwner",
1406                           g_variant_new ("(s)",
1407                                          data->proxy->priv->name),
1408                           G_VARIANT_TYPE ("(s)"),
1409                           G_DBUS_CALL_FLAGS_NONE,
1410                           -1,           /* timeout */
1411                           data->cancellable,
1412                           (GAsyncReadyCallback) async_init_get_name_owner_cb,
1413                           data);
1414 }
1415
1416 static void
1417 async_init_start_service_by_name_cb (GDBusConnection *connection,
1418                                      GAsyncResult    *res,
1419                                      gpointer         user_data)
1420 {
1421   AsyncInitData *data = user_data;
1422   GError *error;
1423   GVariant *result;
1424
1425   error = NULL;
1426   result = g_dbus_connection_call_finish (connection,
1427                                           res,
1428                                           &error);
1429   if (result == NULL)
1430     {
1431       /* Errors are not unexpected; the bus will reply e.g.
1432        *
1433        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1434        *   was not provided by any .service files
1435        *
1436        * This doesn't mean that the name doesn't have an owner, just
1437        * that it's not provided by a .service file. So just proceed to
1438        * invoke GetNameOwner() if dealing with that error.
1439        */
1440       if (error->domain == G_DBUS_ERROR &&
1441           error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1442         {
1443           g_error_free (error);
1444         }
1445       else
1446         {
1447           g_prefix_error (&error,
1448                           _("Error calling StartServiceByName for %s: "),
1449                           data->proxy->priv->name);
1450           goto failed;
1451         }
1452     }
1453   else
1454     {
1455       guint32 start_service_result;
1456       g_variant_get (result,
1457                      "(u)",
1458                      &start_service_result);
1459       g_variant_unref (result);
1460       if (start_service_result == 1 ||  /* DBUS_START_REPLY_SUCCESS */
1461           start_service_result == 2)    /* DBUS_START_REPLY_ALREADY_RUNNING */
1462         {
1463           /* continue to invoke GetNameOwner() */
1464         }
1465       else
1466         {
1467           error = g_error_new (G_IO_ERROR,
1468                                G_IO_ERROR_FAILED,
1469                                _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1470                                start_service_result,
1471                                data->proxy->priv->name);
1472           goto failed;
1473         }
1474     }
1475
1476   async_init_call_get_name_owner (data);
1477   return;
1478
1479  failed:
1480   g_warn_if_fail (error != NULL);
1481   g_simple_async_result_take_error (data->simple, error);
1482   g_simple_async_result_complete_in_idle (data->simple);
1483   async_init_data_free (data);
1484 }
1485
1486 static void
1487 async_init_call_start_service_by_name (AsyncInitData *data)
1488 {
1489   g_dbus_connection_call (data->proxy->priv->connection,
1490                           "org.freedesktop.DBus",  /* name */
1491                           "/org/freedesktop/DBus", /* object path */
1492                           "org.freedesktop.DBus",  /* interface */
1493                           "StartServiceByName",
1494                           g_variant_new ("(su)",
1495                                          data->proxy->priv->name,
1496                                          0),
1497                           G_VARIANT_TYPE ("(u)"),
1498                           G_DBUS_CALL_FLAGS_NONE,
1499                           -1,           /* timeout */
1500                           data->cancellable,
1501                           (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1502                           data);
1503 }
1504
1505 static void
1506 async_initable_init_second_async (GAsyncInitable      *initable,
1507                                   gint                 io_priority,
1508                                   GCancellable        *cancellable,
1509                                   GAsyncReadyCallback  callback,
1510                                   gpointer             user_data)
1511 {
1512   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1513   AsyncInitData *data;
1514
1515   data = g_new0 (AsyncInitData, 1);
1516   data->proxy = g_object_ref (proxy);
1517   data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1518   data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1519                                             callback,
1520                                             user_data,
1521                                             NULL);
1522
1523   /* Check name ownership asynchronously - possibly also start the service */
1524   if (proxy->priv->name == NULL)
1525     {
1526       /* Do nothing */
1527       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1528     }
1529   else if (g_dbus_is_unique_name (proxy->priv->name))
1530     {
1531       proxy->priv->name_owner = g_strdup (proxy->priv->name);
1532       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1533     }
1534   else
1535     {
1536       if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1537         {
1538           async_init_call_get_name_owner (data);
1539         }
1540       else
1541         {
1542           async_init_call_start_service_by_name (data);
1543         }
1544     }
1545 }
1546
1547 static gboolean
1548 async_initable_init_second_finish (GAsyncInitable  *initable,
1549                                    GAsyncResult    *res,
1550                                    GError         **error)
1551 {
1552   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1553   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1554   GVariant *result;
1555   gboolean ret;
1556
1557   ret = FALSE;
1558
1559   if (g_simple_async_result_propagate_error (simple, error))
1560     goto out;
1561
1562   result = g_simple_async_result_get_op_res_gpointer (simple);
1563   if (result != NULL)
1564     {
1565       process_get_all_reply (proxy, result);
1566     }
1567
1568   ret = TRUE;
1569
1570  out:
1571   proxy->priv->initialized = TRUE;
1572   return ret;
1573 }
1574
1575 /* ---------------------------------------------------------------------------------------------------- */
1576
1577 static void
1578 async_initable_init_first (GAsyncInitable *initable)
1579 {
1580   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1581
1582   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1583     {
1584       /* subscribe to PropertiesChanged() */
1585       proxy->priv->properties_changed_subscription_id =
1586         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1587                                             proxy->priv->name,
1588                                             "org.freedesktop.DBus.Properties",
1589                                             "PropertiesChanged",
1590                                             proxy->priv->object_path,
1591                                             proxy->priv->interface_name,
1592                                             G_DBUS_SIGNAL_FLAGS_NONE,
1593                                             on_properties_changed,
1594                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1595                                             (GDestroyNotify) signal_subscription_unref);
1596     }
1597
1598   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1599     {
1600       /* subscribe to all signals for the object */
1601       proxy->priv->signals_subscription_id =
1602         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1603                                             proxy->priv->name,
1604                                             proxy->priv->interface_name,
1605                                             NULL,                        /* member */
1606                                             proxy->priv->object_path,
1607                                             NULL,                        /* arg0 */
1608                                             G_DBUS_SIGNAL_FLAGS_NONE,
1609                                             on_signal_received,
1610                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1611                                             (GDestroyNotify) signal_subscription_unref);
1612     }
1613
1614   if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1615     {
1616       proxy->priv->name_owner_changed_subscription_id =
1617         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1618                                             "org.freedesktop.DBus",  /* name */
1619                                             "org.freedesktop.DBus",  /* interface */
1620                                             "NameOwnerChanged",      /* signal name */
1621                                             "/org/freedesktop/DBus", /* path */
1622                                             proxy->priv->name,       /* arg0 */
1623                                             G_DBUS_SIGNAL_FLAGS_NONE,
1624                                             on_name_owner_changed,
1625                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1626                                             (GDestroyNotify) signal_subscription_unref);
1627     }
1628 }
1629
1630 /* ---------------------------------------------------------------------------------------------------- */
1631
1632 /* initialization is split into two parts - the first is the
1633  * non-blocing part that requires the callers GMainContext - the
1634  * second is a blocking part async part that doesn't require the
1635  * callers GMainContext.. we do this split so the code can be reused
1636  * in the GInitable implementation below.
1637  *
1638  * Note that obtaining a GDBusConnection is not shared between the two
1639  * paths.
1640  */
1641
1642 typedef struct
1643 {
1644   GDBusProxy          *proxy;
1645   gint                 io_priority;
1646   GCancellable        *cancellable;
1647   GAsyncReadyCallback  callback;
1648   gpointer             user_data;
1649 } GetConnectionData;
1650
1651 static void
1652 get_connection_cb (GObject       *source_object,
1653                    GAsyncResult  *res,
1654                    gpointer       user_data)
1655 {
1656   GetConnectionData *data = user_data;
1657   GError *error;
1658
1659   error = NULL;
1660   data->proxy->priv->connection = g_bus_get_finish (res, &error);
1661   if (data->proxy->priv->connection == NULL)
1662     {
1663       GSimpleAsyncResult *simple;
1664       simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1665                                           data->callback,
1666                                           data->user_data,
1667                                           NULL);
1668       g_simple_async_result_take_error (simple, error);
1669       g_simple_async_result_complete_in_idle (simple);
1670       g_object_unref (simple);
1671     }
1672   else
1673     {
1674       async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1675       async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1676                                         data->io_priority,
1677                                         data->cancellable,
1678                                         data->callback,
1679                                         data->user_data);
1680     }
1681
1682   if (data->cancellable != NULL)
1683     g_object_unref (data->cancellable);
1684
1685   g_object_unref (data->proxy);
1686   g_free (data);
1687 }
1688
1689 static void
1690 async_initable_init_async (GAsyncInitable      *initable,
1691                            gint                 io_priority,
1692                            GCancellable        *cancellable,
1693                            GAsyncReadyCallback  callback,
1694                            gpointer             user_data)
1695 {
1696   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1697
1698   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1699     {
1700       GetConnectionData *data;
1701
1702       g_assert (proxy->priv->connection == NULL);
1703
1704       data = g_new0 (GetConnectionData, 1);
1705       data->proxy = g_object_ref (proxy);
1706       data->io_priority = io_priority;
1707       data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1708       data->callback = callback;
1709       data->user_data = user_data;
1710       g_bus_get (proxy->priv->bus_type,
1711                  cancellable,
1712                  get_connection_cb,
1713                  data);
1714     }
1715   else
1716     {
1717       async_initable_init_first (initable);
1718       async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1719     }
1720 }
1721
1722 static gboolean
1723 async_initable_init_finish (GAsyncInitable  *initable,
1724                             GAsyncResult    *res,
1725                             GError         **error)
1726 {
1727   return async_initable_init_second_finish (initable, res, error);
1728 }
1729
1730 static void
1731 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1732 {
1733   async_initable_iface->init_async = async_initable_init_async;
1734   async_initable_iface->init_finish = async_initable_init_finish;
1735 }
1736
1737 /* ---------------------------------------------------------------------------------------------------- */
1738
1739 typedef struct
1740 {
1741   GMainContext *context;
1742   GMainLoop *loop;
1743   GAsyncResult *res;
1744 } InitableAsyncInitableData;
1745
1746 static void
1747 async_initable_init_async_cb (GObject      *source_object,
1748                               GAsyncResult *res,
1749                               gpointer      user_data)
1750 {
1751   InitableAsyncInitableData *data = user_data;
1752   data->res = g_object_ref (res);
1753   g_main_loop_quit (data->loop);
1754 }
1755
1756 /* Simply reuse the GAsyncInitable implementation but run the first
1757  * part (that is non-blocking and requires the callers GMainContext)
1758  * with the callers GMainContext.. and the second with a private
1759  * GMainContext (bug 621310 is slightly related).
1760  *
1761  * Note that obtaining a GDBusConnection is not shared between the two
1762  * paths.
1763  */
1764 static gboolean
1765 initable_init (GInitable     *initable,
1766                GCancellable  *cancellable,
1767                GError       **error)
1768 {
1769   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1770   InitableAsyncInitableData *data;
1771   gboolean ret;
1772
1773   ret = FALSE;
1774
1775   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1776     {
1777       g_assert (proxy->priv->connection == NULL);
1778       proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1779                                                 cancellable,
1780                                                 error);
1781       if (proxy->priv->connection == NULL)
1782         goto out;
1783     }
1784
1785   async_initable_init_first (G_ASYNC_INITABLE (initable));
1786
1787   data = g_new0 (InitableAsyncInitableData, 1);
1788   data->context = g_main_context_new ();
1789   data->loop = g_main_loop_new (data->context, FALSE);
1790
1791   g_main_context_push_thread_default (data->context);
1792
1793   async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1794                                     G_PRIORITY_DEFAULT,
1795                                     cancellable,
1796                                     async_initable_init_async_cb,
1797                                     data);
1798
1799   g_main_loop_run (data->loop);
1800
1801   ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1802                                            data->res,
1803                                            error);
1804
1805   g_main_context_pop_thread_default (data->context);
1806
1807   g_main_context_unref (data->context);
1808   g_main_loop_unref (data->loop);
1809   g_object_unref (data->res);
1810   g_free (data);
1811
1812  out:
1813
1814   return ret;
1815 }
1816
1817 static void
1818 initable_iface_init (GInitableIface *initable_iface)
1819 {
1820   initable_iface->init = initable_init;
1821 }
1822
1823 /* ---------------------------------------------------------------------------------------------------- */
1824
1825 /**
1826  * g_dbus_proxy_new:
1827  * @connection: A #GDBusConnection.
1828  * @flags: Flags used when constructing the proxy.
1829  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1830  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1831  * @object_path: An object path.
1832  * @interface_name: A D-Bus interface name.
1833  * @cancellable: A #GCancellable or %NULL.
1834  * @callback: Callback function to invoke when the proxy is ready.
1835  * @user_data: User data to pass to @callback.
1836  *
1837  * Creates a proxy for accessing @interface_name on the remote object
1838  * at @object_path owned by @name at @connection and asynchronously
1839  * loads D-Bus properties unless the
1840  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1841  * the #GDBusProxy::g-properties-changed signal to get notified about
1842  * property changes.
1843  *
1844  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1845  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1846  * to handle signals from the remote object.
1847  *
1848  * If @name is a well-known name and the
1849  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1850  * owner currently exists, the message bus will be requested to launch
1851  * a name owner for the name.
1852  *
1853  * This is a failable asynchronous constructor - when the proxy is
1854  * ready, @callback will be invoked and you can use
1855  * g_dbus_proxy_new_finish() to get the result.
1856  *
1857  * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1858  *
1859  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1860  *
1861  * Since: 2.26
1862  */
1863 void
1864 g_dbus_proxy_new (GDBusConnection     *connection,
1865                   GDBusProxyFlags      flags,
1866                   GDBusInterfaceInfo  *info,
1867                   const gchar         *name,
1868                   const gchar         *object_path,
1869                   const gchar         *interface_name,
1870                   GCancellable        *cancellable,
1871                   GAsyncReadyCallback  callback,
1872                   gpointer             user_data)
1873 {
1874   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1875   g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1876   g_return_if_fail (g_variant_is_object_path (object_path));
1877   g_return_if_fail (g_dbus_is_interface_name (interface_name));
1878
1879   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1880                               G_PRIORITY_DEFAULT,
1881                               cancellable,
1882                               callback,
1883                               user_data,
1884                               "g-flags", flags,
1885                               "g-interface-info", info,
1886                               "g-name", name,
1887                               "g-connection", connection,
1888                               "g-object-path", object_path,
1889                               "g-interface-name", interface_name,
1890                               NULL);
1891 }
1892
1893 /**
1894  * g_dbus_proxy_new_finish:
1895  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1896  * @error: Return location for error or %NULL.
1897  *
1898  * Finishes creating a #GDBusProxy.
1899  *
1900  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1901  *
1902  * Since: 2.26
1903  */
1904 GDBusProxy *
1905 g_dbus_proxy_new_finish (GAsyncResult  *res,
1906                          GError       **error)
1907 {
1908   GObject *object;
1909   GObject *source_object;
1910
1911   source_object = g_async_result_get_source_object (res);
1912   g_assert (source_object != NULL);
1913
1914   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1915                                         res,
1916                                         error);
1917   g_object_unref (source_object);
1918
1919   if (object != NULL)
1920     return G_DBUS_PROXY (object);
1921   else
1922     return NULL;
1923 }
1924
1925 /**
1926  * g_dbus_proxy_new_sync:
1927  * @connection: A #GDBusConnection.
1928  * @flags: Flags used when constructing the proxy.
1929  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1930  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1931  * @object_path: An object path.
1932  * @interface_name: A D-Bus interface name.
1933  * @cancellable: (allow-none): A #GCancellable or %NULL.
1934  * @error: (allow-none): Return location for error or %NULL.
1935  *
1936  * Creates a proxy for accessing @interface_name on the remote object
1937  * at @object_path owned by @name at @connection and synchronously
1938  * loads D-Bus properties unless the
1939  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1940  *
1941  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1942  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1943  * to handle signals from the remote object.
1944  *
1945  * If @name is a well-known name and the
1946  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1947  * owner currently exists, the message bus will be requested to launch
1948  * a name owner for the name.
1949  *
1950  * This is a synchronous failable constructor. See g_dbus_proxy_new()
1951  * and g_dbus_proxy_new_finish() for the asynchronous version.
1952  *
1953  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1954  *
1955  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1956  *
1957  * Since: 2.26
1958  */
1959 GDBusProxy *
1960 g_dbus_proxy_new_sync (GDBusConnection     *connection,
1961                        GDBusProxyFlags      flags,
1962                        GDBusInterfaceInfo  *info,
1963                        const gchar         *name,
1964                        const gchar         *object_path,
1965                        const gchar         *interface_name,
1966                        GCancellable        *cancellable,
1967                        GError             **error)
1968 {
1969   GInitable *initable;
1970
1971   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1972   g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1973                         g_dbus_is_name (name), NULL);
1974   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1975   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1976
1977   initable = g_initable_new (G_TYPE_DBUS_PROXY,
1978                              cancellable,
1979                              error,
1980                              "g-flags", flags,
1981                              "g-interface-info", info,
1982                              "g-name", name,
1983                              "g-connection", connection,
1984                              "g-object-path", object_path,
1985                              "g-interface-name", interface_name,
1986                              NULL);
1987   if (initable != NULL)
1988     return G_DBUS_PROXY (initable);
1989   else
1990     return NULL;
1991 }
1992
1993 /* ---------------------------------------------------------------------------------------------------- */
1994
1995 /**
1996  * g_dbus_proxy_new_for_bus:
1997  * @bus_type: A #GBusType.
1998  * @flags: Flags used when constructing the proxy.
1999  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2000  * @name: A bus name (well-known or unique).
2001  * @object_path: An object path.
2002  * @interface_name: A D-Bus interface name.
2003  * @cancellable: A #GCancellable or %NULL.
2004  * @callback: Callback function to invoke when the proxy is ready.
2005  * @user_data: User data to pass to @callback.
2006  *
2007  * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2008  *
2009  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2010  *
2011  * Since: 2.26
2012  */
2013 void
2014 g_dbus_proxy_new_for_bus (GBusType             bus_type,
2015                           GDBusProxyFlags      flags,
2016                           GDBusInterfaceInfo  *info,
2017                           const gchar         *name,
2018                           const gchar         *object_path,
2019                           const gchar         *interface_name,
2020                           GCancellable        *cancellable,
2021                           GAsyncReadyCallback  callback,
2022                           gpointer             user_data)
2023 {
2024   g_return_if_fail (g_dbus_is_name (name));
2025   g_return_if_fail (g_variant_is_object_path (object_path));
2026   g_return_if_fail (g_dbus_is_interface_name (interface_name));
2027
2028   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2029                               G_PRIORITY_DEFAULT,
2030                               cancellable,
2031                               callback,
2032                               user_data,
2033                               "g-flags", flags,
2034                               "g-interface-info", info,
2035                               "g-name", name,
2036                               "g-bus-type", bus_type,
2037                               "g-object-path", object_path,
2038                               "g-interface-name", interface_name,
2039                               NULL);
2040 }
2041
2042 /**
2043  * g_dbus_proxy_new_for_bus_finish:
2044  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2045  * @error: Return location for error or %NULL.
2046  *
2047  * Finishes creating a #GDBusProxy.
2048  *
2049  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2050  *
2051  * Since: 2.26
2052  */
2053 GDBusProxy *
2054 g_dbus_proxy_new_for_bus_finish (GAsyncResult  *res,
2055                                  GError       **error)
2056 {
2057   return g_dbus_proxy_new_finish (res, error);
2058 }
2059
2060 /**
2061  * g_dbus_proxy_new_for_bus_sync:
2062  * @bus_type: A #GBusType.
2063  * @flags: Flags used when constructing the proxy.
2064  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2065  *        that @proxy conforms to or %NULL.
2066  * @name: A bus name (well-known or unique).
2067  * @object_path: An object path.
2068  * @interface_name: A D-Bus interface name.
2069  * @cancellable: A #GCancellable or %NULL.
2070  * @error: Return location for error or %NULL.
2071  *
2072  * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2073  *
2074  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2075  *
2076  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2077  *
2078  * Since: 2.26
2079  */
2080 GDBusProxy *
2081 g_dbus_proxy_new_for_bus_sync (GBusType             bus_type,
2082                                GDBusProxyFlags      flags,
2083                                GDBusInterfaceInfo  *info,
2084                                const gchar         *name,
2085                                const gchar         *object_path,
2086                                const gchar         *interface_name,
2087                                GCancellable        *cancellable,
2088                                GError             **error)
2089 {
2090   GInitable *initable;
2091
2092   g_return_val_if_fail (g_dbus_is_name (name), NULL);
2093   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2094   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2095
2096   initable = g_initable_new (G_TYPE_DBUS_PROXY,
2097                              cancellable,
2098                              error,
2099                              "g-flags", flags,
2100                              "g-interface-info", info,
2101                              "g-name", name,
2102                              "g-bus-type", bus_type,
2103                              "g-object-path", object_path,
2104                              "g-interface-name", interface_name,
2105                              NULL);
2106   if (initable != NULL)
2107     return G_DBUS_PROXY (initable);
2108   else
2109     return NULL;
2110 }
2111
2112 /* ---------------------------------------------------------------------------------------------------- */
2113
2114 /**
2115  * g_dbus_proxy_get_connection:
2116  * @proxy: A #GDBusProxy.
2117  *
2118  * Gets the connection @proxy is for.
2119  *
2120  * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2121  *
2122  * Since: 2.26
2123  */
2124 GDBusConnection *
2125 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2126 {
2127   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2128   return proxy->priv->connection;
2129 }
2130
2131 /**
2132  * g_dbus_proxy_get_flags:
2133  * @proxy: A #GDBusProxy.
2134  *
2135  * Gets the flags that @proxy was constructed with.
2136  *
2137  * Returns: Flags from the #GDBusProxyFlags enumeration.
2138  *
2139  * Since: 2.26
2140  */
2141 GDBusProxyFlags
2142 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2143 {
2144   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2145   return proxy->priv->flags;
2146 }
2147
2148 /**
2149  * g_dbus_proxy_get_name:
2150  * @proxy: A #GDBusProxy.
2151  *
2152  * Gets the name that @proxy was constructed for.
2153  *
2154  * Returns: A string owned by @proxy. Do not free.
2155  *
2156  * Since: 2.26
2157  */
2158 const gchar *
2159 g_dbus_proxy_get_name (GDBusProxy *proxy)
2160 {
2161   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2162   return proxy->priv->name;
2163 }
2164
2165 /**
2166  * g_dbus_proxy_get_name_owner:
2167  * @proxy: A #GDBusProxy.
2168  *
2169  * The unique name that owns the name that @proxy is for or %NULL if
2170  * no-one currently owns that name. You may connect to the
2171  * #GObject::notify signal to track changes to the
2172  * #GDBusProxy:g-name-owner property.
2173  *
2174  * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2175  *
2176  * Since: 2.26
2177  */
2178 gchar *
2179 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2180 {
2181   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2182   return g_strdup (proxy->priv->name_owner);
2183 }
2184
2185 /**
2186  * g_dbus_proxy_get_object_path:
2187  * @proxy: A #GDBusProxy.
2188  *
2189  * Gets the object path @proxy is for.
2190  *
2191  * Returns: A string owned by @proxy. Do not free.
2192  *
2193  * Since: 2.26
2194  */
2195 const gchar *
2196 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2197 {
2198   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2199   return proxy->priv->object_path;
2200 }
2201
2202 /**
2203  * g_dbus_proxy_get_interface_name:
2204  * @proxy: A #GDBusProxy.
2205  *
2206  * Gets the D-Bus interface name @proxy is for.
2207  *
2208  * Returns: A string owned by @proxy. Do not free.
2209  *
2210  * Since: 2.26
2211  */
2212 const gchar *
2213 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2214 {
2215   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2216   return proxy->priv->interface_name;
2217 }
2218
2219 /**
2220  * g_dbus_proxy_get_default_timeout:
2221  * @proxy: A #GDBusProxy.
2222  *
2223  * Gets the timeout to use if -1 (specifying default timeout) is
2224  * passed as @timeout_msec in the g_dbus_proxy_call() and
2225  * g_dbus_proxy_call_sync() functions.
2226  *
2227  * See the #GDBusProxy:g-default-timeout property for more details.
2228  *
2229  * Returns: Timeout to use for @proxy.
2230  *
2231  * Since: 2.26
2232  */
2233 gint
2234 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2235 {
2236   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2237   return proxy->priv->timeout_msec;
2238 }
2239
2240 /**
2241  * g_dbus_proxy_set_default_timeout:
2242  * @proxy: A #GDBusProxy.
2243  * @timeout_msec: Timeout in milliseconds.
2244  *
2245  * Sets the timeout to use if -1 (specifying default timeout) is
2246  * passed as @timeout_msec in the g_dbus_proxy_call() and
2247  * g_dbus_proxy_call_sync() functions.
2248  *
2249  * See the #GDBusProxy:g-default-timeout property for more details.
2250  *
2251  * Since: 2.26
2252  */
2253 void
2254 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2255                                   gint        timeout_msec)
2256 {
2257   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2258   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2259
2260   /* TODO: locking? */
2261   if (proxy->priv->timeout_msec != timeout_msec)
2262     {
2263       proxy->priv->timeout_msec = timeout_msec;
2264       g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2265     }
2266 }
2267
2268 /**
2269  * g_dbus_proxy_get_interface_info:
2270  * @proxy: A #GDBusProxy
2271  *
2272  * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2273  * interface that @proxy conforms to.
2274  *
2275  * See the #GDBusProxy:g-interface-info property for more details.
2276  *
2277  * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2278  * object, it is owned by @proxy.
2279  *
2280  * Since: 2.26
2281  */
2282 GDBusInterfaceInfo *
2283 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2284 {
2285   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2286   return proxy->priv->expected_interface;
2287 }
2288
2289 /**
2290  * g_dbus_proxy_set_interface_info:
2291  * @proxy: A #GDBusProxy
2292  * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2293  *
2294  * Ensure that interactions with @proxy conform to the given
2295  * interface.  For example, when completing a method call, if the type
2296  * signature of the message isn't what's expected, the given #GError
2297  * is set.  Signals that have a type signature mismatch are simply
2298  * dropped.
2299  *
2300  * See the #GDBusProxy:g-interface-info property for more details.
2301  *
2302  * Since: 2.26
2303  */
2304 void
2305 g_dbus_proxy_set_interface_info (GDBusProxy         *proxy,
2306                                  GDBusInterfaceInfo *info)
2307 {
2308   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2309   if (proxy->priv->expected_interface != NULL)
2310     {
2311       g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2312       g_dbus_interface_info_unref (proxy->priv->expected_interface);
2313     }
2314   proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2315   if (proxy->priv->expected_interface != NULL)
2316     g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2317 }
2318
2319 /* ---------------------------------------------------------------------------------------------------- */
2320
2321 static gboolean
2322 maybe_split_method_name (const gchar  *method_name,
2323                          gchar       **out_interface_name,
2324                          const gchar **out_method_name)
2325 {
2326   gboolean was_split;
2327
2328   was_split = FALSE;
2329   g_assert (out_interface_name != NULL);
2330   g_assert (out_method_name != NULL);
2331   *out_interface_name = NULL;
2332   *out_method_name = NULL;
2333
2334   if (strchr (method_name, '.') != NULL)
2335     {
2336       gchar *p;
2337       gchar *last_dot;
2338
2339       p = g_strdup (method_name);
2340       last_dot = strrchr (p, '.');
2341       *last_dot = '\0';
2342
2343       *out_interface_name = p;
2344       *out_method_name = last_dot + 1;
2345
2346       was_split = TRUE;
2347     }
2348
2349   return was_split;
2350 }
2351
2352 typedef struct
2353 {
2354   GVariant *value;
2355 #ifdef G_OS_UNIX
2356   GUnixFDList *fd_list;
2357 #endif
2358 } ReplyData;
2359
2360 static void
2361 reply_data_free (ReplyData *data)
2362 {
2363   g_variant_unref (data->value);
2364 #ifdef G_OS_UNIX
2365   if (data->fd_list != NULL)
2366     g_object_unref (data->fd_list);
2367 #endif
2368   g_slice_free (ReplyData, data);
2369 }
2370
2371 static void
2372 reply_cb (GDBusConnection *connection,
2373           GAsyncResult    *res,
2374           gpointer         user_data)
2375 {
2376   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2377   GVariant *value;
2378   GError *error;
2379 #ifdef G_OS_UNIX
2380   GUnixFDList *fd_list;
2381 #endif
2382
2383   error = NULL;
2384 #ifdef G_OS_UNIX
2385   value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2386                                                            &fd_list,
2387                                                            res,
2388                                                            &error);
2389 #else
2390   value = g_dbus_connection_call_finish (connection,
2391                                          res,
2392                                          &error);
2393 #endif
2394   if (error != NULL)
2395     {
2396       g_simple_async_result_take_error (simple, error);
2397     }
2398   else
2399     {
2400       ReplyData *data;
2401       data = g_slice_new0 (ReplyData);
2402       data->value = value;
2403 #ifdef G_OS_UNIX
2404       data->fd_list = fd_list;
2405 #endif
2406       g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2407     }
2408
2409   /* no need to complete in idle since the method GDBusConnection already does */
2410   g_simple_async_result_complete (simple);
2411   g_object_unref (simple);
2412 }
2413
2414 static const GDBusMethodInfo *
2415 lookup_method_info_or_warn (GDBusProxy  *proxy,
2416                             const gchar *method_name)
2417 {
2418   const GDBusMethodInfo *info;
2419
2420   if (proxy->priv->expected_interface == NULL)
2421     return NULL;
2422
2423   info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2424   if (info == NULL)
2425     {
2426       g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2427                  method_name, proxy->priv->expected_interface->name);
2428     }
2429
2430   return info;
2431 }
2432
2433 static const gchar *
2434 get_destination_for_call (GDBusProxy *proxy)
2435 {
2436   const gchar *ret;
2437
2438   ret = NULL;
2439
2440   /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2441    * is never NULL and always the same as proxy->priv->name. We use this
2442    * knowledge to avoid checking if proxy->priv->name is a unique or
2443    * well-known name.
2444    */
2445   ret = proxy->priv->name_owner;
2446   if (ret != NULL)
2447     goto out;
2448
2449   if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2450     goto out;
2451
2452   ret = proxy->priv->name;
2453
2454  out:
2455   return ret;
2456 }
2457
2458 /* ---------------------------------------------------------------------------------------------------- */
2459
2460 static void
2461 g_dbus_proxy_call_internal (GDBusProxy          *proxy,
2462                             const gchar         *method_name,
2463                             GVariant            *parameters,
2464                             GDBusCallFlags       flags,
2465                             gint                 timeout_msec,
2466                             GUnixFDList         *fd_list,
2467                             GCancellable        *cancellable,
2468                             GAsyncReadyCallback  callback,
2469                             gpointer             user_data)
2470 {
2471   GSimpleAsyncResult *simple;
2472   gboolean was_split;
2473   gchar *split_interface_name;
2474   const gchar *split_method_name;
2475   const gchar *target_method_name;
2476   const gchar *target_interface_name;
2477   const gchar *destination;
2478   GVariantType *reply_type;
2479
2480   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2481   g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2482   g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2483   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2484 #ifdef G_OS_UNIX
2485   g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2486 #else
2487   g_return_if_fail (fd_list == NULL);
2488 #endif
2489
2490   reply_type = NULL;
2491   split_interface_name = NULL;
2492
2493   simple = g_simple_async_result_new (G_OBJECT (proxy),
2494                                       callback,
2495                                       user_data,
2496                                       g_dbus_proxy_call_internal);
2497
2498   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2499   target_method_name = was_split ? split_method_name : method_name;
2500   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2501
2502   /* Warn if method is unexpected (cf. :g-interface-info) */
2503   if (!was_split)
2504     {
2505       const GDBusMethodInfo *expected_method_info;
2506       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2507       if (expected_method_info != NULL)
2508         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2509     }
2510
2511   destination = NULL;
2512   if (proxy->priv->name != NULL)
2513     {
2514       destination = get_destination_for_call (proxy);
2515       if (destination == NULL)
2516         {
2517           g_simple_async_result_set_error (simple,
2518                                            G_IO_ERROR,
2519                                            G_IO_ERROR_FAILED,
2520                                            _("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"));
2521           goto out;
2522         }
2523     }
2524
2525 #ifdef G_OS_UNIX
2526   g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2527                                             destination,
2528                                             proxy->priv->object_path,
2529                                             target_interface_name,
2530                                             target_method_name,
2531                                             parameters,
2532                                             reply_type,
2533                                             flags,
2534                                             timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2535                                             fd_list,
2536                                             cancellable,
2537                                             (GAsyncReadyCallback) reply_cb,
2538                                             simple);
2539 #else
2540   g_dbus_connection_call (proxy->priv->connection,
2541                           destination,
2542                           proxy->priv->object_path,
2543                           target_interface_name,
2544                           target_method_name,
2545                           parameters,
2546                           reply_type,
2547                           flags,
2548                           timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2549                           cancellable,
2550                           (GAsyncReadyCallback) reply_cb,
2551                           simple);
2552 #endif
2553
2554  out:
2555   if (reply_type != NULL)
2556     g_variant_type_free (reply_type);
2557
2558   g_free (split_interface_name);
2559 }
2560
2561 static GVariant *
2562 g_dbus_proxy_call_finish_internal (GDBusProxy    *proxy,
2563                                    GUnixFDList  **out_fd_list,
2564                                    GAsyncResult  *res,
2565                                    GError       **error)
2566 {
2567   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2568   GVariant *value;
2569   ReplyData *data;
2570
2571   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2572   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2573   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2574
2575   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2576
2577   value = NULL;
2578
2579   if (g_simple_async_result_propagate_error (simple, error))
2580     goto out;
2581
2582   data = g_simple_async_result_get_op_res_gpointer (simple);
2583   value = g_variant_ref (data->value);
2584 #ifdef G_OS_UNIX
2585   if (out_fd_list != NULL)
2586     *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2587 #endif
2588
2589  out:
2590   return value;
2591 }
2592
2593 static GVariant *
2594 g_dbus_proxy_call_sync_internal (GDBusProxy      *proxy,
2595                                  const gchar     *method_name,
2596                                  GVariant        *parameters,
2597                                  GDBusCallFlags   flags,
2598                                  gint             timeout_msec,
2599                                  GUnixFDList     *fd_list,
2600                                  GUnixFDList    **out_fd_list,
2601                                  GCancellable    *cancellable,
2602                                  GError         **error)
2603 {
2604   GVariant *ret;
2605   gboolean was_split;
2606   gchar *split_interface_name;
2607   const gchar *split_method_name;
2608   const gchar *target_method_name;
2609   const gchar *target_interface_name;
2610   const gchar *destination;
2611   GVariantType *reply_type;
2612
2613   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2614   g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2615   g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2616   g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2617 #ifdef G_OS_UNIX
2618   g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2619 #else
2620   g_return_val_if_fail (fd_list == NULL, NULL);
2621 #endif
2622   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2623
2624   reply_type = NULL;
2625
2626   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2627   target_method_name = was_split ? split_method_name : method_name;
2628   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2629
2630   /* Warn if method is unexpected (cf. :g-interface-info) */
2631   if (!was_split)
2632     {
2633       const GDBusMethodInfo *expected_method_info;
2634       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2635       if (expected_method_info != NULL)
2636         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2637     }
2638
2639   destination = NULL;
2640   if (proxy->priv->name != NULL)
2641     {
2642       destination = get_destination_for_call (proxy);
2643       if (destination == NULL)
2644         {
2645           g_set_error_literal (error,
2646                                G_IO_ERROR,
2647                                G_IO_ERROR_FAILED,
2648                                _("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"));
2649           ret = NULL;
2650           goto out;
2651         }
2652     }
2653
2654 #ifdef G_OS_UNIX
2655   ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2656                                                        destination,
2657                                                        proxy->priv->object_path,
2658                                                        target_interface_name,
2659                                                        target_method_name,
2660                                                        parameters,
2661                                                        reply_type,
2662                                                        flags,
2663                                                        timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2664                                                        fd_list,
2665                                                        out_fd_list,
2666                                                        cancellable,
2667                                                        error);
2668 #else
2669   ret = g_dbus_connection_call_sync (proxy->priv->connection,
2670                                      destination,
2671                                      proxy->priv->object_path,
2672                                      target_interface_name,
2673                                      target_method_name,
2674                                      parameters,
2675                                      reply_type,
2676                                      flags,
2677                                      timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2678                                      cancellable,
2679                                      error);
2680 #endif
2681
2682  out:
2683   if (reply_type != NULL)
2684     g_variant_type_free (reply_type);
2685
2686   g_free (split_interface_name);
2687
2688   return ret;
2689 }
2690
2691 /* ---------------------------------------------------------------------------------------------------- */
2692
2693 /**
2694  * g_dbus_proxy_call:
2695  * @proxy: A #GDBusProxy.
2696  * @method_name: Name of method to invoke.
2697  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2698  * @flags: Flags from the #GDBusCallFlags enumeration.
2699  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2700  *                "infinite") or -1 to use the proxy default timeout.
2701  * @cancellable: A #GCancellable or %NULL.
2702  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2703  * care about the result of the method invocation.
2704  * @user_data: The data to pass to @callback.
2705  *
2706  * Asynchronously invokes the @method_name method on @proxy.
2707  *
2708  * If @method_name contains any dots, then @name is split into interface and
2709  * method name parts. This allows using @proxy for invoking methods on
2710  * other interfaces.
2711  *
2712  * If the #GDBusConnection associated with @proxy is closed then
2713  * the operation will fail with %G_IO_ERROR_CLOSED. If
2714  * @cancellable is canceled, the operation will fail with
2715  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2716  * compatible with the D-Bus protocol, the operation fails with
2717  * %G_IO_ERROR_INVALID_ARGUMENT.
2718  *
2719  * If the @parameters #GVariant is floating, it is consumed. This allows
2720  * convenient 'inline' use of g_variant_new(), e.g.:
2721  * |[
2722  *  g_dbus_proxy_call (proxy,
2723  *                     "TwoStrings",
2724  *                     g_variant_new ("(ss)",
2725  *                                    "Thing One",
2726  *                                    "Thing Two"),
2727  *                     G_DBUS_CALL_FLAGS_NONE,
2728  *                     -1,
2729  *                     NULL,
2730  *                     (GAsyncReadyCallback) two_strings_done,
2731  *                     &amp;data);
2732  * ]|
2733  *
2734  * This is an asynchronous method. When the operation is finished,
2735  * @callback will be invoked in the
2736  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2737  * of the thread you are calling this method from.
2738  * You can then call g_dbus_proxy_call_finish() to get the result of
2739  * the operation. See g_dbus_proxy_call_sync() for the synchronous
2740  * version of this method.
2741  *
2742  * Since: 2.26
2743  */
2744 void
2745 g_dbus_proxy_call (GDBusProxy          *proxy,
2746                    const gchar         *method_name,
2747                    GVariant            *parameters,
2748                    GDBusCallFlags       flags,
2749                    gint                 timeout_msec,
2750                    GCancellable        *cancellable,
2751                    GAsyncReadyCallback  callback,
2752                    gpointer             user_data)
2753 {
2754   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2755 }
2756
2757 /**
2758  * g_dbus_proxy_call_finish:
2759  * @proxy: A #GDBusProxy.
2760  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2761  * @error: Return location for error or %NULL.
2762  *
2763  * Finishes an operation started with g_dbus_proxy_call().
2764  *
2765  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2766  * return values. Free with g_variant_unref().
2767  *
2768  * Since: 2.26
2769  */
2770 GVariant *
2771 g_dbus_proxy_call_finish (GDBusProxy    *proxy,
2772                           GAsyncResult  *res,
2773                           GError       **error)
2774 {
2775   return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2776 }
2777
2778 /**
2779  * g_dbus_proxy_call_sync:
2780  * @proxy: A #GDBusProxy.
2781  * @method_name: Name of method to invoke.
2782  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2783  *              or %NULL if not passing parameters.
2784  * @flags: Flags from the #GDBusCallFlags enumeration.
2785  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2786  *                "infinite") or -1 to use the proxy default timeout.
2787  * @cancellable: A #GCancellable or %NULL.
2788  * @error: Return location for error or %NULL.
2789  *
2790  * Synchronously invokes the @method_name method on @proxy.
2791  *
2792  * If @method_name contains any dots, then @name is split into interface and
2793  * method name parts. This allows using @proxy for invoking methods on
2794  * other interfaces.
2795  *
2796  * If the #GDBusConnection associated with @proxy is disconnected then
2797  * the operation will fail with %G_IO_ERROR_CLOSED. If
2798  * @cancellable is canceled, the operation will fail with
2799  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2800  * compatible with the D-Bus protocol, the operation fails with
2801  * %G_IO_ERROR_INVALID_ARGUMENT.
2802  *
2803  * If the @parameters #GVariant is floating, it is consumed. This allows
2804  * convenient 'inline' use of g_variant_new(), e.g.:
2805  * |[
2806  *  g_dbus_proxy_call_sync (proxy,
2807  *                          "TwoStrings",
2808  *                          g_variant_new ("(ss)",
2809  *                                         "Thing One",
2810  *                                         "Thing Two"),
2811  *                          G_DBUS_CALL_FLAGS_NONE,
2812  *                          -1,
2813  *                          NULL,
2814  *                          &amp;error);
2815  * ]|
2816  *
2817  * The calling thread is blocked until a reply is received. See
2818  * g_dbus_proxy_call() for the asynchronous version of this
2819  * method.
2820  *
2821  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2822  * return values. Free with g_variant_unref().
2823  *
2824  * Since: 2.26
2825  */
2826 GVariant *
2827 g_dbus_proxy_call_sync (GDBusProxy      *proxy,
2828                         const gchar     *method_name,
2829                         GVariant        *parameters,
2830                         GDBusCallFlags   flags,
2831                         gint             timeout_msec,
2832                         GCancellable    *cancellable,
2833                         GError         **error)
2834 {
2835   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2836 }
2837
2838 /* ---------------------------------------------------------------------------------------------------- */
2839
2840 #ifdef G_OS_UNIX
2841
2842 /**
2843  * g_dbus_proxy_call_with_unix_fd_list:
2844  * @proxy: A #GDBusProxy.
2845  * @method_name: Name of method to invoke.
2846  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2847  * @flags: Flags from the #GDBusCallFlags enumeration.
2848  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2849  *                "infinite") or -1 to use the proxy default timeout.
2850  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2851  * @cancellable: A #GCancellable or %NULL.
2852  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2853  * care about the result of the method invocation.
2854  * @user_data: The data to pass to @callback.
2855  *
2856  * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2857  *
2858  * This method is only available on UNIX.
2859  *
2860  * Since: 2.30
2861  */
2862 void
2863 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy          *proxy,
2864                                      const gchar         *method_name,
2865                                      GVariant            *parameters,
2866                                      GDBusCallFlags       flags,
2867                                      gint                 timeout_msec,
2868                                      GUnixFDList         *fd_list,
2869                                      GCancellable        *cancellable,
2870                                      GAsyncReadyCallback  callback,
2871                                      gpointer             user_data)
2872 {
2873   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2874 }
2875
2876 /**
2877  * g_dbus_proxy_call_with_unix_fd_list_finish:
2878  * @proxy: A #GDBusProxy.
2879  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2880  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2881  * @error: Return location for error or %NULL.
2882  *
2883  * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2884  *
2885  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2886  * return values. Free with g_variant_unref().
2887  *
2888  * Since: 2.30
2889  */
2890 GVariant *
2891 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy    *proxy,
2892                                             GUnixFDList  **out_fd_list,
2893                                             GAsyncResult  *res,
2894                                             GError       **error)
2895 {
2896   return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2897 }
2898
2899 /**
2900  * g_dbus_proxy_call_with_unix_fd_list_sync:
2901  * @proxy: A #GDBusProxy.
2902  * @method_name: Name of method to invoke.
2903  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2904  *              or %NULL if not passing parameters.
2905  * @flags: Flags from the #GDBusCallFlags enumeration.
2906  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2907  *                "infinite") or -1 to use the proxy default timeout.
2908  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2909  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2910  * @cancellable: A #GCancellable or %NULL.
2911  * @error: Return location for error or %NULL.
2912  *
2913  * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2914  *
2915  * This method is only available on UNIX.
2916  *
2917  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2918  * return values. Free with g_variant_unref().
2919  *
2920  * Since: 2.30
2921  */
2922 GVariant *
2923 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy      *proxy,
2924                                           const gchar     *method_name,
2925                                           GVariant        *parameters,
2926                                           GDBusCallFlags   flags,
2927                                           gint             timeout_msec,
2928                                           GUnixFDList     *fd_list,
2929                                           GUnixFDList    **out_fd_list,
2930                                           GCancellable    *cancellable,
2931                                           GError         **error)
2932 {
2933   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2934 }
2935
2936 #endif /* G_OS_UNIX */
2937
2938 /* ---------------------------------------------------------------------------------------------------- */
2939
2940 static GDBusInterfaceInfo *
2941 _g_dbus_proxy_get_info (GDBusInterface *interface)
2942 {
2943   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2944   return g_dbus_proxy_get_interface_info (proxy);
2945 }
2946
2947 static GDBusObject *
2948 _g_dbus_proxy_get_object (GDBusInterface *interface)
2949 {
2950   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2951   return proxy->priv->object;
2952 }
2953
2954 static void
2955 _g_dbus_proxy_set_object (GDBusInterface *interface,
2956                           GDBusObject    *object)
2957 {
2958   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2959   if (proxy->priv->object != NULL)
2960     g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2961   proxy->priv->object = object;
2962   if (proxy->priv->object != NULL)
2963     g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2964 }
2965
2966 static void
2967 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2968 {
2969   dbus_interface_iface->get_info   = _g_dbus_proxy_get_info;
2970   dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2971   dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2972 }
2973
2974 /* ---------------------------------------------------------------------------------------------------- */