Coding style fixups
[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 "gsimpleasyncresult.h"
25 #include "gloadableicon.h"
26 #include "glibintl.h"
27
28 #include "gioalias.h"
29
30 /**
31  * SECTION:gloadableicon
32  * @short_description: Loadable Icons
33  * @see_also: #GIcon, #GThemedIcon
34  * 
35  * Implements #GIcon and adds the ability to load icons.
36  *
37  **/
38
39 static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
40                                                        int                   size,
41                                                        GCancellable         *cancellable,
42                                                        GAsyncReadyCallback   callback,
43                                                        gpointer              user_data);
44 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
45                                                        GAsyncResult         *res,
46                                                        char                **type,
47                                                        GError              **error);
48 static void          g_loadable_icon_base_init        (gpointer              g_class);
49 static void          g_loadable_icon_class_init       (gpointer              g_class,
50                                                        gpointer              class_data);
51
52 GType
53 g_loadable_icon_get_type (void)
54 {
55   static GType loadable_icon_type = 0;
56
57   if (! loadable_icon_type)
58     {
59       static const GTypeInfo loadable_icon_info =
60         {
61         sizeof (GLoadableIconIface), /* class_size */
62         g_loadable_icon_base_init,   /* base_init */
63         NULL,           /* base_finalize */
64         g_loadable_icon_class_init,
65         NULL,           /* class_finalize */
66         NULL,           /* class_data */
67         0,
68         0,              /* n_preallocs */
69         NULL
70       };
71
72       loadable_icon_type =
73         g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
74                                 &loadable_icon_info, 0);
75
76       g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
77     }
78
79   return loadable_icon_type;
80 }
81
82 static void
83 g_loadable_icon_class_init (gpointer g_class,
84                             gpointer class_data)
85 {
86   GLoadableIconIface *iface = g_class;
87
88   iface->load_async = g_loadable_icon_real_load_async;
89   iface->load_finish = g_loadable_icon_real_load_finish;
90 }
91
92 static void
93 g_loadable_icon_base_init (gpointer g_class)
94 {
95 }
96
97 /**
98  * g_loadable_icon_load:
99  * @icon: a #GLoadableIcon.
100  * @size: an integer.
101  * @type: 
102  * @cancellable: optional #GCancellable object, %NULL to ignore. 
103  * @error: a #GError location to store the error occuring, or %NULL to 
104  * ignore.
105  * 
106  * Loads a loadable icon. For the asynchronous version of this function, 
107  * see g_loadable_icon_load_async().
108  * 
109  * Returns: a #GInputStream to read the icon from.
110  **/
111 GInputStream *
112 g_loadable_icon_load (GLoadableIcon  *icon,
113                       int             size,
114                       char          **type,
115                       GCancellable   *cancellable,
116                       GError        **error)
117 {
118   GLoadableIconIface *iface;
119
120   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
121
122   iface = G_LOADABLE_ICON_GET_IFACE (icon);
123
124   return (* iface->load) (icon, size, type, cancellable, error);
125   
126 }
127
128 /**
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
135  * 
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().
139  * 
140  **/
141 void
142 g_loadable_icon_load_async (GLoadableIcon       *icon,
143                             int                  size,
144                             GCancellable        *cancellable,
145                             GAsyncReadyCallback  callback,
146                             gpointer             user_data)
147 {
148   GLoadableIconIface *iface;
149   
150   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
151
152   iface = G_LOADABLE_ICON_GET_IFACE (icon);
153
154   (* iface->load_async) (icon, size, cancellable, callback, user_data);
155   
156 }
157
158 /**
159  * g_loadable_icon_load_finish:
160  * @icon: a #GLoadableIcon.
161  * @res: a #GAsyncResult.
162  * @type:
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 /********************************************
195  *   Default implementation of async load   *
196  ********************************************/
197
198 typedef struct {
199   int size;
200   char *type;
201   GInputStream *stream;
202 } LoadData;
203
204 static void
205 load_data_free (LoadData *data)
206 {
207   if (data->stream)
208     g_object_unref (data->stream);
209   g_free (data->type);
210   g_free (data);
211 }
212
213 static void
214 load_async_thread (GSimpleAsyncResult *res,
215                    GObject            *object,
216                    GCancellable       *cancellable)
217 {
218   GLoadableIconIface *iface;
219   GInputStream *stream;
220   LoadData *data;
221   GError *error = NULL;
222   char *type = NULL;
223
224   data = g_simple_async_result_get_op_res_gpointer (res);
225   
226   iface = G_LOADABLE_ICON_GET_IFACE (object);
227   stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
228
229   if (stream == NULL)
230     {
231       g_simple_async_result_set_from_error (res, error);
232       g_error_free (error);
233     }
234   else
235     {
236       data->stream = stream;
237       data->type = type;
238     }
239 }
240
241
242
243 static void
244 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
245                                  int                  size,
246                                  GCancellable        *cancellable,
247                                  GAsyncReadyCallback  callback,
248                                  gpointer             user_data)
249 {
250   GSimpleAsyncResult *res;
251   LoadData *data;
252   
253   res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
254   data = g_new0 (LoadData, 1);
255   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
256   g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
257   g_object_unref (res);
258 }
259
260 static GInputStream *
261 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
262                                   GAsyncResult         *res,
263                                   char                **type,
264                                   GError              **error)
265 {
266   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
267   LoadData *data;
268
269   g_assert (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
270
271   data = g_simple_async_result_get_op_res_gpointer (simple);
272
273   if (type)
274     {
275       *type = data->type;
276       data->type = NULL;
277     }
278
279   return g_object_ref (data->stream);
280 }
281
282 #define __G_LOADABLE_ICON_C__
283 #include "gioaliasdef.c"