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