* src/modest-tny-attachment.h:
[modest] / src / modest-mail-operation.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 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
32 #include <string.h>
33 #include <tny-mime-part.h>
34
35 /* 'private'/'protected' functions */
36 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
37 static void modest_mail_operation_init       (ModestMailOperation *obj);
38 static void modest_mail_operation_finalize   (GObject *obj);
39 /* list my signals  */
40 enum {
41         /* MY_SIGNAL_1, */
42         /* MY_SIGNAL_2, */
43         LAST_SIGNAL
44 };
45
46 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
47 struct _ModestMailOperationPrivate {
48 };
49 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50                                                    MODEST_TYPE_MAIL_OPERATION, \
51                                                    ModestMailOperationPrivate))
52 /* globals */
53 static GObjectClass *parent_class = NULL;
54
55 /* uncomment the following if you have defined any signals */
56 /* static guint signals[LAST_SIGNAL] = {0}; */
57
58 GType
59 modest_mail_operation_get_type (void)
60 {
61         static GType my_type = 0;
62         if (!my_type) {
63                 static const GTypeInfo my_info = {
64                         sizeof(ModestMailOperationClass),
65                         NULL,           /* base init */
66                         NULL,           /* base finalize */
67                         (GClassInitFunc) modest_mail_operation_class_init,
68                         NULL,           /* class finalize */
69                         NULL,           /* class data */
70                         sizeof(ModestMailOperation),
71                         1,              /* n_preallocs */
72                         (GInstanceInitFunc) modest_mail_operation_init,
73                         NULL
74                 };
75                 my_type = g_type_register_static (G_TYPE_OBJECT,
76                                                   "ModestMailOperation",
77                                                   &my_info, 0);
78         }
79         return my_type;
80 }
81
82 static void
83 modest_mail_operation_class_init (ModestMailOperationClass *klass)
84 {
85         GObjectClass *gobject_class;
86         gobject_class = (GObjectClass*) klass;
87
88         parent_class            = g_type_class_peek_parent (klass);
89         gobject_class->finalize = modest_mail_operation_finalize;
90
91         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
92
93         /* signal definitions go here, e.g.: */
94 /*      signals[MY_SIGNAL_1] = */
95 /*              g_signal_new ("my_signal_1",....); */
96 /*      signals[MY_SIGNAL_2] = */
97 /*              g_signal_new ("my_signal_2",....); */
98 /*      etc. */
99 }
100
101 static void
102 modest_mail_operation_init (ModestMailOperation *obj)
103 {
104 /*      initialize this object, eg.: */
105 /*      priv->frobnicate_mode = FALSE; */
106 }
107
108 static void
109 modest_mail_operation_finalize (GObject *obj)
110 {
111 /*      free/unref instance resources here */
112         G_OBJECT_CLASS(parent_class)->finalize (obj);
113 }
114
115 ModestMailOperation*
116 modest_mail_operation_new (void)
117 {
118         return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
119 }
120
121
122 static gboolean
123 is_ascii(const gchar *s)
124 {
125         while (s[0]) {
126                 if (s[0] & 128 || s[0] < 32)
127                         return FALSE;
128                 s++;
129         }
130         return TRUE;
131 }
132
133 static char *
134 get_content_type(const gchar *s)
135 {
136         GString *type;
137         
138         type = g_string_new("text/plain");
139         if (!is_ascii(s)) {
140                 if (g_utf8_validate(s, -1, NULL)) {
141                         g_string_append(type, "; charset=\"utf-8\"");
142                 } else {
143                         /* it should be impossible to reach this, but better safe than sorry */
144                         g_warning("invalid utf8 in message");
145                         g_string_append(type, "; charset=\"latin1\"");
146                 }
147         }
148         return g_string_free(type, FALSE);
149 }
150
151 ModestMailOperation*
152 modest_mail_operation_send_mail (TnyTransportAccount *transport_account,
153                                  const gchar *from,
154                                  const gchar *to,
155                                  const gchar *cc,
156                                  const gchar *bcc,
157                                  const gchar *subject,
158                                  const gchar *body,
159                                  const GList *attachments_list)
160 {
161         ModestMailOperation *mail_operation;
162         TnyMsg *new_msg;
163         TnyHeader *headers;
164         TnyStream *text_body_stream, *attachment_stream;
165         ModestTnyAttachment *attachment;
166         GList *pos;
167         gchar *content_type;
168         const gchar *attachment_content_type;
169         const gchar *attachment_filename;
170
171         /* TODO: better error handling management. Do it inside the
172            ModestMailOperation, for example set operation state to
173            failed or something like this and fill a GError with a
174            short description */
175         if (to == NULL || body == NULL)
176                 return NULL;
177
178         mail_operation   = modest_mail_operation_new ();
179         new_msg          = TNY_MSG (tny_camel_msg_new ());
180         headers          = TNY_HEADER (tny_camel_header_new ());
181         text_body_stream = TNY_STREAM (tny_camel_stream_new
182                                        (camel_stream_mem_new_with_buffer
183                                         (body, strlen(body))));
184
185         /* IMPORTANT: set the header before assign values to it */
186         tny_msg_set_header (new_msg, headers);
187         tny_header_set_from (TNY_HEADER (headers), from);
188         tny_header_set_to (TNY_HEADER (headers), to);
189         tny_header_set_cc (TNY_HEADER (headers), cc);
190         tny_header_set_bcc (TNY_HEADER (headers), bcc);
191         tny_header_set_subject (TNY_HEADER (headers), subject);
192
193         content_type = get_content_type(body);
194         
195         if (attachments_list == NULL) {
196                 tny_stream_reset (text_body_stream);
197                 tny_mime_part_construct_from_stream (TNY_MIME_PART(new_msg),
198                                                      text_body_stream, content_type);
199                 tny_stream_reset (text_body_stream);
200         } else {
201                 TnyMimePart *text_body_part;
202
203                 text_body_part = 
204                         TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
205                 tny_stream_reset (text_body_stream);
206                 tny_mime_part_construct_from_stream (text_body_part,
207                                                      text_body_stream,
208                                                      content_type);
209                 tny_stream_reset (text_body_stream);
210                 tny_msg_add_part (new_msg, text_body_part);
211                 g_object_unref (G_OBJECT(text_body_part));
212         }
213
214         for (pos = (GList *)attachments_list;
215              pos;
216              pos = pos->next) {
217                 TnyMimePart *attachment_part;
218
219                 attachment = pos->data;
220                 attachment_filename = modest_tny_attachment_get_name (attachment);
221                 attachment_stream = modest_tny_attachment_get_stream (attachment);
222                 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
223                 
224                 attachment_content_type = modest_tny_attachment_get_mime_type (attachment);
225                                  
226                 tny_mime_part_construct_from_stream (attachment_part,
227                                                      attachment_stream,
228                                                      attachment_content_type);
229                 tny_stream_reset (attachment_stream);
230                 
231                 tny_mime_part_set_filename (attachment_part, attachment_filename);
232                 
233                 tny_msg_add_part (new_msg, attachment_part);
234                 g_object_unref(G_OBJECT(attachment_part));
235         }
236         
237         tny_transport_account_send (transport_account, new_msg);
238
239         g_object_unref (G_OBJECT(text_body_stream));
240         g_object_unref (G_OBJECT(headers));
241         g_object_unref (G_OBJECT(new_msg));
242         g_free(content_type);
243
244         return mail_operation;
245 }