Blame gio/gsimpleasyncresult.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gsimpleasyncresult.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "gioscheduler.h"
Packit ae235b
#include <gio/gioerror.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gsimpleasyncresult
Packit ae235b
 * @short_description: Simple asynchronous results implementation
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GAsyncResult, #GTask
Packit ae235b
 *
Packit ae235b
 * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
Packit ae235b
 * #GTask, which provides a simpler API.
Packit ae235b
 *
Packit ae235b
 * #GSimpleAsyncResult implements #GAsyncResult.
Packit ae235b
 *
Packit ae235b
 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
Packit ae235b
 * reporting, operation cancellation and the final state of an operation,
Packit ae235b
 * completely transparent to the application. Results can be returned
Packit ae235b
 * as a pointer e.g. for functions that return data that is collected
Packit ae235b
 * asynchronously, a boolean value for checking the success or failure
Packit ae235b
 * of an operation, or a #gssize for operations which return the number
Packit ae235b
 * of bytes modified by the operation; all of the simple return cases
Packit ae235b
 * are covered.
Packit ae235b
 *
Packit ae235b
 * Most of the time, an application will not need to know of the details
Packit ae235b
 * of this API; it is handled transparently, and any necessary operations
Packit ae235b
 * are handled by #GAsyncResult's interface. However, if implementing a
Packit ae235b
 * new GIO module, for writing language bindings, or for complex
Packit ae235b
 * applications that need better control of how asynchronous operations
Packit ae235b
 * are completed, it is important to understand this functionality.
Packit ae235b
 *
Packit ae235b
 * GSimpleAsyncResults are tagged with the calling function to ensure
Packit ae235b
 * that asynchronous functions and their finishing functions are used
Packit ae235b
 * together correctly.
Packit ae235b
 *
Packit ae235b
 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
Packit ae235b
 * If the result needs to be created for a #GError, use
Packit ae235b
 * g_simple_async_result_new_from_error() or
Packit ae235b
 * g_simple_async_result_new_take_error(). If a #GError is not available
Packit ae235b
 * (e.g. the asynchronous operation's doesn't take a #GError argument),
Packit ae235b
 * but the result still needs to be created for an error condition, use
Packit ae235b
 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
Packit ae235b
 * if your application or binding requires passing a variable argument list
Packit ae235b
 * directly), and the error can then be propagated through the use of
Packit ae235b
 * g_simple_async_result_propagate_error().
Packit ae235b
 *
Packit ae235b
 * An asynchronous operation can be made to ignore a cancellation event by
Packit ae235b
 * calling g_simple_async_result_set_handle_cancellation() with a
Packit ae235b
 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
Packit ae235b
 * operations that are dangerous to cancel, such as close (which would
Packit ae235b
 * cause a leak if cancelled before being run).
Packit ae235b
 *
Packit ae235b
 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
Packit ae235b
 * or it can use #GThreads.
Packit ae235b
 * g_simple_async_result_complete() will finish an I/O task directly
Packit ae235b
 * from the point where it is called. g_simple_async_result_complete_in_idle()
Packit ae235b
 * will finish it from an idle handler in the 
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * where the #GSimpleAsyncResult was created.
Packit ae235b
 * g_simple_async_result_run_in_thread() will run the job in a
Packit ae235b
 * separate thread and then use
Packit ae235b
 * g_simple_async_result_complete_in_idle() to deliver the result.
Packit ae235b
 *
Packit ae235b
 * To set the results of an asynchronous function,
Packit ae235b
 * g_simple_async_result_set_op_res_gpointer(),
Packit ae235b
 * g_simple_async_result_set_op_res_gboolean(), and
Packit ae235b
 * g_simple_async_result_set_op_res_gssize()
Packit ae235b
 * are provided, setting the operation's result to a gpointer, gboolean, or
Packit ae235b
 * gssize, respectively.
Packit ae235b
 *
Packit ae235b
 * Likewise, to get the result of an asynchronous function,
Packit ae235b
 * g_simple_async_result_get_op_res_gpointer(),
Packit ae235b
 * g_simple_async_result_get_op_res_gboolean(), and
Packit ae235b
 * g_simple_async_result_get_op_res_gssize() are
Packit ae235b
 * provided, getting the operation's result as a gpointer, gboolean, and
Packit ae235b
 * gssize, respectively.
Packit ae235b
 *
Packit ae235b
 * For the details of the requirements implementations must respect, see
