Added a new boolean parameter to allow specifying when a call to
[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 "common/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   DBusError error;
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   dbus_error_init (&error);
63   if (!dbus_message_get_args
64       (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
65        DBUS_TYPE_INVALID))
66     {
67       return droute_invalid_arguments_error (message);
68     }
69   atk_object = atk_selection_ref_selection (selection, selectedChildIndex);
70   return spi_object_return_reference (message, atk_object, TRUE);
71 }
72
73 static DBusMessage *
74 impl_SelectChild (DBusConnection * bus, DBusMessage * message,
75                   void *user_data)
76 {
77   AtkSelection *selection = (AtkSelection *) user_data;
78   DBusError error;
79   dbus_int32_t childIndex;
80   dbus_bool_t rv;
81   DBusMessage *reply;
82
83   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
84                         droute_not_yet_handled_error (message));
85   dbus_error_init (&error);
86   if (!dbus_message_get_args
87       (message, &error, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
88     {
89       return droute_invalid_arguments_error (message);
90     }
91   rv = atk_selection_add_selection (selection, childIndex);
92   reply = dbus_message_new_method_return (message);
93   if (reply)
94     {
95       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
96                                 DBUS_TYPE_INVALID);
97     }
98   return reply;
99 }
100
101 static DBusMessage *
102 impl_DeselectSelectedChild (DBusConnection * bus, DBusMessage * message,
103                             void *user_data)
104 {
105   AtkSelection *selection = (AtkSelection *) user_data;
106   DBusError error;
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   dbus_error_init (&error);
114   if (!dbus_message_get_args
115       (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
116        DBUS_TYPE_INVALID))
117     {
118       return droute_invalid_arguments_error (message);
119     }
120   rv = atk_selection_remove_selection (selection, selectedChildIndex);
121   reply = dbus_message_new_method_return (message);
122   if (reply)
123     {
124       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
125                                 DBUS_TYPE_INVALID);
126     }
127   return reply;
128 }
129
130 static DBusMessage *
131 impl_IsChildSelected (DBusConnection * bus, DBusMessage * message,
132                       void *user_data)
133 {
134   AtkSelection *selection = (AtkSelection *) user_data;
135   DBusError error;
136   dbus_int32_t childIndex;
137   dbus_bool_t rv;
138   DBusMessage *reply;
139
140   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
141                         droute_not_yet_handled_error (message));
142   dbus_error_init (&error);
143   if (!dbus_message_get_args
144       (message, &error, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
145     {
146       return droute_invalid_arguments_error (message);
147     }
148   rv = atk_selection_is_child_selected (selection, childIndex);
149   reply = dbus_message_new_method_return (message);
150   if (reply)
151     {
152       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
153                                 DBUS_TYPE_INVALID);
154     }
155   return reply;
156 }
157
158 static DBusMessage *
159 impl_SelectAll (DBusConnection * bus, DBusMessage * message, void *user_data)
160 {
161   AtkSelection *selection = (AtkSelection *) user_data;
162   dbus_bool_t rv;
163   DBusMessage *reply;
164
165   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
166                         droute_not_yet_handled_error (message));
167   rv = atk_selection_select_all_selection (selection);
168   reply = dbus_message_new_method_return (message);
169   if (reply)
170     {
171       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
172                                 DBUS_TYPE_INVALID);
173     }
174   return reply;
175 }
176
177 static DBusMessage *
178 impl_ClearSelection (DBusConnection * bus, DBusMessage * message,
179                      void *user_data)
180 {
181   AtkSelection *selection = (AtkSelection *) user_data;
182   dbus_bool_t rv;
183   DBusMessage *reply;
184
185   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
186                         droute_not_yet_handled_error (message));
187   rv = atk_selection_clear_selection (selection);
188   reply = dbus_message_new_method_return (message);
189   if (reply)
190     {
191       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
192                                 DBUS_TYPE_INVALID);
193     }
194   return reply;
195 }
196
197 static DBusMessage *
198 impl_DeselectChild (DBusConnection * bus, DBusMessage * message,
199                     void *user_data)
200 {
201   AtkSelection *selection = (AtkSelection *) user_data;
202   DBusError error;
203   dbus_int32_t selectedChildIndex;
204   dbus_bool_t rv = FALSE;
205   gint i, nselected;
206   DBusMessage *reply;
207
208   g_return_val_if_fail (ATK_IS_SELECTION (user_data),
209                         droute_not_yet_handled_error (message));
210   dbus_error_init (&error);
211   if (!dbus_message_get_args
212       (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
213        DBUS_TYPE_INVALID))
214     {
215       return droute_invalid_arguments_error (message);
216     }
217   nselected = atk_selection_get_selection_count (selection);
218   for (i = 0; i < nselected; ++i)
219     {
220       AtkObject *selected_obj = atk_selection_ref_selection (selection, i);
221       if (atk_object_get_index_in_parent (selected_obj) == selectedChildIndex)
222         {
223           g_object_unref (G_OBJECT (selected_obj));
224           rv = atk_selection_remove_selection (selection, i);
225           break;
226         }
227       g_object_unref (G_OBJECT (selected_obj));
228     }
229   reply = dbus_message_new_method_return (message);
230   if (reply)
231     {
232       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
233                                 DBUS_TYPE_INVALID);
234     }
235   return reply;
236 }
237
238 static DRouteMethod methods[] = {
239   {impl_GetSelectedChild, "GetSelectedChild"},
240   {impl_SelectChild, "SelectChild"},
241   {impl_DeselectSelectedChild, "DeselectSelectedChild"},
242   {impl_IsChildSelected, "IsChildSelected"},
243   {impl_SelectAll, "SelectAll"},
244   {impl_ClearSelection, "ClearSelection"},
245   {impl_DeselectChild, "DeselectChild"},
246   {NULL, NULL}
247 };
248
249 static DRouteProperty properties[] = {
250   {impl_get_NSelectedChildren, NULL, "NSelectedChildren"},
251   {NULL, NULL, NULL}
252 };
253
254 void
255 spi_initialize_selection (DRoutePath * path)
256 {
257   droute_path_add_interface (path,
258                              SPI_DBUS_INTERFACE_SELECTION,
259                              spi_org_a11y_atspi_Selection,
260                              methods, properties);
261 };