summaryrefslogtreecommitdiff
path: root/atk-adaptor/adaptors/streamablecontent-adaptor.c
blob: 8192e1a87f6ac048fd3c5c2a2e4998c98cce567a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap) *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* streamablecontent.c : implements the StreamableContent interface */

#include <config.h>
#include <stdio.h>
#include <string.h>

#include <libspi/component.h>
#include <libspi/streamablecontent.h>

/* Our parent Gtk object type */
#define PARENT_TYPE SPI_TYPE_BASE

/* A pointer to our parent object class */
static GObjectClass *spi_streamable_parent_class;

#define SPI_CONTENT_STREAM_TYPE            (spi_content_stream_get_type ())
#define SPI_CONTENT_STREAM(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), SPI_CONTENT_STREAM_TYPE, SpiContentStream))
#define SPI_CONTENT_STREAM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), SPI_CONTENT_STREAM_TYPE, SpiContentStreamClass))
#define SPI_IS_CONTENT_STREAM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SPI_CONTENT_STREAM_TYPE))
#define SPI_IS_CONTENT_STREAM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SPI_CONTENT_STREAM_TYPE))

typedef struct _SpiContentStream SpiContentStream;
typedef struct _SpiContentStreamClass SpiContentStreamClass;

struct _SpiContentStream
{
  BonoboObject parent;
  GIOChannel *gio;
};

struct _SpiContentStreamClass
{
  BonoboObjectClass parent_class;
  POA_Accessibility_ContentStream__epv epv;
};

GType spi_content_stream_get_type (void);

static SpiContentStream *
spi_content_stream_new (GIOChannel * gio)
{
  SpiContentStream *new_stream = g_object_new (SPI_CONTENT_STREAM_TYPE, NULL);
  new_stream->gio = gio;
  return new_stream;
}

static void
spi_content_stream_dispose (GObject * o)
{
  if (SPI_IS_CONTENT_STREAM (o))
    {
      SpiContentStream *stream = SPI_CONTENT_STREAM (o);
      if (stream->gio)
        g_io_channel_unref (stream->gio);
    }
}

static CORBA_long
impl_content_stream_seek (PortableServer_Servant servant,
                          const CORBA_long offset,
                          const Accessibility_ContentStream_SeekType whence,
                          CORBA_Environment * ev)
{
  SpiContentStream *stream =
    SPI_CONTENT_STREAM (bonobo_object_from_servant (servant));
  if (stream && stream->gio)
    {
      GError *err;
      GSeekType seektype = G_SEEK_SET;
      switch (whence)
        {
        case Accessibility_ContentStream_SEEK_CURRENT:
          seektype = G_SEEK_CUR;
          break;
        case Accessibility_ContentStream_SEEK_END:
          seektype = G_SEEK_END;
          break;
        }
      if (g_io_channel_seek_position (stream->gio, (gint64) offset,
                                      seektype, &err) == G_IO_STATUS_NORMAL)
        return offset;
      else
        return -1;
    }
  else
    return -1;
}

static CORBA_long
impl_content_stream_read (PortableServer_Servant servant,
                          const CORBA_long count,
                          Accessibility_ContentStream_iobuf ** buffer,
                          CORBA_Environment * ev)
{
  SpiContentStream *stream =
    SPI_CONTENT_STREAM (bonobo_object_from_servant (servant));
  CORBA_long realcount = 0;

  if (stream && stream->gio)
    {
      gchar *gbuf = NULL;
      GIOStatus status;
      GError *err = NULL;

      /* read the giochannel and determine the actual bytes read... */
      if (count != -1)
        {
          gbuf = g_malloc (count + 1);
          status =
            g_io_channel_read_chars (stream->gio, gbuf, count, &realcount,
                                     &err);
        }
      else
        status =
          g_io_channel_read_to_end (stream->gio, &gbuf, &realcount, &err);

      if (status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_EOF)
        {
          *buffer = Bonobo_Stream_iobuf__alloc ();
          CORBA_sequence_set_release (*buffer, TRUE);

          (*buffer)->_buffer =
            CORBA_sequence_CORBA_octet_allocbuf (realcount);
          (*buffer)->_length = realcount;

          g_memmove ((*buffer)->_buffer, gbuf, realcount);
        }

      g_free (gbuf);
    }

  return realcount;
}

static void
impl_content_stream_close (PortableServer_Servant servant,
                           CORBA_Environment * ev)
{
  GIOStatus status;
  GError *err;
  SpiContentStream *stream =
    SPI_CONTENT_STREAM (bonobo_object_from_servant (servant));
  if (stream && stream->gio)
    {
      status = g_io_channel_shutdown (stream->gio, TRUE, &err);
      g_io_channel_unref (stream->gio);
    }
  if (err)
    g_free (err);
}

