e-mail-reader-utils.c 55.2 KB
Newer Older
Matthew Barnes's avatar
Matthew Barnes committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * e-mail-reader-utils.c
 *
 * 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
15
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
Matthew Barnes's avatar
Matthew Barnes committed
16
17
18
19
20
21
22
23
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/* Miscellaneous utility functions used by EMailReader actions. */

24
25
26
27
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Matthew Barnes's avatar
Matthew Barnes committed
28
29
30
#include "e-mail-reader-utils.h"

#include <glib/gi18n.h>
31
#include <libxml/tree.h>
Matthew Barnes's avatar
Matthew Barnes committed
32
#include <gtkhtml/gtkhtml.h>
33
#include <camel/camel.h>
Matthew Barnes's avatar
Matthew Barnes committed
34

Matthew Barnes's avatar
Matthew Barnes committed
35
#include "shell/e-shell-utils.h"
Matthew Barnes's avatar
Matthew Barnes committed
36

37
38
39
40
#include "libemail-engine/e-mail-folder-utils.h"
#include "libemail-engine/e-mail-utils.h"
#include "libemail-engine/mail-ops.h"
#include "libemail-engine/mail-tools.h"
41

42
43
#include "em-format/e-mail-parser.h"
#include "em-format/e-mail-part-utils.h"
44

45
#include "composer/e-composer-actions.h"
Matthew Barnes's avatar
Matthew Barnes committed
46

47
48
49
50
51
52
53
54
55
#include "e-mail-backend.h"
#include "e-mail-browser.h"
#include "e-mail-printer.h"
#include "e-mail-display.h"
#include "em-composer-utils.h"
#include "em-utils.h"
#include "mail-autofilter.h"
#include "mail-vfolder-ui.h"
#include "message-list.h"
Dan Vrátil's avatar
Dan Vrátil committed
56
57
58

#define d(x)

59
60
61
62
typedef struct _AsyncContext AsyncContext;

struct _AsyncContext {
	EActivity *activity;
63
	CamelFolder *folder;
64
65
	CamelMimeMessage *message;
	EMailPartList *part_list;
66
	EMailReader *reader;
67
	CamelInternetAddress *address;
68
	gchar *folder_name;
69
70
	gchar *message_uid;

71
72
	EMailReplyType reply_type;
	EMailReplyStyle reply_style;
73
	GtkPrintOperationAction print_action;
74
75
	const gchar *filter_source;
	gint filter_type;
76
77
78
79
80
81
82
83
};

static void
async_context_free (AsyncContext *context)
{
	if (context->activity != NULL)
		g_object_unref (context->activity);

84
85
86
	if (context->folder != NULL)
		g_object_unref (context->folder);

87
88
89
90
91
92
	if (context->message != NULL)
		g_object_unref (context->message);

	if (context->part_list != NULL)
		g_object_unref (context->part_list);

93
94
95
	if (context->reader != NULL)
		g_object_unref (context->reader);

96
97
98
	if (context->address != NULL)
		g_object_unref (context->address);

99
	g_free (context->folder_name);
100
101
	g_free (context->message_uid);

102
103
104
	g_slice_free (AsyncContext, context);
}

Matthew Barnes's avatar
Matthew Barnes committed
105
106
107
108
109
110
111
112
113
114
static gboolean
mail_reader_is_special_local_folder (const gchar *name)
{
	return (strcmp (name, "Drafts") == 0 ||
		strcmp (name, "Inbox") == 0 ||
		strcmp (name, "Outbox") == 0 ||
		strcmp (name, "Sent") == 0 ||
		strcmp (name, "Templates") == 0);
}

Matthew Barnes's avatar
Matthew Barnes committed
115
116
117
118
gboolean
e_mail_reader_confirm_delete (EMailReader *reader)
{
	EShell *shell;
119
	EMailBackend *backend;
120
	EShellBackend *shell_backend;
Matthew Barnes's avatar
Matthew Barnes committed
121
122
	EShellSettings *shell_settings;
	CamelFolder *folder;
Matthew Barnes's avatar
Matthew Barnes committed
123
	CamelStore *parent_store;
Matthew Barnes's avatar
Matthew Barnes committed
124
	GtkWidget *check_button;
125
	GtkWidget *container;
Matthew Barnes's avatar
Matthew Barnes committed
126
127
128
129
130
131
132
133
134
135
	GtkWidget *dialog;
	GtkWindow *window;
	const gchar *label;
	gboolean prompt_delete_in_vfolder;
	gint response;

	/* Remind users what deleting from a search folder does. */

	g_return_val_if_fail (E_IS_MAIL_READER (reader), FALSE);

136
	backend = e_mail_reader_get_backend (reader);
137
	folder = e_mail_reader_get_folder (reader);
Matthew Barnes's avatar
Matthew Barnes committed
138
139
	window = e_mail_reader_get_window (reader);

140
	shell_backend = E_SHELL_BACKEND (backend);
141
	shell = e_shell_backend_get_shell (shell_backend);
Matthew Barnes's avatar
Matthew Barnes committed
142
143
	shell_settings = e_shell_get_shell_settings (shell);

Matthew Barnes's avatar
Matthew Barnes committed
144
145
	prompt_delete_in_vfolder = e_shell_settings_get_boolean (
		shell_settings, "mail-prompt-delete-in-vfolder");
Matthew Barnes's avatar
Matthew Barnes committed
146

Matthew Barnes's avatar
Matthew Barnes committed
147
148
149
	parent_store = camel_folder_get_parent_store (folder);

	if (!CAMEL_IS_VEE_STORE (parent_store))
Matthew Barnes's avatar
Matthew Barnes committed
150
151
152
153
154
		return TRUE;

	if (!prompt_delete_in_vfolder)
		return TRUE;

155
	dialog = e_alert_dialog_new_for_args (
Matthew Barnes's avatar
Matthew Barnes committed
156
		window, "mail:ask-delete-vfolder-msg",
Matthew Barnes's avatar
Matthew Barnes committed
157
		camel_folder_get_full_name (folder), NULL);
Matthew Barnes's avatar
Matthew Barnes committed
158

159
	container = e_alert_dialog_get_content_area (E_ALERT_DIALOG (dialog));
Matthew Barnes's avatar
Matthew Barnes committed
160

161
	label = _("Do not warn me again");
Matthew Barnes's avatar
Matthew Barnes committed
162
	check_button = gtk_check_button_new_with_label (label);
163
	gtk_box_pack_start (GTK_BOX (container), check_button, TRUE, TRUE, 6);
164
	gtk_widget_show (check_button);
Matthew Barnes's avatar
Matthew Barnes committed
165
166
167

	response = gtk_dialog_run (GTK_DIALOG (dialog));

168
	if (response != GTK_RESPONSE_DELETE_EVENT)
Matthew Barnes's avatar
Matthew Barnes committed
169
		e_shell_settings_set_boolean (
Matthew Barnes's avatar
Matthew Barnes committed
170
171
			shell_settings,
			"mail-prompt-delete-in-vfolder",
172
			!gtk_toggle_button_get_active (
Matthew Barnes's avatar
Matthew Barnes committed
173
			GTK_TOGGLE_BUTTON (check_button)));
Matthew Barnes's avatar
Matthew Barnes committed
174
175
176
177
178
179

	gtk_widget_destroy (dialog);

	return (response == GTK_RESPONSE_OK);
}

