Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 35426)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,17 @@
+2008-04-28  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #511337
+
+	* em-format-html.h: (EMFormatHTMLState), (struct _EMFormatHTML):
+	* em-format-html.c: (efh_init), (efh_format_done),
+	(efh_format_timeout): Track state of the object to recognize
+	whether rendering new content or doing nothing.
+	* em-format-html-display.c: (efhd_can_process_attachment),
+	(efhd_attachment_show), (efhd_attachment_popup),
+	(efhd_attachment_image), (efhd_attachment_button):
+	Do nothing when EMFormatHTML object's state is RENDERING,
+	the puri object instance is not valid anyway.
+
 2008-04-28  Srinivasa Ragavan  <sragavan@novell.com>
 
 	* mail/em-utils.c: Avoid crash while detecting mime types.
Index: mail/em-format-html-display.c
===================================================================
--- mail/em-format-html-display.c	(revision 35426)
+++ mail/em-format-html-display.c	(working copy)
@@ -1429,6 +1429,21 @@ static void efhd_format_source(EMFormat 
 
 /* ********************************************************************** */
 
+/* Checks on the widget whether it can be processed, based on the state of EMFormatHTML.
+   The widget should have set "efh" data as the EMFormatHTML instance. */
+static gboolean
+efhd_can_process_attachment (GtkWidget *button)
+{
+	EMFormatHTML *efh;
+
+	if (!button)
+		return FALSE;
+
+	efh = g_object_get_data (G_OBJECT (button), "efh");
+
+	return efh && efh->state != EM_FORMAT_HTML_STATE_RENDERING;
+}
+
 /* if it hasn't been processed yet, format the attachment */
 static void
 efhd_attachment_show(EPopup *ep, EPopupItem *item, void *data)
@@ -1444,6 +1459,9 @@ efhd_attachment_show(EPopup *ep, EPopupI
 static void
 efhd_attachment_button_show(GtkWidget *w, void *data)
 {
+	if (!efhd_can_process_attachment (w))
+		return;
+
 	efhd_attachment_show(NULL, NULL, data);
 }
 
@@ -1504,6 +1522,9 @@ efhd_attachment_popup(GtkWidget *w, GdkE
 		return FALSE;
 	}
 
+	if (!efhd_can_process_attachment (w))
+		return FALSE;
+
 	/** @HookPoint-EMPopup: Attachment Button Context Menu
 	 * @Id: org.gnome.evolution.mail.formathtmldisplay.popup
 	 * @Class: org.gnome.evolution.mail.popup:1.0
@@ -1770,6 +1791,8 @@ efhd_attachment_image(EMFormatHTML *efh,
 	g_signal_connect(box, "popup_menu", G_CALLBACK(efhd_attachment_popup_menu), info);
 	g_signal_connect(box, "button-press-event", G_CALLBACK(efhd_image_fit_width), info);
 
+	g_object_set_data (G_OBJECT (box), "efh", efh);
+
 	return TRUE;
 }
 
@@ -1845,9 +1868,10 @@ efhd_attachment_button(EMFormatHTML *efh
 
 	button = gtk_button_new();
 
-	if (info->handle)
+	if (info->handle) {
 		g_signal_connect(button, "clicked", G_CALLBACK(efhd_attachment_button_show), info);
-	else {
+		g_object_set_data (G_OBJECT (button), "efh", efh);
+	} else {
 		gtk_widget_set_sensitive(button, FALSE);
 		GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
 	}
@@ -1921,6 +1945,8 @@ efhd_attachment_button(EMFormatHTML *efh
 	g_signal_connect(button, "clicked", G_CALLBACK(efhd_attachment_popup_menu), info);
 	gtk_box_pack_start((GtkBox *)mainbox, button, TRUE, TRUE, 0);
 
+	g_object_set_data (G_OBJECT (button), "efh", efh);
+
 	gtk_widget_show_all(mainbox);
 
 	if (info->shown)
Index: mail/em-format-html.c
===================================================================
--- mail/em-format-html.c	(revision 35426)
+++ mail/em-format-html.c	(working copy)
@@ -169,6 +169,7 @@ efh_init(GObject *o)
 	efh->text_html_flags = CAMEL_MIME_FILTER_TOHTML_CONVERT_NL | CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES
 		| CAMEL_MIME_FILTER_TOHTML_MARK_CITATION;
 	efh->show_icon = TRUE;
+	efh->state = EM_FORMAT_HTML_STATE_NONE;
 }
 
 static void
@@ -1329,6 +1330,7 @@ efh_format_done (struct _format_msg *m)
 
 	m->format->load_http_now = FALSE;
 	m->format->priv->format_id = -1;
+	m->format->state = EM_FORMAT_HTML_STATE_NONE;
 	g_signal_emit_by_name(m->format, "complete");
 }
 
@@ -1397,6 +1399,8 @@ efh_format_timeout(struct _format_msg *m
 		mail_msg_unref(m);
 		p->last_part = NULL;
 	} else {
+		efh->state = EM_FORMAT_HTML_STATE_RENDERING;
+
 		if (p->last_part != m->message) {
 			hstream = gtk_html_begin (efh->html);
 			gtk_html_stream_printf (hstream, "<h5>%s</h5>", _("Formatting Message..."));
Index: mail/em-format-html.h
===================================================================
--- mail/em-format-html.h	(revision 35426)
+++ mail/em-format-html.h	(working copy)
@@ -49,6 +49,11 @@ enum _em_format_html_header_flags {
 	EM_FORMAT_HTML_HEADER_BCC = 1<<2,
 };
 
+typedef enum {
+	EM_FORMAT_HTML_STATE_NONE = 0,
+	EM_FORMAT_HTML_STATE_RENDERING
+} EMFormatHTMLState;
+
 /* A HTMLJob will be executed in another thread, in sequence,
    It's job is to write to its stream, close it if successful,
    then exit */
@@ -194,6 +199,8 @@ struct _EMFormatHTML {
 	unsigned int hide_headers:1; /* no headers at all */
 	unsigned int show_icon:1; /* show an icon when the sender used Evo */
 	guint32 header_wrap_flags;
+
+	EMFormatHTMLState state; /* actual state of the object */
 };
 
 struct _EMFormatHTMLClass {
