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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "gfileicon.h"
27 #include "gloadableicon.h"
28 #include "ginputstream.h"
35 * @short_description: Icons pointing to an image file
37 * @see_also: #GIcon, #GLoadableIcon
39 * #GFileIcon specifies an icon by pointing to an image file
44 static void g_file_icon_icon_iface_init (GIconIface
*iface
);
45 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface
*iface
);
46 static void g_file_icon_load_async (GLoadableIcon
*icon
,
48 GCancellable
*cancellable
,
49 GAsyncReadyCallback callback
,
54 GObject parent_instance
;
59 struct _GFileIconClass
61 GObjectClass parent_class
;
70 G_DEFINE_TYPE_WITH_CODE (GFileIcon
, g_file_icon
, G_TYPE_OBJECT
,
71 G_IMPLEMENT_INTERFACE (G_TYPE_ICON
,
72 g_file_icon_icon_iface_init
)
73 G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON
,
74 g_file_icon_loadable_icon_iface_init
))
77 g_file_icon_get_property (GObject
*object
,
82 GFileIcon
*icon
= G_FILE_ICON (object
);
87 g_value_set_object (value
, icon
->file
);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
96 g_file_icon_set_property (GObject
*object
,
101 GFileIcon
*icon
= G_FILE_ICON (object
);
106 icon
->file
= G_FILE (g_value_dup_object (value
));
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
115 g_file_icon_finalize (GObject
*object
)
119 icon
= G_FILE_ICON (object
);
122 g_object_unref (icon
->file
);
124 G_OBJECT_CLASS (g_file_icon_parent_class
)->finalize (object
);
128 g_file_icon_class_init (GFileIconClass
*klass
)
130 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
132 gobject_class
->get_property
= g_file_icon_get_property
;
133 gobject_class
->set_property
= g_file_icon_set_property
;
134 gobject_class
->finalize
= g_file_icon_finalize
;
139 * The file containing the icon.
141 g_object_class_install_property (gobject_class
, PROP_FILE
,
142 g_param_spec_object ("file",
144 P_("The file containing the icon"),
146 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
150 g_file_icon_init (GFileIcon
*file
)
158 * Creates a new icon for a file.
160 * Returns: (transfer full) (type GFileIcon): a #GIcon for the given
161 * @file, or %NULL on error.
164 g_file_icon_new (GFile
*file
)
166 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
168 return G_ICON (g_object_new (G_TYPE_FILE_ICON
, "file", file
, NULL
));
172 * g_file_icon_get_file:
175 * Gets the #GFile associated with the given @icon.
177 * Returns: (transfer none): a #GFile, or %NULL.
180 g_file_icon_get_file (GFileIcon
*icon
)
182 g_return_val_if_fail (G_IS_FILE_ICON (icon
), NULL
);
188 g_file_icon_hash (GIcon
*icon
)
190 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
192 return g_file_hash (file_icon
->file
);
196 g_file_icon_equal (GIcon
*icon1
,
199 GFileIcon
*file1
= G_FILE_ICON (icon1
);
200 GFileIcon
*file2
= G_FILE_ICON (icon2
);
202 return g_file_equal (file1
->file
, file2
->file
);
206 g_file_icon_to_tokens (GIcon
*icon
,
210 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
212 g_return_val_if_fail (out_version
!= NULL
, FALSE
);
216 g_ptr_array_add (tokens
, g_file_get_uri (file_icon
->file
));
221 g_file_icon_from_tokens (gchar
**tokens
,
235 G_IO_ERROR_INVALID_ARGUMENT
,
236 _("Can’t handle version %d of GFileIcon encoding"),
243 g_set_error_literal (error
,
245 G_IO_ERROR_INVALID_ARGUMENT
,
246 _("Malformed input data for GFileIcon"));
250 file
= g_file_new_for_uri (tokens
[0]);
251 icon
= g_file_icon_new (file
);
252 g_object_unref (file
);
259 g_file_icon_serialize (GIcon
*icon
)
261 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
263 return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon
->file
)));
267 g_file_icon_icon_iface_init (GIconIface
*iface
)
269 iface
->hash
= g_file_icon_hash
;
270 iface
->equal
= g_file_icon_equal
;
271 iface
->to_tokens
= g_file_icon_to_tokens
;
272 iface
->from_tokens
= g_file_icon_from_tokens
;
273 iface
->serialize
= g_file_icon_serialize
;
277 static GInputStream
*
278 g_file_icon_load (GLoadableIcon
*icon
,
281 GCancellable
*cancellable
,
284 GFileInputStream
*stream
;
285 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
287 stream
= g_file_read (file_icon
->file
,
294 return G_INPUT_STREAM (stream
);
298 load_async_callback (GObject
*source_object
,
302 GFileInputStream
*stream
;
303 GError
*error
= NULL
;
304 GTask
*task
= user_data
;
306 stream
= g_file_read_finish (G_FILE (source_object
), res
, &error
);
308 g_task_return_error (task
, error
);
310 g_task_return_pointer (task
, stream
, g_object_unref
);
311 g_object_unref (task
);
315 g_file_icon_load_async (GLoadableIcon
*icon
,
317 GCancellable
*cancellable
,
318 GAsyncReadyCallback callback
,
321 GFileIcon
*file_icon
= G_FILE_ICON (icon
);
324 task
= g_task_new (icon
, cancellable
, callback
, user_data
);
325 g_task_set_source_tag (task
, g_file_icon_load_async
);
327 g_file_read_async (file_icon
->file
, 0,
329 load_async_callback
, task
);
332 static GInputStream
*
333 g_file_icon_load_finish (GLoadableIcon
*icon
,
338 g_return_val_if_fail (g_task_is_valid (res
, icon
), NULL
);
343 return g_task_propagate_pointer (G_TASK (res
), error
);
347 g_file_icon_loadable_icon_iface_init (GLoadableIconIface
*iface
)
349 iface
->load
= g_file_icon_load
;
350 iface
->load_async
= g_file_icon_load_async
;
351 iface
->load_finish
= g_file_icon_load_finish
;