em-utils.c 38 KB
Newer Older
Not Zed's avatar
Not Zed committed
1
/*
2
3
4
5
 * 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.
Not Zed's avatar
Not Zed committed
6
 *
7
8
9
10
 * 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.
Not Zed's avatar
Not Zed committed
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
Not Zed's avatar
Not Zed committed
14
15
 *
 *
16
17
18
19
 * Authors:
 *		Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
Not Zed's avatar
Not Zed committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

34
35
36
37
38
39
40
41
42
43
#include <glib/gstdio.h>

#ifdef G_OS_WIN32
/* Work around namespace clobbage in <windows.h> */
#define DATADIR windows_DATADIR
#include <windows.h>
#undef DATADIR
#undef interface
#endif

Not Zed's avatar
Not Zed committed
44
#include "em-filter-editor.h"
Not Zed's avatar
Not Zed committed
45

Matthew Barnes's avatar
Matthew Barnes committed
46
#include <glib/gi18n.h>
47

48
#include <libebook/libebook.h>
Not Zed's avatar
Not Zed committed
49

50
51
52
53
#include <e-util/e-util.h>
#include <e-util/e-util-private.h>
#include <e-util/e-mktemp.h>
#include <e-util/e-dialog-utils.h>
54
#include <libevolution-utils/e-alert-dialog.h>
55
56
57
58

#include <shell/e-shell.h>
#include <widgets/misc/e-attachment.h>

Dan Vrátil's avatar
Dan Vrátil committed
59
60
61
#include <em-format/e-mail-parser.h>
#include <em-format/e-mail-formatter-quote.h>

62
63
64
65
66
67
68
69
#include <libemail-utils/mail-mt.h>

#include <libemail-engine/e-mail-folder-utils.h>
#include <libemail-engine/e-mail-session.h>
#include <libemail-engine/mail-ops.h>
#include <libemail-engine/mail-tools.h>

#include "e-mail-tag-editor.h"
Not Zed's avatar
Not Zed committed
70
#include "em-composer-utils.h"
71
#include "em-utils.h"
72
#include "e-mail-printer.h"
Not Zed's avatar
Not Zed committed
73

74
/* XXX This is a dirty hack on a dirty hack.  We really need
Dan Vrátil's avatar
Dan Vrátil committed
75
#include <em-format/e-mail-print-formatter.h>
76
77
78
 *     to rework or get rid of the functions that use this. */
extern const gchar *shell_builtin_backend;

79
80
81
82
/* How many is too many? */
/* Used in em_util_ask_open_many() */
#define TOO_MANY 10

83
84
85
86
87
88
/* drag and drop resulting file naming possibilities */
enum {
	DND_USE_SENT_DATE = 1, /* YYYYMMDDhhmmssms_<title> and use email sent date */
	DND_USE_DND_DATE  = 2,  /*  YYYYMMDDhhmmssms_<title> and drag'drop date */
};

Not Zed's avatar
Not Zed committed
89
90
#define d(x)

91
92
93
94
95
96
97
98
99
100
gboolean
em_utils_ask_open_many (GtkWindow *parent,
                        gint how_many)
{
	gchar *string;
	gboolean proceed;

	if (how_many < TOO_MANY)
		return TRUE;

101
102
103
104
105
106
107
108
	string = g_strdup_printf (ngettext (
		/* Translators: This message is shown only for ten or more
		 * messages to be opened.  The %d is replaced with the actual
		 * count of messages. If you need a '%' in your text, then
		 * write it doubled, like '%%'. */
		"Are you sure you want to open %d message at once?",
		"Are you sure you want to open %d messages at once?",
		how_many), how_many);
109
	proceed = em_utils_prompt_user (
110
		parent, "prompt-on-open-many",
111
112
113
114
115
116
		"mail:ask-open-many", string, NULL);
	g_free (string);

	return proceed;
}

Not Zed's avatar
Not Zed committed
117
118
119
/**
 * em_utils_prompt_user:
 * @parent: parent window
120
 * @promptkey: settings key to check if we should prompt the user or not.
121
 * @tag: e_alert tag.
Not Zed's avatar
Not Zed committed
122
123
 *
 * Convenience function to query the user with a Yes/No dialog and a
124
 * "Do not show this dialog again" checkbox. If the user checks that
Not Zed's avatar
Not Zed committed
125
 * checkbox, then @promptkey is set to %FALSE, otherwise it is set to
Not Zed's avatar
Not Zed committed
126
127
128
129
130
 * %TRUE.
 *
 * Returns %TRUE if the user clicks Yes or %FALSE otherwise.
 **/
gboolean
131
132
133
134
em_utils_prompt_user (GtkWindow *parent,
                      const gchar *promptkey,
                      const gchar *tag,
                      ...)
