Moved all relevant typedefs into these files.
[platform/upstream/glib.git] / gio / gloadableicon.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gasyncresult.h"
25 #include "gsimpleasyncresult.h"
26 #include "gicon.h"
27 #include "gloadableicon.h"
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:gloadableicon
34  * @short_description: Loadable Icons
35  * @include: gio/gio.h
36  * @see_also: #GIcon, #GThemedIcon
37  * 
38  * Extends the #GIcon interface and adds the ability to 
39  * load icons from streams.
40  **/
41
42 static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
43                                                        int                   size,
44                                                        GCancellable         *cancellable,
45                                                        GAsyncReadyCallback   callback,
46                                                        gpointer              user_data);
47 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
48                                                        GAsyncResult         *res,
49                                                        char                **type,
50                                                        GError              **error);
51 static void          g_loadable_icon_base_init        (gpointer              g_class);
52 static void          g_loadable_icon_class_init       (gpointer              g_class,
53                                                        gpointer              class_data);
54
55 GType
56 g_loadable_icon_get_type (void)
57 {
58   static GType loadable_icon_type = 0;
59
60   if (! loadable_icon_type)
61     {
62       static const GTypeInfo loadable_icon_info =
63         {
64         sizeof (GLoadableIconIface), /* class_size */
65         g_loadable_icon_base_init,   /* base_init */
66         NULL,           /* base_finalize */
67         g_loadable_icon_class_init,
68         NULL,           /* class_finalize */
69         NULL,           /* class_data */
70         0,
71         0,              /* n_preallocs */
72         NULL
73       };
74
75       loadable_icon_type =
76         g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
77                                 &loadable_icon_info, 0);
78
79       g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
80     }
81
82   return loadable_icon_type;
83 }
84
85 static void
86 g_loadable_icon_class_init (gpointer g_class,
87                             gpointer class_data)
88 {
89   GLoadableIconIface *iface = g_class;
90
91   iface->load_async = g_loadable_icon_real_load_async;
92   iface->load_finish = g_loadable_icon_real_load_finish;
93 }
94
95 static void
96 g_loadable_icon_base_init (gpointer g_class)
97 {
98 }
99
100 /**
101  * g_loadable_icon_load:
102  * @icon: a #GLoadableIcon.
103  * @size: an integer.
104  * @type:  a location to store the type of the loaded icon, %NULL to ignore.
105  * @cancellable: optional #GCancellable object, %NULL to ignore. 
106  * @error: a #GError location to store the error occuring, or %NULL to 
107  * ignore.
108  * 
109  * Loads a loadable icon. For the asynchronous version of this function, 
110  * see g_loadable_icon_load_async().
111  * 
112  * Returns: a #GInputStream to read the icon from.
113  **/
114 GInputStream *
115 g_loadable_icon_load (GLoadableIcon  *icon,
116                       int             size,
117                       char          **type,
118                       GCancellable   *cancellable,
119                       GError        **error)
120 {
121   GLoadableIconIface *iface;
122
123   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
124
125   iface = G_LOADABLE_ICON_GET_IFACE (icon);
126
127   return (* iface->load) (icon, size, type, cancellable, error);
128 }
129
130 /**
131  * g_loadable_icon_load_async:
132  * @icon: a #GLoadableIcon.
133  * @size: an integer.
134  * @cancellable: optional #GCancellable object, %NULL to ignore. 
135  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
136  * @user_data: the data to pass to callback function
137  * 
138  * Loads an icon asynchronously. To finish this function, see 
139  * g_loadable_icon_load_finish(). For the synchronous, blocking 
140  * version of this function, see g_loadable_icon_load().
141  **/
142 void
143 g_loadable_icon_load_async (GLoadableIcon       *icon,
144                             int                  size,
145                             GCancellable        *cancellable,
146                             GAsyncReadyCallback  callback,
147                             gpointer             user_data)
148 {
149   GLoadableIconIface *iface;
150   
151   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
152
153   iface = G_LOADABLE_ICON_GET_IFACE (icon);
154
155   (* iface->load_async) (icon, size, cancellable, callback, user_data);
156 }
157
158 /**
159  * g_loadable_icon_load_finish:
160  * @icon: a #GLoadableIcon.
161  * @res: a #GAsyncResult.
162  * @type: a location to store the type of the loaded icon, %NULL to ignore.
163  * @error: a #GError location to store the error occuring, or %NULL to 
164  * ignore.
165  * 
166  * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
167  * 
168  * Returns: a #GInputStream to read the icon from.
169  **/
170 GInputStream *
171 g_loadable_icon_load_finish (GLoadableIcon  *icon,
172                              GAsyncResult   *res,
173                              char          **type,
174                              GError        **error)
175 {
176   GLoadableIconIface *iface;
177   
178   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
179   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
180
181   if (G_IS_SIMPLE_ASYNC_RESULT (res))
182     {
183       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
184       if (g_simple_async_result_propagate_error (simple, error))
185         return NULL;
186     }
187   
188   iface = G_LOADABLE_ICON_GET_IFACE (icon);
189
190   return (* iface->load_finish) (icon, res, type, error);
191 }
192
193 /********************************************
194  *   Default implementation of async load   *
195  ********************************************/
196
197 typedef struct {
198   int size;
199   char *type;
200   GInputStream *stream;
201 } LoadData;
202
203 static void
204 load_data_free (LoadData *data)
205 {
206   if (data->stream)
207     g_object_unref (data->stream);
208   g_free (data->type);
209   g_free (data);
210 }
211
212 static void
213 load_async_thread (GSimpleAsyncResult *res,
214                    GObject            *object,
215                    GCancellable       *cancellable)
216 {
217   GLoadableIconIface *iface;
218   GInputStream *stream;
219   LoadData *data;
220   GError *error = NULL;
221   char *type = NULL;
222
223   data = g_simple_async_result_get_op_res_gpointer (res);
224   
225   iface = G_LOADABLE_ICON_GET_IFACE (object);
226   stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
227
228   if (stream == NULL)
229     {
230       g_simple_async_result_set_from_error (res, error);
231       g_error_free (error);
232     }
233   else
234     {
235       data->stream = stream;
236       data->type = type;
237     }
238 }
239
240
241
242 static void
243 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
244                                  int                  size,
245                                  GCancellable        *cancellable,
246                                  GAsyncReadyCallback  callback,
247                                  gpointer             user_data)
248 {
249   GSimpleAsyncResult *res;
250   LoadData *data;
251   
252   res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
253   data = g_new0 (LoadData, 1);
254   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
255   g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
256   g_object_unref (res);
257 }
258
259 static GInputStream *
260 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
261                                   GAsyncResult         *res,
262                                   char                **type,
263                                   GError              **error)
264 {
265   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
266   LoadData *data;
267
268   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
269
270   data = g_simple_async_result_get_op_res_gpointer (simple);
271
272   if (type)
273     {
274       *type = data->type;
275       data->type = NULL;
276     }
277
278   return g_object_ref (data->stream);
279 }
280
281 #define __G_LOADABLE_ICON_C__
282 #include "gioaliasdef.c"