[evolution/wip/gsettings] em_format_html_print_message(): Require a CamelMimeMessage.



commit 0477a65401f042cfbae9a636ab76b89f25a6468a
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu May 12 18:18:59 2011 -0400

    em_format_html_print_message(): Require a CamelMimeMessage.
    
    Let EMailReader fetch the CamelMimeMessage itself, handle errors, and
    then pass it off to EMFormatHTMLPrint.  This also eliminates the need
    for em_format_html_print_raw_message().

 mail/e-mail-reader-utils.c  |   92 +++++++++++++++++++++++++++++++++++++------
 mail/em-composer-utils.c    |    2 +-
 mail/em-format-html-print.c |   47 ++++++----------------
 mail/em-format-html-print.h |    3 +-
 4 files changed, 93 insertions(+), 51 deletions(-)
---
diff --git a/mail/e-mail-reader-utils.c b/mail/e-mail-reader-utils.c
index 6ed50b1..1947584 100644
--- a/mail/e-mail-reader-utils.c
+++ b/mail/e-mail-reader-utils.c
@@ -50,6 +50,9 @@ struct _AsyncContext {
 	EActivity *activity;
 	CamelFolder *folder;
 	EMailReader *reader;
+	gchar *message_uid;
+
+	GtkPrintOperationAction print_action;
 	const gchar *filter_source;
 	gint filter_type;
 };
@@ -66,6 +69,8 @@ async_context_free (AsyncContext *context)
 	if (context->reader != NULL)
 		g_object_unref (context->reader);
 
+	g_free (context->message_uid);
+
 	g_slice_free (AsyncContext, context);
 }
 
@@ -327,12 +332,61 @@ e_mail_reader_open_selected (EMailReader *reader)
 	return ii;
 }
 
+/* Helper for e_mail_reader_print() */
+static void
+mail_reader_print_cb (CamelFolder *folder,
+                      GAsyncResult *result,
+                      AsyncContext *context)
+{
+	EAlertSink *alert_sink;
+	CamelMimeMessage *message;
+	EMFormatHTML *formatter;
+	EMFormatHTMLPrint *html_print;
+	GError *error = NULL;
+
+	alert_sink = e_activity_get_alert_sink (context->activity);
+
+	message = camel_folder_get_message_finish (folder, result, &error);
+
+	if (e_activity_handle_cancellation (context->activity, error)) {
+		g_warn_if_fail (message == NULL);
+		async_context_free (context);
+		g_error_free (error);
+		return;
+
+	} else if (error != NULL) {
+		g_warn_if_fail (message == NULL);
+		e_alert_submit (
+			alert_sink, "no-retrieve-message",
+			error->message, NULL);
+		async_context_free (context);
+		g_error_free (error);
+		return;
+	}
+
+	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));
+
+	formatter = e_mail_reader_get_formatter (context->reader);
+
+	html_print = em_format_html_print_new (
+		formatter, context->print_action);
+	em_format_merge_handler (
+		EM_FORMAT (html_print), EM_FORMAT (formatter));
+	em_format_html_print_message (
+		html_print, message, folder, context->message_uid);
+	g_object_unref (html_print);
+
+	g_object_unref (message);
+
+	e_activity_set_state (context->activity, E_ACTIVITY_COMPLETED);
+
+	async_context_free (context);
+}
+
 void
 e_mail_reader_print (EMailReader *reader,
                      GtkPrintOperationAction action)
 {
-	EMFormatHTML *formatter;
-	EMFormatHTMLPrint *html_print;
 	CamelFolder *folder;
 	GPtrArray *uids;
 
@@ -343,18 +397,30 @@ e_mail_reader_print (EMailReader *reader,
 
 	/* XXX Learn to handle len > 1. */
 	uids = e_mail_reader_get_selected_uids (reader);
-	if (uids->len != 1)
-		goto exit;
+	g_return_if_fail (uids != NULL);
 
-	formatter = e_mail_reader_get_formatter (reader);
+	if (uids->len == 1) {
+		EActivity *activity;
+		AsyncContext *context;
+		GCancellable *cancellable;
+		const gchar *message_uid;
 
-	html_print = em_format_html_print_new (formatter, action);
-	em_format_merge_handler (
-		EM_FORMAT (html_print), EM_FORMAT (formatter));
-	em_format_html_print_message (html_print, folder, uids->pdata[0]);
-	g_object_unref (html_print);
+		activity = e_mail_reader_new_activity (reader);
+		cancellable = e_activity_get_cancellable (activity);
+		message_uid = uids->pdata[0];
+
+		context = g_slice_new0 (AsyncContext);
+		context->activity = activity;
+		context->reader = g_object_ref (reader);
+		context->message_uid = g_strdup (message_uid);
+		context->print_action = action;
+
+		camel_folder_get_message (
+			folder, message_uid, G_PRIORITY_DEFAULT,
+			cancellable, (GAsyncReadyCallback)
+			mail_reader_print_cb, context);
+	}
 
-exit:
 	em_utils_uids_free (uids);
 }
 
diff --git a/mail/em-composer-utils.c b/mail/em-composer-utils.c
index a8208dc..e72e5db 100644
--- a/mail/em-composer-utils.c
+++ b/mail/em-composer-utils.c
@@ -864,7 +864,7 @@ em_utils_composer_print_cb (EMsgComposer *composer,
 	EMFormatHTMLPrint *efhp;
 
 	efhp = em_format_html_print_new (NULL, action);
-	em_format_html_print_raw_message (efhp, message);
+	em_format_html_print_message (efhp, message, NULL, NULL);
 	g_object_unref (efhp);
 }
 
diff --git a/mail/em-format-html-print.c b/mail/em-format-html-print.c
index 1be0e5e..e6da0c9 100644
--- a/mail/em-format-html-print.c
+++ b/mail/em-format-html-print.c
@@ -222,36 +222,14 @@ emfhp_complete (EMFormatHTMLPrint *efhp)
 	g_object_unref (operation);
 }
 
