e-mail-formatter-text-plain.c 5.68 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-text-plain.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
24
#include <glib/gi18n-lib.h>

Dan Vrátil's avatar
Dan Vrátil committed
25
26
#include <e-util/e-util.h>

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

31
32
33
typedef EMailFormatterExtension EMailFormatterTextPlain;
typedef EMailFormatterExtensionClass EMailFormatterTextPlainClass;

34
35
GType e_mail_formatter_text_plain_get_type (void);

36
37
38
39
40
G_DEFINE_TYPE (
	EMailFormatterTextPlain,
	e_mail_formatter_text_plain,
	E_TYPE_MAIL_FORMATTER_EXTENSION)

41
42
43
44
45
46
47
static const gchar *formatter_mime_types[] = {
	"text/plain",
	"text/*",
	"message/*",
	"application/vnd.evolution.plaintext",
	NULL
};
Dan Vrátil's avatar
Dan Vrátil committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

static gboolean
emfe_text_plain_format (EMailFormatterExtension *extension,
                        EMailFormatter *formatter,
                        EMailFormatterContext *context,
                        EMailPart *part,
                        CamelStream *stream,
                        GCancellable *cancellable)
{
	CamelDataWrapper *dw;
	CamelStream *filtered_stream;
	CamelMimeFilter *html_filter;
	gchar *content;
	const gchar *format;
	guint32 filter_flags;
	guint32 rgb;

	if (g_cancellable_is_cancelled (cancellable))
		return FALSE;

	if ((context->mode == E_MAIL_FORMATTER_MODE_RAW) ||
	    (context->mode == E_MAIL_FORMATTER_MODE_PRINTING)) {

		if (context->mode == E_MAIL_FORMATTER_MODE_RAW) {
			gchar *header;
			header = e_mail_formatter_get_html_header (formatter);
			camel_stream_write_string (stream, header, cancellable, NULL);
			g_free (header);

			/* No need for body margins within <iframe> */
78
79
			camel_stream_write_string (
				stream,
Dan Vrátil's avatar
Dan Vrátil committed
80
81
82
83
84
85
86
87
88
89
90
				"<style>body{ margin: 0; }</style>",
				cancellable, NULL);
		}

		filter_flags = e_mail_formatter_get_text_format_flags (formatter);

		dw = camel_medium_get_content (CAMEL_MEDIUM (part->part));
		if (!dw)
			return FALSE;

		/* Check for RFC 2646 flowed text. */
91
92
93
		if (camel_content_type_is (dw->mime_type, "text", "plain")
		&& (format = camel_content_type_param (dw->mime_type, "format"))
		&& !g_ascii_strcasecmp (format, "flowed"))
Dan Vrátil's avatar
Dan Vrátil committed
94
95
96
97
98
99
100
101
102
103
104
105
106
			filter_flags |= CAMEL_MIME_FILTER_TOHTML_FORMAT_FLOWED;

		rgb = e_color_to_value ((GdkColor *)
			e_mail_formatter_get_color (
				formatter, E_MAIL_FORMATTER_COLOR_CITATION));

		filtered_stream = camel_stream_filter_new (stream);
		html_filter = camel_mime_filter_tohtml_new (filter_flags, rgb);
		camel_stream_filter_add (
			CAMEL_STREAM_FILTER (filtered_stream), html_filter);
		g_object_unref (html_filter);

		content = g_strdup_printf (
107
			"<div class=\"part-container pre\" style=\""
108
			"border: none; padding: 8px; margin: 0; "
109
			"background-color: #%06x; color: #%06x;\">\n",
Dan Vrátil's avatar
Dan Vrátil committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
			e_color_to_value ((GdkColor *)
				e_mail_formatter_get_color (
					formatter, E_MAIL_FORMATTER_COLOR_CONTENT)),
			e_color_to_value ((GdkColor *)
				e_mail_formatter_get_color (
					formatter, E_MAIL_FORMATTER_COLOR_TEXT)));

		camel_stream_write_string (stream, content, cancellable, NULL);
		e_mail_formatter_format_text (formatter, part, filtered_stream, cancellable);
		camel_stream_flush (filtered_stream, cancellable, NULL);

		g_object_unref (filtered_stream);
		g_free (content);

124
		camel_stream_write_string (stream, "</div>\n", cancellable, NULL);
Dan Vrátil's avatar
Dan Vrátil committed
125
126

		if (context->mode == E_MAIL_FORMATTER_MODE_RAW) {
127
128
129
			camel_stream_write_string (
				stream, "</body></html>",
				cancellable, NULL);
Dan Vrátil's avatar
Dan Vrátil committed
130
131
132
133
134
		}

		return TRUE;

	} else {
135
136
		CamelFolder *folder;
		const gchar *message_uid;
Dan Vrátil's avatar
Dan Vrátil committed
137
		gchar *uri, *str;
138
		const gchar *default_charset, *charset;
Dan Vrátil's avatar
Dan Vrátil committed
139

140
141
		folder = e_mail_part_list_get_folder (context->part_list);
		message_uid = e_mail_part_list_get_message_uid (context->part_list);
142
143
144
145
146
147
148
		default_charset = e_mail_formatter_get_default_charset (formatter);
		charset = e_mail_formatter_get_charset (formatter);

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

Dan Vrátil's avatar
Dan Vrátil committed
150
		uri = e_mail_part_build_uri (
151
			folder, message_uid,
Dan Vrátil's avatar
Dan Vrátil committed
152
153
			"part_id", G_TYPE_STRING, part->id,
			"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW,
154
155
			"formatter_default_charset", G_TYPE_STRING, default_charset,
			"formatter_charset", G_TYPE_STRING, charset,
Dan Vrátil's avatar
Dan Vrátil committed
156
157
158
			NULL);

		str = g_strdup_printf (
159
			"<div class=\"part-container-nostyle\" >"
Dan Vrátil's avatar
Dan Vrátil committed
160
			"<iframe width=\"100%%\" height=\"10\""
161
162
163
164
165
166
167
168
169
170
171
172
			" id=\"%s.iframe\" name=\"%s\" "
			" frameborder=\"0\" src=\"%s\" "
			" style=\"border: 1px solid #%06x; background-color: #%06x;\">"
			"</iframe>"
			"</div>",
			part->id, part->id, uri,
			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_CONTENT)));
Dan Vrátil's avatar
Dan Vrátil committed
173
174
175
176
177
178
179
180
181
182
183

		camel_stream_write_string (stream, str, cancellable, NULL);

		g_free (str);
		g_free (uri);
	}

	return TRUE;
}

static void
184
e_mail_formatter_text_plain_class_init (EMailFormatterExtensionClass *class)
Dan Vrátil's avatar
Dan Vrátil committed
185
{
186
	class->display_name = _("Plain Text");
187
	class->description = _("Format part as plain text");
188
189
	class->mime_types = formatter_mime_types;
	class->format = emfe_text_plain_format;
Dan Vrátil's avatar
Dan Vrátil committed
190
191
192
}

static void
193
e_mail_formatter_text_plain_init (EMailFormatterExtension *extension)
Dan Vrátil's avatar
Dan Vrátil committed
194
195
{
}