Matthew Barnes's avatar
Matthew Barnes committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
static void
mail_reader_delete_folder_cb (CamelFolder *folder,
                              GAsyncResult *result,
                              AsyncContext *context)
{
	EAlertSink *alert_sink;
	GError *error = NULL;

	alert_sink = e_activity_get_alert_sink (context->activity);

	e_mail_folder_remove_finish (folder, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-delete-folder",
			camel_folder_get_full_name (folder),
			error->message, NULL);
		g_error_free (error);
201
202
203
204

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
Matthew Barnes's avatar
Matthew Barnes committed
205
206
207
208
209
210
211
212
213
214
215
	}

	async_context_free (context);
}

void
e_mail_reader_delete_folder (EMailReader *reader,
                             CamelFolder *folder)
{
	EMailBackend *backend;
	EMailSession *session;
216
	EShell *shell;
Matthew Barnes's avatar
Matthew Barnes committed
217
	EAlertSink *alert_sink;
218
	CamelStore *parent_store;
219
	CamelProvider *provider;
Matthew Barnes's avatar
Matthew Barnes committed
220
221
222
	MailFolderCache *folder_cache;
	GtkWindow *parent = e_shell_get_active_window (NULL);
	GtkWidget *dialog;
223
	gboolean store_is_local;
224
	const gchar *display_name;
Matthew Barnes's avatar
Matthew Barnes committed
225
226
227
228
229
230
231
232
	const gchar *full_name;
	CamelFolderInfoFlags flags = 0;
	gboolean have_flags;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_FOLDER (folder));

	full_name = camel_folder_get_full_name (folder);
233
	display_name = camel_folder_get_display_name (folder);
Matthew Barnes's avatar
Matthew Barnes committed
234
	parent_store = camel_folder_get_parent_store (folder);
235
	provider = camel_service_get_provider (CAMEL_SERVICE (parent_store));
Matthew Barnes's avatar
Matthew Barnes committed
236

237
	store_is_local = (provider->flags & CAMEL_PROVIDER_IS_LOCAL) != 0;
238

Matthew Barnes's avatar
Matthew Barnes committed
239
240
241
242
243
244
	backend = e_mail_reader_get_backend (reader);
	session = e_mail_backend_get_session (backend);

	alert_sink = e_mail_reader_get_alert_sink (reader);
	folder_cache = e_mail_session_get_folder_cache (session);

245
	if (store_is_local &&
Matthew Barnes's avatar
Matthew Barnes committed
246
		mail_reader_is_special_local_folder (full_name)) {
247
248
249
		e_alert_submit (
			alert_sink, "mail:no-delete-special-folder",
			display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
250
251
252
		return;
	}

253
254
	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));

Matthew Barnes's avatar
Matthew Barnes committed
255
	if (!store_is_local && !e_shell_get_online (shell))
256
257
258
259
260
261
262
	{
		e_alert_submit (
			alert_sink, "mail:online-operation",
			display_name, NULL);
		return;
	}

