diff options
author | Matthias Klumpp <matthias@tenstral.net> | 2016-04-29 06:02:31 +0200 |
---|---|---|
committer | Matthias Klumpp <matthias@tenstral.net> | 2016-04-29 16:42:58 +0200 |
commit | 628e3cfb1c62c3177d6decd86102217f2546e0a2 (patch) | |
tree | fbc990e7528dd47f8904bb9861969d45761ea057 | |
parent | f202867972dfda2e8ddabab74440cc92dfa21d90 (diff) | |
download | appstream-glib-628e3cfb1c62c3177d6decd86102217f2546e0a2.tar.gz |
yaml: Make icon parser spec compliant
This makes the parser read new-style cached icons, local and remote
icons when they are present in DEP-11 YAML.
-rw-r--r-- | data/tests/usr/share/app-info/yaml/aequorea.yml | 5 | ||||
-rw-r--r-- | libappstream-glib/as-app.c | 55 | ||||
-rw-r--r-- | libappstream-glib/as-icon.c | 56 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 7 |
4 files changed, 109 insertions, 14 deletions
diff --git a/data/tests/usr/share/app-info/yaml/aequorea.yml b/data/tests/usr/share/app-info/yaml/aequorea.yml index cca1091..552be08 100644 --- a/data/tests/usr/share/app-info/yaml/aequorea.yml +++ b/data/tests/usr/share/app-info/yaml/aequorea.yml @@ -9,7 +9,10 @@ Name: C: Iceweasel Package: iceweasel Icon: - cached: iceweasel.png + cached: + - name: iceweasel.png + width: 64 + height: 64 Keywords: C: - browser diff --git a/libappstream-glib/as-app.c b/libappstream-glib/as-app.c index b7a8e73..ae80d8f 100644 --- a/libappstream-glib/as-app.c +++ b/libappstream-glib/as-app.c @@ -4138,10 +4138,57 @@ as_app_node_parse_dep11_icons (AsApp *app, GNode *node, guint size; g_autoptr(AsIcon) ic_tmp = NULL; - /* YAML files only specify one icon for various sizes */ - ic_tmp = as_icon_new (); - if (!as_icon_node_parse_dep11 (ic_tmp, node, ctx, error)) - return FALSE; + if (g_strcmp0 (as_yaml_node_get_key (node), "cached") == 0) { + if (node->children == NULL) { + /* legacy compatibility */ + ic_tmp = as_icon_new (); + as_icon_set_kind (ic_tmp, AS_ICON_KIND_CACHED); + as_icon_set_name (ic_tmp, as_yaml_node_get_value (node)); + } else { + GNode *sn; + /* we have a modern YAML file */ + for (sn = node->children; sn != NULL; sn = sn->next) { + g_autoptr(AsIcon) icon = NULL; + icon = as_icon_new (); + as_icon_set_kind (icon, AS_ICON_KIND_CACHED); + if (!as_icon_node_parse_dep11 (icon, sn, ctx, error)) + return FALSE; + as_app_add_icon (app, icon); + } + } + } else if (g_strcmp0 (as_yaml_node_get_key (node), "stock") == 0) { + g_autoptr(AsIcon) icon = NULL; + icon = as_icon_new (); + as_icon_set_name (icon, as_yaml_node_get_value (node)); + as_icon_set_kind (icon, AS_ICON_KIND_STOCK); + as_app_add_icon (app, icon); + } else { + GNode *sn; + AsIconKind ikind; + + if (g_strcmp0 (as_yaml_node_get_key (node), "remote") == 0) { + ikind = AS_ICON_KIND_REMOTE; + } else if (g_strcmp0 (as_yaml_node_get_key (node), "local") == 0) { + ikind = AS_ICON_KIND_REMOTE; + } else { + /* We have an unknown icon type, and just ignore that here */ + return TRUE; + } + + for (sn = node->children; sn != NULL; sn = sn->next) { + g_autoptr(AsIcon) icon = NULL; + icon = as_icon_new (); + as_icon_set_kind (icon, ikind); + if (!as_icon_node_parse_dep11 (icon, sn, ctx, error)) + return FALSE; + as_app_add_icon (app, icon); + } + } + + if (ic_tmp == NULL) { + /* we have no icon which we need to probe sizes for */ + return TRUE; + } /* find each size */ for (i = 0; sizes[i] != NULL; i++) { diff --git a/libappstream-glib/as-icon.c b/libappstream-glib/as-icon.c index 1e6b01d..df719a9 100644 --- a/libappstream-glib/as-icon.c +++ b/libappstream-glib/as-icon.c @@ -714,16 +714,58 @@ as_icon_node_parse (AsIcon *icon, GNode *node, * Since: 0.3.1 **/ gboolean -as_icon_node_parse_dep11 (AsIcon *im, GNode *node, +as_icon_node_parse_dep11 (AsIcon *icon, GNode *node, AsNodeContext *ctx, GError **error) { - if (g_strcmp0 (as_yaml_node_get_key (node), "cached") == 0) { - as_icon_set_name (im, as_yaml_node_get_value (node)); - as_icon_set_kind (im, AS_ICON_KIND_CACHED); - } else if (g_strcmp0 (as_yaml_node_get_key (node), "stock") == 0) { - as_icon_set_name (im, as_yaml_node_get_value (node)); - as_icon_set_kind (im, AS_ICON_KIND_STOCK); + GNode *n; + AsIconPrivate *priv = GET_PRIVATE (icon); + + for (n = node->children; n != NULL; n = n->next) { + const gchar *key; + gint size; + + key = as_yaml_node_get_key (n); + if (g_strcmp0 (key, "width") == 0) { + size = as_yaml_node_get_value_as_int (n); + if (size == G_MAXINT) + size = 64; + priv->width = size; + } else if (g_strcmp0 (key, "height") == 0) { + size = as_yaml_node_get_value_as_int (n); + if (size == G_MAXINT) + size = 64; + priv->height = size; + } else { + if (priv->kind == AS_ICON_KIND_REMOTE) { + if (g_strcmp0 (key, "url") == 0) { + const gchar *media_baseurl; + media_baseurl = as_node_context_get_media_base_url (ctx); + if (media_baseurl == NULL) { + /* no baseurl, we can just set the value as URL */ + as_icon_set_url (icon, as_yaml_node_get_value (n)); + } else { + /* handle the media baseurl */ + g_autofree gchar *url = NULL; + url = g_build_filename (media_baseurl, + as_yaml_node_get_value (n), + NULL); + as_icon_set_url (icon, url); + } + } + } else { + if (g_strcmp0 (key, "name") == 0) { + const gchar *icon_name; + icon_name = as_yaml_node_get_value (n); + + if (g_str_has_prefix (icon_name, "/")) + as_icon_set_filename (icon, icon_name); + else + as_icon_set_name (icon, icon_name); + } + } + } } + return TRUE; } diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index d463983..c1d030b 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -4156,7 +4156,11 @@ as_test_yaml_func (void) " [KVL]C=Iceweasel\n" " [KVL]Package=iceweasel\n" " [MAP]Icon\n" - " [KVL]cached=iceweasel.png\n" + " [SEQ]cached\n" + " [MAP]{\n" + " [KVL]name=iceweasel.png\n" + " [KVL]width=64\n" + " [KVL]height=64\n" " [MAP]Keywords\n" " [SEQ]C\n" " [KEY]browser\n" @@ -4182,7 +4186,6 @@ as_test_yaml_func (void) g_assert (ret); g_string_free (str, TRUE); as_yaml_unref (node); - } static void |