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