# HG changeset patch # User Yoshiki Yazawa # Date 1214154617 -32400 # Node ID 0ddcba9161fd6d6a7be770232025f24c3eafdba5 # Parent 4949d4eb34ece03f810f186bac6ada4d9994eb71 now local icon cache works diff -r 4949d4eb34ec -r 0ddcba9161fd pidgin-twitter.c --- a/pidgin-twitter.c Sun Jun 22 23:57:41 2008 +0900 +++ b/pidgin-twitter.c Mon Jun 23 02:10:17 2008 +0900 @@ -786,6 +786,8 @@ gchar *user_name = (gchar *)user_data; int icon_id; + twitter_debug("called\n"); + requestings = g_list_remove(requestings, url_data); /* Return if user's icon had already been downloaded or @@ -813,17 +815,21 @@ /* store retrieved image to a file in icon dir */ if(ensure_path_exists(dirname)) { gchar *filename = NULL; + gchar *path = NULL; FILE *fp = NULL; + filename = g_strdup_printf("%s.gif", user_name); - filename = g_build_filename(dirname, user_name, ".gif", NULL); - - fp = fopen(filename, "w"); + path = g_build_filename(dirname, filename, NULL); g_free(filename); filename = NULL; + fp = fopen(path, "w"); + g_free(path); path = NULL; + if(fp) { - int len; - len = fwrite(url_text, len, 1, fp); + int wrotelen; + wrotelen = fwrite(url_text, 1, len, fp); } + fclose(fp); fp = NULL; } @@ -873,59 +879,56 @@ /* request user's icon */ /***********************/ + /* look local icon cache for the requested icon */ + gchar *filename = NULL; + gchar *path = NULL; + gint icon_id; + + /* if img has been registerd, just return */ + icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name)); + if(icon_id) { + return; + } + + filename = g_strdup_printf("%s.gif", user_name); + + path = g_build_filename( + purple_prefs_get_string(OPT_ICON_DIR), filename, NULL); + + if(!g_file_test(path, G_FILE_TEST_EXISTS)) { + g_free(path); + filename = g_strdup_printf("%s.png", user_name); + path = g_build_filename( + purple_prefs_get_string(OPT_ICON_DIR), filename, NULL); + } + /* build image from file, if file exists */ + if(g_file_test(path, G_FILE_TEST_EXISTS)) { + gchar *data = NULL; + size_t len; + GError *err = NULL; + + if (!g_file_get_contents(path, &data, &len, &err)) { + twitter_debug("Error reading %s: %s\n", + path, err->message); + g_error_free(err); + } + + icon_id = purple_imgstore_add_with_id(data, len, path); + + g_hash_table_insert(icon_id_by_user, user_name, GINT_TO_POINTER(icon_id)); + //user_name must not be freed!! --yaz + g_free(filename); + g_free(path); + + return; + } + /* Return if user's icon has been requested already. */ if(g_list_find_custom(requested_users, user_name, (GCompareFunc)strcmp)) { g_free(user_name); return; } - - /* look local icon cache for the requested icon */ - gchar *filename = NULL; - gint icon_id; - - icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name)); - if(!icon_id) { - return; - } - - filename = g_build_filename( - purple_prefs_get_string(OPT_ICON_DIR), user_name, ".gif", NULL); - - if(!g_file_test(filename, G_FILE_TEST_EXISTS)) { - g_free(filename); - filename = g_build_filename( - purple_prefs_get_string(OPT_ICON_DIR), user_name, ".png", NULL); - } - /* build image from file, if file exists */ - if(g_file_test(filename, G_FILE_TEST_EXISTS)) { -// PurpleStoredImage *img; -// img = purple_imgstore_new_from_file(filename); - - gchar *data = NULL; - size_t len; - GError *err = NULL; - - if (!g_file_get_contents(filename, &data, &len, &err)) { - twitter_debug("Error reading %s: %s\n", - filename, err->message); - g_error_free(err); -// return NULL; - } - - icon_id = - purple_imgstore_add_with_id(data, - len, - filename); - - g_hash_table_insert(icon_id_by_user, user_name, GINT_TO_POINTER(icon_id)); - g_free(user_name); - g_free(filename); - - return; - } - - /* The string object are owned by the list. */ requested_users = g_list_append(requested_users, user_name); /* Create the URL of the user's icon. @@ -985,9 +988,9 @@ /* If the user's icon has not been downloaded, mark the message */ if(!icon_id) { requested_icon_marks = g_list_append(requested_icon_marks, - gtk_text_buffer_create_mark( - text_buffer, NULL, - &inserting_point, FALSE)); + gtk_text_buffer_create_mark( + text_buffer, NULL, + &inserting_point, FALSE)); twitter_debug("%s's icon has not been downloaded.", user_name); g_free(user_name); return; diff -r 4949d4eb34ec -r 0ddcba9161fd pidgin-twitter.h --- a/pidgin-twitter.h Sun Jun 22 23:57:41 2008 +0900 +++ b/pidgin-twitter.h Mon Jun 23 02:10:17 2008 +0900 @@ -5,6 +5,7 @@ #include #include #include +#include #include "gtkplugin.h" #include "util.h"