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