em-utils.c 55.2 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

44
45
#include <libebook/e-book.h>

Not Zed's avatar
Not Zed committed
46
#include "em-filter-editor.h"
Not Zed's avatar
Not Zed committed
47

Matthew Barnes's avatar
Matthew Barnes committed
48
#include <glib/gi18n.h>
49

50
51
#include <gio/gio.h>

Not Zed's avatar
Not Zed committed
52
53
54
#include "mail-mt.h"
#include "mail-ops.h"
#include "mail-tools.h"
55
#include "e-mail-tag-editor.h"
Not Zed's avatar
Not Zed committed
56

57
#include <libedataserver/e-data-server-util.h>
58
#include <libedataserver/e-flag.h>
59
#include <libedataserver/e-proxy.h>
60
61
62
#include "e-util/e-util.h"
#include "e-util/e-util-private.h"
#include "e-util/e-mktemp.h"
63
#include "e-util/e-account-utils.h"
64
#include "e-util/e-dialog-utils.h"
65
#include "e-util/e-alert-dialog.h"
66
#include "shell/e-shell.h"
67
#include "widgets/misc/e-attachment.h"
Not Zed's avatar
Not Zed committed
68
69
70
71

#include "em-utils.h"
#include "em-composer-utils.h"
#include "em-format-quote.h"
72
#include "e-mail-folder-utils.h"
73
#include "e-mail-local.h"
74
#include "e-mail-session.h"
Not Zed's avatar
Not Zed committed
75

76
77
78
79
/* XXX This is a dirty hack on a dirty hack.  We really need
 *     to rework or get rid of the functions that use this. */
extern const gchar *shell_builtin_backend;

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

Not Zed's avatar
Not Zed committed
84
85
#define d(x)

86
87
88
89
90
91
92
93
94
95
gboolean
em_utils_ask_open_many (GtkWindow *parent,
                        gint how_many)
{
	gchar *string;
	gboolean proceed;

	if (how_many < TOO_MANY)
		return TRUE;

96
97
98
99
100
101
102
103
	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);
104
105
106
107
108
109
110
111
	proceed = em_utils_prompt_user (
		parent, "/apps/evolution/mail/prompts/open_many",
		"mail:ask-open-many", string, NULL);
	g_free (string);

	return proceed;
}

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

139
140
141
142
	client = gconf_client_get_default ();

	if (promptkey && !gconf_client_get_bool (client, promptkey, NULL)) {
		g_object_unref (client);
Not Zed's avatar
Not Zed committed
143
		return TRUE;
144
	}
145

Matthew Barnes's avatar
Matthew Barnes committed
146
	va_start (ap, tag);
147
	alert = e_alert_new_valist (tag, ap);
Matthew Barnes's avatar
Matthew Barnes committed
148
	va_end (ap);
149

150
	dialog = e_alert_dialog_new (parent, alert);
151
	g_object_unref (alert);
152

153
154
	container = e_alert_dialog_get_content_area (E_ALERT_DIALOG (dialog));

Not Zed's avatar
Not Zed committed
155
	if (promptkey) {
156
157
158
159
		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
160
161
		gtk_widget_show (check);
	}
162

163
	button = gtk_dialog_run (GTK_DIALOG (dialog));
Not Zed's avatar
Not Zed committed
164
	if (promptkey)
165
166
167
168
		gconf_client_set_bool (
			client, promptkey,
			!gtk_toggle_button_get_active (
			GTK_TOGGLE_BUTTON (check)), NULL);
Not Zed's avatar
Not Zed committed
169

170
	gtk_widget_destroy (dialog);
171

172
173
	g_object_unref (client);

Not Zed's avatar
Not Zed committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	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;
191
	gint i;
192

Not Zed's avatar
Not Zed committed
193
194
	copy = g_ptr_array_new ();
	g_ptr_array_set_size (copy, uids->len);
195

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

