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