1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright 2012 Red Hat, Inc.
14 #include "soup-io-stream.h"
15 #include "soup-filter-input-stream.h"
17 struct _SoupIOStreamPrivate {
18 GIOStream *base_iostream;
19 gboolean close_on_dispose;
21 GInputStream *istream;
22 GOutputStream *ostream;
33 G_DEFINE_TYPE (SoupIOStream, soup_io_stream, G_TYPE_IO_STREAM)
36 soup_io_stream_init (SoupIOStream *stream)
38 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
44 soup_io_stream_set_property (GObject *object, guint prop_id,
45 const GValue *value, GParamSpec *pspec)
47 SoupIOStream *siostream = SOUP_IO_STREAM (object);
51 case PROP_BASE_IOSTREAM:
52 io = siostream->priv->base_iostream = g_value_dup_object (value);
54 siostream->priv->istream =
55 soup_filter_input_stream_new (g_io_stream_get_input_stream (io));
56 siostream->priv->ostream =
57 g_object_ref (g_io_stream_get_output_stream (io));
59 g_clear_object (&siostream->priv->istream);
60 g_clear_object (&siostream->priv->ostream);
63 case PROP_CLOSE_ON_DISPOSE:
64 siostream->priv->close_on_dispose = g_value_get_boolean (value);
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
73 soup_io_stream_get_property (GObject *object, guint prop_id,
74 GValue *value, GParamSpec *pspec)
76 SoupIOStream *siostream = SOUP_IO_STREAM (object);
79 case PROP_BASE_IOSTREAM:
80 g_value_set_object (value, siostream->priv->base_iostream);
82 case PROP_CLOSE_ON_DISPOSE:
83 g_value_set_boolean (value, siostream->priv->close_on_dispose);
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 soup_io_stream_dispose (GObject *object)
94 SoupIOStream *siostream = SOUP_IO_STREAM (object);
96 siostream->priv->disposing = TRUE;
98 G_OBJECT_CLASS (soup_io_stream_parent_class)->dispose (object);
102 soup_io_stream_finalize (GObject *object)
104 SoupIOStream *siostream = SOUP_IO_STREAM (object);
106 if (siostream->priv->base_iostream)
107 g_object_unref (siostream->priv->base_iostream);
109 G_OBJECT_CLASS (soup_io_stream_parent_class)->finalize (object);
112 static GInputStream *
113 soup_io_stream_get_input_stream (GIOStream *stream)
115 return SOUP_IO_STREAM (stream)->priv->istream;
118 static GOutputStream *
119 soup_io_stream_get_output_stream (GIOStream *stream)
121 return SOUP_IO_STREAM (stream)->priv->ostream;
126 soup_io_stream_close (GIOStream *stream,
127 GCancellable *cancellable,
130 SoupIOStream *siostream = SOUP_IO_STREAM (stream);
132 if (siostream->priv->disposing &&
133 !siostream->priv->close_on_dispose)
136 return g_io_stream_close (siostream->priv->base_iostream,
141 soup_io_stream_close_async (GIOStream *stream,
143 GCancellable *cancellable,
144 GAsyncReadyCallback callback,
147 g_io_stream_close_async (SOUP_IO_STREAM (stream)->priv->base_iostream,
148 io_priority, cancellable, callback, user_data);
152 soup_io_stream_close_finish (GIOStream *stream,
153 GAsyncResult *result,
156 return g_io_stream_close_finish (SOUP_IO_STREAM (stream)->priv->base_iostream,
161 soup_io_stream_class_init (SoupIOStreamClass *stream_class)
163 GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
164 GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (stream_class);
166 g_type_class_add_private (stream_class, sizeof (SoupIOStreamPrivate));
168 object_class->set_property = soup_io_stream_set_property;
169 object_class->get_property = soup_io_stream_get_property;
170 object_class->dispose = soup_io_stream_dispose;
171 object_class->finalize = soup_io_stream_finalize;
173 io_stream_class->get_input_stream = soup_io_stream_get_input_stream;
174 io_stream_class->get_output_stream = soup_io_stream_get_output_stream;
175 io_stream_class->close_fn = soup_io_stream_close;
176 io_stream_class->close_async = soup_io_stream_close_async;
177 io_stream_class->close_finish = soup_io_stream_close_finish;
179 g_object_class_install_property (
180 object_class, PROP_BASE_IOSTREAM,
181 g_param_spec_object ("base-iostream",
186 G_PARAM_CONSTRUCT_ONLY));
187 g_object_class_install_property (
188 object_class, PROP_CLOSE_ON_DISPOSE,
189 g_param_spec_boolean ("close-on-dispose",
191 "Close base GIOStream when closing",
194 G_PARAM_CONSTRUCT_ONLY));
198 soup_io_stream_new (GIOStream *base_iostream,
199 gboolean close_on_dispose)
201 return g_object_new (SOUP_TYPE_IO_STREAM,
202 "base-iostream", base_iostream,
203 "close-on-dispose", close_on_dispose,