em-utils.c 58 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
44
#include <glib.h>
#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

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

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

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

51
52
#include <gio/gio.h>

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

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

#include "em-utils.h"
#include "em-composer-utils.h"
#include "em-format-quote.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
{
	GtkWidget *mbox, *check = NULL;
132
	GtkWidget *container;
Not Zed's avatar
Not Zed committed
133
	va_list ap;
134
	gint button;
135
	GConfClient *client;
136
	EAlert *alert = NULL;
Not Zed's avatar
Not Zed committed
137

138
139
140
141
	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
142
		return TRUE;
143
	}
144

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

149
150
	mbox = e_alert_dialog_new (parent, alert);
	g_object_unref (alert);
151

Not Zed's avatar
Not Zed committed
152
	if (promptkey) {
153
		check = gtk_check_button_new_with_mnemonic (_("_Do not show this message again"));
154
		container = gtk_dialog_get_content_area (GTK_DIALOG (mbox));
155
		gtk_box_pack_start (GTK_BOX (container), check, FALSE, FALSE, 0);
Not Zed's avatar
Not Zed committed
156
157
		gtk_widget_show (check);
	}
158

Not Zed's avatar
Not Zed committed
159
	button = gtk_dialog_run ((GtkDialog *) mbox);
Not Zed's avatar
Not Zed committed
160
	if (promptkey)
161
162
163
164
		gconf_client_set_bool (
			client, promptkey,
			!gtk_toggle_button_get_active (
			GTK_TOGGLE_BUTTON (check)), NULL);
Not Zed's avatar
Not Zed committed
165

Matthew Barnes's avatar
Matthew Barnes committed
166
	gtk_widget_destroy (mbox);
167

168
169
	g_object_unref (client);

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

Not Zed's avatar
Not Zed committed
189
190
	copy = g_ptr_array_new ();
	g_ptr_array_set_size (copy, uids->len);
191

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

Not Zed's avatar
Not Zed committed
195
196
197
198
199
200
201
202
203
204
205
206
	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)
{
207
	gint i;
208

Not Zed's avatar
Not Zed committed
209
210
	for (i = 0; i < uids->len; i++)
		g_free (uids->pdata[i]);
211

Not Zed's avatar
Not Zed committed
212
213
214
215
216
217
218
219
220
221
	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
222
em_utils_check_user_can_send_mail (void)
Not Zed's avatar
Not Zed committed
223
{
224
	EAccountList *account_list;
Not Zed's avatar
Not Zed committed
225
	EAccount *account;
226

227
228
	account_list = e_get_account_list ();

229
230
	if (e_list_length ((EList *) account_list) == 0)
		return FALSE;
231

232
	if (!(account = e_get_default_account ()))
Not Zed's avatar
Not Zed committed
233
		return FALSE;
234

Not Zed's avatar
Not Zed committed
235
236
237
	/* Check for a transport */
	if (!account->transport->url)
		return FALSE;
238

Not Zed's avatar
Not Zed committed
239
240
241
	return TRUE;
}

242
/* Editing Filters/Search Folders... */
Not Zed's avatar
Not Zed committed
243
244
245
246

static GtkWidget *filter_editor = NULL;

static void
247
em_filter_editor_response (GtkWidget *dialog, gint button, gpointer user_data)
Not Zed's avatar
Not Zed committed
248
{
Not Zed's avatar
Not Zed committed
249
	EMFilterContext *fc;
250

251
	if (button == GTK_RESPONSE_OK) {
252
		const gchar *config_dir;
253
		gchar *user;
254

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

Not Zed's avatar
Not Zed committed
262
	gtk_widget_destroy (dialog);
263

Not Zed's avatar
Not Zed committed
264
265
266
	filter_editor = NULL;
}

Not Zed's avatar
Not Zed committed
267
268
269
static EMFilterSource em_filter_source_element_names[] = {
	{ "incoming", },
	{ "outgoing", },
270
	{ NULL }
Not Zed's avatar
Not Zed committed
271
272
273
274
275
};

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

291
	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
292

Not Zed's avatar
Not Zed committed
293
	if (filter_editor) {
294
		gtk_window_present (GTK_WINDOW (filter_editor));
Not Zed's avatar
Not Zed committed
295
296
		return;
	}
297

298
	config_dir = mail_session_get_config_dir ();
299
	session = e_mail_backend_get_session (backend);
300

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

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

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

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

326
	gtk_window_set_title (GTK_WINDOW (filter_editor), _("Message Filters"));
Matthew Barnes's avatar
Matthew Barnes committed
327
	g_object_set_data_full ((GObject *) filter_editor, "context", fc, (GDestroyNotify) g_object_unref);
Not Zed's avatar
Not Zed committed
328
	g_signal_connect (filter_editor, "response", G_CALLBACK (em_filter_editor_response), NULL);
Not Zed's avatar
Not Zed committed
329
330
331
	gtk_widget_show (GTK_WIDGET (filter_editor));
}

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

347
348
349
	g_return_if_fail (string != NULL);
	p = string;

Matthew Barnes's avatar
Matthew Barnes committed
350
	while (p && *p) {
351
352
353
354
355
356
357
		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
358
		if (!g_unichar_isprint (c) || ( c < 0xff && strchr (unsafe_chars, c&0xff ))) {
359
			while (ts<p)
360
361
362
363
364
				*ts++ = '_';
		}
	}
}

