Added a new boolean parameter to allow specifying when a call to
[platform/core/uifw/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 "common/spi-dbus.h"
32 #include "accessible-cache.h"
33 #include "bridge.h"
34 #include "object.h"
35 #include "introspection.h"
36
37 /* TODO - This should possibly be a common define */
38 #define SPI_OBJECT_PREFIX "/org/a11y/atspi"
39 #define SPI_CACHE_OBJECT_SUFFIX "/cache"
40 #define SPI_CACHE_OBJECT_PATH SPI_OBJECT_PREFIX SPI_CACHE_OBJECT_SUFFIX
41
42 #define SPI_OBJECT_REFERENCE_SIGNATURE "(" \
43                                           DBUS_TYPE_STRING_AS_STRING \
44                                           DBUS_TYPE_OBJECT_PATH_AS_STRING \
45                                        ")"
46                                           
47 #define SPI_CACHE_ITEM_SIGNATURE "(" \
48                                    SPI_OBJECT_REFERENCE_SIGNATURE \
49                                    SPI_OBJECT_REFERENCE_SIGNATURE \
50                                    SPI_OBJECT_REFERENCE_SIGNATURE \
51                                    DBUS_TYPE_ARRAY_AS_STRING \
52                                      SPI_OBJECT_REFERENCE_SIGNATURE \
53                                    DBUS_TYPE_ARRAY_AS_STRING \
54                                      DBUS_TYPE_STRING_AS_STRING \
55                                    DBUS_TYPE_STRING_AS_STRING \
56                                    DBUS_TYPE_UINT32_AS_STRING \
57                                    DBUS_TYPE_STRING_AS_STRING \
58                                    DBUS_TYPE_ARRAY_AS_STRING \
59                                      DBUS_TYPE_UINT32_AS_STRING \
60                                  ")"
61
62 /*---------------------------------------------------------------------------*/
63
64 /*
65  * Marshals the given AtkObject into the provided D-Bus iterator.
66  *
67  * The object is marshalled including all its client side cache data.
68  * The format of the structure is (o(so)a(so)assusau).
69  */
70 static void
71 append_cache_item (AtkObject * obj, gpointer data)
72 {
73   DBusMessageIter iter_struct, iter_sub_array;
74   dbus_uint32_t states[2];
75   int count;
76   AtkStateSet *set;
77   DBusMessageIter *iter_array = (DBusMessageIter *) data;
78
79   const char *name, *desc;
80   dbus_uint32_t role;
81
82   g_object_ref (G_OBJECT (obj));
83
84   set = atk_object_ref_state_set (obj);
85   {
86     AtkObject *application, *parent;
87
88     dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
89                                       &iter_struct);
90
91     /* Marshall object path */
92     spi_object_append_reference (&iter_struct, obj);
93
94     role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
95
96     /* Marshall application */
97     application = spi_global_app_data->root; 
98     spi_object_append_reference (&iter_struct, application);
99
100     /* Marshall parent */
101     parent = atk_object_get_parent (obj);
102     if (parent == NULL)
103       {
104         /* TODO, move in to a 'Plug' wrapper. */
105         if (ATK_IS_PLUG (obj))
106           {
107             char *id = g_object_get_data (G_OBJECT (obj), "dbus-plug-parent");
108             char *bus_parent;
109             char *path_parent;
110
111             if (id)
112               {
113                 bus_parent = g_strdup (id);
114                 if (bus_parent && (path_parent = g_utf8_strchr (bus_parent + 1, -1, ':')))
115                   {
116                     DBusMessageIter iter_parent;
117                     *(path_parent++) = '\0';
118                     dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_STRUCT, NULL,
119                                                       &iter_parent);
120                     dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_STRING, &bus_parent);
121                     dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_OBJECT_PATH, &path_parent);
122                     dbus_message_iter_close_container (&iter_struct, &iter_parent);
123                   }
124                 else
125                   {
126                     spi_object_append_null_reference (&iter_struct);
127                   }
128               }
129             else
130               {
131                 spi_object_append_null_reference (&iter_struct);
132               }
133           }
134         else if (role != Accessibility_ROLE_APPLICATION)
135           spi_object_append_null_reference (&iter_struct);
136         else
137           spi_object_append_desktop_reference (&iter_struct);
138       }
139     else
140       {
141         spi_object_append_reference (&iter_struct, parent);
142       }
143
144     /* Marshall children */
145     dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "(so)",
146                                       &iter_sub_array);
147     if (!atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
148       {
149         gint childcount, i;
150
151         childcount = atk_object_get_n_accessible_children (obj);
152         for (i = 0; i < childcount; i++)
153           {
154             AtkObject *child;
155             gchar *child_path;
156
157             child = atk_object_ref_accessible_child (obj, i);
158             spi_object_append_reference (&iter_sub_array, child);
159             g_object_unref (G_OBJECT (child));
160           }
161       }
162     if (ATK_IS_SOCKET (obj) && atk_socket_is_occupied (ATK_SOCKET (obj)))
163       {
164         AtkSocket *socket = ATK_SOCKET (obj);
165         gchar *child_name, *child_path;
166         child_name = g_strdup (socket->embedded_plug_id);
167         child_path = g_utf8_strchr (child_name + 1, -1, ':');
168         if (child_path)
169           {
170             DBusMessageIter iter_socket;
171             *(child_path++) = '\0';
172             dbus_message_iter_open_container (&iter_sub_array, DBUS_TYPE_STRUCT, NULL,
173                                               &iter_socket);
174             dbus_message_iter_append_basic (&iter_socket, DBUS_TYPE_STRING, &child_name);
175             dbus_message_iter_append_basic (&iter_socket, DBUS_TYPE_OBJECT_PATH, &child_path);
176             dbus_message_iter_close_container (&iter_sub_array, &iter_socket);
177           }
178         g_free (child_name);
179       }
180
181     dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
182
183     /* Marshall interfaces */
184     dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
185                                       &iter_sub_array);
186     spi_object_append_interfaces (&iter_sub_array, obj);
187     dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
188
189     /* Marshall name */
190     name = atk_object_get_name (obj);
191     if (!name)
192       name = "";
193     dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
194
195     /* Marshall role */
196     dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
197
198     /* Marshall description */
199     desc = atk_object_get_description (obj);
200     if (!desc)
201       desc = "";
202     dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
203
204     /* Marshall state set */
205     spi_atk_state_set_to_dbus_array (set, states);
206     dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u",
207                                       &iter_sub_array);
208     for (count = 0; count < 2; count++)
209       {
210         dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32,
211                                         &states[count]);
212       }
213     dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
214   }
215   dbus_message_iter_close_container (iter_array, &iter_struct);
216   g_object_unref (set);
217   g_object_unref (obj);
218 }
219
220 /*---------------------------------------------------------------------------*/
221
222 /* For use as a GHFunc */
223 static void
224 append_accessible_hf (gpointer key, gpointer obj_data, gpointer data)
225 {
226   /* Make sure it isn't a hyperlink */
227   if (ATK_IS_OBJECT (key))
228     append_cache_item (ATK_OBJECT (key), data);
229 }
230
231 /*---------------------------------------------------------------------------*/
232
233 static void
234 emit_cache_remove (SpiCache *cache, GObject * obj)
235 {
236   DBusMessage *message;
237
238   if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
239                                           SPI_DBUS_INTERFACE_CACHE,
240                                           "RemoveAccessible")))
241     {
242       DBusMessageIter iter;
243       gchar *path;
244
245       dbus_message_iter_init_append (message, &iter);
246
247       spi_object_append_reference (&iter, ATK_OBJECT (obj));
248
249       dbus_connection_send (spi_global_app_data->bus, message, NULL);
250
251       dbus_message_unref (message);
252     }
253 }
254
255 static void
256 emit_cache_add (SpiCache *cache, GObject * obj)
257 {
258   AtkObject *accessible = ATK_OBJECT (obj);
259   DBusMessage *message;
260
261   if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
262                                           SPI_DBUS_INTERFACE_CACHE,
263                                           "AddAccessible")))
264     {
265       DBusMessageIter iter;
266
267       dbus_message_iter_init_append (message, &iter);
268       append_cache_item (accessible, &iter);
269
270       dbus_connection_send (spi_global_app_data->bus, message, NULL);
271
272       dbus_message_unref (message);
273     }
274 }
275
276
277 /*---------------------------------------------------------------------------*/
278
279 static DBusMessage *
280 impl_GetRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
281 {
282   return spi_object_return_reference (message,
283                                       g_object_ref (G_OBJECT (spi_global_app_data->root)),
284                                       TRUE);
285 }
286
287 /*---------------------------------------------------------------------------*/
288
289 static DBusMessage *
290 impl_GetItems (DBusConnection * bus, DBusMessage * message, void *user_data)
291 {
292   DBusMessage *reply;
293   DBusMessageIter iter, iter_array;
294
295   reply = dbus_message_new_method_return (message);
296
297   dbus_message_iter_init_append (reply, &iter);
298   dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
299                                     SPI_CACHE_ITEM_SIGNATURE, &iter_array);
300   spi_cache_foreach (spi_global_cache, append_accessible_hf, &iter_array);
301   dbus_message_iter_close_container (&iter, &iter_array);
302   return reply;
303 }
304
305 /*---------------------------------------------------------------------------*/
306
307 static DRouteMethod methods[] = {
308   {impl_GetRoot, "GetRoot"},
309   {impl_GetItems, "GetItems"},
310   {NULL, NULL}
311 };
312
313 void
314 spi_initialize_cache (DRoutePath * path)
315 {
316   droute_path_add_interface (path, SPI_DBUS_INTERFACE_CACHE, spi_org_a11y_atspi_Cache, methods, NULL);
317
318   g_signal_connect (spi_global_cache,
319                     "object-added",
320                     (GCallback) emit_cache_add,
321                     NULL);
322
323   g_signal_connect (spi_global_cache,
324                     "object-removed",
325                     (GCallback) emit_cache_remove,
326                     NULL);
327 };
328
329 /*END------------------------------------------------------------------------*/