Not Zed's avatar
Not Zed committed
199
200
201
202
203
204
205
206
207
208
209
210
	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)
{
211
	gint i;
212

Not Zed's avatar
Not Zed committed
213
214
	for (i = 0; i < uids->len; i++)
		g_free (uids->pdata[i]);
215

Not Zed's avatar
Not Zed committed
216
217
218
219
220
221
222
223
224
225
	g_ptr_array_free (uids, TRUE);
}

/**
 * em_utils_check_user_can_send_mail:
 *
 * Returns %TRUE if the user has an account configured (to send mail)
 * or %FALSE otherwise.
 **/
gboolean
226
em_utils_check_user_can_send_mail (void)
Not Zed's avatar
Not Zed committed
227
{
228
	EAccountList *account_list;
Not Zed's avatar
Not Zed committed
229
	EAccount *account;
230

231
232
	account_list = e_get_account_list ();

233
	if (e_list_length (E_LIST (account_list)) == 0)
234
		return FALSE;
235

236
	if (!(account = e_get_default_account ()))
Not Zed's avatar
Not Zed committed
237
		return FALSE;
238

Not Zed's avatar
Not Zed committed
239
240
241
	/* Check for a transport */
	if (!account->transport->url)
		return FALSE;
242

Not Zed's avatar
Not Zed committed
243
244
245
	return TRUE;
}

246
/* Editing Filters/Search Folders... */
Not Zed's avatar
Not Zed committed
247
248
249
250

static GtkWidget *filter_editor = NULL;

static void
251
em_filter_editor_response (GtkWidget *dialog, gint button, gpointer user_data)
Not Zed's avatar
Not Zed committed
252
{
Not Zed's avatar
Not Zed committed
253
	EMFilterContext *fc;
254

255
	if (button == GTK_RESPONSE_OK) {
256
		const gchar *config_dir;
257
		gchar *user;
258

259
		config_dir = mail_session_get_config_dir ();
Not Zed's avatar
Not Zed committed
260
		fc = g_object_get_data ((GObject *) dialog, "context");
261
		user = g_build_filename (config_dir, "filters.xml", NULL);
262
		e_rule_context_save ((ERuleContext *) fc, user);
Not Zed's avatar
Not Zed committed
263
264
		g_free (user);
	}
265

Not Zed's avatar
Not Zed committed
266
	gtk_widget_destroy (dialog);
267

Not Zed's avatar
Not Zed committed
268
269
270
	filter_editor = NULL;
}

Not Zed's avatar
Not Zed committed
271
272
273
static EMFilterSource em_filter_source_element_names[] = {
	{ "incoming", },
	{ "outgoing", },
274
	{ NULL }
Not Zed's avatar
Not Zed committed
275
276
277
278
279
};

/**
 * em_utils_edit_filters:
 * @parent: parent window
280
 * @backend: an #EMailBAckend
Not Zed's avatar
Not Zed committed
281
282
283
284
285
286
 *
 * 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
287
em_utils_edit_filters (GtkWidget *parent,
288
                       EMailBackend *backend)
Not Zed's avatar
Not Zed committed
289
{
290
	const gchar *config_dir;
291
	gchar *user, *system;
Not Zed's avatar
Not Zed committed
292
	EMFilterContext *fc;
293

294
	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
295

Not Zed's avatar
Not Zed committed
296
	if (filter_editor) {
297
		gtk_window_present (GTK_WINDOW (filter_editor));
Not Zed's avatar
Not Zed committed
298
299
		return;
	}
300

301
	config_dir = mail_session_get_config_dir ();
302

303
	fc = em_filter_context_new (backend);
304
	user = g_build_filename (config_dir, "filters.xml", NULL);
305
	system = g_build_filename (EVOLUTION_PRIVDATADIR, "filtertypes.xml", NULL);
306
	e_rule_context_load ((ERuleContext *) fc, system, user);
Not Zed's avatar
Not Zed committed
307
	g_free (user);
308
	g_free (system);
309

310
	if (((ERuleContext *) fc)->error) {
311
312
		e_mail_backend_submit_alert (
			backend, "mail:filter-load-error",
Matthew Barnes's avatar
Matthew Barnes committed
313
			((ERuleContext *) fc)->error, NULL);
Not Zed's avatar
Not Zed committed
314
315
		return;
	}
Not Zed's avatar
Not Zed committed
316
317
318
319
320
321

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

322
323
	filter_editor = (GtkWidget *) em_filter_editor_new (
		fc, em_filter_source_element_names);
Not Zed's avatar
Not Zed committed
324
	if (parent != NULL)
325
326
327
		gtk_window_set_transient_for (
			GTK_WINDOW (filter_editor),
			GTK_WINDOW (parent));
328

329
330
331
332
333
334
335
336
	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
337
338
339
	gtk_widget_show (GTK_WIDGET (filter_editor));
}

340
341
/*
 * Picked this from e-d-s/libedataserver/e-data.
342
343
 * But it allows more characters to occur in filenames, especially
 * when saving attachment.
344
345
346
347
348
349
350
 */
