Modified webpage: now tinymail repository is in gitorious.
[modest] / src / modest-formatter.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <glib/gi18n.h>
31 #include <string.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"
40 #include "modest-stream-html-to-text.h"
41
42 #define LINE_WRAP 78
43 #define MAX_BODY_LINES 1024
44 #define MAX_BODY_LENGTH 1024*128
45
46 typedef struct _ModestFormatterPrivate ModestFormatterPrivate;
47 struct _ModestFormatterPrivate {
48         gchar *content_type;
49         gchar *signature;
50 };
51 #define MODEST_FORMATTER_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
52                                           MODEST_TYPE_FORMATTER, \
53                                           ModestFormatterPrivate))
54
55 static GObjectClass *parent_class = NULL;
56
57 typedef gchar* FormatterFunc (ModestFormatter *self, const gchar *text, TnyHeader *header, GList *attachments);
58
59 static TnyMsg *modest_formatter_do (ModestFormatter *self, TnyMimePart *body,  TnyHeader *header, 
60                                     FormatterFunc func, GList *attachments);
61
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);
68
69 static TnyMimePart *find_body_parent (TnyMimePart *part);
70
71 static gchar *
72 extract_text (ModestFormatter *self, TnyMimePart *body)
73 {
74         TnyStream *mp_stream;
75         TnyStream *stream;
76         TnyStream *input_stream;
77         GtkTextBuffer *buf;
78         GtkTextIter start, end;
79         gchar *text;
80         ModestFormatterPrivate *priv;
81         gint total, lines, total_lines, line_chars;
82         gboolean is_html, first_time;
83         gboolean forced_wrap;
84
85         buf = gtk_text_buffer_new (NULL);
86         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
87         tny_stream_reset (stream);
88         mp_stream = tny_mime_part_get_decoded_stream (body);
89
90         is_html = (g_strcmp0 (tny_mime_part_get_content_type (body), "text/html") == 0);
91         if (is_html) {
92                 input_stream = modest_stream_html_to_text_new (mp_stream);
93         } else {
94                 input_stream = g_object_ref (mp_stream);
95         }
96
97         total = 0;
98         total_lines = 0;
99         line_chars = 0;
100         lines = 0;
101
102         forced_wrap = FALSE;
103         first_time = TRUE;
104         while (!tny_stream_is_eos (input_stream)) {
105                 gchar buffer [128];
106                 gchar *offset;
107                 gint n_read;
108                 gint next_read;
109
110                 next_read = MIN (128, MAX_BODY_LENGTH - total);
111                 if (next_read == 0)
112                         break;
113                 n_read = tny_stream_read (input_stream, buffer, next_read);
114
115                 if (G_UNLIKELY (n_read < 0))
116                         break;
117
118                 offset = buffer;
119                 while (offset < buffer + n_read) {
120
121                         if (*offset == '\n' && !(forced_wrap && offset[1] == '\n')) {
122                                 total_lines ++;
123                                 line_chars = 0;
124                         } else {
125                                 if (*offset == '\n') {
126                                         forced_wrap = FALSE;
127                                 }
128                                 line_chars ++;
129                                 if (line_chars >= LINE_WRAP) {
130                                         total_lines ++;
131                                         line_chars = 0;
132                                         forced_wrap = TRUE;
133                                 }
134                         }
135                         if (total_lines >= MAX_BODY_LINES)
136                                 break;
137                         offset++;
138                 }
139
140                 if (offset - buffer > 0) {
141                         gint n_write = 0, to_write = 0;
142
143                         to_write = offset - buffer;
144                         n_write = tny_stream_write (stream, buffer, to_write);
145                         total += n_write;
146                 } else if (n_read == -1) {
147                         break;
148                 }
149
150                 if (total_lines >= MAX_BODY_LINES)
151                         break;
152         }
153
154         tny_stream_reset (stream);
155
156         g_object_unref (G_OBJECT(stream));
157         g_object_unref (G_OBJECT (mp_stream));
158         g_object_unref (G_OBJECT (input_stream));
159
160         gtk_text_buffer_get_bounds (buf, &start, &end);
161         text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
162         g_object_unref (G_OBJECT(buf));
163
164         /* Convert to desired content type if needed */
165         priv = MODEST_FORMATTER_GET_PRIVATE (self);
166
167         return text;
168 }
169
170 static void
171 construct_from_text (TnyMimePart *part,
172                      const gchar *text,
173                      const gchar *content_type)
174 {
175         TnyStream *text_body_stream;
176
177         /* Create the stream */
178         text_body_stream = TNY_STREAM (tny_camel_mem_stream_new_with_buffer
179                                         (text, strlen(text)));
180
181         /* Construct MIME part */
182         tny_stream_reset (text_body_stream);
183         tny_mime_part_construct (part, text_body_stream, content_type, "7bit");
184         tny_stream_reset (text_body_stream);
185
186         /* Clean */
187         g_object_unref (G_OBJECT (text_body_stream));
188 }
189
190 static TnyMsg *
191 modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, FormatterFunc func,
192                      GList *attachments)
193 {
194         TnyMsg *new_msg = NULL;
195         gchar *body_text = NULL, *txt = NULL;
196         ModestFormatterPrivate *priv;
197         TnyMimePart *body_part = NULL;
198
199         g_return_val_if_fail (self, NULL);
200         g_return_val_if_fail (header, NULL);
201         g_return_val_if_fail (func, NULL);
202
203         /* Build new part */
204         new_msg = modest_formatter_create_message (self, TRUE, attachments != NULL, FALSE);
205         body_part = modest_formatter_create_body_part (self, new_msg);
206
207         if (body)
208                 body_text = extract_text (self, body);
209         else
210                 body_text = g_strdup ("");
211
212         txt = (gchar *) func (self, (const gchar*) body_text, header, attachments);
213         priv = MODEST_FORMATTER_GET_PRIVATE (self);
214         construct_from_text (TNY_MIME_PART (body_part), (const gchar*) txt, priv->content_type);
215         g_object_unref (body_part);
216
217         /* Clean */
218         g_free (body_text);
219         g_free (txt);
220
221         return new_msg;
222 }
223
224 TnyMsg *
225 modest_formatter_cite (ModestFormatter *self, TnyMimePart *body, TnyHeader *header)
226 {
227         return modest_formatter_do (self, body, header, modest_formatter_wrapper_cite, NULL);
228 }
229
230 TnyMsg *
231 modest_formatter_quote (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
232 {
233         return modest_formatter_do (self, body, header, modest_formatter_wrapper_quote, attachments);
234 }
235
236 TnyMsg *
237 modest_formatter_inline (ModestFormatter *self, TnyMimePart *body, TnyHeader *header, GList *attachments)
238 {
239         return modest_formatter_do (self, body, header, modest_formatter_wrapper_inline, attachments);
240 }
241
242 TnyMsg *
243 modest_formatter_attach (ModestFormatter *self, TnyMsg *msg, TnyHeader *header)
244 {
245         TnyMsg *new_msg = NULL;
246         TnyMimePart *body_part = NULL;
247         ModestFormatterPrivate *priv;
248         gchar *txt;
249
250         /* Build new part */
251         new_msg     = modest_formatter_create_message (self, TRUE, TRUE, FALSE);
252         body_part = modest_formatter_create_body_part (self, new_msg);
253
254         /* Create the two parts */
255         priv = MODEST_FORMATTER_GET_PRIVATE (self);
256         txt = modest_text_utils_cite ("", priv->content_type, priv->signature,
257                                       NULL, tny_header_get_date_sent (header));
258         construct_from_text (body_part, txt, priv->content_type);
259         g_free (txt);
260         g_object_unref (body_part);
261
262         if (msg) {
263                 /* Add parts */
264                 tny_mime_part_add_part (TNY_MIME_PART (new_msg), TNY_MIME_PART (msg));
265         }
266
267         return new_msg;
268 }
269
270 ModestFormatter*
271 modest_formatter_new (const gchar *content_type, const gchar *signature)
272 {
273         ModestFormatter *formatter;
274         ModestFormatterPrivate *priv;
275
276         formatter = g_object_new (MODEST_TYPE_FORMATTER, NULL);
277         priv = MODEST_FORMATTER_GET_PRIVATE (formatter);
278         priv->content_type = g_strdup (content_type);
279         priv->signature = g_strdup (signature);
280
281         return formatter;
282 }
283
284 static void
285 modest_formatter_instance_init (GTypeInstance *instance, gpointer g_class)
286 {
287         ModestFormatter *self = (ModestFormatter *)instance;
288         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
289
290         priv->content_type = NULL;
291         priv->signature = NULL;
292 }
293
294 static void
295 modest_formatter_finalize (GObject *object)
296 {
297         ModestFormatter *self = (ModestFormatter *)object;
298         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
299
300         if (priv->content_type)
301                 g_free (priv->content_type);
302
303         if (priv->signature)
304                 g_free (priv->signature);
305
306         (*parent_class->finalize) (object);
307 }
308
309 static void 
310 modest_formatter_class_init (ModestFormatterClass *class)
311 {
312         GObjectClass *object_class;
313
314         parent_class = g_type_class_peek_parent (class);
315         object_class = (GObjectClass*) class;
316         object_class->finalize = modest_formatter_finalize;
317
318         g_type_class_add_private (object_class, sizeof (ModestFormatterPrivate));
319 }
320
321 GType 
322 modest_formatter_get_type (void)
323 {
324         static GType type = 0;
325
326         if (G_UNLIKELY(type == 0))
327         {
328                 static const GTypeInfo info = 
329                 {
330                   sizeof (ModestFormatterClass),
331                   NULL,   /* base_init */
332                   NULL,   /* base_finalize */
333                   (GClassInitFunc) modest_formatter_class_init,   /* class_init */
334                   NULL,   /* class_finalize */
335                   NULL,   /* class_data */
336                   sizeof (ModestFormatter),
337                   0,      /* n_preallocs */
338                   modest_formatter_instance_init    /* instance_init */
339                 };
340                 
341                 type = g_type_register_static (G_TYPE_OBJECT,
342                         "ModestFormatter",
343                         &info, 0);
344         }
345
346         return type;
347 }
348
349 /****************/
350 static gchar *
351 modest_formatter_wrapper_cite (ModestFormatter *self, const gchar *text, TnyHeader *header,
352                                GList *attachments) 
353 {
354         gchar *result, *from;
355         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
356         
357         from = tny_header_dup_from (header);
358         result = modest_text_utils_cite (text, 
359                                          priv->content_type, 
360                                          priv->signature,
361                                          from, 
362                                          tny_header_get_date_sent (header));
363         g_free (from);
364         return result;
365 }
366
367 static gchar *
368 modest_formatter_wrapper_inline (ModestFormatter *self, const gchar *text, TnyHeader *header,
369                                  GList *attachments) 
370 {
371         gchar *result, *from, *to, *subject;
372         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
373
374         from = tny_header_dup_from (header);
375         to = tny_header_dup_to (header);
376         subject = tny_header_dup_subject (header);
377         result =  modest_text_utils_inline (text, 
378                                             priv->content_type, 
379                                             priv->signature,
380                                             from,
381                                             tny_header_get_date_sent (header),
382                                             to,
383                                             subject);
384         g_free (subject);
385         g_free (to);
386         g_free (from);
387         return result;
388 }
389
390 static gchar *
391 modest_formatter_wrapper_quote (ModestFormatter *self, const gchar *text, TnyHeader *header,
392                                 GList *attachments) 
393 {
394         ModestFormatterPrivate *priv = MODEST_FORMATTER_GET_PRIVATE (self);
395         GList *filenames = NULL;
396         GList *node = NULL;
397         gchar *result = NULL;
398         gchar *from;
399
400         /* First we need a GList of attachments filenames */
401         for (node = attachments; node != NULL; node = g_list_next (node)) {
402                 TnyMimePart *part = (TnyMimePart *) node->data;
403                 gchar *filename = NULL;
404                 if (TNY_IS_MSG (part)) {
405                         TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
406                         filename = tny_header_dup_subject (header);
407                         if ((filename == NULL)||(filename[0] == '\0')) {
408                                 g_free (filename);
409                                 filename = g_strdup (_("mail_va_no_subject"));
410                         }
411                         g_object_unref (header);
412                 } else {
413                         filename = g_strdup (tny_mime_part_get_filename (part));
414                         if ((filename == NULL)||(filename[0] == '\0')) {
415                                 g_free (filename);
416                                 filename = g_strdup ("");
417                         }
418                 }
419                 filenames = g_list_prepend (filenames, filename);
420         }
421
422         /* TODO: get 80 from the configuration */
423         from = tny_header_dup_from (header);
424         result = modest_text_utils_quote (text, 
425                                           priv->content_type, 
426                                           priv->signature,
427                                           from,
428                                           tny_header_get_date_sent (header),
429                                           filenames,
430                                           72);
431         g_free (from);
432
433         g_list_foreach (filenames, (GFunc) g_free, NULL);
434         g_list_free (filenames);
435         return result;
436 }
437
438 TnyMsg * 
439 modest_formatter_create_message (ModestFormatter *self, gboolean single_body, 
440                                  gboolean has_attachments, gboolean has_images)
441 {
442         TnyMsg *result = NULL;
443         TnyPlatformFactory *fact = NULL;
444         TnyMimePart *body_mime_part = NULL;
445         TnyMimePart *related_mime_part = NULL;
446
447         fact    = modest_runtime_get_platform_factory ();
448         result = tny_platform_factory_new_msg (fact);
449         if (has_attachments) {
450                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/mixed");
451                 if (has_images) {
452                         related_mime_part = tny_platform_factory_new_mime_part (fact);
453                         tny_mime_part_set_content_type (related_mime_part, "multipart/related");
454                         tny_mime_part_add_part (TNY_MIME_PART (result), related_mime_part);
455                 } else {
456                         related_mime_part = g_object_ref (result);
457                 }
458                         
459                 if (!single_body) {
460                         body_mime_part = tny_platform_factory_new_mime_part (fact);
461                         tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
462                         tny_mime_part_add_part (TNY_MIME_PART (related_mime_part), body_mime_part);
463                         g_object_unref (body_mime_part);
464                 }
465
466                 g_object_unref (related_mime_part);
467         } else if (has_images) {
468                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/related");
469
470                 if (!single_body) {
471                         body_mime_part = tny_platform_factory_new_mime_part (fact);
472                         tny_mime_part_set_content_type (body_mime_part, "multipart/alternative");
473                         tny_mime_part_add_part (TNY_MIME_PART (result), body_mime_part);
474                         g_object_unref (body_mime_part);
475                 }
476
477         } else if (!single_body) {
478                 tny_mime_part_set_content_type (TNY_MIME_PART (result), "multipart/alternative");
479         }
480
481         return result;
482 }
483
484 TnyMimePart *
485 find_body_parent (TnyMimePart *part)
486 {
487         const gchar *msg_content_type = NULL;
488         msg_content_type = tny_mime_part_get_content_type (part);
489
490         if ((msg_content_type != NULL) &&
491             (!g_ascii_strcasecmp (msg_content_type, "multipart/alternative")))
492                 return g_object_ref (part);
493         else if ((msg_content_type != NULL) &&
494                  (g_str_has_prefix (msg_content_type, "multipart/"))) {
495                 TnyIterator *iter = NULL;
496                 TnyMimePart *alternative_part = NULL;
497                 TnyMimePart *related_part = NULL;
498                 TnyList *parts = TNY_LIST (tny_simple_list_new ());
499                 tny_mime_part_get_parts (TNY_MIME_PART (part), parts);
500                 iter = tny_list_create_iterator (parts);
501
502                 while (!tny_iterator_is_done (iter)) {
503                         TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
504                         if (part && !g_ascii_strcasecmp(tny_mime_part_get_content_type (part), "multipart/alternative")) {
505                                 alternative_part = part;
506                                 break;
507                         } else if (part && !g_ascii_strcasecmp (tny_mime_part_get_content_type (part), "multipart/related")) {
508                                 related_part = part;
509                                 break;
510                         }
511
512                         if (part)
513                                 g_object_unref (part);
514
515                         tny_iterator_next (iter);
516                 }
517                 g_object_unref (iter);
518                 g_object_unref (parts);
519                 if (related_part) {
520                         TnyMimePart *result;
521                         result = find_body_parent (related_part);
522                         g_object_unref (related_part);
523                         return result;
524                 } else if (alternative_part)
525                         return alternative_part;
526                 else 
527                         return g_object_ref (part);
528         } else
529                 return NULL;
530 }
531
532 TnyMimePart * 
533 modest_formatter_create_body_part (ModestFormatter *self, TnyMsg *msg)
534 {
535         TnyMimePart *result = NULL;
536         TnyPlatformFactory *fact = NULL;
537         TnyMimePart *parent = NULL;
538
539         parent = find_body_parent (TNY_MIME_PART (msg));
540         fact = modest_runtime_get_platform_factory ();
541         if (parent != NULL) {
542                 result = tny_platform_factory_new_mime_part (fact);
543                 tny_mime_part_add_part (TNY_MIME_PART (parent), result);
544                 g_object_unref (parent);
545         } else {
546                 result = g_object_ref (msg);
547         }
548
549         return result;
550
551 }