Add g_type_ensure() and use it rather than playing games with volatile
[platform/upstream/glib.git] / gio / gicon.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gicon.h"
28 #include "gthemedicon.h"
29 #include "gfileicon.h"
30 #include "gemblemedicon.h"
31 #include "gfile.h"
32 #include "gioerror.h"
33
34 #include "glibintl.h"
35
36
37 /* There versioning of this is implicit, version 1 would be ".1 " */
38 #define G_ICON_SERIALIZATION_MAGIC0 ". "
39
40 /**
41  * SECTION:gicon
42  * @short_description: Interface for icons
43  * @include: gio/gio.h
44  *
45  * #GIcon is a very minimal interface for icons. It provides functions
46  * for checking the equality of two icons, hashing of icons and
47  * serializing an icon to and from strings.
48  *
49  * #GIcon does not provide the actual pixmap for the icon as this is out 
50  * of GIO's scope, however implementations of #GIcon may contain the name 
51  * of an icon (see #GThemedIcon), or the path to an icon (see #GLoadableIcon). 
52  * 
53  * To obtain a hash of a #GIcon, see g_icon_hash().
54  * 
55  * To check if two #GIcons are equal, see g_icon_equal().
56  *
57  * For serializing a #GIcon, use g_icon_to_string() and
58  * g_icon_new_for_string().
59  *
60  * If your application or library provides one or more #GIcon
61  * implementations you need to ensure that each #GType is registered
62  * with the type system prior to calling g_icon_new_for_string().
63  **/
64
65 typedef GIconIface GIconInterface;
66 G_DEFINE_INTERFACE(GIcon, g_icon, G_TYPE_OBJECT)
67
68 static void
69 g_icon_default_init (GIconInterface *iface)
70 {
71 }
72
73 /**
74  * g_icon_hash:
75  * @icon: #gconstpointer to an icon object.
76  * 
77  * Gets a hash for an icon.
78  *
79  * Virtual: hash
80  * Returns: a #guint containing a hash for the @icon, suitable for 
81  * use in a #GHashTable or similar data structure.
82  **/
83 guint
84 g_icon_hash (gconstpointer icon)
85 {
86   GIconIface *iface;
87
88   g_return_val_if_fail (G_IS_ICON (icon), 0);
89
90   iface = G_ICON_GET_IFACE (icon);
91
92   return (* iface->hash) ((GIcon *)icon);
93 }
94
95 /**
96  * g_icon_equal:
97  * @icon1: (allow-none): pointer to the first #GIcon.
98  * @icon2: (allow-none): pointer to the second #GIcon.
99  * 
100  * Checks if two icons are equal.
101  * 
102  * Returns: %TRUE if @icon1 is equal to @icon2. %FALSE otherwise.
103  **/
104 gboolean
105 g_icon_equal (GIcon *icon1,
106               GIcon *icon2)
107 {
108   GIconIface *iface;
109
110   if (icon1 == NULL && icon2 == NULL)
111     return TRUE;
112
113   if (icon1 == NULL || icon2 == NULL)
114     return FALSE;
115   
116   if (G_TYPE_FROM_INSTANCE (icon1) != G_TYPE_FROM_INSTANCE (icon2))
117     return FALSE;
118
119   iface = G_ICON_GET_IFACE (icon1);
120   
121   return (* iface->equal) (icon1, icon2);
122 }
123
124 static gboolean
125 g_icon_to_string_tokenized (GIcon *icon, GString *s)
126 {
127   GPtrArray *tokens;
128   gint version;
129   GIconIface *icon_iface;
130   int i;
131
132   g_return_val_if_fail (icon != NULL, FALSE);
133   g_return_val_if_fail (G_IS_ICON (icon), FALSE);
134
135   icon_iface = G_ICON_GET_IFACE (icon);
136   if (icon_iface->to_tokens == NULL)
137     return FALSE;
138
139   tokens = g_ptr_array_new ();
140   if (!icon_iface->to_tokens (icon, tokens, &version))
141     {
142       g_ptr_array_free (tokens, TRUE);
143       return FALSE;
144     }
145
146   /* format: TypeName[.Version] <token_0> .. <token_N-1>
147      version 0 is implicit and can be omitted
148      all the tokens are url escaped to ensure they have no spaces in them */
149   
150   g_string_append (s, g_type_name_from_instance ((GTypeInstance *)icon));
151   if (version != 0)
152     g_string_append_printf (s, ".%d", version);
153   
154   for (i = 0; i < tokens->len; i++)
155     {
156       char *token;
157
158       token = g_ptr_array_index (tokens, i);
159
160       g_string_append_c (s, ' ');
161       /* We really only need to escape spaces here, so allow lots of otherwise reserved chars */
162       g_string_append_uri_escaped (s, token,
163                                    G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
164
165       g_free (token);
166     }
167   
168   g_ptr_array_free (tokens, TRUE);
169   
170   return TRUE;
171 }
172
173 /**
174  * g_icon_to_string:
175  * @icon: a #GIcon.
176  *
177  * Generates a textual representation of @icon that can be used for
178  * serialization such as when passing @icon to a different process or
179  * saving it to persistent storage. Use g_icon_new_for_string() to
180  * get @icon back from the returned string.
181  *
182  * The encoding of the returned string is proprietary to #GIcon except
183  * in the following two cases
184  *
185  * <itemizedlist>
186  * <listitem><para>
187  *     If @icon is a #GFileIcon, the returned string is a native path
188  *     (such as <literal>/path/to/my icon.png</literal>) without escaping
189  *     if the #GFile for @icon is a native file.  If the file is not
190  *     native, the returned string is the result of g_file_get_uri()
191  *     (such as <literal>sftp://path/to/my&percnt;20icon.png</literal>).
192  * </para></listitem>
193  * <listitem><para>
194  *    If @icon is a #GThemedIcon with exactly one name, the encoding is
195  *    simply the name (such as <literal>network-server</literal>).
196  * </para></listitem>
197  * </itemizedlist>
198  *
199  * Virtual: to_tokens
200  * Returns: An allocated NUL-terminated UTF8 string or %NULL if @icon can't
201  * be serialized. Use g_free() to free.
202  *
203  * Since: 2.20
204  */
205 gchar *
206 g_icon_to_string (GIcon *icon)
207 {
208   gchar *ret;
209
210   g_return_val_if_fail (icon != NULL, NULL);
211   g_return_val_if_fail (G_IS_ICON (icon), NULL);
212
213   ret = NULL;
214
215   if (G_IS_FILE_ICON (icon))
216     {
217       GFile *file;
218
219       file = g_file_icon_get_file (G_FILE_ICON (icon));
220       if (g_file_is_native (file))
221         {
222           ret = g_file_get_path (file);
223           if (!g_utf8_validate (ret, -1, NULL))
224             {
225               g_free (ret);
226               ret = NULL;
227             }
228         }
229       else
230         ret = g_file_get_uri (file);
231     }
232   else if (G_IS_THEMED_ICON (icon))
233     {
234       const char * const *names;
235
236       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
237       if (names != NULL &&
238           names[0] != NULL &&
239           names[0][0] != '.' && /* Allowing icons starting with dot would break G_ICON_SERIALIZATION_MAGIC0 */
240           g_utf8_validate (names[0], -1, NULL) && /* Only return utf8 strings */
241           names[1] == NULL)
242         ret = g_strdup (names[0]);
243     }
244
245   if (ret == NULL)
246     {
247       GString *s;
248
249       s = g_string_new (G_ICON_SERIALIZATION_MAGIC0);
250
251       if (g_icon_to_string_tokenized (icon, s))
252         ret = g_string_free (s, FALSE);
253       else
254         g_string_free (s, TRUE);
255     }
256
257   return ret;
258 }
259
260 static GIcon *
261 g_icon_new_from_tokens (char   **tokens,
262                         GError **error)
263 {
264   GIcon *icon;
265   char *typename, *version_str;
266   GType type;
267   gpointer klass;
268   GIconIface *icon_iface;
269   gint version;
270   char *endp;
271   int num_tokens;
272   int i;
273
274   icon = NULL;
275   klass = NULL;
276
277   num_tokens = g_strv_length (tokens);
278
279   if (num_tokens < 1)
280     {
281       g_set_error (error,
282                    G_IO_ERROR,
283                    G_IO_ERROR_INVALID_ARGUMENT,
284                    _("Wrong number of tokens (%d)"),
285                    num_tokens);
286       goto out;
287     }
288   
289   typename = tokens[0];
290   version_str = strchr (typename, '.');
291   if (version_str)
292     {
293       *version_str = 0;
294       version_str += 1;
295     }
296   
297   
298   type = g_type_from_name (tokens[0]);
299   if (type == 0)
300     {
301       g_set_error (error,
302                    G_IO_ERROR,
303                    G_IO_ERROR_INVALID_ARGUMENT,
304                    _("No type for class name %s"),
305                    tokens[0]);
306       goto out;
307     }
308
309   if (!g_type_is_a (type, G_TYPE_ICON))
310     {
311       g_set_error (error,
312                    G_IO_ERROR,
313                    G_IO_ERROR_INVALID_ARGUMENT,
314                    _("Type %s does not implement the GIcon interface"),
315                    tokens[0]);
316       goto out;
317     }
318
319   klass = g_type_class_ref (type);
320   if (klass == NULL)
321     {
322       g_set_error (error,
323                    G_IO_ERROR,
324                    G_IO_ERROR_INVALID_ARGUMENT,
325                    _("Type %s is not classed"),
326                    tokens[0]);
327       goto out;
328     }
329
330   version = 0;
331   if (version_str)
332     {
333       version = strtol (version_str, &endp, 10);
334       if (endp == NULL || *endp != '\0')
335         {
336           g_set_error (error,
337                        G_IO_ERROR,
338                        G_IO_ERROR_INVALID_ARGUMENT,
339                        _("Malformed version number: %s"),
340                        version_str);
341           goto out;
342         }
343     }
344
345   icon_iface = g_type_interface_peek (klass, G_TYPE_ICON);
346   g_assert (icon_iface != NULL);
347
348   if (icon_iface->from_tokens == NULL)
349     {
350       g_set_error (error,
351                    G_IO_ERROR,
352                    G_IO_ERROR_INVALID_ARGUMENT,
353                    _("Type %s does not implement from_tokens() on the GIcon interface"),
354                    tokens[0]);
355       goto out;
356     }
357
358   for (i = 1;  i < num_tokens; i++)
359     {
360       char *escaped;
361
362       escaped = tokens[i];
363       tokens[i] = g_uri_unescape_string (escaped, NULL);
364       g_free (escaped);
365     }
366   
367   icon = icon_iface->from_tokens (tokens + 1, num_tokens - 1, version, error);
368
369  out:
370   if (klass != NULL)
371     g_type_class_unref (klass);
372   return icon;
373 }
374
375 static void
376 ensure_builtin_icon_types (void)
377 {
378   g_type_ensure (G_TYPE_THEMED_ICON);
379   g_type_ensure (G_TYPE_FILE_ICON);
380   g_type_ensure (G_TYPE_EMBLEMED_ICON);
381   g_type_ensure (G_TYPE_EMBLEM);
382 }
383
384 /**
385  * g_icon_new_for_string:
386  * @str: A string obtained via g_icon_to_string().
387  * @error: Return location for error.
388  *
389  * Generate a #GIcon instance from @str. This function can fail if
390  * @str is not valid - see g_icon_to_string() for discussion.
391  *
392  * If your application or library provides one or more #GIcon
393  * implementations you need to ensure that each #GType is registered
394  * with the type system prior to calling g_icon_new_for_string().
395  *
396  * Returns: (transfer full): An object implementing the #GIcon
397  *          interface or %NULL if @error is set.
398  *
399  * Since: 2.20
400  **/
401 GIcon *
402 g_icon_new_for_string (const gchar   *str,
403                        GError       **error)
404 {
405   GIcon *icon;
406
407   g_return_val_if_fail (str != NULL, NULL);
408
409   ensure_builtin_icon_types ();
410
411   icon = NULL;
412
413   if (*str == '.')
414     {
415       if (g_str_has_prefix (str, G_ICON_SERIALIZATION_MAGIC0))
416         {
417           gchar **tokens;
418           
419           /* handle tokenized encoding */
420           tokens = g_strsplit (str + sizeof (G_ICON_SERIALIZATION_MAGIC0) - 1, " ", 0);
421           icon = g_icon_new_from_tokens (tokens, error);
422           g_strfreev (tokens);
423         }
424       else
425         g_set_error_literal (error,
426                              G_IO_ERROR,
427                              G_IO_ERROR_INVALID_ARGUMENT,
428                              _("Can't handle the supplied version the icon encoding"));
429     }
430   else
431     {
432       gchar *scheme;
433
434       /* handle special GFileIcon and GThemedIcon cases */
435       scheme = g_uri_parse_scheme (str);
436       if (scheme != NULL || str[0] == '/')
437         {
438           GFile *location;
439           location = g_file_new_for_commandline_arg (str);
440           icon = g_file_icon_new (location);
441           g_object_unref (location);
442         }
443       else
444         icon = g_themed_icon_new (str);
445       g_free (scheme);
446     }
447
448   return icon;
449 }