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