Not Zed's avatar
Not Zed committed
135
{
136
137
	GtkWidget *dialog;
	GtkWidget *check = NULL;
138
	GtkWidget *container;
Not Zed's avatar
Not Zed committed
139
	va_list ap;
140
	gint button;
141
	GSettings *settings;
142
	EAlert *alert = NULL;
Not Zed's avatar
Not Zed committed
143

144
	settings = g_settings_new ("org.gnome.evolution.mail");
145

146
147
	if (promptkey && !g_settings_get_boolean (settings, promptkey)) {
		g_object_unref (settings);
Not Zed's avatar
Not Zed committed
148
		return TRUE;
149
	}
150

Matthew Barnes's avatar
Matthew Barnes committed
151
	va_start (ap, tag);
152
	alert = e_alert_new_valist (tag, ap);
Matthew Barnes's avatar
Matthew Barnes committed
153
	va_end (ap);
154

155
	dialog = e_alert_dialog_new (parent, alert);
156
	g_object_unref (alert);
157

158
159
	container = e_alert_dialog_get_content_area (E_ALERT_DIALOG (dialog));

Not Zed's avatar
Not Zed committed
160
	if (promptkey) {
161
162
163
164
		check = gtk_check_button_new_with_mnemonic (
			_("_Do not show this message again"));
		gtk_box_pack_start (
			GTK_BOX (container), check, FALSE, FALSE, 0);
Not Zed's avatar
Not Zed committed
165
166
		gtk_widget_show (check);
	}
167

168
	button = gtk_dialog_run (GTK_DIALOG (dialog));
Not Zed's avatar
Not Zed committed
169
	if (promptkey)
Matthew Barnes's avatar
Matthew Barnes committed
170
171
172
173
		g_settings_set_boolean (
			settings, promptkey,
			!gtk_toggle_button_get_active (
				GTK_TOGGLE_BUTTON (check)));
Not Zed's avatar
Not Zed committed
174

175
	gtk_widget_destroy (dialog);
176

177
	g_object_unref (settings);
178

Not Zed's avatar
Not Zed committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	return button == GTK_RESPONSE_YES;
}

/**
 * em_utils_uids_copy:
 * @uids: array of uids
 *
 * Duplicates the array of uids held by @uids into a new
 * GPtrArray. Use em_utils_uids_free() to free the resultant uid
 * array.
 *
 * Returns a duplicate copy of @uids.
 **/
GPtrArray *
em_utils_uids_copy (GPtrArray *uids)
{
	GPtrArray *copy;
196
	gint i;
197

Not Zed's avatar
Not Zed committed
198
199
	copy = g_ptr_array_new ();
	g_ptr_array_set_size (copy, uids->len);
200

Not Zed's avatar
Not Zed committed
201
202
	for (i = 0; i < uids->len; i++)
		copy->pdata[i] = g_strdup (uids->pdata[i]);
203

Not Zed's avatar
Not Zed committed
204
205
206
207
208
209
210
211
212
213
214
215
	return copy;
}

/**
 * em_utils_uids_free:
 * @uids: array of uids
 *
 * Frees the array of uids pointed to by @uids back to the system.
 **/
void
em_utils_uids_free (GPtrArray *uids)
{
216
	gint i;
217

Not Zed's avatar
Not Zed committed
218
219
	for (i = 0; i < uids->len; i++)
		g_free (uids->pdata[i]);
220

Not Zed's avatar
Not Zed committed
221
222
223
	g_ptr_array_free (uids, TRUE);
}

224
/* Editing Filters/Search Folders... */
Not Zed's avatar
Not Zed committed
225
226
227
228

static GtkWidget *filter_editor = NULL;

static void
229
230
231
em_filter_editor_response (GtkWidget *dialog,
                           gint button,
                           gpointer user_data)
Not Zed's avatar
Not Zed committed
232
{
Not Zed's avatar
Not Zed committed
233
	EMFilterContext *fc;
234

235
	if (button == GTK_RESPONSE_OK) {
236
		const gchar *config_dir;
237
		gchar *user;
238

239
		config_dir = mail_session_get_config_dir ();
Not Zed's avatar
Not Zed committed
240
		fc = g_object_get_data ((GObject *) dialog, "context");
241
		user = g_build_filename (config_dir, "filters.xml", NULL);
242
		e_rule_context_save ((ERuleContext *) fc, user);
Not Zed's avatar
Not Zed committed
243
244
		g_free (user);
	}
245

Not Zed's avatar
Not Zed committed
246
	gtk_widget_destroy (dialog);
247

Not Zed's avatar
Not Zed committed
248
249
250
	filter_editor = NULL;
}

Not Zed's avatar
Not Zed committed
251
252
253
static EMFilterSource em_filter_source_element_names[] = {
	{ "incoming", },
	{ "outgoing", },
254
	{ NULL }
Not Zed's avatar
Not Zed committed
255
256
257
258
};

/**
 * em_utils_edit_filters:
259
260
261
 * @session: an #EMailSession
 * @alert_sink: an #EAlertSink
 * @parent_window: a parent #GtkWindow
Not Zed's avatar
Not Zed committed
262
263
264
265
266
267
 *
 * Opens or raises the filters editor dialog so that the user may edit
 * his/her filters. If @parent is non-NULL, then the dialog will be
 * created as a child window of @parent's toplevel window.
 **/
void
268
269
270
em_utils_edit_filters (EMailSession *session,
                       EAlertSink *alert_sink,
                       GtkWindow *parent_window)
