* make it compile again with latest Tinymail; does not work yet though.
[modest] / src / modest-tny-transport-actions.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
31 #include <tny-msg.h>
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>
44 #include <string.h>
45
46 #include "modest-tny-transport-actions.h"
47 #include "modest-tny-attachment.h"
48
49 static gboolean
50 is_ascii(const gchar *s)
51 {
52         while (s[0]) {
53                 if (s[0] & 128 || s[0] < 32)
54                         return FALSE;
55                 s++;
56         }
57         return TRUE;
58 }
59
60 static char *
61 get_content_type(const gchar *s)
62 {
63         GString *type;
64         
65         type = g_string_new("text/plain");
66         if (!is_ascii(s)) {
67                 if (g_utf8_validate(s, -1, NULL)) {
68                         g_string_append(type, "; charset=\"utf-8\"");
69                 } else {
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\"");
73                 }
74         }
75         return g_string_free(type, FALSE);
76 }
77
78 gboolean
79 modest_tny_transport_actions_send_message (TnyTransportAccount *transport_account,
80                                            const gchar *from,
81                                            const gchar *to,
82                                            const gchar *cc,
83                                            const gchar *bcc,
84                                            const gchar *subject,
85                                            const gchar *body,
86                                            const GList *attachments_list)
87 {
88         TnyMsg *new_msg;
89         TnyMimePart *attachment_part, *text_body_part;
90         TnyHeader *headers;
91         TnyStream *text_body_stream, *attachment_stream;
92         ModestTnyAttachment *attachment;
93         GList *pos;
94         gchar *content_type;
95         const gchar *attachment_content_type;
96         const gchar *attachment_filename;
97         
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))));
103         
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);
110
111         content_type = get_content_type(body);
112         
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);
118         } else {
119                 text_body_part = 
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,
123                                                      text_body_stream,
124                                                      content_type);
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));
128         }
129         
130 /*      for (    pos = (GList *)attachments_list; */
131 /*               pos; */
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())); */
138                 
139 /*              attachment_content_type = modest_tny_attachment_get_mime_type(attachment); */
140                                  
141 /*              tny_mime_part_construct_from_stream (attachment_part, */
142 /*                                                   attachment_stream, */
143 /*                                                   attachment_content_type); */
144 /*              tny_stream_reset (attachment_stream); */
145                 
146 /*              tny_mime_part_set_filename(attachment_part, attachment_filename); */
147                 
148 /*              tny_msg_add_part (new_msg, attachment_part); */
149 /*              //g_object_unref(G_OBJECT(attachment_part)); */
150 /*              //close(file); */
151 /*      } */
152         
153         tny_transport_account_send (transport_account, new_msg);
154
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);
159
160         return TRUE;    
161 }