1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusobjectmanager.h"
26 #include "gdbusobjectmanagerclient.h"
27 #include "gdbusobject.h"
28 #include "gdbusprivate.h"
29 #include "gioenumtypes.h"
30 #include "ginitable.h"
31 #include "gasyncresult.h"
32 #include "gsimpleasyncresult.h"
33 #include "gasyncinitable.h"
34 #include "gdbusconnection.h"
35 #include "gdbusutils.h"
36 #include "gdbusobject.h"
37 #include "gdbusobjectproxy.h"
38 #include "gdbusproxy.h"
39 #include "gdbusinterface.h"
44 * SECTION:gdbusobjectmanagerclient
45 * @short_description: Client-side object manager
48 * #GDBusObjectManagerClient is used to create, monitor and delete object
49 * proxies for remote objects exported by a #GDBusObjectManagerServer (or any
50 * code implementing the <ulink
51 * url="http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager">org.freedesktop.DBus.ObjectManager</ulink>
54 * Once an instance of this type has been created, you can connect to
55 * the #GDBusObjectManager::object-added and
56 * #GDBusObjectManager::object-removed signals and inspect the
57 * #GDBusObjectProxy objects returned by
58 * g_dbus_object_manager_get_objects().
60 * If the name for a #GDBusObjectManagerClient is not owned by anyone at
61 * object construction time, the default behavior is to request the
62 * message bus to launch an owner for the name. This behavior can be
63 * disabled using the %G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START
64 * flag. It's also worth noting that this only works if the name of
65 * interest is activatable in the first place. E.g. in some cases it
66 * is not possible to launch an owner for the requested name. In this
67 * case, #GDBusObjectManagerClient object construction still succeeds but
68 * there will be no object proxies
69 * (e.g. g_dbus_object_manager_get_objects() returns the empty list) and
70 * the #GDBusObjectManagerClient:name-owner property is %NULL.
72 * The owner of the requested name can come and go (for example
73 * consider a system service being restarted) – #GDBusObjectManagerClient
74 * handles this case too; simply connect to the #GObject::notify
75 * signal to watch for changes on the #GDBusObjectManagerClient:name-owner
76 * property. When the name owner vanishes, the behavior is that
77 * #GDBusObjectManagerClient:name-owner is set to %NULL (this includes
78 * emission of the #GObject::notify signal) and then
79 * #GDBusObjectManager::object-removed signals are synthesized
80 * for all currently existing object proxies. Since
81 * #GDBusObjectManagerClient:name-owner is %NULL when this happens, you can
82 * use this information to disambiguate a synthesized signal from a
83 * genuine signal caused by object removal on the remote
84 * #GDBusObjectManager. Similarly, when a new name owner appears,
85 * #GDBusObjectManager::object-added signals are synthesized
86 * while #GDBusObjectManagerClient:name-owner is still %NULL. Only when all
87 * object proxies have been added, the #GDBusObjectManagerClient:name-owner
88 * is set to the new name owner (this includes emission of the
89 * #GObject::notify signal). Furthermore, you are guaranteed that
90 * #GDBusObjectManagerClient:name-owner will alternate between a name owner
91 * (e.g. <literal>:1.42</literal>) and %NULL even in the case where
92 * the name of interest is atomically replaced
94 * Ultimately, #GDBusObjectManagerClient is used to obtain #GDBusProxy
95 * instances. All signals (including the
96 * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
97 * signal) delivered to #GDBusProxy instances are guaranteed to
98 * originate from the name owner. This guarantee along with the
99 * behavior described above, means that certain race conditions
100 * including the <emphasis><quote>half the proxy is from the old owner
101 * and the other half is from the new owner</quote></emphasis> problem
104 * To avoid having the application connect to signals on the returned
105 * #GDBusObjectProxy and #GDBusProxy objects, the
106 * #GDBusObject::interface-added,
107 * #GDBusObject::interface-removed,
108 * #GDBusProxy::g-properties-changed and
109 * #GDBusProxy::g-signal signals
110 * are also emitted on the #GDBusObjectManagerClient instance managing these
111 * objects. The signals emitted are
112 * #GDBusObjectManager::interface-added,
113 * #GDBusObjectManager::interface-removed,
114 * #GDBusObjectManagerClient::interface-proxy-properties-changed and
115 * #GDBusObjectManagerClient::interface-proxy-signal.
117 * Note that all callbacks and signals are emitted in the
118 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
119 * that the #GDBusObjectManagerClient object was constructed
120 * in. Additionally, the #GDBusObjectProxy and #GDBusProxy objects
121 * originating from the #GDBusObjectManagerClient object will be created in
122 * the same context and, consequently, will deliver signals in the
126 struct _GDBusObjectManagerClientPrivate
131 GDBusConnection
*connection
;
135 GDBusObjectManagerClientFlags flags
;
137 GDBusProxy
*control_proxy
;
139 GHashTable
*map_object_path_to_object_proxy
;
141 guint signal_subscription_id
;
144 GDBusProxyTypeFunc get_proxy_type_func
;
145 gpointer get_proxy_type_user_data
;
146 GDestroyNotify get_proxy_type_destroy_notify
;
158 PROP_GET_PROXY_TYPE_FUNC
,
159 PROP_GET_PROXY_TYPE_USER_DATA
,
160 PROP_GET_PROXY_TYPE_DESTROY_NOTIFY
165 INTERFACE_PROXY_SIGNAL_SIGNAL
,
166 INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL
,
170 static guint signals
[LAST_SIGNAL
] = { 0 };
172 static void initable_iface_init (GInitableIface
*initable_iface
);
173 static void async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
);
174 static void dbus_object_manager_interface_init (GDBusObjectManagerIface
*iface
);
176 G_DEFINE_TYPE_WITH_CODE (GDBusObjectManagerClient
, g_dbus_object_manager_client
, G_TYPE_OBJECT
,
177 G_ADD_PRIVATE (GDBusObjectManagerClient
)
178 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
, initable_iface_init
)
179 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE
, async_initable_iface_init
)
180 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT_MANAGER
, dbus_object_manager_interface_init
));
182 static void maybe_unsubscribe_signals (GDBusObjectManagerClient
*manager
);
184 static void on_control_proxy_g_signal (GDBusProxy
*proxy
,
185 const gchar
*sender_name
,
186 const gchar
*signal_name
,
187 GVariant
*parameters
,
190 static void process_get_all_result (GDBusObjectManagerClient
*manager
,
192 const gchar
*name_owner
);
195 g_dbus_object_manager_client_finalize (GObject
*object
)
197 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (object
);
199 maybe_unsubscribe_signals (manager
);
201 g_hash_table_unref (manager
->priv
->map_object_path_to_object_proxy
);
203 if (manager
->priv
->control_proxy
!= NULL
)
205 g_signal_handlers_disconnect_by_func (manager
->priv
->control_proxy
,
206 on_control_proxy_g_signal
,
208 g_object_unref (manager
->priv
->control_proxy
);
210 g_object_unref (manager
->priv
->connection
);
211 g_free (manager
->priv
->object_path
);
212 g_free (manager
->priv
->name
);
213 g_free (manager
->priv
->name_owner
);
215 if (manager
->priv
->get_proxy_type_destroy_notify
!= NULL
)
216 manager
->priv
->get_proxy_type_destroy_notify (manager
->priv
->get_proxy_type_user_data
);
218 g_mutex_clear (&manager
->priv
->lock
);
220 if (G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class
)->finalize
!= NULL
)
221 G_OBJECT_CLASS (g_dbus_object_manager_client_parent_class
)->finalize (object
);
225 g_dbus_object_manager_client_get_property (GObject
*_object
,
230 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (_object
);
234 case PROP_CONNECTION
:
235 g_value_set_object (value
, g_dbus_object_manager_client_get_connection (manager
));
238 case PROP_OBJECT_PATH
:
239 g_value_set_string (value
, g_dbus_object_manager_get_object_path (G_DBUS_OBJECT_MANAGER (manager
)));
243 g_value_set_string (value
, g_dbus_object_manager_client_get_name (manager
));
247 g_value_set_flags (value
, g_dbus_object_manager_client_get_flags (manager
));
250 case PROP_NAME_OWNER
:
251 g_value_take_string (value
, g_dbus_object_manager_client_get_name_owner (manager
));
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (manager
, prop_id
, pspec
);
261 g_dbus_object_manager_client_set_property (GObject
*_object
,
266 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (_object
);
272 manager
->priv
->bus_type
= g_value_get_enum (value
);
275 case PROP_CONNECTION
:
276 if (g_value_get_object (value
) != NULL
)
278 g_assert (manager
->priv
->connection
== NULL
);
279 g_assert (G_IS_DBUS_CONNECTION (g_value_get_object (value
)));
280 manager
->priv
->connection
= g_value_dup_object (value
);
284 case PROP_OBJECT_PATH
:
285 g_assert (manager
->priv
->object_path
== NULL
);
286 g_assert (g_variant_is_object_path (g_value_get_string (value
)));
287 manager
->priv
->object_path
= g_value_dup_string (value
);
291 g_assert (manager
->priv
->name
== NULL
);
292 name
= g_value_get_string (value
);
293 g_assert (name
== NULL
|| g_dbus_is_name (name
));
294 manager
->priv
->name
= g_strdup (name
);
298 manager
->priv
->flags
= g_value_get_flags (value
);
301 case PROP_GET_PROXY_TYPE_FUNC
:
302 manager
->priv
->get_proxy_type_func
= g_value_get_pointer (value
);
305 case PROP_GET_PROXY_TYPE_USER_DATA
:
306 manager
->priv
->get_proxy_type_user_data
= g_value_get_pointer (value
);
309 case PROP_GET_PROXY_TYPE_DESTROY_NOTIFY
:
310 manager
->priv
->get_proxy_type_destroy_notify
= g_value_get_pointer (value
);
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (manager
, prop_id
, pspec
);
320 g_dbus_object_manager_client_class_init (GDBusObjectManagerClientClass
*klass
)
322 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
324 gobject_class
->finalize
= g_dbus_object_manager_client_finalize
;
325 gobject_class
->set_property
= g_dbus_object_manager_client_set_property
;
326 gobject_class
->get_property
= g_dbus_object_manager_client_get_property
;
329 * GDBusObjectManagerClient:connection:
331 * The #GDBusConnection to use.
335 g_object_class_install_property (gobject_class
,
337 g_param_spec_object ("connection",
339 "The connection to use",
340 G_TYPE_DBUS_CONNECTION
,
343 G_PARAM_CONSTRUCT_ONLY
|
344 G_PARAM_STATIC_STRINGS
));
347 * GDBusObjectManagerClient:bus-type:
349 * If this property is not %G_BUS_TYPE_NONE, then
350 * #GDBusObjectManagerClient:connection must be %NULL and will be set to the
351 * #GDBusConnection obtained by calling g_bus_get() with the value
356 g_object_class_install_property (gobject_class
,
358 g_param_spec_enum ("bus-type",
360 "The bus to connect to, if any",
364 G_PARAM_CONSTRUCT_ONLY
|
365 G_PARAM_STATIC_NAME
|
366 G_PARAM_STATIC_BLURB
|
367 G_PARAM_STATIC_NICK
));
370 * GDBusObjectManagerClient:flags:
372 * Flags from the #GDBusObjectManagerClientFlags enumeration.
376 g_object_class_install_property (gobject_class
,
378 g_param_spec_flags ("flags",
380 "Flags for the proxy manager",
381 G_TYPE_DBUS_OBJECT_MANAGER_CLIENT_FLAGS
,
382 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE
,
385 G_PARAM_CONSTRUCT_ONLY
|
386 G_PARAM_STATIC_NAME
|
387 G_PARAM_STATIC_BLURB
|
388 G_PARAM_STATIC_NICK
));
391 * GDBusObjectManagerClient:object-path:
393 * The object path the manager is for.
397 g_object_class_install_property (gobject_class
,
399 g_param_spec_string ("object-path",
401 "The object path of the control object",
405 G_PARAM_CONSTRUCT_ONLY
|
406 G_PARAM_STATIC_STRINGS
));
409 * GDBusObjectManagerClient:name:
411 * The well-known name or unique name that the manager is for.
415 g_object_class_install_property (gobject_class
,
417 g_param_spec_string ("name",
419 "Name that the manager is for",
423 G_PARAM_CONSTRUCT_ONLY
|
424 G_PARAM_STATIC_STRINGS
));
427 * GDBusObjectManagerClient:name-owner:
429 * The unique name that owns #GDBusObjectManagerClient:name or %NULL if
430 * no-one is currently owning the name. Connect to the
431 * #GObject::notify signal to track changes to this property.
435 g_object_class_install_property (gobject_class
,
437 g_param_spec_string ("name-owner",
439 "The owner of the name we are watching",
442 G_PARAM_STATIC_STRINGS
));
445 * GDBusObjectManagerClient:get-proxy-type-func:
447 * The #GDBusProxyTypeFunc to use when determining what #GType to
448 * use for interface proxies or %NULL.
452 g_object_class_install_property (gobject_class
,
453 PROP_GET_PROXY_TYPE_FUNC
,
454 g_param_spec_pointer ("get-proxy-type-func",
455 "GDBusProxyTypeFunc Function Pointer",
456 "The GDBusProxyTypeFunc pointer to use",
459 G_PARAM_CONSTRUCT_ONLY
|
460 G_PARAM_STATIC_STRINGS
));
463 * GDBusObjectManagerClient:get-proxy-type-user-data:
465 * The #gpointer user_data to pass to #GDBusObjectManagerClient:get-proxy-type-func.
469 g_object_class_install_property (gobject_class
,
470 PROP_GET_PROXY_TYPE_USER_DATA
,
471 g_param_spec_pointer ("get-proxy-type-user-data",
472 "GDBusProxyTypeFunc User Data",
473 "The GDBusProxyTypeFunc user_data",
476 G_PARAM_CONSTRUCT_ONLY
|
477 G_PARAM_STATIC_STRINGS
));
480 * GDBusObjectManagerClient:get-proxy-type-destroy-notify:
482 * A #GDestroyNotify for the #gpointer user_data in #GDBusObjectManagerClient:get-proxy-type-user-data.
486 g_object_class_install_property (gobject_class
,
487 PROP_GET_PROXY_TYPE_DESTROY_NOTIFY
,
488 g_param_spec_pointer ("get-proxy-type-destroy-notify",
489 "GDBusProxyTypeFunc user data free function",
490 "The GDBusProxyTypeFunc user data free function",
493 G_PARAM_CONSTRUCT_ONLY
|
494 G_PARAM_STATIC_STRINGS
));
497 * GDBusObjectManagerClient::interface-proxy-signal:
498 * @manager: The #GDBusObjectManagerClient emitting the signal.
499 * @object_proxy: The #GDBusObjectProxy on which an interface is emitting a D-Bus signal.
500 * @interface_proxy: The #GDBusProxy that is emitting a D-Bus signal.
501 * @sender_name: The sender of the signal or NULL if the connection is not a bus connection.
502 * @signal_name: The signal name.
503 * @parameters: A #GVariant tuple with parameters for the signal.
505 * Emitted when a D-Bus signal is received on @interface_proxy.
507 * This signal exists purely as a convenience to avoid having to
508 * connect signals to all interface proxies managed by @manager.
510 * This signal is emitted in the
511 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
512 * that @manager was constructed in.
516 signals
[INTERFACE_PROXY_SIGNAL_SIGNAL
] =
517 g_signal_new ("interface-proxy-signal",
518 G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
520 G_STRUCT_OFFSET (GDBusObjectManagerClientClass
, interface_proxy_signal
),
526 G_TYPE_DBUS_OBJECT_PROXY
,
533 * GDBusObjectManagerClient::interface-proxy-properties-changed:
534 * @manager: The #GDBusObjectManagerClient emitting the signal.
535 * @object_proxy: The #GDBusObjectProxy on which an interface has properties that are changing.
536 * @interface_proxy: The #GDBusProxy that has properties that are changing.
537 * @changed_properties: A #GVariant containing the properties that changed.
538 * @invalidated_properties: A %NULL terminated array of properties that was invalidated.
540 * Emitted when one or more D-Bus properties on proxy changes. The
541 * local cache has already been updated when this signal fires. Note
542 * that both @changed_properties and @invalidated_properties are
543 * guaranteed to never be %NULL (either may be empty though).
545 * This signal exists purely as a convenience to avoid having to
546 * connect signals to all interface proxies managed by @manager.
548 * This signal is emitted in the
549 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
550 * that @manager was constructed in.
554 signals
[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL
] =
555 g_signal_new ("interface-proxy-properties-changed",
556 G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
558 G_STRUCT_OFFSET (GDBusObjectManagerClientClass
, interface_proxy_properties_changed
),
564 G_TYPE_DBUS_OBJECT_PROXY
,
571 g_dbus_object_manager_client_init (GDBusObjectManagerClient
*manager
)
573 manager
->priv
= g_dbus_object_manager_client_get_instance_private (manager
);
574 g_mutex_init (&manager
->priv
->lock
);
575 manager
->priv
->map_object_path_to_object_proxy
= g_hash_table_new_full (g_str_hash
,
578 (GDestroyNotify
) g_object_unref
);
581 /* ---------------------------------------------------------------------------------------------------- */
584 * g_dbus_object_manager_client_new_sync:
585 * @connection: A #GDBusConnection.
586 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
587 * @name: (allow-none): The owner of the control object (unique or well-known name), or %NULL when not using a message bus connection.
588 * @object_path: The object path of the control object.
589 * @get_proxy_type_func: (allow-none): A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
590 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
591 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
592 * @cancellable: (allow-none): A #GCancellable or %NULL
593 * @error: Return location for error or %NULL.
595 * Creates a new #GDBusObjectManagerClient object.
597 * This is a synchronous failable constructor - the calling thread is
598 * blocked until a reply is received. See g_dbus_object_manager_client_new()
599 * for the asynchronous version.
601 * Returns: (transfer full) (type GDBusObjectManagerClient): A
602 * #GDBusObjectManagerClient object or %NULL if @error is set. Free
603 * with g_object_unref().
608 g_dbus_object_manager_client_new_sync (GDBusConnection
*connection
,
609 GDBusObjectManagerClientFlags flags
,
611 const gchar
*object_path
,
612 GDBusProxyTypeFunc get_proxy_type_func
,
613 gpointer get_proxy_type_user_data
,
614 GDestroyNotify get_proxy_type_destroy_notify
,
615 GCancellable
*cancellable
,
620 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
621 g_return_val_if_fail ((name
== NULL
&& g_dbus_connection_get_unique_name (connection
) == NULL
) ||
622 g_dbus_is_name (name
), NULL
);
623 g_return_val_if_fail (g_variant_is_object_path (object_path
), NULL
);
624 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
626 initable
= g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
629 "connection", connection
,
632 "object-path", object_path
,
633 "get-proxy-type-func", get_proxy_type_func
,
634 "get-proxy-type-user-data", get_proxy_type_user_data
,
635 "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify
,
637 if (initable
!= NULL
)
638 return G_DBUS_OBJECT_MANAGER (initable
);
644 * g_dbus_object_manager_client_new:
645 * @connection: A #GDBusConnection.
646 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
647 * @name: The owner of the control object (unique or well-known name).
648 * @object_path: The object path of the control object.
649 * @get_proxy_type_func: (allow-none): A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
650 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
651 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
652 * @cancellable: (allow-none): A #GCancellable or %NULL
653 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
654 * @user_data: The data to pass to @callback.
656 * Asynchronously creates a new #GDBusObjectManagerClient object.
658 * This is an asynchronous failable constructor. When the result is
659 * ready, @callback will be invoked in the
660 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
661 * of the thread you are calling this method from. You can
662 * then call g_dbus_object_manager_client_new_finish() to get the result. See
663 * g_dbus_object_manager_client_new_sync() for the synchronous version.
668 g_dbus_object_manager_client_new (GDBusConnection
*connection
,
669 GDBusObjectManagerClientFlags flags
,
671 const gchar
*object_path
,
672 GDBusProxyTypeFunc get_proxy_type_func
,
673 gpointer get_proxy_type_user_data
,
674 GDestroyNotify get_proxy_type_destroy_notify
,
675 GCancellable
*cancellable
,
676 GAsyncReadyCallback callback
,
679 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
680 g_return_if_fail ((name
== NULL
&& g_dbus_connection_get_unique_name (connection
) == NULL
) ||
681 g_dbus_is_name (name
));
682 g_return_if_fail (g_variant_is_object_path (object_path
));
684 g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
689 "connection", connection
,
692 "object-path", object_path
,
693 "get-proxy-type-func", get_proxy_type_func
,
694 "get-proxy-type-user-data", get_proxy_type_user_data
,
695 "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify
,
700 * g_dbus_object_manager_client_new_finish:
701 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new().
702 * @error: Return location for error or %NULL.
704 * Finishes an operation started with g_dbus_object_manager_client_new().
706 * Returns: (transfer full) (type GDBusObjectManagerClient): A
707 * #GDBusObjectManagerClient object or %NULL if @error is set. Free
708 * with g_object_unref().
713 g_dbus_object_manager_client_new_finish (GAsyncResult
*res
,
717 GObject
*source_object
;
719 source_object
= g_async_result_get_source_object (res
);
720 g_assert (source_object
!= NULL
);
722 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
725 g_object_unref (source_object
);
728 return G_DBUS_OBJECT_MANAGER (object
);
733 /* ---------------------------------------------------------------------------------------------------- */
736 * g_dbus_object_manager_client_new_for_bus_sync:
737 * @bus_type: A #GBusType.
738 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
739 * @name: The owner of the control object (unique or well-known name).
740 * @object_path: The object path of the control object.
741 * @get_proxy_type_func: (allow-none): A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
742 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
743 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
744 * @cancellable: (allow-none): A #GCancellable or %NULL
745 * @error: Return location for error or %NULL.
747 * Like g_dbus_object_manager_client_new_sync() but takes a #GBusType instead
748 * of a #GDBusConnection.
750 * This is a synchronous failable constructor - the calling thread is
751 * blocked until a reply is received. See g_dbus_object_manager_client_new_for_bus()
752 * for the asynchronous version.
754 * Returns: (transfer full) (type GDBusObjectManagerClient): A
755 * #GDBusObjectManagerClient object or %NULL if @error is set. Free
756 * with g_object_unref().
761 g_dbus_object_manager_client_new_for_bus_sync (GBusType bus_type
,
762 GDBusObjectManagerClientFlags flags
,
764 const gchar
*object_path
,
765 GDBusProxyTypeFunc get_proxy_type_func
,
766 gpointer get_proxy_type_user_data
,
767 GDestroyNotify get_proxy_type_destroy_notify
,
768 GCancellable
*cancellable
,
773 g_return_val_if_fail (bus_type
!= G_BUS_TYPE_NONE
, NULL
);
774 g_return_val_if_fail (g_dbus_is_name (name
), NULL
);
775 g_return_val_if_fail (g_variant_is_object_path (object_path
), NULL
);
776 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
778 initable
= g_initable_new (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
781 "bus-type", bus_type
,
784 "object-path", object_path
,
785 "get-proxy-type-func", get_proxy_type_func
,
786 "get-proxy-type-user-data", get_proxy_type_user_data
,
787 "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify
,
789 if (initable
!= NULL
)
790 return G_DBUS_OBJECT_MANAGER (initable
);
796 * g_dbus_object_manager_client_new_for_bus:
797 * @bus_type: A #GBusType.
798 * @flags: Zero or more flags from the #GDBusObjectManagerClientFlags enumeration.
799 * @name: The owner of the control object (unique or well-known name).
800 * @object_path: The object path of the control object.
801 * @get_proxy_type_func: (allow-none): A #GDBusProxyTypeFunc function or %NULL to always construct #GDBusProxy proxies.
802 * @get_proxy_type_user_data: User data to pass to @get_proxy_type_func.
803 * @get_proxy_type_destroy_notify: (allow-none): Free function for @get_proxy_type_user_data or %NULL.
804 * @cancellable: (allow-none): A #GCancellable or %NULL
805 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
806 * @user_data: The data to pass to @callback.
808 * Like g_dbus_object_manager_client_new() but takes a #GBusType instead of a
811 * This is an asynchronous failable constructor. When the result is
812 * ready, @callback will be invoked in the
813 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
814 * of the thread you are calling this method from. You can
815 * then call g_dbus_object_manager_client_new_for_bus_finish() to get the result. See
816 * g_dbus_object_manager_client_new_for_bus_sync() for the synchronous version.
821 g_dbus_object_manager_client_new_for_bus (GBusType bus_type
,
822 GDBusObjectManagerClientFlags flags
,
824 const gchar
*object_path
,
825 GDBusProxyTypeFunc get_proxy_type_func
,
826 gpointer get_proxy_type_user_data
,
827 GDestroyNotify get_proxy_type_destroy_notify
,
828 GCancellable
*cancellable
,
829 GAsyncReadyCallback callback
,
832 g_return_if_fail (bus_type
!= G_BUS_TYPE_NONE
);
833 g_return_if_fail (g_dbus_is_name (name
));
834 g_return_if_fail (g_variant_is_object_path (object_path
));
836 g_async_initable_new_async (G_TYPE_DBUS_OBJECT_MANAGER_CLIENT
,
841 "bus-type", bus_type
,
844 "object-path", object_path
,
845 "get-proxy-type-func", get_proxy_type_func
,
846 "get-proxy-type-user-data", get_proxy_type_user_data
,
847 "get-proxy-type-destroy-notify", get_proxy_type_destroy_notify
,
852 * g_dbus_object_manager_client_new_for_bus_finish:
853 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_object_manager_client_new_for_bus().
854 * @error: Return location for error or %NULL.
856 * Finishes an operation started with g_dbus_object_manager_client_new_for_bus().
858 * Returns: (transfer full) (type GDBusObjectManagerClient): A
859 * #GDBusObjectManagerClient object or %NULL if @error is set. Free
860 * with g_object_unref().
865 g_dbus_object_manager_client_new_for_bus_finish (GAsyncResult
*res
,
869 GObject
*source_object
;
871 source_object
= g_async_result_get_source_object (res
);
872 g_assert (source_object
!= NULL
);
874 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
877 g_object_unref (source_object
);
880 return G_DBUS_OBJECT_MANAGER (object
);
885 /* ---------------------------------------------------------------------------------------------------- */
888 * g_dbus_object_manager_client_get_connection:
889 * @manager: A #GDBusObjectManagerClient
891 * Gets the #GDBusConnection used by @manager.
893 * Returns: (transfer none): A #GDBusConnection object. Do not free,
894 * the object belongs to @manager.
899 g_dbus_object_manager_client_get_connection (GDBusObjectManagerClient
*manager
)
901 GDBusConnection
*ret
;
902 g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
), NULL
);
903 g_mutex_lock (&manager
->priv
->lock
);
904 ret
= manager
->priv
->connection
;
905 g_mutex_unlock (&manager
->priv
->lock
);
910 * g_dbus_object_manager_client_get_name:
911 * @manager: A #GDBusObjectManagerClient
913 * Gets the name that @manager is for, or %NULL if not a message bus
916 * Returns: A unique or well-known name. Do not free, the string
917 * belongs to @manager.
922 g_dbus_object_manager_client_get_name (GDBusObjectManagerClient
*manager
)
925 g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
), NULL
);
926 g_mutex_lock (&manager
->priv
->lock
);
927 ret
= manager
->priv
->name
;
928 g_mutex_unlock (&manager
->priv
->lock
);
933 * g_dbus_object_manager_client_get_flags:
934 * @manager: A #GDBusObjectManagerClient
936 * Gets the flags that @manager was constructed with.
938 * Returns: Zero of more flags from the #GDBusObjectManagerClientFlags
943 GDBusObjectManagerClientFlags
944 g_dbus_object_manager_client_get_flags (GDBusObjectManagerClient
*manager
)
946 GDBusObjectManagerClientFlags ret
;
947 g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
), G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE
);
948 g_mutex_lock (&manager
->priv
->lock
);
949 ret
= manager
->priv
->flags
;
950 g_mutex_unlock (&manager
->priv
->lock
);
955 * g_dbus_object_manager_client_get_name_owner:
956 * @manager: A #GDBusObjectManagerClient.
958 * The unique name that owns the name that @manager is for or %NULL if
959 * no-one currently owns that name. You can connect to the
960 * #GObject::notify signal to track changes to the
961 * #GDBusObjectManagerClient:name-owner property.
963 * Returns: The name owner or %NULL if no name owner exists. Free with
969 g_dbus_object_manager_client_get_name_owner (GDBusObjectManagerClient
*manager
)
972 g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
), NULL
);
973 g_mutex_lock (&manager
->priv
->lock
);
974 ret
= g_strdup (manager
->priv
->name_owner
);
975 g_mutex_unlock (&manager
->priv
->lock
);
979 /* ---------------------------------------------------------------------------------------------------- */
981 /* signal handler for all objects we manage - we dispatch signals
982 * from here to the objects
985 signal_cb (GDBusConnection
*connection
,
986 const gchar
*sender_name
,
987 const gchar
*object_path
,
988 const gchar
*interface_name
,
989 const gchar
*signal_name
,
990 GVariant
*parameters
,
993 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (user_data
);
994 GDBusObjectProxy
*object_proxy
;
995 GDBusInterface
*interface
;
997 g_mutex_lock (&manager
->priv
->lock
);
998 object_proxy
= g_hash_table_lookup (manager
->priv
->map_object_path_to_object_proxy
, object_path
);
999 if (object_proxy
== NULL
)
1001 g_mutex_unlock (&manager
->priv
->lock
);
1004 g_object_ref (object_proxy
);
1005 g_mutex_unlock (&manager
->priv
->lock
);
1007 //g_debug ("yay, signal_cb %s %s: %s\n", signal_name, object_path, g_variant_print (parameters, TRUE));
1009 g_object_ref (manager
);
1010 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0)
1012 if (g_strcmp0 (signal_name
, "PropertiesChanged") == 0)
1014 const gchar
*interface_name
;
1015 GVariant
*changed_properties
;
1016 const gchar
**invalidated_properties
;
1018 g_variant_get (parameters
,
1021 &changed_properties
,
1022 &invalidated_properties
);
1024 interface
= g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy
), interface_name
);
1025 if (interface
!= NULL
)
1027 GVariantIter property_iter
;
1028 const gchar
*property_name
;
1029 GVariant
*property_value
;
1032 /* update caches... */
1033 g_variant_iter_init (&property_iter
, changed_properties
);
1034 while (g_variant_iter_next (&property_iter
,
1039 g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface
),
1042 g_variant_unref (property_value
);
1045 for (n
= 0; invalidated_properties
[n
] != NULL
; n
++)
1047 g_dbus_proxy_set_cached_property (G_DBUS_PROXY (interface
),
1048 invalidated_properties
[n
],
1051 /* ... and then synthesize the signal */
1052 g_signal_emit_by_name (interface
,
1053 "g-properties-changed",
1055 invalidated_properties
);
1056 g_signal_emit (manager
,
1057 signals
[INTERFACE_PROXY_PROPERTIES_CHANGED_SIGNAL
],
1062 invalidated_properties
);
1063 g_object_unref (interface
);
1065 g_variant_unref (changed_properties
);
1066 g_free (invalidated_properties
);
1071 /* regular signal - just dispatch it */
1072 interface
= g_dbus_object_get_interface (G_DBUS_OBJECT (object_proxy
), interface_name
);
1073 if (interface
!= NULL
)
1075 g_signal_emit_by_name (interface
,
1080 g_signal_emit (manager
,
1081 signals
[INTERFACE_PROXY_SIGNAL_SIGNAL
],
1088 g_object_unref (interface
);
1091 g_object_unref (manager
);
1094 g_clear_object (&object_proxy
);
1098 subscribe_signals (GDBusObjectManagerClient
*manager
,
1099 const gchar
*name_owner
)
1101 GError
*error
= NULL
;
1104 g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
));
1105 g_return_if_fail (manager
->priv
->signal_subscription_id
== 0);
1106 g_return_if_fail (name_owner
== NULL
|| g_dbus_is_unique_name (name_owner
));
1108 if (name_owner
!= NULL
)
1110 /* Only add path_namespace if it's non-'/'. This removes a no-op key from
1111 * the match rule, and also works around a D-Bus bug where
1112 * path_namespace='/' matches nothing in D-Bus versions < 1.6.18.
1114 * See: https://bugs.freedesktop.org/show_bug.cgi?id=70799 */
1115 if (g_str_equal (manager
->priv
->object_path
, "/"))
1117 manager
->priv
->match_rule
= g_strdup_printf ("type='signal',sender='%s'",
1122 manager
->priv
->match_rule
= g_strdup_printf ("type='signal',sender='%s',path_namespace='%s'",
1123 name_owner
, manager
->priv
->object_path
);
1126 /* The bus daemon may not implement path_namespace so gracefully
1127 * handle this by using a fallback triggered if @error is set. */
1128 ret
= g_dbus_connection_call_sync (manager
->priv
->connection
,
1129 "org.freedesktop.DBus",
1130 "/org/freedesktop/DBus",
1131 "org.freedesktop.DBus",
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
, /* TODO: Cancellable */
1141 /* yay, bus daemon supports path_namespace */
1143 g_variant_unref (ret
);
1148 /* still need to ask GDBusConnection for the callbacks */
1149 manager
->priv
->signal_subscription_id
=
1150 g_dbus_connection_signal_subscribe (manager
->priv
->connection
,
1152 NULL
, /* interface */
1154 NULL
, /* path - TODO: really want wilcard support here */
1156 G_DBUS_SIGNAL_FLAGS_NONE
|
1157 G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE
,
1160 NULL
); /* user_data_free_func */
1165 /* TODO: we could report this to the user
1166 g_warning ("Message bus daemon does not support path_namespace: %s (%s %d)",
1168 g_quark_to_string (error->domain),
1172 g_error_free (error
);
1174 /* no need to call RemoveMatch when done since it didn't work */
1175 g_free (manager
->priv
->match_rule
);
1176 manager
->priv
->match_rule
= NULL
;
1178 /* Fallback is to subscribe to *all* signals from the name owner which
1179 * is rather wasteful. It's probably not a big practical problem because
1180 * users typically want all objects that the name owner supplies.
1182 manager
->priv
->signal_subscription_id
=
1183 g_dbus_connection_signal_subscribe (manager
->priv
->connection
,
1185 NULL
, /* interface */
1187 NULL
, /* path - TODO: really want wilcard support here */
1189 G_DBUS_SIGNAL_FLAGS_NONE
,
1192 NULL
); /* user_data_free_func */
1197 maybe_unsubscribe_signals (GDBusObjectManagerClient
*manager
)
1199 g_return_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
));
1201 if (manager
->priv
->signal_subscription_id
> 0)
1203 g_dbus_connection_signal_unsubscribe (manager
->priv
->connection
,
1204 manager
->priv
->signal_subscription_id
);
1205 manager
->priv
->signal_subscription_id
= 0;
1208 if (manager
->priv
->match_rule
!= NULL
)
1210 /* Since the AddMatch call succeeded this is guaranteed to not
1211 * fail - therefore, don't bother checking the return value
1213 g_dbus_connection_call (manager
->priv
->connection
,
1214 "org.freedesktop.DBus",
1215 "/org/freedesktop/DBus",
1216 "org.freedesktop.DBus",
1218 g_variant_new ("(s)",
1219 manager
->priv
->match_rule
),
1220 NULL
, /* reply_type */
1221 G_DBUS_CALL_FLAGS_NONE
,
1222 -1, /* default timeout */
1223 NULL
, /* GCancellable */
1224 NULL
, /* GAsyncReadyCallback */
1225 NULL
); /* user data */
1226 g_free (manager
->priv
->match_rule
);
1227 manager
->priv
->match_rule
= NULL
;
1232 /* ---------------------------------------------------------------------------------------------------- */
1235 on_notify_g_name_owner (GObject
*object
,
1239 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (user_data
);
1240 gchar
*old_name_owner
;
1241 gchar
*new_name_owner
;
1243 g_mutex_lock (&manager
->priv
->lock
);
1244 old_name_owner
= manager
->priv
->name_owner
;
1245 new_name_owner
= g_dbus_proxy_get_name_owner (manager
->priv
->control_proxy
);
1246 manager
->priv
->name_owner
= NULL
;
1248 g_object_ref (manager
);
1249 if (g_strcmp0 (old_name_owner
, new_name_owner
) != 0)
1254 /* remote manager changed; nuke all local proxies */
1255 proxies
= g_hash_table_get_values (manager
->priv
->map_object_path_to_object_proxy
);
1256 g_list_foreach (proxies
, (GFunc
) g_object_ref
, NULL
);
1257 g_hash_table_remove_all (manager
->priv
->map_object_path_to_object_proxy
);
1259 g_mutex_unlock (&manager
->priv
->lock
);
1261 /* do the :name-owner notify with a NULL name - this way the user knows
1262 * the ::object-proxy-removed following is because the name owner went
1265 g_object_notify (G_OBJECT (manager
), "name-owner");
1267 for (l
= proxies
; l
!= NULL
; l
= l
->next
)
1269 GDBusObjectProxy
*object_proxy
= G_DBUS_OBJECT_PROXY (l
->data
);
1270 g_signal_emit_by_name (manager
, "object-removed", object_proxy
);
1272 g_list_free_full (proxies
, g_object_unref
);
1274 /* nuke local filter */
1275 maybe_unsubscribe_signals (manager
);
1279 g_mutex_unlock (&manager
->priv
->lock
);
1282 if (new_name_owner
!= NULL
)
1287 //g_debug ("repopulating for %s", new_name_owner);
1289 /* TODO: do this async! */
1290 subscribe_signals (manager
,
1293 value
= g_dbus_proxy_call_sync (manager
->priv
->control_proxy
,
1294 "GetManagedObjects",
1295 NULL
, /* parameters */
1296 G_DBUS_CALL_FLAGS_NONE
,
1302 maybe_unsubscribe_signals (manager
);
1303 g_warning ("Error calling GetManagedObjects() when name owner %s for name %s came back: %s",
1305 manager
->priv
->name
,
1307 g_error_free (error
);
1311 process_get_all_result (manager
, value
, new_name_owner
);
1312 g_variant_unref (value
);
1315 /* do the :name-owner notify *AFTER* emitting ::object-proxy-added signals - this
1316 * way the user knows that the signals were emitted because the name owner came back
1318 g_mutex_lock (&manager
->priv
->lock
);
1319 manager
->priv
->name_owner
= new_name_owner
;
1320 g_mutex_unlock (&manager
->priv
->lock
);
1321 g_object_notify (G_OBJECT (manager
), "name-owner");
1324 g_free (old_name_owner
);
1325 g_object_unref (manager
);
1329 initable_init (GInitable
*initable
,
1330 GCancellable
*cancellable
,
1333 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (initable
);
1336 GDBusProxyFlags proxy_flags
;
1340 if (manager
->priv
->bus_type
!= G_BUS_TYPE_NONE
)
1342 g_assert (manager
->priv
->connection
== NULL
);
1343 manager
->priv
->connection
= g_bus_get_sync (manager
->priv
->bus_type
, cancellable
, error
);
1344 if (manager
->priv
->connection
== NULL
)
1348 proxy_flags
= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
;
1349 if (manager
->priv
->flags
& G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START
)
1350 proxy_flags
|= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
;
1352 manager
->priv
->control_proxy
= g_dbus_proxy_new_sync (manager
->priv
->connection
,
1354 NULL
, /* GDBusInterfaceInfo* */
1355 manager
->priv
->name
,
1356 manager
->priv
->object_path
,
1357 "org.freedesktop.DBus.ObjectManager",
1360 if (manager
->priv
->control_proxy
== NULL
)
1363 g_signal_connect (G_OBJECT (manager
->priv
->control_proxy
),
1364 "notify::g-name-owner",
1365 G_CALLBACK (on_notify_g_name_owner
),
1368 g_signal_connect (manager
->priv
->control_proxy
,
1370 G_CALLBACK (on_control_proxy_g_signal
),
1373 manager
->priv
->name_owner
= g_dbus_proxy_get_name_owner (manager
->priv
->control_proxy
);
1374 if (manager
->priv
->name_owner
== NULL
&& manager
->priv
->name
!= NULL
)
1376 /* it's perfectly fine if there's no name owner.. we're just going to
1377 * wait until one is ready
1382 /* yay, we can get the objects */
1383 subscribe_signals (manager
,
1384 manager
->priv
->name_owner
);
1385 value
= g_dbus_proxy_call_sync (manager
->priv
->control_proxy
,
1386 "GetManagedObjects",
1387 NULL
, /* parameters */
1388 G_DBUS_CALL_FLAGS_NONE
,
1394 maybe_unsubscribe_signals (manager
);
1395 g_warn_if_fail (g_signal_handlers_disconnect_by_func (manager
->priv
->control_proxy
,
1396 on_control_proxy_g_signal
,
1398 g_object_unref (manager
->priv
->control_proxy
);
1399 manager
->priv
->control_proxy
= NULL
;
1403 process_get_all_result (manager
, value
, manager
->priv
->name_owner
);
1404 g_variant_unref (value
);
1414 initable_iface_init (GInitableIface
*initable_iface
)
1416 initable_iface
->init
= initable_init
;
1420 async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
)
1422 /* for now, just use default: run GInitable code in thread */
1425 /* ---------------------------------------------------------------------------------------------------- */
1428 add_interfaces (GDBusObjectManagerClient
*manager
,
1429 const gchar
*object_path
,
1430 GVariant
*ifaces_and_properties
,
1431 const gchar
*name_owner
)
1433 GDBusObjectProxy
*op
;
1436 const gchar
*interface_name
;
1437 GVariant
*properties
;
1438 GList
*interface_added_signals
, *l
;
1439 GDBusProxy
*interface_proxy
;
1441 g_return_if_fail (name_owner
== NULL
|| g_dbus_is_unique_name (name_owner
));
1443 g_mutex_lock (&manager
->priv
->lock
);
1445 interface_added_signals
= NULL
;
1448 op
= g_hash_table_lookup (manager
->priv
->map_object_path_to_object_proxy
, object_path
);
1451 GType object_proxy_type
;
1452 if (manager
->priv
->get_proxy_type_func
!= NULL
)
1454 object_proxy_type
= manager
->priv
->get_proxy_type_func (manager
,
1457 manager
->priv
->get_proxy_type_user_data
);
1458 g_warn_if_fail (g_type_is_a (object_proxy_type
, G_TYPE_DBUS_OBJECT_PROXY
));
1462 object_proxy_type
= G_TYPE_DBUS_OBJECT_PROXY
;
1464 op
= g_object_new (object_proxy_type
,
1465 "g-connection", manager
->priv
->connection
,
1466 "g-object-path", object_path
,
1472 g_variant_iter_init (&iter
, ifaces_and_properties
);
1473 while (g_variant_iter_next (&iter
,
1479 GType interface_proxy_type
;
1481 if (manager
->priv
->get_proxy_type_func
!= NULL
)
1483 interface_proxy_type
= manager
->priv
->get_proxy_type_func (manager
,
1486 manager
->priv
->get_proxy_type_user_data
);
1487 g_warn_if_fail (g_type_is_a (interface_proxy_type
, G_TYPE_DBUS_PROXY
));
1491 interface_proxy_type
= G_TYPE_DBUS_PROXY
;
1494 /* this is fine - there is no blocking IO because we pass DO_NOT_LOAD_PROPERTIES and
1495 * DO_NOT_CONNECT_SIGNALS and use a unique name
1498 interface_proxy
= g_initable_new (interface_proxy_type
,
1499 NULL
, /* GCancellable */
1501 "g-connection", manager
->priv
->connection
,
1502 "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
|
1503 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS
,
1504 "g-name", name_owner
,
1505 "g-object-path", object_path
,
1506 "g-interface-name", interface_name
,
1508 if (interface_proxy
== NULL
)
1510 g_warning ("%s: Error constructing proxy for path %s and interface %s: %s",
1515 g_error_free (error
);
1519 GVariantIter property_iter
;
1520 const gchar
*property_name
;
1521 GVariant
*property_value
;
1523 /* associate the interface proxy with the object */
1524 g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_proxy
),
1525 G_DBUS_OBJECT (op
));
1527 g_variant_iter_init (&property_iter
, properties
);
1528 while (g_variant_iter_next (&property_iter
,
1533 g_dbus_proxy_set_cached_property (interface_proxy
,
1536 g_variant_unref (property_value
);
1539 _g_dbus_object_proxy_add_interface (op
, interface_proxy
);
1541 interface_added_signals
= g_list_append (interface_added_signals
, g_object_ref (interface_proxy
));
1542 g_object_unref (interface_proxy
);
1544 g_variant_unref (properties
);
1547 g_mutex_unlock (&manager
->priv
->lock
);
1549 /* now that we don't hold the lock any more, emit signals */
1550 g_object_ref (manager
);
1551 for (l
= interface_added_signals
; l
!= NULL
; l
= l
->next
)
1553 interface_proxy
= G_DBUS_PROXY (l
->data
);
1554 g_signal_emit_by_name (manager
, "interface-added", op
, interface_proxy
);
1555 g_object_unref (interface_proxy
);
1557 g_list_free (interface_added_signals
);
1561 g_hash_table_insert (manager
->priv
->map_object_path_to_object_proxy
,
1562 g_strdup (object_path
),
1564 g_signal_emit_by_name (manager
, "object-added", op
);
1566 g_object_unref (manager
);
1567 g_object_unref (op
);
1571 remove_interfaces (GDBusObjectManagerClient
*manager
,
1572 const gchar
*object_path
,
1573 const gchar
*const *interface_names
)
1575 GDBusObjectProxy
*op
;
1578 guint num_interfaces
;
1579 guint num_interfaces_to_remove
;
1581 g_mutex_lock (&manager
->priv
->lock
);
1583 op
= g_hash_table_lookup (manager
->priv
->map_object_path_to_object_proxy
, object_path
);
1586 g_warning ("%s: Processing InterfaceRemoved signal for path %s but no object proxy exists",
1589 g_mutex_unlock (&manager
->priv
->lock
);
1593 interfaces
= g_dbus_object_get_interfaces (G_DBUS_OBJECT (op
));
1594 num_interfaces
= g_list_length (interfaces
);
1595 g_list_free_full (interfaces
, g_object_unref
);
1597 num_interfaces_to_remove
= g_strv_length ((gchar
**) interface_names
);
1599 /* see if we are going to completety remove the object */
1600 g_object_ref (manager
);
1601 if (num_interfaces_to_remove
== num_interfaces
)
1604 g_warn_if_fail (g_hash_table_remove (manager
->priv
->map_object_path_to_object_proxy
, object_path
));
1605 g_mutex_unlock (&manager
->priv
->lock
);
1606 g_signal_emit_by_name (manager
, "object-removed", op
);
1607 g_object_unref (op
);
1612 g_mutex_unlock (&manager
->priv
->lock
);
1613 for (n
= 0; interface_names
!= NULL
&& interface_names
[n
] != NULL
; n
++)
1615 GDBusInterface
*interface
;
1616 interface
= g_dbus_object_get_interface (G_DBUS_OBJECT (op
), interface_names
[n
]);
1617 _g_dbus_object_proxy_remove_interface (op
, interface_names
[n
]);
1618 if (interface
!= NULL
)
1620 g_signal_emit_by_name (manager
, "interface-removed", op
, interface
);
1621 g_object_unref (interface
);
1624 g_object_unref (op
);
1626 g_object_unref (manager
);
1632 process_get_all_result (GDBusObjectManagerClient
*manager
,
1634 const gchar
*name_owner
)
1637 const gchar
*object_path
;
1638 GVariant
*ifaces_and_properties
;
1641 g_return_if_fail (name_owner
== NULL
|| g_dbus_is_unique_name (name_owner
));
1643 arg0
= g_variant_get_child_value (value
, 0);
1644 g_variant_iter_init (&iter
, arg0
);
1645 while (g_variant_iter_next (&iter
,
1648 &ifaces_and_properties
))
1650 add_interfaces (manager
, object_path
, ifaces_and_properties
, name_owner
);
1651 g_variant_unref (ifaces_and_properties
);
1653 g_variant_unref (arg0
);
1657 on_control_proxy_g_signal (GDBusProxy
*proxy
,
1658 const gchar
*sender_name
,
1659 const gchar
*signal_name
,
1660 GVariant
*parameters
,
1663 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (user_data
);
1664 const gchar
*object_path
;
1666 //g_debug ("yay, g_signal %s: %s\n", signal_name, g_variant_print (parameters, TRUE));
1668 if (g_strcmp0 (signal_name
, "InterfacesAdded") == 0)
1670 GVariant
*ifaces_and_properties
;
1671 g_variant_get (parameters
,
1674 &ifaces_and_properties
);
1675 add_interfaces (manager
, object_path
, ifaces_and_properties
, manager
->priv
->name_owner
);
1676 g_variant_unref (ifaces_and_properties
);
1678 else if (g_strcmp0 (signal_name
, "InterfacesRemoved") == 0)
1680 const gchar
**ifaces
;
1681 g_variant_get (parameters
,
1685 remove_interfaces (manager
, object_path
, ifaces
);
1690 /* ---------------------------------------------------------------------------------------------------- */
1692 static const gchar
*
1693 g_dbus_object_manager_client_get_object_path (GDBusObjectManager
*_manager
)
1695 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (_manager
);
1696 return manager
->priv
->object_path
;
1699 static GDBusObject
*
1700 g_dbus_object_manager_client_get_object (GDBusObjectManager
*_manager
,
1701 const gchar
*object_path
)
1703 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (_manager
);
1706 g_mutex_lock (&manager
->priv
->lock
);
1707 ret
= g_hash_table_lookup (manager
->priv
->map_object_path_to_object_proxy
, object_path
);
1710 g_mutex_unlock (&manager
->priv
->lock
);
1714 static GDBusInterface
*
1715 g_dbus_object_manager_client_get_interface (GDBusObjectManager
*_manager
,
1716 const gchar
*object_path
,
1717 const gchar
*interface_name
)
1719 GDBusInterface
*ret
;
1720 GDBusObject
*object
;
1724 object
= g_dbus_object_manager_get_object (_manager
, object_path
);
1728 ret
= g_dbus_object_get_interface (object
, interface_name
);
1729 g_object_unref (object
);
1736 g_dbus_object_manager_client_get_objects (GDBusObjectManager
*_manager
)
1738 GDBusObjectManagerClient
*manager
= G_DBUS_OBJECT_MANAGER_CLIENT (_manager
);
1741 g_return_val_if_fail (G_IS_DBUS_OBJECT_MANAGER_CLIENT (manager
), NULL
);
1743 g_mutex_lock (&manager
->priv
->lock
);
1744 ret
= g_hash_table_get_values (manager
->priv
->map_object_path_to_object_proxy
);
1745 g_list_foreach (ret
, (GFunc
) g_object_ref
, NULL
);
1746 g_mutex_unlock (&manager
->priv
->lock
);
1753 dbus_object_manager_interface_init (GDBusObjectManagerIface
*iface
)
1755 iface
->get_object_path
= g_dbus_object_manager_client_get_object_path
;
1756 iface
->get_objects
= g_dbus_object_manager_client_get_objects
;
1757 iface
->get_object
= g_dbus_object_manager_client_get_object
;
1758 iface
->get_interface
= g_dbus_object_manager_client_get_interface
;
1761 /* ---------------------------------------------------------------------------------------------------- */