cd9360961a0fa3131ffc3baa36a421ed402266a2
[platform/upstream/at-spi2-atk.git] / atk-adaptor / adaptors / cache-adaptor.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  * Copyright 2008, 2009 Codethink Ltd.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include <string.h>
27
28 #include <atk/atk.h>
29 #include <droute/droute.h>
30
31 #include "spi-dbus.h"
32 #include "accessible-stateset.h"
33 #include "accessible-cache.h"
34 #include "bridge.h"
35 #include "object.h"
36 #include "introspection.h"
37
38 /* TODO - This should possibly be a common define */
39 #define SPI_OBJECT_PREFIX "/org/a11y/atspi"
40 #define SPI_CACHE_OBJECT_SUFFIX "/cache"
41 #define SPI_CACHE_OBJECT_PATH SPI_OBJECT_PREFIX SPI_CACHE_OBJECT_SUFFIX
42
43 #define SPI_OBJECT_REFERENCE_SIGNATURE "(" \
44                                           DBUS_TYPE_STRING_AS_STRING \
45                                           DBUS_TYPE_OBJECT_PATH_AS_STRING \
46                                        ")"
47                                           
48 #define SPI_CACHE_ITEM_SIGNATURE "(" \
49                                    SPI_OBJECT_REFERENCE_SIGNATURE \
50                                    SPI_OBJECT_REFERENCE_SIGNATURE \
51                                    SPI_OBJECT_REFERENCE_SIGNATURE \
52                                    DBUS_TYPE_INT32_AS_STRING \
53                                    DBUS_TYPE_INT32_AS_STRING \
54                                    DBUS_TYPE_ARRAY_AS_STRING \
55                                      DBUS_TYPE_STRING_AS_STRING \
56                                    DBUS_TYPE_STRING_AS_STRING \
57                                    DBUS_TYPE_UINT32_AS_STRING \
58                                    DBUS_TYPE_STRING_AS_STRING \
59                                    DBUS_TYPE_ARRAY_AS_STRING \
60                                      DBUS_TYPE_UINT32_AS_STRING \
61                                  ")"
62
63 /*---------------------------------------------------------------------------*/
64
65 static const char *
66 get_toolkit_name (AtkObject *obj)
67 {
68   static const char *toolkit_name = NULL;
69
70   if (!toolkit_name)
71     toolkit_name = atk_get_toolkit_name ();
72
73   /* TODO: query object attributes */
74   return toolkit_name;
75 }
76
77 static gboolean
78 should_call_index_in_parent (AtkObject *obj, AtkStateSet *set)
79 {
80   if (atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
81     return FALSE;
82
83   if (!strcmp (get_toolkit_name (obj), "gtk") &&
84       atk_object_get_role (obj) == ATK_ROLE_MENU_ITEM)
85     return FALSE;
86
87   return TRUE;
88 }
89
90 static gboolean
91 should_cache_children (AtkObject *obj, AtkStateSet *set)
92 {
93   if (atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS) ||
94       atk_state_set_contains_state (set, ATK_STATE_DEFUNCT))
95     return FALSE;
96
97   if (!strcmp (get_toolkit_name (obj), "gtk") &&
98       atk_object_get_role (obj) == ATK_ROLE_MENU)
99     return FALSE;
100
101   return TRUE;
102 }
103
104 /*
105  * Marshals the given AtkObject into the provided D-Bus iterator.
106  *
107  * The object is marshalled including all its client side cache data.
108  * The format of the structure is (o(so)iiassusau).
109  */
110 static void
111 append_cache_item (AtkObject * obj, gpointer data)
112 {
113   DBusMessageIter iter_struct, iter_sub_array;
114   dbus_uint32_t states[2];
115   dbus_int32_t count, index;
116   AtkStateSet *set;
117   DBusMessageIter *iter_array = (DBusMessageIter *) data;
118   const char *name, *desc;
119   dbus_uint32_t role;
120
121   set = atk_object_ref_state_set (obj);
122     AtkObject *application, *parent;
123
124   dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
125                                     &iter_struct);
126
127   /* Marshal object path */
128   spi_object_append_reference (&iter_struct, obj);
129
130   role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
131
132   /* Marshal application */
133   application = spi_global_app_data->root;
134   spi_object_append_reference (&iter_struct, application);
135
136   /* Marshal parent */
137   parent = atk_object_get_parent (obj);
138   if (parent == NULL)
139     {
140       /* TODO, move in to a 'Plug' wrapper. */
141       if (ATK_IS_PLUG (obj))
142         {
143           char *id = g_object_get_data (G_OBJECT (obj), "dbus-plug-parent");
144           char *bus_parent;
145           char *path_parent;
146
147           if (id)
148             {
149               bus_parent = g_strdup (id);
150               if (bus_parent && (path_parent = g_utf8_strchr (bus_parent + 1, -1, ':')))
151                 {
152                   DBusMessageIter iter_parent;
153                   *(path_parent++) = '\0';
154                   dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_STRUCT, NULL,
155                                                     &iter_parent);
156                   dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_STRING, &bus_parent);
157                   dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_OBJECT_PATH, &path_parent);
158                   dbus_message_iter_close_container (&iter_struct, &iter_parent);
159                 }
160               else
161                 {
162                   spi_object_append_null_reference (&iter_struct);
163                 }
164             }
165           else
166             {
167               spi_object_append_null_reference (&iter_struct);
168             }
169         }
170       else if (role != ATSPI_ROLE_APPLICATION)
171         spi_object_append_null_reference (&iter_struct);
172       else
173         spi_object_append_desktop_reference (&iter_struct);
174     }
175   else
176     {
177       spi_object_append_reference (&iter_struct, parent);
178     }
179
180   /* Marshal index in parent */
181   index = (should_call_index_in_parent (obj, set)
182            ? atk_object_get_index_in_parent (obj) : -1);
183   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &index);
184
185   /* marshal child count */
186   count = (should_cache_children (obj, set)
187            ? atk_object_get_n_accessible_children (obj) : -1);
188
189   if (ATK_IS_SOCKET (obj) && atk_socket_is_occupied (ATK_SOCKET (obj)))
190     count = 1;
191   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &count);
192
193   /* Marshal interfaces */
194   dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
195                                     &iter_sub_array);
196   spi_object_append_interfaces (&iter_sub_array, obj);
197   dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
198
199   /* Marshal name */
200   name = atk_object_get_name (obj);
201   if (!name)
202     name = "";
203   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
204
205   /* Marshal role */
206   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
207
208   /* Marshal description */
209   desc = atk_object_get_description (obj);
210   if (!desc)
211     desc = "";
212   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
213
214   /* Marshal state set */
215   spi_atk_state_set_to_dbus_array (set, states);
216   dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u",
217                                     &iter_sub_array);
218   for (count = 0; count < 2; count++)
219     {
220       dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32,
221                                       &states[count]);
222     }
223   dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
224
225   dbus_message_iter_close_container (iter_array, &iter_struct);
226   g_object_unref (set);
227 }
228
229 /*---------------------------------------------------------------------------*/
230
231 static void
232 ref_accessible_hf (gpointer key, gpointer obj_data, gpointer data)
233 {
234   g_object_ref (key);
235 }
236
237 /* For use as a GHFunc */
238 static void
239 append_accessible_hf (gpointer key, gpointer obj_data, gpointer data)
240 {
241   /* Make sure it isn't a hyperlink */
242   if (ATK_IS_OBJECT (key))
243     append_cache_item (ATK_OBJECT (key), data);
244 }
245
246 static void
247 add_to_list_hf (gpointer key, gpointer obj_data, gpointer data)
248 {
249   GSList **listptr = data;
250   *listptr = g_slist_prepend (*listptr, key);
251 }
252
253 /*---------------------------------------------------------------------------*/
254
255 static void
256 emit_cache_remove (SpiCache *cache, GObject * obj)
257 {
258   DBusMessage *message;
259
260   if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
261                                           ATSPI_DBUS_INTERFACE_CACHE,
262                                           "RemoveAccessible")))
263     {
264       DBusMessageIter iter;
265
266       dbus_message_iter_init_append (message, &iter);
267
268       spi_object_append_reference (&iter, ATK_OBJECT (obj));
269
270       dbus_connection_send (spi_global_app_data->bus, message, NULL);
271
272       dbus_message_unref (message);
273     }
274 }
275
276 static void
277 emit_cache_add (SpiCache *cache, GObject * obj)
278 {
279   AtkObject *accessible = ATK_OBJECT (obj);
280   DBusMessage *message;
281
282   if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
283                                           ATSPI_DBUS_INTERFACE_CACHE,
284                                           "AddAccessible")))
285     {
286       DBusMessageIter iter;
287
288       dbus_message_iter_init_append (message, &iter);
289       g_object_ref (accessible);
290       append_cache_item (accessible, &iter);
291       g_object_unref (accessible);
292
293       dbus_connection_send (spi_global_app_data->bus, message, NULL);
294
295       dbus_message_unref (message);
296     }
297 }
298
299
300 /*---------------------------------------------------------------------------*/
301
302 static DBusMessage *
303 impl_GetRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
304 {
305   return spi_object_return_reference (message, spi_global_app_data->root);
306 }
307
308 /*---------------------------------------------------------------------------*/
309
310 static DBusMessage *
311 impl_GetItems (DBusConnection * bus, DBusMessage * message, void *user_data)
312 {
313   DBusMessage *reply;
314   DBusMessageIter iter, iter_array;
315   GSList *pending_unrefs = NULL;
316
317   if (bus == spi_global_app_data->bus)
318     spi_atk_add_client (dbus_message_get_sender (message));
319
320   reply = dbus_message_new_method_return (message);
321
322   dbus_message_iter_init_append (reply, &iter);
323   dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
324                                     SPI_CACHE_ITEM_SIGNATURE, &iter_array);
325   spi_cache_foreach (spi_global_cache, ref_accessible_hf, NULL);
326   spi_cache_foreach (spi_global_cache, append_accessible_hf, &iter_array);
327   spi_cache_foreach (spi_global_cache, add_to_list_hf, &pending_unrefs);
328   g_slist_free_full (pending_unrefs, g_object_unref);
329   dbus_message_iter_close_container (&iter, &iter_array);
330   return reply;
331 }
332
333 /*---------------------------------------------------------------------------*/
334
335 static DRouteMethod methods[] = {
336   {impl_GetRoot, "GetRoot"},
337   {impl_GetItems, "GetItems"},
338   {NULL, NULL}
339 };
340
341 void
342 spi_initialize_cache (DRoutePath * path)
343 {
344   droute_path_add_interface (path, ATSPI_DBUS_INTERFACE_CACHE, spi_org_a11y_atspi_Cache, methods, NULL);
345
346   g_signal_connect (spi_global_cache, "object-added",
347                     (GCallback) emit_cache_add, NULL);
348
349   g_signal_connect (spi_global_cache, "object-removed",
350                     (GCallback) emit_cache_remove, NULL);
351 };
352
353 /*END------------------------------------------------------------------------*/