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.
31 /* modest-tny-transport-actions.c */
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>
47 #include <camel/camel-folder.h>
48 #include <camel/camel.h>
49 #include <camel/camel-folder-summary.h>
51 #include "modest-tny-transport-actions.h"
52 #include "modest-tny-attachment.h"
53 /* include other impl specific header files */
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);
69 typedef struct _ModestTnyTransportActionsPrivate ModestTnyTransportActionsPrivate;
70 struct _ModestTnyTransportActionsPrivate {
71 /* my private members go here, eg. */
72 /* gboolean frobnicate_mode; */
74 #define MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
75 MODEST_TYPE_TNY_TRANSPORT_ACTIONS, \
76 ModestTnyTransportActionsPrivate))
78 static GObjectClass *parent_class = NULL;
80 /* uncomment the following if you have defined any signals */
81 /* static guint signals[LAST_SIGNAL] = {0}; */
84 modest_tny_transport_actions_get_type (void)
86 static GType my_type = 0;
88 static const GTypeInfo my_info = {
89 sizeof(ModestTnyTransportActionsClass),
91 NULL, /* base finalize */
92 (GClassInitFunc) modest_tny_transport_actions_class_init,
93 NULL, /* class finalize */
94 NULL, /* class data */
95 sizeof(ModestTnyTransportActions),
97 (GInstanceInitFunc) modest_tny_transport_actions_init,
99 my_type = g_type_register_static (G_TYPE_OBJECT,
100 "ModestTnyTransportActions",
107 modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass)
109 GObjectClass *gobject_class;
110 gobject_class = (GObjectClass*) klass;
112 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->finalize = modest_tny_transport_actions_finalize;
115 g_type_class_add_private (gobject_class, sizeof(ModestTnyTransportActionsPrivate));
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",....); */
126 modest_tny_transport_actions_init (ModestTnyTransportActions *obj)
128 /* uncomment the following if you init any of the private data */
129 /* ModestTnyTransportActionsPrivate *priv = MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(obj); */
131 /* initialize this object, eg.: */
132 /* priv->frobnicate_mode = FALSE; */
136 modest_tny_transport_actions_finalize (GObject *obj)
138 /* free/unref instance resources here */
142 modest_tny_transport_actions_new (void)
144 return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
148 is_ascii(const gchar *s)
151 if (s[0] & 128 || s[0] < 32)
159 get_content_type(const gchar *s)
163 type = g_string_new("text/plain");
165 if (g_utf8_validate(s, -1, NULL)) {
166 g_string_append(type, "; charset=\"utf-8\"");
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\"");
173 return g_string_free(type, FALSE);
177 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
178 TnyTransportAccountIface *transport_account,
183 const gchar *subject,
185 const GList *attachments_list)
187 TnyMsgIface *new_msg;
188 TnyMsgMimePartIface *attachment_part, *text_body_part;
189 TnyMsgHeaderIface *headers;
190 TnyStreamIface *text_body_stream, *attachment_stream;
191 ModestTnyAttachment *attachment;
194 const gchar *attachment_content_type;
195 const gchar *attachment_filename;
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))));
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);
211 content_type = get_content_type(body);
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);
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,
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));
230 for ( pos = (GList *)attachments_list;
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()));
239 attachment_content_type = modest_tny_attachment_get_mime_type(attachment);
241 tny_msg_mime_part_iface_construct_from_stream (attachment_part,
243 attachment_content_type);
244 tny_stream_iface_reset (attachment_stream);
246 tny_msg_mime_part_iface_set_filename(attachment_part, attachment_filename);
248 tny_msg_iface_add_part (new_msg, attachment_part);
249 //g_object_unref(G_OBJECT(attachment_part));
253 tny_transport_account_iface_send (transport_account, new_msg);
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);