summaryrefslogtreecommitdiff
path: root/atspi/atspi-accessible.c
blob: 0a78b6bc5725c66216ddf88aa7e8338c598681f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 * Copyright 2010, 2011 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "atspi-accessible-private.h"
#include "atspi-private.h"
#include <string.h>

/**
 * AtspiAccessible:
 *
 * The base interface which is implemented by all accessible objects.
 *
 * All objects support interfaces for querying their contained 'children'
 * and position in the accessible-object hierarchy, whether or not they
 * actually have children.
 */

enum
{
  REGION_CHANGED,
  MODE_CHANGED,
  LAST_SIGNAL
};

static gboolean enable_caching = FALSE;
static guint quark_locale;

static guint atspi_accessible_signals[LAST_SIGNAL] = {
  0,
};

static gboolean
screen_reader_signal_watcher (GSignalInvocationHint *signal_hint,
                              guint n_param_values,
                              const GValue *param_values,
                              gpointer data)
{
  GObject *object;
  AtspiAccessible *accessible;
  GSignalQuery signal_query;
  const char *name;
  DBusMessage *signal;
  DBusMessageIter iter, iter_struct, iter_variant, iter_array;
  dbus_int32_t detail1 = 0, detail2 = 0;
  const char *detail = "";
  gchar *dbus_name;

  object = g_value_get_object (param_values + 0);
  g_return_val_if_fail (ATSPI_IS_ACCESSIBLE (object), FALSE);

  g_signal_query (signal_hint->signal_id, &signal_query);
  name = signal_query.signal_name;
  if (signal_hint->detail)
    detail = g_quark_to_string (signal_hint->detail);
  if (n_param_values > 1)
    detail1 = g_value_get_int (param_values + 1);
  if (n_param_values > 2 && G_VALUE_HOLDS_INT (param_values + 2))
    detail2 = g_value_get_int (param_values + 2);
  accessible = ATSPI_ACCESSIBLE (object);
  g_return_val_if_fail (accessible->parent.app != NULL, FALSE);

  dbus_name = _atspi_strdup_and_adjust_for_dbus (name);
  signal = dbus_message_new_signal (ATSPI_DBUS_PATH_SCREEN_READER,
                                    ATSPI_DBUS_INTERFACE_EVENT_SCREEN_READER,
                                    dbus_name);
  g_free (dbus_name);
  dbus_message_iter_init_append (signal, &iter);
  dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &detail);
  dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &detail1);
  dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &detail2);
  dbus_message_iter_open_container (&iter, DBUS_TYPE_VARIANT, "(so)",
                                    &iter_variant);
  dbus_message_iter_open_container (&iter_variant, DBUS_TYPE_STRUCT, NULL,
                                    &iter_struct);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &accessible->parent.app->bus_name);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &accessible->parent.path);
  dbus_message_iter_close_container (&iter_variant, &iter_struct);
  dbus_message_iter_close_container (&iter, &iter_variant);
  dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "{sv}",
                                    &iter_array);
  dbus_message_iter_close_container (&iter, &iter_array);
  dbus_connection_send (_atspi_bus (), signal, NULL);
  dbus_message_unref (signal);
  return TRUE;
}

static void
atspi_action_interface_init (AtspiAction *action)
{
}

static void
atspi_collection_interface_init (AtspiCollection *component)
{
}

static void
atspi_component_interface_init (AtspiComponent *component)
{
}

static void
atspi_document_interface_init (AtspiDocument *document)
{
}

static void
atspi_editable_text_interface_init (AtspiEditableText *editable_text)
{
}

static void
atspi_hypertext_interface_init (AtspiHypertext *hypertext)
{
}

static void
atspi_image_interface_init (AtspiImage *image)
{
}

static void
atspi_selection_interface_init (AtspiSelection *selection)
{
}

static void
atspi_table_interface_init (AtspiTable *table)
{
}

static void
atspi_table_cell_interface_init (AtspiTableCell *cell)
{
}

static void
atspi_text_interface_init (AtspiText *text)
{
}

static void
atspi_value_interface_init (AtspiValue *value)
{
}

G_DEFINE_TYPE_WITH_CODE (AtspiAccessible, atspi_accessible, ATSPI_TYPE_OBJECT, G_ADD_PRIVATE (AtspiAccessible) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_ACTION, atspi_action_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_COLLECTION, atspi_collection_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_COMPONENT, atspi_component_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_DOCUMENT, atspi_document_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_EDITABLE_TEXT, atspi_editable_text_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_HYPERTEXT, atspi_hypertext_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_IMAGE, atspi_image_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_SELECTION, atspi_selection_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TABLE, atspi_table_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TABLE_CELL, atspi_table_cell_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_TEXT, atspi_text_interface_init) G_IMPLEMENT_INTERFACE (ATSPI_TYPE_VALUE, atspi_value_interface_init))

#ifdef DEBUG_REF_COUNTS
static gint accessible_count = 0;
#endif

static void
atspi_accessible_unref (GObject *accessible)
{
  if (accessible != NULL)
    g_object_unref (accessible);
}

static void
atspi_accessible_init (AtspiAccessible *accessible)
{
#ifdef DEBUG_REF_COUNTS
  accessible_count++;
  g_hash_table_insert (_atspi_get_live_refs (), accessible, NULL);
  g_print ("at-spi: init: %d objects\n", accessible_count);
#endif

  accessible->priv = atspi_accessible_get_instance_private (accessible);

  accessible->children = g_ptr_array_new_with_free_func ((GDestroyNotify) atspi_accessible_unref);
}

static void
atspi_accessible_dispose (GObject *object)
{
  AtspiAccessible *accessible = ATSPI_ACCESSIBLE (object);
  AtspiEvent e;
  AtspiAccessible *parent;
  gint i;

  /* TODO: Only fire if object not already marked defunct */
  memset (&e, 0, sizeof (e));
  e.type = "object:state-changed:defunct";
  e.source = accessible;
  e.detail1 = 1;
  e.detail2 = 0;
  _atspi_send_event (&e);

  g_clear_object (&accessible->states);

  parent = accessible->accessible_parent;
  if (parent)
    {
      accessible->accessible_parent = NULL;
      if (parent->children)
        g_ptr_array_remove (parent->children, accessible);
      g_object_unref (parent);
    }

  if (accessible->children)
    for (i = accessible->children->len - 1; i >= 0; i--)
      {
        AtspiAccessible *child = g_ptr_array_index (accessible->children, i);
        if (child && child->accessible_parent == accessible)
          {
            child->accessible_parent = NULL;
            g_object_unref (accessible);
          }
      }

  if (accessible->children)
    {
      g_ptr_array_free (accessible->children, TRUE);
      accessible->children = NULL;
    }

  G_OBJECT_CLASS (atspi_accessible_parent_class)->dispose (object);
}

