changeset 152:588da540685a

work in progress icon update feature. the default thresholds have been set to 5 times or 1 day for testing.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Fri, 25 Jul 2008 07:05:04 +0900
parents 8ba0c0593621
children 07cedffb1c63
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 61 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Thu Jul 24 21:49:20 2008 +0900
+++ b/pidgin-twitter.c	Fri Jul 25 07:05:04 2008 +0900
@@ -1610,6 +1610,24 @@
     else
         icon_id = 0;
 
+    /* in this function, we put an icon for pending marks. we should
+     * not invalidate the icon here, otherwise it may result in
+     * thrashing. --yaz */
+#if 0
+    /* check validity of icon */
+    int count_thres = purple_prefs_get_int(OPT_ICON_MAX_COUNT);
+    int days_thres = DAYS_TO_SECONDS(purple_prefs_get_int(OPT_ICON_MAX_DAYS));
+
+    if(data->use_count > count_thres ||
+       (data->mtime && ((time(NULL) - data->mtime)) > days_thres)) {
+        twitter_debug("count=%d mtime=%ld\n", data->use_count, data->mtime);
+        icon_id = 0;
+        data->icon_id = 0;
+        data->use_count = 0;
+        data->mtime = time(NULL); //xxx not good
+    }
+#endif
+
     if(!icon_id) {
         return;
     }
@@ -1621,6 +1639,7 @@
         gtk_text_buffer_insert_pixbuf(target_buffer,
                                       &insertion_point,
                                       (GdkPixbuf *)pixbuf);
+        data->use_count++;
     }
     gtk_text_buffer_delete_mark(target_buffer, requested_mark);
     requested_mark = NULL;
@@ -1662,7 +1681,7 @@
     gotdata->user_name = g_strdup(user_name);
     gotdata->service = service;
 
-    twitter_debug("about to insert icon\n");
+    twitter_debug("about to insert icon for pending requests\n");
 
     if(mark_list) {
         g_list_foreach(mark_list, (GFunc) insert_icon_at_mark, gotdata);
@@ -1913,6 +1932,8 @@
         g_free(filename); filename = NULL;
 
         g_file_set_contents(path, url_text, len, NULL);
+
+        data->mtime = time(NULL);
     }
 
     twitter_debug("Downloading %s's icon has been complete.(icon_id = %d)\n",
@@ -1927,7 +1948,7 @@
 }
 
 static void
