gio/ docs/reference/gio Merged gio-standalone into glib.
[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 enum {
29   PROP_0,
30   PROP_BASE_STREAM
31 };
32
33 static void     g_filter_output_stream_set_property (GObject      *object,
34                                                      guint         prop_id,
35                                                      const GValue *value,
36                                                      GParamSpec   *pspec);
37
38 static void     g_filter_output_stream_get_property (GObject    *object,
39                                                      guint       prop_id,
40                                                      GValue     *value,
41                                                      GParamSpec *pspec);
42 static void     g_filter_output_stream_dispose      (GObject *object);
43
44
45 static gssize   g_filter_output_stream_write        (GOutputStream *stream,
46                                                      const void    *buffer,
47                                                      gsize          count,
48                                                      GCancellable  *cancellable,
49                                                      GError       **error);
50 static gboolean g_filter_output_stream_flush        (GOutputStream    *stream,
51                                                      GCancellable  *cancellable,
52                                                      GError          **error);
53 static gboolean g_filter_output_stream_close        (GOutputStream  *stream,
54                                                      GCancellable   *cancellable,
55                                                      GError        **error);
56 static void     g_filter_output_stream_write_async  (GOutputStream        *stream,
57                                                      const void           *buffer,
58                                                      gsize                 count,
59                                                      int                   io_priority,
60                                                      GCancellable         *cancellable,
61                                                      GAsyncReadyCallback   callback,
62                                                      gpointer              data);
63 static gssize   g_filter_output_stream_write_finish (GOutputStream        *stream,
64                                                      GAsyncResult         *result,
65                                                      GError              **error);
66 static void     g_filter_output_stream_flush_async  (GOutputStream        *stream,
67                                                      int                   io_priority,
68                                                      GCancellable         *cancellable,
69                                                      GAsyncReadyCallback   callback,
70                                                      gpointer              data);
71 static gboolean g_filter_output_stream_flush_finish (GOutputStream        *stream,
72                                                      GAsyncResult         *result,
73                                                      GError              **error);
74 static void     g_filter_output_stream_close_async  (GOutputStream        *stream,
75                                                      int                   io_priority,
76                                                      GCancellable         *cancellable,
77                                                      GAsyncReadyCallback   callback,
78                                                      gpointer              data);
79 static gboolean g_filter_output_stream_close_finish (GOutputStream        *stream,
80                                                      GAsyncResult         *result,
81                                                      GError              **error);
82
83
84
85 G_DEFINE_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
86
87
88
89 static void
90 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
91 {
92   GObjectClass *object_class;
93   GOutputStreamClass *ostream_class;
94
95   object_class = G_OBJECT_CLASS (klass);
96   object_class->get_property = g_filter_output_stream_get_property;
97   object_class->set_property = g_filter_output_stream_set_property;
98   object_class->dispose      = g_filter_output_stream_dispose;
99     
100   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
101   ostream_class->write = g_filter_output_stream_write;
102   ostream_class->flush = g_filter_output_stream_flush;
103   ostream_class->close = g_filter_output_stream_close;
104   ostream_class->write_async  = g_filter_output_stream_write_async;
105   ostream_class->write_finish = g_filter_output_stream_write_finish;
106   ostream_class->flush_async  = g_filter_output_stream_flush_async;
107   ostream_class->flush_finish = g_filter_output_stream_flush_finish;
108   ostream_class->close_async  = g_filter_output_stream_close_async;
109   ostream_class->close_finish = g_filter_output_stream_close_finish;
110
111   g_object_class_install_property (object_class,
112                                    PROP_BASE_STREAM,
113                                    g_param_spec_object ("base-stream",
114                                                          P_("The Filter Base Stream"),
115                                                          P_("The underlying base stream the io ops will be done on"),
116                                                          G_TYPE_OUTPUT_STREAM,
117                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | 
118                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
119
120 }
121
122 static void
123 g_filter_output_stream_set_property (GObject         *object,
124                                      guint            prop_id,
125                                      const GValue    *value,
126                                      GParamSpec      *pspec)
127 {
128   GFilterOutputStream *filter_stream;
129   GObject *obj;
130
131   filter_stream = G_FILTER_OUTPUT_STREAM (object);
132
133   switch (prop_id) 
134     {
135     case PROP_BASE_STREAM:
136       obj = g_value_dup_object (value);
137       filter_stream->base_stream = G_OUTPUT_STREAM (obj);
138       break;
139
140     default:
141       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142       break;
143     }
144
145 }
146
147 static void
148 g_filter_output_stream_get_property (GObject    *object,
149                                      guint       prop_id,
150                                      GValue     *value,
151                                      GParamSpec *pspec)
152 {
153   GFilterOutputStream *filter_stream;
154
155   filter_stream = G_FILTER_OUTPUT_STREAM (object);
156
157   switch (prop_id)
158     {
159     case PROP_BASE_STREAM:
160       g_value_set_object (value, filter_stream->base_stream);
161       break;
162
163     default:
164       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165       break;
166     }
167
168 }
169
170 static void
171 g_filter_output_stream_dispose (GObject *object)
172 {
173   GFilterOutputStream        *stream;
174
175   stream = G_FILTER_OUTPUT_STREAM (object);
176
177   G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
178   
179   if (stream->base_stream)
180     {
181       g_object_unref (stream->base_stream);
182       stream->base_stream = NULL;
183     }
184 }
185
186
187 static void
188 g_filter_output_stream_init (GFilterOutputStream *stream)
189 {
190 }
191
192 GOutputStream *
193 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
194 {
195   g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
196
197   return stream->base_stream;
198 }
199
200 static gssize
201 g_filter_output_stream_write (GOutputStream *stream,
202                               const void *buffer,
203                               gsize          count,
204                               GCancellable  *cancellable,
205                               GError       **error)
206 {
207   GFilterOutputStream *filter_stream;
208   gssize nwritten;
209
210   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
211
212   nwritten = g_output_stream_write (filter_stream->base_stream,
213                                     buffer,
214                                     count,
215                                     cancellable,
216                                     error);
217
218   return nwritten;
219 }
220
221 static gboolean
222 g_filter_output_stream_flush (GOutputStream    *stream,
223                               GCancellable  *cancellable,
224                               GError          **error)
225 {
226   GFilterOutputStream *filter_stream;
227   gboolean res;
228
229   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
230
231   res = g_output_stream_flush (filter_stream->base_stream,
232                                cancellable,
233                                error);
234
235   return res;
236 }
237
238 static gboolean
239 g_filter_output_stream_close (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_close (filter_stream->base_stream,
249                                cancellable,
250                                error);
251
252   return res;
253 }
254
255 static void
256 g_filter_output_stream_write_async (GOutputStream        *stream,
257                                     const void           *buffer,
258                                     gsize                 count,
259                                     int                   io_priority,
260                                     GCancellable         *cancellable,
261                                     GAsyncReadyCallback   callback,
262                                     gpointer              data)
263 {
264   GFilterOutputStream *filter_stream;
265
266   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
267
268   g_output_stream_write_async (filter_stream->base_stream,
269                                buffer,
270                                count,
271                                io_priority,
272                                cancellable,
273                                callback,
274                                data);
275
276 }
277
278 static gssize
279 g_filter_output_stream_write_finish (GOutputStream        *stream,
280                                      GAsyncResult         *result,
281                                      GError              **error)
282 {
283   GFilterOutputStream *filter_stream;
284   gssize nwritten;
285
286   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
287
288   nwritten = g_output_stream_write_finish (filter_stream->base_stream,
289                                            result,
290                                            error);
291
292   return nwritten;
293 }
294
295 static void
296 g_filter_output_stream_flush_async (GOutputStream        *stream,
297                                     int                   io_priority,
298                                     GCancellable         *cancellable,
299                                     GAsyncReadyCallback   callback,
300                                     gpointer              data)
301 {
302   GFilterOutputStream *filter_stream;
303
304   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
305
306   g_output_stream_flush_async (filter_stream->base_stream,
307                                io_priority,
308                                cancellable,
309                                callback,
310                                data);
311 }
312
313 static gboolean
314 g_filter_output_stream_flush_finish (GOutputStream        *stream,
315                                      GAsyncResult         *result,
316                                      GError              **error)
317 {
318   GFilterOutputStream *filter_stream;
319   gboolean res;
320
321   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
322
323   res = g_output_stream_flush_finish (filter_stream->base_stream,
324                                       result,
325                                       error);
326
327   return res;
328 }
329
330 static void
331 g_filter_output_stream_close_async (GOutputStream        *stream,
332                                     int                   io_priority,
333                                     GCancellable         *cancellable,
334                                     GAsyncReadyCallback   callback,
335                                     gpointer              data)
336 {
337   GFilterOutputStream *filter_stream;
338
339   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
340
341   g_output_stream_close_async (filter_stream->base_stream,
342                                io_priority,
343                                cancellable,
344                                callback,
345                                data);
346 }
347
348 static gboolean
349 g_filter_output_stream_close_finish (GOutputStream        *stream,
350                                      GAsyncResult         *result,
351                                      GError              **error)
352 {
353   GFilterOutputStream *filter_stream;
354   gboolean res;
355
356   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
357
358   res = g_output_stream_close_finish (filter_stream->base_stream,
359                                       result,
360                                       error);
361
362   return res;
363 }
364
365
366 /* vim: ts=2 sw=2 et */