1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2013 Canonical Limited
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: Ryan Lortie <desrt@desrt.ca>
25 #include "gbytesicon.h"
29 #include "gloadableicon.h"
30 #include "gmemoryinputstream.h"
37 * @short_description: An icon stored in memory as a GBytes
39 * @see_also: #GIcon, #GLoadableIcon, #GBytes
41 * #GBytesIcon specifies an image held in memory in a common format (usually
42 * png) to be used as icon.
47 typedef GObjectClass GBytesIconClass;
51 GObject parent_instance;
62 static void g_bytes_icon_icon_iface_init (GIconIface *iface);
63 static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
64 G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
66 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
69 g_bytes_icon_get_property (GObject *object,
74 GBytesIcon *icon = G_BYTES_ICON (object);
79 g_value_set_boxed (value, icon->bytes);
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 g_bytes_icon_set_property (GObject *object,
93 GBytesIcon *icon = G_BYTES_ICON (object);
98 icon->bytes = g_value_dup_boxed (value);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 g_bytes_icon_finalize (GObject *object)
111 icon = G_BYTES_ICON (object);
113 g_bytes_unref (icon->bytes);
115 G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
119 g_bytes_icon_class_init (GBytesIconClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123 gobject_class->get_property = g_bytes_icon_get_property;
124 gobject_class->set_property = g_bytes_icon_set_property;
125 gobject_class->finalize = g_bytes_icon_finalize;
130 * The bytes containing the icon.
132 g_object_class_install_property (gobject_class, PROP_BYTES,
133 g_param_spec_boxed ("bytes",
135 P_("The bytes containing the icon"),
137 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
141 g_bytes_icon_init (GBytesIcon *bytes)
149 * Creates a new icon for a bytes.
151 * This cannot fail, but loading and interpreting the bytes may fail later on
152 * (for example, if g_loadable_icon_load() is called) if the image is invalid.
154 * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
160 g_bytes_icon_new (GBytes *bytes)
162 g_return_val_if_fail (bytes != NULL, NULL);
164 return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
168 * g_bytes_icon_get_bytes:
171 * Gets the #GBytes associated with the given @icon.
173 * Returns: (transfer none): a #GBytes.
178 g_bytes_icon_get_bytes (GBytesIcon *icon)
180 g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
186 g_bytes_icon_hash (GIcon *icon)
188 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
190 return g_bytes_hash (bytes_icon->bytes);
194 g_bytes_icon_equal (GIcon *icon1,
197 GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
198 GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
200 return g_bytes_equal (bytes1->bytes, bytes2->bytes);
204 g_bytes_icon_serialize (GIcon *icon)
206 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
208 return g_variant_new ("(sv)", "bytes",
209 g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE));
213 g_bytes_icon_icon_iface_init (GIconIface *iface)
215 iface->hash = g_bytes_icon_hash;
216 iface->equal = g_bytes_icon_equal;
217 iface->serialize = g_bytes_icon_serialize;
220 static GInputStream *
221 g_bytes_icon_load (GLoadableIcon *icon,
224 GCancellable *cancellable,
227 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
232 return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
236 g_bytes_icon_load_async (GLoadableIcon *icon,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
242 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
245 task = g_task_new (icon, cancellable, callback, user_data);
246 g_task_set_source_tag (task, g_bytes_icon_load_async);
247 g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
248 g_object_unref (task);
251 static GInputStream *
252 g_bytes_icon_load_finish (GLoadableIcon *icon,
257 g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
262 return g_task_propagate_pointer (G_TASK (res), error);
266 g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
268 iface->load = g_bytes_icon_load;
269 iface->load_async = g_bytes_icon_load_async;
270 iface->load_finish = g_bytes_icon_load_finish;