* src/widgets/modest-attachment-view.c:
[modest] / src / modest-tny-msg.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 <string.h>
31 #include <gtkhtml/gtkhtml.h>
32 #include <tny-gtk-text-buffer-stream.h>
33 #include <tny-simple-list.h>
34 #include <tny-folder.h>
35 #include <modest-runtime.h>
36 #include "modest-formatter.h"
37 #include <tny-camel-stream.h>
38 #include <camel/camel-stream-mem.h>
39 #include <glib/gprintf.h>
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif /*HAVE_CONFIG_H */
44
45 #include <modest-tny-msg.h>
46 #include "modest-text-utils.h"
47
48 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
49                                     const gchar *content_type);
50 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
51 static void add_attachments (TnyMsg *msg, GList *attachments_list);
52 static char * get_content_type(const gchar *s);
53 static gboolean is_ascii(const gchar *s);
54
55
56 TnyMsg*
57 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
58                     const gchar *bcc, const gchar* subject, const gchar *body,
59                     GSList *attachments)
60 {
61         TnyMsg *new_msg;
62         TnyHeader *header;
63         gchar *content_type;
64         
65         /* Create new msg */
66         new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL));
67         header  = tny_msg_get_header (new_msg);
68         
69         if ((from != NULL) && (strlen(from) > 0)) {
70                 tny_header_set_from (TNY_HEADER (header), from);
71                 tny_header_set_replyto (TNY_HEADER (header), from);
72         }
73         if ((mailto != NULL) && (strlen(mailto) > 0)) 
74                 tny_header_set_to (TNY_HEADER (header), mailto);
75         if ((cc != NULL) && (strlen(cc) > 0)) 
76                 tny_header_set_cc (TNY_HEADER (header), cc);
77         if ((bcc != NULL) && (strlen(bcc) > 0)) 
78                 tny_header_set_bcc (TNY_HEADER (header), bcc);
79         
80         if ((subject != NULL) && (strlen(subject) > 0)) 
81                 tny_header_set_subject (TNY_HEADER (header), subject);
82
83         content_type = get_content_type(body);
84                 
85         /* Add the body of the new mail */
86         /* This is needed even if body is NULL or empty. */
87         add_body_part (new_msg, body, content_type);
88         g_free (content_type);
89                        
90         /* Add attachments */
91         if (attachments)
92                 add_attachments (new_msg, (GList*) attachments);
93
94         return new_msg;
95 }
96
97 TnyMsg*
98 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
99                                const gchar *bcc, const gchar* subject, 
100                                const gchar *html_body, const gchar *plain_body,
101                                GSList *attachments)
102 {
103         TnyMsg *new_msg;
104         TnyHeader *header;
105         gchar *content_type;
106         
107         /* Create new msg */
108         new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL));
109         header  = tny_msg_get_header (new_msg);
110         
111         if ((from != NULL) && (strlen(from) > 0)) {
112                 tny_header_set_from (TNY_HEADER (header), from);
113                 tny_header_set_replyto (TNY_HEADER (header), from);
114         }
115         if ((mailto != NULL) && (strlen(mailto) > 0)) 
116                 tny_header_set_to (TNY_HEADER (header), mailto);
117         if ((cc != NULL) && (strlen(cc) > 0)) 
118                 tny_header_set_cc (TNY_HEADER (header), cc);
119         if ((bcc != NULL) && (strlen(bcc) > 0)) 
120                 tny_header_set_bcc (TNY_HEADER (header), bcc);
121         
122         if ((subject != NULL) && (strlen(subject) > 0)) 
123                 tny_header_set_subject (TNY_HEADER (header), subject);
124
125         content_type = get_content_type(plain_body);
126                 
127         /* Add the body of the new mail */      
128         add_body_part (new_msg, plain_body, content_type);
129         add_html_body_part (new_msg, html_body);
130         g_free (content_type);
131                        
132         /* Add attachments */
133         add_attachments (new_msg, (GList*) attachments);
134
135         return new_msg;
136 }
137
138
139 /* FIXME: this func copy from modest-mail-operation: refactor */
140 static TnyMimePart *
141 add_body_part (TnyMsg *msg, 
142                const gchar *body,
143                const gchar *content_type)
144 {
145         TnyMimePart *text_body_part = NULL;
146         TnyStream *text_body_stream;
147
148         /* Create the stream */
149         text_body_stream = TNY_STREAM (tny_camel_stream_new
150                                        (camel_stream_mem_new_with_buffer
151                                         (body, (body ? strlen(body) : 0))));
152
153         text_body_part = modest_formatter_create_body_part (NULL, msg);
154
155         /* Construct MIME part */
156         tny_stream_reset (text_body_stream);
157         tny_mime_part_construct_from_stream (text_body_part,
158                                              text_body_stream,
159                                              content_type);
160         tny_stream_reset (text_body_stream);
161
162         g_object_unref (G_OBJECT(text_body_part));
163
164         /* Clean */
165         g_object_unref (text_body_stream);
166
167         return text_body_part;
168 }
169
170 static TnyMimePart *
171 add_html_body_part (TnyMsg *msg, 
172                     const gchar *body)
173 {
174         TnyMimePart *html_body_part = NULL;
175         TnyStream *html_body_stream;
176
177         /* Create the stream */
178         html_body_stream = TNY_STREAM (tny_camel_stream_new
179                                        (camel_stream_mem_new_with_buffer
180                                         (body, strlen(body))));
181
182         /* Create body part if needed */
183         html_body_part = modest_formatter_create_body_part (NULL, msg);
184
185         /* Construct MIME part */
186         tny_stream_reset (html_body_stream);
187         tny_mime_part_construct_from_stream (html_body_part,
188                                              html_body_stream,
189                                              "text/html; charset=utf-8");
190         tny_stream_reset (html_body_stream);
191
192         g_object_unref (G_OBJECT(html_body_part));
193
194         /* Clean */
195         g_object_unref (html_body_stream);
196
197         return html_body_part;
198 }
199
200 static TnyMimePart *
201 copy_mime_part (TnyMimePart *part)
202 {
203         TnyMimePart *result = NULL;
204         const gchar *attachment_content_type;
205         const gchar *attachment_filename;
206         const gchar *attachment_cid;
207         TnyList *parts;
208         TnyIterator *iterator;
209         TnyStream *attachment_stream;
210
211         if (TNY_IS_MSG (part)) {
212                 result = TNY_MIME_PART (tny_platform_factory_new_msg (modest_runtime_get_platform_factory ()));
213                 attachment_content_type = "message/rfc822";
214                 tny_mime_part_set_content_type (result, attachment_content_type);
215         } else {
216                 result = tny_platform_factory_new_mime_part (
217                         modest_runtime_get_platform_factory());
218                 attachment_content_type = tny_mime_part_get_content_type (part);
219         }
220
221         /* get mime part headers */
222         attachment_filename = tny_mime_part_get_filename (part);
223         attachment_cid = tny_mime_part_get_content_id (part);
224         
225         /* fill the stream */
226         attachment_stream = tny_mime_part_get_stream (part);
227         tny_stream_reset (attachment_stream);
228         tny_mime_part_construct_from_stream (result,
229                                              attachment_stream,
230                                              attachment_content_type);
231         tny_stream_reset (attachment_stream);
232         
233         /* set other mime part fields */
234         tny_mime_part_set_filename (result, attachment_filename);
235         tny_mime_part_set_content_id (result, attachment_cid);
236
237         /* copy subparts */
238         parts = tny_simple_list_new ();
239         tny_mime_part_get_parts (part, parts);
240         iterator = tny_list_create_iterator (parts);
241         while (!tny_iterator_is_done (iterator)) {
242                 TnyMimePart *subpart = TNY_MIME_PART (tny_iterator_get_current (iterator));
243                 TnyMimePart *subpart_copy = copy_mime_part (subpart);
244                 tny_mime_part_add_part (result, subpart_copy);
245                 g_object_unref (subpart);
246                 tny_iterator_next (iterator);
247         }
248         g_object_unref (iterator);
249         g_object_unref (parts);
250         g_object_unref (attachment_stream);
251
252         return result;
253 }
254
255 static void
256 add_attachments (TnyMsg *msg, GList *attachments_list)
257 {
258         GList *pos;
259         TnyMimePart *attachment_part, *old_attachment;
260
261         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
262
263                 old_attachment = pos->data;
264                 attachment_part = copy_mime_part (old_attachment);
265                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
266         }
267 }
268
269
270 gchar * 
271 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
272 {
273         TnyStream *stream;
274         TnyMimePart *body;
275         GtkTextBuffer *buf;
276         GtkTextIter start, end;
277         gchar *to_quote;
278
279         body = modest_tny_msg_find_body_part(msg, want_html);
280         if (!body)
281                 return NULL;
282
283         buf = gtk_text_buffer_new (NULL);
284         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
285         tny_stream_reset (stream);
286         tny_mime_part_decode_to_stream (body, stream);
287         tny_stream_reset (stream);
288         
289         gtk_text_buffer_get_bounds (buf, &start, &end);
290         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
291         if (tny_mime_part_content_type_is (body, "text/plain")) {
292                 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
293                 g_free (to_quote);
294                 to_quote = to_quote_converted;
295         }
296
297         g_object_unref (buf);
298         g_object_unref (G_OBJECT(stream));
299         g_object_unref (G_OBJECT(body));
300
301         return to_quote;
302 }
303
304
305 TnyMimePart*
306 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
307 {
308         const gchar *mime_type = want_html ? "text/html" : "text/plain";
309         TnyMimePart *part = NULL;
310         TnyList *parts;
311         TnyIterator *iter;
312
313         if (!msg)
314                 return NULL;
315
316         parts = TNY_LIST (tny_simple_list_new());
317         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
318
319         iter  = tny_list_create_iterator(parts);
320
321         /* no parts? assume it's single-part message */
322         if (tny_iterator_is_done(iter)) {
323                 g_object_unref (G_OBJECT(iter));
324                 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
325         } else {
326                 gchar *content_type = NULL;
327                 do {
328                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
329
330                         /* we need to strdown the content type, because
331                          * tny_mime_part_has_content_type does not do it...
332                          */
333                         content_type = g_ascii_strdown
334                                 (tny_mime_part_get_content_type (part), -1);
335                                                         
336                         if (g_str_has_prefix (content_type, mime_type) &&
337                             !tny_mime_part_is_attachment (part))
338                                 break;
339                         
340                         if (g_str_has_prefix(content_type, "multipart")) {
341                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
342                                 if (part)
343                                         break;
344                         }
345                         if (part)
346                                 g_object_unref (G_OBJECT(part));
347
348                         part = NULL;
349                         
350                         g_free (content_type);
351                         content_type = NULL;
352
353                         tny_iterator_next (iter);
354                         
355                 } while (!tny_iterator_is_done(iter));
356                 g_free (content_type);
357         }
358         
359         g_object_unref (G_OBJECT(iter));
360         g_object_unref (G_OBJECT(parts));
361
362         /* if were trying to find an HTML part and couldn't find it,
363          * try to find a text/plain part instead
364          */
365         if (!part && want_html) 
366                 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
367         else
368                 return part; /* this maybe NULL, this is not an error; some message just don't have a body
369                               * part */
370 }
371
372
373 TnyMimePart*
374 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
375 {
376         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
377                                                              want_html);
378 }
379
380
381 static TnyMsg *
382 create_reply_forward_mail (TnyMsg *msg, const gchar *from, const gchar *signature, 
383                            gboolean is_reply, guint type, GList *attachments)
384 {
385         TnyMsg *new_msg;
386         TnyHeader *new_header, *header;
387         gchar *new_subject;
388         TnyMimePart *body;
389         ModestFormatter *formatter;
390         gchar *subject_prefix;
391
392         /* Get body from original msg. Always look for the text/plain
393            part of the message to create the reply/forwarded mail */
394         header = tny_msg_get_header (msg);
395         body   = modest_tny_msg_find_body_part (msg, FALSE);
396
397         /* TODO: select the formatter from account prefs */
398         if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
399                 formatter = modest_formatter_new ("text/html", signature);
400         else
401                 formatter = modest_formatter_new ("text/plain", signature);
402
403         /* Format message body */
404         if (is_reply) {
405                 switch (type) {
406                 case MODEST_TNY_MSG_REPLY_TYPE_CITE:
407                 default:
408                         new_msg = modest_formatter_cite  (formatter, body, header);
409                         break;
410                 case MODEST_TNY_MSG_REPLY_TYPE_QUOTE:
411                         new_msg = modest_formatter_quote (formatter, body, header, attachments);
412                         break;
413                 }
414         } else {
415                 switch (type) {
416                 case MODEST_TNY_MSG_FORWARD_TYPE_INLINE:
417                 default:
418                         new_msg = modest_formatter_inline  (formatter, body, header, attachments);
419                         break;
420                 case MODEST_TNY_MSG_FORWARD_TYPE_ATTACHMENT:
421                         new_msg = modest_formatter_attach (formatter, body, header);
422                         break;
423                 }
424         }
425         g_object_unref (G_OBJECT(formatter));
426         g_object_unref (G_OBJECT(body));
427         
428         /* Fill the header */
429         new_header = tny_msg_get_header (new_msg);      
430         tny_header_set_from (new_header, from);
431         tny_header_set_replyto (new_header, from);
432
433         /* Change the subject */
434         if (is_reply)
435                 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
436         else
437                 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
438         new_subject =
439                 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
440                                                              subject_prefix);
441         g_free (subject_prefix);
442         tny_header_set_subject (new_header, (const gchar *) new_subject);
443         g_free (new_subject);
444
445         /* Clean */
446         g_object_unref (G_OBJECT (new_header));
447         g_object_unref (G_OBJECT (header));
448
449         return new_msg;
450 }
451
452 static void
453 add_if_attachment (gpointer data, gpointer user_data)
454 {
455         TnyMimePart *part;
456         GList **attachments_list;
457
458         part = TNY_MIME_PART (data);
459         attachments_list = ((GList **) user_data);
460
461         if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part)))
462                 *attachments_list = g_list_prepend (*attachments_list, part);
463 }
464
465 TnyMsg* 
466 modest_tny_msg_create_forward_msg (TnyMsg *msg, 
467                                    const gchar *from,
468                                    const gchar *signature,
469                                    ModestTnyMsgForwardType forward_type)
470 {
471         TnyMsg *new_msg;
472         TnyList *parts = NULL;
473         GList *attachments_list = NULL;
474
475         /* Add attachments */
476         parts = TNY_LIST (tny_simple_list_new());
477         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
478         tny_list_foreach (parts, add_if_attachment, &attachments_list);
479
480         new_msg = create_reply_forward_mail (msg, from, signature, FALSE, forward_type, attachments_list);
481         add_attachments (new_msg, attachments_list);
482
483         /* Clean */
484         if (attachments_list)
485                 g_list_free (attachments_list);
486         g_object_unref (G_OBJECT (parts));
487
488         return new_msg;
489 }
490
491 TnyMsg* 
492 modest_tny_msg_create_reply_msg (TnyMsg *msg, 
493                                  const gchar *from,
494                                  const gchar *signature,
495                                  ModestTnyMsgReplyType reply_type,
496                                  ModestTnyMsgReplyMode reply_mode)
497 {
498         TnyMsg *new_msg = NULL;
499         TnyHeader *new_header, *header;
500         const gchar* reply_to;
501         gchar *new_cc = NULL;
502         const gchar *cc = NULL, *bcc = NULL;
503         GString *tmp = NULL;
504         TnyList *parts = NULL;
505         GList *attachments_list = NULL;
506
507         /* Add attachments */
508         parts = TNY_LIST (tny_simple_list_new());
509         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
510         tny_list_foreach (parts, add_if_attachment, &attachments_list);
511
512         new_msg = create_reply_forward_mail (msg, from, signature, TRUE, reply_type, attachments_list);
513         if (attachments_list) {
514                 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
515                 g_list_free (attachments_list);
516         }
517         g_object_unref (G_OBJECT (parts));
518
519         /* Fill the header */
520         header = tny_msg_get_header (msg);
521         new_header = tny_msg_get_header (new_msg);
522         reply_to = tny_header_get_replyto (header);
523
524         if (reply_to)
525                 tny_header_set_to (new_header, reply_to);
526         else
527                 tny_header_set_to (new_header, tny_header_get_from (header));
528
529         switch (reply_mode) {
530         case MODEST_TNY_MSG_REPLY_MODE_SENDER:
531                 /* Do not fill neither cc nor bcc */
532                 break;
533         case MODEST_TNY_MSG_REPLY_MODE_LIST:
534                 /* TODO */
535                 break;
536         case MODEST_TNY_MSG_REPLY_MODE_ALL:
537                 /* Concatenate to, cc and bcc */
538                 cc = tny_header_get_cc (header);
539                 bcc = tny_header_get_bcc (header);
540
541                 tmp = g_string_new (tny_header_get_to (header));
542                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
543                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
544
545                /* Remove my own address from the cc list. TODO:
546                   remove also the To: of the new message, needed due
547                   to the new reply_to feature */
548                 new_cc = (gchar *)
549                         modest_text_utils_remove_address ((const gchar *) tmp->str,
550                                                           from);
551                 /* FIXME: remove also the mails from the new To: */
552                 tny_header_set_cc (new_header, new_cc);
553
554                 /* Clean */
555                 g_string_free (tmp, TRUE);
556                 g_free (new_cc);
557                 break;
558         }
559
560         /* Clean */
561         g_object_unref (G_OBJECT (new_header));
562         g_object_unref (G_OBJECT (header));
563
564         return new_msg;
565 }
566
567
568 static gboolean
569 is_ascii(const gchar *s)
570 {
571         if (!s)
572                 return TRUE;
573         while (s[0]) {
574                 if (s[0] & 128 || s[0] < 32)
575                         return FALSE;
576                 s++;
577         }
578         return TRUE;
579 }
580
581 static char *
582 get_content_type(const gchar *s)
583 {
584         GString *type;
585         
586         type = g_string_new("text/plain");
587         if (!is_ascii(s)) {
588                 if (g_utf8_validate(s, -1, NULL)) {
589                         g_string_append(type, "; charset=\"utf-8\"");
590                 } else {
591                         /* it should be impossible to reach this, but better safe than sorry */
592                         g_warning("invalid utf8 in message");
593                         g_string_append(type, "; charset=\"latin1\"");
594                 }
595         }
596         return g_string_free(type, FALSE);
597 }