[evolution/webkit: 44/196] Initial port of printing



commit f7f608055eb136cc59074289e5c3b96d92154207
Author: Dan VrÃtil <dvratil redhat com>
Date:   Thu Oct 27 16:16:29 2011 +0200

    Initial port of printing
    
    Essentially we clone the original EMFormat to EMFormatHTMLPrint, add
    a special PURI part that, when write_func()'ed will create HTML code
    with <iframe> for each real part.
    
    Because WebKit is somewhat buggy, this approach does not seem to be
    the best since WebKit sometimes refuses to print content of <iframes>
    and when frame flattening is enabled it does not print at all.
    
    I think that the best way would be to create a single HTML file for the
    entire email (like we did in Evo GtkHTML) since there are no GtkWidgets
    to be printed.

 data/webview.css              |   18 ++
 em-format/em-format.h         |    7 +-
 mail/e-mail-reader-utils.c    |  144 ++++++++-------
 mail/e-mail-reader.c          |    8 +-
 mail/em-format-html-display.c |   16 +-
 mail/em-format-html-print.c   |  403 ++++++++++++++++++++++++++++++++++-------
 mail/em-format-html-print.h   |   12 +-
 mail/em-format-html.c         |   38 +++--
 8 files changed, 478 insertions(+), 168 deletions(-)
---
diff --git a/data/webview.css b/data/webview.css
index 5f239f9..0bb9b93 100644
--- a/data/webview.css
+++ b/data/webview.css
@@ -31,3 +31,21 @@ span.navigable, div.navigable, p.navigable {
 img.navigable {
   cursor: hand;
 }
+
+.attachments {
+  backgound: #FFF;
+  border: 1px solid silver;
+  margin: 10px 10px 10px 10px;
+  border-left: 0;
+  border-right: 0;
+  border-bottom: 0;
+}
+
+.attachment {
+  max-width: 100px;
+  word-wrap: break-word;
+  float: left;
+  text-align: center;
+  margin-left: 5px;
+  margin-right: 5px;
+}
diff --git a/em-format/em-format.h b/em-format/em-format.h
index 713a9b9..6d05ebc 100644
--- a/em-format/em-format.h
+++ b/em-format/em-format.h
@@ -87,9 +87,10 @@ typedef enum {
 } EMFormatHandlerFlags;
 
 typedef enum {
-	EM_FORMAT_WRITE_MODE_NORMAL= 0,
-	EM_FORMAT_WRITE_MODE_ALL_HEADERS,
-	EM_FORMAT_WRITE_MODE_SOURCE
+	EM_FORMAT_WRITE_MODE_NORMAL= 1 << 0,
+	EM_FORMAT_WRITE_MODE_ALL_HEADERS = 1 << 1,
+	EM_FORMAT_WRITE_MODE_SOURCE = 1 << 2,
+	EM_FORMAT_WRITE_MODE_PRINTING = 1 << 3
 } EMFormatWriteMode;
 
 struct _EMFormatHandler {
diff --git a/mail/e-mail-reader-utils.c b/mail/e-mail-reader-utils.c
index 345e034..88e29b9 100644
--- a/mail/e-mail-reader-utils.c
+++ b/mail/e-mail-reader-utils.c
@@ -431,59 +431,50 @@ 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)
+mail_reader_print_finished (GtkPrintOperation *operation,
+			    GtkPrintOperationResult result,
+			    EActivity *activity)
 {
-	EAlertSink *alert_sink;
-	CamelMimeMessage *message;
-	EMailDisplay *display;
-	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;
+	WebKitWebView *webview;
 
-	} else if (error != NULL) {
-		g_warn_if_fail (message == NULL);
-		e_alert_submit (
-			alert_sink, "mail:no-retrieve-message",
-			error->message, NULL);
-		async_context_free (context);
-		g_error_free (error);
-		return;
-	}
+	webview = g_object_get_data (G_OBJECT (activity), "webview");
 
-	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));
+	/* Destroy the webview */
+	g_object_unref (webview);
 
-	display = e_mail_reader_get_mail_display (context->reader);
-	formatter = e_mail_display_get_formatter (display);
+	e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
+}
 
