Merge branch 'master' into mgorse
[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
44   dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, NULL, &iter_struct);
45   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &bus_name);
46   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
47   dbus_message_iter_close_container (iter, &iter_struct);
48 }
49
50 void
51 spi_dbus_append_name_and_path (DBusMessage *message, DBusMessageIter *iter, AtkObject *obj, gboolean unref)
52 {
53   gchar *path;
54   DBusMessageIter iter_struct;
55   const char *bus_name = dbus_message_get_sender (message);
56
57   path = atk_dbus_object_to_path (obj, FALSE);
58
59   if (!path)
60     path = g_strdup (SPI_DBUS_PATH_NULL);
61
62   spi_dbus_append_name_and_path_inner (iter, bus_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 (message, &iter);
83       spi_dbus_append_name_and_path (message, &iter, obj, 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   char *path;
133
134   path = atk_dbus_object_to_path (obj, FALSE);
135
136   if (!path)
137     path = g_strdup (SPI_DBUS_PATH_NULL);
138
139   if (unref)
140     g_object_unref (obj);
141
142   return droute_return_v_object (iter, path);
143 }
144
145 /*---------------------------------------------------------------------------*/
146
147 void
148 append_atk_object_interfaces (AtkObject *object, DBusMessageIter *iter)
149 {
150   const gchar *itf;
151
152   itf = SPI_DBUS_INTERFACE_ACCESSIBLE;
153   dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
154
155   if (ATK_IS_ACTION (object))
156     {
157       itf = SPI_DBUS_INTERFACE_ACTION;
158       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
159     }
160
161   if (ATK_IS_COMPONENT (object))
162     {
163       itf = SPI_DBUS_INTERFACE_COMPONENT;
164       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
165     }
166
167   if (ATK_IS_EDITABLE_TEXT (object))
168     {
169       itf = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
170       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
171     }
172
173   if (ATK_IS_TEXT (object))
174     {
175       itf = SPI_DBUS_INTERFACE_TEXT;
176       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
177     }
178
179   if (ATK_IS_HYPERTEXT (object))
180     {
181       itf = SPI_DBUS_INTERFACE_HYPERTEXT;
182       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
183     }
184
185   if (ATK_IS_IMAGE (object))
186     {
187       itf = SPI_DBUS_INTERFACE_IMAGE;
188       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
189     }
190
191   if (ATK_IS_SELECTION (object))
192     {
193       itf = SPI_DBUS_INTERFACE_SELECTION;
194       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
195     }
196
197   if (ATK_IS_TABLE (object))
198     {
199       itf = SPI_DBUS_INTERFACE_TABLE;
200       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
201     }
202
203   if (ATK_IS_VALUE (object))
204     {
205       itf = SPI_DBUS_INTERFACE_VALUE;
206       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
207     }
208
209   if (ATK_IS_STREAMABLE_CONTENT (object))
210     {
211       itf = "org.freedesktop.atspi.StreamableContent";
212       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
213     }
214
215   if (ATK_IS_DOCUMENT (object))
216     {
217       itf = "org.freedesktop.atspi.Collection";
218       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
219       itf = SPI_DBUS_INTERFACE_DOCUMENT;
220       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
221     }
222
223   if (ATK_IS_HYPERLINK_IMPL (object))
224     {
225       itf = SPI_DBUS_INTERFACE_HYPERLINK;
226       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
227     }
228 }
229
230 /*---------------------------------------------------------------------------*/
231
232 /*
233  * Marshals the given AtkObject into the provided D-Bus iterator.
234  *
235  * The object is marshalled including all its client side cache data.
236  * The format of the structure is (o(so)a(so)assusau).
237  * This is used in the updateTree signal and the GetTree method
238  * of the org.freedesktop.atspi.Tree interface.
239  *
240  * To marshal an object its parent, and all its children must already
241  * be registered with D-Bus and have been given a D-Bus object path.
242  */
243 void
244 spi_atk_append_accessible(AtkObject *obj, gpointer data)
245 {
246   DBusMessageIter iter_struct, iter_sub_array;
247   dbus_uint32_t states [2];
248   int count;
249   AtkStateSet *set;
250   DBusMessageIter *iter_array = (DBusMessageIter *)data;
251
252   const char *name, *desc;
253   dbus_uint32_t role;
254
255   set = atk_object_ref_state_set (obj);
256   dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
257     {
258       AtkObject *parent;
259       gchar *path, *path_parent;
260
261       /* Marshall object path */
262       path = atk_dbus_object_to_path (obj, FALSE);
263
264       /* Marshall parent */
265       parent = atk_object_get_parent(obj);
266       if (parent == NULL)
267         {
268           path_parent = atk_dbus_desktop_object_path ();
269         }
270       else
271         {
272           path_parent = atk_dbus_object_to_path (parent, FALSE);
273           if (!path_parent)
274             {
275               /* This should only happen if a widget is re-parented to
276                * an AtkObject that has not been registered and is then
277                * updated. Ideally objects would be de-registered when
278                * they are removed from a registered tree object, but
279                * this would invalidate a huge amount of cache when
280                * re-parenting.
281                */
282 #if SPI_ATK_DEBUG
283               g_warning ("AT-SPI: Registered accessible marshalled when parent not registered");
284 #endif
285               path_parent = atk_dbus_desktop_object_path ();
286             }
287         }
288
289       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
290       spi_dbus_append_name_and_path_inner (&iter_struct, NULL, path_parent);
291       g_free(path_parent);
292
293       /* Marshall children */
294       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "(so)", &iter_sub_array);
295       if (!atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
296         {
297           gint childcount, i;
298
299           childcount = atk_object_get_n_accessible_children (obj);
300           for (i = 0; i < childcount; i++)
301             {
302               AtkObject *child;
303               gchar *child_path;
304
305               child = atk_object_ref_accessible_child (obj, i);
306               child_path = atk_dbus_object_to_path (child, FALSE);
307               if (child_path)
308                 {
309                   spi_dbus_append_name_and_path_inner (&iter_sub_array, NULL, child_path);
310                   g_free (child_path);
311                 }
312               g_object_unref(G_OBJECT(child));
313             }
314         }
315       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
316
317       /* Marshall interfaces */
318       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s", &iter_sub_array);
319       append_atk_object_interfaces (obj, &iter_sub_array);
320       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
321
322       /* Marshall name */
323       name = atk_object_get_name (obj);
324       if (!name)
325         name = "";
326       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
327
328       /* Marshall role */
329       role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
330       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
331
332       /* Marshall description */
333       desc = atk_object_get_description (obj);
334       if (!desc)
335         desc = "";
336       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
337
338       g_free(path);
339
340       /* Marshall state set */
341       spi_atk_state_set_to_dbus_array (set, states);
342       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u", &iter_sub_array);
343       for (count = 0; count < 2; count++)
344         {
345           dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32, &states[count]);
346         }
347       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
348     }
349   dbus_message_iter_close_container (iter_array, &iter_struct);
350   g_object_unref (set);
351 }
352
353 void
354 spi_atk_append_attribute_set (DBusMessageIter *iter, AtkAttributeSet *attr)
355 {
356   DBusMessageIter dictIter;
357
358   dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{ss}", &dictIter);
359   spi_atk_append_attribute_set_inner (&dictIter, attr);
360   dbus_message_iter_close_container (iter, &dictIter);
361 }
362
363 void
364 spi_atk_append_attribute_set_inner (DBusMessageIter *iter, AtkAttributeSet *attr)
365 {
366   DBusMessageIter dictEntryIter;
367
368   while (attr)
369     {
370       AtkAttribute *attribute = (AtkAttribute *) attr->data;
371       dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &dictEntryIter);
372       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->name);
373       dbus_message_iter_append_basic (&dictEntryIter, DBUS_TYPE_STRING, &attribute->value);
374       dbus_message_iter_close_container (iter, &dictEntryIter);
375       attr = g_slist_next (attr);
376     }
377 }
378
379 /*END------------------------------------------------------------------------*/
380