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.
30 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
33 #include <tny-mime-part.h>
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);
46 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
47 struct _ModestMailOperationPrivate {
49 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50 MODEST_TYPE_MAIL_OPERATION, \
51 ModestMailOperationPrivate))
53 static GObjectClass *parent_class = NULL;
55 /* uncomment the following if you have defined any signals */
56 /* static guint signals[LAST_SIGNAL] = {0}; */
59 modest_mail_operation_get_type (void)
61 static GType my_type = 0;
63 static const GTypeInfo my_info = {
64 sizeof(ModestMailOperationClass),
66 NULL, /* base finalize */
67 (GClassInitFunc) modest_mail_operation_class_init,
68 NULL, /* class finalize */
69 NULL, /* class data */
70 sizeof(ModestMailOperation),
72 (GInstanceInitFunc) modest_mail_operation_init,
75 my_type = g_type_register_static (G_TYPE_OBJECT,
76 "ModestMailOperation",
83 modest_mail_operation_class_init (ModestMailOperationClass *klass)
85 GObjectClass *gobject_class;
86 gobject_class = (GObjectClass*) klass;
88 parent_class = g_type_class_peek_parent (klass);
89 gobject_class->finalize = modest_mail_operation_finalize;
91 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
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",....); */
102 modest_mail_operation_init (ModestMailOperation *obj)
104 /* initialize this object, eg.: */
105 /* priv->frobnicate_mode = FALSE; */
109 modest_mail_operation_finalize (GObject *obj)
111 /* free/unref instance resources here */
112 G_OBJECT_CLASS(parent_class)->finalize (obj);
116 modest_mail_operation_new (void)
118 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
123 is_ascii(const gchar *s)
126 if (s[0] & 128 || s[0] < 32)
134 get_content_type(const gchar *s)
138 type = g_string_new("text/plain");
140 if (g_utf8_validate(s, -1, NULL)) {
141 g_string_append(type, "; charset=\"utf-8\"");
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\"");
148 return g_string_free(type, FALSE);
152 modest_mail_operation_send_mail (TnyTransportAccount *transport_account,
157 const gchar *subject,
159 const GList *attachments_list)
161 ModestMailOperation *mail_operation;
164 TnyStream *text_body_stream, *attachment_stream;
165 ModestTnyAttachment *attachment;
168 const gchar *attachment_content_type;
169 const gchar *attachment_filename;
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
175 if (to == NULL || body == NULL)
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))));
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);
193 content_type = get_content_type(body);
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);
201 TnyMimePart *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,
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));
214 for (pos = (GList *)attachments_list;
217 TnyMimePart *attachment_part;
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()));
224 attachment_content_type = modest_tny_attachment_get_mime_type (attachment);
226 tny_mime_part_construct_from_stream (attachment_part,
228 attachment_content_type);
229 tny_stream_reset (attachment_stream);
231 tny_mime_part_set_filename (attachment_part, attachment_filename);
233 tny_msg_add_part (new_msg, attachment_part);
234 g_object_unref(G_OBJECT(attachment_part));
237 tny_transport_account_send (transport_account, new_msg);
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);
244 return mail_operation;