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