em-composer-utils.c 141 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

Not Zed's avatar
Not Zed committed
3
/*
4 5 6
 * 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.
Not Zed's avatar
Not Zed committed
7
 *
8 9 10 11
 * 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 General Public License
 * for more details.
Not Zed's avatar
Not Zed committed
12
 *
13 14
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Not Zed's avatar
Not Zed committed
15 16
 *
 *
17 18 19 20
 * Authors:
 *		Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
Not Zed's avatar
Not Zed committed
21 22 23
 *
 */

Milan Crha's avatar
Milan Crha committed
24
#include "evolution-config.h"
Not Zed's avatar
Not Zed committed
25

26
#include <locale.h>
Not Zed's avatar
Not Zed committed
27
#include <string.h>
28
#include <gtk/gtk.h>
29
#include <glib/gi18n.h>
30

31 32
#include <e-util/e-util.h>

33
#include <libemail-engine/libemail-engine.h>
Not Zed's avatar
Not Zed committed
34

Dan Vrátil's avatar
Dan Vrátil committed
35
#include <em-format/e-mail-parser.h>
36
#include <em-format/e-mail-part-utils.h>
Dan Vrátil's avatar
Dan Vrátil committed
37 38
#include <em-format/e-mail-formatter-quote.h>

39
#include <shell/e-shell.h>
40

41 42 43
#include <composer/e-msg-composer.h>
#include <composer/e-composer-actions.h>
#include <composer/e-composer-post-header.h>
Not Zed's avatar
Not Zed committed
44

45
#include "e-mail-printer.h"
46
#include "e-mail-ui-session.h"
47 48
#include "e-mail-templates.h"
#include "e-mail-templates-store.h"
Not Zed's avatar
Not Zed committed
49 50
#include "em-utils.h"
#include "em-composer-utils.h"
51 52
#include "em-folder-selector.h"
#include "em-folder-tree.h"
53
#include "em-event.h"
54 55
#include "mail-send-recv.h"

56
#ifdef G_OS_WIN32
57
#ifdef gmtime_r
58
#undef gmtime_r
59
#endif
60 61 62
#ifdef localtime_r
#undef localtime_r
#endif
63

64
/* The gmtime() and localtime() in Microsoft's C library are MT-safe */
65
#define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
66
#define localtime_r(tp,tmp) (localtime(tp)?(*(tmp)=*localtime(tp),(tmp)):0)
67 68
#endif

69
typedef struct _AsyncContext AsyncContext;
70
typedef struct _ForwardData ForwardData;
71

72 73
struct _AsyncContext {
	CamelMimeMessage *message;
74
	EMailSession *session;
75 76 77 78
	EMsgComposer *composer;
	EActivity *activity;
	gchar *folder_uri;
	gchar *message_uid;
79 80
	gulong num_loading_handler_id;
	gulong cancelled_handler_id;
Not Zed's avatar
Not Zed committed
81 82
};

83 84 85 86 87 88 89
struct _ForwardData {
	EShell *shell;
	CamelFolder *folder;
	GPtrArray *uids;
	EMailForwardStyle style;
};

Not Zed's avatar
Not Zed committed
90
static void
Matthew Barnes's avatar
Matthew Barnes committed
91
async_context_free (AsyncContext *async_context)
Not Zed's avatar
Not Zed committed
92
{
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	if (async_context->cancelled_handler_id) {
		GCancellable *cancellable;

		cancellable = e_activity_get_cancellable (async_context->activity);
		/* Cannot use g_cancellable_disconnect(), because when this is called
		   from inside the cancelled handler, then the GCancellable deadlocks. */
		g_signal_handler_disconnect (cancellable, async_context->cancelled_handler_id);
		async_context->cancelled_handler_id = 0;
	}

	if (async_context->num_loading_handler_id) {
		EAttachmentView *view;
		EAttachmentStore *store;

		view = e_msg_composer_get_attachment_view (async_context->composer);
		store = e_attachment_view_get_store (view);

		e_signal_disconnect_notify_handler (store, &async_context->num_loading_handler_id);
	}

Matthew Barnes's avatar
Matthew Barnes committed
113 114 115 116
	g_clear_object (&async_context->message);
	g_clear_object (&async_context->session);
	g_clear_object (&async_context->composer);
	g_clear_object (&async_context->activity);
117

Matthew Barnes's avatar
Matthew Barnes committed
118 119
	g_free (async_context->folder_uri);
	g_free (async_context->message_uid);
Not Zed's avatar
Not Zed committed
120

Matthew Barnes's avatar
Matthew Barnes committed
121
	g_slice_free (AsyncContext, async_context);
Not Zed's avatar
Not Zed committed
122 123
}

124 125 126 127 128 129 130 131 132 133
static void
forward_data_free (ForwardData *data)
{
	if (data->shell != NULL)
		g_object_unref (data->shell);

	if (data->folder != NULL)
		g_object_unref (data->folder);

	if (data->uids != NULL)
134
		g_ptr_array_unref (data->uids);
135 136 137 138

	g_slice_free (ForwardData, data);
}

