introspection: add some missing Returns: (nullable) annotations
[platform/upstream/at-spi2-core.git] / atspi / atspi-accessible.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  * Copyright 2010, 2011 Novell, 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 "atspi-private.h"
26 #include "atspi-accessible-private.h"
27 #include <string.h>
28
29 static gboolean enable_caching = FALSE;
30 static guint quark_locale;
31
32 static void
33 atspi_action_interface_init (AtspiAction *action)
34 {
35 }
36
37 static void
38 atspi_collection_interface_init (AtspiCollection *component)
39 {
40 }
41
42 static void
43 atspi_component_interface_init (AtspiComponent *component)
44 {
45 }
46
47 static void
48 atspi_document_interface_init (AtspiDocument *document)
49 {
50 }
51
52 static void
53 atspi_editable_text_interface_init (AtspiEditableText *editable_text)
54 {
55 }
56
57 static void
58 atspi_hypertext_interface_init (AtspiHypertext *hypertext)
59 {
60 }
61
62 static void
63 atspi_image_interface_init (AtspiImage *image)
64 {
65 }
66
67 static void
68 atspi_selection_interface_init (AtspiSelection *selection)
69 {
70 }
71
72 static void
73 atspi_table_interface_init (AtspiTable *table)
74 {
75 }
76
77 static void
78 atspi_table_cell_interface_init (AtspiTableCell *cell)
79 {
80 }
81
82 static void
83 atspi_text_interface_init (AtspiText *text)
84 {
85 }
86
87 static void
88 atspi_value_interface_init (AtspiValue *value)
89 {
90 }
91
92 G_DEFINE_TYPE_WITH_CODE (AtspiAccessible, atspi_accessible, ATSPI_TYPE_OBJECT,
93                          G_ADD_PRIVATE (AtspiAccessible)
94                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_ACTION, atspi_action_interface_init)
95                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_COLLECTION, atspi_collection_interface_init)
96                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_COMPONENT, atspi_component_interface_init)
97                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_DOCUMENT, atspi_document_interface_init)
98                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_EDITABLE_TEXT, atspi_editable_text_interface_init)
99                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_HYPERTEXT, atspi_hypertext_interface_init)
100                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_IMAGE, atspi_image_interface_init)
101                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_SELECTION, atspi_selection_interface_init)
102                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TABLE, atspi_table_interface_init)
103                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TABLE_CELL, atspi_table_cell_interface_init)
104                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TEXT, atspi_text_interface_init)
105                          G_IMPLEMENT_INTERFACE (ATSPI_TYPE_VALUE, atspi_value_interface_init))
106
107 #ifdef DEBUG_REF_COUNTS
108 static gint accessible_count = 0;
109 #endif
110
111 static void
112 atspi_accessible_init (AtspiAccessible *accessible)
113 {
114 #ifdef DEBUG_REF_COUNTS
115   accessible_count++;
116   g_hash_table_insert (_atspi_get_live_refs (), accessible, NULL);
117   g_print("at-spi: init: %d objects\n", accessible_count);
118 #endif
119
120   accessible->priv = atspi_accessible_get_instance_private (accessible);
121 }
122
123 static void
124 atspi_accessible_dispose (GObject *object)
125 {
126   AtspiAccessible *accessible = ATSPI_ACCESSIBLE (object);
127   AtspiEvent e;
128   AtspiAccessible *parent;
129   GList *children;
130   GList *l;
131
132   /* TODO: Only fire if object not already marked defunct */
133   memset (&e, 0, sizeof (e));
134   e.type = "object:state-changed:defunct";
135   e.source = accessible;
136   e.detail1 = 1;
137   e.detail2 = 0;
138   _atspi_send_event (&e);
139
140   if (accessible->states)
141   {
142     g_object_unref (accessible->states);
143     accessible->states = NULL;
144   }
145
146   parent = accessible->accessible_parent;
147   if (parent && parent->children)
148   {
149     GList*ls = g_list_find (parent->children, accessible);
150     if(ls)
151     {
152       gboolean replace = (ls == parent->children);
153       ls = g_list_remove (ls, accessible);
154       if (replace)
155         parent->children = ls;
156       g_object_unref (object);
157     }
158   }
159
160   if (parent)
161   {
162     g_object_unref (parent);
163     accessible->accessible_parent = NULL;
164   }
165
166   children = accessible->children;
167   accessible->children = NULL;
168   for (l = children; l; l = l->next)
169   {
170     AtspiAccessible *child = l->data;
171     if (child && child->accessible_parent == accessible)
172     {
173       g_object_unref (accessible);
174       child->accessible_parent = NULL;
175     }
176     g_object_unref (child);
177   }
178   g_list_free (children);
179
180   G_OBJECT_CLASS (atspi_accessible_parent_class) ->dispose (object);
181 }
182
183 static void
184 atspi_accessible_finalize (GObject *object)
185 {
186   AtspiAccessible *accessible = ATSPI_ACCESSIBLE (object);
187
188     g_free (accessible->description);
189     g_free (accessible->name);
190   if (accessible->attributes)
191     g_hash_table_unref (accessible->attributes);
192
193 #ifdef DEBUG_REF_COUNTS
194   accessible_count--;
195   g_hash_table_remove (_atspi_get_live_refs (), accessible);
196   g_print ("at-spi: finalize: %d objects\n", accessible_count);
197 #endif
198
199   G_OBJECT_CLASS (atspi_accessible_parent_class)
200     ->finalize (object);
201 }
202
203 static void
204 atspi_accessible_class_init (AtspiAccessibleClass *klass)
205 {
206   GObjectClass *object_class = G_OBJECT_CLASS (klass);
207
208   object_class->dispose = atspi_accessible_dispose;
209   object_class->finalize = atspi_accessible_finalize;
210
211   quark_locale = g_quark_from_string ("accessible-locale");
212 }
213
214 /**
215  * atspi_accessible_get_name:
216  * @obj: a pointer to the #AtspiAccessible object on which to operate.
217  *
218  * Gets the name of an #AtspiAccessible object.
219  *
220  * Returns: a UTF-8 string indicating the name of the #AtspiAccessible object 
221  * or NULL on exception.
222  **/
223 gchar *
224 atspi_accessible_get_name (AtspiAccessible *obj, GError **error)
225 {
226   g_return_val_if_fail (obj != NULL, g_strdup (""));
227   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_NAME))
228   {
229     if (!_atspi_dbus_get_property (obj, atspi_interface_accessible, "Name", error,
230                                    "s", &obj->name))
231       return g_strdup ("");
232     _atspi_accessible_add_cache (obj, ATSPI_CACHE_NAME);
233   }
234   return g_strdup (obj->name);
235 }
236
237 /**
238  * atspi_accessible_get_description:
239  * @obj: a pointer to the #AtspiAccessible object on which to operate.
240  *
241  * Gets the description of an #AtspiAccessible object.
242  *
243  * Returns: a UTF-8 string describing the #AtspiAccessible object 
244  * or NULL on exception.
245  **/
246 gchar *
247 atspi_accessible_get_description (AtspiAccessible *obj, GError **error)
248 {
249   g_return_val_if_fail (obj != NULL, g_strdup (""));
250
251   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_DESCRIPTION))
252   {
253     if (!_atspi_dbus_get_property (obj, atspi_interface_accessible,
254                                    "Description", error, "s",
255                                    &obj->description))
256       return g_strdup ("");
257     _atspi_accessible_add_cache (obj, ATSPI_CACHE_DESCRIPTION);
258   }
259   return g_strdup (obj->description);
260 }
261
262 const char *str_parent = "Parent";
263
264 /**
265  * atspi_accessible_get_parent:
266  * @obj: a pointer to the #AtspiAccessible object to query.
267  *
268  * Gets an #AtspiAccessible object's parent container.
269  *
270  * Returns: (nullable) (transfer full): a pointer to the
271  *          #AtspiAccessible object which contains the given
272  *          #AtspiAccessible instance, or NULL if the @obj has no
273  *          parent container.
274  *
275  **/
276 AtspiAccessible *
277 atspi_accessible_get_parent (AtspiAccessible *obj, GError **error)
278 {
279   g_return_val_if_fail (obj != NULL, NULL);
280
281   if (obj->parent.app &&
282       !_atspi_accessible_test_cache (obj, ATSPI_CACHE_PARENT))
283   {
284     DBusMessage *message, *reply;
285     DBusMessageIter iter, iter_variant;
286     message = dbus_message_new_method_call (obj->parent.app->bus_name,
287                                             obj->parent.path,
288                                             DBUS_INTERFACE_PROPERTIES, "Get");
289     if (!message)
290       return NULL;
291     dbus_message_append_args (message, DBUS_TYPE_STRING, &atspi_interface_accessible,
292                                DBUS_TYPE_STRING, &str_parent,
293                               DBUS_TYPE_INVALID);
294     reply = _atspi_dbus_send_with_reply_and_block (message, error);
295     if (!reply)
296       return NULL;
297     if (strcmp (dbus_message_get_signature (reply), "v") != 0)
298     {
299       dbus_message_unref (reply);
300       return NULL;
301     }
302     dbus_message_iter_init (reply, &iter);
303     dbus_message_iter_recurse (&iter, &iter_variant);
304     obj->accessible_parent = _atspi_dbus_return_accessible_from_iter (&iter_variant);
305     dbus_message_unref (reply);
306     _atspi_accessible_add_cache (obj, ATSPI_CACHE_PARENT);
307   }
308   if (!obj->accessible_parent)
309     return NULL;
310   return g_object_ref (obj->accessible_parent);
311 }
312
313 /**
314  * atspi_accessible_get_child_count:
315  * @obj: a pointer to the #AtspiAccessible object on which to operate.
316  *
317  * Gets the number of children contained by an #AtspiAccessible object.
318  *
319  * Returns: a #long indicating the number of #AtspiAccessible children
320  *          contained by an #AtspiAccessible object or -1 on exception.
321  *
322  **/
323 gint
324 atspi_accessible_get_child_count (AtspiAccessible *obj, GError **error)
325 {
326   g_return_val_if_fail (obj != NULL, -1);
327
328   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_CHILDREN))
329   {
330     dbus_int32_t ret;
331     if (!_atspi_dbus_get_property (obj, atspi_interface_accessible,
332                                    "ChildCount", error, "i", &ret))
333       return -1;
334     return ret;
335   }
336
337   return g_list_length (obj->children);
338 }
339
340 /**
341  * atspi_accessible_get_child_at_index:
342  * @obj: a pointer to the #AtspiAccessible object on which to operate.
343  * @child_index: a #long indicating which child is specified.
344  *
345  * Gets the #AtspiAccessible child of an #AtspiAccessible object at a given index.
346  *
347  * Returns: (transfer full): a pointer to the #AtspiAccessible child object at
348  * index @child_index or NULL on exception.
349  **/
350 AtspiAccessible *
351 atspi_accessible_get_child_at_index (AtspiAccessible *obj,
352                             gint    child_index,
353                             GError **error)
354 {
355   AtspiAccessible *child;
356
357   g_return_val_if_fail (obj != NULL, NULL);
358
359   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_CHILDREN))
360   {
361     DBusMessage *reply;
362     reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
363                                      "GetChildAtIndex", error, "i",
364                                      child_index);
365     return _atspi_dbus_return_accessible_from_message (reply);
366   }
367
368   child = g_list_nth_data (obj->children, child_index);
369   if (!child)
370     return NULL;
371   return g_object_ref (child);
372 }
373
374 /**
375  * atspi_accessible_get_index_in_parent:
376  * @obj: a pointer to the #AtspiAccessible object on which to operate.
377  *
378  * Gets the index of an #AtspiAccessible object within its parent's 
379  * #AtspiAccessible children list.
380  *
381  * Returns: a #glong indicating the index of the #AtspiAccessible object
382  *          in its parent,
383  *          or -1 if @obj has no containing parent or on exception.
384  **/
385 gint
386 atspi_accessible_get_index_in_parent (AtspiAccessible *obj, GError **error)
387 {
388   GList *l;
389   gint i = 0;
390
391   g_return_val_if_fail (obj != NULL, -1);
392   if (_atspi_accessible_test_cache (obj, ATSPI_CACHE_PARENT) &&
393       !obj->accessible_parent)
394     return -1;
395   if (!obj->accessible_parent ||
396       !_atspi_accessible_test_cache (obj->accessible_parent,
397                                      ATSPI_CACHE_CHILDREN))
398   {
399     dbus_int32_t ret = -1;
400     _atspi_dbus_call (obj, atspi_interface_accessible,
401                       "GetIndexInParent", NULL, "=>i", &ret);
402     return ret;
403   }
404
405   l = obj->accessible_parent->children;
406   while (l)
407   {
408     if (l->data == obj) return i;
409     l = g_list_next (l);
410     i++;
411   }
412   return -1;
413 }
414
415 typedef struct
416 {
417   dbus_uint32_t type;
418   GArray *targets;
419 } Accessibility_Relation;
420
421 /**
422  * atspi_accessible_get_relation_set:
423  * @obj: a pointer to the #AtspiAccessible object on which to operate.
424  *
425  * Gets the set of #AtspiRelation objects which describes this #AtspiAccessible object's
426  * relationships with other #AtspiAccessible objects.
427  *
428  * Returns: (element-type AtspiAccessible*) (transfer full): a #GArray of
429  *          #AtspiRelation pointers or NULL on exception.
430  **/
431 GArray *
432 atspi_accessible_get_relation_set (AtspiAccessible *obj, GError **error)
433 {
434   DBusMessage *reply;
435   DBusMessageIter iter, iter_array;
436   GArray *ret;
437
438   g_return_val_if_fail (obj != NULL, NULL);
439
440   reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible, "GetRelationSet", error, "");
441   if (!reply)
442     return NULL;
443   _ATSPI_DBUS_CHECK_SIG (reply, "a(ua(so))", error, NULL);
444
445   ret = g_array_new (TRUE, TRUE, sizeof (AtspiRelation *));
446   dbus_message_iter_init (reply, &iter);
447   dbus_message_iter_recurse (&iter, &iter_array);
448   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
449   {
450     AtspiRelation *relation;
451     relation = _atspi_relation_new_from_iter (&iter_array);
452     ret = g_array_append_val (ret, relation);
453     dbus_message_iter_next (&iter_array);
454   }
455   dbus_message_unref (reply);
456   return ret;
457 }
458
459 /**
460  * atspi_accessible_get_role:
461  * @obj: a pointer to the #AtspiAccessible object on which to operate.
462  *
463  * Gets the UI role played by an #AtspiAccessible object.
464  * This role's name can be obtained via atspi_accessible_get_role_name ().
465  *
466  * Returns: the #AtspiRole of an #AtspiAccessible object.
467  *
468  **/
469 AtspiRole
470 atspi_accessible_get_role (AtspiAccessible *obj, GError **error)
471 {
472   g_return_val_if_fail (obj != NULL, ATSPI_ROLE_INVALID);
473
474   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_ROLE))
475   {
476     dbus_uint32_t role;
477     /* TODO: Make this a property */
478     if (_atspi_dbus_call (obj, atspi_interface_accessible, "GetRole", error, "=>u", &role))
479     {
480       obj->role = role;
481     _atspi_accessible_add_cache (obj, ATSPI_CACHE_ROLE);
482     }
483   }
484   return obj->role;
485 }
486
487 /**
488  * atspi_accessible_get_role_name:
489  * @obj: a pointer to the #AtspiAccessible object on which to operate.
490  *
491  * Gets a UTF-8 string corresponding to the name of the role played by an object.
492  * This method will return useful values for roles that fall outside the
493  * enumeration used in atspi_accessible_get_role ().
494  *
495  * Returns: a UTF-8 string specifying the type of UI role played by an
496  * #AtspiAccessible object.
497  *
498  **/
499 gchar *
500 atspi_accessible_get_role_name (AtspiAccessible *obj, GError **error)
501 {
502   gchar *retval = NULL;
503   AtspiRole role;
504
505   g_return_val_if_fail (obj != NULL, NULL);
506
507   role = atspi_accessible_get_role (obj, error);
508   if (role >= 0 && role < ATSPI_ROLE_COUNT && role != ATSPI_ROLE_EXTENDED)
509     return atspi_role_get_name (role);
510
511   _atspi_dbus_call (obj, atspi_interface_accessible, "GetRoleName", error, "=>s", &retval);
512
513   if (!retval)
514     retval = g_strdup ("");
515
516   return retval;
517 }
518
519 /**
520  * atspi_accessible_get_localized_role_name:
521  * @obj: a pointer to the #AtspiAccessible object on which to operate.
522  *
523  * Gets a UTF-8 string corresponding to the name of the role played by an 
524  * object, translated to the current locale.
525  * This method will return useful values for roles that fall outside the
526  * enumeration used in atspi_accessible_getRole ().
527  *
528  * Returns: a localized, UTF-8 string specifying the type of UI role played 
529  * by an #AtspiAccessible object.
530  *
531  **/
532 gchar *
533 atspi_accessible_get_localized_role_name (AtspiAccessible *obj, GError **error)
534 {
535   char *retval = NULL;
536
537   g_return_val_if_fail (obj != NULL, NULL);
538
539   _atspi_dbus_call (obj, atspi_interface_accessible, "GetLocalizedRoleName", error, "=>s", &retval);
540
541   if (!retval)
542     return g_strdup ("");
543
544   return retval;
545 }
546
547 static AtspiStateSet *
548 defunct_set ()
549 {
550   AtspiStateSet *set = atspi_state_set_new (NULL);
551   atspi_state_set_add (set, ATSPI_STATE_DEFUNCT);
552   return set;
553 }
554
555 /**
556  * atspi_accessible_get_state_set:
557  * @obj: a pointer to the #AtspiAccessible object on which to operate.
558  *
559  * Gets the states currently held by an object.
560  *
561  * Returns: (transfer full): a pointer to an #AtspiStateSet representing an
562  * object's current state set.
563  **/
564 AtspiStateSet *
565 atspi_accessible_get_state_set (AtspiAccessible *obj)
566 {
567   /* TODO: Should take a GError **, but would be an API break */
568   if (!obj->parent.app || !obj->parent.app->bus)
569     return defunct_set ();
570
571   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_STATES))
572   {
573     DBusMessage *reply;
574     DBusMessageIter iter;
575     reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
576                                       "GetState", NULL, "");
577     _ATSPI_DBUS_CHECK_SIG (reply, "au", NULL, defunct_set ());
578     dbus_message_iter_init (reply, &iter);
579     _atspi_dbus_set_state (obj, &iter);
580     dbus_message_unref (reply);
581     _atspi_accessible_add_cache (obj, ATSPI_CACHE_STATES);
582   }
583
584   return g_object_ref (obj->states);
585 }
586
587 /**
588  * atspi_accessible_get_attributes:
589  * @obj: The #AtspiAccessible being queried.
590  *
591  * Gets the #AttributeSet representing any assigned 
592  * name-value pair attributes or annotations for this object.
593  * For typographic, textual, or textually-semantic attributes, see
594  * atspi_text_get_attributes instead.
595  *
596  * Returns: (element-type gchar* gchar*) (transfer full): The name-value-pair
597  * attributes assigned to this object.
598  */
599 GHashTable *
600 atspi_accessible_get_attributes (AtspiAccessible *obj, GError **error)
601 {
602   DBusMessage *message;
603
604     g_return_val_if_fail (obj != NULL, NULL);
605
606   if (obj->priv->cache)
607   {
608     GValue *val = g_hash_table_lookup (obj->priv->cache, "Attributes");
609     if (val)
610       return g_value_dup_boxed (val);
611   }
612
613   if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_ATTRIBUTES))
614   {
615     message = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
616                                         "GetAttributes", error, "");
617     obj->attributes = _atspi_dbus_return_hash_from_message (message);
618     _atspi_accessible_add_cache (obj, ATSPI_CACHE_ATTRIBUTES);
619   }
620
621   if (!obj->attributes)
622     return NULL;
623   return g_hash_table_ref (obj->attributes);
624 }
625
626 static void
627 add_to_attribute_array (gpointer key, gpointer value, gpointer data)
628 {
629   GArray **array = (GArray **)data;
630   gchar *str = g_strconcat (key, ":", value, NULL);
631   *array = g_array_append_val (*array, str);
632 }
633
634 /**
635  * atspi_accessible_get_attributes_as_array:
636  * @obj: The #AtspiAccessible being queried.
637  *
638  * Gets a #GArray representing any assigned 
639  * name-value pair attributes or annotations for this object.
640  * For typographic, textual, or textually-semantic attributes, see
641  * atspi_text_get_attributes_as_array instead.
642  *
643  * Returns: (element-type gchar*) (transfer full): The name-value-pair
644  *          attributes assigned to this object.
645  */
646 GArray *
647 atspi_accessible_get_attributes_as_array (AtspiAccessible *obj, GError **error)
648 {
649   DBusMessage *message;
650
651     g_return_val_if_fail (obj != NULL, NULL);
652
653   if (obj->priv->cache)
654   {
655     GValue *val = g_hash_table_lookup (obj->priv->cache, "Attributes");
656     if (val)
657     {
658       GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
659       GHashTable *attributes = g_value_get_boxed (val);
660       g_hash_table_foreach (attributes, add_to_attribute_array, &array);
661       return array;
662     }
663   }
664
665   message = _atspi_dbus_call_partial (obj, atspi_interface_accessible, "GetAttributes", error, "");
666   return _atspi_dbus_return_attribute_array_from_message (message);
667 }
668
669 /**
670  * atspi_accessible_get_application:
671  * @obj: The #AtspiAccessible being queried.
672  *
673  * Gets the containing #AtspiApplication for an object.
674  *
675  * Returns: (transfer full): the containing #AtspiApplication instance for
676  *          this object.
677  */
678 AtspiAccessible *
679 atspi_accessible_get_application (AtspiAccessible *obj, GError **error)
680 {
681   AtspiAccessible *parent;
682
683   g_object_ref (obj);
684   for (;;)
685   {
686     parent = atspi_accessible_get_parent (obj, NULL);
687     if (!parent && obj->parent.app &&
688         atspi_accessible_get_role (obj, NULL) != ATSPI_ROLE_APPLICATION)
689     {
690       AtspiAccessible *root = g_object_ref (obj->parent.app->root);
691       if (root)
692       {
693         g_object_unref (obj);
694         if (atspi_accessible_get_role (root, NULL) == ATSPI_ROLE_DESKTOP_FRAME)
695         {
696           g_object_unref (root);
697           return NULL;
698         }
699         return root;
700       }
701     }
702     if (!parent || parent == obj ||
703         atspi_accessible_get_role (parent, NULL) == ATSPI_ROLE_DESKTOP_FRAME)
704   {
705     if (parent)
706       g_object_unref (parent);
707     return obj;
708   }
709     g_object_unref (obj);
710     obj = parent;
711   }
712 }
713
714 /* Application-specific methods */
715
716 /**
717  * atspi_accessible_get_toolkit_name:
718  * @obj: a pointer to the #AtspiAccessible object on which to operate.
719  *
720  * Gets the toolkit name for an #AtspiAccessible object.
721  * Only works on application root objects.
722  *
723  * Returns: a UTF-8 string indicating the toolkit name for the #AtspiAccessible object or NULL on exception.
724  **/
725 gchar *
726 atspi_accessible_get_toolkit_name (AtspiAccessible *obj, GError **error)
727 {
728   g_return_val_if_fail (obj != NULL, NULL);
729
730   if (!obj->parent.app)
731     return NULL;
732
733   if (!obj->parent.app->toolkit_name)
734     _atspi_dbus_get_property (obj, atspi_interface_application, "ToolkitName",
735                               error, "s", &obj->parent.app->toolkit_name);
736
737   return g_strdup (obj->parent.app->toolkit_name);
738 }
739
740 /**
741  * atspi_accessible_get_toolkit_version:
742  * @obj: a pointer to the #AtspiAccessible object on which to operate.
743  *
744  * Gets the toolkit version for an #AtspiAccessible object.
745  * Only works on application root objects.
746  *
747  * Returns: a UTF-8 string indicating the toolkit version for the #AtspiAccessible object or NULL on exception.
748  **/
749 gchar *
750 atspi_accessible_get_toolkit_version (AtspiAccessible *obj, GError **error)
751 {
752   g_return_val_if_fail (obj != NULL, NULL);
753
754   if (!obj->parent.app)
755     return NULL;
756
757   if (!obj->parent.app->toolkit_version)
758     _atspi_dbus_get_property (obj, atspi_interface_application, "Version",
759                               error, "s", &obj->parent.app->toolkit_version);
760
761   return g_strdup (obj->parent.app->toolkit_version);
762 }
763
764 /**
765  * atspi_accessible_get_atspi_version:
766  * @obj: a pointer to the #AtspiAccessible object on which to operate.
767  *
768  * Gets the AT-SPI IPC specification version supported by the application
769  * pointed to by the #AtspiAccessible object.
770  * Only works on application root objects.
771  *
772  * Returns: a UTF-8 string indicating the AT-SPI version for the #AtspiAccessible object or NULL on exception.
773  **/
774 gchar *
775 atspi_accessible_get_atspi_version (AtspiAccessible *obj, GError **error)
776 {
777   g_return_val_if_fail (obj != NULL, NULL);
778
779   if (!obj->parent.app)
780     return NULL;
781
782   if (!obj->parent.app->atspi_version)
783     _atspi_dbus_get_property (obj, atspi_interface_application, "AtspiVersion",
784                               error, "s", &obj->parent.app->atspi_version);
785
786   return g_strdup (obj->parent.app->atspi_version);
787 }
788
789 /**
790  * atspi_accessible_get_id:
791  * @obj: a pointer to the #AtspiAccessible object on which to operate.
792  *
793  * Gets the application id for a #AtspiAccessible object.
794  * Only works on application root objects.
795  *
796  * Returns: a positive #gint indicating the id for the #AtspiAccessible object 
797  * or -1 on exception.
798  **/
799 gint
800 atspi_accessible_get_id (AtspiAccessible *obj, GError **error)
801 {
802   gint ret = -1;
803
804   g_return_val_if_fail (obj != NULL, -1);
805
806   if (!_atspi_dbus_get_property (obj, atspi_interface_application, "Id", error, "i", &ret))
807       return -1;
808   return ret;
809 }
810
811
812 /* Interface query methods */
813
814 static gboolean
815 _atspi_accessible_is_a (AtspiAccessible *accessible,
816                       const char *interface_name)
817 {
818   int n;
819
820   if (accessible == NULL)
821     {
822       return FALSE;
823     }
824
825   if (!_atspi_accessible_test_cache (accessible, ATSPI_CACHE_INTERFACES))
826   {
827     DBusMessage *reply;
828     DBusMessageIter iter;
829     reply = _atspi_dbus_call_partial (accessible, atspi_interface_accessible,
830                                       "GetInterfaces", NULL, "");
831     _ATSPI_DBUS_CHECK_SIG (reply, "as", NULL, FALSE);
832     dbus_message_iter_init (reply, &iter);
833     _atspi_dbus_set_interfaces (accessible, &iter);
834     dbus_message_unref (reply);
835     _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
836   }
837
838   n = _atspi_get_iface_num (interface_name);
839   if (n == -1) return FALSE;
840   return (gboolean) ((accessible->interfaces & (1 << n))? TRUE: FALSE);
841 }
842
843 /**
844  * atspi_accessible_is_action:
845  * @obj: a pointer to the #AtspiAccessible instance to query.
846  *
847  * Query whether the specified #AtspiAccessible implements the 
848  * #AtspiAction interface.
849  *
850  * Returns: #TRUE if @obj implements the #AtspiAction interface,
851  *          #FALSE otherwise.
852  **/
853 gboolean
854 atspi_accessible_is_action (AtspiAccessible *obj)
855 {
856   return _atspi_accessible_is_a (obj,
857                               atspi_interface_action);
858 }
859
860 /**
861  * atspi_accessible_is_application:
862  * @obj: a pointer to the #AtspiAccessible instance to query.
863  *
864  * Query whether the specified #AtspiAccessible implements the
865  * #AtspiApplication interface.
866  *
867  * Returns: #TRUE if @obj implements the #AtspiApplication interface,
868  *          #FALSE otherwise.
869  **/
870 gboolean
871 atspi_accessible_is_application (AtspiAccessible *obj)
872 {
873   return _atspi_accessible_is_a (obj,
874                               atspi_interface_application);
875 }
876
877 /**                      
878  * atspi_accessible_is_collection:
879  * @obj: a pointer to the #AtspiAccessible instance to query.
880  *
881  * Query whether the specified #AtspiAccessible implements the
882  * #AtspiCollection interface.
883  *
884  * Returns: #TRUE if @obj implements the #AtspiCollection interface,
885  *          #FALSE otherwise.
886  **/
887 gboolean
888 atspi_accessible_is_collection (AtspiAccessible *obj)
889 {
890      return _atspi_accessible_is_a (obj,
891                               atspi_interface_collection);
892 }
893
894 /**
895  * atspi_accessible_is_component:
896  * @obj: a pointer to the #AtspiAccessible instance to query.
897  *
898  * Query whether the specified #AtspiAccessible implements #AtspiComponent.
899  *
900  * Returns: #TRUE if @obj implements the #AtspiComponent interface,
901  *          #FALSE otherwise.
902  **/
903 gboolean
904 atspi_accessible_is_component (AtspiAccessible *obj)
905 {
906   return _atspi_accessible_is_a (obj,
907                               atspi_interface_component);
908 }
909
910 /**
911  * atspi_accessible_is_document:
912  * @obj: a pointer to the #AtspiAccessible instance to query.
913  *
914  * Query whether the specified #AtspiAccessible implements the
915  * #AtspiDocument interface.
916  *
917  * Returns: #TRUE if @obj implements the #AtspiDocument interface,
918  *          #FALSE otherwise.
919  **/
920 gboolean
921 atspi_accessible_is_document (AtspiAccessible *obj)
922 {
923   return _atspi_accessible_is_a (obj,
924                               atspi_interface_document);
925 }
926
927 /**
928  * atspi_accessible_is_editable_text:
929  * @obj: a pointer to the #AtspiAccessible instance to query.
930  *
931  * Query whether the specified #AtspiAccessible implements the
932  * #AtspiEditableText interface.
933  *
934  * Returns: #TRUE if @obj implements the #AtspiEditableText interface,
935  *          #FALSE otherwise.
936  **/
937 gboolean
938 atspi_accessible_is_editable_text (AtspiAccessible *obj)
939 {
940   return _atspi_accessible_is_a (obj,
941                               atspi_interface_editable_text);
942 }
943                                                                                                                                                                         
944 /**
945  * atspi_accessible_is_hypertext:
946  * @obj: a pointer to the #AtspiAccessible instance to query.
947  *
948  * Query whether the specified #AtspiAccessible implements the
949  * #AtspiHypertext interface.
950  *
951  * Returns: #TRUE if @obj implements the #AtspiHypertext interface,
952  *          #FALSE otherwise.
953  **/
954 gboolean
955 atspi_accessible_is_hypertext (AtspiAccessible *obj)
956 {
957   return _atspi_accessible_is_a (obj,
958                               atspi_interface_hypertext);
959 }
960
961 /**
962  * atspi_accessible_is_hyperlink:
963  * @obj: a pointer to the #AtspiAccessible instance to query.
964  *
965  * Query whether the specified #AtspiAccessible implements the 
966  * #AtspiHyperlink interface.
967  *
968  * Returns: #TRUE if @obj implements the #AtspiHypertext interface,
969  *          #FALSE otherwise.
970  **/
971 gboolean
972 atspi_accessible_is_hyperlink (AtspiAccessible *obj)
973 {
974   return _atspi_accessible_is_a (obj,
975                               atspi_interface_hyperlink);
976 }
977
978 /**
979  * atspi_accessible_is_image:
980  * @obj: a pointer to the #AtspiAccessible instance to query.
981  *
982  * Query whether the specified #AtspiAccessible implements the
983  * #AtspiImage interface.
984  *
985  * Returns: #TRUE if @obj implements the #AtspiImage interface,
986  *          #FALSE otherwise.
987 **/
988 gboolean
989 atspi_accessible_is_image (AtspiAccessible *obj)
990 {
991   return _atspi_accessible_is_a (obj,
992                               atspi_interface_image);
993 }
994
995 /**
996  * atspi_accessible_is_selection:
997  * @obj: a pointer to the #AtspiAccessible instance to query.
998  *
999  * Query whether the specified #AtspiAccessible implements the
1000  * #AtspiSelection interface.
1001  *
1002  * Returns: #TRUE if @obj implements the #AtspiSelection interface,
1003  *          #FALSE otherwise.
1004 **/
1005 gboolean
1006 atspi_accessible_is_selection (AtspiAccessible *obj)
1007 {
1008   return _atspi_accessible_is_a (obj,
1009                               atspi_interface_selection);
1010 }
1011
1012 /**
1013  * atspi_accessible_is_table:
1014  * @obj: a pointer to the #AtspiAccessible instance to query.
1015  *
1016  * Query whether the specified #AtspiAccessible implements the
1017  * #AtspiTable interface.
1018  *
1019  * Returns: #TRUE if @obj implements the #AtspiTable interface,
1020  *          #FALSE otherwise.
1021 **/
1022 gboolean
1023 atspi_accessible_is_table (AtspiAccessible *obj)
1024 {
1025   return _atspi_accessible_is_a (obj,
1026                               atspi_interface_table);
1027 }
1028
1029 /**
1030  * atspi_accessible_is_table_cell:
1031  * @obj: a pointer to the #AtspiAccessible instance to query.
1032  *
1033  * Query whether the specified #AtspiAccessible implements the
1034  * #AtspiTableCell interface.
1035  *
1036  * Returns: #TRUE if @obj implements the #AtspiTable interface,
1037  *          #FALSE otherwise.
1038 **/
1039 gboolean
1040 atspi_accessible_is_table_cell (AtspiAccessible *obj)
1041 {
1042   return _atspi_accessible_is_a (obj,
1043                               atspi_interface_table_cell);
1044 }
1045
1046 /**
1047  * atspi_accessible_is_streamable_content:
1048  * @obj: a pointer to the #AtspiAccessible instance to query.
1049  *
1050  * Query whether the specified #AtspiAccessible implements the
1051  * #AtspiStreamableContent interface.
1052  *
1053  * Returns: #TRUE if @obj implements the #AtspiStreamableContent interface,
1054  *          #FALSE otherwise.
1055 **/
1056 gboolean
1057 atspi_accessible_is_streamable_content (AtspiAccessible *obj)
1058 {
1059 #if 0
1060   return _atspi_accessible_is_a (obj,
1061                               atspi_interface_streamable_content);
1062 #else
1063   g_warning ("Streamable content not implemented");
1064   return FALSE;
1065 #endif
1066 }
1067
1068 /**
1069  * atspi_accessible_is_text:
1070  * @obj: a pointer to the #AtspiAccessible instance to query.
1071  *
1072  * Query whether the specified #AtspiAccessible implements the 
1073  * #AtspiText interface.
1074  *
1075  * Returns: #TRUE if @obj implements the #AtspiText interface,
1076  *          #FALSE otherwise.
1077 **/
1078 gboolean
1079 atspi_accessible_is_text (AtspiAccessible *obj)
1080 {
1081   return _atspi_accessible_is_a (obj,
1082                               atspi_interface_text);
1083 }
1084
1085 /**
1086  * atspi_accessible_is_value:
1087  * @obj: a pointer to the #AtspiAccessible instance to query.
1088  *
1089  * Query whether the specified #AtspiAccessible implements the
1090  * #AtspiValue interface.
1091  *
1092  * Returns: #TRUE if @obj implements the #AtspiValue interface,
1093  *          #FALSE otherwise.
1094 **/
1095 gboolean
1096 atspi_accessible_is_value (AtspiAccessible *obj)
1097 {
1098   return _atspi_accessible_is_a (obj,
1099                               atspi_interface_value);
1100 }
1101
1102 /**
1103  * atspi_accessible_get_action:
1104  * @obj: a pointer to the #AtspiAccessible instance to query.
1105  *
1106  * Gets the #AtspiAction interface for an #AtspiAccessible.
1107  *
1108  * Returns: (transfer full): a pointer to an #AtspiAction interface
1109  *          instance, or NULL if @obj does not implement #AtspiAction.
1110  *
1111  * Deprecated: 2.10: Use atspi_accessible_get_action_iface instead.
1112  * Rename to: atspi_accessible_get_action_iface
1113  **/
1114 AtspiAction *
1115 atspi_accessible_get_action (AtspiAccessible *accessible)
1116 {
1117   return (_atspi_accessible_is_a (accessible, atspi_interface_action) ?
1118           g_object_ref (ATSPI_ACTION (accessible)) : NULL);  
1119 }
1120
1121 /**
1122  * atspi_accessible_get_action_iface:
1123  * @obj: a pointer to the #AtspiAccessible instance to query.
1124  *
1125  * Gets the #AtspiAction interface for an #AtspiAccessible.
1126  *
1127  * Returns: (transfer full): a pointer to an #AtspiAction interface
1128  *          instance, or NULL if @obj does not implement #AtspiAction.
1129  **/
1130 AtspiAction *
1131 atspi_accessible_get_action_iface (AtspiAccessible *accessible)
1132 {
1133   return (_atspi_accessible_is_a (accessible, atspi_interface_action) ?
1134           g_object_ref (ATSPI_ACTION (accessible)) : NULL);  
1135 }
1136
1137 /**
1138  * atspi_accessible_get_collection:
1139  * @obj: a pointer to the #AtspiAccessible instance to query.
1140  *
1141  * Gets the #AtspiCollection interface for an #AtspiAccessible.
1142  *
1143  * Returns: (transfer full): a pointer to an #AtspiCollection interface
1144  *          instance, or NULL if @obj does not implement #AtspiCollection.
1145  *
1146  * Deprecated: 2.10: Use atspi_accessible_get_collection_iface instead.
1147  * Rename to: atspi_accessible_get_collection_iface
1148  **/
1149 AtspiCollection *
1150 atspi_accessible_get_collection (AtspiAccessible *accessible)
1151 {
1152   return (_atspi_accessible_is_a (accessible, atspi_interface_collection) ?
1153           g_object_ref (ATSPI_COLLECTION (accessible)) : NULL);  
1154 }
1155
1156 /**
1157  * atspi_accessible_get_collection_iface:
1158  * @obj: a pointer to the #AtspiAccessible instance to query.
1159  *
1160  * Gets the #AtspiCollection interface for an #AtspiAccessible.
1161  *
1162  * Returns: (transfer full): a pointer to an #AtspiCollection interface
1163  *          instance, or NULL if @obj does not implement #AtspiCollection.
1164  **/
1165 AtspiCollection *
1166 atspi_accessible_get_collection_iface (AtspiAccessible *accessible)
1167 {
1168   return (_atspi_accessible_is_a (accessible, atspi_interface_collection) ?
1169           g_object_ref (ATSPI_COLLECTION (accessible)) : NULL);  
1170 }
1171
1172 /**
1173  * atspi_accessible_get_component:
1174  * @obj: a pointer to the #AtspiAccessible instance to query.
1175  *
1176  * Gets the #AtspiComponent interface for an #AtspiAccessible.
1177  *
1178  * Returns: (transfer full): a pointer to an #AtspiComponent interface
1179  *          instance, or NULL if @obj does not implement #AtspiComponent.
1180  *
1181  * Deprecated: 2.10: Use atspi_accessible_get_component_iface instead.
1182  * Rename to: atspi_accessible_get_component_iface
1183  **/
1184 AtspiComponent *
1185 atspi_accessible_get_component (AtspiAccessible *obj)
1186 {
1187   return (_atspi_accessible_is_a (obj, atspi_interface_component) ?
1188           g_object_ref (ATSPI_COMPONENT (obj)) : NULL);
1189 }
1190
1191 /**
1192  * atspi_accessible_get_component_iface:
1193  * @obj: a pointer to the #AtspiAccessible instance to query.
1194  *
1195  * Gets the #AtspiComponent interface for an #AtspiAccessible.
1196  *
1197  * Returns: (transfer full): a pointer to an #AtspiComponent interface
1198  *          instance, or NULL if @obj does not implement #AtspiComponent.
1199  **/
1200 AtspiComponent *
1201 atspi_accessible_get_component_iface (AtspiAccessible *obj)
1202 {
1203   return (_atspi_accessible_is_a (obj, atspi_interface_component) ?
1204           g_object_ref (ATSPI_COMPONENT (obj)) : NULL);
1205 }
1206
1207 /**
1208  * atspi_accessible_get_document:
1209  * @obj: a pointer to the #AtspiAccessible instance to query.
1210  *
1211  * Gets the #AtspiDocument interface for an #AtspiAccessible.
1212  *
1213  * Returns: (transfer full): a pointer to an #AtspiDocument interface
1214  *          instance, or NULL if @obj does not implement #AtspiDocument.
1215  *
1216  * Deprecated: 2.10: Use atspi_accessible_get_document_iface instead.
1217  * Rename to: atspi_accessible_get_document_iface
1218  **/
1219 AtspiDocument *
1220 atspi_accessible_get_document (AtspiAccessible *accessible)
1221 {
1222   return (_atspi_accessible_is_a (accessible, atspi_interface_document) ?
1223           g_object_ref (ATSPI_DOCUMENT (accessible)) : NULL);  
1224 }
1225
1226 /**
1227  * atspi_accessible_get_document_iface:
1228  * @obj: a pointer to the #AtspiAccessible instance to query.
1229  *
1230  * Gets the #AtspiDocument interface for an #AtspiAccessible.
1231  *
1232  * Returns: (transfer full): a pointer to an #AtspiDocument interface
1233  *          instance, or NULL if @obj does not implement #AtspiDocument.
1234  **/
1235 AtspiDocument *
1236 atspi_accessible_get_document_iface (AtspiAccessible *accessible)
1237 {
1238   return (_atspi_accessible_is_a (accessible, atspi_interface_document) ?
1239           g_object_ref (ATSPI_DOCUMENT (accessible)) : NULL);  
1240 }
1241
1242 /**
1243  * atspi_accessible_get_editable_text:
1244  * @obj: a pointer to the #AtspiAccessible instance to query.
1245  *
1246  * Gets the #AtspiEditableText interface for an #AtspiAccessible.
1247  *
1248  * Returns: (transfer full): a pointer to an #AtspiEditableText interface
1249  *          instance, or NULL if @obj does not implement #AtspiEditableText.
1250  *
1251  * Deprecated: 2.10: Use atspi_accessible_get_editable_text_iface instead.
1252  * Rename to: atspi_accessible_get_editable_text_iface
1253  **/
1254 AtspiEditableText *
1255 atspi_accessible_get_editable_text (AtspiAccessible *accessible)
1256 {
1257   return (_atspi_accessible_is_a (accessible, atspi_interface_editable_text) ?
1258           g_object_ref (ATSPI_EDITABLE_TEXT (accessible)) : NULL);  
1259 }
1260
1261 /**
1262  * atspi_accessible_get_editable_text_iface:
1263  * @obj: a pointer to the #AtspiAccessible instance to query.
1264  *
1265  * Gets the #AtspiEditableText interface for an #AtspiAccessible.
1266  *
1267  * Returns: (transfer full): a pointer to an #AtspiEditableText interface
1268  *          instance, or NULL if @obj does not implement #AtspiEditableText.
1269  **/
1270 AtspiEditableText *
1271 atspi_accessible_get_editable_text_iface (AtspiAccessible *accessible)
1272 {
1273   return (_atspi_accessible_is_a (accessible, atspi_interface_editable_text) ?
1274           g_object_ref (ATSPI_EDITABLE_TEXT (accessible)) : NULL);  
1275 }
1276
1277 /**
1278  * atspi_accessible_get_hyperlink:
1279  * @obj: a pointer to the #AtspiAccessible object on which to operate.
1280  *
1281  * Gets the #AtspiHyperlink interface for an #AtspiAccessible.
1282  *
1283  * Returns: (transfer full): the #AtspiHyperlink object associated with
1284  *          the given #AtspiAccessible, or NULL if not supported.
1285  **/
1286 AtspiHyperlink *
1287 atspi_accessible_get_hyperlink (AtspiAccessible *accessible)
1288 {
1289   return (_atspi_accessible_is_a (accessible, atspi_interface_hyperlink) ?
1290           _atspi_hyperlink_new (accessible->parent.app, accessible->parent.path) : NULL);
1291 }
1292
1293 /**
1294  * atspi_accessible_get_hypertext:
1295  * @obj: a pointer to the #AtspiAccessible instance to query.
1296  *
1297  * Gets the #AtspiHypertext interface for an #AtspiAccessible.
1298  *
1299  * Returns: (transfer full): a pointer to an #AtspiHypertext interface
1300  *          instance, or NULL if @obj does not implement #AtspiHypertext.
1301  *
1302  * Deprecated: 2.10: Use atspi_accessible_get_hypertext_iface instead.
1303  * Rename to: atspi_accessible_get_hypertext_iface
1304  **/
1305 AtspiHypertext *
1306 atspi_accessible_get_hypertext (AtspiAccessible *accessible)
1307 {
1308   return (_atspi_accessible_is_a (accessible, atspi_interface_hypertext) ?
1309           g_object_ref (ATSPI_HYPERTEXT (accessible)) : NULL);  
1310 }
1311
1312 /**
1313  * atspi_accessible_get_hypertext_iface:
1314  * @obj: a pointer to the #AtspiAccessible instance to query.
1315  *
1316  * Gets the #AtspiHypertext interface for an #AtspiAccessible.
1317  *
1318  * Returns: (transfer full): a pointer to an #AtspiHypertext interface
1319  *          instance, or NULL if @obj does not implement #AtspiHypertext.
1320  **/
1321 AtspiHypertext *
1322 atspi_accessible_get_hypertext_iface (AtspiAccessible *accessible)
1323 {
1324   return (_atspi_accessible_is_a (accessible, atspi_interface_hypertext) ?
1325           g_object_ref (ATSPI_HYPERTEXT (accessible)) : NULL);  
1326 }
1327
1328 /**
1329  * atspi_accessible_get_image:
1330  * @obj: a pointer to the #AtspiAccessible instance to query.
1331  *
1332  * Gets the #AtspiImage interface for an #AtspiAccessible.
1333  *
1334  * Returns: (transfer full): a pointer to an #AtspiImage interface instance, or
1335  *          NULL if @obj does not implement #AtspiImage.
1336  *
1337  * Deprecated: 2.10: Use atspi_accessible_get_image_iface instead.
1338  * Rename to: atspi_accessible_get_image_iface
1339  **/
1340 AtspiImage *
1341 atspi_accessible_get_image (AtspiAccessible *accessible)
1342 {
1343   return (_atspi_accessible_is_a (accessible, atspi_interface_image) ?
1344           g_object_ref (ATSPI_IMAGE (accessible)) : NULL);  
1345 }
1346
1347 /**
1348  * atspi_accessible_get_image_iface:
1349  * @obj: a pointer to the #AtspiAccessible instance to query.
1350  *
1351  * Gets the #AtspiImage interface for an #AtspiAccessible.
1352  *
1353  * Returns: (transfer full): a pointer to an #AtspiImage interface instance, or
1354  *          NULL if @obj does not implement #AtspiImage.
1355  **/
1356 AtspiImage *
1357 atspi_accessible_get_image_iface (AtspiAccessible *accessible)
1358 {
1359   return (_atspi_accessible_is_a (accessible, atspi_interface_image) ?
1360           g_object_ref (ATSPI_IMAGE (accessible)) : NULL);  
1361 }
1362
1363 /**
1364  * atspi_accessible_get_selection:
1365  * @obj: a pointer to the #AtspiAccessible instance to query.
1366  *
1367  * Gets the #AtspiSelection interface for an #AtspiAccessible.
1368  *
1369  * Returns: (transfer full): a pointer to an #AtspiSelection interface
1370  *          instance, or NULL if @obj does not implement #AtspiSelection.
1371  *
1372  * Deprecated: 2.10: Use atspi_accessible_get_selection_iface instead.
1373  * Rename to: atspi_accessible_get_selection_iface
1374  **/
1375 AtspiSelection *
1376 atspi_accessible_get_selection (AtspiAccessible *accessible)
1377 {
1378   return (_atspi_accessible_is_a (accessible, atspi_interface_selection) ?
1379           g_object_ref (ATSPI_SELECTION (accessible)) : NULL);  
1380 }
1381
1382 /**
1383  * atspi_accessible_get_selection_iface:
1384  * @obj: a pointer to the #AtspiAccessible instance to query.
1385  *
1386  * Gets the #AtspiSelection interface for an #AtspiAccessible.
1387  *
1388  * Returns: (transfer full): a pointer to an #AtspiSelection interface
1389  *          instance, or NULL if @obj does not implement #AtspiSelection.
1390  **/
1391 AtspiSelection *
1392 atspi_accessible_get_selection_iface (AtspiAccessible *accessible)
1393 {
1394   return (_atspi_accessible_is_a (accessible, atspi_interface_selection) ?
1395           g_object_ref (ATSPI_SELECTION (accessible)) : NULL);  
1396 }
1397
1398 #if 0
1399 /**
1400  * atspi_accessible_get_streamable_content:
1401  * @obj: a pointer to the #AtspiAccessible instance to query.
1402  *
1403  * Gets the #AtspiStreamableContent interface for an #AtspiAccessible.
1404  *
1405  * Returns: (transfer full): a pointer to an #AtspiStreamableContent interface
1406  *          instance, or NULL if @obj does not implement #AtspiStreamableContent.
1407  **/
1408 AtspiStreamableContent *
1409 atspi_accessible_get_streamable_content (AtspiAccessible *accessible)
1410 {
1411   return (_atspi_accessible_is_a (accessible, atspi_interface_streamable_content) ?
1412           accessible : NULL);  
1413 }
1414 #endif
1415
1416 /**
1417  * atspi_accessible_get_table:
1418  * @obj: a pointer to the #AtspiAccessible instance to query.
1419  *
1420  * Gets the #AtspiTable interface for an #AtspiAccessible.
1421  *
1422  * Returns: (transfer full): a pointer to an #AtspiTable interface instance, or
1423  *          NULL if @obj does not implement #AtspiTable.
1424  *
1425  * Deprecated: 2.10: Use atspi_accessible_get_table_iface instead.
1426  * Rename to: atspi_accessible_get_table_iface
1427  **/
1428 AtspiTable *
1429 atspi_accessible_get_table (AtspiAccessible *obj)
1430 {
1431   return (_atspi_accessible_is_a (obj, atspi_interface_table) ?
1432           g_object_ref (ATSPI_TABLE (obj)) : NULL);  
1433 }
1434
1435 /**
1436  * atspi_accessible_get_table_iface:
1437  * @obj: a pointer to the #AtspiAccessible instance to query.
1438  *
1439  * Gets the #AtspiTable interface for an #AtspiAccessible.
1440  *
1441  * Returns: (transfer full): a pointer to an #AtspiTable interface instance, or
1442  *          NULL if @obj does not implement #AtspiTable.
1443  **/
1444 AtspiTable *
1445 atspi_accessible_get_table_iface (AtspiAccessible *obj)
1446 {
1447   return (_atspi_accessible_is_a (obj, atspi_interface_table) ?
1448           g_object_ref (ATSPI_TABLE (obj)) : NULL);  
1449 }
1450
1451 /**
1452  * atspi_accessible_get_table_cell:
1453  * @obj: a pointer to the #AtspiAccessible instance to query.
1454  *
1455  * Gets the #AtspiTableCell interface for an #AtspiAccessible.
1456  *
1457  * Returns: (transfer full): a pointer to an #AtspiTableCell interface instance,
1458  *          or NULL if @obj does not implement #AtspiTable.
1459  **/
1460 AtspiTableCell *
1461 atspi_accessible_get_table_cell (AtspiAccessible *obj)
1462 {
1463   return (_atspi_accessible_is_a (obj, atspi_interface_table_cell) ?
1464           g_object_ref (ATSPI_TABLE_CELL (obj)) : NULL);  
1465 }
1466
1467 /**
1468  * atspi_accessible_get_text:
1469  * @obj: a pointer to the #AtspiAccessible instance to query.
1470  *
1471  * Gets the #AtspiTable interface for an #AtspiAccessible.
1472  *
1473  * Returns: (transfer full): a pointer to an #AtspiText interface instance, or
1474  *          NULL if @obj does not implement #AtspiText.
1475  *
1476  * Deprecated: 2.10: Use atspi_accessible_get_text_iface instead.
1477  * Rename to: atspi_accessible_get_text_iface
1478  **/
1479 AtspiText *
1480 atspi_accessible_get_text (AtspiAccessible *obj)
1481 {
1482   return (_atspi_accessible_is_a (obj, atspi_interface_text) ?
1483           g_object_ref (ATSPI_TEXT (obj)) : NULL);
1484 }
1485
1486 /**
1487  * atspi_accessible_get_text_iface:
1488  * @obj: a pointer to the #AtspiAccessible instance to query.
1489  *
1490  * Gets the #AtspiTable interface for an #AtspiAccessible.
1491  *
1492  * Returns: (transfer full): a pointer to an #AtspiText interface instance, or
1493  *          NULL if @obj does not implement #AtspiText.
1494  **/
1495 AtspiText *
1496 atspi_accessible_get_text_iface (AtspiAccessible *obj)
1497 {
1498   return (_atspi_accessible_is_a (obj, atspi_interface_text) ?
1499           g_object_ref (ATSPI_TEXT (obj)) : NULL);
1500 }
1501
1502 /**
1503  * atspi_accessible_get_value:
1504  * @obj: a pointer to the #AtspiAccessible instance to query.
1505  *
1506  * Gets the #AtspiTable interface for an #AtspiAccessible.
1507  *
1508  * Returns: (transfer full): a pointer to an #AtspiValue interface instance, or
1509  *          NULL if @obj does not implement #AtspiValue.
1510  *
1511  * Deprecated: 2.10: Use atspi_accessible_get_value_iface instead.
1512  * Rename to: atspi_accessible_get_value_iface
1513  **/
1514 AtspiValue *
1515 atspi_accessible_get_value (AtspiAccessible *accessible)
1516 {
1517   return (_atspi_accessible_is_a (accessible, atspi_interface_value) ?
1518           g_object_ref (ATSPI_VALUE (accessible)) : NULL);  
1519 }
1520
1521 /**
1522  * atspi_accessible_get_value_iface:
1523  * @obj: a pointer to the #AtspiAccessible instance to query.
1524  *
1525  * Gets the #AtspiTable interface for an #AtspiAccessible.
1526  *
1527  * Returns: (transfer full): a pointer to an #AtspiValue interface instance, or
1528  *          NULL if @obj does not implement #AtspiValue.
1529  **/
1530 AtspiValue *
1531 atspi_accessible_get_value_iface (AtspiAccessible *accessible)
1532 {
1533   return (_atspi_accessible_is_a (accessible, atspi_interface_value) ?
1534           g_object_ref (ATSPI_VALUE (accessible)) : NULL);  
1535 }
1536
1537 static void
1538 append_const_val (GArray *array, const gchar *val)
1539 {
1540   gchar *dup = g_strdup (val);
1541
1542   if (dup)
1543     g_array_append_val (array, dup);
1544 }
1545
1546 /**
1547  * atspi_accessible_get_interfaces:
1548  * @obj: The #AtspiAccessible to query.
1549  *
1550  * A set of pointers to all interfaces supported by an #AtspiAccessible.
1551  *
1552  * Returns: (element-type gchar*) (transfer full): A #GArray of strings
1553  *          describing the interfaces supported by the object.  Interfaces are
1554  *          denoted in short-hand (i.e. "Component", "Text" etc.).
1555  **/
1556 GArray *
1557 atspi_accessible_get_interfaces (AtspiAccessible *obj)
1558 {
1559   GArray *ret = g_array_new (TRUE, TRUE, sizeof (gchar *));
1560
1561   g_return_val_if_fail (obj != NULL, NULL);
1562
1563   append_const_val (ret, "Accessible");
1564   if (atspi_accessible_is_action (obj))
1565     append_const_val (ret, "Action");
1566   if (atspi_accessible_is_collection (obj))
1567     append_const_val (ret, "Collection");
1568   if (atspi_accessible_is_component (obj))
1569     append_const_val (ret, "Component");
1570   if (atspi_accessible_is_document (obj))
1571     append_const_val (ret, "Document");
1572   if (atspi_accessible_is_editable_text (obj))
1573     append_const_val (ret, "EditableText");
1574   if (atspi_accessible_is_hypertext (obj))
1575     append_const_val (ret, "Hypertext");
1576   if (atspi_accessible_is_hyperlink (obj))
1577     append_const_val (ret, "Hyperlink");
1578   if (atspi_accessible_is_image (obj))
1579     append_const_val (ret, "Image");
1580   if (atspi_accessible_is_selection (obj))
1581     append_const_val (ret, "Selection");
1582   if (atspi_accessible_is_table (obj))
1583     append_const_val (ret, "Table");
1584   if (atspi_accessible_is_table_cell (obj))
1585     append_const_val (ret, "TableCell");
1586   if (atspi_accessible_is_text (obj))
1587     append_const_val (ret, "Text");
1588   if (atspi_accessible_is_value (obj))
1589     append_const_val (ret, "Value");
1590
1591   return ret;
1592 }
1593
1594 AtspiAccessible * 
1595 _atspi_accessible_new (AtspiApplication *app, const gchar *path)
1596 {
1597   AtspiAccessible *accessible;
1598   
1599   accessible = g_object_new (ATSPI_TYPE_ACCESSIBLE, NULL);
1600   g_return_val_if_fail (accessible != NULL, NULL);
1601
1602   accessible->parent.app = g_object_ref (app);
1603   accessible->parent.path = g_strdup (path);
1604
1605   return accessible;
1606 }
1607
1608 /**
1609  * atspi_accessible_set_cache_mask:
1610  * @accessible: The #AtspiAccessible to operate on.  Must be the desktop or
1611  *             the root of an application.
1612  * @mask: An #AtspiCache specifying a bit mask of the types of data to cache.
1613  *
1614  * Sets the type of data to cache for accessibles.
1615  * If this is not set for an application or is reset to ATSPI_CACHE_UNDEFINED,
1616  * then the desktop's cache flag will be used.
1617  * If the desktop's cache flag is also undefined, then all possible data will
1618  * be cached.
1619  * This function is intended to work around bugs in toolkits where the proper
1620  * events are not raised / to aid in testing for such bugs.
1621  **/
1622 void
1623 atspi_accessible_set_cache_mask (AtspiAccessible *accessible, AtspiCache mask)
1624 {
1625   g_return_if_fail (accessible != NULL);
1626   g_return_if_fail (accessible->parent.app != NULL);
1627   g_return_if_fail (accessible == accessible->parent.app->root);
1628   accessible->parent.app->cache = mask;
1629   enable_caching = TRUE;
1630 }
1631
1632 /**
1633  * atspi_accessible_clear_cache:
1634  * @accessible: The #AtspiAccessible whose cache to clear.
1635  *
1636  * Clears the cached information for the given accessible and all of its
1637  * descendants.
1638  */
1639 void
1640 atspi_accessible_clear_cache (AtspiAccessible *accessible)
1641 {
1642   GList *l;
1643
1644   if (accessible)
1645   {
1646     accessible->cached_properties = ATSPI_CACHE_NONE;
1647     for (l = accessible->children; l; l = l->next)
1648       atspi_accessible_clear_cache (l->data);
1649   }
1650 }
1651
1652 /**
1653  * atspi_accessible_get_process_id:
1654  * @accessible: The #AtspiAccessible to query.
1655  * @error: a pointer to a %NULL #GError pointer
1656  *
1657  * Returns the process id associated with the given accessible.  Mainly
1658  * added for debugging; it is a shortcut to explicitly querying the
1659  * accessible's app->bus_name and then calling GetConnectionUnixProcessID.
1660  *
1661  * Returns: The process ID or undetermined value if @error is set.
1662  **/
1663 guint
1664 atspi_accessible_get_process_id (AtspiAccessible *accessible, GError **error)
1665 {
1666   DBusMessage *message, *reply;
1667   DBusConnection *bus = _atspi_bus ();
1668   dbus_uint32_t pid = -1;
1669   DBusError d_error;
1670
1671   if (!accessible->parent.app || !accessible->parent.app->bus_name)
1672     {
1673       g_set_error_literal(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "Process is defunct");
1674       return -1;
1675     }
1676
1677   message = dbus_message_new_method_call ("org.freedesktop.DBus",
1678                                           "/org/freedesktop/DBus",
1679                                           "org.freedesktop.DBus",
1680                                           "GetConnectionUnixProcessID");
1681   dbus_message_append_args (message, DBUS_TYPE_STRING,
1682                             &accessible->parent.app->bus_name,
1683                             DBUS_TYPE_INVALID);
1684   dbus_error_init (&d_error);
1685   reply = dbus_connection_send_with_reply_and_block (bus, message, -1, &d_error);
1686   dbus_message_unref (message);
1687   if (reply)
1688   {
1689     if (!strcmp (dbus_message_get_signature (reply), "u"))
1690       dbus_message_get_args (reply, NULL, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID);
1691     dbus_message_unref (reply);
1692   }
1693   if (dbus_error_is_set (&d_error))
1694     {
1695       g_set_error_literal(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "Process is defunct");
1696       dbus_error_free (&d_error);
1697     }
1698   return pid;
1699 }
1700
1701 AtspiCache
1702 _atspi_accessible_get_cache_mask (AtspiAccessible *accessible)
1703 {
1704   AtspiCache mask;
1705
1706   if (!accessible->parent.app)
1707     return ATSPI_CACHE_NONE;
1708
1709  mask = accessible->parent.app->cache;
1710   if (mask == ATSPI_CACHE_UNDEFINED &&
1711       accessible->parent.app->root &&
1712       accessible->parent.app->root->accessible_parent)
1713   {
1714     AtspiAccessible *desktop = atspi_get_desktop (0);
1715     mask = desktop->parent.app->cache;
1716     g_object_unref (desktop);
1717   }
1718
1719   if (mask == ATSPI_CACHE_UNDEFINED)
1720     mask = ATSPI_CACHE_DEFAULT;
1721
1722   return mask;
1723 }
1724
1725 gboolean
1726 _atspi_accessible_test_cache (AtspiAccessible *accessible, AtspiCache flag)
1727 {
1728   AtspiCache mask = _atspi_accessible_get_cache_mask (accessible);
1729   AtspiCache result = accessible->cached_properties & mask & flag;
1730   if (accessible->states && atspi_state_set_contains (accessible->states, ATSPI_STATE_TRANSIENT))
1731     return FALSE;
1732   return (result != 0 && (atspi_main_loop || enable_caching ||
1733                           flag == ATSPI_CACHE_INTERFACES) &&
1734           !atspi_no_cache);
1735 }
1736
1737 void
1738 _atspi_accessible_add_cache (AtspiAccessible *accessible, AtspiCache flag)
1739 {
1740   AtspiCache mask = _atspi_accessible_get_cache_mask (accessible);
1741
1742   accessible->cached_properties |= flag & mask;
1743 }
1744
1745 /**
1746  * atspi_accessible_get_locale:
1747  * @accessible: an #AtspiAccessible
1748  *
1749  * Gets a UTF-8 string indicating the POSIX-style LC_MESSAGES locale
1750  * of @accessible.
1751  *
1752  * Since: 2.7.91
1753  *
1754  * Returns: a UTF-8 string indicating the POSIX-style LC_MESSAGES
1755  *          locale of @accessible.
1756  **/
1757 const gchar*
1758 atspi_accessible_get_object_locale (AtspiAccessible *accessible, GError **error)
1759 {
1760   gchar *locale;
1761
1762   g_return_val_if_fail (accessible != NULL, NULL);
1763
1764   locale = g_object_get_qdata (G_OBJECT (accessible), quark_locale);
1765   if (!locale)
1766   {
1767     if (!_atspi_dbus_get_property (accessible, atspi_interface_accessible,
1768                                    "Locale", error, "s", &locale))
1769       return NULL;
1770     if (locale)
1771       g_object_set_qdata_full (G_OBJECT (accessible), quark_locale, locale,
1772                                g_free);
1773   }
1774   return locale;
1775 }
1776
1777 void
1778 free_value (gpointer data)
1779 {
1780   GValue *value = data;
1781
1782   g_value_unset (value);
1783   g_free (value);
1784 }
1785
1786 GHashTable *
1787 _atspi_accessible_ref_cache (AtspiAccessible *accessible)
1788 {
1789   AtspiAccessiblePrivate *priv = accessible->priv;
1790
1791   priv->cache_ref_count++;
1792   if (priv->cache)
1793     return g_hash_table_ref (priv->cache);
1794   priv->cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
1795                                        free_value);
1796   return priv->cache;
1797 }
1798
1799 void
1800 _atspi_accessible_unref_cache (AtspiAccessible *accessible)
1801 {
1802   AtspiAccessiblePrivate *priv = accessible->priv;
1803
1804   if (priv->cache)
1805   {
1806     g_hash_table_unref (priv->cache);
1807     if (--priv->cache_ref_count == 0)
1808       priv->cache = NULL;
1809   }
1810 }