* first hackish implementation of *sending* mail
[modest] / src / modest-tny-transport-actions.c
1 /* modest-tny-transport-actions.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <tny-msg.h>
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 <string.h>
18 #include <camel/camel-folder.h>
19 #include <camel/camel.h>
20 #include <camel/camel-folder-summary.h>
21
22
23
24 #include "modest-tny-transport-actions.h"
25 /* include other impl specific header files */
26
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
32 /* list my signals */
33 enum {
34         /* MY_SIGNAL_1, */
35         /* MY_SIGNAL_2, */
36         LAST_SIGNAL
37 };
38
39 typedef struct _ModestTnyTransportActionsPrivate ModestTnyTransportActionsPrivate;
40 struct _ModestTnyTransportActionsPrivate {
41         /* my private members go here, eg. */
42         /* gboolean frobnicate_mode; */
43 };
44 #define MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
45                                                           MODEST_TYPE_TNY_TRANSPORT_ACTIONS, \
46                                                           ModestTnyTransportActionsPrivate))
47 /* globals */
48 static GObjectClass *parent_class = NULL;
49
50 /* uncomment the following if you have defined any signals */
51 /* static guint signals[LAST_SIGNAL] = {0}; */
52
53 GType
54 modest_tny_transport_actions_get_type (void)
55 {
56         static GType my_type = 0;
57         if (!my_type) {
58                 static const GTypeInfo my_info = {
59                         sizeof(ModestTnyTransportActionsClass),
60                         NULL,           /* base init */
61                         NULL,           /* base finalize */
62                         (GClassInitFunc) modest_tny_transport_actions_class_init,
63                         NULL,           /* class finalize */
64                         NULL,           /* class data */
65                         sizeof(ModestTnyTransportActions),
66                         1,              /* n_preallocs */
67                         (GInstanceInitFunc) modest_tny_transport_actions_init,
68                 };
69                 my_type = g_type_register_static (G_TYPE_OBJECT,
70                                                   "ModestTnyTransportActions",
71                                                   &my_info, 0);
72         }
73         return my_type;
74 }
75
76 static void
77 modest_tny_transport_actions_class_init (ModestTnyTransportActionsClass *klass)
78 {
79         GObjectClass *gobject_class;
80         gobject_class = (GObjectClass*) klass;
81
82         parent_class            = g_type_class_peek_parent (klass);
83         gobject_class->finalize = modest_tny_transport_actions_finalize;
84
85         g_type_class_add_private (gobject_class, sizeof(ModestTnyTransportActionsPrivate));
86
87         /* signal definitions go here, e.g.: */
88 /*      signals[MY_SIGNAL_1] = */
89 /*              g_signal_new ("my_signal_1",....); */
90 /*      signals[MY_SIGNAL_2] = */
91 /*              g_signal_new ("my_signal_2",....); */
92 /*      etc. */
93 }
94
95 static void
96 modest_tny_transport_actions_init (ModestTnyTransportActions *obj)
97 {
98 /* uncomment the following if you init any of the private data */
99 /*      ModestTnyTransportActionsPrivate *priv = MODEST_TNY_TRANSPORT_ACTIONS_GET_PRIVATE(obj); */
100
101 /*      initialize this object, eg.: */
102 /*      priv->frobnicate_mode = FALSE; */
103 }
104
105 static void
106 modest_tny_transport_actions_finalize (GObject *obj)
107 {
108 /*      free/unref instance resources here */
109 }
110
111 GObject*
112 modest_tny_transport_actions_new (void)
113 {
114         return G_OBJECT(g_object_new(MODEST_TYPE_TNY_TRANSPORT_ACTIONS, NULL));
115 }
116
117
118
119 gboolean
120 modest_tny_transport_actions_send_message (ModestTnyTransportActions *self,
121                                            TnyTransportAccountIface *transport_account,
122                                            const gchar *from,
123                                            const gchar *to,
124                                            const gchar *cc,
125                                            const gchar *bcc,
126                                            const gchar *subject,
127                                            const gchar *body)
128 {
129         TnyMsgIface *new_msg;
130         TnyMsgMimePartIface *body_part;
131         TnyMsgHeaderIface *headers;
132         TnyStreamIface *body_stream;
133
134         new_msg     = TNY_MSG_IFACE(tny_msg_new ());
135         headers     = TNY_MSG_HEADER_IFACE(tny_msg_header_new ());
136         body_stream = TNY_STREAM_IFACE (tny_stream_camel_new
137                                         (camel_stream_mem_new_with_buffer
138                                          (body, strlen(body))));
139         body_part = TNY_MSG_MIME_PART_IFACE (tny_msg_mime_part_new
140                                              (camel_mime_part_new()));
141
142         tny_msg_header_iface_set_from (TNY_MSG_HEADER_IFACE (headers), from);
143         tny_msg_header_iface_set_to (TNY_MSG_HEADER_IFACE (headers), to);
144         tny_msg_header_iface_set_cc (TNY_MSG_HEADER_IFACE (headers), cc);
145         tny_msg_header_iface_set_bcc (TNY_MSG_HEADER_IFACE (headers), bcc);
146         tny_msg_header_iface_set_subject (TNY_MSG_HEADER_IFACE (headers), subject);
147
148         tny_msg_iface_set_header (new_msg, headers);
149         tny_msg_mime_part_iface_construct_from_stream (body_part, body_stream,
150                                                        "text/plain");
151         tny_msg_mime_part_iface_set_content_type  (body_part,"text/plain");     
152         
153         tny_msg_mime_part_iface_set_content_type (
154                 TNY_MSG_MIME_PART_IFACE(new_msg), "text/plain");
155         tny_stream_iface_reset (body_stream);
156         
157         tny_msg_mime_part_iface_construct_from_stream (TNY_MSG_MIME_PART_IFACE(new_msg),
158                                                        body_stream, "text/plain");
159         
160         tny_transport_account_iface_send (transport_account, new_msg);
161
162         g_object_unref (G_OBJECT(body_stream));
163         g_object_unref (G_OBJECT(body_part));
164         g_object_unref (G_OBJECT(headers));
165         g_object_unref (G_OBJECT(new_msg));
166
167         return TRUE;    
168 }
169
170
171