-request_icon(const char *user_name, gint service)
+request_icon(const char *user_name, gint service, gboolean renew)
 {
     gchar *url = NULL;
 
@@ -1961,14 +1982,14 @@
     /* since this function is called after mark_icon_for_user(), data
      * must exist here. */
     data = (icon_data *)g_hash_table_lookup(hash, user_name);
-    g_hash_table_insert(hash, g_strdup(user_name), data);
+//    g_hash_table_insert(hash, g_strdup(user_name), data); // XXX ???
 
     /* if img has been registerd, just return */
     if(data->icon_id > 0)
         return;
 
     /* check if saved file exists */
-    if(suffix) {
+    if(suffix && !renew) {
         gchar *filename = NULL;
         gchar **extp;
 
@@ -1979,12 +2000,13 @@
 
             twitter_debug("path = %s\n", path);
 
-            /* make image from file, if file exists */
+            /* build image from file, if file exists */
             if(g_file_test(path, G_FILE_TEST_EXISTS)) {
                 gchar *imgdata = NULL;
                 size_t len;
                 GError *err = NULL;
                 GdkPixbuf *pixbuf = NULL;
+                struct stat buf;
 
                 if (!g_file_get_contents(path, &imgdata, &len, &err)) {
                     twitter_debug("Error reading %s: %s\n",
@@ -1992,6 +2014,9 @@
                     g_error_free(err);
                 }
 
+                if(stat(path, &buf))
+                    data->mtime = buf.st_mtime;
+
                 pixbuf = make_scaled_pixbuf(imgdata, len);
 
                 if(pixbuf) {
@@ -2138,6 +2163,7 @@
     icon_data *data = NULL;
     gint linenumber;
     GHashTable *hash = NULL;
+    gboolean renew = FALSE;
 
     twitter_debug("called\n");
 
@@ -2185,9 +2211,24 @@
     if(hash)
         data = g_hash_table_lookup(hash, user_name);
 
-    if(data)
+    if(data) {
         icon_id = data->icon_id;
 
+        /* check validity of icon */
+        int count_thres = purple_prefs_get_int(OPT_ICON_MAX_COUNT);
+        int days_thres = DAYS_TO_SECONDS(purple_prefs_get_int(OPT_ICON_MAX_DAYS));
+
+        if(data->use_count > count_thres ||
+           (data->mtime && ((time(NULL) - data->mtime)) > days_thres)) {
+            twitter_debug("count=%d mtime=%ld\n", data->use_count, data->mtime);
+            icon_id = 0;
+            data->icon_id = 0;
+            data->use_count = 0;
+            data->mtime = time(NULL); //xxx not good
+            renew = TRUE;
+        }
+    }
+
     /* if we don't have the icon for this user, put a mark instead and
      * request the icon */
     if(!icon_id) {
@@ -2196,7 +2237,7 @@
                                text_buffer, NULL, &insertion_point, FALSE),
                            user_name, service);
         /* request to attach icon to the buffer */
-        request_icon(user_name, service);
+        request_icon(user_name, service, renew);
         g_free(user_name); user_name = NULL;
         return;
     }
@@ -2208,6 +2249,7 @@
         gtk_text_buffer_insert_pixbuf(text_buffer,
                                       &insertion_point,
                                       (GdkPixbuf *)pixbuf);
+        data->use_count++;
     }
     g_free(user_name); user_name = NULL;
 
@@ -2449,7 +2491,7 @@
     icon_data *data = (icon_data *)value;
 
     g_return_if_fail(data != NULL);
-    g_return_if_fail(data->icon_id >0);
+    g_return_if_fail(data->icon_id > 0);
 
     /* img->data is managed by gdkpixbuf, so we should not free here. */
 #if 0
@@ -2805,6 +2847,9 @@
     purple_prefs_add_string(OPT_SCREEN_NAME_IDENTICA, EMPTY);
     purple_prefs_add_int(OPT_API_BASE_GET_INTERVAL, TWITTER_DEFAULT_INTERVAL);
 
+    purple_prefs_add_int(OPT_ICON_MAX_COUNT, DEFAULT_ICON_MAX_COUNT);
+    purple_prefs_add_int(OPT_ICON_MAX_DAYS, DEFAULT_ICON_MAX_DAYS);
+
 }
 
 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)
--- a/pidgin-twitter.h	Thu Jul 24 21:49:20 2008 +0900
+++ b/pidgin-twitter.h	Fri Jul 25 07:05:04 2008 +0900
@@ -52,6 +52,8 @@
     GList *request_list;    /* marker list */
     PurpleUtilFetchUrlData *fetch_data; /* icon fetch data */
     const gchar *img_type;  /* image type */
+    gint use_count;         /* usage count */
+    time_t mtime;           /* mtime of file */
 } icon_data;
 
 /* used by got_icon_cb */
@@ -110,6 +112,8 @@
 #define OPT_PASSWORD_TWITTER    OPT_PIDGINTWITTER "/password_twitter"
 #define OPT_SHOW_ICON           OPT_PIDGINTWITTER "/show_icon"
 #define OPT_ICON_SIZE           OPT_PIDGINTWITTER "/icon_size"
+#define OPT_ICON_MAX_COUNT      OPT_PIDGINTWITTER "/icon_max_count"
+#define OPT_ICON_MAX_DAYS       OPT_PIDGINTWITTER "/icon_max_days"
 #define OPT_API_BASE_GET_INTERVAL OPT_PIDGINTWITTER "/api_base_get_interval"
 
 /* formats and templates */
@@ -157,6 +161,9 @@
 
 /* misc macros */
 #define DEFAULT_ICON_SIZE (48)
+#define DEFAULT_ICON_MAX_COUNT (5)
+#define DEFAULT_ICON_MAX_DAYS (1)
+#define DAYS_TO_SECONDS(d) ((d) * 86400)
 
 /* debug macros */
 #define twitter_debug(fmt, ...)	purple_debug(PURPLE_DEBUG_INFO, PLUGIN_NAME, "%s():%4d:  " fmt, __FUNCTION__, (int)__LINE__, ## __VA_ARGS__);
@@ -188,7 +195,7 @@
 static void insert_icon_at_mark(GtkTextMark *requested_mark, gpointer user_data);
 static void insert_requested_icon(const gchar *user_name, gint service);
 static void got_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
-static void request_icon(const char *user_name, gint service);
+static void request_icon(const char *user_name, gint service, gboolean renew);
 static void mark_icon_for_user(GtkTextMark *mark, const gchar *user_name, gint service);
 static gboolean displaying_im_cb(PurpleAccount *account, const char *who, char **message, PurpleConversation *conv, PurpleMessageFlags flags, void *data);
 static void displayed_im_cb(PurpleAccount *account, const char *who, char *message, PurpleConversation *conv, PurpleMessageFlags flags);