Clean up GSettingsSchema logic
[glib.git] / gio / gdbusobjectproxy.c
blob2ed1bb4ced31e50b0d9e8ac78784e482c8116c78
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_ADD_PRIVATE (GDBusObjectProxy)
66 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init))
68 static void
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);
85 static void
86 g_dbus_object_proxy_get_property (GObject *object,
87 guint prop_id,
88 GValue *value,
89 GParamSpec *pspec)
91 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
93 switch (prop_id)
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);
99 break;
101 case PROP_G_CONNECTION:
102 g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
103 break;
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy, prop_id, pspec);
107 break;
111 static void
112 g_dbus_object_proxy_set_property (GObject *object,
113 guint prop_id,
114 const GValue *value,
115 GParamSpec *pspec)
117 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
119 switch (prop_id)
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);
125 break;
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);
131 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (proxy, prop_id, pspec);
135 break;
139 static void
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.
153 * Since: 2.30
155 g_object_class_install_property (gobject_class,
156 PROP_G_OBJECT_PATH,
157 g_param_spec_string ("g-object-path",
158 "Object Path",
159 "The object path of the proxy",
160 NULL,
161 G_PARAM_READWRITE |
162 G_PARAM_CONSTRUCT_ONLY |
163 G_PARAM_STATIC_STRINGS));
166 * GDBusObjectProxy:g-connection:
168 * The connection of the proxy.
170 * Since: 2.30
172 g_object_class_install_property (gobject_class,
173 PROP_G_CONNECTION,
174 g_param_spec_object ("g-connection",
175 "Connection",
176 "The connection of the proxy",
177 G_TYPE_DBUS_CONNECTION,
178 G_PARAM_READWRITE |
179 G_PARAM_CONSTRUCT_ONLY |
180 G_PARAM_STATIC_STRINGS));
183 static void
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,
189 g_str_equal,
190 g_free,
191 (GDestroyNotify) g_object_unref);
194 static const gchar *
195 g_dbus_object_proxy_get_object_path (GDBusObject *object)
197 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
198 const gchar *ret;
199 g_mutex_lock (&proxy->priv->lock);
200 ret = proxy->priv->object_path;
201 g_mutex_unlock (&proxy->priv->lock);
202 return ret;
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.
214 * Since: 2.30
216 GDBusConnection *
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);
224 return ret;
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);
232 GDBusProxy *ret;
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);
239 if (ret != NULL)
240 g_object_ref (ret);
241 g_mutex_unlock (&proxy->priv->lock);
243 return (GDBusInterface *) ret; /* TODO: proper cast */
246 static GList *
247 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
249 GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
250 GList *ret;
252 g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
254 ret = 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);
261 return ret;
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
272 * object path.
274 * Returns: a new #GDBusObjectProxy
276 * Since: 2.30
278 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,
287 NULL));
290 void
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);
326 void
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);
346 else
348 g_mutex_unlock (&proxy->priv->lock);
352 static void
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;