void
em_filename_make_safe (gchar *string)
{
	gchar *p, *ts;
	gunichar c;
#ifdef G_OS_WIN32
351
	const gchar *unsafe_chars = "/\":*?<>|\\#";
352
#else
353
	const gchar *unsafe_chars = "/#";
354
355
#endif

356
357
358
	g_return_if_fail (string != NULL);
	p = string;

Matthew Barnes's avatar
Matthew Barnes committed
359
	while (p && *p) {
360
361
362
363
364
365
366
		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
367
		if (!g_unichar_isprint (c) || ( c < 0xff && strchr (unsafe_chars, c&0xff ))) {
368
			while (ts<p)
369
370
371
372
373
				*ts++ = '_';
		}
	}
}

374
/* ********************************************************************** */
Not Zed's avatar
Not Zed committed
375
376
377
378
/* Flag-for-Followup... */

/**
 * em_utils_flag_for_followup:
Matthew Barnes's avatar
Matthew Barnes committed
379
 * @reader: an #EMailReader
Not Zed's avatar
Not Zed committed
380
381
382
383
384
385
386
 * @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
387
388
389
em_utils_flag_for_followup (EMailReader *reader,
                            CamelFolder *folder,
                            GPtrArray *uids)
Not Zed's avatar
Not Zed committed
390
{
391
	EShell *shell;
392
	EMailBackend *backend;
393
394
	EShellSettings *shell_settings;
	EShellBackend *shell_backend;
395
	EMFormatHTML *formatter;
396
	GtkWidget *editor;
397
	GtkWindow *window;
Matthew Barnes's avatar
Matthew Barnes committed
398
	CamelTag *tags;
399
	gint i;
400

Matthew Barnes's avatar
Matthew Barnes committed
401
	g_return_if_fail (E_IS_MAIL_READER (reader));
Not Zed's avatar
Not Zed committed
402
403
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
404

405
	window = e_mail_reader_get_window (reader);
406
407
408
	backend = e_mail_reader_get_backend (reader);

	editor = e_mail_tag_editor_new ();
409
	gtk_window_set_transient_for (GTK_WINDOW (editor), window);
410

411
	shell_backend = E_SHELL_BACKEND (backend);
412
413
414
415
	shell = e_shell_backend_get_shell (shell_backend);
	shell_settings = e_shell_get_shell_settings (shell);

	/* These settings come from the calendar module. */
416
	g_object_bind_property (
417
		shell_settings, "cal-use-24-hour-format",
418
419
420
		editor, "use-24-hour-format",
		G_BINDING_SYNC_CREATE);
	g_object_bind_property (
421
		shell_settings, "cal-week-start-day",
422
423
		editor, "week-start-day",
		G_BINDING_SYNC_CREATE);
424

Not Zed's avatar
Not Zed committed
425
426
	for (i = 0; i < uids->len; i++) {
		CamelMessageInfo *info;
427

Not Zed's avatar
Not Zed committed
428
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
429
430
431
432

		if (info == NULL)
			continue;

433
434
		e_mail_tag_editor_add_message (
			E_MAIL_TAG_EDITOR (editor),
Matthew Barnes's avatar
Matthew Barnes committed
435
436
437
			camel_message_info_from (info),
			camel_message_info_subject (info));

438
		camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
439
	}
