Temporary commit. Non working changes to the registration
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / tree.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  * Copyright 2001, 2002 Sun Microsystems Inc.,
7  * Copyright 2001, 2002 Ximian, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <string.h>
26 #include <droute/introspect-loader.h>
27
28 #include "accessible.h"
29 #include "bridge.h"
30
31 extern SpiAppData *app_data;
32 static gboolean update_pending = FALSE;
33
34 /*---------------------------------------------------------------------------*/
35
36 static const char *dumm = "/APath/1";
37
38 /*
39  * Marshals the given AtkObject into the provided D-Bus iterator.
40  *
41  * The object is marshalled including all its client side cache data.
42  * The format of the strucuture is (ooaoassus).
43  * This is used in the updateTree signal and the getTree method
44  * of the org.freedesktop.atspi.Tree interface.
45  */
46 static void
47 append_accessible(gpointer ref, gpointer obj_data, gpointer iter)
48 {
49   AtkObject *obj;
50   DBusMessageIter *iter_array;
51   DBusMessageIter iter_struct, iter_sub_array;
52   DRouteData *data;
53
54   const char *name, *desc;
55   int i;
56   dbus_uint32_t role;
57   GSList *l;
58
59   obj = ATK_OBJECT(obj_data);
60   iter_array = (DBusMessageIter *) iter;
61   data = &(app_data->droute);
62
63   dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
64     {
65       AtkObject *parent;
66       gchar *path, *path_parent;
67
68       path = atk_dbus_get_path_from_ref(GPOINTER_TO_INT(ref));
69       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
70
71       parent = atk_object_get_parent(obj);
72       if (parent == NULL)
73         path_parent = g_strdup("/");
74       else
75         path_parent = atk_dbus_get_path (parent);
76       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
77       g_free(path_parent);
78
79       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o", &iter_sub_array);
80         {
81           gint childcount, i;
82
83           childcount = atk_object_get_n_accessible_children (obj);
84           for (i = 0; i < childcount; i++)
85             {
86               AtkObject *child;
87               gchar *child_path;
88               
89               child = atk_object_ref_accessible_child (obj, i);
90               child_path = atk_dbus_get_path (child);
91               g_object_unref(G_OBJECT(child));
92               dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_OBJECT_PATH, &child_path);
93               g_free (child_path);
94             }
95         }
96       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
97
98       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s", &iter_sub_array);
99         {
100           for (l = data->interfaces; l; l = g_slist_next (l))
101             {
102               DRouteInterface *iface_def = (DRouteInterface *) l->data;
103               void *datum = NULL;
104
105               if (iface_def->get_datum)
106                 {
107                   datum = (*iface_def->get_datum) (path, data->user_data);
108                   if (!datum)
109                     continue;
110                 }
111               dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_STRING, &iface_def->name);
112               if (iface_def->free_datum)
113                 (*iface_def->free_datum) (datum);
114             }
115         }
116       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
117
118       name = atk_object_get_name (obj);
119       if (!name)
120         name = "";
121       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
122
123       role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
124       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
125
126       desc = atk_object_get_description (obj);
127       if (!desc)
128         desc = "";
129       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
130
131       g_free(path);
132     }      
133   dbus_message_iter_close_container (iter_array, &iter_struct);
134 }
135
136 /*---------------------------------------------------------------------------*/
137
138 /*
139  * Used to marshal array of objects to remove.
140  * Marshalls an object path onto the iter provided.
141  */
142 static void
143 append_accessible_path(gpointer ref_data, gpointer null, gpointer data)
144 {
145   guint ref;
146   gchar *path;
147   DBusMessageIter *iter_array;
148
149   iter_array = (DBusMessageIter *) data;
150   ref = GPOINTER_TO_INT(ref_data);
151   path = atk_dbus_get_path_from_ref(ref);
152   dbus_message_iter_append_basic (iter_array, DBUS_TYPE_OBJECT_PATH, &path);
153   g_free(path);
154 }
155
156 /*---------------------------------------------------------------------------*/
157
158 static gboolean
159 send_cache_update(gpointer d)
160 {
161   DBusMessage *message;
162   DBusMessageIter iter;
163   DBusMessageIter iter_array;
164   DRouteData *data;
165
166   data = &(app_data->droute);
167
168   message = dbus_message_new_signal ("/org/freedesktop/atspi/tree", SPI_DBUS_INTERFACE_TREE, "updateTree");
169
170   dbus_message_iter_init_append (message, &iter);
171
172   dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)", &iter_array);
173   atk_dbus_foreach_update_list(append_accessible, &iter_array);
174   dbus_message_iter_close_container(&iter, &iter_array);
175
176   dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "o", &iter_array);
177   atk_dbus_foreach_remove_list(append_accessible_path, &iter_array);
178   dbus_message_iter_close_container(&iter, &iter_array);
179
180   dbus_connection_send(data->bus, message, NULL);
181   update_pending = FALSE;
182
183   return FALSE;
184 }
185
186 /*---------------------------------------------------------------------------*/
187
188 void
189 atk_tree_cache_needs_update(void)
190 {
191   if (!update_pending)
192     {
193       g_idle_add(send_cache_update, NULL);
194       update_pending = TRUE;
195     }
196 }
197
198 /*---------------------------------------------------------------------------*/
199
200 static DBusMessage *
201 impl_getRoot (DBusConnection *bus, DBusMessage *message, void *user_data)
202 {
203   AtkObject *root = atk_get_root();
204   char *path;
205   DBusMessage *reply;
206
207   if (root) path = atk_dbus_get_path(root);
208   if (!root || !path)
209     return spi_dbus_general_error (message);
210   reply = dbus_message_new_method_return (message);
211   dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
212   g_free (path);
213   return reply;
214 }
215
216 /*---------------------------------------------------------------------------*/
217
218 static DBusMessage *
219 impl_getTree (DBusConnection *bus, DBusMessage *message, void *user_data)
220 {
221   DBusMessage *reply;
222   DBusMessageIter iter, iter_array;
223   AtkObject *root = atk_get_root();
224
225   if (!root) 
226      return spi_dbus_general_error(message);
227   reply = dbus_message_new_method_return (message);
228
229   dbus_message_iter_init_append (reply, &iter);
230   dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ooaoassus)", &iter_array);
231   atk_dbus_foreach_registered(append_accessible, &iter_array);
232   dbus_message_iter_close_container(&iter, &iter_array);
233   return reply;
234 }
235
236 /*---------------------------------------------------------------------------*/
237
238 static DBusMessage *
239 impl_introspect (DBusConnection *bus, DBusMessage *message, void *user_data)
240 {
241   const char *path;
242   GString *output;
243   char *final;
244
245   DBusMessage *reply;
246
247   path = dbus_message_get_path(message);
248
249   output = g_string_new(spi_introspection_header);
250   
251   g_string_append_printf(output, spi_introspection_node_element, path);
252
253   spi_append_interface(output, SPI_DBUS_INTERFACE_TREE);
254
255   g_string_append(output, spi_introspection_footer);
256   final = g_string_free(output, FALSE);
257
258   reply = dbus_message_new_method_return (message);
259   g_assert(reply != NULL);
260   dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
261                            DBUS_TYPE_INVALID);
262
263   g_free(final);
264   return reply;
265 }
266
267 /*---------------------------------------------------------------------------*/
268
269 static DBusHandlerResult
270 message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
271 {
272   const char *iface = dbus_message_get_interface (message);
273   const char *member = dbus_message_get_member (message);
274
275   DBusMessage *reply = NULL;
276
277   g_return_val_if_fail(iface != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
278   
279   if (!strcmp(iface, SPI_DBUS_INTERFACE_TREE))
280     {
281       if (!strcmp(member, "getRoot"))
282         {
283           reply = impl_getRoot(bus, message, user_data);
284         }
285
286       if (!strcmp(member, "getTree"))
287         {
288           reply = impl_getTree(bus, message, user_data);
289         }
290     }
291
292   if (!strcmp(iface, "org.freedesktop.DBus.Introspectable"))
293     {
294       if (!strcmp(member, "Introspect"))
295         {
296           reply = impl_introspect(bus, message, user_data);
297         }
298     }
299
300   if (reply)
301     {
302       dbus_connection_send (bus, reply, NULL);
303       dbus_message_unref (reply);
304     }
305
306   return DBUS_HANDLER_RESULT_HANDLED;
307 }
308
309 /*---------------------------------------------------------------------------*/
310
311 static DBusObjectPathVTable tree_vtable =
312 {
313   NULL,
314   &message_handler,
315   NULL, NULL, NULL, NULL
316 };
317
318 /*---------------------------------------------------------------------------*/
319
320 void
321 spi_register_tree_object(DBusConnection *bus,
322                          DRouteData *data,
323                          const char *path)
324 {
325   dbus_bool_t mem = FALSE;
326   mem = dbus_connection_register_object_path(bus, path, &tree_vtable, data);
327   g_assert(mem == TRUE);
328 }
329
330 /*END------------------------------------------------------------------------*/