static void
atspi_accessible_finalize (GObject *object)
{
  AtspiAccessible *accessible = ATSPI_ACCESSIBLE (object);

  g_free (accessible->description);
  g_free (accessible->name);

  if (accessible->attributes)
    g_hash_table_unref (accessible->attributes);

  if (accessible->priv->cache)
    g_hash_table_destroy (accessible->priv->cache);

#ifdef DEBUG_REF_COUNTS
  accessible_count--;
  g_hash_table_remove (_atspi_get_live_refs (), accessible);
  g_print ("at-spi: finalize: %d objects\n", accessible_count);
#endif

  G_OBJECT_CLASS (atspi_accessible_parent_class)->finalize (object);

  /* TODO: remove emission hook */
}

static void
atspi_accessible_class_init (AtspiAccessibleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = atspi_accessible_dispose;
  object_class->finalize = atspi_accessible_finalize;

  quark_locale = g_quark_from_string ("accessible-locale");

  /**
   * AtspiAccessible::region-changed:
   * @atspiaccessible: the object which received the signal
   * @arg1: an integer specifying the current offset of the text being read,
   *        if the object is textual.
   * @arg2: an integer specifying the ending offset of the text being read,
   *        if the object is textual.
   *
   * The signal "region-changed" is emitted by a screen reader to indicate
   * that it is now reading or tracking a new object, or, a new piece of
   * text within an object. This allows a magnifier to gain the information
   * needed to highlight the object that the screen reader is reading.
   */
  atspi_accessible_signals[REGION_CHANGED] =
      g_signal_new ("region_changed",
                    G_TYPE_FROM_CLASS (klass),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (AtspiAccessibleClass, region_changed),
                    NULL, NULL,
                    atspi_marshal_VOID__INT_INT,
                    G_TYPE_NONE,
                    2, G_TYPE_INT, G_TYPE_INT);

  /**
   * AtspiAccessible::mode-changed:
   * @atspiaccessible: the object which received the signal
   * @arg1: a boolean specifying whether the mode is being toggled on or off.
   * @why: an optional string explaining why the mode changed.
   *
   * The signal "mode-changed" is emitted by a screen reader to indicate
   * that its mode has changed. This signal supports the following details:
   * focus-tracking
   * flat-review
   * mouse-review
   * say-all
   * caret-tracking
   */
  atspi_accessible_signals[MODE_CHANGED] =
      g_signal_new ("mode_changed",
                    G_TYPE_FROM_CLASS (klass),
                    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                    G_STRUCT_OFFSET (AtspiAccessibleClass, mode_changed),
                    NULL, NULL,
                    atspi_marshal_VOID__INT_STRING,
                    G_TYPE_NONE,
                    2, G_TYPE_INT, G_TYPE_STRING);

  g_signal_add_emission_hook (atspi_accessible_signals[REGION_CHANGED], 0,
                              screen_reader_signal_watcher, NULL,
                              (GDestroyNotify) NULL);
  g_signal_add_emission_hook (atspi_accessible_signals[MODE_CHANGED], 0,
                              screen_reader_signal_watcher, NULL,
                              (GDestroyNotify) NULL);
}

/**
 * atspi_accessible_get_name:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the name of an #AtspiAccessible object.
 *
 * Returns: a UTF-8 string indicating the name of the #AtspiAccessible object
 * or NULL on exception.
 **/
gchar *
atspi_accessible_get_name (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, g_strdup (""));

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_NAME))
    {
      g_free (obj->name);
      obj->name = NULL;
      if (!_atspi_dbus_get_property (obj, atspi_interface_accessible, "Name", error,
                                     "s", &obj->name))
        return g_strdup ("");
      _atspi_accessible_add_cache (obj, ATSPI_CACHE_NAME);
    }
  return g_strdup (obj->name);
}

/**
 * atspi_accessible_get_description:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the description of an #AtspiAccessible object.
 *
 * Returns: a UTF-8 string describing the #AtspiAccessible object
 * or NULL on exception.
 **/
gchar *
atspi_accessible_get_description (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, g_strdup (""));

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_DESCRIPTION))
    {
      g_free (obj->description);
      obj->description = NULL;
      if (!_atspi_dbus_get_property (obj, atspi_interface_accessible,
                                     "Description", error, "s",
                                     &obj->description))
        return g_strdup ("");
      _atspi_accessible_add_cache (obj, ATSPI_CACHE_DESCRIPTION);
    }
  return g_strdup (obj->description);
}

const char *str_parent = "Parent";

/**
 * atspi_accessible_get_parent:
 * @obj: a pointer to the #AtspiAccessible object to query.
 *
 * Gets an #AtspiAccessible object's parent container.
 *
 * Returns: (nullable) (transfer full): a pointer to the
 *          #AtspiAccessible object which contains the given
 *          #AtspiAccessible instance, or NULL if the @obj has no
 *          parent container.
 *
 **/
AtspiAccessible *
atspi_accessible_get_parent (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, NULL);

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_PARENT))
    {
      DBusMessage *message, *reply;
      DBusMessageIter iter, iter_variant;
      if (!obj->parent.app)
        return NULL;
      message = dbus_message_new_method_call (obj->parent.app->bus_name,
                                              obj->parent.path,
                                              DBUS_INTERFACE_PROPERTIES, "Get");
      if (!message)
        return NULL;
      dbus_message_append_args (message, DBUS_TYPE_STRING, &atspi_interface_accessible,
                                DBUS_TYPE_STRING, &str_parent,
                                DBUS_TYPE_INVALID);
      reply = _atspi_dbus_send_with_reply_and_block (message, error);
      if (!reply)
        return NULL;
      if (strcmp (dbus_message_get_signature (reply), "v") != 0)
        {
          dbus_message_unref (reply);
          return NULL;
        }
      dbus_message_iter_init (reply, &iter);
      dbus_message_iter_recurse (&iter, &iter_variant);
      g_clear_object (&obj->accessible_parent);
      obj->accessible_parent = _atspi_dbus_consume_accessible (&iter_variant);
      dbus_message_unref (reply);
      _atspi_accessible_add_cache (obj, ATSPI_CACHE_PARENT);
    }
  if (!obj->accessible_parent)
    return NULL;
  return g_object_ref (obj->accessible_parent);
}

