Removed po directory from Makefile.am for now.
[platform/core/uifw/at-spi2-atk.git] / libspi / accessible.c
index fec72e7..1148cb0 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
-/*
- * accessible.c: test for accessibility implementation
- *
- */
-#include <config.h>
-#include <bonobo/Bonobo.h>
+/* accessible.c: the core of the accessibility implementation */
 
+#include <config.h>
 #include <stdio.h>
+#include <bonobo/bonobo-exception.h>
+#include <atk/atk.h>
+#include <libspi/libspi.h>
 
-/*
- * This pulls the CORBA definitions for the "Accessibility::Accessible" server
- */
-#include <libspi/Accessibility.h>
-
-/*
- * This pulls the definition for the BonoboObject (Gtk Type)
- */
-#include "accessible.h"
-
-/*
- * Our parent Gtk object type
- */
-#define PARENT_TYPE BONOBO_OBJECT_TYPE
+/* Our parent Gtk object type  */
+#define PARENT_TYPE SPI_TYPE_BASE
 
-/*
- * A pointer to our parent object class
- */
-static GObjectClass *accessible_parent_class;
-
-/*
- * Implemented GObject::finalize
- */
-static void
-accessible_object_finalize (GObject *object)
+static AtkObject *
+get_accessible_from_servant (PortableServer_Servant servant)
 {
-        Accessible *accessible = ACCESSIBLE (object);
+  SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
 
-        printf("accessible_object_finalize called\n");
-        g_object_unref (accessible->atko);
-        accessible->atko = NULL;
+  if (!object)
+    {
+      return NULL;
+    }
 
-        printf("atko freed, calling parent finalize\n");
-        accessible_parent_class->finalize (object);
+  return object->atko;
 }
 
 /*
@@ -72,14 +51,23 @@ static CORBA_char *
 impl_accessibility_accessible_get_name (PortableServer_Servant servant,
                                         CORBA_Environment     *ev)
 {
-  CORBA_char * retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = atk_object_get_name (accessible->atko);
-  if (retval )
-    retval = CORBA_string_dup (retval);
+  const gchar *name;
+  CORBA_char  *retval;
+  AtkObject   *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_string_dup (""));
+
+  name = atk_object_get_name (object);
+
+  if (name)
+    {
+      retval = CORBA_string_dup (name);
+    }
   else
-    retval = CORBA_string_dup ("");
-  fprintf (stderr, "Accessible get_name called: %s\n", retval);
+    {
+      retval = CORBA_string_dup ("");
+    }
+
   return retval;
 }
 
@@ -91,9 +79,11 @@ impl_accessibility_accessible_set_name (PortableServer_Servant servant,
                                         const CORBA_char      *name,
                                         CORBA_Environment     *ev)
 {
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  atk_object_set_name (accessible->atko, name);
-  printf ("Accessible set_name called: %s\n", name);
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_name (object, name);
 }
 
 /*
@@ -103,10 +93,23 @@ static CORBA_char *
 impl_accessibility_accessible_get_description (PortableServer_Servant servant,
                                                CORBA_Environment     *ev)
 {
-  CORBA_char * retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = CORBA_string_dup (atk_object_get_description (accessible->atko));
-  fprintf (stderr, "Accessible get_description called: %s\n", retval);
+  const gchar *descr;
+  CORBA_char  *retval;
+  AtkObject   *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_string_dup (""));
+
+  descr = atk_object_get_description (object);
+
+  if (descr)
+    {
+      retval = CORBA_string_dup (descr);
+    }
+  else
+    {
+      retval = CORBA_string_dup ("");
+    }
+
   return retval;
 }
 
@@ -115,12 +118,14 @@ impl_accessibility_accessible_get_description (PortableServer_Servant servant,
  */
 static void
 impl_accessibility_accessible_set_description (PortableServer_Servant servant,
-                                               const CORBA_char      *name,
+                                               const CORBA_char      *descr,
                                                CORBA_Environment     *ev)
 {
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  atk_object_set_description (accessible->atko, name);
-  printf ("Accessible set_description called: %s\n", name);
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_if_fail (object != NULL);
+
+  atk_object_set_description (object, descr);
 }
 
 /*
@@ -130,13 +135,28 @@ static Accessibility_Accessible
 impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
                                           CORBA_Environment     *ev)
 {
-  Accessibility_Accessible retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
   AtkObject *parent;
-  parent = atk_object_get_parent (accessible->atko);
-  retval = bonobo_object_corba_objref (bonobo_object (accessible_new (parent)));
-  printf ("Accessible get_parent called\n");
-  return retval;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, CORBA_OBJECT_NIL);
+
+  parent = atk_object_get_parent (object);
+
+  return spi_accessible_new_return (parent, FALSE, ev);
+}
+
+/*
+ * CORBA Accessibility::Accessible::get_IndexInParent method implementation
+ */
+static CORBA_long
+impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
+                                                   CORBA_Environment     *ev)
+{
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, -1);
+
+  return atk_object_get_index_in_parent (object);
 }
 
 /*
@@ -146,11 +166,11 @@ static CORBA_long
 impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
                                                CORBA_Environment     *ev)
 {
-  CORBA_long retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  retval = (CORBA_long) atk_object_get_n_accessible_children (accessible->atko);
-  printf ("Accessible get_childCount called: %d\n", (int) retval);
-  return retval;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  return atk_object_get_n_accessible_children (object);
 }
 
 /*
@@ -161,22 +181,90 @@ impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant
                                                   const CORBA_long      index,
                                                   CORBA_Environment     *ev)
 {
-  Accessibility_Accessible retval;
-  Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
-  AtkObject *child = atk_object_ref_accessible_child (accessible->atko, (gint) index);
-  retval = bonobo_object_corba_objref ( bonobo_object (accessible_new (child)));
-  printf ("Accessible get_child_at_index called.\n");
+  AtkObject *child;
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  child = atk_object_ref_accessible_child (object, index);
+
+  return spi_accessible_new_return (child, TRUE, ev);
+}
+
+/*
+ * CORBA Accessibility::Accessible::getState method implementation
+ */
+static Accessibility_StateSet
+impl_accessibility_accessible_get_state (PortableServer_Servant servant,
+                                        CORBA_Environment     *ev)
+{
+  AtkObject *object = get_accessible_from_servant (servant);
+
+  bonobo_return_val_if_fail (object != NULL, NULL, ev);
+
+  printf ("SpiAccessible get_state.\n");
+
+  /* TODO: implement the bonobo stateset class */
+  return (Accessibility_StateSet) NULL;
+}
+
+/*
+ * CORBA Accessibility::Accessible::getRelationSet method implementation
+ */
+static Accessibility_RelationSet *
+impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
+                                               CORBA_Environment     *ev)
+{
+  Accessibility_RelationSet *retval;
+  gint n_relations;
+  gint i;
+  AtkRelationSet *relation_set;
+  AtkObject      *object = get_accessible_from_servant (servant);
+
+  bonobo_return_val_if_fail (object != NULL, NULL, ev);
+
+  relation_set = atk_object_ref_relation_set (object);
+
+  n_relations = atk_relation_set_get_n_relations (relation_set);
+  retval = CORBA_sequence_Accessibility_Relation__alloc ();
+  CORBA_sequence_Accessibility_Relation_allocbuf (n_relations);
+         
+  for (i = 0; i < n_relations; ++i)
+    {
+      retval->_buffer[i] =
+        bonobo_object_dup_ref (
+          BONOBO_OBJREF (
+            spi_relation_new (atk_relation_set_get_relation (relation_set, i))),
+         ev);
+    }
+  
+  printf ("SpiAccessible get_relation_set.\n");
+  return retval;
+}
+
+/*
+ * CORBA Accessibility::Accessible::getRole method implementation
+ */
+static Accessibility_Role
+impl_accessibility_accessible_get_role (PortableServer_Servant servant,
+                                       CORBA_Environment     *ev)
+{
+  AtkRole            role;
+  Accessibility_Role retval;
+  AtkObject         *object = get_accessible_from_servant (servant);
+
+  g_return_val_if_fail (object != NULL, 0);
+
+  role = atk_object_get_role (object);
+  retval = role; /* FIXME: relies on ability to cast these back and forth */
+
   return retval;
 }
 
 static void
