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_free (proxy->priv->object_path);
78 g_mutex_clear (&proxy->priv->lock);
80 if (G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize != NULL)
81 G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize (object);
85 g_dbus_object_proxy_get_property (GObject *object,
90 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
94 case PROP_G_OBJECT_PATH:
95 g_mutex_lock (&proxy->priv->lock);
96 g_value_set_string (value, proxy->priv->object_path);
97 g_mutex_unlock (&proxy->priv->lock);
100 case PROP_G_CONNECTION:
101 g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
111 g_dbus_object_proxy_set_property (GObject *object,
116 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
120 case PROP_G_OBJECT_PATH:
121 g_mutex_lock (&proxy->priv->lock);
122 proxy->priv->object_path = g_value_dup_string (value);
123 g_mutex_unlock (&proxy->priv->lock);
126 case PROP_G_CONNECTION:
127 g_mutex_lock (&proxy->priv->lock);
128 proxy->priv->connection = g_value_dup_object (value);
129 g_mutex_unlock (&proxy->priv->lock);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
139 g_dbus_object_proxy_class_init (GDBusObjectProxyClass *klass)
141 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143 gobject_class->finalize = g_dbus_object_proxy_finalize;
144 gobject_class->set_property = g_dbus_object_proxy_set_property;
145 gobject_class->get_property = g_dbus_object_proxy_get_property;
148 * GDBusObjectProxy:g-object-path:
150 * The object path of the proxy.
154 g_object_class_install_property (gobject_class,
156 g_param_spec_string ("g-object-path",
158 "The object path of the proxy",
161 G_PARAM_CONSTRUCT_ONLY |
162 G_PARAM_STATIC_STRINGS));
165 * GDBusObjectProxy:g-connection:
167 * The connection of the proxy.
171 g_object_class_install_property (gobject_class,
173 g_param_spec_object ("g-connection",
175 "The connection of the proxy",
176 G_TYPE_DBUS_CONNECTION,
178 G_PARAM_CONSTRUCT_ONLY |
179 G_PARAM_STATIC_STRINGS));
181 g_type_class_add_private (klass, sizeof (GDBusObjectProxyPrivate));
185 g_dbus_object_proxy_init (GDBusObjectProxy *proxy)
187 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy,
188 G_TYPE_DBUS_OBJECT_PROXY,
189 GDBusObjectProxyPrivate);
190 g_mutex_init (&proxy->priv->lock);
191 proxy->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
194 (GDestroyNotify) g_object_unref);
198 g_dbus_object_proxy_get_object_path (GDBusObject *object)
200 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
202 g_mutex_lock (&proxy->priv->lock);
203 ret = proxy->priv->object_path;
204 g_mutex_unlock (&proxy->priv->lock);
209 * g_dbus_object_proxy_get_connection:
210 * @proxy: a #GDBusObjectProxy
212 * Gets the connection that @proxy is for.
214 * Returns: (transfer none): A #GDBusConnection. Do not free, the
215 * object is owned by @proxy.
220 g_dbus_object_proxy_get_connection (GDBusObjectProxy *proxy)
222 GDBusConnection *ret;
223 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
224 g_mutex_lock (&proxy->priv->lock);
225 ret = proxy->priv->connection;
226 g_mutex_unlock (&proxy->priv->lock);
230 static GDBusInterface *
231 g_dbus_object_proxy_get_interface (GDBusObject *object,
232 const gchar *interface_name)
234 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
237 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
238 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
240 g_mutex_lock (&proxy->priv->lock);
241 ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
244 g_mutex_unlock (&proxy->priv->lock);
246 return (GDBusInterface *) ret; /* TODO: proper cast */
250 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
252 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
255 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
259 g_mutex_lock (&proxy->priv->lock);
260 ret = g_hash_table_get_values (proxy->priv->map_name_to_iface);
261 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
262 g_mutex_unlock (&proxy->priv->lock);
267 /* ---------------------------------------------------------------------------------------------------- */
270 * g_dbus_object_proxy_new:
271 * @connection: a #GDBusConnection
272 * @object_path: the object path
274 * Creates a new #GDBusObjectProxy for the given connection and
277 * Returns: a new #GDBusObjectProxy
282 g_dbus_object_proxy_new (GDBusConnection *connection,
283 const gchar *object_path)
285 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
286 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
287 return G_DBUS_OBJECT_PROXY (g_object_new (G_TYPE_DBUS_OBJECT_PROXY,
288 "g-object-path", object_path,
289 "g-connection", connection,
294 _g_dbus_object_proxy_add_interface (GDBusObjectProxy *proxy,
295 GDBusProxy *interface_proxy)
297 const gchar *interface_name;
298 GDBusProxy *interface_proxy_to_remove;
300 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
301 g_return_if_fail (G_IS_DBUS_PROXY (interface_proxy));
303 g_mutex_lock (&proxy->priv->lock);
305 interface_name = g_dbus_proxy_get_interface_name (interface_proxy);
306 interface_proxy_to_remove = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
307 if (interface_proxy_to_remove != NULL)
309 g_object_ref (interface_proxy_to_remove);
310 g_warn_if_fail (g_hash_table_remove (proxy->priv->map_name_to_iface, interface_name));
312 g_hash_table_insert (proxy->priv->map_name_to_iface,
313 g_strdup (interface_name),
314 g_object_ref (interface_proxy));
315 g_object_ref (interface_proxy);
317 g_mutex_unlock (&proxy->priv->lock);
319 if (interface_proxy_to_remove != NULL)
321 g_signal_emit_by_name (proxy, "interface-removed", interface_proxy_to_remove);
322 g_object_unref (interface_proxy_to_remove);
325 g_signal_emit_by_name (proxy, "interface-added", interface_proxy);
326 g_object_unref (interface_proxy);
330 _g_dbus_object_proxy_remove_interface (GDBusObjectProxy *proxy,
331 const gchar *interface_name)
333 GDBusProxy *interface_proxy;
335 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
336 g_return_if_fail (g_dbus_is_interface_name (interface_name));
338 g_mutex_lock (&proxy->priv->lock);
340 interface_proxy = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
341 if (interface_proxy != NULL)
343 g_object_ref (interface_proxy);
344 g_warn_if_fail (g_hash_table_remove (proxy->priv->map_name_to_iface, interface_name));
345 g_mutex_unlock (&proxy->priv->lock);
346 g_signal_emit_by_name (proxy, "interface-removed", interface_proxy);
347 g_object_unref (interface_proxy);
351 g_mutex_unlock (&proxy->priv->lock);
356 dbus_object_interface_init (GDBusObjectIface *iface)
358 iface->get_object_path = g_dbus_object_proxy_get_object_path;
359 iface->get_interfaces = g_dbus_object_proxy_get_interfaces;
360 iface->get_interface = g_dbus_object_proxy_get_interface;