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