Matthew Barnes's avatar
Matthew Barnes committed
263
264
265
266
267
268
	have_flags = mail_folder_cache_get_folder_info_flags (
		folder_cache, folder, &flags);

	if (have_flags && (flags & CAMEL_FOLDER_SYSTEM)) {
		e_alert_submit (
			alert_sink, "mail:no-delete-special-folder",
269
			display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
270
271
272
273
274
275
276
		return;
	}

	if (have_flags && (flags & CAMEL_FOLDER_CHILDREN)) {
		if (CAMEL_IS_VEE_STORE (parent_store))
			dialog = e_alert_dialog_new_for_args (
				parent, "mail:ask-delete-vfolder",
277
				display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
278
279
280
		else
			dialog = e_alert_dialog_new_for_args (
				parent, "mail:ask-delete-folder",
281
				display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
282
283
284
285
	} else {
		if (CAMEL_IS_VEE_STORE (parent_store))
			dialog = e_alert_dialog_new_for_args (
				parent, "mail:ask-delete-vfolder-nochild",
286
				display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
287
288
289
		else
			dialog = e_alert_dialog_new_for_args (
				parent, "mail:ask-delete-folder-nochild",
290
				display_name, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	}

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
		EActivity *activity;
		AsyncContext *context;
		GCancellable *cancellable;

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

		/* Disable the dialog until the activity finishes. */
		gtk_widget_set_sensitive (dialog, FALSE);

		/* Destroy the dialog once the activity finishes. */
		g_object_set_data_full (
			G_OBJECT (activity), "delete-dialog",
			dialog, (GDestroyNotify) gtk_widget_destroy);

		e_mail_folder_remove (
			folder, G_PRIORITY_DEFAULT,
			cancellable, (GAsyncReadyCallback)
			mail_reader_delete_folder_cb, context);
	} else
		gtk_widget_destroy (dialog);
}

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
static void
mail_reader_delete_folder_name_cb (GObject *source_object,
                                   GAsyncResult *result,
                                   gpointer user_data)
{
	CamelStore *store;
	CamelFolder *folder;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	store = CAMEL_STORE (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	/* XXX The returned CamelFolder is a borrowed reference. */
	folder = camel_store_get_folder_finish (store, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-delete-folder",
			context->folder_name, error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
		e_mail_reader_delete_folder (context->reader, folder);
	}

	async_context_free (context);
}

void
e_mail_reader_delete_folder_name (EMailReader *reader,
                                  CamelStore *store,
                                  const gchar *folder_name)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_STORE (store));
	g_return_if_fail (folder_name != NULL);

	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->folder_name = g_strdup (folder_name);

	camel_store_get_folder (
		store, folder_name,
		CAMEL_STORE_FOLDER_INFO_FAST,
		G_PRIORITY_DEFAULT, cancellable,
		mail_reader_delete_folder_name_cb,
		context);
}

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
422
423
424
425
426
427
428
429
430
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
/* Helper for e_mail_reader_expunge_folder() */
static void
mail_reader_expunge_folder_cb (GObject *source_object,
                               GAsyncResult *result,
                               gpointer user_data)
{
	CamelFolder *folder;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	folder = CAMEL_FOLDER (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-expunge-folder",
			camel_folder_get_display_name (folder),
			error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
	}

	async_context_free (context);
}

void
e_mail_reader_expunge_folder (EMailReader *reader,
                              CamelFolder *folder)
{
	GtkWindow *window;
	const gchar *display_name;
	gboolean proceed;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_FOLDER (folder));

	window = e_mail_reader_get_window (reader);
	display_name = camel_folder_get_display_name (folder);

	proceed = em_utils_prompt_user (
		window, "prompt-on-expunge",
		"mail:ask-expunge", display_name, NULL);

	if (proceed) {
		EActivity *activity;
		AsyncContext *context;
		GCancellable *cancellable;

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

		e_mail_folder_expunge (
			folder, G_PRIORITY_DEFAULT, cancellable,
			mail_reader_expunge_folder_cb, context);
	}
}

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
/* Helper for e_mail_reader_expunge_folder_name() */
static void
mail_reader_expunge_folder_name_cb (GObject *source_object,
                                    GAsyncResult *result,
                                    gpointer user_data)
{
	CamelStore *store;
	CamelFolder *folder;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	store = CAMEL_STORE (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	/* XXX The returned CamelFolder is a borrowed reference. */
	folder = camel_store_get_folder_finish (store, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-expunge-folder",
			context->folder_name, error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
		e_mail_reader_expunge_folder (context->reader, folder);
	}

	async_context_free (context);
}

void
e_mail_reader_expunge_folder_name (EMailReader *reader,
                                   CamelStore *store,
                                   const gchar *folder_name)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_STORE (store));
	g_return_if_fail (folder_name != NULL);

	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->folder_name = g_strdup (folder_name);

	camel_store_get_folder (
		store, folder_name,
		CAMEL_STORE_FOLDER_INFO_FAST,
		G_PRIORITY_DEFAULT, cancellable,
		mail_reader_expunge_folder_name_cb,
		context);
}

524
525
526
527
528
529
530
531
532
533
534
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* Helper for e_mail_reader_refresh_folder() */
static void
mail_reader_refresh_folder_cb (GObject *source_object,
                               GAsyncResult *result,
                               gpointer user_data)
{
	CamelFolder *folder;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	folder = CAMEL_FOLDER (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-refresh-folder",
			camel_folder_get_display_name (folder),
			error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
	}

	async_context_free (context);
}

void
e_mail_reader_refresh_folder (EMailReader *reader,
                              CamelFolder *folder)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_FOLDER (folder));

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

	camel_folder_refresh_info (
		folder, G_PRIORITY_DEFAULT, cancellable,
		mail_reader_refresh_folder_cb, context);
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* Helper for e_mail_reader_refresh_folder_name() */
static void
mail_reader_refresh_folder_name_cb (GObject *source_object,
                                    GAsyncResult *result,
                                    gpointer user_data)
{
	CamelStore *store;
	CamelFolder *folder;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	store = CAMEL_STORE (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	/* XXX The returned CamelFolder is a borrowed reference. */
	folder = camel_store_get_folder_finish (store, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:no-refresh-folder",
			context->folder_name, error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
		e_mail_reader_refresh_folder (context->reader, folder);
	}

	async_context_free (context);
}

void
e_mail_reader_refresh_folder_name (EMailReader *reader,
                                   CamelStore *store,
                                   const gchar *folder_name)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_STORE (store));
	g_return_if_fail (folder_name != NULL);

	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->folder_name = g_strdup (folder_name);

	camel_store_get_folder (
		store, folder_name,
		CAMEL_STORE_FOLDER_INFO_FAST,
		G_PRIORITY_DEFAULT, cancellable,
		mail_reader_refresh_folder_name_cb,
		context);
}

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* Helper for e_mail_reader_unsubscribe_folder_name() */
static void
mail_reader_unsubscribe_folder_name_cb (GObject *source_object,
                                        GAsyncResult *result,
                                        gpointer user_data)
{
	CamelSubscribable *subscribable;
	AsyncContext *context;
	EAlertSink *alert_sink;
	GError *error = NULL;

	subscribable = CAMEL_SUBSCRIBABLE (source_object);
	context = (AsyncContext *) user_data;

	alert_sink = e_activity_get_alert_sink (context->activity);

	camel_subscribable_unsubscribe_folder_finish (
		subscribable, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink, "mail:folder-unsubscribe",
			context->folder_name, error->message, NULL);
		g_error_free (error);

	} else {
		e_activity_set_state (
			context->activity, E_ACTIVITY_COMPLETED);
	}

	async_context_free (context);
}