440

Not Zed's avatar
Not Zed committed
441
442
443
	/* special-case... */
	if (uids->len == 1) {
		CamelMessageInfo *info;
Matthew Barnes's avatar
Matthew Barnes committed
444
		const gchar *message_uid;
445

Matthew Barnes's avatar
Matthew Barnes committed
446
447
		message_uid = g_ptr_array_index (uids, 0);
		info = camel_folder_get_message_info (folder, message_uid);
Not Zed's avatar
Not Zed committed
448
		if (info) {
Matthew Barnes's avatar
Matthew Barnes committed
449
			tags = (CamelTag *) camel_message_info_user_tags (info);
450
451

			if (tags)
452
453
				e_mail_tag_editor_set_tag_list (
					E_MAIL_TAG_EDITOR (editor), tags);
454
			camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
455
456
		}
	}
457

Matthew Barnes's avatar
Matthew Barnes committed
458
459
460
	if (gtk_dialog_run (GTK_DIALOG (editor)) != GTK_RESPONSE_OK)
		goto exit;

461
	tags = e_mail_tag_editor_get_tag_list (E_MAIL_TAG_EDITOR (editor));
Matthew Barnes's avatar
Matthew Barnes committed
462
463
464
465
466
467
468
469
	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
470
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
471
472
473
474
475
476
477
478

		if (info == NULL)
			continue;

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

479
		camel_folder_free_message_info (folder, info);
Matthew Barnes's avatar
Matthew Barnes committed
480
481
482
483
484
	}

	camel_folder_thaw (folder);
	camel_tag_list_free (&tags);

485
	formatter = e_mail_reader_get_formatter (reader);
486
	em_format_queue_redraw (EM_FORMAT (formatter));
Matthew Barnes's avatar
Matthew Barnes committed
487
488
489
490

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

Matthew Barnes's avatar
Matthew Barnes committed
492
	gtk_widget_destroy (GTK_WIDGET (editor));
Not Zed's avatar
Not Zed committed
493
494
495
496
497
498
499
500
501
502
503
504
}