365
/* ********************************************************************** */
Not Zed's avatar
Not Zed committed
366
367
368
369
/* Flag-for-Followup... */

/**
 * em_utils_flag_for_followup:
Matthew Barnes's avatar
Matthew Barnes committed
370
 * @reader: an #EMailReader
Not Zed's avatar
Not Zed committed
371
372
373
374
375
376
377
 * @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
378
379
380
em_utils_flag_for_followup (EMailReader *reader,
                            CamelFolder *folder,
                            GPtrArray *uids)
Not Zed's avatar
Not Zed committed
381
{
382
	EShell *shell;
383
	EMailBackend *backend;
384
385
	EShellSettings *shell_settings;
	EShellBackend *shell_backend;
386
	EMFormatHTML *formatter;
387
	GtkWidget *editor;
388
	GtkWindow *window;
Matthew Barnes's avatar
Matthew Barnes committed
389
	CamelTag *tags;
390
	gint i;
391

Matthew Barnes's avatar
Matthew Barnes committed
392
	g_return_if_fail (E_IS_MAIL_READER (reader));
Not Zed's avatar
Not Zed committed
393
394
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
395

396
	window = e_mail_reader_get_window (reader);
397
398
399
	backend = e_mail_reader_get_backend (reader);

	editor = e_mail_tag_editor_new ();
400
	gtk_window_set_transient_for (GTK_WINDOW (editor), window);
401

402
	shell_backend = E_SHELL_BACKEND (backend);
403
404
405
406
	shell = e_shell_backend_get_shell (shell_backend);
	shell_settings = e_shell_get_shell_settings (shell);

	/* These settings come from the calendar module. */
407
	g_object_bind_property (
408
		shell_settings, "cal-use-24-hour-format",
409
410
411
		editor, "use-24-hour-format",
		G_BINDING_SYNC_CREATE);
	g_object_bind_property (
412
		shell_settings, "cal-week-start-day",
413
414
		editor, "week-start-day",
		G_BINDING_SYNC_CREATE);
415

Not Zed's avatar
Not Zed committed
416
417
	for (i = 0; i < uids->len; i++) {
		CamelMessageInfo *info;
418

Not Zed's avatar
Not Zed committed
419
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
420
421
422
423

		if (info == NULL)
			continue;

424
425
		e_mail_tag_editor_add_message (
			E_MAIL_TAG_EDITOR (editor),
Matthew Barnes's avatar
Matthew Barnes committed
426
427
428
			camel_message_info_from (info),
			camel_message_info_subject (info));

429
		camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
430
	}
431

Not Zed's avatar
Not Zed committed
432
433
434
	/* special-case... */
	if (uids->len == 1) {
		CamelMessageInfo *info;
435

Not Zed's avatar
Not Zed committed
436
437
		info = camel_folder_get_message_info (folder, uids->pdata[0]);
		if (info) {
Matthew Barnes's avatar
Matthew Barnes committed
438
			tags = (CamelTag *) camel_message_info_user_tags (info);
439
440

			if (tags)
441
442
				e_mail_tag_editor_set_tag_list (
					E_MAIL_TAG_EDITOR (editor), tags);
443
			camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
444
445
		}
	}
446

Matthew Barnes's avatar
Matthew Barnes committed
447
448
449
	if (gtk_dialog_run (GTK_DIALOG (editor)) != GTK_RESPONSE_OK)
		goto exit;

450
	tags = e_mail_tag_editor_get_tag_list (E_MAIL_TAG_EDITOR (editor));
Matthew Barnes's avatar
Matthew Barnes committed
451
452
453
454
455
456
457
458
	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
