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