gloadableicon: Fix gir bindings for load_finish
[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 "gicon.h"
25 #include "gloadableicon.h"
26 #include "gasyncresult.h"
27 #include "gtask.h"
28 #include "glibintl.h"
29
30
31 /**
32  * SECTION:gloadableicon
33  * @short_description: Loadable Icons
34  * @include: gio/gio.h
35  * @see_also: #GIcon, #GThemedIcon
36  * 
37  * Extends the #GIcon interface and adds the ability to 
38  * load icons from streams.
39  **/
40
41 static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
42                                                        int                   size,
43                                                        GCancellable         *cancellable,
44                                                        GAsyncReadyCallback   callback,
45                                                        gpointer              user_data);
46 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
47                                                        GAsyncResult         *res,
48                                                        char                **type,
49                                                        GError              **error);
50
51 typedef GLoadableIconIface GLoadableIconInterface;
52 G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
53
54 static void
55 g_loadable_icon_default_init (GLoadableIconIface *iface)
56 {
57   iface->load_async = g_loadable_icon_real_load_async;
58   iface->load_finish = g_loadable_icon_real_load_finish;
59 }
60
61 /**
62  * g_loadable_icon_load:
63  * @icon: a #GLoadableIcon.
64  * @size: an integer.
65  * @type: (out) (allow-none): a location to store the type of the
66  *        loaded icon, %NULL to ignore.
67  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
68  * @error: a #GError location to store the error occurring, or %NULL to 
69  * ignore.
70  * 
71  * Loads a loadable icon. For the asynchronous version of this function, 
72  * see g_loadable_icon_load_async().
73  * 
74  * Returns: (transfer full): a #GInputStream to read the icon from.
75  **/
76 GInputStream *
77 g_loadable_icon_load (GLoadableIcon  *icon,
78                       int             size,
79                       char          **type,
80                       GCancellable   *cancellable,
81                       GError        **error)
82 {
83   GLoadableIconIface *iface;
84
85   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
86
87   iface = G_LOADABLE_ICON_GET_IFACE (icon);
88
89   return (* iface->load) (icon, size, type, cancellable, error);
90 }
91
92 /**
93  * g_loadable_icon_load_async:
94  * @icon: a #GLoadableIcon.
95  * @size: an integer.
96  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. 
97  * @callback: (scope async): a #GAsyncReadyCallback to call when the
98  *            request is satisfied
99  * @user_data: (closure): the data to pass to callback function
100  * 
101  * Loads an icon asynchronously. To finish this function, see 
102  * g_loadable_icon_load_finish(). For the synchronous, blocking 
103  * version of this function, see g_loadable_icon_load().
104  **/
105 void
106 g_loadable_icon_load_async (GLoadableIcon       *icon,
107                             int                  size,
108                             GCancellable        *cancellable,
109                             GAsyncReadyCallback  callback,
110                             gpointer             user_data)
111 {
112   GLoadableIconIface *iface;
113   
114   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
115
116   iface = G_LOADABLE_ICON_GET_IFACE (icon);
117
118   (* iface->load_async) (icon, size, cancellable, callback, user_data);
119 }
120
121 /**
122  * g_loadable_icon_load_finish:
123  * @icon: a #GLoadableIcon.
124  * @res: a #GAsyncResult.
125  * @type: (out) (allow-none): a location to store the type of the
126  *        loaded icon, %NULL to ignore.
127  * @error: a #GError location to store the error occurring, or %NULL to 
128  * ignore.
129  * 
130  * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
131  * 
132  * Returns: (transfer full): a #GInputStream to read the icon from.
133  **/
134 GInputStream *
135 g_loadable_icon_load_finish (GLoadableIcon  *icon,
136                              GAsyncResult   *res,
137                              char          **type,
138                              GError        **error)
139 {
140   GLoadableIconIface *iface;
141   
142   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
143   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
144
145   if (g_async_result_legacy_propagate_error (res, error))
146     return NULL;
147
148   iface = G_LOADABLE_ICON_GET_IFACE (icon);
149
150   return (* iface->load_finish) (icon, res, type, error);
151 }
152
153 /********************************************
154  *   Default implementation of async load   *
155  ********************************************/
156
157 typedef struct {
158   int size;
159   char *type;
160 } LoadData;
161
162 static void
163 load_data_free (LoadData *data)
164 {
165   g_free (data->type);
166   g_free (data);
167 }
168
169 static void
170 load_async_thread (GTask        *task,
171                    gpointer      source_object,
172                    gpointer      task_data,
173                    GCancellable *cancellable)
174 {
175   GLoadableIcon *icon = source_object;
176   LoadData *data = task_data;
177   GLoadableIconIface *iface;
178   GInputStream *stream;
179   GError *error = NULL;
180
181   iface = G_LOADABLE_ICON_GET_IFACE (icon);
182   stream = iface->load (icon, data->size, &data->type,
183                         cancellable, &error);
184
185   if (stream)
186     g_task_return_pointer (task, stream, g_object_unref);
187   else
188     g_task_return_error (task, error);
189 }
190
191
192
193 static void
194 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
195                                  int                  size,
196                                  GCancellable        *cancellable,
197                                  GAsyncReadyCallback  callback,
198                                  gpointer             user_data)
199 {
200   GTask *task;
201   LoadData *data;
202
203   task = g_task_new (icon, cancellable, callback, user_data);
204   data = g_new0 (LoadData, 1);
205   g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
206   g_task_run_in_thread (task, load_async_thread);
207   g_object_unref (task);
208 }
209
210 static GInputStream *
211 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
212                                   GAsyncResult         *res,
213                                   char                **type,
214                                   GError              **error)
215 {
216   GTask *task;
217   LoadData *data;
218   GInputStream *stream;
219
220   g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
221
222   task = G_TASK (res);
223   data = g_task_get_task_data (task);
224
225   stream = g_task_propagate_pointer (task, error);
226   if (stream && type)
227     {
228       *type = data->type;
229       data->type = NULL;
230     }
231
232   return stream;
233 }