Avoid reentering spi_dbus_update_cache
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_accessible.c
index a89a36e..958c20c 100644 (file)
+/*
+ * 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.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
 #include <stdlib.h> /* for malloc */
+#include "spi-stateset.h"
 #include <cspi/spi-private.h>
 
 static const char *role_names [] =
 {
-  "<invalid>",
-  "accelerator label",
+  "invalid",
+  "accel-label",
   "alert",
   "animation",
   "arrow",
   "calendar",
   "canvas",
-  "check box",
-  "color chooser",
-  "column header",
-  "combo box",
-  "date editor",
-  "desktop icon",
-  "desktop frame",
+  "check-box",
+  "check-menu-item",
+  "color-chooser",
+  "column-header",
+  "combo-box",
+  "date-editor",
+  "desktop-icon",
+  "desktop-frame",
   "dial",
   "dialog",
-  "directory pane",
-  "file chooser",
+  "directory-pane",
+  "drawing-area",
+  "file-chooser",
   "filler",
-  "focus traversable",
-  "font chooser",
+  "font-chooser",
   "frame",
-  "glass pane",
-  "HTML container",
+  "glass-pane",
+  "html-container",
   "icon",
   "image",
-  "internal frame",
+  "internalframe",
   "label",
-  "layered pane",
+  "layered-pane",
   "list",
-  "list item",
+  "list-item",
   "menu",
-  "menubar",
-  "menu item",
-  "option pane",
-  "page tab",
-  "page tab list",
+  "menu-bar",
+  "menu-item",
+  "option-pane",
+  "page-tab",
+  "page-tab-list",
   "panel",
-  "password text",
-  "popup menu",
-  "progress bar",
-  "pushbutton",
-  "radiobutton",
-  "radio menu item",
-  "root pane",
-  "row header",
-  "scrollbar",
-  "scrollpane",
+  "password-text",
+  "popup-menu",
+  "progress-bar",
+  "push-button",
+  "radio-button",
+  "radio-menu-item",
+  "root-pane",
+  "row-header",
+  "scroll-bar",
+  "scroll-pane",
   "separator",
   "slider",
-  "spin button",
-  "split pane",
-  "status bar",
+  "spin-button",
+  "split-pane",
+  "statusbar",
   "table",
-  "table cell",
-  "table column header",
-  "table row header",
-  "tearoff menu item",
+  "table-cell",
+  "table-column-header",
+  "table-row-header",
+  "tear-off-menu-item",
+  "terminal",
   "text",
-  "toggle button",
-  "toolbar",
-  "tooltip",
+  "toggle-button",
+  "tool-bar",
+  "tool-tip",
   "tree",
   "tree-table",
-  "<unknown>",
+  "unknown",
   "viewport",
   "window",
-
+  NULL,
+  "header",
+  "fooler",
+  "paragraph",
+  "ruler",
+  "application",
+  "autocomplete",
+  "editbar",
+  "embedded",
+  "entry",
+  "chart",
+  "caption",
+  "document_frame",
+  "heading",
+  "page",
+  "section",
+  "form",
+  "redundant object",
+  "link",
+  "input method window"
 };
 
 #define MAX_ROLES (sizeof (role_names) / sizeof (char *))
@@ -101,6 +146,7 @@ cspi_init_role_table (AccessibleRole *role_table)
   role_table [Accessibility_ROLE_DIAL] = SPI_ROLE_DIAL;
   role_table [Accessibility_ROLE_DIALOG] = SPI_ROLE_DIALOG;
   role_table [Accessibility_ROLE_DIRECTORY_PANE] = SPI_ROLE_DIRECTORY_PANE;
+  role_table [Accessibility_ROLE_DRAWING_AREA] = SPI_ROLE_DRAWING_AREA;
   role_table [Accessibility_ROLE_FILE_CHOOSER] = SPI_ROLE_FILE_CHOOSER;
   role_table [Accessibility_ROLE_FILLER] = SPI_ROLE_FILLER;
   role_table [Accessibility_ROLE_FONT_CHOOSER] = SPI_ROLE_FONT_CHOOSER;
@@ -138,23 +184,43 @@ cspi_init_role_table (AccessibleRole *role_table)
   role_table [Accessibility_ROLE_STATUS_BAR] = SPI_ROLE_STATUS_BAR;
   role_table [Accessibility_ROLE_TABLE] = SPI_ROLE_TABLE;
   role_table [Accessibility_ROLE_TABLE_CELL] = SPI_ROLE_TABLE_CELL;
+  role_table[Accessibility_ROLE_TABLE_COLUMN_HEADER] = SPI_ROLE_TABLE_COLUMN_HEADER;
+  role_table[Accessibility_ROLE_TABLE_ROW_HEADER] = SPI_ROLE_TABLE_ROW_HEADER;
   role_table [Accessibility_ROLE_TEAROFF_MENU_ITEM] = SPI_ROLE_TEAROFF_MENU_ITEM;