/**
 * 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
505
506
507
em_utils_flag_for_followup_clear (GtkWindow *parent,
                                  CamelFolder *folder,
                                  GPtrArray *uids)
Not Zed's avatar
Not Zed committed
508
{
509
	gint i;
510

511
	g_return_if_fail (GTK_IS_WINDOW (parent));
Not Zed's avatar
Not Zed committed
512
513
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
514

Not Zed's avatar
Not Zed committed
515
516
	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
Matthew Barnes's avatar
Matthew Barnes committed
517
		CamelMessageInfo *mi = camel_folder_get_message_info (folder, uids->pdata[i]);
Not Zed's avatar
Not Zed committed
518
519
520
521
522

		if (mi) {
			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);
523
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
524
		}
Not Zed's avatar
Not Zed committed
525
	}
Matthew Barnes's avatar
Matthew Barnes committed
526

Not Zed's avatar
Not Zed committed
527
	camel_folder_thaw (folder);
528

Not Zed's avatar
Not Zed committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
	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
543
544
545
em_utils_flag_for_followup_completed (GtkWindow *parent,
                                      CamelFolder *folder,
                                      GPtrArray *uids)
Not Zed's avatar
Not Zed committed
546
{
547
548
	gchar *now;
	gint i;
549

550
	g_return_if_fail (GTK_IS_WINDOW (parent));
Not Zed's avatar
Not Zed committed
551
552
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
553

554
	now = camel_header_format_date (time (NULL), 0);
555

Not Zed's avatar
Not Zed committed
556
557
	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
558
		const gchar *tag;
Matthew Barnes's avatar
Matthew Barnes committed
559
		CamelMessageInfo *mi = camel_folder_get_message_info (folder, uids->pdata[i]);
Not Zed's avatar
Not Zed committed
560
561
562
563
564

		if (mi) {
			tag = camel_message_info_user_tag(mi, "follow-up");
			if (tag && tag[0])
				camel_message_info_set_user_tag(mi, "completed-on", now);
565
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
566
		}
Not Zed's avatar
Not Zed committed
567
	}
Matthew Barnes's avatar
Matthew Barnes committed
568

Not Zed's avatar
Not Zed committed
569
	camel_folder_thaw (folder);
570

Not Zed's avatar
Not Zed committed
571
	g_free (now);
572

Not Zed's avatar
Not Zed committed
573
574
575
	em_utils_uids_free (uids);
}

576
577
578
/* 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
579
static gint
580
581
582
em_utils_write_messages_to_stream (CamelFolder *folder,
                                   GPtrArray *uids,
                                   CamelStream *stream)
Not Zed's avatar
Not Zed committed
583
{
Matthew Barnes's avatar
Matthew Barnes committed
584
585
	CamelStream *filtered_stream;
	CamelMimeFilter *from_filter;
586
	gint i, res = 0;
Not Zed's avatar
Not Zed committed
587

Matthew Barnes's avatar
Matthew Barnes committed
588
	from_filter = camel_mime_filter_from_new ();
Matthew Barnes's avatar
Matthew Barnes committed
589
590
591
	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
592
	g_object_unref (from_filter);
593

Not Zed's avatar
Not Zed committed
594
595
	for (i=0; i<uids->len; i++) {
		CamelMimeMessage *message;
596
		gchar *from;
Not Zed's avatar
Not Zed committed
597

Matthew Barnes's avatar
Matthew Barnes committed
598
599
		/* FIXME camel_folder_get_message_sync() may block. */
		message = camel_folder_get_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
600
			folder, uids->pdata[i], NULL, NULL);
Not Zed's avatar
Not Zed committed
601
602
603
604
605
		if (message == NULL) {
			res = -1;
			break;
		}

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

Matthew Barnes's avatar
Matthew Barnes committed
610
611
		if (camel_stream_write_string (stream, from, NULL, NULL) == -1
		    || camel_stream_flush (stream, NULL, NULL) == -1
612
613
614
615
616
		    || 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
617
			res = -1;
618

Matthew Barnes's avatar
Matthew Barnes committed
619
		g_free (from);
Matthew Barnes's avatar
Matthew Barnes committed
620
		g_object_unref (message);
Not Zed's avatar
Not Zed committed
621
622
623
624
625

		if (res == -1)
			break;
	}

Matthew Barnes's avatar
Matthew Barnes committed
626
	g_object_unref (filtered_stream);
Not Zed's avatar
Not Zed committed
627
628
629
630

	return res;
}

631
632
633
/* 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
634
static gint
635
636
em_utils_read_messages_from_stream (CamelFolder *folder,
                                    CamelStream *stream)
Not Zed's avatar
Not Zed committed
637
{
Matthew Barnes's avatar
Matthew Barnes committed
638
	CamelMimeParser *mp = camel_mime_parser_new ();
639
	gboolean success = TRUE;
Not Zed's avatar
Not Zed committed
640

Matthew Barnes's avatar
Matthew Barnes committed
641
642
	camel_mime_parser_scan_from (mp, TRUE);
	camel_mime_parser_init_with_stream (mp, stream, NULL);
Not Zed's avatar
Not Zed committed
643

Matthew Barnes's avatar
Matthew Barnes committed
644
	while (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM) {
Not Zed's avatar
Not Zed committed
645
		CamelMimeMessage *msg;
646
		gboolean success;
Not Zed's avatar
Not Zed committed
647
648

		/* NB: de-from filter, once written */
Matthew Barnes's avatar
Matthew Barnes committed
649
		msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
