1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
32 #include "gsimpleasyncresult.h"
33 #include "gioscheduler.h"
34 #include <gio/gioerror.h>
37 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
39 struct _GSimpleAsyncResult
41 GObject parent_instance;
43 GObject *source_object;
44 GAsyncReadyCallback callback;
48 gboolean handle_cancellation;
58 GDestroyNotify destroy_op_res;
61 struct _GSimpleAsyncResultClass
63 GObjectClass parent_class;
67 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
69 g_simple_async_result_async_result_iface_init))
72 g_simple_async_result_finalize (GObject *object)
74 GSimpleAsyncResult *simple;
76 simple = G_SIMPLE_ASYNC_RESULT (object);
78 if (simple->source_object)
79 g_object_unref (simple->source_object);
81 if (simple->destroy_op_res)
82 simple->destroy_op_res (simple->op_res.v_pointer);
85 g_error_free (simple->error);
87 if (G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize)
88 (*G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize) (object);
92 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = g_simple_async_result_finalize;
100 g_simple_async_result_init (GSimpleAsyncResult *simple)
102 simple->handle_cancellation = TRUE;
106 * g_simple_async_result_new:
112 * Returns: #GSimpleAsyncResult
115 g_simple_async_result_new (GObject *source_object,
116 GAsyncReadyCallback callback,
120 GSimpleAsyncResult *simple;
122 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
124 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
125 simple->callback = callback;
126 simple->source_object = g_object_ref (source_object);
127 simple->user_data = user_data;
128 simple->source_tag = source_tag;
134 * g_simple_async_result_new_from_error:
138 * @error: a #GError location to store the error occuring, or %NULL to
140 * Returns: #GSimpleAsyncResult
143 g_simple_async_result_new_from_error (GObject *source_object,
144 GAsyncReadyCallback callback,
148 GSimpleAsyncResult *simple;
150 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
152 simple = g_simple_async_result_new (source_object,
155 g_simple_async_result_set_from_error (simple, error);
161 * g_simple_async_result_new_error:
170 * Returns: #GSimpleAsyncResult.
173 g_simple_async_result_new_error (GObject *source_object,
174 GAsyncReadyCallback callback,
181 GSimpleAsyncResult *simple;
184 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
185 g_return_val_if_fail (domain != 0, NULL);
186 g_return_val_if_fail (format != NULL, NULL);
188 simple = g_simple_async_result_new (source_object,
192 va_start (args, format);
193 g_simple_async_result_set_error_va (simple, domain, code, format, args);
201 g_simple_async_result_get_user_data (GAsyncResult *res)
203 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
207 g_simple_async_result_get_source_object (GAsyncResult *res)
209 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
210 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
215 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
217 iface->get_user_data = g_simple_async_result_get_user_data;
218 iface->get_source_object = g_simple_async_result_get_source_object;
222 * g_simple_async_result_set_handle_cancellation:
224 * @handle_cancellation:
228 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
229 gboolean handle_cancellation)
231 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
232 simple->handle_cancellation = handle_cancellation;
236 * g_simple_async_result_get_source_tag:
242 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
244 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
245 return simple->source_tag;
249 * g_simple_async_result_result_propagate_error:
256 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
259 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
263 g_propagate_error (dest, simple->error);
264 simple->error = NULL;
271 * g_simple_async_result_set_op_res_gpointer:
278 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
280 GDestroyNotify destroy_op_res)
282 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
284 simple->op_res.v_pointer = op_res;
285 simple->destroy_op_res = destroy_op_res;
289 * g_simple_async_result_get_op_res_gpointer:
295 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
297 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
298 return simple->op_res.v_pointer;
302 * g_simple_async_result_set_op_res_gssize:
308 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
311 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
312 simple->op_res.v_ssize = op_res;
316 * g_simple_async_result_get_op_res_gssize:
322 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
324 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
325 return simple->op_res.v_ssize;
329 * g_simple_async_result_set_op_res_gboolean:
335 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
338 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
339 simple->op_res.v_boolean = !!op_res;
343 * g_simple_async_result_get_op_res_gboolean:
346 * Returns a #gboolean.
349 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
351 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
352 return simple->op_res.v_boolean;
356 * g_simple_async_result_set_from_error:
360 * Sets the result from given @error.
364 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
367 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
368 g_return_if_fail (error != NULL);
370 simple->error = g_error_copy (error);
371 simple->failed = TRUE;
375 _g_error_new_valist (GQuark domain,
383 message = g_strdup_vprintf (format, args);
385 error = g_error_new_literal (domain, code, message);
392 * g_simple_async_result_set_error_va:
397 * @args: va_list of arguments.
399 * Sets error va_list, suitable for language bindings.
403 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
409 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
410 g_return_if_fail (domain != 0);
411 g_return_if_fail (format != NULL);
413 simple->error = _g_error_new_valist (domain, code, format, args);
414 simple->failed = TRUE;
418 * g_simple_async_result_set_error:
427 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
435 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
436 g_return_if_fail (domain != 0);
437 g_return_if_fail (format != NULL);
439 va_start (args, format);
440 g_simple_async_result_set_error_va (simple, domain, code, format, args);
445 * g_simple_async_result_complete:
450 g_simple_async_result_complete (GSimpleAsyncResult *simple)
452 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
454 if (simple->callback)
455 simple->callback (simple->source_object,
456 G_ASYNC_RESULT (simple),
461 complete_in_idle_cb (gpointer data)
463 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
465 g_simple_async_result_complete (simple);
471 * g_simple_async_result_complete_in_idle:
476 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
481 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
483 g_object_ref (simple);
485 source = g_idle_source_new ();
486 g_source_set_priority (source, G_PRIORITY_DEFAULT);
487 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
489 id = g_source_attach (source, NULL);
490 g_source_unref (source);
494 GSimpleAsyncResult *simple;
495 GSimpleAsyncThreadFunc func;
499 run_in_thread (GIOJob *job,
503 RunInThreadData *data = _data;
504 GSimpleAsyncResult *simple = data->simple;
506 if (simple->handle_cancellation &&
507 g_cancellable_is_cancelled (c))
509 g_simple_async_result_set_error (simple,
511 G_IO_ERROR_CANCELLED,
512 _("Operation was cancelled"));
517 simple->source_object,
521 g_simple_async_result_complete_in_idle (data->simple);
522 g_object_unref (data->simple);
527 * g_simple_async_result_run_in_thread:
530 * @io_priority: the io priority of the request.
531 * @cancellable: optional #GCancellable object, %NULL to ignore.
534 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
535 GSimpleAsyncThreadFunc func,
537 GCancellable *cancellable)
539 RunInThreadData *data;
541 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
542 g_return_if_fail (func != NULL);
544 data = g_new (RunInThreadData, 1);
546 data->simple = g_object_ref (simple);
547 g_schedule_io_job (run_in_thread, data, NULL, io_priority, cancellable);
551 * g_simple_async_report_error_in_idle:
562 g_simple_async_report_error_in_idle (GObject *object,
563 GAsyncReadyCallback callback,
570 GSimpleAsyncResult *simple;
573 g_return_if_fail (G_IS_OBJECT (object));
574 g_return_if_fail (domain != 0);
575 g_return_if_fail (format != NULL);
577 simple = g_simple_async_result_new (object,
581 va_start (args, format);
582 g_simple_async_result_set_error_va (simple, domain, code, format, args);
584 g_simple_async_result_complete_in_idle (simple);
585 g_object_unref (simple);