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 <tny-camel-html-to-text-stream.h>
37 #include "modest-formatter.h"
38 #include "modest-text-utils.h"
39 #include "modest-tny-platform-factory.h"
40 #include <modest-runtime.h>
43 #define MAX_BODY_LINES 1024
44 #define MAX_BODY_LENGTH 1024*128
46 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
47 struct _ModestFormatterPrivate {
51 #define MODEST_FORMATTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
52 MODEST_TYPE_FORMATTER, \
53 ModestFormatterPrivate))
55 static GObjectClass *parent_class = NULL;
57 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
59 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header,
60 FormatterFunc func, GList *attachments);
62 static gchar* modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text,
63 TnyHeader *header, GList *attachments);
64 static gchar* modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text,
65 TnyHeader *header, GList *attachments);
66 static gchar* modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text,
67 TnyHeader *header, GList *attachments);
69 static TnyMimePart *find_body_parent (TnyMimePart *part);
72 extract_text (ModestFormatter *self, TnyMimePart *body)
76 TnyStream *input_stream;
78 GtkTextIter start, end;
80 ModestFormatterPrivate *priv;
81 gint total, total_lines, line_chars;
83 buf = gtk_text_buffer_new (NULL);
84 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
85 tny_stream_reset (stream);
86 mp_stream = tny_mime_part_get_decoded_stream (body);
88 if (g_strcmp0 (tny_mime_part_get_content_type (body), "text/html") == 0) {
89 input_stream = tny_camel_html_to_text_stream_new (mp_stream);
91 input_stream = g_object_ref (mp_stream);
98 while (!tny_stream_is_eos (input_stream)) {
104 next_read = MIN (128, MAX_BODY_LENGTH - total);
107 n_read = tny_stream_read (input_stream, buffer, next_read);
109 if (G_UNLIKELY (n_read < 0))
113 while (offset < buffer + n_read) {
115 if (*offset == '\n') {
120 if (line_chars >= LINE_WRAP) {
125 if (total_lines >= MAX_BODY_LINES)
132 if (offset - buffer > 0) {
134 n_write = tny_stream_write (stream, buffer, offset - buffer);
136 } else if (n_read == -1) {
140 if (total_lines >= MAX_BODY_LINES)
144 tny_stream_reset (stream);
146 g_object_unref (G_OBJECT(stream));
147 g_object_unref (G_OBJECT (mp_stream));
148 g_object_unref (G_OBJECT (input_stream));
150 gtk_text_buffer_get_bounds (buf, &start, &end);
151 text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
152 g_object_unref (G_OBJECT(buf));
154 /* Convert to desired content type if needed */
155 priv = MODEST_FORMATTER_GET_PRIVATE (self);
161 construct_from_text (TnyMimePart *part,
163 const gchar *content_type)
165 TnyStream *text_body_stream;
167 /* Create the stream */
168 text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
169 (text, strlen(text)));
171 /* Construct MIME part */
172 tny_stream_reset (text_body_stream);
173 tny_mime_part_construct (part, text_body_stream, content_type, "7bit");
174 tny_stream_reset (text_body_stream);
177 g_object_unref (G_OBJECT (text_body_stream));
181 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func,
184 TnyMsg *new_msg = NULL;
185 gchar *body_text = NULL, *txt = NULL;
186 ModestFormatterPrivate *priv;
187 TnyMimePart *body_part = NULL;
189 g_return_val_if_fail (self, NULL);
190 g_return_val_if_fail (header, NULL);
191 g_return_val_if_fail (func, NULL);
194 new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL, FALSE);
195 body_part = modest_formatter_create_body_part (self, new_msg);
198 body_text = extract_text (self, body);
200 body_text = g_strdup ("");
202 txt = (gchar *) func (self, (const gchar*) body_text, header, attachments);
203 priv = MODEST_FORMATTER_GET_PRIVATE (self);
204 construct_from_text (TNY_MIME_PART (body_part), (const gchar*) txt, priv->content_type);
205 g_object_unref (body_part);
215 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
217 return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
221 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
223 return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
227 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
229 return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
233 remove_purgeds (TnyMimePart *part)
236 TnyIterator *iterator;
238 parts = TNY_LIST (tny_simple_list_new ());
239 tny_mime_part_get_parts (part, parts);
241 for (iterator = tny_list_create_iterator (parts);
242 !tny_iterator_is_done (iterator);
243 tny_iterator_next (iterator)) {
244 TnyMimePart *current;
246 current = (TnyMimePart *) tny_iterator_get_current (iterator);
247 if (tny_mime_part_is_purged (current)) {
248 tny_mime_part_del_part (part, current);
251 g_object_unref (current);
254 g_object_unref (iterator);
255 g_object_unref (parts);
259 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
261 TnyMsg *new_msg = NULL;
262 TnyMimePart *body_part = NULL;
263 ModestFormatterPrivate *priv;
267 new_msg = modest_formatter_create_message (self, TRUE, TRUE, FALSE);
268 body_part = modest_formatter_create_body_part (self, new_msg);
270 /* Create the two parts */
271 priv = MODEST_FORMATTER_GET_PRIVATE (self);
272 txt = modest_text_utils_cite ("", priv->content_type, priv->signature,
273 NULL, tny_header_get_date_sent (header));
274 construct_from_text (body_part, txt, priv->content_type);
276 g_object_unref (body_part);
281 remove_purgeds (TNY_MIME_PART (msg));
284 tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
291 modest_formatter_new (const gchar *content_type, const gchar *signature)
293 ModestFormatter *formatter;
294 ModestFormatterPrivate *priv;
296 formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
297 priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
298 priv->content_type = g_strdup (content_type);
299 priv->signature = g_strdup (signature);
305 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
307 ModestFormatter *self = (ModestFormatter *)instance;
308 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
310 priv->content_type = NULL;
311 priv->signature = NULL;
315 modest_formatter_finalize (GObject *object)
317 ModestFormatter *self = (ModestFormatter *)object;
318 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
320 if (priv->content_type)
321 g_free (priv->content_type);
324 g_free (priv->signature);
326 (*parent_class->finalize) (object);
330 modest_formatter_class_init (ModestFormatterClass *class)
332 GObjectClass *object_class;
334 parent_class = g_type_class_peek_parent (class);
335 object_class = (GObjectClass*) class;
336 object_class->finalize = modest_formatter_finalize;
338 g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
342 modest_formatter_get_type (void)
344 static GType type = 0;
346 if (G_UNLIKELY(type == 0))
348 static const GTypeInfo info =
350 sizeof (ModestFormatterClass),
351 NULL, /* base_init */
352 NULL, /* base_finalize */
353 (GClassInitFunc) modest_formatter_class_init, /* class_init */
354 NULL, /* class_finalize */
355 NULL, /* class_data */
356 sizeof (ModestFormatter),
358 modest_formatter_instance_init /* instance_init */
361 type = g_type_register_static (G_TYPE_OBJECT,
371 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
374 gchar *result, *from;
375 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
377 from = tny_header_dup_from (header);
378 result = modest_text_utils_cite (text,
382 tny_header_get_date_sent (header));
388 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
391 gchar *result, *from, *to, *subject;
392 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
394 from = tny_header_dup_from (header);
395 to = tny_header_dup_to (header);
396 subject = tny_header_dup_subject (header);
397 result = modest_text_utils_inline (text,
401 tny_header_get_date_sent (header),
411 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
414 ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
415 GList *filenames = NULL;
417 gchar *result = NULL;
420 /* First we need a GList of attachments filenames */
421 for (node = attachments; node != NULL; node = g_list_next (node)) {
422 TnyMimePart *part = (TnyMimePart *) node->data;
423 gchar *filename = NULL;
424 if (TNY_IS_MSG (part)) {
425 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
426 filename = tny_header_dup_subject (header);
427 if ((filename == NULL)||(filename[0] == '\0')) {
429 filename = g_strdup (_("mail_va_no_subject"));
431 g_object_unref (header);
433 filename = g_strdup (tny_mime_part_get_filename (part));
434 if ((filename == NULL)||(filename[0] == '\0')) {
436 filename = g_strdup ("");
439 filenames = g_list_prepend (filenames, filename);
442 /* TODO: get 80 from the configuration */
443 from = tny_header_dup_from (header);
444 result = modest_text_utils_quote (text,
448 tny_header_get_date_sent (header),
453 g_list_foreach (filenames, (GFunc) g_free, NULL);
454 g_list_free (filenames);
459 modest_formatter_create_message (ModestFormatter *self, gboolean single_body,
460 gboolean has_attachments, gboolean has_images)
462 TnyMsg *result = NULL;
463 TnyPlatformFactory *fact = NULL;
464 TnyMimePart *body_mime_part = NULL;
465 TnyMimePart *related_mime_part = NULL;
467 fact = modest_runtime_get_platform_factory ();
468 result = tny_platform_factory_new_msg (fact);
469 if (has_attachments) {
470 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
472 related_mime_part = tny_platform_factory_new_mime_part (fact);
473 tny_mime_part_set_content_type (related_mime_part, "multipart/related");
474 tny_mime_part_add_part (TNY_MIME_PART (result), related_mime_part);
476 related_mime_part = g_object_ref (result);
480 body_mime_part = tny_platform_factory_new_mime_part (fact);
481 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
482 tny_mime_part_add_part (TNY_MIME_PART (related_mime_part), body_mime_part);
483 g_object_unref (body_mime_part);
486 g_object_unref (related_mime_part);
487 } else if (has_images) {
488 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/related");
491 body_mime_part = tny_platform_factory_new_mime_part (fact);
492 tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
493 tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
494 g_object_unref (body_mime_part);
497 } else if (!single_body) {
498 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
505 find_body_parent (TnyMimePart *part)
507 const gchar *msg_content_type = NULL;
508 msg_content_type = tny_mime_part_get_content_type (part);
510 if ((msg_content_type != NULL) &&
511 (!g_strcasecmp (msg_content_type, "multipart/alternative")))
512 return g_object_ref (part);
513 else if ((msg_content_type != NULL) &&
514 (g_str_has_prefix (msg_content_type, "multipart/"))) {
515 TnyIterator *iter = NULL;
516 TnyMimePart *alternative_part = NULL;
517 TnyMimePart *related_part = NULL;
518 TnyList *parts = TNY_LIST (tny_simple_list_new ());
519 tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
520 iter = tny_list_create_iterator (parts);
522 while (!tny_iterator_is_done (iter)) {
523 TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
524 if (part && !g_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
525 alternative_part = part;
527 } else if (part && !g_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
533 g_object_unref (part);
535 tny_iterator_next (iter);
537 g_object_unref (iter);
538 g_object_unref (parts);
541 result = find_body_parent (related_part);
542 g_object_unref (related_part);
544 } else if (alternative_part)
545 return alternative_part;
547 return g_object_ref (part);
553 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
555 TnyMimePart *result = NULL;
556 TnyPlatformFactory *fact = NULL;
557 TnyMimePart *parent = NULL;
559 parent = find_body_parent (TNY_MIME_PART (msg));
560 fact = modest_runtime_get_platform_factory ();
561 if (parent != NULL) {
562 result = tny_platform_factory_new_mime_part (fact);
563 tny_mime_part_add_part (TNY_MIME_PART (parent), result);
564 g_object_unref (parent);
566 result = g_object_ref (msg);