Fix a mixup of singular and plural
[glib.git] / gio / gdbusobjectproxy.c
blob9f28074478050c14e6b9c41f970c74cc0d8487f0
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>
23 #include "config.h"
25 #include "gdbusobject.h"
26 #include "gdbusobjectproxy.h"
27 #include "gdbusconnection.h"
28 #include "gdbusprivate.h"
29 #include "gdbusutils.h"
30 #include "gdbusproxy.h"
32 #include "glibintl.h"
34 /**
35 * SECTION:gdbusobjectproxy
36 * @short_description: Client-side D-Bus object
37 * @include: gio/gio.h
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.
44 * Since: 2.30
47 struct _GDBusObjectProxyPrivate
49 GMutex lock;
50 GHashTable *map_name_to_iface;
51 gchar *object_path;
52 GDBusConnection *connection;
55 enum
57 PROP_0,
58 PROP_G_OBJECT_PATH,
59 PROP_G_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));
67 static void
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);
84 static void
85 g_dbus_object_proxy_get_property (GObject *object,
86 guint prop_id,
87 GValue *value,
88 GParamSpec *pspec)
90 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
92 switch (prop_id)
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);
98 break;
100 case PROP_G_CONNECTION:
101 g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
102 break;
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy, prop_id, pspec);
106 break;
110 static void
111 g_dbus_object_proxy_set_property (GObject *object,
112 guint prop_id,
113 const GValue *value,
114 GParamSpec *pspec)
116 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
118 switch (prop_id)
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);
124 break;
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);
130 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy, prop_id, pspec);
134 break;
138 static void
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.
152 * Since: 2.30
154 g_object_class_install_property (gobject_class,
155 PROP_G_OBJECT_PATH,
156 g_param_spec_string ("g-object-path",
157 "Object Path",
158 "The object path of the proxy",
159 NULL,
160 G_PARAM_READWRITE |
161 G_PARAM_CONSTRUCT_ONLY |
162 G_PARAM_STATIC_STRINGS));
165 * GDBusObjectProxy:g-connection:
167 * The connection of the proxy.
169 * Since: 2.30
171 g_object_class_install_property (gobject_class,
172 PROP_G_CONNECTION,
173 g_param_spec_object ("g-connection",
174 "Connection",
175 "The connection of the proxy",
176 G_TYPE_DBUS_CONNECTION,
177 G_PARAM_READWRITE |
178 G_PARAM_CONSTRUCT_ONLY |
179 G_PARAM_STATIC_STRINGS));
181 g_type_class_add_private (klass, sizeof (GDBusObjectProxyPrivate));
184 static void
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,
192 g_str_equal,
193 g_free,
194 (GDestroyNotify) g_object_unref);
197 static const gchar *
198 g_dbus_object_proxy_get_object_path (GDBusObject *object)
200 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
201 const gchar *ret;
202 g_mutex_lock (&proxy->priv->lock);
203 ret = proxy->priv->object_path;
204 g_mutex_unlock (&proxy->priv->lock);
205 return ret;
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.
217 * Since: 2.30
219 GDBusConnection *
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);
227 return ret;
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);
235 GDBusProxy *ret;
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);
242 if (ret != NULL)
243 g_object_ref (ret);
244 g_mutex_unlock (&proxy->priv->lock);
246 return (GDBusInterface *) ret; /* TODO: proper cast */
249 static GList *
250 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
252 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
253 GList *ret;
255 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
257 ret = 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);
264 return ret;
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
275 * object path.
277 * Returns: a new #GDBusObjectProxy
279 * Since: 2.30
281 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,
290 NULL));
293 void
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);
329 void
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);
349 else
351 g_mutex_unlock (&proxy->priv->lock);
355 static void
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;