Not Zed's avatar
Not Zed committed
271
{
272
	const gchar *config_dir;
273
	gchar *user, *system;
Not Zed's avatar
Not Zed committed
274
	EMFilterContext *fc;
275

276
277
	g_return_if_fail (E_IS_MAIL_SESSION (session));
	g_return_if_fail (E_IS_ALERT_SINK (alert_sink));
278

Not Zed's avatar
Not Zed committed
279
	if (filter_editor) {
280
		gtk_window_present (GTK_WINDOW (filter_editor));
Not Zed's avatar
Not Zed committed
281
282
		return;
	}
283

284
	config_dir = mail_session_get_config_dir ();
285

286
	fc = em_filter_context_new (session);
287
	user = g_build_filename (config_dir, "filters.xml", NULL);
288
	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
289
	e_rule_context_load ((ERuleContext *) fc, system, user);
Not Zed's avatar
Not Zed committed
290
	g_free (user);
291
	g_free (system);
292

293
	if (((ERuleContext *) fc)->error) {
294
295
296
		e_alert_submit (
			alert_sink,
			"mail:filter-load-error",
Matthew Barnes's avatar
Matthew Barnes committed
297
			((ERuleContext *) fc)->error, NULL);
Not Zed's avatar
Not Zed committed
298
299
		return;
	}
Not Zed's avatar
Not Zed committed
300
301
302
303
304
305

	if (em_filter_source_element_names[0].name == NULL) {
		em_filter_source_element_names[0].name = _("Incoming");
		em_filter_source_element_names[1].name = _("Outgoing");
	}

306
307
	filter_editor = (GtkWidget *) em_filter_editor_new (
		fc, em_filter_source_element_names);
308
309

	if (GTK_IS_WINDOW (parent_window))
310
		gtk_window_set_transient_for (
311
			GTK_WINDOW (filter_editor), parent_window);
312

313
314
315
316
317
318
319
320
	gtk_window_set_title (
		GTK_WINDOW (filter_editor), _("Message Filters"));
	g_object_set_data_full (
		G_OBJECT (filter_editor), "context", fc,
		(GDestroyNotify) g_object_unref);
	g_signal_connect (
		filter_editor, "response",
		G_CALLBACK (em_filter_editor_response), NULL);
Not Zed's avatar
Not Zed committed
321
322
323
	gtk_widget_show (GTK_WIDGET (filter_editor));
}

324
325
/*
 * Picked this from e-d-s/libedataserver/e-data.
326
327
 * But it allows more characters to occur in filenames, especially
 * when saving attachment.
328
329
330
331
332
333
334
 */
void
em_filename_make_safe (gchar *string)
{
	gchar *p, *ts;
	gunichar c;
#ifdef G_OS_WIN32
335
	const gchar *unsafe_chars = "/\":*?<>|\\#";
336
#else
337
	const gchar *unsafe_chars = "/#";
338
339
#endif

340
341
342
	g_return_if_fail (string != NULL);
	p = string;

Matthew Barnes's avatar
Matthew Barnes committed
343
	while (p && *p) {
344
345
346
347
348
349
350
		c = g_utf8_get_char (p);
		ts = p;
		p = g_utf8_next_char (p);
		/* I wonder what this code is supposed to actually
		 * achieve, and whether it does that as currently
		 * written?
		 */
Matthew Barnes's avatar
Matthew Barnes committed
351
		if (!g_unichar_isprint (c) || ( c < 0xff && strchr (unsafe_chars, c&0xff ))) {
352
			while (ts < p)
353
354
355
356
357
				*ts++ = '_';
		}
	}
}

358
/* ********************************************************************** */
Not Zed's avatar
Not Zed committed
359
360
361
362
/* Flag-for-Followup... */

/**
 * em_utils_flag_for_followup:
Matthew Barnes's avatar
Matthew Barnes committed
363
 * @reader: an #EMailReader
Not Zed's avatar
Not Zed committed
364
365
366
367
368
369
370
 * @folder: folder containing messages to flag
 * @uids: uids of messages to flag
 *
 * Open the Flag-for-Followup editor for the messages specified by
 * @folder and @uids.
 **/
void
Matthew Barnes's avatar
Matthew Barnes committed
371
372
373
em_utils_flag_for_followup (EMailReader *reader,
                            CamelFolder *folder,
                            GPtrArray *uids)
