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