changeset 19421:e8bd9d92202e

Adding all the message from history in GtkIMHtml takes a bit long causing pidgin to freeze for a second or two. So instead of adding all the messages at one go, add some of them in an idle callback.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sat, 25 Aug 2007 11:28:34 +0000
parents c1c260d41365
children a277162b976e
files pidgin/gtkconv.c pidgin/gtkconv.h
diffstat 2 files changed, 48 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtkconv.c	Sat Aug 25 11:18:26 2007 +0000
+++ b/pidgin/gtkconv.c	Sat Aug 25 11:28:34 2007 +0000
@@ -5002,6 +5002,10 @@
 	g_list_foreach(gtkconv->send_history, (GFunc)g_free, NULL);
 	g_list_free(gtkconv->send_history);
 
+	if (gtkconv->attach.timer) {
+		g_source_remove(gtkconv->attach.timer);
+	}
+
 	if (tooltip.gtkconv == gtkconv)
 		reset_tooltip();
 
@@ -5218,6 +5222,15 @@
 	gtkconv = PIDGIN_CONVERSATION(conv);
 	g_return_if_fail(gtkconv != NULL);
 
+	if (gtkconv->attach.timer) {
+		/* We are currently in the process of filling up the buffer with the message
+		 * history of the conversation. So we do not need to add the message here.
+		 * Instead, this message will be added to the message-list, which in turn will
+		 * be processed and displayed by the attach-callback.
+		 */
+		return;
+	}
+
 	if (conv != gtkconv->active_conv)
 	{
 		if (flags & PURPLE_MESSAGE_ACTIVE_ONLY)
@@ -7141,22 +7154,45 @@
 	pidgin_conv_update_fields(conv, PIDGIN_CONV_TOPIC);
 }
 
+static gboolean
+add_message_history_to_gtkconv(gpointer data)
+{
+	PidginConversation *gtkconv = data;
+	int count = 0;
+	int timer = gtkconv->attach.timer;
+	gtkconv->attach.timer = 0;
+	while (gtkconv->attach.current && count < 100) {  /* XXX: 100 is a random value here */
+		PurpleConvMessage *msg = gtkconv->attach.current->data;
+		pidgin_conv_write_conv(gtkconv->active_conv, msg->who, msg->who, msg->what, msg->flags, msg->when);
+		gtkconv->attach.current = gtkconv->attach.current->prev;
+		count++;
+	}
+	gtkconv->attach.timer = timer;
+	if (gtkconv->attach.current)
+		return TRUE;
+
+	g_source_remove(gtkconv->attach.timer);
+	gtkconv->attach.timer = 0;
+	return FALSE;
+}
+
 gboolean pidgin_conv_attach_to_conversation(PurpleConversation *conv)
 {
 	GList *list;
+	PidginConversation *gtkconv;
 
 	if (PIDGIN_IS_PIDGIN_CONVERSATION(conv))
 		return FALSE;
 
 	purple_conversation_set_ui_ops(conv, pidgin_conversations_get_conv_ui_ops());
 	private_gtkconv_new(conv, FALSE);
+	gtkconv = PIDGIN_CONVERSATION(conv);
 
 	list = purple_conversation_get_message_history(conv);
-	list = g_list_last(list);
-	while (list) {
-		PurpleConvMessage *msg = list->data;
-		pidgin_conv_write_conv(conv, msg->who, msg->who, msg->what, msg->flags, msg->when);
-		list = list->prev;
+	if (list) {
+		list = g_list_last(list);
+		gtkconv->attach.current = list;
+		gtkconv->attach.timer = g_idle_add(add_message_history_to_gtkconv, gtkconv);
 	}
 
 	/* XXX: If this is a chat:
--- a/pidgin/gtkconv.h	Sat Aug 25 11:18:26 2007 +0000
+++ b/pidgin/gtkconv.h	Sat Aug 25 11:28:34 2007 +0000
@@ -162,6 +162,13 @@
 	GtkWidget *infopane;
 	GtkListStore *infopane_model;
 	GtkTreeIter infopane_iter;
+
+	/* Used when attaching a PidginConversation to a PurpleConversation
+	 * with message history */
+	struct {
+		int timer;
+		GList *current;
+	} attach;
 };
 
 /*@}*/