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