Add .po and .pc files from cspi
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_streamablecontent.c
index ca98642..2bb1632 100644 (file)
  */
 
 #include <string.h>
-#include <libbonobo.h>
 #include <cspi/spi-private.h>
 
+#define CORBA_BLOCK_SIZE 65536 /* see libbonobo, dunno where this is officially dictated */
 
-/* TODO: factor/wrap Bonobo_Stream dependency to cspi/bonobo */
-
+#if 0  // TODO
 struct StreamCacheItem {
-  Bonobo_Stream stream;
+  Accessibility_ContentStream stream;
   gchar *mimetype;
 };
 
@@ -52,7 +51,7 @@ stream_cache_item_free (gpointer a)
 
 static GHashTable *streams = NULL;
 
-GHashTable *
+static GHashTable *
 get_streams (void) 
 {
   if (streams == NULL)
@@ -62,12 +61,11 @@ get_streams (void)
 }
 
 static CORBA_long
-accessible_bonobo_stream_client_seek (const Bonobo_Stream stream,
+accessible_content_stream_client_seek (const Accessibility_ContentStream stream,
                                      CORBA_long offset,
-                                     Bonobo_Stream_SeekType seek_type,
+                                     Accessibility_ContentStream_SeekType seek_type,
                                      CORBA_Environment  *opt_ev)
 {
-       Bonobo_StorageInfo *info;
        CORBA_Environment  *ev, temp_ev;
        CORBA_long ret_offset;
        
@@ -77,7 +75,7 @@ accessible_bonobo_stream_client_seek (const Bonobo_Stream stream,
        } else
                ev = opt_ev;
 
-       ret_offset = Bonobo_Stream_seek (stream, offset, seek_type, ev);
+       ret_offset = Accessibility_ContentStream_seek (stream, offset, seek_type, ev);
        if (BONOBO_EX (ev))
                ret_offset = -1;
 
@@ -87,17 +85,86 @@ accessible_bonobo_stream_client_seek (const Bonobo_Stream stream,
        return ret_offset;
 }
 
+static guint8*
+accessible_content_stream_client_read (const Accessibility_ContentStream stream,
+                                      const size_t size,
+                                      CORBA_long *length_read,
+                                      CORBA_Environment  *ev)
+{
+       size_t  pos;
+       guint8 *mem;
+       size_t  length;
+
+       g_return_val_if_fail (ev != NULL, NULL);
+
+       if (length_read)
+               *length_read = size;
+
+       length = size;
+
+       if (length == 0)
+               return NULL;
+
+       mem = g_try_malloc (length);
+       if (!mem) {
+               CORBA_exception_set_system (ev, ex_CORBA_NO_MEMORY,
+                                           CORBA_COMPLETED_NO);
+               return NULL;
+       }
+
+       *length_read = 0;
+
+       for (pos = 0; pos < length;) {
+               Accessibility_ContentStream_iobuf *buf;
+               CORBA_long           len;
+
+               len = (pos + CORBA_BLOCK_SIZE < length) ?
+                       CORBA_BLOCK_SIZE : length - pos;
+
+               Accessibility_ContentStream_read (stream, len, &buf, ev);
+
+               if (BONOBO_EX (ev) || !buf)
+                       goto io_error;
+
+               if (buf->_length > 0) {
+                       memcpy (mem + pos, buf->_buffer, buf->_length);
+                       pos += buf->_length;
+                       *length_read += buf->_length;
+                       /* we assume a short read equals EOF ... is that right? */
+                       if (buf->_length < len || *length_read == size)
+                           return mem;
+               } else {
+                       g_warning ("Buffer length %d", buf->_length);
+                       goto io_error;
+               }
+               *length_read += buf->_length;
+
+               CORBA_free (buf);
+       }
+
+       return mem;
+
+ io_error:
+       return NULL;
+}
+#endif         // TODO
+
 /* internal use only, declared in cspi-private.h */
 void
 cspi_streams_close_all (void)
 {
+#if 0  // TODO
   if (streams)
     {
       g_hash_table_destroy (streams);
       streams = NULL;
     }
+#else
+  g_warning ("Streams not implemented yet");
+#endif // TODO
 }
 
+#if 0  // TODO
 /**
  * AccessibleStreamableContent_ref:
  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to
@@ -105,6 +172,7 @@ cspi_streams_close_all (void)
  *
  * Increment the reference count for an #AccessibleStreamableContent object.
  *
+ * @Since: AT-SPI 1.4
  **/
 void
 AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
@@ -119,6 +187,7 @@ AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
  *
  * Decrement the reference count for an #AccessibleStreamableContent object.
  *
+ * @Since: AT-SPI 1.4
  **/
 void
 AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
@@ -133,6 +202,8 @@ AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
  * Get a list of strings containing the content mimetypes available from an
  *       #AccessibleStreamableContent implementor.
  *
+ * @Since: AT-SPI 1.4
+ *
  * Returns: an array of strings, terminated by a NULL string, specifying the
  *       mimetypes for which the streamed content is available.
  *
@@ -145,19 +216,30 @@ AccessibleStreamableContent_getContentTypes (AccessibleStreamableContent *obj)
   char **content_types;
   int i;
 
+  g_return_val_if_fail (obj != NULL, NULL);
+
   mimeseq = Accessibility_StreamableContent_getContentTypes (CSPI_OBJREF (obj),
                                                             cspi_ev ());
   cspi_return_val_if_ev ("getContentTypes", NULL); 
-
   content_types = g_new0 (char *, mimeseq->_length + 1);
   for (i = 0; i < mimeseq->_length; ++i)
-    content_types[i] = CORBA_string_dup (mimeseq->_buffer[i]);
+    content_types[i] = g_strdup (mimeseq->_buffer[i]);
   content_types [mimeseq->_length] = NULL;
   CORBA_free (mimeseq);
-
+  
   return content_types;
 }
-
+/**
+* AccessibleStreamableContent_freeContentTypesList:
+* @obj: the AccessibleStreamableContent implementor on which to operate.
+* @content_types: a list of content types previously returned by 
+*     #AccessibleStreamableContent_getContentTypes.
+*
+* Free the memory associated with a call to #AccessibleStreamableContent_getContentTypes, once 
+* the result has been used.
+*
+* Since: AT-SPI 1.4
+**/
 void
 AccessibleStreamableContent_freeContentTypesList (AccessibleStreamableContent *obj,
                                                  char **content_types)
@@ -181,7 +263,11 @@ AccessibleStreamableContent_freeContentTypesList (AccessibleStreamableContent *o
  * of the return strings from #AccessibleStreamableContent_getContentTypes ()).
  *
  * Open a streaming connection to an AccessibleStreamableContent implementor,
- *       of a particular content type
+ *       of a particular content type.  Note that a client may only have one
+ *       open stream per streamable interface instance in the current 
+ *       implementation.
+ *
+ * @Since: AT-SPI 1.4
  *
  * Returns: #TRUE if successful, #FALSE if unsuccessful.
  *
@@ -190,18 +276,26 @@ SPIBoolean
 AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
                                  const char *content_type)
 {
-  Bonobo_Stream stream;
+  Accessibility_ContentStream stream;
   struct StreamCacheItem *cache;
-  stream = Accessibility_StreamableContent_getContent (CSPI_OBJREF (obj),
-                                                      content_type,
-                                                      cspi_ev ());
+  stream = Accessibility_StreamableContent_getStream (CSPI_OBJREF (obj),
+                                                     content_type,
+                                                     cspi_ev ());
   cspi_return_val_if_ev ("getContent", FALSE); 
 
   if (stream != CORBA_OBJECT_NIL) {
     cache = g_new0 (struct StreamCacheItem, 1);
     cache->stream = stream;
     cache->mimetype = CORBA_string_dup (content_type);
-    g_hash_table_replace (get_streams (), stream, cache);
+
+    g_hash_table_replace (get_streams (), CSPI_OBJREF (obj), cache);
+    /* FIXME 
+     * This limits us to one concurrent stream per streamable interface
+     * for a given client.
+     * It might be reasonable for a client to open more than one stream
+     * to content, in different mime-types, at the same time.
+     */
+
     return TRUE;
   }
   return FALSE;
@@ -214,6 +308,8 @@ AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
  * Close the current streaming connection to an AccessibleStreamableContent implementor.
  * This must be called before any subsequent AccessibleStreamableContent_open
  * calls on the same object.
+ * 
+ * @Since: AT-SPI 1.4
  *
  * Returns: #TRUE if successful, #FALSE if unsuccessful.
  *
@@ -239,6 +335,8 @@ AccessibleStreamableContent_close (AccessibleStreamableContent *obj)
  *     #AccessibleStreamableContent_open()) to seek to a particular offset in the
  *     stream.
  *
+ * @Since: AT-SPI 1.4
+ *
  * Returns: #TRUE if successful, #FALSE if unsuccessful.
  *
  **/
@@ -247,10 +345,10 @@ AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
                                  long int offset,
                                  AccessibleStreamableContentSeekType seek_type)
 {
-  Bonobo_Stream stream;
+  Accessibility_ContentStream stream;
   long int ret_offset = 0;
   struct StreamCacheItem *cached; 
-  Bonobo_Stream_SeekType bonobo_seek_type;
+  Accessibility_ContentStream_SeekType content_seek_type;
 
   cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
   if (cached)
@@ -258,22 +356,20 @@ AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
       stream = cached->stream;
       if (stream != CORBA_OBJECT_NIL)
        {
-          guint8 *mem;
          switch (seek_type) {
          case SPI_STREAM_SEEK_SET:
-           bonobo_seek_type = Bonobo_Stream_SeekSet
+           content_seek_type = Accessibility_ContentStream_SEEK_SET
            break;
          case SPI_STREAM_SEEK_END:
-           bonobo_seek_type = Bonobo_Stream_SeekEnd
+           content_seek_type = Accessibility_ContentStream_SEEK_END
            break;
          case SPI_STREAM_SEEK_CUR:
          default:
-           bonobo_seek_type = Bonobo_Stream_SeekCur
+           content_seek_type = Accessibility_ContentStream_SEEK_CURRENT
            break;
          }
-         /* bonobo-client doesn't wrap seek yet, so we have to. */
-         ret_offset = accessible_bonobo_stream_client_seek (stream, offset, 
-                                                            seek_type, cspi_ev ());
+         ret_offset = accessible_content_stream_client_seek (stream, offset, 
+                                                            content_seek_type, cspi_ev ());
          cspi_return_val_if_ev ("seek", FALSE);
        }
     }
@@ -294,6 +390,8 @@ AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
  *     return until the bytes have been read, or an error condition is 
  *     detected.
  *
+ * @Since: AT-SPI 1.4
+ *
  * Returns: an integer indicating the number of bytes read, or -1 on error.
  *
  **/
@@ -303,17 +401,18 @@ AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
                                  long int nbytes,
                                  unsigned int read_type)
 {
-  Bonobo_Stream stream;
+  Accessibility_ContentStream stream;
   struct StreamCacheItem *cached; 
   cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
   if (cached)
     {
-      CORBA_long len_read;
+      CORBA_long len_read = 0;
       stream = cached->stream;
       if (stream != CORBA_OBJECT_NIL)
        {
           guint8 *mem;
-         mem = bonobo_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
+
+         mem = accessible_content_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
          cspi_return_val_if_ev ("read", FALSE);
          if (mem)
             {
@@ -324,6 +423,7 @@ AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
            }
        }
     }
+  else g_message ("no matching stream was opened...");
   return FALSE;
 }
-
+#endif // TODO