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