Not Zed's avatar
Not Zed committed
139
static gboolean
140 141
ask_confirm_for_unwanted_html_mail (EMsgComposer *composer,
                                    EDestination **recipients)
Not Zed's avatar
Not Zed committed
142
{
Not Zed's avatar
Not Zed committed
143
	gboolean res;
Not Zed's avatar
Not Zed committed
144
	GString *str;
145
	gint i;
146

147
	str = g_string_new ("");
Not Zed's avatar
Not Zed committed
148
	for (i = 0; recipients[i] != NULL; ++i) {
149
		if (!e_destination_get_html_mail_pref (recipients[i])) {
150
			const gchar *name;
151

152
			name = e_destination_get_textrep (recipients[i], FALSE);
153

Not Zed's avatar
Not Zed committed
154 155 156
			g_string_append_printf (str, "     %s\n", name);
		}
	}
Not Zed's avatar
Not Zed committed
157

158
	if (str->len)
159
		res = e_util_prompt_user (
160
			GTK_WINDOW (composer),
161
			"org.gnome.evolution.mail", "prompt-on-unwanted-html",
162
			"mail:ask-send-html", str->str, NULL);
163 164 165
	else
		res = TRUE;

Matthew Barnes's avatar
Matthew Barnes committed
166
	g_string_free (str, TRUE);
167

Not Zed's avatar
Not Zed committed
168 169 170 171 172 173
	return res;
}

static gboolean
ask_confirm_for_empty_subject (EMsgComposer *composer)
{
174
	return e_util_prompt_user (
175
		GTK_WINDOW (composer),
176
		"org.gnome.evolution.mail",
177
		"prompt-on-empty-subject",
178
		"mail:ask-send-no-subject", NULL);
Not Zed's avatar
Not Zed committed
179 180 181
}

static gboolean
182 183
ask_confirm_for_only_bcc (EMsgComposer *composer,
                          gboolean hidden_list_case)
Not Zed's avatar
Not Zed committed
184 185
{
	/* If the user is mailing a hidden contact list, it is possible for
186 187 188 189
	 * them to create a message with only Bcc recipients without really
	 * realizing it.  To try to avoid being totally confusing, I've changed
	 * this dialog to provide slightly different text in that case, to
	 * better explain what the hell is going on. */
190

191
	return e_util_prompt_user (
192
		GTK_WINDOW (composer),
193
		"org.gnome.evolution.mail",
194
		"prompt-on-only-bcc",
195 196 197
		hidden_list_case ?
		"mail:ask-send-only-bcc-contact" :
		"mail:ask-send-only-bcc", NULL);
Not Zed's avatar
Not Zed committed
198
}
Not Zed's avatar
Not Zed committed
199

200 201 202 203 204 205 206 207 208 209 210 211
static gboolean
is_group_definition (const gchar *str)
{
	const gchar *colon;

	if (!str || !*str)
		return FALSE;

	colon = strchr (str, ':');
	return colon > str && strchr (str, ';') > colon;
}

212
static gboolean
213 214
composer_presend_check_recipients (EMsgComposer *composer,
                                   EMailSession *session)
