Update TODO, and also unref deviceeventcontroller when
[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 <cspi/spi-private.h>
25
26
27 /* TODO: factor/wrap Bonobo_Stream dependency to cspi/bonobo */
28
29 struct StreamCacheItem {
30   Bonobo_Stream stream;
31   gchar *mimetype;
32 };
33
34 static gboolean
35 streams_equal_func (gconstpointer a, gconstpointer b)
36 {
37   const struct StreamCacheItem *c1 = a, *c2 = b;
38   return CORBA_Object_is_equivalent (c1->stream, c2->stream, cspi_ev ());
39 }
40
41 static void
42 stream_release (gpointer a)
43 {
44   bonobo_object_release_unref (a);
45 }
46
47 static void
48 stream_cache_item_free (gpointer a)
49 {
50   struct StreamCacheItem *cache_item = a;
51   if (cache_item) {
52     bonobo_object_release_unref (cache_item->stream);
53     SPI_freeString (cache_item->mimetype);
54     g_free (cache_item);
55   }
56 }
57
58 static GHashTable *streams = NULL;
59
60 GHashTable *
61 get_streams (void) 
62 {
63   if (streams == NULL) {
64     streams = g_hash_table_new_full (g_direct_hash, streams_equal_func, 
65                                      stream_release, stream_cache_item_free);
66   }
67   return streams;
68 }
69
70 /* internal use only, declared in cspi-private.h */
71 void
72 cspi_streams_close_all (void)
73 {
74   g_hash_table_destroy (get_streams ());
75 }
76
77 /**
78  * AccessibleStreamableContent_ref:
79  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to
80  *       operate.
81  *
82  * Increment the reference count for an #AccessibleStreamableContent object.
83  *
84  **/
85 void
86 AccessibleStreamableContent_ref (AccessibleStreamableContent *obj)
87 {
88   cspi_object_ref (obj);
89 }
90
91 /**
92  * AccessibleStreamableContent_unref:
93  * @obj: a pointer to the #AccessibleStreamableContent implementor
94  *       on which to operate. 
95  *
96  * Decrement the reference count for an #AccessibleStreamableContent object.
97  *
98  **/
99 void
100 AccessibleStreamableContent_unref (AccessibleStreamableContent *obj)
101 {
102   cspi_object_unref (obj);
103 }
104
105 /**
106  * AccessibleStreamableContent_getContentTypes:
107  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
108  *
109  * Get a list of strings containing the content mimetypes available from an
110  *       #AccessibleStreamableContent implementor.
111  *
112  * Returns: an array of strings, terminated by a NULL string, specifying the
113  *       mimetypes for which the streamed content is available.
114  *
115  **/
116 char **
117 AccessibleStreamableContent_getContentTypes (AccessibleStreamableContent *obj)
118 {
119   Accessibility_StringSeq *mimeseq;
120   char **content_types;
121   int i;
122
123   mimeseq = Accessibility_StreamableContent_getContentTypes (CSPI_OBJREF (obj),
124                                                              cspi_ev ());
125
126   content_types = g_new0 (char *, mimeseq->_length + 1);
127   for (i = 0; i < mimeseq->_length; ++i) {
128     content_types[i] = CORBA_string_dup (mimeseq->_buffer[i]);
129   }
130   content_types [mimeseq->_length] = NULL;
131   CORBA_free (mimeseq);
132
133   return content_types;
134 }
135
136 /**
137  * AccessibleStreamableContent_open:
138  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
139  * @content_type: a string specifying the content type to retrieve (should match one
140  * of the return strings from #AccessibleStreamableContent_getContentTypes ()).
141  *
142  * Open a streaming connection to an AccessibleStreamableContent implementor,
143  *       of a particular content type
144  *
145  * Returns: #TRUE if successful, #FALSE if unsuccessful.
146  *
147  **/
148 SPIBoolean
149 AccessibleStreamableContent_open (AccessibleStreamableContent *obj,
150                                   const char *content_type)
151 {
152   Bonobo_Stream stream;
153   struct StreamCacheItem *cache;
154   stream = Accessibility_StreamableContent_getContent (CSPI_OBJREF (obj),
155                                                        content_type,
156                                                        cspi_ev ());    
157   if (stream != CORBA_OBJECT_NIL) {
158     cache = g_new0 (struct StreamCacheItem, 1);
159     cache->stream = stream;
160     cache->mimetype = CORBA_string_dup (content_type);
161     g_hash_table_replace (get_streams (), stream, cache);
162     return TRUE;
163   }
164   return FALSE;
165 }
166
167 /**
168  * AccessibleStreamableContent_close:
169  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
170  *
171  * Close the current streaming connection to an AccessibleStreamableContent implementor.
172  * This must be called before any subsequent AccessibleStreamableContent_open
173  * calls on the same object.
174  *
175  * Returns: #TRUE if successful, #FALSE if unsuccessful.
176  *
177  **/
178 SPIBoolean
179 AccessibleStreamableContent_close (AccessibleStreamableContent *obj)
180 {
181   if (CSPI_OBJREF (obj) != CORBA_OBJECT_NIL) {
182     if (g_hash_table_remove (get_streams (), CSPI_OBJREF (obj)))
183       return TRUE;
184   }
185   return FALSE;
186 }
187
188 /**
189  * AccessibleStreamableContent_seek:
190  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
191  * @offset: a long int specifying the offset into the stream.
192  * @seek_type: an enum indicating the seek offset type, may be SEEK_SET,
193  *            SEEK_CUR, SEEK_END (as in the lseek() libc command).
194  *
195  * Cause the current streamable content connection (obtained via
196  *     #AccessibleStreamableContent_open()) to seek to a particular offset in the
197  *     stream.
198  *
199  * Returns: #TRUE if successful, #FALSE if unsuccessful.
200  *
201  **/
202 SPIBoolean
203 AccessibleStreamableContent_seek (AccessibleStreamableContent *obj,
204                                   long int offset,
205                                   unsigned int seek_type)
206 {
207   /* currently Bonobo_Stream does not appear to support seek operations */
208   return FALSE;
209 }
210
211 /**
212  * AccessibleStreamableContent_read:
213  * @obj: a pointer to the #AccessibleStreamableContent implementor on which to operate.
214  * @buff: a pointer to a buffer into which the resulting bytes read from the stream
215  *        are to be written.
216  * @nbytes: a long integer indicating the number of bytes to read/write.
217  * @read_type: currently unused, specifies behavior of reads for streamed content
218  *        if blocking is not allowed, etc.
219  *
220  * Copy (read) bytes from the currently open streamable content connection
221  *     to a buffer.
222  *
223  * Returns: an integer indicating the number of bytes read, or -1 on error.
224  *
225  **/
226 SPIBoolean
227 AccessibleStreamableContent_read (AccessibleStreamableContent *obj,
228                                   void *buff,
229                                   long int nbytes,
230                                   unsigned int read_type)
231 {
232   Bonobo_Stream stream;
233   struct StreamCacheItem *cached; 
234   cached = g_hash_table_lookup (get_streams (), CSPI_OBJREF (obj));
235   if (cached) {
236     CORBA_long len_read;
237     stream = cached->stream;
238     if (stream != CORBA_OBJECT_NIL) {
239       guint8 *mem;
240       mem = bonobo_stream_client_read (stream, (size_t) nbytes, &len_read, cspi_ev ());
241       if (mem) memcpy (buff, mem, len_read);    
242       if (mem && ((nbytes == -1) || (len_read == nbytes)))
243         return TRUE;
244     }
245   }
246   return FALSE;
247 }
248