void
e_mail_reader_unsubscribe_folder_name (EMailReader *reader,
                                       CamelStore *store,
                                       const gchar *folder_name)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;

	g_return_if_fail (E_IS_MAIL_READER (reader));
	g_return_if_fail (CAMEL_IS_SUBSCRIBABLE (store));
	g_return_if_fail (folder_name != NULL);

	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->folder_name = g_strdup (folder_name);

	camel_subscribable_unsubscribe_folder (
		CAMEL_SUBSCRIBABLE (store), folder_name,
		G_PRIORITY_DEFAULT, cancellable,
		mail_reader_unsubscribe_folder_name_cb,
		context);
}

Matthew Barnes's avatar
Matthew Barnes committed
712
713
714
715
716
717
718
719
720
721
722
guint
e_mail_reader_mark_selected (EMailReader *reader,
                             guint32 mask,
                             guint32 set)
{
	CamelFolder *folder;
	GPtrArray *uids;
	guint ii;

	g_return_val_if_fail (E_IS_MAIL_READER (reader), 0);

723
	folder = e_mail_reader_get_folder (reader);
Matthew Barnes's avatar
Matthew Barnes committed
724
725
726
727
728

	if (folder == NULL)
		return 0;

	camel_folder_freeze (folder);
729
	uids = e_mail_reader_get_selected_uids (reader);
Matthew Barnes's avatar
Matthew Barnes committed
730
731
732
733
734

	for (ii = 0; ii < uids->len; ii++)
		camel_folder_set_message_flags (
			folder, uids->pdata[ii], mask, set);

735
	em_utils_uids_free (uids);
Matthew Barnes's avatar
Matthew Barnes committed
736

Matthew Barnes's avatar
Matthew Barnes committed
737
738
739
740
	camel_folder_thaw (folder);

	return ii;
}
741
static void
742
743
copy_tree_state (EMailReader *src_reader,
                 EMailReader *des_reader)
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
{
	GtkWidget *src_mlist, *des_mlist;
	gchar *state;

	g_return_if_fail (src_reader != NULL);
	g_return_if_fail (des_reader != NULL);

	src_mlist = e_mail_reader_get_message_list (src_reader);
	if (!src_mlist)
		return;

	des_mlist = e_mail_reader_get_message_list (des_reader);
	if (!des_mlist)
		return;

	state = e_tree_get_state (E_TREE (src_mlist));
	if (state)
		e_tree_set_state (E_TREE (des_mlist), state);
	g_free (state);
763
764

	message_list_set_search (MESSAGE_LIST (des_mlist), MESSAGE_LIST (src_mlist)->search);
765
}
Matthew Barnes's avatar
Matthew Barnes committed
766
767
768
769

guint
e_mail_reader_open_selected (EMailReader *reader)
{
770
	EShell *shell;
771
	EMailBackend *backend;
772
	ESourceRegistry *registry;
Matthew Barnes's avatar
Matthew Barnes committed
773
774
775
776
777
778
779
780
	CamelFolder *folder;
	GtkWindow *window;
	GPtrArray *views;
	GPtrArray *uids;
	guint ii;

	g_return_val_if_fail (E_IS_MAIL_READER (reader), 0);

781
	backend = e_mail_reader_get_backend (reader);
782
783
784
	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));
	registry = e_shell_get_registry (shell);

785
786
	folder = e_mail_reader_get_folder (reader);
	uids = e_mail_reader_get_selected_uids (reader);
Matthew Barnes's avatar
Matthew Barnes committed
787
788
	window = e_mail_reader_get_window (reader);

789
	if (!em_utils_ask_open_many (window, uids->len)) {
790
		em_utils_uids_free (uids);
791
		return 0;
Matthew Barnes's avatar
Matthew Barnes committed
792
793
	}

794
795
796
	if (em_utils_folder_is_drafts (registry, folder) ||
		em_utils_folder_is_outbox (registry, folder) ||
		em_utils_folder_is_templates (registry, folder)) {
797
		em_utils_edit_messages (reader, folder, uids, TRUE);
Matthew Barnes's avatar
Matthew Barnes committed
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
		return uids->len;
	}

	views = g_ptr_array_new ();

	/* For vfolders we need to edit the original, not the vfolder copy. */
	for (ii = 0; ii < uids->len; ii++) {
		const gchar *uid = uids->pdata[ii];
		CamelFolder *real_folder;
		CamelMessageInfo *info;
		gchar *real_uid;

		if (!CAMEL_IS_VEE_FOLDER (folder)) {
			g_ptr_array_add (views, g_strdup (uid));
			continue;
		}

		info = camel_folder_get_message_info (folder, uid);
		if (info == NULL)
			continue;

		real_folder = camel_vee_folder_get_location (
			CAMEL_VEE_FOLDER (folder),
			(CamelVeeMessageInfo *) info, &real_uid);

823
824
		if (em_utils_folder_is_drafts (registry, real_folder) ||
			em_utils_folder_is_outbox (registry, real_folder)) {
Matthew Barnes's avatar
Matthew Barnes committed
825
826
827
828
			GPtrArray *edits;

			edits = g_ptr_array_new ();
			g_ptr_array_add (edits, real_uid);
829
			em_utils_edit_messages (
830
				reader, real_folder, edits, TRUE);
Matthew Barnes's avatar
Matthew Barnes committed
831
832
833
834
835
		} else {
			g_free (real_uid);
			g_ptr_array_add (views, g_strdup (uid));
		}

836
		camel_folder_free_message_info (folder, info);
Matthew Barnes's avatar
Matthew Barnes committed
837
838
839
840
841
	}

	for (ii = 0; ii < views->len; ii++) {
		const gchar *uid = views->pdata[ii];
		GtkWidget *browser;
842
		MessageList *ml;
Matthew Barnes's avatar
Matthew Barnes committed
843

844
845
846
		browser = e_mail_browser_new (
			backend, folder, uid,
			E_MAIL_FORMATTER_MODE_NORMAL);
847

848
		e_mail_reader_set_folder (E_MAIL_READER (browser), folder);
849
		e_mail_reader_set_message (E_MAIL_READER (browser), uid);
850

851
852
		ml = MESSAGE_LIST (e_mail_reader_get_message_list (
			E_MAIL_READER (browser)));
853
854
		message_list_freeze (ml);

855
		copy_tree_state (reader, E_MAIL_READER (browser));
856
857
858
		e_mail_reader_set_group_by_threads (
			E_MAIL_READER (browser),
			e_mail_reader_get_group_by_threads (reader));
859
860

		message_list_thaw (ml);
Matthew Barnes's avatar
Matthew Barnes committed
861
862
863
		gtk_widget_show (browser);
	}

