Add g_simple_async_result_is_valid(). Implementation by Dan Winship.
[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 #include "gioalias.h"
42
43 /**
44  * SECTION:gsimpleasyncresult
45  * @short_description: Simple asynchronous results implementation
46  * @include: gio/gio.h
47  * @see_also: #GAsyncResult
48  *
49  * Implements #GAsyncResult for simple cases. Most of the time, this 
50  * will be all an application needs, and will be used transparently. 
51  * Because of this, #GSimpleAsyncResult is used throughout GIO for 
52  * handling asynchronous functions. 
53  * 
54  * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error 
55  * reporting, operation cancellation and the final state of an operation, 
56  * completely transparent to the application. Results can be returned 
57  * as a pointer e.g. for functions that return data that is collected 
58  * asynchronously, a boolean value for checking the success or failure 
59  * of an operation, or a #gssize for operations which return the number 
60  * of bytes modified by the operation; all of the simple return cases 
61  * are covered.
62  * 
63  * Most of the time, an application will not need to know of the details 
64  * of this API; it is handled transparently, and any necessary operations 
65  * are handled by #GAsyncResult's interface. However, if implementing a 
66  * new GIO module, for writing language bindings, or for complex 
67  * applications that need better control of how asynchronous operations 
68  * are completed, it is important to understand this functionality.
69  * 
70  * GSimpleAsyncResults are tagged with the calling function to ensure 
71  * that asynchronous functions and their finishing functions are used 
72  * together correctly.
73  * 
74  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new(). 
75  * If the result needs to be created for a #GError, use 
76  * g_simple_async_result_new_from_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 propegated 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 within 
93  * the main event loop. g_simple_async_result_complete_in_idle() will 
94  * integrate the I/O task into the main event loop as an idle function and 
95  * g_simple_async_result_run_in_thread() will run the job in a separate 
96  * thread.
97  * 
98  * To set the results of an asynchronous function, 
99  * g_simple_async_result_set_op_res_gpointer(), 
100  * g_simple_async_result_set_op_res_gboolean(), and 
101  * g_simple_async_result_set_op_res_gssize()
102  * are provided, setting the operation's result to a gpointer, gboolean, or 
103  * gssize, respectively.
104  * 
105  * Likewise, to get the result of an asynchronous function, 
106  * g_simple_async_result_get_op_res_gpointer(),
107  * g_simple_async_result_get_op_res_gboolean(), and 
108  * g_simple_async_result_get_op_res_gssize() are 
109  * provided, getting the operation's result as a gpointer, gboolean, and 
110  * gssize, respectively.
111  **/
112
113 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
114
115 struct _GSimpleAsyncResult
116 {
117   GObject parent_instance;
118
119   GObject *source_object;
120   GAsyncReadyCallback callback;
121   gpointer user_data;
122   GError *error;
123   gboolean failed;
124   gboolean handle_cancellation;
125
126   gpointer source_tag;
127
128   union {
129     gpointer v_pointer;
130     gboolean v_boolean;
131     gssize   v_ssize;
132   } op_res;
133
134   GDestroyNotify destroy_op_res;
135 };
136
137 struct _GSimpleAsyncResultClass
138 {
139   GObjectClass parent_class;
140 };
141
142
143 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
144                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
145                                                 g_simple_async_result_async_result_iface_init))
146
147 static void
148 g_simple_async_result_finalize (GObject *object)
149 {
150   GSimpleAsyncResult *simple;
151
152   simple = G_SIMPLE_ASYNC_RESULT (object);
153
154   if (simple->source_object)
155     g_object_unref (simple->source_object);
156
157   if (simple->destroy_op_res)
158     simple->destroy_op_res (simple->op_res.v_pointer);
159
160   if (simple->error)
161     g_error_free (simple->error);
162
163   G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
164 }
165
166 static void
167 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
168 {
169   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170   
171   gobject_class->finalize = g_simple_async_result_finalize;
172 }
173
174 static void
175 g_simple_async_result_init (GSimpleAsyncResult *simple)
176 {
177   simple->handle_cancellation = TRUE;
178 }
179
180 /**
181  * g_simple_async_result_new:
182  * @source_object: a #GObject the asynchronous function was called with.
183  * @callback: a #GAsyncReadyCallback.
184  * @user_data: user data passed to @callback.
185  * @source_tag: the asynchronous function.
186  * 
187  * Creates a #GSimpleAsyncResult.
188  * 
189  * Returns: a #GSimpleAsyncResult.
190  **/
191 GSimpleAsyncResult *
192 g_simple_async_result_new (GObject             *source_object,
193                            GAsyncReadyCallback  callback,
194                            gpointer             user_data,
195                            gpointer             source_tag)
196 {
197   GSimpleAsyncResult *simple;
198
199   g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
200
201   simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
202   simple->callback = callback;
203   simple->source_object = g_object_ref (source_object);
204   simple->user_data = user_data;
205   simple->source_tag = source_tag;
206   
207   return simple;
208 }
209
210 /**
211  * g_simple_async_result_new_from_error:
212  * @source_object: a #GObject.
213  * @callback: a #GAsyncReadyCallback.
214  * @user_data: user data passed to @callback.
215  * @error: a #GError location.
216  * 
217  * Creates a #GSimpleAsyncResult from an error condition.
218  * 
219  * Returns: a #GSimpleAsyncResult.
220  **/
221 GSimpleAsyncResult *
222 g_simple_async_result_new_from_error (GObject             *source_object,
223                                       GAsyncReadyCallback  callback,
224                                       gpointer             user_data,
225                                       GError              *error)
226 {
227   GSimpleAsyncResult *simple;
228
229   g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
230
231   simple = g_simple_async_result_new (source_object,
232                                       callback,
233                                       user_data, NULL);
234   g_simple_async_result_set_from_error (simple, error);
235
236   return simple;
237 }
238
239 /**
240  * g_simple_async_result_new_error:
241  * @source_object: a #GObject.
242  * @callback: a #GAsyncReadyCallback. 
243  * @user_data: user data passed to @callback.
244  * @domain: a #GQuark.
245  * @code: an error code.
246  * @format: a string with format characters.
247  * @...: a list of values to insert into @format.
248  * 
249  * Creates a new #GSimpleAsyncResult with a set error.
250  * 
251  * Returns: a #GSimpleAsyncResult.
252  **/
253 GSimpleAsyncResult *
254 g_simple_async_result_new_error (GObject             *source_object,
255                                  GAsyncReadyCallback  callback,
256                                  gpointer             user_data,
257                                  GQuark               domain,
258                                  gint                 code,
259                                  const char          *format,
260                                  ...)
261 {
262   GSimpleAsyncResult *simple;
263   va_list args;
264   
265   g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
266   g_return_val_if_fail (domain != 0, NULL);
267   g_return_val_if_fail (format != NULL, NULL);
268
269   simple = g_simple_async_result_new (source_object,
270                                       callback,
271                                       user_data, NULL);
272
273   va_start (args, format);
274   g_simple_async_result_set_error_va (simple, domain, code, format, args);
275   va_end (args);
276   
277   return simple;
278 }
279
280
281 static gpointer
282 g_simple_async_result_get_user_data (GAsyncResult *res)
283 {
284   return G_SIMPLE_ASYNC_RESULT (res)->user_data;
285 }
286
287 static GObject *
288 g_simple_async_result_get_source_object (GAsyncResult *res)
289 {
290   if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
291     return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
292   return NULL;
293 }
294
295 static void
296 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
297 {
298   iface->get_user_data = g_simple_async_result_get_user_data;
299   iface->get_source_object = g_simple_async_result_get_source_object;
300 }
301
302 /**
303  * g_simple_async_result_set_handle_cancellation:
304  * @simple: a #GSimpleAsyncResult.
305  * @handle_cancellation: a #gboolean.
306  * 
307  * Sets whether to handle cancellation within the asynchronous operation.
308  * 
309  **/
310 void
311 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
312                                                gboolean            handle_cancellation)
313 {
314   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
315   simple->handle_cancellation = handle_cancellation;
316 }
317
318 /**
319  * g_simple_async_result_get_source_tag:
320  * @simple: a #GSimpleAsyncResult.
321  * 
322  * Gets the source tag for the #GSimpleAsyncResult.
323  * 
324  * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
325  **/
326 gpointer
327 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
328 {
329   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
330   return simple->source_tag;
331 }
332
333 /**
334  * g_simple_async_result_propagate_error:
335  * @simple: a #GSimpleAsyncResult.
336  * @dest: a location to propegate the error to.
337  * 
338  * Propagates an error from within the simple asynchronous result to
339  * a given destination.
340  * 
341  * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
342  **/
343 gboolean
344 g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
345                                        GError             **dest)
346 {
347   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
348
349   if (simple->failed)
350     {
351       g_propagate_error (dest, simple->error);
352       simple->error = NULL;
353       return TRUE;
354     }
355
356   return FALSE;
357 }
358
359 /**
360  * g_simple_async_result_set_op_res_gpointer:
361  * @simple: a #GSimpleAsyncResult.
362  * @op_res: a pointer result from an asynchronous function.
363  * @destroy_op_res: a #GDestroyNotify function.
364  * 
365  * Sets the operation result within the asynchronous result to a pointer.
366  **/
367 void
368 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
369                                            gpointer            op_res,
370                                            GDestroyNotify      destroy_op_res)
371 {
372   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
373
374   simple->op_res.v_pointer = op_res;
375   simple->destroy_op_res = destroy_op_res;
376 }
377
378 /**
379  * g_simple_async_result_get_op_res_gpointer:
380  * @simple: a #GSimpleAsyncResult.
381  * 
382  * Gets a pointer result as returned by the asynchronous function.
383  * 
384  * Returns: a pointer from the result.
385  **/
386 gpointer
387 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
388 {
389   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
390   return simple->op_res.v_pointer;
391 }
392
393 /**
394  * g_simple_async_result_set_op_res_gssize:
395  * @simple: a #GSimpleAsyncResult.
396  * @op_res: a #gssize.
397  * 
398  * Sets the operation result within the asynchronous result to 
399  * the given @op_res. 
400  **/
401 void
402 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
403                                          gssize              op_res)
404 {
405   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
406   simple->op_res.v_ssize = op_res;
407 }
408
409 /**
410  * g_simple_async_result_get_op_res_gssize:
411  * @simple: a #GSimpleAsyncResult.
412  * 
413  * Gets a gssize from the asynchronous result.
414  * 
415  * Returns: a gssize returned from the asynchronous function.
416  **/
417 gssize
418 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
419 {
420   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
421   return simple->op_res.v_ssize;
422 }
423
424 /**
425  * g_simple_async_result_set_op_res_gboolean:
426  * @simple: a #GSimpleAsyncResult.
427  * @op_res: a #gboolean.
428  * 
429  * Sets the operation result to a boolean within the asynchronous result.
430  **/
431 void
432 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
433                                            gboolean            op_res)
434 {
435   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
436   simple->op_res.v_boolean = !!op_res;
437 }
438
439 /**
440  * g_simple_async_result_get_op_res_gboolean:
441  * @simple: a #GSimpleAsyncResult.
442  * 
443  * Gets the operation result boolean from within the asynchronous result.
444  * 
445  * Returns: %TRUE if the operation's result was %TRUE, %FALSE 
446  *     if the operation's result was %FALSE. 
447  **/
448 gboolean
449 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
450 {
451   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
452   return simple->op_res.v_boolean;
453 }
454
455 /**
456  * g_simple_async_result_set_from_error:
457  * @simple: a #GSimpleAsyncResult.
458  * @error: #GError.
459  * 
460  * Sets the result from a #GError. 
461  **/
462 void
463 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
464                                       GError             *error)
465 {
466   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
467   g_return_if_fail (error != NULL);
468
469   if (simple->error)
470     g_error_free (simple->error);
471   simple->error = g_error_copy (error);
472   simple->failed = TRUE;
473 }
474
475 static GError* 
476 _g_error_new_valist (GQuark     domain,
477                     gint        code,
478                     const char *format,
479                     va_list     args)
480 {
481   GError *error;
482   char *message;
483
484   message = g_strdup_vprintf (format, args);
485
486   error = g_error_new_literal (domain, code, message);
487   g_free (message);
488   
489   return error;
490 }
491
492 /**
493  * g_simple_async_result_set_error_va:
494  * @simple: a #GSimpleAsyncResult.
495  * @domain: a #GQuark (usually #G_IO_ERROR).
496  * @code: an error code.
497  * @format: a formatted error reporting string.
498  * @args: va_list of arguments. 
499  * 
500  * Sets an error within the asynchronous result without a #GError. 
501  * Unless writing a binding, see g_simple_async_result_set_error().
502  **/
503 void
504 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
505                                     GQuark              domain,
506                                     gint                code,
507                                     const char         *format,
508                                     va_list             args)
509 {
510   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
511   g_return_if_fail (domain != 0);
512   g_return_if_fail (format != NULL);
513
514   if (simple->error)
515     g_error_free (simple->error);
516   simple->error = _g_error_new_valist (domain, code, format, args);
517   simple->failed = TRUE;
518 }
519
520 /**
521  * g_simple_async_result_set_error:
522  * @simple: a #GSimpleAsyncResult.
523  * @domain: a #GQuark (usually #G_IO_ERROR).
524  * @code: an error code.
525  * @format: a formatted error reporting string.
526  * @...: a list of variables to fill in @format.
527  * 
528  * Sets an error within the asynchronous result without a #GError.
529  **/
530 void
531 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
532                                  GQuark              domain,
533                                  gint                code,
534                                  const char         *format,
535                                  ...)
536 {
537   va_list args;
538
539   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
540   g_return_if_fail (domain != 0);
541   g_return_if_fail (format != NULL);
542
543   va_start (args, format);
544   g_simple_async_result_set_error_va (simple, domain, code, format, args);
545   va_end (args);
546 }
547
548 /**
549  * g_simple_async_result_complete:
550  * @simple: a #GSimpleAsyncResult.
551  * 
552  * Completes an asynchronous I/O job.
553  * Must be called in the main thread, as it invokes the callback that
554  * should be called in the main thread. If you are in a different thread
555  * use g_simple_async_result_complete_in_idle().
556  **/
557 void
558 g_simple_async_result_complete (GSimpleAsyncResult *simple)
559 {
560   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
561
562   if (simple->callback)
563     simple->callback (simple->source_object,
564                       G_ASYNC_RESULT (simple),
565                       simple->user_data);
566 }
567
568 static gboolean
569 complete_in_idle_cb (gpointer data)
570 {
571   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
572
573   g_simple_async_result_complete (simple);
574
575   return FALSE;
576 }
577
578 /**
579  * g_simple_async_result_complete_in_idle:
580  * @simple: a #GSimpleAsyncResult.
581  * 
582  * Completes an asynchronous function in the main event loop using 
583  * an idle function.
584  **/
585 void
586 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
587 {
588   GSource *source;
589   guint id;
590   
591   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
592   
593   g_object_ref (simple);
594   
595   source = g_idle_source_new ();
596   g_source_set_priority (source, G_PRIORITY_DEFAULT);
597   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
598
599   id = g_source_attach (source, NULL);
600   g_source_unref (source);
601 }
602
603 typedef struct {
604   GSimpleAsyncResult *simple;
605   GCancellable *cancellable;
606   GSimpleAsyncThreadFunc func;
607 } RunInThreadData;
608
609
610 static gboolean
611 complete_in_idle_cb_for_thread (gpointer _data)
612 {
613   RunInThreadData *data = _data;
614   GSimpleAsyncResult *simple;
615
616   simple = data->simple;
617   
618   if (simple->handle_cancellation &&
619       g_cancellable_is_cancelled (data->cancellable))
620     g_simple_async_result_set_error (simple,
621                                      G_IO_ERROR,
622                                      G_IO_ERROR_CANCELLED,
623                                      "%s", _("Operation was cancelled"));
624   
625   g_simple_async_result_complete (simple);
626
627   if (data->cancellable)
628     g_object_unref (data->cancellable);
629   g_object_unref (data->simple);
630   g_free (data);
631   
632   return FALSE;
633 }
634
635 static gboolean
636 run_in_thread (GIOSchedulerJob *job,
637                GCancellable    *c,
638                gpointer         _data)
639 {
640   RunInThreadData *data = _data;
641   GSimpleAsyncResult *simple = data->simple;
642   GSource *source;
643   guint id;
644   
645   if (simple->handle_cancellation &&
646       g_cancellable_is_cancelled (c))
647     g_simple_async_result_set_error (simple,
648                                      G_IO_ERROR,
649                                      G_IO_ERROR_CANCELLED,
650                                      "%s", _("Operation was cancelled"));
651   else
652     data->func (simple,
653                 simple->source_object,
654                 c);
655
656   source = g_idle_source_new ();
657   g_source_set_priority (source, G_PRIORITY_DEFAULT);
658   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
659
660   id = g_source_attach (source, NULL);
661   g_source_unref (source);
662
663   return FALSE;
664 }
665
666 /**
667  * g_simple_async_result_run_in_thread:
668  * @simple: a #GSimpleAsyncResult.
669  * @func: a #GSimpleAsyncThreadFunc.
670  * @io_priority: the io priority of the request.
671  * @cancellable: optional #GCancellable object, %NULL to ignore. 
672  * 
673  * Runs the asynchronous job in a separated thread.
674  **/
675 void
676 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
677                                      GSimpleAsyncThreadFunc  func,
678                                      int                     io_priority, 
679                                      GCancellable           *cancellable)
680 {
681   RunInThreadData *data;
682   
683   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
684   g_return_if_fail (func != NULL);
685
686   data = g_new (RunInThreadData, 1);
687   data->func = func;
688   data->simple = g_object_ref (simple);
689   data->cancellable = cancellable;
690   if (cancellable)
691     g_object_ref (cancellable);
692   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
693 }
694
695 /**
696  * g_simple_async_result_is_valid:
697  * @result: the #GAsyncResult passed to the _finish function.
698  * @source: the #GObject passed to the _finish function.
699  * @source_tag: the asynchronous function.
700  *
701  * Ensures that the data passed to the _finish function of an async
702  * operation is consistent.  Three checks are performed.
703  * 
704  * First, @result is checked to ensure that it is really a
705  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
706  * matches the source object of @result.  Third, @source_tag is
707  * checked to ensure that it is equal to the source_tag argument given
708  * to g_simple_async_result_new() (which, by convention, is a pointer
709  * to the _async function corresponding to the _finish function from
710  * which this function is called).
711  *
712  * Returns: #TRUE if all checks passed or #FALSE if any failed.
713  **/ 
714 gboolean
715 g_simple_async_result_is_valid (GAsyncResult *result,
716                                 GObject      *source,
717                                 gpointer      source_tag)
718 {
719   GSimpleAsyncResult *simple;
720   GObject *cmp_source;
721
722   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
723     return FALSE;
724   simple = (GSimpleAsyncResult *)result;
725
726   cmp_source = g_async_result_get_source_object (result);
727   if (cmp_source != source)
728     {
729       g_object_unref (cmp_source);
730       return FALSE;
731     }
732   g_object_unref (cmp_source);
733
734   return source_tag == g_simple_async_result_get_source_tag (simple);
735 }
736
737 /**
738  * g_simple_async_report_error_in_idle:
739  * @object: a #GObject.
740  * @callback: a #GAsyncReadyCallback. 
741  * @user_data: user data passed to @callback.
742  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
743  * @code: a specific error code.
744  * @format: a formatted error reporting string.
745  * @...: a list of variables to fill in @format.
746  * 
747  * Reports an error in an asynchronous function in an idle function by 
748  * directly setting the contents of the #GAsyncResult with the given error
749  * information.
750  **/
751 void
752 g_simple_async_report_error_in_idle (GObject             *object,
753                                      GAsyncReadyCallback  callback,
754                                      gpointer             user_data,
755                                      GQuark               domain,
756                                      gint                 code,
757                                      const char          *format,
758                                      ...)
759 {
760   GSimpleAsyncResult *simple;
761   va_list args;
762   
763   g_return_if_fail (G_IS_OBJECT (object));
764   g_return_if_fail (domain != 0);
765   g_return_if_fail (format != NULL);
766
767   simple = g_simple_async_result_new (object,
768                                       callback,
769                                       user_data, NULL);
770
771   va_start (args, format);
772   g_simple_async_result_set_error_va (simple, domain, code, format, args);
773   va_end (args);
774   g_simple_async_result_complete_in_idle (simple);
775   g_object_unref (simple);
776 }
777
778 /**
779  * g_simple_async_report_gerror_in_idle:
780  * @object: a #GObject.
781  * @callback: a #GAsyncReadyCallback. 
782  * @user_data: user data passed to @callback.
783  * @error: the #GError to report
784  * 
785  * Reports an error in an idle function. Similar to 
786  * g_simple_async_report_error_in_idle(), but takes a #GError rather 
787  * than building a new one.
788  **/
789 void
790 g_simple_async_report_gerror_in_idle (GObject *object,
791                                       GAsyncReadyCallback callback,
792                                       gpointer user_data,
793                                       GError *error)
794 {
795   GSimpleAsyncResult *simple;
796   
797   g_return_if_fail (G_IS_OBJECT (object));
798   g_return_if_fail (error != NULL);
799
800   simple = g_simple_async_result_new_from_error (object,
801                                                  callback,
802                                                  user_data,
803                                                  error);
804   g_simple_async_result_complete_in_idle (simple);
805   g_object_unref (simple);
806 }
807
808 #define __G_SIMPLE_ASYNC_RESULT_C__
809 #include "gioaliasdef.c"