* modest_ui_new now returns ModestUI* instead of GObject
[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 GObject*
142 modest_tny_transport_actions_new (void)
143 {
144         return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
145 }
146
147 static gboolean
148 is_ascii(const gchar *s)
149 {
150         while (s[0]) {
151                 if (s[0] & 128 || s[0] < 32)
152                         return FALSE;
153                 s++;
154         }
155         return TRUE;
156 }
157
158 static char *
159 get_content_type(const gchar *s)
160 {
161         GString *type;
162         
163         type = g_string_new("text/plain");
164         if (!is_ascii(s)) {
165                 if (g_utf8_validate(s, -1, NULL)) {
166                         g_string_append(type, "; charset=\"utf-8\"");
167                 } else {
168                         /* it should be impossible to reach this, but better safe than sorry */
169                         g_warning("invalid utf8 in message");
170                         g_string_append(type, "; charset=\"latin1\"");
171                 }
172         }
173         return g_string_free(type, FALSE);
174 }
175
176 gboolean
177 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
178                                            TnyTransportAccountIface *transport_account,
179                                            const gchar *from,
180                                            const gchar *to,
181                                            const gchar *cc,
182                                            const gchar *bcc,
183                                            const gchar *subject,
184                                            const gchar *body,
185                                            const GList *attachments_list)
186 {
187         TnyMsgIface *new_msg;
188         TnyMsgMimePartIface *attachment_part, *text_body_part;
189         TnyMsgHeaderIface *headers;
190         TnyStreamIface *text_body_stream, *attachment_stream;
191         ModestTnyAttachment *attachment;
192         GList *pos;
193         gchar *content_type;
194         const gchar *attachment_content_type;
195         const gchar *attachment_filename;
196         
197         new_msg          = TNY_MSG_IFACE(tny_msg_new ());
198         headers          = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
199         text_body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
200                                              (camel_stream_mem_new_with_buffer
201                                               (body, strlen(body))));
202         
203         tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
204         tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
205         tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
206         tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
207         tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
208         tny_msg_iface_set_header (new_msg, headers);
209
210         
211         content_type = get_content_type(body);
212         
213         if (attachments_list == NULL) {
214                 tny_stream_iface_reset (text_body_stream);
215                 tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
216                                                                text_body_stream, content_type);
217                 tny_stream_iface_reset (text_body_stream);
218         } else {
219                 text_body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new(
220                                                           camel_mime_part_new()));
221                 tny_stream_iface_reset (text_body_stream);
222                 tny_msg_mime_part_iface_construct_from_stream (text_body_part,
223                                                                text_body_stream,
224                                                                content_type);
225                 tny_stream_iface_reset (text_body_stream);
226                 tny_msg_iface_add_part(new_msg, text_body_part);
227                 //g_object_unref (G_OBJECT(text_body_part));
228         }
229         
230         for (    pos = (GList *)attachments_list;
231                  pos;
232                  pos = pos->next    ) {
233                 attachment = pos->data;
234                 attachment_filename = modest_tny_attachment_get_name(attachment);
235                 attachment_stream = modest_tny_attachment_get_stream(attachment);
236                 attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new (
237                                                                 camel_mime_part_new()));
238                 
239                 attachment_content_type = modest_tny_attachment_get_mime_type(attachment);
240                                  
241                 tny_msg_mime_part_iface_construct_from_stream (attachment_part,
242                                                                attachment_stream,
243                                                                attachment_content_type);
244                 tny_stream_iface_reset (attachment_stream);
245                 
246                 tny_msg_mime_part_iface_set_filename(attachment_part, attachment_filename);
247                 
248                 tny_msg_iface_add_part (new_msg, attachment_part);
249                 //g_object_unref(G_OBJECT(attachment_part));
250                 //close(file);
251         }
252         
253         tny_transport_account_iface_send (transport_account, new_msg);
254
255         g_object_unref (G_OBJECT(text_body_stream));
256         g_object_unref (G_OBJECT(headers));
257         g_object_unref (G_OBJECT(new_msg));
258         g_free(content_type);
259
260         return TRUE;    
261 }