diff options
author | Richard Hughes <richard@hughsie.com> | 2014-08-09 17:35:34 +0200 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-08-09 18:02:51 +0200 |
commit | 6ce9035f12e7cae693d3af4c2fa6a56a2f46874d (patch) | |
tree | 5a55ca6659715e455d61254b8386a27acaa601d8 | |
parent | dc9bb88f93f35a0057962e65e2cba6a86b454b08 (diff) | |
download | appstream-glib-6ce9035f12e7cae693d3af4c2fa6a56a2f46874d.tar.gz |
Add as_utils_find_icon_filename()
This splits out the functionality from the desktop builder plugin.
-rw-r--r-- | data/tests/Makefile.am | 2 | ||||
-rw-r--r-- | data/tests/usr/share/icons/hicolor/64x64/apps/test2.png | 0 | ||||
-rw-r--r-- | data/tests/usr/share/pixmaps/test.png | 0 | ||||
-rw-r--r-- | libappstream-builder/plugins/asb-plugin-desktop.c | 101 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 55 | ||||
-rw-r--r-- | libappstream-glib/as-utils.c | 117 | ||||
-rw-r--r-- | libappstream-glib/as-utils.h | 3 |
7 files changed, 185 insertions, 93 deletions
diff --git a/data/tests/Makefile.am b/data/tests/Makefile.am index 281e73f..bb8f3e7 100644 --- a/data/tests/Makefile.am +++ b/data/tests/Makefile.am @@ -18,6 +18,8 @@ test_files = \ usr/share/appdata/broken.appdata.xml \ usr/share/app-install/desktop/test.desktop \ usr/share/app-install/icons/test.png \ + usr/share/icons/hicolor/64x64/apps/test2.png \ + usr/share/pixmaps/test.png \ validate.xml.gz EXTRA_DIST = $(test_files) diff --git a/data/tests/usr/share/icons/hicolor/64x64/apps/test2.png b/data/tests/usr/share/icons/hicolor/64x64/apps/test2.png new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data/tests/usr/share/icons/hicolor/64x64/apps/test2.png diff --git a/data/tests/usr/share/pixmaps/test.png b/data/tests/usr/share/pixmaps/test.png new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/data/tests/usr/share/pixmaps/test.png diff --git a/libappstream-builder/plugins/asb-plugin-desktop.c b/libappstream-builder/plugins/asb-plugin-desktop.c index dab9929..e4b788e 100644 --- a/libappstream-builder/plugins/asb-plugin-desktop.c +++ b/libappstream-builder/plugins/asb-plugin-desktop.c @@ -20,6 +20,7 @@ */ #include <config.h> +#include <string.h> #include <fnmatch.h> #include <gdk-pixbuf/gdk-pixbuf.h> @@ -183,101 +184,15 @@ asb_app_find_icon (AsbApp *app, const gchar *something, GError **error) { - guint i; - guint j; - guint k; - guint m; - const gchar *pixmap_dirs[] = { "pixmaps", "icons", NULL }; - const gchar *theme_dirs[] = { "hicolor", "oxygen", NULL }; - const gchar *supported_ext[] = { ".png", - ".gif", - ".svg", - ".xpm", - "", - NULL }; - const gchar *sizes[] = { "64x64", - "128x128", - "96x96", - "256x256", - "scalable", - "48x48", - "32x32", - "24x24", - "16x16", - NULL }; - const gchar *types[] = { "actions", - "animations", - "apps", - "categories", - "devices", - "emblems", - "emotes", - "filesystems", - "intl", - "mimetypes", - "places", - "status", - "stock", - NULL }; - - /* is this an absolute path */ - if (something[0] == '/') { - _cleanup_free_ gchar *tmp; - tmp = g_build_filename (tmpdir, something, NULL); - if (!g_file_test (tmp, G_FILE_TEST_EXISTS)) { - g_set_error (error, - ASB_PLUGIN_ERROR, - ASB_PLUGIN_ERROR_FAILED, - "specified icon '%s' does not exist", - something); - return NULL; - } - return asb_app_load_icon (app, tmp, something, error); - } + _cleanup_free_ gchar *fn = NULL; - /* icon theme apps */ - for (k = 0; theme_dirs[k] != NULL; k++) { - for (i = 0; sizes[i] != NULL; i++) { - for (m = 0; types[m] != NULL; m++) { - for (j = 0; supported_ext[j] != NULL; j++) { - _cleanup_free_ gchar *log; - _cleanup_free_ gchar *tmp; - log = g_strdup_printf ("/usr/share/icons/" - "%s/%s/%s/%s%s", - theme_dirs[k], - sizes[i], - types[m], - something, - supported_ext[j]); - tmp = g_build_filename (tmpdir, log, NULL); - if (g_file_test (tmp, G_FILE_TEST_EXISTS)) - return asb_app_load_icon (app, tmp, log, error); - } - } - } - } - - /* pixmap */ - for (i = 0; pixmap_dirs[i] != NULL; i++) { - for (j = 0; supported_ext[j] != NULL; j++) { - _cleanup_free_ gchar *log; - _cleanup_free_ gchar *tmp; - log = g_strdup_printf ("/usr/share/%s/%s%s", - pixmap_dirs[i], - something, - supported_ext[j]); - tmp = g_build_filename (tmpdir, log, NULL); - if (g_file_test (tmp, G_FILE_TEST_EXISTS)) - return asb_app_load_icon (app, tmp, log, error); - } - } + /* find file */ + fn = as_utils_find_icon_filename (tmpdir, something, error); + if (fn == NULL) + return NULL; - /* failed */ - g_set_error (error, - ASB_PLUGIN_ERROR, - ASB_PLUGIN_ERROR_FAILED, - "Failed to find icon %s", something); - return NULL; + /* load the icon */ + return asb_app_load_icon (app, fn, fn + strlen (tmpdir), error); } /** diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 9f3d194..bbabe29 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -2027,6 +2027,60 @@ as_test_store_speed_desktop_func (void) } static void +as_test_utils_icons_func (void) +{ + gchar *tmp; + GError *error = NULL; + _cleanup_free_ gchar *destdir = NULL; + + destdir = as_test_get_filename ("."); + + /* full path */ + tmp = as_utils_find_icon_filename (destdir, "/usr/share/pixmaps/test.png", &error); + g_assert_cmpstr (tmp, !=, NULL); + g_assert_no_error (error); + g_free (tmp); + + /* full pixmaps name */ + tmp = as_utils_find_icon_filename (destdir, "test.png", &error); + g_assert_cmpstr (tmp, !=, NULL); + g_assert_no_error (error); + g_free (tmp); + + /* pixmaps name */ + tmp = as_utils_find_icon_filename (destdir, "test", &error); + g_assert_cmpstr (tmp, !=, NULL); + g_assert_no_error (error); + g_free (tmp); + + /* full theme name */ + tmp = as_utils_find_icon_filename (destdir, "test2.png", &error); + g_assert_cmpstr (tmp, !=, NULL); + g_assert_no_error (error); + g_free (tmp); + + /* theme name */ + tmp = as_utils_find_icon_filename (destdir, "test2", &error); + g_assert_cmpstr (tmp, !=, NULL); + g_assert_no_error (error); + g_free (tmp); + + /* full pixmaps invalid */ + tmp = as_utils_find_icon_filename (destdir, "/usr/share/pixmaps/not-going-to-exist.png", &error); + g_assert_cmpstr (tmp, ==, NULL); + g_assert_error (error, AS_APP_ERROR, AS_APP_ERROR_FAILED); + g_free (tmp); + g_clear_error (&error); + + /* all invalid */ + tmp = as_utils_find_icon_filename (destdir, "not-going-to-exist.png", &error); + g_assert_cmpstr (tmp, ==, NULL); + g_assert_error (error, AS_APP_ERROR, AS_APP_ERROR_FAILED); + g_free (tmp); + g_clear_error (&error); +} + +static void as_test_utils_spdx_token_func (void) { gchar **tok; @@ -2250,6 +2304,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/node{intltool}", as_test_node_intltool_func); g_test_add_func ("/AppStream/node{sort}", as_test_node_sort_func); g_test_add_func ("/AppStream/utils", as_test_utils_func); + g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func); g_test_add_func ("/AppStream/utils{spdx-token}", as_test_utils_spdx_token_func); g_test_add_func ("/AppStream/store", as_test_store_func); g_test_add_func ("/AppStream/store{merges}", as_test_store_merges_func); diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c index 66896da..b85f364 100644 --- a/libappstream-glib/as-utils.c +++ b/libappstream-glib/as-utils.c @@ -36,6 +36,7 @@ #include <string.h> #include <libsoup/soup.h> +#include "as-app.h" #include "as-cleanup.h" #include "as-enums.h" #include "as-node.h" @@ -669,3 +670,119 @@ as_pixbuf_sharpen (GdkPixbuf *src, gint radius, gdouble amount) p_blurred += rowstride; } } + +/** + * as_utils_find_icon_filename: + * @destdir: the destdir. + * @search: the icon search name, e.g. "microphone.svg" + * @error: A #GError or %NULL + * + * Finds an icon filename from a filesystem root. + * + * Returns: (transfer full): a newly allocated %NULL terminated string + * + * Since: 0.2.5 + **/ +gchar * +as_utils_find_icon_filename (const gchar *destdir, + const gchar *search, + GError **error) +{ + guint i; + guint j; + guint k; + guint m; + const gchar *pixmap_dirs[] = { "pixmaps", "icons", NULL }; + const gchar *theme_dirs[] = { "hicolor", "oxygen", NULL }; + const gchar *supported_ext[] = { ".png", + ".gif", + ".svg", + ".xpm", + "", + NULL }; + const gchar *sizes[] = { "64x64", + "128x128", + "96x96", + "256x256", + "scalable", + "48x48", + "32x32", + "24x24", + "16x16", + NULL }; + const gchar *types[] = { "actions", + "animations", + "apps", + "categories", + "devices", + "emblems", + "emotes", + "filesystems", + "intl", + "mimetypes", + "places", + "status", + "stock", + NULL }; + + /* fallback */ + if (destdir == NULL) + destdir = ""; + + /* is this an absolute path */ + if (search[0] == '/') { + _cleanup_free_ gchar *tmp; + tmp = g_build_filename (destdir, search, NULL); + if (!g_file_test (tmp, G_FILE_TEST_EXISTS)) { + g_set_error (error, + AS_APP_ERROR, + AS_APP_ERROR_FAILED, + "specified icon '%s' does not exist", + search); + return NULL; + } + return g_strdup (tmp); + } + + /* icon theme apps */ + for (k = 0; theme_dirs[k] != NULL; k++) { + for (i = 0; sizes[i] != NULL; i++) { + for (m = 0; types[m] != NULL; m++) { + for (j = 0; supported_ext[j] != NULL; j++) { + _cleanup_free_ gchar *tmp; + tmp = g_strdup_printf ("%s/usr/share/icons/" + "%s/%s/%s/%s%s", + destdir, + theme_dirs[k], + sizes[i], + types[m], + search, + supported_ext[j]); + if (g_file_test (tmp, G_FILE_TEST_EXISTS)) + return g_strdup (tmp); + } + } + } + } + + /* pixmap */ + for (i = 0; pixmap_dirs[i] != NULL; i++) { + for (j = 0; supported_ext[j] != NULL; j++) { + _cleanup_free_ gchar *tmp; + tmp = g_strdup_printf ("%s/usr/share/%s/%s%s", + destdir, + pixmap_dirs[i], + search, + supported_ext[j]); + if (g_file_test (tmp, G_FILE_TEST_EXISTS)) + return g_strdup (tmp); + } + } + + /* failed */ + g_set_error (error, + AS_APP_ERROR, + AS_APP_ERROR_FAILED, + "Failed to find icon %s", search); + return NULL; +} diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h index 2e9b094..f32ca62 100644 --- a/libappstream-glib/as-utils.h +++ b/libappstream-glib/as-utils.h @@ -42,6 +42,9 @@ gchar **as_utils_spdx_license_tokenize (const gchar *license); gboolean as_utils_check_url_exists (const gchar *url, guint timeout, GError **error); +gchar *as_utils_find_icon_filename (const gchar *destdir, + const gchar *search, + GError **error); G_END_DECLS |