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