1 /* modest-tny-transport-actions.c */
3 /* insert (c)/licensing information) */
6 #include <tny-msg-iface.h>
7 #include <tny-msg-mime-part.h>
8 #include <tny-msg-mime-part-iface.h>
9 #include <tny-stream-iface.h>
10 #include <tny-msg-header.h>
11 #include <tny-msg-header-iface.h>
12 #include <tny-account-iface.h>
13 #include <tny-account-store-iface.h>
14 #include <tny-transport-account-iface.h>
15 #include <tny-transport-account.h>
16 #include <tny-stream-camel.h>
17 #include <tny-fs-stream.h>
19 #include <camel/camel-folder.h>
20 #include <camel/camel.h>
21 #include <camel/camel-folder-summary.h>
25 #include "modest-tny-transport-actions.h"
26 /* include other impl specific header files */
28 /* 'private'/'protected' functions */
29 static void modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass);
30 static void modest_tny_transport_actions_init (ModestTnyTransportActions *obj);
31 static void modest_tny_transport_actions_finalize (GObject *obj);
32 static gboolean is_ascii (const gchar *s);
33 static char * get_content_type (const gchar *s);
42 typedef struct _ModestTnyTransportActionsPrivate ModestTnyTransportActionsPrivate;
43 struct _ModestTnyTransportActionsPrivate {
44 /* my private members go here, eg. */
45 /* gboolean frobnicate_mode; */
47 #define MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
48 MODEST_TYPE_TNY_TRANSPORT_ACTIONS, \
49 ModestTnyTransportActionsPrivate))
51 static GObjectClass *parent_class = NULL;
53 /* uncomment the following if you have defined any signals */
54 /* static guint signals[LAST_SIGNAL] = {0}; */
57 modest_tny_transport_actions_get_type (void)
59 static GType my_type = 0;
61 static const GTypeInfo my_info = {
62 sizeof(ModestTnyTransportActionsClass),
64 NULL, /* base finalize */
65 (GClassInitFunc) modest_tny_transport_actions_class_init,
66 NULL, /* class finalize */
67 NULL, /* class data */
68 sizeof(ModestTnyTransportActions),
70 (GInstanceInitFunc) modest_tny_transport_actions_init,
72 my_type = g_type_register_static (G_TYPE_OBJECT,
73 "ModestTnyTransportActions",
80 modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass)
82 GObjectClass *gobject_class;
83 gobject_class = (GObjectClass*) klass;
85 parent_class = g_type_class_peek_parent (klass);
86 gobject_class->finalize = modest_tny_transport_actions_finalize;
88 g_type_class_add_private (gobject_class, sizeof(ModestTnyTransportActionsPrivate));
90 /* signal definitions go here, e.g.: */
91 /* signals[MY_SIGNAL_1] = */
92 /* g_signal_new ("my_signal_1",....); */
93 /* signals[MY_SIGNAL_2] = */
94 /* g_signal_new ("my_signal_2",....); */
99 modest_tny_transport_actions_init (ModestTnyTransportActions *obj)
101 /* uncomment the following if you init any of the private data */
102 /* ModestTnyTransportActionsPrivate *priv = MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(obj); */
104 /* initialize this object, eg.: */
105 /* priv->frobnicate_mode = FALSE; */
109 modest_tny_transport_actions_finalize (GObject *obj)
111 /* free/unref instance resources here */
115 modest_tny_transport_actions_new (void)
117 return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
121 is_ascii(const gchar *s)
124 if (s[0] & 128 || s[0] < 32)
132 get_content_type(const gchar *s)
136 type = g_string_new("text/plain");
138 if (g_utf8_validate(s, -1, NULL)) {
139 g_string_append(type, "; charset=\"utf-8\"");
141 /* it should be impossible to reach this, but better safe than sorry */
142 g_warning("invalid utf8 in message");
143 g_string_append(type, "; charset=\"latin1\"");
146 return g_string_free(type, FALSE);
150 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
151 TnyTransportAccountIface *transport_account,
156 const gchar *subject,
158 const GList *attachments_list)
160 TnyMsgIface *new_msg;
161 TnyMsgMimePartIface *text_body_part, *attachment_part;
162 TnyMsgHeaderIface *headers;
163 TnyStreamIface *text_body_stream, *attachment_stream;
165 gchar *content_type, *attachment_content_type;
166 gchar *filename, *attachment_filename;
169 new_msg = TNY_MSG_IFACE(tny_msg_new ());
170 headers = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
171 text_body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
172 (camel_stream_mem_new_with_buffer
173 (body, strlen(body))));
174 text_body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new
175 (camel_mime_part_new()));
177 tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
178 tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
179 tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
180 tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
181 tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
182 tny_msg_iface_set_header (new_msg, headers);
184 content_type = get_content_type(body);
185 tny_msg_mime_part_iface_construct_from_stream (text_body_part, text_body_stream, content_type);
186 tny_stream_iface_reset (text_body_stream);
188 tny_msg_iface_add_part(new_msg, text_body_part);
190 for (attachment = (GList *)attachments_list; attachment; attachment = attachment->next) {
191 filename = attachment->data;
192 attachment_filename = g_path_get_basename(filename);
193 file = open(filename, O_RDONLY);
194 attachment_stream = TNY_STREAM_IFACE(tny_stream_camel_new(
195 camel_stream_fs_new_with_fd(file)));
197 attachment_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new (
198 camel_mime_part_new()));
200 attachment_content_type = "image/jpeg"; /* later... */
201 tny_msg_mime_part_iface_construct_from_stream (attachment_part,
203 attachment_content_type);
204 tny_stream_iface_reset (attachment_stream);
206 tny_msg_mime_part_iface_set_filename(attachment_part, attachment_filename);
208 tny_msg_iface_add_part (new_msg, attachment_part);
209 g_object_unref(G_OBJECT(attachment_stream));
210 //g_object_unref(G_OBJECT(attachment_part));
211 g_free(attachment_filename);
215 tny_transport_account_iface_send (transport_account, new_msg);
217 g_object_unref (G_OBJECT(text_body_stream));
218 //g_object_unref (G_OBJECT(text_body_part));
219 g_object_unref (G_OBJECT(headers));
220 g_object_unref (G_OBJECT(new_msg));
221 g_free(content_type);