changeset 107:eb77d409c235

- do neither save nor cache icon image if pixbuf can not be obtained. - now icon size can be specified in prefs.xml. - tweaked configuration dialog.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 15 Jul 2008 12:32:06 +0900
parents a132905cece6
children 01b6613a075a
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 131 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Mon Jul 14 12:42:11 2008 +0900
+++ b/pidgin-twitter.c	Tue Jul 15 12:32:06 2008 +0900
@@ -1381,6 +1381,36 @@
     }
 }
 
+static GdkPixbuf *
+make_scaled_pixbuf(const gchar *url_text, gsize len)
+{
+    /* make pixbuf */
+    GdkPixbufLoader *loader;
+    GdkPixbuf *src = NULL, *dest = NULL;
+    gint size;
+
+    g_return_val_if_fail(url_text != NULL, NULL);
+    g_return_val_if_fail(len > 0, NULL);
+
+    loader = gdk_pixbuf_loader_new();
+    gdk_pixbuf_loader_write(loader, (guchar *)url_text, len, NULL);
+    gdk_pixbuf_loader_close(loader, NULL);
+
+    src = gdk_pixbuf_loader_get_pixbuf(loader);
+    if(!src)
+        return NULL;
+
+    size = purple_prefs_get_int(OPT_ICON_SIZE);
+    if(size == 0)
+        size = 48; /* twitter icon size */
+
+    dest = gdk_pixbuf_scale_simple(src, size, size, GDK_INTERP_HYPER);
+    gdk_pixbuf_unref (src);
+
+    return dest;
+}
+
+
 static void
 got_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
             const gchar *url_text, gsize len, const gchar *error_message)
@@ -1392,6 +1422,8 @@
     gint icon_id;
     icon_data *data = NULL;
     GHashTable *hash = NULL;
+    GdkPixbuf *pixbuf = NULL;
+    const gchar *dirname = NULL;
 
     twitter_debug("called: service = %d\n", service);
 
@@ -1418,8 +1450,7 @@
                       user_name, error_message);
         if(data)
             data->requested = FALSE;
-        g_free(gotdata->user_name);
-        g_free(gotdata);
+        goto fin_got_icon_cb;
         return;
     }
 
@@ -1432,24 +1463,20 @@
         if(data->icon_id > 0) {
             twitter_debug("%s's icon has already been downloaded\n",
                           user_name);
-            g_free(gotdata->user_name);
-            g_free(gotdata);
+            goto fin_got_icon_cb;
             return;
         }
     }
 
-    /* make pixbuf */
-    GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
-    gdk_pixbuf_loader_write(loader, (guchar *)url_text, len, NULL);
-    gdk_pixbuf_loader_close(loader, NULL);
-    GdkPixbuf *src  = gdk_pixbuf_loader_get_pixbuf(loader);
-    GdkPixbuf *dest = gdk_pixbuf_scale_simple(src, 48, 48,
-                                              GDK_INTERP_HYPER);
-    gdk_pixbuf_unref (src);
+    pixbuf = make_scaled_pixbuf(url_text, len);
+
+    if(!pixbuf)
+        goto fin_got_icon_cb;
+
     icon_id =