Not Zed's avatar
Not Zed committed
215
{
216 217
	EDestination **recipients;
	EDestination **recipients_bcc;
Not Zed's avatar
Not Zed committed
218
	CamelInternetAddress *cia;
219
	EComposerHeaderTable *table;
220 221
	EComposerHeader *post_to_header;
	GString *invalid_addrs = NULL;
222
	GSettings *settings;
223 224 225 226
	gboolean check_passed = FALSE;
	gint hidden = 0;
	gint shown = 0;
	gint num = 0;
227
	gint num_to_cc = 0;
228 229 230
	gint num_bcc = 0;
	gint num_post = 0;
	gint ii;
231

232 233 234
	/* We should do all of the validity checks based on the composer,
	 * and not on the created message, as extra interaction may occur
	 * when we get the message (e.g. passphrase to sign a message). */
235

236
	table = e_msg_composer_get_header_table (composer);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

	recipients = e_composer_header_table_get_destinations_to (table);
	if (recipients) {
		for (ii = 0; recipients[ii] != NULL; ii++) {
			const gchar *addr;

			addr = e_destination_get_address (recipients[ii]);
			if (addr == NULL || *addr == '\0')
				continue;

			num_to_cc++;
		}

		e_destination_freev (recipients);
	}

	recipients = e_composer_header_table_get_destinations_cc (table);
	if (recipients) {
		for (ii = 0; recipients[ii] != NULL; ii++) {
			const gchar *addr;

			addr = e_destination_get_address (recipients[ii]);
			if (addr == NULL || *addr == '\0')
				continue;

			num_to_cc++;
		}

		e_destination_freev (recipients);
	}

268
	recipients = e_composer_header_table_get_destinations (table);
269

Not Zed's avatar
Not Zed committed
270
	cia = camel_internet_address_new ();
271

272 273 274 275
	/* See which ones are visible, present, etc. */
	for (ii = 0; recipients != NULL && recipients[ii] != NULL; ii++) {
		const gchar *addr;
		gint len, j;
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
		addr = e_destination_get_address (recipients[ii]);
		if (addr == NULL || *addr == '\0')
			continue;

		camel_address_decode (CAMEL_ADDRESS (cia), addr);
		len = camel_address_length (CAMEL_ADDRESS (cia));

		if (len > 0) {
			if (!e_destination_is_evolution_list (recipients[ii])) {
				for (j = 0; j < len; j++) {
					const gchar *name = NULL, *eml = NULL;

					if (!camel_internet_address_get (cia, j, &name, &eml) ||
					    !eml ||
					    strchr (eml, '@') <= eml) {
						if (!invalid_addrs)
							invalid_addrs = g_string_new ("");
						else
							g_string_append (invalid_addrs, ", ");

						if (name)
							g_string_append (invalid_addrs, name);
						if (eml) {
							g_string_append (invalid_addrs, name ? " <" : "");
							g_string_append (invalid_addrs, eml);
							g_string_append (invalid_addrs, name ? ">" : "");
						}
Not Zed's avatar
Not Zed committed
304 305 306
					}
				}
			}
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

			camel_address_remove (CAMEL_ADDRESS (cia), -1);
			num++;
			if (e_destination_is_evolution_list (recipients[ii])
			    && !e_destination_list_show_addresses (recipients[ii])) {
				hidden++;
			} else {
				shown++;
			}
		} else if (is_group_definition (addr)) {
			/* like an address, it will not claim on only-bcc */
			shown++;
			num++;
		} else if (!invalid_addrs) {
			invalid_addrs = g_string_new (addr);
		} else {
			g_string_append (invalid_addrs, ", ");
			g_string_append (invalid_addrs, addr);
Not Zed's avatar
Not Zed committed
325 326
		}
	}
327

328
	recipients_bcc = e_composer_header_table_get_destinations_bcc (table);
Not Zed's avatar
Not Zed committed
329
	if (recipients_bcc) {
330 331 332 333 334 335 336 337 338 339 340
		for (ii = 0; recipients_bcc[ii] != NULL; ii++) {
			const gchar *addr;

			addr = e_destination_get_address (recipients_bcc[ii]);
			if (addr == NULL || *addr == '\0')
				continue;

			camel_address_decode (CAMEL_ADDRESS (cia), addr);
			if (camel_address_length (CAMEL_ADDRESS (cia)) > 0) {
				camel_address_remove (CAMEL_ADDRESS (cia), -1);
				num_bcc++;
Not Zed's avatar
Not Zed committed
341 342
			}
		}
343

344
		e_destination_freev (recipients_bcc);
Not Zed's avatar
Not Zed committed
345
	}
346

Matthew Barnes's avatar
Matthew Barnes committed
347
	g_object_unref (cia);
Not Zed's avatar
Not Zed committed
348

349 350
	post_to_header = e_composer_header_table_get_header (
		table, E_COMPOSER_HEADER_POST_TO);
351 352 353 354 355
	if (e_composer_header_get_visible (post_to_header)) {
		GList *postlist;

		postlist = e_composer_header_table_get_post_to (table);
		num_post = g_list_length (postlist);
356
		g_list_foreach (postlist, (GFunc) g_free, NULL);
357 358
		g_list_free (postlist);
	}
359

Not Zed's avatar
Not Zed committed
360
	/* I'm sensing a lack of love, er, I mean recipients. */
Not Zed's avatar
Not Zed committed
361
	if (num == 0 && num_post == 0) {
362 363 364 365 366
		EHTMLEditor *editor;

		editor = e_msg_composer_get_editor (composer);
		e_alert_submit (E_ALERT_SINK (editor), "mail:send-no-recipients", NULL);

Not Zed's avatar
Not Zed committed
367
		goto finished;
Not Zed's avatar
Not Zed committed
368
	}
369

370
	if (invalid_addrs) {
371
		if (!e_util_prompt_user (
372
			GTK_WINDOW (composer),
373
			"org.gnome.evolution.mail",
374
			"prompt-on-invalid-recip",
375 376 377
			strstr (invalid_addrs->str, ", ") ?
				"mail:ask-send-invalid-recip-multi" :
				"mail:ask-send-invalid-recip-one",
378
			invalid_addrs->str, NULL)) {
379 380 381 382 383 384 385
			g_string_free (invalid_addrs, TRUE);
			goto finished;
		}

		g_string_free (invalid_addrs, TRUE);
	}

386
	settings = e_util_ref_settings ("org.gnome.evolution.mail");
387 388 389
	if (num_to_cc > 1 && num_to_cc >= g_settings_get_int (settings, "composer-many-to-cc-recips-num")) {
		gchar *head;
		gchar *msg;
390 391 392

		g_clear_object (&settings);

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		head = g_strdup_printf (ngettext (
			/* Translators: The %d is replaced with the actual count of recipients, which is always more than one. */
			"Are you sure you want to send a message with %d To and CC recipients?",
			"Are you sure you want to send a message with %d To and CC recipients?",
			num_to_cc), num_to_cc);

		msg = g_strdup_printf (ngettext (
			/* Translators: The %d is replaced with the actual count of recipients, which is always more than one. */
			"You are trying to send a message to %d recipients in To and CC fields."
			" This would result in all recipients seeing the email addresses of each"
			" other. In some cases this behaviour is undesired, especially if they"
			" do not know each other or if privacy is a concern. Consider adding"
			" recipients to the BCC field instead.",
			"You are trying to send a message to %d recipients in To and CC fields."
			" This would result in all recipients seeing the email addresses of each"
			" other. In some cases this behaviour is undesired, especially if they"
			" do not know each other or if privacy is a concern. Consider adding"
			" recipients to the BCC field instead.",
			num_to_cc), num_to_cc);
412 413 414 415 416 417

		if (!e_util_prompt_user (
			GTK_WINDOW (composer),
			"org.gnome.evolution.mail",
			"prompt-on-many-to-cc-recips",
			"mail:ask-many-to-cc-recips",
418
			head, msg, NULL)) {
419 420
			GtkAction *action;

421 422
			g_free (head);
			g_free (msg);
423 424 425 426 427 428 429

			action = E_COMPOSER_ACTION_VIEW_BCC (composer);
			gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);

			goto finished;
		}

430 431
		g_free (head);
		g_free (msg);
432 433 434
	}
	g_clear_object (&settings);

