2009-06-11 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-marshaller.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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "spi-common/spi-dbus.h"
24
25 #include "accessible-register.h"
26 #include "accessible-marshaller.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 /*
31  * Marshals the D-Bus path of an AtkObject into a D-Bus message.
32  *
33  * Unrefs the AtkObject if unref is true.
34  */
35 DBusMessage *
36 spi_dbus_return_object (DBusMessage *message, AtkObject *obj, gboolean unref)
37 {
38   DBusMessage *reply;
39   gchar *path;
40
41   path = atk_dbus_object_to_path (obj);
42
43   if (obj && unref)
44     g_object_unref (obj);
45
46   if (!path)
47     path = g_strdup (SPI_DBUS_PATH_NULL);
48
49   reply = dbus_message_new_method_return (message);
50   if (reply)
51     {
52       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path,
53                                 DBUS_TYPE_INVALID);
54     }
55
56   g_free (path);
57
58   return reply;
59 }
60
61 /*---------------------------------------------------------------------------*/
62
63 /*
64  * Marshals a variant containing the D-Bus path of an AtkObject into a D-Bus
65  * message.
66  *
67  * Unrefs the object if unref is true.
68  */
69 dbus_bool_t
70 spi_dbus_return_v_object (DBusMessageIter *iter, AtkObject *obj, int unref)
71 {
72   char *path;
73
74   path = atk_dbus_object_to_path (obj);
75
76   if (unref)
77     g_object_unref (obj);
78
79   return droute_return_v_object (iter, path);
80 }
81
82 /*---------------------------------------------------------------------------*/
83
84 static void
85 append_atk_object_interfaces (AtkObject *object, DBusMessageIter *iter)
86 {
87   const gchar *itf;
88
89   itf = SPI_DBUS_INTERFACE_ACCESSIBLE;
90   dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
91
92   if (ATK_IS_ACTION (object))
93     {
94       itf = SPI_DBUS_INTERFACE_ACTION;
95       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
96     }
97
98   if (ATK_IS_COMPONENT (object))
99     {
100       itf = SPI_DBUS_INTERFACE_COMPONENT;
101       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
102     }
103
104   if (ATK_IS_EDITABLE_TEXT (object))
105     {
106       itf = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
107       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
108     }
109
110   if (ATK_IS_TEXT (object))
111     {
112       itf = SPI_DBUS_INTERFACE_TEXT;
113       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
114     }
115
116   if (ATK_IS_HYPERTEXT (object))
117     {
118       itf = SPI_DBUS_INTERFACE_HYPERTEXT;
119       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
120     }
121
122   if (ATK_IS_IMAGE (object))
123     {
124       itf = SPI_DBUS_INTERFACE_IMAGE;
125       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
126     }
127
128   if (ATK_IS_SELECTION (object))
129     {
130       itf = SPI_DBUS_INTERFACE_SELECTION;
131       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
132     }
133
134   if (ATK_IS_TABLE (object))
135     {
136       itf = SPI_DBUS_INTERFACE_TABLE;
137       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
138     }
139
140   if (ATK_IS_VALUE (object))
141     {
142       itf = SPI_DBUS_INTERFACE_VALUE;
143       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
144     }
145
146   if (ATK_IS_STREAMABLE_CONTENT (object))
147     {
148       itf = "org.freedesktop.atspi.StreamableContent";
149       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
150     }
151
152   if (ATK_IS_DOCUMENT (object))
153     {
154       itf = "org.freedesktop.atspi.Collection";
155       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
156       itf = SPI_DBUS_INTERFACE_DOCUMENT;
157       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
158     }
159
160   if (ATK_IS_HYPERLINK_IMPL (object))
161     {
162       itf = SPI_DBUS_INTERFACE_HYPERLINK;
163       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
164     }
165 }
166
167 /*---------------------------------------------------------------------------*/
168
169 /*
170  * Marshals the given AtkObject into the provided D-Bus iterator.
171  *
172  * The object is marshalled including all its client side cache data.
173  * The format of the structure is (ooaoassusau).
174  * This is used in the updateTree signal and the getTree method
175  * of the org.freedesktop.atspi.Tree interface.
176  *
177  * To marshal an object its parent, and all its children must already
178  * be registered with D-Bus and have been given a D-Bus object path.
179  */
180 void
181 spi_atk_append_accessible(AtkObject *obj, gpointer iter)
182 {
183   DBusMessageIter *iter_array;
184   DBusMessageIter iter_struct, iter_sub_array;
185   dbus_int32_t states [2];
186   int count;
187
188   const char *name, *desc;
189   int i;
190   dbus_uint32_t role;
191   GSList *l;
192
193   iter_array = (DBusMessageIter *) iter;
194
195   dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
196     {
197       AtkObject *parent;
198       gchar *path, *path_parent;
199
200       /* Marshall object path */
201       path = atk_dbus_object_to_path (obj);
202       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
203
204       /* Marshall parent */
205       parent = atk_object_get_parent(obj);
206       if (parent == NULL)
207         {
208           path_parent = atk_dbus_desktop_object_path ();
209         }
210       else
211         {
212           path_parent = atk_dbus_object_to_path (parent);
213           if (!path_parent)
214             {
215               /* This should only happen if a widget is re-parented to
216                * an AtkObject that has not been registered and is then
217                * updated. Ideally objects would be de-registered when
218                * they are removed from a registered tree object, but
219                * this would invalidate a huge amount of cache when
220                * re-parenting.
221                */
222 #if SPI_ATK_DEBUG
223               g_warning ("AT-SPI: Registered accessible marshalled when parent not registered");
224 #endif
225               path_parent = atk_dbus_desktop_object_path ();
226             }
227         }
228       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
229       g_free(path_parent);
230
231       /* Marshall children */
232       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o", &iter_sub_array);
233         {
234           gint childcount, i;
235
236           childcount = atk_object_get_n_accessible_children (obj);
237           for (i = 0; i < childcount; i++)
238             {
239               AtkObject *child;
240               gchar *child_path;
241
242               child = atk_object_ref_accessible_child (obj, i);
243               child_path = atk_dbus_object_to_path (child);
244               if (child_path)
245                 {
246                   dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_OBJECT_PATH, &child_path);
247                   g_free (child_path);
248                 }
249               g_object_unref(G_OBJECT(child));
250             }
251         }
252       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
253
254       /* Marshall interfaces */
255       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s", &iter_sub_array);
256       append_atk_object_interfaces (obj, &iter_sub_array);
257       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
258
259       /* Marshall name */
260       name = atk_object_get_name (obj);
261       if (!name)
262         name = "";
263       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
264
265       /* Marshall role */
266       role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
267       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
268
269       /* Marshall description */
270       desc = atk_object_get_description (obj);
271       if (!desc)
272         desc = "";
273       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
274
275       g_free(path);
276
277       /* Marshall state set */
278       spi_atk_state_to_dbus_array (obj, &states);
279       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u", &iter_sub_array);
280       for (count = 0; count < 2; count++)
281         {
282           dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32, &states[count]);
283         }
284       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
285     }
286   dbus_message_iter_close_container (iter_array, &iter_struct);
287 }
288
289 void
290 spi_atk_append_attribute_set (DBusMessageIter *iter, AtkAttributeSet *attr)
291 {
292   DBusMessageIter dictIter;
293
294   dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{ss}", &dictIter);
295   spi_atk_append_attribute_set_inner (&dictIter, attr);
296   dbus_message_iter_close_container (iter, &dictIter);
297 }
298
299 void
300 spi_atk_append_attribute_set_inner (DBusMessageIter *iter, AtkAttributeSet *attr)
301 {
302   DBusMessageIter dictEntryIter;
303
304   while (attr)
305     {
306       AtkAttribute *attribute = (AtkAttribute *) attr->data;
307       dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &dictEntryIter);
308       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->name);
309       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->value);
310       dbus_message_iter_close_container (iter, &dictEntryIter);
311       attr = g_slist_next (attr);
312     }
313 }
314
315 /*END------------------------------------------------------------------------*/
316