diff options
author | Richard Hughes <richard@hughsie.com> | 2016-01-14 16:57:43 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2016-01-14 16:57:43 +0000 |
commit | 7c4d511f1e1c01670a9e6551470698caf5ec2ee9 (patch) | |
tree | a94ba3965773d6f34e2ba60b094e027b1ab7f64e | |
parent | 806b72f6dafbe6e2968955833ee93c07d3dbc793 (diff) | |
download | appstream-glib-7c4d511f1e1c01670a9e6551470698caf5ec2ee9.tar.gz |
Prefer stock icons when using as_app_get_icon_default()
-rw-r--r-- | libappstream-glib/as-app.c | 27 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 13 |
2 files changed, 36 insertions, 4 deletions
diff --git a/libappstream-glib/as-app.c b/libappstream-glib/as-app.c index 996cb33..31ffd58 100644 --- a/libappstream-glib/as-app.c +++ b/libappstream-glib/as-app.c @@ -4479,9 +4479,36 @@ as_app_get_icon_default (AsApp *app) { AsAppPrivate *priv = GET_PRIVATE (app); AsIcon *icon; + guint i; + guint j; + AsIconKind kinds[] = { + AS_ICON_KIND_STOCK, + AS_ICON_KIND_LOCAL, + AS_ICON_KIND_CACHED, + AS_ICON_KIND_EMBEDDED, + AS_ICON_KIND_REMOTE, + AS_ICON_KIND_UNKNOWN }; + /* nothing */ if (priv->icons->len == 0) return NULL; + + /* optimise common case */ + if (priv->icons->len == 1) { + icon = g_ptr_array_index (priv->icons, 0); + return icon; + } + + /* search for icons in the preferred order */ + for (j = 0; kinds[j] != AS_ICON_KIND_UNKNOWN; j++) { + for (i = 0; i < priv->icons->len; i++) { + icon = g_ptr_array_index (priv->icons, i); + if (as_icon_get_kind (icon) == kinds[j]) + return icon; + } + } + + /* we can't decide, just return the first added */ icon = g_ptr_array_index (priv->icons, 0); return icon; } diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index add9a8f..fd1e8d2 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -2354,13 +2354,18 @@ as_test_app_subsume_func (void) g_autoptr(AsApp) app = NULL; g_autoptr(AsApp) donor = NULL; g_autoptr(AsIcon) icon = NULL; + g_autoptr(AsIcon) icon2 = NULL; g_autoptr(AsScreenshot) ss = NULL; donor = as_app_new (); icon = as_icon_new (); - as_icon_set_name (icon, "gtk-find"); - as_icon_set_kind (icon, AS_ICON_KIND_LOCAL); + as_icon_set_name (icon, "some-custom-icon"); + as_icon_set_kind (icon, AS_ICON_KIND_CACHED); as_app_add_icon (donor, icon); + icon2 = as_icon_new (); + as_icon_set_name (icon2, "gtk-find"); + as_icon_set_kind (icon2, AS_ICON_KIND_STOCK); + as_app_add_icon (donor, icon2); as_app_set_state (donor, AS_APP_STATE_INSTALLED); as_app_add_pkgname (donor, "hal"); as_app_add_language (donor, -1, "en_GB"); @@ -2390,11 +2395,11 @@ as_test_app_subsume_func (void) g_list_free (list); /* check icon */ - g_assert_cmpint (as_app_get_icons(app)->len, ==, 1); + g_assert_cmpint (as_app_get_icons(app)->len, ==, 2); ic = as_app_get_icon_default (app); g_assert (ic != NULL); g_assert_cmpstr (as_icon_get_name (ic), ==, "gtk-find"); - g_assert_cmpint (as_icon_get_kind (ic), ==, AS_ICON_KIND_LOCAL); + g_assert_cmpint (as_icon_get_kind (ic), ==, AS_ICON_KIND_STOCK); g_assert_cmpint (as_icon_get_width (ic), ==, 0); g_assert_cmpint (as_icon_get_height (ic), ==, 0); |