/**
 * atspi_accessible_get_child_count:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the number of children contained by an #AtspiAccessible object.
 *
 * Returns: a #long indicating the number of #AtspiAccessible children
 *          contained by an #AtspiAccessible object or -1 on exception.
 *
 **/
gint
atspi_accessible_get_child_count (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, -1);

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_CHILDREN))
    {
      dbus_int32_t ret;
      if (!_atspi_dbus_get_property (obj, atspi_interface_accessible,
                                     "ChildCount", error, "i", &ret))
        return -1;
      return ret;
    }

  if (!obj->children)
    return 0; /* assume it's disposed */

  return obj->children->len;
}

/**
 * atspi_accessible_get_child_at_index:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 * @child_index: a #long indicating which child is specified.
 *
 * Gets the #AtspiAccessible child of an #AtspiAccessible object at a given index.
 *
 * Returns: (transfer full): a pointer to the #AtspiAccessible child object at
 * index @child_index or NULL on exception.
 **/
AtspiAccessible *
atspi_accessible_get_child_at_index (AtspiAccessible *obj,
                                     gint child_index,
                                     GError **error)
{
  AtspiAccessible *child;
  DBusMessage *reply;

  g_return_val_if_fail (obj != NULL, NULL);

  if (_atspi_accessible_test_cache (obj, ATSPI_CACHE_CHILDREN))
    {
      if (!obj->children)
        return NULL; /* assume disposed */

      if (child_index < obj->children->len)
        {
          child = g_ptr_array_index (obj->children, child_index);
          if (child)
            return g_object_ref (child);
        }
    }

  reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
                                    "GetChildAtIndex", error, "i", child_index);
  child = _atspi_dbus_return_accessible_from_message (reply);

  if (!child)
    return NULL;

  if (_atspi_accessible_test_cache (obj, ATSPI_CACHE_CHILDREN))
    {
      if (child_index >= obj->children->len)
        g_ptr_array_set_size (obj->children, child_index + 1);
      g_ptr_array_index (obj->children, child_index) = g_object_ref (child);
    }
  return child;
}

/**
 * atspi_accessible_get_index_in_parent:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the index of an #AtspiAccessible object within its parent's
 * #AtspiAccessible children list.
 *
 * Returns: a #glong indicating the index of the #AtspiAccessible object
 *          in its parent,
 *          or -1 if @obj has no containing parent or on exception.
 **/
gint
atspi_accessible_get_index_in_parent (AtspiAccessible *obj, GError **error)
{
  gint i = 0;
  dbus_int32_t ret = -1;

  g_return_val_if_fail (obj != NULL, -1);
  if (_atspi_accessible_test_cache (obj, ATSPI_CACHE_PARENT))
    {
      if (!obj->accessible_parent)
        return -1;

      if (!_atspi_accessible_test_cache (obj->accessible_parent, ATSPI_CACHE_CHILDREN) || !obj->accessible_parent->children)
        goto dbus;

      for (i = 0; i < obj->accessible_parent->children->len; i++)
        if (g_ptr_array_index (obj->accessible_parent->children, i) == obj)
          return i;
    }

dbus:
  _atspi_dbus_call (obj, atspi_interface_accessible,
                    "GetIndexInParent", NULL, "=>i", &ret);
  return ret;
}

typedef struct
{
  dbus_uint32_t type;
  GArray *targets;
} Accessibility_Relation;

/**
 * atspi_accessible_get_relation_set:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the set of #AtspiRelation objects which describes this #AtspiAccessible object's
 * relationships with other #AtspiAccessible objects.
 *
 * Returns: (element-type AtspiRelation*) (transfer full): a #GArray of
 *          #AtspiRelation pointers or NULL on exception.
 **/
GArray *
atspi_accessible_get_relation_set (AtspiAccessible *obj, GError **error)
{
  DBusMessage *reply;
  DBusMessageIter iter, iter_array;
  GArray *ret;

  g_return_val_if_fail (obj != NULL, NULL);

  reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible, "GetRelationSet", error, "");
  if (!reply)
    return NULL;
  _ATSPI_DBUS_CHECK_SIG (reply, "a(ua(so))", error, NULL);

  ret = g_array_new (TRUE, TRUE, sizeof (AtspiRelation *));
  dbus_message_iter_init (reply, &iter);
  dbus_message_iter_recurse (&iter, &iter_array);
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
    {
      AtspiRelation *relation;
      relation = _atspi_relation_new_from_iter (&iter_array);
      ret = g_array_append_val (ret, relation);
      dbus_message_iter_next (&iter_array);
    }
  dbus_message_unref (reply);
  return ret;
}

/**
 * atspi_accessible_get_role:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the UI role played by an #AtspiAccessible object.
 * This role's name can be obtained via atspi_accessible_get_role_name ().
 *
 * Returns: the #AtspiRole of an #AtspiAccessible object.
 *
 **/
AtspiRole
atspi_accessible_get_role (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, ATSPI_ROLE_INVALID);

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_ROLE))
    {
      dbus_uint32_t role;
      /* TODO: Make this a property */
      if (_atspi_dbus_call (obj, atspi_interface_accessible, "GetRole", error, "=>u", &role))
        {
          obj->role = role;
          _atspi_accessible_add_cache (obj, ATSPI_CACHE_ROLE);
        }
    }
  return obj->role;
}

/**
 * atspi_accessible_get_role_name:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets a UTF-8 string corresponding to the name of the role played by an object.
 * This method will return useful values for roles that fall outside the
 * enumeration used in atspi_accessible_get_role ().
 *
 * Returns: a UTF-8 string specifying the type of UI role played by an
 * #AtspiAccessible object.
 *
 **/