Not Zed's avatar
Not Zed committed
374
{
375
	EShell *shell;
376
	EMailBackend *backend;
377
378
	EShellSettings *shell_settings;
	EShellBackend *shell_backend;
379
	EMailDisplay *display;
380
	GtkWidget *editor;
381
	GtkWindow *window;
Matthew Barnes's avatar
Matthew Barnes committed
382
	CamelTag *tags;
383
	gint i;
384

Matthew Barnes's avatar
Matthew Barnes committed
385
	g_return_if_fail (E_IS_MAIL_READER (reader));
Not Zed's avatar
Not Zed committed
386
387
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
388

389
	window = e_mail_reader_get_window (reader);
390
391
392
	backend = e_mail_reader_get_backend (reader);

	editor = e_mail_tag_editor_new ();
393
	gtk_window_set_transient_for (GTK_WINDOW (editor), window);
394

395
	shell_backend = E_SHELL_BACKEND (backend);
396
397
398
399
	shell = e_shell_backend_get_shell (shell_backend);
	shell_settings = e_shell_get_shell_settings (shell);

	/* These settings come from the calendar module. */
400
	g_object_bind_property (
401
		shell_settings, "cal-use-24-hour-format",
402
403
404
		editor, "use-24-hour-format",
		G_BINDING_SYNC_CREATE);
	g_object_bind_property (
405
		shell_settings, "cal-week-start-day",
406
407
		editor, "week-start-day",
		G_BINDING_SYNC_CREATE);
408

Not Zed's avatar
Not Zed committed
409
410
	for (i = 0; i < uids->len; i++) {
		CamelMessageInfo *info;
411

Not Zed's avatar
Not Zed committed
412
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
413
414
415
416

		if (info == NULL)
			continue;

417
418
		e_mail_tag_editor_add_message (
			E_MAIL_TAG_EDITOR (editor),
Matthew Barnes's avatar
Matthew Barnes committed
419
420
421
			camel_message_info_from (info),
			camel_message_info_subject (info));

422
		camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
423
	}
424

Not Zed's avatar
Not Zed committed
425
426
427
	/* special-case... */
	if (uids->len == 1) {
		CamelMessageInfo *info;
Matthew Barnes's avatar
Matthew Barnes committed
428
		const gchar *message_uid;
429

Matthew Barnes's avatar
Matthew Barnes committed
430
431
		message_uid = g_ptr_array_index (uids, 0);
		info = camel_folder_get_message_info (folder, message_uid);
Not Zed's avatar
Not Zed committed
432
		if (info) {
Matthew Barnes's avatar
Matthew Barnes committed
433
			tags = (CamelTag *) camel_message_info_user_tags (info);
434
435

			if (tags)
436
437
				e_mail_tag_editor_set_tag_list (
					E_MAIL_TAG_EDITOR (editor), tags);
438
			camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
439
440
		}
	}
441

Matthew Barnes's avatar
Matthew Barnes committed
442
443
444
	if (gtk_dialog_run (GTK_DIALOG (editor)) != GTK_RESPONSE_OK)
		goto exit;

445
	tags = e_mail_tag_editor_get_tag_list (E_MAIL_TAG_EDITOR (editor));
Matthew Barnes's avatar
Matthew Barnes committed
446
447
448
449
450
451
452
453
	if (tags == NULL)
		goto exit;

	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
		CamelMessageInfo *info;
		CamelTag *iter;

Matthew Barnes's avatar
Matthew Barnes committed
454
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
455
456
457
458
459
460
461
462

		if (info == NULL)
			continue;

		for (iter = tags; iter != NULL; iter = iter->next)
			camel_message_info_set_user_tag (
				info, iter->name, iter->value);

463
		camel_folder_free_message_info (folder, info);
Matthew Barnes's avatar
Matthew Barnes committed
464
465
466
467
468
	}

	camel_folder_thaw (folder);
	camel_tag_list_free (&tags);

469
470
	display = e_mail_reader_get_mail_display (reader);
	e_mail_display_reload (display);
Matthew Barnes's avatar
Matthew Barnes committed
471
472
473
474

exit:
	/* XXX We shouldn't be freeing this. */
	em_utils_uids_free (uids);
475

Matthew Barnes's avatar
Matthew Barnes committed
476
	gtk_widget_destroy (GTK_WIDGET (editor));
Not Zed's avatar
Not Zed committed
477
478
479
480
481
482
483
484
485
486
487
488
}

/**
 * em_utils_flag_for_followup_clear:
 * @parent: parent window
 * @folder: folder containing messages to unflag
 * @uids: uids of messages to unflag
 *
 * Clears the Flag-for-Followup flag on the messages referenced by
 * @folder and @uids.
 **/
void
489
490
491
em_utils_flag_for_followup_clear (GtkWindow *parent,
                                  CamelFolder *folder,
                                  GPtrArray *uids)
Not Zed's avatar
Not Zed committed
492
{
493
	gint i;
494

495
	g_return_if_fail (GTK_IS_WINDOW (parent));
Not Zed's avatar
Not Zed committed
496
497
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
498

Not Zed's avatar
Not Zed committed
499
500
	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
Matthew Barnes's avatar
Matthew Barnes committed
501
		CamelMessageInfo *mi = camel_folder_get_message_info (folder, uids->pdata[i]);
Not Zed's avatar
Not Zed committed
502
503

		if (mi) {
504
505
506
			camel_message_info_set_user_tag (mi, "follow-up", NULL);
			camel_message_info_set_user_tag (mi, "due-by", NULL);
			camel_message_info_set_user_tag (mi, "completed-on", NULL);
507
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
508
		}
Not Zed's avatar
Not Zed committed
509
	}
Matthew Barnes's avatar
Matthew Barnes committed
510

Not Zed's avatar
Not Zed committed
511
	camel_folder_thaw (folder);
512

Not Zed's avatar
Not Zed committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
	em_utils_uids_free (uids);
}

/**
 * em_utils_flag_for_followup_completed:
 * @parent: parent window
 * @folder: folder containing messages to 'complete'
 * @uids: uids of messages to 'complete'
 *
 * Sets the completed state (and date/time) for each message
 * referenced by @folder and @uids that is marked for
 * Flag-for-Followup.
 **/
