1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <glib/gi18n.h>
32 #include <tny-header.h>
33 #include <tny-simple-list.h>
34 #include <tny-gtk-text-buffer-stream.h>
35 #include <tny-camel-mem-stream.h>
36 #include "modest-formatter.h"
37 #include "modest-text-utils.h"
38 #include "modest-tny-platform-factory.h"
39 #include <modest-runtime.h>
41 #define MAX_BODY_LENGTH 4096
43 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
44 struct _ModestFormatterPrivate {
48 #define MODEST_FORMATTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
49 MODEST_TYPE_FORMATTER, \
50 ModestFormatterPrivate))
52 static GObjectClass *parent_class = NULL;
54 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
56 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header,
57 FormatterFunc func, GList *attachments);
59 static gchar* modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text,
60 TnyHeader *header, GList *attachments);
61 static gchar* modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text,
62 TnyHeader *header, GList *attachments);
63 static gchar* modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text,
64 TnyHeader *header, GList *attachments);
66 static TnyMimePart *find_body_parent (TnyMimePart *part);
69 extract_text (ModestFormatter *self, TnyMimePart *body)
74 GtkTextIter start, end;
75 gchar *text, *converted_text;
76 ModestFormatterPrivate *priv;
79 buf = gtk_text_buffer_new (NULL);
80 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
81 tny_stream_reset (stream);
82 mp_stream = tny_mime_part_get_decoded_stream (body);
86 while (!tny_stream_is_eos (mp_stream)) {
91 next_read = MIN (128, MAX_BODY_LENGTH - total);
94 n_read = tny_stream_read (mp_stream, buffer, next_read);
97 n_write = tny_stream_write (stream, buffer, n_read);
99 } else if (n_read == -1) {
104 tny_stream_reset (stream);
106 g_object_unref (G_OBJECT(stream));
108 gtk_text_buffer_get_bounds (buf, &start, &end);
109 text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
110 g_object_unref (G_OBJECT(buf));
112 /* Convert to desired content type if needed */
113 priv = MODEST_FORMATTER_GET_PRIVATE (self);
115 if (strcmp (tny_mime_part_get_content_type (body), priv->content_type) == 0) {
116 if (!strcmp (priv->content_type, "text/html"))
117 converted_text = modest_text_utils_convert_to_html (text);
119 converted_text = g_strdup (text);
122 text = converted_text;
128 construct_from_text (TnyMimePart *part,
130 const gchar *content_type)
132 TnyStream *text_body_stream;
134 /* Create the stream */
135 text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
136 (text, strlen(text)));
138 /* Construct MIME part */
139 tny_stream_reset (text_body_stream);
140 tny_mime_part_construct (part, text_body_stream, content_type, "7bit");
141 tny_stream_reset (text_body_stream);
144 g_object_unref (G_OBJECT (text_body_stream));
148 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func,
151 TnyMsg *new_msg = NULL;
152 gchar *body_text = NULL, *txt = NULL;
153 ModestFormatterPrivate *priv;
154 TnyMimePart *body_part = NULL;
156 g_return_val_if_fail (self, NULL);
157 g_return_val_if_fail (header, NULL);
158 g_return_val_if_fail (func, NULL);
161 new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL, FALSE);
162 body_part = modest_formatter_create_body_part (self, new_msg);
165 body_text = extract_text (self, body);
167 body_text = g_strdup ("");
169 txt = (gchar *) func (self, (const gchar*) body_text, header, attachments);
170 priv = MODEST_FORMATTER_GET_PRIVATE (self);
171 construct_from_text (TNY_MIME_PART (body_part), (const gchar*) txt, priv->content_type);
172 g_object_unref (body_part);
182 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
184 return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
188 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
190 return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
194 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
196 return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
200 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
202 TnyMsg *new_msg = NULL;
203 TnyMimePart *body_part = NULL;
204 ModestFormatterPrivate *priv;
208 new_msg = modest_formatter_create_message (self, TRUE, TRUE, FALSE);
209 body_part = modest_formatter_create_body_part (self, new_msg);
211 /* Create the two parts */
212 priv = MODEST_FORMATTER_GET_PRIVATE (self);
213 txt = modest_text_utils_cite ("", priv->content_type, priv->signature,
214 NULL, tny_header_get_date_sent (header));
215 construct_from_text (body_part, txt, priv->content_type);
217 g_object_unref (body_part);
221 tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
228 modest_formatter_new (const gchar *content_type, const gchar *signature)
230 ModestFormatter *formatter;
231 ModestFormatterPrivate *priv;
233 formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
234 priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
235 priv->content_type = g_strdup (content_type);
236 priv->signature = g_strdup (signature);
242 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
244 ModestFormatter *self = (ModestFormatter *)instance;
245 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
247 priv->content_type = NULL;
248 priv->signature = NULL;
252 modest_formatter_finalize (GObject *object)
254 ModestFormatter *self = (ModestFormatter *)object;
255 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
257 if (priv->content_type)
258 g_free (priv->content_type);
261 g_free (priv->signature);
263 (*parent_class->finalize) (object);
267 modest_formatter_class_init (ModestFormatterClass *class)
269 GObjectClass *object_class;
271 parent_class = g_type_class_peek_parent (class);
272 object_class = (GObjectClass*) class;
273 object_class->finalize = modest_formatter_finalize;
275 g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
279 modest_formatter_get_type (void)
281 static GType type = 0;
283 if (G_UNLIKELY(type == 0))
285 static const GTypeInfo info =
287 sizeof (ModestFormatterClass),
288 NULL, /* base_init */
289 NULL, /* base_finalize */
290 (GClassInitFunc) modest_formatter_class_init, /* class_init */
291 NULL, /* class_finalize */
292 NULL, /* class_data */
293 sizeof (ModestFormatter),
295 modest_formatter_instance_init /* instance_init */
298 type = g_type_register_static (G_TYPE_OBJECT,
308 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
311 gchar *result, *from;
312 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
314 from = tny_header_dup_from (header);
315 result = modest_text_utils_cite (text,
319 tny_header_get_date_sent (header));
325 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
328 gchar *result, *from, *to, *subject;
329 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
331 from = tny_header_dup_from (header);
332 to = tny_header_dup_to (header);
333 subject = tny_header_dup_subject (header);
334 result = modest_text_utils_inline (text,
338 tny_header_get_date_sent (header),
348 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
351 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
352 GList *filenames = NULL;
354 gchar *result = NULL;
357 /* First we need a GList of attachments filenames */
358 for (node = attachments; node != NULL; node = g_list_next (node)) {
359 TnyMimePart *part = (TnyMimePart *) node->data;
360 gchar *filename = NULL;
361 if (TNY_IS_MSG (part)) {
362 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
363 filename = tny_header_dup_subject (header);
364 if ((filename == NULL)||(filename[0] == '\0')) {
366 filename = g_strdup (_("mail_va_no_subject"));
368 g_object_unref (header);
370 filename = g_strdup (tny_mime_part_get_filename (part));
371 if ((filename == NULL)||(filename[0] == '\0')) {
373 filename = g_strdup ("");
376 filenames = g_list_prepend (filenames, filename);
379 /* TODO: get 80 from the configuration */
380 from = tny_header_dup_from (header);
381 result = modest_text_utils_quote (text,
385 tny_header_get_date_sent (header),
390 g_list_foreach (filenames, (GFunc) g_free, NULL);
391 g_list_free (filenames);
396 modest_formatter_create_message (ModestFormatter *self, gboolean single_body,
397 gboolean has_attachments, gboolean has_images)
399 TnyMsg *result = NULL;
400 TnyPlatformFactory *fact = NULL;
401 TnyMimePart *body_mime_part = NULL;
402 TnyMimePart *related_mime_part = NULL;
404 fact = modest_runtime_get_platform_factory ();
405 result = tny_platform_factory_new_msg (fact);
406 if (has_attachments) {
407 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
409 related_mime_part = tny_platform_factory_new_mime_part (fact);
410 tny_mime_part_set_content_type (related_mime_part, "multipart/related");
411 tny_mime_part_add_part (TNY_MIME_PART (result), related_mime_part);
413 related_mime_part = g_object_ref (result);
417 body_mime_part = tny_platform_factory_new_mime_part (fact);
418 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
419 tny_mime_part_add_part (TNY_MIME_PART (related_mime_part), body_mime_part);
420 g_object_unref (body_mime_part);
423 g_object_unref (related_mime_part);
424 } else if (has_images) {
425 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/related");
428 body_mime_part = tny_platform_factory_new_mime_part (fact);
429 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
430 tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
431 g_object_unref (body_mime_part);
434 } else if (!single_body) {
435 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
442 find_body_parent (TnyMimePart *part)
444 const gchar *msg_content_type = NULL;
445 msg_content_type = tny_mime_part_get_content_type (part);
447 if ((msg_content_type != NULL) &&
448 (!g_strcasecmp (msg_content_type, "multipart/alternative")))
449 return g_object_ref (part);
450 else if ((msg_content_type != NULL) &&
451 (g_str_has_prefix (msg_content_type, "multipart/"))) {
452 TnyIterator *iter = NULL;
453 TnyMimePart *alternative_part = NULL;
454 TnyMimePart *related_part = NULL;
455 TnyList *parts = TNY_LIST (tny_simple_list_new ());
456 tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
457 iter = tny_list_create_iterator (parts);
459 while (!tny_iterator_is_done (iter)) {
460 TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
461 if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
462 alternative_part = part;
464 } else if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
470 g_object_unref (part);
472 tny_iterator_next (iter);
474 g_object_unref (iter);
475 g_object_unref (parts);
478 result = find_body_parent (related_part);
479 g_object_unref (related_part);
481 } else if (alternative_part)
482 return alternative_part;
484 return g_object_ref (part);
490 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
492 TnyMimePart *result = NULL;
493 TnyPlatformFactory *fact = NULL;
494 TnyMimePart *parent = NULL;
496 parent = find_body_parent (TNY_MIME_PART (msg));
497 fact = modest_runtime_get_platform_factory ();
498 if (parent != NULL) {
499 result = tny_platform_factory_new_mime_part (fact);
500 tny_mime_part_add_part (TNY_MIME_PART (parent), result);
501 g_object_unref (parent);
503 result = g_object_ref (msg);