Change LGPL-2.1+ to LGPL-2.1-or-later
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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) (optional): a location to store the type of the loaded
66  * icon, %NULL to ignore.
67  * @cancellable: (nullable): optional #GCancellable object, %NULL to
68  * ignore.
69  * @error: a #GError location to store the error occurring, or %NULL
70  * to ignore.
71  * 
72  * Loads a loadable icon. For the asynchronous version of this function, 
73  * see g_loadable_icon_load_async().
74  * 
75  * Returns: (transfer full): a #GInputStream to read the icon from.
76  **/
77 GInputStream *
78 g_loadable_icon_load (GLoadableIcon  *icon,
79                       int             size,
80                       char          **type,
81                       GCancellable   *cancellable,
82                       GError        **error)
83 {
84   GLoadableIconIface *iface;
85
86   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
87
88   iface = G_LOADABLE_ICON_GET_IFACE (icon);
89
90   return (* iface->load) (icon, size, type, cancellable, error);
91 }
92
93 /**
94  * g_loadable_icon_load_async:
95  * @icon: a #GLoadableIcon.
96  * @size: an integer.
97  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. 
98  * @callback: (scope async): a #GAsyncReadyCallback
99  *   to call when the request is satisfied
100  * @user_data: the data to pass to callback function
101  * 
102  * Loads an icon asynchronously. To finish this function, see 
103  * g_loadable_icon_load_finish(). For the synchronous, blocking 
104  * version of this function, see g_loadable_icon_load().
105  **/
106 void
107 g_loadable_icon_load_async (GLoadableIcon       *icon,
108                             int                  size,
109                             GCancellable        *cancellable,
110                             GAsyncReadyCallback  callback,
111                             gpointer             user_data)
112 {
113   GLoadableIconIface *iface;
114   
115   g_return_if_fail (G_IS_LOADABLE_ICON (icon));
116
117   iface = G_LOADABLE_ICON_GET_IFACE (icon);
118
119   (* iface->load_async) (icon, size, cancellable, callback, user_data);
120 }
121
122 /**
123  * g_loadable_icon_load_finish:
124  * @icon: a #GLoadableIcon.
125  * @res: a #GAsyncResult.
126  * @type: (out) (optional): a location to store the type of the loaded
127  *        icon, %NULL to ignore.
128  * @error: a #GError location to store the error occurring, or %NULL to 
129  * ignore.
130  * 
131  * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
132  * 
133  * Returns: (transfer full): a #GInputStream to read the icon from.
134  **/
135 GInputStream *
136 g_loadable_icon_load_finish (GLoadableIcon  *icon,
137                              GAsyncResult   *res,
138                              char          **type,
139                              GError        **error)
140 {
141   GLoadableIconIface *iface;
142   
143   g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
144   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
145
146   if (g_async_result_legacy_propagate_error (res, error))
147     return NULL;
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 } LoadData;
162
163 static void
164 load_data_free (LoadData *data)
165 {
166   g_free (data->type);
167   g_free (data);
168 }
169
170 static void
171 load_async_thread (GTask        *task,
172                    gpointer      source_object,
173                    gpointer      task_data,
174                    GCancellable *cancellable)
175 {
176   GLoadableIcon *icon = source_object;
177   LoadData *data = task_data;
178   GLoadableIconIface *iface;
179   GInputStream *stream;
180   GError *error = NULL;
181
182   iface = G_LOADABLE_ICON_GET_IFACE (icon);
183   stream = iface->load (icon, data->size, &data->type,
184                         cancellable, &error);
185
186   if (stream)
187     g_task_return_pointer (task, stream, g_object_unref);
188   else
189     g_task_return_error (task, error);
190 }
191
192
193
194 static void
195 g_loadable_icon_real_load_async (GLoadableIcon       *icon,
196                                  int                  size,
197                                  GCancellable        *cancellable,
198                                  GAsyncReadyCallback  callback,
199                                  gpointer             user_data)
200 {
201   GTask *task;
202   LoadData *data;
203
204   task = g_task_new (icon, cancellable, callback, user_data);
205   g_task_set_source_tag (task, g_loadable_icon_real_load_async);
206   data = g_new0 (LoadData, 1);
207   g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
208   g_task_run_in_thread (task, load_async_thread);
209   g_object_unref (task);
210 }
211
212 static GInputStream *
213 g_loadable_icon_real_load_finish (GLoadableIcon        *icon,
214                                   GAsyncResult         *res,
215                                   char                **type,
216                                   GError              **error)
217 {
218   GTask *task;
219   LoadData *data;
220   GInputStream *stream;
221
222   g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
223
224   task = G_TASK (res);
225   data = g_task_get_task_data (task);
226
227   stream = g_task_propagate_pointer (task, error);
228   if (stream && type)
229     {
230       *type = data->type;
231       data->type = NULL;
232     }
233
234   return stream;
235 }