gchar *
atspi_accessible_get_role_name (AtspiAccessible *obj, GError **error)
{
  gchar *retval = NULL;
  AtspiRole role;

  g_return_val_if_fail (obj != NULL, NULL);

  role = atspi_accessible_get_role (obj, error);
  if (role >= 0 && role < ATSPI_ROLE_COUNT && role != ATSPI_ROLE_EXTENDED)
    return atspi_role_get_name (role);

  _atspi_dbus_call (obj, atspi_interface_accessible, "GetRoleName", error, "=>s", &retval);

  if (!retval)
    retval = g_strdup ("");

  return retval;
}

/**
 * atspi_accessible_get_localized_role_name:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets a UTF-8 string corresponding to the name of the role played by an
 * object, translated to the current locale.
 * This method will return useful values for roles that fall outside the
 * enumeration used in atspi_accessible_getRole ().
 *
 * Returns: a localized, UTF-8 string specifying the type of UI role played
 * by an #AtspiAccessible object.
 *
 **/
gchar *
atspi_accessible_get_localized_role_name (AtspiAccessible *obj, GError **error)
{
  char *retval = NULL;
  AtspiRole role;

  g_return_val_if_fail (obj != NULL, NULL);

  role = atspi_accessible_get_role (obj, error);
  if (role >= 0 && role < ATSPI_ROLE_COUNT && role != ATSPI_ROLE_EXTENDED)
    return g_strdup (atspi_role_get_localized_name (role));

  _atspi_dbus_call (obj, atspi_interface_accessible, "GetLocalizedRoleName", error, "=>s", &retval);

  if (!retval)
    return g_strdup ("");

  return retval;
}

static AtspiStateSet *
defunct_set ()
{
  AtspiStateSet *set = atspi_state_set_new (NULL);
  atspi_state_set_add (set, ATSPI_STATE_DEFUNCT);
  return set;
}

/**
 * atspi_accessible_get_state_set:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the states currently held by an object.
 *
 * Returns: (transfer full): a pointer to an #AtspiStateSet representing an
 * object's current state set.
 **/
AtspiStateSet *
atspi_accessible_get_state_set (AtspiAccessible *obj)
{
  /* TODO: Should take a GError **, but would be an API break */
  if (!obj->parent.app || !obj->parent.app->bus)
    return defunct_set ();

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_STATES))
    {
      DBusMessage *reply;
      DBusMessageIter iter;
      reply = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
                                        "GetState", NULL, "");
      _ATSPI_DBUS_CHECK_SIG (reply, "au", NULL, defunct_set ());
      dbus_message_iter_init (reply, &iter);
      _atspi_dbus_set_state (obj, &iter);
      dbus_message_unref (reply);
      _atspi_accessible_add_cache (obj, ATSPI_CACHE_STATES);
    }

  return g_object_ref (obj->states);
}

/**
 * atspi_accessible_get_attributes:
 * @obj: The #AtspiAccessible being queried.
 *
 * Gets the #AttributeSet representing any assigned
 * name-value pair attributes or annotations for this object.
 * For typographic, textual, or textually-semantic attributes, see
 * atspi_text_get_attributes instead.
 *
 * Returns: (element-type gchar* gchar*) (transfer full): The name-value-pair
 * attributes assigned to this object.
 */
GHashTable *
atspi_accessible_get_attributes (AtspiAccessible *obj, GError **error)
{
  DBusMessage *message;

  g_return_val_if_fail (obj != NULL, NULL);

  if (obj->priv->cache)
    {
      GValue *val = g_hash_table_lookup (obj->priv->cache, "Attributes");
      if (val)
        return g_value_dup_boxed (val);
    }

  if (!_atspi_accessible_test_cache (obj, ATSPI_CACHE_ATTRIBUTES))
    {
      message = _atspi_dbus_call_partial (obj, atspi_interface_accessible,
                                          "GetAttributes", error, "");
      g_clear_pointer (&(obj->attributes), g_hash_table_unref);

      obj->attributes = _atspi_dbus_return_hash_from_message (message);
      _atspi_accessible_add_cache (obj, ATSPI_CACHE_ATTRIBUTES);
    }

  if (!obj->attributes)
    return NULL;
  return g_hash_table_ref (obj->attributes);
}

static void
add_to_attribute_array (gpointer key, gpointer value, gpointer data)
{
  GArray **array = (GArray **) data;
  gchar *str = g_strconcat (key, ":", value, NULL);
  *array = g_array_append_val (*array, str);
}

/**
 * atspi_accessible_get_attributes_as_array:
 * @obj: The #AtspiAccessible being queried.
 *
 * Gets a #GArray representing any assigned
 * name-value pair attributes or annotations for this object.
 * For typographic, textual, or textually-semantic attributes, see
 * atspi_text_get_attributes_as_array instead.
 *
 * Returns: (element-type gchar*) (transfer full): The name-value-pair
 *          attributes assigned to this object.
 */
GArray *
atspi_accessible_get_attributes_as_array (AtspiAccessible *obj, GError **error)
{
  DBusMessage *message;

  g_return_val_if_fail (obj != NULL, NULL);

  if (obj->priv->cache)
    {
      GValue *val = g_hash_table_lookup (obj->priv->cache, "Attributes");
      if (val)
        {
          GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
          GHashTable *attributes = g_value_get_boxed (val);
          g_hash_table_foreach (attributes, add_to_attribute_array, &array);
          return array;
        }
    }

  message = _atspi_dbus_call_partial (obj, atspi_interface_accessible, "GetAttributes", error, "");
  return _atspi_dbus_return_attribute_array_from_message (message);
}

/**
 * atspi_accessible_get_application:
 * @obj: The #AtspiAccessible being queried.
 *
 * Gets the containing #AtspiApplication for an object.
 *
 * Returns: (transfer full): the containing #AtspiApplication instance for
 *          this object.
 */
AtspiAccessible *
atspi_accessible_get_application (AtspiAccessible *obj, GError **error)
{
  AtspiAccessible *parent;

  g_object_ref (obj);
  for (;;)
    {
      parent = atspi_accessible_get_parent (obj, NULL);
      if (!parent && obj->parent.app &&
          atspi_accessible_get_role (obj, NULL) != ATSPI_ROLE_APPLICATION)
        {
          AtspiAccessible *root = g_object_ref (obj->parent.app->root);
          if (root)
            {
              g_object_unref (obj);
              if (atspi_accessible_get_role (root, NULL) == ATSPI_ROLE_DESKTOP_FRAME)
                {
                  g_object_unref (root);
                  return NULL;
                }
              return root;
            }
        }
      if (!parent || parent == obj ||
          atspi_accessible_get_role (parent, NULL) == ATSPI_ROLE_DESKTOP_FRAME)
        {
          if (parent)
            g_object_unref (parent);
          return obj;
        }
      g_object_unref (obj);
      obj = parent;
    }
}

