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"
28 static void g_file_icon_icon_iface_init (GIconIface *iface);
29 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface);
30 static void g_file_icon_load_async (GLoadableIcon *icon,
32 GCancellable *cancellable,
33 GAsyncReadyCallback callback,
38 GObject parent_instance;
43 struct _GFileIconClass
45 GObjectClass parent_class;
48 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
49 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
50 g_file_icon_icon_iface_init);
51 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
52 g_file_icon_loadable_icon_iface_init);
56 g_file_icon_finalize (GObject *object)
60 icon = G_FILE_ICON (object);
62 g_object_unref (icon->file);
64 if (G_OBJECT_CLASS (g_file_icon_parent_class)->finalize)
65 (*G_OBJECT_CLASS (g_file_icon_parent_class)->finalize) (object);
69 g_file_icon_class_init (GFileIconClass *klass)
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
73 gobject_class->finalize = g_file_icon_finalize;
77 g_file_icon_init (GFileIcon *file)
88 g_file_icon_new (GFile *file)
92 g_return_val_if_fail (G_IS_FILE (file), NULL);
94 icon = g_object_new (G_TYPE_FILE_ICON, NULL);
95 icon->file = g_object_ref (file);
101 * g_file_icon_get_file:
107 g_file_icon_get_file (GFileIcon *icon)
109 g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
115 g_file_icon_hash (GIcon *icon)
117 GFileIcon *file_icon = G_FILE_ICON (icon);
119 return g_file_hash (file_icon->file);
123 g_file_icon_equal (GIcon *icon1,
126 GFileIcon *file1 = G_FILE_ICON (icon1);
127 GFileIcon *file2 = G_FILE_ICON (icon2);
129 return g_file_equal (file1->file, file2->file);
134 g_file_icon_icon_iface_init (GIconIface *iface)
136 iface->hash = g_file_icon_hash;
137 iface->equal = g_file_icon_equal;
141 static GInputStream *
142 g_file_icon_load (GLoadableIcon *icon,
145 GCancellable *cancellable,
148 GFileInputStream *stream;
149 GFileIcon *file_icon = G_FILE_ICON (icon);
151 stream = g_file_read (file_icon->file,
155 return G_INPUT_STREAM (stream);
160 GAsyncReadyCallback callback;
165 load_data_free (LoadData *data)
167 g_object_unref (data->icon);
172 load_async_callback (GObject *source_object,
176 GFileInputStream *stream;
177 GError *error = NULL;
178 GSimpleAsyncResult *simple;
179 LoadData *data = user_data;
181 stream = g_file_read_finish (G_FILE (source_object), res, &error);
185 simple = g_simple_async_result_new_from_error (G_OBJECT (data->icon),
189 g_error_free (error);
193 simple = g_simple_async_result_new (G_OBJECT (data->icon),
196 g_file_icon_load_async);
198 g_simple_async_result_set_op_res_gpointer (simple,
204 g_simple_async_result_complete (simple);
206 load_data_free (data);
210 g_file_icon_load_async (GLoadableIcon *icon,
212 GCancellable *cancellable,
213 GAsyncReadyCallback callback,
216 GFileIcon *file_icon = G_FILE_ICON (icon);
219 data = g_new0 (LoadData, 1);
220 data->icon = g_object_ref (icon);
221 data->callback = callback;
222 data->user_data = user_data;
224 g_file_read_async (file_icon->file, 0,
226 load_async_callback, data);
230 static GInputStream *
231 g_file_icon_load_finish (GLoadableIcon *icon,
236 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
239 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
244 op = g_simple_async_result_get_op_res_gpointer (simple);
246 return g_object_ref (op);
252 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
254 iface->load = g_file_icon_load;
255 iface->load_async = g_file_icon_load_async;
256 iface->load_finish = g_file_icon_load_finish;