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>
25 #include "gsimpleasyncresult.h"
26 #include "gloadableicon.h"
29 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
31 GCancellable *cancellable,
32 GAsyncReadyCallback callback,
34 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
38 static void g_loadable_icon_base_init (gpointer g_class);
39 static void g_loadable_icon_class_init (gpointer g_class,
43 g_loadable_icon_get_type (void)
45 static GType loadable_icon_type = 0;
47 if (! loadable_icon_type)
49 static const GTypeInfo loadable_icon_info =
51 sizeof (GLoadableIconIface), /* class_size */
52 g_loadable_icon_base_init, /* base_init */
53 NULL, /* base_finalize */
54 g_loadable_icon_class_init,
55 NULL, /* class_finalize */
56 NULL, /* class_data */
63 g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
64 &loadable_icon_info, 0);
66 g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
69 return loadable_icon_type;
73 g_loadable_icon_class_init (gpointer g_class,
76 GLoadableIconIface *iface = g_class;
78 iface->load_async = g_loadable_icon_real_load_async;
79 iface->load_finish = g_loadable_icon_real_load_finish;
83 g_loadable_icon_base_init (gpointer g_class)
88 * g_loadable_icon_load:
92 * @cancellable: optional #GCancellable object, %NULL to ignore.
93 * @error: a #GError location to store the error occuring, or %NULL to
98 g_loadable_icon_load (GLoadableIcon *icon,
101 GCancellable *cancellable,
104 GLoadableIconIface *iface;
106 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
108 iface = G_LOADABLE_ICON_GET_IFACE (icon);
110 return (* iface->load) (icon, size, type, cancellable, error);
115 * g_loadable_icon_load_async:
118 * @cancellable: optional #GCancellable object, %NULL to ignore. @callback:
121 * Loads an icon asynchronously.
125 g_loadable_icon_load_async (GLoadableIcon *icon,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
131 GLoadableIconIface *iface;
133 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
135 iface = G_LOADABLE_ICON_GET_IFACE (icon);
137 (* iface->load_async) (icon, size, cancellable, callback, user_data);
142 * g_loadable_icon_load_finish:
146 * @error: a #GError location to store the error occuring, or %NULL to
151 g_loadable_icon_load_finish (GLoadableIcon *icon,
156 GLoadableIconIface *iface;
158 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
159 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
161 if (G_IS_SIMPLE_ASYNC_RESULT (res))
163 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
164 if (g_simple_async_result_propagate_error (simple, error))
168 iface = G_LOADABLE_ICON_GET_IFACE (icon);
170 return (* iface->load_finish) (icon, res, type, error);
174 /********************************************
175 * Default implementation of async load *
176 ********************************************/
181 GInputStream *stream;
185 load_data_free (LoadData *data)
188 g_object_unref (data->stream);
194 load_async_thread (GSimpleAsyncResult *res,
196 GCancellable *cancellable)
198 GLoadableIconIface *iface;
199 GInputStream *stream;
201 GError *error = NULL;
204 data = g_simple_async_result_get_op_res_gpointer (res);
206 iface = G_LOADABLE_ICON_GET_IFACE (object);
207 stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
211 g_simple_async_result_set_from_error (res, error);
212 g_error_free (error);
216 data->stream = stream;
224 g_loadable_icon_real_load_async (GLoadableIcon *icon,
226 GCancellable *cancellable,
227 GAsyncReadyCallback callback,
230 GSimpleAsyncResult *res;
233 res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
234 data = g_new0 (LoadData, 1);
235 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
236 g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
237 g_object_unref (res);
240 static GInputStream *
241 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
246 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
249 g_assert (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
251 data = g_simple_async_result_get_op_res_gpointer (simple);
259 return g_object_ref (data->stream);