+  role_table[Accessibility_ROLE_TERMINAL] = SPI_ROLE_TERMINAL;
   role_table [Accessibility_ROLE_TEXT] = SPI_ROLE_TEXT;
   role_table [Accessibility_ROLE_TOGGLE_BUTTON] = SPI_ROLE_TOGGLE_BUTTON;
   role_table [Accessibility_ROLE_TOOL_BAR] = SPI_ROLE_TOOL_BAR;
   role_table [Accessibility_ROLE_TOOL_TIP] = SPI_ROLE_TOOL_TIP;
   role_table [Accessibility_ROLE_TREE] = SPI_ROLE_TREE;
   role_table [Accessibility_ROLE_TREE_TABLE] = SPI_ROLE_TREE_TABLE;
-  role_table [Accessibility_ROLE_UNKNOWN] = SPI_ROLE_UNKNOWN;
   role_table [Accessibility_ROLE_VIEWPORT] = SPI_ROLE_VIEWPORT;
   role_table [Accessibility_ROLE_WINDOW] = SPI_ROLE_WINDOW;
   role_table [Accessibility_ROLE_EXTENDED] = SPI_ROLE_EXTENDED;
-  role_table [Accessibility_ROLE_LAST_DEFINED] = SPI_ROLE_EXTENDED;
+  role_table [Accessibility_ROLE_HEADER] = SPI_ROLE_HEADER;
+  role_table [Accessibility_ROLE_FOOTER] = SPI_ROLE_FOOTER;
+  role_table [Accessibility_ROLE_PARAGRAPH] = SPI_ROLE_PARAGRAPH;
+  role_table [Accessibility_ROLE_RULER] = SPI_ROLE_RULER;
+  role_table [Accessibility_ROLE_APPLICATION] = SPI_ROLE_APPLICATION;
+  role_table [Accessibility_ROLE_AUTOCOMPLETE] = SPI_ROLE_AUTOCOMPLETE;
+  role_table [Accessibility_ROLE_EDITBAR] = SPI_ROLE_EDITBAR;
+  role_table [Accessibility_ROLE_EMBEDDED] = SPI_ROLE_EMBEDDED;
+  role_table [Accessibility_ROLE_ENTRY] = SPI_ROLE_ENTRY;
+  role_table [Accessibility_ROLE_CHART] = SPI_ROLE_CHART;
+  role_table [Accessibility_ROLE_CAPTION] = SPI_ROLE_CAPTION;
+  role_table [Accessibility_ROLE_DOCUMENT_FRAME] = SPI_ROLE_DOCUMENT_FRAME;
+  role_table [Accessibility_ROLE_HEADING] = SPI_ROLE_HEADING;
+  role_table [Accessibility_ROLE_PAGE] = SPI_ROLE_PAGE;
+  role_table [Accessibility_ROLE_SECTION] = SPI_ROLE_SECTION;
+  role_table [Accessibility_ROLE_FORM] = SPI_ROLE_FORM;
+  role_table [Accessibility_ROLE_REDUNDANT_OBJECT] = SPI_ROLE_REDUNDANT_OBJECT;
+  role_table [Accessibility_ROLE_LINK] = SPI_ROLE_LINK;
+  role_table [Accessibility_ROLE_INPUT_METHOD_WINDOW] = SPI_ROLE_INPUT_METHOD_WINDOW;
 
   return TRUE;
 }
 
-static AccessibleRole
+AccessibleRole
 cspi_role_from_spi_role (Accessibility_Role role)
 {
   /* array is sized according to IDL roles because IDL roles are the index */  
@@ -176,6 +242,23 @@ cspi_role_from_spi_role (Accessibility_Role role)
   return cspi_role; 
 }
 
