2008-12-17 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / document.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  * Copyright 2001, 2002 Sun Microsystems Inc.,
7  * Copyright 2001, 2002 Ximian, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <atk/atk.h>
26 #include <droute/droute.h>
27
28 #include "spi-common/spi-dbus.h"
29
30 static DBusMessage *
31 impl_getLocale (DBusConnection *bus,
32                 DBusMessage *message,
33                 void *user_data)
34 {
35   AtkDocument *document = (AtkDocument *) user_data;
36   const gchar *lc;
37   DBusMessage *reply;
38
39   g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
40                         droute_not_yet_handled_error (message));
41   lc = atk_document_get_locale (document);
42   if (!lc)
43     lc = "";
44   reply = dbus_message_new_method_return (message);
45   if (reply)
46     {
47       dbus_message_append_args (reply, DBUS_TYPE_STRING, &lc,
48                                 DBUS_TYPE_INVALID);
49     }
50   return reply;
51 }
52
53 static DBusMessage *
54 impl_getAttributeValue (DBusConnection * bus, DBusMessage * message,
55                         void *user_data)
56 {
57   AtkDocument *document = (AtkDocument *) user_data;
58   DBusError error;
59   gchar *attributename;
60   const gchar *atr;
61   DBusMessage *reply;
62
63   g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
64                         droute_not_yet_handled_error (message));
65   dbus_error_init (&error);
66   if (!dbus_message_get_args
67       (message, &error, DBUS_TYPE_STRING, &attributename, DBUS_TYPE_INVALID))
68     {
69       return SPI_DBUS_RETURN_ERROR (message, &error);
70     }
71   atr = atk_document_get_attribute_value (document, attributename);
72   if (!atr)
73     atr = "";
74   reply = dbus_message_new_method_return (message);
75   if (reply)
76     {
77       dbus_message_append_args (reply, DBUS_TYPE_STRING, &atr,
78                                 DBUS_TYPE_INVALID);
79     }
80   return reply;
81 }
82
83 static DBusMessage *
84 impl_getAttributes (DBusConnection * bus, DBusMessage * message,
85                     void *user_data)
86 {
87   AtkDocument *document = (AtkDocument *) user_data;
88   DBusMessage *reply;
89   AtkAttributeSet *attributes;
90   AtkAttribute *attr = NULL;
91   char **retval;
92   gint n_attributes = 0;
93   gint i;
94
95   g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
96                         droute_not_yet_handled_error (message));
97
98   attributes = atk_document_get_attributes (document);
99   if (attributes)
100     n_attributes = g_slist_length (attributes);
101
102   retval = (char **) g_malloc (n_attributes * sizeof (char *));
103
104   for (i = 0; i < n_attributes; ++i)
105     {
106       attr = g_slist_nth_data (attributes, i);
107       retval[i] = g_strconcat (attr->name, ":", attr->value, NULL);
108     }
109   if (attributes)
110     atk_attribute_set_free (attributes);
111   reply = dbus_message_new_method_return (message);
112   if (reply)
113     {
114       dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
115                                 &retval, n_attributes, DBUS_TYPE_INVALID);
116     }
117   for (i = 0; i < n_attributes; i++)
118     g_free (retval[i]);
119   g_free (retval);
120   return reply;
121 }
122
123 static DRouteMethod methods[] = {
124   {impl_getLocale, "getLocale"},
125   {impl_getAttributeValue, "getAttributeValue"},
126   {impl_getAttributes, "getAttributes"},
127   {NULL, NULL}
128 };
129
130 void
131 spi_initialize_document (DRoutePath *path)
132 {
133   droute_path_add_interface (path,
134                              SPI_DBUS_INTERFACE_DOCUMENT,
135                              methods,
136                              NULL);
137 };