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