Fixed headers in galician translation file
[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 "gsimpleasyncresult.h"
26 #include "goutputstream.h"
27 #include "glibintl.h"
28
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   PROP_CLOSE_BASE
41 };
42
43 static void     g_filter_output_stream_set_property (GObject      *object,
44                                                      guint         prop_id,
45                                                      const GValue *value,
46                                                      GParamSpec   *pspec);
47
48 static void     g_filter_output_stream_get_property (GObject    *object,
49                                                      guint       prop_id,
50                                                      GValue     *value,
51                                                      GParamSpec *pspec);
52 static void     g_filter_output_stream_dispose      (GObject *object);
53
54
55 static gssize   g_filter_output_stream_write        (GOutputStream *stream,
56                                                      const void    *buffer,
57                                                      gsize          count,
58                                                      GCancellable  *cancellable,
59                                                      GError       **error);
60 static gboolean g_filter_output_stream_flush        (GOutputStream    *stream,
61                                                      GCancellable  *cancellable,
62                                                      GError          **error);
63 static gboolean g_filter_output_stream_close        (GOutputStream  *stream,
64                                                      GCancellable   *cancellable,
65                                                      GError        **error);
66
67 G_DEFINE_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
68
69 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
70   G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
71
72 typedef struct
73 {
74   gboolean close_base;
75 } GFilterOutputStreamPrivate;
76
77 static void
78 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
79 {
80   GObjectClass *object_class;
81   GOutputStreamClass *ostream_class;
82
83   object_class = G_OBJECT_CLASS (klass);
84   object_class->get_property = g_filter_output_stream_get_property;
85   object_class->set_property = g_filter_output_stream_set_property;
86   object_class->dispose      = g_filter_output_stream_dispose;
87     
88   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
89   ostream_class->write_fn = g_filter_output_stream_write;
90   ostream_class->flush = g_filter_output_stream_flush;
91   ostream_class->close_fn = g_filter_output_stream_close;
92
93   g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
94
95   g_object_class_install_property (object_class,
96                                    PROP_BASE_STREAM,
97                                    g_param_spec_object ("base-stream",
98                                                          P_("The Filter Base Stream"),
99                                                          P_("The underlying base stream on which the io ops will be done."),
100                                                          G_TYPE_OUTPUT_STREAM,
101                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | 
102                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
103
104   g_object_class_install_property (object_class,
105                                    PROP_CLOSE_BASE,
106                                    g_param_spec_boolean ("close-base-stream",
107                                                          P_("Close Base Stream"),
108                                                          P_("If the base stream should be closed when the filter stream is closed."),
109                                                          TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
110                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
111 }
112
113 static void
114 g_filter_output_stream_set_property (GObject      *object,
115                                      guint         prop_id,
116                                      const GValue *value,
117                                      GParamSpec   *pspec)
118 {
119   GFilterOutputStream *filter_stream;
120   GObject *obj;
121
122   filter_stream = G_FILTER_OUTPUT_STREAM (object);
123
124   switch (prop_id) 
125     {
126     case PROP_BASE_STREAM:
127       obj = g_value_dup_object (value);
128       filter_stream->base_stream = G_OUTPUT_STREAM (obj);
129       break;
130
131     case PROP_CLOSE_BASE:
132       g_filter_output_stream_set_close_base_stream (filter_stream,
133                                                     g_value_get_boolean (value));
134       break;
135
136     default:
137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138       break;
139     }
140
141 }
142
143 static void
144 g_filter_output_stream_get_property (GObject    *object,
145                                      guint       prop_id,
146                                      GValue     *value,
147                                      GParamSpec *pspec)
148 {
149   GFilterOutputStream *filter_stream;
150
151   filter_stream = G_FILTER_OUTPUT_STREAM (object);
152
153   switch (prop_id)
154     {
155     case PROP_BASE_STREAM:
156       g_value_set_object (value, filter_stream->base_stream);
157       break;
158
159     case PROP_CLOSE_BASE:
160       g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
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 /**
193  * g_filter_output_stream_get_base_stream:
194  * @stream: a #GFilterOutputStream.
195  * 
196  * Gets the base stream for the filter stream.
197  *
198  * Returns: (transfer none): a #GOutputStream.
199  **/
200 GOutputStream *
201 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
202 {
203   g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
204
205   return stream->base_stream;
206 }
207
208 /**
209  * g_filter_output_stream_get_close_base_stream:
210  * @stream: a #GFilterOutputStream.
211  *
212  * Returns whether the base stream will be closed when @stream is
213  * closed.
214  *
215  * Return value: %TRUE if the base stream will be closed.
216  **/
217 gboolean
218 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
219 {
220   g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
221
222   return GET_PRIVATE (stream)->close_base;
223 }
224
225 /**
226  * g_filter_output_stream_set_close_base_stream:
227  * @stream: a #GFilterOutputStream.
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_output_stream_set_close_base_stream (GFilterOutputStream *stream,
234                                               gboolean             close_base)
235 {
236   GFilterOutputStreamPrivate *priv;
237
238   g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
239
240   close_base = !!close_base;
241
242   priv = 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_output_stream_write (GOutputStream  *stream,
253                               const void     *buffer,
254                               gsize           count,
255                               GCancellable   *cancellable,
256                               GError        **error)
257 {
258   GFilterOutputStream *filter_stream;
259   gssize nwritten;
260
261   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
262
263   nwritten = g_output_stream_write (filter_stream->base_stream,
264                                     buffer,
265                                     count,
266                                     cancellable,
267                                     error);
268
269   return nwritten;
270 }
271
272 static gboolean
273 g_filter_output_stream_flush (GOutputStream  *stream,
274                               GCancellable   *cancellable,
275                               GError        **error)
276 {
277   GFilterOutputStream *filter_stream;
278   gboolean res;
279
280   filter_stream = G_FILTER_OUTPUT_STREAM (stream);
281
282   res = g_output_stream_flush (filter_stream->base_stream,
283                                cancellable,
284                                error);
285
286   return res;
287 }
288
289 static gboolean
290 g_filter_output_stream_close (GOutputStream  *stream,
291                               GCancellable   *cancellable,
292                               GError        **error)
293 {
294   gboolean res = TRUE;
295
296   if (GET_PRIVATE (stream)->close_base)
297     {
298       GFilterOutputStream *filter_stream;
299
300       filter_stream = G_FILTER_OUTPUT_STREAM (stream);
301
302       res = g_output_stream_close (filter_stream->base_stream,
303                                    cancellable,
304                                    error);
305     }
306
307   return res;
308 }