Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 37223)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,18 @@
+2009-02-04  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #314333
+
+	* em-format.h: (struct _EMFormat):
+	* em-format.c: (emf_init), (add_validity_found),
+	(emf_application_xpkcs7mime), (emf_multipart_encrypted),
+	(emf_multipart_signed), (emf_inlinepgp_signed),
+	(emf_inlinepgp_encrypted):
+	* em-utils.h: (em_utils_message_to_html):
+	* em-utils.c: (em_utils_message_to_html):
+	* em-composer-utils.c: (emu_update_composers_security),
+	(forward_non_attached), (composer_set_body):
+	Set composer's Security options based on parts of the source message.
+
 2009-02-03  Srinivasa Ragavan  <sragavan@novell.com>
 
 	** Fix for bug #569986
Index: mail/em-composer-utils.c
===================================================================
--- mail/em-composer-utils.c	(revision 37223)
+++ mail/em-composer-utils.c	(working copy)
@@ -45,6 +45,7 @@
 #include "em-composer-utils.h"
 #include "composer/e-msg-composer.h"
 #include "composer/e-composer-autosave.h"
+#include "composer/e-composer-actions.h"
 #include "em-format-html.h"
 #include "em-format-quote.h"
 #include "em-event.h"
@@ -1010,6 +1011,24 @@ em_utils_edit_messages (CamelFolder *fol
 	mail_get_messages (folder, uids, edit_messages, GINT_TO_POINTER (replace));
 }
 
