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