d12f2d7855f384eb8c9492f35fbb2fcf7c7de9c4
[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 /* modest-tny-transport-actions.c */
32
33 #include <tny-msg.h>
34 #include <tny-msg-iface.h>                      
35 #include <tny-msg-mime-part.h>
36 #include <tny-msg-mime-part-iface.h>            
37 #include <tny-stream-iface.h>
38 #include <tny-msg-header.h>
39 #include <tny-msg-header-iface.h>
40 #include <tny-account-iface.h>  
41 #include <tny-account-store-iface.h>
42 #include <tny-transport-account-iface.h>        
43 #include <tny-transport-account.h>
44 #include <tny-stream-camel.h>
45 #include <tny-fs-stream.h>
46 #include <string.h>
47 #include <camel/camel-folder.h>
48 #include <camel/camel.h>
49 #include <camel/camel-folder-summary.h>
50
51 #include "modest-tny-transport-actions.h"
52 #include "modest-tny-attachment.h"
53 /* include other impl specific header files */
54
55 /* 'private'/'protected' functions */
56 static void                              modest_tny_transport_actions_class_init   (ModestTnyTransportActionsClass *klass);
57 static void                              modest_tny_transport_actions_init         (ModestTnyTransportActions *obj);
58 static void                              modest_tny_transport_actions_finalize     (GObject *obj);
59 static gboolean                          is_ascii                                  (const gchar *s);
60 static char *                            get_content_type                          (const gchar *s);
61
62 /* list my signals */
63 enum {
64         /* MY_SIGNAL_1, */
65         /* MY_SIGNAL_2, */
66         LAST_SIGNAL
67 };
68
69 typedef struct _ModestTnyTransportActionsPrivate ModestTnyTransportActionsPrivate;
70 struct _ModestTnyTransportActionsPrivate {
71         /* my private members go here, eg. */
72         /* gboolean frobnicate_mode; */
73 };
74 #define MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
75                                                           MODEST_TYPE_TNY_TRANSPORT_ACTIONS, \
76                                                           ModestTnyTransportActionsPrivate))
77 /* globals */
78 static GObjectClass *parent_class = NULL;
79
80 /* uncomment the following if you have defined any signals */
81 /* static guint signals[LAST_SIGNAL] = {0}; */
82
83 GType
84 modest_tny_transport_actions_get_type (void)
85 {
86         static GType my_type = 0;
87         if (!my_type) {
88                 static const GTypeInfo my_info = {
89                         sizeof(ModestTnyTransportActionsClass),
90                         NULL,           /* base init */
91                         NULL,           /* base finalize */
92                         (GClassInitFunc) modest_tny_transport_actions_class_init,
93                         NULL,           /* class finalize */
94                         NULL,           /* class data */
95                         sizeof(ModestTnyTransportActions),
96                         1,              /* n_preallocs */
97                         (GInstanceInitFunc) modest_tny_transport_actions_init,
98                 };
99                 my_type = g_type_register_static (G_TYPE_OBJECT,
100                                                   "ModestTnyTransportActions",
101                                                   &my_info, 0);
102         }
103         return my_type;
104 }
105
106 static void
107 modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass)
108 {
109         GObjectClass *gobject_class;
110         gobject_class = (GObjectClass*) klass;
111
112         parent_class            = g_type_class_peek_parent (klass);
113         gobject_class->finalize = modest_tny_transport_actions_finalize;
114
115         g_type_class_add_private (gobject_class, sizeof(ModestTnyTransportActionsPrivate));
116
117         /* signal definitions go here, e.g.: */
118 /*      signals[MY_SIGNAL_1] = */
119 /*              g_signal_new ("my_signal_1",....); */
120 /*      signals[MY_SIGNAL_2] = */
121 /*              g_signal_new ("my_signal_2",....); */
122 /*      etc. */
123 }
124
125 static void
126 modest_tny_transport_actions_init (ModestTnyTransportActions *obj)
127 {
128 /* uncomment the following if you init any of the private data */
129 /*      ModestTnyTransportActionsPrivate *priv = MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(obj); */
130
131 /*      initialize this object, eg.: */
132 /*      priv->frobnicate_mode = FALSE; */
133 }
134
135 static void
136 modest_tny_transport_actions_finalize (GObject *obj)
137 {
138 /*      free/unref instance resources here */
139 }
140
141 ModestTnyTransportActions *
142 modest_tny_transport_actions_new (void)
143 {
144         return MODEST_TNY_TRANSPORT_ACTIONS(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS,
145                                                          NULL));
146 }
147
148 static gboolean
149 is_ascii(const gchar *s)
150 {
151         while (s[0]) {
152                 if (s[0] & 128 || s[0] < 32)
153                         return FALSE;
154                 s++;
155         }
156         return TRUE;
157 }
158
159 static char *
160 get_content_type(const gchar *s)
161 {
162         GString *type;
163         
164         type = g_string_new("text/plain");
165         if (!is_ascii(s)) {
166                 if (g_utf8_validate(s, -1, NULL)) {
167                         g_string_append(type, "; charset=\"utf-8\"");
168                 } else {
169                         /* it should be impossible to reach this, but better safe than sorry */
170                         g_warning("invalid utf8 in message");
171                         g_string_append(type, "; charset=\"latin1\"");
172                 }
173         }
174         return g_string_free(type, FALSE);
175 }
176
177 gboolean
178 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
179                                            TnyTransportAccountIface *transport_account,
180                                            const gchar *from,
181                                            const gchar *to,
182                                            const gchar *cc,
183                                            const gchar *bcc,
184                                            const gchar *subject,
185                                            const gchar *body,
186                                            const GList *attachments_list)
187 {
188         TnyMsgIface *new_msg;
189         TnyMsgMimePartIface *attachment_part, *text_body_part;
190         TnyMsgHeaderIface *headers;
191         TnyStreamIface *text_body_stream, *attachment_stream;
192         ModestTnyAttachment *attachment;
193         GList *pos;
194         gchar *content_type;
195         const gchar *attachment_content_type;
196         const gchar *attachment_filename;
197         
198         new_msg          = TNY_MSG_IFACE(tny_msg_new ());
199         headers          = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
200         text_body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
201                                              (camel_stream_mem_new_with_buffer
202                                               (body, strlen(body))));
203         
204         tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
205         tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
206         tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
207         tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
208         tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
209         tny_msg_iface_set_header (new_msg, headers);
210
211         
212         content_type = get_content_type(body);
213         
214         if (attachments_list == NULL) {
215                 tny_stream_iface_reset (text_body_stream);
216                 tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
217                                                                text_body_stream, content_type);
218                 tny_stream_iface_reset (text_body_stream);
219         } else {
220                 text_body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new(
221                                                           camel_mime_part_new()));
222                 tny_stream_iface_reset (text_body_stream);
223                 tny_msg_mime_part_iface_construct_from_stream (text_body_part,
224                                                                text_body_stream,
225                                                                content_type);
226                 tny_stream_iface_reset (text_body_stream);
227                 tny_msg_iface_add_part(new_msg, text_body_part);
228                 //g_object_unref (G_OBJECT(text_body_part));
229         }
230         
231         for (    pos = (GList *)attachments_list;
232                  pos;
233                  pos = pos->next    ) {
234                 attachment = pos->data;
235                 attachment_filename = modest_tny_attachment_get_name(attachment);
236                 attachment_stream = modest_tny_attachment_get_stream(attachment);
237                 attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new (
238                                                                 camel_mime_part_new()));
239                 
240                 attachment_content_type = modest_tny_attachment_get_mime_type(attachment);
241                                  
242                 tny_msg_mime_part_iface_construct_from_stream (attachment_part,
243                                                                attachment_stream,
244                                                                attachment_content_type);
245                 tny_stream_iface_reset (attachment_stream);
246                 
247                 tny_msg_mime_part_iface_set_filename(attachment_part, attachment_filename);
248                 
249                 tny_msg_iface_add_part (new_msg, attachment_part);
250                 //g_object_unref(G_OBJECT(attachment_part));
251                 //close(file);
252         }
253         
254         tny_transport_account_iface_send (transport_account, new_msg);
255
256         g_object_unref (G_OBJECT(text_body_stream));
257         g_object_unref (G_OBJECT(headers));
258         g_object_unref (G_OBJECT(new_msg));
259         g_free(content_type);
260
261         return TRUE;    
262 }