864
	g_ptr_array_foreach (views, (GFunc) g_free, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
865
866
	g_ptr_array_free (views, TRUE);

867
	em_utils_uids_free (uids);
Matthew Barnes's avatar
Matthew Barnes committed
868
869
870
871

	return ii;
}

872
873
static gboolean
destroy_printing_activity (EActivity *activity)
874
{
875
	g_object_unref (activity);
876

877
878
	return FALSE;
}
879

880
881
882
883
884
885
886
static void
printing_done_cb (EMailPrinter *printer,
                  GtkPrintOperation *operation,
                  GtkPrintOperationResult result,
                  gpointer user_data)
{
	EActivity *activity = user_data;
887

888
	if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
889

890
891
		EAlertSink *alert_sink;
		GError *error = NULL;
892

893
894
		alert_sink = e_activity_get_alert_sink (activity);
		gtk_print_operation_get_error (operation, &error);
895

896
		if (error != NULL) {
897
898
			e_alert_submit (
				alert_sink, "mail:printing-failed",
899
900
901
				error->message, NULL);
			g_error_free (error);
		}
902

903
904
905
906
		g_object_unref (activity);
		g_object_unref (printer);
		return;
	}
907

908
909
910
	/* 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);
911
912
913
914

	/* We can't destroy the printer and associated WebKitWebView directly from
	 * here, because this callback is a handler of a WebKit's signal. This
	 * will destroy the printer later, together with the activity */
915
916
	g_object_set_data_full (
		G_OBJECT (activity),
917
918
		"printer", printer, (GDestroyNotify) g_object_unref);

919
920
	g_timeout_add_seconds_full (
		G_PRIORITY_DEFAULT, 3,
921
		(GSourceFunc) destroy_printing_activity, activity, NULL);
922
923
}

Dan Vrátil's avatar
Dan Vrátil committed
924
925
926
927
struct _MessagePrintingContext {
	EMailReader *reader;
	CamelFolder *folder;
	gchar *message_uid;
928
	GtkPrintOperationAction action;
Dan Vrátil's avatar
Dan Vrátil committed
929

Matthew Barnes's avatar
Matthew Barnes committed
930
	EActivity *activity;
Dan Vrátil's avatar
Dan Vrátil committed
931
};
Matthew Barnes's avatar
Matthew Barnes committed
932

Dan Vrátil's avatar
Dan Vrátil committed
933
934
935
936
static void
free_message_printing_context (struct _MessagePrintingContext *context)
{
	g_return_if_fail (context != NULL);
Matthew Barnes's avatar
Matthew Barnes committed
937

Dan Vrátil's avatar
Dan Vrátil committed
938
939
940
	g_clear_object (&context->reader);
	g_clear_object (&context->folder);
	g_clear_object (&context->activity);
941

Dan Vrátil's avatar
Dan Vrátil committed
942
943
944
945
946
947
948
	if (context->message_uid)
		g_free (context->message_uid);

	g_free (context);
}

static void
949
mail_reader_do_print_message (GObject *object,
950
                              GAsyncResult *result,
Dan Vrátil's avatar
Dan Vrátil committed
951
952
                              gpointer user_data)
{
953
	EMailReader *reader = E_MAIL_READER (object);
954
	EMailDisplay *mail_display;
Dan Vrátil's avatar
Dan Vrátil committed
955
956
957
	EActivity *activity;
	GCancellable *cancellable;
	EMailPrinter *printer;
958
	EMailPartList *part_list;
Dan Vrátil's avatar
Dan Vrátil committed
959
960
961
	struct _MessagePrintingContext *context = user_data;

	activity = e_mail_reader_new_activity (context->reader);
962
963
	e_activity_set_text (activity, _("Printing"));
	e_activity_set_state (activity, E_ACTIVITY_RUNNING);
Matthew Barnes's avatar
Matthew Barnes committed
964
965
	cancellable = e_activity_get_cancellable (activity);

966
967
	part_list = e_mail_reader_parse_message_finish (reader, result);

Dan Vrátil's avatar
Dan Vrátil committed
968
	printer = e_mail_printer_new (part_list);
969
970
	g_signal_connect (
		printer, "done",
971
		G_CALLBACK (printing_done_cb), activity);
972

973
974
975
976
977
	mail_display = e_mail_reader_get_mail_display (reader);

	e_mail_printer_print (printer, context->action,
		e_mail_display_get_formatter (mail_display),
		cancellable);
Dan Vrátil's avatar
Dan Vrátil committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

	free_message_printing_context (context);
}

static void
mail_reader_get_message_to_print_ready_cb (GObject *object,
                                           GAsyncResult *result,
                                           gpointer user_data)
{
	CamelMimeMessage *message;
	struct _MessagePrintingContext *context = user_data;

	message = camel_folder_get_message_finish (CAMEL_FOLDER (object), result, NULL);
	if (!CAMEL_IS_MIME_MESSAGE (message)) {
		free_message_printing_context (context);
		return;
	}

	/* "Retrieving message" activity (or NULL) */
	g_clear_object (&context->activity);

	e_mail_reader_parse_message (
		context->reader, context->folder, context->message_uid,
1001
		message, NULL, mail_reader_do_print_message, context);
Dan Vrátil's avatar
Dan Vrátil committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
}

