e-mail-formatter-message-rfc822.c 6.42 KB
Newer Older
Dan Vrátil's avatar
Dan Vrátil committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * e-mail-formatter-message-rfc822.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

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

23
#include <string.h>
Dan Vrátil's avatar
Dan Vrátil committed
24
25
26
27
#include <glib/gi18n-lib.h>

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

28
29
30
#include "e-mail-formatter-extension.h"
#include "e-mail-part-list.h"
#include "e-mail-part-utils.h"
Dan Vrátil's avatar
Dan Vrátil committed
31

32
33
34
typedef EMailFormatterExtension EMailFormatterMessageRFC822;
typedef EMailFormatterExtensionClass EMailFormatterMessageRFC822Class;

35
36
GType e_mail_formatter_message_rfc822_get_type (void);

37
38
39
40
41
G_DEFINE_TYPE (
	EMailFormatterMessageRFC822,
	e_mail_formatter_message_rfc822,
	E_TYPE_MAIL_FORMATTER_EXTENSION)

42
43
44
45
46
static const gchar *formatter_mime_types[] = {
	"message/rfc822",
	"application/vnd.evolution.rfc822.end",
	NULL
};
Dan Vrátil's avatar
Dan Vrátil committed
47
48
49
50
51
52
53
54
55
56
57
58
59

