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  * Conversion parameters:
26  * inFile  = GCancellable.html
27  * outPack = gio
28  * outFile = Cancellable
29  * strct   = GCancellable
30  * realStrct=
31  * ctorStrct=
32  * clss    = Cancellable
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_cancellable_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.ErrorG
47  * 	- glib.GException
48  * 	- glib.Source
49  * structWrap:
50  * 	- GCancellable* -> Cancellable
51  * 	- GSource* -> Source
52  * module aliases:
53  * local aliases:
54  * overrides:
55  */
56 
57 module gio.Cancellable;
58 
59 public  import gtkc.giotypes;
60 
61 private import gtkc.gio;
62 private import glib.ConstructionException;
63 private import gobject.ObjectG;
64 
65 private import gobject.Signals;
66 public  import gtkc.gdktypes;
67 private import glib.ErrorG;
68 private import glib.GException;
69 private import glib.Source;
70 
71 
72 private import gobject.ObjectG;
73 
74 /**
75  * GCancellable is a thread-safe operation cancellation stack used
76  * throughout GIO to allow for cancellation of synchronous and
77  * asynchronous operations.
78  */
79 public class Cancellable : ObjectG
80 {
81 	
82 	/** the main Gtk struct */
83 	protected GCancellable* gCancellable;
84 	
85 	
86 	/** Get the main Gtk struct */
87 	public GCancellable* getCancellableStruct()
88 	{
89 		return gCancellable;
90 	}
91 	
92 	
93 	/** the main Gtk struct as a void* */
94 	protected override void* getStruct()
95 	{
96 		return cast(void*)gCancellable;
97 	}
98 	
99 	/**
100 	 * Sets our main struct and passes it to the parent class
101 	 */
102 	public this (GCancellable* gCancellable)
103 	{
104 		super(cast(GObject*)gCancellable);
105 		this.gCancellable = gCancellable;
106 	}
107 	
108 	protected override void setStruct(GObject* obj)
109 	{
110 		super.setStruct(obj);
111 		gCancellable = cast(GCancellable*)obj;
112 	}
113 	
114 	/**
115 	 */
116 	int[string] connectedSignals;
117 	
118 	void delegate(Cancellable)[] onCancelledListeners;
119 	/**
120 	 * Emitted when the operation has been cancelled.
121 	 * Can be used by implementations of cancellable operations. If the
122 	 * operation is cancelled from another thread, the signal will be
123 	 * emitted in the thread that cancelled the operation, not the
124 	 * thread that is running the operation.
125 	 * Note that disconnecting from this signal (or any signal) in a
126 	 * multi-threaded program is prone to race conditions. For instance
127 	 * it is possible that a signal handler may be invoked even
128 	 * after a call to
129 	 * g_signal_handler_disconnect() for that handler has already
130 	 * returned.
131 	 * There is also a problem when cancellation happen
132 	 * right before connecting to the signal. If this happens the
133 	 * signal will unexpectedly not be emitted, and checking before
134 	 * connecting to the signal leaves a race condition where this is
135 	 * still happening.
136 	 * In order to make it safe and easy to connect handlers there
137 	 * are two helper functions: g_cancellable_connect() and
138 	 * g_cancellable_disconnect() which protect against problems
139 	 * like this.
140 	 * $(DDOC_COMMENT example)
141 	 * Note that the cancelled signal is emitted in the thread that
142 	 * the user cancelled from, which may be the main thread. So, the
143 	 * cancellable signal should not do something that can block.
144 	 */
145 	void addOnCancelled(void delegate(Cancellable) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
146 	{
147 		if ( !("cancelled" in connectedSignals) )
148 		{
149 			Signals.connectData(
150 			getStruct(),
151 			"cancelled",
152 			cast(GCallback)&callBackCancelled,
153 			cast(void*)this,
154 			null,
155 			connectFlags);
156 			connectedSignals["cancelled"] = 1;
157 		}
158 		onCancelledListeners ~= dlg;
159 	}
160 	extern(C) static void callBackCancelled(GCancellable* cancellableStruct, Cancellable _cancellable)
161 	{
162 		foreach ( void delegate(Cancellable) dlg ; _cancellable.onCancelledListeners )
163 		{
164 			dlg(_cancellable);
165 		}
166 	}
167 	
168 	
169 	/**
170 	 * Creates a new GCancellable object.
171 	 * Applications that want to start one or more operations
172 	 * that should be cancellable should create a GCancellable
173 	 * and pass it to the operations.
174 	 * One GCancellable can be used in multiple consecutive
175 	 * operations or in multiple concurrent operations.
176 	 * Throws: ConstructionException GTK+ fails to create the object.
177 	 */
178 	public this ()
179 	{
180 		// GCancellable * g_cancellable_new (void);
181 		auto p = g_cancellable_new();
182 		if(p is null)
183 		{
184 			throw new ConstructionException("null returned by g_cancellable_new()");
185 		}
186 		this(cast(GCancellable*) p);
187 	}
188 	
189 	/**
190 	 * Checks if a cancellable job has been cancelled.
191 	 * Returns: TRUE if cancellable is cancelled, FALSE if called with NULL or if item is not cancelled.
192 	 */
193 	public int isCancelled()
194 	{
195 		// gboolean g_cancellable_is_cancelled (GCancellable *cancellable);
196 		return g_cancellable_is_cancelled(gCancellable);
197 	}
198 	
199 	/**
200 	 * If the cancellable is cancelled, sets the error to notify
201 	 * that the operation was cancelled.
202 	 * Returns: TRUE if cancellable was cancelled, FALSE if it was not
203 	 */
204 	public int setErrorIfCancelled()
205 	{
206 		// gboolean g_cancellable_set_error_if_cancelled  (GCancellable *cancellable,  GError **error);
207 		GError* err = null;
208 		
209 		auto p = g_cancellable_set_error_if_cancelled(gCancellable, &err);
210 		
211 		if (err !is null)
212 		{
213 			throw new GException( new ErrorG(err) );
214 		}
215 		
216 		return p;
217 	}
218 	
219 	/**
220 	 * Gets the file descriptor for a cancellable job. This can be used to
221 	 * implement cancellable operations on Unix systems. The returned fd will
222 	 * turn readable when cancellable is cancelled.
223 	 * You are not supposed to read from the fd yourself, just check for
224 	 * readable status. Reading to unset the readable status is done
225 	 * with g_cancellable_reset().
226 	 * After a successful return from this function, you should use
227 	 * g_cancellable_release_fd() to free up resources allocated for
228 	 * the returned file descriptor.
229 	 * See also g_cancellable_make_pollfd().
230 	 * Returns: A valid file descriptor. -1 if the file descriptor is not supported, or on errors.
231 	 */
232 	public int getFd()
233 	{
234 		// int g_cancellable_get_fd (GCancellable *cancellable);
235 		return g_cancellable_get_fd(gCancellable);
236 	}
237 	
238 	/**
239 	 * Creates a GPollFD corresponding to cancellable; this can be passed
240 	 * to g_poll() and used to poll for cancellation. This is useful both
241 	 * for unix systems without a native poll and for portability to
242 	 * windows.
243 	 * When this function returns TRUE, you should use
244 	 * g_cancellable_release_fd() to free up resources allocated for the
245 	 * pollfd. After a FALSE return, do not call g_cancellable_release_fd().
246 	 * If this function returns FALSE, either no cancellable was given or
247 	 * resource limits prevent this function from allocating the necessary
248 	 * structures for polling. (On Linux, you will likely have reached
249 	 * the maximum number of file descriptors.) The suggested way to handle
250 	 * these cases is to ignore the cancellable.
251 	 * You are not supposed to read from the fd yourself, just check for
252 	 * readable status. Reading to unset the readable status is done
253 	 * with g_cancellable_reset().
254 	 * Since 2.22
255 	 * Params:
256 	 * pollfd = a pointer to a GPollFD
257 	 * Returns: TRUE if pollfd was successfully initialized, FALSE on failure to prepare the cancellable.
258 	 */
259 	public int makePollfd(GPollFD* pollfd)
260 	{
261 		// gboolean g_cancellable_make_pollfd (GCancellable *cancellable,  GPollFD *pollfd);
262 		return g_cancellable_make_pollfd(gCancellable, pollfd);
263 	}
264 	
265 	/**
266 	 * Releases a resources previously allocated by g_cancellable_get_fd()
267 	 * or g_cancellable_make_pollfd().
268 	 * For compatibility reasons with older releases, calling this function
269 	 * is not strictly required, the resources will be automatically freed
270 	 * when the cancellable is finalized. However, the cancellable will
271 	 * block scarce file descriptors until it is finalized if this function
272 	 * is not called. This can cause the application to run out of file
273 	 * descriptors when many GCancellables are used at the same time.
274 	 * Since 2.22
275 	 */
276 	public void releaseFd()
277 	{
278 		// void g_cancellable_release_fd (GCancellable *cancellable);
279 		g_cancellable_release_fd(gCancellable);
280 	}
281 	
282 	/**
283 	 * Creates a source that triggers if cancellable is cancelled and
284 	 * calls its callback of type GCancellableSourceFunc. This is
285 	 * primarily useful for attaching to another (non-cancellable) source
286 	 * with g_source_add_child_source() to add cancellability to it.
287 	 * For convenience, you can call this with a NULL GCancellable,
288 	 * in which case the source will never trigger.
289 	 * Since 2.28
290 	 * Returns: the new GSource. [transfer full]
291 	 */
292 	public Source sourceNew()
293 	{
294 		// GSource * g_cancellable_source_new (GCancellable *cancellable);
295 		auto p = g_cancellable_source_new(gCancellable);
296 		
297 		if(p is null)
298 		{
299 			return null;
300 		}
301 		
302 		return ObjectG.getDObject!(Source)(cast(GSource*) p);
303 	}
304 	
305 	/**
306 	 * Gets the top cancellable from the stack.
307 	 * Returns: a GCancellable from the top of the stack, or NULL if the stack is empty. [transfer none]
308 	 */
309 	public static Cancellable getCurrent()
310 	{
311 		// GCancellable * g_cancellable_get_current (void);
312 		auto p = g_cancellable_get_current();
313 		
314 		if(p is null)
315 		{
316 			return null;
317 		}
318 		
319 		return ObjectG.getDObject!(Cancellable)(cast(GCancellable*) p);
320 	}
321 	
322 	/**
323 	 * Pops cancellable off the cancellable stack (verifying that cancellable
324 	 * is on the top of the stack).
325 	 */
326 	public void popCurrent()
327 	{
328 		// void g_cancellable_pop_current (GCancellable *cancellable);
329 		g_cancellable_pop_current(gCancellable);
330 	}
331 	
332 	/**
333 	 * Pushes cancellable onto the cancellable stack. The current
334 	 * cancellable can then be received using g_cancellable_get_current().
335 	 * This is useful when implementing cancellable operations in
336 	 * code that does not allow you to pass down the cancellable object.
337 	 * This is typically called automatically by e.g. GFile operations,
338 	 * so you rarely have to call this yourself.
339 	 */
340 	public void pushCurrent()
341 	{
342 		// void g_cancellable_push_current (GCancellable *cancellable);
343 		g_cancellable_push_current(gCancellable);
344 	}
345 	
346 	/**
347 	 * Resets cancellable to its uncancelled state.
348 	 * If cancellable is currently in use by any cancellable operation
349 	 * then the behavior of this function is undefined.
350 	 */
351 	public void reset()
352 	{
353 		// void g_cancellable_reset (GCancellable *cancellable);
354 		g_cancellable_reset(gCancellable);
355 	}
356 	
357 	/**
358 	 * Convenience function to connect to the "cancelled"
359 	 * signal. Also handles the race condition that may happen
360 	 * if the cancellable is cancelled right before connecting.
361 	 * callback is called at most once, either directly at the
362 	 * time of the connect if cancellable is already cancelled,
363 	 * or when cancellable is cancelled in some thread.
364 	 * data_destroy_func will be called when the handler is
365 	 * disconnected, or immediately if the cancellable is already
366 	 * cancelled.
367 	 * See "cancelled" for details on how to use this.
368 	 * Since 2.22
369 	 * Params:
370 	 * callback = The GCallback to connect.
371 	 * data = Data to pass to callback.
372 	 * dataDestroyFunc = Free function for data or NULL. [allow-none]
373 	 * Returns: The id of the signal handler or 0 if cancellable has already been cancelled.
374 	 */
375 	public gulong connect(GCallback callback, void* data, GDestroyNotify dataDestroyFunc)
376 	{
377 		// gulong g_cancellable_connect (GCancellable *cancellable,  GCallback callback,  gpointer data,  GDestroyNotify data_destroy_func);
378 		return g_cancellable_connect(gCancellable, callback, data, dataDestroyFunc);
379 	}
380 	
381 	/**
382 	 * Disconnects a handler from a cancellable instance similar to
383 	 * g_signal_handler_disconnect(). Additionally, in the event that a
384 	 * signal handler is currently running, this call will block until the
385 	 * handler has finished. Calling this function from a
386 	 * "cancelled" signal handler will therefore result in a
387 	 * deadlock.
388 	 * This avoids a race condition where a thread cancels at the
389 	 * same time as the cancellable operation is finished and the
390 	 * signal handler is removed. See "cancelled" for
391 	 * details on how to use this.
392 	 * If cancellable is NULL or handler_id is 0 this function does
393 	 * nothing.
394 	 * Since 2.22
395 	 * Params:
396 	 * handlerId = Handler id of the handler to be disconnected, or 0.
397 	 */
398 	public void disconnect(gulong handlerId)
399 	{
400 		// void g_cancellable_disconnect (GCancellable *cancellable,  gulong handler_id);
401 		g_cancellable_disconnect(gCancellable, handlerId);
402 	}
403 	
404 	/**
405 	 * Will set cancellable to cancelled, and will emit the
406 	 * "cancelled" signal. (However, see the warning about
407 	 * race conditions in the documentation for that signal if you are
408 	 * planning to connect to it.)
409 	 * This function is thread-safe. In other words, you can safely call
410 	 * it from a thread other than the one running the operation that was
411 	 * passed the cancellable.
412 	 * The convention within gio is that cancelling an asynchronous
413 	 * operation causes it to complete asynchronously. That is, if you
414 	 * cancel the operation from the same thread in which it is running,
415 	 * then the operation's GAsyncReadyCallback will not be invoked until
416 	 * the application returns to the main loop.
417 	 */
418 	public void cancel()
419 	{
420 		// void g_cancellable_cancel (GCancellable *cancellable);
421 		g_cancellable_cancel(gCancellable);
422 	}
423 }