/* Application-specific methods */

/**
 * atspi_accessible_get_toolkit_name:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the toolkit name for an #AtspiAccessible object.
 * Only works on application root objects.
 *
 * Returns: a UTF-8 string indicating the toolkit name for the #AtspiAccessible object or NULL on exception.
 **/
gchar *
atspi_accessible_get_toolkit_name (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, NULL);

  if (!obj->parent.app)
    return NULL;

  if (!obj->parent.app->toolkit_name)
    _atspi_dbus_get_property (obj, atspi_interface_application, "ToolkitName",
                              error, "s", &obj->parent.app->toolkit_name);

  return g_strdup (obj->parent.app->toolkit_name);
}

/**
 * atspi_accessible_get_toolkit_version:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the toolkit version for an #AtspiAccessible object.
 * Only works on application root objects.
 *
 * Returns: a UTF-8 string indicating the toolkit version for the #AtspiAccessible object or NULL on exception.
 **/
gchar *
atspi_accessible_get_toolkit_version (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, NULL);

  if (!obj->parent.app)
    return NULL;

  if (!obj->parent.app->toolkit_version)
    _atspi_dbus_get_property (obj, atspi_interface_application, "Version",
                              error, "s", &obj->parent.app->toolkit_version);

  return g_strdup (obj->parent.app->toolkit_version);
}

/**
 * atspi_accessible_get_atspi_version:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the AT-SPI IPC specification version supported by the application
 * pointed to by the #AtspiAccessible object.
 * Only works on application root objects.
 *
 * Returns: a UTF-8 string indicating the AT-SPI version for the #AtspiAccessible object or NULL on exception.
 **/
gchar *
atspi_accessible_get_atspi_version (AtspiAccessible *obj, GError **error)
{
  g_return_val_if_fail (obj != NULL, NULL);

  if (!obj->parent.app)
    return NULL;

  if (!obj->parent.app->atspi_version)
    _atspi_dbus_get_property (obj, atspi_interface_application, "AtspiVersion",
                              error, "s", &obj->parent.app->atspi_version);

  return g_strdup (obj->parent.app->atspi_version);
}

/**
 * atspi_accessible_get_id:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the application id for a #AtspiAccessible object.
 * Only works on application root objects.
 *
 * Returns: a positive #gint indicating the id for the #AtspiAccessible object
 * or -1 on exception.
 **/
gint
atspi_accessible_get_id (AtspiAccessible *obj, GError **error)
{
  gint ret = -1;

  g_return_val_if_fail (obj != NULL, -1);

  if (!_atspi_dbus_get_property (obj, atspi_interface_application, "Id", error, "i", &ret))
    return -1;
  return ret;
}

/* Interface query methods */

static gboolean
_atspi_accessible_is_a (AtspiAccessible *accessible,
                        const char *interface_name)
{
  int n;

  if (accessible == NULL)
    {
      return FALSE;
    }

  if (!_atspi_accessible_test_cache (accessible, ATSPI_CACHE_INTERFACES))
    {
      DBusMessage *reply;
      DBusMessageIter iter;
      reply = _atspi_dbus_call_partial (accessible, atspi_interface_accessible,
                                        "GetInterfaces", NULL, "");
      _ATSPI_DBUS_CHECK_SIG (reply, "as", NULL, FALSE);
      dbus_message_iter_init (reply, &iter);
      _atspi_dbus_set_interfaces (accessible, &iter);
      dbus_message_unref (reply);
    }

  n = _atspi_get_iface_num (interface_name);
  if (n == -1)
    return FALSE;
  return (gboolean) ((accessible->interfaces & (1 << n)) ? TRUE : FALSE);
}

/**
 * atspi_accessible_is_action:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiAction interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiAction interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_action (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_action);
}

/**
 * atspi_accessible_is_application:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiApplication interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiApplication interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_application (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_application);
}

/**
 * atspi_accessible_is_collection:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiCollection interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiCollection interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_collection (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_collection);
}

/**
 * atspi_accessible_is_component:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements #AtspiComponent.
 *
 * Returns: #TRUE if @obj implements the #AtspiComponent interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_component (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_component);
}

/**
 * atspi_accessible_is_document:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiDocument interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiDocument interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_document (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_document);
}

/**
 * atspi_accessible_is_editable_text:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiEditableText interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiEditableText interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_editable_text (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_editable_text);
}

/**
 * atspi_accessible_is_hypertext:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiHypertext interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiHypertext interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_hypertext (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_hypertext);
}

/**
 * atspi_accessible_is_hyperlink:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiHyperlink interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiHypertext interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_hyperlink (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_hyperlink);
}

/**
 * atspi_accessible_is_image:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiImage interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiImage interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_image (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_image);
}

/**
 * atspi_accessible_is_selection:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiSelection interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiSelection interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_selection (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_selection);
}

/**
 * atspi_accessible_is_table:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiTable interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiTable interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_table (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_table);
}

/**
 * atspi_accessible_is_table_cell:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiTableCell interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiTable interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_table_cell (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_table_cell);
}

/**
 * atspi_accessible_is_streamable_content:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiStreamableContent interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiStreamableContent interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_streamable_content (AtspiAccessible *obj)
{
#if 0
  return _atspi_accessible_is_a (obj,
			      atspi_interface_streamable_content);
#else
  g_warning ("Streamable content not implemented");
  return FALSE;
#endif
}

/**
 * atspi_accessible_is_text:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiText interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiText interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_text (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_text);
}

/**
 * atspi_accessible_is_value:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Query whether the specified #AtspiAccessible implements the
 * #AtspiValue interface.
 *
 * Returns: #TRUE if @obj implements the #AtspiValue interface,
 *          #FALSE otherwise.
 **/
gboolean
atspi_accessible_is_value (AtspiAccessible *obj)
{
  return _atspi_accessible_is_a (obj,
                                 atspi_interface_value);
}

