Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 35556)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,18 @@
+2008-05-29  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #525966
+
+	* em-composer-utils.c: (composer_set_no_change):
+	New helper function to set composers state properly to "no change".
+	* em-composer-utils.c: (em_utils_compose_new_message),
+	(em_utils_compose_new_message_with_mailto), (em_utils_post_to_folder),
+	(em_utils_post_to_url), (edit_message), (forward_attached),
+	(forward_non_attached), (em_utils_redirect_message),
+	(em_utils_reply_to_message), (post_reply_to_message):
+	Use the above helper function.
+	* em-composer-utils.c: (reply_get_composer), (composer_set_body):
+	Do not drop undo in the editor multiple times, do that only once.
+
 2008-05-26  Milan Crha  <mcrha@redhat.com>
 
 	** Fix for bug #317755
Index: mail/em-composer-utils.c
===================================================================
--- mail/em-composer-utils.c	(revision 35556)
+++ mail/em-composer-utils.c	(working copy)
@@ -44,6 +44,7 @@
 #include "em-utils.h"
 #include "em-composer-utils.h"
 #include "composer/e-msg-composer.h"
+#include "composer/e-composer-autosave.h"
 #include "em-format-html.h"
 #include "em-format-quote.h"
 #include "em-event.h"
@@ -462,6 +463,22 @@ struct _save_draft_info {
 };
 
 static void