650
		if (!camel_mime_part_construct_from_parser_sync (
Matthew Barnes's avatar
Matthew Barnes committed
651
			(CamelMimePart *) msg, mp, NULL, NULL)) {
Matthew Barnes's avatar
Matthew Barnes committed
652
			g_object_unref (msg);
Not Zed's avatar
Not Zed committed
653
654
			break;
		}
655

Matthew Barnes's avatar
Matthew Barnes committed
656
657
		/* FIXME camel_folder_append_message_sync() may block. */
		success = camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
658
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
659
		g_object_unref (msg);
660

661
		if (!success)
Not Zed's avatar
Not Zed committed
662
			break;
663

Matthew Barnes's avatar
Matthew Barnes committed
664
		camel_mime_parser_step (mp, NULL, NULL);
Not Zed's avatar
Not Zed committed
665
	}
666

Matthew Barnes's avatar
Matthew Barnes committed
667
	g_object_unref (mp);
Not Zed's avatar
Not Zed committed
668

669
	return success ? 0 : -1;
Not Zed's avatar
Not Zed committed
670
671
672
673
674
675
676
}

/**
 * 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
677
 *
Not Zed's avatar
Not Zed committed
678
679
680
681
682
 * 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
683
684
685
em_utils_selection_set_mailbox (GtkSelectionData *data,
                                CamelFolder *folder,
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
686
{
Matthew Barnes's avatar
Matthew Barnes committed
687
	GByteArray *byte_array;
Not Zed's avatar
Not Zed committed
688
	CamelStream *stream;
689
690
691
	GdkAtom target;

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

Matthew Barnes's avatar
Matthew Barnes committed
693
694
695
696
	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
697
		gtk_selection_data_set (
698
			data, target, 8,
Matthew Barnes's avatar
Matthew Barnes committed
699
			byte_array->data, byte_array->len);
Not Zed's avatar
Not Zed committed
700

Matthew Barnes's avatar
Matthew Barnes committed
701
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
702
703
704
705
}

/**
 * em_utils_selection_get_mailbox:
706
 * @selection_data: selection data
707
708
 * @folder:
 *
Not Zed's avatar
Not Zed committed
709
710
711
712
713
714
 * Receive a mailbox selection/dnd
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 * FIXME: Exceptions?
 **/