459
		info = camel_folder_get_message_info (folder, uids->pdata[i]);
Matthew Barnes's avatar
Matthew Barnes committed
460
461
462
463
464
465
466
467

		if (info == NULL)
			continue;

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

468
		camel_folder_free_message_info (folder, info);
Matthew Barnes's avatar
Matthew Barnes committed
469
470
471
472
473
	}

	camel_folder_thaw (folder);
	camel_tag_list_free (&tags);

474
	formatter = e_mail_reader_get_formatter (reader);
475
	em_format_queue_redraw (EM_FORMAT (formatter));
Matthew Barnes's avatar
Matthew Barnes committed
476
477
478
479

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

Matthew Barnes's avatar
Matthew Barnes committed
481
	gtk_widget_destroy (GTK_WIDGET (editor));
Not Zed's avatar
Not Zed committed
482
483
484
485
486
487
488
489
490
491
492
493
}

/**
 * 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
494
em_utils_flag_for_followup_clear (GtkWindow *parent, CamelFolder *folder, GPtrArray *uids)
Not Zed's avatar
Not Zed committed
495
{
496
	gint i;
497

498
	g_return_if_fail (GTK_IS_WINDOW (parent));
Not Zed's avatar
Not Zed committed
499
500
	g_return_if_fail (CAMEL_IS_FOLDER (folder));
	g_return_if_fail (uids != NULL);
501

Not Zed's avatar
Not Zed committed
502
503
	camel_folder_freeze (folder);
	for (i = 0; i < uids->len; i++) {
Matthew Barnes's avatar
Matthew Barnes committed
504
		CamelMessageInfo *mi = camel_folder_get_message_info (folder, uids->pdata[i]);
Not Zed's avatar
Not Zed committed
505
506
507
508
509

		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);
510
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
511
		}
Not Zed's avatar
Not Zed committed
512
	}
Matthew Barnes's avatar
Matthew Barnes committed
513

Not Zed's avatar
Not Zed committed
514
	camel_folder_thaw (folder);
515

Not Zed's avatar
Not Zed committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
	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
530
em_utils_flag_for_followup_completed (GtkWindow *parent, CamelFolder *folder, GPtrArray *uids)
Not Zed's avatar
Not Zed committed
531
{
532
533
	gchar *now;
	gint i;
534

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

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

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

		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);
550
			camel_folder_free_message_info (folder, mi);
Not Zed's avatar
Not Zed committed
551
		}
Not Zed's avatar
Not Zed committed
552
	}
Matthew Barnes's avatar
Matthew Barnes committed
553

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

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

Not Zed's avatar
Not Zed committed
558
559
560
561
562
	em_utils_uids_free (uids);
}

/* 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
Matthew Barnes's avatar
Matthew Barnes committed
564
em_utils_write_messages_to_stream (CamelFolder *folder, GPtrArray *uids, CamelStream *stream)
Not Zed's avatar
Not Zed committed
565
{
Matthew Barnes's avatar
Matthew Barnes committed
566
567
	CamelStream *filtered_stream;
	CamelMimeFilter *from_filter;
568
	gint i, res = 0;
Not Zed's avatar
Not Zed committed
569

Matthew Barnes's avatar
Matthew Barnes committed
570
	from_filter = camel_mime_filter_from_new ();
Matthew Barnes's avatar
Matthew Barnes committed
571
572
573
	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
574
	g_object_unref (from_filter);
575

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

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

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

Matthew Barnes's avatar
Matthew Barnes committed
591
592
593
594
		if (camel_stream_write_string (stream, from, NULL, NULL) == -1
		    || camel_stream_flush (stream, NULL, NULL) == -1
		    || 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
595
			res = -1;
596

Matthew Barnes's avatar
Matthew Barnes committed
597
		g_free (from);
Matthew Barnes's avatar
Matthew Barnes committed
598
		g_object_unref (message);
Not Zed's avatar
Not Zed committed
599
600
601
602
603

		if (res == -1)
			break;
	}

Matthew Barnes's avatar
Matthew Barnes committed
604
	g_object_unref (filtered_stream);
Not Zed's avatar
Not Zed committed
605
606
607
608
609
610

	return res;
}

/* 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
611
static gint
Matthew Barnes's avatar
Matthew Barnes committed
612
em_utils_read_messages_from_stream (CamelFolder *folder, CamelStream *stream)
Not Zed's avatar
Not Zed committed
613
{
Matthew Barnes's avatar
Matthew Barnes committed
614
	CamelMimeParser *mp = camel_mime_parser_new ();
615
	gboolean success = TRUE;
Not Zed's avatar
Not Zed committed
616

Matthew Barnes's avatar
Matthew Barnes committed
617
618
	camel_mime_parser_scan_from (mp, TRUE);
	camel_mime_parser_init_with_stream (mp, stream, NULL);
Not Zed's avatar
Not Zed committed
619

Matthew Barnes's avatar
Matthew Barnes committed
620
	while (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM) {
Not Zed's avatar
Not Zed committed
621
		CamelMimeMessage *msg;
622
		gboolean success;
Not Zed's avatar
Not Zed committed
623
624

		/* NB: de-from filter, once written */