void
e_mail_reader_print (EMailReader *reader,
                     GtkPrintOperationAction action)
{
	struct _MessagePrintingContext *context;
	MessageList *message_list;

	context = g_new0 (struct _MessagePrintingContext, 1);

	message_list = MESSAGE_LIST (e_mail_reader_get_message_list (reader));
	context->reader = g_object_ref (reader);
	context->message_uid = g_strdup (message_list->cursor_uid);
	context->folder = g_object_ref (e_mail_reader_get_folder (reader));
1017
	context->activity = e_mail_reader_new_activity (reader);
1018
	context->action = action;
Dan Vrátil's avatar
Dan Vrátil committed
1019
1020
1021

	g_return_if_fail (E_IS_MAIL_READER (reader));

1022
1023
1024
1025
1026
	camel_folder_get_message (
		context->folder, context->message_uid,
		G_PRIORITY_DEFAULT, e_activity_get_cancellable (context->activity),
		(GAsyncReadyCallback) mail_reader_get_message_to_print_ready_cb,
		context);
Matthew Barnes's avatar
Matthew Barnes committed
1027
1028
}

1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
static void
mail_reader_remove_attachments_cb (CamelFolder *folder,
                                   GAsyncResult *result,
                                   AsyncContext *context)
{
	EAlertSink *alert_sink;
	GError *error = NULL;

	alert_sink = e_mail_reader_get_alert_sink (context->reader);

	e_mail_folder_remove_attachments_finish (folder, result, &error);

	if (e_activity_handle_cancellation (context->activity, error)) {
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			alert_sink,
			"mail:remove-attachments",
			error->message, NULL);
		g_error_free (error);
	}

	async_context_free (context);
}

void
e_mail_reader_remove_attachments (EMailReader *reader)
{
	EActivity *activity;
	AsyncContext *context;
	GCancellable *cancellable;
	CamelFolder *folder;
	GPtrArray *uids;

	g_return_if_fail (E_IS_MAIL_READER (reader));

	folder = e_mail_reader_get_folder (reader);
	uids = e_mail_reader_get_selected_uids (reader);
	g_return_if_fail (uids != NULL);

	/* XXX Either e_mail_reader_get_selected_uids()
	 *     or MessageList should do this itself. */
	g_ptr_array_set_free_func (uids, (GDestroyNotify) g_free);

	/* Remove attachments asynchronously. */

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

	e_mail_folder_remove_attachments (
		folder, uids, G_PRIORITY_DEFAULT,
		cancellable, (GAsyncReadyCallback)
		mail_reader_remove_attachments_cb,
		context);

	g_ptr_array_unref (uids);
}

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
static void
mail_reader_remove_duplicates_cb (CamelFolder *folder,
                                  GAsyncResult *result,
                                  AsyncContext *context)
{
	EAlertSink *alert_sink;
	GHashTable *duplicates;
	GtkWindow *parent_window;
	guint n_duplicates;
	GError *error = NULL;

	alert_sink = e_mail_reader_get_alert_sink (context->reader);
	parent_window = e_mail_reader_get_window (context->reader);

	duplicates = e_mail_folder_find_duplicate_messages_finish (
		folder, result, &error);

1109
	if (e_activity_handle_cancellation (context->activity, error)) {
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
		g_warn_if_fail (duplicates == NULL);
		async_context_free (context);
		g_error_free (error);
		return;

	} else if (error != NULL) {
		g_warn_if_fail (duplicates == NULL);
		e_alert_submit (
			alert_sink,
			"mail:find-duplicate-messages",
			error->message, NULL);
		async_context_free (context);
		g_error_free (error);
		return;
	}

	g_return_if_fail (duplicates != NULL);

	/* Finalize the activity here so we don't leave a message in
	 * the task bar while prompting the user for confirmation. */
	e_activity_set_state (context->activity, E_ACTIVITY_COMPLETED);
	g_object_unref (context->activity);
	context->activity = NULL;

	n_duplicates = g_hash_table_size (duplicates);

	if (n_duplicates == 0) {
		em_utils_prompt_user (
			parent_window, NULL,
			"mail:info-no-remove-duplicates",
			camel_folder_get_display_name (folder), NULL);
	} else {
		gchar *confirmation;
		gboolean proceed;

		confirmation = g_strdup_printf (ngettext (
			/* Translators: %s is replaced with a folder
			 * name %u with count of duplicate messages. */
			"Folder '%s' contains %u duplicate message. "
			"Are you sure you want to delete it?",
			"Folder '%s' contains %u duplicate messages. "
			"Are you sure you want to delete them?",
			n_duplicates),
			camel_folder_get_display_name (folder),
			n_duplicates);

		proceed = em_utils_prompt_user (
			parent_window, NULL,
			"mail:ask-remove-duplicates",
			confirmation, NULL);

		if (proceed) {
			GHashTableIter iter;
			gpointer key;

			camel_folder_freeze (folder);

			g_hash_table_iter_init (&iter, duplicates);

			/* Mark duplicate messages for deletion. */
			while (g_hash_table_iter_next (&iter, &key, NULL))
				camel_folder_delete_message (folder, key);

			camel_folder_thaw (folder);
		}

		g_free (confirmation);
	}

	g_hash_table_destroy (duplicates);

	async_context_free (context);
}

void
e_mail_reader_remove_duplicates (EMailReader *reader)
{
1187
	EActivity *activity;
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
	AsyncContext *context;
	GCancellable *cancellable;
	CamelFolder *folder;
	GPtrArray *uids;

	g_return_if_fail (E_IS_MAIL_READER (reader));

	folder = e_mail_reader_get_folder (reader);
	uids = e_mail_reader_get_selected_uids (reader);
	g_return_if_fail (uids != NULL);

	/* XXX Either e_mail_reader_get_selected_uids()
	 *     or MessageList should do this itself. */
	g_ptr_array_set_free_func (uids, (GDestroyNotify) g_free);

	/* Find duplicate messages asynchronously. */

1205
1206
1207
	activity = e_mail_reader_new_activity (reader);
	cancellable = e_activity_get_cancellable (activity);

1208
	context = g_slice_new0 (AsyncContext);
1209
	context->activity = activity;
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
	context->reader = g_object_ref (reader);

	e_mail_folder_find_duplicate_messages (
		folder, uids, G_PRIORITY_DEFAULT,
		cancellable, (GAsyncReadyCallback)
		mail_reader_remove_duplicates_cb,
		context);

	g_ptr_array_unref (uids);
}

