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