changeset 85:fb9831fae969

- fixed the bug that icons didn't appear correctly for wassr conversation. - new function get_service_type() has been added. it takes PurpleConversation and returns service type number. - numerous is_*_conv() have been replaced with get_service_type() and switch clause.
author Yoshiki Yazawa <yaz@honeyplnaet.jp>
date Sun, 06 Jul 2008 01:51:07 +0900
parents 0c1f63882b8a
children 89f52081f115 2b7ef3538cd3
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 220 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sat Jul 05 21:10:43 2008 +0900
+++ b/pidgin-twitter.c	Sun Jul 06 01:51:07 2008 +0900
@@ -29,7 +29,7 @@
 #define WASSR_POST_LEN (255 * 4)
 static gchar *wassr_post = NULL;
 
-typedef struct icon_data {
+typedef struct _icon_data {
     gint icon_id;        // image id
     gboolean requested;  // TRUE if download icon has been requested
     GList *request_list; // marker list
@@ -37,13 +37,14 @@
 } icon_data;
 
 enum {
-    TWITTER_SERVICE,
-    WASSR_SERVICE
+    unknown_service,
+    twitter_service,
+    wassr_service
 };
 
 typedef struct _eval_data {
-    int which;
-    int service;
+    gint which;
+    gint service;
 } eval_data;
 
 typedef struct _got_icon_data {
@@ -506,8 +507,8 @@
 eval(const GMatchInfo *match_info, GString *result, gpointer user_data)
 {
     eval_data *data = (eval_data *)user_data;
-    int which = data->which;
-    int service = data->service;
+    gint which = data->which;
+    gint service = data->service;
     gchar sub[128];
 
     twitter_debug("which = %d service = %d\n", which, service);
@@ -515,10 +516,17 @@
     if(which == RECIPIENT) {
         gchar *match = g_match_info_fetch(match_info, 1);
 
-        if(service == TWITTER_SERVICE)
+        switch(service) {
+        case twitter_service:
             snprintf(sub, 128, RECIPIENT_FORMAT, match, match);
-        else if(service == WASSR_SERVICE)
+            break;
+        case wassr_service:
             snprintf(sub, 128, RECIPIENT_FORMAT_WASSR, match, match);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
+        }
 
         g_free(match);
     }
@@ -526,14 +534,20 @@
         gchar *match1 = g_match_info_fetch(match_info, 1); //preceding CR|LF
         gchar *match2 = g_match_info_fetch(match_info, 2); //sender
 
-        if(service == TWITTER_SERVICE) {
+        switch(service) {
+        case twitter_service:
             snprintf(sub, 128, SENDER_FORMAT, match1 ? match1: "",
                      match2, match2);
-        }
-        else if(service == WASSR_SERVICE) {
+            break;
+        case wassr_service:
             snprintf(sub, 128, SENDER_FORMAT_WASSR, match1 ? match1: "",
                      match2, match2);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
         }
+
         g_free(match1);
         g_free(match2);
     }
@@ -545,7 +559,7 @@
 }
 
 static void
-translate(gchar **str, int which, int service)
+translate(gchar **str, gint which, gint service)
 {
     gchar *newstr;
 
@@ -572,7 +586,7 @@
 }
 
 static void
-playsound(gchar **str, int which)
+playsound(gchar **str, gint which)
 {
     GMatchInfo *match_info;
     const gchar *list;
@@ -621,8 +635,10 @@
 {
     twitter_debug("called\n");
 
+    gint service = get_service_type(conv);
+
     /* check if the conversation is between twitter */
-    if(!is_twitter_conv(conv) && !is_wassr_conv(conv))
+    if(service == unknown_service)
         return FALSE;
 
     /* strip all markups */
@@ -636,13 +652,6 @@
         playsound(buffer, RECIPIENT);
     }
 
-    /* XXX should cleanup */
-    int service = 0;
-    if(is_twitter_conv(conv))
-        service = TWITTER_SERVICE;
-    else if(is_wassr_conv(conv))
-        service = WASSR_SERVICE;
-
     /* translate */
     if(purple_prefs_get_bool(OPT_TRANSLATE_SENDER)) {
         translate(buffer, SENDER, service);
@@ -670,18 +679,25 @@
     gchar *markup = NULL;
     guint count;
 
+    gint service = get_service_type(conv);
+
     g_return_if_fail(gtkconv != NULL);
 
     count = gtk_text_buffer_get_char_count(textbuffer) +
         (unsigned int)g_utf8_strlen(new_text, -1);
 
-    if(is_twitter_conv(conv)) {
+    switch(service) {
+    case twitter_service:
         markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
                                          count <= 140 ? "black" : "red", count);
-    }
-    else if(is_wassr_conv(conv)) {
+        break;
+    case wassr_service:
         markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
                                          count <= 255 ? "black" : "red", count);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
     }
 
     box = gtkconv->toolbar;
@@ -700,6 +716,7 @@
     PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     GtkWidget *box, *counter = NULL;
     gchar *markup = NULL;
+    gint service = get_service_type(conv);
 
     g_return_if_fail(gtkconv != NULL);
 
@@ -707,13 +724,18 @@
         (gtk_text_iter_get_offset(end_pos) -
          gtk_text_iter_get_offset(start_pos));
 
-    if(is_twitter_conv(conv)) {
+    switch(service) {
+    case twitter_service:
         markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
                                          count <= 140 ? "black" : "red", count);
-    }
-    else if(is_wassr_conv(conv)) {
+        break;
+    case wassr_service:
         markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
                                          count <= 255 ? "black" : "red", count);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
     }
 
     box = gtkconv->toolbar;
@@ -734,10 +756,18 @@
         PidginWindow *win = list->data;
         PurpleConversation *conv =
             pidgin_conv_window_get_active_conversation(win);
-        if(is_twitter_conv(conv))
+        gint service = get_service_type(conv);
+        switch(service) {
+        case twitter_service:
             detach_from_conv(conv, NULL);
-        if(is_wassr_conv(conv))
+            break;
+        case wassr_service:
             detach_from_conv(conv, NULL);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
+        }
     }
 }
 
