* all:
[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 <tny-camel-stream.h>
37 #include <camel/camel-stream-mem.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif /*HAVE_CONFIG_H */
42
43 #include <modest-tny-msg.h>
44 #include "modest-text-utils.h"
45
46 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
47                                     const gchar *content_type, gboolean has_attachments);
48 static void add_attachments (TnyMsg *msg, GList *attachments_list);
49 static char * get_content_type(const gchar *s);
50 static gboolean is_ascii(const gchar *s);
51
52 TnyMsg*
53 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
54                     const gchar *bcc, const gchar* subject, const gchar *body,
55                     GSList *attachments)
56 {
57         TnyPlatformFactory *fact;
58         TnyMsg *new_msg;
59         TnyHeader *header;
60         gchar *content_type;
61         
62         /* Create new msg */
63         fact    = modest_runtime_get_platform_factory ();
64         new_msg = tny_platform_factory_new_msg (fact);
65         header  = tny_platform_factory_new_header (fact);
66         
67         /* WARNING: set the header before assign values to it */
68         tny_msg_set_header (new_msg, header);
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                        
81         /* Add attachments */
82         add_attachments (new_msg, (GList*) attachments);
83
84         return new_msg;
85 }
86
87
88 /* FIXME: this func copy from modest-mail-operation: refactor */
89 static TnyMimePart *
90 add_body_part (TnyMsg *msg, 
91                const gchar *body,
92                const gchar *content_type,
93                gboolean has_attachments)
94 {
95         TnyMimePart *text_body_part = NULL;
96         TnyStream *text_body_stream;
97
98         /* Create the stream */
99         text_body_stream = TNY_STREAM (tny_camel_stream_new
100                                        (camel_stream_mem_new_with_buffer
101                                         (body, strlen(body))));
102
103         /* Create body part if needed */
104         if (has_attachments)
105                 text_body_part = tny_platform_factory_new_mime_part
106                         (modest_runtime_get_platform_factory ());
107         else
108                 text_body_part = TNY_MIME_PART(msg);
109
110         /* Construct MIME part */
111         tny_stream_reset (text_body_stream);
112         tny_mime_part_construct_from_stream (text_body_part,
113                                              text_body_stream,
114                                              content_type);
115         tny_stream_reset (text_body_stream);
116
117         /* Add part if needed */
118         if (has_attachments) {
119                 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
120                 g_object_unref (G_OBJECT(text_body_part));
121         }
122
123         /* Clean */
124         g_object_unref (text_body_stream);
125
126         return text_body_part;
127 }
128
129 static void
130 add_attachments (TnyMsg *msg, GList *attachments_list)
131 {
132         GList *pos;
133         TnyMimePart *attachment_part, *old_attachment;
134         const gchar *attachment_content_type;
135         const gchar *attachment_filename;
136         TnyStream *attachment_stream;
137
138         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
139
140                 old_attachment = pos->data;
141                 attachment_filename = tny_mime_part_get_filename (old_attachment);
142                 attachment_stream = tny_mime_part_get_stream (old_attachment);
143                 attachment_part = tny_platform_factory_new_mime_part (
144                         modest_runtime_get_platform_factory());
145                 
146                 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
147                                  
148                 tny_mime_part_construct_from_stream (attachment_part,
149                                                      attachment_stream,
150                                                      attachment_content_type);
151                 tny_stream_reset (attachment_stream);
152                 
153                 tny_mime_part_set_filename (attachment_part, attachment_filename);
154                 
155                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
156 /*              g_object_unref (attachment_part); */
157         }
158 }
159
160
161 gchar * 
162 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
163 {
164         TnyStream *stream;
165         TnyMimePart *body;
166         GtkTextBuffer *buf;
167         GtkTextIter start, end;
168         gchar *to_quote;
169
170         body = modest_tny_msg_find_body_part(msg, want_html);
171         if (!body)
172                 return NULL;
173
174         buf = gtk_text_buffer_new (NULL);
175         stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
176         tny_stream_reset (stream);
177         tny_mime_part_decode_to_stream (body, stream);
178         tny_stream_reset (stream);
179
180         g_object_unref (G_OBJECT(stream));
181         g_object_unref (G_OBJECT(body));
182         
183         gtk_text_buffer_get_bounds (buf, &start, &end);
184         to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
185         g_object_unref (buf);
186
187         return to_quote;
188 }
189
190
191 TnyMimePart*
192 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
193 {
194         const gchar *mime_type = want_html ? "text/html" : "text/plain";
195         TnyMimePart *part = NULL;
196         TnyList *parts;
197         TnyIterator *iter;
198
199         if (!msg)
200                 return NULL;
201
202         parts = TNY_LIST (tny_simple_list_new());
203         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
204
205         iter  = tny_list_create_iterator(parts);
206
207         /* no parts? assume it's single-part message */
208         if (tny_iterator_is_done(iter)) 
209                 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
210         else {
211                 gchar *content_type = NULL;
212                 do {
213                         part = TNY_MIME_PART(tny_iterator_get_current (iter));
214
215                         /* we need to strdown the content type, because
216                          * tny_mime_part_has_content_type does not do it...
217                          */
218                         content_type = g_ascii_strdown
219                                 (tny_mime_part_get_content_type (part), -1);
220                                                         
221                         if (g_str_has_prefix (content_type, mime_type) &&
222                             !tny_mime_part_is_attachment (part))
223                                 break;
224                         
225                         if (g_str_has_prefix(content_type, "multipart")) {
226                                 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
227                                 if (part)
228                                         break;
229                         }
230                         if (part)
231                                 g_object_unref (G_OBJECT(part));
232
233                         part = NULL;
234                         
235                         g_free (content_type);
236                         content_type = NULL;
237
238                         tny_iterator_next (iter);
239                         
240                 } while (!tny_iterator_is_done(iter));
241                 g_free (content_type);
242         }
243         
244         g_object_unref (G_OBJECT(iter));
245         g_object_unref (G_OBJECT(parts));
246
247         /* if were trying to find an HTML part and couldn't find it,
248          * try to find a text/plain part instead
249          */
250         if (!part && want_html) 
251                 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
252         else
253                 return part; /* this maybe NULL, this is not an error; some message just don't have a body
254                               * part */
255 }
256
257
258 TnyMimePart*
259 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
260 {
261         return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
262                                                              want_html);
263 }
264
265
266
267 static gboolean
268 is_ascii(const gchar *s)
269 {
270         if (!s)
271                 return TRUE;
272         while (s[0]) {
273                 if (s[0] & 128 || s[0] < 32)
274                         return FALSE;
275                 s++;
276         }
277         return TRUE;
278 }
279
280 static char *
281 get_content_type(const gchar *s)
282 {
283         GString *type;
284         
285         type = g_string_new("text/plain");
286         if (!is_ascii(s)) {
287                 if (g_utf8_validate(s, -1, NULL)) {
288                         g_string_append(type, "; charset=\"utf-8\"");
289                 } else {
290                         /* it should be impossible to reach this, but better safe than sorry */
291                         g_warning("invalid utf8 in message");
292                         g_string_append(type, "; charset=\"latin1\"");
293                 }
294         }
295         return g_string_free(type, FALSE);
296 }