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