e-mail-printer.c 25.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * 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>

27
#include <webkit/webkitdom.h>
Dan Vrátil's avatar
Dan Vrátil committed
28

29
#include "e-util/e-util.h"
30

31
32
#include "em-format/e-mail-formatter-print.h"
#include "em-format/e-mail-part-utils.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include "e-mail-printer.h"
#include "e-mail-display.h"

enum {
        BUTTON_SELECT_ALL,
        BUTTON_SELECT_NONE,
        BUTTON_TOP,
        BUTTON_UP,
        BUTTON_DOWN,
        BUTTON_BOTTOM,
        BUTTONS_COUNT
};

#define w(x)

struct _EMailPrinterPrivate {
Dan Vrátil's avatar
Dan Vrátil committed
50
51
	EMailFormatterPrint *formatter;
	EMailPartList *parts_list;
52

Dan Vrátil's avatar
Dan Vrátil committed
53
	gchar *export_filename;
54
55
56
57
58
59
60
61
62

	GtkListStore *headers;

	WebKitWebView *webview; /* WebView to print from */
	gchar *uri;
	GtkWidget *buttons[BUTTONS_COUNT];
	GtkWidget *treeview;

	GtkPrintOperation *operation;
63
	GtkPrintOperationAction print_action;
64
65
66
67
68
69
70
71
72
};

G_DEFINE_TYPE (
	EMailPrinter,
	e_mail_printer,
	G_TYPE_OBJECT);

enum {
	PROP_0,
Dan Vrátil's avatar
Dan Vrátil committed
73
	PROP_PART_LIST
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
};

enum {
	SIGNAL_DONE,
	LAST_SIGNAL
};

enum {
	COLUMN_ACTIVE,
	COLUMN_HEADER_NAME,
	COLUMN_HEADER_VALUE,
	COLUMN_HEADER_STRUCT,
	LAST_COLUMN
};

static guint signals[LAST_SIGNAL];

static gint
Dan Vrátil's avatar
Dan Vrátil committed
92
93
emp_header_name_equal (const EMailFormatterHeader *h1,
                       const EMailFormatterHeader *h2)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
	if ((h2->value == NULL) || (h1->value == NULL)) {
		return g_strcmp0 (h1->name, h2->name);
	} else {
		if ((g_strcmp0 (h1->name, h2->name) == 0) &&
		    (g_strcmp0 (h1->value, h2->value) == 0))
			return 0;
		else
			return 1;
	}
}

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);
}

153
154
155
156
157
158
159
160
161
162
163
164
165
static gboolean
do_run_print_operation (EMailPrinter *emp)
{
	WebKitWebFrame *frame;

	frame = webkit_web_view_get_main_frame (emp->priv->webview);

	webkit_web_frame_print_full (
		frame, emp->priv->operation, emp->priv->print_action, NULL);

	return FALSE;
}

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
static void
emp_start_printing (GObject *object,
                    GParamSpec *pspec,
                    gpointer user_data)
{
	WebKitWebView *web_view;
	WebKitLoadStatus load_status;
	EMailPrinter *emp = user_data;

	web_view = WEBKIT_WEB_VIEW (object);
	load_status = webkit_web_view_get_load_status (web_view);

	if (load_status != WEBKIT_LOAD_FINISHED)
		return;

181
182
183
184
185
186
187
	/* WebKit reloads the page once more right before starting to print, so
	 * disconnect this handler after the first time, so that we don't start
	 * another printing operation */
	g_signal_handlers_disconnect_by_func (
		object, emp_start_printing, user_data);

	if (emp->priv->print_action == GTK_PRINT_OPERATION_ACTION_EXPORT) {
188
		gtk_print_operation_set_export_filename (
Dan Vrátil's avatar
Dan Vrátil committed
189
			emp->priv->operation, emp->priv->export_filename);
190
191
	}

192
193
194
195
196
197
	/* Give WebKit some time to perform layouting and rendering before
	 * we start printing. 500ms should be enough in most cases... */
	g_timeout_add_full (
		G_PRIORITY_DEFAULT, 500,
		(GSourceFunc) do_run_print_operation,
		g_object_ref (emp), g_object_unref);
198
199
200
}

static void
201
202
emp_run_print_operation (EMailPrinter *emp,
			 EMailFormatter *formatter)
