62f68f7ab03bd5d1e2c0dacc8084b77e3bfb0255
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / adaptors / action-adaptor.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 "common/spi-dbus.h"
29
30 #include "introspection.h"
31
32 static dbus_bool_t
33 impl_get_NActions (DBusMessageIter * iter, void *user_data)
34 {
35   AtkAction *action = (AtkAction *) user_data;
36
37   g_return_val_if_fail (ATK_IS_ACTION (user_data), FALSE);
38   return droute_return_v_int32 (iter, atk_action_get_n_actions (action));
39 }
40
41 static DBusMessage *
42 impl_get_description (DBusConnection * bus, DBusMessage * message,
43                       void *user_data)
44 {
45   AtkAction *action = (AtkAction *) user_data;
46   DBusError error;
47   DBusMessage *reply;
48   dbus_int32_t index;
49   const char *desc;
50
51   dbus_error_init (&error);
52   g_return_val_if_fail (ATK_IS_ACTION (user_data),
53                         droute_not_yet_handled_error (message));
54   if (!dbus_message_get_args
55       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
56     {
57       return droute_invalid_arguments_error (message);
58     }
59   desc = atk_action_get_description (action, index);
60   if (!desc)
61     desc = "";
62   reply = dbus_message_new_method_return (message);
63   if (reply)
64     {
65       dbus_message_append_args (reply, DBUS_TYPE_STRING, &desc,
66                                 DBUS_TYPE_INVALID);
67     }
68   return reply;
69 }
70
71 static DBusMessage *
72 impl_get_name (DBusConnection * bus, DBusMessage * message, void *user_data)
73 {
74   DBusMessage *reply;
75   DBusError error;
76   dbus_int32_t index;
77   const char *name;
78   AtkAction *action = (AtkAction *) user_data;
79
80   dbus_error_init (&error);
81   g_return_val_if_fail (ATK_IS_ACTION (user_data),
82                         droute_not_yet_handled_error (message));
83   if (!dbus_message_get_args
84       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
85     {
86       return droute_invalid_arguments_error (message);
87     }
88   name = atk_action_get_name (action, index);
89   if (!name)
90     name = "";
91   reply = dbus_message_new_method_return (message);
92   if (reply)
93     {
94       dbus_message_append_args (reply, DBUS_TYPE_STRING, &name,
95                                 DBUS_TYPE_INVALID);
96     }
97   return reply;
98 }
99
100 static DBusMessage *
101 impl_get_keybinding (DBusConnection * bus, DBusMessage * message,
102                      void *user_data)
103 {
104   DBusMessage *reply;
105   DBusError error;
106   dbus_int32_t index;
107   const char *kb;
108   AtkAction *action = (AtkAction *) user_data;
109
110   dbus_error_init (&error);
111   g_return_val_if_fail (ATK_IS_ACTION (user_data),
112                         droute_not_yet_handled_error (message));
113   if (!dbus_message_get_args
114       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
115     {
116       return droute_invalid_arguments_error (message);
117     }
118   kb = atk_action_get_keybinding (action, index);
119   if (!kb)
120     kb = "";
121   reply = dbus_message_new_method_return (message);
122   if (reply)
123     {
124       dbus_message_append_args (reply, DBUS_TYPE_STRING, &kb,
125                                 DBUS_TYPE_INVALID);
126     }
127   return reply;
128 }
129
130 static DBusMessage *
131 impl_GetActions (DBusConnection * bus, DBusMessage * message, void *user_data)
132 {
133   AtkAction *action = (AtkAction *) user_data;
134   DBusMessage *reply;
135   gint count;
136   gint i;
137   DBusMessageIter iter, iter_array, iter_struct;
138
139   g_return_val_if_fail (ATK_IS_ACTION (user_data),
140                         droute_not_yet_handled_error (message));
141   count = atk_action_get_n_actions (action);
142   reply = dbus_message_new_method_return (message);
143   if (!reply)
144     goto oom;
145   dbus_message_iter_init_append (reply, &iter);
146   if (!dbus_message_iter_open_container
147       (&iter, DBUS_TYPE_ARRAY, "(sss)", &iter_array))
148     goto oom;
149   for (i = 0; i < count; i++)
150     {
151       const char *name = atk_action_get_name (action, i);
152       const char *desc = atk_action_get_description (action, i);
153       const char *kb = atk_action_get_keybinding (action, i);
154       if (!name)
155         name = "";
156       if (!desc)
157         desc = "";
158       if (!kb)
159         kb = "";
160       if (!dbus_message_iter_open_container
161           (&iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct))
162         goto oom;
163       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
164       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
165       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &kb);
166       if (!dbus_message_iter_close_container (&iter_array, &iter_struct))
167         goto oom;
168     }
169   if (!dbus_message_iter_close_container (&iter, &iter_array))
170     goto oom;
171   return reply;
172 oom:
173   // TODO: handle out-of-memory
174   return reply;
175 }
176
177 static DBusMessage *
178 impl_DoAction (DBusConnection * bus, DBusMessage * message, void *user_data)
179 {
180   AtkAction *action = (AtkAction *) user_data;
181   DBusError error;
182   dbus_int32_t index;
183   dbus_bool_t rv;
184   DBusMessage *reply;
185
186   g_return_val_if_fail (ATK_IS_ACTION (user_data),
187                         droute_not_yet_handled_error (message));
188   dbus_error_init (&error);
189   if (!dbus_message_get_args
190       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
191     {
192       return droute_invalid_arguments_error (message);
193     }
194   rv = atk_action_do_action (action, index);
195   reply = dbus_message_new_method_return (message);
196   if (reply)
197     {
198       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
199                                 DBUS_TYPE_INVALID);
200     }
201   return reply;
202 }
203
204 DRouteMethod methods[] = {
205   {impl_get_description, "GetDescription"}
206   ,
207   {impl_get_name, "GetName"}
208   ,
209   {impl_get_keybinding, "GetKeyBinding"}
210   ,
211   {impl_GetActions, "GetActions"}
212   ,
213   {impl_DoAction, "DoAction"}
214   ,
215   {NULL, NULL}
216 };
217
218 static DRouteProperty properties[] = {
219   {impl_get_NActions, NULL, "NActions"},
220   {NULL, NULL}
221 };
222
223 void
224 spi_initialize_action (DRoutePath * path)
225 {
226   droute_path_add_interface (path,
227                              SPI_DBUS_INTERFACE_ACTION,
228                              spi_org_a11y_atspi_Action, methods, properties);
229 };