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