void
715
716
em_utils_selection_get_mailbox (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
717
718
{
	CamelStream *stream;
719
720
721
722
723
	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
724

725
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
726
727
728
729
		return;

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

Not Zed's avatar
Not Zed committed
736
737
/**
 * em_utils_selection_get_message:
738
 * @selection_data:
739
740
 * @folder:
 *
Not Zed's avatar
Not Zed committed
741
742
743
 * get a message/rfc822 data.
 **/
void
744
745
em_utils_selection_get_message (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
746
747
748
{
	CamelStream *stream;
	CamelMimeMessage *msg;
749
750
751
752
753
	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
754

755
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
756
757
		return;

758
	stream = (CamelStream *)
Matthew Barnes's avatar
Matthew Barnes committed
759
		camel_stream_mem_new_with_buffer ((gchar *) data, length);
Matthew Barnes's avatar
Matthew Barnes committed
760
	msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
761
	if (camel_data_wrapper_construct_from_stream_sync (
Matthew Barnes's avatar
Matthew Barnes committed
762
		(CamelDataWrapper *) msg, stream, NULL, NULL))
Matthew Barnes's avatar
Matthew Barnes committed
763
764
		/* FIXME camel_folder_append_message_sync() may block. */
		camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
765
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
766
767
	g_object_unref (msg);
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
768
769
}

Not Zed's avatar
Not Zed committed
770
771
/**
 * em_utils_selection_set_uidlist:
772
 * @selection_data: selection data
773
 * @folder:
774
775
 * @uids:
 *
776
 * Sets a "x-uid-list" format selection data.
Not Zed's avatar
Not Zed committed
777
778
 **/
void
779
em_utils_selection_set_uidlist (GtkSelectionData *selection_data,
780
                                CamelFolder *folder,
781
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
782
{
Matthew Barnes's avatar
Matthew Barnes committed
783
	GByteArray *array = g_byte_array_new ();
784
	GdkAtom target;
785
	gchar *folder_uri;
786
	gint i;
Not Zed's avatar
Not Zed committed
787
788

	/* format: "uri\0uid1\0uid2\0uid3\0...\0uidn\0" */
789

790
791
792
793
	folder_uri = e_mail_folder_uri_from_folder (folder);

	g_byte_array_append (
		array, (guchar *) folder_uri, strlen (folder_uri) + 1);
Not Zed's avatar
Not Zed committed
794
795

	for (i=0; i<uids->len; i++)
Matthew Barnes's avatar
Matthew Barnes committed
796
		g_byte_array_append (array, uids->pdata[i], strlen (uids->pdata[i])+1);
797

798
799
800
	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
801
	g_byte_array_free (array, TRUE);
802
803

	g_free (folder_uri);
Not Zed's avatar
Not Zed committed
804
805
806
807
808
}

/**
 * em_utils_selection_get_uidlist:
 * @data: selection data
809
 * @session: an #EMailSession
Not Zed's avatar
Not Zed committed
810
 * @move: do we delete the messages.
811
 *
Not Zed's avatar
Not Zed committed
812
 * Convert a uid list into a copy/move operation.
813
 *
Not Zed's avatar
Not Zed committed
814
 * Warning: Could take some time to run.
Not Zed's avatar
Not Zed committed
815
 **/
Not Zed's avatar
Not Zed committed
816
void
817
em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
818
                                EMailSession *session,
819
820
                                CamelFolder *dest,
                                gint move,
Matthew Barnes's avatar
Matthew Barnes committed
821
                                GCancellable *cancellable,
822
                                GError **error)
Not Zed's avatar
Not Zed committed
823
824
{
	/* format: "uri\0uid1\0uid2\0uid3\0...\0uidn" */
825
	gchar *inptr, *inend;
Not Zed's avatar
Not Zed committed
826
	GPtrArray *uids;
Not Zed's avatar
Not Zed committed
827
	CamelFolder *folder;
828
829
830
831
	const guchar *data;
	gint length;

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

834
835
836
837
	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
838
		return;
839

Matthew Barnes's avatar
Matthew Barnes committed
840
	uids = g_ptr_array_new ();
Not Zed's avatar
Not Zed committed
841

842
843
	inptr = (gchar *) data;
	inend = (gchar *) (data + length);
Not Zed's avatar
Not Zed committed
844
	while (inptr < inend) {
845
		gchar *start = inptr;
Not Zed's avatar
Not Zed committed
846
847
848
849

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

850
		if (start > (gchar *) data)
Matthew Barnes's avatar
Matthew Barnes committed
851
			g_ptr_array_add (uids, g_strndup (start, inptr-start));
Not Zed's avatar
Not Zed committed
852
853
854
855
856

		inptr++;
	}

	if (uids->len == 0) {
Matthew Barnes's avatar
Matthew Barnes committed
857
		g_ptr_array_free (uids, TRUE);
Not Zed's avatar
Not Zed committed
858
		return;
Not Zed's avatar
Not Zed committed
859
860
	}

861
862
863
	/* FIXME e_mail_session_uri_to_folder_sync() may block. */
	folder = e_mail_session_uri_to_folder_sync (
		session, (gchar *) data, 0, cancellable, error);
Not Zed's avatar
Not Zed committed
864
	if (folder) {
Matthew Barnes's avatar
Matthew Barnes committed
865
866
867
		/* FIXME camel_folder_transfer_messages_to_sync() may block. */
		camel_folder_transfer_messages_to_sync (
			folder, uids, dest, move, NULL, cancellable, error);
Matthew Barnes's avatar
Matthew Barnes committed
868
		g_object_unref (folder);
Not Zed's avatar
Not Zed committed
869
870
	}

Matthew Barnes's avatar
Matthew Barnes committed
871
	em_utils_uids_free (uids);
Not Zed's avatar
Not Zed committed
872
873
874
875
}

/**
 * em_utils_selection_set_urilist:
876
877
878
879
 * @data:
 * @folder:
 * @uids:
 *
Not Zed's avatar
Not Zed committed
880
881
882
883
884
 * Set the selection data @data to a uri which points to a file, which is
 * a berkely mailbox format mailbox.  The file is automatically cleaned
 * up when the application quits.
 **/
void
885
886
887
em_utils_selection_set_urilist (GtkSelectionData *data,
                                CamelFolder *folder,
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
888
{
889
	gchar *tmpdir;
Not Zed's avatar
Not Zed committed
890
	CamelStream *fstream;
891
892
	gchar *uri, *file = NULL, *tmpfile;
	gint fd;
Not Zed's avatar
Not Zed committed
893
	CamelMessageInfo *info;
Not Zed's avatar
Not Zed committed
894
895
896
897
898

	tmpdir = e_mkdtemp("drag-n-drop-XXXXXX");
	if (tmpdir == NULL)
		return;

Not Zed's avatar
Not Zed committed
899
900
	/* Try to get the drop filename from the message or folder */
	if (uids->len == 1) {
Matthew Barnes's avatar
Matthew Barnes committed
901
902
903
904
		const gchar *message_uid;

		message_uid = g_ptr_array_index (uids, 0);
		info = camel_folder_get_message_info (folder, message_uid);
Not Zed's avatar
Not Zed committed
905
		if (info) {
Matthew Barnes's avatar
Matthew Barnes committed
906
			file = g_strdup (camel_message_info_subject (info));
907
			camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
908
909
910
911
912
913
		}
	}

	/* TODO: Handle conflicts? */
	if (file == NULL) {
		/* Drop filename for messages from a mailbox */
Matthew Barnes's avatar
Matthew Barnes committed
914
915
		file = g_strdup_printf (
			_("Messages from %s"),
916
			camel_folder_get_display_name (folder));
Not Zed's avatar
Not Zed committed
917
918
	}

Matthew Barnes's avatar
Matthew Barnes committed
919
	e_filename_make_safe (file);
Not Zed's avatar
Not Zed committed
920

Matthew Barnes's avatar
Matthew Barnes committed
921
922
923
	tmpfile = g_build_filename (tmpdir, file, NULL);
	g_free (tmpdir);
	g_free (file);
924

Matthew Barnes's avatar
Matthew Barnes committed
925
	fd = g_open (tmpfile, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0666);
926
	if (fd == -1) {
Matthew Barnes's avatar
Matthew Barnes committed
927
		g_free (tmpfile);
Not Zed's avatar
Not Zed committed
928
		return;
929
930
	}

Matthew Barnes's avatar
Matthew Barnes committed
931
932
933
	uri = g_filename_to_uri (tmpfile, NULL, NULL);
	g_free (tmpfile);
	fstream = camel_stream_fs_new_with_fd (fd);
Not Zed's avatar
Not Zed committed
934
	if (fstream) {
Matthew Barnes's avatar
Matthew Barnes committed
935
		if (em_utils_write_messages_to_stream (folder, uids, fstream) == 0) {
936
			/* terminate with \r\n to be compliant with the spec */
937
			gchar *uri_crlf = g_strconcat(uri, "\r\n", NULL);
938
			GdkAtom target;
939

940
			target = gtk_selection_data_get_target (data);
941
942
943
			gtk_selection_data_set (
				data, target, 8, (guchar *)
				uri_crlf, strlen (uri_crlf));
Matthew Barnes's avatar
Matthew Barnes committed
944
			g_free (uri_crlf);
945
		}
Not Zed's avatar
Not Zed committed
946

Matthew Barnes's avatar
Matthew Barnes committed
947
		g_object_unref (fstream);
Jules Colding's avatar
Jules Colding committed
948
	} else
Matthew Barnes's avatar
Matthew Barnes committed
949
		close (fd);
Jules Colding's avatar
Jules Colding committed
950

Matthew Barnes's avatar
Matthew Barnes committed
951
	g_free (uri);