1 /*
2 * Copyright © 2014 NICE s.r.l.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Ignacio Casal Quinteiro <ignacio.casal@nice-software.com>
18 */
19
20
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
24
25 #include "gsimpleiostream.h"
26 #include "gtask.h"
27
28 /**
29 * SECTION:gsimpleiostream
30 * @short_description: A wrapper around an input and an output stream.
31 * @include: gio/gio.h
32 * @see_also: #GIOStream
33 *
34 * GSimpleIOStream creates a #GIOStream from an arbitrary #GInputStream and
35 * #GOutputStream. This allows any pair of input and output streams to be used
36 * with #GIOStream methods.
37 *
38 * This is useful when you obtained a #GInputStream and a #GOutputStream
39 * by other means, for instance creating them with platform specific methods as
40 * g_unix_input_stream_new() or g_win32_input_stream_new(), and you want
41 * to take advantage of the methods provided by #GIOStream.
42 *
43 * Since: 2.44
44 */
45
46 /**
47 * GSimpleIOStream:
48 *
49 * A wrapper around a #GInputStream and a #GOutputStream.
50 *
51 * Since: 2.44
52 */
53 struct _GSimpleIOStream
54 {
55 GIOStream parent;
56
57 GInputStream *input_stream;
58 GOutputStream *output_stream;
59 };
60
61 struct _GSimpleIOStreamClass
62 {
63 GIOStreamClass parent;
64 };
65
66 typedef struct _GSimpleIOStreamClass GSimpleIOStreamClass;
67
68 enum
69 {
70 PROP_0,
71 PROP_INPUT_STREAM,
72 PROP_OUTPUT_STREAM
73 };
74
G_DEFINE_TYPE(GSimpleIOStream,g_simple_io_stream,G_TYPE_IO_STREAM)75 G_DEFINE_TYPE (GSimpleIOStream, g_simple_io_stream, G_TYPE_IO_STREAM)
76
77 static void
78 g_simple_io_stream_finalize (GObject *object)
79 {
80 GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
81
82 if (stream->input_stream)
83 g_object_unref (stream->input_stream);
84
85 if (stream->output_stream)
86 g_object_unref (stream->output_stream);
87
88 G_OBJECT_CLASS (g_simple_io_stream_parent_class)->finalize (object);
89 }
90
91 static void
g_simple_io_stream_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)92 g_simple_io_stream_set_property (GObject *object,
93 guint prop_id,
94 const GValue *value,
95 GParamSpec *pspec)
96 {
97 GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
98
99 switch (prop_id)
100 {
101 case PROP_INPUT_STREAM:
102 stream->input_stream = g_value_dup_object (value);
103 break;
104
105 case PROP_OUTPUT_STREAM:
106 stream->output_stream = g_value_dup_object (value);
107 break;
108
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 break;
112 }
113 }
114
115 static void
g_simple_io_stream_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)116 g_simple_io_stream_get_property (GObject *object,
117 guint prop_id,
118 GValue *value,
119 GParamSpec *pspec)
120 {
121 GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
122
123 switch (prop_id)
124 {
125 case PROP_INPUT_STREAM:
126 g_value_set_object (value, stream->input_stream);
127 break;
128
129 case PROP_OUTPUT_STREAM:
130 g_value_set_object (value, stream->output_stream);
131 break;
132
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 break;
136 }
137 }
138
139 static GInputStream *
g_simple_io_stream_get_input_stream(GIOStream * stream)140 g_simple_io_stream_get_input_stream (GIOStream *stream)
141 {
142 GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
143
144 return simple_stream->input_stream;
145 }
146
147 static GOutputStream *
g_simple_io_stream_get_output_stream(GIOStream * stream)148 g_simple_io_stream_get_output_stream (GIOStream *stream)
149 {
150 GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
151
152 return simple_stream->output_stream;
153 }
154
155 static void
g_simple_io_stream_class_init(GSimpleIOStreamClass * class)156 g_simple_io_stream_class_init (GSimpleIOStreamClass *class)
157 {
158 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
159 GIOStreamClass *io_class = G_IO_STREAM_CLASS (class);
160
161 gobject_class->finalize = g_simple_io_stream_finalize;
162 gobject_class->get_property = g_simple_io_stream_get_property;
163 gobject_class->set_property = g_simple_io_stream_set_property;
164
165 io_class->get_input_stream = g_simple_io_stream_get_input_stream;
166 io_class->get_output_stream = g_simple_io_stream_get_output_stream;
167
168 /**
169 * GSimpleIOStream:input-stream:
170 *
171 * Since: 2.44
172 */
173 g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
174 g_param_spec_object ("input-stream",
175 P_("Input stream"),
176 P_("The GInputStream to read from"),
177 G_TYPE_INPUT_STREAM,
178 G_PARAM_READWRITE |
179 G_PARAM_STATIC_STRINGS |
180 G_PARAM_CONSTRUCT_ONLY));
181
182 /**
183 * GSimpleIOStream:output-stream:
184 *
185 * Since: 2.44
186 */
187 g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
188 g_param_spec_object ("output-stream",
189 P_("Output stream"),
190 P_("The GOutputStream to write to"),
191 G_TYPE_OUTPUT_STREAM,
192 G_PARAM_READWRITE |
193 G_PARAM_STATIC_STRINGS |
194 G_PARAM_CONSTRUCT_ONLY));
195 }
196
197 static void
g_simple_io_stream_init(GSimpleIOStream * stream)198 g_simple_io_stream_init (GSimpleIOStream *stream)
199 {
200 }
201
202 /**
203 * g_simple_io_stream_new:
204 * @input_stream: a #GInputStream.
205 * @output_stream: a #GOutputStream.
206 *
207 * Creates a new #GSimpleIOStream wrapping @input_stream and @output_stream.
208 * See also #GIOStream.
209 *
210 * Returns: a new #GSimpleIOStream instance.
211 *
212 * Since: 2.44
213 */
214 GIOStream *
g_simple_io_stream_new(GInputStream * input_stream,GOutputStream * output_stream)215 g_simple_io_stream_new (GInputStream *input_stream,
216 GOutputStream *output_stream)
217 {
218 return g_object_new (G_TYPE_SIMPLE_IO_STREAM,
219 "input-stream", input_stream,
220 "output-stream", output_stream,
221 NULL);
222 }
223