Matthew Barnes's avatar
Matthew Barnes committed
625
		msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
626
627
		if (!camel_mime_part_construct_from_parser_sync (
			(CamelMimePart *)msg, mp, NULL, NULL)) {
Matthew Barnes's avatar
Matthew Barnes committed
628
			g_object_unref (msg);
Not Zed's avatar
Not Zed committed
629
630
			break;
		}
631

Matthew Barnes's avatar
Matthew Barnes committed
632
633
		/* FIXME camel_folder_append_message_sync() may block. */
		success = camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
634
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
635
		g_object_unref (msg);
636

637
		if (!success)
Not Zed's avatar
Not Zed committed
638
			break;
639

Matthew Barnes's avatar
Matthew Barnes committed
640
		camel_mime_parser_step (mp, NULL, NULL);
Not Zed's avatar
Not Zed committed
641
	}
642

Matthew Barnes's avatar
Matthew Barnes committed
643
	g_object_unref (mp);
Not Zed's avatar
Not Zed committed
644

645
	return success ? 0 : -1;
Not Zed's avatar
Not Zed committed
646
647
648
649
650
651
652
}

/**
 * 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
653
 *
Not Zed's avatar
Not Zed committed
654
655
656
657
658
 * 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
659
660
661
em_utils_selection_set_mailbox (GtkSelectionData *data,
                                CamelFolder *folder,
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
662
{
Matthew Barnes's avatar
Matthew Barnes committed
663
	GByteArray *byte_array;
Not Zed's avatar
Not Zed committed
664
	CamelStream *stream;
665
666
667
	GdkAtom target;

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

Matthew Barnes's avatar
Matthew Barnes committed
669
670
671
672
	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
673
		gtk_selection_data_set (
674
			data, target, 8,
Matthew Barnes's avatar
Matthew Barnes committed
675
			byte_array->data, byte_array->len);
Not Zed's avatar
Not Zed committed
676

Matthew Barnes's avatar
Matthew Barnes committed
677
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
678
679
680
681
}

/**
 * em_utils_selection_get_mailbox:
682
 * @selection_data: selection data
683
684
 * @folder:
 *
Not Zed's avatar
Not Zed committed
685
686
687
688
689
690
 * Receive a mailbox selection/dnd
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 * FIXME: Exceptions?
 **/