203
{
204
205
206
	EMailPartList *part_list;
	CamelFolder *folder;
	const gchar *message_uid;
207
	const gchar *default_charset, *charset;
208
209
	gchar *mail_uri;

210
211
212
	part_list = emp->priv->parts_list;
	folder = e_mail_part_list_get_folder (part_list);
	message_uid = e_mail_part_list_get_message_uid (part_list);
213
214
215
216
217
218
219
	default_charset = formatter ? e_mail_formatter_get_default_charset (formatter) : NULL;
	charset = formatter ? e_mail_formatter_get_charset (formatter) : NULL;

	if (!default_charset)
		default_charset = "";
	if (!charset)
		charset = "";
220

221
	mail_uri = e_mail_part_build_uri (
222
		folder, message_uid,
Dan Vrátil's avatar
Dan Vrátil committed
223
224
		"__evo-load-image", G_TYPE_BOOLEAN, TRUE,
		"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_PRINTING,
225
226
		"formatter_default_charset", G_TYPE_STRING, default_charset,
		"formatter_charset", G_TYPE_STRING, charset,
Dan Vrátil's avatar
Dan Vrátil committed
227
		NULL);
228
229
230

	/* Print_layout is a special EMPart created by EMFormatHTMLPrint */
	if (emp->priv->webview == NULL) {
231
232
		EMailFormatter *emp_formatter;

Dan Vrátil's avatar
Dan Vrátil committed
233
234
235
		emp->priv->webview = g_object_new (
			E_TYPE_MAIL_DISPLAY,
			"mode", E_MAIL_FORMATTER_MODE_PRINTING, NULL);
236
237
		e_web_view_set_enable_frame_flattening (E_WEB_VIEW (emp->priv->webview), FALSE);
		e_mail_display_set_force_load_images (
Dan Vrátil's avatar
Dan Vrátil committed
238
239
			E_MAIL_DISPLAY (emp->priv->webview), TRUE);

240
241
242
243
244
245
		emp_formatter = e_mail_display_get_formatter (E_MAIL_DISPLAY (emp->priv->webview));
		if (default_charset && *default_charset)
			e_mail_formatter_set_default_charset (emp_formatter, default_charset);
		if (charset && *charset)
			e_mail_formatter_set_charset (emp_formatter, charset);

246
		g_object_ref_sink (emp->priv->webview);
247
248
		g_signal_connect (
			emp->priv->webview, "notify::load-status",
249
250
251
252
253
254
			G_CALLBACK (emp_start_printing), emp);

		w ({
			GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
			GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
			gtk_container_add (GTK_CONTAINER (window), sw);
255
256
257
			gtk_container_add (
				GTK_CONTAINER (sw),
				GTK_WIDGET (emp->priv->webview));
258
259
260
			gtk_widget_show_all (window);
		});
	}
Dan Vrátil's avatar
Dan Vrátil committed
261
262
263
	e_mail_display_set_parts_list (
		E_MAIL_DISPLAY (emp->priv->webview), emp->priv->parts_list);
	webkit_web_view_load_uri (emp->priv->webview, mail_uri);
264
265
266
267
268
269

	g_free (mail_uri);
}

static void
set_header_visible (EMailPrinter *emp,
Dan Vrátil's avatar
Dan Vrátil committed
270
                    EMailFormatterHeader *header,
271
272
273
274
275
276
277
278
279
                    gint index,
                    gboolean visible)
{
	WebKitDOMDocument *document;
	WebKitDOMNodeList *headers;
	WebKitDOMElement *element;
	WebKitDOMCSSStyleDeclaration *style;

	document = webkit_web_view_get_dom_document (emp->priv->webview);
Matthew Barnes's avatar
Matthew Barnes committed
280
	headers = webkit_dom_document_get_elements_by_class_name (document, "header-item");
281
282
283
284
285

	g_return_if_fail (index < webkit_dom_node_list_get_length (headers));

	element = WEBKIT_DOM_ELEMENT (webkit_dom_node_list_item (headers, index));
	style = webkit_dom_element_get_style (element);
286
287
	webkit_dom_css_style_declaration_set_property (
		style,
Matthew Barnes's avatar
Matthew Barnes committed
288
		"display", (visible ? "table-row" : "none"), "", NULL);
289
290
291
292
293
294
295
296
297
298
}

