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"
29 * SECTION:gloadableicon
30 * @short_description: Loadable Icons
31 * @see_also: #GIcon, #GThemedIcon
33 * Implements #GIcon and adds the ability to load icons.
37 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
39 GCancellable *cancellable,
40 GAsyncReadyCallback callback,
42 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
46 static void g_loadable_icon_base_init (gpointer g_class);
47 static void g_loadable_icon_class_init (gpointer g_class,
51 g_loadable_icon_get_type (void)
53 static GType loadable_icon_type = 0;
55 if (! loadable_icon_type)
57 static const GTypeInfo loadable_icon_info =
59 sizeof (GLoadableIconIface), /* class_size */
60 g_loadable_icon_base_init, /* base_init */
61 NULL, /* base_finalize */
62 g_loadable_icon_class_init,
63 NULL, /* class_finalize */
64 NULL, /* class_data */
71 g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
72 &loadable_icon_info, 0);
74 g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
77 return loadable_icon_type;
81 g_loadable_icon_class_init (gpointer g_class,
84 GLoadableIconIface *iface = g_class;
86 iface->load_async = g_loadable_icon_real_load_async;
87 iface->load_finish = g_loadable_icon_real_load_finish;
91 g_loadable_icon_base_init (gpointer g_class)
96 * g_loadable_icon_load:
97 * @icon: a #GLoadableIcon.
100 * @cancellable: optional #GCancellable object, %NULL to ignore.
101 * @error: a #GError location to store the error occuring, or %NULL to
104 * Loads a loadable icon. For the asynchronous version of this function,
105 * see g_loadable_icon_load_async().
107 * Returns: a #GInputStream to read the icon from.
110 g_loadable_icon_load (GLoadableIcon *icon,
113 GCancellable *cancellable,
116 GLoadableIconIface *iface;
118 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
120 iface = G_LOADABLE_ICON_GET_IFACE (icon);
122 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().
140 g_loadable_icon_load_async (GLoadableIcon *icon,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
146 GLoadableIconIface *iface;
148 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
150 iface = G_LOADABLE_ICON_GET_IFACE (icon);
152 (* iface->load_async) (icon, size, cancellable, callback, user_data);
157 * g_loadable_icon_load_finish:
158 * @icon: a #GLoadableIcon.
159 * @res: a #GAsyncResult.
161 * @error: a #GError location to store the error occuring, or %NULL to
164 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
166 * Returns: a #GInputStream to read the icon from.
169 g_loadable_icon_load_finish (GLoadableIcon *icon,
174 GLoadableIconIface *iface;
176 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
177 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
179 if (G_IS_SIMPLE_ASYNC_RESULT (res))
181 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
182 if (g_simple_async_result_propagate_error (simple, error))
186 iface = G_LOADABLE_ICON_GET_IFACE (icon);
188 return (* iface->load_finish) (icon, res, type, error);
192 /********************************************
193 * Default implementation of async load *
194 ********************************************/
199 GInputStream *stream;
203 load_data_free (LoadData *data)
206 g_object_unref (data->stream);
212 load_async_thread (GSimpleAsyncResult *res,
214 GCancellable *cancellable)
216 GLoadableIconIface *iface;
217 GInputStream *stream;
219 GError *error = NULL;
222 data = g_simple_async_result_get_op_res_gpointer (res);
224 iface = G_LOADABLE_ICON_GET_IFACE (object);
225 stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
229 g_simple_async_result_set_from_error (res, error);
230 g_error_free (error);
234 data->stream = stream;
242 g_loadable_icon_real_load_async (GLoadableIcon *icon,
244 GCancellable *cancellable,
245 GAsyncReadyCallback callback,
248 GSimpleAsyncResult *res;
251 res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
252 data = g_new0 (LoadData, 1);
253 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
254 g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
255 g_object_unref (res);
258 static GInputStream *
259 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
264 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
267 g_assert (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
269 data = g_simple_async_result_get_op_res_gpointer (simple);
277 return g_object_ref (data->stream);