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. You cannot instantiate a
41 * #GDBusObjectProxy yourself - you need to use a
42 * #GDBusObjectManagerClient to get one.
45 struct _GDBusObjectProxyPrivate
47 GHashTable *map_name_to_iface;
49 GDBusConnection *connection;
59 static void dbus_object_interface_init (GDBusObjectIface *iface);
61 G_DEFINE_TYPE_WITH_CODE (GDBusObjectProxy, g_dbus_object_proxy, G_TYPE_OBJECT,
62 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init));
65 g_dbus_object_proxy_finalize (GObject *object)
67 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
69 g_hash_table_unref (proxy->priv->map_name_to_iface);
71 if (G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize != NULL)
72 G_OBJECT_CLASS (g_dbus_object_proxy_parent_class)->finalize (object);
76 g_dbus_object_proxy_get_property (GObject *object,
81 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
85 case PROP_OBJECT_PATH:
86 g_value_set_string (value, proxy->priv->object_path);
90 g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
100 g_dbus_object_proxy_set_property (GObject *object,
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
114 g_dbus_object_proxy_class_init (GDBusObjectProxyClass *klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 gobject_class->finalize = g_dbus_object_proxy_finalize;
119 gobject_class->set_property = g_dbus_object_proxy_set_property;
120 gobject_class->get_property = g_dbus_object_proxy_get_property;
123 * GDBusObjectProxy:object-path:
125 * The object path of the proxy.
127 g_object_class_install_property (gobject_class,
129 g_param_spec_string ("object-path",
131 "The object path of the proxy",
134 G_PARAM_STATIC_STRINGS));
137 * GDBusObjectProxy:connection:
139 * The connection of the proxy.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_string ("connection",
145 "The connection of the proxy",
148 G_PARAM_STATIC_STRINGS));
150 g_type_class_add_private (klass, sizeof (GDBusObjectProxyPrivate));
154 g_dbus_object_proxy_init (GDBusObjectProxy *proxy)
156 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy,
157 G_TYPE_DBUS_OBJECT_PROXY,
158 GDBusObjectProxyPrivate);
159 proxy->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
162 (GDestroyNotify) g_object_unref);
166 g_dbus_object_proxy_get_object_path (GDBusObject *object)
168 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
169 return proxy->priv->object_path;
173 * g_dbus_object_proxy_get_connection:
174 * @proxy: A #GDBusObjectProxy.
176 * Gets the connection that @proxy is for.
178 * Returns: A #GDBusConnection. Do not free, the object is owned by @proxy.
181 g_dbus_object_proxy_get_connection (GDBusObjectProxy *proxy)
183 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
184 return proxy->priv->connection;
187 static GDBusInterface *
188 g_dbus_object_proxy_get_interface (GDBusObject *object,
189 const gchar *interface_name)
191 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
194 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
195 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
197 ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
200 return (GDBusInterface *) ret; /* TODO: proper cast */
204 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
206 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
209 GDBusProxy *interface_proxy;
211 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
215 g_hash_table_iter_init (&iter, proxy->priv->map_name_to_iface);
216 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &interface_proxy))
217 ret = g_list_prepend (ret, g_object_ref (interface_proxy));
222 /* ---------------------------------------------------------------------------------------------------- */
225 _g_dbus_object_proxy_new (GDBusConnection *connection,
226 const gchar *object_path)
228 GDBusObjectProxy *proxy;
230 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
231 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
233 proxy = G_DBUS_OBJECT_PROXY (g_object_new (G_TYPE_DBUS_OBJECT_PROXY, NULL));
234 proxy->priv->object_path = g_strdup (object_path);
235 proxy->priv->connection = g_object_ref (connection);
240 _g_dbus_object_proxy_add_interface (GDBusObjectProxy *proxy,
241 GDBusProxy *interface_proxy)
243 const gchar *interface_name;
245 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
246 g_return_if_fail (G_IS_DBUS_PROXY (interface_proxy));
248 interface_name = g_dbus_proxy_get_interface_name (interface_proxy);
249 _g_dbus_object_proxy_remove_interface (proxy, interface_name);
250 g_hash_table_insert (proxy->priv->map_name_to_iface,
251 g_strdup (interface_name),
252 g_object_ref (interface_proxy));
253 g_signal_emit_by_name (proxy, "interface-added", interface_proxy);
257 _g_dbus_object_proxy_remove_interface (GDBusObjectProxy *proxy,
258 const gchar *interface_name)
260 GDBusProxy *interface_proxy;
262 g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
263 g_return_if_fail (g_dbus_is_interface_name (interface_name));
265 interface_proxy = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
266 if (interface_proxy != NULL)
268 g_object_ref (interface_proxy);
269 g_warn_if_fail (g_hash_table_remove (proxy->priv->map_name_to_iface, interface_name));
270 g_signal_emit_by_name (proxy, "interface-removed", interface_proxy);
271 g_object_unref (interface_proxy);
276 g_dbus_object_proxy_lookup_with_typecheck (GDBusObject *object,
277 const gchar *interface_name,
280 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
283 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
284 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
286 ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
289 g_warn_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (ret, type));
296 g_dbus_object_proxy_peek_with_typecheck (GDBusObject *object,
297 const gchar *interface_name,
301 ret = g_dbus_object_proxy_lookup_with_typecheck (object, interface_name, type);
303 g_object_unref (ret);
308 dbus_object_interface_init (GDBusObjectIface *iface)
310 iface->get_object_path = g_dbus_object_proxy_get_object_path;
311 iface->get_interfaces = g_dbus_object_proxy_get_interfaces;
312 iface->get_interface = g_dbus_object_proxy_get_interface;
313 iface->peek_with_typecheck = g_dbus_object_proxy_peek_with_typecheck;
314 iface->lookup_with_typecheck = g_dbus_object_proxy_lookup_with_typecheck;