1 /* GIO - GLib Input, Output and Streaming Library
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>
25 #include "gfileicon.h"
26 #include "gsimpleasyncresult.h"
32 * @short_description: Icons for given files
33 * @see_also: #GIcon, #GLoadableIcon
34 * @include: gio/gfileicon.h
36 * #GFileIcon gets the default icon for a #GFile.
40 static void g_file_icon_icon_iface_init (GIconIface *iface);
41 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
42 static void g_file_icon_load_async (GLoadableIcon *icon,
44 GCancellable *cancellable,
45 GAsyncReadyCallback callback,
50 GObject parent_instance;
55 struct _GFileIconClass
57 GObjectClass parent_class;
60 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
62 g_file_icon_icon_iface_init);
63 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
64 g_file_icon_loadable_icon_iface_init);
68 g_file_icon_finalize (GObject *object)
72 icon = G_FILE_ICON (object);
74 g_object_unref (icon->file);
76 if (G_OBJECT_CLASS (g_file_icon_parent_class)->finalize)
77 (*G_OBJECT_CLASS (g_file_icon_parent_class)->finalize) (object);
81 g_file_icon_class_init (GFileIconClass *klass)
83 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->finalize = g_file_icon_finalize;
89 g_file_icon_init (GFileIcon *file)
97 * Creates a new icon for a file.
99 * Returns: a #GIcon for the given @file, or %NULL on error.
102 g_file_icon_new (GFile *file)
106 g_return_val_if_fail (G_IS_FILE (file), NULL);
108 icon = g_object_new (G_TYPE_FILE_ICON, NULL);
109 icon->file = g_object_ref (file);
111 return G_ICON (icon);
115 * g_file_icon_get_file:
118 * Gets the #GFile associated with the given @icon.
120 * Returns: a #GFile, or %NULL.
123 g_file_icon_get_file (GFileIcon *icon)
125 g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
131 g_file_icon_hash (GIcon *icon)
133 GFileIcon *file_icon = G_FILE_ICON (icon);
135 return g_file_hash (file_icon->file);
139 g_file_icon_equal (GIcon *icon1,
142 GFileIcon *file1 = G_FILE_ICON (icon1);
143 GFileIcon *file2 = G_FILE_ICON (icon2);
145 return g_file_equal (file1->file, file2->file);
150 g_file_icon_icon_iface_init (GIconIface *iface)
152 iface->hash = g_file_icon_hash;
153 iface->equal = g_file_icon_equal;
157 static GInputStream *
158 g_file_icon_load (GLoadableIcon *icon,
161 GCancellable *cancellable,
164 GFileInputStream *stream;
165 GFileIcon *file_icon = G_FILE_ICON (icon);
167 stream = g_file_read (file_icon->file,
171 return G_INPUT_STREAM (stream);
176 GAsyncReadyCallback callback;
181 load_data_free (LoadData *data)
183 g_object_unref (data->icon);
188 load_async_callback (GObject *source_object,
192 GFileInputStream *stream;
193 GError *error = NULL;
194 GSimpleAsyncResult *simple;
195 LoadData *data = user_data;
197 stream = g_file_read_finish (G_FILE (source_object), res, &error);
201 simple = g_simple_async_result_new_from_error (G_OBJECT (data->icon),
205 g_error_free (error);
209 simple = g_simple_async_result_new (G_OBJECT (data->icon),
212 g_file_icon_load_async);
214 g_simple_async_result_set_op_res_gpointer (simple,
220 g_simple_async_result_complete (simple);
222 load_data_free (data);
226 g_file_icon_load_async (GLoadableIcon *icon,
228 GCancellable *cancellable,
229 GAsyncReadyCallback callback,
232 GFileIcon *file_icon = G_FILE_ICON (icon);
235 data = g_new0 (LoadData, 1);
236 data->icon = g_object_ref (icon);
237 data->callback = callback;
238 data->user_data = user_data;
240 g_file_read_async (file_icon->file, 0,
242 load_async_callback, data);
246 static GInputStream *
247 g_file_icon_load_finish (GLoadableIcon *icon,
252 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
255 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
260 op = g_simple_async_result_get_op_res_gpointer (simple);
262 return g_object_ref (op);
268 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
270 iface->load = g_file_icon_load;
271 iface->load_async = g_file_icon_load_async;
272 iface->load_finish = g_file_icon_load_finish;
275 #define __G_FILE_ICON_C__
276 #include "gioaliasdef.c"