Reapplying patch to disable attempts to use gtk-doc
[profile/ivi/libsoup2.4.git] / libsoup / soup-io-stream.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-io-stream.c
4  *
5  * Copyright 2012 Red Hat, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <gio/gio.h>
13
14 #include "soup-io-stream.h"
15 #include "soup-filter-input-stream.h"
16
17 struct _SoupIOStreamPrivate {
18         GIOStream *base_iostream;
19         gboolean close_on_dispose;
20
21         GInputStream *istream;
22         GOutputStream *ostream;
23         gboolean disposing;
24 };
25
26 enum {
27         PROP_0,
28
29         PROP_BASE_IOSTREAM,
30         PROP_CLOSE_ON_DISPOSE
31 };
32
33 G_DEFINE_TYPE (SoupIOStream, soup_io_stream, G_TYPE_IO_STREAM)
34
35 static void
36 soup_io_stream_init (SoupIOStream *stream)
37 {
38         stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
39                                                     SOUP_TYPE_IO_STREAM,
40                                                     SoupIOStreamPrivate);
41 }
42
43 static void
44 soup_io_stream_set_property (GObject *object, guint prop_id,
45                              const GValue *value, GParamSpec *pspec)
46 {
47         SoupIOStream *siostream = SOUP_IO_STREAM (object);
48         GIOStream *io;
49
50         switch (prop_id) {
51         case PROP_BASE_IOSTREAM:
52                 io = siostream->priv->base_iostream = g_value_dup_object (value);
53                 if (io) {
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));
58                 } else {
59                         g_clear_object (&siostream->priv->istream);
60                         g_clear_object (&siostream->priv->ostream);
61                 }
62                 break;
63         case PROP_CLOSE_ON_DISPOSE:
64                 siostream->priv->close_on_dispose = g_value_get_boolean (value);
65                 break;
66         default:
67                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
68                 break;
69         }
70 }
71
72 static void
73 soup_io_stream_get_property (GObject *object, guint prop_id,
74                              GValue *value, GParamSpec *pspec)
75 {
76         SoupIOStream *siostream = SOUP_IO_STREAM (object);
77
78         switch (prop_id) {
79         case PROP_BASE_IOSTREAM:
80                 g_value_set_object (value, siostream->priv->base_iostream);
81                 break;
82         case PROP_CLOSE_ON_DISPOSE:
83                 g_value_set_boolean (value, siostream->priv->close_on_dispose);
84                 break;
85         default:
86                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87                 break;
88         }
89 }
90
91 static void
92 soup_io_stream_dispose (GObject *object)
93 {
94         SoupIOStream *siostream = SOUP_IO_STREAM (object);
95
96         siostream->priv->disposing = TRUE;
97
98         G_OBJECT_CLASS (soup_io_stream_parent_class)->dispose (object);
99 }
100
101 static void
102 soup_io_stream_finalize (GObject *object)
103 {
104         SoupIOStream *siostream = SOUP_IO_STREAM (object);
105
106         if (siostream->priv->base_iostream)
107                 g_object_unref (siostream->priv->base_iostream);
108
109         G_OBJECT_CLASS (soup_io_stream_parent_class)->finalize (object);
110 }
111
112 static GInputStream *
113 soup_io_stream_get_input_stream (GIOStream *stream)
114 {
115         return SOUP_IO_STREAM (stream)->priv->istream;
116 }
117
118 static GOutputStream *
119 soup_io_stream_get_output_stream (GIOStream *stream)
120 {
121         return SOUP_IO_STREAM (stream)->priv->ostream;
122 }
123
124
125 static gboolean
126 soup_io_stream_close (GIOStream     *stream,
127                       GCancellable  *cancellable,
128                       GError       **error)
129 {
130         SoupIOStream *siostream = SOUP_IO_STREAM (stream);
131
132         if (siostream->priv->disposing &&
133             !siostream->priv->close_on_dispose)
134                 return TRUE;
135
136         return g_io_stream_close (siostream->priv->base_iostream,
137                                   cancellable, error);
138 }
139
140 static void    
141 soup_io_stream_close_async (GIOStream           *stream,
142                             int                  io_priority,
143                             GCancellable        *cancellable,
144                             GAsyncReadyCallback  callback,
145                             gpointer             user_data)
146 {
147         g_io_stream_close_async (SOUP_IO_STREAM (stream)->priv->base_iostream,
148                                  io_priority, cancellable, callback, user_data);
149 }
150
151 static gboolean
152 soup_io_stream_close_finish (GIOStream     *stream,
153                              GAsyncResult  *result,
154                              GError       **error)
155 {
156         return g_io_stream_close_finish (SOUP_IO_STREAM (stream)->priv->base_iostream,
157                                          result, error);
158 }
159
160 static void
161 soup_io_stream_class_init (SoupIOStreamClass *stream_class)
162 {
163         GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
164         GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (stream_class);
165
166         g_type_class_add_private (stream_class, sizeof (SoupIOStreamPrivate));
167
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;
172
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;
178
179         g_object_class_install_property (
180                 object_class, PROP_BASE_IOSTREAM,
181                 g_param_spec_object ("base-iostream",
182                                      "Base IOStream",
183                                      "Base GIOStream",
184                                      G_TYPE_IO_STREAM,
185                                      G_PARAM_READWRITE |
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",
190                                       "Close base stream",
191                                       "Close base GIOStream when closing",
192                                       TRUE,
193                                       G_PARAM_READWRITE |
194                                       G_PARAM_CONSTRUCT_ONLY));
195 }
196
197 GIOStream *
198 soup_io_stream_new (GIOStream *base_iostream,
199                     gboolean   close_on_dispose)
200 {
201         return g_object_new (SOUP_TYPE_IO_STREAM,
202                              "base-iostream", base_iostream,
203                              "close-on-dispose", close_on_dispose,
204                              NULL);
205 }