More error handling fixes
[platform/upstream/at-spi2-core.git] / atspi / atspi-collection.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2007 IBM Corp.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "atspi-private.h"
24
25 /* TODO: Improve documentation and implement some missing functions */
26
27 /**
28  * atspi_collection_is_ancester_of:
29  *
30  * @collection: The #AtspiCollection to test against.
31  * @test: The #AtspiAccessible to test.
32  *
33  * Returns: TRUE if @collection is an ancestor of @test; FALSE otherwise.
34  *
35  * Not yet implemented.
36  **/
37 gboolean
38 atspi_collection_is_ancestor_of (AtspiCollection *collection,
39                                  AtspiAccessible *test,
40                                  GError **error)
41 {
42   g_warning ("Atspi: TODO: Implement is_ancester_of");
43   return FALSE;
44 }
45
46 static DBusMessage *
47 new_message (AtspiCollection *collection, char *method)
48 {
49   AtspiAccessible *accessible;
50
51   if (!collection)
52     return NULL;
53
54   accessible = ATSPI_ACCESSIBLE (collection);
55   return dbus_message_new_method_call (accessible->parent.app->bus_name,
56                                        accessible->parent.path,
57                                        atspi_interface_collection,
58                                        method);
59 }
60
61 static gboolean
62 append_match_rule (DBusMessage *message, AtspiMatchRule *rule)
63 {
64   DBusMessageIter iter;
65
66   dbus_message_iter_init_append (message, &iter);
67   return _atspi_match_rule_marshal (rule, &iter);
68 }
69
70 static gboolean
71 append_accessible (DBusMessage *message, AtspiAccessible *accessible)
72 {
73   DBusMessageIter iter;
74
75   dbus_message_iter_init_append (message, &iter);
76   dbus_message_iter_append_basic (&iter, DBUS_TYPE_OBJECT_PATH,
77                                   &accessible->parent.path);
78 }
79
80 static GArray *
81 return_accessibles (DBusMessage *message)
82 {
83   DBusMessageIter iter, iter_array;
84   GArray *ret = g_array_new (TRUE, TRUE, sizeof (AtspiAccessible *));
85
86   _ATSPI_DBUS_CHECK_SIG (message, "a(so)", NULL, NULL);
87
88   dbus_message_iter_init (message, &iter);
89   dbus_message_iter_recurse (&iter, &iter_array);
90
91   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
92   {
93     AtspiAccessible *accessible;
94     GArray *new_array;
95     accessible = _atspi_dbus_return_accessible_from_iter (&iter_array);
96     new_array = g_array_append_val (ret, accessible);
97     if (new_array)
98       ret = new_array;
99     /* Iter was moved already, so no need to call dbus_message_iter_next */
100   }
101   dbus_message_unref (message);
102   return ret;
103 }
104
105 /**
106  * atspi_collection_get_matches:
107  *
108  * @collection: The #AtspiCollection.
109  * @rule: A #AtspiMatchRule describing the match criteria.
110  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
111  *          be sorted.
112  * @count: The maximum number of results to return, or 0 for no limit.
113  * @traverse: TODO
114  *
115  * Returns: (element-type AtspiAccessible*) (transfer full): A #GArray of
116  *          #AtspiAccessibles matching the given match rule.
117  **/
118 GArray *
119 atspi_collection_get_matches (AtspiCollection *collection,
120                               AtspiMatchRule *rule,
121                               AtspiCollectionSortOrder sortby,
122                               gint count,
123                               gboolean traverse,
124                               GError **error)
125 {
126   DBusMessage *message = new_message (collection, "GetMatches");
127   DBusMessage *reply;
128   dbus_int32_t d_sortby = sortby;
129   dbus_int32_t d_count = count;
130   dbus_bool_t d_traverse = traverse;
131
132   if (!message)
133     return NULL;
134
135   if (!append_match_rule (message, rule))
136     return NULL;
137   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
138                             DBUS_TYPE_INT32, &d_count,
139                             DBUS_TYPE_BOOLEAN, &d_traverse,
140                             DBUS_TYPE_INVALID);
141   reply = _atspi_dbus_send_with_reply_and_block (message, error);
142   if (!reply)
143     return NULL;
144   return return_accessibles (reply);
145 }
146
147 /**
148  * atspi_collection_get_matches_to:
149  *
150  * @collection: The #AtspiCollection.
151  * @current_object: The object at which to start searching.
152  * @rule: A #AtspiMatchRule describing the match criteria.
153  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
154  *          be sorted.
155  * @tree: An #AtspiCollectionTreeTraversalType specifying restrictions on
156  *        the objects to be traversed.
157  * @recurse: TODO
158  * @count: The maximum number of results to return, or 0 for no limit.
159  * @traverse: TODO
160  *
161  * Returns: (element-type AtspiAccessible*) (transfer full): A #GArray of
162  *          #AtspiAccessibles matching the given match rule after
163  *          @current_object.
164  **/
165 GArray *
166 atspi_collection_get_matches_to (AtspiCollection *collection,
167                               AtspiAccessible *current_object,
168                               AtspiMatchRule *rule,
169                               AtspiCollectionSortOrder sortby,
170                               AtspiCollectionTreeTraversalType tree,
171                               gboolean recurse,
172                               gint count,
173                               gboolean traverse,
174                               GError **error)
175 {
176   DBusMessage *message = new_message (collection, "GetMatchesTo");
177   DBusMessage *reply;
178   dbus_int32_t d_sortby = sortby;
179   dbus_int32_t d_tree = tree;
180   dbus_bool_t d_recurse = recurse;
181   dbus_int32_t d_count = count;
182   dbus_bool_t d_traverse = traverse;
183
184   if (!message)
185     return NULL;
186
187   if (!append_accessible (message, current_object))
188     return NULL;
189   if (!append_match_rule (message, rule))
190     return NULL;
191   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
192                                      DBUS_TYPE_UINT32, &d_tree,
193                             DBUS_TYPE_BOOLEAN, &d_recurse,
194                             DBUS_TYPE_INT32, &d_count,
195                             DBUS_TYPE_BOOLEAN, &d_traverse,
196                             DBUS_TYPE_INVALID);
197   reply = _atspi_dbus_send_with_reply_and_block (message, error);
198   if (!reply)
199     return NULL;
200   return return_accessibles (reply);
201 }
202
203 /**
204  * atspi_collection_get_matches_from:
205  *
206  * @collection: The #AtspiCollection.
207  * @current_object: Upon reaching this object, searching should stop.
208  * @rule: A #AtspiMatchRule describing the match criteria.
209  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
210  *          be sorted.
211  * @tree: An #AtspiCollectionTreeTraversalType specifying restrictions on
212  *        the objects to be traversed.
213  * @count: The maximum number of results to return, or 0 for no limit.
214  * @traverse: TODO
215  *
216  * Returns: (element-type AtspiAccessible*) (transfer full): A #GArray of
217  *          #AtspiAccessibles matching the given match rule that preceed
218  *          @current_object.
219  **/
220 GArray *
221 atspi_collection_get_matches_from (AtspiCollection *collection,
222                               AtspiAccessible *current_object,
223                               AtspiMatchRule *rule,
224                               AtspiCollectionSortOrder sortby,
225                               AtspiCollectionTreeTraversalType tree,
226                               gint count,
227                               gboolean traverse,
228                               GError **error)
229 {
230   DBusMessage *message = new_message (collection, "GetMatchesFrom");
231   DBusMessage *reply;
232   dbus_int32_t d_sortby = sortby;
233   dbus_int32_t d_tree = tree;
234   dbus_int32_t d_count = count;
235   dbus_bool_t d_traverse = traverse;
236
237   if (!message)
238     return NULL;
239
240   if (!append_accessible (message, current_object))
241     return NULL;
242   if (!append_match_rule (message, rule))
243     return NULL;
244   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
245                             DBUS_TYPE_UINT32, &d_tree,
246                             DBUS_TYPE_INT32, &d_count,
247                             DBUS_TYPE_BOOLEAN, &d_traverse,
248                             DBUS_TYPE_INVALID);
249   reply = _atspi_dbus_send_with_reply_and_block (message, error);
250   if (!reply)
251     return NULL;
252   return return_accessibles (reply);
253 }
254
255 /**
256  * atspi_collection_get_active_descendant:
257  * 
258  * @collection: The #AtspiCollection to query.
259  *
260  * Returns: (transfer full): The active descendant of #collection.
261  *
262  * Not yet implemented.
263  **/
264 AtspiAccessible *
265 atspi_collection_get_active_descendant (AtspiCollection *collection, GError **error)
266 {
267   g_warning ("atspi: TODO: Implement get_active_descendants");
268   return NULL;
269 }
270
271 static void
272 atspi_collection_base_init (AtspiCollection *klass)
273 {
274 }
275
276 GType
277 atspi_collection_get_type (void)
278 {
279   static GType type = 0;
280
281   if (!type) {
282     static const GTypeInfo tinfo =
283     {
284       sizeof (AtspiCollection),
285       (GBaseInitFunc) atspi_collection_base_init,
286       (GBaseFinalizeFunc) NULL,
287     };
288
289     type = g_type_register_static (G_TYPE_INTERFACE, "AtspiCollection", &tinfo, 0);
290
291   }
292   return type;
293 }