static void
header_active_renderer_toggled_cb (GtkCellRendererToggle *renderer,
                                   gchar *path,
                                   EMailPrinter *emp)
{
	GtkTreeIter iter;
	GtkTreePath *p;
	gboolean active;
Dan Vrátil's avatar
Dan Vrátil committed
299
	EMailFormatterHeader *header;
300
301
	gint *indices;

302
303
	gtk_tree_model_get_iter_from_string (
		GTK_TREE_MODEL (emp->priv->headers),
304
305
		&iter, path);

306
307
	gtk_tree_model_get (
		GTK_TREE_MODEL (emp->priv->headers), &iter,
308
		COLUMN_ACTIVE, &active, -1);
309
310
	gtk_tree_model_get (
		GTK_TREE_MODEL (emp->priv->headers), &iter,
311
		COLUMN_HEADER_STRUCT, &header, -1);
312
313
	gtk_list_store_set (
		GTK_LIST_STORE (emp->priv->headers), &iter,
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
		COLUMN_ACTIVE, !active, -1);

	p = gtk_tree_path_new_from_string (path);
	indices = gtk_tree_path_get_indices (p);
	set_header_visible (emp, header, indices[0], !active);
	gtk_tree_path_free (p);
}

static void
emp_headers_tab_toggle_selection (GtkWidget *button,
                                  gpointer user_data)
{
	EMailPrinter *emp = user_data;
	GtkTreeIter iter;
	gboolean select;

	if (button == emp->priv->buttons[BUTTON_SELECT_ALL])
		select = TRUE;
	else if (button == emp->priv->buttons[BUTTON_SELECT_NONE])
		select = FALSE;
	else
		return;

	if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (emp->priv->headers), &iter))
		return;

	do {
Dan Vrátil's avatar
Dan Vrátil committed
341
		EMailFormatterHeader *header;
342
343
344
		GtkTreePath *path;
		gint *indices;

345
346
		gtk_tree_model_get (
			GTK_TREE_MODEL (emp->priv->headers), &iter,
347
			COLUMN_HEADER_STRUCT, &header, -1);
348
349
		gtk_list_store_set (
			GTK_LIST_STORE (emp->priv->headers), &iter,
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
			COLUMN_ACTIVE, select, -1);

		path = gtk_tree_model_get_path (GTK_TREE_MODEL (emp->priv->headers), &iter);
		indices = gtk_tree_path_get_indices (path);
		set_header_visible (emp, header, indices[0], select);
		gtk_tree_path_free (path);

	} while (gtk_tree_model_iter_next (GTK_TREE_MODEL (emp->priv->headers), &iter));
}

static void
emp_headers_tab_selection_changed (GtkTreeSelection *selection,
                                   gpointer user_data)
{
	EMailPrinter *emp = user_data;
	gboolean enabled;
	GList *selected_rows;
	GtkTreeIter iter;
	GtkTreeModel *model;
	GtkTreePath *path;

	if (gtk_tree_selection_count_selected_rows (selection) == 0) {
		gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_TOP], FALSE);
		gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_UP], FALSE);
		gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_DOWN], FALSE);
		gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_BOTTOM], FALSE);

		return;
	}

	model = GTK_TREE_MODEL (emp->priv->headers);
	selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);

	path = gtk_tree_path_copy (selected_rows->data);
	enabled = gtk_tree_path_prev (path);
	gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_TOP], enabled);
	gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_UP], enabled);

	gtk_tree_model_get_iter (model, &iter, g_list_last (selected_rows)->data);
	enabled = gtk_tree_model_iter_next (model, &iter);
	gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_DOWN], enabled);
	gtk_widget_set_sensitive (emp->priv->buttons[BUTTON_BOTTOM], enabled);

	g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
	g_list_free (selected_rows);
	gtk_tree_path_free (path);
}

static void
emp_headers_tab_move (GtkWidget *button,
                      gpointer user_data)
{
	EMailPrinter *emp = user_data;
	GtkTreeSelection *selection;
	GList *selected_rows, *references, *l;
	GtkTreePath *path;
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkTreeRowReference *selection_middle;
	gint *indices;

	WebKitDOMDocument *document;
	WebKitDOMNodeList *headers;
	WebKitDOMNode *header, *parent;

	model = GTK_TREE_MODEL (emp->priv->headers);
	selection = gtk_tree_view_get_selection  (GTK_TREE_VIEW (emp->priv->treeview));
	selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);

        /* The order of header rows in the HMTL document should be in sync with
	   order of headers in the listview and in efhp->headers_list */
	document = webkit_web_view_get_dom_document (emp->priv->webview);
