Generalise theme extension handling
authorRicardo Mones <ricardo@mones.org>
Mon, 30 Jan 2017 23:11:47 +0000 (00:11 +0100)
committerRicardo Mones <ricardo@mones.org>
Tue, 31 Jan 2017 11:21:45 +0000 (12:21 +0100)
src/prefs_themes.c
src/stock_pixmap.c
src/stock_pixmap.h

index c9d2ecc7637fd970eea3912c8834b43b42ea6f84..1d24f18b4000f55684b2c20e12938d112499f4af 100644 (file)
@@ -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;
 }
index 2fd5a7bbda96d0263f81286f55d71c2ca818c6ed..70ff84e1e7454fd8d15699435cf8b1295cdf5ae2 100644 (file)
@@ -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",
index 0e7a96b3be9c77111a7714ed6591ed7c49908edd..447e5dab5e588102a6b64fd5fc67661ca820ac59 100644 (file)
@@ -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__ */