Add a check for lack of memory
[platform/core/uifw/at-spi2-atk.git] / libspi / selection.c
index f80af59..87d57fa 100644 (file)
@@ -2,7 +2,9 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
- * Copyright 2001 Sun Microsystems Inc.
+ * Copyright 2008 Novell, Inc.
+ * 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
  * Boston, MA 02111-1307, USA.
  */
 
-/*
- * component.c : bonobo wrapper for accessible component implementation
- *
- */
-#include <config.h>
-#include <bonobo/Bonobo.h>
-
-#include <stdio.h>
-
-/*
- * This pulls the CORBA definitions for the "Accessibility::Accessible" server
- */
-#include <libspi/Accessibility.h>
-
-/*
- * This pulls the definition of the selection bonobo object
- */
-#include "selection.h"
+#include "accessible.h"
 
-/*
- * Static function declarations
- */
-
-static void
-selection_class_init (SelectionClass *klass);
-static void
-selection_init (Selection *selection);
-static void
-selection_finalize (GObject *obj);
-static CORBA_long
-impl__get_nSelectedChildren (PortableServer_Servant _servant,
-                            CORBA_Environment * ev);
-static Accessibility_Accessible
-impl_getSelectedChild (PortableServer_Servant _servant,
-                      const CORBA_long selectedChildIndex,
-                      CORBA_Environment * ev);
-static CORBA_boolean
-impl_selectChild (PortableServer_Servant _servant,
-                 const CORBA_long childIndex,
-                 CORBA_Environment * ev);
-static CORBA_boolean
-impl_deselectSelectedChild (PortableServer_Servant _servant,
-                           const CORBA_long selectedChildIndex,
-                           CORBA_Environment * ev);
-static CORBA_boolean
-impl_isChildSelected (PortableServer_Servant _servant,
-                     const CORBA_long childIndex,
-                     CORBA_Environment * ev);
-static void 
-impl_selectAll (PortableServer_Servant _servant,
-               CORBA_Environment * ev);
-static void 
-impl_clearSelection (PortableServer_Servant _servant,
-                    CORBA_Environment * ev);
-
-
-static GObjectClass *parent_class;
-
-GType
-selection_get_type (void)
-{
-  static GType type = 0;
-
-  if (!type) {
-    static const GTypeInfo tinfo = {
-      sizeof (SelectionClass),
-      (GBaseInitFunc) NULL,
-      (GBaseFinalizeFunc) NULL,
-      (GClassInitFunc) selection_class_init,
-      (GClassFinalizeFunc) NULL,
-      NULL, /* class data */
-      sizeof (Selection),
-      0, /* n preallocs */
-      (GInstanceInitFunc) selection_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 (
-                              BONOBO_OBJECT_TYPE,
-                              POA_Accessibility_Selection__init,
-                              NULL,
-                              G_STRUCT_OFFSET (SelectionClass, epv),
-                              &tinfo,
-                              "AccessibleSelection");
-  }
-
-  return type;
-}
-
-static void
-selection_class_init (SelectionClass *klass)
+static AtkSelection *
+get_selection (DBusMessage * message)
 {
-  GObjectClass * object_class = (GObjectClass *) klass;
-  POA_Accessibility_Selection__epv *epv = &klass->epv;
-  parent_class = g_type_class_peek_parent (klass);
-
-  object_class->finalize = selection_finalize;
-
-
-  /* Initialize epv table */
-
-  epv->_get_nSelectedChildren = impl__get_nSelectedChildren;
-  epv->getSelectedChild = impl_getSelectedChild;
-  epv->selectChild = impl_selectChild;
-  epv->deselectSelectedChild = impl_deselectSelectedChild;
-  epv->isChildSelected = impl_isChildSelected;
-  epv->selectAll = impl_selectAll;
-  epv->clearSelection = impl_clearSelection;
+  AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
+  if (!obj)
+    return NULL;
+  return ATK_SELECTION (obj);
 }
 
-static void
-selection_init (Selection *selection)
+static AtkSelection *
+get_selection_from_path (const char *path, void *user_data)
 {
+  AtkObject *obj = spi_dbus_get_object (path);
+  if (!obj)
+    return NULL;
+  return ATK_SELECTION (obj);
 }
 
-static void
-selection_finalize (GObject *obj)
+static dbus_bool_t
+impl_get_nSelectedChildren (const char *path, DBusMessageIter * iter,
+                           void *user_data)
 {
-  Selection *selection = SELECTION (obj);
-  g_object_unref (selection->atko);
-  selection->atko = NULL;
-  parent_class->finalize (obj);
+  AtkSelection *selection = get_selection_from_path (path, user_data);
+  if (!selection)
+    return FALSE;
+  return droute_return_v_int32 (iter,
+                               atk_selection_get_selection_count
+                               (selection));
 }
 
-Selection *
-selection_interface_new (AtkObject *obj)
+static char *
+impl_get_nSelectedChildren_str (void *datum)
 {
-  Selection *new_selection = 
-    SELECTION(g_object_new (SELECTION_TYPE, NULL));
-  new_selection->atko = obj;
-  g_object_ref (obj);
-  return new_selection;
+  g_assert (ATK_IS_HYPERLINK (datum));
+  return g_strdup_printf ("%d",
+                         atk_selection_get_selection_count ((AtkSelection *)
+                                                            datum));
 }
 
-
-
-static CORBA_long
-impl__get_nSelectedChildren (PortableServer_Servant _servant,
-                            CORBA_Environment * ev)
-{
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  return (CORBA_long)
-    atk_selection_get_selection_count (ATK_SELECTION(selection->atko));
-} 
-
-
-
-
-static Accessibility_Accessible
-impl_getSelectedChild (PortableServer_Servant _servant,
-                      const CORBA_long selectedChildIndex,
-                      CORBA_Environment * ev)
+static DBusMessage *
+impl_getSelectedChild (DBusConnection * bus, DBusMessage * message,
+                      void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
+  AtkSelection *selection = get_selection (message);
+  DBusError error;
+  dbus_int32_t selectedChildIndex;
   AtkObject *atk_object;
-  Accessibility_Accessible rv;
 
-  atk_object = atk_selection_ref_selection (ATK_SELECTION(selection->atko), (gint) selectedChildIndex);
-  rv = bonobo_object_corba_objref (BONOBO_OBJECT(accessible_new(atk_object)));
-  return rv;
+  if (!selection)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
+       DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  atk_object = atk_selection_ref_selection (selection, selectedChildIndex);
+  return spi_dbus_return_object (message, atk_object, TRUE);
 }
 
-
-
-static CORBA_boolean
-impl_selectChild (PortableServer_Servant _servant,
-                 const CORBA_long childIndex,
-                 CORBA_Environment * ev)
+static DBusMessage *
+impl_selectChild (DBusConnection * bus, DBusMessage * message,
+                 void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  return (CORBA_boolean)
-    atk_selection_add_selection (ATK_SELECTION(selection->atko), (gint) childIndex);
+  AtkSelection *selection = get_selection (message);
+  DBusError error;
+  dbus_int32_t childIndex;
+  dbus_bool_t rv;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  rv = atk_selection_add_selection (selection, childIndex);
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
-
-
-
-static CORBA_boolean
-impl_deselectSelectedChild (PortableServer_Servant _servant,
-                           const CORBA_long selectedChildIndex,
-                           CORBA_Environment * ev)
+static DBusMessage *
+impl_deselectSelectedChild (DBusConnection * bus, DBusMessage * message,
+                           void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  return (CORBA_boolean)
-    atk_selection_remove_selection (ATK_SELECTION(selection->atko), (gint) selectedChildIndex);
+  AtkSelection *selection = get_selection (message);
+  DBusError error;
+  dbus_int32_t selectedChildIndex;
+  dbus_bool_t rv;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
+       DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  rv = atk_selection_remove_selection (selection, selectedChildIndex);
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
-
-
-static CORBA_boolean
-impl_isChildSelected (PortableServer_Servant _servant,
-                     const CORBA_long childIndex,
-                     CORBA_Environment * ev)
+static DBusMessage *
+impl_isChildSelected (DBusConnection * bus, DBusMessage * message,
+                     void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  return (CORBA_boolean)
-    atk_selection_is_child_selected (ATK_SELECTION(selection->atko), (gint) childIndex);
+  AtkSelection *selection = get_selection (message);
+  DBusError error;
+  dbus_int32_t childIndex;
+  dbus_bool_t rv;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_INT32, &childIndex, DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  rv = atk_selection_is_child_selected (selection, childIndex);
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
-
-
-static void 
-impl_selectAll (PortableServer_Servant _servant,
-               CORBA_Environment * ev)
+static DBusMessage *
+impl_selectAll (DBusConnection * bus, DBusMessage * message, void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  atk_selection_select_all_selection (ATK_SELECTION(selection->atko));
+  AtkSelection *selection = get_selection (message);
+  dbus_bool_t rv;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  rv = atk_selection_select_all_selection (selection);
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
+static DBusMessage *
+impl_clearSelection (DBusConnection * bus, DBusMessage * message,
+                    void *user_data)
+{
+  AtkSelection *selection = get_selection (message);
+  dbus_bool_t rv;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  rv = atk_selection_clear_selection (selection);
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
+}
 
-
-static void 
-impl_clearSelection (PortableServer_Servant _servant,
-                    CORBA_Environment * ev)
+static DBusMessage *
+impl_deselectChild (DBusConnection * bus, DBusMessage * message,
+                   void *user_data)
 {
-  Selection *selection = SELECTION (bonobo_object_from_servant (_servant));
-  atk_selection_clear_selection (ATK_SELECTION(selection->atko));
+  AtkSelection *selection = get_selection (message);
+  DBusError error;
+  dbus_int32_t selectedChildIndex;
+  dbus_bool_t rv = FALSE;
+  gint i, nselected;
+  DBusMessage *reply;
+
+  if (!selection)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_INT32, &selectedChildIndex,
+       DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  nselected = atk_selection_get_selection_count (selection);
+  for (i = 0; i < nselected; ++i)
+    {
+      AtkObject *selected_obj = atk_selection_ref_selection (selection, i);
+      if (atk_object_get_index_in_parent (selected_obj) == selectedChildIndex)
+       {
+         g_object_unref (G_OBJECT (selected_obj));
+         rv = atk_selection_remove_selection (selection, i);
+         break;
+       }
+      g_object_unref (G_OBJECT (selected_obj));
+    }
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
+                               DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
+static DRouteMethod methods[] = {
+  {DROUTE_METHOD, impl_getSelectedChild, "getSelectedChild",
+   "i,selectedChildIndex,i:o,,o"},
+  {DROUTE_METHOD, impl_selectChild, "selectChild", "i,childIndex,i:b,,o"},
+  {DROUTE_METHOD, impl_deselectSelectedChild, "deselectSelectedChild",
+   "i,selectedChildIndex,i:b,,o"},
+  {DROUTE_METHOD, impl_isChildSelected, "isChildSelected",
+   "i,childIndex,i:b,,o"},
+  {DROUTE_METHOD, impl_selectAll, "selectAll", "b,,o"},
+  {DROUTE_METHOD, impl_clearSelection, "clearSelection", "b,,o"},
+  {DROUTE_METHOD, impl_deselectChild, "deselectChild", "i,childIndex,i:b,,o"},
+  {0, NULL, NULL, NULL}
+};
+
+static DRouteProperty properties[] = {
+  {impl_get_nSelectedChildren, impl_get_nSelectedChildren_str, NULL, NULL,
+   "nSelectedChildren"},
+  {NULL, NULL, NULL, NULL, NULL}
+};
+
+void
+spi_initialize_selection (DRouteData * data)
+{
+  droute_add_interface (data, "org.freedesktop.accessibility.Selection",
+                       methods, properties,
+                       (DRouteGetDatumFunction) get_selection_from_path,
+                       NULL);
+};