[evolution/webkit: 46/177] Continue porting of printing



commit 3b89882142206eb65f7c88399e2b217f0b181b50
Author: Dan VrÃtil <dvratil redhat com>
Date:   Wed Nov 16 15:59:12 2011 +0100

    Continue porting of printing
    
    - added EMailPrinter class to handle printing via webkit
    - added custom dialog to Print dialog to choose which email headers should be printed
    - ported footer with page numbers

 e-util/e-marshal.list       |    1 +
 mail/Makefile.am            |    2 +
 mail/e-mail-printer.c       |  417 +++++++++++++++++++++++++++++++++++++++++++
 mail/e-mail-printer.h       |   78 ++++++++
 mail/e-mail-reader-utils.c  |   97 ++++++----
 mail/em-format-html-print.c |  125 ++------------
 mail/em-format-html.c       |    2 +-
 mail/mail.error.xml         |    5 +
 8 files changed, 572 insertions(+), 155 deletions(-)
---
diff --git a/e-util/e-marshal.list b/e-util/e-marshal.list
index bf15633..62be8aa 100644
--- a/e-util/e-marshal.list
+++ b/e-util/e-marshal.list
@@ -39,6 +39,7 @@ NONE:INT,POINTER,INT,OBJECT,INT,INT,BOXED,UINT,UINT
 NONE:INT,POINTER,INT,OBJECT,UINT
 NONE:LONG,LONG
 NONE:OBJECT,BOOLEAN
+NONE:OBJECT,INT
 NONE:OBJECT,DOUBLE,DOUBLE,BOOLEAN
 NONE:OBJECT,OBJECT
 NONE:OBJECT,STRING
diff --git a/mail/Makefile.am b/mail/Makefile.am
index 4c1e974..6a277c0 100644
--- a/mail/Makefile.am
+++ b/mail/Makefile.am
@@ -58,6 +58,7 @@ mailinclude_HEADERS =					\
 	e-mail-migrate.h				\
 	e-mail-notebook-view.h				\
 	e-mail-paned-view.h				\
+	e-mail-printer.h				\
 	e-mail-reader-utils.h				\
 	e-mail-reader.h					\
 	e-mail-request.h				\
@@ -122,6 +123,7 @@ libevolution_mail_la_SOURCES =				\
 	e-mail-migrate.c				\
 	e-mail-notebook-view.c				\
 	e-mail-paned-view.c				\
+	e-mail-printer.c				\
 	e-mail-reader-utils.c				\
 	e-mail-reader.c					\
 	e-mail-request.c				\