Matthew Barnes's avatar
Matthew Barnes committed
422
	headers = webkit_dom_document_get_elements_by_class_name (document, "header-item");
423
424
425
426
427
428

	l = g_list_nth (selected_rows, g_list_length (selected_rows) / 2);
	selection_middle = gtk_tree_row_reference_new (model, l->data);

	references = NULL;
	for (l = selected_rows; l; l = l->next) {
429
430
		references = g_list_prepend (
			references,
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
			gtk_tree_row_reference_new (model, l->data));
	}

	if (button == emp->priv->buttons[BUTTON_TOP]) {

		for (l = references; l; l = l->next) {
                        /* Move the rows in the view  */
			path = gtk_tree_row_reference_get_path (l->data);
			gtk_tree_model_get_iter (model, &iter, path);
			gtk_list_store_move_after (emp->priv->headers, &iter, NULL);

                        /* Move the header row in HTML document */
			indices = gtk_tree_path_get_indices (path);
			header = webkit_dom_node_list_item (headers, indices[0]);
			parent = webkit_dom_node_get_parent_node (header);
			webkit_dom_node_remove_child (parent, header, NULL);
			webkit_dom_node_insert_before (parent, header,
				webkit_dom_node_get_first_child (parent), NULL);

			gtk_tree_path_free (path);
		}

       } else if (button == emp->priv->buttons[BUTTON_UP]) {

		GtkTreeIter *iter_prev;
		WebKitDOMNode *node2;

		references = g_list_reverse (references);

		for (l = references; l; l = l->next) {

			path = gtk_tree_row_reference_get_path (l->data);
			gtk_tree_model_get_iter (model, &iter, path);
			iter_prev = gtk_tree_iter_copy (&iter);
			gtk_tree_model_iter_previous (model, iter_prev);

			gtk_list_store_move_before (emp->priv->headers, &iter, iter_prev);

			indices = gtk_tree_path_get_indices (path);
			header = webkit_dom_node_list_item (headers, indices[0]);
			node2 = webkit_dom_node_get_previous_sibling (header);
			parent = webkit_dom_node_get_parent_node (header);

			webkit_dom_node_remove_child (parent, header, NULL);
			webkit_dom_node_insert_before (parent, header, node2, NULL);

			gtk_tree_path_free (path);
			gtk_tree_iter_free (iter_prev);
		}

	} else if (button == emp->priv->buttons[BUTTON_DOWN]) {

		GtkTreeIter *iter_next;
		WebKitDOMNode *node2;

		for (l = references; l; l = l->next) {

			path = gtk_tree_row_reference_get_path (l->data);
			gtk_tree_model_get_iter (model, &iter, path);
			iter_next = gtk_tree_iter_copy (&iter);
			gtk_tree_model_iter_next (model, iter_next);

			gtk_list_store_move_after (emp->priv->headers, &iter, iter_next);

			indices = gtk_tree_path_get_indices (path);
			header = webkit_dom_node_list_item (headers, indices[0]);
			node2 = webkit_dom_node_get_next_sibling (header);
			parent = webkit_dom_node_get_parent_node (header);

			webkit_dom_node_remove_child (parent, header, NULL);
			webkit_dom_node_insert_before (parent, header,
				webkit_dom_node_get_next_sibling (node2), NULL);

			gtk_tree_path_free (path);
			gtk_tree_iter_free (iter_next);
		}

	} else if (button == emp->priv->buttons[BUTTON_BOTTOM]) {

		references = g_list_reverse (references);

		for (l = references; l; l = l->next) {
			path = gtk_tree_row_reference_get_path (l->data);
			gtk_tree_model_get_iter (model, &iter, path);
			gtk_list_store_move_before (emp->priv->headers, &iter, NULL);

                        /* Move the header row in HTML document */
			indices = gtk_tree_path_get_indices (path);
			header = webkit_dom_node_list_item (headers, indices[0]);
			parent = webkit_dom_node_get_parent_node (header);
			webkit_dom_node_remove_child (parent, header, NULL);
			webkit_dom_node_append_child (parent, header, NULL);

			gtk_tree_path_free (path);
		}
	};

	g_list_foreach (references, (GFunc) gtk_tree_row_reference_free, NULL);
	g_list_free (references);

        /* Keep the selection in middle of the screen */
	path = gtk_tree_row_reference_get_path (selection_middle);
