changeset 65:4949d4eb34ec

- revised store icon feature - renamed a few functions
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Sun, 22 Jun 2008 23:57:41 +0900
parents da37857f3033
children 0ddcba9161fd
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 48 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sun Jun 22 16:02:44 2008 +0900
+++ b/pidgin-twitter.c	Sun Jun 22 23:57:41 2008 +0900
@@ -1,3 +1,4 @@
+
 /*
  * Pidgin-Twitter plugin.
  *
@@ -660,18 +661,17 @@
     twitter_debug("called\n");
     twitter_debug("buffer = %s suppress_oops = %d\n", *buffer, suppress_oops);
 
-    /* Check that conv is not null to avoid a clash.
-     * conv is null when the conversation window has not opened yet.
-     */
+    /* check if the conv is not NULL and is a twitter conv. */
     if(!(conv && is_twitter_conv(conv))) {
         return FALSE;
     }
 
-    /* Prevent notifications of incoming messages. */
+    /* suppress notification of incoming messages. */
     if(purple_prefs_get_bool(OPT_PREVENT_NOTIFICATION)) {
         *flags |= PURPLE_MESSAGE_SYSTEM;
     }
 
+    /* xxx request to attach icon to the buffer */
     request_icon(*buffer);
 
     if(!suppress_oops || !purple_prefs_get_bool(OPT_SUPPRESS_OOPS))
@@ -679,10 +679,8 @@
 
     if(strstr(*buffer, OOPS_MESSAGE)) {
         twitter_debug("clearing sender and buffer\n");
-        g_free(*sender);
-        *sender = NULL;
-        g_free(*buffer);
-        *buffer = NULL;
+        g_free(*sender); *sender = NULL;
+        g_free(*buffer); *buffer = NULL;
         suppress_oops = FALSE;
     }
     return FALSE;
@@ -782,8 +780,8 @@
 }
 
 static void
-downloaded_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-                   const gchar *url_text, gsize len, const gchar *error_message) 
+got_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
+                   const gchar *url_text, gsize len, const gchar *error_message)
 {
     gchar *user_name = (gchar *)user_data;
     int icon_id;
@@ -847,7 +845,7 @@
     gchar *url = NULL;
     PurpleUtilFetchUrlData *fetch_data = NULL;
 
-    /* get user's name */
+    /* get user name */
     g_regex_match(regp[MESSAGE], buffer, 0, &match_info);
     if(!g_match_info_matches(match_info)) {
         twitter_debug("Message was not matched : %s\n", buffer);
@@ -875,8 +873,22 @@
     /* request user's icon */
     /***********************/
 
-    /* first of all, look local icon cache for the requested icon */
+    /* 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);
 
@@ -885,10 +897,27 @@
         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)) {
-        purple_imgstore_new_from_file(filename);
-        icon_id = purple_imgstore_add_from_file_with_id();
+//        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);
@@ -897,12 +926,6 @@
     }
 
 
-    /* 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;
-    }
-
     /* 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.
@@ -912,7 +935,7 @@
                           user_name);
 
     fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
-                                       downloaded_icon_cb, user_name);
+                                       got_icon_cb, user_name);
     g_free(url);
     requestings = g_list_append(requestings, fetch_data);
 
@@ -1232,10 +1255,10 @@
     PURPLE_PRIORITY_DEFAULT,    /**< priority	*/
     PLUGIN_ID,                  /**< id		*/
     "Pidgin-Twitter",           /**< name	*/
-    "0.6.0",                    /**< version	*/
+    "0.7.0d1",                    /**< version	*/
     "replaces usernames with links and plays sounds", /**  summary	*/
     "replaces usernames with links and plays sounds", /**  desc	*/
-    "Yoshiki Yazawa and the pidging-twitter team",     /**< author	*/
+    "Yoshiki Yazawa, mikanbako and the pidging-twitter team",     /**< author	*/
     "http://www.honeyplanet.jp/",   /**< homepage	*/
     load_plugin,                /**< load	*/
     unload_plugin,              /**< unload	*/
--- a/pidgin-twitter.h	Sun Jun 22 16:02:44 2008 +0900
+++ b/pidgin-twitter.h	Sun Jun 22 23:57:41 2008 +0900
@@ -84,7 +84,7 @@
 static void deleting_conv_cb(PurpleConversation *conv);
 static gboolean receiving_im_cb(PurpleAccount *account, char **sender, char **buffer, PurpleConversation *conv, PurpleMessageFlags *flags, void *data);
 static void insert_requested_icon(gpointer data, gpointer user_data);
-static void downloaded_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
+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 *buffer);
 static void displayed_im_cb(PurpleAccount *account, const char *who, char *message, PurpleConversation *conv, PurpleMessageFlags flags);
 static gboolean load_plugin(PurplePlugin *plugin);