changeset 94:31cddb2c2acc

fix icon position when the displaying message contains new line character. try 1.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 09 Jul 2008 16:13:02 +0900
parents 7219f7dee52f
children ab612180e7d0
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 55 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sun Jul 06 20:02:34 2008 +0900
+++ b/pidgin-twitter.c	Wed Jul 09 16:13:02 2008 +0900
@@ -25,6 +25,7 @@
 static gboolean suppress_oops = FALSE;
 static GHashTable *icon_data_by_user = NULL;
 static GHashTable *icon_data_by_user2 = NULL;
+static GHashTable *conv_hash = NULL;
 
 #define WASSR_POST_LEN (255 * 4)
 static gchar *wassr_post = NULL;
@@ -37,7 +38,7 @@
 } icon_data;
 
 enum {
-    unknown_service,
+    unknown_service = 0,
     twitter_service,
     wassr_service
 };
@@ -1104,7 +1105,7 @@
     GList *win_list;
     GtkIMHtml *target_imhtml = NULL;
     GtkTextBuffer *target_buffer = NULL;
-    GtkTextIter inserting_point;
+    GtkTextIter insertion_point;
     gint icon_id;
     icon_data *data = NULL;
 
@@ -1143,7 +1144,7 @@
     /* insert icon to the mark */
 
     gtk_text_buffer_get_iter_at_mark(target_buffer,
-                                     &inserting_point, requested_mark);
+                                     &insertion_point, requested_mark);
 
     /* insert icon */
     switch(service) {
@@ -1167,7 +1168,7 @@
     }
 
     /* insert icon actually */
-    gtk_imhtml_insert_image_at_iter(target_imhtml, icon_id, &inserting_point);
+    gtk_imhtml_insert_image_at_iter(target_imhtml, icon_id, &insertion_point);
     gtk_text_buffer_delete_mark(target_buffer, requested_mark);
     requested_mark = NULL;
 }
@@ -1498,6 +1499,35 @@
     data->request_list = g_list_append(data->request_list, mark);
 }
 
+static gboolean
+displaying_im_cb(PurpleAccount *account, const char *who, char **message,
+                 PurpleConversation *conv, PurpleMessageFlags flags, void *unused)
+{
+    GtkIMHtml *imhtml;
+    GtkTextBuffer *text_buffer;
+    gint service = get_service_type(conv);
+    gint linenumber = 0;
+
+    twitter_debug("called\n");
+
+    if(service == unknown_service) {
+        twitter_debug("neither twitter or wassr conv\n");
+        return FALSE;
+    }
+
+
+    /* get text buffer */
+    imhtml = GTK_IMHTML(PIDGIN_CONVERSATION(conv)->imhtml);
+    text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
+
+    /* store number of lines */
+    linenumber = gtk_text_buffer_get_line_count(text_buffer);
+    g_hash_table_insert(conv_hash, conv, GINT_TO_POINTER(linenumber));
+    twitter_debug("conv = %p linenumber = %d\n", conv, linenumber);
+
+    return FALSE;
+}
+
 static void
 displayed_im_cb(PurpleAccount *account, const char *who, char *message,
                 PurpleConversation *conv, PurpleMessageFlags flags)
@@ -1506,10 +1536,11 @@
     gchar *user_name = NULL;
     GtkIMHtml *imhtml;
     GtkTextBuffer *text_buffer;
-    GtkTextIter inserting_point;
-    gint icon_id;
+    GtkTextIter insertion_point;
+    gint icon_id = 0;
     gint service = get_service_type(conv);
     icon_data *data = NULL;
+    gint linenumber;
 
     twitter_debug("called\n");
 
@@ -1533,9 +1564,12 @@
     imhtml = GTK_IMHTML(PIDGIN_CONVERSATION(conv)->imhtml);
     text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(imhtml));
 
-    /* get GtkTextIter of the last line */
-    gtk_text_buffer_get_iter_at_line(text_buffer, &inserting_point,
-        gtk_text_buffer_get_line_count(text_buffer) - 1);
+    /* get GtkTextIter in the target line */
+    linenumber = GPOINTER_TO_INT(g_hash_table_lookup(conv_hash, conv));
+    gtk_text_buffer_get_iter_at_line(text_buffer,
+                                     &insertion_point,
+                                     linenumber);
+
 
     switch(service) {
     case twitter_service:
@@ -1548,15 +1582,13 @@
 
     if(data)
         icon_id = data->icon_id;
-    else
-        icon_id = 0;
 
-    /* If the user's icon has not been downloaded,
-     * mark the message and request the icon. */
+    /* if we don't have the icon for this user, put a mark instead and
+     * request the icon */
     if(!icon_id) {
         twitter_debug("%s's icon is not in memory.\n", user_name);
         mark_icon_for_user(gtk_text_buffer_create_mark(
-                                text_buffer, NULL, &inserting_point, FALSE),
+                                text_buffer, NULL, &insertion_point, FALSE),
                            user_name, service);
         /* request to attach icon to the buffer */
         request_icon(user_name, service);
@@ -1564,7 +1596,8 @@
         return;
     }
 
-    gtk_imhtml_insert_image_at_iter(imhtml, icon_id, &inserting_point);
+    /* if we have icon for this user, insert icon immediately */
+    gtk_imhtml_insert_image_at_iter(imhtml, icon_id, &insertion_point);
     g_free(user_name); user_name = NULL;
 
     twitter_debug("reach end of function\n");
@@ -1583,6 +1616,9 @@
                           plugin, PURPLE_CALLBACK(conv_created_cb), NULL);
     purple_signal_connect(purple_conversations_get_handle(), "receiving-im-msg",
                           plugin, PURPLE_CALLBACK(receiving_im_cb), NULL);
+    purple_signal_connect(pidgin_conversations_get_handle(), "displaying-im-msg",
+                          plugin, PURPLE_CALLBACK(displaying_im_cb), NULL);
+
     purple_signal_connect(pidgin_conversations_get_handle(), "displayed-im-msg",
                           plugin, PURPLE_CALLBACK(displayed_im_cb), NULL);
     purple_signal_connect(purple_conversations_get_handle(),
@@ -1602,6 +1638,8 @@
                                               g_free, NULL);
     icon_data_by_user2 = g_hash_table_new_full(g_str_hash, g_str_equal,
                                               g_free, NULL);
+    conv_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+                                              NULL, NULL);
 
     /* attach counter to the existing twitter window */
     if(purple_prefs_get_bool(OPT_COUNTER)) {
@@ -1679,6 +1717,7 @@
     /* destroy hash table for icon_data */
     g_hash_table_destroy(icon_data_by_user); //XXX all memory freed? --yaz
     g_hash_table_destroy(icon_data_by_user2); //XXX all memory freed? --yaz
+    g_hash_table_destroy(conv_hash);
 
     /* detach from twitter window */
     detach_from_window();
--- a/pidgin-twitter.h	Sun Jul 06 20:02:34 2008 +0900
+++ b/pidgin-twitter.h	Wed Jul 09 16:13:02 2008 +0900
@@ -95,6 +95,7 @@
 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 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);
 static gboolean load_plugin(PurplePlugin *plugin);
 static gboolean unload_plugin(PurplePlugin *plugin);