-	html_print = em_format_html_print_new (
-		formatter, context->print_action);
-	/* FIXME WEBKIT
-	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);
+static void
+webview_document_load_finished_cb (WebKitWebView *webview,
+				   GParamSpec *pspec,
+				   EActivity *activity)
+{
+	GtkPrintOperation *operation;
+	GtkPrintOperationAction action;
+	EMFormatHTMLPrint *efhp;
+	WebKitWebFrame *frame;
 
-	e_activity_set_state (context->activity, E_ACTIVITY_COMPLETED);
+	WebKitLoadStatus status = webkit_web_view_get_load_status (webview);
 
-	async_context_free (context);
+	if (status != WEBKIT_LOAD_FINISHED)
+		return;
+	
+	frame = webkit_web_view_get_main_frame (webview);
+	efhp = g_object_get_data (G_OBJECT (activity), "efhp");
+	action = em_format_html_print_get_action (efhp);
+	operation = gtk_print_operation_new ();
+	g_object_set_data (G_OBJECT (operation), "webview", webview);
+	g_signal_connect (operation, "done",
+		G_CALLBACK (mail_reader_print_finished), activity);
+
+	action = em_format_html_print_get_action (efhp);
+	webkit_web_frame_print_full (frame, 
+		operation, action, NULL);
+
+	g_object_unref (efhp);
 }
 
 void
@@ -491,37 +482,48 @@ e_mail_reader_print (EMailReader *reader,
                      GtkPrintOperationAction action)
 {
 	EActivity *activity;
-	AsyncContext *context;
-	GCancellable *cancellable;
-	CamelFolder *folder;
-	GPtrArray *uids;
-	const gchar *message_uid;
+	EMailDisplay *display;
+	EMFormatHTML *formatter;
+	EMFormatHTMLPrint *efhp;
+	gchar *mail_uri, *tmp;
+	SoupSession *session;
+	GHashTable *formatters;
+	GtkWidget *webview;
+	WebKitWebSettings *settings;
+	GtkPrintOperation *operation;
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
-	folder = e_mail_reader_get_folder (reader);
-	g_return_if_fail (CAMEL_IS_FOLDER (folder));
-
-	/* XXX Learn to handle len > 1. */
-	uids = e_mail_reader_get_selected_uids (reader);
-	g_return_if_fail (uids != NULL && uids->len == 1);
-	message_uid = g_ptr_array_index (uids, 0);
-
+	display = e_mail_reader_get_mail_display (reader);
+	formatter = e_mail_display_get_formatter (display);
+	
+	mail_uri = em_format_build_mail_uri(EM_FORMAT (formatter)->folder, 
+			EM_FORMAT (formatter)->message_uid, NULL, NULL);
+	
+	/* Clone EMFormatHTMLDisplay */
+	efhp = em_format_html_print_new (formatter, action);
+
+	/* It's safe to assume that session exists and contains formatters table,
+	 * because at least the message we are about to print now must be already
+	 * there */
+	session = webkit_get_default_session ();
+	formatters = g_object_get_data (G_OBJECT (session), "formatters");
+	g_hash_table_insert (formatters, g_strdup (mail_uri), efhp);
+	
 	activity = e_mail_reader_new_activity (reader);
-	cancellable = e_activity_get_cancellable (activity);
-
-	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);
-
-	em_utils_uids_free (uids);
+	g_object_set_data (G_OBJECT (activity), "efhp", efhp);
+	
+	/* Print_layout is a special PURI created by EMFormatHTMLPrint */
+	tmp = g_strconcat (mail_uri, "?part_id=print_layout", NULL);
+	webview = webkit_web_view_new ();
+	settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (webview));
+	g_object_set (G_OBJECT (settings), "enable-frame-flattening", TRUE, NULL);
+	webkit_web_view_set_settings (WEBKIT_WEB_VIEW (webview), settings);
+
+	g_signal_connect (webview, "notify::load-status",
+		G_CALLBACK (webview_document_load_finished_cb), activity);
+	webkit_web_view_load_uri (WEBKIT_WEB_VIEW (webview), tmp);
+	g_free (tmp);
 }
 
 static void
diff --git a/mail/e-mail-reader.c b/mail/e-mail-reader.c
index 0f75ad6..a65c08e 100644
--- a/mail/e-mail-reader.c
+++ b/mail/e-mail-reader.c
@@ -70,6 +70,8 @@
 	((EMailReaderPrivate *) g_object_get_qdata \
 	(G_OBJECT (obj), quark_private))
 
