Add a check for lack of memory
[platform/core/uifw/at-spi2-atk.git] / libspi / image.c
index b957849..4557ff6 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.
  */
 
-/* image.c : implements the Image interface */
-
-#include <config.h>
-#include <stdio.h>
-#include <libspi/image.h>
-
-/* Static function declarations */
-
-static void
-spi_image_class_init (SpiImageClass *klass);
-static void
-spi_image_init (SpiImage *image);
-static void
-spi_image_finalize (GObject *obj);
-static void 
-impl_getImagePosition (PortableServer_Servant _servant,
-                      CORBA_long * x, CORBA_long * y,
-                      const CORBA_short coordType,
-                      CORBA_Environment * ev);
-static void 
-impl_getImageSize (PortableServer_Servant _servant,
-                  CORBA_long * width, CORBA_long * height,
-                  CORBA_Environment * ev);
-static CORBA_string 
-impl__get_imageDescription (PortableServer_Servant _servant,
-                         CORBA_Environment * ev);
-
-
-static GObjectClass *parent_class;
-
-GType
-spi_image_get_type (void)
+#include "accessible.h"
+
+static AtkImage *
+get_image (DBusMessage * message)
 {
-  static GType type = 0;
-
-  if (!type) {
-    static const GTypeInfo tinfo = {
-      sizeof (SpiImageClass),
-      (GBaseInitFunc) NULL,
-      (GBaseFinalizeFunc) NULL,
-      (GClassInitFunc) spi_image_class_init,
-      (GClassFinalizeFunc) NULL,
-      NULL, /* class data */
-      sizeof (SpiImage),
-      0, /* n preallocs */
-      (GInstanceInitFunc) spi_image_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_Image__init,
-                              NULL,
-                              G_STRUCT_OFFSET (SpiImageClass, epv),
-                              &tinfo,
-                              "SpiAccessibleImage");
-  }
-
-  return type;
+  AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
+  if (!obj)
+    return NULL;
+  return ATK_IMAGE (obj);
 }
 
-static void
-spi_image_class_init (SpiImageClass *klass)
+static AtkImage *
+get_image_from_path (const char *path, void *user_data)
 {
-  GObjectClass * object_class = (GObjectClass *) klass;
-  POA_Accessibility_Image__epv *epv = &klass->epv;
-  parent_class = g_type_class_peek_parent (klass);
-
-  object_class->finalize = spi_image_finalize;
-
-
-  /* Initialize epv table */
-
-  epv->getImagePosition = impl_getImagePosition;
-  epv->getImageSize = impl_getImageSize;
-  epv->_get_imageDescription = impl__get_imageDescription;
+  AtkObject *obj = spi_dbus_get_object (path);
+  if (!obj)
+    return NULL;
+  return ATK_IMAGE (obj);
 }
 
-static void
-spi_image_init (SpiImage *image)
+static dbus_bool_t
+impl_get_imageDescription (const char *path, DBusMessageIter * iter,
+                          void *user_data)
 {
+  AtkImage *image = get_image_from_path (path, user_data);
+  if (!image)
+    return FALSE;
+  return droute_return_v_string (iter,
+                                atk_image_get_image_description (image));
 }
 
-static void
-spi_image_finalize (GObject *obj)
+static char *
+impl_get_imageDescription_str (void *datum)
 {
-  SpiImage *image = SPI_IMAGE (obj);
-  g_object_unref (image->atko);
-  image->atko = NULL;
-  parent_class->finalize (obj);
+  AtkImage *image = (AtkImage *) datum;
+  g_assert (ATK_IS_IMAGE (datum));
+  return g_strdup (atk_image_get_image_description (image));
 }
 
-SpiImage *
-spi_image_interface_new (AtkObject *obj)
+static dbus_bool_t
+impl_get_imageLocale (const char *path, DBusMessageIter * iter,
+                     void *user_data)
 {
-  SpiImage *new_image = 
-    SPI_IMAGE(g_object_new (SPI_IMAGE_TYPE, NULL));
-  new_image->atko = obj;
-  g_object_ref (obj);
-  return new_image;
+  AtkImage *image = get_image_from_path (path, user_data);
+  if (!image)
+    return FALSE;
+  return droute_return_v_string (iter, atk_image_get_image_locale (image));
 }
 