+composer_set_no_change (EMsgComposer *composer, gboolean drop_undo)
+{
+	GtkhtmlEditor *editor;
+	
+	g_return_if_fail (composer != NULL);
+
+	editor = GTKHTML_EDITOR (composer);
+
+	if (drop_undo)
+		gtkhtml_editor_drop_undo (editor);
+
+	gtkhtml_editor_set_changed (editor, FALSE);
+	e_composer_autosave_set_saved (composer, TRUE);
+}
+
+static void
 save_draft_done (CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info, int ok,
 		 const char *appended_uid, void *user_data)
 {
@@ -471,6 +488,7 @@ save_draft_done (CamelFolder *folder, Ca
 	if (!ok)
 		goto done;
 
+	/* do not set autosave saved to TRUE because e_msg_composer_save_draft doesn't do that too */
 	gtkhtml_editor_set_changed (GTKHTML_EDITOR (sdi->composer), FALSE);
 
 	if ((emcs = sdi->emcs) == NULL) {
@@ -663,15 +681,12 @@ void
 em_utils_compose_new_message (const char *fromuri)
 {
 	GtkWidget *composer;
-	GtkhtmlEditor *editor;
 
 	composer = (GtkWidget *) create_new_composer ("", fromuri);
 	if (composer == NULL)
 		return;
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (E_MSG_COMPOSER (composer), TRUE);
 
 	gtk_widget_show (composer);
 }
@@ -688,7 +703,6 @@ void
 em_utils_compose_new_message_with_mailto (const char *url, const char *fromuri)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 	EComposerHeaderTable *table;
 	EAccount *account = NULL;
 
@@ -704,9 +718,7 @@ em_utils_compose_new_message_with_mailto
 	    && (account = mail_config_get_account_by_source_url(fromuri)))
 		e_composer_header_table_set_account_name (table, account->name);
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 
 	gtk_widget_show ((GtkWidget *) composer);
 	gdk_window_raise (((GtkWidget *) composer)->window);
@@ -724,7 +736,6 @@ void
 em_utils_post_to_folder (CamelFolder *folder)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 	EComposerHeaderTable *table;
 	EAccount *account;
 
@@ -751,9 +762,7 @@ em_utils_post_to_folder (CamelFolder *fo
 
 	em_composer_utils_setup_default_callbacks (composer);
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 
 	gtk_widget_show ((GtkWidget *) composer);
 	gdk_window_raise (((GtkWidget *) composer)->window);
@@ -771,7 +780,6 @@ void
 em_utils_post_to_url (const char *url)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 	EComposerHeaderTable *table;
 
 	composer = e_msg_composer_new_with_type (E_MSG_COMPOSER_POST);
@@ -787,9 +795,7 @@ em_utils_post_to_url (const char *url)
 
 	em_composer_utils_setup_default_callbacks (composer);
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 
 	gtk_widget_show ((GtkWidget *) composer);
 }
@@ -800,14 +806,11 @@ static void
 edit_message (CamelMimeMessage *message, CamelFolder *drafts, const char *uid)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 
 	composer = e_msg_composer_new_with_message (message);
 	em_composer_utils_setup_callbacks (composer, NULL, NULL, 0, 0, drafts, uid);
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 
 	gtk_widget_show (GTK_WIDGET (composer));
 }
@@ -868,7 +871,6 @@ static void
 forward_attached (CamelFolder *folder, GPtrArray *messages, CamelMimePart *part, char *subject, const char *fromuri)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 
 	composer = create_new_composer (subject, fromuri);
 	if (composer == NULL)
@@ -876,9 +878,7 @@ forward_attached (CamelFolder *folder, G
 
 	e_msg_composer_attach (composer, part);
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 
 	gtk_widget_show (GTK_WIDGET (composer));
 }
@@ -917,7 +917,6 @@ forward_non_attached (GPtrArray *message
 {
 	CamelMimeMessage *message;
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 	char *subject, *text;
 	int i;
 	guint32 flags;
@@ -946,9 +945,7 @@ forward_non_attached (GPtrArray *message
 
 				e_msg_composer_set_body_text (composer, text, len);
 
-				editor = GTKHTML_EDITOR (composer);
-				gtkhtml_editor_set_changed (editor, FALSE);
-				gtkhtml_editor_drop_undo (editor);
+				composer_set_no_change (composer, TRUE);
 
 				gtk_widget_show (GTK_WIDGET (composer));
 			}
@@ -1118,7 +1115,6 @@ void
 em_utils_redirect_message (CamelMimeMessage *message)
 {
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 
 	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));
 
@@ -1126,9 +1122,7 @@ em_utils_redirect_message (CamelMimeMess
 
 	gtk_widget_show (GTK_WIDGET (composer));
 
-	editor = GTKHTML_EDITOR (composer);
-	gtkhtml_editor_set_changed (editor, FALSE);
-	gtkhtml_editor_drop_undo (editor);
+	composer_set_no_change (composer, TRUE);
 }
 
 static void
@@ -1522,8 +1516,6 @@ reply_get_composer (CamelMimeMessage *me
 		e_msg_composer_add_header (composer, "References", references);
 	}
 
-	gtkhtml_editor_drop_undo (GTKHTML_EDITOR (composer));
-
 	return composer;
 }
 
@@ -1954,8 +1946,6 @@ composer_set_body (EMsgComposer *compose
 		g_free (text);
 		break;
 	}
-
-	gtkhtml_editor_drop_undo (GTKHTML_EDITOR (composer));
 }
 
 struct _reply_data {
@@ -2069,9 +2059,9 @@ em_utils_reply_to_message(CamelFolder *f
 
 	em_composer_utils_setup_callbacks (composer, folder, uid, flags, flags, NULL, NULL);
 
-	gtk_widget_show (GTK_WIDGET (composer));
+	composer_set_no_change (composer, TRUE);
 
-	gtkhtml_editor_set_changed (GTKHTML_EDITOR (composer), FALSE);
+	gtk_widget_show (GTK_WIDGET (composer));
 }
 
 /* Posting replies... */
@@ -2085,7 +2075,6 @@ post_reply_to_message (CamelFolder *fold
 	EDestination **tov = NULL;
 	CamelFolder *real_folder;
 	EMsgComposer *composer;
-	GtkhtmlEditor *editor;
 	EComposerHeaderTable *table;
 	char *subject, *url;
 	EAccount *account;
@@ -2115,7 +2104,6 @@ post_reply_to_message (CamelFolder *fold
 	get_reply_sender (message, to, NULL);
 
 	composer = e_msg_composer_new_with_type (E_MSG_COMPOSER_MAIL_POST);
-	editor = GTKHTML_EDITOR (composer);
 
 	/* construct the tov/ccv */
 	tov = em_utils_camel_address_to_destination (to);
@@ -2164,17 +2152,15 @@ post_reply_to_message (CamelFolder *fold
 		e_msg_composer_add_header (composer, "References", references);
 	}
 
-	gtkhtml_editor_drop_undo (editor);
-
 	e_msg_composer_add_message_attachments (composer, message, TRUE);
 
 	composer_set_body (composer, message, NULL);
 
 	em_composer_utils_setup_callbacks (composer, real_folder, real_uid, flags, flags, NULL, NULL);
 
-	gtk_widget_show (GTK_WIDGET (composer));
+	composer_set_no_change (composer, TRUE);
 
-	gtkhtml_editor_set_changed (editor, FALSE);
+	gtk_widget_show (GTK_WIDGET (composer));
 
 	camel_object_unref (real_folder);
 	camel_object_unref(to);