Not Zed's avatar
Not Zed committed
435
	if (num > 0 && (num == num_bcc || shown == 0)) {
436
		/* this means that the only recipients are Bcc's */
Not Zed's avatar
Not Zed committed
437 438 439
		if (!ask_confirm_for_only_bcc (composer, shown == 0))
			goto finished;
	}
440

441
	check_passed = TRUE;
442

443 444 445
finished:
	if (recipients != NULL)
		e_destination_freev (recipients);
446

447 448
	return check_passed;
}
449

450
static gboolean
451 452
composer_presend_check_identity (EMsgComposer *composer,
                                 EMailSession *session)
453 454
{
	EComposerHeaderTable *table;
455
	ESource *source = NULL;
456
	gchar *uid;
457
	gboolean success = TRUE;
458

459
	table = e_msg_composer_get_header_table (composer);
460

461
	uid = e_composer_header_table_dup_identity_uid (table, NULL, NULL);
462 463
	if (uid)
		source = e_composer_header_table_ref_source (table, uid);
464
	g_free (uid);
465

466 467 468
	if (source) {
		EClientCache *client_cache;
		ESourceRegistry *registry;
469

470 471 472 473 474 475 476 477 478 479 480 481 482
		client_cache = e_composer_header_table_ref_client_cache (table);
		registry = e_client_cache_ref_registry (client_cache);

		success = e_source_registry_check_enabled (registry, source);
		if (!success) {
			e_alert_submit (
				E_ALERT_SINK (e_msg_composer_get_editor (composer)),
				"mail:send-no-account-enabled", NULL);
		}

		g_object_unref (client_cache);
		g_object_unref (registry);
	} else {
483
		success = FALSE;
484 485 486
		e_alert_submit (
			E_ALERT_SINK (e_msg_composer_get_editor (composer)),
			"mail:send-no-account", NULL);
487
	}
488

489 490

	g_clear_object (&source);
491 492

	return success;
493 494 495
}

static gboolean
496 497
composer_presend_check_plugins (EMsgComposer *composer,
                                EMailSession *session)
498 499 500 501 502
{
	EMEvent *eme;
	EMEventTargetComposer *target;
	gpointer data;

503 504 505
	/** @Event: composer.presendchecks
	 * @Title: Composer PreSend Checks
	 * @Target: EMEventTargetMessage
506
	 *
507 508 509 510
	 * composer.presendchecks is emitted during pre-checks for the
	 * message just before sending.  Since the e-plugin framework
	 * doesn't provide a way to return a value from the plugin,
	 * use 'presend_check_status' to set whether the check passed.
511
	 */
Matthew Barnes's avatar
Matthew Barnes committed
512
	eme = em_event_peek ();
513 514
	target = em_event_target_new_composer (eme, composer, 0);

515 516 517
	e_event_emit (
		(EEvent *) eme, "composer.presendchecks",
		(EEventTarget *) target);
518

519 520
	/* A non-NULL value for this key means the check failed. */
	data = g_object_get_data (G_OBJECT (composer), "presend_check_status");
521

522 523 524
	/* Clear the value in case we have to run these checks again. */
	g_object_set_data (G_OBJECT (composer), "presend_check_status", NULL);

525 526 527 528
	return (data == NULL);
}

