* Migration to GtkUIManager almost completed
[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 */
313         header = tny_msg_get_header (msg);
314         body   = modest_tny_msg_actions_find_body_part (msg, TRUE);
315
316         /* TODO: select the formatter from account prefs */
317         formatter = modest_formatter_new ("text/plain");
318
319         /* Format message body */
320         if (is_reply) {
321                 switch (type) {
322                 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
323                 default:
324                         new_msg = modest_formatter_cite  (formatter, body, header);
325                         break;
326                 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
327                         new_msg = modest_formatter_quote (formatter, body, header);
328                         break;
329                 }
330         } else {
331                 switch (type) {
332                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
333                 default:
334                         new_msg = modest_formatter_inline  (formatter, body, header);
335                         break;
336                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
337                         new_msg = modest_formatter_attach (formatter, body, header);
338                         break;
339                 }
340         }
341         g_object_unref (G_OBJECT (formatter));
342
343         /* Fill the header */
344         fact = modest_tny_platform_factory_get_instance ();
345         new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
346         tny_msg_set_header (new_msg, new_header);
347         tny_header_set_from (new_header, from);
348         tny_header_set_replyto (new_header, from);
349
350         /* Change the subject */
351         new_subject = 
352                 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header), 
353                                                              (is_reply) ? _("Re:") : _("Fwd:"));
354         tny_header_set_subject (new_header, (const gchar *) new_subject);
355         g_free (new_subject);
356
357         /* Clean */
358         g_object_unref (G_OBJECT (new_header));
359         g_object_unref (G_OBJECT (header));
360
361         return new_msg;
362 }
363
364 TnyMsg* 
365 modest_mail_operation_create_forward_mail (TnyMsg *msg, 
366                                            const gchar *from,
367                                            ModestMailOperationForwardType forward_type)
368 {
369         TnyMsg *new_msg;
370         TnyList *parts = NULL;
371         GList *attachments_list = NULL;
372
373         new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
374
375         /* Add attachments */
376         parts = TNY_LIST (tny_simple_list_new());
377         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
378         tny_list_foreach (parts, add_if_attachment, attachments_list);
379         add_attachments (new_msg, attachments_list);
380
381         /* Clean */
382         if (attachments_list) g_list_free (attachments_list);
383         g_object_unref (G_OBJECT (parts));
384
385         return new_msg;
386 }
387
388 TnyMsg* 
389 modest_mail_operation_create_reply_mail (TnyMsg *msg, 
390                                          const gchar *from,
391                                          ModestMailOperationReplyType reply_type,
392                                          ModestMailOperationReplyMode reply_mode)
393 {
394         TnyMsg *new_msg;
395         TnyHeader *new_header, *header;
396         const gchar* reply_to;
397
398         new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
399
400         /* Fill the header */
401         header = tny_msg_get_header (msg);
402         new_header = tny_msg_get_header (new_msg);
403         reply_to = tny_header_get_replyto (header);
404         if (reply_to)
405                 tny_header_set_to (new_header, reply_to);
406         else
407                 tny_header_set_to (new_header, tny_header_get_from (header));
408
409         switch (reply_mode) {
410                 gchar *new_cc = NULL;
411                 const gchar *cc = NULL, *bcc = NULL;
412                 GString *tmp = NULL;
413
414         case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
415                 /* Do not fill neither cc nor bcc */
416                 break;
417         case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
418                 /* TODO */
419                 break;
420         case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
421                 /* Concatenate to, cc and bcc */
422                 cc = tny_header_get_cc (header);
423                 bcc = tny_header_get_bcc (header);
424
425                 tmp = g_string_new (tny_header_get_to (header));
426                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
427                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
428
429                /* Remove my own address from the cc list. TODO:
430                   remove also the To: of the new message, needed due
431                   to the new reply_to feature */
432                 new_cc = (gchar *) 
433                         modest_text_utils_remove_address ((const gchar *) tmp->str, 
434                                                           from);
435                 /* FIXME: remove also the mails from the new To: */
436                 tny_header_set_cc (new_header, new_cc);
437
438                 /* Clean */
439                 g_string_free (tmp, TRUE);
440                 g_free (new_cc);
441                 break;
442         }
443
444         /* Clean */
445         g_object_unref (G_OBJECT (new_header));
446         g_object_unref (G_OBJECT (header));
447
448         return new_msg;
449 }
450
451 static void
452 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data) 
453 {
454         g_print ("%s status: %d, of total %d\n", what, status, oftotal);
455 }
456
457 static void
458 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
459 {
460         ModestMailOperation *self = NULL;
461         ModestMailOperationPrivate *priv = NULL;
462         RefreshFolderAsyncHelper *helper;
463
464         helper = (RefreshFolderAsyncHelper *) user_data;
465         self = MODEST_MAIL_OPERATION (helper->mail_op);
466         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
467
468         if ((canceled && *err) || *err) {
469                 priv->error = g_error_copy (*err);
470                 helper->failed++;
471         } else if (canceled) {
472                 helper->canceled++;
473                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
474                              MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
475                              _("Error trying to refresh folder %s. Operation canceled"),
476                              tny_folder_get_name (folder));
477         } else {
478                 priv->done++;
479         }
480
481         if (priv->done == priv->total)
482                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
483         else if ((priv->done + helper->canceled + helper->failed) == priv->total)
484                 if (helper->failed == priv->total)
485                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
486                 else if (helper->failed == priv->total)
487                         priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
488                 else
489                         priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
490
491         tny_iterator_next (helper->iter);
492         if (tny_iterator_is_done (helper->iter)) {
493                 TnyList *list;
494                 list = tny_iterator_get_list (helper->iter);
495                 g_object_unref (G_OBJECT (helper->iter));
496                 g_object_unref (G_OBJECT (list));
497                 g_slice_free (RefreshFolderAsyncHelper, helper);
498         } else {
499                 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
500                                           folder_refresh_cb,
501                                           status_update_cb, 
502                                           helper);
503         }
504         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
505 }
506
507
508 static void
509 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
510 {
511         ModestMailOperation *self;
512         ModestMailOperationPrivate *priv;
513         TnyList *folders;
514         RefreshFolderAsyncHelper *helper;
515
516         self  = MODEST_MAIL_OPERATION (user_data);
517         priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
518
519         if (*err) {
520                 priv->error = g_error_copy (*err);
521                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
522                 return;
523         }
524
525         priv->total = tny_list_get_length (list);
526         priv->done = 0;
527         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
528
529         helper = g_slice_new0 (RefreshFolderAsyncHelper);
530         helper->mail_op = self;
531         helper->iter = tny_list_create_iterator (list);
532         helper->failed = 0;
533         helper->canceled = 0;
534
535         /* Async refresh folders */
536         tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
537                                   folder_refresh_cb,
538                                   status_update_cb, 
539                                   helper);
540 }
541
542 gboolean
543 modest_mail_operation_update_account (ModestMailOperation *self,
544                                       TnyStoreAccount *store_account)
545 {
546         ModestMailOperationPrivate *priv;
547         TnyList *folders;
548         TnyFolderStoreQuery *query;
549
550         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
551         g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
552
553         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
554
555         /* Get subscribed folders & refresh them */
556         folders = TNY_LIST (tny_simple_list_new ());
557         query = tny_folder_store_query_new ();
558         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
559         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
560                                             folders, update_folders_cb, query, self);
561         g_object_unref (query);
562
563         return TRUE;
564 }
565
566 ModestMailOperationStatus
567 modest_mail_operation_get_status (ModestMailOperation *self)
568 {
569         ModestMailOperationPrivate *priv;
570
571         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
572         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
573                               MODEST_MAIL_OPERATION_STATUS_INVALID);
574
575         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
576         return priv->status;
577 }
578
579 const GError *
580 modest_mail_operation_get_error (ModestMailOperation *self)
581 {
582         ModestMailOperationPrivate *priv;
583
584         g_return_val_if_fail (self, NULL);
585         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
586
587         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
588         return priv->error;
589 }
590
591 gboolean 
592 modest_mail_operation_cancel (ModestMailOperation *self)
593 {
594         /* TODO */
595         return TRUE;
596 }
597
598 guint 
599 modest_mail_operation_get_task_done (ModestMailOperation *self)
600 {
601         ModestMailOperationPrivate *priv;
602
603         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
604
605         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
606         return priv->done;
607 }
608
609 guint 
610 modest_mail_operation_get_task_total (ModestMailOperation *self)
611 {
612         ModestMailOperationPrivate *priv;
613
614         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
615
616         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
617         return priv->total;
618 }
619
620 gboolean
621 modest_mail_operation_is_finished (ModestMailOperation *self)
622 {
623         ModestMailOperationPrivate *priv;
624         gboolean retval = FALSE;
625
626         if (!MODEST_IS_MAIL_OPERATION (self)) {
627                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
628                 return retval;
629         }
630
631         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
632
633         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
634             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
635             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
636             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
637                 retval = TRUE;
638         } else {
639                 retval = FALSE;
640         }
641
642         return retval;
643 }
644
645 /* ******************************************************************* */
646 /* ************************** STORE  ACTIONS ************************* */
647 /* ******************************************************************* */
648
649
650 TnyFolder *
651 modest_mail_operation_create_folder (ModestMailOperation *self,
652                                      TnyFolderStore *parent,
653                                      const gchar *name)
654 {
655         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
656         g_return_val_if_fail (name, NULL);
657
658         TnyFolder *new_folder = NULL;
659         TnyStoreAccount *store_account;
660
661         /* Create the folder */
662         new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
663         if (!new_folder) 
664                 return NULL;
665
666         /* Subscribe to folder */
667         if (!tny_folder_is_subscribed (new_folder)) {
668                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
669                 tny_store_account_subscribe (store_account, new_folder);
670                 g_object_unref (G_OBJECT (store_account));
671         }
672
673         return new_folder;
674 }
675
676 void
677 modest_mail_operation_remove_folder (ModestMailOperation *self,
678                                      TnyFolder *folder,
679                                      gboolean remove_to_trash)
680 {
681         TnyFolderStore *folder_store;
682
683         g_return_if_fail (TNY_IS_FOLDER (folder));
684
685         /* Get folder store */
686         folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
687
688         /* Delete folder or move to trash */
689         if (remove_to_trash) {
690                 TnyFolder *trash_folder;
691
692                 trash_folder = modest_mail_operation_find_trash_folder (self,
693                                                                         TNY_STORE_ACCOUNT (folder_store));
694
695                 /* TODO: error_handling */
696                 modest_mail_operation_move_folder (self, 
697                                                    folder, 
698                                                    TNY_FOLDER_STORE (trash_folder));
699         } else {
700                 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
701                 g_object_unref (G_OBJECT (folder));
702         }
703
704         /* Free instances */
705         g_object_unref (G_OBJECT (folder_store));
706 }
707
708 void
709 modest_mail_operation_rename_folder (ModestMailOperation *self,
710                                      TnyFolder *folder,
711                                      const gchar *name)
712 {
713         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
714         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
715         g_return_if_fail (name);
716
717         /* FIXME: better error handling */
718         if (strrchr (name, '/') != NULL)
719                 return;
720
721         /* Rename. Camel handles folder subscription/unsubscription */
722         tny_folder_set_name (folder, name, NULL); /* FIXME */
723  }
724
725 void
726 modest_mail_operation_move_folder (ModestMailOperation *self,
727                                    TnyFolder *folder,
728                                    TnyFolderStore *parent)
729 {
730         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
731         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
732         g_return_if_fail (TNY_IS_FOLDER (folder));
733         
734         modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
735 }
736
737 void
738 modest_mail_operation_copy_folder (ModestMailOperation *self,
739                                    TnyFolder *folder,
740                                    TnyFolderStore *parent)
741 {
742         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
743         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
744         g_return_if_fail (TNY_IS_FOLDER (folder));
745
746         modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
747 }
748
749 static void
750 modest_mail_operation_xfer_folder (ModestMailOperation *self,
751                                    TnyFolder *folder,
752                                    TnyFolderStore *parent,
753                                    gboolean delete_original)
754 {
755         const gchar *folder_name;
756         TnyFolder *dest_folder, *child;
757         TnyIterator *iter;
758         TnyList *folders, *headers;
759
760         g_return_if_fail (TNY_IS_FOLDER (folder));
761         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
762
763         /* Create the destination folder */
764         folder_name = tny_folder_get_name (folder);
765         dest_folder = modest_mail_operation_create_folder (self, 
766                                                            parent, folder_name);
767
768         /* Transfer messages */
769         headers = TNY_LIST (tny_simple_list_new ());
770         tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
771         tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
772
773         /* Recurse children */
774         folders = TNY_LIST (tny_simple_list_new ());
775         tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
776         iter = tny_list_create_iterator (folders);
777
778         while (!tny_iterator_is_done (iter)) {
779
780                 child = TNY_FOLDER (tny_iterator_get_current (iter));
781                 modest_mail_operation_xfer_folder (self, child,
782                                                    TNY_FOLDER_STORE (dest_folder),
783                                                    delete_original);
784                 tny_iterator_next (iter);
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 /* FIXME: this method should be rewritten when the policy for the
800    Trash folder becomes clearer */
801 static TnyFolder *
802 modest_mail_operation_find_trash_folder (ModestMailOperation *self,
803                                          TnyStoreAccount *store_account)
804 {
805         TnyList *folders;
806         TnyIterator *iter;
807         gboolean found;
808         /*TnyFolderStoreQuery *query;*/
809         TnyFolder *trash_folder;
810
811         /* Look for Trash folder */
812         folders = TNY_LIST (tny_simple_list_new ());
813         tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
814                                       folders, NULL, NULL); /* FIXME */
815         iter = tny_list_create_iterator (folders);
816
817         found = FALSE;
818         while (!tny_iterator_is_done (iter) && !found) {
819
820                 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
821                 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
822                         found = TRUE;
823                 else
824                         tny_iterator_next (iter);
825         }
826
827         /* Clean up */
828         g_object_unref (G_OBJECT (folders));
829         g_object_unref (G_OBJECT (iter));
830
831         /* TODO: better error handling management */
832         if (!found) 
833                 return NULL;
834         else
835                 return trash_folder;
836 }
837
838 /* ******************************************************************* */
839 /* **************************  MSG  ACTIONS  ************************* */
840 /* ******************************************************************* */
841
842 gboolean 
843 modest_mail_operation_copy_msg (ModestMailOperation *self,
844                                 TnyHeader *header, 
845                                 TnyFolder *folder)
846 {
847         g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
848         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
849
850         return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
851 }
852
853 gboolean 
854 modest_mail_operation_move_msg (ModestMailOperation *self,
855                                 TnyHeader *header, 
856                                 TnyFolder *folder)
857 {
858         g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
859         g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
860
861         return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
862 }
863
864 void 
865 modest_mail_operation_remove_msg (ModestMailOperation *self,
866                                   TnyHeader *header,
867                                   gboolean remove_to_trash)
868 {
869         TnyFolder *folder;
870
871         g_return_if_fail (TNY_IS_HEADER (header));
872
873         folder = tny_header_get_folder (header);
874
875         /* Delete or move to trash */
876         if (remove_to_trash) {
877                 TnyFolder *trash_folder;
878                 TnyStoreAccount *store_account;
879
880                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
881                 trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
882
883                 if (trash_folder) {
884                         modest_mail_operation_move_msg (self, header, trash_folder);
885 /*                      g_object_unref (trash_folder); */
886                 } else {
887                         ModestMailOperationPrivate *priv;
888
889                         /* Set status failed and set an error */
890                         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
891                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
892                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
893                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
894                                      _("Error trying to delete a message. Trash folder not found"));
895                 }
896
897                 g_object_unref (G_OBJECT (store_account));
898         } else {
899                 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
900                 tny_folder_expunge (folder, NULL); /* FIXME */
901         }
902
903         /* Free */
904         g_object_unref (folder);
905 }
906
907 static void
908 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
909 {
910         ModestMailOperationPrivate *priv;
911
912         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
913         priv->done = 1;
914         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
915
916         g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
917 }
918
919 static gboolean
920 modest_mail_operation_xfer_msg (ModestMailOperation *self,
921                                 TnyHeader *header, 
922                                 TnyFolder *folder, 
923                                 gboolean delete_original)
924 {
925         ModestMailOperationPrivate *priv;
926         TnyFolder *src_folder;
927         TnyList *headers;
928
929         src_folder = tny_header_get_folder (header);
930         headers = tny_simple_list_new ();
931
932         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
933         priv->total = 1;
934         priv->done = 0;
935         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
936
937         tny_list_prepend (headers, G_OBJECT (header));
938         tny_folder_transfer_msgs_async (src_folder, headers, folder, 
939                                         delete_original, transfer_msgs_cb, self);
940
941         /* Free */
942         g_object_unref (headers);
943         g_object_unref (folder);
944
945         return TRUE;
946 }
947
948
949 /* ******************************************************************* */
950 /* ************************* UTILIY FUNCTIONS ************************ */
951 /* ******************************************************************* */
952 static gboolean
953 is_ascii(const gchar *s)
954 {
955         while (s[0]) {
956                 if (s[0] & 128 || s[0] < 32)
957                         return FALSE;
958                 s++;
959         }
960         return TRUE;
961 }
962
963 static char *
964 get_content_type(const gchar *s)
965 {
966         GString *type;
967         
968         type = g_string_new("text/plain");
969         if (!is_ascii(s)) {
970                 if (g_utf8_validate(s, -1, NULL)) {
971                         g_string_append(type, "; charset=\"utf-8\"");
972                 } else {
973                         /* it should be impossible to reach this, but better safe than sorry */
974                         g_warning("invalid utf8 in message");
975                         g_string_append(type, "; charset=\"latin1\"");
976                 }
977         }
978         return g_string_free(type, FALSE);
979 }
980
981 static void
982 add_attachments (TnyMsg *msg, GList *attachments_list)
983 {
984         GList *pos;
985         TnyMimePart *attachment_part, *old_attachment;
986         const gchar *attachment_content_type;
987         const gchar *attachment_filename;
988         TnyStream *attachment_stream;
989         TnyPlatformFactory *fact;
990
991         fact = modest_tny_platform_factory_get_instance ();
992         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
993
994                 old_attachment = pos->data;
995                 attachment_filename = tny_mime_part_get_filename (old_attachment);
996                 attachment_stream = tny_mime_part_get_stream (old_attachment);
997                 attachment_part = tny_platform_factory_new_mime_part (fact);
998                 
999                 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1000                                  
1001                 tny_mime_part_construct_from_stream (attachment_part,
1002                                                      attachment_stream,
1003                                                      attachment_content_type);
1004                 tny_stream_reset (attachment_stream);
1005                 
1006                 tny_mime_part_set_filename (attachment_part, attachment_filename);
1007                 
1008                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1009 /*              g_object_unref (attachment_part); */
1010         }
1011 }
1012
1013
1014 static TnyMimePart *
1015 add_body_part (TnyMsg *msg, 
1016                const gchar *body,
1017                const gchar *content_type,
1018                gboolean has_attachments)
1019 {
1020         TnyMimePart *text_body_part = NULL;
1021         TnyStream *text_body_stream;
1022         TnyPlatformFactory *fact;
1023
1024         fact = modest_tny_platform_factory_get_instance ();
1025
1026         /* Create the stream */
1027         text_body_stream = TNY_STREAM (tny_camel_stream_new
1028                                        (camel_stream_mem_new_with_buffer
1029                                         (body, strlen(body))));
1030
1031         /* Create body part if needed */
1032         if (has_attachments)
1033                 text_body_part = tny_platform_factory_new_mime_part (fact);
1034         else
1035                 text_body_part = TNY_MIME_PART(msg);
1036
1037         /* Construct MIME part */
1038         tny_stream_reset (text_body_stream);
1039         tny_mime_part_construct_from_stream (text_body_part,
1040                                              text_body_stream,
1041                                              content_type);
1042         tny_stream_reset (text_body_stream);
1043
1044         /* Add part if needed */
1045         if (has_attachments) {
1046                 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1047                 g_object_unref (G_OBJECT(text_body_part));
1048         }
1049
1050         /* Clean */
1051         g_object_unref (text_body_stream);
1052
1053         return text_body_part;
1054 }