-static void
-emfhp_got_message (CamelFolder *folder,
-                   const gchar *uid,
-                   CamelMimeMessage *msg,
-                   gpointer data)
-{
-	EMFormatHTMLPrint *efhp = data;
-
-	if (msg == NULL) {
-		g_object_unref (efhp);
-		return;
-	}
-
-	em_format_html_load_images (EM_FORMAT_HTML (efhp));
-
-	g_signal_connect (
-		efhp, "complete", G_CALLBACK (emfhp_complete), efhp);
-
-	/* FIXME Not passing a GCancellable here. */
-	em_format_format_clone (
-		(EMFormat *) efhp, folder, uid, msg,
-		(EMFormat *) efhp->source, NULL);
-}
-
 void
 em_format_html_print_message (EMFormatHTMLPrint *efhp,
+                              CamelMimeMessage *message,
                               CamelFolder *folder,
-                              const gchar *uid)
+                              const gchar *message_uid)
 {
-	g_object_ref (efhp);
+	g_return_if_fail (EM_IS_FORMAT_HTML_PRINT (efhp));
+	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));
 
 	/* Wrap flags to display all entries by default.*/
 	EM_FORMAT_HTML (efhp)->header_wrap_flags |=
@@ -259,15 +237,14 @@ em_format_html_print_message (EMFormatHTMLPrint *efhp,
 		EM_FORMAT_HTML_HEADER_CC |
 		EM_FORMAT_HTML_HEADER_BCC;
 
-	mail_get_message (
-		folder, uid, emfhp_got_message, efhp, mail_msg_unordered_push);
-}
+	em_format_html_load_images (EM_FORMAT_HTML (efhp));
 
-void
-em_format_html_print_raw_message (EMFormatHTMLPrint *efhp,
-                                  CamelMimeMessage *msg)
-{
-	g_object_ref (efhp);
+	g_signal_connect (
+		efhp, "complete", G_CALLBACK (emfhp_complete), efhp);
 
-	emfhp_got_message (NULL, NULL, msg, efhp);
+	/* FIXME Not passing a GCancellable here. */
+	em_format_format_clone (
+		EM_FORMAT (efhp),
+		folder, message_uid, message,
+		EM_FORMAT (efhp->source), NULL);
 }
diff --git a/mail/em-format-html-print.h b/mail/em-format-html-print.h
index 7b05d7a..446f93f 100644
--- a/mail/em-format-html-print.h
+++ b/mail/em-format-html-print.h
@@ -64,10 +64,9 @@ EMFormatHTMLPrint *
 		em_format_html_print_new	(EMFormatHTML *source,
 						 GtkPrintOperationAction action);
 void		em_format_html_print_message	(EMFormatHTMLPrint *efhp,
+						 CamelMimeMessage *message,
 						 CamelFolder *folder,
 						 const gchar *uid);
-void		em_format_html_print_raw_message (EMFormatHTMLPrint *efhp,
-						 CamelMimeMessage *msg);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]