* src/gtk/pixmaps/internet-mail.svg:
[modest] / src / modest-mail-operation.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
32 #include <string.h>
33 #include <stdarg.h>
34 #include <tny-mime-part.h>
35 #include <tny-store-account.h>
36 #include <tny-folder-store.h>
37 #include <tny-folder-store-query.h>
38 #include <tny-camel-msg.h>
39 #include <tny-camel-header.h>
40 #include <tny-camel-stream.h>
41 #include <tny-camel-mime-part.h>
42 #include <tny-simple-list.h>
43
44 #include <glib/gi18n.h>
45
46 #include <modest-text-utils.h>
47 #include <modest-tny-msg-actions.h>
48 #include "modest-tny-platform-factory.h"
49
50 /* 'private'/'protected' functions */
51 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
52 static void modest_mail_operation_init       (ModestMailOperation *obj);
53 static void modest_mail_operation_finalize   (GObject *obj);
54
55 #define MODEST_ERROR modest_error_quark ()
56
57 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
58 enum _ModestMailOperationErrorCode {
59         MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
60         MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
61
62         MODEST_MAIL_OPERATION_NUM_ERROR_CODES
63 };
64
65 static void       set_error          (ModestMailOperation *mail_operation, 
66                                       ModestMailOperationErrorCode error_code,
67                                       const gchar *fmt, ...);
68 static void       status_update_cb   (TnyFolder *folder, 
69                                       const gchar *what, 
70                                       gint status, 
71                                       gpointer user_data);
72 static void       folder_refresh_cb  (TnyFolder *folder, 
73                                       gboolean cancelled, 
74                                       gpointer user_data);
75 static void       add_attachments    (TnyMsg *msg, 
76                                       const GList *attachments_list);
77
78
79 static TnyMimePart *         add_body_part    (TnyMsg *msg, 
80                                                const gchar *body,
81                                                const gchar *content_type, 
82                                                gboolean has_attachments);
83
84
85 /* list my signals  */
86 enum {
87         /* MY_SIGNAL_1, */
88         /* MY_SIGNAL_2, */
89         LAST_SIGNAL
90 };
91
92 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
93 struct _ModestMailOperationPrivate {
94         TnyAccount                *account;
95         ModestMailOperationStatus  status;
96         GError                    *error;
97 };
98 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99                                                    MODEST_TYPE_MAIL_OPERATION, \
100                                                    ModestMailOperationPrivate))
101
102 /* some utility functions */
103 static char * get_content_type(const gchar *s);
104 static gboolean is_ascii(const gchar *s);
105
106 /* globals */
107 static GObjectClass *parent_class = NULL;
108
109 /* uncomment the following if you have defined any signals */
110 /* static guint signals[LAST_SIGNAL] = {0}; */
111
112 GType
113 modest_mail_operation_get_type (void)
114 {
115         static GType my_type = 0;
116         if (!my_type) {
117                 static const GTypeInfo my_info = {
118                         sizeof(ModestMailOperationClass),
119                         NULL,           /* base init */
120                         NULL,           /* base finalize */
121                         (GClassInitFunc) modest_mail_operation_class_init,
122                         NULL,           /* class finalize */
123                         NULL,           /* class data */
124                         sizeof(ModestMailOperation),
125                         1,              /* n_preallocs */
126                         (GInstanceInitFunc) modest_mail_operation_init,
127                         NULL
128                 };
129                 my_type = g_type_register_static (G_TYPE_OBJECT,
130                                                   "ModestMailOperation",
131                                                   &my_info, 0);
132         }
133         return my_type;
134 }
135
136 static void
137 modest_mail_operation_class_init (ModestMailOperationClass *klass)
138 {
139         GObjectClass *gobject_class;
140         gobject_class = (GObjectClass*) klass;
141
142         parent_class            = g_type_class_peek_parent (klass);
143         gobject_class->finalize = modest_mail_operation_finalize;
144
145         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
146
147         /* signal definitions go here, e.g.: */
148 /*      signals[MY_SIGNAL_1] = */
149 /*              g_signal_new ("my_signal_1",....); */
150 /*      signals[MY_SIGNAL_2] = */
151 /*              g_signal_new ("my_signal_2",....); */
152 /*      etc. */
153 }
154
155 static void
156 modest_mail_operation_init (ModestMailOperation *obj)
157 {
158         ModestMailOperationPrivate *priv;
159
160         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
161
162         priv->account = NULL;
163         priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
164         priv->error = NULL;
165 }
166
167 static void
168 modest_mail_operation_finalize (GObject *obj)
169 {
170         ModestMailOperationPrivate *priv;
171
172         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
173
174         if (priv->account) {
175                 g_object_unref (priv->account);
176                 priv->account = NULL;
177         }
178         if (priv->error) {
179                 g_error_free (priv->error);
180                 priv->error = NULL;
181         }
182
183         G_OBJECT_CLASS(parent_class)->finalize (obj);
184 }
185
186 ModestMailOperation*
187 modest_mail_operation_new (TnyAccount *account)
188 {
189         ModestMailOperation *mail_operation;
190         ModestMailOperationPrivate *priv;
191
192         mail_operation = 
193                 MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
194         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
195
196         priv->account = g_object_ref (account);
197
198         return mail_operation;
199 }
200
201
202 void
203 modest_mail_operation_send_mail (ModestMailOperation *mail_operation,
204                                  TnyMsg* msg)
205 {
206         ModestMailOperationPrivate *priv;
207         TnyTransportAccount *transport_account;
208
209         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
210
211         if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
212                 set_error (mail_operation,
213                            MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
214                            _("Error trying to send a mail. Use a transport account"));
215         }
216
217         transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
218
219         mail_operation = modest_mail_operation_new (NULL);
220         tny_transport_account_send (transport_account, msg);
221 }
222
223 void
224 modest_mail_operation_send_new_mail (ModestMailOperation *mail_operation,
225                                      const gchar *from,
226                                      const gchar *to,
227                                      const gchar *cc,
228                                      const gchar *bcc,
229                                      const gchar *subject,
230                                      const gchar *body,
231                                      const GList *attachments_list)
232 {
233         TnyMsg *new_msg;
234         TnyHeader *header;
235         TnyTransportAccount *transport_account;
236         ModestMailOperationPrivate *priv;
237         gchar *content_type;
238
239         g_return_if_fail (mail_operation);
240
241         /* Check parametters */
242         if (to == NULL) {
243                 set_error (mail_operation,
244                            MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
245                            _("Error trying to send a mail. You need to set almost one a recipient"));
246                 return;
247         }
248
249         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
250
251         if (!TNY_IS_TRANSPORT_ACCOUNT (priv->account)) {
252                 set_error (mail_operation,
253                            MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
254                            _("Error trying to send a mail. Use a transport account"));
255                 return;
256         }
257
258         /* Create new msg */
259         transport_account = TNY_TRANSPORT_ACCOUNT (priv->account);
260
261         new_msg          = TNY_MSG (tny_camel_msg_new ());
262         header           = TNY_HEADER (tny_camel_header_new ());
263
264         /* WARNING: set the header before assign values to it */
265         tny_msg_set_header (new_msg, header);
266         tny_header_set_from (TNY_HEADER (header), from);
267         tny_header_set_to (TNY_HEADER (header), to);
268         tny_header_set_cc (TNY_HEADER (header), cc);
269         tny_header_set_bcc (TNY_HEADER (header), bcc);
270         tny_header_set_subject (TNY_HEADER (header), subject);
271
272         content_type = get_content_type(body);
273
274         /* Add the body of the new mail */      
275         add_body_part (new_msg, body, (const gchar *) content_type,
276                        (attachments_list == NULL) ? FALSE : TRUE);
277
278         /* Add attachments */
279         add_attachments (new_msg, attachments_list);
280
281         /* Send mail */ 
282         tny_transport_account_send (transport_account, new_msg);
283
284         /* Clean */
285         g_object_unref (header);
286         g_object_unref (new_msg);
287         g_free(content_type);
288 }
289
290 /**
291  * modest_mail_operation_create_forward_mail:
292  * @msg: a valid #TnyMsg instance
293  * @forward_type: the type of forwarded message
294  * 
295  * creates a forwarded message from an existing one
296  * 
297  * Returns: a new #TnyMsg, or NULL in case of error
298  **/
299 TnyMsg* 
300 modest_mail_operation_create_forward_mail (TnyMsg *msg, 
301                                            const gchar *from,
302                                            ModestMailOperationForwardType forward_type)
303 {
304         TnyMsg *new_msg;
305         TnyHeader *new_header, *header;
306         gchar *new_subject, *new_body, *content_type;
307         TnyMimePart *text_body_part = NULL;
308         GList *attachments_list;
309         TnyList *parts;
310         TnyIterator *iter;
311
312         /* Create new objects */
313         new_msg          = TNY_MSG (tny_camel_msg_new ());
314         new_header       = TNY_HEADER (tny_camel_header_new ());
315
316         header = tny_msg_get_header (msg);
317
318         /* Fill the header */
319         tny_msg_set_header (new_msg, new_header);
320         tny_header_set_from (new_header, from);
321
322         /* Change the subject */
323         new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
324                                                                    _("Fwd:"));
325         tny_header_set_subject (new_header, (const gchar *) new_subject);
326         g_free (new_subject);
327
328         /* Get body from original msg */
329         new_body = (gchar *) modest_tny_msg_actions_find_body (msg, FALSE);
330         if (!new_body) {
331                 g_object_unref (new_msg);
332                 return NULL;
333         }
334         content_type = get_content_type(new_body);
335
336         /* Create the list of attachments */
337         parts = TNY_LIST (tny_simple_list_new());
338         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
339         iter = tny_list_create_iterator (parts);
340         attachments_list = NULL;
341
342         while (!tny_iterator_is_done(iter)) {
343                 TnyMimePart *part;
344
345                 part = TNY_MIME_PART (tny_iterator_get_current (iter));
346                 if (tny_mime_part_is_attachment (part))
347                         attachments_list = g_list_prepend (attachments_list, part);
348
349                 tny_iterator_next (iter);
350         }
351
352         /* Add attachments */
353         add_attachments (new_msg, attachments_list);
354
355         switch (forward_type) {
356                 TnyMimePart *attachment_part;
357                 gchar *inlined_text;
358
359         case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
360                 /* Prepend "Original message" text */
361                 inlined_text = (gchar *) 
362                         modest_text_utils_inlined_text (tny_header_get_from (header),
363                                                         tny_header_get_date_sent (header),
364                                                         tny_header_get_to (header),
365                                                         tny_header_get_subject (header),
366                                                         (const gchar*) new_body);
367                 g_free (new_body);
368                 new_body = inlined_text;
369
370                 /* Add body part */
371                 add_body_part (new_msg, new_body, 
372                                (const gchar *) content_type, 
373                                (tny_list_get_length (parts) > 0) ? TRUE : FALSE);
374
375                 break;
376         case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
377                 attachment_part = add_body_part (new_msg, new_body, 
378                                                  (const gchar *) content_type, TRUE);
379
380                 /* Set the subject as the name of the attachment */
381                 tny_mime_part_set_filename (attachment_part, tny_header_get_subject (header));
382                 
383                 break;
384         }
385
386         /* Clean */
387         if (attachments_list) g_list_free (attachments_list);
388         g_object_unref (parts);
389         if (text_body_part) g_free (text_body_part);
390         g_free (content_type);
391         g_free (new_body);
392
393         return new_msg;
394 }
395
396 /**
397  * modest_mail_operation_create_reply_mail:
398  * @msg: a valid #TnyMsg instance
399  * @reply_type: the format of the new message
400  * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
401  * 
402  * creates a new message to reply to an existing one
403  * 
404  * Returns: Returns: a new #TnyMsg, or NULL in case of error
405  **/
406 TnyMsg* 
407 modest_mail_operation_create_reply_mail (TnyMsg *msg, 
408                                          const gchar *from,
409                                          ModestMailOperationReplyType reply_type,
410                                          ModestMailOperationReplyMode reply_mode)
411 {
412         TnyMsg *new_msg;
413         TnyHeader *new_header, *header;
414         gchar *new_subject, *new_body, *content_type, *quoted;
415         TnyMimePart *text_body_part;
416
417         /* Create new objects */
418         new_msg          = TNY_MSG (tny_camel_msg_new ());
419         new_header       = TNY_HEADER (tny_camel_header_new ());
420         header           = tny_msg_get_header (msg);
421
422         /* Fill the header */
423         tny_msg_set_header (new_msg, new_header);
424         tny_header_set_to (new_header, tny_header_get_from (header));
425         tny_header_set_from (new_header, from);
426
427         switch (reply_mode) {
428                 gchar *new_cc = NULL;
429                 const gchar *cc = NULL, *bcc = NULL;
430                 GString *tmp = NULL;
431
432         case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
433                 /* Do not fill neither cc nor bcc */
434                 break;
435         case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
436                 /* TODO */
437                 break;
438         case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
439                 /* Concatenate to, cc and bcc */
440                 cc = tny_header_get_cc (header);
441                 bcc = tny_header_get_bcc (header);
442
443                 tmp = g_string_new (tny_header_get_to (header));
444                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
445                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
446
447                 /* Remove my own address from the cc list */
448                 new_cc = (gchar *) 
449                         modest_text_utils_remove_address ((const gchar *) tmp->str, 
450                                                           (const gchar *) from);
451                 /* FIXME: remove also the mails from the new To: */
452                 tny_header_set_cc (new_header, new_cc);
453
454                 /* Clean */
455                 g_string_free (tmp, TRUE);
456                 g_free (new_cc);
457                 break;
458         }
459
460         /* Change the subject */
461         new_subject = (gchar*) modest_text_utils_derived_subject (tny_header_get_subject(header),
462                                                                   _("Re:"));
463         tny_header_set_subject (new_header, (const gchar *) new_subject);
464         g_free (new_subject);
465
466         /* Get body from original msg */
467         new_body = (gchar*) modest_tny_msg_actions_find_body (msg, FALSE);
468         if (!new_body) {
469                 g_object_unref (new_msg);
470                 return NULL;
471         }
472         content_type = get_content_type(new_body);
473
474         switch (reply_type) {
475                 gchar *cited_text;
476
477         case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
478                 /* Prepend "Original message" text */
479                 cited_text = (gchar *) modest_text_utils_cited_text (tny_header_get_from (header),
480                                                                      tny_header_get_date_sent (header),
481                                                                      (const gchar*) new_body);
482                 g_free (new_body);
483                 new_body = cited_text;
484                 break;
485         case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
486                 /* FIXME: replace 80 with a value from ModestConf */
487                 quoted = (gchar*) modest_text_utils_quote (new_body, 
488                                                            tny_header_get_from (header),
489                                                            tny_header_get_date_sent (header),
490                                                            80);
491                 g_free (new_body);
492                 new_body = quoted;
493                 break;
494         }
495         /* Add body part */
496         text_body_part = add_body_part (new_msg, new_body, 
497                                         (const gchar *) content_type, TRUE);
498
499         /* Clean */
500 /*      g_free (text_body_part); */
501         g_free (content_type);
502         g_free (new_body);
503
504         return new_msg;
505 }
506
507 void
508 modest_mail_operation_update_account (ModestMailOperation *mail_operation)
509 {
510         TnyStoreAccount *storage_account;
511         ModestMailOperationPrivate *priv;
512         TnyList *folders;
513         TnyIterator *ifolders;
514         TnyFolder *cur_folder;
515         TnyFolderStoreQuery *query;
516
517         g_return_if_fail (mail_operation);
518         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation));
519
520         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
521
522         /* Check that it is a store account */
523         if (!TNY_IS_STORE_ACCOUNT (priv->account)) {
524                 set_error (mail_operation,
525                            MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
526                            _("Error trying to update an account. Use a store account"));
527                 return;
528         }
529         storage_account = TNY_STORE_ACCOUNT (priv->account);
530
531         /* Get subscribed folders */
532         folders = TNY_LIST (tny_simple_list_new ());
533         query = tny_folder_store_query_new ();
534         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
535         tny_folder_store_get_folders (TNY_FOLDER_STORE (storage_account),
536                                       folders, query);
537         g_object_unref (query);
538         
539         ifolders = tny_list_create_iterator (folders);
540
541         /* Async refresh folders */     
542         for (tny_iterator_first (ifolders); 
543              !tny_iterator_is_done (ifolders); 
544              tny_iterator_next (ifolders)) {
545                 
546                 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
547                 tny_folder_refresh_async (cur_folder, folder_refresh_cb,
548                                           status_update_cb, mail_operation);
549         }
550         
551         g_object_unref (ifolders);
552 }
553
554 ModestMailOperationStatus
555 modest_mail_operation_get_status (ModestMailOperation *mail_operation)
556 {
557         ModestMailOperationPrivate *priv;
558
559 /*      g_return_val_if_fail (mail_operation, MODEST_MAIL_OPERATION_STATUS_INVALID); */
560 /*      g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation),  */
561 /*                            MODEST_MAIL_OPERATION_STATUS_INVALID); */
562
563         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
564         return priv->status;
565 }
566
567 const GError *
568 modest_mail_operation_get_error (ModestMailOperation *mail_operation)
569 {
570         ModestMailOperationPrivate *priv;
571
572 /*      g_return_val_if_fail (mail_operation, NULL); */
573 /*      g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_operation), NULL); */
574
575         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_operation);
576         return priv->error;
577 }
578
579 void 
580 modest_mail_operation_cancel (ModestMailOperation *mail_operation)
581 {
582         /* TODO */
583 }
584
585 static gboolean
586 is_ascii(const gchar *s)
587 {
588         while (s[0]) {
589                 if (s[0] & 128 || s[0] < 32)
590                         return FALSE;
591                 s++;
592         }
593         return TRUE;
594 }
595
596 static char *
597 get_content_type(const gchar *s)
598 {
599         GString *type;
600         
601         type = g_string_new("text/plain");
602         if (!is_ascii(s)) {
603                 if (g_utf8_validate(s, -1, NULL)) {
604                         g_string_append(type, "; charset=\"utf-8\"");
605                 } else {
606                         /* it should be impossible to reach this, but better safe than sorry */
607                         g_warning("invalid utf8 in message");
608                         g_string_append(type, "; charset=\"latin1\"");
609                 }
610         }
611         return g_string_free(type, FALSE);
612 }
613
614 static GQuark 
615 modest_error_quark (void)
616 {
617         static GQuark err_q = 0;
618         
619         if (err_q == 0)
620                 err_q = g_quark_from_static_string ("modest-error-quark");
621         
622         return err_q;
623 }
624
625
626 static void 
627 set_error (ModestMailOperation *mail_operation, 
628            ModestMailOperationErrorCode error_code,
629            const gchar *fmt, ...)
630 {
631         ModestMailOperationPrivate *priv;
632         GError* error;
633         va_list args;
634         gchar* orig;
635
636         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
637
638         va_start (args, fmt);
639
640         orig = g_strdup_vprintf(fmt, args);
641         error = g_error_new (MODEST_ERROR, error_code, orig);
642
643         va_end (args);
644
645         if (priv->error)
646                 g_object_unref (priv->error);
647
648         priv->error = error;
649         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
650 }
651
652 static void
653 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gpointer user_data) 
654 {
655         /* TODO: update main window progress bar */
656 }
657
658 static void
659 folder_refresh_cb (TnyFolder *folder, gboolean cancelled, gpointer user_data) 
660 {
661         if (cancelled) {
662                 ModestMailOperation *mail_operation;
663                 ModestMailOperationPrivate *priv;
664
665                 mail_operation = MODEST_MAIL_OPERATION (user_data);
666                 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_operation);
667
668                 priv->status =  MODEST_MAIL_OPERATION_STATUS_CANCELLED;
669         }
670 }
671
672 static void
673 add_attachments (TnyMsg *msg, const GList *attachments_list)
674 {
675         GList *pos;
676         TnyMimePart *attachment_part, *old_attachment;
677         const gchar *attachment_content_type;
678         const gchar *attachment_filename;
679         TnyStream *attachment_stream;
680
681         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
682
683                 old_attachment = pos->data;
684                 attachment_filename = tny_mime_part_get_filename (old_attachment);
685                 attachment_stream = tny_mime_part_get_stream (old_attachment);
686                 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
687                 
688                 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
689                                  
690                 tny_mime_part_construct_from_stream (attachment_part,
691                                                      attachment_stream,
692                                                      attachment_content_type);
693                 tny_stream_reset (attachment_stream);
694                 
695                 tny_mime_part_set_filename (attachment_part, attachment_filename);
696                 
697                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
698 /*              g_object_unref (attachment_part); */
699         }
700 }
701
702
703 static TnyMimePart *
704 add_body_part (TnyMsg *msg, 
705                const gchar *body,
706                const gchar *content_type,
707                gboolean has_attachments)
708 {
709         TnyMimePart *text_body_part = NULL;
710         TnyStream *text_body_stream;
711
712         /* Create the stream */
713         text_body_stream = TNY_STREAM (tny_camel_stream_new
714                                        (camel_stream_mem_new_with_buffer
715                                         (body, strlen(body))));
716
717         /* Create body part if needed */
718         if (has_attachments)
719                 text_body_part = 
720                         TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
721         else
722                 text_body_part = TNY_MIME_PART(msg);
723
724         /* Construct MIME part */
725         tny_stream_reset (text_body_stream);
726         tny_mime_part_construct_from_stream (text_body_part,
727                                              text_body_stream,
728                                              content_type);
729         tny_stream_reset (text_body_stream);
730
731         /* Add part if needed */
732         if (has_attachments) {
733                 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
734                 g_object_unref (G_OBJECT(text_body_part));
735         }
736
737         /* Clean */
738         g_object_unref (text_body_stream);
739
740         return text_body_part;
741 }