void
527
528
529
em_utils_flag_for_followup_completed (GtkWindow *parent,
                                      CamelFolder *folder,
                                      GPtrArray *uids)
Not Zed's avatar
Not Zed committed
530
{
531
532
	gchar *now;
	gint i;
533

534
	g_return_if_fail (GTK_IS_WINDOW (parent));
Not Zed's avatar
Not Zed committed
535
536
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
537

538
	now = camel_header_format_date (time (NULL), 0);
539

Not Zed's avatar
Not Zed committed
540
541
	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
542
		const gchar *tag;
Matthew Barnes's avatar
Matthew Barnes committed
543
		CamelMessageInfo *mi = camel_folder_get_message_info (folder, uids->pdata[i]);
Not Zed's avatar
Not Zed committed
544
545

		if (mi) {
546
			tag = camel_message_info_user_tag (mi, "follow-up");
Not Zed's avatar
Not Zed committed
547
			if (tag && tag[0])
548
				camel_message_info_set_user_tag (mi, "completed-on", now);
549
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
550
		}
Not Zed's avatar
Not Zed committed
551
	}
Matthew Barnes's avatar
Matthew Barnes committed
552

Not Zed's avatar
Not Zed committed
553
	camel_folder_thaw (folder);
554

Not Zed's avatar
Not Zed committed
555
	g_free (now);
556

Not Zed's avatar
Not Zed committed
557
558
559
	em_utils_uids_free (uids);
}

560
561
562
/* This kind of sucks, because for various reasons most callers need to run
 * synchronously in the gui thread, however this could take a long, blocking
 * time to run. */
Matthew Barnes's avatar
Matthew Barnes committed
563
static gint
564
565
566
em_utils_write_messages_to_stream (CamelFolder *folder,
                                   GPtrArray *uids,
                                   CamelStream *stream)
Not Zed's avatar
Not Zed committed
567
{
Matthew Barnes's avatar
Matthew Barnes committed
568
569
	CamelStream *filtered_stream;
	CamelMimeFilter *from_filter;
570
	gint i, res = 0;
Not Zed's avatar
Not Zed committed
571

Matthew Barnes's avatar
Matthew Barnes committed
572
	from_filter = camel_mime_filter_from_new ();
Matthew Barnes's avatar
Matthew Barnes committed
573
574
575
	filtered_stream = camel_stream_filter_new (stream);
	camel_stream_filter_add (
		CAMEL_STREAM_FILTER (filtered_stream), from_filter);
Matthew Barnes's avatar
Matthew Barnes committed
576
	g_object_unref (from_filter);
577

578
	for (i = 0; i < uids->len; i++) {
Not Zed's avatar
Not Zed committed
579
		CamelMimeMessage *message;
580
		gchar *from;
Not Zed's avatar
Not Zed committed
581

Matthew Barnes's avatar
Matthew Barnes committed
582
583
		/* FIXME camel_folder_get_message_sync() may block. */
		message = camel_folder_get_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
584
			folder, uids->pdata[i], NULL, NULL);
Not Zed's avatar
Not Zed committed
585
586
587
588
589
		if (message == NULL) {
			res = -1;
			break;
		}

590
591
		/* We need to flush after each stream write since we are
		 * writing to the same stream. */
Matthew Barnes's avatar
Matthew Barnes committed
592
		from = camel_mime_message_build_mbox_from (message);
Not Zed's avatar
Not Zed committed
593

Matthew Barnes's avatar
Matthew Barnes committed
594
595
		if (camel_stream_write_string (stream, from, NULL, NULL) == -1
		    || camel_stream_flush (stream, NULL, NULL) == -1
596
597
598
599
600
		    || camel_data_wrapper_write_to_stream_sync (
			(CamelDataWrapper *) message, (CamelStream *)
			filtered_stream, NULL, NULL) == -1
		    || camel_stream_flush (
			(CamelStream *) filtered_stream, NULL, NULL) == -1)
Not Zed's avatar
Not Zed committed
601
			res = -1;
602

Matthew Barnes's avatar
Matthew Barnes committed
603
		g_free (from);
Matthew Barnes's avatar
Matthew Barnes committed
604
		g_object_unref (message);
Not Zed's avatar
Not Zed committed
605
606
607
608
609

		if (res == -1)
			break;
	}

Matthew Barnes's avatar
Matthew Barnes committed
610
	g_object_unref (filtered_stream);
Not Zed's avatar
Not Zed committed
611
612
613
614

	return res;
}

615
616
617
618
619
static void
do_print_msg_to_file (GObject *source,
                      GAsyncResult *result,
                      gpointer user_data)
{
Dan Vrátil's avatar
Dan Vrátil committed
620
621
	EMailParser *parser;
	EMailPartList *parts_list;
622
623
624
	gchar *filename = user_data;
	EMailPrinter *printer;

Dan Vrátil's avatar
Dan Vrátil committed
625
626
627
628
	parser = E_MAIL_PARSER (source);
	parts_list = e_mail_parser_parse_finish (parser, result, NULL);

	printer = e_mail_printer_new (parts_list);
629
	e_mail_printer_set_export_filename (printer, filename);
630
631
	g_signal_connect_swapped (
		printer, "done",
632
633
		G_CALLBACK (g_object_unref), printer);

634
	e_mail_printer_print (printer, GTK_PRINT_OPERATION_ACTION_EXPORT, NULL);
635

Dan Vrátil's avatar
Dan Vrátil committed
636
	g_object_unref (parser);
637
638
}

