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