static gboolean
529 530
composer_presend_check_subject (EMsgComposer *composer,
                                EMailSession *session)
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
{
	EComposerHeaderTable *table;
	const gchar *subject;
	gboolean check_passed = TRUE;

	table = e_msg_composer_get_header_table (composer);
	subject = e_composer_header_table_get_subject (table);

	if (subject == NULL || subject[0] == '\0') {
		if (!ask_confirm_for_empty_subject (composer))
			check_passed = FALSE;
	}

	return check_passed;
}

static gboolean
548 549
composer_presend_check_unwanted_html (EMsgComposer *composer,
                                      EMailSession *session)
550 551
{
	EDestination **recipients;
552
	EHTMLEditor *editor;
Tomas Popela's avatar
Tomas Popela committed
553
	EContentEditor *cnt_editor;
554
	EComposerHeaderTable *table;
555
	GSettings *settings;
556 557 558 559 560 561
	gboolean check_passed = TRUE;
	gboolean html_mode;
	gboolean send_html;
	gboolean confirm_html;
	gint ii;

562
	settings = e_util_ref_settings ("org.gnome.evolution.mail");
563

564
	editor = e_msg_composer_get_editor (composer);
Tomas Popela's avatar
Tomas Popela committed
565 566
	cnt_editor = e_html_editor_get_content_editor (editor);
	html_mode = e_content_editor_get_html_mode (cnt_editor);
567

568 569 570
	table = e_msg_composer_get_header_table (composer);
	recipients = e_composer_header_table_get_destinations (table);

571 572
	send_html = g_settings_get_boolean (settings, "composer-send-html");
	confirm_html = g_settings_get_boolean (settings, "prompt-on-unwanted-html");
573 574 575 576 577 578 579 580

	/* Only show this warning if our default is to send html.  If it
	 * isn't, we've manually switched into html mode in the composer
	 * and (presumably) had a good reason for doing this. */
	if (html_mode && send_html && confirm_html && recipients != NULL) {
		gboolean html_problem = FALSE;

		for (ii = 0; recipients[ii] != NULL; ii++) {
581
			if (!e_destination_get_html_mail_pref (recipients[ii])) {
582 583
				html_problem = TRUE;
				break;
584
			}
585 586 587 588 589 590 591 592 593 594 595 596
		}

		if (html_problem) {
			if (!ask_confirm_for_unwanted_html_mail (
				composer, recipients))
				check_passed = FALSE;
		}
	}

	if (recipients != NULL)
		e_destination_freev (recipients);

597
	g_object_unref (settings);
598 599 600 601 602

	return check_passed;
}

static void
Matthew Barnes's avatar
Matthew Barnes committed
603
composer_send_completed (GObject *source_object,
604
                         GAsyncResult *result,
Matthew Barnes's avatar
Matthew Barnes committed
605
                         gpointer user_data)
