Ensure that DBus errors are freed
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / adaptors / selection-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 #include "object.h"
30 #include "introspection.h"
31
32 static dbus_bool_t
33 impl_get_NSelectedChildren (DBusMessageIter * iter, void *user_data)
34 {
35   AtkSelection *selection = (AtkSelection *) user_data;
36   g_return_val_if_fail (ATK_IS_SELECTION (user_data), FALSE);
37   return droute_return_v_int32 (iter,
38                                 atk_selection_get_selection_count
39                                 (selection));
40 }
41
42 /*static char *
43 impl_get_NSelectedChildren_str (void *datum)
44 {
45   g_return_val_if_fail (ATK_IS_SELECTION (user_data), FALSE);
46   return g_strdup_printf ("%d",
47                           atk_selection_get_selection_count ((AtkSelection *)
48                                                              datum));
49 }*/
50
51 static DBusMessage *
52 impl_GetSelectedChild (DBusConnection * bus, DBusMessage * message,
53                        void *user_data)
54 {
55   AtkSelection *selection = (AtkSelection *) user_data;
56   DBusMessage *reply;
57   dbus_int32_t selectedChildIndex;
58   AtkObject *atk_object;
59
60   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
61                         droute_not_yet_handled_error (message));
62   if (!dbus_message_get_args
63       (message, NULL, DBUS_TYPE_INT32, &selectedChildIndex,
64        DBUS_TYPE_INVALID))
65     {
66       return droute_invalid_arguments_error (message);
67     }
68   atk_object = atk_selection_ref_selection (selection, selectedChildIndex);
69   reply = spi_object_return_reference (message, atk_object);
70   if (atk_object)
71     g_object_unref (atk_object);
72
73   return reply;
74 }
75
76 static DBusMessage *
77 impl_SelectChild (DBusConnection * bus, DBusMessage * message,
78                   void *user_data)
79 {
80   AtkSelection *selection = (AtkSelection *) user_data;
81   dbus_int32_t childIndex;
82   dbus_bool_t rv;
83   DBusMessage *reply;
84
85   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
86                         droute_not_yet_handled_error (message));
87   if (!dbus_message_get_args
88       (message, NULL, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
89     {
90       return droute_invalid_arguments_error (message);
91     }
92   rv = atk_selection_add_selection (selection, childIndex);
93   reply = dbus_message_new_method_return (message);
94   if (reply)
95     {
96       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
97                                 DBUS_TYPE_INVALID);
98     }
99   return reply;
100 }
101
102 static DBusMessage *
103 impl_DeselectSelectedChild (DBusConnection * bus, DBusMessage * message,
104                             void *user_data)
105 {
106   AtkSelection *selection = (AtkSelection *) user_data;
107   dbus_int32_t selectedChildIndex;
108   dbus_bool_t rv;
109   DBusMessage *reply;
110
111   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
112                         droute_not_yet_handled_error (message));
113   if (!dbus_message_get_args
114       (message, NULL, DBUS_TYPE_INT32, &selectedChildIndex,
115        DBUS_TYPE_INVALID))
116     {
117       return droute_invalid_arguments_error (message);
118     }
119   rv = atk_selection_remove_selection (selection, selectedChildIndex);
120   reply = dbus_message_new_method_return (message);
121   if (reply)
122     {
123       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
124                                 DBUS_TYPE_INVALID);
125     }
126   return reply;
127 }
128
129 static DBusMessage *
130 impl_IsChildSelected (DBusConnection * bus, DBusMessage * message,
131                       void *user_data)
132 {
133   AtkSelection *selection = (AtkSelection *) user_data;
134   dbus_int32_t childIndex;
135   dbus_bool_t rv;
136   DBusMessage *reply;
137
138   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
139                         droute_not_yet_handled_error (message));
140   if (!dbus_message_get_args
141       (message, NULL, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
142     {
143       return droute_invalid_arguments_error (message);
144     }
145   rv = atk_selection_is_child_selected (selection, childIndex);
146   reply = dbus_message_new_method_return (message);
147   if (reply)
148     {
149       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
150                                 DBUS_TYPE_INVALID);
151     }
152   return reply;
153 }
154
155 static DBusMessage *
156 impl_SelectAll (DBusConnection * bus, DBusMessage * message, void *user_data)
157 {
158   AtkSelection *selection = (AtkSelection *) user_data;
159   dbus_bool_t rv;
160   DBusMessage *reply;
161
162   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
163                         droute_not_yet_handled_error (message));
164   rv = atk_selection_select_all_selection (selection);
165   reply = dbus_message_new_method_return (message);
166   if (reply)
167     {
168       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
169                                 DBUS_TYPE_INVALID);
170     }
171   return reply;
172 }
173
174 static DBusMessage *
175 impl_ClearSelection (DBusConnection * bus, DBusMessage * message,
176                      void *user_data)
177 {
178   AtkSelection *selection = (AtkSelection *) user_data;
179   dbus_bool_t rv;
180   DBusMessage *reply;
181
182   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
183                         droute_not_yet_handled_error (message));
184   rv = atk_selection_clear_selection (selection);
185   reply = dbus_message_new_method_return (message);
186   if (reply)
187     {
188       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
189                                 DBUS_TYPE_INVALID);
190     }
191   return reply;
192 }
193
194 static DBusMessage *
195 impl_DeselectChild (DBusConnection * bus, DBusMessage * message,
196                     void *user_data)
197 {
198   AtkSelection *selection = (AtkSelection *) user_data;
199   dbus_int32_t selectedChildIndex;
200   dbus_bool_t rv = FALSE;
201   gint i, nselected;
202   DBusMessage *reply;
203
204   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
205                         droute_not_yet_handled_error (message));
206   if (!dbus_message_get_args
207       (message, NULL, DBUS_TYPE_INT32, &selectedChildIndex,
208        DBUS_TYPE_INVALID))
209     {
210       return droute_invalid_arguments_error (message);
211     }
212   nselected = atk_selection_get_selection_count (selection);
213   for (i = 0; i < nselected; ++i)
214     {
215       AtkObject *selected_obj = atk_selection_ref_selection (selection, i);
216       if (atk_object_get_index_in_parent (selected_obj) == selectedChildIndex)
217         {
218           g_object_unref (G_OBJECT (selected_obj));
219           rv = atk_selection_remove_selection (selection, i);
220           break;
221         }
222       g_object_unref (G_OBJECT (selected_obj));
223     }
224   reply = dbus_message_new_method_return (message);
225   if (reply)
226     {
227       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
228                                 DBUS_TYPE_INVALID);
229     }
230   return reply;
231 }
232
233 static DRouteMethod methods[] = {
234   {impl_GetSelectedChild, "GetSelectedChild"},
235   {impl_SelectChild, "SelectChild"},
236   {impl_DeselectSelectedChild, "DeselectSelectedChild"},
237   {impl_IsChildSelected, "IsChildSelected"},
238   {impl_SelectAll, "SelectAll"},
239   {impl_ClearSelection, "ClearSelection"},
240   {impl_DeselectChild, "DeselectChild"},
241   {NULL, NULL}
242 };
243
244 static DRouteProperty properties[] = {
245   {impl_get_NSelectedChildren, NULL, "NSelectedChildren"},
246   {NULL, NULL, NULL}
247 };
248
249 void
250 spi_initialize_selection (DRoutePath * path)
251 {
252   droute_path_add_interface (path,
253                              ATSPI_DBUS_INTERFACE_SELECTION,
254                              spi_org_a11y_atspi_Selection,
255                              methods, properties);
256 };