GMenuModel exporter: remove workaround
[glib.git] / gio / gfileicon.c
blobcba928f00727892a6c9387457a7c8324ca9abf4e
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
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 "gsimpleasyncresult.h"
32 #include "gioerror.h"
35 /**
36 * SECTION:gfileicon
37 * @short_description: Icons pointing to an image file
38 * @include: gio/gio.h
39 * @see_also: #GIcon, #GLoadableIcon
41 * #GFileIcon specifies an icon by pointing to an image file
42 * to be used as icon.
44 **/
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);
54 struct _GFileIcon
56 GObject parent_instance;
58 GFile *file;
61 struct _GFileIconClass
63 GObjectClass parent_class;
66 enum
68 PROP_0,
69 PROP_FILE
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))
78 static void
79 g_file_icon_get_property (GObject *object,
80 guint prop_id,
81 GValue *value,
82 GParamSpec *pspec)
84 GFileIcon *icon = G_FILE_ICON (object);
86 switch (prop_id)
88 case PROP_FILE:
89 g_value_set_object (value, icon->file);
90 break;
92 default:
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 static void
98 g_file_icon_set_property (GObject *object,
99 guint prop_id,
100 const GValue *value,
101 GParamSpec *pspec)
103 GFileIcon *icon = G_FILE_ICON (object);
105 switch (prop_id)
107 case PROP_FILE:
108 icon->file = G_FILE (g_value_dup_object (value));
109 break;
111 default:
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 static void
117 g_file_icon_finalize (GObject *object)
119 GFileIcon *icon;
121 icon = G_FILE_ICON (object);
123 g_object_unref (icon->file);
125 G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
128 static void
129 g_file_icon_class_init (GFileIconClass *klass)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 gobject_class->get_property = g_file_icon_get_property;
134 gobject_class->set_property = g_file_icon_set_property;
135 gobject_class->finalize = g_file_icon_finalize;
138 * GFileIcon:file:
140 * The file containing the icon.
142 g_object_class_install_property (gobject_class, PROP_FILE,
143 g_param_spec_object ("file",
144 P_("file"),
145 P_("The file containing the icon"),
146 G_TYPE_FILE,
147 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
150 static void
151 g_file_icon_init (GFileIcon *file)
156 * g_file_icon_new:
157 * @file: a #GFile.
159 * Creates a new icon for a file.
161 * Returns: (transfer full): a #GIcon for the given @file, or %NULL on error.
163 GIcon *
164 g_file_icon_new (GFile *file)
166 g_return_val_if_fail (G_IS_FILE (file), NULL);
168 return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL));
172 * g_file_icon_get_file:
173 * @icon: a #GIcon.
175 * Gets the #GFile associated with the given @icon.
177 * Returns: (transfer none): a #GFile, or %NULL.
179 GFile *
180 g_file_icon_get_file (GFileIcon *icon)
182 g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
184 return icon->file;
187 static guint
188 g_file_icon_hash (GIcon *icon)
190 GFileIcon *file_icon = G_FILE_ICON (icon);
192 return g_file_hash (file_icon->file);
195 static gboolean
196 g_file_icon_equal (GIcon *icon1,
197 GIcon *icon2)
199 GFileIcon *file1 = G_FILE_ICON (icon1);
200 GFileIcon *file2 = G_FILE_ICON (icon2);
202 return g_file_equal (file1->file, file2->file);
205 static gboolean
206 g_file_icon_to_tokens (GIcon *icon,
207 GPtrArray *tokens,
208 gint *out_version)
210 GFileIcon *file_icon = G_FILE_ICON (icon);
212 g_return_val_if_fail (out_version != NULL, FALSE);
214 *out_version = 0;
216 g_ptr_array_add (tokens, g_file_get_uri (file_icon->file));
217 return TRUE;
220 static GIcon *
221 g_file_icon_from_tokens (gchar **tokens,
222 gint num_tokens,
223 gint version,
224 GError **error)
226 GIcon *icon;
227 GFile *file;
229 icon = NULL;
231 if (version != 0)
233 g_set_error (error,
234 G_IO_ERROR,
235 G_IO_ERROR_INVALID_ARGUMENT,
236 _("Can't handle version %d of GFileIcon encoding"),
237 version);
238 goto out;
241 if (num_tokens != 1)
243 g_set_error_literal (error,
244 G_IO_ERROR,
245 G_IO_ERROR_INVALID_ARGUMENT,
246 _("Malformed input data for GFileIcon"));
247 goto out;
250 file = g_file_new_for_uri (tokens[0]);
251 icon = g_file_icon_new (file);
252 g_object_unref (file);
254 out:
255 return icon;
258 static void
259 g_file_icon_icon_iface_init (GIconIface *iface)
261 iface->hash = g_file_icon_hash;
262 iface->equal = g_file_icon_equal;
263 iface->to_tokens = g_file_icon_to_tokens;
264 iface->from_tokens = g_file_icon_from_tokens;
268 static GInputStream *
269 g_file_icon_load (GLoadableIcon *icon,
270 int size,
271 char **type,
272 GCancellable *cancellable,
273 GError **error)
275 GFileInputStream *stream;
276 GFileIcon *file_icon = G_FILE_ICON (icon);
278 stream = g_file_read (file_icon->file,
279 cancellable,
280 error);
282 return G_INPUT_STREAM (stream);
285 typedef struct {
286 GLoadableIcon *icon;
287 GAsyncReadyCallback callback;
288 gpointer user_data;
289 } LoadData;
291 static void
292 load_data_free (LoadData *data)
294 g_object_unref (data->icon);
295 g_free (data);
298 static void
299 load_async_callback (GObject *source_object,
300 GAsyncResult *res,
301 gpointer user_data)
303 GFileInputStream *stream;
304 GError *error = NULL;
305 GSimpleAsyncResult *simple;
306 LoadData *data = user_data;
308 stream = g_file_read_finish (G_FILE (source_object), res, &error);
310 if (stream == NULL)
312 simple = g_simple_async_result_new_take_error (G_OBJECT (data->icon),
313 data->callback,
314 data->user_data,
315 error);
317 else
319 simple = g_simple_async_result_new (G_OBJECT (data->icon),
320 data->callback,
321 data->user_data,
322 g_file_icon_load_async);
324 g_simple_async_result_set_op_res_gpointer (simple,
325 stream,
326 g_object_unref);
330 g_simple_async_result_complete (simple);
332 load_data_free (data);
333 g_object_unref (simple);
336 static void
337 g_file_icon_load_async (GLoadableIcon *icon,
338 int size,
339 GCancellable *cancellable,
340 GAsyncReadyCallback callback,
341 gpointer user_data)
343 GFileIcon *file_icon = G_FILE_ICON (icon);
344 LoadData *data;
346 data = g_new0 (LoadData, 1);
347 data->icon = g_object_ref (icon);
348 data->callback = callback;
349 data->user_data = user_data;
351 g_file_read_async (file_icon->file, 0,
352 cancellable,
353 load_async_callback, data);
357 static GInputStream *
358 g_file_icon_load_finish (GLoadableIcon *icon,
359 GAsyncResult *res,
360 char **type,
361 GError **error)
363 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
364 gpointer op;
366 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
368 if (type)
369 *type = NULL;
371 op = g_simple_async_result_get_op_res_gpointer (simple);
372 if (op)
373 return g_object_ref (op);
375 return NULL;
378 static void
379 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
381 iface->load = g_file_icon_load;
382 iface->load_async = g_file_icon_load_async;
383 iface->load_finish = g_file_icon_load_finish;