Packit ae235b
 * #GAsyncResult.  A typical implementation of an asynchronous operation
Packit ae235b
 * using GSimpleAsyncResult looks something like this:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * static void
Packit ae235b
 * baked_cb (Cake    *cake,
Packit ae235b
 *           gpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   // In this example, this callback is not given a reference to the cake,
Packit ae235b
 *   // so the GSimpleAsyncResult has to take a reference to it.
Packit ae235b
 *   GSimpleAsyncResult *result = user_data;
Packit ae235b
 *
Packit ae235b
 *   if (cake == NULL)
Packit ae235b
 *     g_simple_async_result_set_error (result,
Packit ae235b
 *                                      BAKER_ERRORS,
Packit ae235b
 *                                      BAKER_ERROR_NO_FLOUR,
Packit ae235b
 *                                      "Go to the supermarket");
Packit ae235b
 *   else
Packit ae235b
 *     g_simple_async_result_set_op_res_gpointer (result,
Packit ae235b
 *                                                g_object_ref (cake),
Packit ae235b
 *                                                g_object_unref);
Packit ae235b
 *
Packit ae235b
 *
Packit ae235b
 *   // In this example, we assume that baked_cb is called as a callback from
Packit ae235b
 *   // the mainloop, so it's safe to complete the operation synchronously here.
Packit ae235b
 *   // If, however, _baker_prepare_cake () might call its callback without
Packit ae235b
 *   // first returning to the mainloop — inadvisable, but some APIs do so —
Packit ae235b
 *   // we would need to use g_simple_async_result_complete_in_idle().
Packit ae235b
 *   g_simple_async_result_complete (result);
Packit ae235b
 *   g_object_unref (result);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * void
Packit ae235b
 * baker_bake_cake_async (Baker              *self,
Packit ae235b
 *                        guint               radius,
Packit ae235b
 *                        GAsyncReadyCallback callback,
Packit ae235b
 *                        gpointer            user_data)
Packit ae235b
 * {
Packit ae235b
 *   GSimpleAsyncResult *simple;
Packit ae235b
 *   Cake               *cake;
Packit ae235b
 *
Packit ae235b
 *   if (radius < 3)
Packit ae235b
 *     {
Packit ae235b
 *       g_simple_async_report_error_in_idle (G_OBJECT (self),
Packit ae235b
 *                                            callback,
Packit ae235b
 *                                            user_data,
Packit ae235b
 *                                            BAKER_ERRORS,
Packit ae235b
 *                                            BAKER_ERROR_TOO_SMALL,
Packit ae235b
 *                                            "%ucm radius cakes are silly",
Packit ae235b
 *                                            radius);
Packit ae235b
 *       return;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   simple = g_simple_async_result_new (G_OBJECT (self),
Packit ae235b
 *                                       callback,
Packit ae235b
 *                                       user_data,
Packit ae235b
 *                                       baker_bake_cake_async);
Packit ae235b
 *   cake = _baker_get_cached_cake (self, radius);
Packit ae235b
 *
Packit ae235b
 *   if (cake != NULL)
Packit ae235b
 *     {
Packit ae235b
 *       g_simple_async_result_set_op_res_gpointer (simple,
Packit ae235b
 *                                                  g_object_ref (cake),
Packit ae235b
 *                                                  g_object_unref);
Packit ae235b
 *       g_simple_async_result_complete_in_idle (simple);
Packit ae235b
 *       g_object_unref (simple);
Packit ae235b
 *       // Drop the reference returned by _baker_get_cached_cake();
Packit ae235b
 *       // the GSimpleAsyncResult has taken its own reference.
Packit ae235b
 *       g_object_unref (cake);
Packit ae235b
 *       return;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   _baker_prepare_cake (self, radius, baked_cb, simple);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * Cake *
Packit ae235b
 * baker_bake_cake_finish (Baker        *self,
Packit ae235b
 *                         GAsyncResult *result,
Packit ae235b
 *                         GError      **error)
Packit ae235b
 * {
Packit ae235b
 *   GSimpleAsyncResult *simple;
Packit ae235b
 *   Cake               *cake;
Packit ae235b
 *
Packit ae235b
 *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
Packit ae235b
 *                                                         G_OBJECT (self),
Packit ae235b
 *                                                         baker_bake_cake_async),
Packit ae235b
 *                         NULL);
Packit ae235b
 *
Packit ae235b
 *   simple = (GSimpleAsyncResult *) result;
Packit ae235b
 *
Packit ae235b
 *   if (g_simple_async_result_propagate_error (simple, error))
Packit ae235b
 *     return NULL;
Packit ae235b
 *
Packit ae235b
 *   cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
Packit ae235b
 *   return g_object_ref (cake);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
Packit ae235b
static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
Packit ae235b
Packit ae235b
struct _GSimpleAsyncResult
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  GObject *source_object;
Packit ae235b
  GAsyncReadyCallback callback;
Packit ae235b
  gpointer user_data;
Packit ae235b
  GMainContext *context;
Packit ae235b
  GError *error;
Packit ae235b
  gboolean failed;
Packit ae235b
  gboolean handle_cancellation;
Packit ae235b
  GCancellable *check_cancellable;
Packit ae235b
Packit ae235b
  gpointer source_tag;
Packit ae235b
Packit ae235b
  union {
Packit ae235b
    gpointer v_pointer;
Packit ae235b
    gboolean v_boolean;
Packit ae235b
    gssize   v_ssize;
Packit ae235b
  } op_res;
Packit ae235b
Packit ae235b
  GDestroyNotify destroy_op_res;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GSimpleAsyncResultClass
Packit ae235b
{
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
Packit ae235b
						g_simple_async_result_async_result_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
clear_op_res (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  if (simple->destroy_op_res)
Packit ae235b
    simple->destroy_op_res (simple->op_res.v_pointer);
Packit ae235b
  simple->destroy_op_res = NULL;
Packit ae235b
  simple->op_res.v_ssize = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_async_result_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  simple = G_SIMPLE_ASYNC_RESULT (object);
Packit ae235b
Packit ae235b
  if (simple->source_object)
Packit ae235b
    g_object_unref (simple->source_object);
Packit ae235b
Packit ae235b
  if (simple->check_cancellable)
Packit ae235b
    g_object_unref (simple->check_cancellable);
Packit ae235b
Packit ae235b
  g_main_context_unref (simple->context);
Packit ae235b
Packit ae235b
  clear_op_res (simple);
Packit ae235b
Packit ae235b
  if (simple->error)
Packit ae235b
    g_error_free (simple->error);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
 
Packit ae235b
  gobject_class->finalize = g_simple_async_result_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_async_result_init (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  simple->handle_cancellation = TRUE;
Packit ae235b
Packit ae235b
  simple->context = g_main_context_ref_thread_default ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_new:
Packit ae235b
 * @source_object: (nullable): a #GObject, or %NULL.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: (closure): user data passed to @callback.
Packit ae235b
 * @source_tag: the asynchronous function.
Packit ae235b
 *
Packit ae235b
 * Creates a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * The common convention is to create the #GSimpleAsyncResult in the
Packit ae235b
 * function that starts the asynchronous operation and use that same
Packit ae235b
 * function as the @source_tag.
Packit ae235b
 *
Packit ae235b
 * If your operation supports cancellation with #GCancellable (which it
Packit ae235b
 * probably should) then you should provide the user's cancellable to
Packit ae235b
 * g_simple_async_result_set_check_cancellable() immediately after
Packit ae235b
 * this function returns.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_new() instead.
Packit ae235b
 **/
Packit ae235b
GSimpleAsyncResult *
Packit ae235b
g_simple_async_result_new (GObject             *source_object,
Packit ae235b
                           GAsyncReadyCallback  callback,
Packit ae235b
                           gpointer             user_data,
Packit ae235b
                           gpointer             source_tag)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
Packit ae235b
Packit ae235b
  simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
Packit ae235b
  simple->callback = callback;
Packit ae235b
  if (source_object)
Packit ae235b
    simple->source_object = g_object_ref (source_object);
Packit ae235b
  else
Packit ae235b
    simple->source_object = NULL;
Packit ae235b
  simple->user_data = user_data;
Packit ae235b
  simple->source_tag = source_tag;
Packit ae235b
 
Packit ae235b
  return simple;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_new_from_error:
Packit ae235b
 * @source_object: (nullable): a #GObject, or %NULL.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: (closure): user data passed to @callback.
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Creates a #GSimpleAsyncResult from an error condition.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
Packit ae235b
 **/
Packit ae235b
GSimpleAsyncResult *
Packit ae235b
g_simple_async_result_new_from_error (GObject             *source_object,
Packit ae235b
                                      GAsyncReadyCallback  callback,
Packit ae235b
                                      gpointer             user_data,
Packit ae235b
                                      const GError        *error)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new (source_object,
Packit ae235b
				      callback,
Packit ae235b
				      user_data, NULL);
Packit ae235b
  g_simple_async_result_set_from_error (simple, error);
Packit ae235b
Packit ae235b
  return simple;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_new_take_error: (skip)
Packit ae235b
 * @source_object: (nullable): a #GObject, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): user data passed to @callback
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
Packit ae235b
 * caller's ownership of @error, so the caller does not need to free it anymore.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSimpleAsyncResult
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
Packit ae235b
 **/
Packit ae235b
GSimpleAsyncResult *
Packit ae235b
g_simple_async_result_new_take_error (GObject             *source_object,
Packit ae235b
                                      GAsyncReadyCallback  callback,
Packit ae235b
                                      gpointer             user_data,
Packit ae235b
                                      GError              *error)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new (source_object,
Packit ae235b
				      callback,
Packit ae235b
				      user_data, NULL);
Packit ae235b
  g_simple_async_result_take_error (simple, error);
Packit ae235b
Packit ae235b
  return simple;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_new_error:
Packit ae235b
 * @source_object: (nullable): a #GObject, or %NULL.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: (closure): user data passed to @callback.
Packit ae235b
 * @domain: a #GQuark.
Packit ae235b
 * @code: an error code.
Packit ae235b
 * @format: a string with format characters.
Packit ae235b
 * @...: a list of values to insert into @format.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GSimpleAsyncResult with a set error.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
Packit ae235b
 **/
Packit ae235b
GSimpleAsyncResult *
Packit ae235b
g_simple_async_result_new_error (GObject             *source_object,
Packit ae235b
                                 GAsyncReadyCallback  callback,
Packit ae235b
                                 gpointer             user_data,
Packit ae235b
                                 GQuark               domain,
Packit ae235b
                                 gint                 code,
Packit ae235b
                                 const char          *format,
Packit ae235b
                                 ...)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
  va_list args;
Packit ae235b
 
Packit ae235b
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
Packit ae235b
  g_return_val_if_fail (domain != 0, NULL);
Packit ae235b
  g_return_val_if_fail (format != NULL, NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new (source_object,
Packit ae235b
				      callback,
Packit ae235b
				      user_data, NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
 
Packit ae235b
  return simple;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
g_simple_async_result_get_user_data (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  return G_SIMPLE_ASYNC_RESULT (res)->user_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GObject *
Packit ae235b
g_simple_async_result_get_source_object (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
Packit ae235b
    return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_simple_async_result_is_tagged (GAsyncResult *res,
Packit ae235b
				 gpointer      source_tag)
Packit ae235b
{
Packit ae235b
  return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_user_data = g_simple_async_result_get_user_data;
Packit ae235b
  iface->get_source_object = g_simple_async_result_get_source_object;
Packit ae235b
  iface->is_tagged = g_simple_async_result_is_tagged;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_handle_cancellation:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @handle_cancellation: a #gboolean.
Packit ae235b
 *
Packit ae235b
 * Sets whether to handle cancellation within the asynchronous operation.
Packit ae235b
 *
Packit ae235b
 * This function has nothing to do with
Packit ae235b
 * g_simple_async_result_set_check_cancellable().  It only refers to the
Packit ae235b
 * #GCancellable passed to g_simple_async_result_run_in_thread().
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
Packit ae235b
                                               gboolean            handle_cancellation)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  simple->handle_cancellation = handle_cancellation;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_get_source_tag: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Gets the source tag for the #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
Packit ae235b
 **/
Packit ae235b
gpointer
Packit ae235b
g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
Packit ae235b
  return simple->source_tag;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_propagate_error:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @dest: (out): a location to propagate the error to.
Packit ae235b
 *
Packit ae235b
 * Propagates an error from within the simple asynchronous result to
Packit ae235b
 * a given destination.
Packit ae235b
 *
Packit ae235b
 * If the #GCancellable given to a prior call to
Packit ae235b
 * g_simple_async_result_set_check_cancellable() is cancelled then this
Packit ae235b
 * function will return %TRUE with @dest set appropriately.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask instead.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
Packit ae235b
                                       GError             **dest)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
Packit ae235b
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (simple->failed)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (dest, simple->error);
Packit ae235b
      simple->error = NULL;
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_op_res_gpointer: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @op_res: a pointer result from an asynchronous function.
Packit ae235b
 * @destroy_op_res: a #GDestroyNotify function.
Packit ae235b
 *
Packit ae235b
 * Sets the operation result within the asynchronous result to a pointer.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
Packit ae235b
                                           gpointer            op_res,
Packit ae235b
                                           GDestroyNotify      destroy_op_res)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
Packit ae235b
  clear_op_res (simple);
Packit ae235b
  simple->op_res.v_pointer = op_res;
Packit ae235b
  simple->destroy_op_res = destroy_op_res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_get_op_res_gpointer: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Gets a pointer result as returned by the asynchronous function.
Packit ae235b
 *
Packit ae235b
 * Returns: a pointer from the result.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
Packit ae235b
 **/
Packit ae235b
gpointer
Packit ae235b
g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
Packit ae235b
  return simple->op_res.v_pointer;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_op_res_gssize:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @op_res: a #gssize.
Packit ae235b
 *
Packit ae235b
 * Sets the operation result within the asynchronous result to
Packit ae235b
 * the given @op_res.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
Packit ae235b
                                         gssize              op_res)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  clear_op_res (simple);
Packit ae235b
  simple->op_res.v_ssize = op_res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_get_op_res_gssize:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Gets a gssize from the asynchronous result.
Packit ae235b
 *
Packit ae235b
 * Returns: a gssize returned from the asynchronous function.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
Packit ae235b
 **/
Packit ae235b
gssize
Packit ae235b
g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
Packit ae235b
  return simple->op_res.v_ssize;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_op_res_gboolean:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @op_res: a #gboolean.
Packit ae235b
 *
Packit ae235b
 * Sets the operation result to a boolean within the asynchronous result.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
Packit ae235b
                                           gboolean            op_res)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  clear_op_res (simple);
Packit ae235b
  simple->op_res.v_boolean = !!op_res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_get_op_res_gboolean:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Gets the operation result boolean from within the asynchronous result.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
Packit ae235b
 *     if the operation's result was %FALSE.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
Packit ae235b
  return simple->op_res.v_boolean;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_from_error:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @error: #GError.
Packit ae235b
 *
Packit ae235b
 * Sets the result from a #GError.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
Packit ae235b
                                      const GError       *error)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  if (simple->error)
Packit ae235b
    g_error_free (simple->error);
Packit ae235b
  simple->error = g_error_copy (error);
Packit ae235b
  simple->failed = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_take_error: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Sets the result from @error, and takes over the caller's ownership
Packit ae235b
 * of @error, so the caller does not need to free it any more.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_take_error (GSimpleAsyncResult *simple,
Packit ae235b
                                  GError             *error)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  if (simple->error)
Packit ae235b
    g_error_free (simple->error);
Packit ae235b
  simple->error = error;
Packit ae235b
  simple->failed = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_error_va: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @domain: a #GQuark (usually #G_IO_ERROR).
Packit ae235b
 * @code: an error code.
Packit ae235b
 * @format: a formatted error reporting string.
Packit ae235b
 * @args: va_list of arguments.
Packit ae235b
 *
Packit ae235b
 * Sets an error within the asynchronous result without a #GError.
Packit ae235b
 * Unless writing a binding, see g_simple_async_result_set_error().
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
Packit ae235b
                                    GQuark              domain,
Packit ae235b
                                    gint                code,
Packit ae235b
                                    const char         *format,
Packit ae235b
                                    va_list             args)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (domain != 0);
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  if (simple->error)
Packit ae235b
    g_error_free (simple->error);
Packit ae235b
  simple->error = g_error_new_valist (domain, code, format, args);
Packit ae235b
  simple->failed = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_error: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @domain: a #GQuark (usually #G_IO_ERROR).
Packit ae235b
 * @code: an error code.
Packit ae235b
 * @format: a formatted error reporting string.
Packit ae235b
 * @...: a list of variables to fill in @format.
Packit ae235b
 *
Packit ae235b
 * Sets an error within the asynchronous result without a #GError.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_error (GSimpleAsyncResult *simple,
Packit ae235b
                                 GQuark              domain,
Packit ae235b
                                 gint                code,
Packit ae235b
                                 const char         *format,
Packit ae235b
                                 ...)
Packit ae235b
{
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (domain != 0);
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_complete:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Completes an asynchronous I/O job immediately. Must be called in
Packit ae235b
 * the thread where the asynchronous result was to be delivered, as it
Packit ae235b
 * invokes the callback directly. If you are in a different thread use
Packit ae235b
 * g_simple_async_result_complete_in_idle().
Packit ae235b
 *
Packit ae235b
 * Calling this function takes a reference to @simple for as long as
Packit ae235b
 * is needed to complete the call.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_complete (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
#ifndef G_DISABLE_CHECKS
Packit ae235b
  GSource *current_source;
Packit ae235b
  GMainContext *current_context;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_CHECKS
Packit ae235b
  current_source = g_main_current_source ();
Packit ae235b
  if (current_source && !g_source_is_destroyed (current_source))
Packit ae235b
    {
Packit ae235b
      current_context = g_source_get_context (current_source);
Packit ae235b
      if (simple->context != current_context)
Packit ae235b
	g_warning ("g_simple_async_result_complete() called from wrong context!");
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (simple->callback)
Packit ae235b
    {
Packit ae235b
      g_main_context_push_thread_default (simple->context);
Packit ae235b
      simple->callback (simple->source_object,
Packit ae235b
			G_ASYNC_RESULT (simple),
Packit ae235b
			simple->user_data);
Packit ae235b
      g_main_context_pop_thread_default (simple->context);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
complete_in_idle_cb (gpointer data)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
Packit ae235b
Packit ae235b
  g_simple_async_result_complete (simple);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_complete_in_idle:
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 *
Packit ae235b
 * Completes an asynchronous function in an idle handler in the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * of the thread that @simple was initially created in
Packit ae235b
 * (and re-pushes that context around the invocation of the callback).
Packit ae235b
 *
Packit ae235b
 * Calling this function takes a reference to @simple for as long as
Packit ae235b
 * is needed to complete the call.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask instead.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
Packit ae235b
  g_object_ref (simple);
Packit ae235b
 
Packit ae235b
  source = g_idle_source_new ();
Packit ae235b
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
Packit ae235b
  g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
Packit ae235b
  g_source_set_name (source, "[gio] complete_in_idle_cb");
Packit ae235b
Packit ae235b
  g_source_attach (source, simple->context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  GSimpleAsyncThreadFunc func;
Packit ae235b
} RunInThreadData;
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
complete_in_idle_cb_for_thread (gpointer _data)
Packit ae235b
{
Packit ae235b
  RunInThreadData *data = _data;
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  simple = data->simple;
Packit ae235b
 
Packit ae235b
  if (simple->handle_cancellation &&
Packit ae235b
      g_cancellable_is_cancelled (data->cancellable))
Packit ae235b
    g_simple_async_result_set_error (simple,
Packit ae235b
                                     G_IO_ERROR,
Packit ae235b
                                     G_IO_ERROR_CANCELLED,
Packit ae235b
                                     "%s", _("Operation was cancelled"));
Packit ae235b
 
Packit ae235b
  g_simple_async_result_complete (simple);
Packit ae235b
Packit ae235b
  if (data->cancellable)
Packit ae235b
    g_object_unref (data->cancellable);
Packit ae235b
  g_object_unref (data->simple);
Packit ae235b
  g_free (data);
Packit ae235b
 
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
run_in_thread (GIOSchedulerJob *job,
Packit ae235b
               GCancellable    *c,
Packit ae235b
               gpointer         _data)
Packit ae235b
{
Packit ae235b
  RunInThreadData *data = _data;
Packit ae235b
  GSimpleAsyncResult *simple = data->simple;
Packit ae235b
  GSource *source;
Packit ae235b
 
Packit ae235b
  if (simple->handle_cancellation &&
Packit ae235b
      g_cancellable_is_cancelled (c))
Packit ae235b
    g_simple_async_result_set_error (simple,
Packit ae235b
                                     G_IO_ERROR,
Packit ae235b
                                     G_IO_ERROR_CANCELLED,
Packit ae235b
                                     "%s", _("Operation was cancelled"));
Packit ae235b
  else
Packit ae235b
    data->func (simple,
Packit ae235b
                simple->source_object,
Packit ae235b
                c);
Packit ae235b
Packit ae235b
  source = g_idle_source_new ();
Packit ae235b
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
Packit ae235b
  g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
Packit ae235b
  g_source_set_name (source, "[gio] complete_in_idle_cb_for_thread");
Packit ae235b
Packit ae235b
  g_source_attach (source, simple->context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_run_in_thread: (skip)
Packit ae235b
 * @simple: a #GSimpleAsyncResult.
Packit ae235b
 * @func: a #GSimpleAsyncThreadFunc.
Packit ae235b
 * @io_priority: the io priority of the request.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Runs the asynchronous job in a separate thread and then calls
Packit ae235b
 * g_simple_async_result_complete_in_idle() on @simple to return
Packit ae235b
 * the result to the appropriate main loop.
Packit ae235b
 *
Packit ae235b
 * Calling this function takes a reference to @simple for as long as
Packit ae235b
 * is needed to run the job and report its completion.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
Packit ae235b
                                     GSimpleAsyncThreadFunc  func,
Packit ae235b
                                     int                     io_priority,
Packit ae235b
                                     GCancellable           *cancellable)
Packit ae235b
{
Packit ae235b
  RunInThreadData *data;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
  data = g_new (RunInThreadData, 1);
Packit ae235b
  data->func = func;
Packit ae235b
  data->simple = g_object_ref (simple);
Packit ae235b
  data->cancellable = cancellable;
Packit ae235b
  if (cancellable)
Packit ae235b
    g_object_ref (cancellable);
Packit ae235b
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
Packit ae235b
  g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
Packit ae235b
  G_GNUC_END_IGNORE_DEPRECATIONS;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_is_valid:
Packit ae235b
 * @result: the #GAsyncResult passed to the _finish function.
Packit ae235b
 * @source: (nullable): the #GObject passed to the _finish function.
Packit ae235b
 * @source_tag: (nullable): the asynchronous function.
Packit ae235b
 *
Packit ae235b
 * Ensures that the data passed to the _finish function of an async
Packit ae235b
 * operation is consistent.  Three checks are performed.
Packit ae235b
 *
Packit ae235b
 * First, @result is checked to ensure that it is really a
Packit ae235b
 * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
Packit ae235b
 * matches the source object of @result.  Third, @source_tag is
Packit ae235b
 * checked to ensure that it is equal to the @source_tag argument given
Packit ae235b
 * to g_simple_async_result_new() (which, by convention, is a pointer
Packit ae235b
 * to the _async function corresponding to the _finish function from
Packit ae235b
 * which this function is called).  (Alternatively, if either
Packit ae235b
 * @source_tag or @result's source tag is %NULL, then the source tag
Packit ae235b
 * check is skipped.)
Packit ae235b
 *
Packit ae235b
 * Returns: #TRUE if all checks passed or #FALSE if any failed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_simple_async_result_is_valid (GAsyncResult *result,
Packit ae235b
                                GObject      *source,
Packit ae235b
                                gpointer      source_tag)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
  GObject *cmp_source;
Packit ae235b
  gpointer result_source_tag;
Packit ae235b
Packit ae235b
  if (!G_IS_SIMPLE_ASYNC_RESULT (result))
Packit ae235b
    return FALSE;
Packit ae235b
  simple = (GSimpleAsyncResult *)result;
Packit ae235b
Packit ae235b
  cmp_source = g_async_result_get_source_object (result);
Packit ae235b
  if (cmp_source != source)
Packit ae235b
    {
Packit ae235b
      if (cmp_source != NULL)
Packit ae235b
        g_object_unref (cmp_source);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  if (cmp_source != NULL)
Packit ae235b
    g_object_unref (cmp_source);
Packit ae235b
Packit ae235b
  result_source_tag = g_simple_async_result_get_source_tag (simple);
Packit ae235b
  return source_tag == NULL || result_source_tag == NULL ||
Packit ae235b
         source_tag == result_source_tag;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_report_error_in_idle: (skip)
Packit ae235b
 * @object: (nullable): a #GObject, or %NULL.
Packit ae235b
 * @callback: a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: user data passed to @callback.
Packit ae235b
 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
Packit ae235b
 * @code: a specific error code.
Packit ae235b
 * @format: a formatted error reporting string.
Packit ae235b
 * @...: a list of variables to fill in @format.
Packit ae235b
 *
Packit ae235b
 * Reports an error in an asynchronous function in an idle function by
Packit ae235b
 * directly setting the contents of the #GAsyncResult with the given error
Packit ae235b
 * information.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_report_error().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_report_error_in_idle (GObject             *object,
Packit ae235b
                                     GAsyncReadyCallback  callback,
Packit ae235b
                                     gpointer             user_data,
Packit ae235b
                                     GQuark               domain,
Packit ae235b
                                     gint                 code,
Packit ae235b
                                     const char          *format,
Packit ae235b
                                     ...)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
  va_list args;
Packit ae235b
 
Packit ae235b
  g_return_if_fail (!object || G_IS_OBJECT (object));
Packit ae235b
  g_return_if_fail (domain != 0);
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new (object,
Packit ae235b
				      callback,
Packit ae235b
				      user_data, NULL);
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
  g_simple_async_result_complete_in_idle (simple);
Packit ae235b
  g_object_unref (simple);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_report_gerror_in_idle:
Packit ae235b
 * @object: (nullable): a #GObject, or %NULL
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: (closure): user data passed to @callback.
Packit ae235b
 * @error: the #GError to report
Packit ae235b
 *
Packit ae235b
 * Reports an error in an idle function. Similar to
Packit ae235b
 * g_simple_async_report_error_in_idle(), but takes a #GError rather
Packit ae235b
 * than building a new one.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_report_error().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_report_gerror_in_idle (GObject *object,
Packit ae235b
				      GAsyncReadyCallback callback,
Packit ae235b
				      gpointer user_data,
Packit ae235b
				      const GError *error)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
 
Packit ae235b
  g_return_if_fail (!object || G_IS_OBJECT (object));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new_from_error (object,
Packit ae235b
						 callback,
Packit ae235b
						 user_data,
Packit ae235b
						 error);
Packit ae235b
  g_simple_async_result_complete_in_idle (simple);
Packit ae235b
  g_object_unref (simple);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_report_take_gerror_in_idle: (skip)
Packit ae235b
 * @object: (nullable): a #GObject, or %NULL
Packit ae235b
 * @callback: a #GAsyncReadyCallback.
Packit ae235b
 * @user_data: user data passed to @callback.
Packit ae235b
 * @error: the #GError to report
Packit ae235b
 *
Packit ae235b
 * Reports an error in an idle function. Similar to
Packit ae235b
 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
Packit ae235b
 * ownership of @error, so the caller does not have to free it any more.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use g_task_report_error().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_report_take_gerror_in_idle (GObject *object,
Packit ae235b
                                           GAsyncReadyCallback callback,
Packit ae235b
                                           gpointer user_data,
Packit ae235b
                                           GError *error)
Packit ae235b
{
Packit ae235b
  GSimpleAsyncResult *simple;
Packit ae235b
Packit ae235b
  g_return_if_fail (!object || G_IS_OBJECT (object));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  simple = g_simple_async_result_new_take_error (object,
Packit ae235b
                                                 callback,
Packit ae235b
                                                 user_data,
Packit ae235b
                                                 error);
Packit ae235b
  g_simple_async_result_complete_in_idle (simple);
Packit ae235b
  g_object_unref (simple);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_simple_async_result_set_check_cancellable:
Packit ae235b
 * @simple: a #GSimpleAsyncResult
Packit ae235b
 * @check_cancellable: (nullable): a #GCancellable to check, or %NULL to unset
Packit ae235b
 *
Packit ae235b
 * Sets a #GCancellable to check before dispatching results.
Packit ae235b
 *
Packit ae235b
 * This function has one very specific purpose: the provided cancellable
Packit ae235b
 * is checked at the time of g_simple_async_result_propagate_error() If
Packit ae235b
 * it is cancelled, these functions will return an "Operation was
Packit ae235b
 * cancelled" error (%G_IO_ERROR_CANCELLED).
Packit ae235b
 *
Packit ae235b
 * Implementors of cancellable asynchronous functions should use this in
Packit ae235b
 * order to provide a guarantee to their callers that cancelling an
Packit ae235b
 * async operation will reliably result in an error being returned for
Packit ae235b
 * that operation (even if a positive result for the operation has
Packit ae235b
 * already been sent as an idle to the main context to be dispatched).
Packit ae235b
 *
Packit ae235b
 * The checking described above is done regardless of any call to the
Packit ae235b
 * unrelated g_simple_async_result_set_handle_cancellation() function.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.46: Use #GTask instead.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
Packit ae235b
                                             GCancellable *check_cancellable)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
Packit ae235b
  g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
Packit ae235b
Packit ae235b
  g_clear_object (&simple->check_cancellable);
Packit ae235b
  if (check_cancellable)
Packit ae235b
    simple->check_cancellable = g_object_ref (check_cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
G_GNUC_END_IGNORE_DEPRECATIONS