static gboolean
emfe_message_rfc822_format (EMailFormatterExtension *extension,
                            EMailFormatter *formatter,
                            EMailFormatterContext *context,
                            EMailPart *part,
                            CamelStream *stream,
                            GCancellable *cancellable)
{
	if (g_cancellable_is_cancelled (cancellable))
		return FALSE;

	if (context->mode == E_MAIL_FORMATTER_MODE_RAW) {
60
61
		GQueue queue = G_QUEUE_INIT;
		GList *head, *link;
Dan Vrátil's avatar
Dan Vrátil committed
62
63
64
65
66
67
68
69
70
		gchar *header, *end;

		header = e_mail_formatter_get_html_header (formatter);
		camel_stream_write_string (stream, header, cancellable, NULL);
		g_free (header);

		/* Print content of the message normally */
		context->mode = E_MAIL_FORMATTER_MODE_NORMAL;

71
72
73
74
75
76
77
78
		e_mail_part_list_queue_parts (
			context->part_list, part->id, &queue);

		/* Discard the first EMailPart. */
		if (!g_queue_is_empty (&queue))
			e_mail_part_unref (g_queue_pop_head (&queue));

		head = g_queue_peek_head_link (&queue);
Dan Vrátil's avatar
Dan Vrátil committed
79
80

		end = g_strconcat (part->id, ".end", NULL);
81
82
83

		for (link = head; link != NULL; link = g_list_next (link)) {
			EMailPart *p = link->data;
Dan Vrátil's avatar
Dan Vrátil committed
84
85
86
87
88

			/* Check for nested rfc822 messages */
			if (g_str_has_suffix (p->id, ".rfc822")) {
				gchar *sub_end = g_strconcat (p->id, ".end", NULL);

89
90
				while (link != NULL) {
					p = link->data;
Dan Vrátil's avatar
Dan Vrátil committed
91

92
					if (g_strcmp0 (p->id, sub_end) == 0)
Dan Vrátil's avatar
Dan Vrátil committed
93
94
						break;

95
					link = g_list_next (link);
Dan Vrátil's avatar
Dan Vrátil committed
96
97
98
99
				}
				g_free (sub_end);
				continue;
			}
100

Dan Vrátil's avatar
Dan Vrátil committed
101
102
103
104
105
106
107
108
109
110
111
112
113
			if ((g_strcmp0 (p->id, end) == 0))
				break;

			if (p->is_hidden)
				continue;

			e_mail_formatter_format_as (
				formatter, context, p,
				stream, NULL, cancellable);
		}

		g_free (end);

114
115
116
		while (!g_queue_is_empty (&queue))
			e_mail_part_unref (g_queue_pop_head (&queue));

Dan Vrátil's avatar
Dan Vrátil committed
117
118
119
120
121
		context->mode = E_MAIL_FORMATTER_MODE_RAW;

		camel_stream_write_string (stream, "</body></html>", cancellable, NULL);

	} else if (context->mode == E_MAIL_FORMATTER_MODE_PRINTING) {
122
123
		GQueue queue = G_QUEUE_INIT;
		GList *head, *link;
Dan Vrátil's avatar
Dan Vrátil committed
124
125
126
		gchar *end;

		/* Part is EMailPartAttachment */
127
128
129
130
131
132
		e_mail_part_list_queue_parts (
			context->part_list, part->id, &queue);

		/* Discard the first EMailPart. */
		if (!g_queue_is_empty (&queue))
			e_mail_part_unref (g_queue_pop_head (&queue));
Dan Vrátil's avatar
Dan Vrátil committed
133

134
		if (g_queue_is_empty (&queue))
Dan Vrátil's avatar
Dan Vrátil committed
135
136
			return FALSE;

137
		part = g_queue_pop_head (&queue);
Dan Vrátil's avatar
Dan Vrátil committed
138
		end = g_strconcat (part->id, ".end", NULL);
139
		e_mail_part_unref (part);
Dan Vrátil's avatar
Dan Vrátil committed
140

141
142
143
144
		head = g_queue_peek_head_link (&queue);

		for (link = head; link != NULL; link = g_list_next (link)) {
			EMailPart *p = link->data;
Dan Vrátil's avatar
Dan Vrátil committed
145
146
147
148
149
150
151
152
153

			/* Skip attachment bar */
			if (g_str_has_suffix (part->id, ".attachment-bar"))
				continue;

			/* Check for nested rfc822 messages */
			if (g_str_has_suffix (p->id, ".rfc822")) {
				gchar *sub_end = g_strconcat (p->id, ".end", NULL);

154
155
				while (link != NULL) {
					p = link->data;
Dan Vrátil's avatar
Dan Vrátil committed
156

157
					if (g_strcmp0 (p->id, sub_end) == 0)
Dan Vrátil's avatar
Dan Vrátil committed
158
159
						break;

160
					link = g_list_next (link);
Dan Vrátil's avatar
Dan Vrátil committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
				}
				g_free (sub_end);
				continue;
			}

			if ((g_strcmp0 (p->id, end) == 0))
				break;

			if (p->is_hidden)
				continue;

			e_mail_formatter_format_as (
				formatter, context, p,
				stream, NULL, cancellable);
		}

		g_free (end);

179
180
181
		while (!g_queue_is_empty (&queue))
			e_mail_part_unref (g_queue_pop_head (&queue));

Dan Vrátil's avatar
Dan Vrátil committed
182
	} else {
183
		EMailPart *p;
184
185
		CamelFolder *folder;
		const gchar *message_uid;
186
		const gchar *default_charset, *charset;
Dan Vrátil's avatar
Dan Vrátil committed
187
188
189
		gchar *str;
		gchar *uri;

190
191
		p = e_mail_part_list_ref_part (context->part_list, part->id);
		if (p == NULL)
Dan Vrátil's avatar
Dan Vrátil committed
192
193
			return FALSE;

194
195
		folder = e_mail_part_list_get_folder (context->part_list);
		message_uid = e_mail_part_list_get_message_uid (context->part_list);
196
197
198
199
200
201
202
		default_charset = e_mail_formatter_get_default_charset (formatter);
		charset = e_mail_formatter_get_charset (formatter);

		if (!default_charset)
			default_charset = "";
		if (!charset)
			charset = "";
203

204
		uri = e_mail_part_build_uri (
205
			folder, message_uid,
Dan Vrátil's avatar
Dan Vrátil committed
206
207
208
			"part_id", G_TYPE_STRING, p->id,
			"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW,
			"headers_collapsable", G_TYPE_INT, 0,
209
210
			"formatter_default_charset", G_TYPE_STRING, default_charset,
			"formatter_charset", G_TYPE_STRING, charset,
Dan Vrátil's avatar
Dan Vrátil committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
			NULL);

		str = g_strdup_printf (
			"<div class=\"part-container\" style=\"border-color: #%06x; "
			"background-color: #%06x;\">\n"
			"<iframe width=\"100%%\" height=\"10\""
			" id=\"%s.iframe\" "
			" frameborder=\"0\" src=\"%s\" name=\"%s\"></iframe>"
			"</div>",
			e_color_to_value ((GdkColor *)
				e_mail_formatter_get_color (
					formatter, E_MAIL_FORMATTER_COLOR_FRAME)),
			e_color_to_value ((GdkColor *)
				e_mail_formatter_get_color (
					formatter, E_MAIL_FORMATTER_COLOR_BODY)),
			part->id, uri, part->id);

		camel_stream_write_string (stream, str, cancellable, NULL);

		g_free (str);
		g_free (uri);
232
233

		e_mail_part_unref (p);
Dan Vrátil's avatar
Dan Vrátil committed
234
235
236
237
238
239
	}

	return TRUE;
}

static void
240
e_mail_formatter_message_rfc822_class_init (EMailFormatterExtensionClass *class)
Dan Vrátil's avatar
Dan Vrátil committed
241
{
242
	class->display_name = _("RFC822 message");
243
	class->description = _("Format part as an RFC822 message");
244
245
	class->mime_types = formatter_mime_types;
	class->format = emfe_message_rfc822_format;
Dan Vrátil's avatar
Dan Vrátil committed
246
247
248
}

static void
249
e_mail_formatter_message_rfc822_init (EMailFormatterExtension *extension)
Dan Vrátil's avatar
Dan Vrátil committed
250
251
{
}