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"
28 #include "gloadableicon.h"
29 #include "ginputstream.h"
30 #include "gsimpleasyncresult.h"
36 * @short_description: Icons pointing to an image file
38 * @see_also: #GIcon, #GLoadableIcon
40 * #GFileIcon specifies an icon by pointing to an image file
45 static void g_file_icon_icon_iface_init (GIconIface
*iface
);
46 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface
*iface
);
47 static void g_file_icon_load_async (GLoadableIcon
*icon
,
49 GCancellable
*cancellable
,
50 GAsyncReadyCallback callback
,
55 GObject parent_instance
;
60 struct _GFileIconClass
62 GObjectClass parent_class
;
65 G_DEFINE_TYPE_WITH_CODE (GFileIcon
, g_file_icon
, G_TYPE_OBJECT
,
66 G_IMPLEMENT_INTERFACE (G_TYPE_ICON
,
67 g_file_icon_icon_iface_init
)
68 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON
,
69 g_file_icon_loadable_icon_iface_init
))
72 g_file_icon_finalize (GObject
*object
)
76 icon
= G_FILE_ICON (object
);
78 g_object_unref (icon
->file
);
80 G_OBJECT_CLASS (g_file_icon_parent_class
)->finalize (object
);
84 g_file_icon_class_init (GFileIconClass
*klass
)
86 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
88 gobject_class
->finalize
= g_file_icon_finalize
;
92 g_file_icon_init (GFileIcon
*file
)
100 * Creates a new icon for a file.
102 * Returns: a #GIcon for the given @file, or %NULL on error.
105 g_file_icon_new (GFile
*file
)
109 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
111 icon
= g_object_new (G_TYPE_FILE_ICON
, NULL
);
112 icon
->file
= g_object_ref (file
);
114 return G_ICON (icon
);
118 * g_file_icon_get_file:
121 * Gets the #GFile associated with the given @icon.
123 * Returns: a #GFile, or %NULL.
126 g_file_icon_get_file (GFileIcon
*icon
)
128 g_return_val_if_fail (G_IS_FILE_ICON (icon
), NULL
);
134 g_file_icon_hash (GIcon
*icon
)
136 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
138 return g_file_hash (file_icon
->file
);
142 g_file_icon_equal (GIcon
*icon1
,
145 GFileIcon
*file1
= G_FILE_ICON (icon1
);
146 GFileIcon
*file2
= G_FILE_ICON (icon2
);
148 return g_file_equal (file1
->file
, file2
->file
);
153 g_file_icon_icon_iface_init (GIconIface
*iface
)
155 iface
->hash
= g_file_icon_hash
;
156 iface
->equal
= g_file_icon_equal
;
160 static GInputStream
*
161 g_file_icon_load (GLoadableIcon
*icon
,
164 GCancellable
*cancellable
,
167 GFileInputStream
*stream
;
168 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
170 stream
= g_file_read (file_icon
->file
,
174 return G_INPUT_STREAM (stream
);
179 GAsyncReadyCallback callback
;
184 load_data_free (LoadData
*data
)
186 g_object_unref (data
->icon
);
191 load_async_callback (GObject
*source_object
,
195 GFileInputStream
*stream
;
196 GError
*error
= NULL
;
197 GSimpleAsyncResult
*simple
;
198 LoadData
*data
= user_data
;
200 stream
= g_file_read_finish (G_FILE (source_object
), res
, &error
);
204 simple
= g_simple_async_result_new_from_error (G_OBJECT (data
->icon
),
208 g_error_free (error
);
212 simple
= g_simple_async_result_new (G_OBJECT (data
->icon
),
215 g_file_icon_load_async
);
217 g_simple_async_result_set_op_res_gpointer (simple
,
223 g_simple_async_result_complete (simple
);
225 load_data_free (data
);
229 g_file_icon_load_async (GLoadableIcon
*icon
,
231 GCancellable
*cancellable
,
232 GAsyncReadyCallback callback
,
235 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
238 data
= g_new0 (LoadData
, 1);
239 data
->icon
= g_object_ref (icon
);
240 data
->callback
= callback
;
241 data
->user_data
= user_data
;
243 g_file_read_async (file_icon
->file
, 0,
245 load_async_callback
, data
);
249 static GInputStream
*
250 g_file_icon_load_finish (GLoadableIcon
*icon
,
255 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
258 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_file_icon_load_async
);
263 op
= g_simple_async_result_get_op_res_gpointer (simple
);
265 return g_object_ref (op
);
271 g_file_icon_loadable_icon_iface_init (GLoadableIconIface
*iface
)
273 iface
->load
= g_file_icon_load
;
274 iface
->load_async
= g_file_icon_load_async
;
275 iface
->load_finish
= g_file_icon_load_finish
;
278 #define __G_FILE_ICON_C__
279 #include "gioaliasdef.c"