From 1de81553a6c7b3dafbed6567c917ee600b4d6d0b Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 11 Jan 2011 00:45:59 +0200 Subject: [PATCH] * mu-widget-util.[ch]: be smarter about pixmaps (icons) for unknown mime types --- widgets/mu-widget-util.c | 51 ++++++++++++++++++++++++++++++++-------- widgets/mu-widget-util.h | 4 +++- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/widgets/mu-widget-util.c b/widgets/mu-widget-util.c index f9824510..8aa084b1 100644 --- a/widgets/mu-widget-util.c +++ b/widgets/mu-widget-util.c @@ -22,22 +22,33 @@ #include #include "mu-widget-util.h" -GdkPixbuf* -mu_widget_util_get_icon_pixbuf_for_content_type (const char *ctype, - size_t size) +static const char* +second_guess_content_type (const char *ctype) +{ + int i; + struct { + const char *orig, *subst; + } substtable [] = { + {"text", "text/plain"}, + {"image/pjpeg", "image/jpeg" } + }; + + for (i = 0; i != G_N_ELEMENTS(substtable); ++i) + if (g_str_has_prefix (ctype, substtable[i].orig)) + return substtable[i].subst; + + return "application/octet-stream"; +} + +static GdkPixbuf* +get_icon_pixbuf_for_content_type (const char *ctype, size_t size) { GIcon *icon; GdkPixbuf *pixbuf; - - g_return_val_if_fail (ctype, NULL); - g_return_val_if_fail (size > 0, NULL); icon = g_content_type_get_icon (ctype); pixbuf = NULL; - - /* if (g_strcmp0 (ctype, "image/pjpeg") == 0) */ - /* ctype = "image/jpeg"; */ - + /* based on a snippet from http://www.gtkforums.com/about4721.html */ if (G_IS_THEMED_ICON(icon)) { gchar const * const *names; @@ -57,3 +68,23 @@ mu_widget_util_get_icon_pixbuf_for_content_type (const char *ctype, return pixbuf; } + + +GdkPixbuf* +mu_widget_util_get_icon_pixbuf_for_content_type (const char *ctype, size_t size) +{ + GdkPixbuf *pixbuf; + + g_return_val_if_fail (ctype, NULL); + g_return_val_if_fail (size > 0, NULL); + + pixbuf = get_icon_pixbuf_for_content_type (ctype, size); + if (!pixbuf) + pixbuf = get_icon_pixbuf_for_content_type + (second_guess_content_type (ctype), size); + + return pixbuf; +} + + + diff --git a/widgets/mu-widget-util.h b/widgets/mu-widget-util.h index 5d47fe38..cc3060a7 100644 --- a/widgets/mu-widget-util.h +++ b/widgets/mu-widget-util.h @@ -32,6 +32,8 @@ * g_object_unref when the pixbuf is no longer needed. */ GdkPixbuf* mu_widget_util_get_icon_pixbuf_for_content_type (const char *ctype, - size_t size); + size_t size) + G_GNUC_WARN_UNUSED_RESULT; + #endif /*__MU_WIDGET_UTIL_H__*/