-        purple_imgstore_add_with_id(dest,
-                                    gdk_pixbuf_get_rowstride(dest) *
-                                    gdk_pixbuf_get_height(dest),
+        purple_imgstore_add_with_id(pixbuf,
+                                    gdk_pixbuf_get_rowstride(pixbuf) *
+                                    gdk_pixbuf_get_height(pixbuf),
                                     user_name);
     if(!data) {
         twitter_debug("allocate icon_data (shouldn't be called)\n");
@@ -1461,13 +1488,12 @@
     if(hash)
         g_hash_table_insert(hash, g_strdup(user_name), data);
 
-    const gchar *dirname = purple_prefs_get_string(OPT_ICON_DIR);
+    dirname = purple_prefs_get_string(OPT_ICON_DIR);
 
     /* store retrieved image to a file in icon dir */
     if(ensure_path_exists(dirname)) {
         gchar *filename = NULL;
         gchar *path = NULL;
-//        FILE *fp = NULL;
 
         switch(service) {
         case twitter_service:
@@ -1495,6 +1521,8 @@
 
     /* Insert the icon to messages that had been received. */
     insert_requested_icon(user_name, service);
+
+fin_got_icon_cb:
     g_free(gotdata->user_name);
     g_free(gotdata);
 }
@@ -1505,7 +1533,6 @@
     gchar *url = NULL;
 
     /* look local icon cache for the requested icon */
-    gchar *filename = NULL;
     gchar *path = NULL;
     icon_data *data = NULL;
     GHashTable *hash = NULL;
@@ -1543,6 +1570,8 @@
 
     /* check if saved file exists */
     if(suffix) {
+        gchar *filename = NULL;
+
         filename = g_strdup_printf("%s_%s.gif", user_name, suffix);
         path = g_build_filename(
             purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
@@ -1553,6 +1582,8 @@
             path = g_build_filename(
                 purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
         }
+
+        g_free(filename);
     }
 
     twitter_debug("path = %s\n", path);
@@ -1562,6 +1593,7 @@
         gchar *imgdata = NULL;
         size_t len;
         GError *err = NULL;
+        GdkPixbuf *pixbuf = NULL;
 
         if (!g_file_get_contents(path, &imgdata, &len, &err)) {
             twitter_debug("Error reading %s: %s\n",
@@ -1569,26 +1601,20 @@
             g_error_free(err);
         }
 
-        /* make pixbuf */
-        GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
-        gdk_pixbuf_loader_write(loader, (guchar *)imgdata, len, NULL);
-        gdk_pixbuf_loader_close(loader, NULL);
-        GdkPixbuf *src  = gdk_pixbuf_loader_get_pixbuf(loader);
-        GdkPixbuf *dest = gdk_pixbuf_scale_simple(src, 48, 48,
-                                                  GDK_INTERP_HYPER);
-        gdk_pixbuf_unref (src);
-        data->icon_id =
-            purple_imgstore_add_with_id(dest,
-                                        gdk_pixbuf_get_rowstride(dest) *
-                                        gdk_pixbuf_get_height(dest),
-                                        user_name);
+        pixbuf = make_scaled_pixbuf(imgdata, len);
 
-        g_free(filename);
-        g_free(path);
+        if(pixbuf) {
+            data->icon_id =
+                purple_imgstore_add_with_id(pixbuf,
+                                            gdk_pixbuf_get_rowstride(pixbuf) *
+                                            gdk_pixbuf_get_height(pixbuf),
+                                            user_name);
 
-        twitter_debug("icon data has been loaded from file\n");
+            twitter_debug("icon data has been loaded from file\n");
+            insert_requested_icon(user_name, service);
+        }
 
-        insert_requested_icon(user_name, service);
+        g_free(path);
         return;
     }
 
@@ -1949,7 +1975,25 @@
     PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new();
 
     /************************/
-    /* translatione heading */
+    /* user config heading */
+    /************************/
+    pref = purple_plugin_pref_new_with_label("User Account");
+    purple_plugin_pref_frame_add(frame, pref);
+
+    /* screenname configurations */
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_SCREEN_NAME,
+                                                      "Screen Name");
+    purple_plugin_pref_frame_add(frame, pref);
+
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_PASSWORD,
+                                                      "Password");
+    purple_plugin_pref_set_masked(pref, TRUE);
+
+    purple_plugin_pref_frame_add(frame, pref);
+
+
+    /************************/
+    /* translation heading */
     /************************/
     pref = purple_plugin_pref_new_with_label("Translation Configurations");
     purple_plugin_pref_frame_add(frame, pref);
@@ -1968,10 +2012,39 @@
     purple_plugin_pref_frame_add(frame, pref);
 
 
-    /*************************/
-    /* miscellaneous heading */
-    /*************************/
-    pref = purple_plugin_pref_new_with_label("Miscellaneous Configurations");
+    /***********************/
+    /* GUI config heading */
+    /***********************/
+    pref = purple_plugin_pref_new_with_label("GUI Configurations");
+    purple_plugin_pref_frame_add(frame, pref);
+
+    /* show icon */
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_SHOW_ICON,
+                                                      "Show icons in conversation");
+    purple_plugin_pref_frame_add(frame, pref);
+
+#if 0
+    /* icon size */
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_ICON_SIZE,
+                                                      "Icon size in pixel");
+	purple_plugin_pref_set_bounds(pref, 16, 128);
+    purple_plugin_pref_frame_add(frame, pref);
+
+    /* XXX should invalidate pixbufs in memory --yaz */
+#endif
+
+    /* show text counter  */
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_COUNTER,
+                                                      "Show text counter widget");
+    purple_plugin_pref_frame_add(frame, pref);
+    purple_prefs_connect_callback(plugin, OPT_COUNTER,
+                                  counter_prefs_cb, NULL);
+
+
+    /****************************/
+    /* advanced config heading */
+    /****************************/
+    pref = purple_plugin_pref_new_with_label("Advanced Configurations");
     purple_plugin_pref_frame_add(frame, pref);
 
     /* escape pseudo command setting */
