For atspi_collection_get_matches_to, rename restrict to limit_scope
[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   if (!accessible->parent.app)
53     return NULL;
54   return dbus_message_new_method_call (accessible->parent.app->bus_name,
55                                        accessible->parent.path,
56                                        atspi_interface_collection,
57                                        method);
58 }
59
60 static gboolean
61 append_match_rule (DBusMessage *message, AtspiMatchRule *rule)
62 {
63   DBusMessageIter iter;
64
65   dbus_message_iter_init_append (message, &iter);
66   return _atspi_match_rule_marshal (rule, &iter);
67 }
68
69 static gboolean
70 append_accessible (DBusMessage *message, AtspiAccessible *accessible)
71 {
72   DBusMessageIter iter;
73
74   dbus_message_iter_init_append (message, &iter);
75   dbus_message_iter_append_basic (&iter, DBUS_TYPE_OBJECT_PATH,
76                                   &accessible->parent.path);
77   return TRUE;  /* TODO: Check for out-of-memory */
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: A pointer to the #AtspiCollection to query.
109  *
110  * @rule: An #AtspiMatchRule describing the match criteria.
111  *
112  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
113  *          be sorted.
114  *
115  * @count: The maximum number of results to return, or 0 for no limit.
116  *
117  * @traverse: Not supported.
118  *
119  * Gets all #AtspiAccessible objects from the @collection matching a given
120  * @rule.  
121  *
122  * Returns: (element-type AtspiAccessible*) (transfer full): All 
123  *          #AtspiAccessible objects matching the given match rule.
124  **/
125 GArray *
126 atspi_collection_get_matches (AtspiCollection *collection,
127                               AtspiMatchRule *rule,
128                               AtspiCollectionSortOrder sortby,
129                               gint count,
130                               gboolean traverse,
131                               GError **error)
132 {
133   DBusMessage *message = new_message (collection, "GetMatches");
134   DBusMessage *reply;
135   dbus_int32_t d_sortby = sortby;
136   dbus_int32_t d_count = count;
137   dbus_bool_t d_traverse = traverse;
138
139   if (!message)
140     return NULL;
141
142   if (!append_match_rule (message, rule))
143     return NULL;
144   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
145                             DBUS_TYPE_INT32, &d_count,
146                             DBUS_TYPE_BOOLEAN, &d_traverse,
147                             DBUS_TYPE_INVALID);
148   reply = _atspi_dbus_send_with_reply_and_block (message, error);
149   if (!reply)
150     return NULL;
151   return return_accessibles (reply);
152 }
153
154 /**
155  * atspi_collection_get_matches_to:
156  *
157  * @collection: A pointer to the #AtspiCollection to query.
158  *
159  * @current_object: The object at which to start searching.
160  *
161  * @rule: An #AtspiMatchRule describing the match criteria.
162  *
163  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
164  *          be sorted.
165  *
166  * @tree: An #AtspiCollectionTreeTraversalType specifying restrictions on
167  *        the objects to be traversed.
168  *
169  * @limit_scope: If #TRUE, only descendants of @current_object's parent
170  * will be returned. Otherwise (if #FALSE), any accessible may be returned
171  * if it would preceed @current_object in a flattened hierarchy.
172  *
173  * @count: The maximum number of results to return, or 0 for no limit.
174  *
175  * @traverse: Not supported.
176  *
177  * Gets all #AtspiAccessible objects from the @collection, after 
178  * @current_object, matching a given @rule.  
179  *
180  * Returns: (element-type AtspiAccessible*) (transfer full): All
181  *          #AtspiAccessible objects matching the given match rule after
182  *          @current_object.
183  **/
184 GArray *
185 atspi_collection_get_matches_to (AtspiCollection *collection,
186                               AtspiAccessible *current_object,
187                               AtspiMatchRule *rule,
188                               AtspiCollectionSortOrder sortby,
189                               AtspiCollectionTreeTraversalType tree,
190                               gboolean limit_scope,
191                               gint count,
192                               gboolean traverse,
193                               GError **error)
194 {
195   DBusMessage *message = new_message (collection, "GetMatchesTo");
196   DBusMessage *reply;
197   dbus_int32_t d_sortby = sortby;
198   dbus_int32_t d_tree = tree;
199   dbus_bool_t d_limit_scope = limit_scope;
200   dbus_int32_t d_count = count;
201   dbus_bool_t d_traverse = traverse;
202
203   if (!message)
204     return NULL;
205
206   if (!append_accessible (message, current_object))
207     return NULL;
208   if (!append_match_rule (message, rule))
209     return NULL;
210   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
211                                      DBUS_TYPE_UINT32, &d_tree,
212                             DBUS_TYPE_BOOLEAN, &d_limit_scope,
213                             DBUS_TYPE_INT32, &d_count,
214                             DBUS_TYPE_BOOLEAN, &d_traverse,
215                             DBUS_TYPE_INVALID);
216   reply = _atspi_dbus_send_with_reply_and_block (message, error);
217   if (!reply)
218     return NULL;
219   return return_accessibles (reply);
220 }
221
222 /**
223  * atspi_collection_get_matches_from:
224  *
225  * @collection: A pointer to the #AtspiCollection to query.
226  *
227  * @current_object: Upon reaching this object, searching should stop.
228  *
229  * @rule: An #AtspiMatchRule describing the match criteria.
230  *
231  * @sortby: An #AtspiCollectionSortOrder specifying the way the results are to
232  *          be sorted.
233  *
234  * @tree: An #AtspiCollectionTreeTraversalType specifying restrictions on
235  *        the objects to be traversed.
236  *
237  * @count: The maximum number of results to return, or 0 for no limit.
238  *
239  * @traverse: Not supported.
240  *
241  * Gets all #AtspiAccessible objects from the @collection, before  
242  * @current_object, matching a given @rule.  
243  *
244  * Returns: (element-type AtspiAccessible*) (transfer full): All 
245  *          #AtspiAccessible objects matching the given match rule that preceed
246  *          @current_object.
247  **/
248 GArray *
249 atspi_collection_get_matches_from (AtspiCollection *collection,
250                               AtspiAccessible *current_object,
251                               AtspiMatchRule *rule,
252                               AtspiCollectionSortOrder sortby,
253                               AtspiCollectionTreeTraversalType tree,
254                               gint count,
255                               gboolean traverse,
256                               GError **error)
257 {
258   DBusMessage *message = new_message (collection, "GetMatchesFrom");
259   DBusMessage *reply;
260   dbus_int32_t d_sortby = sortby;
261   dbus_int32_t d_tree = tree;
262   dbus_int32_t d_count = count;
263   dbus_bool_t d_traverse = traverse;
264
265   if (!message)
266     return NULL;
267
268   if (!append_accessible (message, current_object))
269     return NULL;
270   if (!append_match_rule (message, rule))
271     return NULL;
272   dbus_message_append_args (message, DBUS_TYPE_UINT32, &d_sortby,
273                             DBUS_TYPE_UINT32, &d_tree,
274                             DBUS_TYPE_INT32, &d_count,
275                             DBUS_TYPE_BOOLEAN, &d_traverse,
276                             DBUS_TYPE_INVALID);
277   reply = _atspi_dbus_send_with_reply_and_block (message, error);
278   if (!reply)
279     return NULL;
280   return return_accessibles (reply);
281 }
282
283 /**
284  * atspi_collection_get_active_descendant:
285  *
286  * Not yet implemented.
287  **/
288 AtspiAccessible *
289 atspi_collection_get_active_descendant (AtspiCollection *collection, GError **error)
290 {
291   g_warning ("atspi: TODO: Implement get_active_descendants");
292   return NULL;
293 }
294
295 static void
296 atspi_collection_base_init (AtspiCollection *klass)
297 {
298 }
299
300 GType
301 atspi_collection_get_type (void)
302 {
303   static GType type = 0;
304
305   if (!type) {
306     static const GTypeInfo tinfo =
307     {
308       sizeof (AtspiCollection),
309       (GBaseInitFunc) atspi_collection_base_init,
310       (GBaseFinalizeFunc) NULL,
311     };
312
313     type = g_type_register_static (G_TYPE_INTERFACE, "AtspiCollection", &tinfo, 0);
314
315   }
316   return type;
317 }