Add new api to symbol lists and docs
[glib.git] / gio / gloadableicon.c
blobe8eec2d26e6b47508a32cb2b825c10d747631c5f
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
24 #include "gasyncresult.h"
25 #include "gsimpleasyncresult.h"
26 #include "gicon.h"
27 #include "gloadableicon.h"
28 #include "glibintl.h"
31 /**
32 * SECTION:gloadableicon
33 * @short_description: Loadable Icons
34 * @include: gio/gio.h
35 * @see_also: #GIcon, #GThemedIcon
37 * Extends the #GIcon interface and adds the ability to
38 * load icons from streams.
39 **/
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);
51 typedef GLoadableIconIface GLoadableIconInterface;
52 G_DEFINE_INTERFACE(GLoadableIcon, g_loadable_icon, G_TYPE_ICON)
54 static void
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;
61 /**
62 * g_loadable_icon_load:
63 * @icon: a #GLoadableIcon.
64 * @size: an integer.
65 * @type: (out) (allow-none): a location to store the type of the
66 * loaded icon, %NULL to ignore.
67 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
68 * @error: a #GError location to store the error occurring, or %NULL to
69 * ignore.
71 * Loads a loadable icon. For the asynchronous version of this function,
72 * see g_loadable_icon_load_async().
74 * Returns: (transfer full): a #GInputStream to read the icon from.
75 **/
76 GInputStream *
77 g_loadable_icon_load (GLoadableIcon *icon,
78 int size,
79 char **type,
80 GCancellable *cancellable,
81 GError **error)
83 GLoadableIconIface *iface;
85 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
87 iface = G_LOADABLE_ICON_GET_IFACE (icon);
89 return (* iface->load) (icon, size, type, cancellable, error);
92 /**
93 * g_loadable_icon_load_async:
94 * @icon: a #GLoadableIcon.
95 * @size: an integer.
96 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
97 * @callback: (scope async): a #GAsyncReadyCallback to call when the
98 * request is satisfied
99 * @user_data: (closure): the data to pass to callback function
101 * Loads an icon asynchronously. To finish this function, see
102 * g_loadable_icon_load_finish(). For the synchronous, blocking
103 * version of this function, see g_loadable_icon_load().
105 void
106 g_loadable_icon_load_async (GLoadableIcon *icon,
107 int size,
108 GCancellable *cancellable,
109 GAsyncReadyCallback callback,
110 gpointer user_data)
112 GLoadableIconIface *iface;
114 g_return_if_fail (G_IS_LOADABLE_ICON (icon));
116 iface = G_LOADABLE_ICON_GET_IFACE (icon);
118 (* iface->load_async) (icon, size, cancellable, callback, user_data);
122 * g_loadable_icon_load_finish:
123 * @icon: a #GLoadableIcon.
124 * @res: a #GAsyncResult.
125 * @type: a location to store the type of the loaded icon, %NULL to ignore.
126 * @error: a #GError location to store the error occurring, or %NULL to
127 * ignore.
129 * Finishes an asynchronous icon load started in g_loadable_icon_load_async().
131 * Returns: (transfer full): a #GInputStream to read the icon from.
133 GInputStream *
134 g_loadable_icon_load_finish (GLoadableIcon *icon,
135 GAsyncResult *res,
136 char **type,
137 GError **error)
139 GLoadableIconIface *iface;
141 g_return_val_if_fail (G_IS_LOADABLE_ICON (icon), NULL);
142 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
144 if (g_async_result_legacy_propagate_error (res, error))
145 return NULL;
147 iface = G_LOADABLE_ICON_GET_IFACE (icon);
149 return (* iface->load_finish) (icon, res, type, error);
152 /********************************************
153 * Default implementation of async load *
154 ********************************************/
156 typedef struct {
157 int size;
158 char *type;
159 GInputStream *stream;
160 } LoadData;
162 static void
163 load_data_free (LoadData *data)
165 if (data->stream)
166 g_object_unref (data->stream);
167 g_free (data->type);
168 g_free (data);
171 static void
172 load_async_thread (GSimpleAsyncResult *res,
173 GObject *object,
174 GCancellable *cancellable)
176 GLoadableIconIface *iface;
177 GInputStream *stream;
178 LoadData *data;
179 GError *error = NULL;
180 char *type = NULL;
182 data = g_simple_async_result_get_op_res_gpointer (res);
184 iface = G_LOADABLE_ICON_GET_IFACE (object);
185 stream = iface->load (G_LOADABLE_ICON (object), data->size, &type, cancellable, &error);
187 if (stream == NULL)
189 g_simple_async_result_take_error (res, error);
191 else
193 data->stream = stream;
194 data->type = type;
200 static void
201 g_loadable_icon_real_load_async (GLoadableIcon *icon,
202 int size,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
205 gpointer user_data)
207 GSimpleAsyncResult *res;
208 LoadData *data;
210 res = g_simple_async_result_new (G_OBJECT (icon), callback, user_data, g_loadable_icon_real_load_async);
211 data = g_new0 (LoadData, 1);
212 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify) load_data_free);
213 g_simple_async_result_run_in_thread (res, load_async_thread, 0, cancellable);
214 g_object_unref (res);
217 static GInputStream *
218 g_loadable_icon_real_load_finish (GLoadableIcon *icon,
219 GAsyncResult *res,
220 char **type,
221 GError **error)
223 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
224 LoadData *data;
226 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_loadable_icon_real_load_async);
228 if (g_simple_async_result_propagate_error (simple, error))
229 return NULL;
231 data = g_simple_async_result_get_op_res_gpointer (simple);
233 if (type)
235 *type = data->type;
236 data->type = NULL;
239 return g_object_ref (data->stream);