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