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 pointing to an image file
34 * @see_also: #GIcon, #GLoadableIcon
36 * #GFileIcon specifies an icon by pointing to an image file
41 static void g_file_icon_icon_iface_init (GIconIface *iface);
42 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
43 static void g_file_icon_load_async (GLoadableIcon *icon,
45 GCancellable *cancellable,
46 GAsyncReadyCallback callback,
51 GObject parent_instance;
56 struct _GFileIconClass
58 GObjectClass parent_class;
61 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
62 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
63 g_file_icon_icon_iface_init);
64 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
65 g_file_icon_loadable_icon_iface_init);
69 g_file_icon_finalize (GObject *object)
73 icon = G_FILE_ICON (object);
75 g_object_unref (icon->file);
77 if (G_OBJECT_CLASS (g_file_icon_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_file_icon_parent_class)->finalize) (object);
82 g_file_icon_class_init (GFileIconClass *klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 gobject_class->finalize = g_file_icon_finalize;
90 g_file_icon_init (GFileIcon *file)
98 * Creates a new icon for a file.
100 * Returns: a #GIcon for the given @file, or %NULL on error.
103 g_file_icon_new (GFile *file)
107 g_return_val_if_fail (G_IS_FILE (file), NULL);
109 icon = g_object_new (G_TYPE_FILE_ICON, NULL);
110 icon->file = g_object_ref (file);
112 return G_ICON (icon);
116 * g_file_icon_get_file:
119 * Gets the #GFile associated with the given @icon.
121 * Returns: a #GFile, or %NULL.
124 g_file_icon_get_file (GFileIcon *icon)
126 g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
132 g_file_icon_hash (GIcon *icon)
134 GFileIcon *file_icon = G_FILE_ICON (icon);
136 return g_file_hash (file_icon->file);
140 g_file_icon_equal (GIcon *icon1,
143 GFileIcon *file1 = G_FILE_ICON (icon1);
144 GFileIcon *file2 = G_FILE_ICON (icon2);
146 return g_file_equal (file1->file, file2->file);
151 g_file_icon_icon_iface_init (GIconIface *iface)
153 iface->hash = g_file_icon_hash;
154 iface->equal = g_file_icon_equal;
158 static GInputStream *
159 g_file_icon_load (GLoadableIcon *icon,
162 GCancellable *cancellable,
165 GFileInputStream *stream;
166 GFileIcon *file_icon = G_FILE_ICON (icon);
168 stream = g_file_read (file_icon->file,
172 return G_INPUT_STREAM (stream);
177 GAsyncReadyCallback callback;
182 load_data_free (LoadData *data)
184 g_object_unref (data->icon);
189 load_async_callback (GObject *source_object,
193 GFileInputStream *stream;
194 GError *error = NULL;
195 GSimpleAsyncResult *simple;
196 LoadData *data = user_data;
198 stream = g_file_read_finish (G_FILE (source_object), res, &error);
202 simple = g_simple_async_result_new_from_error (G_OBJECT (data->icon),
206 g_error_free (error);
210 simple = g_simple_async_result_new (G_OBJECT (data->icon),
213 g_file_icon_load_async);
215 g_simple_async_result_set_op_res_gpointer (simple,
221 g_simple_async_result_complete (simple);
223 load_data_free (data);
227 g_file_icon_load_async (GLoadableIcon *icon,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
233 GFileIcon *file_icon = G_FILE_ICON (icon);
236 data = g_new0 (LoadData, 1);
237 data->icon = g_object_ref (icon);
238 data->callback = callback;
239 data->user_data = user_data;
241 g_file_read_async (file_icon->file, 0,
243 load_async_callback, data);
247 static GInputStream *
248 g_file_icon_load_finish (GLoadableIcon *icon,
253 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
256 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
261 op = g_simple_async_result_get_op_res_gpointer (simple);
263 return g_object_ref (op);
269 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
271 iface->load = g_file_icon_load;
272 iface->load_async = g_file_icon_load_async;
273 iface->load_finish = g_file_icon_load_finish;
276 #define __G_FILE_ICON_C__
277 #include "gioaliasdef.c"