533
534
	gtk_tree_view_scroll_to_cell (
		GTK_TREE_VIEW (emp->priv->treeview),
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
		path, COLUMN_ACTIVE, TRUE, 0.5, 0.5);
	gtk_tree_path_free (path);
	gtk_tree_row_reference_free (selection_middle);

	g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
	g_list_free (selected_rows);

	emp_headers_tab_selection_changed (selection, user_data);
}

static GtkWidget *
emp_create_headers_tab (GtkPrintOperation *operation,
                        EMailPrinter *emp)
{
	GtkWidget *vbox, *hbox, *scw, *button;
	GtkTreeView *view;
	GtkTreeSelection *selection;
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
	gtk_box_pack_end (GTK_BOX (hbox), vbox, FALSE, FALSE, 5);

	emp->priv->treeview = gtk_tree_view_new_with_model (
		GTK_TREE_MODEL (emp->priv->headers));
	view = GTK_TREE_VIEW (emp->priv->treeview);
	selection = gtk_tree_view_get_selection (view);
	gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
564
565
	g_signal_connect (
		selection, "changed",
566
567
568
		G_CALLBACK (emp_headers_tab_selection_changed), emp);

	renderer = gtk_cell_renderer_toggle_new ();
569
570
	g_signal_connect (
		renderer, "toggled",
571
572
		G_CALLBACK (header_active_renderer_toggled_cb), emp);
	column = gtk_tree_view_column_new_with_attributes (
573
		_("Print"), renderer,
574
575
576
577
578
		"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 (
579
		_("Header Name"), renderer,
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
		"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 (hbox), scw, TRUE, TRUE, 0);

	button = gtk_button_new_from_stock (GTK_STOCK_SELECT_ALL);
	emp->priv->buttons[BUTTON_SELECT_ALL] = button;
595
596
	g_signal_connect (
		button, "clicked",
597
598
599
600
601
		G_CALLBACK (emp_headers_tab_toggle_selection), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	button = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
	emp->priv->buttons[BUTTON_SELECT_NONE] = button;
602
603
	g_signal_connect (
		button, "clicked",
604
605
606
607
608
609
		G_CALLBACK (emp_headers_tab_toggle_selection), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	button = gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP);
	emp->priv->buttons[BUTTON_TOP] = button;
	gtk_widget_set_sensitive (button, FALSE);
610
611
	g_signal_connect (
		button, "clicked",
612
613
614
615
616
617
		G_CALLBACK (emp_headers_tab_move), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	button = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
	emp->priv->buttons[BUTTON_UP] = button;
	gtk_widget_set_sensitive (button, FALSE);
618
619
	g_signal_connect (
		button, "clicked",
620
621
622
623
624
625
		G_CALLBACK (emp_headers_tab_move), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	button = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
	emp->priv->buttons[BUTTON_DOWN] = button;
	gtk_widget_set_sensitive (button, FALSE);
626
627
	g_signal_connect (
		button, "clicked",
628
629
630
631
632
633
		G_CALLBACK (emp_headers_tab_move), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	button = gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM);
	emp->priv->buttons[BUTTON_BOTTOM] = button;
	gtk_widget_set_sensitive (button, FALSE);
634
635
	g_signal_connect (
		button, "clicked",
636
637
638
639
640
641
642
643
644
645
		G_CALLBACK (emp_headers_tab_move), emp);
	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, TRUE, 5);

	gtk_print_operation_set_custom_tab_label (operation, _("Headers"));
	gtk_widget_show_all (hbox);

	return hbox;
}

static void
Dan Vrátil's avatar
Dan Vrátil committed
646
647
emp_set_parts_list (EMailPrinter *emp,
                    EMailPartList *parts_list)
648
649
{
	CamelMediumHeader *header;
650
	CamelMimeMessage *message;
651
652
	GArray *headers;
	gint i;
653
	GtkTreeIter last_known = { 0 };
654

Dan Vrátil's avatar
Dan Vrátil committed
655
	g_return_if_fail (parts_list);
656

Dan Vrátil's avatar
Dan Vrátil committed
657
	emp->priv->parts_list = g_object_ref (parts_list);
658
659
660

	if (emp->priv->headers)
		g_object_unref (emp->priv->headers);
661
662
	emp->priv->headers = gtk_list_store_new (
		5,
663
664
		G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_INT);

665
666
	message = e_mail_part_list_get_message (parts_list);
	headers = camel_medium_get_headers (CAMEL_MEDIUM (message));
667
668
669
670
671
672
	if (!headers)
		return;

	for (i = 0; i < headers->len; i++) {
		GtkTreeIter iter;
		GList *found_header;
Dan Vrátil's avatar
Dan Vrátil committed
673
		EMailFormatterHeader *emfh;
674
675

		header = &g_array_index (headers, CamelMediumHeader, i);
Dan Vrátil's avatar
Dan Vrátil committed
676
		emfh = e_mail_formatter_header_new (header->name, header->value);
677

Dan Vrátil's avatar
Dan Vrátil committed
678
679
680
		found_header = g_queue_find_custom (
				(GQueue *) e_mail_formatter_get_headers (
					E_MAIL_FORMATTER (emp->priv->formatter)),
681
682
683
				emfh, (GCompareFunc) emp_header_name_equal);

		if (!found_header) {
Dan Vrátil's avatar
Dan Vrátil committed
684
685
686
			emfh->flags |= E_MAIL_FORMATTER_HEADER_FLAG_HIDDEN;
			e_mail_formatter_add_header_struct (
				E_MAIL_FORMATTER (emp->priv->formatter), emfh);
687
688
689
690
691
692
693
694
695
696
			gtk_list_store_append (emp->priv->headers, &iter);
		} else {
			if (gtk_list_store_iter_is_valid (emp->priv->headers, &last_known))
				gtk_list_store_insert_after (emp->priv->headers, &iter, &last_known);
			else
				gtk_list_store_insert_after (emp->priv->headers, &iter, NULL);

			last_known = iter;
		}

697
698
		gtk_list_store_set (
			emp->priv->headers, &iter,
699
700
701
702
703
704
			COLUMN_ACTIVE, (found_header != NULL),
			COLUMN_HEADER_NAME, emfh->name,
			COLUMN_HEADER_VALUE, emfh->value,
			COLUMN_HEADER_STRUCT, emfh, -1);
	}

705
	camel_medium_free_headers (CAMEL_MEDIUM (message), headers);
706
707
708
709
710
711
712
713
714
715
716
717
}

static void
emp_set_property (GObject *object,
                  guint property_id,
                  const GValue *value,
                  GParamSpec *pspec)
{
	EMailPrinter *emp = E_MAIL_PRINTER (object);

	switch (property_id) {

Dan Vrátil's avatar
Dan Vrátil committed
718
719
		case PROP_PART_LIST:
			emp_set_parts_list (emp, g_value_get_pointer (value));
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
			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) {

Dan Vrátil's avatar
Dan Vrátil committed
736
		case PROP_PART_LIST:
737
			g_value_set_pointer (value, emp->priv->parts_list);
738
739
740
741
742
743
744
745
746
747
748
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
emp_finalize (GObject *object)
{
	EMailPrinterPrivate *priv = E_MAIL_PRINTER (object)->priv;

Dan Vrátil's avatar
Dan Vrátil committed
749
750
751
	if (priv->formatter) {
		g_object_unref (priv->formatter);
		priv->formatter = NULL;
752
753
754
755
756
757
758
	}

	if (priv->headers) {
		GtkTreeIter iter;

		if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->headers), &iter)) {
			do {
Dan Vrátil's avatar
Dan Vrátil committed
759
				EMailFormatterHeader *header = NULL;
760
761
				gtk_tree_model_get (
					GTK_TREE_MODEL (priv->headers), &iter,
762
					COLUMN_HEADER_STRUCT, &header, -1);
Dan Vrátil's avatar
Dan Vrátil committed
763
				e_mail_formatter_header_free (header);
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
			} while (gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->headers), &iter));
		}
		g_object_unref (priv->headers);
		priv->headers = NULL;
	}

	if (priv->webview) {
		g_object_unref (priv->webview);
		priv->webview = NULL;
	}

	if (priv->uri) {
		g_free (priv->uri);
		priv->uri = NULL;
	}

	if (priv->operation) {
		g_object_unref (priv->operation);
		priv->operation = NULL;
	}

Dan Vrátil's avatar
Dan Vrátil committed
785
786
787
788
789
	if (priv->parts_list) {
		g_object_unref (priv->parts_list);
		priv->parts_list = NULL;
	}

790
        /* Chain up to parent's finalize() method. */
791
	G_OBJECT_CLASS (e_mail_printer_parent_class)->finalize (object);
792
793
794
}

