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>
24 #include "gsimpleasyncresult.h"
25 #include "gloadableicon.h"
31 * SECTION:gloadableicon
32 * @short_description: Loadable Icons
33 * @see_also: #GIcon, #GThemedIcon
35 * Extends the #GIcon interface and adds the ability to load icons.
38 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
40 GCancellable *cancellable,
41 GAsyncReadyCallback callback,
43 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
47 static void g_loadable_icon_base_init (gpointer g_class);
48 static void g_loadable_icon_class_init (gpointer g_class,
52 g_loadable_icon_get_type (void)
54 static GType loadable_icon_type = 0;
56 if (! loadable_icon_type)
58 static const GTypeInfo loadable_icon_info =
60 sizeof (GLoadableIconIface), /* class_size */
61 g_loadable_icon_base_init, /* base_init */
62 NULL, /* base_finalize */
63 g_loadable_icon_class_init,
64 NULL, /* class_finalize */
65 NULL, /* class_data */
72 g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
73 &loadable_icon_info, 0);
75 g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
78 return loadable_icon_type;
82 g_loadable_icon_class_init (gpointer g_class,
85 GLoadableIconIface *iface = g_class;
87 iface->load_async = g_loadable_icon_real_load_async;
88 iface->load_finish = g_loadable_icon_real_load_finish;
92 g_loadable_icon_base_init (gpointer g_class)
97 * g_loadable_icon_load:
98 * @icon: a #GLoadableIcon.
100 * @type: a location to store the type of the loaded icon, %NULL to ignore.
101 * @cancellable: optional #GCancellable object, %NULL to ignore.
102 * @error: a #GError location to store the error occuring, or %NULL to
105 * Loads a loadable icon. For the asynchronous version of this function,
106 * see g_loadable_icon_load_async().
108 * Returns: a #GInputStream to read the icon from.
111 g_loadable_icon_load (GLoadableIcon *icon,
114 GCancellable *cancellable,
117 GLoadableIconIface *iface;
119 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
121 iface = G_LOADABLE_ICON_GET_IFACE (icon);
123 return (* iface->load) (icon, size, type, cancellable, error);
127 * g_loadable_icon_load_async:
128 * @icon: a #GLoadableIcon.
130 * @cancellable: optional #GCancellable object, %NULL to ignore.
131 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
132 * @user_data: the data to pass to callback function
134 * Loads an icon asynchronously. To finish this function, see
135 * g_loadable_icon_load_finish(). For the synchronous, blocking
136 * version of this function, see g_loadable_icon_load().
139 g_loadable_icon_load_async (GLoadableIcon *icon,
141 GCancellable *cancellable,
142 GAsyncReadyCallback callback,
145 GLoadableIconIface *iface;
147 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
149 iface = G_LOADABLE_ICON_GET_IFACE (icon);
151 (* iface->load_async) (icon, size, cancellable, callback, user_data);
155 * g_loadable_icon_load_finish:
156 * @icon: a #GLoadableIcon.
157 * @res: a #GAsyncResult.
158 * @type: a location to store the type of the loaded icon, %NULL to ignore.
159 * @error: a #GError location to store the error occuring, or %NULL to
162 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
164 * Returns: a #GInputStream to read the icon from.
167 g_loadable_icon_load_finish (GLoadableIcon *icon,
172 GLoadableIconIface *iface;
174 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
175 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
177 if (G_IS_SIMPLE_ASYNC_RESULT (res))
179 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
180 if (g_simple_async_result_propagate_error (simple, error))
184 iface = G_LOADABLE_ICON_GET_IFACE (icon);
186 return (* iface->load_finish) (icon, res, type, error);
189 /********************************************
190 * Default implementation of async load *
191 ********************************************/
196 GInputStream *stream;
200 load_data_free (LoadData *data)
203 g_object_unref (data->stream);
209 load_async_thread (GSimpleAsyncResult *res,
211 GCancellable *cancellable)
213 GLoadableIconIface *iface;
214 GInputStream *stream;
216 GError *error = NULL;
219 data = g_simple_async_result_get_op_res_gpointer (res);
221 iface = G_LOADABLE_ICON_GET_IFACE (object);
222 stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
226 g_simple_async_result_set_from_error (res, error);
227 g_error_free (error);
231 data->stream = stream;
239 g_loadable_icon_real_load_async (GLoadableIcon *icon,
241 GCancellable *cancellable,
242 GAsyncReadyCallback callback,
245 GSimpleAsyncResult *res;
248 res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
249 data = g_new0 (LoadData, 1);
250 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
251 g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
252 g_object_unref (res);
255 static GInputStream *
256 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
261 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
264 g_assert (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
266 data = g_simple_async_result_get_op_res_gpointer (simple);
274 return g_object_ref (data->stream);
277 #define __G_LOADABLE_ICON_C__
278 #include "gioaliasdef.c"