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