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