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>
28 #include "gthemedicon.h"
29 #include "gfileicon.h"
30 #include "gemblemedicon.h"
31 #include "gbytesicon.h"
34 #include "gioenumtypes.h"
40 /* There versioning of this is implicit, version 1 would be ".1 " */
41 #define G_ICON_SERIALIZATION_MAGIC0 ". "
45 * @short_description: Interface for icons
48 * #GIcon is a very minimal interface for icons. It provides functions
49 * for checking the equality of two icons, hashing of icons and
50 * serializing an icon to and from strings.
52 * #GIcon does not provide the actual pixmap for the icon as this is out
53 * of GIO's scope, however implementations of #GIcon may contain the name
54 * of an icon (see #GThemedIcon), or the path to an icon (see #GLoadableIcon).
56 * To obtain a hash of a #GIcon, see g_icon_hash().
58 * To check if two #GIcons are equal, see g_icon_equal().
60 * For serializing a #GIcon, use g_icon_serialize() and
61 * g_icon_deserialize().
63 * If you want to consume #GIcon (for example, in a toolkit) you must
64 * be prepared to handle at least the three following cases:
65 * #GLoadableIcon, #GThemedIcon and #GEmblemedIcon. It may also make
66 * sense to have fast-paths for other cases (like handling #GdkPixbuf
67 * directly, for example) but all compliant #GIcon implementations
68 * outside of GIO must implement #GLoadableIcon.
70 * If your application or library provides one or more #GIcon
71 * implementations you need to ensure that your new implementation also
72 * implements #GLoadableIcon. Additionally, you must provide an
73 * implementation of g_icon_serialize() that gives a result that is
74 * understood by g_icon_deserialize(), yielding one of the built-in icon
78 typedef GIconIface GIconInterface;
79 G_DEFINE_INTERFACE(GIcon, g_icon, G_TYPE_OBJECT)
82 g_icon_default_init (GIconInterface *iface)
88 * @icon: #gconstpointer to an icon object.
90 * Gets a hash for an icon.
93 * Returns: a #guint containing a hash for the @icon, suitable for
94 * use in a #GHashTable or similar data structure.
97 g_icon_hash (gconstpointer icon)
101 g_return_val_if_fail (G_IS_ICON (icon), 0);
103 iface = G_ICON_GET_IFACE (icon);
105 return (* iface->hash) ((GIcon *)icon);
110 * @icon1: (allow-none): pointer to the first #GIcon.
111 * @icon2: (allow-none): pointer to the second #GIcon.
113 * Checks if two icons are equal.
115 * Returns: %TRUE if @icon1 is equal to @icon2. %FALSE otherwise.
118 g_icon_equal (GIcon *icon1,
123 if (icon1 == NULL && icon2 == NULL)
126 if (icon1 == NULL || icon2 == NULL)
129 if (G_TYPE_FROM_INSTANCE (icon1) != G_TYPE_FROM_INSTANCE (icon2))
132 iface = G_ICON_GET_IFACE (icon1);
134 return (* iface->equal) (icon1, icon2);
138 g_icon_to_string_tokenized (GIcon *icon, GString *s)
142 GIconIface *icon_iface;
145 g_return_val_if_fail (icon != NULL, FALSE);
146 g_return_val_if_fail (G_IS_ICON (icon), FALSE);
148 icon_iface = G_ICON_GET_IFACE (icon);
149 if (icon_iface->to_tokens == NULL)
152 tokens = g_ptr_array_new ();
153 if (!icon_iface->to_tokens (icon, tokens, &version))
155 g_ptr_array_free (tokens, TRUE);
159 /* format: TypeName[.Version] <token_0> .. <token_N-1>
160 version 0 is implicit and can be omitted
161 all the tokens are url escaped to ensure they have no spaces in them */
163 g_string_append (s, g_type_name_from_instance ((GTypeInstance *)icon));
165 g_string_append_printf (s, ".%d", version);
167 for (i = 0; i < tokens->len; i++)
171 token = g_ptr_array_index (tokens, i);
173 g_string_append_c (s, ' ');
174 /* We really only need to escape spaces here, so allow lots of otherwise reserved chars */
175 g_string_append_uri_escaped (s, token,
176 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
181 g_ptr_array_free (tokens, TRUE);
190 * Generates a textual representation of @icon that can be used for
191 * serialization such as when passing @icon to a different process or
192 * saving it to persistent storage. Use g_icon_new_for_string() to
193 * get @icon back from the returned string.
195 * The encoding of the returned string is proprietary to #GIcon except
196 * in the following two cases
200 * If @icon is a #GFileIcon, the returned string is a native path
201 * (such as <literal>/path/to/my icon.png</literal>) without escaping
202 * if the #GFile for @icon is a native file. If the file is not
203 * native, the returned string is the result of g_file_get_uri()
204 * (such as <literal>sftp://path/to/my%20icon.png</literal>).
207 * If @icon is a #GThemedIcon with exactly one name, the encoding is
208 * simply the name (such as <literal>network-server</literal>).
213 * Returns: An allocated NUL-terminated UTF8 string or %NULL if @icon can't
214 * be serialized. Use g_free() to free.
219 g_icon_to_string (GIcon *icon)
223 g_return_val_if_fail (icon != NULL, NULL);
224 g_return_val_if_fail (G_IS_ICON (icon), NULL);
228 if (G_IS_FILE_ICON (icon))
232 file = g_file_icon_get_file (G_FILE_ICON (icon));
233 if (g_file_is_native (file))
235 ret = g_file_get_path (file);
236 if (!g_utf8_validate (ret, -1, NULL))
243 ret = g_file_get_uri (file);
245 else if (G_IS_THEMED_ICON (icon))
247 const char * const *names;
249 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
252 names[0][0] != '.' && /* Allowing icons starting with dot would break G_ICON_SERIALIZATION_MAGIC0 */
253 g_utf8_validate (names[0], -1, NULL) && /* Only return utf8 strings */
255 ret = g_strdup (names[0]);
262 s = g_string_new (G_ICON_SERIALIZATION_MAGIC0);
264 if (g_icon_to_string_tokenized (icon, s))
265 ret = g_string_free (s, FALSE);
267 g_string_free (s, TRUE);
274 g_icon_new_from_tokens (char **tokens,
278 char *typename, *version_str;
281 GIconIface *icon_iface;
290 num_tokens = g_strv_length (tokens);
296 G_IO_ERROR_INVALID_ARGUMENT,
297 _("Wrong number of tokens (%d)"),
302 typename = tokens[0];
303 version_str = strchr (typename, '.');
311 type = g_type_from_name (tokens[0]);
316 G_IO_ERROR_INVALID_ARGUMENT,
317 _("No type for class name %s"),
322 if (!g_type_is_a (type, G_TYPE_ICON))
326 G_IO_ERROR_INVALID_ARGUMENT,
327 _("Type %s does not implement the GIcon interface"),
332 klass = g_type_class_ref (type);
337 G_IO_ERROR_INVALID_ARGUMENT,
338 _("Type %s is not classed"),
346 version = strtol (version_str, &endp, 10);
347 if (endp == NULL || *endp != '\0')
351 G_IO_ERROR_INVALID_ARGUMENT,
352 _("Malformed version number: %s"),
358 icon_iface = g_type_interface_peek (klass, G_TYPE_ICON);
359 g_assert (icon_iface != NULL);
361 if (icon_iface->from_tokens == NULL)
365 G_IO_ERROR_INVALID_ARGUMENT,
366 _("Type %s does not implement from_tokens() on the GIcon interface"),
371 for (i = 1; i < num_tokens; i++)
376 tokens[i] = g_uri_unescape_string (escaped, NULL);
380 icon = icon_iface->from_tokens (tokens + 1, num_tokens - 1, version, error);
384 g_type_class_unref (klass);
389 ensure_builtin_icon_types (void)
391 g_type_ensure (G_TYPE_THEMED_ICON);
392 g_type_ensure (G_TYPE_FILE_ICON);
393 g_type_ensure (G_TYPE_EMBLEMED_ICON);
394 g_type_ensure (G_TYPE_EMBLEM);
397 /* handles the 'simple' cases: GFileIcon and GThemedIcon */
399 g_icon_new_for_string_simple (const gchar *str)
407 /* handle special GFileIcon and GThemedIcon cases */
408 scheme = g_uri_parse_scheme (str);
409 if (scheme != NULL || str[0] == '/' || str[0] == G_DIR_SEPARATOR)
412 location = g_file_new_for_commandline_arg (str);
413 icon = g_file_icon_new (location);
414 g_object_unref (location);
417 icon = g_themed_icon_new (str);
425 * g_icon_new_for_string:
426 * @str: A string obtained via g_icon_to_string().
427 * @error: Return location for error.
429 * Generate a #GIcon instance from @str. This function can fail if
430 * @str is not valid - see g_icon_to_string() for discussion.
432 * If your application or library provides one or more #GIcon
433 * implementations you need to ensure that each #GType is registered
434 * with the type system prior to calling g_icon_new_for_string().
436 * Returns: (transfer full): An object implementing the #GIcon
437 * interface or %NULL if @error is set.
442 g_icon_new_for_string (const gchar *str,
447 g_return_val_if_fail (str != NULL, NULL);
449 icon = g_icon_new_for_string_simple (str);
453 ensure_builtin_icon_types ();
455 if (g_str_has_prefix (str, G_ICON_SERIALIZATION_MAGIC0))
459 /* handle tokenized encoding */
460 tokens = g_strsplit (str + sizeof (G_ICON_SERIALIZATION_MAGIC0) - 1, " ", 0);
461 icon = g_icon_new_from_tokens (tokens, error);
465 g_set_error_literal (error,
467 G_IO_ERROR_INVALID_ARGUMENT,
468 _("Can't handle the supplied version of the icon encoding"));
474 g_icon_deserialize_emblem (GVariant *value)
476 GVariant *emblem_metadata;
477 GVariant *emblem_data;
478 const gchar *origin_nick;
482 g_variant_get (value, "(v@a{sv})", &emblem_data, &emblem_metadata);
486 emblem_icon = g_icon_deserialize (emblem_data);
487 if (emblem_icon != NULL)
489 /* Check if we should create it with an origin. */
490 if (g_variant_lookup (emblem_metadata, "origin", "&s", &origin_nick))
492 GEnumClass *origin_class;
493 GEnumValue *origin_value;
495 origin_class = g_type_class_ref (G_TYPE_EMBLEM_ORIGIN);
496 origin_value = g_enum_get_value_by_nick (origin_class, origin_nick);
498 emblem = g_emblem_new_with_origin (emblem_icon, origin_value->value);
499 g_type_class_unref (origin_class);
502 /* We didn't create it with an origin, so do it without. */
504 emblem = g_emblem_new (emblem_icon);
506 g_object_unref (emblem_icon);
509 g_variant_unref (emblem_metadata);
510 g_variant_unref (emblem_data);
516 g_icon_deserialize_emblemed (GVariant *value)
518 GVariantIter *emblems;
523 g_variant_get (value, "(va(va{sv}))", &icon_data, &emblems);
524 main_icon = g_icon_deserialize (icon_data);
528 GVariant *emblem_data;
530 icon = g_emblemed_icon_new (main_icon, NULL);
532 while ((emblem_data = g_variant_iter_next_value (emblems)))
536 emblem = g_icon_deserialize_emblem (emblem_data);
540 g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon), emblem);
541 g_object_unref (emblem);
544 g_variant_unref (emblem_data);
547 g_object_unref (main_icon);
552 g_variant_iter_free (emblems);
553 g_variant_unref (icon_data);
559 * g_icon_deserialize:
560 * @value: a #GVariant created with g_icon_serialize()
562 * Deserializes a #GIcon previously serialized using g_icon_serialize().
564 * Returns: (transfer full): a #GIcon, or %NULL when deserialization fails.
569 g_icon_deserialize (GVariant *value)
575 g_return_val_if_fail (value != NULL, NULL);
576 g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
577 g_variant_is_of_type (value, G_VARIANT_TYPE ("(sv)")), NULL);
579 /* Handle some special cases directly so that people can hard-code
580 * stuff into GMenuModel xml files without resorting to using GVariant
581 * text format to describe one of the explicitly-tagged possibilities
584 if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
585 return g_icon_new_for_string_simple (g_variant_get_string (value, NULL));
587 /* Otherwise, use the tagged union format */
588 g_variant_get (value, "(&sv)", &tag, &val);
592 if (g_str_equal (tag, "file") && g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
596 file = g_file_new_for_commandline_arg (g_variant_get_string (val, NULL));
597 icon = g_file_icon_new (file);
598 g_object_unref (file);
600 else if (g_str_equal (tag, "themed") && g_variant_is_of_type (val, G_VARIANT_TYPE_STRING_ARRAY))
605 names = g_variant_get_strv (val, &size);
606 icon = g_themed_icon_new_from_names ((gchar **) names, size);
609 else if (g_str_equal (tag, "bytes") && g_variant_is_of_type (val, G_VARIANT_TYPE_BYTESTRING))
613 bytes = g_variant_get_data_as_bytes (val);
614 icon = g_bytes_icon_new (bytes);
615 g_bytes_unref (bytes);
617 else if (g_str_equal (tag, "emblem") && g_variant_is_of_type (val, G_VARIANT_TYPE ("(va{sv})")))
621 emblem = g_icon_deserialize_emblem (val);
623 icon = G_ICON (emblem);
625 else if (g_str_equal (tag, "emblemed") && g_variant_is_of_type (val, G_VARIANT_TYPE ("(va(va{sv}))")))
627 icon = g_icon_deserialize_emblemed (val);
629 else if (g_str_equal (tag, "gvfs"))
634 vfs = g_vfs_get_default ();
635 class = G_VFS_GET_CLASS (vfs);
636 if (class->deserialize_icon)
637 icon = (* class->deserialize_icon) (vfs, val);
640 g_variant_unref (val);
649 * Serializes a #GIcon into a #GVariant. An equivalent #GIcon can be retrieved
650 * back by calling g_icon_deserialize() on the returned value.
651 * As serialization will avoid using raw icon data when possible, it only
652 * makes sense to transfer the #GVariant between processes on the same machine,
653 * (as opposed to over the network), and within the same file system namespace.
655 * Returns: (transfer full): a #GVariant, or %NULL when serialization fails.
660 g_icon_serialize (GIcon *icon)
662 GIconInterface *iface;
665 iface = G_ICON_GET_IFACE (icon);
667 if (!iface->serialize)
669 g_critical ("g_icon_serialize() on icon type '%s' is not implemented", G_OBJECT_TYPE_NAME (icon));
673 result = (* iface->serialize) (icon);
677 g_variant_take_ref (result);
679 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(sv)")))
681 g_critical ("g_icon_serialize() on icon type '%s' returned GVariant of type '%s' but it must return "
682 "one with type '(sv)'", G_OBJECT_TYPE_NAME (icon), g_variant_get_type_string (result));
683 g_variant_unref (result);