2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
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.
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.
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.
25 #include <libbonobo.h>
26 #include <cspi/spi-private.h>
28 #define CORBA_BLOCK_SIZE 65536 /* see libbonobo, dunno where this is officially dictated */
30 struct StreamCacheItem {
31 Accessibility_ContentStream stream;
36 streams_equal_func (gconstpointer a, gconstpointer b)
38 const struct StreamCacheItem *c1 = a, *c2 = b;
39 return CORBA_Object_is_equivalent (c1->stream, c2->stream, cspi_ev ());
43 stream_cache_item_free (gpointer a)
45 struct StreamCacheItem *cache_item = a;
47 cspi_release_unref (cache_item->stream);
48 SPI_freeString (cache_item->mimetype);
52 static GHashTable *streams = NULL;
58 streams = g_hash_table_new_full (g_direct_hash, streams_equal_func,
59 NULL, stream_cache_item_free);
64 accessible_content_stream_client_seek (const Accessibility_ContentStream stream,
66 Accessibility_ContentStream_SeekType seek_type,
67 CORBA_Environment *opt_ev)
69 CORBA_Environment *ev, temp_ev;
70 CORBA_long ret_offset;
73 CORBA_exception_init (&temp_ev);
78 ret_offset = Accessibility_ContentStream_seek (stream, offset, seek_type, ev);
83 CORBA_exception_free (&temp_ev);
89 accessible_content_stream_client_read (const Accessibility_ContentStream stream,
91 CORBA_long *length_read,
92 CORBA_Environment *ev)
98 g_return_val_if_fail (ev != NULL, NULL);
108 mem = g_try_malloc (length);
110 CORBA_exception_set_system (ev, ex_CORBA_NO_MEMORY,
117 for (pos = 0; pos < length;) {
118 Bonobo_Stream_iobuf *buf;
121 len = (pos + CORBA_BLOCK_SIZE < length) ?
122 CORBA_BLOCK_SIZE : length - pos;
124 Accessibility_ContentStream_read (stream, len, &buf, ev);
126 if (BONOBO_EX (ev) || !buf)
129 if (buf->_length > 0) {
130 memcpy (mem + pos, buf->_buffer, buf->_length);
133 g_warning ("Buffer length %d", buf->_length);
136 *length_read += buf->_length;
147 /* internal use only, declared in cspi-private.h */
149 cspi_streams_close_all (void)
153 g_hash_table_destroy (streams);
159 * AccessibleStreamableContent_ref:
160 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to
163 * Increment the reference count for an #AccessibleStreamableContent object.
168 AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
170 cspi_object_ref (obj);
174 * AccessibleStreamableContent_unref:
175 * @obj: a pointer to the #AccessibleStreamableContent implementor
176 * on which to operate.
178 * Decrement the reference count for an #AccessibleStreamableContent object.
183 AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
185 cspi_object_unref (obj);
189 * AccessibleStreamableContent_getContentTypes:
190 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
192 * Get a list of strings containing the content mimetypes available from an
193 * #AccessibleStreamableContent implementor.
197 * Returns: an array of strings, terminated by a NULL string, specifying the
198 * mimetypes for which the streamed content is available.
203 AccessibleStreamableContent_getContentTypes (AccessibleStreamableContent *obj)
205 Accessibility_StringSeq *mimeseq;
206 char **content_types;
209 mimeseq = Accessibility_StreamableContent_getContentTypes (CSPI_OBJREF (obj),
211 cspi_return_val_if_ev ("getContentTypes", NULL);
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);
219 return content_types;
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.
227 * Free the memory associated with a call to #AccessibleStreamableContent_getContentTypes, once
228 * the result has been used.
233 AccessibleStreamableContent_freeContentTypesList (AccessibleStreamableContent *obj,
234 char **content_types)
239 while (content_types[i])
241 g_free (content_types[i]);
244 g_free (content_types);
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 ()).
254 * Open a streaming connection to an AccessibleStreamableContent implementor,
255 * of a particular content type
259 * Returns: #TRUE if successful, #FALSE if unsuccessful.
263 AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
264 const char *content_type)
266 Accessibility_ContentStream stream;
267 struct StreamCacheItem *cache;
268 stream = Accessibility_StreamableContent_getContent (CSPI_OBJREF (obj),
271 cspi_return_val_if_ev ("getContent", FALSE);
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);
284 * AccessibleStreamableContent_close:
285 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
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.
293 * Returns: #TRUE if successful, #FALSE if unsuccessful.
297 AccessibleStreamableContent_close (AccessibleStreamableContent *obj)
299 if (CSPI_OBJREF (obj) != CORBA_OBJECT_NIL) {
300 if (g_hash_table_remove (get_streams (), CSPI_OBJREF (obj)))
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).
313 * Cause the current streamable content connection (obtained via
314 * #AccessibleStreamableContent_open()) to seek to a particular offset in the
319 * Returns: #TRUE if successful, #FALSE if unsuccessful.
323 AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
325 AccessibleStreamableContentSeekType seek_type)
327 Accessibility_ContentStream stream;
328 long int ret_offset = 0;
329 struct StreamCacheItem *cached;
330 Accessibility_ContentStream_SeekType content_seek_type;
332 cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
335 stream = cached->stream;
336 if (stream != CORBA_OBJECT_NIL)
339 case SPI_STREAM_SEEK_SET:
340 content_seek_type = Accessibility_ContentStream_SEEK_SET;
342 case SPI_STREAM_SEEK_END:
343 content_seek_type = Accessibility_ContentStream_SEEK_END;
345 case SPI_STREAM_SEEK_CUR:
347 content_seek_type = Accessibility_ContentStream_SEEK_CURRENT;
350 ret_offset = accessible_content_stream_client_seek (stream, offset,
351 content_seek_type, cspi_ev ());
352 cspi_return_val_if_ev ("seek", FALSE);
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
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.
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
374 * Returns: an integer indicating the number of bytes read, or -1 on error.
378 AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
381 unsigned int read_type)
383 Accessibility_ContentStream stream;
384 struct StreamCacheItem *cached;
385 cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
389 stream = cached->stream;
390 if (stream != CORBA_OBJECT_NIL)
393 mem = accessible_content_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
394 cspi_return_val_if_ev ("read", FALSE);
397 memcpy (buff, mem, len_read);
399 if ((nbytes == -1) || (len_read == nbytes))