1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gloadableicon.h"
26 #include "gasyncresult.h"
32 * SECTION:gloadableicon
33 * @short_description: Loadable Icons
35 * @see_also: #GIcon, #GThemedIcon
37 * Extends the #GIcon interface and adds the ability to
38 * load icons from streams.
41 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
43 GCancellable *cancellable,
44 GAsyncReadyCallback callback,
46 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
51 typedef GLoadableIconIface GLoadableIconInterface;
52 G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
55 g_loadable_icon_default_init (GLoadableIconIface *iface)
57 iface->load_async = g_loadable_icon_real_load_async;
58 iface->load_finish = g_loadable_icon_real_load_finish;
62 * g_loadable_icon_load:
63 * @icon: a #GLoadableIcon.
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
69 * @error: a #GError location to store the error occurring, or %NULL
72 * Loads a loadable icon. For the asynchronous version of this function,
73 * see g_loadable_icon_load_async().
75 * Returns: (transfer full): a #GInputStream to read the icon from.
78 g_loadable_icon_load (GLoadableIcon *icon,
81 GCancellable *cancellable,
84 GLoadableIconIface *iface;
86 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
88 iface = G_LOADABLE_ICON_GET_IFACE (icon);
90 return (* iface->load) (icon, size, type, cancellable, error);
94 * g_loadable_icon_load_async:
95 * @icon: a #GLoadableIcon.
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
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().
107 g_loadable_icon_load_async (GLoadableIcon *icon,
109 GCancellable *cancellable,
110 GAsyncReadyCallback callback,
113 GLoadableIconIface *iface;
115 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
117 iface = G_LOADABLE_ICON_GET_IFACE (icon);
119 (* iface->load_async) (icon, size, cancellable, callback, user_data);
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
131 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
133 * Returns: (transfer full): a #GInputStream to read the icon from.
136 g_loadable_icon_load_finish (GLoadableIcon *icon,
141 GLoadableIconIface *iface;
143 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
144 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
146 if (g_async_result_legacy_propagate_error (res, error))
149 iface = G_LOADABLE_ICON_GET_IFACE (icon);
151 return (* iface->load_finish) (icon, res, type, error);
154 /********************************************
155 * Default implementation of async load *
156 ********************************************/
164 load_data_free (LoadData *data)
171 load_async_thread (GTask *task,
172 gpointer source_object,
174 GCancellable *cancellable)
176 GLoadableIcon *icon = source_object;
177 LoadData *data = task_data;
178 GLoadableIconIface *iface;
179 GInputStream *stream;
180 GError *error = NULL;
182 iface = G_LOADABLE_ICON_GET_IFACE (icon);
183 stream = iface->load (icon, data->size, &data->type,
184 cancellable, &error);
187 g_task_return_pointer (task, stream, g_object_unref);
189 g_task_return_error (task, error);
195 g_loadable_icon_real_load_async (GLoadableIcon *icon,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
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);
212 static GInputStream *
213 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
220 GInputStream *stream;
222 g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
225 data = g_task_get_task_data (task);
227 stream = g_task_propagate_pointer (task, error);