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