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.
32 #include <tny-mime-part.h>
33 #include <tny-stream.h>
34 #include <tny-header.h>
35 #include <tny-account.h>
36 #include <tny-account-store.h>
37 #include <tny-transport-account.h>
38 #include <tny-stream-camel.h>
39 #include <tny-fs-stream.h>
40 #include <tny-camel-msg.h>
41 #include <tny-camel-header.h>
42 #include <tny-camel-stream.h>
43 #include <camel/camel-stream-mem.h>
46 #include "modest-tny-transport-actions.h"
47 #include "modest-tny-attachment.h"
50 is_ascii(const gchar *s)
53 if (s[0] & 128 || s[0] < 32)
61 get_content_type(const gchar *s)
65 type = g_string_new("text/plain");
67 if (g_utf8_validate(s, -1, NULL)) {
68 g_string_append(type, "; charset=\"utf-8\"");
70 /* it should be impossible to reach this, but better safe than sorry */
71 g_warning("invalid utf8 in message");
72 g_string_append(type, "; charset=\"latin1\"");
75 return g_string_free(type, FALSE);
79 modest_tny_transport_actions_send_message (TnyTransportAccount *transport_account,
86 const GList *attachments_list)
89 TnyMimePart *attachment_part, *text_body_part;
91 TnyStream *text_body_stream, *attachment_stream;
92 ModestTnyAttachment *attachment;
95 const gchar *attachment_content_type;
96 const gchar *attachment_filename;
98 new_msg = tny_camel_msg_new ();
99 headers = tny_camel_header_new ();
100 text_body_stream = TNY_STREAM (tny_camel_stream_new
101 (camel_stream_mem_new_with_buffer
102 (body, strlen(body))));
104 tny_header_set_from (TNY_HEADER (headers), from);
105 tny_header_set_to (TNY_HEADER (headers), to);
106 tny_header_set_cc (TNY_HEADER (headers), cc);
107 tny_header_set_bcc (TNY_HEADER (headers), bcc);
108 tny_header_set_subject (TNY_HEADER (headers), subject);
109 tny_msg_set_header (new_msg, headers);
111 content_type = get_content_type(body);
113 if (attachments_list == NULL) {
114 tny_stream_reset (text_body_stream);
115 tny_mime_part_construct_from_stream (TNY_MIME_PART(new_msg),
116 text_body_stream, content_type);
117 tny_stream_reset (text_body_stream);
120 TNY_MIME_PART (tny_camel_mime_part_new(camel_mime_part_new()));
121 tny_stream_reset (text_body_stream);
122 tny_mime_part_construct_from_stream (text_body_part,
125 tny_stream_reset (text_body_stream);
126 tny_msg_add_part(new_msg, text_body_part);
127 //g_object_unref (G_OBJECT(text_body_part));
130 /* for ( pos = (GList *)attachments_list; */
132 /* pos = pos->next ) { */
133 /* attachment = pos->data; */
134 /* attachment_filename = modest_tny_attachment_get_name(attachment); */
135 /* attachment_stream = modest_tny_attachment_get_stream(attachment); */
136 /* attachment_part = TNY_MIME_PART_IFACE (tny_camel_mime_part_new ( */
137 /* camel_mime_part_new())); */
139 /* attachment_content_type = modest_tny_attachment_get_mime_type(attachment); */
141 /* tny_mime_part_construct_from_stream (attachment_part, */
142 /* attachment_stream, */
143 /* attachment_content_type); */
144 /* tny_stream_reset (attachment_stream); */
146 /* tny_mime_part_set_filename(attachment_part, attachment_filename); */
148 /* tny_msg_add_part (new_msg, attachment_part); */
149 /* //g_object_unref(G_OBJECT(attachment_part)); */
153 tny_transport_account_send (transport_account, new_msg);
155 g_object_unref (G_OBJECT(text_body_stream));
156 g_object_unref (G_OBJECT(headers));
157 g_object_unref (G_OBJECT(new_msg));
158 g_free(content_type);