Matthew Barnes's avatar
Matthew Barnes committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
/* Helper for e_mail_reader_reply_to_message()
 * XXX This function belongs in e-html-utils.c */
static gboolean
html_contains_nonwhitespace (const gchar *html,
                             gint len)
{
	const gchar *cp;
	gunichar uc = 0;

	if (html == NULL || len <= 0)
		return FALSE;

	cp = html;

	while (cp != NULL && cp - html < len) {
		uc = g_utf8_get_char (cp);
		if (uc == 0)
			break;

		if (uc == '<') {
			/* skip until next '>' */
			uc = g_utf8_get_char (cp);
			while (uc != 0 && uc != '>' && cp - html < len) {
				cp = g_utf8_next_char (cp);
				uc = g_utf8_get_char (cp);
			}
			if (uc == 0)
				break;
		} else if (uc == '&') {
			/* sequence '&nbsp;' is a space */
			if (g_ascii_strncasecmp (cp, "&nbsp;", 6) == 0)
				cp = cp + 5;
			else
				break;
		} else if (!g_unichar_isspace (uc))
			break;

		cp = g_utf8_next_char (cp);
	}

	return cp - html < len - 1 && uc != 0;
}

Dan Vrátil's avatar
Dan Vrátil committed
1264
static void
1265
mail_reader_reply_message_parsed (GObject *object,
1266
                                  GAsyncResult *result,
Dan Vrátil's avatar
Dan Vrátil committed
1267
1268
1269
1270
                                  gpointer user_data)
{
	EShell *shell;
	EMailBackend *backend;
1271
1272
	EMailReader *reader = E_MAIL_READER (object);
	EMailPartList *part_list;
1273
	CamelMimeMessage *message;
Dan Vrátil's avatar
Dan Vrátil committed
1274
1275
	AsyncContext *context = user_data;

1276
	part_list = e_mail_reader_parse_message_finish (reader, result);
1277
	message = e_mail_part_list_get_message (part_list);
1278

Dan Vrátil's avatar
Dan Vrátil committed
1279
1280
1281
1282
	backend = e_mail_reader_get_backend (context->reader);
	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));

	em_utils_reply_to_message (
1283
		shell, message,
Dan Vrátil's avatar
Dan Vrátil committed
1284
1285
1286
1287
		context->folder, context->message_uid,
		context->reply_type, context->reply_style,
		part_list, context->address);

1288
	g_object_unref (part_list);
Dan Vrátil's avatar
Dan Vrátil committed
1289
1290
1291
	async_context_free (context);
}

1292
static void
1293
1294
1295
mail_reader_get_message_ready_cb (CamelFolder *folder,
                                  GAsyncResult *result,
                                  AsyncContext *context)
1296
{
1297
	EAlertSink *alert_sink;
1298
1299
1300
	CamelMimeMessage *message;
	GError *error = NULL;

1301
	alert_sink = e_mail_reader_get_alert_sink (context->reader);
1302
1303
1304

	message = camel_folder_get_message_finish (folder, result, &error);

1305
1306
1307
1308
1309
	if (e_activity_handle_cancellation (context->activity, error)) {
		g_warn_if_fail (message == NULL);
		async_context_free (context);
		g_error_free (error);
		return;
1310

1311
1312
	} else if (error != NULL) {
		g_warn_if_fail (message == NULL);
1313
1314
1315
		e_alert_submit (
			alert_sink, "mail:no-retrieve-message",
			error->message, NULL);
1316
1317
1318
		async_context_free (context);
		g_error_free (error);
		return;
1319
1320
	}

1321
1322
	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));

1323
1324
	e_mail_reader_parse_message (
		context->reader, context->folder,
1325
		context->message_uid, message, NULL,
1326
		mail_reader_reply_message_parsed, context);
1327
1328
}

Matthew Barnes's avatar
Matthew Barnes committed
1329
void
1330
1331
e_mail_reader_reply_to_message (EMailReader *reader,
                                CamelMimeMessage *src_message,
1332
                                EMailReplyType reply_type)