+#define d(x) x
+
 typedef struct _EMailReaderClosure EMailReaderClosure;
 typedef struct _EMailReaderPrivate EMailReaderPrivate;
 
@@ -1603,7 +1605,6 @@ action_mail_show_source_cb (GtkAction *action,
 	CamelFolder *folder;
 	GtkWidget *browser;
 	GPtrArray *uids;
-	EMFormatWriteMode mode;
 	const gchar *message_uid;
 
 	backend = e_mail_reader_get_backend (reader);
@@ -2531,9 +2532,8 @@ schedule_timeout_mark_seen (EMailReader *reader)
 	gboolean schedule_timeout;
 	gint timeout_interval;
 	const gchar *message_uid;
-
-	backend = e_mail_reader_get_backend (reader);
-	message_list = MESSAGE_LIST (e_mail_reader_get_message_list (reader));
+        backend = e_mail_reader_get_backend (reader);
+        message_list = MESSAGE_LIST (e_mail_reader_get_message_list (reader));	
 	shell_backend = E_SHELL_BACKEND (backend);
 	shell = e_shell_backend_get_shell (shell_backend);
 	shell_settings = e_shell_get_shell_settings (shell);
diff --git a/mail/em-format-html-display.c b/mail/em-format-html-display.c
index 553c241..114cd32 100644
--- a/mail/em-format-html-display.c
+++ b/mail/em-format-html-display.c
@@ -568,8 +568,8 @@ efhd_format_optional (EMFormat *emf,
 	info->attachment = e_attachment_new ();
 	e_attachment_set_mime_part (info->attachment, info->puri.part);
 	info->description = g_strdup(_("Evolution cannot render this email as it is too "
-			  	  	  	  	  	   "large to process. You can view it unformatted or "
-			  	  	  	  	  	   "with an external text editor."));
+				       "large to process. You can view it unformatted or "
+				       "with an external text editor."));
 
 	/* MStream holds content of the 'attachment' to be displayed */
 	info->mstream = (CamelStreamMem *) g_object_ref (mstream);
@@ -1045,7 +1045,6 @@ efhd_attachment_bar (EMFormat *emf,
 	GtkWidget *widget;
 
 	widget = e_mail_attachment_bar_new (abp->store);
-	printf("Create bar for PURI %p", puri);
 
 	return widget;
 }
@@ -1257,6 +1256,13 @@ efhd_free_attach_puri_data (EMFormatPURI *puri)
 		info->attachment = NULL;
 	}
 
-	g_free (info->attachment_view_part_id);
-	info->attachment_view_part_id = NULL;
+	if (info->description) {
+		g_free (info->description);
+		info->description = NULL;
+	}
+
+	if (info->attachment_view_part_id) {
+		g_free (info->attachment_view_part_id);
+		info->attachment_view_part_id = NULL;
+	}
 }
diff --git a/mail/em-format-html-print.c b/mail/em-format-html-print.c
index 4323822..a630e9b 100644
--- a/mail/em-format-html-print.c
+++ b/mail/em-format-html-print.c
@@ -28,27 +28,209 @@
 #include <string.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
-#include <gtkhtml/gtkhtml.h>
 
+#include "em-format-html-print.h"
+#include "em-format-html-display.h"
+#include "e-mail-attachment-bar.h"
 #include <e-util/e-print.h>
-
-#include <libemail-utils/mail-mt.h>
+#include <e-util/e-util.h>
+#include <widgets/misc/e-attachment-store.h>
 #include <libemail-engine/mail-ops.h>
 
 #include "em-format-html-print.h"
 
 static gpointer parent_class = NULL;
 
