Implement RFE #326532: remove BonoboStream usage from
[platform/core/uifw/at-spi2-atk.git] / libspi / 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 /* streamablecontent.c : implements the StreamableContent interface */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <libspi/accessible.h>
29 #include <libspi/component.h>
30 #include <libspi/streamablecontent.h>
31
32 /* Our parent Gtk object type */
33 #define PARENT_TYPE SPI_TYPE_BASE
34
35 /* A pointer to our parent object class */
36 static GObjectClass *spi_streamable_parent_class;
37
38 #define SPI_CONTENT_STREAM_TYPE            (spi_content_stream_get_type ())
39 #define SPI_CONTENT_STREAM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), SPI_CONTENT_STREAM_TYPE, SpiContentStream))
40 #define SPI_CONTENT_STREAM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), SPI_CONTENT_STREAM_TYPE, SpiContentStreamClass))
41 #define SPI_IS_CONTENT_STREAM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SPI_CONTENT_STREAM_TYPE))
42 #define SPI_IS_CONTENT_STREAM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SPI_CONTENT_STREAM_TYPE))
43
44 typedef struct _SpiContentStream SpiContentStream;
45 typedef struct _SpiContentStreamClass SpiContentStreamClass;
46
47 struct _SpiContentStream {
48   BonoboObject parent;
49   GIOChannel *gio;
50 };
51
52 struct _SpiContentStreamClass {
53   BonoboObjectClass parent_class;
54   POA_Accessibility_ContentStream__epv epv;
55 };
56
57 GType        spi_content_stream_get_type (void);
58
59 static SpiContentStream*
60 spi_content_stream_new (GIOChannel *gio)
61 {
62   SpiContentStream *new_stream = g_object_new (SPI_CONTENT_STREAM_TYPE, NULL);
63   new_stream->gio = gio;
64   return new_stream;
65 }
66
67 static void
68 spi_content_stream_dispose (GObject *o)
69 {
70     if (SPI_IS_CONTENT_STREAM (o))
71     {
72         SpiContentStream *stream = SPI_CONTENT_STREAM (o);
73         if (stream->gio) g_io_channel_unref (stream->gio);
74     }
75 }
76
77 static CORBA_long
78 impl_content_stream_seek (PortableServer_Servant servant,
79                           const CORBA_long offset, 
80                           const Accessibility_ContentStream_SeekType whence,
81                           CORBA_Environment *ev)
82 {
83   SpiContentStream *stream = SPI_CONTENT_STREAM (bonobo_object_from_servant(servant));
84   if (stream && stream->gio)
85   {
86       GError *err;
87       GSeekType seektype = G_SEEK_SET;
88       switch (whence) {
89           case Accessibility_ContentStream_SEEK_CURRENT:
90               seektype = G_SEEK_CUR;
91               break;
92           case Accessibility_ContentStream_SEEK_END:
93               seektype = G_SEEK_END;
94               break;
95       }
96       if (g_io_channel_seek_position (stream->gio, (gint64) offset, 
97                                      seektype, &err) == G_IO_STATUS_NORMAL)
98           return offset;
99       else
100           return -1;
101   }
102   else
103       return -1;
104 }
105
106 static CORBA_long
107 impl_content_stream_read (PortableServer_Servant servant,
108                           const CORBA_long count, 
109                           Accessibility_ContentStream_iobuf** buffer,
110                           CORBA_Environment *ev)
111 {
112   SpiContentStream *stream = SPI_CONTENT_STREAM (bonobo_object_from_servant(servant));
113   CORBA_long realcount = 0;
114   if (stream && stream->gio)
115   {
116       gchar *gbuf = NULL;
117       GIOStatus status;
118       GError *err;
119       /* read the giochannel and determine the actual bytes read...*/
120       if (count != -1)
121           status = g_io_channel_read_chars (stream->gio, &gbuf, count, &realcount, &err);
122       else
123           status = g_io_channel_read_to_end (stream->gio, &gbuf, &realcount, &err);
124
125       if (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_EOF)
126       {
127           *buffer = Bonobo_Stream_iobuf__alloc ();
128           CORBA_sequence_set_release (*buffer, TRUE);
129
130           (*buffer)->_buffer = CORBA_sequence_CORBA_octet_allocbuf (realcount);
131           (*buffer)->_length = realcount;
132       
133           memcpy ((*buffer)->_buffer, gbuf, realcount);  
134       }
135
136       g_free (gbuf);
137   }
138
139   return realcount;
140 }
141
142 static void
143 impl_content_stream_close (PortableServer_Servant servant, 
144                            CORBA_Environment *ev)
145 {
146     GIOStatus status;
147     GError *err;
148     SpiContentStream *stream = SPI_CONTENT_STREAM (bonobo_object_from_servant(servant));
149     if (stream && stream->gio) status = g_io_channel_shutdown (stream->gio, TRUE, &err);
150     if (err) g_free (err);
151 }
152
153 static void
154 spi_content_stream_class_init (SpiContentStreamClass *klass)
155 {
156   POA_Accessibility_ContentStream__epv *epv = &klass->epv;
157   GObjectClass * object_class = (GObjectClass *) klass;
158
159   epv->seek = impl_content_stream_seek;
160   epv->read = impl_content_stream_read;
161   epv->close = impl_content_stream_close;
162
163   object_class->dispose = spi_content_stream_dispose;
164 }
165
166
167 static void
168 spi_content_stream_init (SpiContentStream *stream)
169 {
170 }
171
172
173 BONOBO_TYPE_FUNC_FULL (SpiContentStream,
174                        Accessibility_ContentStream,
175                        BONOBO_TYPE_OBJECT,
176                        spi_content_stream)
177
178 static AtkStreamableContent *
179 get_streamable_from_servant (PortableServer_Servant servant)
180 {
181   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
182   g_return_val_if_fail (object != NULL, NULL);
183   g_return_val_if_fail (ATK_IS_STREAMABLE_CONTENT(object->gobj), NULL);
184   return ATK_STREAMABLE_CONTENT (object->gobj);
185 }
186
187 /*
188  * CORBA Accessibility::StreamableContent::getContentTypes method implementation
189  */
190 static Accessibility_StringSeq*
191 impl_accessibility_streamable_get_content_types (PortableServer_Servant servant,
192                                                  CORBA_Environment     *ev)
193 {
194   Accessibility_StringSeq *typelist = Accessibility_StringSeq__alloc ();
195   AtkStreamableContent *streamable = get_streamable_from_servant (servant);
196   int n_types, i;
197
198   typelist->_length = 0;
199   g_return_val_if_fail (streamable != NULL, typelist);
200
201   n_types = atk_streamable_content_get_n_mime_types (streamable);
202   typelist->_length = n_types;
203   typelist->_buffer = Accessibility_StringSeq_allocbuf (n_types);
204   for (i = 0; i < n_types; ++i) {
205     const gchar *mimetype = atk_streamable_content_get_mime_type (streamable, i);
206     typelist->_buffer[i] = CORBA_string_dup (mimetype ? mimetype : "");
207   }
208
209   return typelist;
210 }
211
212 /*
213  * CORBA Accessibility::StreamableContent::getContent method implementation
214  */
215 static Bonobo_Stream
216 impl_accessibility_streamable_get_content (PortableServer_Servant servant,
217                                            const CORBA_char * content_type,
218                                            CORBA_Environment     *ev)
219 {
220   Bonobo_Stream stream;
221   AtkStreamableContent *streamable = get_streamable_from_servant (servant);
222   GIOChannel *gio;
223
224   g_return_val_if_fail (streamable != NULL, NULL);
225
226   gio = atk_streamable_content_get_stream (streamable, content_type);
227
228   stream = CORBA_OBJECT_NIL; /* FIXME! */
229
230   return stream;
231 }
232
233 /*
234  * CORBA Accessibility::StreamableContent::getStream method implementation
235  */
236 static Accessibility_ContentStream
237 impl_accessibility_streamable_get_stream (PortableServer_Servant servant,
238                                            const CORBA_char * content_type,
239                                            CORBA_Environment     *ev)
240 {
241   SpiContentStream *stream;
242   AtkStreamableContent *streamable = get_streamable_from_servant (servant);
243   GIOChannel *gio;
244
245   g_return_val_if_fail (streamable != NULL, NULL);
246
247   gio = atk_streamable_content_get_stream (streamable, content_type);
248
249   stream = spi_content_stream_new (gio); 
250
251   return bonobo_object_dup_ref (BONOBO_OBJREF (stream), ev);
252 }
253
254 /*
255  * CORBA Accessibility::StreamableContent::getURI method implementation
256  */
257 static CORBA_string
258 impl_accessibility_streamable_get_uri (PortableServer_Servant servant,
259                                            const CORBA_char * content_type,
260                                            CORBA_Environment     *ev)
261 {
262   gchar *uri;
263   AtkStreamableContent *streamable = get_streamable_from_servant (servant);
264
265   g_return_val_if_fail (streamable != NULL, NULL);
266
267   uri = atk_streamable_content_get_uri (streamable, content_type);
268
269   return (uri != NULL ? CORBA_string_dup (uri) : CORBA_string_dup (""));
270 }
271
272 static void
273 spi_streamable_class_init (SpiStreamableClass *klass)
274 {
275         POA_Accessibility_StreamableContent__epv *epv = &klass->epv;
276         spi_streamable_parent_class = g_type_class_peek_parent (klass);
277
278         epv->getContentTypes = impl_accessibility_streamable_get_content_types;
279         epv->getContent = impl_accessibility_streamable_get_content;
280         epv->getStream = impl_accessibility_streamable_get_stream;
281         epv->getURI = impl_accessibility_streamable_get_uri;
282 }
283
284 static void
285 spi_streamable_init (SpiStreamable *streamable)
286 {
287 }
288
289 BONOBO_TYPE_FUNC_FULL (SpiStreamable,
290                        Accessibility_StreamableContent,
291                        PARENT_TYPE,
292                        spi_streamable)
293
294 SpiStreamable *
295 spi_streamable_interface_new (AtkObject *o)
296 {
297     SpiStreamable *retval = g_object_new (SPI_STREAMABLE_TYPE, NULL);
298
299     spi_base_construct (SPI_BASE (retval), G_OBJECT(o));
300
301     return retval;
302 }