Fix the build
[glib.git] / gio / gloadableicon.c
blob33d7a5fe90262ce402cc52bb33e1b1fa68815e08
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include <config.h>
24 #include "gsimpleasyncresult.h"
25 #include "gloadableicon.h"
26 #include "glibintl.h"
28 #include "gioalias.h"
30 /**
31 * SECTION:gloadableicon
32 * @short_description: Loadable Icons
33 * @include: gio/gio.h
34 * @see_also: #GIcon, #GThemedIcon
36 * Extends the #GIcon interface and adds the ability to
37 * load icons from streams.
38 **/
40 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
41 int size,
42 GCancellable *cancellable,
43 GAsyncReadyCallback callback,
44 gpointer user_data);
45 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
46 GAsyncResult *res,
47 char **type,
48 GError **error);
49 static void g_loadable_icon_base_init (gpointer g_class);
50 static void g_loadable_icon_class_init (gpointer g_class,
51 gpointer class_data);
53 GType
54 g_loadable_icon_get_type (void)
56 static GType loadable_icon_type = 0;
58 if (! loadable_icon_type)
60 static const GTypeInfo loadable_icon_info =
62 sizeof (GLoadableIconIface), /* class_size */
63 g_loadable_icon_base_init, /* base_init */
64 NULL, /* base_finalize */
65 g_loadable_icon_class_init,
66 NULL, /* class_finalize */
67 NULL, /* class_data */
69 0, /* n_preallocs */
70 NULL
73 loadable_icon_type =
74 g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
75 &loadable_icon_info, 0);
77 g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
80 return loadable_icon_type;
83 static void
84 g_loadable_icon_class_init (gpointer g_class,
85 gpointer class_data)
87 GLoadableIconIface *iface = g_class;
89 iface->load_async = g_loadable_icon_real_load_async;
90 iface->load_finish = g_loadable_icon_real_load_finish;
93 static void
94 g_loadable_icon_base_init (gpointer g_class)
98 /**
99 * g_loadable_icon_load:
100 * @icon: a #GLoadableIcon.
101 * @size: an integer.
102 * @type: a location to store the type of the loaded icon, %NULL to ignore.
103 * @cancellable: optional #GCancellable object, %NULL to ignore.
104 * @error: a #GError location to store the error occuring, or %NULL to
105 * ignore.
107 * Loads a loadable icon. For the asynchronous version of this function,
108 * see g_loadable_icon_load_async().
110 * Returns: a #GInputStream to read the icon from.
112 GInputStream *
113 g_loadable_icon_load (GLoadableIcon *icon,
114 int size,
115 char **type,
116 GCancellable *cancellable,
117 GError **error)
119 GLoadableIconIface *iface;
121 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
123 iface = G_LOADABLE_ICON_GET_IFACE (icon);
125 return (* iface->load) (icon, size, type, cancellable, error);
129 * g_loadable_icon_load_async:
130 * @icon: a #GLoadableIcon.
131 * @size: an integer.
132 * @cancellable: optional #GCancellable object, %NULL to ignore.
133 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
134 * @user_data: the data to pass to callback function
136 * Loads an icon asynchronously. To finish this function, see
137 * g_loadable_icon_load_finish(). For the synchronous, blocking
138 * version of this function, see g_loadable_icon_load().
140 void
141 g_loadable_icon_load_async (GLoadableIcon *icon,
142 int size,
143 GCancellable *cancellable,
144 GAsyncReadyCallback callback,
145 gpointer user_data)
147 GLoadableIconIface *iface;
149 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
151 iface = G_LOADABLE_ICON_GET_IFACE (icon);
153 (* iface->load_async) (icon, size, cancellable, callback, user_data);
157 * g_loadable_icon_load_finish:
158 * @icon: a #GLoadableIcon.
159 * @res: a #GAsyncResult.
160 * @type: a location to store the type of the loaded icon, %NULL to ignore.
161 * @error: a #GError location to store the error occuring, or %NULL to
162 * ignore.
164 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
166 * Returns: a #GInputStream to read the icon from.
168 GInputStream *
169 g_loadable_icon_load_finish (GLoadableIcon *icon,
170 GAsyncResult *res,
171 char **type,
172 GError **error)
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))
183 return NULL;
186 iface = G_LOADABLE_ICON_GET_IFACE (icon);
188 return (* iface->load_finish) (icon, res, type, error);
191 /********************************************
192 * Default implementation of async load *
193 ********************************************/
195 typedef struct {
196 int size;
197 char *type;
198 GInputStream *stream;
199 } LoadData;
201 static void
202 load_data_free (LoadData *data)
204 if (data->stream)
205 g_object_unref (data->stream);
206 g_free (data->type);
207 g_free (data);
210 static void
211 load_async_thread (GSimpleAsyncResult *res,
212 GObject *object,
213 GCancellable *cancellable)
215 GLoadableIconIface *iface;
216 GInputStream *stream;
217 LoadData *data;
218 GError *error = NULL;
219 char *type = NULL;
221 data = g_simple_async_result_get_op_res_gpointer (res);
223 iface = G_LOADABLE_ICON_GET_IFACE (object);
224 stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
226 if (stream == NULL)
228 g_simple_async_result_set_from_error (res, error);
229 g_error_free (error);
231 else
233 data->stream = stream;
234 data->type = type;
240 static void
241 g_loadable_icon_real_load_async (GLoadableIcon *icon,
242 int size,
243 GCancellable *cancellable,
244 GAsyncReadyCallback callback,
245 gpointer user_data)
247 GSimpleAsyncResult *res;
248 LoadData *data;
250 res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
251 data = g_new0 (LoadData, 1);
252 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
253 g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
254 g_object_unref (res);
257 static GInputStream *
258 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
259 GAsyncResult *res,
260 char **type,
261 GError **error)
263 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
264 LoadData *data;
266 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
268 data = g_simple_async_result_get_op_res_gpointer (simple);
270 if (type)
272 *type = data->type;
273 data->type = NULL;
276 return g_object_ref (data->stream);
279 #define __G_LOADABLE_ICON_C__
280 #include "gioaliasdef.c"