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