From 8e02f3f3c6b091009410b081d40d187e76fe45aa Mon Sep 17 00:00:00 2001 From: Ricardo Mones Date: Tue, 31 Jan 2017 00:11:47 +0100 Subject: [PATCH] Generalise theme extension handling --- src/prefs_themes.c | 32 +++++++++++++++++++++++--------- src/stock_pixmap.c | 15 +++++++++++++-- src/stock_pixmap.h | 1 + 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/prefs_themes.c b/src/prefs_themes.c index c9d2ecc76..1d24f18b4 100644 --- a/src/prefs_themes.c +++ b/src/prefs_themes.c @@ -103,6 +103,9 @@ typedef struct _DirInfo { gint bytes; gint files; gint pixms; + /* extensions info */ + const char **supported; + gint *length; } DirInfo; typedef struct _CopyInfo { @@ -157,17 +160,21 @@ static void prefs_themes_file_stats(const gchar *filename, gpointer data) GStatBuf s; DirInfo *di = (DirInfo *)data; gint len; + gint i; if (0 == g_stat(filename, &s) && 0 != S_ISREG(s.st_mode)) { di->bytes += s.st_size; di->files++; len = strlen(filename); - if (len > 4) { - const gchar *extension = filename+(len-4); - if (!strcmp(extension, ".xpm")) - di->pixms++; - else if (!strcmp(extension, ".png")) + for (i = 0; (di->supported)[i] != NULL; ++i) { + gint curlen = (di->length)[i]; + if (len <= curlen) + continue; + const gchar *extension = filename + (len - curlen); + if (!strcmp(extension, (di->supported)[i])) { di->pixms++; + break; + } } } } @@ -748,13 +755,20 @@ static gchar *prefs_themes_get_theme_stats(const gchar *dirname) { gchar *stats; DirInfo *dinfo; + gint i; dinfo = g_new0(DirInfo, 1); - + dinfo->supported = stock_pixmap_theme_extensions(); + for (i = 0; (dinfo->supported)[i] != NULL; ++i); + dinfo->length = g_malloc(i * sizeof(gint)); + for (i = 0; (dinfo->supported)[i] != NULL; ++i) { + (dinfo->length)[i] = strlen((dinfo->supported)[i]); + } prefs_themes_foreach_file(dirname, prefs_themes_file_stats, dinfo); - stats = g_strdup_printf(_("%d files (%d icons), size: %s"), - dinfo->files, dinfo->pixms, to_human_readable((goffset)dinfo->bytes)); - + stats = g_strdup_printf(_("%d files (%d icons), size: %s"), + dinfo->files, dinfo->pixms, + to_human_readable((goffset)dinfo->bytes)); + g_free(dinfo->length); g_free(dinfo); return stats; } diff --git a/src/stock_pixmap.c b/src/stock_pixmap.c index 2fd5a7bbd..70ff84e1e 100644 --- a/src/stock_pixmap.c +++ b/src/stock_pixmap.c @@ -460,6 +460,19 @@ static StockPixmapData pixmaps[] = {empty_xpm , NULL, NULL, "empty", NULL, NULL} }; +/* Supported theme extensions */ +static const char *extension[] = { + ".png", + ".xpm", + NULL +}; + +/* return current supported extensions */ +const char **stock_pixmap_theme_extensions(void) +{ + return extension; +} + /* return newly constructed GtkPixmap from GdkPixmap */ GtkWidget *stock_pixmap_widget(StockPixmap icon) { @@ -479,7 +492,6 @@ GtkWidget *stock_pixmap_widget(StockPixmap icon) gint stock_pixbuf_gdk(StockPixmap icon, GdkPixbuf **pixbuf) { StockPixmapData *pix_d; - static const char *extension[]={".png", ".xpm", NULL}; int i = 0; gboolean theme_changed = FALSE; @@ -556,7 +568,6 @@ static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname) gchar *fullentry; GDir *dp; GError *error = NULL; - static const char *extension[]={".png", ".xpm", NULL}; if ((dp = g_dir_open(dirname, 0, &error)) == NULL) { debug_print("skipping theme scan, dir %s could not be opened: %s (%d)\n", diff --git a/src/stock_pixmap.h b/src/stock_pixmap.h index 0e7a96b3b..447e5dab5 100644 --- a/src/stock_pixmap.h +++ b/src/stock_pixmap.h @@ -245,5 +245,6 @@ GtkWidget *stock_pixmap_widget_with_overlay (StockPixmap icon, gint border_x, gint border_y); gchar *stock_pixmap_get_system_theme_dir_for_theme(const gchar *theme); +const char **stock_pixmap_theme_extensions(void); #endif /* __STOCK_PIXMAP_H__ */ -- 2.25.1