1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
41 #endif /*HAVE_CONFIG_H */
43 #include <modest-tny-msg.h>
44 #include "modest-text-utils.h"
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);
53 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
54 const gchar *bcc, const gchar* subject, const gchar *body,
57 TnyPlatformFactory *fact;
63 fact = modest_runtime_get_platform_factory ();
64 new_msg = tny_platform_factory_new_msg (fact);
65 header = tny_msg_get_header (new_msg);
67 tny_header_set_from (TNY_HEADER (header), from);
68 tny_header_set_replyto (TNY_HEADER (header), from);
69 tny_header_set_to (TNY_HEADER (header), mailto);
70 tny_header_set_cc (TNY_HEADER (header), cc);
71 tny_header_set_bcc (TNY_HEADER (header), bcc);
72 tny_header_set_subject (TNY_HEADER (header), subject);
74 content_type = get_content_type(body);
76 /* Add the body of the new mail */
77 add_body_part (new_msg, body, content_type, (attachments ? TRUE: FALSE));
80 add_attachments (new_msg, (GList*) attachments);
86 /* FIXME: this func copy from modest-mail-operation: refactor */
88 add_body_part (TnyMsg *msg,
90 const gchar *content_type,
91 gboolean has_attachments)
93 TnyMimePart *text_body_part = NULL;
94 TnyStream *text_body_stream;
96 /* Create the stream */
97 text_body_stream = TNY_STREAM (tny_camel_stream_new
98 (camel_stream_mem_new_with_buffer
99 (body, strlen(body))));
101 /* Create body part if needed */
103 text_body_part = tny_platform_factory_new_mime_part
104 (modest_runtime_get_platform_factory ());
106 text_body_part = TNY_MIME_PART(msg);
108 /* Construct MIME part */
109 tny_stream_reset (text_body_stream);
110 tny_mime_part_construct_from_stream (text_body_part,
113 tny_stream_reset (text_body_stream);
115 /* Add part if needed */
116 if (has_attachments) {
117 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
118 g_object_unref (G_OBJECT(text_body_part));
122 g_object_unref (text_body_stream);
124 return text_body_part;
128 add_attachments (TnyMsg *msg, GList *attachments_list)
131 TnyMimePart *attachment_part, *old_attachment;
132 const gchar *attachment_content_type;
133 const gchar *attachment_filename;
134 TnyStream *attachment_stream;
136 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
138 old_attachment = pos->data;
139 attachment_filename = tny_mime_part_get_filename (old_attachment);
140 attachment_stream = tny_mime_part_get_stream (old_attachment);
141 attachment_part = tny_platform_factory_new_mime_part (
142 modest_runtime_get_platform_factory());
144 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
146 tny_mime_part_construct_from_stream (attachment_part,
148 attachment_content_type);
149 tny_stream_reset (attachment_stream);
151 tny_mime_part_set_filename (attachment_part, attachment_filename);
153 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
154 /* g_object_unref (attachment_part); */
160 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
165 GtkTextIter start, end;
168 body = modest_tny_msg_find_body_part(msg, want_html);
172 buf = gtk_text_buffer_new (NULL);
173 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
174 tny_stream_reset (stream);
175 tny_mime_part_decode_to_stream (body, stream);
176 tny_stream_reset (stream);
178 g_object_unref (G_OBJECT(stream));
179 g_object_unref (G_OBJECT(body));
181 gtk_text_buffer_get_bounds (buf, &start, &end);
182 to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
183 g_object_unref (buf);
190 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
192 const gchar *mime_type = want_html ? "text/html" : "text/plain";
193 TnyMimePart *part = NULL;
200 parts = TNY_LIST (tny_simple_list_new());
201 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
203 iter = tny_list_create_iterator(parts);
205 /* no parts? assume it's single-part message */
206 if (tny_iterator_is_done(iter))
207 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
209 gchar *content_type = NULL;
211 part = TNY_MIME_PART(tny_iterator_get_current (iter));
213 /* we need to strdown the content type, because
214 * tny_mime_part_has_content_type does not do it...
216 content_type = g_ascii_strdown
217 (tny_mime_part_get_content_type (part), -1);
219 if (g_str_has_prefix (content_type, mime_type) &&
220 !tny_mime_part_is_attachment (part))
223 if (g_str_has_prefix(content_type, "multipart")) {
224 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
229 g_object_unref (G_OBJECT(part));
233 g_free (content_type);
236 tny_iterator_next (iter);
238 } while (!tny_iterator_is_done(iter));
239 g_free (content_type);
242 g_object_unref (G_OBJECT(iter));
243 g_object_unref (G_OBJECT(parts));
245 /* if were trying to find an HTML part and couldn't find it,
246 * try to find a text/plain part instead
248 if (!part && want_html)
249 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
251 return part; /* this maybe NULL, this is not an error; some message just don't have a body
257 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
259 return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
266 is_ascii(const gchar *s)
271 if (s[0] & 128 || s[0] < 32)
279 get_content_type(const gchar *s)
283 type = g_string_new("text/plain");
285 if (g_utf8_validate(s, -1, NULL)) {
286 g_string_append(type, "; charset=\"utf-8\"");
288 /* it should be impossible to reach this, but better safe than sorry */
289 g_warning("invalid utf8 in message");
290 g_string_append(type, "; charset=\"latin1\"");
293 return g_string_free(type, FALSE);