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 "gdbusobject.h"
26 #include "gdbusobjectproxy.h"
27 #include "gdbusconnection.h"
28 #include "gdbusprivate.h"
29 #include "gdbusutils.h"
30 #include "gdbusproxy.h"
35 * SECTION:gdbusobjectproxy
36 * @short_description: Client-side D-Bus object
39 * A #GDBusObjectProxy is an object used to represent a remote object
40 * with one or more D-Bus interfaces. Normally, you don't instantiate
41 * a #GDBusObjectProxy yourself - typically #GDBusObjectManagerClient
42 * is used to obtain it.
47 struct _GDBusObjectProxyPrivate
50 GHashTable
*map_name_to_iface
;
52 GDBusConnection
*connection
;
62 static void dbus_object_interface_init (GDBusObjectIface
*iface
);
64 G_DEFINE_TYPE_WITH_CODE (GDBusObjectProxy
, g_dbus_object_proxy
, G_TYPE_OBJECT
,
65 G_ADD_PRIVATE (GDBusObjectProxy
)
66 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT
, dbus_object_interface_init
))
69 g_dbus_object_proxy_finalize (GObject
*object
)
71 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
73 g_hash_table_unref (proxy
->priv
->map_name_to_iface
);
75 g_clear_object (&proxy
->priv
->connection
);
77 g_free (proxy
->priv
->object_path
);
79 g_mutex_clear (&proxy
->priv
->lock
);
81 if (G_OBJECT_CLASS (g_dbus_object_proxy_parent_class
)->finalize
!= NULL
)
82 G_OBJECT_CLASS (g_dbus_object_proxy_parent_class
)->finalize (object
);
86 g_dbus_object_proxy_get_property (GObject
*object
,
91 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
95 case PROP_G_OBJECT_PATH
:
96 g_mutex_lock (&proxy
->priv
->lock
);
97 g_value_set_string (value
, proxy
->priv
->object_path
);
98 g_mutex_unlock (&proxy
->priv
->lock
);
101 case PROP_G_CONNECTION
:
102 g_value_set_object (value
, g_dbus_object_proxy_get_connection (proxy
));
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy
, prop_id
, pspec
);
112 g_dbus_object_proxy_set_property (GObject
*object
,
117 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
121 case PROP_G_OBJECT_PATH
:
122 g_mutex_lock (&proxy
->priv
->lock
);
123 proxy
->priv
->object_path
= g_value_dup_string (value
);
124 g_mutex_unlock (&proxy
->priv
->lock
);
127 case PROP_G_CONNECTION
:
128 g_mutex_lock (&proxy
->priv
->lock
);
129 proxy
->priv
->connection
= g_value_dup_object (value
);
130 g_mutex_unlock (&proxy
->priv
->lock
);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy
, prop_id
, pspec
);
140 g_dbus_object_proxy_class_init (GDBusObjectProxyClass
*klass
)
142 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
144 gobject_class
->finalize
= g_dbus_object_proxy_finalize
;
145 gobject_class
->set_property
= g_dbus_object_proxy_set_property
;
146 gobject_class
->get_property
= g_dbus_object_proxy_get_property
;
149 * GDBusObjectProxy:g-object-path:
151 * The object path of the proxy.
155 g_object_class_install_property (gobject_class
,
157 g_param_spec_string ("g-object-path",
159 "The object path of the proxy",
162 G_PARAM_CONSTRUCT_ONLY
|
163 G_PARAM_STATIC_STRINGS
));
166 * GDBusObjectProxy:g-connection:
168 * The connection of the proxy.
172 g_object_class_install_property (gobject_class
,
174 g_param_spec_object ("g-connection",
176 "The connection of the proxy",
177 G_TYPE_DBUS_CONNECTION
,
179 G_PARAM_CONSTRUCT_ONLY
|
180 G_PARAM_STATIC_STRINGS
));
184 g_dbus_object_proxy_init (GDBusObjectProxy
*proxy
)
186 proxy
->priv
= g_dbus_object_proxy_get_instance_private (proxy
);
187 g_mutex_init (&proxy
->priv
->lock
);
188 proxy
->priv
->map_name_to_iface
= g_hash_table_new_full (g_str_hash
,
191 (GDestroyNotify
) g_object_unref
);
195 g_dbus_object_proxy_get_object_path (GDBusObject
*object
)
197 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
199 g_mutex_lock (&proxy
->priv
->lock
);
200 ret
= proxy
->priv
->object_path
;
201 g_mutex_unlock (&proxy
->priv
->lock
);
206 * g_dbus_object_proxy_get_connection:
207 * @proxy: a #GDBusObjectProxy
209 * Gets the connection that @proxy is for.
211 * Returns: (transfer none): A #GDBusConnection. Do not free, the
212 * object is owned by @proxy.
217 g_dbus_object_proxy_get_connection (GDBusObjectProxy
*proxy
)
219 GDBusConnection
*ret
;
220 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy
), NULL
);
221 g_mutex_lock (&proxy
->priv
->lock
);
222 ret
= proxy
->priv
->connection
;
223 g_mutex_unlock (&proxy
->priv
->lock
);
227 static GDBusInterface
*
228 g_dbus_object_proxy_get_interface (GDBusObject
*object
,
229 const gchar
*interface_name
)
231 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
234 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy
), NULL
);
235 g_return_val_if_fail (g_dbus_is_interface_name (interface_name
), NULL
);
237 g_mutex_lock (&proxy
->priv
->lock
);
238 ret
= g_hash_table_lookup (proxy
->priv
->map_name_to_iface
, interface_name
);
241 g_mutex_unlock (&proxy
->priv
->lock
);
243 return (GDBusInterface
*) ret
; /* TODO: proper cast */
247 g_dbus_object_proxy_get_interfaces (GDBusObject
*object
)
249 GDBusObjectProxy
*proxy
= G_DBUS_OBJECT_PROXY (object
);
252 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy
), NULL
);
256 g_mutex_lock (&proxy
->priv
->lock
);
257 ret
= g_hash_table_get_values (proxy
->priv
->map_name_to_iface
);
258 g_list_foreach (ret
, (GFunc
) g_object_ref
, NULL
);
259 g_mutex_unlock (&proxy
->priv
->lock
);
264 /* ---------------------------------------------------------------------------------------------------- */
267 * g_dbus_object_proxy_new:
268 * @connection: a #GDBusConnection
269 * @object_path: the object path
271 * Creates a new #GDBusObjectProxy for the given connection and
274 * Returns: a new #GDBusObjectProxy
279 g_dbus_object_proxy_new (GDBusConnection
*connection
,
280 const gchar
*object_path
)
282 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
283 g_return_val_if_fail (g_variant_is_object_path (object_path
), NULL
);
284 return G_DBUS_OBJECT_PROXY (g_object_new (G_TYPE_DBUS_OBJECT_PROXY
,
285 "g-object-path", object_path
,
286 "g-connection", connection
,
291 _g_dbus_object_proxy_add_interface (GDBusObjectProxy
*proxy
,
292 GDBusProxy
*interface_proxy
)
294 const gchar
*interface_name
;
295 GDBusProxy
*interface_proxy_to_remove
;
297 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy
));
298 g_return_if_fail (G_IS_DBUS_PROXY (interface_proxy
));
300 g_mutex_lock (&proxy
->priv
->lock
);
302 interface_name
= g_dbus_proxy_get_interface_name (interface_proxy
);
303 interface_proxy_to_remove
= g_hash_table_lookup (proxy
->priv
->map_name_to_iface
, interface_name
);
304 if (interface_proxy_to_remove
!= NULL
)
306 g_object_ref (interface_proxy_to_remove
);
307 g_warn_if_fail (g_hash_table_remove (proxy
->priv
->map_name_to_iface
, interface_name
));
309 g_hash_table_insert (proxy
->priv
->map_name_to_iface
,
310 g_strdup (interface_name
),
311 g_object_ref (interface_proxy
));
312 g_object_ref (interface_proxy
);
314 g_mutex_unlock (&proxy
->priv
->lock
);
316 if (interface_proxy_to_remove
!= NULL
)
318 g_signal_emit_by_name (proxy
, "interface-removed", interface_proxy_to_remove
);
319 g_object_unref (interface_proxy_to_remove
);
322 g_signal_emit_by_name (proxy
, "interface-added", interface_proxy
);
323 g_object_unref (interface_proxy
);
327 _g_dbus_object_proxy_remove_interface (GDBusObjectProxy
*proxy
,
328 const gchar
*interface_name
)
330 GDBusProxy
*interface_proxy
;
332 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy
));
333 g_return_if_fail (g_dbus_is_interface_name (interface_name
));
335 g_mutex_lock (&proxy
->priv
->lock
);
337 interface_proxy
= g_hash_table_lookup (proxy
->priv
->map_name_to_iface
, interface_name
);
338 if (interface_proxy
!= NULL
)
340 g_object_ref (interface_proxy
);
341 g_warn_if_fail (g_hash_table_remove (proxy
->priv
->map_name_to_iface
, interface_name
));
342 g_mutex_unlock (&proxy
->priv
->lock
);
343 g_signal_emit_by_name (proxy
, "interface-removed", interface_proxy
);
344 g_object_unref (interface_proxy
);
348 g_mutex_unlock (&proxy
->priv
->lock
);
353 dbus_object_interface_init (GDBusObjectIface
*iface
)
355 iface
->get_object_path
= g_dbus_object_proxy_get_object_path
;
356 iface
->get_interfaces
= g_dbus_object_proxy_get_interfaces
;
357 iface
->get_interface
= g_dbus_object_proxy_get_interface
;