gio/: fully remove gioalias hacks
[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 "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
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   PROP_CLOSE_BASE
41 };
42
43 static void     g_filter_input_stream_set_property (GObject      *object,
44                                                     guint         prop_id,
45                                                     const GValue *value,
46                                                     GParamSpec   *pspec);
47
48 static void     g_filter_input_stream_get_property (GObject      *object,
49                                                     guint         prop_id,
50                                                     GValue       *value,
51                                                     GParamSpec   *pspec);
52 static void     g_filter_input_stream_finalize     (GObject *object);
53
54
55 static gssize   g_filter_input_stream_read         (GInputStream         *stream,
56                                                     void                 *buffer,
57                                                     gsize                 count,
58                                                     GCancellable         *cancellable,
59                                                     GError              **error);
60 static gssize   g_filter_input_stream_skip         (GInputStream         *stream,
61                                                     gsize                 count,
62                                                     GCancellable         *cancellable,
63                                                     GError              **error);
64 static gboolean g_filter_input_stream_close        (GInputStream         *stream,
65                                                     GCancellable         *cancellable,
66                                                     GError              **error);
67
68 G_DEFINE_TYPE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
69
70 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
71   G_TYPE_FILTER_INPUT_STREAM, GFilterInputStreamPrivate)
72
73 typedef struct
74 {
75   gboolean close_base;
76   GAsyncReadyCallback outstanding_callback;
77   gpointer outstanding_user_data;
78 } GFilterInputStreamPrivate;
79
80 static void
81 g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
82 {
83   GObjectClass *object_class;
84   GInputStreamClass *istream_class;
85
86   object_class = G_OBJECT_CLASS (klass);
87   object_class->get_property = g_filter_input_stream_get_property;
88   object_class->set_property = g_filter_input_stream_set_property;
89   object_class->finalize     = g_filter_input_stream_finalize;
90
91   istream_class = G_INPUT_STREAM_CLASS (klass);
92   istream_class->read_fn  = g_filter_input_stream_read;
93   istream_class->skip  = g_filter_input_stream_skip;
94   istream_class->close_fn = g_filter_input_stream_close;
95
96   g_type_class_add_private (klass, sizeof (GFilterInputStreamPrivate));
97
98   g_object_class_install_property (object_class,
99                                    PROP_BASE_STREAM,
100                                    g_param_spec_object ("base-stream",
101                                                          P_("The Filter Base Stream"),
102                                                          P_("The underlying base stream on which the io ops will be done."),
103                                                          G_TYPE_INPUT_STREAM,
104                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | 
105                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
106
107   g_object_class_install_property (object_class,
108                                    PROP_CLOSE_BASE,
109                                    g_param_spec_boolean ("close-base-stream",
110                                                          P_("Close Base Stream"),
111                                                          P_("If the base stream should be closed when the filter stream is closed."),
112                                                          TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | 
113                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
114 }
115
116 static void
117 g_filter_input_stream_set_property (GObject         *object,
118                                     guint            prop_id,
119                                     const GValue    *value,
120                                     GParamSpec      *pspec)
121 {
122   GFilterInputStream *filter_stream;
123   GObject *obj;
124
125   filter_stream = G_FILTER_INPUT_STREAM (object);
126
127   switch (prop_id) 
128     {
129     case PROP_BASE_STREAM:
130       obj = g_value_dup_object (value);
131       filter_stream->base_stream = G_INPUT_STREAM (obj); 
132       break;
133
134     case PROP_CLOSE_BASE:
135       g_filter_input_stream_set_close_base_stream (filter_stream,
136                                                    g_value_get_boolean (value));
137       break;
138
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141       break;
142     }
143
144 }
145
146 static void
147 g_filter_input_stream_get_property (GObject    *object,
148                                     guint       prop_id,
149                                     GValue     *value,
150                                     GParamSpec *pspec)
151 {
152   GFilterInputStream *filter_stream;
153
154   filter_stream = G_FILTER_INPUT_STREAM (object);
155
156   switch (prop_id)
157     {
158     case PROP_BASE_STREAM:
159       g_value_set_object (value, filter_stream->base_stream);
160       break;
161
162     case PROP_CLOSE_BASE:
163       g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
164       break;
165
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168       break;
169     }
170
171 }
172
173 static void
174 g_filter_input_stream_finalize (GObject *object)
175 {
176   GFilterInputStream *stream;
177
178   stream = G_FILTER_INPUT_STREAM (object);
179
180   g_object_unref (stream->base_stream);
181
182   G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
183 }
184
185 static void
186 g_filter_input_stream_init (GFilterInputStream *stream)
187 {
188
189 }
190
191 /**
192  * g_filter_input_stream_get_base_stream:
193  * @stream: a #GFilterInputStream.
194  * 
195  * Gets the base stream for the filter stream.
196  *
197  * Returns: (transfer none): a #GInputStream.
198  **/
199 GInputStream *
200 g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
201 {
202   g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
203
204   return stream->base_stream;
205 }
206
207 /**
208  * g_filter_input_stream_get_close_base_stream:
209  * @stream: a #GFilterInputStream.
210  *
211  * Returns whether the base stream will be closed when @stream is
212  * closed.
213  *
214  * Return value: %TRUE if the base stream will be closed.
215  **/
216 gboolean
217 g_filter_input_stream_get_close_base_stream (GFilterInputStream *stream)
218 {
219   g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), FALSE);
220
221   return GET_PRIVATE (stream)->close_base;
222 }
223
224 /**
225  * g_filter_input_stream_set_close_base_stream:
226  * @stream: a #GFilterInputStream.
227  * @close_base: %TRUE to close the base stream.
228  *
229  * Sets whether the base stream will be closed when @stream is closed.
230  **/
231 void
232 g_filter_input_stream_set_close_base_stream (GFilterInputStream *stream,
233                                              gboolean            close_base)
234 {
235   GFilterInputStreamPrivate *priv;
236
237   g_return_if_fail (G_IS_FILTER_INPUT_STREAM (stream));
238
239   close_base = !!close_base;
240  
241   priv = GET_PRIVATE (stream);
242
243   if (priv->close_base != close_base)
244     {
245       priv->close_base = close_base;
246       g_object_notify (G_OBJECT (stream), "close-base-stream");
247     }
248 }
249
250 static gssize
251 g_filter_input_stream_read (GInputStream  *stream,
252                             void          *buffer,
253                             gsize          count,
254                             GCancellable  *cancellable,
255                             GError       **error)
256 {
257   GFilterInputStream *filter_stream;
258   GInputStream       *base_stream;
259   gssize              nread;
260
261   filter_stream = G_FILTER_INPUT_STREAM (stream);
262   base_stream = filter_stream->base_stream;
263
264   nread = g_input_stream_read (base_stream,
265                                buffer,
266                                count,
267                                cancellable,
268                                error);
269
270   return nread;
271 }
272
273 static gssize
274 g_filter_input_stream_skip (GInputStream  *stream,
275                             gsize          count,
276                             GCancellable  *cancellable,
277                             GError       **error)
278 {
279   GFilterInputStream *filter_stream;
280   GInputStream       *base_stream;
281   gssize              nskipped;
282
283   filter_stream = G_FILTER_INPUT_STREAM (stream);
284   base_stream = filter_stream->base_stream;
285
286   nskipped = g_input_stream_skip (base_stream,
287                                   count,
288                                   cancellable,
289                                   error);
290   return nskipped;
291 }
292
293 static gboolean
294 g_filter_input_stream_close (GInputStream  *stream,
295                              GCancellable  *cancellable,
296                              GError       **error)
297 {
298   gboolean res = TRUE;
299
300   if (GET_PRIVATE (stream)->close_base)
301     {
302       GFilterInputStream *filter_stream;
303       GInputStream       *base_stream;
304
305       filter_stream = G_FILTER_INPUT_STREAM (stream);
306       base_stream = filter_stream->base_stream;
307
308       res = g_input_stream_close (base_stream,
309                                   cancellable,
310                                   error);
311     }
312
313   return res;
314 }