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_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init));
68 g_dbus_object_proxy_finalize (GObject *object)
70 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
72 g_hash_table_unref (proxy->priv->map_name_to_iface);
74 g_clear_object (&proxy->priv->connection);
76 g_mutex_clear (&proxy->priv->lock);
78 if (G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize != NULL)
79 G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize (object);
83 g_dbus_object_proxy_get_property (GObject *object,
88 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
92 case PROP_G_OBJECT_PATH:
93 g_mutex_lock (&proxy->priv->lock);
94 g_value_set_string (value, proxy->priv->object_path);
95 g_mutex_unlock (&proxy->priv->lock);
98 case PROP_G_CONNECTION:
99 g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
109 g_dbus_object_proxy_set_property (GObject *object,
114 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
118 case PROP_G_OBJECT_PATH:
119 g_mutex_lock (&proxy->priv->lock);
120 proxy->priv->object_path = g_value_dup_string (value);
121 g_mutex_unlock (&proxy->priv->lock);
124 case PROP_G_CONNECTION:
125 g_mutex_lock (&proxy->priv->lock);
126 proxy->priv->connection = g_value_dup_object (value);
127 g_mutex_unlock (&proxy->priv->lock);
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
137 g_dbus_object_proxy_class_init (GDBusObjectProxyClass *klass)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 gobject_class->finalize = g_dbus_object_proxy_finalize;
142 gobject_class->set_property = g_dbus_object_proxy_set_property;
143 gobject_class->get_property = g_dbus_object_proxy_get_property;
146 * GDBusObjectProxy:g-object-path:
148 * The object path of the proxy.
152 g_object_class_install_property (gobject_class,
154 g_param_spec_string ("g-object-path",
156 "The object path of the proxy",
159 G_PARAM_CONSTRUCT_ONLY |
160 G_PARAM_STATIC_STRINGS));
163 * GDBusObjectProxy:g-connection:
165 * The connection of the proxy.
169 g_object_class_install_property (gobject_class,
171 g_param_spec_object ("g-connection",
173 "The connection of the proxy",
174 G_TYPE_DBUS_CONNECTION,
176 G_PARAM_CONSTRUCT_ONLY |
177 G_PARAM_STATIC_STRINGS));
179 g_type_class_add_private (klass, sizeof (GDBusObjectProxyPrivate));
183 g_dbus_object_proxy_init (GDBusObjectProxy *proxy)
185 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy,
186 G_TYPE_DBUS_OBJECT_PROXY,
187 GDBusObjectProxyPrivate);
188 g_mutex_init (&proxy->priv->lock);
189 proxy->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
192 (GDestroyNotify) g_object_unref);
196 g_dbus_object_proxy_get_object_path (GDBusObject *object)
198 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
200 g_mutex_lock (&proxy->priv->lock);
201 ret = proxy->priv->object_path;
202 g_mutex_unlock (&proxy->priv->lock);
207 * g_dbus_object_proxy_get_connection:
208 * @proxy: a #GDBusObjectProxy
210 * Gets the connection that @proxy is for.
212 * Returns: (transfer none): A #GDBusConnection. Do not free, the
213 * object is owned by @proxy.
218 g_dbus_object_proxy_get_connection (GDBusObjectProxy *proxy)
220 GDBusConnection *ret;
221 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
222 g_mutex_lock (&proxy->priv->lock);
223 ret = proxy->priv->connection;
224 g_mutex_unlock (&proxy->priv->lock);
228 static GDBusInterface *
229 g_dbus_object_proxy_get_interface (GDBusObject *object,
230 const gchar *interface_name)
232 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
235 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
236 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
238 g_mutex_lock (&proxy->priv->lock);
239 ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
242 g_mutex_unlock (&proxy->priv->lock);
244 return (GDBusInterface *) ret; /* TODO: proper cast */
248 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
250 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
253 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
257 g_mutex_lock (&proxy->priv->lock);
258 ret = g_hash_table_get_values (proxy->priv->map_name_to_iface);
259 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
260 g_mutex_unlock (&proxy->priv->lock);
265 /* ---------------------------------------------------------------------------------------------------- */
268 * g_dbus_object_proxy_new:
269 * @connection: a #GDBusConnection
270 * @object_path: the object path
272 * Creates a new #GDBusObjectProxy for the given connection and
275 * Returns: a new #GDBusObjectProxy
280 g_dbus_object_proxy_new (GDBusConnection *connection,
281 const gchar *object_path)
283 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
284 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
285 return G_DBUS_OBJECT_PROXY (g_object_new (G_TYPE_DBUS_OBJECT_PROXY,
286 "g-object-path", object_path,
287 "g-connection", connection,
292 _g_dbus_object_proxy_add_interface (GDBusObjectProxy *proxy,
293 GDBusProxy *interface_proxy)
295 const gchar *interface_name;
296 GDBusProxy *interface_proxy_to_remove;
298 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
299 g_return_if_fail (G_IS_DBUS_PROXY (interface_proxy));
301 g_mutex_lock (&proxy->priv->lock);
303 interface_name = g_dbus_proxy_get_interface_name (interface_proxy);
304 interface_proxy_to_remove = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
305 if (interface_proxy_to_remove != NULL)
307 g_object_ref (interface_proxy_to_remove);
308 g_warn_if_fail (g_hash_table_remove (proxy->priv->map_name_to_iface, interface_name));
310 g_hash_table_insert (proxy->priv->map_name_to_iface,
311 g_strdup (interface_name),
312 g_object_ref (interface_proxy));
313 g_object_ref (interface_proxy);
315 g_mutex_unlock (&proxy->priv->lock);
317 if (interface_proxy_to_remove != NULL)
319 g_signal_emit_by_name (proxy, "interface-removed", interface_proxy_to_remove);
320 g_object_unref (interface_proxy_to_remove);
323 g_signal_emit_by_name (proxy, "interface-added", interface_proxy);
324 g_object_unref (interface_proxy);
328 _g_dbus_object_proxy_remove_interface (GDBusObjectProxy *proxy,
329 const gchar *interface_name)
331 GDBusProxy *interface_proxy;
333 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
334 g_return_if_fail (g_dbus_is_interface_name (interface_name));
336 g_mutex_lock (&proxy->priv->lock);
338 interface_proxy = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
339 if (interface_proxy != NULL)
341 g_object_ref (interface_proxy);
342 g_warn_if_fail (g_hash_table_remove (proxy->priv->map_name_to_iface, interface_name));
343 g_mutex_unlock (&proxy->priv->lock);
344 g_signal_emit_by_name (proxy, "interface-removed", interface_proxy);
345 g_object_unref (interface_proxy);
349 g_mutex_unlock (&proxy->priv->lock);
354 dbus_object_interface_init (GDBusObjectIface *iface)
356 iface->get_object_path = g_dbus_object_proxy_get_object_path;
357 iface->get_interfaces = g_dbus_object_proxy_get_interfaces;
358 iface->get_interface = g_dbus_object_proxy_get_interface;