build: Enable -fno-strict-aliasing
[glib.git] / gio / gbytesicon.c
blobca78be1be76f7875aa50b5bd00d415173a131d91
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2013 Canonical Limited
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.
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: Ryan Lortie <desrt@desrt.ca>
21 #include "config.h"
23 #include "gbytesicon.h"
24 #include "gbytes.h"
25 #include "gicon.h"
26 #include "glibintl.h"
27 #include "gloadableicon.h"
28 #include "gmemoryinputstream.h"
29 #include "gtask.h"
30 #include "gioerror.h"
33 /**
34 * SECTION:gbytesicon
35 * @short_description: An icon stored in memory as a GBytes
36 * @include: gio/gio.h
37 * @see_also: #GIcon, #GLoadableIcon, #GBytes
39 * #GBytesIcon specifies an image held in memory in a common format (usually
40 * png) to be used as icon.
42 * Since: 2.38
43 **/
45 typedef GObjectClass GBytesIconClass;
47 struct _GBytesIcon
49 GObject parent_instance;
51 GBytes *bytes;
54 enum
56 PROP_0,
57 PROP_BYTES
60 static void g_bytes_icon_icon_iface_init (GIconIface *iface);
61 static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
62 G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
63 G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
64 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
66 static void
67 g_bytes_icon_get_property (GObject *object,
68 guint prop_id,
69 GValue *value,
70 GParamSpec *pspec)
72 GBytesIcon *icon = G_BYTES_ICON (object);
74 switch (prop_id)
76 case PROP_BYTES:
77 g_value_set_boxed (value, icon->bytes);
78 break;
80 default:
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 static void
86 g_bytes_icon_set_property (GObject *object,
87 guint prop_id,
88 const GValue *value,
89 GParamSpec *pspec)
91 GBytesIcon *icon = G_BYTES_ICON (object);
93 switch (prop_id)
95 case PROP_BYTES:
96 icon->bytes = g_value_dup_boxed (value);
97 break;
99 default:
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 static void
105 g_bytes_icon_finalize (GObject *object)
107 GBytesIcon *icon;
109 icon = G_BYTES_ICON (object);
111 g_bytes_unref (icon->bytes);
113 G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
116 static void
117 g_bytes_icon_class_init (GBytesIconClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 gobject_class->get_property = g_bytes_icon_get_property;
122 gobject_class->set_property = g_bytes_icon_set_property;
123 gobject_class->finalize = g_bytes_icon_finalize;
126 * GBytesIcon:bytes:
128 * The bytes containing the icon.
130 g_object_class_install_property (gobject_class, PROP_BYTES,
131 g_param_spec_boxed ("bytes",
132 P_("bytes"),
133 P_("The bytes containing the icon"),
134 G_TYPE_BYTES,
135 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 static void
139 g_bytes_icon_init (GBytesIcon *bytes)
144 * g_bytes_icon_new:
145 * @bytes: a #GBytes.
147 * Creates a new icon for a bytes.
149 * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
150 * @bytes, or %NULL on error.
152 * Since: 2.38
154 GIcon *
155 g_bytes_icon_new (GBytes *bytes)
157 g_return_val_if_fail (bytes != NULL, NULL);
159 return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
163 * g_bytes_icon_get_bytes:
164 * @icon: a #GIcon.
166 * Gets the #GBytes associated with the given @icon.
168 * Returns: (transfer none): a #GBytes, or %NULL.
170 * Since: 2.38
172 GBytes *
173 g_bytes_icon_get_bytes (GBytesIcon *icon)
175 g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
177 return icon->bytes;
180 static guint
181 g_bytes_icon_hash (GIcon *icon)
183 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
185 return g_bytes_hash (bytes_icon->bytes);
188 static gboolean
189 g_bytes_icon_equal (GIcon *icon1,
190 GIcon *icon2)
192 GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
193 GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
195 return g_bytes_equal (bytes1->bytes, bytes2->bytes);
198 static GVariant *
199 g_bytes_icon_serialize (GIcon *icon)
201 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
203 return g_variant_new ("(sv)", "bytes",
204 g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE));
207 static void
208 g_bytes_icon_icon_iface_init (GIconIface *iface)
210 iface->hash = g_bytes_icon_hash;
211 iface->equal = g_bytes_icon_equal;
212 iface->serialize = g_bytes_icon_serialize;
215 static GInputStream *
216 g_bytes_icon_load (GLoadableIcon *icon,
217 int size,
218 char **type,
219 GCancellable *cancellable,
220 GError **error)
222 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
224 if (type)
225 *type = NULL;
227 return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
230 static void
231 g_bytes_icon_load_async (GLoadableIcon *icon,
232 int size,
233 GCancellable *cancellable,
234 GAsyncReadyCallback callback,
235 gpointer user_data)
237 GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
238 GTask *task;
240 task = g_task_new (icon, cancellable, callback, user_data);
241 g_task_set_source_tag (task, g_bytes_icon_load_async);
242 g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
243 g_object_unref (task);
246 static GInputStream *
247 g_bytes_icon_load_finish (GLoadableIcon *icon,
248 GAsyncResult *res,
249 char **type,
250 GError **error)
252 g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
254 if (type)
255 *type = NULL;
257 return g_task_propagate_pointer (G_TASK (res), error);
260 static void
261 g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
263 iface->load = g_bytes_icon_load;
264 iface->load_async = g_bytes_icon_load_async;
265 iface->load_finish = g_bytes_icon_load_finish;