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