/**
 * atspi_accessible_get_action: (rename-to atspi_accessible_get_action_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiAction interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiAction interface
 *          instance, or NULL if @obj does not implement #AtspiAction.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_action_iface instead.
 **/
AtspiAction *
atspi_accessible_get_action (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_action) ? g_object_ref (ATSPI_ACTION (accessible)) : NULL);
}

/**
 * atspi_accessible_get_action_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiAction interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiAction interface
 *          instance, or NULL if @obj does not implement #AtspiAction.
 **/
AtspiAction *
atspi_accessible_get_action_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_action) ? g_object_ref (ATSPI_ACTION (accessible)) : NULL);
}

/**
 * atspi_accessible_get_collection: (rename-to atspi_accessible_get_collection_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiCollection interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiCollection interface
 *          instance, or NULL if @obj does not implement #AtspiCollection.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_collection_iface instead.
 **/
AtspiCollection *
atspi_accessible_get_collection (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_collection) ? g_object_ref (ATSPI_COLLECTION (accessible)) : NULL);
}

/**
 * atspi_accessible_get_collection_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiCollection interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiCollection interface
 *          instance, or NULL if @obj does not implement #AtspiCollection.
 **/
AtspiCollection *
atspi_accessible_get_collection_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_collection) ? g_object_ref (ATSPI_COLLECTION (accessible)) : NULL);
}

/**
 * atspi_accessible_get_component: (rename-to atspi_accessible_get_component_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiComponent interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiComponent interface
 *          instance, or NULL if @obj does not implement #AtspiComponent.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_component_iface instead.
 **/
AtspiComponent *
atspi_accessible_get_component (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_component) ? g_object_ref (ATSPI_COMPONENT (obj)) : NULL);
}

/**
 * atspi_accessible_get_component_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiComponent interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiComponent interface
 *          instance, or NULL if @obj does not implement #AtspiComponent.
 **/
AtspiComponent *
atspi_accessible_get_component_iface (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_component) ? g_object_ref (ATSPI_COMPONENT (obj)) : NULL);
}

/**
 * atspi_accessible_get_document: (rename-to atspi_accessible_get_document_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiDocument interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiDocument interface
 *          instance, or NULL if @obj does not implement #AtspiDocument.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_document_iface instead.
 **/
AtspiDocument *
atspi_accessible_get_document (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_document) ? g_object_ref (ATSPI_DOCUMENT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_document_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiDocument interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiDocument interface
 *          instance, or NULL if @obj does not implement #AtspiDocument.
 **/
AtspiDocument *
atspi_accessible_get_document_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_document) ? g_object_ref (ATSPI_DOCUMENT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_editable_text: (rename-to atspi_accessible_get_editable_text_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiEditableText interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiEditableText interface
 *          instance, or NULL if @obj does not implement #AtspiEditableText.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_editable_text_iface instead.
 **/
AtspiEditableText *
atspi_accessible_get_editable_text (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_editable_text) ? g_object_ref (ATSPI_EDITABLE_TEXT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_editable_text_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiEditableText interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiEditableText interface
 *          instance, or NULL if @obj does not implement #AtspiEditableText.
 **/
AtspiEditableText *
atspi_accessible_get_editable_text_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_editable_text) ? g_object_ref (ATSPI_EDITABLE_TEXT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_hyperlink:
 * @obj: a pointer to the #AtspiAccessible object on which to operate.
 *
 * Gets the #AtspiHyperlink interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): the #AtspiHyperlink object associated with
 *          the given #AtspiAccessible, or NULL if not supported.
 **/
AtspiHyperlink *
atspi_accessible_get_hyperlink (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_hyperlink) ? _atspi_hyperlink_new (accessible->parent.app, accessible->parent.path) : NULL);
}

/**
 * atspi_accessible_get_hypertext: (rename-to atspi_accessible_get_hypertext_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiHypertext interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiHypertext interface
 *          instance, or NULL if @obj does not implement #AtspiHypertext.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_hypertext_iface instead.
 **/
AtspiHypertext *
atspi_accessible_get_hypertext (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_hypertext) ? g_object_ref (ATSPI_HYPERTEXT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_hypertext_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiHypertext interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiHypertext interface
 *          instance, or NULL if @obj does not implement #AtspiHypertext.
 **/
AtspiHypertext *
atspi_accessible_get_hypertext_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_hypertext) ? g_object_ref (ATSPI_HYPERTEXT (accessible)) : NULL);
}

/**
 * atspi_accessible_get_image: (rename-to atspi_accessible_get_image_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiImage interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiImage interface instance, or
 *          NULL if @obj does not implement #AtspiImage.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_image_iface instead.
 **/
AtspiImage *
atspi_accessible_get_image (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_image) ? g_object_ref (ATSPI_IMAGE (accessible)) : NULL);
}

/**
 * atspi_accessible_get_image_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiImage interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiImage interface instance, or
 *          NULL if @obj does not implement #AtspiImage.
 **/
AtspiImage *
atspi_accessible_get_image_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_image) ? g_object_ref (ATSPI_IMAGE (accessible)) : NULL);
}

/**
 * atspi_accessible_get_selection: (rename-to atspi_accessible_get_selection_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiSelection interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiSelection interface
 *          instance, or NULL if @obj does not implement #AtspiSelection.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_selection_iface instead.
 **/
AtspiSelection *
atspi_accessible_get_selection (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_selection) ? g_object_ref (ATSPI_SELECTION (accessible)) : NULL);
}

/**
 * atspi_accessible_get_selection_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiSelection interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiSelection interface
 *          instance, or NULL if @obj does not implement #AtspiSelection.
 **/
AtspiSelection *
atspi_accessible_get_selection_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_selection) ? g_object_ref (ATSPI_SELECTION (accessible)) : NULL);
}

#if 0
/**
 * atspi_accessible_get_streamable_content:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiStreamableContent interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiStreamableContent interface
 *          instance, or NULL if @obj does not implement #AtspiStreamableContent.
 **/
AtspiStreamableContent *
atspi_accessible_get_streamable_content (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_streamable_content) ?
          accessible : NULL);  
}
#endif

/**
 * atspi_accessible_get_table: (rename-to atspi_accessible_get_table_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiTable interface instance, or
 *          NULL if @obj does not implement #AtspiTable.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_table_iface instead.
 **/
