Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / gio / gfileicon.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gfileicon.h"
26 #include "gfile.h"
27 #include "gicon.h"
28 #include "glibintl.h"
29 #include "gloadableicon.h"
30 #include "ginputstream.h"
31 #include "gtask.h"
32 #include "gioerror.h"
33
34
35 /**
36  * SECTION:gfileicon
37  * @short_description: Icons pointing to an image file
38  * @include: gio/gio.h
39  * @see_also: #GIcon, #GLoadableIcon
40  * 
41  * #GFileIcon specifies an icon by pointing to an image file
42  * to be used as icon.
43  * 
44  **/
45
46 static void g_file_icon_icon_iface_init          (GIconIface          *iface);
47 static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
48 static void g_file_icon_load_async               (GLoadableIcon       *icon,
49                                                   int                  size,
50                                                   GCancellable        *cancellable,
51                                                   GAsyncReadyCallback  callback,
52                                                   gpointer             user_data);
53
54 struct _GFileIcon
55 {
56   GObject parent_instance;
57
58   GFile *file;
59 };
60
61 struct _GFileIconClass
62 {
63   GObjectClass parent_class;
64 };
65
66 enum
67 {
68   PROP_0,
69   PROP_FILE
70 };
71
72 G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
73                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
74                                                 g_file_icon_icon_iface_init)
75                          G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
76                                                 g_file_icon_loadable_icon_iface_init))
77
78 static void
79 g_file_icon_get_property (GObject    *object,
80                           guint       prop_id,
81                           GValue     *value,
82                           GParamSpec *pspec)
83 {
84   GFileIcon *icon = G_FILE_ICON (object);
85
86   switch (prop_id)
87     {
88       case PROP_FILE:
89         g_value_set_object (value, icon->file);
90         break;
91
92       default:
93         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94     }
95 }
96
97 static void
98 g_file_icon_set_property (GObject      *object,
99                           guint         prop_id,
100                           const GValue *value,
101                           GParamSpec   *pspec)
102 {
103   GFileIcon *icon = G_FILE_ICON (object);
104
105   switch (prop_id)
106     {
107       case PROP_FILE:
108         icon->file = G_FILE (g_value_dup_object (value));
109         break;
110
111       default:
112         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113     }
114 }
115
116 static void
117 g_file_icon_constructed (GObject *object)
118 {
119 #ifndef G_DISABLE_ASSERT
120   GFileIcon *icon = G_FILE_ICON (object);
121 #endif
122
123   G_OBJECT_CLASS (g_file_icon_parent_class)->constructed (object);
124
125   /* Must have be set during construction */
126   g_assert (icon->file != NULL);
127 }
128
129 static void
130 g_file_icon_finalize (GObject *object)
131 {
132   GFileIcon *icon;
133
134   icon = G_FILE_ICON (object);
135
136   if (icon->file)
137     g_object_unref (icon->file);
138
139   G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
140 }
141
142 static void
143 g_file_icon_class_init (GFileIconClass *klass)
144 {
145   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
146   
147   gobject_class->get_property = g_file_icon_get_property;
148   gobject_class->set_property = g_file_icon_set_property;
149   gobject_class->finalize = g_file_icon_finalize;
150   gobject_class->constructed = g_file_icon_constructed;
151
152   /**
153    * GFileIcon:file:
154    *
155    * The file containing the icon.
156    */
157   g_object_class_install_property (gobject_class, PROP_FILE,
158                                    g_param_spec_object ("file",
159                                                         P_("file"),
160                                                         P_("The file containing the icon"),
161                                                         G_TYPE_FILE,
162                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
163 }
164
165 static void
166 g_file_icon_init (GFileIcon *file)
167 {
168 }
169
170 /**
171  * g_file_icon_new:
172  * @file: a #GFile.
173  * 
174  * Creates a new icon for a file.
175  * 
176  * Returns: (transfer full) (type GFileIcon): a #GIcon for the given
177  *   @file, or %NULL on error.
178  **/
179 GIcon *
180 g_file_icon_new (GFile *file)
181 {
182   g_return_val_if_fail (G_IS_FILE (file), NULL);
183
184   return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL));
185 }
186
187 /**
188  * g_file_icon_get_file:
189  * @icon: a #GIcon.
190  * 
191  * Gets the #GFile associated with the given @icon.
192  * 
193  * Returns: (transfer none): a #GFile.
194  **/
195 GFile *
196 g_file_icon_get_file (GFileIcon *icon)
197 {
198   g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);
199
200   return icon->file;
201 }
202
203 static guint
204 g_file_icon_hash (GIcon *icon)
205 {
206   GFileIcon *file_icon = G_FILE_ICON (icon);
207
208   return g_file_hash (file_icon->file);
209 }
210
211 static gboolean
212 g_file_icon_equal (GIcon *icon1,
213                    GIcon *icon2)
214 {
215   GFileIcon *file1 = G_FILE_ICON (icon1);
216   GFileIcon *file2 = G_FILE_ICON (icon2);
217   
218   return g_file_equal (file1->file, file2->file);
219 }
220
221 static gboolean
222 g_file_icon_to_tokens (GIcon *icon,
223                        GPtrArray *tokens,
224                        gint  *out_version)
225 {
226   GFileIcon *file_icon = G_FILE_ICON (icon);
227
228   g_return_val_if_fail (out_version != NULL, FALSE);
229
230   *out_version = 0;
231
232   g_ptr_array_add (tokens, g_file_get_uri (file_icon->file));
233   return TRUE;
234 }
235
236 static GIcon *
237 g_file_icon_from_tokens (gchar  **tokens,
238                          gint     num_tokens,
239                          gint     version,
240                          GError **error)
241 {
242   GIcon *icon;
243   GFile *file;
244
245   icon = NULL;
246
247   if (version != 0)
248     {
249       g_set_error (error,
250                    G_IO_ERROR,
251                    G_IO_ERROR_INVALID_ARGUMENT,
252                    _("Can’t handle version %d of GFileIcon encoding"),
253                    version);
254       goto out;
255     }
256
257   if (num_tokens != 1)
258     {
259       g_set_error_literal (error,
260                            G_IO_ERROR,
261                            G_IO_ERROR_INVALID_ARGUMENT,
262                            _("Malformed input data for GFileIcon"));
263       goto out;
264     }
265
266   file = g_file_new_for_uri (tokens[0]);
267   icon = g_file_icon_new (file);
268   g_object_unref (file);
269
270  out:
271   return icon;
272 }
273
274 static GVariant *
275 g_file_icon_serialize (GIcon *icon)
276 {
277   GFileIcon *file_icon = G_FILE_ICON (icon);
278
279   return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon->file)));
280 }
281
282 static void
283 g_file_icon_icon_iface_init (GIconIface *iface)
284 {
285   iface->hash = g_file_icon_hash;
286   iface->equal = g_file_icon_equal;
287   iface->to_tokens = g_file_icon_to_tokens;
288   iface->from_tokens = g_file_icon_from_tokens;
289   iface->serialize = g_file_icon_serialize;
290 }
291
292
293 static GInputStream *
294 g_file_icon_load (GLoadableIcon  *icon,
295                   int            size,
296                   char          **type,
297                   GCancellable   *cancellable,
298                   GError        **error)
299 {
300   GFileInputStream *stream;
301   GFileIcon *file_icon = G_FILE_ICON (icon);
302
303   stream = g_file_read (file_icon->file,
304                         cancellable,
305                         error);
306
307   if (stream && type)
308     *type = NULL;
309   
310   return G_INPUT_STREAM (stream);
311 }
312
313 static void
314 load_async_callback (GObject      *source_object,
315                      GAsyncResult *res,
316                      gpointer      user_data)
317 {
318   GFileInputStream *stream;
319   GError *error = NULL;
320   GTask *task = user_data;
321
322   stream = g_file_read_finish (G_FILE (source_object), res, &error);
323   if (stream == NULL)
324     g_task_return_error (task, error);
325   else
326     g_task_return_pointer (task, stream, g_object_unref);
327   g_object_unref (task);
328 }
329
330 static void
331 g_file_icon_load_async (GLoadableIcon       *icon,
332                         int                  size,
333                         GCancellable        *cancellable,
334                         GAsyncReadyCallback  callback,
335                         gpointer             user_data)
336 {
337   GFileIcon *file_icon = G_FILE_ICON (icon);
338   GTask *task;
339
340   task = g_task_new (icon, cancellable, callback, user_data);
341   g_task_set_source_tag (task, g_file_icon_load_async);
342   
343   g_file_read_async (file_icon->file, 0,
344                      cancellable,
345                      load_async_callback, task);
346 }
347
348 static GInputStream *
349 g_file_icon_load_finish (GLoadableIcon  *icon,
350                          GAsyncResult   *res,
351                          char          **type,
352                          GError        **error)
353 {
354   g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
355
356   if (type)
357     *type = NULL;
358   
359   return g_task_propagate_pointer (G_TASK (res), error);
360 }
361
362 static void
363 g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
364 {
365   iface->load = g_file_icon_load;
366   iface->load_async = g_file_icon_load_async;
367   iface->load_finish = g_file_icon_load_finish;
368 }