-
-
-static void 
-impl_getImagePosition (PortableServer_Servant _servant,
-                      CORBA_long * x, CORBA_long * y,
-                      const CORBA_short coordType,
-                      CORBA_Environment * ev)
+static char *
+impl_get_imageLocale_str (void *datum)
 {
-  SpiImage *image = SPI_IMAGE (bonobo_object_from_servant(_servant));
-  atk_image_get_image_position (ATK_IMAGE(image->atko),
-                               (gint *) x, (gint *) y,
-                               (AtkCoordType) coordType);
+  AtkImage *image = (AtkImage *) datum;
+  g_assert (ATK_IS_IMAGE (datum));
+  return g_strdup (atk_image_get_image_locale (image));
 }
 
-
-
-static void 
-impl_getImageSize (PortableServer_Servant _servant,
-                  CORBA_long * width, CORBA_long * height,
-                           CORBA_Environment * ev)
+static DBusMessage *
+impl_getImageExtents (DBusConnection * bus, DBusMessage * message,
+                     void *user_data)
 {
-  SpiImage *image = SPI_IMAGE (bonobo_object_from_servant(_servant));
-  atk_image_get_image_size (ATK_IMAGE(image->atko),
-                           (gint *) width, (gint *) height);
+  AtkImage *image = get_image (message);
+  DBusError error;
+  dbus_uint32_t coordType;
+  gint ix, iy, iwidth, iheight;
+
+  if (!image)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_UINT32, &coordType, DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  atk_image_get_image_size (image, &iwidth, &iheight);
+  atk_image_get_image_position (image, &ix, &iy, (AtkCoordType) coordType);
+  return spi_dbus_return_rect (message, ix, iy, iwidth, iheight);
 }
 
-
-
-static CORBA_string 
-impl__get_imageDescription (PortableServer_Servant servant,
-                           CORBA_Environment     *ev)
+static DBusMessage *
+impl_getImagePosition (DBusConnection * bus, DBusMessage * message,
+                      void *user_data)
 {
-  SpiImage *image;
-  const char *rv;
-
-  image = SPI_IMAGE (bonobo_object_from_servant (servant));
-
-  rv = atk_image_get_image_description (ATK_IMAGE (image->atko));
-  if (rv)
-    return CORBA_string_dup (rv);
-  else
-    return CORBA_string_dup ("");
+  AtkImage *image = get_image (message);
+  DBusError error;
+  dbus_uint32_t coord_type;
+  gint ix = 0, iy = 0;
+  dbus_int32_t x, y;
+  DBusMessage *reply;
+
+  if (!image)
+    return spi_dbus_general_error (message);
+  dbus_error_init (&error);
+  if (!dbus_message_get_args
+      (message, &error, DBUS_TYPE_UINT32, &coord_type, DBUS_TYPE_INVALID))
+    {
+      return SPI_DBUS_RETURN_ERROR (message, &error);
+    }
+  atk_image_get_image_position (image, &ix, &iy, (AtkCoordType) coord_type);
+  x = ix;
+  y = iy;
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32,
+                               &y, DBUS_TYPE_INVALID);
+    }
+  return reply;
 }
 
+static DBusMessage *
+impl_getImageSize (DBusConnection * bus, DBusMessage * message,
+                  void *user_data)
+{
+  AtkImage *image = get_image (message);
+  gint iwidth = 0, iheight = 0;
+  dbus_int32_t width, height;
+  DBusMessage *reply;
+
+  if (!image)
+    return spi_dbus_general_error (message);
+  atk_image_get_image_size (image, &iwidth, &iheight);
+  width = iwidth;
+  height = iheight;
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+    {
+      dbus_message_append_args (reply, DBUS_TYPE_INT32, &width,
+                               DBUS_TYPE_INT32, &height, DBUS_TYPE_INVALID);
+    }
+  return reply;
+}
 
-
+static DRouteMethod methods[] = {
+  {DROUTE_METHOD, impl_getImageExtents, "getImageExtents",
+   "n,coordType,i:(uuuu),,o"},
+  {DROUTE_METHOD, impl_getImagePosition, "getImagePosition",
+   "i,x,o:i,y,o:n,coordType,i"},
+  {DROUTE_METHOD, impl_getImageSize, "getImageSize", "i,width,o:i,height,o"},
+  {0, NULL, NULL, NULL}
+};
+
+static DRouteProperty properties[] = {
+  {impl_get_imageDescription, impl_get_imageDescription_str, NULL, NULL,
+   "imageDescription", "s"},
+  {impl_get_imageLocale, impl_get_imageLocale_str, NULL, NULL, "imageLocale",
+   "s"},
+  {NULL, NULL, NULL, NULL, NULL, NULL}
+};
+
+void
+spi_initialize_image (DRouteData * data)
+{
+  droute_add_interface (data, "org.freedesktop.accessibility.Image", methods,
+                       properties,
+                       (DRouteGetDatumFunction) get_image_from_path, NULL);
+};