1 | /* GIO - GLib Input, Output and Streaming Library |
2 | * |
3 | * Copyright © 2013 Canonical Limited |
4 | * |
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. |
9 | * |
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. |
14 | * |
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/>. |
17 | * |
18 | * Author: Ryan Lortie <desrt@desrt.ca> |
19 | */ |
20 | |
21 | #include "config.h" |
22 | |
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" |
31 | |
32 | |
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 |
38 | * |
39 | * #GBytesIcon specifies an image held in memory in a common format (usually |
40 | * png) to be used as icon. |
41 | * |
42 | * Since: 2.38 |
43 | **/ |
44 | |
45 | typedef GObjectClass GBytesIconClass; |
46 | |
47 | struct _GBytesIcon |
48 | { |
49 | GObject parent_instance; |
50 | |
51 | GBytes *bytes; |
52 | }; |
53 | |
54 | enum |
55 | { |
56 | PROP_0, |
57 | PROP_BYTES |
58 | }; |
59 | |
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)) |
65 | |
66 | static void |
67 | g_bytes_icon_get_property (GObject *object, |
68 | guint prop_id, |
69 | GValue *value, |
70 | GParamSpec *pspec) |
71 | { |
72 | GBytesIcon *icon = G_BYTES_ICON (object); |
73 | |
74 | switch (prop_id) |
75 | { |
76 | case PROP_BYTES: |
77 | g_value_set_boxed (value, v_boxed: icon->bytes); |
78 | break; |
79 | |
80 | default: |
81 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
82 | } |
83 | } |
84 | |
85 | static void |
86 | g_bytes_icon_set_property (GObject *object, |
87 | guint prop_id, |
88 | const GValue *value, |
89 | GParamSpec *pspec) |
90 | { |
91 | GBytesIcon *icon = G_BYTES_ICON (object); |
92 | |
93 | switch (prop_id) |
94 | { |
95 | case PROP_BYTES: |
96 | icon->bytes = g_value_dup_boxed (value); |
97 | break; |
98 | |
99 | default: |
100 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
101 | } |
102 | } |
103 | |
104 | static void |
105 | g_bytes_icon_finalize (GObject *object) |
106 | { |
107 | GBytesIcon *icon; |
108 | |
109 | icon = G_BYTES_ICON (object); |
110 | |
111 | g_bytes_unref (bytes: icon->bytes); |
112 | |
113 | G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object); |
114 | } |
115 | |
116 | static void |
117 | g_bytes_icon_class_init (GBytesIconClass *klass) |
118 | { |
119 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
120 | |
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; |
124 | |
125 | /** |
126 | * GBytesIcon:bytes: |
127 | * |
128 | * The bytes containing the icon. |
129 | */ |
130 | g_object_class_install_property (oclass: gobject_class, property_id: PROP_BYTES, |
131 | pspec: g_param_spec_boxed (name: "bytes" , |
132 | P_("bytes" ), |
133 | P_("The bytes containing the icon" ), |
134 | G_TYPE_BYTES, |
135 | flags: G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
136 | } |
137 | |
138 | static void |
139 | g_bytes_icon_init (GBytesIcon *bytes) |
140 | { |
141 | } |
142 | |
143 | /** |
144 | * g_bytes_icon_new: |
145 | * @bytes: a #GBytes. |
146 | * |
147 | * Creates a new icon for a bytes. |
148 | * |
149 | * This cannot fail, but loading and interpreting the bytes may fail later on |
150 | * (for example, if g_loadable_icon_load() is called) if the image is invalid. |
151 | * |
152 | * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given |
153 | * @bytes. |
154 | * |
155 | * Since: 2.38 |
156 | **/ |
157 | GIcon * |
158 | g_bytes_icon_new (GBytes *bytes) |
159 | { |
160 | g_return_val_if_fail (bytes != NULL, NULL); |
161 | |
162 | return g_object_new (G_TYPE_BYTES_ICON, first_property_name: "bytes" , bytes, NULL); |
163 | } |
164 | |
165 | /** |
166 | * g_bytes_icon_get_bytes: |
167 | * @icon: a #GIcon. |
168 | * |
169 | * Gets the #GBytes associated with the given @icon. |
170 | * |
171 | * Returns: (transfer none): a #GBytes. |
172 | * |
173 | * Since: 2.38 |
174 | **/ |
175 | GBytes * |
176 | g_bytes_icon_get_bytes (GBytesIcon *icon) |
177 | { |
178 | g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL); |
179 | |
180 | return icon->bytes; |
181 | } |
182 | |
183 | static guint |
184 | g_bytes_icon_hash (GIcon *icon) |
185 | { |
186 | GBytesIcon *bytes_icon = G_BYTES_ICON (icon); |
187 | |
188 | return g_bytes_hash (bytes: bytes_icon->bytes); |
189 | } |
190 | |
191 | static gboolean |
192 | g_bytes_icon_equal (GIcon *icon1, |
193 | GIcon *icon2) |
194 | { |
195 | GBytesIcon *bytes1 = G_BYTES_ICON (icon1); |
196 | GBytesIcon *bytes2 = G_BYTES_ICON (icon2); |
197 | |
198 | return g_bytes_equal (bytes1: bytes1->bytes, bytes2: bytes2->bytes); |
199 | } |
200 | |
201 | static GVariant * |
202 | g_bytes_icon_serialize (GIcon *icon) |
203 | { |
204 | GBytesIcon *bytes_icon = G_BYTES_ICON (icon); |
205 | |
206 | return g_variant_new (format_string: "(sv)" , "bytes" , |
207 | g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes: bytes_icon->bytes, TRUE)); |
208 | } |
209 | |
210 | static void |
211 | g_bytes_icon_icon_iface_init (GIconIface *iface) |
212 | { |
213 | iface->hash = g_bytes_icon_hash; |
214 | iface->equal = g_bytes_icon_equal; |
215 | iface->serialize = g_bytes_icon_serialize; |
216 | } |
217 | |
218 | static GInputStream * |
219 | g_bytes_icon_load (GLoadableIcon *icon, |
220 | int size, |
221 | char **type, |
222 | GCancellable *cancellable, |
223 | GError **error) |
224 | { |
225 | GBytesIcon *bytes_icon = G_BYTES_ICON (icon); |
226 | |
227 | if (type) |
228 | *type = NULL; |
229 | |
230 | return g_memory_input_stream_new_from_bytes (bytes: bytes_icon->bytes); |
231 | } |
232 | |
233 | static void |
234 | g_bytes_icon_load_async (GLoadableIcon *icon, |
235 | int size, |
236 | GCancellable *cancellable, |
237 | GAsyncReadyCallback callback, |
238 | gpointer user_data) |
239 | { |
240 | GBytesIcon *bytes_icon = G_BYTES_ICON (icon); |
241 | GTask *task; |
242 | |
243 | task = g_task_new (source_object: icon, cancellable, callback, callback_data: user_data); |
244 | g_task_set_source_tag (task, g_bytes_icon_load_async); |
245 | g_task_return_pointer (task, result: g_memory_input_stream_new_from_bytes (bytes: bytes_icon->bytes), result_destroy: g_object_unref); |
246 | g_object_unref (object: task); |
247 | } |
248 | |
249 | static GInputStream * |
250 | g_bytes_icon_load_finish (GLoadableIcon *icon, |
251 | GAsyncResult *res, |
252 | char **type, |
253 | GError **error) |
254 | { |
255 | g_return_val_if_fail (g_task_is_valid (res, icon), NULL); |
256 | |
257 | if (type) |
258 | *type = NULL; |
259 | |
260 | return g_task_propagate_pointer (G_TASK (res), error); |
261 | } |
262 | |
263 | static void |
264 | g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface) |
265 | { |
266 | iface->load = g_bytes_icon_load; |
267 | iface->load_async = g_bytes_icon_load_async; |
268 | iface->load_finish = g_bytes_icon_load_finish; |
269 | } |
270 | |