2008-05-16 Mark Doffman <mark.doffman@codethink.co.uk>
[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 GHashTable *path2ptr;
30 static guint objindex;
31
32 static void
33 deregister_object (gpointer data, GObject *obj)
34 {
35   spi_dbus_notify_remove(ATK_OBJECT(obj), NULL);
36   g_hash_table_remove (path2ptr, &obj);
37 }
38
39 static guint
40 register_object (GObject * obj)
41 {
42   gint *new_int;
43
44   if (!path2ptr)
45     {
46       path2ptr = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
47       if (!path2ptr)
48         return ++objindex;
49     }
50   objindex++;
51   while (g_hash_table_lookup (path2ptr, &objindex))
52     {
53       objindex++;
54       /* g_object_get_data returning 0 means no data, so handle wrap-around */
55       if (objindex == 0)
56         objindex++;
57     }
58   new_int = (gint *)g_malloc(sizeof(gint));
59   if (new_int)
60   {
61     *new_int = objindex;
62     g_hash_table_insert (path2ptr, new_int, obj);
63   }
64   g_object_set_data (G_OBJECT (obj), "dbus-id", (gpointer) objindex);
65   g_object_weak_ref(G_OBJECT(obj), deregister_object, NULL);
66   spi_dbus_notify_change(obj, TRUE, NULL);
67   return objindex;
68 }
69
70 AtkObject *
71 spi_dbus_get_object (const char *path)
72 {
73   guint index;
74   void *data;
75
76   g_assert (path);
77   if (strncmp(path, "/org/freedesktop/atspi/accessible", 33) != 0) return NULL;
78   path += 33;   /* skip over preamble */
79   if (path[0] == '\0') return atk_get_root();
80   if (path[0] != '/') return NULL;
81   path++;
82   index = atoi (path);
83   data = g_hash_table_lookup (path2ptr, &index);
84   if (data)
85     return ATK_OBJECT (data);
86   return NULL;
87 }
88
89 gchar *
90 spi_dbus_get_path (AtkObject * obj)
91 {
92   if (!obj) return NULL;
93   guint index = (guint) g_object_get_data (G_OBJECT (obj), "dbus-id");
94   if (!index)
95     index = register_object (obj);
96   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", index);
97 }
98
99 /* Reply with the given object and dereference it if unref is TRUE */
100 DBusMessage *
101 spi_dbus_return_object (DBusMessage * message, AtkObject * obj, int unref)
102 {
103   DBusMessage *reply;
104   const char *path = spi_dbus_get_path (obj);
105   if (unref)
106     g_object_unref (obj);
107   if (!path)
108     {
109       /* Should we have a more specific error for this? */
110       return spi_dbus_general_error (message);
111     }
112   reply = dbus_message_new_method_return (message);
113   if (reply)
114     {
115       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
116                                 DBUS_TYPE_INVALID);
117     }
118   return reply;
119 }
120
121 dbus_bool_t
122 spi_dbus_return_v_object (DBusMessageIter * iter, AtkObject * obj, int unref)
123 {
124   const char *path = spi_dbus_get_path (obj);
125   if (unref)
126     g_object_unref (obj);
127   if (!path)
128     return FALSE;
129   return droute_return_v_object (iter, path);
130 }
131
132 void
133 spi_dbus_initialize (DRouteData * data)
134 {
135   spi_initialize_accessible (data);
136   spi_initialize_action(data);
137   spi_initialize_component (data);
138   spi_initialize_document (data);
139   spi_initialize_editabletext (data);
140   spi_initialize_hyperlink (data);
141   spi_initialize_hypertext (data);
142   spi_initialize_image (data);
143   spi_initialize_selection (data);
144   spi_initialize_table (data);
145   spi_initialize_text (data);
146   spi_initialize_value (data);
147   spi_initialize_introspectable(data, (DRouteGetDatumFunction) spi_dbus_get_object);
148 }
149
150 static GString *
151 spi_get_tree (AtkObject * obj, GString * str, DRouteData * data)
152 {
153   int role;
154   const char *name;
155   gchar *path;
156   GSList *l;
157   gint childcount;
158   gint i;
159
160   if (!obj)
161     return NULL;
162   role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));;
163   name = atk_object_get_name (obj);
164   if (!name)
165     name = "";
166   path = spi_dbus_get_path (obj);
167   g_string_append_printf (str,
168                           "<object path=\"%s\" name=\"%s\" role=\"%d\">\n",
169                           path, name, role);
170   for (l = data->interfaces; l; l = g_slist_next (l))
171     {
172       DRouteInterface *iface_def = (DRouteInterface *) l->data;
173       void *datum = NULL;
174       if (iface_def->get_datum)
175         datum = (*iface_def->get_datum) (path, data->user_data);
176       if (datum)
177         {
178           g_string_append_printf (str, "<interface name=\"%s\"/>\n",
179                                   iface_def->name);
180           if (iface_def->free_datum)
181             (*iface_def->free_datum) (datum);
182         }
183     }
184   childcount = atk_object_get_n_accessible_children (obj);
185   for (i = 0; i < childcount; i++)
186     {
187       AtkObject *child = atk_object_ref_accessible_child (obj, i);
188       str = spi_get_tree (child, str, data);
189       g_object_unref (child);
190     }
191   str = g_string_append (str, "</object>\n");
192   return str;
193 }