+static void
+emu_update_composers_security (EMsgComposer *composer, guint32 validity_found)
+{
+	GtkToggleAction *action;
+
+	g_return_if_fail (composer != NULL);
+
+	if (validity_found & EM_FORMAT_VALIDITY_FOUND_SIGNED) {
+		action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_PGP_SIGN (composer));
+		gtk_toggle_action_set_active (action, TRUE);
+	}
+
+	if (validity_found & EM_FORMAT_VALIDITY_FOUND_ENCRYPTED) {
+		action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_PGP_ENCRYPT (composer));
+		gtk_toggle_action_set_active (action, TRUE);
+	}
+}
+
 /* Forwarding messages... */
 struct forward_attached_data
 {
@@ -1143,11 +1162,12 @@ forward_non_attached (CamelFolder *folde
 
 	for (i = 0; i < messages->len; i++) {
 		ssize_t len;
+		guint32 validity_found = 0;
 
 		message = messages->pdata[i];
 		subject = mail_tool_generate_forward_subject (message);
 
-		text = em_utils_message_to_html (message, _("-------- Forwarded Message --------"), flags, &len, NULL, NULL);
+		text = em_utils_message_to_html (message, _("-------- Forwarded Message --------"), flags, &len, NULL, NULL, &validity_found);
 
 		if (text) {
 			composer = create_new_composer (subject, fromuri, !uids || !uids->pdata [i]);
@@ -1161,6 +1181,7 @@ forward_non_attached (CamelFolder *folde
 				if (uids && uids->pdata[i])
 					em_composer_utils_setup_callbacks (composer, folder, uids->pdata[i], CAMEL_MESSAGE_FORWARDED, CAMEL_MESSAGE_FORWARDED, NULL, NULL);
 
+				emu_update_composers_security (composer, validity_found);
 				composer_set_no_change (composer, TRUE);
 
 				gtk_widget_show (GTK_WIDGET (composer));
@@ -2230,6 +2251,7 @@ composer_set_body (EMsgComposer *compose
 	GConfClient *gconf;
 	ssize_t len = 0;
 	gboolean start_bottom;
+	guint32 validity_found = 0;
 
 	gconf = mail_config_get_gconf_client ();
 	start_bottom = gconf_client_get_bool (gconf, "/apps/evolution/mail/composer/reply_start_bottom", NULL);
@@ -2245,19 +2267,21 @@ composer_set_body (EMsgComposer *compose
 		camel_object_unref (part);
 		break;
 	case MAIL_CONFIG_REPLY_OUTLOOK:
-		text = em_utils_message_to_html (message, _("-----Original Message-----"), EM_FORMAT_QUOTE_HEADERS, &len, source, start_bottom ? "<BR>" : NULL);
+		text = em_utils_message_to_html (message, _("-----Original Message-----"), EM_FORMAT_QUOTE_HEADERS, &len, source, start_bottom ? "<BR>" : NULL, &validity_found);
 		e_msg_composer_set_body_text(composer, text, len);
 		g_free (text);
+		emu_update_composers_security (composer, validity_found);
 		break;
 
 	case MAIL_CONFIG_REPLY_QUOTED:
 	default:
 		/* do what any sane user would want when replying... */
 		credits = attribution_format (ATTRIBUTION, message);
-		text = em_utils_message_to_html (message, credits, EM_FORMAT_QUOTE_CITE, &len, source, start_bottom ? "<BR>" : NULL);
+		text = em_utils_message_to_html (message, credits, EM_FORMAT_QUOTE_CITE, &len, source, start_bottom ? "<BR>" : NULL, &validity_found);
 		g_free (credits);
 		e_msg_composer_set_body_text(composer, text, len);
 		g_free (text);
+		emu_update_composers_security (composer, validity_found);
 		break;
 	}
 
Index: mail/em-format.c
===================================================================
--- mail/em-format.c	(revision 37223)
+++ mail/em-format.c	(working copy)
@@ -126,6 +126,7 @@ emf_init(GObject *o)
 	e_dlist_init(&emf->header_list);
 	em_format_default_headers(emf);
 	emf->part_id = g_string_new("");
+	emf->validity_found = 0;
 }
 
 static void
@@ -1171,6 +1172,21 @@ em_format_describe_part(CamelMimePart *p
 	return out;
 }
 
+static void
+add_validity_found (EMFormat *emf, CamelCipherValidity *valid)
+{
+	g_return_if_fail (emf != NULL);
+
+	if (!valid)
+		return;
+
+	if (valid->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE)
+		emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SIGNED;
+
+	if (valid->encrypt.status != CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE)
+		emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_ENCRYPTED;
+}
+
 /* ********************************************************************** */
 
 #ifdef ENABLE_SMIME
@@ -1195,6 +1211,8 @@ emf_application_xpkcs7mime(EMFormat *emf
 
 	context = camel_smime_context_new(session);
 
+	emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_ENCRYPTED;
+
 	opart = camel_mime_part_new();
 	valid = camel_cipher_decrypt(context, part, opart, ex);
 	if (valid == NULL) {
@@ -1207,6 +1225,7 @@ emf_application_xpkcs7mime(EMFormat *emf
 		emfc->valid = camel_cipher_validity_clone(valid);
 		camel_object_ref((emfc->secured = opart));
 
+		add_validity_found (emf, valid);
 		em_format_format_secure(emf, stream, opart, valid);
 	}
 
@@ -1348,6 +1367,8 @@ emf_multipart_encrypted(EMFormat *emf, C
 		return;
 	}
 
+	emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_ENCRYPTED;
+
 	ex = camel_exception_new();
 	context = camel_gpg_context_new(emf->session);
 	opart = camel_mime_part_new();
@@ -1364,6 +1385,7 @@ emf_multipart_encrypted(EMFormat *emf, C
 		emfc->valid = camel_cipher_validity_clone(valid);
 		camel_object_ref((emfc->secured = opart));
 
+		add_validity_found (emf, valid);
 		em_format_format_secure(emf, stream, opart, valid);
 	}
 
@@ -1509,6 +1531,8 @@ emf_multipart_signed(EMFormat *emf, Came
 				cipher = camel_gpg_context_new(emf->session);
 	}
 
+	emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SIGNED;
+
 	if (cipher == NULL) {
 		em_format_format_error(emf, stream, _("Unsupported signature format"));
 		em_format_part_as(emf, stream, part, "multipart/mixed");
@@ -1529,6 +1553,7 @@ emf_multipart_signed(EMFormat *emf, Came
 			emfc->valid = camel_cipher_validity_clone(valid);
 			camel_object_ref((emfc->secured = cpart));
 
+			add_validity_found (emf, valid);
 			em_format_format_secure(emf, stream, cpart, valid);
 		}
 
@@ -1584,6 +1609,8 @@ emf_inlinepgp_signed(EMFormat *emf, Came
 		return;
 	}
 
+	emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_SIGNED;
+
 	ex = camel_exception_new();
 	cipher = camel_gpg_context_new(emf->session);
 	/* Verify the signature of the message */
@@ -1636,6 +1663,7 @@ emf_inlinepgp_signed(EMFormat *emf, Came
 	camel_medium_set_content_object ((CamelMedium *) opart, dw);
 	camel_data_wrapper_set_mime_type_field ((CamelDataWrapper *) opart, dw->mime_type);
 
+	add_validity_found (emf, valid);
 	/* Pass it off to the real formatter */
 	em_format_format_secure(emf, stream, opart, valid);
 
@@ -1657,6 +1685,8 @@ emf_inlinepgp_encrypted(EMFormat *emf, C
 	CamelDataWrapper *dw;
 	char *mime_type;
 
+	emf->validity_found |= EM_FORMAT_VALIDITY_FOUND_ENCRYPTED;
+
 	cipher = camel_gpg_context_new(emf->session);
 	ex = camel_exception_new();
 	opart = camel_mime_part_new();
@@ -1687,6 +1717,7 @@ emf_inlinepgp_encrypted(EMFormat *emf, C
 
 	g_free (mime_type);
 
+	add_validity_found (emf, valid);
 	/* Pass it off to the real formatter */
 	em_format_format_secure(emf, stream, opart, valid);
 
Index: mail/em-format.h
===================================================================
--- mail/em-format.h	(revision 37223)
+++ mail/em-format.h	(working copy)
@@ -162,6 +162,9 @@ struct _EMFormatHeader {
 #define EM_FORMAT_HEADER_BOLD (1<<0)
 #define EM_FORMAT_HEADER_LAST (1<<4) /* reserve 4 slots */
 
+#define EM_FORMAT_VALIDITY_FOUND_SIGNED    (1<<0)
+#define EM_FORMAT_VALIDITY_FOUND_ENCRYPTED (1<<1)
+
 /**
  * struct _EMFormat - Mail formatter object.
  *
@@ -212,6 +215,8 @@ struct _EMFormat {
 	/* for validity enveloping */
 	struct _CamelCipherValidity *valid;
 	struct _CamelCipherValidity *valid_parent;
+	/* for checking whether found any signed/encrypted parts */
+	guint32 validity_found;
 
 	/* for forcing inlining */
 	GHashTable *inline_table;
Index: mail/em-utils.c
===================================================================
--- mail/em-utils.c	(revision 37223)
+++ mail/em-utils.c	(working copy)
@@ -1693,6 +1693,9 @@ em_utils_part_to_html(CamelMimePart *par
  * @len:
  * @source:
  * @append: Text to append, can be NULL.
+ * @validity_found: if not NULL, then here will be set what validities
+ *         had been found during message conversion. Value is a bit OR
+ *         of EM_FORMAT_VALIDITY_FOUND_* constants.
  *
  * Convert a message to html, quoting if the @credits attribution
  * string is given.
@@ -1700,7 +1703,7 @@ em_utils_part_to_html(CamelMimePart *par
  * Return value: The html version.
  **/
 char *
-em_utils_message_to_html(CamelMimeMessage *message, const char *credits, guint32 flags, ssize_t *len, EMFormat *source, const char *append)
+em_utils_message_to_html(CamelMimeMessage *message, const char *credits, guint32 flags, ssize_t *len, EMFormat *source, const char *append, guint32 *validity_found)
 {
 	EMFormatQuote *emfq;
 	CamelStreamMem *mem;
@@ -1728,6 +1731,8 @@ em_utils_message_to_html(CamelMimeMessag
 	}
 
 	em_format_format_clone((EMFormat *)emfq, NULL, NULL, message, source);
+	if (validity_found)
+		*validity_found = ((EMFormat *)emfq)->validity_found;
 	g_object_unref (emfq);
 
 	if (append && *append)
Index: mail/em-utils.h
===================================================================
--- mail/em-utils.h	(revision 37223)
+++ mail/em-utils.h	(working copy)
@@ -92,7 +92,7 @@ char *em_utils_get_proxy_uri(void);
 
 /* FIXME: should this have an override charset? */
 char *em_utils_part_to_html(struct _CamelMimePart *part, ssize_t *len, struct _EMFormat *source);
-char *em_utils_message_to_html(struct _CamelMimeMessage *msg, const char *credits, guint32 flags, ssize_t *len, struct _EMFormat *source, const char *append);
+char *em_utils_message_to_html(struct _CamelMimeMessage *msg, const char *credits, guint32 flags, ssize_t *len, struct _EMFormat *source, const char *append, guint32 *validity_found);
 
 void em_utils_expunge_folder (struct _GtkWidget *parent, struct _CamelFolder *folder);
 void em_utils_empty_trash (struct _GtkWidget *parent);