639
static gboolean
640
641
642
em_utils_print_messages_to_file (CamelFolder *folder,
                                 const gchar *uid,
                                 const gchar *filename)
643
{
Dan Vrátil's avatar
Dan Vrátil committed
644
	EMailParser *parser;
645
	CamelMimeMessage *message;
646
647
	CamelStore *parent_store;
	CamelSession *session;
648
649
650
651
652

	message = camel_folder_get_message_sync (folder, uid, NULL, NULL);
	if (message == NULL)
		return FALSE;

653
654
655
	parent_store = camel_folder_get_parent_store (folder);
	session = camel_service_get_session (CAMEL_SERVICE (parent_store));

Dan Vrátil's avatar
Dan Vrátil committed
656
	parser = e_mail_parser_new (session);
657

658
659
660
661
	e_mail_parser_parse (
		parser, folder, uid, message,
		(GAsyncReadyCallback) do_print_msg_to_file,
		NULL, g_strdup (filename));
662
663
664
665

	return TRUE;
}

666
667
668
/* This kind of sucks, because for various reasons most callers need to run
 * synchronously in the gui thread, however this could take a long, blocking
 * time to run. */
Matthew Barnes's avatar
Matthew Barnes committed
669
static gint
670
671
em_utils_read_messages_from_stream (CamelFolder *folder,
                                    CamelStream *stream)
Not Zed's avatar
Not Zed committed
672
{
Matthew Barnes's avatar
Matthew Barnes committed
673
	CamelMimeParser *mp = camel_mime_parser_new ();
674
	gboolean success = TRUE;
Not Zed's avatar
Not Zed committed
675

Matthew Barnes's avatar
Matthew Barnes committed
676
677
	camel_mime_parser_scan_from (mp, TRUE);
	camel_mime_parser_init_with_stream (mp, stream, NULL);
Not Zed's avatar
Not Zed committed
678

Matthew Barnes's avatar
Matthew Barnes committed
679
	while (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM) {
Not Zed's avatar
Not Zed committed
680
		CamelMimeMessage *msg;
681
		gboolean success;
Not Zed's avatar
Not Zed committed
682
683

		/* NB: de-from filter, once written */
Matthew Barnes's avatar
Matthew Barnes committed
684
		msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
685
		if (!camel_mime_part_construct_from_parser_sync (
Matthew Barnes's avatar
Matthew Barnes committed
686
			(CamelMimePart *) msg, mp, NULL, NULL)) {
Matthew Barnes's avatar
Matthew Barnes committed
687
			g_object_unref (msg);
Not Zed's avatar
Not Zed committed
688
689
			break;
		}
690

Matthew Barnes's avatar
Matthew Barnes committed
691
692
		/* FIXME camel_folder_append_message_sync() may block. */
		success = camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
693
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
694
		g_object_unref (msg);
695

696
		if (!success)
Not Zed's avatar
Not Zed committed
697
			break;
698

Matthew Barnes's avatar
Matthew Barnes committed
699
		camel_mime_parser_step (mp, NULL, NULL);
Not Zed's avatar
Not Zed committed
700
	}
701

Matthew Barnes's avatar
Matthew Barnes committed
702
	g_object_unref (mp);
Not Zed's avatar
Not Zed committed
703

704
	return success ? 0 : -1;
Not Zed's avatar
Not Zed committed
705
706
707
708
709
710
711
}

