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 "modest-formatter.h"
37 #include <tny-camel-stream.h>
38 #include <tny-camel-mime-part.h>
39 #include <camel/camel-stream-buffer.h>
40 #include <camel/camel-stream-mem.h>
41 #include <glib/gprintf.h>
45 #endif /*HAVE_CONFIG_H */
47 #include <modest-tny-msg.h>
48 #include "modest-text-utils.h"
50 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
51 const gchar *content_type);
52 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
53 static void add_attachments (TnyMsg *msg, GList *attachments_list);
54 static char * get_content_type(const gchar *s);
55 static gboolean is_ascii(const gchar *s);
59 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
60 const gchar *bcc, const gchar* subject, const gchar *body,
68 new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL));
69 header = tny_msg_get_header (new_msg);
71 if ((from != NULL) && (strlen(from) > 0)) {
72 tny_header_set_from (TNY_HEADER (header), from);
73 tny_header_set_replyto (TNY_HEADER (header), from);
75 if ((mailto != NULL) && (strlen(mailto) > 0))
76 tny_header_set_to (TNY_HEADER (header), mailto);
77 if ((cc != NULL) && (strlen(cc) > 0))
78 tny_header_set_cc (TNY_HEADER (header), cc);
79 if ((bcc != NULL) && (strlen(bcc) > 0))
80 tny_header_set_bcc (TNY_HEADER (header), bcc);
82 if ((subject != NULL) && (strlen(subject) > 0))
83 tny_header_set_subject (TNY_HEADER (header), subject);
85 content_type = get_content_type(body);
87 /* Add the body of the new mail */
88 /* This is needed even if body is NULL or empty. */
89 add_body_part (new_msg, body, content_type);
90 g_free (content_type);
94 add_attachments (new_msg, attachments);
100 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
101 const gchar *bcc, const gchar* subject,
102 const gchar *html_body, const gchar *plain_body,
110 new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL));
111 header = tny_msg_get_header (new_msg);
113 if ((from != NULL) && (strlen(from) > 0)) {
114 tny_header_set_from (TNY_HEADER (header), from);
115 tny_header_set_replyto (TNY_HEADER (header), from);
117 if ((mailto != NULL) && (strlen(mailto) > 0))
118 tny_header_set_to (TNY_HEADER (header), mailto);
119 if ((cc != NULL) && (strlen(cc) > 0))
120 tny_header_set_cc (TNY_HEADER (header), cc);
121 if ((bcc != NULL) && (strlen(bcc) > 0))
122 tny_header_set_bcc (TNY_HEADER (header), bcc);
124 if ((subject != NULL) && (strlen(subject) > 0))
125 tny_header_set_subject (TNY_HEADER (header), subject);
127 content_type = get_content_type(plain_body);
129 /* Add the body of the new mail */
130 add_body_part (new_msg, plain_body, content_type);
131 add_html_body_part (new_msg, html_body);
132 g_free (content_type);
134 /* Add attachments */
135 add_attachments (new_msg, attachments);
141 /* FIXME: this func copy from modest-mail-operation: refactor */
143 add_body_part (TnyMsg *msg,
145 const gchar *content_type)
147 TnyMimePart *text_body_part = NULL;
148 TnyStream *text_body_stream;
150 /* Create the stream */
151 text_body_stream = TNY_STREAM (tny_camel_stream_new
152 (camel_stream_mem_new_with_buffer
153 (body, (body ? strlen(body) : 0))));
155 text_body_part = modest_formatter_create_body_part (NULL, msg);
157 /* Construct MIME part */
158 tny_stream_reset (text_body_stream);
159 tny_mime_part_construct_from_stream (text_body_part,
162 tny_stream_reset (text_body_stream);
164 g_object_unref (G_OBJECT(text_body_part));
167 g_object_unref (text_body_stream);
169 return text_body_part;
173 add_html_body_part (TnyMsg *msg,
176 TnyMimePart *html_body_part = NULL;
177 TnyStream *html_body_stream;
179 /* Create the stream */
180 html_body_stream = TNY_STREAM (tny_camel_stream_new
181 (camel_stream_mem_new_with_buffer
182 (body, strlen(body))));
184 /* Create body part if needed */
185 html_body_part = modest_formatter_create_body_part (NULL, msg);
187 /* Construct MIME part */
188 tny_stream_reset (html_body_stream);
189 tny_mime_part_construct_from_stream (html_body_part,
191 "text/html; charset=utf-8");
192 tny_stream_reset (html_body_stream);
194 g_object_unref (G_OBJECT(html_body_part));
197 g_object_unref (html_body_stream);
199 return html_body_part;
203 copy_mime_part (TnyMimePart *part)
205 TnyMimePart *result = NULL;
206 const gchar *attachment_content_type;
207 const gchar *attachment_filename;
208 const gchar *attachment_cid;
210 TnyIterator *iterator;
211 TnyStream *attachment_stream;
213 if (TNY_IS_MSG (part)) {
214 result = TNY_MIME_PART (tny_platform_factory_new_msg (modest_runtime_get_platform_factory ()));
215 attachment_content_type = "message/rfc822";
216 tny_mime_part_set_content_type (result, attachment_content_type);
218 result = tny_platform_factory_new_mime_part (
219 modest_runtime_get_platform_factory());
220 attachment_content_type = tny_mime_part_get_content_type (part);
223 /* get mime part headers */
224 attachment_filename = tny_mime_part_get_filename (part);
225 attachment_cid = tny_mime_part_get_content_id (part);
227 /* fill the stream */
228 attachment_stream = tny_mime_part_get_stream (part);
229 tny_stream_reset (attachment_stream);
230 tny_mime_part_construct_from_stream (result,
232 attachment_content_type);
233 tny_stream_reset (attachment_stream);
235 /* set other mime part fields */
236 tny_mime_part_set_filename (result, attachment_filename);
237 tny_mime_part_set_content_id (result, attachment_cid);
240 parts = tny_simple_list_new ();
241 tny_mime_part_get_parts (part, parts);
242 iterator = tny_list_create_iterator (parts);
243 while (!tny_iterator_is_done (iterator)) {
244 TnyMimePart *subpart = TNY_MIME_PART (tny_iterator_get_current (iterator));
245 TnyMimePart *subpart_copy = copy_mime_part (subpart);
246 tny_mime_part_add_part (result, subpart_copy);
247 g_object_unref (subpart);
248 tny_iterator_next (iterator);
250 g_object_unref (iterator);
251 g_object_unref (parts);
252 g_object_unref (attachment_stream);
258 add_attachments (TnyMsg *msg, GList *attachments_list)
261 TnyMimePart *attachment_part, *old_attachment;
263 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
265 old_attachment = pos->data;
266 attachment_part = copy_mime_part (old_attachment);
267 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
273 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
278 GtkTextIter start, end;
281 body = modest_tny_msg_find_body_part(msg, want_html);
285 buf = gtk_text_buffer_new (NULL);
286 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
287 tny_stream_reset (stream);
288 tny_mime_part_decode_to_stream (body, stream);
289 tny_stream_reset (stream);
291 gtk_text_buffer_get_bounds (buf, &start, &end);
292 to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
293 if (tny_mime_part_content_type_is (body, "text/plain")) {
294 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
296 to_quote = to_quote_converted;
299 g_object_unref (buf);
300 g_object_unref (G_OBJECT(stream));
301 g_object_unref (G_OBJECT(body));
308 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
310 const gchar *mime_type = want_html ? "text/html" : "text/plain";
311 TnyMimePart *part = NULL;
318 parts = TNY_LIST (tny_simple_list_new());
319 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
321 iter = tny_list_create_iterator(parts);
323 /* no parts? assume it's single-part message */
324 if (tny_iterator_is_done(iter)) {
325 g_object_unref (G_OBJECT(iter));
326 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
328 gchar *content_type = NULL;
330 part = TNY_MIME_PART(tny_iterator_get_current (iter));
332 /* we need to strdown the content type, because
333 * tny_mime_part_has_content_type does not do it...
335 content_type = g_ascii_strdown
336 (tny_mime_part_get_content_type (part), -1);
338 if (g_str_has_prefix (content_type, mime_type) &&
339 !tny_mime_part_is_attachment (part))
342 if (g_str_has_prefix(content_type, "multipart")) {
343 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
348 g_object_unref (G_OBJECT(part));
352 g_free (content_type);
355 tny_iterator_next (iter);
357 } while (!tny_iterator_is_done(iter));
358 g_free (content_type);
361 g_object_unref (G_OBJECT(iter));
362 g_object_unref (G_OBJECT(parts));
364 /* if were trying to find an HTML part and couldn't find it,
365 * try to find a text/plain part instead
367 if (!part && want_html)
368 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
370 return part; /* this maybe NULL, this is not an error; some message just don't have a body
376 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
378 return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
384 create_reply_forward_mail (TnyMsg *msg, const gchar *from, const gchar *signature,
385 gboolean is_reply, guint type, GList *attachments)
388 TnyHeader *new_header, *header;
391 ModestFormatter *formatter;
392 gchar *subject_prefix;
394 /* Get body from original msg. Always look for the text/plain
395 part of the message to create the reply/forwarded mail */
396 header = tny_msg_get_header (msg);
397 body = modest_tny_msg_find_body_part (msg, FALSE);
399 /* TODO: select the formatter from account prefs */
400 if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
401 formatter = modest_formatter_new ("text/html", signature);
403 formatter = modest_formatter_new ("text/plain", signature);
405 /* Format message body */
408 case MODEST_TNY_MSG_REPLY_TYPE_CITE:
410 new_msg = modest_formatter_cite (formatter, body, header);
412 case MODEST_TNY_MSG_REPLY_TYPE_QUOTE:
413 new_msg = modest_formatter_quote (formatter, body, header, attachments);
418 case MODEST_TNY_MSG_FORWARD_TYPE_INLINE:
420 new_msg = modest_formatter_inline (formatter, body, header, attachments);
422 case MODEST_TNY_MSG_FORWARD_TYPE_ATTACHMENT:
423 new_msg = modest_formatter_attach (formatter, body, header);
427 g_object_unref (G_OBJECT(formatter));
428 g_object_unref (G_OBJECT(body));
430 /* Fill the header */
431 new_header = tny_msg_get_header (new_msg);
432 tny_header_set_from (new_header, from);
433 tny_header_set_replyto (new_header, from);
435 /* Change the subject */
437 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
439 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
441 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
443 g_free (subject_prefix);
444 tny_header_set_subject (new_header, (const gchar *) new_subject);
445 g_free (new_subject);
448 g_object_unref (G_OBJECT (new_header));
449 g_object_unref (G_OBJECT (header));
455 add_if_attachment (gpointer data, gpointer user_data)
458 GList **attachments_list;
460 part = TNY_MIME_PART (data);
461 attachments_list = ((GList **) user_data);
463 if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part))) {
464 *attachments_list = g_list_prepend (*attachments_list, part);
470 modest_tny_msg_create_forward_msg (TnyMsg *msg,
472 const gchar *signature,
473 ModestTnyMsgForwardType forward_type)
476 TnyList *parts = NULL;
477 GList *attachments_list = NULL;
479 /* Add attachments */
480 parts = TNY_LIST (tny_simple_list_new());
481 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
482 tny_list_foreach (parts, add_if_attachment, &attachments_list);
484 new_msg = create_reply_forward_mail (msg, from, signature, FALSE, forward_type, attachments_list);
485 add_attachments (new_msg, attachments_list);
488 if (attachments_list)
489 g_list_free (attachments_list);
490 g_object_unref (G_OBJECT (parts));
496 modest_tny_msg_create_reply_msg (TnyMsg *msg,
498 const gchar *signature,
499 ModestTnyMsgReplyType reply_type,
500 ModestTnyMsgReplyMode reply_mode)
502 TnyMsg *new_msg = NULL;
503 TnyHeader *new_header, *header;
504 const gchar* reply_to;
505 gchar *new_cc = NULL;
506 const gchar *cc = NULL, *bcc = NULL;
508 TnyList *parts = NULL;
509 GList *attachments_list = NULL;
511 /* Add attachments */
512 parts = TNY_LIST (tny_simple_list_new());
513 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
514 tny_list_foreach (parts, add_if_attachment, &attachments_list);
516 new_msg = create_reply_forward_mail (msg, from, signature, TRUE, reply_type, attachments_list);
517 if (attachments_list) {
518 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
519 g_list_free (attachments_list);
521 g_object_unref (G_OBJECT (parts));
523 /* Fill the header */
524 header = tny_msg_get_header (msg);
525 new_header = tny_msg_get_header (new_msg);
526 reply_to = tny_header_get_replyto (header);
529 tny_header_set_to (new_header, reply_to);
531 tny_header_set_to (new_header, tny_header_get_from (header));
533 switch (reply_mode) {
534 case MODEST_TNY_MSG_REPLY_MODE_SENDER:
535 /* Do not fill neither cc nor bcc */
537 case MODEST_TNY_MSG_REPLY_MODE_LIST:
540 case MODEST_TNY_MSG_REPLY_MODE_ALL:
541 /* Concatenate to, cc and bcc */
542 cc = tny_header_get_cc (header);
543 bcc = tny_header_get_bcc (header);
545 tmp = g_string_new (tny_header_get_to (header));
546 if (cc) g_string_append_printf (tmp, ",%s",cc);
547 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
549 /* Remove my own address from the cc list. TODO:
550 remove also the To: of the new message, needed due
551 to the new reply_to feature */
553 modest_text_utils_remove_address ((const gchar *) tmp->str,
555 /* FIXME: remove also the mails from the new To: */
556 tny_header_set_cc (new_header, new_cc);
559 g_string_free (tmp, TRUE);
565 g_object_unref (G_OBJECT (new_header));
566 g_object_unref (G_OBJECT (header));
573 is_ascii(const gchar *s)
578 if (s[0] & 128 || s[0] < 32)
586 get_content_type(const gchar *s)
590 type = g_string_new("text/plain");
592 if (g_utf8_validate(s, -1, NULL)) {
593 g_string_append(type, "; charset=\"utf-8\"");
595 /* it should be impossible to reach this, but better safe than sorry */
596 g_warning("invalid utf8 in message");
597 g_string_append(type, "; charset=\"latin1\"");
600 return g_string_free(type, FALSE);