diff --git a/mail/e-mail-printer.c b/mail/e-mail-printer.c
new file mode 100644
index 0000000..daac905
--- /dev/null
+++ b/mail/e-mail-printer.c
@@ -0,0 +1,417 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Copyright (C) 2011 Dan Vratil <dvratil redhat com>
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include <e-util/e-marshal.h>
+
+#include "e-mail-printer.h"
+#include "em-format-html-print.h"
+
+static gpointer parent_class = NULL;
+
+struct _EMailPrinterPrivate {
+	EMFormatHTMLPrint *efhp;
+
+	GtkListStore *headers;
+	
+	WebKitWebView *webview; /* WebView to print from */
+};
+
+G_DEFINE_TYPE (
+	EMailPrinter, 
+	e_mail_printer, 
+	G_TYPE_OBJECT);
+
+enum {
+	PROP_0,
+	PROP_PRINT_FORMATTER
+};
+
+enum {
+	SIGNAL_DONE,
+	LAST_SIGNAL
+};
+
+enum {
+	COLUMN_ACTIVE,
+	COLUMN_HEADER_NAME,
+	COLUMN_HEADER_VALUE,
+	LAST_COLUMN
+};
+
+static guint signals[LAST_SIGNAL]; 
+
+static void
+emp_draw_footer (GtkPrintOperation *operation,
+                 GtkPrintContext *context,
+                 gint page_nr)
+{
+	PangoFontDescription *desc;
+	PangoLayout *layout;
+	gint n_pages;
+	gdouble width, height;
+	gchar *text;
+	cairo_t *cr;
+
+	cr = gtk_print_context_get_cairo_context (context);	
+	width = gtk_print_context_get_width (context);
+	height = gtk_print_context_get_height (context);
+
+	g_object_get (operation, "n-pages", &n_pages, NULL);
+	text = g_strdup_printf (_("Page %d of %d"), page_nr + 1, n_pages);
+
+	cairo_set_source_rgb (cr, 0.1, 0.1, 0.1);
+	cairo_fill (cr);
+
+	desc = pango_font_description_from_string ("Sans Regular 10");
+	layout = gtk_print_context_create_pango_layout (context);
+	pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
+	pango_layout_set_font_description (layout, desc);
+	pango_layout_set_text (layout, text, -1);
+	pango_layout_set_width (layout, width * PANGO_SCALE);
+	pango_font_description_free (desc);
+
+	cairo_move_to (cr, 0, height + 5);
+	pango_cairo_show_layout (cr, layout);
+
+	g_object_unref (layout);
+	g_free (text);
+}
+
+static void
+emp_printing_done (GtkPrintOperation *operation,
+		   GtkPrintOperationResult result,
+		   gpointer user_data)
+{
+	EMailPrinter *emp = user_data;
+
+	g_signal_emit (emp, signals[SIGNAL_DONE], 0, operation, result);
+}
+
+static void
+emp_run_print_operation (EMailPrinter *emp,
+		      	 GtkPrintOperation *operation)
+{
+	EMFormat *emf;
+	SoupSession *session;
+	GHashTable *formatters;
+	gchar *mail_uri, *tmp;
+	WebKitWebFrame *frame;
+
+	emf = EM_FORMAT (emp->priv->efhp);
+	mail_uri = em_format_build_mail_uri (emf->folder, emf->message_uid, NULL, NULL);
+
+	/* 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), emp->priv->efhp);
+
+	/* FIXME: We want an EActivity, but how?
+	activity = e_mail_reader_new_activity (reader);
+	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);
+	if (emp->priv->webview == NULL) {
+		emp->priv->webview = WEBKIT_WEB_VIEW (webkit_web_view_new ());
+		g_object_ref_sink (emp->priv->webview);
+	}
+
+	webkit_web_view_load_uri (emp->priv->webview, tmp);
+
+	frame = webkit_web_view_get_main_frame (emp->priv->webview);
+	webkit_web_frame_print_full (frame, operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL);
+
+	g_free (tmp);	
+	g_free (mail_uri);
+}
+
+
+static void
+header_active_renderer_toggled_cb (GtkCellRendererToggle *renderer,
+				   gchar *path,
+				   EMailPrinter *emp)
+{
+	GtkTreeIter iter;
+	gboolean active;
+
+	gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (emp->priv->headers),
+		&iter, path);
+	gtk_tree_model_get (GTK_TREE_MODEL (emp->priv->headers), &iter,
+		COLUMN_ACTIVE, &active, -1);
+	gtk_list_store_set (GTK_LIST_STORE (emp->priv->headers),
+		&iter, COLUMN_ACTIVE, !active, -1);		
+}
+
+static GtkWidget*
+emp_get_headers_tab (GtkPrintOperation *operation,
+		     EMailPrinter *emp)
+{
+	GtkWidget *box, *label, *scw;
+	GtkTreeView *view;
+	GtkTreeViewColumn *column;
+	GtkCellRenderer *renderer;
+
+	box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
+
+	label = gtk_label_new (_("Select headers you want to print"));
+	gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 5);
+
+	view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (
+		GTK_TREE_MODEL (emp->priv->headers)));
+
+	renderer = gtk_cell_renderer_toggle_new ();
+	g_signal_connect (renderer, "toggled",
+		G_CALLBACK (header_active_renderer_toggled_cb), emp);
+	column = gtk_tree_view_column_new_with_attributes (
+		_("Print"), renderer, 
+		"active", COLUMN_ACTIVE, NULL);
+	gtk_tree_view_append_column (view, column);
+
+	renderer = gtk_cell_renderer_text_new ();
+	column = gtk_tree_view_column_new_with_attributes (
+		_("Header Name"), renderer, 
+		"text", COLUMN_HEADER_NAME, NULL);
+	gtk_tree_view_append_column (view, column);
+
+	renderer = gtk_cell_renderer_text_new ();
+	column = gtk_tree_view_column_new_with_attributes (
+		_("Header Value"), renderer,
+		"text", COLUMN_HEADER_VALUE, NULL);
+	gtk_tree_view_append_column (view, column);
+
+	scw = gtk_scrolled_window_new (NULL, NULL);
+	gtk_container_add (GTK_CONTAINER (scw), GTK_WIDGET (view));
+	gtk_box_pack_start (GTK_BOX (box), scw, TRUE, TRUE, 5);
+	gtk_widget_show_all (box);
+
+	gtk_print_operation_set_custom_tab_label (operation, _("Headers"));
+	return box;
+}
+
+static gint
+emf_header_name_equal (const EMFormatHeader *header,
+		       const gchar *name)
+{
+	return g_strcmp0 (name, header->name);
+}
+
+static void
+emp_set_formatter (EMailPrinter *emp,
+		   EMFormatHTMLPrint *formatter)
+{
+	CamelMedium *msg;
+	GArray *headers;
+	gint i;
+
+	g_return_if_fail (EM_IS_FORMAT_HTML_PRINT (formatter));
+
+	g_object_ref (formatter);
+
+	if (emp->priv->efhp)
+		g_object_unref (emp->priv->efhp);
+
+	emp->priv->efhp = formatter;
+
+	if (emp->priv->headers)
+		g_object_unref (emp->priv->headers);
+	emp->priv->headers = gtk_list_store_new (3, 
+		G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING);
+
+	msg = CAMEL_MEDIUM (EM_FORMAT (formatter)->message);
+	headers = camel_medium_get_headers (msg);
+	for (i = 0; i < headers->len; i++) {
+		CamelMediumHeader *header;
+		GtkTreeIter iter;
+		gboolean active = false;
+
+		header = &g_array_index (headers, CamelMediumHeader, i);
+
+		active = (g_queue_find_custom (&EM_FORMAT (formatter)->header_list,
+				header->name, (GCompareFunc) emf_header_name_equal) != NULL);
+
+		gtk_list_store_append (emp->priv->headers, &iter);
+		gtk_list_store_set (emp->priv->headers, &iter,
+			COLUMN_ACTIVE, active,
+			COLUMN_HEADER_NAME, header->name,
+			COLUMN_HEADER_VALUE, header->value, -1);
+	}
+}
+
+static void
+emp_set_property (GObject *object,
+                  guint property_id,
+                  const GValue *value,
+                  GParamSpec *pspec)
+{
+	EMailPrinter *emp = E_MAIL_PRINTER (object);
+
+	switch (property_id) {
+
+		case PROP_PRINT_FORMATTER:
+			emp_set_formatter (emp, g_value_get_object (value));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+emp_get_property (GObject *object,
+		  guint property_id,
+		  GValue *value,
+		  GParamSpec *pspec)
+{
+	EMailPrinter *emp = E_MAIL_PRINTER (object);
+
+	switch (property_id) {
+
+		case PROP_PRINT_FORMATTER:
+			g_value_set_object (value,
+				e_mail_printer_get_print_formatter (emp));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+emp_finalize (GObject *object)
+{
+	EMailPrinterPrivate *priv = E_MAIL_PRINTER (object)->priv;
+
+	if (priv->efhp) {
+		g_object_unref (priv->efhp);
+		priv->efhp = NULL;
+	}
+
+	if (priv->headers) {
+		g_object_unref (priv->headers);
+		priv->headers = NULL;
+	}
+
+	if (priv->webview) {
+		g_object_unref (priv->webview);
+		priv->webview = NULL;
+	}
+
+	/* Chain up to parent's finalize() method. */
+	G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+e_mail_printer_class_init (EMailPrinterClass *class)
+{
+	GObjectClass *object_class;
+
+	parent_class = g_type_class_peek_parent (class);
+	g_type_class_add_private (class, sizeof (EMailPrinterPrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = emp_set_property;
+	object_class->get_property = emp_get_property;
+	object_class->finalize = emp_finalize;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_PRINT_FORMATTER,
+		g_param_spec_object (
+			"print-formatter",
+			NULL,
+			NULL,
+			EM_TYPE_FORMAT_HTML_PRINT,
+		        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+	signals[SIGNAL_DONE] =	g_signal_new ("done",
+		G_TYPE_FROM_CLASS (class),
+		G_SIGNAL_RUN_FIRST,
+		G_STRUCT_OFFSET (EMailPrinterClass, done),
+		NULL, NULL,
+		e_marshal_VOID__OBJECT_INT,
+		G_TYPE_NONE, 2,
+		GTK_TYPE_PRINT_OPERATION, G_TYPE_INT);
+}
+
+static void
+e_mail_printer_init (EMailPrinter *emp)
+{
+	emp->priv = G_TYPE_INSTANCE_GET_PRIVATE (
+		emp, E_TYPE_MAIL_PRINTER, EMailPrinterPrivate);
+
+	emp->priv->efhp = NULL;
+	emp->priv->headers = NULL;
+	emp->priv->webview = NULL;
+}
+
+EMailPrinter* 
+e_mail_printer_new (EMFormatHTML* source,
+		    GtkPrintOperationAction action)
+{
+	EMailPrinter *emp;
+	EMFormatHTMLPrint *efhp;
+
+	efhp = em_format_html_print_new (source, action);
+
+	emp = g_object_new (E_TYPE_MAIL_PRINTER,
+		"print-formatter", efhp, NULL);
+
+	g_object_unref (efhp);
+
+	return emp;
+}
+
+void
+e_mail_printer_print (EMailPrinter *emp,
+		      GCancellable *cancellable)
+{
+	GtkPrintOperation *operation;	
+
+	g_return_if_fail (E_IS_MAIL_PRINTER (emp));
+
+	operation = gtk_print_operation_new ();
+	gtk_print_operation_set_show_progress (operation, TRUE);
+	g_signal_connect (operation, "create-custom-widget",
+		G_CALLBACK (emp_get_headers_tab), emp);
+	g_signal_connect (operation, "done",
+		G_CALLBACK (emp_printing_done), emp);
+	g_signal_connect_swapped (cancellable, "cancelled",
+		G_CALLBACK (gtk_print_operation_cancel), operation);
+	g_signal_connect (operation, "draw-page",
+		G_CALLBACK (emp_draw_footer), NULL);
+
+	emp_run_print_operation (emp, operation);
+}
+
+EMFormatHTMLPrint*
+e_mail_printer_get_print_formatter (EMailPrinter *emp)
+{
+	g_return_val_if_fail (E_IS_MAIL_PRINTER (emp), NULL);
+
+	return emp->priv->efhp;
+}
+
diff --git a/mail/e-mail-printer.h b/mail/e-mail-printer.h
new file mode 100644
index 0000000..e8eea75
--- /dev/null
+++ b/mail/e-mail-printer.h
@@ -0,0 +1,78 @@
+/*
+ * Class for printing emails
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Copyright (C) 2011 Dan Vratil <dvratil redhat com>
+ */
+
+#ifndef E_MAIL_PRINTER_H
+#define E_MAIL_PRINTER_H
+
+#include "mail/em-format-html-print.h"
+
+/* Standard GObject macros */
+#define E_TYPE_MAIL_PRINTER \
+	(e_mail_printer_get_type ())
+#define E_MAIL_PRINTER(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), E_TYPE_MAIL_PRINTER, EMailPrinter))
+#define E_MAIL_PRINTER_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), E_TYPE_MAIL_PRINTER, EMailPrinterClass))
+#define E_IS_MAIL_PRINTER(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), E_TYPE_MAIL_PRINTER))
+#define E_IS_MAIL_PRINTER_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), E_TYPE_MAIL_PRINTER_CLASS))
+#define E_MAIL_PRINTER_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), E_TYPE_MAIL_PRINTER, EMailPrinterClass))
+
+G_BEGIN_DECLS
+
+typedef struct _EMailPrinter EMailPrinter;
+typedef struct _EMailPrinterClass EMailPrinterClass;
+typedef struct _EMailPrinterPrivate EMailPrinterPrivate;
+
+struct _EMailPrinter {
+	GObject parent;
+	EMailPrinterPrivate *priv;
+};
+
+struct _EMailPrinterClass {
+	GObjectClass parent_class;
+
+	void 	(*done)			(EMailPrinter *printer,
+					 GtkPrintOperation *operation,
+		  			 GtkPrintOperationResult *result,
+		  			 gpointer user_data);
+};
+
+GType		e_mail_printer_get_type	(void);
+
+EMailPrinter *	e_mail_printer_new	(EMFormatHTML *source,
+					 GtkPrintOperationAction action);
+
+void		e_mail_printer_print	(EMailPrinter *printer,
+					 GCancellable *cancellable);
+
+EMFormatHTMLPrint *
+		e_mail_printer_get_print_formatter
+					(EMailPrinter *printer);
+
+G_END_DECLS
+
+#endif /* E_MAIL_PRINTER_H */
diff --git a/mail/e-mail-reader-utils.c b/mail/e-mail-reader-utils.c
index 91614cf..95c04a0 100644
--- a/mail/e-mail-reader-utils.c
+++ b/mail/e-mail-reader-utils.c
@@ -44,6 +44,7 @@
 
 #include "mail/e-mail-backend.h"
 #include "mail/e-mail-browser.h"
+#include "mail/e-mail-printer.h"
 #include "mail/em-composer-utils.h"
 #include "mail/em-format-html-print.h"
 #include "mail/em-utils.h"
@@ -432,9 +433,9 @@ e_mail_reader_open_selected (EMailReader *reader)
 }
 
 static void
-mail_reader_print_finished (GtkPrintOperation *operation,
-			    GtkPrintOperationResult result,
-			    EActivity *activity)
+mail_reader_printing_finished (GtkPrintOperation *operation,
+			       GtkPrintOperationResult result,
+			       EActivity *activity)
 {
 	WebKitWebView *webview;
 
@@ -461,14 +462,17 @@ webview_document_load_finished_cb (WebKitWebView *webview,
 
 	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 ();
+	gtk_print_operation_set_show_progress (operation, TRUE);
+
 	g_object_set_data (G_OBJECT (operation), "webview", webview);
 	g_signal_connect (operation, "done",
-		G_CALLBACK (mail_reader_print_finished), activity);
+		G_CALLBACK (mail_reader_printing_finished), activity);
 
 	action = em_format_html_print_get_action (efhp);
 	webkit_web_frame_print_full (frame, 
@@ -477,53 +481,66 @@ webview_document_load_finished_cb (WebKitWebView *webview,
 	g_object_unref (efhp);
 }
 
+static void
+printing_done_cb (EMailPrinter *printer,
+		  GtkPrintOperation *operation,
+		  GtkPrintOperationResult result,
+		  gpointer user_data)
+{
+	EActivity *activity = user_data;
+
+	if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
+
+		EAlertSink *alert_sink;
+		GError *error = NULL;
+
+		alert_sink = e_activity_get_alert_sink (activity);
+		gtk_print_operation_get_error (operation, &error);
+
+		if (error != NULL) {
+			e_alert_submit (alert_sink, "mail:printing-failed",
+				error->message, NULL);
+			g_error_free (error);
+		}
+
+		g_object_unref (activity);
+		g_object_unref (printer);
+		return;
+	}
+
+	/* Set activity as completed, and keep it displayed for a few seconds
+	   so that user can actually see the the printing was sucesfully finished. */
+	e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
+	g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, 3, 
+		(GSourceFunc) g_object_unref, activity, NULL);
+
+	g_object_unref (printer);
+}
+
 void
 e_mail_reader_print (EMailReader *reader,
                      GtkPrintOperationAction action)
 {
-	EActivity *activity;
 	EMailDisplay *display;
+	EMailPrinter *printer;
 	EMFormatHTML *formatter;
-	EMFormatHTMLPrint *efhp;
-	gchar *mail_uri, *tmp;
-	SoupSession *session;
-	GHashTable *formatters;
-	GtkWidget *webview;
-	WebKitWebSettings *settings;
-	GtkPrintOperation *operation;
+	EActivity *activity;
+	GCancellable *cancellable;
 
 	g_return_if_fail (E_IS_MAIL_READER (reader));
 
 	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);
-	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);
+	e_activity_set_text (activity, _("Printing"));
+	e_activity_set_state (activity, E_ACTIVITY_RUNNING);
+	cancellable = e_activity_get_cancellable (activity);
+
+	printer = e_mail_printer_new (formatter, action);
+	g_signal_connect (printer, "done",
+		G_CALLBACK (printing_done_cb), activity);
+	e_mail_printer_print (printer, cancellable);
 }
 
 static void
diff --git a/mail/em-format-html-print.c b/mail/em-format-html-print.c
index 7017560..f8e3f4e 100644
--- a/mail/em-format-html-print.c
+++ b/mail/em-format-html-print.c
@@ -52,7 +52,7 @@ struct _EMFormatHTMLPrintPrivate {
 G_DEFINE_TYPE (
 	EMFormatHTMLPrint, 
 	em_format_html_print, 
-	EM_TYPE_FORMAT_HTML)
+	EM_TYPE_FORMAT_HTML);
 
 enum {
 	PROP_0,
@@ -121,34 +121,29 @@ efhp_write_print_layout (EMFormat *emf,
 			 GCancellable *cancellable)
 {
 	GList *iter;
-	GString *str = g_string_new ("");
 	GString *mail_uri;
-	gint len;
+	EMFormatWriterInfo print_info = {
+		EM_FORMAT_WRITE_MODE_PRINTING, FALSE, FALSE, FALSE };
 
-	g_string_append (str,
+	camel_stream_write_string (stream,
 		"<!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;\">");
+		"<body style=\"background: #FFF; color: #000;\">", cancellable, NULL);
 
-	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 */
+		/* FIXME WEBKIT
 		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 */
@@ -171,6 +166,7 @@ efhp_write_print_layout (EMFormat *emf,
 			if (!em_format_is_inline (puri->emf,  puri->uri, puri->part, handler))
 				continue;
 
+			/* FIXME WEBKIT
 			attachment = ((EMFormatAttachmentPURI *) puri)->attachment;
 			fi = e_attachment_get_file_info (attachment);
 			g_string_append_printf (str, "<table border=\"0\" width=\"100%%\"><tr>" \
@@ -180,40 +176,13 @@ efhp_write_print_layout (EMFormat *emf,
 			   	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);
+		puri->write_func(puri->emf, puri, stream, &print_info, cancellable);
 	}
-	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);
+	camel_stream_write_string (stream, "</body></html>", cancellable, NULL);
 }
 
 
@@ -404,78 +373,6 @@ em_format_html_print_new (EMFormatHTML *source,
 	return efhp;
 }
 
-
-static gint
-efhp_calc_footer_height (//GtkHTML *html,
-                         GtkPrintOperation *operation,
-                         GtkPrintContext *context)
-{
-/* FIXME WEBKIT
-	PangoContext *pango_context;
-	PangoFontDescription *desc;
-	PangoFontMetrics *metrics;
-	gint footer_height;
-
-	pango_context = gtk_print_context_create_pango_context (context);
-	desc = pango_font_description_from_string ("Sans Regular 10");
-
-	metrics = pango_context_get_metrics (
-		pango_context, desc, pango_language_get_default ());
-	footer_height =
-		pango_font_metrics_get_ascent (metrics) +
-		pango_font_metrics_get_descent (metrics);
-	pango_font_metrics_unref (metrics);
-
-	pango_font_description_free (desc);
-	g_object_unref (pango_context);
-
-	return footer_height;
-*/
-}
-
-static void
-efhp_draw_footer (//GtkHTML *html,
-                  GtkPrintOperation *operation,
-                  GtkPrintContext *context,
-                  gint page_nr,
-                  PangoRectangle *rec)
-{
-/* FIXME WEBKIT
-	PangoFontDescription *desc;
-	PangoLayout *layout;
-	gdouble x, y;
-	gint n_pages;
-	gchar *text;
-	cairo_t *cr;
-
-	g_object_get (operation, "n-pages", &n_pages, NULL);
-	text = g_strdup_printf (_("Page %d of %d"), page_nr + 1, n_pages);
-
-	desc = pango_font_description_from_string ("Sans Regular 10");
-	layout = gtk_print_context_create_pango_layout (context);
-	pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
-	pango_layout_set_font_description (layout, desc);
-	pango_layout_set_text (layout, text, -1);
-	pango_layout_set_width (layout, rec->width);
-
-	x = pango_units_to_double (rec->x);
-	y = pango_units_to_double (rec->y);
-
-	cr = gtk_print_context_get_cairo_context (context);
-
-	cairo_save (cr);
-	cairo_set_source_rgb (cr, .0, .0, .0);
-	cairo_move_to (cr, x, y);
-	pango_cairo_show_layout (cr, layout);
-	cairo_restore (cr);
-
-	g_object_unref (layout);
-	pango_font_description_free (desc);
-
-	g_free (text);
-*/
-}
-
 static void
 emfhp_complete (EMFormatHTMLPrint *efhp)
 {
diff --git a/mail/em-format-html.c b/mail/em-format-html.c
index c28dfe0..b54029e 100644
--- a/mail/em-format-html.c
+++ b/mail/em-format-html.c
@@ -895,7 +895,7 @@ efh_write_headers (EMFormat *emf,
 		"}\n" \
 		"</script>\n" \
 		"<table border=\"0\" width=\"100%\" height=\"100%\" style=\"background: #%06x; color: #%06x;\">\n" \
-		"<tr><td valign=\"top\" width=\"16\">\n");
+		"<tr><td valign=\"top\" width=\"16\">\n",
 		bg_color,
 		e_color_to_value (&efh->priv->colors[EM_FORMAT_HTML_COLOR_HEADER]));
 
diff --git a/mail/mail.error.xml b/mail/mail.error.xml
index 118acc1..cd85deb 100644
--- a/mail/mail.error.xml
+++ b/mail/mail.error.xml
@@ -520,5 +520,10 @@ An mbox account will be created to preserve the old mbox folders. You can delete
     <_secondary xml:space="preserve">The attachment named {0} is a hidden file and may contain sensitive data. Please review it before sending.</_secondary>
   </error>
 
+  <error id="printing-failed" type="error">
+    <_primary>Printing failed.</_primary>
+    <_secondary>The printer replied &quot;{0}&quot;.</_secondary>
+  </error>
+
 </error-list>
 



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