Remove build warning
[platform/upstream/libsoup.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 "soup-io-stream.h"
13 #include "soup.h"
14 #include "soup-filter-input-stream.h"
15
16 struct _SoupIOStreamPrivate {
17         GIOStream *base_iostream;
18         gboolean close_on_dispose;
19
20         GInputStream *istream;
21         GOutputStream *ostream;
22         gboolean disposing;
23 };
24
25 enum {
26         PROP_0,
27
28         PROP_BASE_IOSTREAM,
29         PROP_CLOSE_ON_DISPOSE
30 };
31
32 G_DEFINE_TYPE (SoupIOStream, soup_io_stream, G_TYPE_IO_STREAM)
33
34 static void
35 soup_io_stream_init (SoupIOStream *stream)
36 {
37         stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
38                                                     SOUP_TYPE_IO_STREAM,
39                                                     SoupIOStreamPrivate);
40 }
41
42 static void
43 soup_io_stream_set_property (GObject *object, guint prop_id,
44                              const GValue *value, GParamSpec *pspec)
45 {
46         SoupIOStream *siostream = SOUP_IO_STREAM (object);
47         GIOStream *io;
48
49         switch (prop_id) {
50         case PROP_BASE_IOSTREAM:
51                 io = siostream->priv->base_iostream = g_value_dup_object (value);
52                 if (io) {
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));
57                 } else {
58                         g_clear_object (&siostream->priv->istream);
59                         g_clear_object (&siostream->priv->ostream);
60                 }
61                 break;
62         case PROP_CLOSE_ON_DISPOSE:
63                 siostream->priv->close_on_dispose = g_value_get_boolean (value);
64                 break;
65         default:
66                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
67                 break;
68         }
69 }
70
71 static void
72 soup_io_stream_get_property (GObject *object, guint prop_id,
73                              GValue *value, GParamSpec *pspec)
74 {
75         SoupIOStream *siostream = SOUP_IO_STREAM (object);
76
77         switch (prop_id) {
78         case PROP_BASE_IOSTREAM:
79                 g_value_set_object (value, siostream->priv->base_iostream);
80                 break;
81         case PROP_CLOSE_ON_DISPOSE:
82                 g_value_set_boolean (value, siostream->priv->close_on_dispose);
83                 break;
84         default:
85                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86                 break;
87         }
88 }
89
90 static void
91 soup_io_stream_dispose (GObject *object)
92 {
93         SoupIOStream *siostream = SOUP_IO_STREAM (object);
94
95         siostream->priv->disposing = TRUE;
96
97         G_OBJECT_CLASS (soup_io_stream_parent_class)->dispose (object);
98 }
99
100 static void
101 soup_io_stream_finalize (GObject *object)
102 {
103         SoupIOStream *siostream = SOUP_IO_STREAM (object);
104
105         g_clear_object (&siostream->priv->base_iostream);
106         g_clear_object (&siostream->priv->istream);
107         g_clear_object (&siostream->priv->ostream);
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 }