+AccessibleAttributeSet *
+_cspi_attribute_set_from_sequence (const GArray *seq)
+{
+    AccessibleAttributeSet *set = g_new0 (AccessibleAttributeSet, 1);
+    int i;
+
+    set->len = seq->len;
+    set->attributes = g_new0 (char *, set->len);
+    for (i = 0; i < set->len; ++i)
+    {
+       set->attributes[i] = g_array_index (seq, char *, i);
+    }
+
+  g_array_free (seq, TRUE);
+    return set;
+}
+
 /**
  * AccessibleRole_getName:
  * @role: an #AccessibleRole object to query.
@@ -188,7 +271,7 @@ cspi_role_from_spi_role (Accessibility_Role role)
 char *
 AccessibleRole_getName (AccessibleRole role)
 {
-  if (role < MAX_ROLES)
+  if (role < MAX_ROLES && role_names [(int) role])
     {
       return g_strdup (role_names [(int) role]);
     }
@@ -234,16 +317,8 @@ Accessible_unref (Accessible *obj)
 char *
 Accessible_getName (Accessible *obj)
 {
-  char *retval;
-
   cspi_return_val_if_fail (obj != NULL, NULL);
-
-  retval = (char *)
-    Accessibility_Accessible__get_name (CSPI_OBJREF (obj), cspi_ev ());
-
-  cspi_return_val_if_ev ("getName", NULL); 
-
-  return retval;
+  return g_strdup (obj->name);
 }
 
 /**
@@ -258,17 +333,9 @@ Accessible_getName (Accessible *obj)
 char *
 Accessible_getDescription (Accessible *obj)
 {
-  char *retval;
-
   cspi_return_val_if_fail (obj != NULL, NULL);
 
-  retval = (char *)
-    Accessibility_Accessible__get_description (CSPI_OBJREF (obj),
-                                              cspi_ev ());
-
-  cspi_return_val_if_ev ("getDescription", NULL); 
-
-  return retval;
+  return g_strdup (obj->description);
 }
 
 /**
@@ -284,17 +351,9 @@ Accessible_getDescription (Accessible *obj)
 Accessible *
 Accessible_getParent (Accessible *obj)
 {
-  Accessible *retval;
-
   cspi_return_val_if_fail (obj != NULL, NULL);
 
-  retval = cspi_object_add (
-    Accessibility_Accessible__get_parent (CSPI_OBJREF (obj),
-                                         cspi_ev ()));
-
-  cspi_return_val_if_ev ("getParent", NULL); 
-
-  return retval;
+  return cspi_object_add (obj->parent);
 }
 
 /**
@@ -310,17 +369,9 @@ Accessible_getParent (Accessible *obj)
 long
 Accessible_getChildCount (Accessible *obj)
 {
-  long retval;
-
   cspi_return_val_if_fail (obj != NULL, -1);
 
-  retval = (long) 
-    Accessibility_Accessible__get_childCount (CSPI_OBJREF (obj),
-                                             cspi_ev ());
-
-  cspi_return_val_if_ev ("getChildCount", -1); 
-
-  return retval;
+  return g_list_length (obj->children);
 }
 
 /**
@@ -337,15 +388,12 @@ Accessible *
 Accessible_getChildAtIndex (Accessible *obj,
                             long int    childIndex)
 {
-  Accessible *retval;
+  Accessible *child;
 
   cspi_return_val_if_fail (obj != NULL, NULL);
 
-  retval = cspi_object_add (
-    Accessibility_Accessible_getChildAtIndex (CSPI_OBJREF (obj),
-                                             childIndex, cspi_ev ()));
-
-  return retval;
+  child = g_list_nth_data (obj->children, childIndex);
+  return cspi_object_add(child);
 }
 
 /**
@@ -361,17 +409,27 @@ Accessible_getChildAtIndex (Accessible *obj,
 long
 Accessible_getIndexInParent (Accessible *obj)
 {
-  long retval;
+  GList *l;
+  gint i;
 
   cspi_return_val_if_fail (obj != NULL, -1);
-
-  retval = (long)
-    Accessibility_Accessible_getIndexInParent (CSPI_OBJREF (obj), cspi_ev ());
-
-  cspi_return_val_if_ev ("getIndexInparent", -1); 
-  return retval;
+  if (!obj->parent) return -1;
+  l = obj->parent->children;
+  while (l)
+  {
+    if (l->data == obj) return i;
+    l = g_list_next (l);
+    i++;
+  }
+  return -1;
 }
 
+typedef struct
+{
+  dbus_uint32_t type;
+  GArray *targets;
+} Accessibility_Relation;
+
 /**
  * Accessible_getRelationSet:
  * @obj: a pointer to the #Accessible object on which to operate.
@@ -387,27 +445,33 @@ Accessible_getRelationSet (Accessible *obj)
   int i;
   int n_relations;
   AccessibleRelation **relations;
-  Accessibility_RelationSet *relation_set;
+  GArray *relation_set;
+  DBusError error;
 
   cspi_return_val_if_fail (obj != NULL, NULL);
 
-  relation_set =
-    Accessibility_Accessible_getRelationSet (CSPI_OBJREF (obj), cspi_ev ());
+  g_assert (!cspi_exception ());
+
+  dbus_error_init (&error);
+  cspi_dbus_call (obj, spi_interface_accessible, "getAttributes", &error, "=>a(uao)", &relation_set);
 
   cspi_return_val_if_ev ("getRelationSet", NULL); 
   
-  /* this looks hack-ish, but it's based on the CORBA C bindings spec */
-  n_relations = relation_set->_length;
-  relations = malloc (sizeof (AccessibleRelation *) * n_relations);
+  n_relations = relation_set->len;
+  relations = malloc (sizeof (AccessibleRelation *) * (n_relations + 1));
   
   for (i = 0; i < n_relations; ++i)
     {
-      relations[i] = cspi_object_add (CORBA_Object_duplicate (
-             relation_set->_buffer[i], cspi_ev ()));
+      Accessibility_Relation *r = g_array_index (relation_set, Accessibility_Relation *, i);
+      relations[i] = g_new (AccessibleRelation, 1);
+      if (!relations[i]) continue;
+      relations[i]->ref_count = 1;
+      relations[i]->type = r->type;
+      relations[i]->targets = r->targets;
     }
-  relations[i] = CORBA_OBJECT_NIL;
+  relations[i] = NULL;
 
-  CORBA_free (relation_set);
+  g_array_free (relation_set, TRUE);
 
   return relations;
 }
