Start merging gdbus-codegen code
[platform/upstream/glib.git] / gio / gdbusobjectproxy.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gdbusobject.h"
26 #include "gdbusobjectproxy.h"
27 #include "gdbusconnection.h"
28 #include "gdbusprivate.h"
29 #include "gdbusutils.h"
30 #include "gdbusproxy.h"
31
32 #include "glibintl.h"
33
34 /**
35  * SECTION:gdbusobjectproxy
36  * @short_description: Client-side D-Bus object
37  * @include: gio/gio.h
38  *
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.
43  */
44
45 struct _GDBusObjectProxyPrivate
46 {
47   GHashTable *map_name_to_iface;
48   gchar *object_path;
49   GDBusConnection *connection;
50 };
51
52 enum
53 {
54   PROP_0,
55   PROP_OBJECT_PATH,
56   PROP_CONNECTION
57 };
58
59 static void dbus_object_interface_init (GDBusObjectIface *iface);
60
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));
63
64 static void
65 g_dbus_object_proxy_finalize (GObject *object)
66 {
67   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
68
69   g_hash_table_unref (proxy->priv->map_name_to_iface);
70
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);
73 }
74
75 static void
76 g_dbus_object_proxy_get_property (GObject    *object,
77                                   guint       prop_id,
78                                   GValue     *value,
79                                   GParamSpec *pspec)
80 {
81   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
82
83   switch (prop_id)
84     {
85     case PROP_OBJECT_PATH:
86       g_value_set_string (value, proxy->priv->object_path);
87       break;
88
89     case PROP_CONNECTION:
90       g_value_set_object (value, g_dbus_object_proxy_get_connection (proxy));
91       break;
92
93     default:
94       G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
95       break;
96     }
97 }
98
99 static void
100 g_dbus_object_proxy_set_property (GObject       *object,
101                                   guint          prop_id,
102                                   const GValue  *value,
103                                   GParamSpec    *pspec)
104 {
105   switch (prop_id)
106     {
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
109       break;
110     }
111 }
112
113 static void
114 g_dbus_object_proxy_class_init (GDBusObjectProxyClass *klass)
115 {
116   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117
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;
121
122   /**
123    * GDBusObjectProxy:object-path:
124    *
125    * The object path of the proxy.
126    */
127   g_object_class_install_property (gobject_class,
128                                    PROP_OBJECT_PATH,
129                                    g_param_spec_string ("object-path",
130                                                         "Object Path",
131                                                         "The object path of the proxy",
132                                                         NULL,
133                                                         G_PARAM_READABLE |
134                                                         G_PARAM_STATIC_STRINGS));
135
136   /**
137    * GDBusObjectProxy:connection:
138    *
139    * The connection of the proxy.
140    */
141   g_object_class_install_property (gobject_class,
142                                    PROP_CONNECTION,
143                                    g_param_spec_string ("connection",
144                                                         "Connection",
145                                                         "The connection of the proxy",
146                                                         NULL,
147                                                         G_PARAM_READABLE |
148                                                         G_PARAM_STATIC_STRINGS));
149
150   g_type_class_add_private (klass, sizeof (GDBusObjectProxyPrivate));
151 }
152
153 static void
154 g_dbus_object_proxy_init (GDBusObjectProxy *proxy)
155 {
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,
160                                                           g_str_equal,
161                                                           g_free,
162                                                           (GDestroyNotify) g_object_unref);
163 }
164
165 static const gchar *
166 g_dbus_object_proxy_get_object_path (GDBusObject *object)
167 {
168   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
169   return proxy->priv->object_path;
170 }
171
172 /**
173  * g_dbus_object_proxy_get_connection:
174  * @proxy: A #GDBusObjectProxy.
175  *
176  * Gets the connection that @proxy is for.
177  *
178  * Returns: A #GDBusConnection. Do not free, the object is owned by @proxy.
179  */
180 GDBusConnection *
181 g_dbus_object_proxy_get_connection (GDBusObjectProxy *proxy)
182 {
183   g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
184   return proxy->priv->connection;
185 }
186
187 static GDBusInterface *
188 g_dbus_object_proxy_get_interface (GDBusObject *object,
189                                    const gchar *interface_name)
190 {
191   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
192   GDBusProxy *ret;
193
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);
196
197   ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
198   if (ret != NULL)
199     g_object_ref (ret);
200   return (GDBusInterface *) ret; /* TODO: proper cast */
201 }
202
203 static GList *
204 g_dbus_object_proxy_get_interfaces (GDBusObject *object)
205 {
206   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
207   GList *ret;
208   GHashTableIter iter;
209   GDBusProxy *interface_proxy;
210
211   g_return_val_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy), NULL);
212
213   ret = NULL;
214
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));
218
219   return ret;
220 }
221
222 /* ---------------------------------------------------------------------------------------------------- */
223
224 GDBusObjectProxy *
225 _g_dbus_object_proxy_new (GDBusConnection *connection,
226                           const gchar *object_path)
227 {
228   GDBusObjectProxy *proxy;
229
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);
232
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);
236   return proxy;
237 }
238
239 void
240 _g_dbus_object_proxy_add_interface (GDBusObjectProxy *proxy,
241                                     GDBusProxy       *interface_proxy)
242 {
243   const gchar *interface_name;
244
245   g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
246   g_return_if_fail (G_IS_DBUS_PROXY (interface_proxy));
247
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);
254 }
255
256 void
257 _g_dbus_object_proxy_remove_interface (GDBusObjectProxy *proxy,
258                                        const gchar      *interface_name)
259 {
260   GDBusProxy *interface_proxy;
261
262   g_return_if_fail (G_IS_DBUS_OBJECT_PROXY (proxy));
263   g_return_if_fail (g_dbus_is_interface_name (interface_name));
264
265   interface_proxy = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
266   if (interface_proxy != NULL)
267     {
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);
272     }
273 }
274
275 static gpointer
276 g_dbus_object_proxy_lookup_with_typecheck (GDBusObject *object,
277                                            const gchar *interface_name,
278                                            GType        type)
279 {
280   GDBusObjectProxy *proxy = G_DBUS_OBJECT_PROXY (object);
281   GDBusProxy *ret;
282
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);
285
286   ret = g_hash_table_lookup (proxy->priv->map_name_to_iface, interface_name);
287   if (ret != NULL)
288     {
289       g_warn_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (ret, type));
290       g_object_ref (ret);
291     }
292   return ret;
293 }
294
295 static gpointer
296 g_dbus_object_proxy_peek_with_typecheck (GDBusObject  *object,
297                                          const gchar  *interface_name,
298                                          GType         type)
299 {
300   GDBusProxy *ret;
301   ret = g_dbus_object_proxy_lookup_with_typecheck (object, interface_name, type);
302   if (ret != NULL)
303     g_object_unref (ret);
304   return ret;
305 }
306
307 static void
308 dbus_object_interface_init (GDBusObjectIface *iface)
309 {
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;
315 }