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 
99 	public static GType getType()
100 	{
101 		return g_io_stream_get_type();
102 	}
103 
104 	/**
105 	 * Finishes an asynchronous io stream splice operation.
106 	 *
107 	 * Params:
108 	 *     result = a #GAsyncResult.
109 	 *
110 	 * Return: %TRUE on success, %FALSE otherwise.
111 	 *
112 	 * Since: 2.28
113 	 *
114 	 * Throws: GException on failure.
115 	 */
116 	public static bool spliceFinish(AsyncResultIF result)
117 	{
118 		GError* err = null;
119 		
120 		auto p = g_io_stream_splice_finish((result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
121 		
122 		if (err !is null)
123 		{
124 			throw new GException( new ErrorG(err) );
125 		}
126 		
127 		return p;
128 	}
129 
130 	/**
131 	 * Clears the pending flag on @stream.
132 	 *
133 	 * Since: 2.22
134 	 */
135 	public void clearPending()
136 	{
137 		g_io_stream_clear_pending(gIOStream);
138 	}
139 
140 	/**
141 	 * Closes the stream, releasing resources related to it. This will also
142 	 * closes the individual input and output streams, if they are not already
143 	 * closed.
144 	 *
145 	 * Once the stream is closed, all other operations will return
146 	 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
147 	 * return an error.
148 	 *
149 	 * Closing a stream will automatically flush any outstanding buffers
150 	 * in the stream.
151 	 *
152 	 * Streams will be automatically closed when the last reference
153 	 * is dropped, but you might want to call this function to make sure
154 	 * resources are released as early as possible.
155 	 *
156 	 * Some streams might keep the backing store of the stream (e.g. a file
157 	 * descriptor) open after the stream is closed. See the documentation for
158 	 * the individual stream for details.
159 	 *
160 	 * On failure the first error that happened will be reported, but the
161 	 * close operation will finish as much as possible. A stream that failed
162 	 * to close will still return %G_IO_ERROR_CLOSED for all operations.
163 	 * Still, it is important to check and report the error to the user,
164 	 * otherwise there might be a loss of data as all data might not be written.
165 	 *
166 	 * If @cancellable is not NULL, then the operation can be cancelled by
167 	 * triggering the cancellable object from another thread. If the operation
168 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
169 	 * Cancelling a close will still leave the stream closed, but some streams
170 	 * can use a faster close that doesn't block to e.g. check errors.
171 	 *
172 	 * The default implementation of this method just calls close on the
173 	 * individual input/output streams.
174 	 *
175 	 * Params:
176 	 *     cancellable = optional #GCancellable object, %NULL to ignore
177 	 *
178 	 * Return: %TRUE on success, %FALSE on failure
179 	 *
180 	 * Since: 2.22
181 	 *
182 	 * Throws: GException on failure.
183 	 */
184 	public bool close(Cancellable cancellable)
185 	{
186 		GError* err = null;
187 		
188 		auto p = g_io_stream_close(gIOStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
189 		
190 		if (err !is null)
191 		{
192 			throw new GException( new ErrorG(err) );
193 		}
194 		
195 		return p;
196 	}
197 
198 	/**
199 	 * Requests an asynchronous close of the stream, releasing resources
200 	 * related to it. When the operation is finished @callback will be
201 	 * called. You can then call g_io_stream_close_finish() to get
202 	 * the result of the operation.
203 	 *
204 	 * For behaviour details see g_io_stream_close().
205 	 *
206 	 * The asynchronous methods have a default fallback that uses threads
207 	 * to implement asynchronicity, so they are optional for inheriting
208 	 * classes. However, if you override one you must override all.
209 	 *
210 	 * Params:
211 	 *     ioPriority = the io priority of the request
212 	 *     cancellable = optional cancellable object
213 	 *     callback = callback to call when the request is satisfied
214 	 *     userData = the data to pass to callback function
215 	 *
216 	 * Since: 2.22
217 	 */
218 	public void closeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
219 	{
220 		g_io_stream_close_async(gIOStream, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
221 	}
222 
223 	/**
224 	 * Closes a stream.
225 	 *
226 	 * Params:
227 	 *     result = a #GAsyncResult
228 	 *
229 	 * Return: %TRUE if stream was successfully closed, %FALSE otherwise.
230 	 *
231 	 * Since: 2.22
232 	 *
233 	 * Throws: GException on failure.
234 	 */
235 	public bool closeFinish(AsyncResultIF result)
236 	{
237 		GError* err = null;
238 		
239 		auto p = g_io_stream_close_finish(gIOStream, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
240 		
241 		if (err !is null)
242 		{
243 			throw new GException( new ErrorG(err) );
244 		}
245 		
246 		return p;
247 	}
248 
249 	/**
250 	 * Gets the input stream for this object. This is used
251 	 * for reading.
252 	 *
253 	 * Return: a #GInputStream, owned by the #GIOStream.
254 	 *     Do not free.
255 	 *
256 	 * Since: 2.22
257 	 */
258 	public InputStream getInputStream()
259 	{
260 		auto p = g_io_stream_get_input_stream(gIOStream);
261 		
262 		if(p is null)
263 		{
264 			return null;
265 		}
266 		
267 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) p);
268 	}
269 
270 	/**
271 	 * Gets the output stream for this object. This is used for
272 	 * writing.
273 	 *
274 	 * Return: a #GOutputStream, owned by the #GIOStream.
275 	 *     Do not free.
276 	 *
277 	 * Since: 2.22
278 	 */
279 	public OutputStream getOutputStream()
280 	{
281 		auto p = g_io_stream_get_output_stream(gIOStream);
282 		
283 		if(p is null)
284 		{
285 			return null;
286 		}
287 		
288 		return ObjectG.getDObject!(OutputStream)(cast(GOutputStream*) p);
289 	}
290 
291 	/**
292 	 * Checks if a stream has pending actions.
293 	 *
294 	 * Return: %TRUE if @stream has pending actions.
295 	 *
296 	 * Since: 2.22
297 	 */
298 	public bool hasPending()
299 	{
300 		return g_io_stream_has_pending(gIOStream) != 0;
301 	}
302 
303 	/**
304 	 * Checks if a stream is closed.
305 	 *
306 	 * Return: %TRUE if the stream is closed.
307 	 *
308 	 * Since: 2.22
309 	 */
310 	public bool isClosed()
311 	{
312 		return g_io_stream_is_closed(gIOStream) != 0;
313 	}
314 
315 	/**
316 	 * Sets @stream to have actions pending. If the pending flag is
317 	 * already set or @stream is closed, it will return %FALSE and set
318 	 * @error.
319 	 *
320 	 * Return: %TRUE if pending was previously unset and is now set.
321 	 *
322 	 * Since: 2.22
323 	 *
324 	 * Throws: GException on failure.
325 	 */
326 	public bool setPending()
327 	{
328 		GError* err = null;
329 		
330 		auto p = g_io_stream_set_pending(gIOStream, &err) != 0;
331 		
332 		if (err !is null)
333 		{
334 			throw new GException( new ErrorG(err) );
335 		}
336 		
337 		return p;
338 	}
339 
340 	/**
341 	 * Asyncronously splice the output stream of @stream1 to the input stream of
342 	 * @stream2, and splice the output stream of @stream2 to the input stream of
343 	 * @stream1.
344 	 *
345 	 * When the operation is finished @callback will be called.
346 	 * You can then call g_io_stream_splice_finish() to get the
347 	 * result of the operation.
348 	 *
349 	 * Params:
350 	 *     stream2 = a #GIOStream.
351 	 *     flags = a set of #GIOStreamSpliceFlags.
352 	 *     ioPriority = the io priority of the request.
353 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
354 	 *     callback = a #GAsyncReadyCallback.
355 	 *     userData = user data passed to @callback.
356 	 *
357 	 * Since: 2.28
358 	 */
359 	public void spliceAsync(IOStream stream2, GIOStreamSpliceFlags flags, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
360 	{
361 		g_io_stream_splice_async(gIOStream, (stream2 is null) ? null : stream2.getIOStreamStruct(), flags, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
362 	}
363 }