@@ -425,16 +489,9 @@ Accessible_getRelationSet (Accessible *obj)
 AccessibleRole
 Accessible_getRole (Accessible *obj)
 {
-  AccessibleRole retval;
-
   cspi_return_val_if_fail (obj != NULL, SPI_ROLE_INVALID);
 
-  retval = 
-    Accessibility_Accessible_getRole (CSPI_OBJREF (obj), cspi_ev ());
-
-  cspi_return_val_if_ev ("getRole", SPI_ROLE_INVALID); 
-
-  return cspi_role_from_spi_role (retval);
+  return obj->role;
 }
 
 /**
@@ -453,12 +510,38 @@ Accessible_getRoleName (Accessible *obj)
 {
   char *retval;
 
-  cspi_return_val_if_fail (obj != NULL, CORBA_string_dup ("invalid"));
+  cspi_return_val_if_fail (obj != NULL, g_strdup ("invalid"));
+
+  cspi_dbus_call (obj, spi_interface_accessible, "getRoleName", NULL, "=>s", &retval);
+
+  cspi_return_val_if_ev ("getRoleName", g_strdup ("invalid")); 
+
+  return retval;
+}
+
+/**
+ * Accessible_getLocalizedRoleName:
+ * @obj: a pointer to the #Accessible object on which to operate.
+ *
+ * Get a UTF-8 string describing the (localized) role this object plays in the UI.
+ * This method will return useful values for roles that fall outside the
+ * enumeration used in Accessible_getRole ().
+ *
+ * @Since: AT-SPI 1.4
+ *
+ * Returns: a UTF-8 string specifying the role of this #Accessible object.
+ *
+ **/
+char *
+Accessible_getLocalizedRoleName (Accessible *obj)
+{
+  char *retval;
+
+  cspi_return_val_if_fail (obj != NULL, g_strdup ("invalid"));
 
-  retval = 
-    Accessibility_Accessible_getRoleName (CSPI_OBJREF (obj), cspi_ev ());
+  cspi_dbus_call (obj, spi_interface_accessible, "getLocalizedRoleName", NULL, "=>s", &retval);
 
-  cspi_return_val_if_ev ("getRoleName", CORBA_string_dup ("invalid")); 
+  cspi_return_val_if_ev ("getLocalizedRoleName", g_strdup ("invalid")); 
 
   return retval;
 }
@@ -467,14 +550,69 @@ Accessible_getRoleName (Accessible *obj)
  * Accessible_getStateSet:
  * @obj: a pointer to the #Accessible object on which to operate.
  *
- * Not Yet Implemented.
+ * Gets the current state of an object.
  *
  * Returns: a pointer to an #AccessibleStateSet representing the object's current state.
  **/
 AccessibleStateSet *
 Accessible_getStateSet (Accessible *obj)
 {
-  return NULL;
+  GArray *state_bitflags;
+  AccessibleStateSet *retval;
+
+  cspi_return_val_if_fail (obj != NULL, NULL);
+
+  cspi_dbus_call (obj, spi_interface_accessible, "getStateSet", NULL, "=>au", &state_bitflags);
+  cspi_return_val_if_ev ("getState", NULL);
+
+  retval = spi_state_set_cache_new (state_bitflags);
+
+  g_array_free (state_bitflags, TRUE);
+
+  return retval;
+}
+
+/**
+ * Accessible_getAttributes:
+ * @obj: The #Accessible being queried.
+ *
+ * Get the #AttributeSet representing any assigned 
+ * name-value pair attributes or annotations for this object.
+ * For typographic, textual, or textually-semantic attributes, see
+ * AccessibleText_getAttributes instead.
+ *
+ * Returns: The name-value-pair attributes assigned to this object.
+ */
+AccessibleAttributeSet *
+Accessible_getAttributes (Accessible *obj)
+{
+    AccessibleAttributeSet *retval;
+  GArray *dbus_seq;
+
+    cspi_return_val_if_fail (obj != NULL, NULL);
+
+  cspi_dbus_call (obj, spi_interface_accessible, "getAttributes", NULL, "=>as", &dbus_seq);
+
+    cspi_return_val_if_ev ("getAttributes", NULL);
+    
+    retval = _cspi_attribute_set_from_sequence (dbus_seq);
+
+    return retval;
+}
+
+/**
+ * Accessible_getHostApplication:
+ * @obj: The #Accessible being queried.
+ *
+ * Get the containing #AccessibleApplication for an object.
+ *
+ * Returns: the containing AccessibleApplication instance for this object.
+ */
+AccessibleApplication *
+Accessible_getHostApplication (Accessible *obj)
+{
+  while (obj->parent) obj = obj->parent;
+  return obj;
 }
 
 /* Interface query methods */
