Doc 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  * @include: gio/gio.h
34  * @see_also: #GIcon, #GThemedIcon
35  * 
36  * Extends the #GIcon interface and adds the ability to 
37  * load icons from streams.
38  **/
39
40 static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
41                                                        int                   size,
42                                                        GCancellable         *cancellable,
43                                                        GAsyncReadyCallback   callback,
44                                                        gpointer              user_data);
45 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
46                                                        GAsyncResult         *res,
47                                                        char                **type,
48                                                        GError              **error);
49 static void          g_loadable_icon_base_init        (gpointer              g_class);
50 static void          g_loadable_icon_class_init       (gpointer              g_class,
51                                                        gpointer              class_data);
52
53 GType
54 g_loadable_icon_get_type (void)
55 {
56   static GType loadable_icon_type = 0;
57
58   if (! loadable_icon_type)
59     {
60       static const GTypeInfo loadable_icon_info =
61         {
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 */
68         0,
69         0,              /* n_preallocs */
70         NULL
71       };
72
73       loadable_icon_type =
74         g_type_register_static (G_TYPE_INTERFACE, I_("GLoadableIcon"),
75                                 &loadable_icon_info, 0);
76
77       g_type_interface_add_prerequisite (loadable_icon_type, G_TYPE_ICON);
78     }
79
80   return loadable_icon_type;
81 }
82
83 static void
84 g_loadable_icon_class_init (gpointer g_class,
85                             gpointer class_data)
86 {
87   GLoadableIconIface *iface = g_class;
88
89   iface->load_async = g_loadable_icon_real_load_async;
90   iface->load_finish = g_loadable_icon_real_load_finish;
91 }
92
93 static void
94 g_loadable_icon_base_init (gpointer g_class)
95 {
96 }
97
98 /**
99  * g_loadable_icon_load:
100  * @icon: a #GLoadableIcon.
101  * @size: an integer.
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 
105  * ignore.
106  * 
107  * Loads a loadable icon. For the asynchronous version of this function, 
108  * see g_loadable_icon_load_async().
109  * 
110  * Returns: a #GInputStream to read the icon from.
111  **/
112 GInputStream *
113 g_loadable_icon_load (GLoadableIcon  *icon,
114                       int             size,
115                       char          **type,
116                       GCancellable   *cancellable,
117                       GError        **error)
118 {
119   GLoadableIconIface *iface;
120
121   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
122
123   iface = G_LOADABLE_ICON_GET_IFACE (icon);
124
125   return (* iface->load) (icon, size, type, cancellable, error);
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 void
141 g_loadable_icon_load_async (GLoadableIcon       *icon,
142                             int                  size,
143                             GCancellable        *cancellable,
144                             GAsyncReadyCallback  callback,
145                             gpointer             user_data)
146 {
147   GLoadableIconIface *iface;
148   
149   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
150
151   iface = G_LOADABLE_ICON_GET_IFACE (icon);
152
153   (* iface->load_async) (icon, size, cancellable, callback, user_data);
154 }
155
156 /**
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 
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  *   Default implementation of async load   *
193  ********************************************/
194
195 typedef struct {
196   int size;
197   char *type;
198   GInputStream *stream;
199 } LoadData;
200
201 static void
202 load_data_free (LoadData *data)
203 {
204   if (data->stream)
205     g_object_unref (data->stream);
206   g_free (data->type);
207   g_free (data);
208 }
209
210 static void
211 load_async_thread (GSimpleAsyncResult *res,
212                    GObject            *object,
213                    GCancellable       *cancellable)
214 {
215   GLoadableIconIface *iface;
216   GInputStream *stream;
217   LoadData *data;
218   GError *error = NULL;
219   char *type = NULL;
220
221   data = g_simple_async_result_get_op_res_gpointer (res);
222   
223   iface = G_LOADABLE_ICON_GET_IFACE (object);
224   stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
225
226   if (stream == NULL)
227     {
228       g_simple_async_result_set_from_error (res, error);
229       g_error_free (error);
230     }
231   else
232     {
233       data->stream = stream;
234       data->type = type;
235     }
236 }
237
238
239
240 static void
241 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
242                                  int                  size,
243                                  GCancellable        *cancellable,
244                                  GAsyncReadyCallback  callback,
245                                  gpointer             user_data)
246 {
247   GSimpleAsyncResult *res;
248   LoadData *data;
249   
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);
255 }
256
257 static GInputStream *
258 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
259                                   GAsyncResult         *res,
260                                   char                **type,
261                                   GError              **error)
262 {
263   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
264   LoadData *data;
265
266   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
267
268   data = g_simple_async_result_get_op_res_gpointer (simple);
269
270   if (type)
271     {
272       *type = data->type;
273       data->type = NULL;
274     }
275
276   return g_object_ref (data->stream);
277 }
278
279 #define __G_LOADABLE_ICON_C__
280 #include "gioaliasdef.c"