Implement GetLocalizedName for actions
[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 "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_localized_name (DBusConnection * bus, DBusMessage * message, void *user_data)
102 {
103   DBusMessage *reply;
104   DBusError error;
105   dbus_int32_t index;
106   const char *name;
107   AtkAction *action = (AtkAction *) user_data;
108
109   dbus_error_init (&error);
110   g_return_val_if_fail (ATK_IS_ACTION (user_data),
111                         droute_not_yet_handled_error (message));
112   if (!dbus_message_get_args
113       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
114     {
115       return droute_invalid_arguments_error (message);
116     }
117   name = atk_action_get_localized_name (action, index);
118   if (!name)
119     name = "";
120   reply = dbus_message_new_method_return (message);
121   if (reply)
122     {
123       dbus_message_append_args (reply, DBUS_TYPE_STRING, &name,
124                                 DBUS_TYPE_INVALID);
125     }
126   return reply;
127 }
128
129 static DBusMessage *
130 impl_get_keybinding (DBusConnection * bus, DBusMessage * message,
131                      void *user_data)
132 {
133   DBusMessage *reply;
134   DBusError error;
135   dbus_int32_t index;
136   const char *kb;
137   AtkAction *action = (AtkAction *) user_data;
138
139   dbus_error_init (&error);
140   g_return_val_if_fail (ATK_IS_ACTION (user_data),
141                         droute_not_yet_handled_error (message));
142   if (!dbus_message_get_args
143       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
144     {
145       return droute_invalid_arguments_error (message);
146     }
147   kb = atk_action_get_keybinding (action, index);
148   if (!kb)
149     kb = "";
150   reply = dbus_message_new_method_return (message);
151   if (reply)
152     {
153       dbus_message_append_args (reply, DBUS_TYPE_STRING, &kb,
154                                 DBUS_TYPE_INVALID);
155     }
156   return reply;
157 }
158
159 static DBusMessage *
160 impl_GetActions (DBusConnection * bus, DBusMessage * message, void *user_data)
161 {
162   AtkAction *action = (AtkAction *) user_data;
163   DBusMessage *reply;
164   gint count;
165   gint i;
166   DBusMessageIter iter, iter_array, iter_struct;
167
168   g_return_val_if_fail (ATK_IS_ACTION (user_data),
169                         droute_not_yet_handled_error (message));
170   count = atk_action_get_n_actions (action);
171   reply = dbus_message_new_method_return (message);
172   if (!reply)
173     goto oom;
174   dbus_message_iter_init_append (reply, &iter);
175   if (!dbus_message_iter_open_container
176       (&iter, DBUS_TYPE_ARRAY, "(sss)", &iter_array))
177     goto oom;
178   for (i = 0; i < count; i++)
179     {
180       const char *name = atk_action_get_name (action, i);
181       const char *lname = atk_action_get_localized_name (action, i);
182       const char *desc = atk_action_get_description (action, i);
183       const char *kb = atk_action_get_keybinding (action, i);
184       if (!name)
185         name = "";
186       if (!lname)
187         lname = "";
188       if (!desc)
189         desc = "";
190       if (!kb)
191         kb = "";
192       if (!dbus_message_iter_open_container
193           (&iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct))
194         goto oom;
195       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
196       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &lname);
197       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
198       dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &kb);
199       if (!dbus_message_iter_close_container (&iter_array, &iter_struct))
200         goto oom;
201     }
202   if (!dbus_message_iter_close_container (&iter, &iter_array))
203     goto oom;
204   return reply;
205 oom:
206   // TODO: handle out-of-memory
207   return reply;
208 }
209
210 static DBusMessage *
211 impl_DoAction (DBusConnection * bus, DBusMessage * message, void *user_data)
212 {
213   AtkAction *action = (AtkAction *) user_data;
214   DBusError error;
215   dbus_int32_t index;
216   dbus_bool_t rv = TRUE;
217   DBusMessage *reply;
218
219   g_return_val_if_fail (ATK_IS_ACTION (user_data),
220                         droute_not_yet_handled_error (message));
221   dbus_error_init (&error);
222   if (!dbus_message_get_args
223       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
224     {
225       return droute_invalid_arguments_error (message);
226     }
227   reply = dbus_message_new_method_return (message);
228   if (reply)
229     {
230       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
231                                 DBUS_TYPE_INVALID);
232     }
233   dbus_connection_send (bus, reply, NULL);
234   dbus_message_unref (reply);
235   atk_action_do_action (action, index);
236   return NULL;
237 }
238
239 DRouteMethod methods[] = {
240   {impl_get_description, "GetDescription"}
241   ,
242   {impl_get_name, "GetName"}
243   ,
244   {impl_get_localized_name, "GetLocalizedName"}
245   ,
246   {impl_get_keybinding, "GetKeyBinding"}
247   ,
248   {impl_GetActions, "GetActions"}
249   ,
250   {impl_DoAction, "DoAction"}
251   ,
252   {NULL, NULL}
253 };
254
255 static DRouteProperty properties[] = {
256   {impl_get_NActions, NULL, "NActions"},
257   {NULL, NULL}
258 };
259
260 void
261 spi_initialize_action (DRoutePath * path)
262 {
263   droute_path_add_interface (path,
264                              ATSPI_DBUS_INTERFACE_ACTION,
265                              spi_org_a11y_atspi_Action, methods, properties);
266 };