@@ -1979,27 +2052,21 @@
                                                       "Escape pseudo command string");
     purple_plugin_pref_frame_add(frame, pref);
 
-    /* show text counter  */
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_COUNTER,
-                                                      "Show text counter");
-    purple_plugin_pref_frame_add(frame, pref);
-
-    purple_prefs_connect_callback(plugin, OPT_COUNTER, counter_prefs_cb, NULL);
-
     /* suppress oops message */
     pref = purple_plugin_pref_new_with_name_and_label(OPT_SUPPRESS_OOPS,
                                                       "Suppress oops message");
     purple_plugin_pref_frame_add(frame, pref);
 
-    /* show icon */
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_SHOW_ICON,
-                                                      "Show icon");
+
+    /* notification setting */
+    pref =
+        purple_plugin_pref_new_with_name_and_label(OPT_PREVENT_NOTIFICATION,
+                                "Do not notify incoming message");
     purple_plugin_pref_frame_add(frame, pref);
 
-
-    /*****************/
+    /******************/
     /* sound heading */
-    /*****************/
+    /******************/
     pref = purple_plugin_pref_new_with_label("Sound Configurations");
     purple_plugin_pref_frame_add(frame, pref);
 
@@ -2060,39 +2127,19 @@
     purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8));
     purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9));
     purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10));
-
-    purple_plugin_pref_frame_add(frame, pref);
-
-    /************************/
-    /* notification heading */
-    /************************/
-    pref = purple_plugin_pref_new_with_label("Notification Configuration");
-    purple_plugin_pref_frame_add(frame, pref);
-
-    /* notification setting */
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_PREVENT_NOTIFICATION,
-                                "Prevent notifications of incoming messages");
     purple_plugin_pref_frame_add(frame, pref);
 
-
-    /* API based post */
-
-    pref = purple_plugin_pref_new_with_label("API Based Post");
-    purple_plugin_pref_frame_add(frame, pref);
-
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_API_BASE_POST, 
-                                             "Post Status with API");
+    /****************/
+    /* API heading */
+    /****************/
+    pref = purple_plugin_pref_new_with_label("API Based Post Configuration");
     purple_plugin_pref_frame_add(frame, pref);
 
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_SCREEN_NAME,
-                                                      "Screen Name");
+    /* post configuration */
+    pref = purple_plugin_pref_new_with_name_and_label(OPT_API_BASE_POST, 
+                                             "Post Status via API");
     purple_plugin_pref_frame_add(frame, pref);
 
-    pref = purple_plugin_pref_new_with_name_and_label(OPT_PASSWORD,
-                                                      "Password");
-    purple_plugin_pref_set_masked(pref, TRUE);
-
-    purple_plugin_pref_frame_add(frame, pref);
 
     return frame;
 }
@@ -2159,6 +2206,8 @@
     purple_prefs_add_bool(OPT_API_BASE_POST, FALSE);
     purple_prefs_add_string(OPT_SCREEN_NAME, EMPTY);
     purple_prefs_add_string(OPT_PASSWORD, EMPTY);
+
+    purple_prefs_add_int(OPT_ICON_SIZE, 48);
 }
 
 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)
--- a/pidgin-twitter.h	Mon Jul 14 12:42:11 2008 +0900
+++ b/pidgin-twitter.h	Tue Jul 15 12:32:06 2008 +0900
@@ -52,6 +52,7 @@
 #define OPT_SCREEN_NAME         OPT_PIDGINTWITTER "/screen_name"
 #define OPT_PASSWORD            OPT_PIDGINTWITTER "/password"
 #define OPT_SHOW_ICON           OPT_PIDGINTWITTER "/show_icon"
+#define OPT_ICON_SIZE           OPT_PIDGINTWITTER "/icon_size"
 
 /* formats and templates */
 #define RECIPIENT_FORMAT_TWITTER "@<a href='http://twitter.com/%s'>%s</a>"
@@ -118,5 +119,6 @@
 static void remove_marks_func(gpointer key, gpointer value, gpointer user_data);
 static void cancel_fetch_func(gpointer key, gpointer value, gpointer user_data);
 static gint get_service_type(PurpleConversation *conv);
+static GdkPixbuf *make_scaled_pixbuf(const gchar *url_text, gsize len);
 
 #endif