changeset 308:bc7d8baf79dd

more robust image type detection.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 02 Jul 2009 05:15:43 +0900
parents 554c8c956a8a
children e922ad73c07e
files icon.c
diffstat 1 files changed, 31 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/icon.c	Sat Jun 13 18:17:54 2009 +0900
+++ b/icon.c	Thu Jul 02 05:15:43 2009 +0900
@@ -4,6 +4,14 @@
 extern GHashTable *icon_hash[];
 extern GRegex *regp[];
 
+static gchar *ext_list[] = {
+    "png",
+    "gif",
+    "jpg",
+    "bmp",
+    NULL
+};
+
 /* prototypes */
 static void insert_icon_at_mark(GtkTextMark *requested_mark, gpointer user_data);
 static void insert_requested_icon(const gchar *user_name, gint service);
@@ -245,17 +253,6 @@
     gchar *slash = strrchr(url, '/');
     *slash = '\0';
 
-    gchar *lower = g_ascii_strdown(slash+1, -1);
-
-    if(strstr(lower, ".png"))
-        data->img_type = "png";
-    else if(strstr(lower, ".gif"))
-        data->img_type = "gif";
-    else if(strstr(lower, ".jpg") || strstr(lower, ".jpeg"))
-        data->img_type = "jpg";
-
-    g_free(lower);
-
     gchar *tmp;
     /* url encode basename. twitter needs this. */
     if(service == twitter_service)
@@ -340,13 +337,6 @@
     return dest;
 }
 
-static gchar *ext_list[] = {
-    "png",
-    "gif",
-    "jpg",
-    NULL
-};
-
 /* this function will be called when requested icon has been retrieved */
 static void
 got_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
@@ -452,16 +442,31 @@
 
     /* find out extension */
     gchar *slash = strrchr(url_data->url, '/');
-    gchar *lower = g_ascii_strdown(slash+1, -1);
+    gchar *lower = g_ascii_strdown(purple_url_decode(slash+1), -1);
+
+    gchar **extp;
+    gchar *dot;
+    data->img_type = NULL;
 
-    if(strstr(lower, ".png"))
-        data->img_type = "png";
-    else if(strstr(lower, ".gif"))
-        data->img_type = "gif";
-    else if(strstr(lower, ".jpg") || strstr(lower, ".jpeg"))
-        data->img_type = "jpg";
+    dot = strrchr(lower, '.');
+    if(dot) {
+        for(extp = ext_list; *extp; extp++) {
+            if(!strcmp(dot+1, *extp)) {
+                data->img_type = *extp;
+                break;
+            }
+        }
+        if(!data->img_type && !strcmp(dot+1, "jpeg"))
+            data->img_type = ext_list[2]; /* jpg */
+    }
+    g_free(lower);
 
-    g_free(lower);
+    /* return if no suitable image found */
+    if(!data->img_type) {
+        g_free(gotdata->user_name);
+        g_free(gotdata);
+        return;
+    }
 
     dirname = purple_prefs_get_string(OPT_ICON_DIR);