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