2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gicon.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gloadableicon.h"
|
2012-08-02 21:50:35 +02:00
|
|
|
#include "gasyncresult.h"
|
|
|
|
#include "gtask.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "glibintl.h"
|
|
|
|
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gloadableicon
|
|
|
|
* @short_description: Loadable Icons
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
* @see_also: #GIcon, #GThemedIcon
|
|
|
|
*
|
2007-12-18 03:52:11 +01:00
|
|
|
* Extends the #GIcon interface and adds the ability to
|
|
|
|
* load icons from streams.
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data);
|
|
|
|
static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
|
|
|
|
GAsyncResult *res,
|
|
|
|
char **type,
|
|
|
|
GError **error);
|
|
|
|
|
2009-12-01 10:42:58 +01:00
|
|
|
typedef GLoadableIconIface GLoadableIconInterface;
|
|
|
|
G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
static void
|
2009-12-01 10:42:58 +01:00
|
|
|
g_loadable_icon_default_init (GLoadableIconIface *iface)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
iface->load_async = g_loadable_icon_real_load_async;
|
|
|
|
iface->load_finish = g_loadable_icon_real_load_finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_loadable_icon_load:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @icon: a #GLoadableIcon.
|
|
|
|
* @size: an integer.
|
2010-12-27 16:08:46 +01:00
|
|
|
* @type: (out) (allow-none): a location to store the type of the
|
|
|
|
* loaded icon, %NULL to ignore.
|
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
* ignore.
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* Loads a loadable icon. For the asynchronous version of this function,
|
|
|
|
* see g_loadable_icon_load_async().
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GInputStream to read the icon from.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
GInputStream *
|
2007-11-29 08:17:59 +01:00
|
|
|
g_loadable_icon_load (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
char **type,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GLoadableIconIface *iface;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
|
|
|
|
|
|
|
|
iface = G_LOADABLE_ICON_GET_IFACE (icon);
|
|
|
|
|
|
|
|
return (* iface->load) (icon, size, type, cancellable, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_loadable_icon_load_async:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @icon: a #GLoadableIcon.
|
|
|
|
* @size: an integer.
|
2010-12-27 16:08:46 +01:00
|
|
|
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
|
|
|
|
* @callback: (scope async): a #GAsyncReadyCallback to call when the
|
|
|
|
* request is satisfied
|
|
|
|
* @user_data: (closure): the data to pass to callback function
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Loads an icon asynchronously. To finish this function, see
|
|
|
|
* g_loadable_icon_load_finish(). For the synchronous, blocking
|
|
|
|
* version of this function, see g_loadable_icon_load().
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
void
|
2007-11-29 08:17:59 +01:00
|
|
|
g_loadable_icon_load_async (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GLoadableIconIface *iface;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_LOADABLE_ICON (icon));
|
|
|
|
|
|
|
|
iface = G_LOADABLE_ICON_GET_IFACE (icon);
|
|
|
|
|
|
|
|
(* iface->load_async) (icon, size, cancellable, callback, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_loadable_icon_load_finish:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @icon: a #GLoadableIcon.
|
|
|
|
* @res: a #GAsyncResult.
|
2013-08-24 13:12:45 +02:00
|
|
|
* @type: (out) (allow-none): a location to store the type of the
|
|
|
|
* loaded icon, %NULL to ignore.
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: a #GError location to store the error occurring, or %NULL to
|
2007-11-26 17:13:05 +01:00
|
|
|
* ignore.
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
|
|
|
* Finishes an asynchronous icon load started in g_loadable_icon_load_async().
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a #GInputStream to read the icon from.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
GInputStream *
|
2007-11-29 08:17:59 +01:00
|
|
|
g_loadable_icon_load_finish (GLoadableIcon *icon,
|
|
|
|
GAsyncResult *res,
|
|
|
|
char **type,
|
|
|
|
GError **error)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
GLoadableIconIface *iface;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
|
|
|
|
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
|
|
|
|
|
2012-05-10 15:00:45 +02:00
|
|
|
if (g_async_result_legacy_propagate_error (res, error))
|
|
|
|
return NULL;
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
iface = G_LOADABLE_ICON_GET_IFACE (icon);
|
|
|
|
|
|
|
|
return (* iface->load_finish) (icon, res, type, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************
|
|
|
|
* Default implementation of async load *
|
|
|
|
********************************************/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int size;
|
|
|
|
char *type;
|
|
|
|
} LoadData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_data_free (LoadData *data)
|
|
|
|
{
|
|
|
|
g_free (data->type);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-02 21:50:35 +02:00
|
|
|
load_async_thread (GTask *task,
|
|
|
|
gpointer source_object,
|
|
|
|
gpointer task_data,
|
|
|
|
GCancellable *cancellable)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2012-08-02 21:50:35 +02:00
|
|
|
GLoadableIcon *icon = source_object;
|
|
|
|
LoadData *data = task_data;
|
2007-11-26 17:13:05 +01:00
|
|
|
GLoadableIconIface *iface;
|
|
|
|
GInputStream *stream;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
iface = G_LOADABLE_ICON_GET_IFACE (icon);
|
|
|
|
stream = iface->load (icon, data->size, &data->type,
|
|
|
|
cancellable, &error);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
if (stream)
|
|
|
|
g_task_return_pointer (task, stream, g_object_unref);
|
2007-11-26 17:13:05 +01:00
|
|
|
else
|
2012-08-02 21:50:35 +02:00
|
|
|
g_task_return_error (task, error);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-11-29 08:17:59 +01:00
|
|
|
g_loadable_icon_real_load_async (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2012-08-02 21:50:35 +02:00
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
LoadData *data;
|
2012-08-02 21:50:35 +02:00
|
|
|
|
|
|
|
task = g_task_new (icon, cancellable, callback, user_data);
|
2007-11-26 17:13:05 +01:00
|
|
|
data = g_new0 (LoadData, 1);
|
2012-08-02 21:50:35 +02:00
|
|
|
g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
|
|
|
|
g_task_run_in_thread (task, load_async_thread);
|
|
|
|
g_object_unref (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static GInputStream *
|
|
|
|
g_loadable_icon_real_load_finish (GLoadableIcon *icon,
|
|
|
|
GAsyncResult *res,
|
|
|
|
char **type,
|
|
|
|
GError **error)
|
|
|
|
{
|
2012-08-02 21:50:35 +02:00
|
|
|
GTask *task;
|
2007-11-26 17:13:05 +01:00
|
|
|
LoadData *data;
|
2012-08-02 21:50:35 +02:00
|
|
|
GInputStream *stream;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
|
2012-06-11 19:44:19 +02:00
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
task = G_TASK (res);
|
|
|
|
data = g_task_get_task_data (task);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
stream = g_task_propagate_pointer (task, error);
|
|
|
|
if (stream && type)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
*type = data->type;
|
|
|
|
data->type = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-02 21:50:35 +02:00
|
|
|
return stream;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|