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"
31 * SECTION:gfilteroutputstream
32 * @short_description: Filter Output Stream
43 static void g_filter_output_stream_set_property (GObject *object,
48 static void g_filter_output_stream_get_property (GObject *object,
52 static void g_filter_output_stream_dispose (GObject *object);
55 static gssize g_filter_output_stream_write (GOutputStream *stream,
58 GCancellable *cancellable,
60 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
61 GCancellable *cancellable,
63 static gboolean g_filter_output_stream_close (GOutputStream *stream,
64 GCancellable *cancellable,
67 G_DEFINE_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
69 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
70 G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
75 } GFilterOutputStreamPrivate;
78 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
80 GObjectClass *object_class;
81 GOutputStreamClass *ostream_class;
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;
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;
93 g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
95 g_object_class_install_property (object_class,
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));
104 g_object_class_install_property (object_class,
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));
114 g_filter_output_stream_set_property (GObject *object,
119 GFilterOutputStream *filter_stream;
122 filter_stream = G_FILTER_OUTPUT_STREAM (object);
126 case PROP_BASE_STREAM:
127 obj = g_value_dup_object (value);
128 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
131 case PROP_CLOSE_BASE:
132 g_filter_output_stream_set_close_base_stream (filter_stream,
133 g_value_get_boolean (value));
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 g_filter_output_stream_get_property (GObject *object,
149 GFilterOutputStream *filter_stream;
151 filter_stream = G_FILTER_OUTPUT_STREAM (object);
155 case PROP_BASE_STREAM:
156 g_value_set_object (value, filter_stream->base_stream);
159 case PROP_CLOSE_BASE:
160 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 g_filter_output_stream_dispose (GObject *object)
173 GFilterOutputStream *stream;
175 stream = G_FILTER_OUTPUT_STREAM (object);
177 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
179 if (stream->base_stream)
181 g_object_unref (stream->base_stream);
182 stream->base_stream = NULL;
188 g_filter_output_stream_init (GFilterOutputStream *stream)
193 * g_filter_output_stream_get_base_stream:
194 * @stream: a #GFilterOutputStream.
196 * Gets the base stream for the filter stream.
198 * Returns: (transfer none): a #GOutputStream.
201 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
203 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
205 return stream->base_stream;
209 * g_filter_output_stream_get_close_base_stream:
210 * @stream: a #GFilterOutputStream.
212 * Returns whether the base stream will be closed when @stream is
215 * Return value: %TRUE if the base stream will be closed.
218 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
220 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
222 return GET_PRIVATE (stream)->close_base;
226 * g_filter_output_stream_set_close_base_stream:
227 * @stream: a #GFilterOutputStream.
228 * @close_base: %TRUE to close the base stream.
230 * Sets whether the base stream will be closed when @stream is closed.
233 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
236 GFilterOutputStreamPrivate *priv;
238 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
240 close_base = !!close_base;
242 priv = GET_PRIVATE (stream);
244 if (priv->close_base != close_base)
246 priv->close_base = close_base;
247 g_object_notify (G_OBJECT (stream), "close-base-stream");
252 g_filter_output_stream_write (GOutputStream *stream,
255 GCancellable *cancellable,
258 GFilterOutputStream *filter_stream;
261 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
263 nwritten = g_output_stream_write (filter_stream->base_stream,
273 g_filter_output_stream_flush (GOutputStream *stream,
274 GCancellable *cancellable,
277 GFilterOutputStream *filter_stream;
280 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
282 res = g_output_stream_flush (filter_stream->base_stream,
290 g_filter_output_stream_close (GOutputStream *stream,
291 GCancellable *cancellable,
296 if (GET_PRIVATE (stream)->close_base)
298 GFilterOutputStream *filter_stream;
300 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
302 res = g_output_stream_close (filter_stream->base_stream,