1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Christian Kellner <gicmo@gnome.org>
24 #include "gfilteroutputstream.h"
25 #include "gsimpleasyncresult.h"
26 #include "goutputstream.h"
32 * SECTION:gfilteroutputstream
33 * @short_description: Filter Output Stream
44 static void g_filter_output_stream_set_property (GObject *object,
49 static void g_filter_output_stream_get_property (GObject *object,
53 static void g_filter_output_stream_dispose (GObject *object);
56 static gssize g_filter_output_stream_write (GOutputStream *stream,
59 GCancellable *cancellable,
61 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_filter_output_stream_close (GOutputStream *stream,
65 GCancellable *cancellable,
68 G_DEFINE_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
70 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
71 G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
76 } GFilterOutputStreamPrivate;
79 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
81 GObjectClass *object_class;
82 GOutputStreamClass *ostream_class;
84 object_class = G_OBJECT_CLASS (klass);
85 object_class->get_property = g_filter_output_stream_get_property;
86 object_class->set_property = g_filter_output_stream_set_property;
87 object_class->dispose = g_filter_output_stream_dispose;
89 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
90 ostream_class->write_fn = g_filter_output_stream_write;
91 ostream_class->flush = g_filter_output_stream_flush;
92 ostream_class->close_fn = g_filter_output_stream_close;
94 g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
96 g_object_class_install_property (object_class,
98 g_param_spec_object ("base-stream",
99 P_("The Filter Base Stream"),
100 P_("The underlying base stream on which the io ops will be done."),
101 G_TYPE_OUTPUT_STREAM,
102 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
103 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
105 g_object_class_install_property (object_class,
107 g_param_spec_boolean ("close-base-stream",
108 P_("Close Base Stream"),
109 P_("If the base stream should be closed when the filter stream is closed."),
110 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
111 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
115 g_filter_output_stream_set_property (GObject *object,
120 GFilterOutputStream *filter_stream;
123 filter_stream = G_FILTER_OUTPUT_STREAM (object);
127 case PROP_BASE_STREAM:
128 obj = g_value_dup_object (value);
129 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
132 case PROP_CLOSE_BASE:
133 g_filter_output_stream_set_close_base_stream (filter_stream,
134 g_value_get_boolean (value));
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 g_filter_output_stream_get_property (GObject *object,
150 GFilterOutputStream *filter_stream;
152 filter_stream = G_FILTER_OUTPUT_STREAM (object);
156 case PROP_BASE_STREAM:
157 g_value_set_object (value, filter_stream->base_stream);
160 case PROP_CLOSE_BASE:
161 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 g_filter_output_stream_dispose (GObject *object)
174 GFilterOutputStream *stream;
176 stream = G_FILTER_OUTPUT_STREAM (object);
178 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
180 if (stream->base_stream)
182 g_object_unref (stream->base_stream);
183 stream->base_stream = NULL;
189 g_filter_output_stream_init (GFilterOutputStream *stream)
194 * g_filter_output_stream_get_base_stream:
195 * @stream: a #GFilterOutputStream.
197 * Gets the base stream for the filter stream.
199 * Returns: a #GOutputStream.
202 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
204 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
206 return stream->base_stream;
210 * g_filter_output_stream_get_close_base_stream:
211 * @stream: a #GFilterOutputStream.
213 * Returns whether the base stream will be closed when @stream is
216 * Return value: %TRUE if the base stream will be closed.
219 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
221 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
223 return GET_PRIVATE (stream)->close_base;
227 * g_filter_output_stream_set_close_base_stream:
228 * @stream: a #GFilterOutputStream.
229 * @close_base: %TRUE to close the base stream.
231 * Sets whether the base stream will be closed when @stream is closed.
234 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
237 GFilterOutputStreamPrivate *priv;
239 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
241 close_base = !!close_base;
243 priv = GET_PRIVATE (stream);
245 if (priv->close_base != close_base)
247 priv->close_base = close_base;
248 g_object_notify (G_OBJECT (stream), "close-base-stream");
253 g_filter_output_stream_write (GOutputStream *stream,
256 GCancellable *cancellable,
259 GFilterOutputStream *filter_stream;
262 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
264 nwritten = g_output_stream_write (filter_stream->base_stream,
274 g_filter_output_stream_flush (GOutputStream *stream,
275 GCancellable *cancellable,
278 GFilterOutputStream *filter_stream;
281 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
283 res = g_output_stream_flush (filter_stream->base_stream,
291 g_filter_output_stream_close (GOutputStream *stream,
292 GCancellable *cancellable,
297 if (GET_PRIVATE (stream)->close_base)
299 GFilterOutputStream *filter_stream;
301 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
303 res = g_output_stream_close (filter_stream->base_stream,
311 #define __G_FILTER_OUTPUT_STREAM_C__
312 #include "gioaliasdef.c"