AtspiTable *
atspi_accessible_get_table (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_table) ? g_object_ref (ATSPI_TABLE (obj)) : NULL);
}

/**
 * atspi_accessible_get_table_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiTable interface instance, or
 *          NULL if @obj does not implement #AtspiTable.
 **/
AtspiTable *
atspi_accessible_get_table_iface (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_table) ? g_object_ref (ATSPI_TABLE (obj)) : NULL);
}

/**
 * atspi_accessible_get_table_cell:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTableCell interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiTableCell interface instance,
 *          or NULL if @obj does not implement #AtspiTable.
 **/
AtspiTableCell *
atspi_accessible_get_table_cell (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_table_cell) ? g_object_ref (ATSPI_TABLE_CELL (obj)) : NULL);
}

/**
 * atspi_accessible_get_text: (rename-to atspi_accessible_get_text_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiText interface instance, or
 *          NULL if @obj does not implement #AtspiText.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_text_iface instead.
 **/
AtspiText *
atspi_accessible_get_text (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_text) ? g_object_ref (ATSPI_TEXT (obj)) : NULL);
}

/**
 * atspi_accessible_get_text_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiText interface instance, or
 *          NULL if @obj does not implement #AtspiText.
 **/
AtspiText *
atspi_accessible_get_text_iface (AtspiAccessible *obj)
{
  return (_atspi_accessible_is_a (obj, atspi_interface_text) ? g_object_ref (ATSPI_TEXT (obj)) : NULL);
}

/**
 * atspi_accessible_get_value: (rename-to atspi_accessible_get_value_iface)
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiValue interface instance, or
 *          NULL if @obj does not implement #AtspiValue.
 *
 * Deprecated: 2.10: Use atspi_accessible_get_value_iface instead.
 **/
AtspiValue *
atspi_accessible_get_value (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_value) ? g_object_ref (ATSPI_VALUE (accessible)) : NULL);
}

/**
 * atspi_accessible_get_value_iface:
 * @obj: a pointer to the #AtspiAccessible instance to query.
 *
 * Gets the #AtspiTable interface for an #AtspiAccessible.
 *
 * Returns: (transfer full): a pointer to an #AtspiValue interface instance, or
 *          NULL if @obj does not implement #AtspiValue.
 **/
AtspiValue *
atspi_accessible_get_value_iface (AtspiAccessible *accessible)
{
  return (_atspi_accessible_is_a (accessible, atspi_interface_value) ? g_object_ref (ATSPI_VALUE (accessible)) : NULL);
}

static void
append_const_val (GArray *array, const gchar *val)
{
  gchar *dup = g_strdup (val);

  if (dup)
    g_array_append_val (array, dup);
}

/**
 * atspi_accessible_get_interfaces:
 * @obj: The #AtspiAccessible to query.
 *
 * A set of pointers to all interfaces supported by an #AtspiAccessible.
 *
 * Returns: (element-type gchar*) (transfer full): A #GArray of strings
 *          describing the interfaces supported by the object.  Interfaces are
 *          denoted in short-hand (i.e. "Component", "Text" etc.).
 **/
GArray *
atspi_accessible_get_interfaces (AtspiAccessible *obj)
{
  GArray *ret = g_array_new (TRUE, TRUE, sizeof (gchar *));

  g_return_val_if_fail (obj != NULL, NULL);

  append_const_val (ret, "Accessible");
  if (atspi_accessible_is_action (obj))
    append_const_val (ret, "Action");
  if (atspi_accessible_is_collection (obj))
    append_const_val (ret, "Collection");
  if (atspi_accessible_is_component (obj))
    append_const_val (ret, "Component");
  if (atspi_accessible_is_document (obj))
    append_const_val (ret, "Document");
  if (atspi_accessible_is_editable_text (obj))
    append_const_val (ret, "EditableText");
  if (atspi_accessible_is_hypertext (obj))
    append_const_val (ret, "Hypertext");
  if (atspi_accessible_is_hyperlink (obj))
    append_const_val (ret, "Hyperlink");
  if (atspi_accessible_is_image (obj))
    append_const_val (ret, "Image");
  if (atspi_accessible_is_selection (obj))
    append_const_val (ret, "Selection");
  if (atspi_accessible_is_table (obj))
    append_const_val (ret, "Table");
  if (atspi_accessible_is_table_cell (obj))
    append_const_val (ret, "TableCell");
  if (atspi_accessible_is_text (obj))
    append_const_val (ret, "Text");
  if (atspi_accessible_is_value (obj))
    append_const_val (ret, "Value");

  return ret;
}

AtspiAccessible *
_atspi_accessible_new (AtspiApplication *app, const gchar *path)
{
  AtspiAccessible *accessible;

  accessible = g_object_new (ATSPI_TYPE_ACCESSIBLE, NULL);
  g_return_val_if_fail (accessible != NULL, NULL);

  accessible->parent.app = g_object_ref (app);
  accessible->parent.path = g_strdup (path);

  return accessible;
}

/**
 * atspi_accessible_set_cache_mask:
 * @accessible: The #AtspiAccessible to operate on.  Must be the desktop or
 *             the root of an application.
 * @mask: An #AtspiCache specifying a bit mask of the types of data to cache.
 *
 * Sets the type of data to cache for accessibles.
 * If this is not set for an application or is reset to ATSPI_CACHE_UNDEFINED,
 * then the desktop's cache flag will be used.
 * If the desktop's cache flag is also undefined, then all possible data will
 * be cached.
 * This function is intended to work around bugs in toolkits where the proper
 * events are not raised / to aid in testing for such bugs.
 **/
void
atspi_accessible_set_cache_mask (AtspiAccessible *accessible, AtspiCache mask)
{
  g_return_if_fail (accessible != NULL);
  g_return_if_fail (accessible->parent.app != NULL);
  g_return_if_fail (accessible == accessible->parent.app->root || accessible->role == ATSPI_ROLE_APPLICATION);
  accessible->parent.app->cache = mask;
  enable_caching = TRUE;
}

static void
atspi_accessible_clear_cache_internal (AtspiAccessible *obj, guint iteration_stamp)
{
  gint i;

  if (obj && obj->priv->iteration_stamp != iteration_stamp)
    {
      obj->priv->iteration_stamp = iteration_stamp;
      obj->cached_properties = ATSPI_CACHE_NONE;
      if (obj->children)
        for (i = 0; i < obj->children->len; i++)
          atspi_accessible_clear_cache_internal (g_ptr_array_index (obj->children, i), iteration_stamp);
    }
}

