chain up unconditionally in finalize() and dispose(). Also don't
[platform/upstream/glib.git] / gio / gfilterinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Christian Kellner <gicmo@gnome.org> 
21  */
22
23 #include <config.h>
24 #include "gfilterinputstream.h"
25 #include "ginputstream.h"
26 #include "glibintl.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:gfilterinputstream
32  * @short_description: Filter Input Stream
33  * @include: gio/gio.h
34  *
35  **/
36
37 enum {
38   PROP_0,
39   PROP_BASE_STREAM
40 };
41
42 static void     g_filter_input_stream_set_property (GObject      *object,
43                                                     guint         prop_id,
44                                                     const GValue *value,
45                                                     GParamSpec   *pspec);
46
47 static void     g_filter_input_stream_get_property (GObject      *object,
48                                                     guint         prop_id,
49                                                     GValue       *value,
50                                                     GParamSpec   *pspec);
51 static void     g_filter_input_stream_finalize     (GObject *object);
52
53
54 static gssize   g_filter_input_stream_read         (GInputStream         *stream,
55                                                     void                 *buffer,
56                                                     gsize                 count,
57                                                     GCancellable         *cancellable,
58                                                     GError              **error);
59 static gssize   g_filter_input_stream_skip         (GInputStream         *stream,
60                                                     gsize                 count,
61                                                     GCancellable         *cancellable,
62                                                     GError              **error);
63 static gboolean g_filter_input_stream_close        (GInputStream         *stream,
64                                                     GCancellable         *cancellable,
65                                                     GError              **error);
66 static void     g_filter_input_stream_read_async   (GInputStream         *stream,
67                                                     void                 *buffer,
68                                                     gsize                 count,
69                                                     int                   io_priority,
70                                                     GCancellable         *cancellable,
71                                                     GAsyncReadyCallback   callback,
72                                                     gpointer              user_data);
73 static gssize   g_filter_input_stream_read_finish  (GInputStream         *stream,
74                                                     GAsyncResult         *result,
75                                                     GError              **error);
76 static void     g_filter_input_stream_skip_async   (GInputStream         *stream,
77                                                     gsize                 count,
78                                                     int                   io_priority,
79                                                     GCancellable         *cancellabl,
80                                                     GAsyncReadyCallback   callback,
81                                                     gpointer              datae);
82 static gssize   g_filter_input_stream_skip_finish  (GInputStream         *stream,
83                                                     GAsyncResult         *result,
84                                                     GError              **error);
85 static void     g_filter_input_stream_close_async  (GInputStream         *stream,
86                                                     int                   io_priority,
87                                                     GCancellable         *cancellabl,
88                                                     GAsyncReadyCallback   callback,
89                                                     gpointer              data);
90 static gboolean g_filter_input_stream_close_finish (GInputStream         *stream,
91                                                     GAsyncResult         *result,
92                                                     GError              **error);
93
94 G_DEFINE_TYPE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
95
96
97 static void
98 g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
99 {
100   GObjectClass *object_class;
101   GInputStreamClass *istream_class;
102
103   object_class = G_OBJECT_CLASS (klass);
104   object_class->get_property = g_filter_input_stream_get_property;
105   object_class->set_property = g_filter_input_stream_set_property;
106   object_class->finalize     = g_filter_input_stream_finalize;
107
108   istream_class = G_INPUT_STREAM_CLASS (klass);
109   istream_class->read_fn  = g_filter_input_stream_read;
110   istream_class->skip  = g_filter_input_stream_skip;
111   istream_class->close_fn = g_filter_input_stream_close;
112
113   istream_class->read_async   = g_filter_input_stream_read_async;
114   istream_class->read_finish  = g_filter_input_stream_read_finish;
115   istream_class->skip_async   = g_filter_input_stream_skip_async;
116   istream_class->skip_finish  = g_filter_input_stream_skip_finish;
117   istream_class->close_async  = g_filter_input_stream_close_async;
118   istream_class->close_finish = g_filter_input_stream_close_finish;
119
120   g_object_class_install_property (object_class,
121                                    PROP_BASE_STREAM,
122                                    g_param_spec_object ("base-stream",
123                                                          P_("The Filter Base Stream"),
124                                                          P_("The underlying base stream the io ops will be done on"),
125                                                          G_TYPE_INPUT_STREAM,
126                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | 
127                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
128
129 }
130
131 static void
132 g_filter_input_stream_set_property (GObject         *object,
133                                     guint            prop_id,
134                                     const GValue    *value,
135                                     GParamSpec      *pspec)
136 {
137   GFilterInputStream *filter_stream;
138   GObject *obj;
139
140   filter_stream = G_FILTER_INPUT_STREAM (object);
141
142   switch (prop_id) 
143     {
144     case PROP_BASE_STREAM:
145       obj = g_value_dup_object (value);
146       filter_stream->base_stream = G_INPUT_STREAM (obj); 
147       break;
148
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152     }
153
154 }
155
156 static void
157 g_filter_input_stream_get_property (GObject    *object,
158                                     guint       prop_id,
159                                     GValue     *value,
160                                     GParamSpec *pspec)
161 {
162   GFilterInputStream *filter_stream;
163
164   filter_stream = G_FILTER_INPUT_STREAM (object);
165
166   switch (prop_id)
167     {
168     case PROP_BASE_STREAM:
169       g_value_set_object (value, filter_stream->base_stream);
170       break;
171
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175     }
176
177 }
178
179 static void
180 g_filter_input_stream_finalize (GObject *object)
181 {
182   GFilterInputStream *stream;
183
184   stream = G_FILTER_INPUT_STREAM (object);
185
186   g_object_unref (stream->base_stream);
187
188   G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
189 }
190
191 static void
192 g_filter_input_stream_init (GFilterInputStream *stream)
193 {
194
195 }
196
197 /**
198  * g_filter_input_stream_get_base_stream:
199  * @stream: a #GFilterInputStream.
200  * 
201  * Gets the base stream for the filter stream.
202  *
203  * Returns: a #GInputStream.
204  **/
205 GInputStream *
206 g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
207 {
208   g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
209
210   return stream->base_stream;
211 }
212
213 static gssize
214 g_filter_input_stream_read (GInputStream  *stream,
215                             void          *buffer,
216                             gsize          count,
217                             GCancellable  *cancellable,
218                             GError       **error)
219 {
220   GFilterInputStream *filter_stream;
221   GInputStream       *base_stream;
222   gssize              nread;
223
224   filter_stream = G_FILTER_INPUT_STREAM (stream);
225   base_stream = filter_stream->base_stream;
226
227   nread = g_input_stream_read (base_stream,
228                                buffer,
229                                count,
230                                cancellable,
231                                error);
232
233   return nread;
234 }
235
236 static gssize
237 g_filter_input_stream_skip (GInputStream  *stream,
238                             gsize          count,
239                             GCancellable  *cancellable,
240                             GError       **error)
241 {
242   GFilterInputStream *filter_stream;
243   GInputStream       *base_stream;
244   gssize              nskipped;
245
246   filter_stream = G_FILTER_INPUT_STREAM (stream);
247   base_stream = filter_stream->base_stream;
248
249   nskipped = g_input_stream_skip (base_stream,
250                                   count,
251                                   cancellable,
252                                   error);
253   return nskipped;
254 }
255
256 static gboolean
257 g_filter_input_stream_close (GInputStream  *stream,
258                              GCancellable  *cancellable,
259                              GError       **error)
260 {
261   GFilterInputStream *filter_stream;
262   GInputStream       *base_stream;
263   gboolean            res;
264
265   filter_stream = G_FILTER_INPUT_STREAM (stream);
266   base_stream = filter_stream->base_stream;
267
268   res = g_input_stream_close (base_stream,
269                               cancellable,
270                               error);
271
272   return res;
273 }
274
275 static void
276 g_filter_input_stream_read_async (GInputStream        *stream,
277                                   void                *buffer,
278                                   gsize                count,
279                                   int                  io_priority,
280                                   GCancellable        *cancellable,
281                                   GAsyncReadyCallback  callback,
282                                   gpointer             user_data)
283 {
284   GFilterInputStream *filter_stream;
285   GInputStream       *base_stream;
286
287   filter_stream = G_FILTER_INPUT_STREAM (stream);
288   base_stream = filter_stream->base_stream;
289
290   g_input_stream_read_async (base_stream,
291                              buffer,
292                              count,
293                              io_priority,
294                              cancellable,
295                              callback,
296                              user_data);
297 }
298
299 static gssize
300 g_filter_input_stream_read_finish (GInputStream  *stream,
301                                    GAsyncResult  *result,
302                                    GError       **error)
303 {
304   GFilterInputStream *filter_stream;
305   GInputStream       *base_stream;
306   gssize nread;
307
308   filter_stream = G_FILTER_INPUT_STREAM (stream);
309   base_stream = filter_stream->base_stream;
310
311   nread = g_input_stream_read_finish (base_stream,
312                                       result,
313                                       error);
314   
315   return nread;
316 }
317
318 static void
319 g_filter_input_stream_skip_async (GInputStream        *stream,
320                                   gsize                count,
321                                   int                  io_priority,
322                                   GCancellable        *cancellable,
323                                   GAsyncReadyCallback  callback,
324                                   gpointer             user_data)
325 {
326   GFilterInputStream *filter_stream;
327   GInputStream       *base_stream;
328
329   filter_stream = G_FILTER_INPUT_STREAM (stream);
330   base_stream = filter_stream->base_stream;
331
332   g_input_stream_skip_async (base_stream,
333                              count,
334                              io_priority,
335                              cancellable,
336                              callback,
337                              user_data);
338
339 }
340
341 static gssize
342 g_filter_input_stream_skip_finish (GInputStream  *stream,
343                                    GAsyncResult  *result,
344                                    GError       **error)
345 {
346   GFilterInputStream *filter_stream;
347   GInputStream       *base_stream;
348   gssize nskipped;
349
350   filter_stream = G_FILTER_INPUT_STREAM (stream);
351   base_stream = filter_stream->base_stream;
352
353   nskipped = g_input_stream_skip_finish (base_stream,
354                                          result,
355                                          error);
356
357   return nskipped;
358 }
359
360 static void
361 g_filter_input_stream_close_async (GInputStream        *stream,
362                                    int                  io_priority,
363                                    GCancellable        *cancellable,
364                                    GAsyncReadyCallback  callback,
365                                    gpointer             user_data)
366 {
367   GFilterInputStream *filter_stream;
368   GInputStream       *base_stream;
369
370   filter_stream = G_FILTER_INPUT_STREAM (stream);
371   base_stream = filter_stream->base_stream;
372
373   g_input_stream_close_async (base_stream,
374                               io_priority,
375                               cancellable,
376                               callback,
377                               user_data);
378 }
379
380 static gboolean
381 g_filter_input_stream_close_finish (GInputStream  *stream,
382                                     GAsyncResult  *result,
383                                     GError       **error)
384 {
385   GFilterInputStream *filter_stream;
386   GInputStream       *base_stream;
387   gboolean res;
388
389   filter_stream = G_FILTER_INPUT_STREAM (stream);
390   base_stream = filter_stream->base_stream;
391
392   res = g_input_stream_close_finish (stream,
393                                      result,
394                                      error);
395
396   return res;
397 }
398
399 #define __G_FILTER_INPUT_STREAM_C__
400 #include "gioaliasdef.c"