@@ -492,7 +630,7 @@ SPIBoolean
 Accessible_isAction (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Action:1.0");
+                             spi_interface_action);
 }
 
 /**
@@ -508,7 +646,27 @@ SPIBoolean
 Accessible_isApplication (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Application:1.0");
+                             spi_interface_application);
+}
+
+/**                      
+ * Accessible_isCollection:                                                                                                                                                                          * @obj: a pointer to the #Accessible instance to query.                                                                                                                                          
+ *                          
+ * Query whether the specified #Accessible implements #AccessibleCollection.    
+ * Returns: #TRUE if @obj implements the #AccessibleCollection interface,                                                                                                               
+ *          #FALSE otherwise.
+ **/
+
+SPIBoolean
+Accessible_isCollection (Accessible *obj)
+{
+#if 0
+     g_warning ("Collections not implemented");
+     return cspi_accessible_is_a (obj,
+                             spi_interface_collection);
+#else
+     return FALSE;
+#endif
 }
 
 /**
@@ -524,7 +682,23 @@ SPIBoolean
 Accessible_isComponent (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Component:1.0");
+                             spi_interface_component);
+}
+
+/**
+ * Accessible_isDocument:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Query whether the specified #Accessible implements #AccessibleDocument.
+ *
+ * Returns: #TRUE if @obj implements the #AccessibleDocument interface,
+ *          #FALSE otherwise.
+ **/
+SPIBoolean
+Accessible_isDocument (Accessible *obj)
+{
+  return cspi_accessible_is_a (obj,
+                             spi_interface_document);
 }
 
 /**
@@ -540,7 +714,28 @@ SPIBoolean
 Accessible_isEditableText (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/EditableText:1.0");
+                             spi_interface_editable_text);
+}
+                                                                                                                                                                        
+/**
+ * Accessible_isMatchRule:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Query whether the specified #Accessible implements #AccessibleMatchRule.
+ *
+ * Returns: #TRUE if @obj implements the #AccessibleMatchRule interface,
+ *          #FALSE otherwise.
+ **/
+SPIBoolean
+Accessible_isMatchRule (Accessible *obj)
+{
+#if 0
+     return cspi_accessible_is_a (obj, 
+                                 spi_interface_match_rule);
+#else
+     g_warning ("Match rules not implemented");
+     return FALSE;
+#endif
 }
 
 /**
@@ -556,7 +751,7 @@ SPIBoolean
 Accessible_isHypertext (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Hypertext:1.0");
+                             spi_interface_hypertext);
 }
 
 /**
@@ -572,7 +767,7 @@ SPIBoolean
 Accessible_isImage (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Image:1.0");
+                             spi_interface_image);
 }
 
 /**
@@ -588,7 +783,7 @@ SPIBoolean
 Accessible_isSelection (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Selection:1.0");
+                             spi_interface_selection);
 }
 
 /**
@@ -604,7 +799,7 @@ SPIBoolean
 Accessible_isTable (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Table:1.0");
+                             spi_interface_table);
 }
 
 /**
@@ -620,8 +815,13 @@ Accessible_isTable (Accessible *obj)
 SPIBoolean
 Accessible_isStreamableContent (Accessible *obj)
 {
+#if 0
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/StreamableContent:1.0");
+                             spi_interface_streamable_content);
+#else
+  g_warning ("Streamable content not implemented");
+  return FALSE;
+#endif
 }
 
 /**
@@ -637,7 +837,7 @@ SPIBoolean
 Accessible_isText (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Text:1.0");
+                             spi_interface_text);
 }
 
 /**
@@ -653,7 +853,7 @@ SPIBoolean
 Accessible_isValue (Accessible *obj)
 {
   return cspi_accessible_is_a (obj,
-                             "IDL:Accessibility/Value:1.0");
+                             spi_interface_value);
 }
 
 /**
@@ -669,7 +869,7 @@ AccessibleApplication *
 Accessible_getApplication (Accessible *obj)
 {
   return (AccessibleApplication *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Application:1.0");
+         obj, spi_interface_application);
 }
 
 /**
@@ -685,7 +885,28 @@ AccessibleAction *
 Accessible_getAction (Accessible *obj)
 {
   return (AccessibleAction *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Action:1.0");
+         obj, spi_interface_action);
+}
+
+
+/**
+ * Accessible_getCollection:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Get the #AccessibleCollection interface for an #Accessible.
+ *
+ * Returns: a pointer to an #AccessibleCollection interface instance, or
+ *          NULL if @obj does not implement #AccessibleCollection.
+ **/
+AccessibleCollection *
+Accessible_getCollection (Accessible *obj)
+{
+#if 0
+  return (AccessibleCollection *) Accessible_queryInterface (
+         obj, spi_interface_collection);
+#else
+  g_warning ("Collections not implemented");
+#endif
 }
 
 /**
@@ -701,7 +922,23 @@ AccessibleComponent *
 Accessible_getComponent (Accessible *obj)
 {
   return (AccessibleComponent *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Component:1.0");
+         obj, spi_interface_component);
+}
+
+/**
+ * Accessible_getDocument:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Get the #AccessibleDocument interface for an #Accessible.
+ *
+ * Returns: a pointer to an #AccessibleDocument interface instance, or
+ *          NULL if @obj does not implement #AccessibleDocument.
+ **/
+AccessibleDocument *
+Accessible_getDocument (Accessible *obj)
+{
+  return (AccessibleDocument *) Accessible_queryInterface (
+         obj, spi_interface_document);
 }
 
 /**
@@ -717,11 +954,10 @@ AccessibleEditableText *
 Accessible_getEditableText (Accessible *obj)
 {
   return (AccessibleEditableText *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/EditableText:1.0");
+         obj, spi_interface_editable_text);
 }
 
 
-
 /**
  * Accessible_getHypertext:
  * @obj: a pointer to the #Accessible instance to query.
@@ -735,7 +971,7 @@ AccessibleHypertext *
 Accessible_getHypertext (Accessible *obj)
 {
   return (AccessibleHypertext *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Hypertext:1.0");
+         obj, spi_interface_hypertext);
 }
 
 
@@ -753,10 +989,28 @@ AccessibleImage *
 Accessible_getImage (Accessible *obj)
 {
   return (AccessibleImage *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Image:1.0");
+         obj, spi_interface_image);
 }
 
-
+/**
+ * Accessible_getMatchRule:
+ * @obj: a pointer to the #Accessible instance to query.
+ *
+ * Get the #AccessibleMatchRule interface for an #Accessible.
+ *
+ * Returns: a pointer to an #AccessibleMatchRule interface instance, or
+ *          NULL if @obj does not implement #AccessibleMatchRule.
+ **/
+AccessibleMatchRule *
+Accessible_getMatchRule (Accessible *obj)
+{
+#if 0
+  return (AccessibleMatchRule *) Accessible_queryInterface (
+         obj, spi_interface_match_rule);
+#else
+  g_warning ("Match rules not supported");
+#endif
+}
 
 /**
  * Accessible_getSelection:
@@ -771,7 +1025,7 @@ AccessibleSelection *
 Accessible_getSelection (Accessible *obj)
 {
   return (AccessibleSelection *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Selection:1.0");
+         obj, spi_interface_selection);
 }
 
 
@@ -788,8 +1042,12 @@ Accessible_getSelection (Accessible *obj)
 AccessibleStreamableContent *
 Accessible_getStreamableContent (Accessible *obj)
 {
+#if 0
   return (AccessibleStreamableContent *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/StreamableContent:1.0");
+         obj, spi_interface_streamable_content);
+#else
+  g_warning ("Streamable content not supported");
+#endif
 }
 
 /**
@@ -805,7 +1063,7 @@ AccessibleTable *
 Accessible_getTable (Accessible *obj)
 {
   return (AccessibleTable *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Table:1.0");
+         obj, spi_interface_table);
 }
 
 /**
@@ -821,7 +1079,7 @@ AccessibleText *
 Accessible_getText (Accessible *obj)
 {
   return (AccessibleText *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Text:1.0");
+         obj, spi_interface_text);
 }
 
 
@@ -839,7 +1097,7 @@ AccessibleValue *
 Accessible_getValue (Accessible *obj)
 {
   return (AccessibleValue *) Accessible_queryInterface (
-         obj, "IDL:Accessibility/Value:1.0");
+         obj, spi_interface_value);
 }
 
 
@@ -859,30 +1117,14 @@ AccessibleUnknown *
 Accessible_queryInterface (Accessible *obj,
                           const char *interface_name)
 {
-  Bonobo_Unknown iface;
-  
-  if (!obj)
-    {
-      return NULL;
-    }
-
-  iface = Accessibility_Accessible_queryInterface (CSPI_OBJREF (obj),
-                                                  interface_name,
-                                                  cspi_ev ());
-
-
-  cspi_return_val_if_ev ("queryInterface", NULL); 
-
-  /*
-   * FIXME: we need to be fairly sure that references are going
-   * to mach up if we are going to expose QueryInterface, ie. we
-   * can't allow people to do:
-   * b = a.qi ("b"); b.unref, b.unref to release a's reference.
-   * this should be no real problem though for this level of API
-   * user.
-   */
-
-  return cspi_object_add (iface);
+  if (cspi_accessible_is_a (obj, interface_name))
+  {
+    /* The original code called cspi_object_add(obj) instead, but gok and
+     * simple-at don't treat interfaces as references, so I'm confused
+     * and not going to replicate this bit of code */
+    return obj;
+  }
+  return NULL;
 }
 
 