-accessible_class_init (AccessibleClass *klass)
+spi_accessible_class_init (SpiAccessibleClass *klass)
 {
-        GObjectClass * object_class = (GObjectClass *) klass;
         POA_Accessibility_Accessible__epv *epv = &klass->epv;
-        accessible_parent_class = g_type_class_peek_parent (klass);
-
-        object_class->finalize = accessible_object_finalize;
 
         epv->_get_name = impl_accessibility_accessible_get_name;
         epv->_set_name = impl_accessibility_accessible_set_name;
@@ -186,120 +274,154 @@ accessible_class_init (AccessibleClass *klass)
         epv->_get_parent = impl_accessibility_accessible_get_parent;
         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
+        epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
 
-        /* epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent; */
-        /* epv->getRelationSet = impl_accessibility_accessible_get_relation_set;      */
-        /* epv->getState = impl_accessibility_accessible_get_state;                   */
-        /* epv->getRole = impl_accessibility_accessible_get_role;                     */
+        epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
+        epv->getState = impl_accessibility_accessible_get_state;
+        epv->getRole = impl_accessibility_accessible_get_role;
 }
 
 static void
-accessible_init (Accessible *accessible)
+spi_accessible_init (SpiAccessible *accessible)
+{
+}
+
+BONOBO_TYPE_FUNC_FULL (SpiAccessible,
+                      Accessibility_Accessible,
+                      PARENT_TYPE,
+                      spi_accessible);
+
+static GHashTable *public_corba_refs = NULL;
+
+static GHashTable *
+get_public_refs (void)
 {
+  if (!public_corba_refs)
+    {
+      public_corba_refs = g_hash_table_new (NULL, NULL);
+    }
+  return public_corba_refs;
 }
 
