Improve docs
[glib.git] / gio / gfileicon.c
blob0bd3a52466545c3f63ab6d91f7245d999ff80364
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 "gloadableicon.h"
29 #include "ginputstream.h"
30 #include "gsimpleasyncresult.h"
32 #include "gioalias.h"
34 /**
35 * SECTION:gfileicon
36 * @short_description: Icons pointing to an image file
37 * @include: gio/gio.h
38 * @see_also: #GIcon, #GLoadableIcon
40 * #GFileIcon specifies an icon by pointing to an image file
41 * to be used as icon.
43 **/
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,
48 int size,
49 GCancellable *cancellable,
50 GAsyncReadyCallback callback,
51 gpointer user_data);
53 struct _GFileIcon
55 GObject parent_instance;
57 GFile *file;
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))
71 static void
72 g_file_icon_finalize (GObject *object)
74 GFileIcon *icon;
76 icon = G_FILE_ICON (object);
78 g_object_unref (icon->file);
80 G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
83 static void
84 g_file_icon_class_init (GFileIconClass *klass)
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88 gobject_class->finalize = g_file_icon_finalize;
91 static void
92 g_file_icon_init (GFileIcon *file)
96 /**
97 * g_file_icon_new:
98 * @file: a #GFile.
100 * Creates a new icon for a file.
102 * Returns: a #GIcon for the given @file, or %NULL on error.
104 GIcon *
105 g_file_icon_new (GFile *file)
107 GFileIcon *icon;
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:
119 * @icon: a #GIcon.
121 * Gets the #GFile associated with the given @icon.
123 * Returns: a #GFile, or %NULL.
125 GFile *
126 g_file_icon_get_file (GFileIcon *icon)
128 g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
130 return icon->file;
133 static guint
134 g_file_icon_hash (GIcon *icon)
136 GFileIcon *file_icon = G_FILE_ICON (icon);
138 return g_file_hash (file_icon->file);
141 static gboolean
142 g_file_icon_equal (GIcon *icon1,
143 GIcon *icon2)
145 GFileIcon *file1 = G_FILE_ICON (icon1);
146 GFileIcon *file2 = G_FILE_ICON (icon2);
148 return g_file_equal (file1->file, file2->file);
152 static void
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,
162 int size,
163 char **type,
164 GCancellable *cancellable,
165 GError **error)
167 GFileInputStream *stream;
168 GFileIcon *file_icon = G_FILE_ICON (icon);
170 stream = g_file_read (file_icon->file,
171 cancellable,
172 error);
174 return G_INPUT_STREAM (stream);
177 typedef struct {
178 GLoadableIcon *icon;
179 GAsyncReadyCallback callback;
180 gpointer user_data;
181 } LoadData;
183 static void
184 load_data_free (LoadData *data)
186 g_object_unref (data->icon);
187 g_free (data);
190 static void
191 load_async_callback (GObject *source_object,
192 GAsyncResult *res,
193 gpointer user_data)
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);
202 if (stream == NULL)
204 simple = g_simple_async_result_new_from_error (G_OBJECT (data->icon),
205 data->callback,
206 data->user_data,
207 error);
208 g_error_free (error);
210 else
212 simple = g_simple_async_result_new (G_OBJECT (data->icon),
213 data->callback,
214 data->user_data,
215 g_file_icon_load_async);
217 g_simple_async_result_set_op_res_gpointer (simple,
218 stream,
219 g_object_unref);
223 g_simple_async_result_complete (simple);
225 load_data_free (data);
228 static void
229 g_file_icon_load_async (GLoadableIcon *icon,
230 int size,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
233 gpointer user_data)
235 GFileIcon *file_icon = G_FILE_ICON (icon);
236 LoadData *data;
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,
244 cancellable,
245 load_async_callback, data);
249 static GInputStream *
250 g_file_icon_load_finish (GLoadableIcon *icon,
251 GAsyncResult *res,
252 char **type,
253 GError **error)
255 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
256 gpointer op;
258 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_icon_load_async);
260 if (type)
261 *type = NULL;
263 op = g_simple_async_result_get_op_res_gpointer (simple);
264 if (op)
265 return g_object_ref (op);
267 return NULL;
270 static void
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"