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