/**
 * atspi_accessible_clear_cache:
 * @obj: The #AtspiAccessible whose cache to clear.
 *
 * Clears the cached information for the given accessible and all of its
 * descendants.
 */
void
atspi_accessible_clear_cache (AtspiAccessible *obj)
{
  static guint iteration_stamp = 0;

  atspi_accessible_clear_cache_internal (obj, ++iteration_stamp);
}

/**
 * atspi_accessible_get_process_id:
 * @accessible: The #AtspiAccessible to query.
 * @error: a pointer to a %NULL #GError pointer
 *
 * Returns the process id associated with the given accessible.  Mainly
 * added for debugging; it is a shortcut to explicitly querying the
 * accessible's app->bus_name and then calling GetConnectionUnixProcessID.
 *
 * Returns: The process ID or undetermined value if @error is set.
 **/
guint
atspi_accessible_get_process_id (AtspiAccessible *accessible, GError **error)
{
  DBusMessage *message, *reply;
  DBusConnection *bus = _atspi_bus ();
  dbus_uint32_t pid = -1;
  DBusError d_error;

  if (!accessible->parent.app || !accessible->parent.app->bus_name)
    {
      g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, "Process is defunct");
      return -1;
    }

  message = dbus_message_new_method_call ("org.freedesktop.DBus",
                                          "/org/freedesktop/DBus",
                                          "org.freedesktop.DBus",
                                          "GetConnectionUnixProcessID");
  dbus_message_append_args (message, DBUS_TYPE_STRING,
                            &accessible->parent.app->bus_name,
                            DBUS_TYPE_INVALID);
  dbus_error_init (&d_error);
  reply = dbus_connection_send_with_reply_and_block (bus, message, -1, &d_error);
  dbus_message_unref (message);
  if (reply)
    {
      if (!strcmp (dbus_message_get_signature (reply), "u"))
        dbus_message_get_args (reply, NULL, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID);
      dbus_message_unref (reply);
    }
  if (dbus_error_is_set (&d_error))
    {
      g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, "Process is defunct");
      dbus_error_free (&d_error);
    }
  return pid;
}

AtspiCache
_atspi_accessible_get_cache_mask (AtspiAccessible *accessible)
{
  AtspiCache mask;

  if (!accessible->parent.app)
    return ATSPI_CACHE_NONE;

  mask = accessible->parent.app->cache;
  if (mask == ATSPI_CACHE_UNDEFINED &&
      accessible->parent.app->root &&
      accessible->parent.app->root->accessible_parent)
    {
      AtspiAccessible *desktop = atspi_get_desktop (0);
      mask = desktop->parent.app->cache;
      g_object_unref (desktop);
    }

  if (mask == ATSPI_CACHE_UNDEFINED)
    mask = ATSPI_CACHE_DEFAULT;

  return mask;
}

gboolean
_atspi_accessible_test_cache (AtspiAccessible *accessible, AtspiCache flag)
{
  AtspiCache mask = _atspi_accessible_get_cache_mask (accessible);
  AtspiCache result = accessible->cached_properties & mask & flag;
  if (accessible->states && atspi_state_set_contains (accessible->states, ATSPI_STATE_TRANSIENT))
    return FALSE;
  return (result != 0 && (atspi_main_loop || enable_caching || flag == ATSPI_CACHE_INTERFACES) &&
          !atspi_no_cache);
}

void
_atspi_accessible_add_cache (AtspiAccessible *accessible, AtspiCache flag)
{
  AtspiCache mask = _atspi_accessible_get_cache_mask (accessible);

  accessible->cached_properties |= flag & mask;
}

/**
 * atspi_accessible_get_locale:
 * @accessible: an #AtspiAccessible
 *
 * Gets a UTF-8 string indicating the POSIX-style LC_MESSAGES locale
 * of @accessible.
 *
 * Since: 2.7.91
 *
 * Returns: a UTF-8 string indicating the POSIX-style LC_MESSAGES
 *          locale of @accessible.
 **/
const gchar *
atspi_accessible_get_object_locale (AtspiAccessible *accessible, GError **error)
{
  gchar *locale;

  g_return_val_if_fail (accessible != NULL, NULL);

  locale = g_object_get_qdata (G_OBJECT (accessible), quark_locale);
  if (!locale)
    {
      if (!_atspi_dbus_get_property (accessible, atspi_interface_accessible,
                                     "Locale", error, "s", &locale))
        return NULL;
      if (locale)
        g_object_set_qdata_full (G_OBJECT (accessible), quark_locale, locale,
                                 g_free);
    }
  return locale;
}

/**
 * atspi_accessible_get_accessible_id:
 * @obj: an #AtspiAccessible
 *
 * Gets the accessible id of the accessible.  This is not meant to be presented
 * to the user, but to be an id which is stable over application development.
 * Typically, this is the gtkbuilder id.
 *
 * Since: 2.34
 *
 * Returns: a character string representing the accessible id of the
 * #AtspiAccessible object or NULL on exception.
 **/
gchar *
atspi_accessible_get_accessible_id (AtspiAccessible *obj, GError **error)
{
  gchar *accessible_id;

  g_return_val_if_fail (obj != NULL, NULL);

  if (!_atspi_dbus_get_property (obj, atspi_interface_accessible,
                                 "AccessibleId", error, "s", &accessible_id))
    return NULL;

  return accessible_id;
}

void
free_value (gpointer data)
{
  GValue *value = data;

  g_value_unset (value);
  g_free (value);
}

GHashTable *
_atspi_accessible_ref_cache (AtspiAccessible *accessible)
{
  AtspiAccessiblePrivate *priv = accessible->priv;

  priv->cache_ref_count++;
  if (priv->cache)
    return g_hash_table_ref (priv->cache);
  priv->cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                                       free_value);
  return priv->cache;
}

void
_atspi_accessible_unref_cache (AtspiAccessible *accessible)
{
  AtspiAccessiblePrivate *priv = accessible->priv;

  if (priv->cache)
    {
      g_hash_table_unref (priv->cache);
      if (--priv->cache_ref_count == 0)
        priv->cache = NULL;
    }
}