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