-GType
-accessible_get_type (void)
+static void
+de_register_public_ref (SpiBase *object)
 {
-        static GType type = 0;
-
-        if (!type) {
-                static const GTypeInfo tinfo = {
-                        sizeof (AccessibleClass),
-                        (GBaseInitFunc) NULL,
-                        (GBaseFinalizeFunc) NULL,
-                        (GClassInitFunc) accessible_class_init,
-                        (GClassFinalizeFunc) NULL,
-                        NULL, /* class data */
-                        sizeof (Accessible),
-                        0, /* n preallocs */
-                        (GInstanceInitFunc) accessible_init,
-                        NULL /* value table */
-                };
-                /*
-                 * Bonobo_type_unique auto-generates a load of
-                 * CORBA structures for us. All derived types must
-                 * use bonobo_type_unique.
-                 */
-                type = bonobo_type_unique (
-                        PARENT_TYPE,
-                        POA_Accessibility_Accessible__init,
-                        NULL,
-                        G_STRUCT_OFFSET (AccessibleClass, epv),
-                        &tinfo,
-                        "Accessible");
-        }
-
-        return type;
+  g_hash_table_remove (get_public_refs (), object->atko);
 }
 
-Accessible *
-accessible_new (AtkObject *o)
+SpiAccessible *
+spi_accessible_new (AtkObject *o)
 {
-    Accessible *retval =
-               ACCESSIBLE (g_object_new (accessible_get_type (), NULL));
-    g_object_ref (o);
-    retval->atko = ATK_OBJECT (o);
+    SpiAccessible *retval;
+    CORBA_Environment ev;
 
-    /*
-     * TODO: add interface containers/constructors for ACTION, EDITABLE_TEXT, HYPERTEXT,
-     *  IMAGE, SELECTION, TABLE, TEXT, VALUE.
-     */
+    CORBA_exception_init (&ev);
 
-    /* add appropriate ATK interfaces */
+    g_assert (o);
+
+    if ((retval = g_hash_table_lookup (get_public_refs (), o)))
+      {
+        bonobo_object_ref (BONOBO_OBJECT (retval));
+       return retval;
+      }
+
+    retval = g_object_new (SPI_ACCESSIBLE_TYPE, NULL);
+
+    spi_base_construct (SPI_BASE (retval), o);
+
+    g_hash_table_insert (get_public_refs (), o, retval);
+    g_signal_connect (G_OBJECT (retval), "destroy",
+                     G_CALLBACK (de_register_public_ref),
+                     NULL);
+
+    /* aggregate appropriate SPI interfaces based on ATK interfaces */
 
-    /* Action: not yet implemented
     if (ATK_IS_ACTION (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (action_interface_new (o)));
+                                     BONOBO_OBJECT (spi_action_interface_new (o)));
       }
-    */
 
     if (ATK_IS_COMPONENT (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (component_interface_new (o)));
+                                     BONOBO_OBJECT (spi_component_interface_new (o)));
       }
 
-    /* Others: not yet implemented
     if (ATK_IS_EDITABLE_TEXT (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (editable_text_interface_new (o)));
+                                     BONOBO_OBJECT(spi_editable_text_interface_new (o)));
       }
+
     else if (ATK_IS_HYPERTEXT (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (hypertext_interface_new (o)));
+                                     BONOBO_OBJECT (spi_hypertext_interface_new (o)));
       }
+
     else if (ATK_IS_TEXT (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (text_interface_new (o)));
+                                     BONOBO_OBJECT (spi_text_interface_new (o)));
       }
+
     if (ATK_IS_IMAGE (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (image_interface_new (o)));
+                                     BONOBO_OBJECT (spi_image_interface_new (o)));
       }
+
     if (ATK_IS_SELECTION (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (selection_interface_new (o)));
+                                     BONOBO_OBJECT (spi_selection_interface_new (o)));
       }
+
     if (ATK_IS_TABLE (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (table_interface_new (o)));
+                                     BONOBO_OBJECT (spi_table_interface_new (o)));
       }
+
     if (ATK_IS_VALUE (o))
       {
         bonobo_object_add_interface (bonobo_object (retval),
-                                     bonobo_object (value_interface_new (o)));
+                                     BONOBO_OBJECT (spi_value_interface_new (o)));
       }
 
-    */
-
     return retval;
 }
+
+/**
+ * spi_accessible_new_return:
+ * @o: an AtkObject or NULL
+ * @release_ref: whether to unref this AtkObject before return
+ * @ev: a CORBA environment
+ * 
+ * A helper function to instantiate a CORBA accessiblility
+ * proxy from an AtkObject.
+ * 
+ * Return value: the proxy or CORBA_OBJECT_NIL
+ **/
+Accessibility_Accessible
+spi_accessible_new_return (AtkObject         *o,
+                          gboolean           release_ref,
+                          CORBA_Environment *ev)
+{
+  SpiAccessible *accessible;
+
+  if (!o)
+    {
+      return CORBA_OBJECT_NIL;
+    }
+
+  accessible = spi_accessible_new (o);
+
+  if (release_ref)
+    {
+      g_object_unref (G_OBJECT (o));
+    }
+
+  return CORBA_Object_duplicate (BONOBO_OBJREF (accessible), ev);
+}