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