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