Matthew Barnes's avatar
Matthew Barnes committed
1333
{
1334
	EShell *shell;
1335
	EMailBackend *backend;
1336
	EShellBackend *shell_backend;
1337
	EMailDisplay *display;
1338
	EMailPartList *part_list = NULL;
1339
	GtkWidget *message_list;
Matthew Barnes's avatar
Matthew Barnes committed
1340
	CamelMimeMessage *new_message;
1341
	CamelInternetAddress *address = NULL;
Matthew Barnes's avatar
Matthew Barnes committed
1342
	CamelFolder *folder;
1343
	EMailReplyStyle reply_style;
1344
	EWebView *web_view;
Matthew Barnes's avatar
Matthew Barnes committed
1345
1346
1347
1348
	struct _camel_header_raw *header;
	const gchar *uid;
	gchar *selection = NULL;
	gint length;
Dan Vrátil's avatar
Dan Vrátil committed
1349
	gchar *mail_uri;
1350
	CamelObjectBag *registry;
1351
1352
	EMsgComposer *composer;
	guint32 validity_pgp_sum = 0, validity_smime_sum = 0;
Matthew Barnes's avatar
Matthew Barnes committed
1353
1354
1355
1356
1357
1358
1359

	/* This handles quoting only selected text in the reply.  If
	 * nothing is selected or only whitespace is selected, fall
	 * back to the normal em_utils_reply_to_message(). */

	g_return_if_fail (E_IS_MAIL_READER (reader));

1360
1361
	backend = e_mail_reader_get_backend (reader);
	folder = e_mail_reader_get_folder (reader);
1362
	display = e_mail_reader_get_mail_display (reader);
1363
	message_list = e_mail_reader_get_message_list (reader);
1364
	reply_style = e_mail_reader_get_reply_style (reader);
1365
1366

	shell_backend = E_SHELL_BACKEND (backend);
1367
1368
	shell = e_shell_backend_get_shell (shell_backend);

1369
	web_view = E_WEB_VIEW (display);
Matthew Barnes's avatar
Matthew Barnes committed
1370

1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
	if (reply_type == E_MAIL_REPLY_TO_RECIPIENT) {
		const gchar *uri;

		uri = e_web_view_get_selected_uri (web_view);

		if (uri) {
			CamelURL *curl;

			curl = camel_url_new (uri, NULL);

			if (curl && curl->path && *curl->path) {
				address = camel_internet_address_new ();
1383
1384
1385
				if (camel_address_decode (
						CAMEL_ADDRESS (address),
						curl->path) < 0) {
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
					g_object_unref (address);
					address = NULL;
				}
			}

			if (curl)
				camel_url_free (curl);
		}
	}

1396
	uid = MESSAGE_LIST (message_list)->cursor_uid;
Matthew Barnes's avatar
Matthew Barnes committed
1397
1398
	g_return_if_fail (uid != NULL);

Dan Vrátil's avatar
Dan Vrátil committed
1399
1400
1401
	if (!gtk_widget_get_visible (GTK_WIDGET (web_view)))
		goto whole_message;

1402
	registry = e_mail_part_list_get_registry ();
Dan Vrátil's avatar
Dan Vrátil committed
1403
	mail_uri = e_mail_part_build_uri (folder, uid, NULL, NULL);
1404
	part_list = camel_object_bag_get (registry, mail_uri);
Dan Vrátil's avatar
Dan Vrátil committed
1405
1406
	g_free (mail_uri);

1407
	if (!part_list) {
1408
		goto whole_message;
1409
	} else {
1410
1411
1412
		GQueue queue = G_QUEUE_INIT;

		e_mail_part_list_queue_parts (part_list, NULL, &queue);
1413

1414
1415
		while (!g_queue_is_empty (&queue)) {
			EMailPart *part = g_queue_pop_head (&queue);
1416

1417
			if (part->validities) {
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
				GSList *viter;

				for (viter = part->validities; viter; viter = viter->next) {
					EMailPartValidityPair *vpair = viter->data;

					if (vpair) {
						if ((vpair->validity_type & E_MAIL_PART_VALIDITY_PGP) != 0)
							validity_pgp_sum |= vpair->validity_type;
						if ((vpair->validity_type & E_MAIL_PART_VALIDITY_SMIME) != 0)
							validity_smime_sum |= vpair->validity_type;
					}
				}
			}
1431
1432

			e_mail_part_unref (part);
1433
1434
		}
	}
1435

1436
	if (src_message == NULL) {
1437
		src_message = e_mail_part_list_get_message (part_list);
1438
1439
		if (src_message != NULL)
			g_object_ref (src_message);
1440

1441
		g_object_unref (part_list);
1442
		part_list = NULL;
1443

1444
		g_return_if_fail (src_message != NULL);
1445
1446
	} else {
		g_object_unref (part_list);
1447
		part_list = NULL;
1448
	}
1449

1450
	if (!e_web_view_is_selection_active (web_view))
Matthew Barnes's avatar
Matthew Barnes committed
1451
1452
		goto whole_message;

1453
	selection = e_web_view_get_selection_html (web_view);
Matthew Barnes's avatar
Matthew Barnes committed
1454
1455
1456
	if (selection == NULL || *selection == '\0')
		goto whole_message;

1457
	length = strlen (selection);
Matthew Barnes's avatar
Matthew Barnes committed
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
	if (!html_contains_nonwhitespace (selection, length))
		goto whole_message;

	new_message = camel_mime_message_new ();

	/* Filter out "content-*" headers. */
	header = CAMEL_MIME_PART (src_message)->headers;
	while (header != NULL) {
		if (g_ascii_strncasecmp (header->name, "content-", 8) != 0)
			camel_medium_add_header (
				CAMEL_MEDIUM (new_message),
				header->name, header->value);
Milan Crha's avatar
Milan Crha committed
1470
1471

		header = header->next;
Matthew Barnes's avatar
Matthew Barnes committed
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
	}

	camel_mime_part_set_encoding (
		CAMEL_MIME_PART (new_message),
		CAMEL_TRANSFER_ENCODING_8BIT);

	camel_mime_part_set_content (
		CAMEL_MIME_PART (new_message),
		selection, length, "text/html");

1482
1483
	g_object_unref (src_message);

1484
	composer = em_utils_reply_to_message (
1485
		shell, new_message, folder, uid,
1486
		reply_type, reply_style, NULL, address);
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
	if (composer && (validity_pgp_sum != 0 || validity_smime_sum != 0)) {
		GtkToggleAction *action;

		if ((validity_pgp_sum & E_MAIL_PART_VALIDITY_PGP) != 0) {
			if ((validity_pgp_sum & E_MAIL_PART_VALIDITY_SIGNED) != 0) {
				action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_PGP_SIGN (composer));
				gtk_toggle_action_set_active (action, TRUE);
			}

			if ((validity_pgp_sum & E_MAIL_PART_VALIDITY_ENCRYPTED) != 0) {
				action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_PGP_ENCRYPT (composer));
				gtk_toggle_action_set_active (action, TRUE);
			}
		}

		if ((validity_smime_sum & E_MAIL_PART_VALIDITY_SMIME) != 0) {
			if ((validity_smime_sum & E_MAIL_PART_VALIDITY_SIGNED) != 0) {
				action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_SMIME_SIGN (composer));
				gtk_toggle_action_set_active (action, TRUE);
			}

			if ((validity_smime_sum & E_MAIL_PART_VALIDITY_ENCRYPTED) != 0) {
				action = GTK_TOGGLE_ACTION (E_COMPOSER_ACTION_SMIME_ENCRYPT (composer));
				gtk_toggle_action_set_active (action, TRUE);
			}
		}
	}
1514
1515
1516

	if (address)
		g_object_unref (address);
Matthew Barnes's avatar
Matthew Barnes committed
1517

1518
1519
	g_object_unref (new_message);