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>
18 #include <camel/camel-folder.h>
19 #include <camel/camel.h>
20 #include <camel/camel-folder-summary.h>
24 #include "modest-tny-transport-actions.h"
25 /* include other impl specific header files */
27 /* 'private'/'protected' functions */
28 static void modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass);
29 static void modest_tny_transport_actions_init (ModestTnyTransportActions *obj);
30 static void modest_tny_transport_actions_finalize (GObject *obj);
31 static gboolean is_ascii (const gchar *s);
32 static char * get_content_type (const gchar *s);
41 typedef struct _ModestTnyTransportActionsPrivate ModestTnyTransportActionsPrivate;
42 struct _ModestTnyTransportActionsPrivate {
43 /* my private members go here, eg. */
44 /* gboolean frobnicate_mode; */
46 #define MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
47 MODEST_TYPE_TNY_TRANSPORT_ACTIONS, \
48 ModestTnyTransportActionsPrivate))
50 static GObjectClass *parent_class = NULL;
52 /* uncomment the following if you have defined any signals */
53 /* static guint signals[LAST_SIGNAL] = {0}; */
56 modest_tny_transport_actions_get_type (void)
58 static GType my_type = 0;
60 static const GTypeInfo my_info = {
61 sizeof(ModestTnyTransportActionsClass),
63 NULL, /* base finalize */
64 (GClassInitFunc) modest_tny_transport_actions_class_init,
65 NULL, /* class finalize */
66 NULL, /* class data */
67 sizeof(ModestTnyTransportActions),
69 (GInstanceInitFunc) modest_tny_transport_actions_init,
71 my_type = g_type_register_static (G_TYPE_OBJECT,
72 "ModestTnyTransportActions",
79 modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass)
81 GObjectClass *gobject_class;
82 gobject_class = (GObjectClass*) klass;
84 parent_class = g_type_class_peek_parent (klass);
85 gobject_class->finalize = modest_tny_transport_actions_finalize;
87 g_type_class_add_private (gobject_class, sizeof(ModestTnyTransportActionsPrivate));
89 /* signal definitions go here, e.g.: */
90 /* signals[MY_SIGNAL_1] = */
91 /* g_signal_new ("my_signal_1",....); */
92 /* signals[MY_SIGNAL_2] = */
93 /* g_signal_new ("my_signal_2",....); */
98 modest_tny_transport_actions_init (ModestTnyTransportActions *obj)
100 /* uncomment the following if you init any of the private data */
101 /* ModestTnyTransportActionsPrivate *priv = MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(obj); */
103 /* initialize this object, eg.: */
104 /* priv->frobnicate_mode = FALSE; */
108 modest_tny_transport_actions_finalize (GObject *obj)
110 /* free/unref instance resources here */
114 modest_tny_transport_actions_new (void)
116 return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
120 is_ascii(const gchar *s)
123 if (s[0] & 128 || s[0] < 32)
131 get_content_type(const gchar *s)
135 type = g_string_new("text/plain");
137 if (g_utf8_validate(s, -1, NULL)) {
138 g_string_append(type, "; charset=\"utf-8\"");
140 /* it should be impossible to reach this, but better safe than sorry */
141 g_warning("invalid utf8 in message");
142 g_string_append(type, "; charset=\"latin1\"");
145 return g_string_free(type, FALSE);
149 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
150 TnyTransportAccountIface *transport_account,
155 const gchar *subject,
158 TnyMsgIface *new_msg;
159 TnyMsgMimePartIface *body_part;
160 TnyMsgHeaderIface *headers;
161 TnyStreamIface *body_stream;
164 new_msg = TNY_MSG_IFACE(tny_msg_new ());
165 headers = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
166 body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
167 (camel_stream_mem_new_with_buffer
168 (body, strlen(body))));
169 body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new
170 (camel_mime_part_new()));
172 tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
173 tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
174 tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
175 tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
176 tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
178 content_type = get_content_type(body);
180 tny_msg_iface_set_header (new_msg, headers);
181 tny_msg_mime_part_iface_construct_from_stream (body_part, body_stream,
183 tny_msg_mime_part_iface_set_content_type (body_part, content_type);
185 tny_msg_mime_part_iface_set_content_type (
186 TNY_MSG_MIME_PART_IFACE(new_msg), content_type);
187 tny_stream_iface_reset (body_stream);
189 tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
190 body_stream, content_type);
192 tny_transport_account_iface_send (transport_account, new_msg);
194 g_object_unref (G_OBJECT(body_stream));
195 g_object_unref (G_OBJECT(body_part));
196 g_object_unref (G_OBJECT(headers));
197 g_object_unref (G_OBJECT(new_msg));
198 g_free(content_type);