* modest-mail-operation.c, modest-folder-view.c:
[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-stream.h>
39 #include <tny-simple-list.h>
40 #include <camel/camel-stream-mem.h>
41 #include <glib/gi18n.h>
42
43 #include "modest-text-utils.h"
44 #include "modest-tny-msg-actions.h"
45 #include "modest-tny-platform-factory.h"
46 #include "modest-marshal.h"
47 #include "modest-formatter.h"
48 #include "modest-error.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 static void     status_update_cb     (TnyFolder *folder, 
56                                       const gchar *what, 
57                                       gint status, 
58                                       gint oftotal,
59                                       gpointer user_data);
60 static void     folder_refresh_cb    (TnyFolder *folder, 
61                                       gboolean canceled,
62                                       GError **err,
63                                       gpointer user_data);
64 static void     update_folders_cb    (TnyFolderStore *self, 
65                                       TnyList *list, 
66                                       GError **err, 
67                                       gpointer user_data);
68 static void     add_attachments      (TnyMsg *msg, 
69                                       GList *attachments_list);
70
71
72 static TnyMimePart *         add_body_part    (TnyMsg *msg, 
73                                                const gchar *body,
74                                                const gchar *content_type, 
75                                                gboolean has_attachments);
76
77
78 static void        modest_mail_operation_xfer_folder       (ModestMailOperation *self,
79                                                             TnyFolder *folder,
80                                                             TnyFolderStore *parent,
81                                                             gboolean delete_original);
82
83 static gboolean    modest_mail_operation_xfer_msg          (ModestMailOperation *self,
84                                                             TnyHeader *header, 
85                                                             TnyFolder *folder, 
86                                                             gboolean delete_original);
87
88 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *self,
89                                                             TnyStoreAccount *store_account);
90
91
92 enum _ModestMailOperationSignals 
93 {
94         PROGRESS_CHANGED_SIGNAL,
95
96         NUM_SIGNALS
97 };
98
99 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
100 struct _ModestMailOperationPrivate {
101         guint                      done;
102         guint                      total;
103         ModestMailOperationStatus  status;
104         GError                    *error;
105 };
106 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107                                                    MODEST_TYPE_MAIL_OPERATION, \
108                                                    ModestMailOperationPrivate))
109
110 typedef struct _RefreshFolderAsyncHelper
111 {
112         ModestMailOperation *mail_op;
113         TnyIterator *iter;
114         guint failed;
115         guint canceled;
116
117 } RefreshFolderAsyncHelper;
118
119 /* some utility functions */
120 static char * get_content_type(const gchar *s);
121 static gboolean is_ascii(const gchar *s);
122
123 /* globals */
124 static GObjectClass *parent_class = NULL;
125
126 static guint signals[NUM_SIGNALS] = {0};
127
128 GType
129 modest_mail_operation_get_type (void)
130 {
131         static GType my_type = 0;
132         if (!my_type) {
133                 static const GTypeInfo my_info = {
134                         sizeof(ModestMailOperationClass),
135                         NULL,           /* base init */
136                         NULL,           /* base finalize */
137                         (GClassInitFunc) modest_mail_operation_class_init,
138                         NULL,           /* class finalize */
139                         NULL,           /* class data */
140                         sizeof(ModestMailOperation),
141                         1,              /* n_preallocs */
142                         (GInstanceInitFunc) modest_mail_operation_init,
143                         NULL
144                 };
145                 my_type = g_type_register_static (G_TYPE_OBJECT,
146                                                   "ModestMailOperation",
147                                                   &my_info, 0);
148         }
149         return my_type;
150 }
151
152 static void
153 modest_mail_operation_class_init (ModestMailOperationClass *klass)
154 {
155         GObjectClass *gobject_class;
156         gobject_class = (GObjectClass*) klass;
157
158         parent_class            = g_type_class_peek_parent (klass);
159         gobject_class->finalize = modest_mail_operation_finalize;
160
161         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
162
163         /**
164          * ModestMailOperation::progress-changed
165          * @self: the #MailOperation that emits the signal
166          * @user_data: user data set when the signal handler was connected
167          *
168          * Emitted when the progress of a mail operation changes
169          */
170         signals[PROGRESS_CHANGED_SIGNAL] = 
171                 g_signal_new ("progress_changed",
172                               G_TYPE_FROM_CLASS (gobject_class),
173                               G_SIGNAL_RUN_FIRST,
174                               G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
175                               NULL, NULL,
176                               g_cclosure_marshal_VOID__VOID,
177                               G_TYPE_NONE, 0);
178 }
179
180 static void
181 modest_mail_operation_init (ModestMailOperation *obj)
182 {
183         ModestMailOperationPrivate *priv;
184
185         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
186
187         priv->status   = MODEST_MAIL_OPERATION_STATUS_INVALID;
188         priv->error    = NULL;
189         priv->done     = 0;
190         priv->total    = 0;
191 }
192
193 static void
194 modest_mail_operation_finalize (GObject *obj)
195 {
196         ModestMailOperationPrivate *priv;
197
198         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
199
200         if (priv->error) {
201                 g_error_free (priv->error);
202                 priv->error = NULL;
203         }
204
205         G_OBJECT_CLASS(parent_class)->finalize (obj);
206 }
207
208 ModestMailOperation*
209 modest_mail_operation_new (void)
210 {
211         return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
212 }
213
214
215 void
216 modest_mail_operation_send_mail (ModestMailOperation *self,
217                                  TnyTransportAccount *transport_account,
218                                  TnyMsg* msg)
219 {
220         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
221         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
222
223         tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
224 }
225
226 void
227 modest_mail_operation_send_new_mail (ModestMailOperation *self,
228                                      TnyTransportAccount *transport_account,
229                                      const gchar *from,
230                                      const gchar *to,
231                                      const gchar *cc,
232                                      const gchar *bcc,
233                                      const gchar *subject,
234                                      const gchar *body,
235                                      const GList *attachments_list)
236 {
237         TnyPlatformFactory *fact;
238         TnyMsg *new_msg;
239         TnyHeader *header;
240         gchar *content_type;
241         ModestMailOperationPrivate *priv = NULL;
242
243         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
244         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
245
246         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
247
248         /* Check parametters */
249         if (to == NULL) {
250                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
251                              MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
252                              _("Error trying to send a mail. You need to set almost one a recipient"));
253                 return;
254         }
255
256         /* Create new msg */
257         fact    = modest_tny_platform_factory_get_instance ();
258         new_msg = tny_platform_factory_new_msg (fact);
259         header  = tny_platform_factory_new_header (fact);
260
261         /* WARNING: set the header before assign values to it */
262         tny_msg_set_header (new_msg, header);
263         tny_header_set_from (TNY_HEADER (header), from);
264         tny_header_set_replyto (TNY_HEADER (header), from);
265         tny_header_set_to (TNY_HEADER (header), to);
266         tny_header_set_cc (TNY_HEADER (header), cc);
267         tny_header_set_bcc (TNY_HEADER (header), bcc);
268         tny_header_set_subject (TNY_HEADER (header), subject);
269
270         content_type = get_content_type(body);
271
272         /* Add the body of the new mail */      
273         add_body_part (new_msg, body, (const gchar *) content_type,
274                        (attachments_list == NULL) ? FALSE : TRUE);
275
276         /* Add attachments */
277         add_attachments (new_msg, (GList*) attachments_list);
278
279         /* Send mail */
280         tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
281
282         /* Clean */
283         g_object_unref (header);
284         g_object_unref (new_msg);
285         g_free(content_type);
286 }
287
288 static void
289 add_if_attachment (gpointer data, gpointer user_data)
290 {
291         TnyMimePart *part;
292         GList *attachments_list;
293
294         part = TNY_MIME_PART (data);
295         attachments_list = (GList *) user_data;
296
297         if (tny_mime_part_is_attachment (part))
298                 attachments_list = g_list_prepend (attachments_list, part);
299 }
300
301
302 static TnyMsg *
303 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
304 {
305         TnyPlatformFactory *fact;
306         TnyMsg *new_msg;
307         TnyHeader *new_header, *header;
308         gchar *new_subject;
309         TnyMimePart *body;
310         ModestFormatter *formatter;
311
312         /* Get body from original msg. Always look for the text/plain
313            part of the message to create the reply/forwarded mail */
314         header = tny_msg_get_header (msg);
315         body   = modest_tny_msg_actions_find_body_part (msg, FALSE);
316
317         /* TODO: select the formatter from account prefs */
318         formatter = modest_formatter_new ("text/plain");
319
320         /* Format message body */
321         if (is_reply) {
322                 switch (type) {
323                 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
324                 default:
325                         new_msg = modest_formatter_cite  (formatter, body, header);
326                         break;
327                 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
328                         new_msg = modest_formatter_quote (formatter, body, header);
329                         break;
330                 }
331         } else {
332                 switch (type) {
333                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
334                 default:
335                         new_msg = modest_formatter_inline  (formatter, body, header);
336                         break;
337                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
338                         new_msg = modest_formatter_attach (formatter, body, header);
339                         break;
340                 }
341         }
342         g_object_unref (G_OBJECT (formatter));
343
344         /* Fill the header */
345         fact = modest_tny_platform_factory_get_instance ();
346         new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
347         tny_msg_set_header (new_msg, new_header);
348         tny_header_set_from (new_header, from);
349         tny_header_set_replyto (new_header, from);
350
351         /* Change the subject */
352         new_subject =
353                 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
354                                                              (is_reply) ? _("Re:") : _("Fwd:"));
355         tny_header_set_subject (new_header, (const gchar *) new_subject);
356         g_free (new_subject);
357
358         /* Clean */
359         g_object_unref (G_OBJECT (new_header));
360         g_object_unref (G_OBJECT (header));
361
362         return new_msg;
363 }
364
365 TnyMsg* 
366 modest_mail_operation_create_forward_mail (TnyMsg *msg, 
367                                            const gchar *from,
368                                            ModestMailOperationForwardType forward_type)
369 {
370         TnyMsg *new_msg;
371         TnyList *parts = NULL;
372         GList *attachments_list = NULL;
373
374         new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
375
376         /* Add attachments */
377         parts = TNY_LIST (tny_simple_list_new());
378         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
379         tny_list_foreach (parts, add_if_attachment, attachments_list);
380         add_attachments (new_msg, attachments_list);
381
382         /* Clean */
383         if (attachments_list) g_list_free (attachments_list);
384         g_object_unref (G_OBJECT (parts));
385
386         return new_msg;
387 }
388
389 TnyMsg* 
390 modest_mail_operation_create_reply_mail (TnyMsg *msg, 
391                                          const gchar *from,
392                                          ModestMailOperationReplyType reply_type,
393                                          ModestMailOperationReplyMode reply_mode)
394 {
395         TnyMsg *new_msg = NULL;
396         TnyHeader *new_header, *header;
397         const gchar* reply_to;
398         gchar *new_cc = NULL;
399         const gchar *cc = NULL, *bcc = NULL;
400         GString *tmp = NULL;
401
402         new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
403
404         /* Fill the header */
405         header = tny_msg_get_header (msg);
406         new_header = tny_msg_get_header (new_msg);
407         reply_to = tny_header_get_replyto (header);
408
409         /* TODO: tinymail returns Invalid and it should return NULL */
410         if (strcmp (reply_to, "Invalid"))
411                 tny_header_set_to (new_header, reply_to);
412         else
413                 tny_header_set_to (new_header, tny_header_get_from (header));
414
415         switch (reply_mode) {
416         case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
417                 /* Do not fill neither cc nor bcc */
418                 break;
419         case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
420                 /* TODO */
421                 break;
422         case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
423                 /* Concatenate to, cc and bcc */
424                 cc = tny_header_get_cc (header);
425                 bcc = tny_header_get_bcc (header);
426
427                 tmp = g_string_new (tny_header_get_to (header));
428                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
429                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
430
431                /* Remove my own address from the cc list. TODO:
432                   remove also the To: of the new message, needed due
433                   to the new reply_to feature */
434                 new_cc = (gchar *)
435                         modest_text_utils_remove_address ((const gchar *) tmp->str,
436                                                           from);
437                 /* FIXME: remove also the mails from the new To: */
438                 tny_header_set_cc (new_header, new_cc);
439
440                 /* Clean */
441                 g_string_free (tmp, TRUE);
442                 g_free (new_cc);
443                 break;
444         }
445
446         /* Clean */
447         g_object_unref (G_OBJECT (new_header));
448         g_object_unref (G_OBJECT (header));
449
450         return new_msg;
451 }
452
453 static void
454 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data) 
455 {
456         g_print ("%s status: %d, of total %d\n", what, status, oftotal);
457 }
458
459 static void
460 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
461 {
462         ModestMailOperation *self = NULL;
463         ModestMailOperationPrivate *priv = NULL;
464         RefreshFolderAsyncHelper *helper;
465
466         helper = (RefreshFolderAsyncHelper *) user_data;
467         self = MODEST_MAIL_OPERATION (helper->mail_op);
468         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
469
470         if ((canceled && *err) || *err) {
471                 priv->error = g_error_copy (*err);
472                 helper->failed++;
473         } else if (canceled) {
474                 helper->canceled++;
475                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
476                              MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
477                              _("Error trying to refresh folder %s. Operation canceled"),
478                              tny_folder_get_name (folder));
479         } else {
480                 priv->done++;
481         }
482
483         if (priv->done == priv->total)
484                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
485         else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
486                 if (helper->failed == priv->total)
487                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
488                 else if (helper->failed == priv->total)
489                         priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
490                 else
491                         priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
492         }
493         tny_iterator_next (helper->iter);
494         if (tny_iterator_is_done (helper->iter)) {
495                 TnyList *list;
496                 list = tny_iterator_get_list (helper->iter);
497                 g_object_unref (G_OBJECT (helper->iter));
498                 g_object_unref (G_OBJECT (list));
499                 g_slice_free (RefreshFolderAsyncHelper, helper);
500         } else {
501                 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
502                                           folder_refresh_cb,
503                                           status_update_cb, 
504                                           helper);
505         }
506         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
507 }
508
509
510 static void
511 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
512 {
513         ModestMailOperation *self;
514         ModestMailOperationPrivate *priv;
515         RefreshFolderAsyncHelper *helper;
516
517         self  = MODEST_MAIL_OPERATION (user_data);
518         priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
519
520         if (*err) {
521                 priv->error = g_error_copy (*err);
522                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
523                 return;
524         }
525
526         priv->total = tny_list_get_length (list);
527         priv->done = 0;
528         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
529
530         helper = g_slice_new0 (RefreshFolderAsyncHelper);
531         helper->mail_op = self;
532         helper->iter = tny_list_create_iterator (list);
533         helper->failed = 0;
534         helper->canceled = 0;
535
536         /* Async refresh folders */
537         tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
538                                   folder_refresh_cb,
539                                   status_update_cb, 
540                                   helper);
541 }
542
543 gboolean
544 modest_mail_operation_update_account (ModestMailOperation *self,
545                                       TnyStoreAccount *store_account)
546 {
547         ModestMailOperationPrivate *priv;
548         TnyList *folders;
549         TnyFolderStoreQuery *query;
550
551         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
552         g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
553
554         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
555
556         /* Get subscribed folders & refresh them */
557         folders = TNY_LIST (tny_simple_list_new ());
558         query = tny_folder_store_query_new ();
559         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
560         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
561                                             folders, update_folders_cb, query, self);
562         g_object_unref (query);
563
564         return TRUE;
565 }
566
567 ModestMailOperationStatus
568 modest_mail_operation_get_status (ModestMailOperation *self)
569 {
570         ModestMailOperationPrivate *priv;
571
572         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
573         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
574                               MODEST_MAIL_OPERATION_STATUS_INVALID);
575
576         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
577         return priv->status;
578 }
579
580 const GError *
581 modest_mail_operation_get_error (ModestMailOperation *self)
582 {
583         ModestMailOperationPrivate *priv;
584
585         g_return_val_if_fail (self, NULL);
586         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
587
588         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
589         return priv->error;
590 }
591
592 gboolean 
593 modest_mail_operation_cancel (ModestMailOperation *self)
594 {
595         /* TODO */
596         return TRUE;
597 }
598
599 guint 
600 modest_mail_operation_get_task_done (ModestMailOperation *self)
601 {
602         ModestMailOperationPrivate *priv;
603
604         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
605
606         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
607         return priv->done;
608 }
609
610 guint 
611 modest_mail_operation_get_task_total (ModestMailOperation *self)
612 {
613         ModestMailOperationPrivate *priv;
614
615         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
616
617         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
618         return priv->total;
619 }
620
621 gboolean
622 modest_mail_operation_is_finished (ModestMailOperation *self)
623 {
624         ModestMailOperationPrivate *priv;
625         gboolean retval = FALSE;
626
627         if (!MODEST_IS_MAIL_OPERATION (self)) {
628                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
629                 return retval;
630         }
631
632         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
633
634         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
635             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
636             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
637             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
638                 retval = TRUE;
639         } else {
640                 retval = FALSE;
641         }
642
643         return retval;
644 }
645
646 /* ******************************************************************* */
647 /* ************************** STORE  ACTIONS ************************* */
648 /* ******************************************************************* */
649
650
651 TnyFolder *
652 modest_mail_operation_create_folder (ModestMailOperation *self,
653                                      TnyFolderStore *parent,
654                                      const gchar *name)
655 {
656         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
657         g_return_val_if_fail (name, NULL);
658
659         TnyFolder *new_folder = NULL;
660         TnyStoreAccount *store_account;
661
662         /* Create the folder */
663         new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
664         if (!new_folder) 
665                 return NULL;
666
667         /* Subscribe to folder */
668         if (!tny_folder_is_subscribed (new_folder)) {
669                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
670                 tny_store_account_subscribe (store_account, new_folder);
671                 g_object_unref (G_OBJECT (store_account));
672         }
673
674         return new_folder;
675 }
676
677 void
678 modest_mail_operation_remove_folder (ModestMailOperation *self,
679                                      TnyFolder *folder,
680                                      gboolean remove_to_trash)
681 {
682         TnyFolderStore *folder_store;
683
684         g_return_if_fail (TNY_IS_FOLDER (folder));
685
686         /* Get folder store */
687         folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
688
689         /* Delete folder or move to trash */
690         if (remove_to_trash) {
691                 TnyFolder *trash_folder;
692
693                 trash_folder = modest_mail_operation_find_trash_folder (self,
694                                                                         TNY_STORE_ACCOUNT (folder_store));
695
696                 /* TODO: error_handling */
697                 modest_mail_operation_move_folder (self, 
698                                                    folder, 
699                                                    TNY_FOLDER_STORE (trash_folder));
700         } else {
701                 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
702                 g_object_unref (G_OBJECT (folder));
703         }
704
705         /* Free instances */
706         g_object_unref (G_OBJECT (folder_store));
707 }
708
709 void
710 modest_mail_operation_rename_folder (ModestMailOperation *self,
711                                      TnyFolder *folder,
712                                      const gchar *name)
713 {
714         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
715         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
716         g_return_if_fail (name);
717
718         /* FIXME: better error handling */
719         if (strrchr (name, '/') != NULL)
720                 return;
721
722         /* Rename. Camel handles folder subscription/unsubscription */
723         tny_folder_set_name (folder, name, NULL); /* FIXME */
724  }
725
726 void
727 modest_mail_operation_move_folder (ModestMailOperation *self,
728                                    TnyFolder *folder,
729                                    TnyFolderStore *parent)
730 {
731         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
732         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
733         g_return_if_fail (TNY_IS_FOLDER (folder));
734         
735         modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
736 }
737
738 void
739 modest_mail_operation_copy_folder (ModestMailOperation *self,
740                                    TnyFolder *folder,
741                                    TnyFolderStore *parent)
742 {
743         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
744         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
745         g_return_if_fail (TNY_IS_FOLDER (folder));
746
747         modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
748 }
749
750 static void
751 modest_mail_operation_xfer_folder (ModestMailOperation *self,
752                                    TnyFolder *folder,
753                                    TnyFolderStore *parent,
754                                    gboolean delete_original)
755 {
756         const gchar *folder_name;
757         TnyFolder *dest_folder, *child;
758         TnyIterator *iter;
759         TnyList *folders, *headers;
760
761         g_return_if_fail (TNY_IS_FOLDER (folder));
762         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
763
764         /* Create the destination folder */
765         folder_name = tny_folder_get_name (folder);
766         dest_folder = modest_mail_operation_create_folder (self, 
767                                                            parent, folder_name);
768
769         /* Transfer messages */
770         headers = TNY_LIST (tny_simple_list_new ());
771         tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
772         tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
773
774         /* Recurse children */
775         folders = TNY_LIST (tny_simple_list_new ());
776         tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
777         iter = tny_list_create_iterator (folders);
778
779         while (!tny_iterator_is_done (iter)) {
780
781                 child = TNY_FOLDER (tny_iterator_get_current (iter));
782                 modest_mail_operation_xfer_folder (self, child,
783                                                    TNY_FOLDER_STORE (dest_folder),
784                                                    delete_original);
785                 tny_iterator_next (iter);
786         }
787
788         /* Delete source folder (if needed) */
789         if (delete_original)
790                 modest_mail_operation_remove_folder (self, folder, FALSE);
791
792         /* Clean up */
793         g_object_unref (G_OBJECT (dest_folder));
794         g_object_unref (G_OBJECT (headers));
795         g_object_unref (G_OBJECT (folders));
796         g_object_unref (G_OBJECT (iter));
797 }
798
799
800 /* FIXME: this method should be rewritten when the policy for the
801    Trash folder becomes clearer */
802 static TnyFolder *
803 modest_mail_operation_find_trash_folder (ModestMailOperation *self,
804                                          TnyStoreAccount *store_account)
805 {
806         TnyList *folders;
807         TnyIterator *iter;
808         gboolean found;
809         /*TnyFolderStoreQuery *query;*/
810         TnyFolder *trash_folder = NULL;
811
812         /* Look for Trash folder */
813         folders = TNY_LIST (tny_simple_list_new ());
814         tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
815                                       folders, NULL, NULL); /* FIXME */
816         iter = tny_list_create_iterator (folders);
817
818         found = FALSE;
819         while (!tny_iterator_is_done (iter) && !found) {
820
821                 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
822                 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
823                         found = TRUE;
824                 else
825                         tny_iterator_next (iter);
826         }
827
828         /* Clean up */
829         g_object_unref (G_OBJECT (folders));
830         g_object_unref (G_OBJECT (iter));
831
832         /* TODO: better error handling management */
833         if (!found) 
834                 return NULL;
835         else
836                 return trash_folder;
837 }
838
839 /* ******************************************************************* */
840 /* **************************  MSG  ACTIONS  ************************* */
841 /* ******************************************************************* */
842
843 gboolean 
844 modest_mail_operation_copy_msg (ModestMailOperation *self,
845                                 TnyHeader *header, 
846                                 TnyFolder *folder)
847 {
848         g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
849         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
850
851         return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
852 }
853
854 gboolean 
855 modest_mail_operation_move_msg (ModestMailOperation *self,
856                                 TnyHeader *header, 
857                                 TnyFolder *folder)
858 {
859         g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
860         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
861
862         return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
863 }
864
865 void 
866 modest_mail_operation_remove_msg (ModestMailOperation *self,
867                                   TnyHeader *header,
868                                   gboolean remove_to_trash)
869 {
870         TnyFolder *folder;
871
872         g_return_if_fail (TNY_IS_HEADER (header));
873
874         folder = tny_header_get_folder (header);
875
876         /* Delete or move to trash */
877         if (remove_to_trash) {
878                 TnyFolder *trash_folder;
879                 TnyStoreAccount *store_account;
880
881                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
882                 trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
883
884                 if (trash_folder) {
885                         modest_mail_operation_move_msg (self, header, trash_folder);
886 /*                      g_object_unref (trash_folder); */
887                 } else {
888                         ModestMailOperationPrivate *priv;
889
890                         /* Set status failed and set an error */
891                         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
892                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
893                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
894                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
895                                      _("Error trying to delete a message. Trash folder not found"));
896                 }
897
898                 g_object_unref (G_OBJECT (store_account));
899         } else {
900                 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
901                 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
902         }
903
904         /* Free */
905         g_object_unref (folder);
906 }
907
908 static void
909 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
910 {
911         ModestMailOperationPrivate *priv;
912
913         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
914         priv->done = 1;
915         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
916
917         g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
918 }
919
920 static gboolean
921 modest_mail_operation_xfer_msg (ModestMailOperation *self,
922                                 TnyHeader *header, 
923                                 TnyFolder *folder, 
924                                 gboolean delete_original)
925 {
926         ModestMailOperationPrivate *priv;
927         TnyFolder *src_folder;
928         TnyList *headers;
929
930         src_folder = tny_header_get_folder (header);
931         headers = tny_simple_list_new ();
932
933         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
934         priv->total = 1;
935         priv->done = 0;
936         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
937
938         tny_list_prepend (headers, G_OBJECT (header));
939         tny_folder_transfer_msgs_async (src_folder, headers, folder, 
940                                         delete_original, transfer_msgs_cb, self);
941
942         /* Free */
943         g_object_unref (headers);
944         g_object_unref (folder);
945
946         return TRUE;
947 }
948
949
950 /* ******************************************************************* */
951 /* ************************* UTILIY FUNCTIONS ************************ */
952 /* ******************************************************************* */
953 static gboolean
954 is_ascii(const gchar *s)
955 {
956         while (s[0]) {
957                 if (s[0] & 128 || s[0] < 32)
958                         return FALSE;
959                 s++;
960         }
961         return TRUE;
962 }
963
964 static char *
965 get_content_type(const gchar *s)
966 {
967         GString *type;
968         
969         type = g_string_new("text/plain");
970         if (!is_ascii(s)) {
971                 if (g_utf8_validate(s, -1, NULL)) {
972                         g_string_append(type, "; charset=\"utf-8\"");
973                 } else {
974                         /* it should be impossible to reach this, but better safe than sorry */
975                         g_warning("invalid utf8 in message");
976                         g_string_append(type, "; charset=\"latin1\"");
977                 }
978         }
979         return g_string_free(type, FALSE);
980 }
981
982 static void
983 add_attachments (TnyMsg *msg, GList *attachments_list)
984 {
985         GList *pos;
986         TnyMimePart *attachment_part, *old_attachment;
987         const gchar *attachment_content_type;
988         const gchar *attachment_filename;
989         TnyStream *attachment_stream;
990         TnyPlatformFactory *fact;
991
992         fact = modest_tny_platform_factory_get_instance ();
993         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
994
995                 old_attachment = pos->data;
996                 attachment_filename = tny_mime_part_get_filename (old_attachment);
997                 attachment_stream = tny_mime_part_get_stream (old_attachment);
998                 attachment_part = tny_platform_factory_new_mime_part (fact);
999                 
1000                 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1001                                  
1002                 tny_mime_part_construct_from_stream (attachment_part,
1003                                                      attachment_stream,
1004                                                      attachment_content_type);
1005                 tny_stream_reset (attachment_stream);
1006                 
1007                 tny_mime_part_set_filename (attachment_part, attachment_filename);
1008                 
1009                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1010 /*              g_object_unref (attachment_part); */
1011         }
1012 }
1013
1014
1015 static TnyMimePart *
1016 add_body_part (TnyMsg *msg, 
1017                const gchar *body,
1018                const gchar *content_type,
1019                gboolean has_attachments)
1020 {
1021         TnyMimePart *text_body_part = NULL;
1022         TnyStream *text_body_stream;
1023         TnyPlatformFactory *fact;
1024
1025         fact = modest_tny_platform_factory_get_instance ();
1026
1027         /* Create the stream */
1028         text_body_stream = TNY_STREAM (tny_camel_stream_new
1029                                        (camel_stream_mem_new_with_buffer
1030                                         (body, strlen(body))));
1031
1032         /* Create body part if needed */
1033         if (has_attachments)
1034                 text_body_part = tny_platform_factory_new_mime_part (fact);
1035         else
1036                 text_body_part = TNY_MIME_PART(msg);
1037
1038         /* Construct MIME part */
1039         tny_stream_reset (text_body_stream);
1040         tny_mime_part_construct_from_stream (text_body_part,
1041                                              text_body_stream,
1042                                              content_type);
1043         tny_stream_reset (text_body_stream);
1044
1045         /* Add part if needed */
1046         if (has_attachments) {
1047                 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1048                 g_object_unref (G_OBJECT(text_body_part));
1049         }
1050
1051         /* Clean */
1052         g_object_unref (text_body_stream);
1053
1054         return text_body_part;
1055 }