ac5e3c2c372874de58cb69423f7a4112c98d04ed
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / atk-dbus.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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "accessible.h"
28
29 extern GHashTable *path2ptr;
30
31 AtkObject *
32 spi_dbus_get_object (const char *path)
33 {
34   guint index;
35   void *data;
36
37   g_assert (path);
38   if (strncmp(path, "/org/freedesktop/atspi/accessible", 33) != 0) return NULL;
39   path += 33;   /* skip over preamble */
40   if (path[0] == '\0') return atk_get_root();
41   if (path[0] != '/') return NULL;
42   path++;
43   index = atoi (path);
44   data = g_hash_table_lookup (path2ptr, &index);
45   if (data)
46     return ATK_OBJECT (data);
47   return NULL;
48 }
49
50 gchar *
51 spi_dbus_get_path (AtkObject * obj)
52 {
53   if (!obj) return NULL;
54   guint index = (guint) g_object_get_data (G_OBJECT (obj), "dbus-id");
55   if (!index)
56     index = register_object (obj);
57   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", index);
58 }
59
60 /* Reply with the given object and dereference it if unref is TRUE */
61 DBusMessage *
62 spi_dbus_return_object (DBusMessage * message, AtkObject * obj, int unref)
63 {
64   DBusMessage *reply;
65   const char *path = spi_dbus_get_path (obj);
66   if (unref)
67     g_object_unref (obj);
68   if (!path)
69     {
70       /* Should we have a more specific error for this? */
71       return spi_dbus_general_error (message);
72     }
73   reply = dbus_message_new_method_return (message);
74   if (reply)
75     {
76       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
77                                 DBUS_TYPE_INVALID);
78     }
79   return reply;
80 }
81
82 dbus_bool_t
83 spi_dbus_return_v_object (DBusMessageIter * iter, AtkObject * obj, int unref)
84 {
85   const char *path = spi_dbus_get_path (obj);
86   if (unref)
87     g_object_unref (obj);
88   if (!path)
89     return FALSE;
90   return droute_return_v_object (iter, path);
91 }
92
93 void
94 spi_dbus_initialize (DRouteData * data)
95 {
96   spi_initialize_accessible (data);
97   spi_initialize_action(data);
98   spi_initialize_component (data);
99   spi_initialize_document (data);
100   spi_initialize_editabletext (data);
101   spi_initialize_hyperlink (data);
102   spi_initialize_hypertext (data);
103   spi_initialize_image (data);
104   spi_initialize_selection (data);
105   spi_initialize_table (data);
106   spi_initialize_text (data);
107   spi_initialize_value (data);
108   spi_initialize_introspectable(data);
109 }
110
111 static GString *
112 spi_get_tree (AtkObject * obj, GString * str, DRouteData * data)
113 {
114   int role;
115   const char *name;
116   gchar *path;
117   GSList *l;
118   gint childcount;
119   gint i;
120
121   if (!obj)
122     return NULL;
123   role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));;
124   name = atk_object_get_name (obj);
125   if (!name)
126     name = "";
127   path = spi_dbus_get_path (obj);
128   g_string_append_printf (str,
129                           "<object path=\"%s\" name=\"%s\" role=\"%d\">\n",
130                           path, name, role);
131   for (l = data->interfaces; l; l = g_slist_next (l))
132     {
133       DRouteInterface *iface_def = (DRouteInterface *) l->data;
134       void *datum = NULL;
135       if (iface_def->get_datum)
136         datum = (*iface_def->get_datum) (path, data->user_data);
137       if (datum)
138         {
139           g_string_append_printf (str, "<interface name=\"%s\"/>\n",
140                                   iface_def->name);
141           if (iface_def->free_datum)
142             (*iface_def->free_datum) (datum);
143         }
144     }
145   childcount = atk_object_get_n_accessible_children (obj);
146   for (i = 0; i < childcount; i++)
147     {
148       AtkObject *child = atk_object_ref_accessible_child (obj, i);
149       str = spi_get_tree (child, str, data);
150       g_object_unref (child);
151     }
152   str = g_string_append (str, "</object>\n");
153   return str;
154 }