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