Include "config.h" instead of <config.h> Command used: find -name
[platform/upstream/glib.git] / gio / gfilteroutputstream.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 "gfilteroutputstream.h"
25 #include "goutputstream.h"
26 #include "glibintl.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:gfilteroutputstream
32  * @short_description: Filter Output 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_output_stream_set_property (GObject      *object,
43                                                      guint         prop_id,
44                                                      const GValue *value,
45                                                      GParamSpec   *pspec);
46
47 static void     g_filter_output_stream_get_property (GObject    *object,
48                                                      guint       prop_id,
49                                                      GValue     *value,
50                                                      GParamSpec *pspec);
51 static void     g_filter_output_stream_dispose      (GObject *object);
52
53
54 static gssize   g_filter_output_stream_write        (GOutputStream *stream,
55                                                      const void    *buffer,
56                                                      gsize          count,
57                                                      GCancellable  *cancellable,
58                                                      GError       **error);
59 static gboolean g_filter_output_stream_flush        (GOutputStream    *stream,
60                                                      GCancellable  *cancellable,
61                                                      GError          **error);
62 static gboolean g_filter_output_stream_close        (GOutputStream  *stream,
63                                                      GCancellable   *cancellable,
64                                                      GError        **error);
65 static void     g_filter_output_stream_write_async  (GOutputStream        *stream,
66                                                      const void           *buffer,
67                                                      gsize                 count,
68                                                      int                   io_priority,
69                                                      GCancellable         *cancellable,
70                                                      GAsyncReadyCallback   callback,
71                                                      gpointer              data);
72 static gssize   g_filter_output_stream_write_finish (GOutputStream        *stream,
73                                                      GAsyncResult         *result,
74                                                      GError              **error);
75 static void     g_filter_output_stream_flush_async  (GOutputStream        *stream,
76                                                      int                   io_priority,
77                                                      GCancellable         *cancellable,
78                                                      GAsyncReadyCallback   callback,
79                                                      gpointer              data);
80 static gboolean g_filter_output_stream_flush_finish (GOutputStream        *stream,
81                                                      GAsyncResult         *result,
82                                                      GError              **error);
83 static void     g_filter_output_stream_close_async  (GOutputStream        *stream,
84                                                      int                   io_priority,
85                                                      GCancellable         *cancellable,
86                                                      GAsyncReadyCallback   callback,
87                                                      gpointer              data);
88 static gboolean g_filter_output_stream_close_finish (GOutputStream        *stream,
89                                                      GAsyncResult         *result,
90                                                      GError              **error);
91
92
93
94 G_DEFINE_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
95
96
97
98 static void
99 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
100 {
101   GObjectClass *object_class;
102   GOutputStreamClass *ostream_class;
103
104   object_class = G_OBJECT_CLASS (klass);
105   object_class->get_property = g_filter_output_stream_get_property;
106   object_class->set_property = g_filter_output_stream_set_property;
107   object_class->dispose      = g_filter_output_stream_dispose;
108     
109   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
110   ostream_class->write_fn = g_filter_output_stream_write;
111   ostream_class->flush = g_filter_output_stream_flush;
112   ostream_class->close_fn = g_filter_output_stream_close;
113   ostream_class->write_async  = g_filter_output_stream_write_async;
114   ostream_class->write_finish = g_filter_output_stream_write_finish;
115   ostream_class->flush_async  = g_filter_output_stream_flush_async;
116   ostream_class->flush_finish = g_filter_output_stream_flush_finish;
117   ostream_class->close_async  = g_filter_output_stream_close_async;
118   ostream_class->close_finish = g_filter_output_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_OUTPUT_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_output_stream_set_property (GObject      *object,
133                                      guint         prop_id,
134                                      const GValue *value,
135                                      GParamSpec   *pspec)
136 {
137   GFilterOutputStream *filter_stream;
138   GObject *obj;
139
140   filter_stream = G_FILTER_OUTPUT_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_OUTPUT_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_output_stream_get_property (GObject    *object,
158                                      guint       prop_id,
159                                      GValue     *value,
160                                      GParamSpec *pspec)
161 {
162   GFilterOutputStream *filter_stream;
163
164   filter_stream = G_FILTER_OUTPUT_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_output_stream_dispose (GObject *object)
181 {
182   GFilterOutputStream *stream;
183
184   stream = G_FILTER_OUTPUT_STREAM (object);
185
186   G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
187   
188   if (stream->base_stream)
189     {
190       g_object_unref (stream->base_stream);
191       stream->base_stream = NULL;
192     }
193 }
194
195
196 static void
197 g_filter_output_stream_init (GFilterOutputStream *stream)
198 {
199 }
200
201 /**
202  * g_filter_output_stream_get_base_stream:
203  * @stream: a #GFilterOutputStream.
204  * 
205  * Gets the base stream for the filter stream.
206  *
207  * Returns: a #GOutputStream.
208  **/
209 GOutputStream *
210 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
211 {
212   g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
213
214   return stream->base_stream;
215 }
216
217 static gssize
218 g_filter_output_stream_write (GOutputStream  *stream,
219                               const void     *buffer,
220                               gsize           count,
221                               GCancellable   *cancellable,
222                               GError        **error)
223 {
224   GFilterOutputStream *filter_stream;
225   gssize nwritten;
226
227   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
228
229   nwritten = g_output_stream_write (filter_stream->base_stream,
230                                     buffer,
231                                     count,
232                                     cancellable,
233                                     error);
234
235   return nwritten;
236 }
237
238 static gboolean
239 g_filter_output_stream_flush (GOutputStream  *stream,
240                               GCancellable   *cancellable,
241                               GError        **error)
242 {
243   GFilterOutputStream *filter_stream;
244   gboolean res;
245
246   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
247
248   res = g_output_stream_flush (filter_stream->base_stream,
249                                cancellable,
250                                error);
251
252   return res;
253 }
254
255 static gboolean
256 g_filter_output_stream_close (GOutputStream  *stream,
257                               GCancellable   *cancellable,
258                               GError        **error)
259 {
260   GFilterOutputStream *filter_stream;
261   gboolean res;
262
263   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
264
265   res = g_output_stream_close (filter_stream->base_stream,
266                                cancellable,
267                                error);
268
269   return res;
270 }
271
272 static void
273 g_filter_output_stream_write_async (GOutputStream       *stream,
274                                     const void          *buffer,
275                                     gsize                count,
276                                     int                  io_priority,
277                                     GCancellable        *cancellable,
278                                     GAsyncReadyCallback  callback,
279                                     gpointer             data)
280 {
281   GFilterOutputStream *filter_stream;
282
283   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
284
285   g_output_stream_write_async (filter_stream->base_stream,
286                                buffer,
287                                count,
288                                io_priority,
289                                cancellable,
290                                callback,
291                                data);
292
293 }
294
295 static gssize
296 g_filter_output_stream_write_finish (GOutputStream  *stream,
297                                      GAsyncResult   *result,
298                                      GError        **error)
299 {
300   GFilterOutputStream *filter_stream;
301   gssize nwritten;
302
303   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
304
305   nwritten = g_output_stream_write_finish (filter_stream->base_stream,
306                                            result,
307                                            error);
308
309   return nwritten;
310 }
311
312 static void
313 g_filter_output_stream_flush_async (GOutputStream       *stream,
314                                     int                  io_priority,
315                                     GCancellable        *cancellable,
316                                     GAsyncReadyCallback  callback,
317                                     gpointer             data)
318 {
319   GFilterOutputStream *filter_stream;
320
321   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
322
323   g_output_stream_flush_async (filter_stream->base_stream,
324                                io_priority,
325                                cancellable,
326                                callback,
327                                data);
328 }
329
330 static gboolean
331 g_filter_output_stream_flush_finish (GOutputStream  *stream,
332                                      GAsyncResult   *result,
333                                      GError        **error)
334 {
335   GFilterOutputStream *filter_stream;
336   gboolean res;
337
338   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
339
340   res = g_output_stream_flush_finish (filter_stream->base_stream,
341                                       result,
342                                       error);
343
344   return res;
345 }
346
347 static void
348 g_filter_output_stream_close_async (GOutputStream       *stream,
349                                     int                  io_priority,
350                                     GCancellable        *cancellable,
351                                     GAsyncReadyCallback  callback,
352                                     gpointer             data)
353 {
354   GFilterOutputStream *filter_stream;
355
356   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
357
358   g_output_stream_close_async (filter_stream->base_stream,
359                                io_priority,
360                                cancellable,
361                                callback,
362                                data);
363 }
364
365 static gboolean
366 g_filter_output_stream_close_finish (GOutputStream  *stream,
367                                      GAsyncResult   *result,
368                                      GError        **error)
369 {
370   GFilterOutputStream *filter_stream;
371   gboolean res;
372
373   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
374
375   res = g_output_stream_close_finish (filter_stream->base_stream,
376                                       result,
377                                       error);
378
379   return res;
380 }
381
382 #define __G_FILTER_OUTPUT_STREAM_C__
383 #include "gioaliasdef.c"