1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.IOStream;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.InputStream;
30 private import gio.OutputStream;
31 private import glib.ErrorG;
32 private import glib.GException;
33 private import gobject.ObjectG;
34 private import gtkc.gio;
35 public  import gtkc.giotypes;
36 
37 
38 /**
39  * GIOStream represents an object that has both read and write streams.
40  * Generally the two streams acts as separate input and output streams,
41  * but they share some common resources and state. For instance, for
42  * seekable streams they may use the same position in both streams.
43  * 
44  * Examples of #GIOStream objects are #GSocketConnection which represents
45  * a two-way network connection, and #GFileIOStream which represent a
46  * file handle opened in read-write mode.
47  * 
48  * To do the actual reading and writing you need to get the substreams
49  * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
50  * 
51  * The #GIOStream object owns the input and the output streams, not the other
52  * way around, so keeping the substreams alive will not keep the #GIOStream
53  * object alive. If the #GIOStream object is freed it will be closed, thus
54  * closing the substream, so even if the substreams stay alive they will
55  * always just return a %G_IO_ERROR_CLOSED for all operations.
56  * 
57  * To close a stream use g_io_stream_close() which will close the common
58  * stream object and also the individual substreams. You can also close
59  * the substreams themselves. In most cases this only marks the
60  * substream as closed, so further I/O on it fails but common state in the
61  * #GIOStream may still be open. However, some streams may support
62  * "half-closed" states where one direction of the stream is actually shut down.
63  */
64 public class IOStream : ObjectG
65 {
66 	/** the main Gtk struct */
67 	protected GIOStream* gIOStream;
68 
69 	/** Get the main Gtk struct */
70 	public GIOStream* getIOStreamStruct()
71 	{
72 		return gIOStream;
73 	}
74 
75 	/** the main Gtk struct as a void* */
76 	protected override void* getStruct()
77 	{
78 		return cast(void*)gIOStream;
79 	}
80 
81 	protected override void setStruct(GObject* obj)
82 	{
83 		gIOStream = cast(GIOStream*)obj;
84 		super.setStruct(obj);
85 	}
86 
87 	/**
88 	 * Sets our main struct and passes it to the parent class.
89 	 */
90 	public this (GIOStream* gIOStream, bool ownedRef = false)
91 	{
92 		this.gIOStream = gIOStream;
93 		super(cast(GObject*)gIOStream, ownedRef);
94 	}
95 
96 
97 	/** */
98 	public static GType getType()
99 	{
100 		return g_io_stream_get_type();
101 	}
102 
103 	/**
104 	 * Finishes an asynchronous io stream splice operation.
105 	 *
106 	 * Params:
107 	 *     result = a #GAsyncResult.
108 	 *
109 	 * Return: %TRUE on success, %FALSE otherwise.
110 	 *
111 	 * Since: 2.28
112 	 *
113 	 * Throws: GException on failure.
114 	 */
115 	public static bool spliceFinish(AsyncResultIF result)
116 	{
117 		GError* err = null;
118 		
119 		auto p = g_io_stream_splice_finish((result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
120 		
121 		if (err !is null)
122 		{
123 			throw new GException( new ErrorG(err) );
124 		}
125 		
126 		return p;
127 	}
128 
129 	/**
130 	 * Clears the pending flag on @stream.
131 	 *
132 	 * Since: 2.22
133 	 */
134 	public void clearPending()
135 	{
136 		g_io_stream_clear_pending(gIOStream);
137 	}
138 
139 	/**
140 	 * Closes the stream, releasing resources related to it. This will also
141 	 * closes the individual input and output streams, if they are not already
142 	 * closed.
143 	 *
144 	 * Once the stream is closed, all other operations will return
145 	 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
146 	 * return an error.
147 	 *
148 	 * Closing a stream will automatically flush any outstanding buffers
149 	 * in the stream.
150 	 *
151 	 * Streams will be automatically closed when the last reference
152 	 * is dropped, but you might want to call this function to make sure
153 	 * resources are released as early as possible.
154 	 *
155 	 * Some streams might keep the backing store of the stream (e.g. a file
156 	 * descriptor) open after the stream is closed. See the documentation for
157 	 * the individual stream for details.
158 	 *
159 	 * On failure the first error that happened will be reported, but the
160 	 * close operation will finish as much as possible. A stream that failed
161 	 * to close will still return %G_IO_ERROR_CLOSED for all operations.
162 	 * Still, it is important to check and report the error to the user,
163 	 * otherwise there might be a loss of data as all data might not be written.
164 	 *
165 	 * If @cancellable is not NULL, then the operation can be cancelled by
166 	 * triggering the cancellable object from another thread. If the operation
167 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
168 	 * Cancelling a close will still leave the stream closed, but some streams
169 	 * can use a faster close that doesn't block to e.g. check errors.
170 	 *
171 	 * The default implementation of this method just calls close on the
172 	 * individual input/output streams.
173 	 *
174 	 * Params:
175 	 *     cancellable = optional #GCancellable object, %NULL to ignore
176 	 *
177 	 * Return: %TRUE on success, %FALSE on failure
178 	 *
179 	 * Since: 2.22
180 	 *
181 	 * Throws: GException on failure.
182 	 */
183 	public bool close(Cancellable cancellable)
184 	{
185 		GError* err = null;
186 		
187 		auto p = g_io_stream_close(gIOStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
188 		
189 		if (err !is null)
190 		{
191 			throw new GException( new ErrorG(err) );
192 		}
193 		
194 		return p;
195 	}
196 
197 	/**
198 	 * Requests an asynchronous close of the stream, releasing resources
199 	 * related to it. When the operation is finished @callback will be
200 	 * called. You can then call g_io_stream_close_finish() to get
201 	 * the result of the operation.
202 	 *
203 	 * For behaviour details see g_io_stream_close().
204 	 *
205 	 * The asynchronous methods have a default fallback that uses threads
206 	 * to implement asynchronicity, so they are optional for inheriting
207 	 * classes. However, if you override one you must override all.
208 	 *
209 	 * Params:
210 	 *     ioPriority = the io priority of the request
211 	 *     cancellable = optional cancellable object
212 	 *     callback = callback to call when the request is satisfied
213 	 *     userData = the data to pass to callback function
214 	 *
215 	 * Since: 2.22
216 	 */
217 	public void closeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
218 	{
219 		g_io_stream_close_async(gIOStream, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
220 	}
221 
222 	/**
223 	 * Closes a stream.
224 	 *
225 	 * Params:
226 	 *     result = a #GAsyncResult
227 	 *
228 	 * Return: %TRUE if stream was successfully closed, %FALSE otherwise.
229 	 *
230 	 * Since: 2.22
231 	 *
232 	 * Throws: GException on failure.
233 	 */
234 	public bool closeFinish(AsyncResultIF result)
235 	{
236 		GError* err = null;
237 		
238 		auto p = g_io_stream_close_finish(gIOStream, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
239 		
240 		if (err !is null)
241 		{
242 			throw new GException( new ErrorG(err) );
243 		}
244 		
245 		return p;
246 	}
247 
248 	/**
249 	 * Gets the input stream for this object. This is used
250 	 * for reading.
251 	 *
252 	 * Return: a #GInputStream, owned by the #GIOStream.
253 	 *     Do not free.
254 	 *
255 	 * Since: 2.22
256 	 */
257 	public InputStream getInputStream()
258 	{
259 		auto p = g_io_stream_get_input_stream(gIOStream);
260 		
261 		if(p is null)
262 		{
263 			return null;
264 		}
265 		
266 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) p);
267 	}
268 
269 	/**
270 	 * Gets the output stream for this object. This is used for
271 	 * writing.
272 	 *
273 	 * Return: a #GOutputStream, owned by the #GIOStream.
274 	 *     Do not free.
275 	 *
276 	 * Since: 2.22
277 	 */
278 	public OutputStream getOutputStream()
279 	{
280 		auto p = g_io_stream_get_output_stream(gIOStream);
281 		
282 		if(p is null)
283 		{
284 			return null;
285 		}
286 		
287 		return ObjectG.getDObject!(OutputStream)(cast(GOutputStream*) p);
288 	}
289 
290 	/**
291 	 * Checks if a stream has pending actions.
292 	 *
293 	 * Return: %TRUE if @stream has pending actions.
294 	 *
295 	 * Since: 2.22
296 	 */
297 	public bool hasPending()
298 	{
299 		return g_io_stream_has_pending(gIOStream) != 0;
300 	}
301 
302 	/**
303 	 * Checks if a stream is closed.
304 	 *
305 	 * Return: %TRUE if the stream is closed.
306 	 *
307 	 * Since: 2.22
308 	 */
309 	public bool isClosed()
310 	{
311 		return g_io_stream_is_closed(gIOStream) != 0;
312 	}
313 
314 	/**
315 	 * Sets @stream to have actions pending. If the pending flag is
316 	 * already set or @stream is closed, it will return %FALSE and set
317 	 * @error.
318 	 *
319 	 * Return: %TRUE if pending was previously unset and is now set.
320 	 *
321 	 * Since: 2.22
322 	 *
323 	 * Throws: GException on failure.
324 	 */
325 	public bool setPending()
326 	{
327 		GError* err = null;
328 		
329 		auto p = g_io_stream_set_pending(gIOStream, &err) != 0;
330 		
331 		if (err !is null)
332 		{
333 			throw new GException( new ErrorG(err) );
334 		}
335 		
336 		return p;
337 	}
338 
339 	/**
340 	 * Asyncronously splice the output stream of @stream1 to the input stream of
341 	 * @stream2, and splice the output stream of @stream2 to the input stream of
342 	 * @stream1.
343 	 *
344 	 * When the operation is finished @callback will be called.
345 	 * You can then call g_io_stream_splice_finish() to get the
346 	 * result of the operation.
347 	 *
348 	 * Params:
349 	 *     stream2 = a #GIOStream.
350 	 *     flags = a set of #GIOStreamSpliceFlags.
351 	 *     ioPriority = the io priority of the request.
352 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
353 	 *     callback = a #GAsyncReadyCallback.
354 	 *     userData = user data passed to @callback.
355 	 *
356 	 * Since: 2.28
357 	 */
358 	public void spliceAsync(IOStream stream2, GIOStreamSpliceFlags flags, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
359 	{
360 		g_io_stream_splice_async(gIOStream, (stream2 is null) ? null : stream2.getIOStreamStruct(), flags, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
361 	}
362 }