static void
795
e_mail_printer_class_init (EMailPrinterClass *class)
796
797
798
{
	GObjectClass *object_class;

799
	g_type_class_add_private (class, sizeof (EMailPrinterPrivate));
800

801
	object_class = G_OBJECT_CLASS (class);
802
803
804
805
806
807
	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,
Dan Vrátil's avatar
Dan Vrátil committed
808
809
810
811
		PROP_PART_LIST,
		g_param_spec_pointer (
			"parts-list",
			"Parts List",
812
			NULL,
Dan Vrátil's avatar
Dan Vrátil committed
813
814
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT_ONLY));
815

816
817
818
	signals[SIGNAL_DONE] = g_signal_new (
		"done",
		G_TYPE_FROM_CLASS (class),
819
820
821
822
823
824
825
826
827
828
829
830
831
832
		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);

Dan Vrátil's avatar
Dan Vrátil committed
833
	emp->priv->formatter = (EMailFormatterPrint *) e_mail_formatter_print_new ();
834
835
836
837
838
	emp->priv->headers = NULL;
	emp->priv->webview = NULL;
}

EMailPrinter *
Dan Vrátil's avatar
Dan Vrátil committed
839
e_mail_printer_new (EMailPartList *source)
840
841
842
{
	EMailPrinter *emp;

843
844
	emp = g_object_new (
		E_TYPE_MAIL_PRINTER,
Dan Vrátil's avatar
Dan Vrátil committed
845
		"parts-list", source, NULL);
846
847
848
849
850
851

	return emp;
}

