Merge remote branch 'gvdb/master'
[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 "gasyncresult.h"
25 #include "gsimpleasyncresult.h"
26 #include "gicon.h"
27 #include "gloadableicon.h"
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:gloadableicon
34  * @short_description: Loadable Icons
35  * @include: gio/gio.h
36  * @see_also: #GIcon, #GThemedIcon
37  * 
38  * Extends the #GIcon interface and adds the ability to 
39  * load icons from streams.
40  **/
41
42 static void          g_loadable_icon_real_load_async  (GLoadableIcon        *icon,
43                                                        int                   size,
44                                                        GCancellable         *cancellable,
45                                                        GAsyncReadyCallback   callback,
46                                                        gpointer              user_data);
47 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
48                                                        GAsyncResult         *res,
49                                                        char                **type,
50                                                        GError              **error);
51
52 typedef GLoadableIconIface GLoadableIconInterface;
53 G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
54
55 static void
56 g_loadable_icon_default_init (GLoadableIconIface *iface)
57 {
58   iface->load_async = g_loadable_icon_real_load_async;
59   iface->load_finish = g_loadable_icon_real_load_finish;
60 }
61
62 /**
63  * g_loadable_icon_load:
64  * @icon: a #GLoadableIcon.
65  * @size: an integer.
66  * @type:  a location to store the type of the loaded icon, %NULL to ignore.
67  * @cancellable: optional #GCancellable object, %NULL to ignore. 
68  * @error: a #GError location to store the error occuring, 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: 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: optional #GCancellable object, %NULL to ignore. 
97  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
98  * @user_data: the data to pass to callback function
99  * 
100  * Loads an icon asynchronously. To finish this function, see 
101  * g_loadable_icon_load_finish(). For the synchronous, blocking 
102  * version of this function, see g_loadable_icon_load().
103  **/
104 void
105 g_loadable_icon_load_async (GLoadableIcon       *icon,
106                             int                  size,
107                             GCancellable        *cancellable,
108                             GAsyncReadyCallback  callback,
109                             gpointer             user_data)
110 {
111   GLoadableIconIface *iface;
112   
113   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
114
115   iface = G_LOADABLE_ICON_GET_IFACE (icon);
116
117   (* iface->load_async) (icon, size, cancellable, callback, user_data);
118 }
119
120 /**
121  * g_loadable_icon_load_finish:
122  * @icon: a #GLoadableIcon.
123  * @res: a #GAsyncResult.
124  * @type: a location to store the type of the loaded icon, %NULL to ignore.
125  * @error: a #GError location to store the error occuring, or %NULL to 
126  * ignore.
127  * 
128  * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
129  * 
130  * Returns: a #GInputStream to read the icon from.
131  **/
132 GInputStream *
133 g_loadable_icon_load_finish (GLoadableIcon  *icon,
134                              GAsyncResult   *res,
135                              char          **type,
136                              GError        **error)
137 {
138   GLoadableIconIface *iface;
139   
140   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
141   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
142
143   if (G_IS_SIMPLE_ASYNC_RESULT (res))
144     {
145       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
146       if (g_simple_async_result_propagate_error (simple, error))
147         return NULL;
148     }
149   
150   iface = G_LOADABLE_ICON_GET_IFACE (icon);
151
152   return (* iface->load_finish) (icon, res, type, error);
153 }
154
155 /********************************************
156  *   Default implementation of async load   *
157  ********************************************/
158
159 typedef struct {
160   int size;
161   char *type;
162   GInputStream *stream;
163 } LoadData;
164
165 static void
166 load_data_free (LoadData *data)
167 {
168   if (data->stream)
169     g_object_unref (data->stream);
170   g_free (data->type);
171   g_free (data);
172 }
173
174 static void
175 load_async_thread (GSimpleAsyncResult *res,
176                    GObject            *object,
177                    GCancellable       *cancellable)
178 {
179   GLoadableIconIface *iface;
180   GInputStream *stream;
181   LoadData *data;
182   GError *error = NULL;
183   char *type = NULL;
184
185   data = g_simple_async_result_get_op_res_gpointer (res);
186   
187   iface = G_LOADABLE_ICON_GET_IFACE (object);
188   stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
189
190   if (stream == NULL)
191     {
192       g_simple_async_result_set_from_error (res, error);
193       g_error_free (error);
194     }
195   else
196     {
197       data->stream = stream;
198       data->type = type;
199     }
200 }
201
202
203
204 static void
205 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
206                                  int                  size,
207                                  GCancellable        *cancellable,
208                                  GAsyncReadyCallback  callback,
209                                  gpointer             user_data)
210 {
211   GSimpleAsyncResult *res;
212   LoadData *data;
213   
214   res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
215   data = g_new0 (LoadData, 1);
216   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
217   g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
218   g_object_unref (res);
219 }
220
221 static GInputStream *
222 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
223                                   GAsyncResult         *res,
224                                   char                **type,
225                                   GError              **error)
226 {
227   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
228   LoadData *data;
229
230   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
231
232   data = g_simple_async_result_get_op_res_gpointer (simple);
233
234   if (type)
235     {
236       *type = data->type;
237       data->type = NULL;
238     }
239
240   return g_object_ref (data->stream);
241 }
242
243 #define __G_LOADABLE_ICON_C__
244 #include "gioaliasdef.c"