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