606
{
Matthew Barnes's avatar
Matthew Barnes committed
607
	EActivity *activity;
608
	gboolean service_unavailable;
609
	gboolean set_changed = FALSE;
Matthew Barnes's avatar
Matthew Barnes committed
610 611 612 613
	AsyncContext *async_context;
	GError *local_error = NULL;

	async_context = (AsyncContext *) user_data;
614

Matthew Barnes's avatar
Matthew Barnes committed
615
	activity = async_context->activity;
Matthew Barnes's avatar
Matthew Barnes committed
616

Matthew Barnes's avatar
Matthew Barnes committed
617 618 619 620
	e_mail_session_send_to_finish (
		E_MAIL_SESSION (source_object), result, &local_error);

	if (e_activity_handle_cancellation (activity, local_error)) {
621
		set_changed = TRUE;
622
		goto exit;
Matthew Barnes's avatar
Matthew Barnes committed
623 624
	}

625 626 627 628
	/* Check for error codes which may indicate we're offline
	 * or name resolution failed or connection attempt failed. */
	service_unavailable =
		g_error_matches (
Matthew Barnes's avatar
Matthew Barnes committed
629
			local_error, CAMEL_SERVICE_ERROR,
630
			CAMEL_SERVICE_ERROR_UNAVAILABLE) ||
631 632 633 634 635 636 637 638
		/* name resolution failed */
		g_error_matches (
			local_error, G_RESOLVER_ERROR,
			G_RESOLVER_ERROR_NOT_FOUND) ||
		g_error_matches (
			local_error, G_RESOLVER_ERROR,
			G_RESOLVER_ERROR_TEMPORARY_FAILURE) ||
		/* something internal to Camel failed */
Matthew Barnes's avatar
Matthew Barnes committed
639
		g_error_matches (
640 641
			local_error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_URL_INVALID);
642 643 644
	if (service_unavailable) {
		/* Inform the user. */
		e_alert_run_dialog_for_args (
Matthew Barnes's avatar
Matthew Barnes committed
645
			GTK_WINDOW (async_context->composer),
646
			"mail-composer:saving-to-outbox", NULL);
647 648 649 650 651 652
		if (async_context->message)
			g_signal_emit_by_name (
				async_context->composer, "save-to-outbox",
				async_context->message, activity);
		else
			e_msg_composer_save_to_outbox (async_context->composer);
653 654 655
		goto exit;
	}

656
	/* Post-processing errors are shown in the shell window. */
Matthew Barnes's avatar
Matthew Barnes committed
657 658 659
	if (g_error_matches (
		local_error, E_MAIL_ERROR,
		E_MAIL_ERROR_POST_PROCESSING)) {
660 661 662
		EAlert *alert;
		EShell *shell;

Matthew Barnes's avatar
Matthew Barnes committed
663
		shell = e_msg_composer_get_shell (async_context->composer);
664 665 666

		alert = e_alert_new (
			"mail-composer:send-post-processing-error",
Matthew Barnes's avatar
Matthew Barnes committed
667
			local_error->message, NULL);
668 669 670 671
		e_shell_submit_alert (shell, alert);
		g_object_unref (alert);

	/* All other errors are shown in the composer window. */
Matthew Barnes's avatar
Matthew Barnes committed
672
	} else if (local_error != NULL) {
673 674 675 676
		gint response;

		/* Clear the activity bar before
		 * presenting the error dialog. */
Matthew Barnes's avatar
Matthew Barnes committed
677 678
		g_clear_object (&async_context->activity);
		activity = async_context->activity;
679 680

		response = e_alert_run_dialog_for_args (
Matthew Barnes's avatar
Matthew Barnes committed
681
			GTK_WINDOW (async_context->composer),
682
			"mail-composer:send-error",
Matthew Barnes's avatar
Matthew Barnes committed
683
			local_error->message, NULL);
684
		if (response == GTK_RESPONSE_OK)  /* Try Again */
Matthew Barnes's avatar
Matthew Barnes committed
685
			e_msg_composer_send (async_context->composer);
686 687 688 689 690 691 692 693
		if (response == GTK_RESPONSE_ACCEPT) { /* Save to Outbox */
			if (async_context->message)
				g_signal_emit_by_name (
					async_context->composer, "save-to-outbox",
					async_context->message, activity);
			else
				e_msg_composer_save_to_outbox (async_context->composer);
		}
694
		set_changed = TRUE;
695
		goto exit;
Not Zed's avatar
Not Zed committed
696
	}
697

Matthew Barnes's avatar
Matthew Barnes committed
698
	e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
699

700 701 702
	/* Wait for the EActivity's completion message to
	 * time out and then destroy the composer window. */
	g_object_weak_ref (
Matthew Barnes's avatar
Matthew Barnes committed
703 704
		G_OBJECT (activity), (GWeakNotify)
		gtk_widget_destroy, async_context->composer);
705

706
exit:
Matthew Barnes's avatar
Matthew Barnes committed
707
	g_clear_error (&local_error);
708

709
	if (set_changed) {
710
		EHTMLEditor *editor;
Tomas Popela's avatar
Tomas Popela committed
711
		EContentEditor *cnt_editor;
712 713

		editor = e_msg_composer_get_editor (async_context->composer);
Tomas Popela's avatar
Tomas Popela committed
714 715 716
		cnt_editor = e_html_editor_get_content_editor (editor);

		e_content_editor_set_changed (cnt_editor, TRUE);
717

Matthew Barnes's avatar
Matthew Barnes committed
718
		gtk_window_present (GTK_WINDOW (async_context->composer));
719
	}
720

Matthew Barnes's avatar
Matthew Barnes committed
721
	async_context_free (async_context);
Not Zed's avatar
Not Zed committed
722 723
}

724
static void
725 726 727 728
em_utils_composer_real_send (EMsgComposer *composer,
			     CamelMimeMessage *message,
			     EActivity *activity,
			     EMailSession *session)
729
{
Matthew Barnes's avatar
Matthew Barnes committed
730
	AsyncContext *async_context;
731
	GCancellable *cancellable;
732 733
	GSettings *settings;

734
	settings = e_util_ref_settings ("org.gnome.evolution.mail");
735
	if (g_settings_get_boolean (settings, "composer-use-outbox")) {
736 737 738 739 740 741
		/* Using e_msg_composer_save_to_outbox() means building
		   the message again; better to use the built message here. */
		g_signal_emit_by_name (
			composer, "save-to-outbox",
			message, activity);

742 743 744 745 746
		g_object_unref (settings);
		return;
	}

	g_object_unref (settings);
747

748 749 750 751
	if (!camel_session_get_online (CAMEL_SESSION (session))) {
		e_alert_run_dialog_for_args (
			GTK_WINDOW (composer),
			"mail-composer:saving-to-outbox", NULL);
752 753 754 755 756 757 758

		/* Using e_msg_composer_save_to_outbox() means building
		   the message again; better to use the built message here. */
		g_signal_emit_by_name (
			composer, "save-to-outbox",
			message, activity);

759 760 761
		return;
	}

Matthew Barnes's avatar
Matthew Barnes committed
762 763 764 765
	async_context = g_slice_new0 (AsyncContext);
	async_context->message = g_object_ref (message);
	async_context->composer = g_object_ref (composer);
	async_context->activity = g_object_ref (activity);
766 767 768

	cancellable = e_activity_get_cancellable (activity);

769
	e_mail_session_send_to (
770
		session, message,
Matthew Barnes's avatar
Matthew Barnes committed
771 772 773 774
		G_PRIORITY_DEFAULT,
		cancellable, NULL, NULL,
		composer_send_completed,
		async_context);
775
}
Not Zed's avatar
Not Zed committed
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 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 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
static void
composer_num_loading_notify_cb (EAttachmentStore *store,
				GParamSpec *param,
				AsyncContext *async_context)
{
	if (e_attachment_store_get_num_loading (store) > 0)
		return;

	em_utils_composer_real_send (
		async_context->composer,
		async_context->message,
		async_context->activity,
		async_context->session);

	async_context_free (async_context);
}

