/src/glib/gio/gfileicon.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
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: Alexander Larsson <alexl@redhat.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include "gfileicon.h" |
26 | | #include "gfile.h" |
27 | | #include "gicon.h" |
28 | | #include "glibintl.h" |
29 | | #include "gloadableicon.h" |
30 | | #include "ginputstream.h" |
31 | | #include "gtask.h" |
32 | | #include "gioerror.h" |
33 | | |
34 | | |
35 | | /** |
36 | | * SECTION:gfileicon |
37 | | * @short_description: Icons pointing to an image file |
38 | | * @include: gio/gio.h |
39 | | * @see_also: #GIcon, #GLoadableIcon |
40 | | * |
41 | | * #GFileIcon specifies an icon by pointing to an image file |
42 | | * to be used as icon. |
43 | | * |
44 | | **/ |
45 | | |
46 | | static void g_file_icon_icon_iface_init (GIconIface *iface); |
47 | | static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface); |
48 | | static void g_file_icon_load_async (GLoadableIcon *icon, |
49 | | int size, |
50 | | GCancellable *cancellable, |
51 | | GAsyncReadyCallback callback, |
52 | | gpointer user_data); |
53 | | |
54 | | struct _GFileIcon |
55 | | { |
56 | | GObject parent_instance; |
57 | | |
58 | | GFile *file; |
59 | | }; |
60 | | |
61 | | struct _GFileIconClass |
62 | | { |
63 | | GObjectClass parent_class; |
64 | | }; |
65 | | |
66 | | enum |
67 | | { |
68 | | PROP_0, |
69 | | PROP_FILE |
70 | | }; |
71 | | |
72 | | G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT, |
73 | | G_IMPLEMENT_INTERFACE (G_TYPE_ICON, |
74 | | g_file_icon_icon_iface_init) |
75 | | G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, |
76 | | g_file_icon_loadable_icon_iface_init)) |
77 | | |
78 | | static void |
79 | | g_file_icon_get_property (GObject *object, |
80 | | guint prop_id, |
81 | | GValue *value, |
82 | | GParamSpec *pspec) |
83 | 0 | { |
84 | 0 | GFileIcon *icon = G_FILE_ICON (object); |
85 | |
|
86 | 0 | switch (prop_id) |
87 | 0 | { |
88 | 0 | case PROP_FILE: |
89 | 0 | g_value_set_object (value, icon->file); |
90 | 0 | break; |
91 | | |
92 | 0 | default: |
93 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | static void |
98 | | g_file_icon_set_property (GObject *object, |
99 | | guint prop_id, |
100 | | const GValue *value, |
101 | | GParamSpec *pspec) |
102 | 0 | { |
103 | 0 | GFileIcon *icon = G_FILE_ICON (object); |
104 | |
|
105 | 0 | switch (prop_id) |
106 | 0 | { |
107 | 0 | case PROP_FILE: |
108 | 0 | icon->file = G_FILE (g_value_dup_object (value)); |
109 | 0 | break; |
110 | | |
111 | 0 | default: |
112 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | static void |
117 | | g_file_icon_constructed (GObject *object) |
118 | 0 | { |
119 | 0 | #ifndef G_DISABLE_ASSERT |
120 | 0 | GFileIcon *icon = G_FILE_ICON (object); |
121 | 0 | #endif |
122 | |
|
123 | 0 | G_OBJECT_CLASS (g_file_icon_parent_class)->constructed (object); |
124 | | |
125 | | /* Must have be set during construction */ |
126 | 0 | g_assert (icon->file != NULL); |
127 | 0 | } |
128 | | |
129 | | static void |
130 | | g_file_icon_finalize (GObject *object) |
131 | 0 | { |
132 | 0 | GFileIcon *icon; |
133 | |
|
134 | 0 | icon = G_FILE_ICON (object); |
135 | |
|
136 | 0 | if (icon->file) |
137 | 0 | g_object_unref (icon->file); |
138 | |
|
139 | 0 | G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object); |
140 | 0 | } |
141 | | |
142 | | static void |
143 | | g_file_icon_class_init (GFileIconClass *klass) |
144 | 0 | { |
145 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
146 | | |
147 | 0 | gobject_class->get_property = g_file_icon_get_property; |
148 | 0 | gobject_class->set_property = g_file_icon_set_property; |
149 | 0 | gobject_class->finalize = g_file_icon_finalize; |
150 | 0 | gobject_class->constructed = g_file_icon_constructed; |
151 | | |
152 | | /** |
153 | | * GFileIcon:file: |
154 | | * |
155 | | * The file containing the icon. |
156 | | */ |
157 | 0 | g_object_class_install_property (gobject_class, PROP_FILE, |
158 | 0 | g_param_spec_object ("file", |
159 | 0 | P_("file"), |
160 | 0 | P_("The file containing the icon"), |
161 | 0 | G_TYPE_FILE, |
162 | 0 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK)); |
163 | 0 | } |
164 | | |
165 | | static void |
166 | | g_file_icon_init (GFileIcon *file) |
167 | 0 | { |
168 | 0 | } |
169 | | |
170 | | /** |
171 | | * g_file_icon_new: |
172 | | * @file: a #GFile. |
173 | | * |
174 | | * Creates a new icon for a file. |
175 | | * |
176 | | * Returns: (transfer full) (type GFileIcon): a #GIcon for the given |
177 | | * @file, or %NULL on error. |
178 | | **/ |
179 | | GIcon * |
180 | | g_file_icon_new (GFile *file) |
181 | 0 | { |
182 | 0 | g_return_val_if_fail (G_IS_FILE (file), NULL); |
183 | | |
184 | 0 | return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL)); |
185 | 0 | } |
186 | | |
187 | | /** |
188 | | * g_file_icon_get_file: |
189 | | * @icon: a #GIcon. |
190 | | * |
191 | | * Gets the #GFile associated with the given @icon. |
192 | | * |
193 | | * Returns: (transfer none): a #GFile. |
194 | | **/ |
195 | | GFile * |
196 | | g_file_icon_get_file (GFileIcon *icon) |
197 | 0 | { |
198 | 0 | g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL); |
199 | | |
200 | 0 | return icon->file; |
201 | 0 | } |
202 | | |
203 | | static guint |
204 | | g_file_icon_hash (GIcon *icon) |
205 | 0 | { |
206 | 0 | GFileIcon *file_icon = G_FILE_ICON (icon); |
207 | |
|
208 | 0 | return g_file_hash (file_icon->file); |
209 | 0 | } |
210 | | |
211 | | static gboolean |
212 | | g_file_icon_equal (GIcon *icon1, |
213 | | GIcon *icon2) |
214 | 0 | { |
215 | 0 | GFileIcon *file1 = G_FILE_ICON (icon1); |
216 | 0 | GFileIcon *file2 = G_FILE_ICON (icon2); |
217 | | |
218 | 0 | return g_file_equal (file1->file, file2->file); |
219 | 0 | } |
220 | | |
221 | | static gboolean |
222 | | g_file_icon_to_tokens (GIcon *icon, |
223 | | GPtrArray *tokens, |
224 | | gint *out_version) |
225 | 0 | { |
226 | 0 | GFileIcon *file_icon = G_FILE_ICON (icon); |
227 | |
|
228 | 0 | g_return_val_if_fail (out_version != NULL, FALSE); |
229 | | |
230 | 0 | *out_version = 0; |
231 | |
|
232 | 0 | g_ptr_array_add (tokens, g_file_get_uri (file_icon->file)); |
233 | 0 | return TRUE; |
234 | 0 | } |
235 | | |
236 | | static GIcon * |
237 | | g_file_icon_from_tokens (gchar **tokens, |
238 | | gint num_tokens, |
239 | | gint version, |
240 | | GError **error) |
241 | 0 | { |
242 | 0 | GIcon *icon; |
243 | 0 | GFile *file; |
244 | |
|
245 | 0 | icon = NULL; |
246 | |
|
247 | 0 | if (version != 0) |
248 | 0 | { |
249 | 0 | g_set_error (error, |
250 | 0 | G_IO_ERROR, |
251 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
252 | 0 | _("Can’t handle version %d of GFileIcon encoding"), |
253 | 0 | version); |
254 | 0 | goto out; |
255 | 0 | } |
256 | | |
257 | 0 | if (num_tokens != 1) |
258 | 0 | { |
259 | 0 | g_set_error_literal (error, |
260 | 0 | G_IO_ERROR, |
261 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
262 | 0 | _("Malformed input data for GFileIcon")); |
263 | 0 | goto out; |
264 | 0 | } |
265 | | |
266 | 0 | file = g_file_new_for_uri (tokens[0]); |
267 | 0 | icon = g_file_icon_new (file); |
268 | 0 | g_object_unref (file); |
269 | |
|
270 | 0 | out: |
271 | 0 | return icon; |
272 | 0 | } |
273 | | |
274 | | static GVariant * |
275 | | g_file_icon_serialize (GIcon *icon) |
276 | 0 | { |
277 | 0 | GFileIcon *file_icon = G_FILE_ICON (icon); |
278 | |
|
279 | 0 | return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon->file))); |
280 | 0 | } |
281 | | |
282 | | static void |
283 | | g_file_icon_icon_iface_init (GIconIface *iface) |
284 | 0 | { |
285 | 0 | iface->hash = g_file_icon_hash; |
286 | 0 | iface->equal = g_file_icon_equal; |
287 | 0 | iface->to_tokens = g_file_icon_to_tokens; |
288 | 0 | iface->from_tokens = g_file_icon_from_tokens; |
289 | 0 | iface->serialize = g_file_icon_serialize; |
290 | 0 | } |
291 | | |
292 | | |
293 | | static GInputStream * |
294 | | g_file_icon_load (GLoadableIcon *icon, |
295 | | int size, |
296 | | char **type, |
297 | | GCancellable *cancellable, |
298 | | GError **error) |
299 | 0 | { |
300 | 0 | GFileInputStream *stream; |
301 | 0 | GFileIcon *file_icon = G_FILE_ICON (icon); |
302 | |
|
303 | 0 | stream = g_file_read (file_icon->file, |
304 | 0 | cancellable, |
305 | 0 | error); |
306 | |
|
307 | 0 | if (stream && type) |
308 | 0 | *type = NULL; |
309 | | |
310 | 0 | return G_INPUT_STREAM (stream); |
311 | 0 | } |
312 | | |
313 | | static void |
314 | | load_async_callback (GObject *source_object, |
315 | | GAsyncResult *res, |
316 | | gpointer user_data) |
317 | 0 | { |
318 | 0 | GFileInputStream *stream; |
319 | 0 | GError *error = NULL; |
320 | 0 | GTask *task = user_data; |
321 | |
|
322 | 0 | stream = g_file_read_finish (G_FILE (source_object), res, &error); |
323 | 0 | if (stream == NULL) |
324 | 0 | g_task_return_error (task, error); |
325 | 0 | else |
326 | 0 | g_task_return_pointer (task, stream, g_object_unref); |
327 | 0 | g_object_unref (task); |
328 | 0 | } |
329 | | |
330 | | static void |
331 | | g_file_icon_load_async (GLoadableIcon *icon, |
332 | | int size, |
333 | | GCancellable *cancellable, |
334 | | GAsyncReadyCallback callback, |
335 | | gpointer user_data) |
336 | 0 | { |
337 | 0 | GFileIcon *file_icon = G_FILE_ICON (icon); |
338 | 0 | GTask *task; |
339 | |
|
340 | 0 | task = g_task_new (icon, cancellable, callback, user_data); |
341 | 0 | g_task_set_source_tag (task, g_file_icon_load_async); |
342 | | |
343 | 0 | g_file_read_async (file_icon->file, 0, |
344 | 0 | cancellable, |
345 | 0 | load_async_callback, task); |
346 | 0 | } |
347 | | |
348 | | static GInputStream * |
349 | | g_file_icon_load_finish (GLoadableIcon *icon, |
350 | | GAsyncResult *res, |
351 | | char **type, |
352 | | GError **error) |
353 | 0 | { |
354 | 0 | g_return_val_if_fail (g_task_is_valid (res, icon), NULL); |
355 | | |
356 | 0 | if (type) |
357 | 0 | *type = NULL; |
358 | | |
359 | 0 | return g_task_propagate_pointer (G_TASK (res), error); |
360 | 0 | } |
361 | | |
362 | | static void |
363 | | g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface) |
364 | 0 | { |
365 | 0 | iface->load = g_file_icon_load; |
366 | 0 | iface->load_async = g_file_icon_load_async; |
367 | 0 | iface->load_finish = g_file_icon_load_finish; |
368 | 0 | } |