Add "Since: 2.30" for new D-Bus routines
[platform/upstream/glib.git] / gio / gdbusobjectmanagerclient.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 "gdbusobjectmanager.h"
26 #include "gdbusobjectmanagerclient.h"
27 #include "gdbusobject.h"
28 #include "gdbusprivate.h"
29 #include "gio-marshal.h"
30 #include "gioenumtypes.h"
31 #include "ginitable.h"
32 #include "gasyncresult.h"
33 #include "gsimpleasyncresult.h"
34 #include "gasyncinitable.h"
35 #include "gdbusconnection.h"
36 #include "gdbusutils.h"
37 #include "gdbusobject.h"
38 #include "gdbusobjectproxy.h"
39 #include "gdbusproxy.h"
40 #include "gdbusinterface.h"
41
42 #include "glibintl.h"
43
44 /**
45  * SECTION:gdbusobjectmanagerclient
46  * @short_description: Client-side object manager
47  * @include: gio/gio.h
48  *
49  * #GDBusObjectManagerClient is used to create, monitor and delete object
50  * proxies for remote objects exported by a #GDBusObjectManagerServer (or any
51  * code implementing the <ulink
52  * url="http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager">org.freedesktop.DBus.ObjectManager</ulink>
53  * interface).
54  *
55  * Once an instance of this type has been created, you can connect to
56  * the #GDBusObjectManager::object-added and
57  * #GDBusObjectManager::object-removed signals and inspect the
58  * #GDBusObjectProxy objects returned by
59  * g_dbus_object_manager_get_objects().
60  *
61  * If the name for a #GDBusObjectManagerClient is not owned by anyone at
62  * object construction time, the default behavior is to request the
63  * message bus to launch an owner for the name. This behavior can be
64  * disabled using the %G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START
65  * flag. It's also worth noting that this only works if the name of
66  * interest is activatable in the first place. E.g. in some cases it
67  * is not possible to launch an owner for the requested name. In this
68  * case, #GDBusObjectManagerClient object construction still succeeds but
69  * there will be no object proxies
70  * (e.g. g_dbus_object_manager_get_objects() returns the empty list) and
71  * the #GDBusObjectManagerClient:name-owner property is %NULL.
72  *
73  * The owner of the requested name can come and go (for example
74  * consider a system service being restarted) – #GDBusObjectManagerClient
75  * handles this case too; simply connect to the #GObject::notify
76  * signal to watch for changes on the #GDBusObjectManagerClient:name-owner
77  * property. When the name owner vanishes, the behavior is that
78  * #GDBusObjectManagerClient:name-owner is set to %NULL (this includes
79  * emission of the #GObject::notify signal) and then
80  * #GDBusObjectManager::object-removed signals are synthesized
81  * for all currently existing object proxies. Since
82  * #GDBusObjectManagerClient:name-owner is %NULL when this happens, you can
83  * use this information to disambiguate a synthesized signal from a
84  * genuine signal caused by object removal on the remote
85  * #GDBusObjectManager. Similarly, when a new name owner appears,
86  * #GDBusObjectManager::object-added signals are synthesized
87  * while #GDBusObjectManagerClient:name-owner is still %NULL. Only when all
88  * object proxies have been added, the #GDBusObjectManagerClient:name-owner
89  * is set to the new name owner (this includes emission of the
90  * #GObject::notify signal).  Furthermore, you are guaranteed that
91  * #GDBusObjectManagerClient:name-owner will alternate between a name owner
92  * (e.g. <literal>:1.42</literal>) and %NULL even in the case where
93  * the name of interest is atomically replaced
94  *
95  * Ultimately, #GDBusObjectManagerClient is used to obtain #GDBusProxy
96  * instances. All signals (including the
97  * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
98  * signal) delivered to #GDBusProxy instances are guaranteed to
99  * originate from the name owner. This guarantee along with the
100  * behavior described above, means that certain race conditions
101  * including the <emphasis><quote>half the proxy is from the old owner
102  * and the other half is from the new owner</quote></emphasis> problem
103  * cannot happen.
104  *
105  * To avoid having the application connect to signals on the returned
106  * #GDBusObjectProxy and #GDBusProxy objects, the
107  * #GDBusObject::interface-added,
108  * #GDBusObject::interface-removed,
109  * #GDBusProxy::g-properties-changed and
110  * #GDBusProxy::g-signal signals
111  * are also emitted on the #GDBusObjectManagerClient instance managing these
112  * objects. The signals emitted are
113  * #GDBusObjectManager::interface-added,
114  * #GDBusObjectManager::interface-removed,
115  * #GDBusObjectManagerClient::interface-proxy-properties-changed and
116  * #GDBusObjectManagerClient::interface-proxy-signal.
117  *
118  * Note that all callbacks and signals are emitted in the
119  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
120  * that the #GDBusObjectManagerClient object was constructed
121  * in. Additionally, the #GDBusObjectProxy and #GDBusProxy objects
122  * originating from the #GDBusObjectManagerClient object will be created in
123  * the same context and, consequently, will deliver signals in the
124  * same main loop.
125  */
126
127 struct _GDBusObjectManagerClientPrivate
128 {
129   GBusType bus_type;
130   GDBusConnection *connection;
131   gchar *object_path;
132   gchar *name;
133   gchar *name_owner;
134   GDBusObjectManagerClientFlags flags;
135
136   GDBusProxy *control_proxy;
137
138   GHashTable *map_object_path_to_object_proxy;
139
140   guint signal_subscription_id;
141   gchar *match_rule;
142
143   GDBusProxyTypeFunc get_proxy_type_func;
144   gpointer get_proxy_type_user_data;
145 };
146
147 enum
148 {
149   PROP_0,
150   PROP_BUS_TYPE,
151   PROP_CONNECTION,
152   PROP_FLAGS,
153   PROP_OBJECT_PATH,
154   PROP_NAME,
155   PROP_NAME_OWNER,
156   PROP_GET_PROXY_TYPE_FUNC,
157   PROP_GET_PROXY_TYPE_USER_DATA
158 };
159
160 enum
161 {
162   INTERFACE_PROXY_SIGNAL_SIGNAL,
163   INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL,
164   LAST_SIGNAL
165 };
166
167 static guint signals[LAST_SIGNAL] = { 0 };
168
169 static void initable_iface_init       (GInitableIface *initable_iface);
170 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
171 static void dbus_object_manager_interface_init (GDBusObjectManagerIface *iface);
172
173 G_DEFINE_TYPE_WITH_CODE (GDBusObjectManagerClient, g_dbus_object_manager_client, G_TYPE_OBJECT,
174                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
175                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
176                          G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT_MANAGER, dbus_object_manager_interface_init));
177
178 static void maybe_unsubscribe_signals (GDBusObjectManagerClient *manager);
179
180 static void on_control_proxy_g_signal (GDBusProxy   *proxy,
181                                        const gchar  *sender_name,
182                                        const gchar  *signal_name,
183                                        GVariant     *parameters,
184                                        gpointer      user_data);
185
186 static void process_get_all_result (GDBusObjectManagerClient *manager,
187                                     GVariant          *value,
188                                     const gchar       *name_owner);
189
190 static void
191 g_dbus_object_manager_client_finalize (GObject *object)
192 {
193   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (object);
194
195   maybe_unsubscribe_signals (manager);
196
197   g_hash_table_unref (manager->priv->map_object_path_to_object_proxy);
198
199   if (manager->priv->control_proxy != NULL)
200     {
201       g_warn_if_fail (g_signal_handlers_disconnect_by_func (manager->priv->control_proxy,
202                                                             on_control_proxy_g_signal,
203                                                             manager) == 1);
204       g_object_unref (manager->priv->control_proxy);
205     }
206   g_object_unref (manager->priv->connection);
207   g_free (manager->priv->object_path);
208   g_free (manager->priv->name);
209   g_free (manager->priv->name_owner);
210
211   if (G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class)->finalize != NULL)
212     G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class)->finalize (object);
213 }
214
215 static void
216 g_dbus_object_manager_client_get_property (GObject    *_object,
217                                     guint       prop_id,
218                                     GValue     *value,
219                                     GParamSpec *pspec)
220 {
221   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_object);
222
223   switch (prop_id)
224     {
225     case PROP_CONNECTION:
226       g_value_set_object (value, g_dbus_object_manager_client_get_connection (manager));
227       break;
228
229     case PROP_OBJECT_PATH:
230       g_value_set_string (value, g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager)));
231       break;
232
233     case PROP_NAME:
234       g_value_set_string (value, g_dbus_object_manager_client_get_name (manager));
235       break;
236
237     case PROP_FLAGS:
238       g_value_set_flags (value, g_dbus_object_manager_client_get_flags (manager));
239       break;
240
241     case PROP_NAME_OWNER:
242       g_value_take_string (value, g_dbus_object_manager_client_get_name_owner (manager));
243       break;
244
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
247       break;
248     }
249 }
250
251 static void
252 g_dbus_object_manager_client_set_property (GObject       *_object,
253                                     guint          prop_id,
254                                     const GValue  *value,
255                                     GParamSpec    *pspec)
256 {
257   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_object);
258
259   switch (prop_id)
260     {
261     case PROP_BUS_TYPE:
262       manager->priv->bus_type = g_value_get_enum (value);
263       break;
264
265     case PROP_CONNECTION:
266       if (g_value_get_object (value) != NULL)
267         {
268           g_assert (manager->priv->connection == NULL);
269           g_assert (G_IS_DBUS_CONNECTION (g_value_get_object (value)));
270           manager->priv->connection = g_value_dup_object (value);
271         }
272       break;
273
274     case PROP_OBJECT_PATH:
275       g_assert (manager->priv->object_path == NULL);
276       g_assert (g_variant_is_object_path (g_value_get_string (value)));
277       manager->priv->object_path = g_value_dup_string (value);
278       break;
279
280     case PROP_NAME:
281       g_assert (manager->priv->name == NULL);
282       g_assert (g_dbus_is_name (g_value_get_string (value)));
283       manager->priv->name = g_value_dup_string (value);
284       break;
285
286     case PROP_FLAGS:
287       manager->priv->flags = g_value_get_flags (value);
288       break;
289
290     case PROP_GET_PROXY_TYPE_FUNC:
291       manager->priv->get_proxy_type_func = g_value_get_pointer (value);
292       break;
293
294     case PROP_GET_PROXY_TYPE_USER_DATA:
295       manager->priv->get_proxy_type_user_data = g_value_get_pointer (value);
296       break;
297
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
300       break;
301     }
302 }
303
304 static void
305 g_dbus_object_manager_client_class_init (GDBusObjectManagerClientClass *klass)
306 {
307   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
308
309   gobject_class->finalize     = g_dbus_object_manager_client_finalize;
310   gobject_class->set_property = g_dbus_object_manager_client_set_property;
311   gobject_class->get_property = g_dbus_object_manager_client_get_property;
312
313   /**
314    * GDBusObjectManagerClient:connection:
315    *
316    * The #GDBusConnection to use.
317    *
318    * Since: 2.30
319    */
320   g_object_class_install_property (gobject_class,
321                                    PROP_CONNECTION,
322                                    g_param_spec_object ("connection",
323                                                         "Connection",
324                                                         "The connection to use",
325                                                         G_TYPE_DBUS_CONNECTION,
326                                                         G_PARAM_READABLE |
327                                                         G_PARAM_WRITABLE |
328                                                         G_PARAM_CONSTRUCT_ONLY |
329                                                         G_PARAM_STATIC_STRINGS));
330
331   /**
332    * GDBusObjectManagerClient:bus-type:
333    *
334    * If this property is not %G_BUS_TYPE_NONE, then
335    * #GDBusObjectManagerClient:connection must be %NULL and will be set to the
336    * #GDBusConnection obtained by calling g_bus_get() with the value
337    * of this property.
338    *
339    * Since: 2.30
340    */
341   g_object_class_install_property (gobject_class,
342                                    PROP_BUS_TYPE,
343                                    g_param_spec_enum ("bus-type",
344                                                       "Bus Type",
345                                                       "The bus to connect to, if any",
346                                                       G_TYPE_BUS_TYPE,
347                                                       G_BUS_TYPE_NONE,
348                                                       G_PARAM_WRITABLE |
349                                                       G_PARAM_CONSTRUCT_ONLY |
350                                                       G_PARAM_STATIC_NAME |
351                                                       G_PARAM_STATIC_BLURB |
352                                                       G_PARAM_STATIC_NICK));
353
354   /**
355    * GDBusObjectManagerClient:flags:
356    *
357    * Flags from the #GDBusObjectManagerClientFlags enumeration.
358    *
359    * Since: 2.30
360    */
361   g_object_class_install_property (gobject_class,
362                                    PROP_FLAGS,
363                                    g_param_spec_flags ("flags",
364                                                        "Flags",
365                                                        "Flags for the proxy manager",
366                                                        G_TYPE_DBUS_OBJECT_MANAGER_CLIENT_FLAGS,
367                                                        G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
368                                                        G_PARAM_READABLE |
369                                                        G_PARAM_WRITABLE |
370                                                        G_PARAM_CONSTRUCT_ONLY |
371                                                        G_PARAM_STATIC_NAME |
372                                                        G_PARAM_STATIC_BLURB |
373                                                        G_PARAM_STATIC_NICK));
374
375   /**
376    * GDBusObjectManagerClient:object-path:
377    *
378    * The object path the manager is for.
379    *
380    * Since: 2.30
381    */
382   g_object_class_install_property (gobject_class,
383                                    PROP_OBJECT_PATH,
384                                    g_param_spec_string ("object-path",
385                                                         "Object Path",
386                                                         "The object path of the control object",
387                                                         NULL,
388                                                         G_PARAM_READABLE |
389                                                         G_PARAM_WRITABLE |
390                                                         G_PARAM_CONSTRUCT_ONLY |
391                                                         G_PARAM_STATIC_STRINGS));
392
393   /**
394    * GDBusObjectManagerClient:name:
395    *
396    * The well-known name or unique name that the manager is for.
397    *
398    * Since: 2.30
399    */
400   g_object_class_install_property (gobject_class,
401                                    PROP_NAME,
402                                    g_param_spec_string ("name",
403                                                         "Name",
404                                                         "Name that the manager is for",
405                                                         NULL,
406                                                         G_PARAM_READABLE |
407                                                         G_PARAM_WRITABLE |
408                                                         G_PARAM_CONSTRUCT_ONLY |
409                                                         G_PARAM_STATIC_STRINGS));
410
411   /**
412    * GDBusObjectManagerClient:name-owner:
413    *
414    * The unique name that owns #GDBusObjectManagerClient:name or %NULL if
415    * no-one is currently owning the name. Connect to the
416    * #GObject::notify signal to track changes to this property.
417    *
418    * Since: 2.30
419    */
420   g_object_class_install_property (gobject_class,
421                                    PROP_NAME_OWNER,
422                                    g_param_spec_string ("name-owner",
423                                                         "Name Owner",
424                                                         "The owner of the name we are watching",
425                                                         NULL,
426                                                         G_PARAM_READABLE |
427                                                         G_PARAM_STATIC_STRINGS));
428
429   /**
430    * GDBusObjectManagerClient:get-proxy-type-func:
431    *
432    * The #GDBusProxyTypeFunc to use when determining what #GType to
433    * use for interface proxies or %NULL.
434    *
435    * Since: 2.30
436    */
437   g_object_class_install_property (gobject_class,
438                                    PROP_GET_PROXY_TYPE_FUNC,
439                                    g_param_spec_pointer ("get-proxy-type-func",
440                                                          "GDBusProxyTypeFunc Function Pointer",
441                                                          "The GDBusProxyTypeFunc pointer to use",
442                                                          G_PARAM_READABLE |
443                                                          G_PARAM_WRITABLE |
444                                                          G_PARAM_CONSTRUCT_ONLY |
445                                                          G_PARAM_STATIC_STRINGS));
446
447   /**
448    * GDBusObjectManagerClient:get-proxy-type-user-data:
449    *
450    * The #gpointer user_data to pass to #GDBusObjectManagerClient:get-proxy-type-func.
451    *
452    * Since: 2.30
453    */
454   g_object_class_install_property (gobject_class,
455                                    PROP_GET_PROXY_TYPE_USER_DATA,
456                                    g_param_spec_pointer ("get-proxy-type-user-data",
457                                                          "GDBusProxyTypeFunc User Data",
458                                                          "The GDBusProxyTypeFunc user_data",
459                                                          G_PARAM_READABLE |
460                                                          G_PARAM_WRITABLE |
461                                                          G_PARAM_CONSTRUCT_ONLY |
462                                                          G_PARAM_STATIC_STRINGS));
463
464   /**
465    * GDBusObjectManagerClient::interface-proxy-signal:
466    * @manager: The #GDBusObjectManagerClient emitting the signal.
467    * @object_proxy: The #GDBusObjectProxy on which an interface is emitting a D-Bus signal.
468    * @interface_proxy: The #GDBusProxy that is emitting a D-Bus signal.
469    * @sender_name: The sender of the signal or NULL if the connection is not a bus connection.
470    * @signal_name: The signal name.
471    * @parameters: A #GVariant tuple with parameters for the signal.
472    *
473    * Emitted when a D-Bus signal is received on @interface_proxy.
474    *
475    * This signal exists purely as a convenience to avoid having to
476    * connect signals to all interface proxies managed by @manager.
477    *
478    * This signal is emitted in the
479    * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
480    * that @manager was constructed in.
481    *
482    * Since: 2.30
483    */
484   signals[INTERFACE_PROXY_SIGNAL_SIGNAL] =
485     g_signal_new ("interface-proxy-signal",
486                   G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
487                   G_SIGNAL_RUN_LAST,
488                   G_STRUCT_OFFSET (GDBusObjectManagerClientClass, interface_proxy_signal),
489                   NULL,
490                   NULL,
491                   _gio_marshal_VOID__OBJECT_OBJECT_STRING_STRING_VARIANT,
492                   G_TYPE_NONE,
493                   5,
494                   G_TYPE_DBUS_OBJECT_PROXY,
495                   G_TYPE_DBUS_PROXY,
496                   G_TYPE_STRING,
497                   G_TYPE_STRING,
498                   G_TYPE_VARIANT);
499
500   /**
501    * GDBusObjectManagerClient::interface-proxy-properties-changed:
502    * @manager: The #GDBusObjectManagerClient emitting the signal.
503    * @object_proxy: The #GDBusObjectProxy on which an interface has properties that are changing.
504    * @interface_proxy: The #GDBusProxy that has properties that are changing.
505    * @changed_properties: A #GVariant containing the properties that changed.
506    * @invalidated_properties: A %NULL terminated array of properties that was invalidated.
507    *
508    * Emitted when one or more D-Bus properties on proxy changes. The
509    * local cache has already been updated when this signal fires. Note
510    * that both @changed_properties and @invalidated_properties are
511    * guaranteed to never be %NULL (either may be empty though).
512    *
513    * This signal exists purely as a convenience to avoid having to
514    * connect signals to all interface proxies managed by @manager.
515    *
516    * This signal is emitted in the
517    * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
518    * that @manager was constructed in.
519    *
520    * Since: 2.30
521    */
522   signals[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL] =
523     g_signal_new ("interface-proxy-properties-changed",
524                   G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
525                   G_SIGNAL_RUN_LAST,
526                   G_STRUCT_OFFSET (GDBusObjectManagerClientClass, interface_proxy_properties_changed),
527                   NULL,
528                   NULL,
529                   _gio_marshal_VOID__OBJECT_OBJECT_VARIANT_BOXED,
530                   G_TYPE_NONE,
531                   4,
532                   G_TYPE_DBUS_OBJECT_PROXY,
533                   G_TYPE_DBUS_PROXY,
534                   G_TYPE_VARIANT,
535                   G_TYPE_STRV);
536
537   g_type_class_add_private (klass, sizeof (GDBusObjectManagerClientPrivate));
538 }
539
540 static void
541 g_dbus_object_manager_client_init (GDBusObjectManagerClient *manager)
542 {
543   manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
544                                                G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
545                                                GDBusObjectManagerClientPrivate);
546   manager->priv->map_object_path_to_object_proxy = g_hash_table_new_full (g_str_hash,
547                                                                           g_str_equal,
548                                                                           g_free,
549                                                                           (GDestroyNotify) g_object_unref);
550 }
551
552 /* ---------------------------------------------------------------------------------------------------- */
553
554 /**
555  * g_dbus_object_manager_client_new_sync:
556  * @connection: A #GDBusConnection.
557  * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
558  * @name: The owner of the control object (unique or well-known name).
559  * @object_path: The object path of the control object.
560  * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
561  * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
562  * @cancellable: A #GCancellable or %NULL
563  * @error: Return location for error or %NULL.
564  *
565  * Creates a new #GDBusObjectManagerClient object.
566  *
567  * This is a synchronous failable constructor - the calling thread is
568  * blocked until a reply is received. See g_dbus_object_manager_client_new()
569  * for the asynchronous version.
570  *
571  * Returns: A #GDBusObjectManagerClient object or %NULL if @error is
572  * set. Free with g_object_unref().
573  *
574  * Since: 2.30
575  */
576 GDBusObjectManager *
577 g_dbus_object_manager_client_new_sync (GDBusConnection               *connection,
578                                        GDBusObjectManagerClientFlags  flags,
579                                        const gchar                   *name,
580                                        const gchar                   *object_path,
581                                        GDBusProxyTypeFunc             get_proxy_type_func,
582                                        gpointer                       get_proxy_type_user_data,
583                                        GCancellable                  *cancellable,
584                                        GError                       **error)
585 {
586   GInitable *initable;
587
588   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
589   g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
590                         g_dbus_is_name (name), NULL);
591   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
592   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
593
594   initable = g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
595                              cancellable,
596                              error,
597                              "connection", connection,
598                              "flags", flags,
599                              "name", name,
600                              "object-path", object_path,
601                              "get-proxy-type-func", get_proxy_type_func,
602                              "get-proxy-type-user-data", get_proxy_type_user_data,
603                              NULL);
604   if (initable != NULL)
605     return G_DBUS_OBJECT_MANAGER (initable);
606   else
607     return NULL;
608 }
609
610 /**
611  * g_dbus_object_manager_client_new:
612  * @connection: A #GDBusConnection.
613  * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
614  * @name: The owner of the control object (unique or well-known name).
615  * @object_path: The object path of the control object.
616  * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
617  * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
618  * @cancellable: A #GCancellable or %NULL
619  * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
620  * @user_data: The data to pass to @callback.
621  *
622  * Asynchronously creates a new #GDBusObjectManagerClient object.
623  *
624  * This is an asynchronous failable constructor. When the result is
625  * ready, @callback will be invoked in the
626  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
627  * of the thread you are calling this method from. You can
628  * then call g_dbus_object_manager_client_new_finish() to get the result. See
629  * g_dbus_object_manager_client_new_sync() for the synchronous version.
630  *
631  * Since: 2.30
632  */
633 void
634 g_dbus_object_manager_client_new (GDBusConnection               *connection,
635                                   GDBusObjectManagerClientFlags  flags,
636                                   const gchar                   *name,
637                                   const gchar                   *object_path,
638                                   GDBusProxyTypeFunc             get_proxy_type_func,
639                                   gpointer                       get_proxy_type_user_data,
640                                   GCancellable                  *cancellable,
641                                   GAsyncReadyCallback            callback,
642                                   gpointer                       user_data)
643 {
644   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
645   g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
646                         g_dbus_is_name (name));
647   g_return_if_fail (g_variant_is_object_path (object_path));
648
649   g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
650                               G_PRIORITY_DEFAULT,
651                               cancellable,
652                               callback,
653                               user_data,
654                               "connection", connection,
655                               "flags", flags,
656                               "name", name,
657                               "object-path", object_path,
658                               "get-proxy-type-func", get_proxy_type_func,
659                               "get-proxy-type-user-data", get_proxy_type_user_data,
660                               NULL);
661 }
662
663 /**
664  * g_dbus_object_manager_client_new_finish:
665  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new().
666  * @error: Return location for error or %NULL.
667  *
668  * Finishes an operation started with g_dbus_object_manager_client_new().
669  *
670  * Returns: A #GDBusObjectManagerClient object or %NULL if @error is
671  * set. Free with g_object_unref().
672  *
673  * Since: 2.30
674  */
675 GDBusObjectManager *
676 g_dbus_object_manager_client_new_finish (GAsyncResult   *res,
677                                  GError        **error)
678 {
679   GObject *object;
680   GObject *source_object;
681
682   source_object = g_async_result_get_source_object (res);
683   g_assert (source_object != NULL);
684
685   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
686                                         res,
687                                         error);
688   g_object_unref (source_object);
689
690   if (object != NULL)
691     return G_DBUS_OBJECT_MANAGER (object);
692   else
693     return NULL;
694 }
695
696 /* ---------------------------------------------------------------------------------------------------- */
697
698 /**
699  * g_dbus_object_manager_client_new_for_bus_sync:
700  * @bus_type: A #GBusType.
701  * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
702  * @name: The owner of the control object (unique or well-known name).
703  * @object_path: The object path of the control object.
704  * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
705  * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
706  * @cancellable: A #GCancellable or %NULL
707  * @error: Return location for error or %NULL.
708  *
709  * Like g_dbus_object_manager_client_new_sync() but takes a #GBusType instead
710  * of a #GDBusConnection.
711  *
712  * This is a synchronous failable constructor - the calling thread is
713  * blocked until a reply is received. See g_dbus_object_manager_client_new_for_bus()
714  * for the asynchronous version.
715  *
716  * Returns: A #GDBusObjectManagerClient object or %NULL if @error is
717  * set. Free with g_object_unref().
718  *
719  * Since: 2.30
720  */
721 GDBusObjectManager *
722 g_dbus_object_manager_client_new_for_bus_sync (GBusType                       bus_type,
723                                                GDBusObjectManagerClientFlags  flags,
724                                                const gchar                   *name,
725                                                const gchar                   *object_path,
726                                                GDBusProxyTypeFunc             get_proxy_type_func,
727                                                gpointer                       get_proxy_type_user_data,
728                                                GCancellable                  *cancellable,
729                                                GError                       **error)
730 {
731   GInitable *initable;
732
733   g_return_val_if_fail (bus_type != G_BUS_TYPE_NONE, NULL);
734   g_return_val_if_fail (g_dbus_is_name (name), NULL);
735   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
736   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
737
738   initable = g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
739                              cancellable,
740                              error,
741                              "bus-type", bus_type,
742                              "flags", flags,
743                              "name", name,
744                              "object-path", object_path,
745                              "get-proxy-type-func", get_proxy_type_func,
746                              "get-proxy-type-user-data", get_proxy_type_user_data,
747                              NULL);
748   if (initable != NULL)
749     return G_DBUS_OBJECT_MANAGER (initable);
750   else
751     return NULL;
752 }
753
754 /**
755  * g_dbus_object_manager_client_new_for_bus:
756  * @bus_type: A #GBusType.
757  * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
758  * @name: The owner of the control object (unique or well-known name).
759  * @object_path: The object path of the control object.
760  * @get_proxy_type_func: A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
761  * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
762  * @cancellable: A #GCancellable or %NULL
763  * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
764  * @user_data: The data to pass to @callback.
765  *
766  * Like g_dbus_object_manager_client_new() but takes a #GBusType instead of a
767  * #GDBusConnection.
768  *
769  * This is an asynchronous failable constructor. When the result is
770  * ready, @callback will be invoked in the
771  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
772  * of the thread you are calling this method from. You can
773  * then call g_dbus_object_manager_client_new_for_bus_finish() to get the result. See
774  * g_dbus_object_manager_client_new_for_bus_sync() for the synchronous version.
775  *
776  * Since: 2.30
777  */
778 void
779 g_dbus_object_manager_client_new_for_bus (GBusType                       bus_type,
780                                           GDBusObjectManagerClientFlags  flags,
781                                           const gchar                   *name,
782                                           const gchar                   *object_path,
783                                           GDBusProxyTypeFunc             get_proxy_type_func,
784                                           gpointer                       get_proxy_type_user_data,
785                                           GCancellable                  *cancellable,
786                                           GAsyncReadyCallback            callback,
787                                           gpointer                       user_data)
788 {
789   g_return_if_fail (bus_type != G_BUS_TYPE_NONE);
790   g_return_if_fail (g_dbus_is_name (name));
791   g_return_if_fail (g_variant_is_object_path (object_path));
792
793   g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT,
794                               G_PRIORITY_DEFAULT,
795                               cancellable,
796                               callback,
797                               user_data,
798                               "bus-type", bus_type,
799                               "flags", flags,
800                               "name", name,
801                               "object-path", object_path,
802                               "get-proxy-type-func", get_proxy_type_func,
803                               "get-proxy-type-user-data", get_proxy_type_user_data,
804                               NULL);
805 }
806
807 /**
808  * g_dbus_object_manager_client_new_for_bus_finish:
809  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new_for_bus().
810  * @error: Return location for error or %NULL.
811  *
812  * Finishes an operation started with g_dbus_object_manager_client_new_for_bus().
813  *
814  * Returns: A #GDBusObjectManagerClient object or %NULL if @error is
815  * set. Free with g_object_unref().
816  *
817  * Since: 2.30
818  */
819 GDBusObjectManager *
820 g_dbus_object_manager_client_new_for_bus_finish (GAsyncResult   *res,
821                                                  GError        **error)
822 {
823   GObject *object;
824   GObject *source_object;
825
826   source_object = g_async_result_get_source_object (res);
827   g_assert (source_object != NULL);
828
829   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
830                                         res,
831                                         error);
832   g_object_unref (source_object);
833
834   if (object != NULL)
835     return G_DBUS_OBJECT_MANAGER (object);
836   else
837     return NULL;
838 }
839
840 /* ---------------------------------------------------------------------------------------------------- */
841
842 /**
843  * g_dbus_object_manager_client_get_connection:
844  * @manager: A #GDBusObjectManagerClient
845  *
846  * Gets the #GDBusConnection used by @manager.
847  *
848  * Returns: A #GDBusConnection object. Do not free, the object belongs
849  * to @manager.
850  *
851  * Since: 2.30
852  */
853 GDBusConnection *
854 g_dbus_object_manager_client_get_connection (GDBusObjectManagerClient *manager)
855 {
856   g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
857   return manager->priv->connection;
858 }
859
860 /**
861  * g_dbus_object_manager_client_get_name:
862  * @manager: A #GDBusObjectManagerClient
863  *
864  * Gets the name that @manager is for.
865  *
866  * Returns: A unique or well-known name. Do not free, the string
867  * belongs to @manager.
868  *
869  * Since: 2.30
870  */
871 const gchar *
872 g_dbus_object_manager_client_get_name (GDBusObjectManagerClient *manager)
873 {
874   g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
875   return manager->priv->name;
876 }
877
878 /**
879  * g_dbus_object_manager_client_get_flags:
880  * @manager: A #GDBusObjectManagerClient
881  *
882  * Gets the flags that @manager was constructed with.
883  *
884  * Returns: Zero of more flags from the #GDBusObjectManagerClientFlags
885  * enumeration.
886  *
887  * Since: 2.30
888  */
889 GDBusObjectManagerClientFlags
890 g_dbus_object_manager_client_get_flags (GDBusObjectManagerClient *manager)
891 {
892   g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE);
893   return manager->priv->flags;
894 }
895
896 /**
897  * g_dbus_object_manager_client_get_name_owner:
898  * @manager: A #GDBusObjectManagerClient.
899  *
900  * The unique name that owns the name that @manager is for or %NULL if
901  * no-one currently owns that name. You can connect to the
902  * #GObject::notify signal to track changes to the
903  * #GDBusObjectManagerClient:name-owner property.
904  *
905  * Returns: The name owner or %NULL if no name owner exists. Free with
906  * g_free().
907  *
908  * Since: 2.30
909  */
910 gchar *
911 g_dbus_object_manager_client_get_name_owner (GDBusObjectManagerClient *manager)
912 {
913   g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
914   return g_strdup (manager->priv->name_owner);
915 }
916
917 /* ---------------------------------------------------------------------------------------------------- */
918
919 /* signal handler for all objects we manage - we dispatch signals
920  * from here to the objects
921  */
922 static void
923 signal_cb (GDBusConnection *connection,
924            const gchar     *sender_name,
925            const gchar     *object_path,
926            const gchar     *interface_name,
927            const gchar     *signal_name,
928            GVariant        *parameters,
929            gpointer         user_data)
930 {
931   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
932   GDBusObjectProxy *object_proxy;
933   GDBusInterface *interface;
934
935   object_proxy = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
936   if (object_proxy == NULL)
937     goto out;
938
939   //g_debug ("yay, signal_cb %s %s: %s\n", signal_name, object_path, g_variant_print (parameters, TRUE));
940
941   if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0)
942     {
943       if (g_strcmp0 (signal_name, "PropertiesChanged") == 0)
944         {
945           const gchar *interface_name;
946           GVariant *changed_properties;
947           const gchar **invalidated_properties;
948
949           g_variant_get (parameters,
950                          "(&s@a{sv}^a&s)",
951                          &interface_name,
952                          &changed_properties,
953                          &invalidated_properties);
954
955           interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy), interface_name);
956           if (interface != NULL)
957             {
958               GVariantIter property_iter;
959               const gchar *property_name;
960               GVariant *property_value;
961               guint n;
962
963               /* update caches... */
964               g_variant_iter_init (&property_iter, changed_properties);
965               while (g_variant_iter_next (&property_iter,
966                                           "{&sv}",
967                                           &property_name,
968                                           &property_value))
969                 {
970                   g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface),
971                                                     property_name,
972                                                     property_value);
973                   g_variant_unref (property_value);
974                 }
975
976               for (n = 0; invalidated_properties[n] != NULL; n++)
977                 {
978                   g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface),
979                                                     invalidated_properties[n],
980                                                     NULL);
981                 }
982               /* ... and then synthesize the signal */
983               g_signal_emit (manager,
984                              signals[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL],
985                              0,
986                              object_proxy,
987                              interface,
988                              changed_properties,
989                              invalidated_properties);
990               g_signal_emit_by_name (interface,
991                                      "g-properties-changed",
992                                      changed_properties,
993                                      invalidated_properties);
994               g_object_unref (interface);
995             }
996           g_variant_unref (changed_properties);
997           g_free (invalidated_properties);
998         }
999     }
1000   else
1001     {
1002       /* regular signal - just dispatch it */
1003       interface = g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy), interface_name);
1004       if (interface != NULL)
1005         {
1006           g_signal_emit (manager,
1007                          signals[INTERFACE_PROXY_SIGNAL_SIGNAL],
1008                          0,
1009                          object_proxy,
1010                          interface,
1011                          sender_name,
1012                          signal_name,
1013                          parameters);
1014           g_signal_emit_by_name (interface,
1015                                  "g-signal",
1016                                  sender_name,
1017                                  signal_name,
1018                                  parameters);
1019           g_object_unref (interface);
1020         }
1021     }
1022
1023  out:
1024   ;
1025 }
1026
1027 static void
1028 subscribe_signals (GDBusObjectManagerClient *manager,
1029                    const gchar *name_owner)
1030 {
1031   GError *error;
1032   GVariant *ret;
1033
1034   g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager));
1035   g_return_if_fail (manager->priv->signal_subscription_id == 0);
1036   g_return_if_fail (g_dbus_is_unique_name (name_owner));
1037
1038   /* the bus daemon may not implement path_prefix so gracefully
1039    * handle this by using a fallback
1040    */
1041   manager->priv->match_rule = g_strdup_printf ("type='signal',sender='%s',path_namespace='%s'",
1042                                                name_owner,
1043                                                manager->priv->object_path);
1044
1045   error = NULL;
1046   ret = g_dbus_connection_call_sync (manager->priv->connection,
1047                                      "org.freedesktop.DBus",
1048                                      "/org/freedeskop/DBus",
1049                                      "org.freedesktop.DBus",
1050                                      "AddMatch",
1051                                      g_variant_new ("(s)",
1052                                                     manager->priv->match_rule),
1053                                      NULL, /* reply_type */
1054                                      G_DBUS_CALL_FLAGS_NONE,
1055                                      -1, /* default timeout */
1056                                      NULL, /* TODO: Cancellable */
1057                                      &error);
1058   if (ret != NULL)
1059     {
1060       /* yay, bus daemon supports path_namespace */
1061       g_variant_unref (ret);
1062
1063       /* still need to ask GDBusConnection for the callbacks */
1064       manager->priv->signal_subscription_id =
1065         g_dbus_connection_signal_subscribe (manager->priv->connection,
1066                                             name_owner,
1067                                             NULL, /* interface */
1068                                             NULL, /* member */
1069                                             NULL, /* path - TODO: really want wilcard support here */
1070                                             NULL, /* arg0 */
1071                                             G_DBUS_SIGNAL_FLAGS_NONE |
1072                                             G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
1073                                             signal_cb,
1074                                             manager,
1075                                             NULL); /* user_data_free_func */
1076
1077     }
1078   else
1079     {
1080       /* TODO: we could report this to the user
1081       g_warning ("Message bus daemon does not support path_namespace: %s (%s %d)",
1082                  error->message,
1083                  g_quark_to_string (error->domain),
1084                  error->code);
1085       */
1086
1087       g_error_free (error);
1088
1089       /* no need to call RemoveMatch when done since it didn't work */
1090       g_free (manager->priv->match_rule);
1091       manager->priv->match_rule = NULL;
1092
1093       /* Fallback is to subscribe to *all* signals from the name owner which
1094        * is rather wasteful. It's probably not a big practical problem because
1095        * users typically want all objects that the name owner supplies.
1096        */
1097       manager->priv->signal_subscription_id =
1098         g_dbus_connection_signal_subscribe (manager->priv->connection,
1099                                             name_owner,
1100                                             NULL, /* interface */
1101                                             NULL, /* member */
1102                                             NULL, /* path - TODO: really want wilcard support here */
1103                                             NULL, /* arg0 */
1104                                             G_DBUS_SIGNAL_FLAGS_NONE,
1105                                             signal_cb,
1106                                             manager,
1107                                             NULL); /* user_data_free_func */
1108     }
1109 }
1110
1111 static void
1112 maybe_unsubscribe_signals (GDBusObjectManagerClient *manager)
1113 {
1114   g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager));
1115
1116   if (manager->priv->signal_subscription_id > 0)
1117     {
1118       g_dbus_connection_signal_unsubscribe (manager->priv->connection,
1119                                             manager->priv->signal_subscription_id);
1120       manager->priv->signal_subscription_id = 0;
1121     }
1122
1123   if (manager->priv->match_rule != NULL)
1124     {
1125       /* Since the AddMatch call succeeded this is guaranteed to not
1126        * fail - therefore, don't bother checking the return value
1127        */
1128       g_dbus_connection_call (manager->priv->connection,
1129                               "org.freedesktop.DBus",
1130                               "/org/freedeskop/DBus",
1131                               "org.freedesktop.DBus",
1132                               "RemoveMatch",
1133                               g_variant_new ("(s)",
1134                                              manager->priv->match_rule),
1135                               NULL, /* reply_type */
1136                               G_DBUS_CALL_FLAGS_NONE,
1137                               -1, /* default timeout */
1138                               NULL, /* GCancellable */
1139                               NULL, /* GAsyncReadyCallback */
1140                               NULL); /* user data */
1141       g_free (manager->priv->match_rule);
1142       manager->priv->match_rule = NULL;
1143     }
1144
1145 }
1146
1147 /* ---------------------------------------------------------------------------------------------------- */
1148
1149 static void
1150 on_notify_g_name_owner (GObject    *object,
1151                         GParamSpec *pspec,
1152                         gpointer    user_data)
1153 {
1154   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
1155   gchar *old_name_owner;
1156   gchar *new_name_owner;
1157
1158   old_name_owner = manager->priv->name_owner;
1159   new_name_owner = g_dbus_proxy_get_name_owner (manager->priv->control_proxy);
1160   manager->priv->name_owner = NULL;
1161
1162   if (g_strcmp0 (old_name_owner, new_name_owner) != 0)
1163     {
1164       GList *l;
1165       GList *proxies;
1166
1167       /* do the :name-owner notify with a NULL name - this way the user knows
1168        * the ::object-proxy-removed following is because the name owner went
1169        * away
1170        */
1171       g_object_notify (G_OBJECT (manager), "name-owner");
1172
1173       /* remote manager changed; nuke all local proxies  */
1174       proxies = g_hash_table_get_values (manager->priv->map_object_path_to_object_proxy);
1175       g_list_foreach (proxies, (GFunc) g_object_ref, NULL);
1176       g_hash_table_remove_all (manager->priv->map_object_path_to_object_proxy);
1177       for (l = proxies; l != NULL; l = l->next)
1178         {
1179           GDBusObjectProxy *object_proxy = G_DBUS_OBJECT_PROXY (l->data);
1180           g_signal_emit_by_name (manager, "object-removed", object_proxy);
1181         }
1182       g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
1183       g_list_free (proxies);
1184
1185       /* nuke local filter */
1186       maybe_unsubscribe_signals (manager);
1187     }
1188
1189   if (new_name_owner != NULL)
1190     {
1191       GError *error;
1192       GVariant *value;
1193
1194       //g_debug ("repopulating for %s", new_name_owner);
1195
1196       /* TODO: do this async! */
1197       subscribe_signals (manager,
1198                          new_name_owner);
1199       error = NULL;
1200       value = g_dbus_proxy_call_sync (manager->priv->control_proxy,
1201                                       "GetManagedObjects",
1202                                       NULL, /* parameters */
1203                                       G_DBUS_CALL_FLAGS_NONE,
1204                                       -1,
1205                                       NULL,
1206                                       &error);
1207       if (value == NULL)
1208         {
1209           maybe_unsubscribe_signals (manager);
1210           g_warning ("Error calling GetManagedObjects() when name owner %s for name %s came back: %s",
1211                      new_name_owner,
1212                      manager->priv->name,
1213                      error->message);
1214           g_error_free (error);
1215         }
1216       else
1217         {
1218           process_get_all_result (manager, value, new_name_owner);
1219           g_variant_unref (value);
1220         }
1221
1222       /* do the :name-owner notify *AFTER* emitting ::object-proxy-added signals - this
1223        * way the user knows that the signals were emitted because the name owner came back
1224        */
1225       manager->priv->name_owner = new_name_owner;
1226       g_object_notify (G_OBJECT (manager), "name-owner");
1227
1228     }
1229   g_free (old_name_owner);
1230 }
1231
1232 static gboolean
1233 initable_init (GInitable     *initable,
1234                GCancellable  *cancellable,
1235                GError       **error)
1236 {
1237   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (initable);
1238   gboolean ret;
1239   GVariant *value;
1240   GDBusProxyFlags proxy_flags;
1241
1242   ret = FALSE;
1243
1244   if (manager->priv->bus_type != G_BUS_TYPE_NONE)
1245     {
1246       g_assert (manager->priv->connection == NULL);
1247       manager->priv->connection = g_bus_get_sync (manager->priv->bus_type, cancellable, error);
1248       if (manager->priv->connection == NULL)
1249         goto out;
1250     }
1251
1252   proxy_flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
1253   if (manager->priv->flags & G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START)
1254     proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;;
1255
1256   manager->priv->control_proxy = g_dbus_proxy_new_sync (manager->priv->connection,
1257                                                         proxy_flags,
1258                                                         NULL, /* GDBusInterfaceInfo* */
1259                                                         manager->priv->name,
1260                                                         manager->priv->object_path,
1261                                                         "org.freedesktop.DBus.ObjectManager",
1262                                                         cancellable,
1263                                                         error);
1264   if (manager->priv->control_proxy == NULL)
1265     goto out;
1266
1267   g_signal_connect (G_OBJECT (manager->priv->control_proxy),
1268                     "notify::g-name-owner",
1269                     G_CALLBACK (on_notify_g_name_owner),
1270                     manager);
1271
1272   manager->priv->name_owner = g_dbus_proxy_get_name_owner (manager->priv->control_proxy);
1273   if (manager->priv->name_owner == NULL)
1274     {
1275       /* it's perfectly fine if there's no name owner.. we're just going to
1276        * wait until one is ready
1277        */
1278     }
1279   else
1280     {
1281       /* yay, we have a name owner */
1282       g_signal_connect (manager->priv->control_proxy,
1283                         "g-signal",
1284                         G_CALLBACK (on_control_proxy_g_signal),
1285                         manager);
1286       subscribe_signals (manager,
1287                          manager->priv->name_owner);
1288       value = g_dbus_proxy_call_sync (manager->priv->control_proxy,
1289                                       "GetManagedObjects",
1290                                       NULL, /* parameters */
1291                                       G_DBUS_CALL_FLAGS_NONE,
1292                                       -1,
1293                                       cancellable,
1294                                       error);
1295       if (value == NULL)
1296         {
1297           maybe_unsubscribe_signals (manager);
1298           g_warn_if_fail (g_signal_handlers_disconnect_by_func (manager->priv->control_proxy,
1299                                                                 on_control_proxy_g_signal,
1300                                                                 manager) == 1);
1301           g_object_unref (manager->priv->control_proxy);
1302           manager->priv->control_proxy = NULL;
1303           goto out;
1304         }
1305
1306       process_get_all_result (manager, value, manager->priv->name_owner);
1307       g_variant_unref (value);
1308     }
1309
1310   ret = TRUE;
1311
1312  out:
1313   return ret;
1314 }
1315
1316 static void
1317 initable_iface_init (GInitableIface *initable_iface)
1318 {
1319   initable_iface->init = initable_init;
1320 }
1321
1322 static void
1323 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1324 {
1325   /* for now, just use default: run GInitable code in thread */
1326 }
1327
1328 /* ---------------------------------------------------------------------------------------------------- */
1329
1330 static void
1331 add_interfaces (GDBusObjectManagerClient *manager,
1332                 const gchar       *object_path,
1333                 GVariant          *ifaces_and_properties,
1334                 const gchar       *name_owner)
1335 {
1336   GDBusObjectProxy *op;
1337   gboolean added;
1338   GVariantIter iter;
1339   const gchar *interface_name;
1340   GVariant *properties;
1341
1342   g_return_if_fail (g_dbus_is_unique_name (name_owner));
1343
1344   added = FALSE;
1345   op = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
1346   if (op == NULL)
1347     {
1348       op = _g_dbus_object_proxy_new (manager->priv->connection, object_path);
1349       added = TRUE;
1350     }
1351
1352   g_variant_iter_init (&iter, ifaces_and_properties);
1353   while (g_variant_iter_next (&iter,
1354                               "{&s@a{sv}}",
1355                               &interface_name,
1356                               &properties))
1357     {
1358       GDBusProxy *interface_proxy;
1359       GError *error;
1360       GType interface_proxy_type;
1361
1362       if (manager->priv->get_proxy_type_func != NULL)
1363         {
1364           interface_proxy_type = manager->priv->get_proxy_type_func (manager,
1365                                                                      object_path,
1366                                                                      interface_name,
1367                                                                      manager->priv->get_proxy_type_user_data);
1368           g_warn_if_fail (g_type_is_a (interface_proxy_type, G_TYPE_DBUS_PROXY));
1369         }
1370       else
1371         {
1372           interface_proxy_type = G_TYPE_DBUS_PROXY;
1373         }
1374
1375       /* this is fine - there is no blocking IO because we pass DO_NOT_LOAD_PROPERTIES and
1376        * DO_NOT_CONNECT_SIGNALS and use a unique name
1377        */
1378       error = NULL;
1379       interface_proxy = g_initable_new (interface_proxy_type,
1380                                         NULL, /* GCancellable */
1381                                         &error,
1382                                         "g-connection", manager->priv->connection,
1383                                         "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1384                                                    G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1385                                         "g-name", name_owner,
1386                                         "g-object-path", object_path,
1387                                         "g-interface-name", interface_name,
1388                                         NULL);
1389       if (interface_proxy == NULL)
1390         {
1391           g_warning ("%s: Error constructing proxy for path %s and interface %s: %s",
1392                      G_STRLOC,
1393                      object_path,
1394                      interface_name,
1395                      error->message);
1396           g_error_free (error);
1397         }
1398       else
1399         {
1400           GVariantIter property_iter;
1401           const gchar *property_name;
1402           GVariant *property_value;
1403
1404           /* associate the interface proxy with the object */
1405           g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_proxy),
1406                                        G_DBUS_OBJECT (op));
1407
1408           g_variant_iter_init (&property_iter, properties);
1409           while (g_variant_iter_next (&property_iter,
1410                                       "{&sv}",
1411                                       &property_name,
1412                                       &property_value))
1413             {
1414               g_dbus_proxy_set_cached_property (interface_proxy,
1415                                                 property_name,
1416                                                 property_value);
1417               g_variant_unref (property_value);
1418             }
1419
1420           _g_dbus_object_proxy_add_interface (op, interface_proxy);
1421           if (!added)
1422             g_signal_emit_by_name (manager, "interface-added", op, interface_proxy);
1423           g_object_unref (interface_proxy);
1424         }
1425       g_variant_unref (properties);
1426     }
1427
1428   if (added)
1429     {
1430       g_hash_table_insert (manager->priv->map_object_path_to_object_proxy,
1431                            g_strdup (object_path),
1432                            op);
1433       g_signal_emit_by_name (manager, "object-added", op);
1434     }
1435 }
1436
1437 static void
1438 remove_interfaces (GDBusObjectManagerClient   *manager,
1439                    const gchar         *object_path,
1440                    const gchar *const  *interface_names)
1441 {
1442   GDBusObjectProxy *op;
1443   GList *interfaces;
1444   guint n;
1445   guint num_interfaces;
1446   guint num_interfaces_to_remove;
1447
1448   op = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
1449   if (op == NULL)
1450     {
1451       g_warning ("%s: Processing InterfaceRemoved signal for path %s but no object proxy exists",
1452                  G_STRLOC,
1453                  object_path);
1454       goto out;
1455     }
1456
1457   interfaces = g_dbus_object_get_interfaces (G_DBUS_OBJECT (op));
1458   num_interfaces = g_list_length (interfaces);
1459   g_list_foreach (interfaces, (GFunc) g_object_unref, NULL);
1460   g_list_free (interfaces);
1461
1462   num_interfaces_to_remove = g_strv_length ((gchar **) interface_names);
1463
1464   /* see if we are going to completety remove the object */
1465   if (num_interfaces_to_remove == num_interfaces)
1466     {
1467       g_object_ref (op);
1468       g_warn_if_fail (g_hash_table_remove (manager->priv->map_object_path_to_object_proxy, object_path));
1469       g_signal_emit_by_name (manager, "object-removed", op);
1470       g_object_unref (op);
1471     }
1472   else
1473     {
1474       for (n = 0; interface_names != NULL && interface_names[n] != NULL; n++)
1475         {
1476           GDBusInterface *interface;
1477           interface = g_dbus_object_get_interface (G_DBUS_OBJECT (op), interface_names[n]);
1478           _g_dbus_object_proxy_remove_interface (op, interface_names[n]);
1479           if (interface != NULL)
1480             {
1481               g_signal_emit_by_name (manager, "interface-removed", op, interface);
1482               g_object_unref (interface);
1483             }
1484         }
1485     }
1486  out:
1487   ;
1488 }
1489
1490 static void
1491 process_get_all_result (GDBusObjectManagerClient *manager,
1492                         GVariant          *value,
1493                         const gchar       *name_owner)
1494 {
1495   GVariant *arg0;
1496   const gchar *object_path;
1497   GVariant *ifaces_and_properties;
1498   GVariantIter iter;
1499
1500   g_return_if_fail (g_dbus_is_unique_name (name_owner));
1501
1502   arg0 = g_variant_get_child_value (value, 0);
1503   g_variant_iter_init (&iter, arg0);
1504   while (g_variant_iter_next (&iter,
1505                               "{&o@a{sa{sv}}}",
1506                               &object_path,
1507                               &ifaces_and_properties))
1508     {
1509       add_interfaces (manager, object_path, ifaces_and_properties, name_owner);
1510       g_variant_unref (ifaces_and_properties);
1511     }
1512   g_variant_unref (arg0);
1513 }
1514
1515 static void
1516 on_control_proxy_g_signal (GDBusProxy   *proxy,
1517                            const gchar  *sender_name,
1518                            const gchar  *signal_name,
1519                            GVariant     *parameters,
1520                            gpointer      user_data)
1521 {
1522   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (user_data);
1523   const gchar *object_path;
1524
1525   //g_debug ("yay, g_signal %s: %s\n", signal_name, g_variant_print (parameters, TRUE));
1526
1527   if (g_strcmp0 (signal_name, "InterfacesAdded") == 0)
1528     {
1529       GVariant *ifaces_and_properties;
1530       g_variant_get (parameters,
1531                      "(&o@a{sa{sv}})",
1532                      &object_path,
1533                      &ifaces_and_properties);
1534       add_interfaces (manager, object_path, ifaces_and_properties, manager->priv->name_owner);
1535       g_variant_unref (ifaces_and_properties);
1536     }
1537   else if (g_strcmp0 (signal_name, "InterfacesRemoved") == 0)
1538     {
1539       const gchar **ifaces;
1540       g_variant_get (parameters,
1541                      "(&o^a&s)",
1542                      &object_path,
1543                      &ifaces);
1544       remove_interfaces (manager, object_path, ifaces);
1545       g_free (ifaces);
1546     }
1547 }
1548
1549 /* ---------------------------------------------------------------------------------------------------- */
1550
1551 static const gchar *
1552 g_dbus_object_manager_client_get_object_path (GDBusObjectManager *_manager)
1553 {
1554   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
1555   return manager->priv->object_path;
1556 }
1557
1558 static GDBusObject *
1559 g_dbus_object_manager_client_get_object (GDBusObjectManager *_manager,
1560                                          const gchar        *object_path)
1561 {
1562   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
1563   GDBusObject *ret;
1564
1565   ret = g_hash_table_lookup (manager->priv->map_object_path_to_object_proxy, object_path);
1566   if (ret != NULL)
1567     g_object_ref (ret);
1568   return ret;
1569 }
1570
1571 static GDBusInterface *
1572 g_dbus_object_manager_client_get_interface  (GDBusObjectManager  *_manager,
1573                                              const gchar         *object_path,
1574                                              const gchar         *interface_name)
1575 {
1576   GDBusInterface *ret;
1577   GDBusObject *object;
1578
1579   ret = NULL;
1580
1581   object = g_dbus_object_manager_get_object (_manager, object_path);
1582   if (object == NULL)
1583     goto out;
1584
1585   ret = g_dbus_object_get_interface (object, interface_name);
1586   g_object_unref (object);
1587
1588  out:
1589   return ret;
1590 }
1591
1592 static GList *
1593 g_dbus_object_manager_client_get_objects (GDBusObjectManager *_manager)
1594 {
1595   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (_manager);
1596   GList *ret;
1597
1598   g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager), NULL);
1599
1600   ret = g_hash_table_get_values (manager->priv->map_object_path_to_object_proxy);
1601   g_list_foreach (ret, (GFunc) g_object_ref, NULL);
1602   return ret;
1603 }
1604
1605
1606 static void
1607 dbus_object_manager_interface_init (GDBusObjectManagerIface *iface)
1608 {
1609   iface->get_object_path = g_dbus_object_manager_client_get_object_path;
1610   iface->get_objects     = g_dbus_object_manager_client_get_objects;
1611   iface->get_object      = g_dbus_object_manager_client_get_object;
1612   iface->get_interface   = g_dbus_object_manager_client_get_interface;
1613 }
1614
1615 /* ---------------------------------------------------------------------------------------------------- */