static void
composer_wait_for_attachment_load_cancelled_cb (GCancellable *cancellable,
						AsyncContext *async_context)
{
	async_context_free (async_context);
}

static void
em_utils_composer_send_cb (EMsgComposer *composer,
                           CamelMimeMessage *message,
                           EActivity *activity,
                           EMailSession *session)
{
	AsyncContext *async_context;
	GCancellable *cancellable;
	EAttachmentView *view;
	EAttachmentStore *store;

	view = e_msg_composer_get_attachment_view (composer);
	store = e_attachment_view_get_store (view);

	if (e_attachment_store_get_num_loading (store) <= 0) {
		em_utils_composer_real_send (composer, message, activity, session);
		return;
	}

	async_context = g_slice_new0 (AsyncContext);
	async_context->session = g_object_ref (session);
	async_context->message = g_object_ref (message);
	async_context->composer = g_object_ref (composer);
	async_context->activity = g_object_ref (activity);

	cancellable = e_activity_get_cancellable (activity);
	/* This message is never removed from the camel operation, otherwise the GtkInfoBar
	   hides itself and the user sees no feedback. */
	camel_operation_push_message (cancellable, "%s", _("Waiting for attachments to load..."));

	async_context->num_loading_handler_id = e_signal_connect_notify (store, "notify::num-loading",
		G_CALLBACK (composer_num_loading_notify_cb), async_context);
	/* Cannot use g_cancellable_connect() here, see async_context_free() */
	async_context->cancelled_handler_id = g_signal_connect (cancellable, "cancelled",
		G_CALLBACK (composer_wait_for_attachment_load_cancelled_cb), async_context);
}

838
static void
839
composer_set_no_change (EMsgComposer *composer)
840
{
841
	EHTMLEditor *editor;
Tomas Popela's avatar
Tomas Popela committed
842
	EContentEditor *cnt_editor;
843

844 845
	g_return_if_fail (composer != NULL);

846
	editor = e_msg_composer_get_editor (composer);
Tomas Popela's avatar
Tomas Popela committed
847
	cnt_editor = e_html_editor_get_content_editor (editor);
848

Tomas Popela's avatar
Tomas Popela committed
849
	e_content_editor_set_changed (cnt_editor, FALSE);
850 851
}

852 853
/* delete original messages from Outbox folder */
static void
854
manage_x_evolution_replace_outbox (EMsgComposer *composer,
855
                                   EMailSession *session,
856 857
                                   CamelMimeMessage *message,
                                   GCancellable *cancellable)
858 859
{
	const gchar *message_uid;
860
	const gchar *header;
861 862 863 864 865
	CamelFolder *outbox;

	g_return_if_fail (composer != NULL);
	g_return_if_fail (CAMEL_IS_MIME_MESSAGE (message));

866 867 868
	header = "X-Evolution-Replace-Outbox-UID";
	message_uid = camel_medium_get_header (CAMEL_MEDIUM (message), header);
	e_msg_composer_remove_header (composer, header);
869 870 871 872

	if (!message_uid)
		return;

873 874
	outbox = e_mail_session_get_local_folder (
		session, E_MAIL_LOCAL_FOLDER_OUTBOX);
875 876 877 878 879 880 881 882
	g_return_if_fail (outbox != NULL);

	camel_folder_set_message_flags (
		outbox, message_uid,
		CAMEL_MESSAGE_DELETED | CAMEL_MESSAGE_SEEN,
		CAMEL_MESSAGE_DELETED | CAMEL_MESSAGE_SEEN);

	/* ignore errors here */
883 884
	camel_folder_synchronize_message_sync (
		outbox, message_uid, cancellable, NULL);
885 886
}

Not Zed's avatar
Not Zed committed
887
static void
Matthew Barnes's avatar
Matthew Barnes committed
888
composer_save_to_drafts_complete (GObject *source_object,
889
                                  GAsyncResult *result,
Matthew Barnes's avatar
Matthew Barnes committed
890
                                  gpointer user_data)
