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 Accessibility_ContentStream_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);
132 *length_read += buf->_length;
133 /* we assume a short read equals EOF ... is that right? */
134 if (buf->_length < len || *length_read == size)
137 g_warning ("Buffer length %d", buf->_length);
140 *length_read += buf->_length;
151 /* internal use only, declared in cspi-private.h */
153 cspi_streams_close_all (void)
157 g_hash_table_destroy (streams);
163 * AccessibleStreamableContent_ref:
164 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to
167 * Increment the reference count for an #AccessibleStreamableContent object.
172 AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
174 cspi_object_ref (obj);
178 * AccessibleStreamableContent_unref:
179 * @obj: a pointer to the #AccessibleStreamableContent implementor
180 * on which to operate.
182 * Decrement the reference count for an #AccessibleStreamableContent object.
187 AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
189 cspi_object_unref (obj);
193 * AccessibleStreamableContent_getContentTypes:
194 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
196 * Get a list of strings containing the content mimetypes available from an
197 * #AccessibleStreamableContent implementor.
201 * Returns: an array of strings, terminated by a NULL string, specifying the
202 * mimetypes for which the streamed content is available.
207 AccessibleStreamableContent_getContentTypes (AccessibleStreamableContent *obj)
209 Accessibility_StringSeq *mimeseq;
210 char **content_types;
213 g_return_val_if_fail (obj != NULL, NULL);
215 mimeseq = Accessibility_StreamableContent_getContentTypes (CSPI_OBJREF (obj),
217 cspi_return_val_if_ev ("getContentTypes", NULL);
218 content_types = g_new0 (char *, mimeseq->_length + 1);
219 for (i = 0; i < mimeseq->_length; ++i)
220 content_types[i] = g_strdup (mimeseq->_buffer[i]);
221 content_types [mimeseq->_length] = NULL;
222 CORBA_free (mimeseq);
224 return content_types;
227 * AccessibleStreamableContent_freeContentTypesList:
228 * @obj: the AccessibleStreamableContent implementor on which to operate.
229 * @content_types: a list of content types previously returned by
230 * #AccessibleStreamableContent_getContentTypes.
232 * Free the memory associated with a call to #AccessibleStreamableContent_getContentTypes, once
233 * the result has been used.
238 AccessibleStreamableContent_freeContentTypesList (AccessibleStreamableContent *obj,
239 char **content_types)
244 while (content_types[i])
246 g_free (content_types[i]);
249 g_free (content_types);
254 * AccessibleStreamableContent_open:
255 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
256 * @content_type: a string specifying the content type to retrieve (should match one
257 * of the return strings from #AccessibleStreamableContent_getContentTypes ()).
259 * Open a streaming connection to an AccessibleStreamableContent implementor,
260 * of a particular content type. Note that a client may only have one
261 * open stream per streamable interface instance in the current
266 * Returns: #TRUE if successful, #FALSE if unsuccessful.
270 AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
271 const char *content_type)
273 Accessibility_ContentStream stream;
274 struct StreamCacheItem *cache;
275 stream = Accessibility_StreamableContent_getStream (CSPI_OBJREF (obj),
278 cspi_return_val_if_ev ("getContent", FALSE);
280 if (stream != CORBA_OBJECT_NIL) {
281 cache = g_new0 (struct StreamCacheItem, 1);
282 cache->stream = stream;
283 cache->mimetype = CORBA_string_dup (content_type);
285 g_hash_table_replace (get_streams (), CSPI_OBJREF (obj), cache);
287 * This limits us to one concurrent stream per streamable interface
288 * for a given client.
289 * It might be reasonable for a client to open more than one stream
290 * to content, in different mime-types, at the same time.
299 * AccessibleStreamableContent_close:
300 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
302 * Close the current streaming connection to an AccessibleStreamableContent implementor.
303 * This must be called before any subsequent AccessibleStreamableContent_open
304 * calls on the same object.
308 * Returns: #TRUE if successful, #FALSE if unsuccessful.
312 AccessibleStreamableContent_close (AccessibleStreamableContent *obj)
314 if (CSPI_OBJREF (obj) != CORBA_OBJECT_NIL) {
315 if (g_hash_table_remove (get_streams (), CSPI_OBJREF (obj)))
322 * AccessibleStreamableContent_seek:
323 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
324 * @offset: a long int specifying the offset into the stream.
325 * @seek_type: an enum indicating the seek offset type, may be SEEK_SET,
326 * SEEK_CUR, SEEK_END (as in the lseek() libc command).
328 * Cause the current streamable content connection (obtained via
329 * #AccessibleStreamableContent_open()) to seek to a particular offset in the
334 * Returns: #TRUE if successful, #FALSE if unsuccessful.
338 AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
340 AccessibleStreamableContentSeekType seek_type)
342 Accessibility_ContentStream stream;
343 long int ret_offset = 0;
344 struct StreamCacheItem *cached;
345 Accessibility_ContentStream_SeekType content_seek_type;
347 cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
350 stream = cached->stream;
351 if (stream != CORBA_OBJECT_NIL)
354 case SPI_STREAM_SEEK_SET:
355 content_seek_type = Accessibility_ContentStream_SEEK_SET;
357 case SPI_STREAM_SEEK_END:
358 content_seek_type = Accessibility_ContentStream_SEEK_END;
360 case SPI_STREAM_SEEK_CUR:
362 content_seek_type = Accessibility_ContentStream_SEEK_CURRENT;
365 ret_offset = accessible_content_stream_client_seek (stream, offset,
366 content_seek_type, cspi_ev ());
367 cspi_return_val_if_ev ("seek", FALSE);
374 * AccessibleStreamableContent_read:
375 * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
376 * @buff: a pointer to a buffer into which the resulting bytes read from the stream
378 * @nbytes: a long integer indicating the number of bytes to read/write.
379 * @read_type: currently unused, specifies behavior of reads for streamed content
380 * if blocking is not allowed, etc.
382 * Copy (read) bytes from the currently open streamable content connection
383 * to a buffer. This is a blocking API, in the sense that it does not
384 * return until the bytes have been read, or an error condition is
389 * Returns: an integer indicating the number of bytes read, or -1 on error.
393 AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
396 unsigned int read_type)
398 Accessibility_ContentStream stream;
399 struct StreamCacheItem *cached;
400 cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
404 stream = cached->stream;
405 if (stream != CORBA_OBJECT_NIL)
409 mem = accessible_content_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
410 cspi_return_val_if_ev ("read", FALSE);
413 memcpy (buff, mem, len_read);
415 if ((nbytes == -1) || (len_read == nbytes))
420 else g_message ("no matching stream was opened...");