b715e03d1f354d2f9f9bc131a065f8558e75d03f
[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 <droute/droute.h>
24
25 #include "common/spi-dbus.h"
26 #include "common/spi-stateset.h"
27
28 #include "accessible-register.h"
29 #include "accessible-marshaller.h"
30 #include "bridge.h"
31
32 #include "adaptors.h"
33
34 /*---------------------------------------------------------------------------*/
35
36 void
37 spi_dbus_append_name_and_path_inner (DBusMessageIter *iter, const char *bus_name, const char *path)
38 {
39   DBusMessageIter iter_struct;
40
41   if (!bus_name)
42     bus_name = "";
43   if (!path)
44     path = SPI_DBUS_PATH_NULL;
45
46   dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, NULL, &iter_struct);
47   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &bus_name);
48   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
49   dbus_message_iter_close_container (iter, &iter_struct);
50 }
51
52 extern gchar *atspi_dbus_name;
53
54 void
55 spi_dbus_append_name_and_path (DBusMessage *message, DBusMessageIter *iter, AtkObject *obj, gboolean do_register, gboolean unref)
56 {
57   gchar *path;
58   DBusMessageIter iter_struct;
59
60   path = atk_dbus_object_to_path (obj, do_register);
61
62   spi_dbus_append_name_and_path_inner (iter, atspi_dbus_name, path);
63
64   g_free (path);
65   if (obj && unref)
66     g_object_unref (obj);
67 }
68
69 /*
70  * Marshals the D-Bus path of an AtkObject into a D-Bus message.
71  *
72  * Unrefs the AtkObject if unref is true.
73  */
74 DBusMessage *
75 spi_dbus_return_object (DBusMessage *message, AtkObject *obj, gboolean do_register, gboolean unref)
76 {
77   DBusMessage *reply;
78   reply = dbus_message_new_method_return (message);
79   if (reply)
80     {
81       DBusMessageIter iter;
82       dbus_message_iter_init_append (reply, &iter);
83       spi_dbus_append_name_and_path (message, &iter, obj, do_register, unref);
84     }
85
86   return reply;
87 }
88
89 DBusMessage *
90 spi_dbus_return_hyperlink (DBusMessage *message, AtkHyperlink *link, AtkObject *container, gboolean unref)
91 {
92   return spi_dbus_return_sub_object (message, G_OBJECT (link), G_OBJECT (container), unref);
93 }
94
95 DBusMessage *
96 spi_dbus_return_sub_object (DBusMessage *message, GObject *sub, GObject *container, gboolean unref)
97 {
98   DBusMessage *reply;
99   gchar *path;
100
101   path = atk_dbus_sub_object_to_path (sub, container);
102
103   if (sub && unref)
104     g_object_unref (sub);
105
106   if (!path)
107     path = g_strdup (SPI_DBUS_PATH_NULL);
108
109   reply = dbus_message_new_method_return (message);
110   if (reply)
111     {
112       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path,
113                                 DBUS_TYPE_INVALID);
114     }
115
116   g_free (path);
117
118   return reply;
119 }
120
121 /*---------------------------------------------------------------------------*/
122
123 /*
124  * Marshals a variant containing the D-Bus path of an AtkObject into a D-Bus
125  * message.
126  *
127  * Unrefs the object if unref is true.
128  */
129 dbus_bool_t
130 spi_dbus_return_v_object (DBusMessageIter *iter, AtkObject *obj, int unref)
131 {
132   DBusMessageIter iter_variant;
133   char *path;
134
135   path = atk_dbus_object_to_path (obj, FALSE);
136
137   if (!path)
138     path = g_strdup (SPI_DBUS_PATH_NULL);
139
140   if (unref)
141     g_object_unref (obj);
142
143   dbus_message_iter_open_container (iter, DBUS_TYPE_VARIANT, "(so)", &iter_variant);
144   spi_dbus_append_name_and_path_inner (&iter_variant, NULL, path);
145   dbus_message_iter_close_container (iter, &iter_variant);
146   return TRUE;
147 }
148
149 /*---------------------------------------------------------------------------*/
150
151 void
152 append_atk_object_interfaces (AtkObject *object, DBusMessageIter *iter)
153 {
154   const gchar *itf;
155
156   itf = SPI_DBUS_INTERFACE_ACCESSIBLE;
157   dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
158
159   if (ATK_IS_ACTION (object))
160     {
161       itf = SPI_DBUS_INTERFACE_ACTION;
162       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
163     }
164
165   if (ATK_IS_COMPONENT (object))
166     {
167       itf = SPI_DBUS_INTERFACE_COMPONENT;
168       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
169     }
170
171   if (ATK_IS_EDITABLE_TEXT (object))
172     {
173       itf = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
174       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
175     }
176
177   if (ATK_IS_TEXT (object))
178     {
179       itf = SPI_DBUS_INTERFACE_TEXT;
180       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
181     }
182
183   if (ATK_IS_HYPERTEXT (object))
184     {
185       itf = SPI_DBUS_INTERFACE_HYPERTEXT;
186       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
187     }
188
189   if (ATK_IS_IMAGE (object))
190     {
191       itf = SPI_DBUS_INTERFACE_IMAGE;
192       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
193     }
194
195   if (ATK_IS_SELECTION (object))
196     {
197       itf = SPI_DBUS_INTERFACE_SELECTION;
198       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
199     }
200
201   if (ATK_IS_TABLE (object))
202     {
203       itf = SPI_DBUS_INTERFACE_TABLE;
204       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
205     }
206
207   if (ATK_IS_VALUE (object))
208     {
209       itf = SPI_DBUS_INTERFACE_VALUE;
210       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
211     }
212
213   if (ATK_IS_STREAMABLE_CONTENT (object))
214     {
215       itf = "org.freedesktop.atspi.StreamableContent";
216       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
217     }
218
219   if (ATK_IS_DOCUMENT (object))
220     {
221       itf = "org.freedesktop.atspi.Collection";
222       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
223       itf = SPI_DBUS_INTERFACE_DOCUMENT;
224       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
225     }
226
227   if (ATK_IS_HYPERLINK_IMPL (object))
228     {
229       itf = SPI_DBUS_INTERFACE_HYPERLINK;
230       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
231     }
232 }
233
234 /*---------------------------------------------------------------------------*/
235
236 /*
237  * Marshals the given AtkObject into the provided D-Bus iterator.
238  *
239  * The object is marshalled including all its client side cache data.
240  * The format of the structure is (o(so)a(so)assusau).
241  * This is used in the updateTree signal and the GetTree method
242  * of the org.freedesktop.atspi.Tree interface.
243  *
244  * To marshal an object its parent, and all its children must already
245  * be registered with D-Bus and have been given a D-Bus object path.
246  */
247 void
248 spi_atk_append_accessible(AtkObject *obj, gpointer data)
249 {
250   DBusMessageIter iter_struct, iter_sub_array;
251   dbus_uint32_t states [2];
252   int count;
253   AtkStateSet *set;
254   DBusMessageIter *iter_array = (DBusMessageIter *)data;
255
256   const char *name, *desc;
257   dbus_uint32_t role;
258
259   set = atk_object_ref_state_set (obj);
260     {
261       AtkObject *parent;
262       gchar *path;
263       gchar *bus_parent = NULL, *path_parent = NULL;
264
265       /* Marshall object path */
266       path = atk_dbus_object_to_path (obj, FALSE);
267
268       role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
269
270       /* Marshall parent */
271       parent = atk_object_get_parent(obj);
272       if (parent == NULL)
273         {
274           /* TODO: Support getting parent of an AtkPlug */
275 #ifdef __ATK_PLUG_H__
276           if (ATK_IS_PLUG (obj))
277             {
278               char *id = g_object_get_data (G_OBJECT (obj), "dbus-plug-parent");
279               if (id)
280                 bus_parent = g_strdup (id);
281               if (bus_parent && (path_parent = g_utf8_strchr (bus_parent + 1, -1, ':')))
282                 {
283                   *(path_parent++) = '\0';
284                   /* path_parent is going to be freed, so dup it */
285                   path_parent = g_strdup (path_parent);
286                 }
287             }
288           else if (role != Accessibility_ROLE_APPLICATION)
289 #else
290           if (role != Accessibility_ROLE_APPLICATION)
291 #endif
292             path_parent = g_strdup (SPI_DBUS_PATH_NULL);
293           else
294             path_parent = atk_dbus_desktop_object_path ();
295         }
296       else
297         {
298           path_parent = atk_dbus_object_to_path (parent, FALSE);
299           if (!path_parent)
300             {
301               /* This should only happen if a widget is re-parented to
302                * an AtkObject that has not been registered and is then
303                * updated. Ideally objects would be de-registered when
304                * they are removed from a registered tree object, but
305                * this would invalidate a huge amount of cache when
306                * re-parenting.
307                */
308 #if SPI_ATK_DEBUG
309               g_warning ("AT-SPI: Registered accessible marshalled when parent not registered");
310 #endif
311               path_parent = atk_dbus_desktop_object_path ();
312             }
313         }
314
315       dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
316       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
317       spi_dbus_append_name_and_path_inner (&iter_struct, bus_parent, path_parent);
318       g_free(path_parent);
319       g_free (bus_parent);
320
321       /* Marshall children */
322       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "(so)", &iter_sub_array);
323       if (!atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
324         {
325           gint childcount, i;
326
327           childcount = atk_object_get_n_accessible_children (obj);
328           for (i = 0; i < childcount; i++)
329             {
330               AtkObject *child;
331               gchar *child_path;
332
333               child = atk_object_ref_accessible_child (obj, i);
334               child_path = atk_dbus_object_to_path (child, FALSE);
335               if (child_path)
336                 {
337                   spi_dbus_append_name_and_path_inner (&iter_sub_array, NULL, child_path);
338                   g_free (child_path);
339                 }
340               g_object_unref(G_OBJECT(child));
341             }
342         }
343 #ifdef __ATK_PLUG_H__
344       if (ATK_IS_SOCKET (obj) && atk_socket_is_occupied (ATK_SOCKET(obj)))
345         {
346           AtkSocket *socket = ATK_SOCKET(obj);
347           gchar *child_name, *child_path;
348           child_name = g_strdup (socket->embedded_plug_id);
349           child_path = g_utf8_strchr (child_name + 1, -1, ':');
350           if (child_path)
351             {
352               *(child_path++) = '\0';
353               spi_dbus_append_name_and_path_inner (&iter_sub_array, child_name, child_path);
354             }
355           g_free (child_name);
356         }
357 #endif
358
359       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
360
361       /* Marshall interfaces */
362       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s", &iter_sub_array);
363       append_atk_object_interfaces (obj, &iter_sub_array);
364       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
365
366       /* Marshall name */
367       name = atk_object_get_name (obj);
368       if (!name)
369         name = "";
370       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
371
372       /* Marshall role */
373       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
374
375       /* Marshall description */
376       desc = atk_object_get_description (obj);
377       if (!desc)
378         desc = "";
379       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
380
381       g_free(path);
382
383       /* Marshall state set */
384       spi_atk_state_set_to_dbus_array (set, states);
385       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u", &iter_sub_array);
386       for (count = 0; count < 2; count++)
387         {
388           dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32, &states[count]);
389         }
390       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
391     }
392   dbus_message_iter_close_container (iter_array, &iter_struct);
393   g_object_unref (set);
394 }
395
396 void
397 spi_atk_append_attribute_set (DBusMessageIter *iter, AtkAttributeSet *attr)
398 {
399   DBusMessageIter dictIter;
400
401   dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{ss}", &dictIter);
402   spi_atk_append_attribute_set_inner (&dictIter, attr);
403   dbus_message_iter_close_container (iter, &dictIter);
404 }
405
406 void
407 spi_atk_append_attribute_set_inner (DBusMessageIter *iter, AtkAttributeSet *attr)
408 {
409   DBusMessageIter dictEntryIter;
410
411   while (attr)
412     {
413       AtkAttribute *attribute = (AtkAttribute *) attr->data;
414       dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &dictEntryIter);
415       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->name);
416       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->value);
417       dbus_message_iter_close_container (iter, &dictEntryIter);
418       attr = g_slist_next (attr);
419     }
420 }
421
422 /*END------------------------------------------------------------------------*/
423