changeset 264:b7ac562d758c

- changed the pattern for twitter icons so that it may match to protected user's icon link too. - when pixbuf is null, pidgin-twitter will renew icon even if new and old URLs are same. - will fall back to default icon if pixbuf couldn't be made from retrieved data. - code cleanup.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 04 Dec 2008 18:24:06 +0900
parents b2c64cd550da
children c2944685ac8e
files icon.c pidgin-twitter.h
diffstat 2 files changed, 71 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/icon.c	Thu Dec 04 02:03:35 2008 +0900
+++ b/icon.c	Thu Dec 04 18:24:06 2008 +0900
@@ -156,6 +156,8 @@
     gchar *url = NULL;
     gint regp_id = -1;
 
+    twitter_debug("called\n");
+
     if(service == twitter_service) {
         data = (icon_data *)g_hash_table_lookup(
             icon_hash[service], user_name);
@@ -177,7 +179,10 @@
         regp_id = IMAGE_JISKO;
     }
 
-    if(!url_text) {
+    /* retrieved nothing or got a bad response */
+    if(!url_text ||
+       (!strstr(url_text, "HTTP/1.1 200 OK") &&
+        !strstr(url_text, "HTTP/1.0 200 OK"))) {
         if(data) {
             data->requested = FALSE;
             data->fetch_data = NULL;
@@ -187,19 +192,7 @@
         return;
     }
 
-    /* if we receive bad response */
-    if(!strstr(url_text, "HTTP/1.1 200 OK") &&
-       !strstr(url_text, "HTTP/1.0 200 OK")) {
-        if(data) {
-            data->requested = FALSE;
-            data->fetch_data = NULL;
-        }
-        g_free(gotdata->user_name);
-        g_free(gotdata);
-        return;
-    }
-
-    /* setup image url */ /* xxx need simplify --yaz */
+    /* setup image url */
     g_regex_match(regp[regp_id], url_text, 0, &match_info);
     if(!g_match_info_matches(match_info)) {
         g_match_info_free(match_info);
@@ -271,7 +264,8 @@
     url = tmp;
 
     /* if requesting icon url is the same as old, return. */
-    if(url && data->icon_url && !strcmp(data->icon_url, url)) {
+    if(data && data->pixbuf &&
+       url && data->icon_url && !strcmp(data->icon_url, url)) {
         twitter_debug("old url = %s new url = %s\n", data->icon_url, url);
         data->requested = FALSE;
         data->fetch_data = NULL;
@@ -296,9 +290,9 @@
     if(url) {
         /* reuse gotdata. just pass given one */
         /* gotdata will be released in got_icon_cb */
-        data->fetch_data = purple_util_fetch_url(url,
-                                                 TRUE, NULL, TRUE,
-                                                 got_icon_cb, gotdata);
+        data->fetch_data =
+            purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
+                                          FALSE, got_icon_cb, gotdata);
         twitter_debug("request %s's icon\n", user_name);
         g_free(url);
     }
@@ -370,39 +364,65 @@
     if(hash)
         data = (icon_data *)g_hash_table_lookup(hash, user_name);
 
+    if(!data) {
+        twitter_debug("cannot retrieve icon_data from hash (should not be called)\n");
+        goto fin_got_icon_cb;
+    }
+
     /* return if download failed */
     if(!url_text) {
         twitter_debug("downloading %s's icon failed : %s\n",
                       user_name, error_message);
-        if(data)
-            data->requested = FALSE;
+
+        data->requested = FALSE;
+
+        goto fin_got_icon_cb;
+    }
+
+    /* remove download request */
+    data->requested = FALSE;
+    data->fetch_data = NULL;
+
+    /* return if user's icon has been downloaded */
+    if(data->pixbuf) {
+        twitter_debug("%s's icon has already been downloaded\n",
+                      user_name);
 
         goto fin_got_icon_cb;
     }
 
-    if(data) {
-        /* remove download request */
-        data->requested = FALSE;
-        data->fetch_data = NULL;
-
-        /* return if user's icon has been downloaded */
-        if(data->pixbuf) {
-            twitter_debug("%s's icon has already been downloaded\n",
-                          user_name);
-
-            goto fin_got_icon_cb;
-        }
-    }
-
     pixbuf = make_scaled_pixbuf(url_text, len);
 
-    if(!pixbuf)
-        goto fin_got_icon_cb;
+    if(!pixbuf) {
+        twitter_debug("cannot make pixbuf from downloaded data\n");
 
+        /* ask to download default icon instead */
+        got_icon_data *gotdata2 = g_new0(got_icon_data, 1);
+        const gchar *url = NULL;
+
+        gotdata2->user_name = g_strdup(gotdata->user_name);
+        gotdata2->service = service;
 
-    if(!data) {
-        twitter_debug("allocate icon_data (shouldn't be called)\n");
-        data = g_new0(icon_data, 1);
+        switch(service) {
+        case twitter_service:
+            url = TWITTER_DEFAULT_ICON_URL;
+            break;
+        case identica_service:
+            url = IDENTICA_DEFAULT_ICON_URL;
+            break;
+        case jisko_service:
+            url = JISKO_DEFAULT_ICON_URL;
+            break;
+        }
+
+        g_free(data->icon_url);
+        data->icon_url = g_strdup(url);
+
+        data->requested = TRUE;
+        data->fetch_data =
+            purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
+                                          FALSE, got_icon_cb, gotdata2);
+        goto fin_got_icon_cb;
     }
 
     data->pixbuf = pixbuf;
@@ -490,19 +510,19 @@
 
     switch(service) {
     case twitter_service:
-        hash = icon_hash[twitter_service];
+        hash = icon_hash[service];
         suffix = "twitter";
         break;
     case wassr_service:
-        hash = icon_hash[wassr_service];
+        hash = icon_hash[service];
         suffix = "wassr";
         break;
     case identica_service:
-        hash = icon_hash[identica_service];
+        hash = icon_hash[service];
         suffix = "identica";
         break;
     case jisko_service:
-        hash = icon_hash[jisko_service];
+        hash = icon_hash[service];
         suffix = "jisko";
         break;
     default:
@@ -612,12 +632,14 @@
            service == wassr_service ||
            service == identica_service ||
            service == jisko_service) {
-            data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
-                                                     got_page_cb, gotdata);
+            data->fetch_data =
+                purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
+                                              TRUE, got_page_cb, gotdata);
         }
-        else {
-            data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
-                                                     got_icon_cb, gotdata);
+        else { /* unused */
+            data->fetch_data =
+                purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
+                                              FALSE, got_icon_cb, gotdata);
         }
         g_free(url); url = NULL;
 
--- a/pidgin-twitter.h	Thu Dec 04 02:03:35 2008 +0900
+++ b/pidgin-twitter.h	Thu Dec 04 18:24:06 2008 +0900
@@ -171,7 +171,7 @@
 #define P_USER              "^.*?(?:<a .+?>)?([-A-Za-z0-9_]+)(?:</a>)?:"
 #define P_CHANNEL           "^(.*?(?:<a .+?>)?[-A-Za-z0-9_]+(?:</a>)?: \\r?\\n?#)([A-Za-z0-9_]+) "
 #define P_TAG_IDENTICA      "#([-A-Za-z0-9_]+)"
-#define P_IMAGE_TWITTER     "<a href=\"/account/profile_image/.+?\"><img .+? id=\"profile-image\".*src=\"(http://.+)\" .+?/>"
+#define P_IMAGE_TWITTER   "<img .*=\"profile-(?:image|img)\".*src=\"(http://.+?)\".*/>"
 #define P_IMAGE_WASSR       "<div class=\"image\"><a href=\".+\"><img src=\"(.+)\" width=\".+?\" /></a></div>"
 #define P_IMAGE_IDENTICA    "<img src=\"(http://.+.identi.ca/.+)\" class=\"avatar profile\" width=\"96\" height=\"96\" alt=\"[A-Za-z0-9_]+\"/>"
 #define P_IMAGE_JISKO       "<img src=\"(http://jisko.net/users/.+/img/avatar/thumb_side\\..+)\" alt=\"Avatar\" />"