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