@@ -896,7 +1138,7 @@ Accessible_queryInterface (Accessible *obj,
 void
 AccessibleRelation_ref (AccessibleRelation *obj)
 {
-  cspi_object_ref (obj);
+  obj->ref_count++;
 }
 
 /**
@@ -909,9 +1151,64 @@ AccessibleRelation_ref (AccessibleRelation *obj)
 void
 AccessibleRelation_unref (AccessibleRelation *obj)
 {
-  cspi_object_unref (obj);
+  obj->ref_count--;
+  if (obj->ref_count <= 0)
+  {
+    g_array_free (obj->targets, TRUE);
+    g_free (obj);
+  }
+}
+
+static SPIBoolean
+cspi_init_relation_type_table (AccessibleRelationType *relation_type_table)
+{
+  int i;
+  for (i = 0; i < Accessibility_RELATION_LAST_DEFINED; ++i)
+    {
+      relation_type_table [i] = SPI_RELATION_NULL;
+    }
+  relation_type_table [Accessibility_RELATION_NULL] = SPI_RELATION_NULL;
+  relation_type_table [Accessibility_RELATION_LABEL_FOR] = SPI_RELATION_LABEL_FOR;
+  relation_type_table [Accessibility_RELATION_LABELLED_BY] = SPI_RELATION_LABELED_BY;
+  relation_type_table [Accessibility_RELATION_CONTROLLER_FOR] = SPI_RELATION_CONTROLLER_FOR;
+  relation_type_table [Accessibility_RELATION_CONTROLLED_BY] = SPI_RELATION_CONTROLLED_BY;
+  relation_type_table [Accessibility_RELATION_MEMBER_OF] = SPI_RELATION_MEMBER_OF;
+  relation_type_table [Accessibility_RELATION_TOOLTIP_FOR] = SPI_RELATION_NULL;
+  relation_type_table [Accessibility_RELATION_NODE_CHILD_OF] = SPI_RELATION_NODE_CHILD_OF;
+  relation_type_table [Accessibility_RELATION_EXTENDED] = SPI_RELATION_EXTENDED;
+  relation_type_table [Accessibility_RELATION_FLOWS_TO] = SPI_RELATION_FLOWS_TO;
+  relation_type_table [Accessibility_RELATION_FLOWS_FROM] = SPI_RELATION_FLOWS_FROM;
+  relation_type_table [Accessibility_RELATION_SUBWINDOW_OF] = SPI_RELATION_SUBWINDOW_OF;
+  relation_type_table [Accessibility_RELATION_EMBEDS] = SPI_RELATION_EMBEDS;
+  relation_type_table [Accessibility_RELATION_EMBEDDED_BY] = SPI_RELATION_EMBEDDED_BY;
+  relation_type_table [Accessibility_RELATION_POPUP_FOR] = SPI_RELATION_POPUP_FOR;
+  relation_type_table [Accessibility_RELATION_PARENT_WINDOW_OF] = SPI_RELATION_PARENT_WINDOW_OF;
+  relation_type_table [Accessibility_RELATION_DESCRIBED_BY] = SPI_RELATION_DESCRIBED_BY;
+  relation_type_table [Accessibility_RELATION_DESCRIPTION_FOR] = SPI_RELATION_DESCRIPTION_FOR;
+  return TRUE;
 }
 
+static AccessibleRelationType
+cspi_relation_type_from_spi_relation_type (Accessibility_RelationType type)
+{
+  /* array is sized according to IDL RelationType because IDL RelationTypes are the index */   
+  static AccessibleRelationType cspi_relation_type_table [Accessibility_RELATION_LAST_DEFINED];
+  static SPIBoolean is_initialized = FALSE;
+  AccessibleRelationType cspi_type;
+  if (!is_initialized)
+    {
+      is_initialized = cspi_init_relation_type_table (cspi_relation_type_table);           
+    }
+  if (type >= 0 && type < Accessibility_RELATION_LAST_DEFINED)
+    {
+      cspi_type = cspi_relation_type_table [type];         
+    }
+  else
+    {
+      cspi_type = SPI_RELATION_NULL;
+    }
+  return cspi_type; 
+}
 /**
  * AccessibleRelation_getRelationType:
  * @obj: a pointer to the #AccessibleRelation object to query.
@@ -925,13 +1222,8 @@ AccessibleRelation_unref (AccessibleRelation *obj)
 AccessibleRelationType
 AccessibleRelation_getRelationType (AccessibleRelation *obj)
 {
-  AccessibleRelationType retval;
-  
   cspi_return_val_if_fail (obj, SPI_RELATION_NULL);
-  retval =
-    Accessibility_Relation_getRelationType (CSPI_OBJREF (obj), cspi_ev());
-  cspi_return_val_if_ev ("getRelationType", SPI_RELATION_NULL);
-  return retval;
+  return cspi_relation_type_from_spi_relation_type (obj->type);
 }
 
 /**
@@ -949,13 +1241,8 @@ AccessibleRelation_getRelationType (AccessibleRelation *obj)
 int
 AccessibleRelation_getNTargets (AccessibleRelation *obj)
 {
-  int retval;
-  
   cspi_return_val_if_fail (obj, -1);
-  retval = (int)
-    Accessibility_Relation_getNTargets (CSPI_OBJREF (obj), cspi_ev());
-  cspi_return_val_if_ev ("getNTargets", -1);
-  return retval;
+  return obj->targets->len;
 }
 
 /**
@@ -973,15 +1260,11 @@ AccessibleRelation_getNTargets (AccessibleRelation *obj)
 Accessible *
 AccessibleRelation_getTarget (AccessibleRelation *obj, int i)
 {
-  Accessible *retval;
-
   cspi_return_val_if_fail (obj, NULL);
 
-  retval = cspi_object_add (
-                        Accessibility_Relation_getTarget (CSPI_OBJREF(obj),
-                                                          (CORBA_short) i, cspi_ev()));
-  cspi_return_val_if_ev ("getTarget", NULL);
-  return retval;
+  if (i < 0 || i >= obj->targets->len) return NULL;
+  return cspi_object_add (
+                        g_array_index (obj->targets, Accessible *, i));
 }
 
 /**
@@ -994,7 +1277,7 @@ AccessibleRelation_getTarget (AccessibleRelation *obj, int i)
 void
 AccessibleStateSet_ref (AccessibleStateSet *obj)
 {
-  cspi_object_ref (obj);
+  spi_state_set_cache_ref (obj);
 }
 
 /**
@@ -1007,9 +1290,62 @@ AccessibleStateSet_ref (AccessibleStateSet *obj)
 void
 AccessibleStateSet_unref (AccessibleStateSet *obj)
 {
-  cspi_object_unref (obj);
+  spi_state_set_cache_unref (obj);
 }
 
+static Accessibility_StateType
+spi_state_to_dbus (AccessibleState state)
+{
+#define MAP_STATE(a) \
+  case SPI_STATE_##a: \
+    return Accessibility_STATE_##a
+
+  switch (state)
+    {
+      MAP_STATE (INVALID);
+      MAP_STATE (ACTIVE);
+      MAP_STATE (ARMED);
+      MAP_STATE (BUSY);
+      MAP_STATE (CHECKED);
+      MAP_STATE (DEFUNCT);
+      MAP_STATE (EDITABLE);
+      MAP_STATE (ENABLED);
+      MAP_STATE (EXPANDABLE);
+      MAP_STATE (EXPANDED);
+      MAP_STATE (FOCUSABLE);
+      MAP_STATE (FOCUSED);
+      MAP_STATE (HORIZONTAL);
+      MAP_STATE (ICONIFIED);
+      MAP_STATE (MODAL);
+      MAP_STATE (MULTI_LINE);
+      MAP_STATE (MULTISELECTABLE);
+      MAP_STATE (OPAQUE);
+      MAP_STATE (PRESSED);
+      MAP_STATE (RESIZABLE);
+      MAP_STATE (SELECTABLE);
+      MAP_STATE (SELECTED);
+      MAP_STATE (SENSITIVE);
+      MAP_STATE (SHOWING);
+      MAP_STATE (SINGLE_LINE);
+      MAP_STATE (STALE);
+      MAP_STATE (TRANSIENT);
+      MAP_STATE (VERTICAL);
+      MAP_STATE (VISIBLE);
+      MAP_STATE (MANAGES_DESCENDANTS);
+      MAP_STATE (INDETERMINATE);
+      MAP_STATE (TRUNCATED);
+      MAP_STATE (REQUIRED);
+      MAP_STATE (INVALID_ENTRY);
+      MAP_STATE (SUPPORTS_AUTOCOMPLETION);
+      MAP_STATE (SELECTABLE_TEXT);
+      MAP_STATE (IS_DEFAULT);
+      MAP_STATE (VISITED);
+    default:
+      return Accessibility_STATE_INVALID;
+  }
+#undef MAP_STATE
+}            
+
 /**
  * AccessibleStateSet_contains:
  * @obj: a pointer to the #AccessibleStateSet object on which to operate.
@@ -1027,16 +1363,7 @@ SPIBoolean
 AccessibleStateSet_contains (AccessibleStateSet *obj,
                             AccessibleState state)
 {
-  CORBA_boolean retval;
-
-  cspi_return_val_if_fail (obj != NULL, FALSE);
-
-  retval = Accessibility_StateSet_contains (CSPI_OBJREF (obj),
-                                           state, cspi_ev ());
-
-  cspi_return_val_if_ev ("contains", FALSE);
-
-  return (SPIBoolean) retval;
+  return spi_state_set_cache_contains (obj, spi_state_to_dbus (state));
 }
 
 /**
@@ -1052,10 +1379,7 @@ void
 AccessibleStateSet_add (AccessibleStateSet *obj,
                        AccessibleState state)
 {
-  cspi_return_if_fail (obj != NULL);
-
-  Accessibility_StateSet_add (CSPI_OBJREF (obj), state, cspi_ev ());
-  cspi_check_ev ("add");
+  spi_state_set_cache_add (obj, spi_state_to_dbus (state));
 }
 
 /**
@@ -1071,10 +1395,7 @@ void
 AccessibleStateSet_remove (AccessibleStateSet *obj,
                           AccessibleState state)
 {
-  cspi_return_if_fail (obj != NULL);
-
-  Accessibility_StateSet_remove (CSPI_OBJREF (obj), state, cspi_ev ());
-  cspi_check_ev ("remove");
+  spi_state_set_cache_remove (obj, spi_state_to_dbus (state));
 }
 
 /**
@@ -1096,16 +1417,19 @@ SPIBoolean
 AccessibleStateSet_equals (AccessibleStateSet *obj,
                            AccessibleStateSet *obj2)
 {
+  SPIBoolean   eq;
+  AtkStateSet *cmp;
+
   if (obj == obj2)
     {
       return TRUE;
     }
 
-  cspi_return_val_if_fail (obj != NULL, FALSE);
-  cspi_return_val_if_fail (obj2 != NULL, FALSE);
+  cmp = spi_state_set_cache_xor (obj, obj2);
+  eq = spi_state_set_cache_is_empty (cmp);
+  spi_state_set_cache_unref (cmp);
 
-  return Accessibility_StateSet_equals (CSPI_OBJREF (obj),
-                                       CSPI_OBJREF (obj2), cspi_ev ());
+  return eq;
 }
 
 /**
@@ -1126,9 +1450,7 @@ AccessibleStateSet *
 AccessibleStateSet_compare (AccessibleStateSet *obj,
                             AccessibleStateSet *obj2)
 {
-  cspi_return_val_if_fail (obj != NULL, NULL);
-  cspi_return_val_if_fail (obj2 != NULL, NULL);
-  return NULL; 
+  return spi_state_set_cache_xor (obj, obj2);
 }
 
 /**
@@ -1144,9 +1466,6 @@ AccessibleStateSet_compare (AccessibleStateSet *obj,
 SPIBoolean
 AccessibleStateSet_isEmpty (AccessibleStateSet *obj)
 {
-  cspi_return_val_if_fail (obj != NULL, FALSE);
-  return TRUE; 
-  /*  return Accessibility_StateSet_isEmpty (CSPI_OBJREF (obj), cspi_ev ());*/
+  return spi_state_set_cache_is_empty (obj);
 }
 
-