Implement RFE #326532: remove BonoboStream usage from
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_streamablecontent.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <string.h>
25 #include <libbonobo.h>
26 #include <cspi/spi-private.h>
27
28 #define CORBA_BLOCK_SIZE 65536 /* see libbonobo, dunno where this is officially dictated */
29
30 struct StreamCacheItem {
31   Accessibility_ContentStream stream;
32   gchar *mimetype;
33 };
34
35 static gboolean
36 streams_equal_func (gconstpointer a, gconstpointer b)
37 {
38   const struct StreamCacheItem *c1 = a, *c2 = b;
39   return CORBA_Object_is_equivalent (c1->stream, c2->stream, cspi_ev ());
40 }
41
42 static void
43 stream_cache_item_free (gpointer a)
44 {
45   struct StreamCacheItem *cache_item = a;
46
47   cspi_release_unref (cache_item->stream);
48   SPI_freeString (cache_item->mimetype);
49   g_free (cache_item);
50 }
51
52 static GHashTable *streams = NULL;
53
54 static GHashTable *
55 get_streams (void) 
56 {
57   if (streams == NULL)
58       streams = g_hash_table_new_full (g_direct_hash, streams_equal_func, 
59                                        NULL, stream_cache_item_free);
60   return streams;
61 }
62
63 static CORBA_long
64 accessible_content_stream_client_seek (const Accessibility_ContentStream stream,
65                                       CORBA_long offset,
66                                       Accessibility_ContentStream_SeekType seek_type,
67                                       CORBA_Environment  *opt_ev)
68 {
69         CORBA_Environment  *ev, temp_ev;
70         CORBA_long ret_offset;
71        
72         if (!opt_ev) {
73                 CORBA_exception_init (&temp_ev);
74                 ev = &temp_ev;
75         } else
76                 ev = opt_ev;
77
78         ret_offset = Accessibility_ContentStream_seek (stream, offset, seek_type, ev);
79         if (BONOBO_EX (ev))
80                 ret_offset = -1;
81
82         if (!opt_ev)
83                 CORBA_exception_free (&temp_ev);
84         
85         return ret_offset;
86 }
87
88 static guint8*
89 accessible_content_stream_client_read (const Accessibility_ContentStream stream,
90                                        const size_t size,
91                                        CORBA_long *length_read,
92                                        CORBA_Environment  *ev)
93 {
94         size_t  pos;
95         guint8 *mem;
96         size_t  length;
97
98         g_return_val_if_fail (ev != NULL, NULL);
99
100         if (length_read)
101                 *length_read = size;
102
103         length = size;
104
105         if (length == 0)
106                 return NULL;
107
108         mem = g_try_malloc (length);
109         if (!mem) {
110                 CORBA_exception_set_system (ev, ex_CORBA_NO_MEMORY,
111                                             CORBA_COMPLETED_NO);
112                 return NULL;
113         }
114
115         *length_read = 0;
116
117         for (pos = 0; pos < length;) {
118                 Bonobo_Stream_iobuf *buf;
119                 CORBA_long           len;
120
121                 len = (pos + CORBA_BLOCK_SIZE < length) ?
122                         CORBA_BLOCK_SIZE : length - pos;
123
124                 Accessibility_ContentStream_read (stream, len, &buf, ev);
125
126                 if (BONOBO_EX (ev) || !buf)
127                         goto io_error;
128
129                 if (buf->_length > 0) {
130                         memcpy (mem + pos, buf->_buffer, buf->_length);
131                         pos += buf->_length;
132                 } else {
133                         g_warning ("Buffer length %d", buf->_length);
134                         goto io_error;
135                 }
136                 *length_read += buf->_length;
137
138                 CORBA_free (buf);
139         }
140
141         return mem;
142
143  io_error:
144         return NULL;
145 }
146
147 /* internal use only, declared in cspi-private.h */
148 void
149 cspi_streams_close_all (void)
150 {
151   if (streams)
152     {
153       g_hash_table_destroy (streams);
154       streams = NULL;
155     }
156 }
157
158 /**
159  * AccessibleStreamableContent_ref:
160  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to
161  *       operate.
162  *
163  * Increment the reference count for an #AccessibleStreamableContent object.
164  *
165  * @Since: AT-SPI 1.4
166  **/
167 void
168 AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
169 {
170   cspi_object_ref (obj);
171 }
172
173 /**
174  * AccessibleStreamableContent_unref:
175  * @obj: a pointer to the #AccessibleStreamableContent implementor
176  *       on which to operate. 
177  *
178  * Decrement the reference count for an #AccessibleStreamableContent object.
179  *
180  * @Since: AT-SPI 1.4
181  **/
182 void
183 AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
184 {
185   cspi_object_unref (obj);
186 }
187
188 /**
189  * AccessibleStreamableContent_getContentTypes:
190  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
191  *
192  * Get a list of strings containing the content mimetypes available from an
193  *       #AccessibleStreamableContent implementor.
194  *
195  * @Since: AT-SPI 1.4
196  *
197  * Returns: an array of strings, terminated by a NULL string, specifying the
198  *       mimetypes for which the streamed content is available.
199  *
200  **/
201
202 char **
203 AccessibleStreamableContent_getContentTypes (AccessibleStreamableContent *obj)
204 {
205   Accessibility_StringSeq *mimeseq;
206   char **content_types;
207   int i;
208
209   mimeseq = Accessibility_StreamableContent_getContentTypes (CSPI_OBJREF (obj),
210                                                              cspi_ev ());
211   cspi_return_val_if_ev ("getContentTypes", NULL); 
212
213   content_types = g_new0 (char *, mimeseq->_length + 1);
214   for (i = 0; i < mimeseq->_length; ++i)
215     content_types[i] = CORBA_string_dup (mimeseq->_buffer[i]);
216   content_types [mimeseq->_length] = NULL;
217   CORBA_free (mimeseq);
218
219   return content_types;
220 }
221 /**
222 * AccessibleStreamableContent_freeContentTypesList:
223 * @obj: the AccessibleStreamableContent implementor on which to operate.
224 * @content_types: a list of content types previously returned by 
225 *     #AccessibleStreamableContent_getContentTypes.
226 *
227 * Free the memory associated with a call to #AccessibleStreamableContent_getContentTypes, once 
228 * the result has been used.
229 *
230 * @Since: AT-SPI 1.4
231 **/
232 void
233 AccessibleStreamableContent_freeContentTypesList (AccessibleStreamableContent *obj,
234                                                   char **content_types)
235 {
236   if (content_types) 
237     {
238       gint i = 0;
239       while (content_types[i])
240         {
241           g_free (content_types[i]);
242           i++;
243         }
244       g_free (content_types);
245     }
246 }
247
248 /**
249  * AccessibleStreamableContent_open:
250  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
251  * @content_type: a string specifying the content type to retrieve (should match one
252  * of the return strings from #AccessibleStreamableContent_getContentTypes ()).
253  *
254  * Open a streaming connection to an AccessibleStreamableContent implementor,
255  *       of a particular content type
256  *
257  * @Since: AT-SPI 1.4
258  *
259  * Returns: #TRUE if successful, #FALSE if unsuccessful.
260  *
261  **/
262 SPIBoolean
263 AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
264                                   const char *content_type)
265 {
266   Accessibility_ContentStream stream;
267   struct StreamCacheItem *cache;
268   stream = Accessibility_StreamableContent_getContent (CSPI_OBJREF (obj),
269                                                        content_type,
270                                                        cspi_ev ());
271   cspi_return_val_if_ev ("getContent", FALSE); 
272
273   if (stream != CORBA_OBJECT_NIL) {
274     cache = g_new0 (struct StreamCacheItem, 1);
275     cache->stream = stream;
276     cache->mimetype = CORBA_string_dup (content_type);
277     g_hash_table_replace (get_streams (), stream, cache);
278     return TRUE;
279   }
280   return FALSE;
281 }
282
283 /**
284  * AccessibleStreamableContent_close:
285  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
286  *
287  * Close the current streaming connection to an AccessibleStreamableContent implementor.
288  * This must be called before any subsequent AccessibleStreamableContent_open
289  * calls on the same object.
290  * 
291  * @Since: AT-SPI 1.4
292  *
293  * Returns: #TRUE if successful, #FALSE if unsuccessful.
294  *
295  **/
296 SPIBoolean
297 AccessibleStreamableContent_close (AccessibleStreamableContent *obj)
298 {
299   if (CSPI_OBJREF (obj) != CORBA_OBJECT_NIL) {
300     if (g_hash_table_remove (get_streams (), CSPI_OBJREF (obj)))
301       return TRUE;
302   }
303   return FALSE;
304 }
305
306 /**
307  * AccessibleStreamableContent_seek:
308  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
309  * @offset: a long int specifying the offset into the stream.
310  * @seek_type: an enum indicating the seek offset type, may be SEEK_SET,
311  *            SEEK_CUR, SEEK_END (as in the lseek() libc command).
312  *
313  * Cause the current streamable content connection (obtained via
314  *     #AccessibleStreamableContent_open()) to seek to a particular offset in the
315  *     stream.
316  *
317  * @Since: AT-SPI 1.4
318  *
319  * Returns: #TRUE if successful, #FALSE if unsuccessful.
320  *
321  **/
322 long int
323 AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
324                                   long int offset,
325                                   AccessibleStreamableContentSeekType seek_type)
326 {
327   Accessibility_ContentStream stream;
328   long int ret_offset = 0;
329   struct StreamCacheItem *cached; 
330   Accessibility_ContentStream_SeekType content_seek_type;
331
332   cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
333   if (cached)
334     {
335       stream = cached->stream;
336       if (stream != CORBA_OBJECT_NIL)
337         {
338           switch (seek_type) {
339           case SPI_STREAM_SEEK_SET:
340             content_seek_type = Accessibility_ContentStream_SEEK_SET; 
341             break;
342           case SPI_STREAM_SEEK_END:
343             content_seek_type = Accessibility_ContentStream_SEEK_END; 
344             break;
345           case SPI_STREAM_SEEK_CUR:
346           default:
347             content_seek_type = Accessibility_ContentStream_SEEK_CURRENT; 
348             break;
349           }
350           ret_offset = accessible_content_stream_client_seek (stream, offset, 
351                                                              content_seek_type, cspi_ev ());
352           cspi_return_val_if_ev ("seek", FALSE);
353         }
354     }
355   return ret_offset;
356 }
357
358 /**
359  * AccessibleStreamableContent_read:
360  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
361  * @buff: a pointer to a buffer into which the resulting bytes read from the stream
362  *        are to be written.
363  * @nbytes: a long integer indicating the number of bytes to read/write.
364  * @read_type: currently unused, specifies behavior of reads for streamed content
365  *        if blocking is not allowed, etc.
366  *
367  * Copy (read) bytes from the currently open streamable content connection
368  *     to a buffer.  This is a blocking API, in the sense that it does not 
369  *     return until the bytes have been read, or an error condition is 
370  *     detected.
371  *
372  * @Since: AT-SPI 1.4
373  *
374  * Returns: an integer indicating the number of bytes read, or -1 on error.
375  *
376  **/
377 SPIBoolean
378 AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
379                                   void *buff,
380                                   long int nbytes,
381                                   unsigned int read_type)
382 {
383   Accessibility_ContentStream stream;
384   struct StreamCacheItem *cached; 
385   cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
386   if (cached)
387     {
388       CORBA_long len_read;
389       stream = cached->stream;
390       if (stream != CORBA_OBJECT_NIL)
391         {
392           guint8 *mem;
393           mem = accessible_content_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
394           cspi_return_val_if_ev ("read", FALSE);
395           if (mem)
396             {
397               memcpy (buff, mem, len_read);
398               g_free (mem);
399               if ((nbytes == -1) || (len_read == nbytes))
400                 return TRUE;
401             }
402         }
403     }
404   return FALSE;
405 }
406