static void
spi_content_stream_class_init (SpiContentStreamClass * klass)
{
  POA_Accessibility_ContentStream__epv *epv = &klass->epv;
  GObjectClass *object_class = (GObjectClass *) klass;

  epv->seek = impl_content_stream_seek;
  epv->read = impl_content_stream_read;
  epv->close = impl_content_stream_close;

  object_class->dispose = spi_content_stream_dispose;
}


static void
spi_content_stream_init (SpiContentStream * stream)
{
}


BONOBO_TYPE_FUNC_FULL (SpiContentStream,
                       Accessibility_ContentStream,
                       BONOBO_TYPE_OBJECT, spi_content_stream)
     static AtkStreamableContent
       *get_streamable_from_servant (PortableServer_Servant servant)
{
  SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
  g_return_val_if_fail (object != NULL, NULL);
  g_return_val_if_fail (ATK_IS_STREAMABLE_CONTENT (object->gobj), NULL);
  return ATK_STREAMABLE_CONTENT (object->gobj);
}

/*
 * CORBA Accessibility::StreamableContent::getContentTypes method implementation
 */
static Accessibility_StringSeq *
impl_accessibility_streamable_get_content_types (PortableServer_Servant
                                                 servant,
                                                 CORBA_Environment * ev)
{
  Accessibility_StringSeq *typelist = Accessibility_StringSeq__alloc ();
  AtkStreamableContent *streamable = get_streamable_from_servant (servant);
  int n_types, i;

  typelist->_length = typelist->_maximum = 0;

  g_return_val_if_fail (streamable != NULL, typelist);

  n_types = atk_streamable_content_get_n_mime_types (streamable);

  if (n_types)
    {
      typelist->_length = typelist->_maximum = n_types;
      typelist->_buffer = Accessibility_StringSeq_allocbuf (n_types);
      for (i = 0; i < n_types; ++i)
        {
          const gchar *mimetype =
            atk_streamable_content_get_mime_type (streamable, i);
          typelist->_buffer[i] = CORBA_string_dup (mimetype ? mimetype : "");
        }
    }
  return typelist;
}

/*
 * CORBA Accessibility::StreamableContent::getContent method implementation
 */
static Bonobo_Stream
impl_accessibility_streamable_get_content (PortableServer_Servant servant,
                                           const CORBA_char * content_type,
                                           CORBA_Environment * ev)
{
  Bonobo_Stream stream;
  AtkStreamableContent *streamable = get_streamable_from_servant (servant);
  GIOChannel *gio;

  g_return_val_if_fail (streamable != NULL, NULL);

  gio = atk_streamable_content_get_stream (streamable, content_type);

  stream = CORBA_OBJECT_NIL;    /* deprecated, 
                                 * and it was never implemented,
                                 * so don't bother fixing this 
                                 */
  return stream;
}

/*
 * CORBA Accessibility::StreamableContent::getStream method implementation
 */
static Accessibility_ContentStream
impl_accessibility_streamable_get_stream (PortableServer_Servant servant,
                                          const CORBA_char * content_type,
                                          CORBA_Environment * ev)
{
  SpiContentStream *stream;
  AtkStreamableContent *streamable = get_streamable_from_servant (servant);
  GIOChannel *gio;

  g_return_val_if_fail (streamable != NULL, NULL);

  gio = atk_streamable_content_get_stream (streamable, content_type);

  stream = spi_content_stream_new (gio);

  return bonobo_object_dup_ref (BONOBO_OBJREF (stream), ev);
}

/*
 * CORBA Accessibility::StreamableContent::GetURI method implementation
 */
static CORBA_string
impl_accessibility_streamable_get_uri (PortableServer_Servant servant,
                                       const CORBA_char * content_type,
                                       CORBA_Environment * ev)
{
  gchar *uri;
  AtkStreamableContent *streamable = get_streamable_from_servant (servant);

  g_return_val_if_fail (streamable != NULL, NULL);

  uri = atk_streamable_content_get_uri (streamable, content_type);

  return (uri != NULL ? CORBA_string_dup (uri) : CORBA_string_dup (""));
}

static void
spi_streamable_class_init (SpiStreamableClass * klass)
{
  POA_Accessibility_StreamableContent__epv *epv = &klass->epv;
  spi_streamable_parent_class = g_type_class_peek_parent (klass);

  epv->getContentTypes = impl_accessibility_streamable_get_content_types;
  epv->getContent = impl_accessibility_streamable_get_content;
  epv->getStream = impl_accessibility_streamable_get_stream;
  epv->GetURI = impl_accessibility_streamable_get_uri;
}

static void
spi_streamable_init (SpiStreamable * streamable)
{
}


SpiStreamable *
spi_streamable_interface_new (AtkObject * o)
{
  SpiStreamable *retval = g_object_new (SPI_STREAMABLE_TYPE, NULL);

  spi_base_construct (SPI_BASE (retval), G_OBJECT (o));

  return retval;
}

BONOBO_TYPE_FUNC_FULL (SpiStreamable,
                       Accessibility_StreamableContent,
                       PARENT_TYPE, spi_streamable)