Blame gio/gloadableicon.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
#include "gicon.h"
Packit ae235b
#include "gloadableicon.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gloadableicon
Packit ae235b
 * @short_description: Loadable Icons
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GIcon, #GThemedIcon
Packit ae235b
 * 
Packit ae235b
 * Extends the #GIcon interface and adds the ability to 
Packit ae235b
 * load icons from streams.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
Packit ae235b
						       int                   size,
Packit ae235b
						       GCancellable         *cancellable,
Packit ae235b
						       GAsyncReadyCallback   callback,
Packit ae235b
						       gpointer              user_data);
Packit ae235b
static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
Packit ae235b
						       GAsyncResult         *res,
Packit ae235b
						       char                **type,
Packit ae235b
						       GError              **error);
Packit ae235b
Packit ae235b
typedef GLoadableIconIface GLoadableIconInterface;
Packit ae235b
G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_loadable_icon_default_init (GLoadableIconIface *iface)
Packit ae235b
{
Packit ae235b
  iface->load_async = g_loadable_icon_real_load_async;
Packit ae235b
  iface->load_finish = g_loadable_icon_real_load_finish;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_loadable_icon_load:
Packit ae235b
 * @icon: a #GLoadableIcon.
Packit ae235b
 * @size: an integer.
Packit ae235b
 * @type: (out) (optional): a location to store the type of the loaded
Packit ae235b
 * icon, %NULL to ignore.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to
Packit ae235b
 * ignore.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL
Packit ae235b
 * to ignore.
Packit ae235b
 * 
Packit ae235b
 * Loads a loadable icon. For the asynchronous version of this function, 
Packit ae235b
 * see g_loadable_icon_load_async().
Packit ae235b
 * 
Packit ae235b
 * Returns: (transfer full): a #GInputStream to read the icon from.
Packit ae235b
 **/
Packit ae235b
GInputStream *
Packit ae235b
g_loadable_icon_load (GLoadableIcon  *icon,
Packit ae235b
		      int             size,
Packit ae235b
		      char          **type,
Packit ae235b
		      GCancellable   *cancellable,
Packit ae235b
		      GError        **error)
Packit ae235b
{
Packit ae235b
  GLoadableIconIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
Packit ae235b
Packit ae235b
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
Packit ae235b
Packit ae235b
  return (* iface->load) (icon, size, type, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_loadable_icon_load_async:
Packit ae235b
 * @icon: a #GLoadableIcon.
Packit ae235b
 * @size: an integer.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback to call when the
Packit ae235b
 *            request is satisfied
Packit ae235b
 * @user_data: (closure): the data to pass to callback function
Packit ae235b
 * 
Packit ae235b
 * Loads an icon asynchronously. To finish this function, see 
Packit ae235b
 * g_loadable_icon_load_finish(). For the synchronous, blocking 
Packit ae235b
 * version of this function, see g_loadable_icon_load().
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_loadable_icon_load_async (GLoadableIcon       *icon,
Packit ae235b
                            int                  size,
Packit ae235b
                            GCancellable        *cancellable,
Packit ae235b
                            GAsyncReadyCallback  callback,
Packit ae235b
                            gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GLoadableIconIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (G_IS_LOADABLE_ICON (icon));
Packit ae235b
Packit ae235b
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
Packit ae235b
Packit ae235b
  (* iface->load_async) (icon, size, cancellable, callback, user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_loadable_icon_load_finish:
Packit ae235b
 * @icon: a #GLoadableIcon.
Packit ae235b
 * @res: a #GAsyncResult.
Packit ae235b
 * @type: (out) (optional): a location to store the type of the loaded
Packit ae235b
 *        icon, %NULL to ignore.
Packit ae235b
 * @error: a #GError location to store the error occurring, or %NULL to 
Packit ae235b
 * ignore.
Packit ae235b
 * 
Packit ae235b
 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
Packit ae235b
 * 
Packit ae235b
 * Returns: (transfer full): a #GInputStream to read the icon from.
Packit ae235b
 **/
Packit ae235b
GInputStream *
Packit ae235b
g_loadable_icon_load_finish (GLoadableIcon  *icon,
Packit ae235b
			     GAsyncResult   *res,
Packit ae235b
			     char          **type,
Packit ae235b
			     GError        **error)
Packit ae235b
{
Packit ae235b
  GLoadableIconIface *iface;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
Packit ae235b
Packit ae235b
  if (g_async_result_legacy_propagate_error (res, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
Packit ae235b
Packit ae235b
  return (* iface->load_finish) (icon, res, type, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/********************************************
Packit ae235b
 *   Default implementation of async load   *
Packit ae235b
 ********************************************/
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  int size;
Packit ae235b
  char *type;
Packit ae235b
} LoadData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
load_data_free (LoadData *data)
Packit ae235b
{
Packit ae235b
  g_free (data->type);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
load_async_thread (GTask        *task,
Packit ae235b
                   gpointer      source_object,
Packit ae235b
                   gpointer      task_data,
Packit ae235b
                   GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GLoadableIcon *icon = source_object;
Packit ae235b
  LoadData *data = task_data;
Packit ae235b
  GLoadableIconIface *iface;
Packit ae235b
  GInputStream *stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  iface = G_LOADABLE_ICON_GET_IFACE (icon);
Packit ae235b
  stream = iface->load (icon, data->size, &data->type,
Packit ae235b
                        cancellable, &error);
Packit ae235b
Packit ae235b
  if (stream)
Packit ae235b
    g_task_return_pointer (task, stream, g_object_unref);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_loadable_icon_real_load_async (GLoadableIcon       *icon,
Packit ae235b
				 int                  size,
Packit ae235b
				 GCancellable        *cancellable,
Packit ae235b
				 GAsyncReadyCallback  callback,
Packit ae235b
				 gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  LoadData *data;
Packit ae235b
Packit ae235b
  task = g_task_new (icon, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_loadable_icon_real_load_async);
Packit ae235b
  data = g_new0 (LoadData, 1);
Packit ae235b
  g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
Packit ae235b
  g_task_run_in_thread (task, load_async_thread);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GInputStream *
Packit ae235b
g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
Packit ae235b
				  GAsyncResult         *res,
Packit ae235b
				  char                **type,
Packit ae235b
				  GError              **error)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  LoadData *data;
Packit ae235b
  GInputStream *stream;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
Packit ae235b
Packit ae235b
  task = G_TASK (res);
Packit ae235b
  data = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
  stream = g_task_propagate_pointer (task, error);
Packit ae235b
  if (stream && type)
Packit ae235b
    {
Packit ae235b
      *type = data->type;
Packit ae235b
      data->type = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return stream;
Packit ae235b
}