void
691
692
em_utils_selection_get_mailbox (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
693
694
{
	CamelStream *stream;
695
696
697
698
699
	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
700

701
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
702
703
704
705
		return;

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

Not Zed's avatar
Not Zed committed
712
713
/**
 * em_utils_selection_get_message:
714
 * @selection_data:
715
716
 * @folder:
 *
Not Zed's avatar
Not Zed committed
717
718
719
 * get a message/rfc822 data.
 **/
void
720
721
em_utils_selection_get_message (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
722
723
724
{
	CamelStream *stream;
	CamelMimeMessage *msg;
725
726
727
728
729
	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
730

731
	if (data == NULL || length == -1)
Not Zed's avatar
Not Zed committed
732
733
		return;

734
735
	stream = (CamelStream *)
		camel_stream_mem_new_with_buffer ((gchar *)data, length);
Matthew Barnes's avatar
Matthew Barnes committed
736
	msg = camel_mime_message_new ();
Matthew Barnes's avatar
Matthew Barnes committed
737
738
739
740
	if (camel_data_wrapper_construct_from_stream_sync (
		(CamelDataWrapper *)msg, stream, NULL, NULL))
		/* FIXME camel_folder_append_message_sync() may block. */
		camel_folder_append_message_sync (
Matthew Barnes's avatar
Matthew Barnes committed
741
			folder, msg, NULL, NULL, NULL, NULL);
Matthew Barnes's avatar
Matthew Barnes committed
742
743
	g_object_unref (msg);
	g_object_unref (stream);
Not Zed's avatar
Not Zed committed
744
745
}

Not Zed's avatar
Not Zed committed
746
747
/**
 * em_utils_selection_set_uidlist:
748
 * @selection_data: selection data
Not Zed's avatar
Not Zed committed
749
 * @uri:
750
751
 * @uids:
 *
752
 * Sets a "x-uid-list" format selection data.
Not Zed's avatar
Not Zed committed
753
754
755
756
 *
 * FIXME: be nice if this could take a folder argument rather than uri
 **/
void
757
758
759
em_utils_selection_set_uidlist (GtkSelectionData *selection_data,
                                const gchar *uri,
                                GPtrArray *uids)
Not Zed's avatar
Not Zed committed
760
{
Matthew Barnes's avatar
Matthew Barnes committed
761
	GByteArray *array = g_byte_array_new ();
762
	GdkAtom target;
763
	gint i;
Not Zed's avatar
Not Zed committed
764
765

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

Matthew Barnes's avatar
Matthew Barnes committed
767
	g_byte_array_append (array, (guchar *)uri, strlen (uri)+1);
Not Zed's avatar
Not Zed committed
768
769

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

772
773
774
	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
775
	g_byte_array_free (array, TRUE);
Not Zed's avatar
Not Zed committed
776
777
778
779
780
}

/**
 * em_utils_selection_get_uidlist:
 * @data: selection data
781
 * @session: an #EMailSession
Not Zed's avatar
Not Zed committed
782
 * @move: do we delete the messages.
783
 *
Not Zed's avatar
Not Zed committed
784
 * Convert a uid list into a copy/move operation.
785
 *
Not Zed's avatar
Not Zed committed
786
 * Warning: Could take some time to run.
Not Zed's avatar
Not Zed committed
787
 **/
Not Zed's avatar
Not Zed committed
788
void
789
em_utils_selection_get_uidlist (GtkSelectionData *selection_data,
790
                                EMailSession *session,
791
792
                                CamelFolder *dest,
                                gint move,
Matthew Barnes's avatar
Matthew Barnes committed
793
                                GCancellable *cancellable,
794
                                GError **error)
Not Zed's avatar
Not Zed committed
795
796
{
	/* format: "uri\0uid1\0uid2\0uid3\0...\0uidn" */
797
	gchar *inptr, *inend;
Not Zed's avatar
Not Zed committed
798
	GPtrArray *uids;
Not Zed's avatar
Not Zed committed
799
	CamelFolder *folder;
800
801
802
803
	const guchar *data;
	gint length;

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

806
807
808
809
	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
810
		return;
811

Matthew Barnes's avatar
Matthew Barnes committed
812
	uids = g_ptr_array_new ();
Not Zed's avatar
Not Zed committed
813

814
815
	inptr = (gchar *) data;
	inend = (gchar *) (data + length);
Not Zed's avatar
Not Zed committed
816
	while (inptr < inend) {
817
		gchar *start = inptr;
Not Zed's avatar
Not Zed committed
818
819
820
821

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

822
		if (start > (gchar *) data)
Matthew Barnes's avatar
Matthew Barnes committed
823
			g_ptr_array_add (uids, g_strndup (start, inptr-start));
Not Zed's avatar
Not Zed committed
824
825
826
827
828

		inptr++;
	}

	if (uids->len == 0) {
Matthew Barnes's avatar
Matthew Barnes committed
829
		g_ptr_array_free (uids, TRUE);
Not Zed's avatar
Not Zed committed
830
		return;
Not Zed's avatar
Not Zed committed
831
832
	}

833
834
835
	/* 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
836
	if (folder) {
Matthew Barnes's avatar
Matthew Barnes committed
837
838
839
		/* 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
840
		g_object_unref (folder);
Not Zed's avatar
Not Zed committed
841
842
	}

Matthew Barnes's avatar
Matthew Barnes committed
843
	em_utils_uids_free (uids);
Not Zed's avatar
Not Zed committed
844
845
846
847
}

/**
 * em_utils_selection_set_urilist:
848
849
850
851
 * @data:
 * @folder:
 * @uids:
 *
Not Zed's avatar
Not Zed committed
852
853
854
855
856
 * 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
Matthew Barnes's avatar
Matthew Barnes committed
857
em_utils_selection_set_urilist (GtkSelectionData *data, CamelFolder *folder, GPtrArray *uids)
Not Zed's avatar
Not Zed committed
858
{
859
	gchar *tmpdir;
Not Zed's avatar
Not Zed committed
860
	CamelStream *fstream;
861
862
	gchar *uri, *file = NULL, *tmpfile;
	gint fd;
Not Zed's avatar
Not Zed committed
863
	CamelMessageInfo *info;
Not Zed's avatar
Not Zed committed
864
865
866
867
868

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

Not Zed's avatar
Not Zed committed
869
870
	/* Try to get the drop filename from the message or folder */
	if (uids->len == 1) {
Matthew Barnes's avatar
Matthew Barnes committed
871
		info = camel_folder_get_message_info (folder, uids->pdata[0]);
Not Zed's avatar
Not Zed committed
872
		if (info) {
Matthew Barnes's avatar
Matthew Barnes committed
873
			file = g_strdup (camel_message_info_subject (info));
874
			camel_folder_free_message_info (folder, info);
Not Zed's avatar
Not Zed committed
875
876
877
878
879
880
		}
	}

	/* TODO: Handle conflicts? */
	if (file == NULL) {
		/* Drop filename for messages from a mailbox */
Matthew Barnes's avatar
Matthew Barnes committed
881
882
883
		file = g_strdup_printf (
			_("Messages from %s"),
			camel_folder_get_name (folder));
Not Zed's avatar
Not Zed committed
884
885
	}

Matthew Barnes's avatar
Matthew Barnes committed
886
	e_filename_make_safe (file);
Not Zed's avatar
Not Zed committed
887

Matthew Barnes's avatar
Matthew Barnes committed
888
889
890
	tmpfile = g_build_filename (tmpdir, file, NULL);
	g_free (tmpdir);
	g_free (file);
891

Matthew Barnes's avatar
Matthew Barnes committed
892
	fd = g_open (tmpfile, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0666);
893
	if (fd == -1) {
Matthew Barnes's avatar
Matthew Barnes committed
894
		g_free (tmpfile);
Not Zed's avatar
Not Zed committed
895
		return;
896
897
	}

Matthew Barnes's avatar
Matthew Barnes committed
898
899
900
	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
901
	if (fstream) {
Matthew Barnes's avatar
Matthew Barnes committed
902
		if (em_utils_write_messages_to_stream (folder, uids, fstream) == 0) {
903
			/* terminate with \r\n to be compliant with the spec */
904
			gchar *uri_crlf = g_strconcat(uri, "\r\n", NULL);
905
			GdkAtom target;
906

907
			target = gtk_selection_data_get_target (data);
Matthew Barnes's avatar
Matthew Barnes committed
908
909
			gtk_selection_data_set (data, target, 8, (guchar *)uri_crlf, strlen (uri_crlf));
			g_free (uri_crlf);
910
		}
Not Zed's avatar
Not Zed committed
911

Matthew Barnes's avatar
Matthew Barnes committed
912
		g_object_unref (fstream);
Jules Colding's avatar
Jules Colding committed
913
	} else
Matthew Barnes's avatar
Matthew Barnes committed
914
		close (fd);
Jules Colding's avatar
Jules Colding committed
915

Matthew Barnes's avatar
Matthew Barnes committed
916
	g_free (uri);
Not Zed's avatar
Not Zed committed
917
918
}

Not Zed's avatar
Not Zed committed
919
920
/**
 * em_utils_selection_set_urilist:
921
922
923
924
 * @data:
 * @folder:
 * @uids:
 *
Not Zed's avatar
Not Zed committed
925
926
927
928
929
 * Get the selection data @data from a uri list which points to a
 * file, which is a berkely mailbox format mailbox.  The file is
 * automatically cleaned up when the application quits.
 **/
void
930
931
em_utils_selection_get_urilist (GtkSelectionData *selection_data,
                                CamelFolder *folder)
Not Zed's avatar
Not Zed committed
932
933
934
{
	CamelStream *stream;
	CamelURL *url;
935
	gint fd, i, res = 0;
936
	gchar **uris;
Not Zed's avatar
Not Zed committed
937
938
939

	d(printf(" * drop uri list\n"));

940
	uris = gtk_selection_data_get_uris (selection_data);
941

Not Zed's avatar
Not Zed committed
942
	for (i=0;res == 0 && uris[i];i++) {
Matthew Barnes's avatar
Matthew Barnes committed
943
		g_strstrip (uris[i]);
Not Zed's avatar
Not Zed committed
944
945
		if (uris[i][0] == '#')
			continue;
Not Zed's avatar
Not Zed committed
946