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