void
e_mail_printer_print (EMailPrinter *emp,
852
                      GtkPrintOperationAction action,
853
		      EMailFormatter *formatter,
854
855
856
857
858
859
860
                      GCancellable *cancellable)
{
	g_return_if_fail (E_IS_MAIL_PRINTER (emp));

	if (emp->priv->operation)
		g_object_unref (emp->priv->operation);
	emp->priv->operation = e_print_operation_new ();
861
	emp->priv->print_action = action;
862
863
864
	gtk_print_operation_set_unit (emp->priv->operation, GTK_UNIT_PIXEL);

	gtk_print_operation_set_show_progress (emp->priv->operation, TRUE);
865
866
	g_signal_connect (
		emp->priv->operation, "create-custom-widget",
867
		G_CALLBACK (emp_create_headers_tab), emp);
868
869
	g_signal_connect (
		emp->priv->operation, "done",
870
		G_CALLBACK (emp_printing_done), emp);
871
872
	g_signal_connect (
		emp->priv->operation, "draw-page",
873
874
875
		G_CALLBACK (emp_draw_footer), NULL);

	if (cancellable)
876
877
		g_signal_connect_swapped (
			cancellable, "cancelled",
878
879
			G_CALLBACK (gtk_print_operation_cancel), emp->priv->operation);

880
	emp_run_print_operation (emp, formatter);
881
882
883
884
885
886
887
}

const gchar *
e_mail_printer_get_export_filename (EMailPrinter *printer)
{
	g_return_val_if_fail (E_IS_MAIL_PRINTER (printer), NULL);

Dan Vrátil's avatar
Dan Vrátil committed
888
	return printer->priv->export_filename;
889
890
891
892
893
894
895
896
}

void
e_mail_printer_set_export_filename (EMailPrinter *printer,
                                    const gchar *filename)
{
	g_return_if_fail (E_IS_MAIL_PRINTER (printer));

Dan Vrátil's avatar
Dan Vrátil committed
897
898
	if (printer->priv->export_filename)
	  g_free (printer->priv->export_filename);
899

Dan Vrátil's avatar
Dan Vrátil committed
900
	printer->priv->export_filename = g_strdup (filename);
901
}