comparison pidgin-twitter.c @ 66:0ddcba9161fd

now local icon cache works
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Mon, 23 Jun 2008 02:10:17 +0900
parents 4949d4eb34ec
children 000575bce35d
comparison
equal deleted inserted replaced
65:4949d4eb34ec 66:0ddcba9161fd
784 const gchar *url_text, gsize len, const gchar *error_message) 784 const gchar *url_text, gsize len, const gchar *error_message)
785 { 785 {
786 gchar *user_name = (gchar *)user_data; 786 gchar *user_name = (gchar *)user_data;
787 int icon_id; 787 int icon_id;
788 788
789 twitter_debug("called\n");
790
789 requestings = g_list_remove(requestings, url_data); 791 requestings = g_list_remove(requestings, url_data);
790 792
791 /* Return if user's icon had already been downloaded or 793 /* Return if user's icon had already been downloaded or
792 * the download is failure. */ 794 * the download is failure. */
793 if(g_hash_table_lookup(icon_id_by_user, user_name) || !url_text) { 795 if(g_hash_table_lookup(icon_id_by_user, user_name) || !url_text) {
811 const gchar *dirname = purple_prefs_get_string(OPT_ICON_DIR); 813 const gchar *dirname = purple_prefs_get_string(OPT_ICON_DIR);
812 814
813 /* store retrieved image to a file in icon dir */ 815 /* store retrieved image to a file in icon dir */
814 if(ensure_path_exists(dirname)) { 816 if(ensure_path_exists(dirname)) {
815 gchar *filename = NULL; 817 gchar *filename = NULL;
818 gchar *path = NULL;
816 FILE *fp = NULL; 819 FILE *fp = NULL;
817 820 filename = g_strdup_printf("%s.gif", user_name);
818 filename = g_build_filename(dirname, user_name, ".gif", NULL); 821
819 822 path = g_build_filename(dirname, filename, NULL);
820 fp = fopen(filename, "w");
821 g_free(filename); filename = NULL; 823 g_free(filename); filename = NULL;
822 824
825 fp = fopen(path, "w");
826 g_free(path); path = NULL;
827
823 if(fp) { 828 if(fp) {
824 int len; 829 int wrotelen;
825 len = fwrite(url_text, len, 1, fp); 830 wrotelen = fwrite(url_text, 1, len, fp);
826 } 831 }
832
827 fclose(fp); fp = NULL; 833 fclose(fp); fp = NULL;
828 } 834 }
829 835
830 twitter_debug("Downloading %s's icon has been complete.(icon_id = %d)\n", 836 twitter_debug("Downloading %s's icon has been complete.(icon_id = %d)\n",
831 user_name, icon_id); 837 user_name, icon_id);
871 877
872 /***********************/ 878 /***********************/
873 /* request user's icon */ 879 /* request user's icon */
874 /***********************/ 880 /***********************/
875 881
882 /* look local icon cache for the requested icon */
883 gchar *filename = NULL;
884 gchar *path = NULL;
885 gint icon_id;
886
887 /* if img has been registerd, just return */
888 icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name));
889 if(icon_id) {
890 return;
891 }
892
893 filename = g_strdup_printf("%s.gif", user_name);
894
895 path = g_build_filename(
896 purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
897
898 if(!g_file_test(path, G_FILE_TEST_EXISTS)) {
899 g_free(path);
900 filename = g_strdup_printf("%s.png", user_name);
901 path = g_build_filename(
902 purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
903 }
904 /* build image from file, if file exists */
905 if(g_file_test(path, G_FILE_TEST_EXISTS)) {
906 gchar *data = NULL;
907 size_t len;
908 GError *err = NULL;
909
910 if (!g_file_get_contents(path, &data, &len, &err)) {
911 twitter_debug("Error reading %s: %s\n",
912 path, err->message);
913 g_error_free(err);
914 }
915
916 icon_id = purple_imgstore_add_with_id(data, len, path);
917
918 g_hash_table_insert(icon_id_by_user, user_name, GINT_TO_POINTER(icon_id));
919 //user_name must not be freed!! --yaz
920 g_free(filename);
921 g_free(path);
922
923 return;
924 }
925
876 /* Return if user's icon has been requested already. */ 926 /* Return if user's icon has been requested already. */
877 if(g_list_find_custom(requested_users, user_name, (GCompareFunc)strcmp)) { 927 if(g_list_find_custom(requested_users, user_name, (GCompareFunc)strcmp)) {
878 g_free(user_name); 928 g_free(user_name);
879 return; 929 return;
880 } 930 }
881
882
883 /* look local icon cache for the requested icon */
884 gchar *filename = NULL;
885 gint icon_id;
886
887 icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name));
888 if(!icon_id) {
889 return;
890 }
891
892 filename = g_build_filename(
893 purple_prefs_get_string(OPT_ICON_DIR), user_name, ".gif", NULL);
894
895 if(!g_file_test(filename, G_FILE_TEST_EXISTS)) {
896 g_free(filename);
897 filename = g_build_filename(
898 purple_prefs_get_string(OPT_ICON_DIR), user_name, ".png", NULL);
899 }
900 /* build image from file, if file exists */
901 if(g_file_test(filename, G_FILE_TEST_EXISTS)) {
902 // PurpleStoredImage *img;
903 // img = purple_imgstore_new_from_file(filename);
904
905 gchar *data = NULL;
906 size_t len;
907 GError *err = NULL;
908
909 if (!g_file_get_contents(filename, &data, &len, &err)) {
910 twitter_debug("Error reading %s: %s\n",
911 filename, err->message);
912 g_error_free(err);
913 // return NULL;
914 }
915
916 icon_id =
917 purple_imgstore_add_with_id(data,
918 len,
919 filename);
920
921 g_hash_table_insert(icon_id_by_user, user_name, GINT_TO_POINTER(icon_id));
922 g_free(user_name);
923 g_free(filename);
924
925 return;
926 }
927
928 931
929 /* The string object are owned by the list. */ 932 /* The string object are owned by the list. */
930 requested_users = g_list_append(requested_users, user_name); 933 requested_users = g_list_append(requested_users, user_name);
931 /* Create the URL of the user's icon. 934 /* Create the URL of the user's icon.
932 * See http://twitter.g.hatena.ne.jp/ikko615/20080107/1199703400 935 * See http://twitter.g.hatena.ne.jp/ikko615/20080107/1199703400
983 icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name)); 986 icon_id = GPOINTER_TO_INT(g_hash_table_lookup(icon_id_by_user, user_name));
984 987
985 /* If the user's icon has not been downloaded, mark the message */ 988 /* If the user's icon has not been downloaded, mark the message */
986 if(!icon_id) { 989 if(!icon_id) {
987 requested_icon_marks = g_list_append(requested_icon_marks, 990 requested_icon_marks = g_list_append(requested_icon_marks,
988 gtk_text_buffer_create_mark( 991 gtk_text_buffer_create_mark(
989 text_buffer, NULL, 992 text_buffer, NULL,
990 &inserting_point, FALSE)); 993 &inserting_point, FALSE));
991 twitter_debug("%s's icon has not been downloaded.", user_name); 994 twitter_debug("%s's icon has not been downloaded.", user_name);
992 g_free(user_name); 995 g_free(user_name);
993 return; 996 return;
994 } 997 }
995 998