mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
GIcon: add g_icon_[de]serialize()
Add support for serialising a GIcon to a GVariant and deserialising the result back to a GIcon. This solves a number of problems suffered by the existing to_string() API, primarily these: - not forcing the icon to be a utf8 string means that we can efficiently encode a PNG (ie: just give the array of bytes) - there is no need to ensure that proper types are loaded before using the deserialisation interface. 'Foreign' icon types will probably emit a serialised format the deserialises to a GBytesIcon. We additionally clearly document what is required for being a consumer or implementation of #GIcon. Further patches will be required to GdkPixbuf and GVfsIcon to bring their implementations in line with the new rules (essentially: introduce implementations of the new serialize() API). https://bugzilla.gnome.org/show_bug.cgi?id=688820
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
test_g_icon_serialize (void)
|
||||
test_g_icon_to_string (void)
|
||||
{
|
||||
GIcon *icon;
|
||||
GIcon *icon2;
|
||||
@@ -247,6 +247,125 @@ test_g_icon_serialize (void)
|
||||
g_free (data);
|
||||
}
|
||||
|
||||
static void
|
||||
test_g_icon_serialize (void)
|
||||
{
|
||||
GIcon *icon;
|
||||
GIcon *icon2;
|
||||
GIcon *icon3;
|
||||
GIcon *icon4;
|
||||
GIcon *icon5;
|
||||
GEmblem *emblem1;
|
||||
GEmblem *emblem2;
|
||||
GFile *location;
|
||||
GVariant *data;
|
||||
gint origin;
|
||||
GIcon *i;
|
||||
|
||||
/* Check that we can deserialize from well-known specified formats */
|
||||
data = g_variant_new_string ("network-server%");
|
||||
icon = g_icon_deserialize (g_variant_ref_sink (data));
|
||||
g_variant_unref (data);
|
||||
icon2 = g_themed_icon_new ("network-server%");
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
|
||||
data = g_variant_new_string ("/path/to/somewhere.png");
|
||||
icon = g_icon_deserialize (g_variant_ref_sink (data));
|
||||
g_variant_unref (data);
|
||||
location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
|
||||
icon2 = g_file_icon_new (location);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
g_object_unref (location);
|
||||
|
||||
data = g_variant_new_string ("/path/to/somewhere with whitespace.png");
|
||||
icon = g_icon_deserialize (g_variant_ref_sink (data));
|
||||
g_variant_unref (data);
|
||||
location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
|
||||
icon2 = g_file_icon_new (location);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_object_unref (location);
|
||||
g_object_unref (icon2);
|
||||
location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
|
||||
icon2 = g_file_icon_new (location);
|
||||
g_assert (!g_icon_equal (icon, icon2));
|
||||
g_object_unref (location);
|
||||
g_object_unref (icon2);
|
||||
g_object_unref (icon);
|
||||
|
||||
data = g_variant_new_string ("sftp:///path/to/somewhere.png");
|
||||
icon = g_icon_deserialize (g_variant_ref_sink (data));
|
||||
g_variant_unref (data);
|
||||
location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
|
||||
icon2 = g_file_icon_new (location);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
g_object_unref (location);
|
||||
|
||||
/* Check that GThemedIcon serialization works */
|
||||
|
||||
icon = g_themed_icon_new ("network-server");
|
||||
g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
|
||||
data = g_icon_serialize (icon);
|
||||
icon2 = g_icon_deserialize (data);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_variant_unref (data);
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
|
||||
icon = g_themed_icon_new ("icon name with whitespace");
|
||||
g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
|
||||
data = g_icon_serialize (icon);
|
||||
icon2 = g_icon_deserialize (data);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_variant_unref (data);
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
|
||||
icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
|
||||
g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
|
||||
data = g_icon_serialize (icon);
|
||||
icon2 = g_icon_deserialize (data);
|
||||
g_assert (g_icon_equal (icon, icon2));
|
||||
g_variant_unref (data);
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
|
||||
/* Check that GEmblemedIcon serialization works */
|
||||
|
||||
icon = g_themed_icon_new ("face-smirk");
|
||||
icon2 = g_themed_icon_new ("emblem-important");
|
||||
g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
|
||||
location = g_file_new_for_uri ("file:///some/path/somewhere.png");
|
||||
icon3 = g_file_icon_new (location);
|
||||
g_object_unref (location);
|
||||
emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
|
||||
emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
|
||||
icon4 = g_emblemed_icon_new (icon, emblem1);
|
||||
g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
|
||||
data = g_icon_serialize (icon4);
|
||||
icon5 = g_icon_deserialize (data);
|
||||
g_assert (g_icon_equal (icon4, icon5));
|
||||
|
||||
g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
|
||||
g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
|
||||
g_assert (i == icon2);
|
||||
g_object_unref (i);
|
||||
|
||||
g_object_unref (emblem1);
|
||||
g_object_unref (emblem2);
|
||||
g_object_unref (icon);
|
||||
g_object_unref (icon2);
|
||||
g_object_unref (icon3);
|
||||
g_object_unref (icon4);
|
||||
g_object_unref (icon5);
|
||||
g_variant_unref (data);
|
||||
}
|
||||
|
||||
static void
|
||||
test_themed_icon (void)
|
||||
{
|
||||
@@ -373,6 +492,7 @@ main (int argc,
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/icons/to-string", test_g_icon_to_string);
|
||||
g_test_add_func ("/icons/serialize", test_g_icon_serialize);
|
||||
g_test_add_func ("/icons/themed", test_themed_icon);
|
||||
g_test_add_func ("/icons/emblemed", test_emblemed_icon);
|
||||
|
Reference in New Issue
Block a user