cff97faf742ed39e319d66e99ee3a553693c73ac
[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 "accessible-register.h"
24 #include "accessible-marshaller.h"
25
26 #include "spi-common/spi-dbus.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 (unref)
44     g_object_unref (obj);
45
46   reply = dbus_message_new_method_return (message);
47   if (reply)
48     {
49       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
50                                 DBUS_TYPE_INVALID);
51     }
52   return reply;
53 }
54
55 /*---------------------------------------------------------------------------*/
56
57 /*
58  * Marshals a variant containing the D-Bus path of an AtkObject into a D-Bus
59  * message.
60  *
61  * Unrefs the object if unref is true.
62  */
63 dbus_bool_t
64 spi_dbus_return_v_object (DBusMessageIter *iter, AtkObject *obj, int unref)
65 {
66   char *path;
67
68   path = atk_dbus_object_to_path (obj);
69
70   if (unref)
71     g_object_unref (obj);
72
73   return droute_return_v_object (iter, path);
74 }
75
76 /*---------------------------------------------------------------------------*/
77
78 static void
79 append_atk_object_interfaces (AtkObject *object, DBusMessageIter *iter)
80 {
81   const gchar *itf;
82
83   itf = SPI_DBUS_INTERFACE_ACCESSIBLE;
84   dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
85
86   if (ATK_IS_ACTION (object))
87     {
88       itf = SPI_DBUS_INTERFACE_ACTION;
89       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
90     }
91
92   if (ATK_IS_COMPONENT (object))
93     {
94       itf = SPI_DBUS_INTERFACE_COMPONENT;
95       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
96     }
97
98   if (ATK_IS_EDITABLE_TEXT (object))
99     {
100       itf = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
101       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
102     }
103
104   if (ATK_IS_TEXT (object))
105     {
106       itf = SPI_DBUS_INTERFACE_TEXT;
107       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
108     }
109
110   if (ATK_IS_HYPERTEXT (object))
111     {
112       itf = SPI_DBUS_INTERFACE_HYPERTEXT;
113       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
114     }
115
116   if (ATK_IS_IMAGE (object))
117     {
118       itf = SPI_DBUS_INTERFACE_IMAGE;
119       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
120     }
121
122   if (ATK_IS_SELECTION (object))
123     {
124       itf = SPI_DBUS_INTERFACE_SELECTION;
125       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
126     }
127
128   if (ATK_IS_TABLE (object))
129     {
130       itf = SPI_DBUS_INTERFACE_TABLE;
131       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
132     }
133
134   if (ATK_IS_VALUE (object))
135     {
136       itf = SPI_DBUS_INTERFACE_VALUE;
137       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
138     }
139
140   if (ATK_IS_STREAMABLE_CONTENT (object))
141     {
142       itf = "org.freedesktop.atspi.StreamableContent";
143       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
144     }
145
146   if (ATK_IS_DOCUMENT (object))
147     {
148       itf = "org.freedesktop.atspi.Collection";
149       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
150       itf = SPI_DBUS_INTERFACE_DOCUMENT;
151       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
152     }
153
154   if (ATK_IS_HYPERLINK_IMPL (object))
155     {
156       itf = SPI_DBUS_INTERFACE_HYPERLINK;
157       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &itf);
158     }
159 }
160
161 /*---------------------------------------------------------------------------*/
162
163 /*
164  * Marshals the given AtkObject into the provided D-Bus iterator.
165  *
166  * The object is marshalled including all its client side cache data.
167  * The format of the structure is (ooaoassusau).
168  * This is used in the updateTree signal and the getTree method
169  * of the org.freedesktop.atspi.Tree interface.
170  *
171  * To marshal an object its parent, and all its children must already
172  * be registered with D-Bus and have been given a D-Bus object path.
173  */
174 void
175 spi_atk_append_accessible(AtkObject *obj, gpointer iter)
176 {
177   DBusMessageIter *iter_array;
178   DBusMessageIter iter_struct, iter_sub_array;
179   dbus_int32_t states [2];
180   int count;
181
182   const char *name, *desc;
183   int i;
184   dbus_uint32_t role;
185   GSList *l;
186
187   iter_array = (DBusMessageIter *) iter;
188
189   dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
190     {
191       AtkObject *parent;
192       gchar *path, *path_parent;
193
194       /* Marshall object path */
195       path = atk_dbus_object_to_path (obj);
196       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
197
198       /* Marshall parent */
199       parent = atk_object_get_parent(obj);
200       if (parent == NULL)
201         {
202           path_parent = g_strdup("/");
203         }
204       else
205         {
206           path_parent = atk_dbus_object_to_path (parent);
207           if (!path_parent)
208             {
209               g_critical ("AT-SPI: Object registered without registering parent");
210               path_parent = g_strdup("/");
211             }
212         }
213       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
214       g_free(path_parent);
215
216       /* Marshall children */
217       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o", &iter_sub_array);
218         {
219           gint childcount, i;
220
221           childcount = atk_object_get_n_accessible_children (obj);
222           for (i = 0; i < childcount; i++)
223             {
224               AtkObject *child;
225               gchar *child_path;
226
227               child = atk_object_ref_accessible_child (obj, i);
228               child_path = atk_dbus_object_to_path (child);
229               g_object_unref(G_OBJECT(child));
230               if (G_LIKELY (child_path))
231                 {
232                   dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_OBJECT_PATH, &child_path);
233                 }
234               else
235                 {
236                   g_critical ("AT-SPI: Child object exists in accessible tree but has not been registered");
237                   g_free (child_path);
238                 }
239             }
240         }
241       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
242
243       /* Marshall interfaces */
244       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s", &iter_sub_array);
245       append_atk_object_interfaces (obj, &iter_sub_array);
246       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
247
248       /* Marshall name */
249       name = atk_object_get_name (obj);
250       if (!name)
251         name = "";
252       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
253
254       /* Marshall role */
255       role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
256       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
257
258       /* Marshall description */
259       desc = atk_object_get_description (obj);
260       if (!desc)
261         desc = "";
262       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
263
264       g_free(path);
265
266       /* Marshall state set */
267       spi_atk_state_to_dbus_array (obj, &states);
268       dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u", &iter_sub_array);
269       for (count = 0; count < 2; count++)
270         {
271           dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32, &states[count]);
272         }
273       dbus_message_iter_close_container (&iter_struct, &iter_sub_array);
274     }
275   dbus_message_iter_close_container (iter_array, &iter_struct);
276 }
277
278 /*END------------------------------------------------------------------------*/
279