1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright 2012 Red Hat, Inc.
12 #include "soup-io-stream.h"
14 #include "soup-filter-input-stream.h"
16 struct _SoupIOStreamPrivate {
17 GIOStream *base_iostream;
18 gboolean close_on_dispose;
20 GInputStream *istream;
21 GOutputStream *ostream;
32 G_DEFINE_TYPE (SoupIOStream, soup_io_stream, G_TYPE_IO_STREAM)
35 soup_io_stream_init (SoupIOStream *stream)
37 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
43 soup_io_stream_set_property (GObject *object, guint prop_id,
44 const GValue *value, GParamSpec *pspec)
46 SoupIOStream *siostream = SOUP_IO_STREAM (object);
50 case PROP_BASE_IOSTREAM:
51 io = siostream->priv->base_iostream = g_value_dup_object (value);
53 siostream->priv->istream =
54 soup_filter_input_stream_new (g_io_stream_get_input_stream (io));
55 siostream->priv->ostream =
56 g_object_ref (g_io_stream_get_output_stream (io));
58 g_clear_object (&siostream->priv->istream);
59 g_clear_object (&siostream->priv->ostream);
62 case PROP_CLOSE_ON_DISPOSE:
63 siostream->priv->close_on_dispose = g_value_get_boolean (value);
66 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72 soup_io_stream_get_property (GObject *object, guint prop_id,
73 GValue *value, GParamSpec *pspec)
75 SoupIOStream *siostream = SOUP_IO_STREAM (object);
78 case PROP_BASE_IOSTREAM:
79 g_value_set_object (value, siostream->priv->base_iostream);
81 case PROP_CLOSE_ON_DISPOSE:
82 g_value_set_boolean (value, siostream->priv->close_on_dispose);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91 soup_io_stream_dispose (GObject *object)
93 SoupIOStream *siostream = SOUP_IO_STREAM (object);
95 siostream->priv->disposing = TRUE;
97 G_OBJECT_CLASS (soup_io_stream_parent_class)->dispose (object);
101 soup_io_stream_finalize (GObject *object)
103 SoupIOStream *siostream = SOUP_IO_STREAM (object);
105 g_clear_object (&siostream->priv->base_iostream);
106 g_clear_object (&siostream->priv->istream);
107 g_clear_object (&siostream->priv->ostream);
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,