introspection: Remove spurious trailing :
[platform/upstream/glib.git] / gio / gsimpleasyncresult.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include "gsimpleasyncresult.h"
35 #include "gasyncresult.h"
36 #include "gcancellable.h"
37 #include "gioscheduler.h"
38 #include <gio/gioerror.h>
39 #include "glibintl.h"
40
41
42 /**
43  * SECTION:gsimpleasyncresult
44  * @short_description: Simple asynchronous results implementation
45  * @include: gio/gio.h
46  * @see_also: #GAsyncResult
47  *
48  * Implements #GAsyncResult for simple cases. Most of the time, this
49  * will be all an application needs, and will be used transparently.
50  * Because of this, #GSimpleAsyncResult is used throughout GIO for
51  * handling asynchronous functions.
52  *
53  * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
54  * reporting, operation cancellation and the final state of an operation,
55  * completely transparent to the application. Results can be returned
56  * as a pointer e.g. for functions that return data that is collected
57  * asynchronously, a boolean value for checking the success or failure
58  * of an operation, or a #gssize for operations which return the number
59  * of bytes modified by the operation; all of the simple return cases
60  * are covered.
61  *
62  * Most of the time, an application will not need to know of the details
63  * of this API; it is handled transparently, and any necessary operations
64  * are handled by #GAsyncResult's interface. However, if implementing a
65  * new GIO module, for writing language bindings, or for complex
66  * applications that need better control of how asynchronous operations
67  * are completed, it is important to understand this functionality.
68  *
69  * GSimpleAsyncResults are tagged with the calling function to ensure
70  * that asynchronous functions and their finishing functions are used
71  * together correctly.
72  *
73  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
74  * If the result needs to be created for a #GError, use
75  * g_simple_async_result_new_from_error() or
76  * g_simple_async_result_new_take_error(). If a #GError is not available
77  * (e.g. the asynchronous operation's doesn't take a #GError argument),
78  * but the result still needs to be created for an error condition, use
79  * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
80  * if your application or binding requires passing a variable argument list
81  * directly), and the error can then be propagated through the use of
82  * g_simple_async_result_propagate_error().
83  *
84  * An asynchronous operation can be made to ignore a cancellation event by
85  * calling g_simple_async_result_set_handle_cancellation() with a
86  * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
87  * operations that are dangerous to cancel, such as close (which would
88  * cause a leak if cancelled before being run).
89  *
90  * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
91  * or it can use #GThread<!-- -->s if available.
92  * g_simple_async_result_complete() will finish an I/O task directly
93  * from the point where it is called. g_simple_async_result_complete_in_idle()
94  * will finish it from an idle handler in the <link
95  * linkend="g-main-context-push-thread-default">thread-default main
96  * context</link>. g_simple_async_result_run_in_thread() will run the
97  * job in a separate thread and then deliver the result to the
98  * thread-default main context.
99  *
100  * To set the results of an asynchronous function,
101  * g_simple_async_result_set_op_res_gpointer(),
102  * g_simple_async_result_set_op_res_gboolean(), and
103  * g_simple_async_result_set_op_res_gssize()
104  * are provided, setting the operation's result to a gpointer, gboolean, or
105  * gssize, respectively.
106  *
107  * Likewise, to get the result of an asynchronous function,
108  * g_simple_async_result_get_op_res_gpointer(),
109  * g_simple_async_result_get_op_res_gboolean(), and
110  * g_simple_async_result_get_op_res_gssize() are
111  * provided, getting the operation's result as a gpointer, gboolean, and
112  * gssize, respectively.
113  *
114  * For the details of the requirements implementations must respect, see
115  * #GAsyncResult.  A typical implementation of an asynchronous operation
116  * using GSimpleAsyncResult looks something like this:
117  *
118  * |[
119  * static void
120  * baked_cb (Cake    *cake,
121  *           gpointer user_data)
122  * {
123  *   /&ast; In this example, this callback is not given a reference to the cake, so
124  *    &ast; the GSimpleAsyncResult has to take a reference to it.
125  *    &ast;/
126  *   GSimpleAsyncResult *result = user_data;
127  *
128  *   if (cake == NULL)
129  *     g_simple_async_result_set_error (result,
130  *                                      BAKER_ERRORS,
131  *                                      BAKER_ERROR_NO_FLOUR,
132  *                                      "Go to the supermarket");
133  *   else
134  *     g_simple_async_result_set_op_res_gpointer (result,
135  *                                                g_object_ref (cake),
136  *                                                g_object_unref);
137  *
138  *
139  *   /&ast; In this example, we assume that baked_cb is called as a callback from
140  *    &ast; the mainloop, so it's safe to complete the operation synchronously here.
141  *    &ast; If, however, _baker_prepare_cake () might call its callback without
142  *    &ast; first returning to the mainloop — inadvisable, but some APIs do so —
143  *    &ast; we would need to use g_simple_async_result_complete_in_idle().
144  *    &ast;/
145  *   g_simple_async_result_complete (result);
146  *   g_object_unref (result);
147  * }
148  *
149  * void
150  * baker_bake_cake_async (Baker              *self,
151  *                        guint               radius,
152  *                        GAsyncReadyCallback callback,
153  *                        gpointer            user_data)
154  * {
155  *   GSimpleAsyncResult *simple;
156  *   Cake               *cake;
157  *
158  *   if (radius < 3)
159  *     {
160  *       g_simple_async_report_error_in_idle (G_OBJECT (self),
161  *                                            callback,
162  *                                            user_data,
163  *                                            BAKER_ERRORS,
164  *                                            BAKER_ERROR_TOO_SMALL,
165  *                                            "%ucm radius cakes are silly",
166  *                                            radius);
167  *       return;
168  *     }
169  *
170  *   simple = g_simple_async_result_new (G_OBJECT (self),
171  *                                       callback,
172  *                                       user_data,
173  *                                       baker_bake_cake_async);
174  *   cake = _baker_get_cached_cake (self, radius);
175  *
176  *   if (cake != NULL)
177  *     {
178  *       g_simple_async_result_set_op_res_gpointer (simple,
179  *                                                  g_object_ref (cake),
180  *                                                  g_object_unref);
181  *       g_simple_async_result_complete_in_idle (simple);
182  *       g_object_unref (simple);
183  *       /&ast; Drop the reference returned by _baker_get_cached_cake(); the
184  *        &ast; GSimpleAsyncResult has taken its own reference.
185  *        &ast;/
186  *       g_object_unref (cake);
187  *       return;
188  *     }
189  *
190  *   _baker_prepare_cake (self, radius, baked_cb, user_data);
191  * }
192  *
193  * Cake *
194  * baker_bake_cake_finish (Baker        *self,
195  *                         GAsyncResult *result,
196  *                         GError      **error)
197  * {
198  *   GSimpleAsyncResult *simple;
199  *   Cake               *cake;
200  *
201  *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
202  *                                                         G_OBJECT (self),
203  *                                                         baker_bake_cake_async),
204  *                         NULL);
205  *
206  *   simple = (GSimpleAsyncResult *) result;
207  *
208  *   if (g_simple_async_result_propagate_error (simple, error))
209  *     return NULL;
210  *
211  *   cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
212  *   return g_object_ref (cake);
213  * }
214  * ]|
215  */
216
217 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
218
219 struct _GSimpleAsyncResult
220 {
221   GObject parent_instance;
222
223   GObject *source_object;
224   GAsyncReadyCallback callback;
225   gpointer user_data;
226   GMainContext *context;
227   GError *error;
228   gboolean failed;
229   gboolean handle_cancellation;
230
231   gpointer source_tag;
232
233   union {
234     gpointer v_pointer;
235     gboolean v_boolean;
236     gssize   v_ssize;
237   } op_res;
238
239   GDestroyNotify destroy_op_res;
240 };
241
242 struct _GSimpleAsyncResultClass
243 {
244   GObjectClass parent_class;
245 };
246
247
248 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
249                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
250                                                 g_simple_async_result_async_result_iface_init))
251
252 static void
253 clear_op_res (GSimpleAsyncResult *simple)
254 {
255   if (simple->destroy_op_res)
256     simple->destroy_op_res (simple->op_res.v_pointer);
257   simple->destroy_op_res = NULL;
258   simple->op_res.v_ssize = 0;
259 }
260
261 static void
262 g_simple_async_result_finalize (GObject *object)
263 {
264   GSimpleAsyncResult *simple;
265
266   simple = G_SIMPLE_ASYNC_RESULT (object);
267
268   if (simple->source_object)
269     g_object_unref (simple->source_object);
270
271   if (simple->context)
272     g_main_context_unref (simple->context);
273
274   clear_op_res (simple);
275
276   if (simple->error)
277     g_error_free (simple->error);
278
279   G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
280 }
281
282 static void
283 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
284 {
285   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
286  
287   gobject_class->finalize = g_simple_async_result_finalize;
288 }
289
290 static void
291 g_simple_async_result_init (GSimpleAsyncResult *simple)
292 {
293   simple->handle_cancellation = TRUE;
294
295   simple->context = g_main_context_get_thread_default ();
296   if (simple->context)
297     g_main_context_ref (simple->context);
298 }
299
300 /**
301  * g_simple_async_result_new:
302  * @source_object: a #GObject the asynchronous function was called with,
303  * or %NULL.
304  * @callback: a #GAsyncReadyCallback.
305  * @user_data: user data passed to @callback.
306  * @source_tag: the asynchronous function.
307  *
308  * Creates a #GSimpleAsyncResult.
309  *
310  * Returns: a #GSimpleAsyncResult.
311  **/
312 GSimpleAsyncResult *
313 g_simple_async_result_new (GObject             *source_object,
314                            GAsyncReadyCallback  callback,
315                            gpointer             user_data,
316                            gpointer             source_tag)
317 {
318   GSimpleAsyncResult *simple;
319
320   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
321
322   simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
323   simple->callback = callback;
324   if (source_object)
325     simple->source_object = g_object_ref (source_object);
326   else
327     simple->source_object = NULL;
328   simple->user_data = user_data;
329   simple->source_tag = source_tag;
330  
331   return simple;
332 }
333
334 /**
335  * g_simple_async_result_new_from_error:
336  * @source_object: a #GObject, or %NULL.
337  * @callback: a #GAsyncReadyCallback.
338  * @user_data: user data passed to @callback.
339  * @error: a #GError
340  *
341  * Creates a #GSimpleAsyncResult from an error condition.
342  *
343  * Returns: a #GSimpleAsyncResult.
344  **/
345 GSimpleAsyncResult *
346 g_simple_async_result_new_from_error (GObject             *source_object,
347                                       GAsyncReadyCallback  callback,
348                                       gpointer             user_data,
349                                       const GError        *error)
350 {
351   GSimpleAsyncResult *simple;
352
353   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
354
355   simple = g_simple_async_result_new (source_object,
356                                       callback,
357                                       user_data, NULL);
358   g_simple_async_result_set_from_error (simple, error);
359
360   return simple;
361 }
362
363 /**
364  * g_simple_async_result_new_take_error:
365  * @source_object: (allow-none): a #GObject, or %NULL
366  * @callback: a #GAsyncReadyCallback
367  * @user_data: (allow-none): user data passed to @callback
368  * @error: a #GError
369  *
370  * Creates a #GSimpleAsyncResult from an error condition, and takes over the
371  * caller's ownership of @error, so the caller does not need to free it anymore.
372  *
373  * Returns: a #GSimpleAsyncResult
374  *
375  * Since: 2.28
376  **/
377 GSimpleAsyncResult *
378 g_simple_async_result_new_take_error (GObject             *source_object,
379                                       GAsyncReadyCallback  callback,
380                                       gpointer             user_data,
381                                       GError              *error)
382 {
383   GSimpleAsyncResult *simple;
384
385   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
386
387   simple = g_simple_async_result_new (source_object,
388                                       callback,
389                                       user_data, NULL);
390   g_simple_async_result_take_error (simple, error);
391
392   return simple;
393 }
394
395 /**
396  * g_simple_async_result_new_error:
397  * @source_object: a #GObject, or %NULL.
398  * @callback: a #GAsyncReadyCallback.
399  * @user_data: user data passed to @callback.
400  * @domain: a #GQuark.
401  * @code: an error code.
402  * @format: a string with format characters.
403  * @...: a list of values to insert into @format.
404  *
405  * Creates a new #GSimpleAsyncResult with a set error.
406  *
407  * Returns: a #GSimpleAsyncResult.
408  **/
409 GSimpleAsyncResult *
410 g_simple_async_result_new_error (GObject             *source_object,
411                                  GAsyncReadyCallback  callback,
412                                  gpointer             user_data,
413                                  GQuark               domain,
414                                  gint                 code,
415                                  const char          *format,
416                                  ...)
417 {
418   GSimpleAsyncResult *simple;
419   va_list args;
420  
421   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
422   g_return_val_if_fail (domain != 0, NULL);
423   g_return_val_if_fail (format != NULL, NULL);
424
425   simple = g_simple_async_result_new (source_object,
426                                       callback,
427                                       user_data, NULL);
428
429   va_start (args, format);
430   g_simple_async_result_set_error_va (simple, domain, code, format, args);
431   va_end (args);
432  
433   return simple;
434 }
435
436
437 static gpointer
438 g_simple_async_result_get_user_data (GAsyncResult *res)
439 {
440   return G_SIMPLE_ASYNC_RESULT (res)->user_data;
441 }
442
443 static GObject *
444 g_simple_async_result_get_source_object (GAsyncResult *res)
445 {
446   if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
447     return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
448   return NULL;
449 }
450
451 static void
452 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
453 {
454   iface->get_user_data = g_simple_async_result_get_user_data;
455   iface->get_source_object = g_simple_async_result_get_source_object;
456 }
457
458 /**
459  * g_simple_async_result_set_handle_cancellation:
460  * @simple: a #GSimpleAsyncResult.
461  * @handle_cancellation: a #gboolean.
462  *
463  * Sets whether to handle cancellation within the asynchronous operation.
464  *
465  **/
466 void
467 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
468                                                gboolean            handle_cancellation)
469 {
470   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
471   simple->handle_cancellation = handle_cancellation;
472 }
473
474 /**
475  * g_simple_async_result_get_source_tag: (skip)
476  * @simple: a #GSimpleAsyncResult.
477  *
478  * Gets the source tag for the #GSimpleAsyncResult.
479  *
480  * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
481  **/
482 gpointer
483 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
484 {
485   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
486   return simple->source_tag;
487 }
488
489 /**
490  * g_simple_async_result_propagate_error:
491  * @simple: a #GSimpleAsyncResult.
492  * @dest: a location to propegate the error to.
493  *
494  * Propagates an error from within the simple asynchronous result to
495  * a given destination.
496  *
497  * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
498  **/
499 gboolean
500 g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
501                                        GError             **dest)
502 {
503   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
504
505   if (simple->failed)
506     {
507       g_propagate_error (dest, simple->error);
508       simple->error = NULL;
509       return TRUE;
510     }
511
512   return FALSE;
513 }
514
515 /**
516  * g_simple_async_result_set_op_res_gpointer:
517  * @simple: a #GSimpleAsyncResult.
518  * @op_res: a pointer result from an asynchronous function.
519  * @destroy_op_res: a #GDestroyNotify function.
520  *
521  * Sets the operation result within the asynchronous result to a pointer.
522  **/
523 void
524 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
525                                            gpointer            op_res,
526                                            GDestroyNotify      destroy_op_res)
527 {
528   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
529
530   clear_op_res (simple);
531   simple->op_res.v_pointer = op_res;
532   simple->destroy_op_res = destroy_op_res;
533 }
534
535 /**
536  * g_simple_async_result_get_op_res_gpointer: (skip)
537  * @simple: a #GSimpleAsyncResult.
538  *
539  * Gets a pointer result as returned by the asynchronous function.
540  *
541  * Returns: a pointer from the result.
542  **/
543 gpointer
544 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
545 {
546   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
547   return simple->op_res.v_pointer;
548 }
549
550 /**
551  * g_simple_async_result_set_op_res_gssize:
552  * @simple: a #GSimpleAsyncResult.
553  * @op_res: a #gssize.
554  *
555  * Sets the operation result within the asynchronous result to
556  * the given @op_res.
557  **/
558 void
559 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
560                                          gssize              op_res)
561 {
562   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
563   clear_op_res (simple);
564   simple->op_res.v_ssize = op_res;
565 }
566
567 /**
568  * g_simple_async_result_get_op_res_gssize:
569  * @simple: a #GSimpleAsyncResult.
570  *
571  * Gets a gssize from the asynchronous result.
572  *
573  * Returns: a gssize returned from the asynchronous function.
574  **/
575 gssize
576 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
577 {
578   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
579   return simple->op_res.v_ssize;
580 }
581
582 /**
583  * g_simple_async_result_set_op_res_gboolean:
584  * @simple: a #GSimpleAsyncResult.
585  * @op_res: a #gboolean.
586  *
587  * Sets the operation result to a boolean within the asynchronous result.
588  **/
589 void
590 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
591                                            gboolean            op_res)
592 {
593   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
594   clear_op_res (simple);
595   simple->op_res.v_boolean = !!op_res;
596 }
597
598 /**
599  * g_simple_async_result_get_op_res_gboolean:
600  * @simple: a #GSimpleAsyncResult.
601  *
602  * Gets the operation result boolean from within the asynchronous result.
603  *
604  * Returns: %TRUE if the operation's result was %TRUE, %FALSE
605  *     if the operation's result was %FALSE.
606  **/
607 gboolean
608 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
609 {
610   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
611   return simple->op_res.v_boolean;
612 }
613
614 /**
615  * g_simple_async_result_set_from_error:
616  * @simple: a #GSimpleAsyncResult.
617  * @error: #GError.
618  *
619  * Sets the result from a #GError.
620  **/
621 void
622 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
623                                       const GError       *error)
624 {
625   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
626   g_return_if_fail (error != NULL);
627
628   if (simple->error)
629     g_error_free (simple->error);
630   simple->error = g_error_copy (error);
631   simple->failed = TRUE;
632 }
633
634 /**
635  * g_simple_async_result_take_error:
636  * @simple: a #GSimpleAsyncResult
637  * @error: a #GError
638  *
639  * Sets the result from @error, and takes over the caller's ownership
640  * of @error, so the caller does not need to free it any more.
641  *
642  * Since: 2.28
643  **/
644 void
645 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
646                                   GError             *error)
647 {
648   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
649   g_return_if_fail (error != NULL);
650
651   if (simple->error)
652     g_error_free (simple->error);
653   simple->error = error;
654   simple->failed = TRUE;
655 }
656
657 /**
658  * g_simple_async_result_set_error_va:
659  * @simple: a #GSimpleAsyncResult.
660  * @domain: a #GQuark (usually #G_IO_ERROR).
661  * @code: an error code.
662  * @format: a formatted error reporting string.
663  * @args: va_list of arguments.
664  *
665  * Sets an error within the asynchronous result without a #GError.
666  * Unless writing a binding, see g_simple_async_result_set_error().
667  **/
668 void
669 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
670                                     GQuark              domain,
671                                     gint                code,
672                                     const char         *format,
673                                     va_list             args)
674 {
675   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
676   g_return_if_fail (domain != 0);
677   g_return_if_fail (format != NULL);
678
679   if (simple->error)
680     g_error_free (simple->error);
681   simple->error = g_error_new_valist (domain, code, format, args);
682   simple->failed = TRUE;
683 }
684
685 /**
686  * g_simple_async_result_set_error:
687  * @simple: a #GSimpleAsyncResult.
688  * @domain: a #GQuark (usually #G_IO_ERROR).
689  * @code: an error code.
690  * @format: a formatted error reporting string.
691  * @...: a list of variables to fill in @format.
692  *
693  * Sets an error within the asynchronous result without a #GError.
694  **/
695 void
696 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
697                                  GQuark              domain,
698                                  gint                code,
699                                  const char         *format,
700                                  ...)
701 {
702   va_list args;
703
704   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
705   g_return_if_fail (domain != 0);
706   g_return_if_fail (format != NULL);
707
708   va_start (args, format);
709   g_simple_async_result_set_error_va (simple, domain, code, format, args);
710   va_end (args);
711 }
712
713 /**
714  * g_simple_async_result_complete:
715  * @simple: a #GSimpleAsyncResult.
716  *
717  * Completes an asynchronous I/O job immediately. Must be called in
718  * the thread where the asynchronous result was to be delivered, as it
719  * invokes the callback directly. If you are in a different thread use
720  * g_simple_async_result_complete_in_idle().
721  *
722  * Calling this function takes a reference to @simple for as long as
723  * is needed to complete the call.
724  **/
725 void
726 g_simple_async_result_complete (GSimpleAsyncResult *simple)
727 {
728 #ifndef G_DISABLE_CHECKS
729   GSource *current_source;
730   GMainContext *current_context;
731 #endif
732
733   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
734
735 #ifndef G_DISABLE_CHECKS
736   current_source = g_main_current_source ();
737   if (current_source && !g_source_is_destroyed (current_source))
738     {
739       current_context = g_source_get_context (current_source);
740       if (current_context == g_main_context_default ())
741         current_context = NULL;
742       if (simple->context != current_context)
743         g_warning ("g_simple_async_result_complete() called from wrong context!");
744     }
745 #endif
746
747   if (simple->callback)
748     simple->callback (simple->source_object,
749                       G_ASYNC_RESULT (simple),
750                       simple->user_data);
751 }
752
753 static gboolean
754 complete_in_idle_cb (gpointer data)
755 {
756   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
757
758   g_simple_async_result_complete (simple);
759
760   return FALSE;
761 }
762
763 /**
764  * g_simple_async_result_complete_in_idle:
765  * @simple: a #GSimpleAsyncResult.
766  *
767  * Completes an asynchronous function in an idle handler in the <link
768  * linkend="g-main-context-push-thread-default">thread-default main
769  * loop</link> of the thread that @simple was initially created in.
770  *
771  * Calling this function takes a reference to @simple for as long as
772  * is needed to complete the call.
773  */
774 void
775 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
776 {
777   GSource *source;
778
779   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
780
781   g_object_ref (simple);
782  
783   source = g_idle_source_new ();
784   g_source_set_priority (source, G_PRIORITY_DEFAULT);
785   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
786
787   g_source_attach (source, simple->context);
788   g_source_unref (source);
789 }
790
791 typedef struct {
792   GSimpleAsyncResult *simple;
793   GCancellable *cancellable;
794   GSimpleAsyncThreadFunc func;
795 } RunInThreadData;
796
797
798 static gboolean
799 complete_in_idle_cb_for_thread (gpointer _data)
800 {
801   RunInThreadData *data = _data;
802   GSimpleAsyncResult *simple;
803
804   simple = data->simple;
805  
806   if (simple->handle_cancellation &&
807       g_cancellable_is_cancelled (data->cancellable))
808     g_simple_async_result_set_error (simple,
809                                      G_IO_ERROR,
810                                      G_IO_ERROR_CANCELLED,
811                                      "%s", _("Operation was cancelled"));
812  
813   g_simple_async_result_complete (simple);
814
815   if (data->cancellable)
816     g_object_unref (data->cancellable);
817   g_object_unref (data->simple);
818   g_free (data);
819  
820   return FALSE;
821 }
822
823 static gboolean
824 run_in_thread (GIOSchedulerJob *job,
825                GCancellable    *c,
826                gpointer         _data)
827 {
828   RunInThreadData *data = _data;
829   GSimpleAsyncResult *simple = data->simple;
830   GSource *source;
831  
832   if (simple->handle_cancellation &&
833       g_cancellable_is_cancelled (c))
834     g_simple_async_result_set_error (simple,
835                                      G_IO_ERROR,
836                                      G_IO_ERROR_CANCELLED,
837                                      "%s", _("Operation was cancelled"));
838   else
839     data->func (simple,
840                 simple->source_object,
841                 c);
842
843   source = g_idle_source_new ();
844   g_source_set_priority (source, G_PRIORITY_DEFAULT);
845   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
846
847   g_source_attach (source, simple->context);
848   g_source_unref (source);
849
850   return FALSE;
851 }
852
853 /**
854  * g_simple_async_result_run_in_thread:
855  * @simple: a #GSimpleAsyncResult.
856  * @func: a #GSimpleAsyncThreadFunc.
857  * @io_priority: the io priority of the request.
858  * @cancellable: optional #GCancellable object, %NULL to ignore.
859  *
860  * Runs the asynchronous job in a separate thread and then calls
861  * g_simple_async_result_complete_in_idle() on @simple to return
862  * the result to the appropriate main loop.
863  *
864  * Calling this function takes a reference to @simple for as long as
865  * is needed to run the job and report its completion.
866  */
867 void
868 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
869                                      GSimpleAsyncThreadFunc  func,
870                                      int                     io_priority,
871                                      GCancellable           *cancellable)
872 {
873   RunInThreadData *data;
874
875   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
876   g_return_if_fail (func != NULL);
877
878   data = g_new (RunInThreadData, 1);
879   data->func = func;
880   data->simple = g_object_ref (simple);
881   data->cancellable = cancellable;
882   if (cancellable)
883     g_object_ref (cancellable);
884   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
885 }
886
887 /**
888  * g_simple_async_result_is_valid:
889  * @result: the #GAsyncResult passed to the _finish function.
890  * @source: the #GObject passed to the _finish function.
891  * @source_tag: the asynchronous function.
892  *
893  * Ensures that the data passed to the _finish function of an async
894  * operation is consistent.  Three checks are performed.
895  *
896  * First, @result is checked to ensure that it is really a
897  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
898  * matches the source object of @result.  Third, @source_tag is
899  * checked to ensure that it is either %NULL (as it is when the result was
900  * created by g_simple_async_report_error_in_idle() or
901  * g_simple_async_report_gerror_in_idle()) or equal to the
902  * @source_tag argument given to g_simple_async_result_new() (which, by
903  * convention, is a pointer to the _async function corresponding to the
904  * _finish function from which this function is called).
905  *
906  * Returns: #TRUE if all checks passed or #FALSE if any failed.
907  **/
908 gboolean
909 g_simple_async_result_is_valid (GAsyncResult *result,
910                                 GObject      *source,
911                                 gpointer      source_tag)
912 {
913   GSimpleAsyncResult *simple;
914   GObject *cmp_source;
915
916   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
917     return FALSE;
918   simple = (GSimpleAsyncResult *)result;
919
920   cmp_source = g_async_result_get_source_object (result);
921   if (cmp_source != source)
922     {
923       if (cmp_source != NULL)
924         g_object_unref (cmp_source);
925       return FALSE;
926     }
927   if (cmp_source != NULL)
928     g_object_unref (cmp_source);
929
930   return source_tag == NULL ||
931          source_tag == g_simple_async_result_get_source_tag (simple);
932 }
933
934 /**
935  * g_simple_async_report_error_in_idle:
936  * @object: a #GObject.
937  * @callback: a #GAsyncReadyCallback.
938  * @user_data: user data passed to @callback.
939  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
940  * @code: a specific error code.
941  * @format: a formatted error reporting string.
942  * @...: a list of variables to fill in @format.
943  *
944  * Reports an error in an asynchronous function in an idle function by
945  * directly setting the contents of the #GAsyncResult with the given error
946  * information.
947  **/
948 void
949 g_simple_async_report_error_in_idle (GObject             *object,
950                                      GAsyncReadyCallback  callback,
951                                      gpointer             user_data,
952                                      GQuark               domain,
953                                      gint                 code,
954                                      const char          *format,
955                                      ...)
956 {
957   GSimpleAsyncResult *simple;
958   va_list args;
959  
960   g_return_if_fail (G_IS_OBJECT (object));
961   g_return_if_fail (domain != 0);
962   g_return_if_fail (format != NULL);
963
964   simple = g_simple_async_result_new (object,
965                                       callback,
966                                       user_data, NULL);
967
968   va_start (args, format);
969   g_simple_async_result_set_error_va (simple, domain, code, format, args);
970   va_end (args);
971   g_simple_async_result_complete_in_idle (simple);
972   g_object_unref (simple);
973 }
974
975 /**
976  * g_simple_async_report_gerror_in_idle:
977  * @object: a #GObject.
978  * @callback: a #GAsyncReadyCallback.
979  * @user_data: user data passed to @callback.
980  * @error: the #GError to report
981  *
982  * Reports an error in an idle function. Similar to
983  * g_simple_async_report_error_in_idle(), but takes a #GError rather
984  * than building a new one.
985  **/
986 void
987 g_simple_async_report_gerror_in_idle (GObject *object,
988                                       GAsyncReadyCallback callback,
989                                       gpointer user_data,
990                                       const GError *error)
991 {
992   GSimpleAsyncResult *simple;
993  
994   g_return_if_fail (G_IS_OBJECT (object));
995   g_return_if_fail (error != NULL);
996
997   simple = g_simple_async_result_new_from_error (object,
998                                                  callback,
999                                                  user_data,
1000                                                  error);
1001   g_simple_async_result_complete_in_idle (simple);
1002   g_object_unref (simple);
1003 }
1004
1005 /**
1006  * g_simple_async_report_take_gerror_in_idle:
1007  * @object: a #GObject.
1008  * @callback: a #GAsyncReadyCallback.
1009  * @user_data: user data passed to @callback.
1010  * @error: the #GError to report
1011  *
1012  * Reports an error in an idle function. Similar to
1013  * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1014  * ownership of @error, so the caller does not have to free it any more.
1015  *
1016  * Since: 2.28
1017  **/
1018 void
1019 g_simple_async_report_take_gerror_in_idle (GObject *object,
1020                                            GAsyncReadyCallback callback,
1021                                            gpointer user_data,
1022                                            GError *error)
1023 {
1024   GSimpleAsyncResult *simple;
1025
1026   g_return_if_fail (G_IS_OBJECT (object));
1027   g_return_if_fail (error != NULL);
1028
1029   simple = g_simple_async_result_new_take_error (object,
1030                                                  callback,
1031                                                  user_data,
1032                                                  error);
1033   g_simple_async_result_complete_in_idle (simple);
1034   g_object_unref (simple);
1035 }