Not Zed's avatar
Not Zed committed
891
{
Matthew Barnes's avatar
Matthew Barnes committed
892 893
	EActivity *activity;
	AsyncContext *async_context;
894
	EHTMLEditor *editor;
Tomas Popela's avatar
Tomas Popela committed
895
	EContentEditor *cnt_editor;
Matthew Barnes's avatar
Matthew Barnes committed
896
	GError *local_error = NULL;
897

898 899 900
	async_context = (AsyncContext *) user_data;

	editor = e_msg_composer_get_editor (async_context->composer);
Tomas Popela's avatar
Tomas Popela committed
901
	cnt_editor = e_html_editor_get_content_editor (editor);
902

903 904 905
	/* We don't really care if this failed.  If something other than
	 * cancellation happened, emit a runtime warning so the error is
	 * not completely lost. */
Matthew Barnes's avatar
Matthew Barnes committed
906 907
	e_mail_session_handle_draft_headers_finish (
		E_MAIL_SESSION (source_object), result, &local_error);
908

909 910
	activity = async_context->activity;

Matthew Barnes's avatar
Matthew Barnes committed
911
	if (e_activity_handle_cancellation (activity, local_error)) {
Tomas Popela's avatar
Tomas Popela committed
912
		e_content_editor_set_changed (cnt_editor, TRUE);
Matthew Barnes's avatar
Matthew Barnes committed
913 914 915
		g_error_free (local_error);

	} else if (local_error != NULL) {
Tomas Popela's avatar
Tomas Popela committed
916
		e_content_editor_set_changed (cnt_editor, TRUE);
Matthew Barnes's avatar
Matthew Barnes committed
917 918
		g_warning ("%s", local_error->message);
		g_error_free (local_error);
Matthew Barnes's avatar
Matthew Barnes committed
919
	} else
Matthew Barnes's avatar
Matthew Barnes committed
920
		e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
921

922 923 924 925 926
	/* Encode the draft message we just saved into the EMsgComposer
	 * as X-Evolution-Draft headers.  The message will be marked for
	 * deletion if the user saves a newer draft message or sends the
	 * composed message. */
	e_msg_composer_set_draft_headers (
Matthew Barnes's avatar
Matthew Barnes committed
927 928 929
		async_context->composer,
		async_context->folder_uri,
		async_context->message_uid);
930

931 932
	e_content_editor_set_changed (cnt_editor, FALSE);

Matthew Barnes's avatar
Matthew Barnes committed
933
	async_context_free (async_context);
Not Zed's avatar
Not Zed committed
934 935
}

936 937 938 939
static void
composer_save_to_drafts_append_mail (AsyncContext *async_context,
                                     CamelFolder *drafts_folder);

Not Zed's avatar
Not Zed committed
940
static void
Matthew Barnes's avatar
Matthew Barnes committed
941
composer_save_to_drafts_cleanup (GObject *source_object,
942
                                 GAsyncResult *result,
Matthew Barnes's avatar
Matthew Barnes committed
943
                                 gpointer user_data)
Not Zed's avatar
Not Zed committed
944
{
945
	CamelSession *session;
Matthew Barnes's avatar
Matthew Barnes committed
946
	EActivity *activity;
947
	EAlertSink *alert_sink;
948
	GCancellable *cancellable;
949
	EHTMLEditor *editor;
Tomas Popela's avatar
Tomas Popela committed
950
	EContentEditor *cnt_editor;
Matthew Barnes's avatar
Matthew Barnes committed
951 952
	AsyncContext *async_context;
	GError *local_error = NULL;
953

Matthew Barnes's avatar
Matthew Barnes committed
954
	async_context = (AsyncContext *) user_data;
955

956
	editor = e_msg_composer_get_editor (async_context->composer);
Tomas Popela's avatar
Tomas Popela committed
957
	cnt_editor = e_html_editor_get_content_editor (editor);
958

Matthew Barnes's avatar
Matthew Barnes committed
959 960 961
	activity = async_context->activity;
	alert_sink = e_activity_get_alert_sink (activity);
	cancellable = e_activity_get_cancellable (activity);
962

963 964 965 966
	e_mail_folder_append_message_finish (
		CAMEL_FOLDER (source_object), result,
		&async_context->message_uid, &local_error);

Matthew Barnes's avatar
Matthew Barnes committed
967
	if (e_activity_handle_cancellation (activity, local_error)) {
968
		g_warn_if_fail (async_context->message_uid == NULL);
Tomas Popela's avatar
Tomas Popela committed
969
		e_content_editor_set_changed (cnt_editor, TRUE);
Matthew Barnes's avatar
Matthew Barnes committed
970 971
		async_context_free (async_context);
		g_error_free (local_error);
972
		return;
973

Matthew Barnes's avatar
Matthew Barnes committed
974
	} else if (local_error != NULL) {
975
		g_warn_if_fail (async_context->message_uid == NULL);
976 977 978 979 980 981 982 983 984 985 986