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
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:  a location to store the type of the loaded icon, %NULL to ignore.
66  * @cancellable: optional #GCancellable object, %NULL to ignore. 
67  * @error: a #GError location to store the error occuring, or %NULL to 
68  * ignore.
69  * 
70  * Loads a loadable icon. For the asynchronous version of this function, 
71  * see g_loadable_icon_load_async().
72  * 
73  * Returns: (transfer full): a #GInputStream to read the icon from.
74  **/
75 GInputStream *
76 g_loadable_icon_load (GLoadableIcon  *icon,
77                       int             size,
78                       char          **type,
79                       GCancellable   *cancellable,
80                       GError        **error)
81 {
82   GLoadableIconIface *iface;
83
84   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
85
86   iface = G_LOADABLE_ICON_GET_IFACE (icon);
87
88   return (* iface->load) (icon, size, type, cancellable, error);
89 }
90
91 /**
92  * g_loadable_icon_load_async:
93  * @icon: a #GLoadableIcon.
94  * @size: an integer.
95  * @cancellable: optional #GCancellable object, %NULL to ignore. 
96  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
97  * @user_data: the data to pass to callback function
98  * 
99  * Loads an icon asynchronously. To finish this function, see 
100  * g_loadable_icon_load_finish(). For the synchronous, blocking 
101  * version of this function, see g_loadable_icon_load().
102  **/
103 void
104 g_loadable_icon_load_async (GLoadableIcon       *icon,
105                             int                  size,
106                             GCancellable        *cancellable,
107                             GAsyncReadyCallback  callback,
108                             gpointer             user_data)
109 {
110   GLoadableIconIface *iface;
111   
112   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
113
114   iface = G_LOADABLE_ICON_GET_IFACE (icon);
115
116   (* iface->load_async) (icon, size, cancellable, callback, user_data);
117 }
118
119 /**
120  * g_loadable_icon_load_finish:
121  * @icon: a #GLoadableIcon.
122  * @res: a #GAsyncResult.
123  * @type: a location to store the type of the loaded icon, %NULL to ignore.
124  * @error: a #GError location to store the error occuring, or %NULL to 
125  * ignore.
126  * 
127  * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
128  * 
129  * Returns: (transfer full): a #GInputStream to read the icon from.
130  **/
131 GInputStream *
132 g_loadable_icon_load_finish (GLoadableIcon  *icon,
133                              GAsyncResult   *res,
134                              char          **type,
135                              GError        **error)
136 {
137   GLoadableIconIface *iface;
138   
139   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
140   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
141
142   if (G_IS_SIMPLE_ASYNC_RESULT (res))
143     {
144       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
145       if (g_simple_async_result_propagate_error (simple, error))
146         return NULL;
147     }
148   
149   iface = G_LOADABLE_ICON_GET_IFACE (icon);
150
151   return (* iface->load_finish) (icon, res, type, error);
152 }
153
154 /********************************************
155  *   Default implementation of async load   *
156  ********************************************/
157
158 typedef struct {
159   int size;
160   char *type;
161   GInputStream *stream;
162 } LoadData;
163
164 static void
165 load_data_free (LoadData *data)
166 {
167   if (data->stream)
168     g_object_unref (data->stream);
169   g_free (data->type);
170   g_free (data);
171 }
172
173 static void
174 load_async_thread (GSimpleAsyncResult *res,
175                    GObject            *object,
176                    GCancellable       *cancellable)
177 {
178   GLoadableIconIface *iface;
179   GInputStream *stream;
180   LoadData *data;
181   GError *error = NULL;
182   char *type = NULL;
183
184   data = g_simple_async_result_get_op_res_gpointer (res);
185   
186   iface = G_LOADABLE_ICON_GET_IFACE (object);
187   stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
188
189   if (stream == NULL)
190     {
191       g_simple_async_result_set_from_error (res, error);
192       g_error_free (error);
193     }
194   else
195     {
196       data->stream = stream;
197       data->type = type;
198     }
199 }
200
201
202
203 static void
204 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
205                                  int                  size,
206                                  GCancellable        *cancellable,
207                                  GAsyncReadyCallback  callback,
208                                  gpointer             user_data)
209 {
210   GSimpleAsyncResult *res;
211   LoadData *data;
212   
213   res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
214   data = g_new0 (LoadData, 1);
215   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
216   g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
217   g_object_unref (res);
218 }
219
220 static GInputStream *
221 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
222                                   GAsyncResult         *res,
223                                   char                **type,
224                                   GError              **error)
225 {
226   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
227   LoadData *data;
228
229   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
230
231   data = g_simple_async_result_get_op_res_gpointer (simple);
232
233   if (type)
234     {
235       *type = data->type;
236       data->type = NULL;
237     }
238
239   return g_object_ref (data->stream);
240 }