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