+struct _EMFormatHTMLPrintPrivate {
+
+	EMFormatHTML *original_formatter;
+	EMFormatPURI *top_level_puri;
+	GtkPrintOperationAction print_action;
+
+};
+
+G_DEFINE_TYPE (
+	EMFormatHTMLPrint, 
+	em_format_html_print, 
+	EM_TYPE_FORMAT_HTML)
+
+enum {
+	PROP_0,
+	PROP_ORIGINAL_FORMATTER,
+	PROP_PRINT_ACTION
+};
+
+static void efhp_write_print_layout (EMFormat *emf, EMFormatPURI *puri, CamelStream *stream, EMFormatWriterInfo *info, GCancellable *cancellable);
+
+static void
+attachment_bar_html (EMFormatPURI *puri,
+		     GString *buffer,
+		     GCancellable *cancellable)
+{
+	EAttachmentStore *store;
+	EMailAttachmentBar *bar;
+	GList *attachments, *iter;
+	GtkIconTheme *icon_theme;
+
+	bar = E_MAIL_ATTACHMENT_BAR (
+		puri->widget_func (puri->emf, puri, cancellable));
+	store = e_mail_attachment_bar_get_store (bar);
+
+	g_string_append_printf (buffer, 
+		"<fieldset class=\"attachments\"><legend>%s</legend>",
+		_("Attachments"));
+
+	icon_theme = gtk_icon_theme_get_default ();
+	attachments = e_attachment_store_get_attachments (store);
+	for (iter = attachments; iter != NULL; iter = iter->next) {
+		EAttachment *attachment = iter->data;
+		GFileInfo *finfo = e_attachment_get_file_info (attachment);
+		GIcon *icon;
+		GtkIconInfo *icon_info;
+
+		icon = g_file_info_get_icon (finfo);
+		if (icon) {
+			icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme,
+				icon, 48, 0);
+		} 
+
+		if (!icon || !icon_info) {
+			icon_info = gtk_icon_theme_lookup_icon (icon_theme,
+				"gtk-file", 48, 0);
+		}
+
+		g_string_append_printf (buffer,
+			"<div class=\"attachment\" >" \
+			"<img src=\"evo-file://%s\" width=\"64\" height=\"64\" />"
+			"<br>%s</div>",
+			gtk_icon_info_get_filename (icon_info),
+			g_file_info_get_display_name (finfo));
+	}
+
+	g_string_append (buffer, "<div style=\"clear: both; width: 100%\"></div></fieldset>");
+
+	g_list_free (attachments);
+}
+
+
+static void
+efhp_write_print_layout (EMFormat *emf,
+			 EMFormatPURI *puri,
+			 CamelStream *stream,
+			 EMFormatWriterInfo *info,
+			 GCancellable *cancellable)
+{
+	GList *iter;
+	GString *str = g_string_new ("");
+	GString *mail_uri;
+	gint len;
+
+	g_string_append (str,
+		"<!DOCTYPE HTML>\n<html>\n"  \
+		"<head>\n<meta name=\"generator\" content=\"Evolution Mail Component\" />\n" \
+		"<title>Evolution Mail Display</title>\n" \
+		"<link type=\"text/css\" rel=\"stylesheet\" href=\"evo-file://" EVOLUTION_PRIVDATADIR "/theme/webview.css\" />\n" \
+		"</head>\n" \
+		"<body style=\"background: #FFF; color: #000;\">");
+
+	mail_uri = g_string_new ("");
+	g_string_assign (mail_uri, em_format_build_mail_uri (emf->folder,
+				emf->message_uid, NULL, NULL));
+	len = mail_uri->len;
+
+	int height;
+	int i = 0;
+	for (iter = emf->mail_part_list; iter != NULL; iter = iter->next) {
+
+		EMFormatPURI *puri = iter->data;
+
+		/* Convert attachment bar to fancy HTML */
+		if (g_str_has_suffix (puri->uri, ".attachment-bar")) {
+			attachment_bar_html (puri, str, cancellable);
+			continue;
+		}
+
+		/* Skip widget-parts. We either don't want them displayed
+		 * or we will handle them manually */
+		if (puri->write_func == NULL ||
+		    g_str_has_prefix (puri->uri, "print_layout"))
+			continue;
+
+		if (puri->is_attachment || g_str_has_suffix (puri->uri, ".attachment")) {
+			const EMFormatHandler *handler;
+			EAttachment *attachment;
+			GFileInfo *fi;
+
+			CamelContentType *ct = camel_mime_part_get_content_type (puri->part);
+			gchar *mime_type = camel_content_type_simple (ct);
+
+			handler = em_format_find_handler (puri->emf, mime_type);
+			g_free (mime_type);
+
+			/* If we can't inline this attachment, skip it */
+			if (!em_format_is_inline (puri->emf,  puri->uri, puri->part, handler))
+				continue;
+
+			attachment = ((EMFormatAttachmentPURI *) puri)->attachment;
+			fi = e_attachment_get_file_info (attachment);
+			g_string_append_printf (str, "<table border=\"0\" width=\"100%%\"><tr>" \
+				"<td><strong><big>%s</big></strong></td>" \
+				"<td style=\"text-align: right;\">Type: %s&nbsp;&nbsp;&nbsp;&nbsp;" \
+				"Size: %ld bytes</td></tr></table>",
+			   	e_attachment_get_description (attachment),
+				e_attachment_get_mime_type (attachment),
+				g_file_info_get_size (fi));
+		}
+
+		if (i == 0)
+			height = 120;
+		else if (i == 1)
+			height = 360;
+		else if (i == 2)
+			height = 250;
+		else if (i == 3)
+			height = 150;
+		else if (i == 4)
+			height = 600;
+		else 
+			height = 600;
+		
+		i++;
+		
+
+		g_string_append_printf (mail_uri, "?part_id=%s&mode=%d", puri->uri,
+			EM_FORMAT_WRITE_MODE_PRINTING);
+		g_message ("%s", mail_uri->str);
+		
+		g_string_append_printf (str, 
+			"<iframe frameborder=\"0\" width=\"100%%\" "
+			"src=\"%s\" height=\"%d\"></iframe>\n", mail_uri->str, height); 
+
+		g_string_truncate (mail_uri, len);
+	}
+	g_string_append (str, "</body></html>");
+
+	camel_stream_write_string (stream, str->str, cancellable, NULL);
+
+	g_string_free (mail_uri, TRUE);
+	g_string_free (str, TRUE);
+}
+
+
 static void
 efhp_finalize (GObject *object)
 {
-	EMFormatHTMLPrint *efhp = (EMFormatHTMLPrint *) object;
+	EMFormatHTMLPrint *efhp = EM_FORMAT_HTML_PRINT (object);
 
-	g_free (efhp->export_filename);
-	efhp->export_filename = NULL;
-	gtk_widget_destroy (efhp->window);
-	if (efhp->source != NULL)
-		g_object_unref (efhp->source);
+	if (efhp->priv->original_formatter) {
+		g_object_unref (efhp->priv->original_formatter);
+		efhp->priv->original_formatter = NULL;
+	}
+
+	if (efhp->priv->top_level_puri) {
+		em_format_puri_free (efhp->priv->top_level_puri);
+		efhp->priv->top_level_puri = NULL;
+	}
 
 	/* Chain up to parent's finalize() method. */
 	G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -65,69 +247,148 @@ efhp_is_inline (EMFormat *emf,
 }
 
 static void
-efhp_class_init (EMFormatHTMLPrintClass *class)
+efhp_set_orig_formatter (EMFormatHTMLPrint *efhp,
+		    	 EMFormat *formatter)
+{
+	EMFormat *emfp, *emfs;
+	EMFormatPURI *puri;
+	GHashTableIter iter;
+	gpointer key, value;
+
+	efhp->priv->original_formatter = g_object_ref (formatter);
+
+	emfp = EM_FORMAT (efhp);
+	emfs = EM_FORMAT (formatter);
+
+	emfp->mail_part_list = g_list_copy (emfs->mail_part_list);
+
+	/* Make a shallow copy of the table. This table will NOT destroy
+	 * the PURIs when free'd! */
+	emfp->mail_part_table = 
+		g_hash_table_new (g_str_hash, g_str_equal);
+	g_hash_table_iter_init (&iter, emfs->mail_part_table);
+	while (g_hash_table_iter_next (&iter, &key, &value))
+		g_hash_table_insert (emfp->mail_part_table, key, value);
+
+	emfp->folder = g_object_ref (emfs->folder);
+	emfp->message_uid = g_strdup (emfs->message_uid);
+	emfp->message = g_object_ref (emfs->message);
+
+	/* Add a generic PURI that will write a HTML layout
+	   for all the parts */
+	puri = em_format_puri_new (EM_FORMAT (efhp),
+		sizeof (EMFormatPURI), NULL, "print_layout");
+	puri->write_func = efhp_write_print_layout;
+	puri->mime_type = g_strdup ("text/html");
+	em_format_add_puri (EM_FORMAT (efhp), puri);		
+	efhp->priv->top_level_puri = puri;	
+}
+
+
+static void
+efhp_set_property (GObject *object,
+		   guint prop_id,
+		   const GValue *value,
+		   GParamSpec *pspec)
+{
+	EMFormatHTMLPrintPrivate *priv;
+
+	priv = EM_FORMAT_HTML_PRINT (object)->priv;
+
+	switch (prop_id) {
+		
+		case PROP_ORIGINAL_FORMATTER:
+			efhp_set_orig_formatter (
+				EM_FORMAT_HTML_PRINT (object),
+				(EMFormat *) g_value_get_object (value));
+			return;
+
+		case PROP_PRINT_ACTION:
+			priv->print_action = 
+				g_value_get_int (value);
+			return;
+		
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+}
+
+static void
+efhp_get_property (GObject *object,
+		   guint prop_id,
+		   GValue *value,
+		   GParamSpec *pspec)
+{
+	EMFormatHTMLPrintPrivate *priv;
+
+	priv = EM_FORMAT_HTML_PRINT (object)->priv;
+
+	switch (prop_id) {
+		
+		case PROP_ORIGINAL_FORMATTER:
+			g_value_set_pointer (value,
+				priv->original_formatter);
+			return;
+
+		case PROP_PRINT_ACTION:
+			g_value_set_int (value,
+				priv->print_action);
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+}
+
+static void
+em_format_html_print_class_init (EMFormatHTMLPrintClass *class)
 {
 	GObjectClass *object_class;
 	EMFormatClass *format_class;
 
 	parent_class = g_type_class_peek_parent (class);
+	g_type_class_add_private (class, sizeof (EMFormatHTMLPrintPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
 	object_class->finalize = efhp_finalize;
+	object_class->set_property = efhp_set_property;
+	object_class->get_property = efhp_get_property;
 
 	format_class = EM_FORMAT_CLASS (class);
 	format_class->is_inline = efhp_is_inline;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_ORIGINAL_FORMATTER,
+		g_param_spec_object (
+			"original-formatter",
+			NULL,
+			NULL,
+			EM_TYPE_FORMAT,
+			G_PARAM_WRITABLE |
+			G_PARAM_CONSTRUCT_ONLY));
+
+	g_object_class_install_property (
+		object_class,
+		PROP_PRINT_ACTION,
+		g_param_spec_int (
+			"print-action",
+			NULL,
+			NULL,
+			GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
+			GTK_PRINT_OPERATION_ACTION_EXPORT,
+			GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
-efhp_init (EMFormatHTMLPrint *efhp)
+em_format_html_print_init (EMFormatHTMLPrint *efhp)
 {
-	EWebView *web_view;
-
-	/* FIXME WEBKIT: this ain't gonna work
-	web_view = em_format_html_get_web_view (EM_FORMAT_HTML (efhp));
-	*/
-
-	/* gtk widgets don't like to be realized outside top level widget
-	 * so we put new html widget into gtk window
-	/* FIXME WEBKIT: this ain't gonna work
-	efhp->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-	gtk_container_add (GTK_CONTAINER (efhp->window), GTK_WIDGET (web_view));
-	gtk_widget_realize (GTK_WIDGET (web_view));
-	efhp->parent.show_icon = FALSE;
-	*/
-	/* FIXME WEBKIT
-	((EMFormat *) efhp)->print = TRUE;
-	*/
+	efhp->priv = G_TYPE_INSTANCE_GET_PRIVATE (
+		efhp, EM_TYPE_FORMAT_HTML_PRINT, EMFormatHTMLPrintPrivate);
 
 	efhp->export_filename = NULL;
-	efhp->async = TRUE;
-}
-
-GType
-em_format_html_print_get_type (void)
-{
-	static GType type = 0;
-
-	if (G_UNLIKELY (type == 0)) {
-		static const GTypeInfo type_info = {
-			sizeof (EMFormatHTMLPrintClass),
-			(GBaseInitFunc) NULL,
-			(GBaseFinalizeFunc) NULL,
-			(GClassInitFunc) efhp_class_init,
-			(GClassFinalizeFunc) NULL,
-			NULL,  /* class_data */
-			sizeof (EMFormatHTMLPrint),
-			0,     /* n_preallocs */
-			(GInstanceInitFunc) efhp_init
-		};
-
-		type = g_type_register_static (
-			EM_TYPE_FORMAT_HTML, "EMFormatHTMLPrint",
-			&type_info, 0);
-	}
-
-	return type;
+	efhp->async = TRUE;		
 }
 
 EMFormatHTMLPrint *
@@ -136,19 +397,21 @@ em_format_html_print_new (EMFormatHTML *source,
 {
 	EMFormatHTMLPrint *efhp;
 
-	efhp = g_object_new (EM_TYPE_FORMAT_HTML_PRINT, NULL);
-	if (source != NULL)
-		efhp->source = g_object_ref (source);
-	efhp->action = action;
+	efhp = g_object_new (EM_TYPE_FORMAT_HTML_PRINT, 
+		"original-formatter", source,
+		"print-action", action,
+		NULL);
 
 	return efhp;
 }
 
+
 static gint
-efhp_calc_footer_height (GtkHTML *html,
+efhp_calc_footer_height (//GtkHTML *html,
                          GtkPrintOperation *operation,
                          GtkPrintContext *context)
 {
+/* FIXME WEBKIT
 	PangoContext *pango_context;
 	PangoFontDescription *desc;
 	PangoFontMetrics *metrics;
@@ -168,15 +431,17 @@ efhp_calc_footer_height (GtkHTML *html,
 	g_object_unref (pango_context);
 
 	return footer_height;
+*/
 }
 
 static void
-efhp_draw_footer (GtkHTML *html,
+efhp_draw_footer (//GtkHTML *html,
                   GtkPrintOperation *operation,
                   GtkPrintContext *context,
                   gint page_nr,
                   PangoRectangle *rec)
 {
+/* FIXME WEBKIT
 	PangoFontDescription *desc;
 	PangoLayout *layout;
 	gdouble x, y;
@@ -209,17 +474,19 @@ efhp_draw_footer (GtkHTML *html,
 	pango_font_description_free (desc);
 
 	g_free (text);
+*/
 }
 
 static void
 emfhp_complete (EMFormatHTMLPrint *efhp)
 {
+/* FIXME WEBKIT
 	GtkPrintOperation *operation;
 	EWebView *web_view;
 	GError *error = NULL;
 
 	operation = e_print_operation_new ();
-/* FIXME WEBKIT: Port to webkit's API, probably from outside
+// FIXME WEBKIT: Port to webkit's API, probably from outside
 	if (efhp->action == GTK_PRINT_OPERATION_ACTION_EXPORT)
 		gtk_print_operation_set_export_filename (operation, efhp->export_filename);
 
@@ -231,8 +498,9 @@ emfhp_complete (EMFormatHTMLPrint *efhp)
 		(GtkHTMLPrintDrawFunc) NULL,
 		(GtkHTMLPrintDrawFunc) efhp_draw_footer,
 		NULL, &error);
-*/
+
 	g_object_unref (operation);
+*/
 }
 
 void
@@ -256,3 +524,12 @@ em_format_html_print_message (EMFormatHTMLPrint *efhp,
 	/* FIXME Not passing a GCancellable here. */
 	em_format_parse ((EMFormat *) efhp, message, folder, NULL);
 }
+
+GtkPrintOperationAction
+em_format_html_print_get_action (EMFormatHTMLPrint *efhp)
+{
+	g_return_val_if_fail (EM_IS_FORMAT_HTML_PRINT (efhp), 
+		GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG);
+	
+	return efhp->priv->print_action;
+}
\ No newline at end of file
diff --git a/mail/em-format-html-print.h b/mail/em-format-html-print.h
index 5f08b6e..abcb815 100644
--- a/mail/em-format-html-print.h
+++ b/mail/em-format-html-print.h
@@ -45,17 +45,11 @@ G_BEGIN_DECLS
 
 typedef struct _EMFormatHTMLPrint EMFormatHTMLPrint;
 typedef struct _EMFormatHTMLPrintClass EMFormatHTMLPrintClass;
+typedef struct _EMFormatHTMLPrintPrivate EMFormatHTMLPrintPrivate;
 
 struct _EMFormatHTMLPrint {
 	EMFormatHTML parent;
-
-	/* Used to realize the gtkhtml in a toplevel. */
-	GtkWidget *window;
-
-	/* Used for print_message. */
-	EMFormatHTML *source;
-
-	GtkPrintOperationAction action;
+	EMFormatHTMLPrintPrivate *priv;
 	gchar *export_filename;
 
 	gboolean async;
@@ -73,6 +67,8 @@ void		em_format_html_print_message	(EMFormatHTMLPrint *efhp,
 						 CamelMimeMessage *message,
 						 CamelFolder *folder,
 						 const gchar *uid);
+GtkPrintOperationAction
+		em_format_html_print_get_action	(EMFormatHTMLPrint *efhp);
 
 G_END_DECLS
 
diff --git a/mail/em-format-html.c b/mail/em-format-html.c
index 2ae36a3..911eeb1 100644
--- a/mail/em-format-html.c
+++ b/mail/em-format-html.c
@@ -98,16 +98,8 @@ enum {
 	PROP_TEXT_COLOR,
 };
 
-#define EFM_MESSAGE_START_ANAME "evolution_message_start"
-#define EFH_MESSAGE_START "<A name=\"" EFM_MESSAGE_START_ANAME "\"></A>"
-#define EFH_HTML_HEADER "<!DOCTYPE HTML>\n<html>\n"  \
-		"<head>\n<meta name=\"generator\" content=\"Evolution Mail Component\" />\n" \
-		"<title>Evolution Mail Display</title>\n" \
-		"<link type=\"text/css\" rel=\"stylesheet\" href=\"evo-file://" EVOLUTION_PRIVDATADIR "/theme/webview.css\" />\n" \
-		"<style type=\"text/css\">\n" \
-		"  table th { color: #000; font-weight: bold; }\n" \
-		"</style>\n" \
-		"<script type=\"text/javascript\">\n" \
+/*
+ * 		"<script type=\"text/javascript\">\n" \
 		"function body_loaded() { window.location.hash='" EFM_MESSAGE_START_ANAME "'; }\n" \
 		"function collapse_addresses(field) {\n" \
 		"  var e=window.document.getElementById(\"moreaddr-\"+field).style;\n" \
@@ -126,8 +118,20 @@ enum {
 		"	 i.src=i.src.substr(0,i.src.lastIndexOf(\"/\"))+\"/minus.png\"; window.headers_collapsed(false, window.em_format_html); }\n" \
 		"}\n" \
 		"</script>\n" \
+		*/
+
+#define EFM_MESSAGE_START_ANAME "evolution_message_start"
+#define EFH_MESSAGE_START "<A name=\"" EFM_MESSAGE_START_ANAME "\"></A>"
+#define EFH_HTML_HEADER "<!DOCTYPE HTML>\n<html>\n"  \
+		"<head>\n<meta name=\"generator\" content=\"Evolution Mail Component\" />\n" \
+		"<title>Evolution Mail Display</title>\n" \
+		"<link type=\"text/css\" rel=\"stylesheet\" href=\"evo-file://" EVOLUTION_PRIVDATADIR "/theme/webview.css\" />\n" \
+		"<style type=\"text/css\">\n" \
+		"  table th { color: #000; font-weight: bold; }\n" \
+		"</style>\n" \
 		"</head>\n" \
-		"<body style=\"background: #%06x; color: #%06x;\" onLoad=\"body_loaded();\">"
+		"<body style=\"background: #%06x; color: #%06x;\">"
+//		"<body style=\"background: #%06x; color: #%06x;\" onLoad=\"body_loaded();\">"
 #define EFH_HTML_FOOTER "</body></html>"
 
 static void efh_parse_image			(EMFormat *emf, CamelMimePart *part, GString *part_id, EMFormatParserInfo *info, GCancellable *cancellable);
@@ -790,17 +794,23 @@ efh_write_headers (EMFormat *emf,
 {
 	GString *buffer;
 	EMFormatHTML *efh = (EMFormatHTML *) emf;
+	gint bg_color;
 
 	if (!puri->part)
 		return;
 
 	buffer = g_string_new ("");
 
+	if (info->mode & EM_FORMAT_WRITE_MODE_PRINTING) {
+		GdkColor white = { 0, G_MAXUINT16, G_MAXUINT16, G_MAXUINT16 };
+		bg_color = e_color_to_value(&white);
+	} else {
+		bg_color = e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_BODY]);
+	}
+
 	g_string_append_printf (
 		buffer, EFH_HTML_HEADER,
-		e_color_to_value (
-			&efh->priv->colors[
-			EM_FORMAT_HTML_COLOR_BODY]),
+		bg_color,
 		e_color_to_value (
 			&efh->priv->colors[
 			EM_FORMAT_HTML_COLOR_HEADER]));



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