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 <camel/camel-stream-mem.h>
42 #endif /*HAVE_CONFIG_H */
44 #include <modest-tny-msg.h>
45 #include "modest-text-utils.h"
47 static TnyMimePart * add_body_part (TnyMsg *msg, const gchar *body,
48 const gchar *content_type);
49 static TnyMimePart * add_html_body_part (TnyMsg *msg, const gchar *body);
50 static void add_attachments (TnyMsg *msg, GList *attachments_list);
51 static char * get_content_type(const gchar *s);
52 static gboolean is_ascii(const gchar *s);
55 modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc,
56 const gchar *bcc, const gchar* subject, const gchar *body,
64 new_msg = modest_formatter_create_message (NULL, TRUE, (attachments != NULL));
65 header = tny_msg_get_header (new_msg);
67 if ((from != NULL) && (strlen(from) > 0)) {
68 tny_header_set_from (TNY_HEADER (header), from);
69 tny_header_set_replyto (TNY_HEADER (header), from);
71 if ((mailto != NULL) && (strlen(mailto) > 0))
72 tny_header_set_to (TNY_HEADER (header), mailto);
73 if ((cc != NULL) && (strlen(cc) > 0))
74 tny_header_set_cc (TNY_HEADER (header), cc);
75 if ((bcc != NULL) && (strlen(bcc) > 0))
76 tny_header_set_bcc (TNY_HEADER (header), bcc);
78 if ((subject != NULL) && (strlen(subject) > 0))
79 tny_header_set_subject (TNY_HEADER (header), subject);
81 content_type = get_content_type(body);
83 /* Add the body of the new mail */
84 /* This is needed even if body is NULL or empty. */
85 add_body_part (new_msg, body, content_type);
86 g_free (content_type);
90 add_attachments (new_msg, (GList*) attachments);
96 modest_tny_msg_new_html_plain (const gchar* mailto, const gchar* from, const gchar *cc,
97 const gchar *bcc, const gchar* subject,
98 const gchar *html_body, const gchar *plain_body,
106 new_msg = modest_formatter_create_message (NULL, FALSE, (attachments != NULL));
107 header = tny_msg_get_header (new_msg);
109 if ((from != NULL) && (strlen(from) > 0)) {
110 tny_header_set_from (TNY_HEADER (header), from);
111 tny_header_set_replyto (TNY_HEADER (header), from);
113 if ((mailto != NULL) && (strlen(mailto) > 0))
114 tny_header_set_to (TNY_HEADER (header), mailto);
115 if ((cc != NULL) && (strlen(cc) > 0))
116 tny_header_set_cc (TNY_HEADER (header), cc);
117 if ((bcc != NULL) && (strlen(bcc) > 0))
118 tny_header_set_bcc (TNY_HEADER (header), bcc);
120 if ((subject != NULL) && (strlen(subject) > 0))
121 tny_header_set_subject (TNY_HEADER (header), subject);
123 content_type = get_content_type(plain_body);
125 /* Add the body of the new mail */
126 add_body_part (new_msg, plain_body, content_type);
127 add_html_body_part (new_msg, html_body);
128 g_free (content_type);
130 /* Add attachments */
131 add_attachments (new_msg, (GList*) attachments);
137 /* FIXME: this func copy from modest-mail-operation: refactor */
139 add_body_part (TnyMsg *msg,
141 const gchar *content_type)
143 TnyMimePart *text_body_part = NULL;
144 TnyStream *text_body_stream;
146 /* Create the stream */
147 text_body_stream = TNY_STREAM (tny_camel_stream_new
148 (camel_stream_mem_new_with_buffer
149 (body, (body ? strlen(body) : 0))));
151 text_body_part = modest_formatter_create_body_part (NULL, msg);
153 /* Construct MIME part */
154 tny_stream_reset (text_body_stream);
155 tny_mime_part_construct_from_stream (text_body_part,
158 tny_stream_reset (text_body_stream);
160 g_object_unref (G_OBJECT(text_body_part));
163 g_object_unref (text_body_stream);
165 return text_body_part;
169 add_html_body_part (TnyMsg *msg,
172 TnyMimePart *html_body_part = NULL;
173 TnyStream *html_body_stream;
175 /* Create the stream */
176 html_body_stream = TNY_STREAM (tny_camel_stream_new
177 (camel_stream_mem_new_with_buffer
178 (body, strlen(body))));
180 /* Create body part if needed */
181 html_body_part = modest_formatter_create_body_part (NULL, msg);
183 /* Construct MIME part */
184 tny_stream_reset (html_body_stream);
185 tny_mime_part_construct_from_stream (html_body_part,
187 "text/html; charset=utf-8");
188 tny_stream_reset (html_body_stream);
190 g_object_unref (G_OBJECT(html_body_part));
193 g_object_unref (html_body_stream);
195 return html_body_part;
199 add_attachments (TnyMsg *msg, GList *attachments_list)
202 TnyMimePart *attachment_part, *old_attachment;
203 const gchar *attachment_content_type;
204 const gchar *attachment_filename;
205 const gchar *attachment_cid;
206 TnyStream *attachment_stream;
208 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
210 old_attachment = pos->data;
211 attachment_filename = tny_mime_part_get_filename (old_attachment);
212 attachment_stream = tny_mime_part_get_stream (old_attachment);
213 if (TNY_IS_MSG (old_attachment)) {
214 attachment_part = TNY_MIME_PART (tny_platform_factory_new_msg (modest_runtime_get_platform_factory ()));
216 attachment_part = tny_platform_factory_new_mime_part (
217 modest_runtime_get_platform_factory());
219 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
220 attachment_cid = tny_mime_part_get_content_id (old_attachment);
222 tny_stream_reset (attachment_stream);
223 tny_mime_part_construct_from_stream (attachment_part,
225 attachment_content_type);
226 tny_stream_reset (attachment_stream);
228 tny_mime_part_set_filename (attachment_part, attachment_filename);
229 tny_mime_part_set_content_id (attachment_part, attachment_cid);
231 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
232 g_object_unref (attachment_stream);
233 /* g_object_unref (attachment_part); */
239 modest_tny_msg_get_body (TnyMsg *msg, gboolean want_html)
244 GtkTextIter start, end;
247 body = modest_tny_msg_find_body_part(msg, want_html);
251 buf = gtk_text_buffer_new (NULL);
252 stream = TNY_STREAM (tny_gtk_text_buffer_stream_new (buf));
253 tny_stream_reset (stream);
254 tny_mime_part_decode_to_stream (body, stream);
255 tny_stream_reset (stream);
257 gtk_text_buffer_get_bounds (buf, &start, &end);
258 to_quote = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
259 if (tny_mime_part_content_type_is (body, "text/plain")) {
260 gchar *to_quote_converted = modest_text_utils_convert_to_html (to_quote);
262 to_quote = to_quote_converted;
265 g_object_unref (buf);
266 g_object_unref (G_OBJECT(stream));
267 g_object_unref (G_OBJECT(body));
274 modest_tny_msg_find_body_part_from_mime_part (TnyMimePart *msg, gboolean want_html)
276 const gchar *mime_type = want_html ? "text/html" : "text/plain";
277 TnyMimePart *part = NULL;
284 parts = TNY_LIST (tny_simple_list_new());
285 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
287 iter = tny_list_create_iterator(parts);
289 /* no parts? assume it's single-part message */
290 if (tny_iterator_is_done(iter))
291 return TNY_MIME_PART (g_object_ref(G_OBJECT(msg)));
293 gchar *content_type = NULL;
295 part = TNY_MIME_PART(tny_iterator_get_current (iter));
297 /* we need to strdown the content type, because
298 * tny_mime_part_has_content_type does not do it...
300 content_type = g_ascii_strdown
301 (tny_mime_part_get_content_type (part), -1);
303 if (g_str_has_prefix (content_type, mime_type) &&
304 !tny_mime_part_is_attachment (part))
307 if (g_str_has_prefix(content_type, "multipart")) {
308 part = modest_tny_msg_find_body_part_from_mime_part (part, want_html);
313 g_object_unref (G_OBJECT(part));
317 g_free (content_type);
320 tny_iterator_next (iter);
322 } while (!tny_iterator_is_done(iter));
323 g_free (content_type);
326 g_object_unref (G_OBJECT(iter));
327 g_object_unref (G_OBJECT(parts));
329 /* if were trying to find an HTML part and couldn't find it,
330 * try to find a text/plain part instead
332 if (!part && want_html)
333 return modest_tny_msg_find_body_part_from_mime_part (msg, FALSE);
335 return part; /* this maybe NULL, this is not an error; some message just don't have a body
341 modest_tny_msg_find_body_part (TnyMsg *msg, gboolean want_html)
343 return modest_tny_msg_find_body_part_from_mime_part (TNY_MIME_PART(msg),
349 create_reply_forward_mail (TnyMsg *msg, const gchar *from, const gchar *signature,
350 gboolean is_reply, guint type, GList *attachments)
353 TnyHeader *new_header, *header;
356 ModestFormatter *formatter;
357 gchar *subject_prefix;
359 /* Get body from original msg. Always look for the text/plain
360 part of the message to create the reply/forwarded mail */
361 header = tny_msg_get_header (msg);
362 body = modest_tny_msg_find_body_part (msg, FALSE);
364 /* TODO: select the formatter from account prefs */
365 if (modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL))
366 formatter = modest_formatter_new ("text/html", signature);
368 formatter = modest_formatter_new ("text/plain", signature);
370 /* Format message body */
373 case MODEST_TNY_MSG_REPLY_TYPE_CITE:
375 new_msg = modest_formatter_cite (formatter, body, header);
377 case MODEST_TNY_MSG_REPLY_TYPE_QUOTE:
378 new_msg = modest_formatter_quote (formatter, body, header, attachments);
383 case MODEST_TNY_MSG_FORWARD_TYPE_INLINE:
385 new_msg = modest_formatter_inline (formatter, body, header, attachments);
387 case MODEST_TNY_MSG_FORWARD_TYPE_ATTACHMENT:
388 new_msg = modest_formatter_attach (formatter, body, header);
392 g_object_unref (G_OBJECT(formatter));
393 g_object_unref (G_OBJECT(body));
395 /* Fill the header */
396 new_header = tny_msg_get_header (new_msg);
397 tny_header_set_from (new_header, from);
398 tny_header_set_replyto (new_header, from);
400 /* Change the subject */
402 subject_prefix = g_strconcat (_("mail_va_re"), ":", NULL);
404 subject_prefix = g_strconcat (_("mail_va_fw"), ":", NULL);
406 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
408 g_free (subject_prefix);
409 tny_header_set_subject (new_header, (const gchar *) new_subject);
410 g_free (new_subject);
413 g_object_unref (G_OBJECT (new_header));
414 g_object_unref (G_OBJECT (header));
420 add_if_attachment (gpointer data, gpointer user_data)
423 GList **attachments_list;
425 part = TNY_MIME_PART (data);
426 attachments_list = ((GList **) user_data);
428 if ((tny_mime_part_is_attachment (part))||(TNY_IS_MSG (part)))
429 *attachments_list = g_list_prepend (*attachments_list, part);
433 modest_tny_msg_create_forward_msg (TnyMsg *msg,
435 const gchar *signature,
436 ModestTnyMsgForwardType forward_type)
439 TnyList *parts = NULL;
440 GList *attachments_list = NULL;
442 /* Add attachments */
443 parts = TNY_LIST (tny_simple_list_new());
444 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
445 tny_list_foreach (parts, add_if_attachment, &attachments_list);
447 new_msg = create_reply_forward_mail (msg, from, signature, FALSE, forward_type, attachments_list);
448 add_attachments (new_msg, attachments_list);
451 if (attachments_list)
452 g_list_free (attachments_list);
453 g_object_unref (G_OBJECT (parts));
459 modest_tny_msg_create_reply_msg (TnyMsg *msg,
461 const gchar *signature,
462 ModestTnyMsgReplyType reply_type,
463 ModestTnyMsgReplyMode reply_mode)
465 TnyMsg *new_msg = NULL;
466 TnyHeader *new_header, *header;
467 const gchar* reply_to;
468 gchar *new_cc = NULL;
469 const gchar *cc = NULL, *bcc = NULL;
471 TnyList *parts = NULL;
472 GList *attachments_list = NULL;
474 /* Add attachments */
475 parts = TNY_LIST (tny_simple_list_new());
476 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
477 tny_list_foreach (parts, add_if_attachment, &attachments_list);
479 new_msg = create_reply_forward_mail (msg, from, signature, TRUE, reply_type, attachments_list);
480 if (attachments_list) {
481 g_list_foreach (attachments_list, (GFunc) g_object_unref, NULL);
482 g_list_free (attachments_list);
484 g_object_unref (G_OBJECT (parts));
486 /* Fill the header */
487 header = tny_msg_get_header (msg);
488 new_header = tny_msg_get_header (new_msg);
489 reply_to = tny_header_get_replyto (header);
492 tny_header_set_to (new_header, reply_to);
494 tny_header_set_to (new_header, tny_header_get_from (header));
496 switch (reply_mode) {
497 case MODEST_TNY_MSG_REPLY_MODE_SENDER:
498 /* Do not fill neither cc nor bcc */
500 case MODEST_TNY_MSG_REPLY_MODE_LIST:
503 case MODEST_TNY_MSG_REPLY_MODE_ALL:
504 /* Concatenate to, cc and bcc */
505 cc = tny_header_get_cc (header);
506 bcc = tny_header_get_bcc (header);
508 tmp = g_string_new (tny_header_get_to (header));
509 if (cc) g_string_append_printf (tmp, ",%s",cc);
510 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
512 /* Remove my own address from the cc list. TODO:
513 remove also the To: of the new message, needed due
514 to the new reply_to feature */
516 modest_text_utils_remove_address ((const gchar *) tmp->str,
518 /* FIXME: remove also the mails from the new To: */
519 tny_header_set_cc (new_header, new_cc);
522 g_string_free (tmp, TRUE);
528 g_object_unref (G_OBJECT (new_header));
529 g_object_unref (G_OBJECT (header));
536 is_ascii(const gchar *s)
541 if (s[0] & 128 || s[0] < 32)
549 get_content_type(const gchar *s)
553 type = g_string_new("text/plain");
555 if (g_utf8_validate(s, -1, NULL)) {
556 g_string_append(type, "; charset=\"utf-8\"");
558 /* it should be impossible to reach this, but better safe than sorry */
559 g_warning("invalid utf8 in message");
560 g_string_append(type, "; charset=\"latin1\"");
563 return g_string_free(type, FALSE);