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.SimpleAsyncResult;
26 
27 private import gio.AsyncResultIF;
28 private import gio.AsyncResultT;
29 private import gio.Cancellable;
30 private import gio.c.functions;
31 public  import gio.c.types;
32 private import glib.ConstructionException;
33 private import glib.ErrorG;
34 private import glib.GException;
35 private import glib.Str;
36 private import gobject.ObjectG;
37 
38 
39 /**
40  * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
41  * #GTask, which provides a simpler API.
42  * 
43  * #GSimpleAsyncResult implements #GAsyncResult.
44  * 
45  * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
46  * reporting, operation cancellation and the final state of an operation,
47  * completely transparent to the application. Results can be returned
48  * as a pointer e.g. for functions that return data that is collected
49  * asynchronously, a boolean value for checking the success or failure
50  * of an operation, or a #gssize for operations which return the number
51  * of bytes modified by the operation; all of the simple return cases
52  * are covered.
53  * 
54  * Most of the time, an application will not need to know of the details
55  * of this API; it is handled transparently, and any necessary operations
56  * are handled by #GAsyncResult's interface. However, if implementing a
57  * new GIO module, for writing language bindings, or for complex
58  * applications that need better control of how asynchronous operations
59  * are completed, it is important to understand this functionality.
60  * 
61  * GSimpleAsyncResults are tagged with the calling function to ensure
62  * that asynchronous functions and their finishing functions are used
63  * together correctly.
64  * 
65  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
66  * If the result needs to be created for a #GError, use
67  * g_simple_async_result_new_from_error() or
68  * g_simple_async_result_new_take_error(). If a #GError is not available
69  * (e.g. the asynchronous operation's doesn't take a #GError argument),
70  * but the result still needs to be created for an error condition, use
71  * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
72  * if your application or binding requires passing a variable argument list
73  * directly), and the error can then be propagated through the use of
74  * g_simple_async_result_propagate_error().
75  * 
76  * An asynchronous operation can be made to ignore a cancellation event by
77  * calling g_simple_async_result_set_handle_cancellation() with a
78  * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
79  * operations that are dangerous to cancel, such as close (which would
80  * cause a leak if cancelled before being run).
81  * 
82  * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
83  * or it can use #GThreads.
84  * g_simple_async_result_complete() will finish an I/O task directly
85  * from the point where it is called. g_simple_async_result_complete_in_idle()
86  * will finish it from an idle handler in the
87  * [thread-default main context][g-main-context-push-thread-default]
88  * where the #GSimpleAsyncResult was created.
89  * g_simple_async_result_run_in_thread() will run the job in a
90  * separate thread and then use
91  * g_simple_async_result_complete_in_idle() to deliver the result.
92  * 
93  * To set the results of an asynchronous function,
94  * g_simple_async_result_set_op_res_gpointer(),
95  * g_simple_async_result_set_op_res_gboolean(), and
96  * g_simple_async_result_set_op_res_gssize()
97  * are provided, setting the operation's result to a gpointer, gboolean, or
98  * gssize, respectively.
99  * 
100  * Likewise, to get the result of an asynchronous function,
101  * g_simple_async_result_get_op_res_gpointer(),
102  * g_simple_async_result_get_op_res_gboolean(), and
103  * g_simple_async_result_get_op_res_gssize() are
104  * provided, getting the operation's result as a gpointer, gboolean, and
105  * gssize, respectively.
106  * 
107  * For the details of the requirements implementations must respect, see
108  * #GAsyncResult.  A typical implementation of an asynchronous operation
109  * using GSimpleAsyncResult looks something like this:
110  * 
111  * |[<!-- language="C" -->
112  * static void
113  * baked_cb (Cake    *cake,
114  * gpointer user_data)
115  * {
116  * // In this example, this callback is not given a reference to the cake,
117  * // so the GSimpleAsyncResult has to take a reference to it.
118  * GSimpleAsyncResult *result = user_data;
119  * 
120  * if (cake == NULL)
121  * g_simple_async_result_set_error (result,
122  * BAKER_ERRORS,
123  * BAKER_ERROR_NO_FLOUR,
124  * "Go to the supermarket");
125  * else
126  * g_simple_async_result_set_op_res_gpointer (result,
127  * g_object_ref (cake),
128  * g_object_unref);
129  * 
130  * 
131  * // In this example, we assume that baked_cb is called as a callback from
132  * // the mainloop, so it's safe to complete the operation synchronously here.
133  * // If, however, _baker_prepare_cake () might call its callback without
134  * // first returning to the mainloop — inadvisable, but some APIs do so —
135  * // we would need to use g_simple_async_result_complete_in_idle().
136  * g_simple_async_result_complete (result);
137  * g_object_unref (result);
138  * }
139  * 
140  * void
141  * baker_bake_cake_async (Baker              *self,
142  * guint               radius,
143  * GAsyncReadyCallback callback,
144  * gpointer            user_data)
145  * {
146  * GSimpleAsyncResult *simple;
147  * Cake               *cake;
148  * 
149  * if (radius < 3)
150  * {
151  * g_simple_async_report_error_in_idle (G_OBJECT (self),
152  * callback,
153  * user_data,
154  * BAKER_ERRORS,
155  * BAKER_ERROR_TOO_SMALL,
156  * "%ucm radius cakes are silly",
157  * radius);
158  * return;
159  * }
160  * 
161  * simple = g_simple_async_result_new (G_OBJECT (self),
162  * callback,
163  * user_data,
164  * baker_bake_cake_async);
165  * cake = _baker_get_cached_cake (self, radius);
166  * 
167  * if (cake != NULL)
168  * {
169  * g_simple_async_result_set_op_res_gpointer (simple,
170  * g_object_ref (cake),
171  * g_object_unref);
172  * g_simple_async_result_complete_in_idle (simple);
173  * g_object_unref (simple);
174  * // Drop the reference returned by _baker_get_cached_cake();
175  * // the GSimpleAsyncResult has taken its own reference.
176  * g_object_unref (cake);
177  * return;
178  * }
179  * 
180  * _baker_prepare_cake (self, radius, baked_cb, simple);
181  * }
182  * 
183  * Cake *
184  * baker_bake_cake_finish (Baker        *self,
185  * GAsyncResult *result,
186  * GError      **error)
187  * {
188  * GSimpleAsyncResult *simple;
189  * Cake               *cake;
190  * 
191  * g_return_val_if_fail (g_simple_async_result_is_valid (result,
192  * G_OBJECT (self),
193  * baker_bake_cake_async),
194  * NULL);
195  * 
196  * simple = (GSimpleAsyncResult *) result;
197  * 
198  * if (g_simple_async_result_propagate_error (simple, error))
199  * return NULL;
200  * 
201  * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
202  * return g_object_ref (cake);
203  * }
204  * ]|
205  */
206 public class SimpleAsyncResult : ObjectG, AsyncResultIF
207 {
208 	/** the main Gtk struct */
209 	protected GSimpleAsyncResult* gSimpleAsyncResult;
210 
211 	/** Get the main Gtk struct */
212 	public GSimpleAsyncResult* getSimpleAsyncResultStruct(bool transferOwnership = false)
213 	{
214 		if (transferOwnership)
215 			ownedRef = false;
216 		return gSimpleAsyncResult;
217 	}
218 
219 	/** the main Gtk struct as a void* */
220 	protected override void* getStruct()
221 	{
222 		return cast(void*)gSimpleAsyncResult;
223 	}
224 
225 	/**
226 	 * Sets our main struct and passes it to the parent class.
227 	 */
228 	public this (GSimpleAsyncResult* gSimpleAsyncResult, bool ownedRef = false)
229 	{
230 		this.gSimpleAsyncResult = gSimpleAsyncResult;
231 		super(cast(GObject*)gSimpleAsyncResult, ownedRef);
232 	}
233 
234 	// add the AsyncResult capabilities
235 	mixin AsyncResultT!(GSimpleAsyncResult);
236 
237 
238 	/** */
239 	public static GType getType()
240 	{
241 		return g_simple_async_result_get_type();
242 	}
243 
244 	/**
245 	 * Creates a #GSimpleAsyncResult.
246 	 *
247 	 * The common convention is to create the #GSimpleAsyncResult in the
248 	 * function that starts the asynchronous operation and use that same
249 	 * function as the @source_tag.
250 	 *
251 	 * If your operation supports cancellation with #GCancellable (which it
252 	 * probably should) then you should provide the user's cancellable to
253 	 * g_simple_async_result_set_check_cancellable() immediately after
254 	 * this function returns.
255 	 *
256 	 * Deprecated: Use g_task_new() instead.
257 	 *
258 	 * Params:
259 	 *     sourceObject = a #GObject, or %NULL.
260 	 *     callback = a #GAsyncReadyCallback.
261 	 *     userData = user data passed to @callback.
262 	 *     sourceTag = the asynchronous function.
263 	 *
264 	 * Returns: a #GSimpleAsyncResult.
265 	 *
266 	 * Throws: ConstructionException GTK+ fails to create the object.
267 	 */
268 	public this(ObjectG sourceObject, GAsyncReadyCallback callback, void* userData, void* sourceTag)
269 	{
270 		auto __p = g_simple_async_result_new((sourceObject is null) ? null : sourceObject.getObjectGStruct(), callback, userData, sourceTag);
271 
272 		if(__p is null)
273 		{
274 			throw new ConstructionException("null returned by new");
275 		}
276 
277 		this(cast(GSimpleAsyncResult*) __p, true);
278 	}
279 
280 	/**
281 	 * Creates a #GSimpleAsyncResult from an error condition.
282 	 *
283 	 * Deprecated: Use g_task_new() and g_task_return_error() instead.
284 	 *
285 	 * Params:
286 	 *     sourceObject = a #GObject, or %NULL.
287 	 *     callback = a #GAsyncReadyCallback.
288 	 *     userData = user data passed to @callback.
289 	 *     error = a #GError
290 	 *
291 	 * Returns: a #GSimpleAsyncResult.
292 	 *
293 	 * Throws: ConstructionException GTK+ fails to create the object.
294 	 */
295 	public this(ObjectG sourceObject, GAsyncReadyCallback callback, void* userData, ErrorG error)
296 	{
297 		auto __p = g_simple_async_result_new_from_error((sourceObject is null) ? null : sourceObject.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct());
298 
299 		if(__p is null)
300 		{
301 			throw new ConstructionException("null returned by new_from_error");
302 		}
303 
304 		this(cast(GSimpleAsyncResult*) __p, true);
305 	}
306 
307 	/**
308 	 * Ensures that the data passed to the _finish function of an async
309 	 * operation is consistent.  Three checks are performed.
310 	 *
311 	 * First, @result is checked to ensure that it is really a
312 	 * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
313 	 * matches the source object of @result.  Third, @source_tag is
314 	 * checked to ensure that it is equal to the @source_tag argument given
315 	 * to g_simple_async_result_new() (which, by convention, is a pointer
316 	 * to the _async function corresponding to the _finish function from
317 	 * which this function is called).  (Alternatively, if either
318 	 * @source_tag or @result's source tag is %NULL, then the source tag
319 	 * check is skipped.)
320 	 *
321 	 * Deprecated: Use #GTask and g_task_is_valid() instead.
322 	 *
323 	 * Params:
324 	 *     result = the #GAsyncResult passed to the _finish function.
325 	 *     source = the #GObject passed to the _finish function.
326 	 *     sourceTag = the asynchronous function.
327 	 *
328 	 * Returns: #TRUE if all checks passed or #FALSE if any failed.
329 	 *
330 	 * Since: 2.20
331 	 */
332 	public static bool isValid(AsyncResultIF result, ObjectG source, void* sourceTag)
333 	{
334 		return g_simple_async_result_is_valid((result is null) ? null : result.getAsyncResultStruct(), (source is null) ? null : source.getObjectGStruct(), sourceTag) != 0;
335 	}
336 
337 	/**
338 	 * Completes an asynchronous I/O job immediately. Must be called in
339 	 * the thread where the asynchronous result was to be delivered, as it
340 	 * invokes the callback directly. If you are in a different thread use
341 	 * g_simple_async_result_complete_in_idle().
342 	 *
343 	 * Calling this function takes a reference to @simple for as long as
344 	 * is needed to complete the call.
345 	 *
346 	 * Deprecated: Use #GTask instead.
347 	 */
348 	public void complete()
349 	{
350 		g_simple_async_result_complete(gSimpleAsyncResult);
351 	}
352 
353 	/**
354 	 * Completes an asynchronous function in an idle handler in the
355 	 * [thread-default main context][g-main-context-push-thread-default]
356 	 * of the thread that @simple was initially created in
357 	 * (and re-pushes that context around the invocation of the callback).
358 	 *
359 	 * Calling this function takes a reference to @simple for as long as
360 	 * is needed to complete the call.
361 	 *
362 	 * Deprecated: Use #GTask instead.
363 	 */
364 	public void completeInIdle()
365 	{
366 		g_simple_async_result_complete_in_idle(gSimpleAsyncResult);
367 	}
368 
369 	/**
370 	 * Gets the operation result boolean from within the asynchronous result.
371 	 *
372 	 * Deprecated: Use #GTask and g_task_propagate_boolean() instead.
373 	 *
374 	 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
375 	 *     if the operation's result was %FALSE.
376 	 */
377 	public bool getOpResGboolean()
378 	{
379 		return g_simple_async_result_get_op_res_gboolean(gSimpleAsyncResult) != 0;
380 	}
381 
382 	/**
383 	 * Gets a pointer result as returned by the asynchronous function.
384 	 *
385 	 * Deprecated: Use #GTask and g_task_propagate_pointer() instead.
386 	 *
387 	 * Returns: a pointer from the result.
388 	 */
389 	public void* getOpResGpointer()
390 	{
391 		return g_simple_async_result_get_op_res_gpointer(gSimpleAsyncResult);
392 	}
393 
394 	/**
395 	 * Gets a gssize from the asynchronous result.
396 	 *
397 	 * Deprecated: Use #GTask and g_task_propagate_int() instead.
398 	 *
399 	 * Returns: a gssize returned from the asynchronous function.
400 	 */
401 	public ptrdiff_t getOpResGssize()
402 	{
403 		return g_simple_async_result_get_op_res_gssize(gSimpleAsyncResult);
404 	}
405 
406 	/**
407 	 * Gets the source tag for the #GSimpleAsyncResult.
408 	 *
409 	 * Deprecated: Use #GTask and g_task_get_source_tag() instead.
410 	 *
411 	 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
412 	 */
413 	public void* getSourceTag()
414 	{
415 		return g_simple_async_result_get_source_tag(gSimpleAsyncResult);
416 	}
417 
418 	/**
419 	 * Propagates an error from within the simple asynchronous result to
420 	 * a given destination.
421 	 *
422 	 * If the #GCancellable given to a prior call to
423 	 * g_simple_async_result_set_check_cancellable() is cancelled then this
424 	 * function will return %TRUE with @dest set appropriately.
425 	 *
426 	 * Deprecated: Use #GTask instead.
427 	 *
428 	 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
429 	 *
430 	 * Throws: GException on failure.
431 	 */
432 	public bool propagateError()
433 	{
434 		GError* err = null;
435 
436 		auto __p = g_simple_async_result_propagate_error(gSimpleAsyncResult, &err) != 0;
437 
438 		if (err !is null)
439 		{
440 			throw new GException( new ErrorG(err) );
441 		}
442 
443 		return __p;
444 	}
445 
446 	/**
447 	 * Runs the asynchronous job in a separate thread and then calls
448 	 * g_simple_async_result_complete_in_idle() on @simple to return
449 	 * the result to the appropriate main loop.
450 	 *
451 	 * Calling this function takes a reference to @simple for as long as
452 	 * is needed to run the job and report its completion.
453 	 *
454 	 * Deprecated: Use #GTask and g_task_run_in_thread() instead.
455 	 *
456 	 * Params:
457 	 *     func = a #GSimpleAsyncThreadFunc.
458 	 *     ioPriority = the io priority of the request.
459 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
460 	 */
461 	public void runInThread(GSimpleAsyncThreadFunc func, int ioPriority, Cancellable cancellable)
462 	{
463 		g_simple_async_result_run_in_thread(gSimpleAsyncResult, func, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct());
464 	}
465 
466 	/**
467 	 * Sets a #GCancellable to check before dispatching results.
468 	 *
469 	 * This function has one very specific purpose: the provided cancellable
470 	 * is checked at the time of g_simple_async_result_propagate_error() If
471 	 * it is cancelled, these functions will return an "Operation was
472 	 * cancelled" error (%G_IO_ERROR_CANCELLED).
473 	 *
474 	 * Implementors of cancellable asynchronous functions should use this in
475 	 * order to provide a guarantee to their callers that cancelling an
476 	 * async operation will reliably result in an error being returned for
477 	 * that operation (even if a positive result for the operation has
478 	 * already been sent as an idle to the main context to be dispatched).
479 	 *
480 	 * The checking described above is done regardless of any call to the
481 	 * unrelated g_simple_async_result_set_handle_cancellation() function.
482 	 *
483 	 * Deprecated: Use #GTask instead.
484 	 *
485 	 * Params:
486 	 *     checkCancellable = a #GCancellable to check, or %NULL to unset
487 	 *
488 	 * Since: 2.32
489 	 */
490 	public void setCheckCancellable(Cancellable checkCancellable)
491 	{
492 		g_simple_async_result_set_check_cancellable(gSimpleAsyncResult, (checkCancellable is null) ? null : checkCancellable.getCancellableStruct());
493 	}
494 
495 	/**
496 	 * Sets an error within the asynchronous result without a #GError.
497 	 * Unless writing a binding, see g_simple_async_result_set_error().
498 	 *
499 	 * Deprecated: Use #GTask and g_task_return_error() instead.
500 	 *
501 	 * Params:
502 	 *     domain = a #GQuark (usually #G_IO_ERROR).
503 	 *     code = an error code.
504 	 *     format = a formatted error reporting string.
505 	 *     args = va_list of arguments.
506 	 */
507 	public void setErrorVa(GQuark domain, int code, string format, void* args)
508 	{
509 		g_simple_async_result_set_error_va(gSimpleAsyncResult, domain, code, Str.toStringz(format), args);
510 	}
511 
512 	/**
513 	 * Sets the result from a #GError.
514 	 *
515 	 * Deprecated: Use #GTask and g_task_return_error() instead.
516 	 *
517 	 * Params:
518 	 *     error = #GError.
519 	 */
520 	public void setFromError(ErrorG error)
521 	{
522 		g_simple_async_result_set_from_error(gSimpleAsyncResult, (error is null) ? null : error.getErrorGStruct());
523 	}
524 
525 	/**
526 	 * Sets whether to handle cancellation within the asynchronous operation.
527 	 *
528 	 * This function has nothing to do with
529 	 * g_simple_async_result_set_check_cancellable().  It only refers to the
530 	 * #GCancellable passed to g_simple_async_result_run_in_thread().
531 	 *
532 	 * Params:
533 	 *     handleCancellation = a #gboolean.
534 	 */
535 	public void setHandleCancellation(bool handleCancellation)
536 	{
537 		g_simple_async_result_set_handle_cancellation(gSimpleAsyncResult, handleCancellation);
538 	}
539 
540 	/**
541 	 * Sets the operation result to a boolean within the asynchronous result.
542 	 *
543 	 * Deprecated: Use #GTask and g_task_return_boolean() instead.
544 	 *
545 	 * Params:
546 	 *     opRes = a #gboolean.
547 	 */
548 	public void setOpResGboolean(bool opRes)
549 	{
550 		g_simple_async_result_set_op_res_gboolean(gSimpleAsyncResult, opRes);
551 	}
552 
553 	/**
554 	 * Sets the operation result within the asynchronous result to a pointer.
555 	 *
556 	 * Deprecated: Use #GTask and g_task_return_pointer() instead.
557 	 *
558 	 * Params:
559 	 *     opRes = a pointer result from an asynchronous function.
560 	 *     destroyOpRes = a #GDestroyNotify function.
561 	 */
562 	public void setOpResGpointer(void* opRes, GDestroyNotify destroyOpRes)
563 	{
564 		g_simple_async_result_set_op_res_gpointer(gSimpleAsyncResult, opRes, destroyOpRes);
565 	}
566 
567 	/**
568 	 * Sets the operation result within the asynchronous result to
569 	 * the given @op_res.
570 	 *
571 	 * Deprecated: Use #GTask and g_task_return_int() instead.
572 	 *
573 	 * Params:
574 	 *     opRes = a #gssize.
575 	 */
576 	public void setOpResGssize(ptrdiff_t opRes)
577 	{
578 		g_simple_async_result_set_op_res_gssize(gSimpleAsyncResult, opRes);
579 	}
580 
581 	/**
582 	 * Sets the result from @error, and takes over the caller's ownership
583 	 * of @error, so the caller does not need to free it any more.
584 	 *
585 	 * Deprecated: Use #GTask and g_task_return_error() instead.
586 	 *
587 	 * Params:
588 	 *     error = a #GError
589 	 *
590 	 * Since: 2.28
591 	 */
592 	public void takeError(ErrorG error)
593 	{
594 		g_simple_async_result_take_error(gSimpleAsyncResult, (error is null) ? null : error.getErrorGStruct());
595 	}
596 
597 	/**
598 	 * Reports an error in an idle function. Similar to
599 	 * g_simple_async_report_error_in_idle(), but takes a #GError rather
600 	 * than building a new one.
601 	 *
602 	 * Deprecated: Use g_task_report_error().
603 	 *
604 	 * Params:
605 	 *     object = a #GObject, or %NULL
606 	 *     callback = a #GAsyncReadyCallback.
607 	 *     userData = user data passed to @callback.
608 	 *     error = the #GError to report
609 	 */
610 	public static void simpleAsyncReportGerrorInIdle(ObjectG object, GAsyncReadyCallback callback, void* userData, ErrorG error)
611 	{
612 		g_simple_async_report_gerror_in_idle((object is null) ? null : object.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct());
613 	}
614 
615 	/**
616 	 * Reports an error in an idle function. Similar to
617 	 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
618 	 * ownership of @error, so the caller does not have to free it any more.
619 	 *
620 	 * Deprecated: Use g_task_report_error().
621 	 *
622 	 * Params:
623 	 *     object = a #GObject, or %NULL
624 	 *     callback = a #GAsyncReadyCallback.
625 	 *     userData = user data passed to @callback.
626 	 *     error = the #GError to report
627 	 *
628 	 * Since: 2.28
629 	 */
630 	public static void simpleAsyncReportTakeGerrorInIdle(ObjectG object, GAsyncReadyCallback callback, void* userData, ErrorG error)
631 	{
632 		g_simple_async_report_take_gerror_in_idle((object is null) ? null : object.getObjectGStruct(), callback, userData, (error is null) ? null : error.getErrorGStruct());
633 	}
634 }