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
34 * @see_also: #GIcon, #GThemedIcon
36 * Extends the #GIcon interface and adds the ability to
37 * load icons from streams.
40 static void g_loadable_icon_real_load_async (GLoadableIcon
*icon
,
42 GCancellable
*cancellable
,
43 GAsyncReadyCallback callback
,
45 static GInputStream
*g_loadable_icon_real_load_finish (GLoadableIcon
*icon
,
49 static void g_loadable_icon_base_init (gpointer g_class
);
50 static void g_loadable_icon_class_init (gpointer g_class
,
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 */
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
;
84 g_loadable_icon_class_init (gpointer g_class
,
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
;
94 g_loadable_icon_base_init (gpointer g_class
)
99 * g_loadable_icon_load:
100 * @icon: a #GLoadableIcon.
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
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.
113 g_loadable_icon_load (GLoadableIcon
*icon
,
116 GCancellable
*cancellable
,
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.
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().
141 g_loadable_icon_load_async (GLoadableIcon
*icon
,
143 GCancellable
*cancellable
,
144 GAsyncReadyCallback callback
,
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
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
);
191 /********************************************
192 * Default implementation of async load *
193 ********************************************/
198 GInputStream
*stream
;
202 load_data_free (LoadData
*data
)
205 g_object_unref (data
->stream
);
211 load_async_thread (GSimpleAsyncResult
*res
,
213 GCancellable
*cancellable
)
215 GLoadableIconIface
*iface
;
216 GInputStream
*stream
;
218 GError
*error
= 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
);
228 g_simple_async_result_set_from_error (res
, error
);
229 g_error_free (error
);
233 data
->stream
= stream
;
241 g_loadable_icon_real_load_async (GLoadableIcon
*icon
,
243 GCancellable
*cancellable
,
244 GAsyncReadyCallback callback
,
247 GSimpleAsyncResult
*res
;
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
,
263 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
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
);
276 return g_object_ref (data
->stream
);
279 #define __G_LOADABLE_ICON_C__
280 #include "gioaliasdef.c"