/**
 * em_utils_selection_set_mailbox:
 * @data: selection data
 * @folder: folder containign messages to copy into the selection
 * @uids: uids of the messages to copy into the selection
712
 *
Not Zed's avatar
Not Zed committed
713
714
715
716
717
 * Creates a mailbox-format selection.
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 **/
void
Matthew Barnes's avatar
Matthew Barnes committed
718
719
720
em_utils_selection_set_mailbox (GtkSelectionData *data,
                                CamelFolder *folder,
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
721
{
Matthew Barnes's avatar
Matthew Barnes committed
722
	GByteArray *byte_array;
Not Zed's avatar
Not Zed committed
723
	CamelStream *stream;
724
725
726
	GdkAtom target;

	target = gtk_selection_data_get_target (data);
Not Zed's avatar
Not Zed committed
727

Matthew Barnes's avatar
Matthew Barnes committed
728
729
730
731
	byte_array = g_byte_array_new ();
	stream = camel_stream_mem_new_with_byte_array (byte_array);

	if (em_utils_write_messages_to_stream (folder, uids, stream) == 0)
Matthew Barnes's avatar
Matthew Barnes committed
732
		gtk_selection_data_set (
733
			data, target, 8,
Matthew Barnes's avatar
Matthew Barnes committed
734
			byte_array->data, byte_array->len);
Not Zed's avatar
Not Zed committed
735

Matthew Barnes's avatar
Matthew Barnes committed
736
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
737
738
739
740
}

/**
 * em_utils_selection_get_mailbox:
741
 * @selection_data: selection data
742
743
 * @folder:
 *
Not Zed's avatar
Not Zed committed
744
745
746
747
748
749
 * Receive a mailbox selection/dnd
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 * FIXME: Exceptions?
 **/
void
750
751
em_utils_selection_get_mailbox (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
752
753
{
	CamelStream *stream;
754
755
756
757
758
	const guchar *data;
	gint length;

	data = gtk_selection_data_get_data (selection_data);
	length = gtk_selection_data_get_length (selection_data);
Not Zed's avatar
Not Zed committed
759

760
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
761
762
763
764
		return;

	/* TODO: a stream mem with read-only access to existing data? */
	/* NB: Although copying would let us run this async ... which it should */
765
766
	stream = (CamelStream *)
		camel_stream_mem_new_with_buffer ((gchar *) data, length);
Matthew Barnes's avatar
Matthew Barnes committed
767
	em_utils_read_messages_from_stream (folder, stream);
Matthew Barnes's avatar
Matthew Barnes committed
768
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
769
770
}

Not Zed's avatar
Not Zed committed
771
772
/**
 * em_utils_selection_get_message:
773
 * @selection_data:
774
775
 * @folder:
 *
Not Zed's avatar
Not Zed committed
776
777
778
 * get a message/rfc822 data.
 **/
void
779
780
em_utils_selection_get_message (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
781
782
783
{
	CamelStream *stream;
	CamelMimeMessage *msg;
784
785
786
787
788
	const guchar *data;
	gint length;

	data = gtk_selection_data_get_data (selection_data);
	length = gtk_selection_data_get_length (selection_data);
Not Zed's avatar
Not Zed committed
789

790
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
791
792
		return;

793
	stream = (CamelStream *)
Matthew Barnes's avatar
Matthew Barnes committed
794
		camel_stream_mem_new_with_buffer ((gchar *) data, length);
Matthew Barnes's avatar
Matthew Barnes committed
795
	msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
796
	if (camel_data_wrapper_construct_from_stream_sync (
Matthew Barnes's avatar
Matthew Barnes committed
797
		(CamelDataWrapper *) msg, stream, NULL, NULL))
Matthew Barnes's avatar
Matthew Barnes committed
798
799
		/* FIXME camel_folder_append_message_sync() may block. */
		camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
800
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
801
802
	g_object_unref (msg);
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
803
804
}

Not Zed's avatar
Not Zed committed
805
806
/**
 * em_utils_selection_set_uidlist:
807
 * @selection_data: selection data
808
 * @folder:
809
810
 * @uids:
 *
811
 * Sets a "x-uid-list" format selection data.
Not Zed's avatar
Not Zed committed
812
813
 **/
void
814
em_utils_selection_set_uidlist (GtkSelectionData *selection_data,
815
                                CamelFolder *folder,
816
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
817
{
Matthew Barnes's avatar
Matthew Barnes committed
818
	GByteArray *array = g_byte_array_new ();
819
	GdkAtom target;
820
	gchar *folder_uri;
821
822
823
	gint ii;

	/* format: "uri1\0uid1\0uri2\0uid2\0...\0urin\0uidn\0" */
Not Zed's avatar
Not Zed committed
824

825
826
827
828
829
830
	if (CAMEL_IS_VEE_FOLDER (folder) &&
	    CAMEL_IS_VEE_STORE (camel_folder_get_parent_store (folder))) {
		CamelVeeFolder *vfolder = CAMEL_VEE_FOLDER (folder);
		CamelFolder *real_folder;
		CamelMessageInfo *info;
		gchar *real_uid;
831

832
833
834
835
836
837
		for (ii = 0; ii < uids->len; ii++) {
			info = camel_folder_get_message_info (folder, uids->pdata[ii]);
			if (!info) {
				g_warn_if_reached ();
				continue;
			}
838

839
840
			real_folder = camel_vee_folder_get_location (
				vfolder, (CamelVeeMessageInfo *) info, &real_uid);
Not Zed's avatar
Not Zed committed
841

842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
			if (real_folder) {
				folder_uri = e_mail_folder_uri_from_folder (real_folder);

				g_byte_array_append (array, (guchar *) folder_uri, strlen (folder_uri) + 1);
				g_byte_array_append (array, (guchar *) real_uid, strlen (real_uid) + 1);

				g_free (folder_uri);
			}

			camel_folder_free_message_info (folder, info);
		}
	} else {
		folder_uri = e_mail_folder_uri_from_folder (folder);

		for (ii = 0; ii < uids->len; ii++) {
			g_byte_array_append (array, (guchar *) folder_uri, strlen (folder_uri) + 1);
			g_byte_array_append (array, uids->pdata[ii], strlen (uids->pdata[ii]) + 1);
		}

		g_free (folder_uri);
	}
863

864
865
866
	target = gtk_selection_data_get_target (selection_data);
	gtk_selection_data_set (
		selection_data, target, 8, array->data, array->len);
Matthew Barnes's avatar
Matthew Barnes committed
867
	g_byte_array_free (array, TRUE);
Not Zed's avatar
Not Zed committed
868
869
870
871
872
}

/**
 * em_utils_selection_get_uidlist:
 * @data: selection data
873
 * @session: an #EMailSession
Not Zed's avatar
Not Zed committed
874
 * @move: do we delete the messages.
875
 *
Not Zed's avatar
Not Zed committed
876
 * Convert a uid list into a copy/move operation.
877
 *
Not Zed's avatar
Not Zed committed
878
 * Warning: Could take some time to run.
Not Zed's avatar
Not Zed committed
879
 **/
Not Zed's avatar
Not Zed committed
880
void
881
em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
882
                                EMailSession *session,
883
884
                                CamelFolder *dest,
                                gint move,
Matthew Barnes's avatar
Matthew Barnes committed
885
                                GCancellable *cancellable,
886
                                GError **error)
Not Zed's avatar
Not Zed committed
887
{
888
	/* format: "uri1\0uid1\0uri2\0uid2\0...\0urin\0uidn\0" */
889
	gchar *inptr, *inend;
890
	GPtrArray *items;
Not Zed's avatar
Not Zed committed
891
	CamelFolder *folder;
892
	const guchar *data;
893
894
895
896
897
	gint length, ii;
	GHashTable *uids_by_uri;
	GHashTableIter iter;
	gpointer key, value;
	GError *local_error = NULL;
898
899

	g_return_if_fail (selection_data != NULL);
900
	g_return_if_fail (E_IS_MAIL_SESSION (session));
Not Zed's avatar
Not Zed committed
901

902
903
904
905
	data = gtk_selection_data_get_data (selection_data);
	length = gtk_selection_data_get_length (selection_data);

	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
906
		return;
907

908
	items = g_ptr_array_new ();
Not Zed's avatar
Not Zed committed
909

910
911
	inptr = (gchar *) data;
	inend = (gchar *) (data + length);
Not Zed's avatar
Not Zed committed
912
	while (inptr < inend) {
913
		gchar *start = inptr;
Not Zed's avatar
Not Zed committed
914
915
916
917

		while (inptr < inend && *inptr)
			inptr++;

918
		g_ptr_array_add (items, g_strndup (start, inptr - start));
Not Zed's avatar
Not Zed committed
919
920
921
922

		inptr++;
	}

923
924
	if (items->len == 0) {
		g_ptr_array_free (items, TRUE);
Not Zed's avatar
Not Zed committed
925
		return;
Not Zed's avatar
Not Zed committed
926
927
	}

928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
	uids_by_uri = g_hash_table_new (g_str_hash, g_str_equal);
	for (ii = 0; ii < items->len - 1; ii += 2) {
		gchar *uri, *uid;
		GPtrArray *uids;

		uri = items->pdata[ii];
		uid = items->pdata[ii + 1];

		uids = g_hash_table_lookup (uids_by_uri, uri);
		if (!uids) {
			uids = g_ptr_array_new ();
			g_hash_table_insert (uids_by_uri, uri, uids);
		}

		/* reuse uid pointer from uids, do not strdup it */
		g_ptr_array_add (uids, uid);
Not Zed's avatar
Not Zed committed
944
945
	}

946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
	g_hash_table_iter_init (&iter, uids_by_uri);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		const gchar *uri = key;
		GPtrArray *uids = value;

		if (!local_error) {
			/* FIXME e_mail_session_uri_to_folder_sync() may block. */
			folder = e_mail_session_uri_to_folder_sync (
				session, uri, 0, cancellable, &local_error);
			if (folder) {
				/* FIXME camel_folder_transfer_messages_to_sync() may block. */
				camel_folder_transfer_messages_to_sync (
					folder, uids, dest, move, NULL, cancellable, &local_error);
				g_object_unref (folder);
			}
		}

		g_ptr_array_free (uids, TRUE);
	}

	g_hash_table_destroy (uids_by_uri);
	em_utils_uids_free (items);

	if (local_error)
		g_propagate_error (error, local_error);
Not Zed's avatar
Not Zed committed
971
972
}

973
static gchar *
974
975
976
977
978
em_utils_build_export_filename (CamelFolder *folder,
                                const gchar *uid,
                                const gchar *exporttype,
                                gint exportname,
                                const gchar *tmpdir)
979
980
{
	CamelMessageInfo *info;
Matthew Barnes's avatar
Matthew Barnes committed
981
982
	gchar *file = NULL;
	gchar *tmpfile = NULL;
983
984
985
986
	struct tm  *ts;
	gchar datetmp[15];

	/* Try to get the drop filename from the message or folder */
987
	info = camel_folder_get_message_info (folder, uid);
988
989
990
991
	if (info) {
		if (camel_message_info_subject (info)) {
			time_t reftime;
			reftime = camel_message_info_date_sent (info);
992
993
			if (exportname == DND_USE_DND_DATE) {
				reftime = time (NULL);
994
995
			}

996
			ts = localtime (&reftime);
997
			strftime (datetmp, 15, "%Y%m%d%H%M%S", ts);
998

999
			if (g_ascii_strcasecmp (exporttype, "pdf") == 0)
1000
1001
				file = g_strdup_printf ("%s_%s.pdf", datetmp, camel_message_info_subject (info));
			else
1002
				file = g_strdup_printf ("%s_%s", datetmp, camel_message_info_subject (info));