1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "gloadableicon.h"
24 #include "gasyncresult.h"
30 * SECTION:gloadableicon
31 * @short_description: Loadable Icons
33 * @see_also: #GIcon, #GThemedIcon
35 * Extends the #GIcon interface and adds the ability to
36 * load icons from streams.
39 static void g_loadable_icon_real_load_async (GLoadableIcon *icon,
41 GCancellable *cancellable,
42 GAsyncReadyCallback callback,
44 static GInputStream *g_loadable_icon_real_load_finish (GLoadableIcon *icon,
49 typedef GLoadableIconIface GLoadableIconInterface;
50 G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
53 g_loadable_icon_default_init (GLoadableIconIface *iface)
55 iface->load_async = g_loadable_icon_real_load_async;
56 iface->load_finish = g_loadable_icon_real_load_finish;
60 * g_loadable_icon_load:
61 * @icon: a #GLoadableIcon.
63 * @type: (out) (allow-none): a location to store the type of the
64 * loaded icon, %NULL to ignore.
65 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
66 * @error: a #GError location to store the error occurring, or %NULL to
69 * Loads a loadable icon. For the asynchronous version of this function,
70 * see g_loadable_icon_load_async().
72 * Returns: (transfer full): a #GInputStream to read the icon from.
75 g_loadable_icon_load (GLoadableIcon *icon,
78 GCancellable *cancellable,
81 GLoadableIconIface *iface;
83 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
85 iface = G_LOADABLE_ICON_GET_IFACE (icon);
87 return (* iface->load) (icon, size, type, cancellable, error);
91 * g_loadable_icon_load_async:
92 * @icon: a #GLoadableIcon.
94 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
95 * @callback: (scope async): a #GAsyncReadyCallback to call when the
96 * request is satisfied
97 * @user_data: (closure): the data to pass to callback function
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().
104 g_loadable_icon_load_async (GLoadableIcon *icon,
106 GCancellable *cancellable,
107 GAsyncReadyCallback callback,
110 GLoadableIconIface *iface;
112 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
114 iface = G_LOADABLE_ICON_GET_IFACE (icon);
116 (* iface->load_async) (icon, size, cancellable, callback, user_data);
120 * g_loadable_icon_load_finish:
121 * @icon: a #GLoadableIcon.
122 * @res: a #GAsyncResult.
123 * @type: (out) (allow-none): a location to store the type of the
124 * loaded icon, %NULL to ignore.
125 * @error: a #GError location to store the error occurring, or %NULL to
128 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
130 * Returns: (transfer full): a #GInputStream to read the icon from.
133 g_loadable_icon_load_finish (GLoadableIcon *icon,
138 GLoadableIconIface *iface;
140 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
141 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
143 if (g_async_result_legacy_propagate_error (res, error))
146 iface = G_LOADABLE_ICON_GET_IFACE (icon);
148 return (* iface->load_finish) (icon, res, type, error);
151 /********************************************
152 * Default implementation of async load *
153 ********************************************/
161 load_data_free (LoadData *data)
168 load_async_thread (GTask *task,
169 gpointer source_object,
171 GCancellable *cancellable)
173 GLoadableIcon *icon = source_object;
174 LoadData *data = task_data;
175 GLoadableIconIface *iface;
176 GInputStream *stream;
177 GError *error = NULL;
179 iface = G_LOADABLE_ICON_GET_IFACE (icon);
180 stream = iface->load (icon, data->size, &data->type,
181 cancellable, &error);
184 g_task_return_pointer (task, stream, g_object_unref);
186 g_task_return_error (task, error);
192 g_loadable_icon_real_load_async (GLoadableIcon *icon,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
201 task = g_task_new (icon, cancellable, callback, user_data);
202 data = g_new0 (LoadData, 1);
203 g_task_set_task_data (task, data, (GDestroyNotify) load_data_free);
204 g_task_run_in_thread (task, load_async_thread);
205 g_object_unref (task);
208 static GInputStream *
209 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
216 GInputStream *stream;
218 g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
221 data = g_task_get_task_data (task);
223 stream = g_task_propagate_pointer (task, error);