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