@@ -832,11 +862,17 @@
         PidginWindow *win = list->data;
         PurpleConversation *conv =
             pidgin_conv_window_get_active_conversation(win);
+        gint service = get_service_type(conv);
         /* only attach to twitter conversation window */
-        if(is_twitter_conv(conv))
+        switch(service) {
+        case twitter_service:
+        case wassr_service:
             attach_to_conv(conv, NULL);
-        if(is_wassr_conv(conv))
-            attach_to_conv(conv, NULL);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
+        }
     }
 }
 
@@ -948,17 +984,38 @@
     return is_wassr_account(account, name);
 }
 
+static gint
+get_service_type(PurpleConversation *conv)
+{
+    gint service = unknown_service;
+
+    g_return_val_if_fail(conv != NULL, unknown_service);
+
+    if(is_twitter_conv(conv))
+        service = twitter_service;
+    else if(is_wassr_conv(conv))
+        service = wassr_service;
+
+    return service;
+}
+
 static void
 conv_created_cb(PurpleConversation *conv, gpointer null)
 {
     PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     g_return_if_fail(gtkconv != NULL);
 
+    gint service = get_service_type(conv);
     /* only attach to twitter conversation window */
-    if(is_twitter_conv(conv))
+    switch(service) {
+    case twitter_service:
+    case wassr_service:
         attach_to_conv(conv, NULL);
-    if(is_wassr_conv(conv))
-        attach_to_conv(conv, NULL);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 }
 
 static void
@@ -967,12 +1024,19 @@
     PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     g_return_if_fail(gtkconv != NULL);
 
+    gint service = get_service_type(conv);
     /* only attach to twitter conversation window */
-    if(is_twitter_conv(conv))
+    switch(service) {
+    case twitter_service:
         delete_requested_icon_marks(gtkconv, icon_data_by_user);
-
-    if(is_wassr_conv(conv))
+        break;
+    case wassr_service:
         delete_requested_icon_marks(gtkconv, icon_data_by_user2);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 }
 
 static gboolean
@@ -1036,6 +1100,7 @@
     GtkTextBuffer *target_buffer = NULL;
     GtkTextIter inserting_point;
     int icon_id;
+    icon_data *data = NULL;
 
     twitter_debug("called: service = %d\n", service);
 
@@ -1050,7 +1115,9 @@
             PidginConversation *conv = conv_list->data;
             PurpleConversation *purple_conv = conv->active_conv;
 
-            if(purple_conv && is_twitter_conv(purple_conv)) {
+            gint service = get_service_type(purple_conv);
+
+            if(service != unknown_service) {
                 GtkIMHtml *current_imhtml = GTK_IMHTML(conv->imhtml);
                 GtkTextBuffer *current_buffer = gtk_text_view_get_buffer(
                      GTK_TEXT_VIEW(current_imhtml));
@@ -1073,11 +1140,16 @@
                                      &inserting_point, requested_mark);
 
     /* insert icon */
-    icon_data *data = NULL;
-    if(service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
-    else if(service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+    }
 
     if(data)
         icon_id = data->icon_id;
@@ -1100,10 +1172,17 @@
     icon_data *data = NULL;
     GList *mark_list = NULL;
 
-    if(service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
-    else if(service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 
     if(!data)
         return;
@@ -1137,10 +1216,16 @@
 
     twitter_debug("called: service = %d\n", service);
 
-    if (service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
-    else if (service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+    }
 
     if(data && data->fetch_data) {
         data->fetch_data = NULL;
@@ -1159,7 +1244,6 @@
 
         data->requested = FALSE;
 
-
         g_free(user_name);
         g_free(data);
         return;
@@ -1170,13 +1254,22 @@
     if(!data) {
         data = g_new0(icon_data, 1);
     }
+
     data->icon_id = icon_id;
-    if (service == TWITTER_SERVICE)
+
+    switch(service) {
+    case twitter_service:
         g_hash_table_insert(icon_data_by_user,
                             g_strdup(user_name), data);
-    if (service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         g_hash_table_insert(icon_data_by_user2,
                             g_strdup(user_name), data);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 
     const gchar *dirname = purple_prefs_get_string(OPT_ICON_DIR);
 
@@ -1186,10 +1279,17 @@
         gchar *path = NULL;
         FILE *fp = NULL;
 
-        if(service == TWITTER_SERVICE)
+        switch(service) {
+        case twitter_service:
             filename = g_strdup_printf("%s.gif", user_name);
-        else if(service == WASSR_SERVICE)
+            break;
+        case wassr_service:
             filename = g_strdup_printf("%s_wassr.png", user_name);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
+        }
 
         path = g_build_filename(dirname, filename, NULL);
         g_free(filename); filename = NULL;
@@ -1224,19 +1324,35 @@
     gchar *path = NULL;
     icon_data *data = NULL;
 
-    if(service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
-    else if(service == WASSR_SERVICE)
-        data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
+        break;
+    case wassr_service:
+        data = (icon_data *)g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        return;
+        break;
+    }
 
     if(!data) {
         data = g_new0(icon_data, 1);
-        if(service == TWITTER_SERVICE)
+        switch(service) {
+        case twitter_service:
             g_hash_table_insert(icon_data_by_user,
                                 g_strdup(user_name), data);
-        else if(service == WASSR_SERVICE)
+            break;
+        case wassr_service:
             g_hash_table_insert(icon_data_by_user2,
                                 g_strdup(user_name), data);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            return;
+            break;
+        }
     }
 
     /* if img has been registerd, just return */
@@ -1244,7 +1360,8 @@
         return;
 
     /* check if saved file exists */
-    if (service == TWITTER_SERVICE) {
+    switch(service) {
+    case twitter_service:
         filename = g_strdup_printf("%s.gif", user_name);
         path = g_build_filename(
             purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
@@ -1255,8 +1372,8 @@
             path = g_build_filename(
                 purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
         }
-    }
-    else if (service == WASSR_SERVICE) {
+        break;
+    case wassr_service:
         filename = g_strdup_printf("%s_wassr.gif", user_name);
         path = g_build_filename(
             purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
@@ -1267,6 +1384,10 @@
             path = g_build_filename(
                 purple_prefs_get_string(OPT_ICON_DIR), filename, NULL);
         }
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
     }
 
     /* build image from file, if file exists */
@@ -1301,12 +1422,19 @@
     /* Create the URL of the user's icon.
      * See http://twitter.g.hatena.ne.jp/ikko615/20080107/1199703400
      */
-    if (service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         url = g_strdup_printf("http://img.twitty.jp/twitter/user/%s/m.gif",
                               user_name);
-    else if (service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         url = g_strdup_printf("http://wassr.jp/user/%s/profile_img.png.64",
                               user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 
     got_icon_data *gotdata = g_new0(got_icon_data, 1);
     gotdata->user_name = g_strdup(user_name);
@@ -1327,19 +1455,33 @@
 
     twitter_debug("called\n");
 
-    if(service == TWITTER_SERVICE)
+    switch(service) {
+    case twitter_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user, user_name);
-    else if(service == WASSR_SERVICE)
+        break;
+    case wassr_service:
         data = (icon_data *)g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    default:
+        twitter_debug("unknown service\n");
+        break;
+    }
 
     if(!data) {
         data = g_new0(icon_data, 1);
-        if(service == TWITTER_SERVICE)
+        switch(service) {
+        case twitter_service:
             g_hash_table_insert(icon_data_by_user,
                                 g_strdup(user_name), data);
-        else if(service == WASSR_SERVICE)
+            break;
+        case wassr_service:
             g_hash_table_insert(icon_data_by_user2,
                                 g_strdup(user_name), data);
+            break;
+        default:
+            twitter_debug("unknown service\n");
+            break;
+        }
     }
 
     data->request_list = g_list_append(data->request_list, mark);
@@ -1355,8 +1497,12 @@
     GtkTextBuffer *text_buffer;
     GtkTextIter inserting_point;
     int icon_id;
+    int service = get_service_type(conv);
+    icon_data *data = NULL;
 
-    if(!is_twitter_conv(conv) && !is_wassr_conv(conv)) {
+    twitter_debug("called\n");
+
+    if(service == unknown_service) {
         twitter_debug("neither twitter or wassr conv\n");
         return;
     }
@@ -1372,7 +1518,6 @@
     user_name = g_match_info_fetch(match_info, 1);
     g_match_info_free(match_info);
 
-
     /* insert icon */
     imhtml = GTK_IMHTML(PIDGIN_CONVERSATION(conv)->imhtml);
     text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
@@ -1381,11 +1526,14 @@
     gtk_text_buffer_get_iter_at_line(text_buffer, &inserting_point,
         gtk_text_buffer_get_line_count(text_buffer) - 1);
 
-    icon_data *data = NULL;
-    if(is_twitter_conv(conv))
+    switch(service) {
+    case twitter_service:
         data = g_hash_table_lookup(icon_data_by_user, user_name);
-    else if(is_wassr_conv(conv))
+        break;
+    case wassr_service:
         data = g_hash_table_lookup(icon_data_by_user2, user_name);
+        break;
+    }
 
     if(data)
         icon_id = data->icon_id;
@@ -1396,12 +1544,6 @@
      * mark the message and request the icon. */
     if(!icon_id) {
         twitter_debug("%s's icon is not in memory.\n", user_name);
-        int service = 0;
-        if(is_twitter_conv(conv))
-            service = TWITTER_SERVICE;
-        else if(is_wassr_conv(conv))
-            service = WASSR_SERVICE;
-
         mark_icon_for_user(gtk_text_buffer_create_mark(
                                 text_buffer, NULL, &inserting_point, FALSE),
                            user_name, service);
--- a/pidgin-twitter.h	Sat Jul 05 21:10:43 2008 +0900
+++ b/pidgin-twitter.h	Sun Jul 06 01:51:07 2008 +0900
@@ -103,5 +103,6 @@
 static void init_plugin(PurplePlugin *plugin);
 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);
 
 #endif