f39bb8464ce529594df5e16b39939a6fb44ac38c
[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 <string.h>
31 #include <stdarg.h>
32 #include <tny-mime-part.h>
33 #include <tny-store-account.h>
34 #include <tny-folder-store.h>
35 #include <tny-folder-store-query.h>
36 #include <tny-camel-stream.h>
37 #include <tny-camel-pop-store-account.h>
38 #include <tny-camel-pop-folder.h>
39 #include <tny-camel-imap-folder.h>
40 #include <tny-camel-mem-stream.h>
41 #include <tny-simple-list.h>
42 #include <tny-send-queue.h>
43 #include <tny-status.h>
44 #include <tny-folder-observer.h>
45 #include <camel/camel-stream-mem.h>
46 #include <glib/gi18n.h>
47 #include "modest-platform.h"
48 #include <modest-tny-account.h>
49 #include <modest-tny-send-queue.h>
50 #include <modest-runtime.h>
51 #include "modest-text-utils.h"
52 #include "modest-tny-msg.h"
53 #include "modest-tny-folder.h"
54 #include "modest-tny-account-store.h"
55 #include "modest-tny-platform-factory.h"
56 #include "modest-marshal.h"
57 #include "modest-error.h"
58 #include "modest-mail-operation.h"
59
60 #define KB 1024
61
62 /* 
63  * Remove all these #ifdef stuff when the tinymail's idle calls become
64  * locked
65  */
66 #define TINYMAIL_IDLES_NOT_LOCKED_YET 1
67
68 /* 'private'/'protected' functions */
69 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
70 static void modest_mail_operation_init       (ModestMailOperation *obj);
71 static void modest_mail_operation_finalize   (GObject *obj);
72
73 static void     get_msg_cb (TnyFolder *folder, 
74                             gboolean cancelled, 
75                             TnyMsg *msg, 
76                             GError *rr, 
77                             gpointer user_data);
78
79 static void     get_msg_status_cb (GObject *obj,
80                                    TnyStatus *status,  
81                                    gpointer user_data);
82
83 static void     modest_mail_operation_notify_end (ModestMailOperation *self);
84
85 enum _ModestMailOperationSignals 
86 {
87         PROGRESS_CHANGED_SIGNAL,
88
89         NUM_SIGNALS
90 };
91
92 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
93 struct _ModestMailOperationPrivate {
94         TnyAccount                 *account;
95         gchar                                                                            *account_name;
96         guint                      done;
97         guint                      total;
98         GObject                   *source;
99         GError                    *error;
100         ErrorCheckingUserCallback  error_checking;
101         gpointer                   error_checking_user_data;
102         ModestMailOperationStatus  status;      
103         ModestMailOperationTypeOperation op_type;
104 };
105
106 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107                                                    MODEST_TYPE_MAIL_OPERATION, \
108                                                    ModestMailOperationPrivate))
109
110 #define CHECK_EXCEPTION(priv, new_status)  if (priv->error) {\
111                                                    priv->status = new_status;\
112                                                }
113
114 typedef struct _GetMsgAsyncHelper {     
115         ModestMailOperation *mail_op;
116         TnyHeader *header;
117         GetMsgAsyncUserCallback user_callback;  
118         gpointer user_data;
119 } GetMsgAsyncHelper;
120
121 typedef struct _RefreshAsyncHelper {    
122         ModestMailOperation *mail_op;
123         RefreshAsyncUserCallback user_callback; 
124         gpointer user_data;
125 } RefreshAsyncHelper;
126
127 typedef struct _XFerMsgAsyncHelper
128 {
129         ModestMailOperation *mail_op;
130         TnyList *headers;
131         TnyFolder *dest_folder;
132         XferMsgsAsynUserCallback user_callback; 
133         gboolean delete;
134         gpointer user_data;
135 } XFerMsgAsyncHelper;
136
137 typedef void (*ModestMailOperationCreateMsgCallback) (ModestMailOperation *mail_op,
138                                                       TnyMsg *msg,
139                                                       gpointer userdata);
140
141 static void          modest_mail_operation_create_msg (ModestMailOperation *self,
142                                                        const gchar *from, const gchar *to,
143                                                        const gchar *cc, const gchar *bcc,
144                                                        const gchar *subject, const gchar *plain_body,
145                                                        const gchar *html_body, const GList *attachments_list,
146                                                        TnyHeaderFlags priority_flags,
147                                                        ModestMailOperationCreateMsgCallback callback,
148                                                        gpointer userdata);
149
150 static gboolean      idle_notify_queue (gpointer data);
151 typedef struct
152 {
153         ModestMailOperation *mail_op;
154         gchar *from;
155         gchar *to;
156         gchar *cc;
157         gchar *bcc;
158         gchar *subject;
159         gchar *plain_body;
160         gchar *html_body;
161         GList *attachments_list;
162         TnyHeaderFlags priority_flags;
163         ModestMailOperationCreateMsgCallback callback;
164         gpointer userdata;
165 } CreateMsgInfo;
166
167 typedef struct
168 {
169         ModestMailOperation *mail_op;
170         TnyMsg *msg;
171         ModestMailOperationCreateMsgCallback callback;
172         gpointer userdata;
173 } CreateMsgIdleInfo;
174
175 /* globals */
176 static GObjectClass *parent_class = NULL;
177
178 static guint signals[NUM_SIGNALS] = {0};
179
180 GType
181 modest_mail_operation_get_type (void)
182 {
183         static GType my_type = 0;
184         if (!my_type) {
185                 static const GTypeInfo my_info = {
186                         sizeof(ModestMailOperationClass),
187                         NULL,           /* base init */
188                         NULL,           /* base finalize */
189                         (GClassInitFunc) modest_mail_operation_class_init,
190                         NULL,           /* class finalize */
191                         NULL,           /* class data */
192                         sizeof(ModestMailOperation),
193                         1,              /* n_preallocs */
194                         (GInstanceInitFunc) modest_mail_operation_init,
195                         NULL
196                 };
197                 my_type = g_type_register_static (G_TYPE_OBJECT,
198                                                   "ModestMailOperation",
199                                                   &my_info, 0);
200         }
201         return my_type;
202 }
203
204 static void
205 modest_mail_operation_class_init (ModestMailOperationClass *klass)
206 {
207         GObjectClass *gobject_class;
208         gobject_class = (GObjectClass*) klass;
209
210         parent_class            = g_type_class_peek_parent (klass);
211         gobject_class->finalize = modest_mail_operation_finalize;
212
213         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
214
215         /**
216          * ModestMailOperation::progress-changed
217          * @self: the #MailOperation that emits the signal
218          * @user_data: user data set when the signal handler was connected
219          *
220          * Emitted when the progress of a mail operation changes
221          */
222         signals[PROGRESS_CHANGED_SIGNAL] = 
223                 g_signal_new ("progress-changed",
224                               G_TYPE_FROM_CLASS (gobject_class),
225                               G_SIGNAL_RUN_FIRST,
226                               G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
227                               NULL, NULL,
228                               g_cclosure_marshal_VOID__POINTER,
229                               G_TYPE_NONE, 1, G_TYPE_POINTER);
230
231 }
232
233 static void
234 modest_mail_operation_init (ModestMailOperation *obj)
235 {
236         ModestMailOperationPrivate *priv;
237
238         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
239
240         priv->account        = NULL;
241         priv->status         = MODEST_MAIL_OPERATION_STATUS_INVALID;
242         priv->op_type        = MODEST_MAIL_OPERATION_TYPE_UNKNOWN;
243         priv->error          = NULL;
244         priv->done           = 0;
245         priv->total          = 0;
246         priv->source         = NULL;
247         priv->error_checking = NULL;
248         priv->error_checking_user_data = NULL;
249 }
250
251 static void
252 modest_mail_operation_finalize (GObject *obj)
253 {
254         ModestMailOperationPrivate *priv;
255
256         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
257
258         
259         
260         if (priv->error) {
261                 g_error_free (priv->error);
262                 priv->error = NULL;
263         }
264         if (priv->source) {
265                 g_object_unref (priv->source);
266                 priv->source = NULL;
267         }
268         if (priv->account) {
269                 g_object_unref (priv->account);
270                 priv->account = NULL;
271         }
272
273
274         G_OBJECT_CLASS(parent_class)->finalize (obj);
275 }
276
277 ModestMailOperation*
278 modest_mail_operation_new (ModestMailOperationTypeOperation op_type, 
279                            GObject *source)
280 {
281         ModestMailOperation *obj;
282         ModestMailOperationPrivate *priv;
283                 
284         obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
285         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
286
287         priv->op_type = op_type;
288         if (source != NULL)
289                 priv->source = g_object_ref(source);
290
291         return obj;
292 }
293
294 ModestMailOperation*
295 modest_mail_operation_new_with_error_handling (ModestMailOperationTypeOperation op_type,
296                                                GObject *source,
297                                                ErrorCheckingUserCallback error_handler,
298                                                gpointer user_data)
299 {
300         ModestMailOperation *obj;
301         ModestMailOperationPrivate *priv;
302                 
303         obj = modest_mail_operation_new (op_type, source);
304         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
305         
306         g_return_val_if_fail (error_handler != NULL, obj);
307         priv->error_checking = error_handler;
308         priv->error_checking_user_data = user_data;
309
310         return obj;
311 }
312
313 void
314 modest_mail_operation_execute_error_handler (ModestMailOperation *self)
315 {
316         ModestMailOperationPrivate *priv;
317         
318         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
319         g_return_if_fail(priv->status != MODEST_MAIL_OPERATION_STATUS_SUCCESS);     
320
321         if (priv->error_checking != NULL)
322                 priv->error_checking (self, priv->error_checking_user_data);
323 }
324
325
326 ModestMailOperationTypeOperation
327 modest_mail_operation_get_type_operation (ModestMailOperation *self)
328 {
329         ModestMailOperationPrivate *priv;
330
331         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
332         
333         return priv->op_type;
334 }
335
336 gboolean 
337 modest_mail_operation_is_mine (ModestMailOperation *self, 
338                                GObject *me)
339 {
340         ModestMailOperationPrivate *priv;
341
342         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
343         if (priv->source == NULL) return FALSE;
344
345         return priv->source == me;
346 }
347
348 GObject *
349 modest_mail_operation_get_source (ModestMailOperation *self)
350 {
351         ModestMailOperationPrivate *priv;
352
353         g_return_val_if_fail (self, NULL);
354         
355         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
356         if (!priv) {
357                 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
358                 return NULL;
359         }
360         
361         return g_object_ref (priv->source);
362 }
363
364 ModestMailOperationStatus
365 modest_mail_operation_get_status (ModestMailOperation *self)
366 {
367         ModestMailOperationPrivate *priv;
368
369         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
370         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
371                               MODEST_MAIL_OPERATION_STATUS_INVALID);
372
373         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
374         if (!priv) {
375                 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
376                 return MODEST_MAIL_OPERATION_STATUS_INVALID;
377         }
378         
379         return priv->status;
380 }
381
382 const GError *
383 modest_mail_operation_get_error (ModestMailOperation *self)
384 {
385         ModestMailOperationPrivate *priv;
386
387         g_return_val_if_fail (self, NULL);
388         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
389
390         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
391
392         if (!priv) {
393                 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
394                 return NULL;
395         }
396
397         return priv->error;
398 }
399
400 gboolean 
401 modest_mail_operation_cancel (ModestMailOperation *self)
402 {
403         ModestMailOperationPrivate *priv;
404         gboolean canceled = FALSE;
405
406         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
407
408         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
409
410         /* Note that if we call cancel with an already canceled mail
411            operation the progress changed signal won't be emitted */
412         if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
413                 return FALSE;
414
415         /* Set new status */
416         priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
417         
418         /* Cancel the mail operation. We need to wrap it between this
419            start/stop operations to allow following calls to the
420            account */
421         g_return_val_if_fail (priv->account, FALSE);
422         tny_account_cancel (priv->account);
423
424         return canceled;
425 }
426
427 guint 
428 modest_mail_operation_get_task_done (ModestMailOperation *self)
429 {
430         ModestMailOperationPrivate *priv;
431
432         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
433
434         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
435         return priv->done;
436 }
437
438 guint 
439 modest_mail_operation_get_task_total (ModestMailOperation *self)
440 {
441         ModestMailOperationPrivate *priv;
442
443         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
444
445         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
446         return priv->total;
447 }
448
449 gboolean
450 modest_mail_operation_is_finished (ModestMailOperation *self)
451 {
452         ModestMailOperationPrivate *priv;
453         gboolean retval = FALSE;
454
455         if (!MODEST_IS_MAIL_OPERATION (self)) {
456                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
457                 return retval;
458         }
459
460         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
461
462         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
463             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
464             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
465             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
466                 retval = TRUE;
467         } else {
468                 retval = FALSE;
469         }
470
471         return retval;
472 }
473
474 guint
475 modest_mail_operation_get_id (ModestMailOperation *self)
476 {
477         ModestMailOperationPrivate *priv;
478
479         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
480
481         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
482         return priv->done;
483 }
484
485 guint 
486 modest_mail_operation_set_id (ModestMailOperation *self,
487                               guint id)
488 {
489         ModestMailOperationPrivate *priv;
490
491         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
492
493         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
494         return priv->done;
495 }
496
497 /*
498  * Creates an image of the current state of a mail operation, the
499  * caller must free it
500  */
501 static ModestMailOperationState *
502 modest_mail_operation_clone_state (ModestMailOperation *self)
503 {
504         ModestMailOperationState *state;
505         ModestMailOperationPrivate *priv;
506
507         /* FIXME: this should be fixed properly
508          * 
509          * in some cases, priv was NULL, so checking here to
510          * make sure.
511          */
512         g_return_val_if_fail (self, NULL);
513         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
514         g_return_val_if_fail (priv, NULL);
515
516         if (!priv)
517                 return NULL;
518
519         state = g_slice_new (ModestMailOperationState);
520
521         state->status = priv->status;
522         state->op_type = priv->op_type;
523         state->done = priv->done;
524         state->total = priv->total;
525         state->finished = modest_mail_operation_is_finished (self);
526         state->bytes_done = 0;
527         state->bytes_total = 0;
528
529         return state;
530 }
531
532 /* ******************************************************************* */
533 /* ************************** SEND   ACTIONS ************************* */
534 /* ******************************************************************* */
535
536 void
537 modest_mail_operation_send_mail (ModestMailOperation *self,
538                                  TnyTransportAccount *transport_account,
539                                  TnyMsg* msg)
540 {
541         TnySendQueue *send_queue = NULL;
542         ModestMailOperationPrivate *priv;
543         
544         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
545         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
546         g_return_if_fail (TNY_IS_MSG (msg));
547         
548         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
549
550         /* Get account and set it into mail_operation */
551         priv->account = g_object_ref (transport_account);
552         priv->done = 1;
553         priv->total = 1;
554
555         send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
556         if (!TNY_IS_SEND_QUEUE(send_queue)) {
557                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
558                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
559                              "modest: could not find send queue for account\n");
560                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
561
562         } else {
563                 /* TODO: connect to the msg-sent in order to know when
564                    the mail operation is finished */
565
566 /*              tny_send_queue_add (send_queue, msg, &(priv->error)); */
567                 modest_tny_send_queue_add (MODEST_TNY_SEND_QUEUE(send_queue), 
568                                            msg, 
569                                            &(priv->error));
570
571                 /* TODO: we're setting always success, do the check in
572                    the handler */
573                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
574         }
575
576         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS)
577                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_outbox_waiting_to_be_sent"));
578
579         /* TODO: do this in the handler of the "msg-sent"
580            signal.Notify about operation end */
581         modest_mail_operation_notify_end (self);
582 }
583
584 static gboolean
585 idle_create_msg_cb (gpointer idle_data)
586 {
587         CreateMsgIdleInfo *info = (CreateMsgIdleInfo *) idle_data;
588
589         /* This is a GDK lock because we are an idle callback and
590          * info->callback can contain Gtk+ code */
591
592         gdk_threads_enter (); /* CHECKED */
593         info->callback (info->mail_op, info->msg, info->userdata);
594
595         g_object_unref (info->mail_op);
596         if (info->msg)
597                 g_object_unref (info->msg);
598         g_slice_free (CreateMsgIdleInfo, info);
599         gdk_threads_leave (); /* CHECKED */
600
601         return FALSE;
602 }
603
604 static gpointer 
605 create_msg_thread (gpointer thread_data)
606 {
607         CreateMsgInfo *info = (CreateMsgInfo *) thread_data;
608         TnyMsg *new_msg = NULL;
609         ModestMailOperationPrivate *priv;
610
611         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mail_op);
612         if (info->html_body == NULL) {
613                 new_msg = modest_tny_msg_new (info->to, info->from, info->cc, 
614                                               info->bcc, info->subject, info->plain_body, 
615                                               info->attachments_list); /* FIXME: attachments */
616         } else {
617                 new_msg = modest_tny_msg_new_html_plain (info->to, info->from, info->cc,
618                                                          info->bcc, info->subject, info->html_body,
619                                                          info->plain_body, info->attachments_list);
620         }
621
622         if (new_msg) {
623                 TnyHeader *header;
624                 TnyHeaderFlags flags = 0;
625
626                 /* Set priority flags in message */
627                 header = tny_msg_get_header (new_msg);
628                 if (info->priority_flags != 0)
629                         flags |= info->priority_flags;
630
631                 /* Set attachment flags in message */
632                 if (info->attachments_list != NULL)
633                         flags |= TNY_HEADER_FLAG_ATTACHMENTS;
634
635                 tny_header_set_flags (header, flags);
636                 g_object_unref (G_OBJECT(header));
637         } else {
638                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
639                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
640                              MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
641                              "modest: failed to create a new msg\n");
642         }
643
644
645         g_free (info->to);
646         g_free (info->from);
647         g_free (info->cc);
648         g_free (info->bcc);
649         g_free (info->plain_body);
650         g_free (info->html_body);
651         g_free (info->subject);
652         g_list_foreach (info->attachments_list, (GFunc) g_object_unref, NULL);
653         g_list_free (info->attachments_list);
654
655         if (info->callback) {
656                 CreateMsgIdleInfo *idle_info;
657                 idle_info = g_slice_new0 (CreateMsgIdleInfo);
658                 idle_info->mail_op = info->mail_op;
659                 g_object_ref (info->mail_op);
660                 idle_info->msg = new_msg;
661                 if (new_msg)
662                         g_object_ref (new_msg);
663                 idle_info->callback = info->callback;
664                 idle_info->userdata = info->userdata;
665                 g_idle_add (idle_create_msg_cb, idle_info);
666         } else {
667                 g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
668         }
669
670         g_object_unref (info->mail_op);
671         g_slice_free (CreateMsgInfo, info);
672         return NULL;
673 }
674
675 void
676 modest_mail_operation_create_msg (ModestMailOperation *self,
677                                   const gchar *from, const gchar *to,
678                                   const gchar *cc, const gchar *bcc,
679                                   const gchar *subject, const gchar *plain_body,
680                                   const gchar *html_body,
681                                   const GList *attachments_list,
682                                   TnyHeaderFlags priority_flags,
683                                   ModestMailOperationCreateMsgCallback callback,
684                                   gpointer userdata)
685 {
686         CreateMsgInfo *info = NULL;
687
688         info = g_slice_new0 (CreateMsgInfo);
689         info->mail_op = self;
690         g_object_ref (self);
691
692         info->from = g_strdup (from);
693         info->to = g_strdup (to);
694         info->cc = g_strdup (cc);
695         info->bcc  = g_strdup (bcc);
696         info->subject = g_strdup (subject);
697         info->plain_body = g_strdup (plain_body);
698         info->html_body = g_strdup (html_body);
699         info->attachments_list = g_list_copy ((GList *) attachments_list);
700         g_list_foreach (info->attachments_list, (GFunc) g_object_ref, NULL);
701         info->priority_flags = priority_flags;
702
703         info->callback = callback;
704         info->userdata = userdata;
705
706         g_thread_create (create_msg_thread, info, FALSE, NULL);
707 }
708
709 typedef struct
710 {
711         TnyTransportAccount *transport_account;
712         TnyMsg *draft_msg;
713 } SendNewMailInfo;
714
715 static void
716 modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
717                                         TnyMsg *msg,
718                                         gpointer userdata)
719 {
720         SendNewMailInfo *info = (SendNewMailInfo *) userdata;
721         TnyFolder *draft_folder = NULL;
722         TnyFolder *outbox_folder = NULL;
723         TnyHeader *header;
724         GError *err = NULL;
725
726         if (!msg) {
727                 goto end;
728         }
729
730         /* Call mail operation */
731         modest_mail_operation_send_mail (self, info->transport_account, msg);
732
733         /* Remove old mail from its source folder */
734         draft_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account), TNY_FOLDER_TYPE_DRAFTS);
735         outbox_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account), TNY_FOLDER_TYPE_OUTBOX);
736         if (info->draft_msg != NULL) {
737                 TnyFolder *folder = NULL;
738                 TnyFolder *src_folder = NULL;
739                 TnyFolderType folder_type;              
740                 folder = tny_msg_get_folder (info->draft_msg);          
741                 if (folder == NULL) goto end;
742                 folder_type = modest_tny_folder_guess_folder_type (folder);
743                 if (folder_type == TNY_FOLDER_TYPE_OUTBOX) 
744                         src_folder = outbox_folder;
745                 else 
746                         src_folder = draft_folder;
747
748                 /* Note: This can fail (with a warning) if the message is not really already in a folder,
749                  * because this function requires it to have a UID. */          
750                 header = tny_msg_get_header (info->draft_msg);
751                 tny_folder_remove_msg (src_folder, header, NULL);
752
753                 tny_folder_sync (folder, TRUE, &err); /* FALSE --> don't expunge */
754 /*              tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL);  /\* expunge *\/ */
755                 
756                 g_object_unref (header);
757                 g_object_unref (folder);
758         }
759
760 end:
761         if (err != NULL)
762                 g_error_free(err);      
763         if (info->draft_msg)
764                 g_object_unref (info->draft_msg);
765         if (draft_folder)
766                 g_object_unref (draft_folder);
767         if (outbox_folder)
768                 g_object_unref (outbox_folder);
769         if (info->transport_account)
770                 g_object_unref (info->transport_account);
771         g_slice_free (SendNewMailInfo, info);
772         modest_mail_operation_notify_end (self);
773 }
774
775 void
776 modest_mail_operation_send_new_mail (ModestMailOperation *self,
777                                      TnyTransportAccount *transport_account,
778                                      TnyMsg *draft_msg,
779                                      const gchar *from,  const gchar *to,
780                                      const gchar *cc,  const gchar *bcc,
781                                      const gchar *subject, const gchar *plain_body,
782                                      const gchar *html_body,
783                                      const GList *attachments_list,
784                                      TnyHeaderFlags priority_flags)
785 {
786         ModestMailOperationPrivate *priv = NULL;
787         SendNewMailInfo *info;
788
789         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
790         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
791
792         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
793
794         /* Check parametters */
795         if (to == NULL) {
796                 /* Set status failed and set an error */
797                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
798                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
799                              MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
800                              _("Error trying to send a mail. You need to set at least one recipient"));
801                 return;
802         }
803         info = g_slice_new0 (SendNewMailInfo);
804         info->transport_account = transport_account;
805         if (transport_account)
806                 g_object_ref (transport_account);
807         info->draft_msg = draft_msg;
808         if (draft_msg)
809                 g_object_ref (draft_msg);
810         modest_mail_operation_create_msg (self, from, to, cc, bcc, subject, plain_body, html_body,
811                                           attachments_list, priority_flags,
812                                           modest_mail_operation_send_new_mail_cb, info);
813
814 }
815
816 typedef struct
817 {
818         TnyTransportAccount *transport_account;
819         TnyMsg *draft_msg;
820         ModestMsgEditWindow *edit_window;
821 } SaveToDraftsInfo;
822
823 static void
824 modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
825                                          TnyMsg *msg,
826                                          gpointer userdata)
827 {
828         TnyFolder *src_folder = NULL;
829         TnyFolder *folder = NULL;
830         TnyHeader *header = NULL;
831         ModestMailOperationPrivate *priv = NULL;
832         SaveToDraftsInfo *info = (SaveToDraftsInfo *) userdata;
833
834         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
835         if (!msg) {
836                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
837                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
838                              MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
839                              "modest: failed to create a new msg\n");
840                 goto end;
841         }
842
843         folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account), TNY_FOLDER_TYPE_DRAFTS);
844         if (!folder) {
845                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
846                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
847                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
848                              "modest: failed to create a new msg\n");
849                 goto end;
850         }
851
852         if (!priv->error)
853                 tny_folder_add_msg (folder, msg, &(priv->error));
854
855         if ((!priv->error) && (info->draft_msg != NULL)) {
856                 header = tny_msg_get_header (info->draft_msg);
857                 src_folder = tny_header_get_folder (header); 
858
859                 /* Remove the old draft expunging it */
860                 tny_folder_remove_msg (src_folder, header, NULL);
861 /*              tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED); */
862 /*              tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN); */
863
864                 tny_folder_sync (folder, TRUE, &(priv->error)); /* FALSE --> don't expunge */
865                 tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL);  /* expunge */
866
867                 g_object_unref (header);
868         }
869         
870         if (!priv->error)
871                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
872         else
873                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
874
875         if (info->edit_window)
876                 modest_msg_edit_window_set_draft (info->edit_window, msg);
877
878
879 end:
880         if (folder)
881                 g_object_unref (G_OBJECT(folder));
882         if (src_folder)
883                 g_object_unref (G_OBJECT(src_folder));
884         if (info->edit_window)
885                 g_object_unref (G_OBJECT(info->edit_window));
886         if (info->draft_msg)
887                 g_object_unref (G_OBJECT (info->draft_msg));
888         if (info->transport_account)
889                 g_object_unref (G_OBJECT(info->transport_account));
890         g_slice_free (SaveToDraftsInfo, info);
891
892         modest_mail_operation_notify_end (self);
893 }
894
895 void
896 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
897                                       TnyTransportAccount *transport_account,
898                                       TnyMsg *draft_msg,
899                                       ModestMsgEditWindow *edit_window,
900                                       const gchar *from,  const gchar *to,
901                                       const gchar *cc,  const gchar *bcc,
902                                       const gchar *subject, const gchar *plain_body,
903                                       const gchar *html_body,
904                                       const GList *attachments_list,
905                                       TnyHeaderFlags priority_flags)
906 {
907         ModestMailOperationPrivate *priv = NULL;
908         SaveToDraftsInfo *info = NULL;
909
910         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
911         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
912
913         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
914
915         /* Get account and set it into mail_operation */
916         priv->account = g_object_ref (transport_account);
917
918         info = g_slice_new0 (SaveToDraftsInfo);
919         info->transport_account = g_object_ref (transport_account);
920         info->draft_msg = draft_msg;
921         if (draft_msg)
922                 g_object_ref (draft_msg);
923         info->edit_window = edit_window;
924         if (edit_window)
925                 g_object_ref (edit_window);
926
927         modest_mail_operation_create_msg (self, from, to, cc, bcc, subject, plain_body, html_body,
928                                           attachments_list, priority_flags,
929                                           modest_mail_operation_save_to_drafts_cb, info);
930
931 }
932
933 typedef struct 
934 {
935         ModestMailOperation *mail_op;
936         TnyStoreAccount *account;
937         TnyTransportAccount *transport_account;
938         gint max_size;
939         gint retrieve_limit;
940         gchar *retrieve_type;
941         gchar *account_name;
942         UpdateAccountCallback callback;
943         gpointer user_data;
944         TnyList *new_headers;
945 } UpdateAccountInfo;
946
947 typedef struct
948 {
949         ModestMailOperation *mail_op;
950         TnyMimePart *mime_part;
951         gssize size;
952         GetMimePartSizeCallback callback;
953         gpointer userdata;
954 } GetMimePartSizeInfo;
955
956 /***** I N T E R N A L    F O L D E R    O B S E R V E R *****/
957 /* We use this folder observer to track the headers that have been
958  * added to a folder */
959 typedef struct {
960         GObject parent;
961         TnyList *new_headers;
962 } InternalFolderObserver;
963
964 typedef struct {
965         GObjectClass parent;
966 } InternalFolderObserverClass;
967
968 static void tny_folder_observer_init (TnyFolderObserverIface *idace);
969
970 G_DEFINE_TYPE_WITH_CODE (InternalFolderObserver,
971                          internal_folder_observer,
972                          G_TYPE_OBJECT,
973                          G_IMPLEMENT_INTERFACE(TNY_TYPE_FOLDER_OBSERVER, tny_folder_observer_init));
974
975
976 static void
977 foreach_add_item (gpointer header, gpointer user_data)
978 {
979         tny_list_prepend (TNY_LIST (user_data), 
980                           g_object_ref (G_OBJECT (header)));
981 }
982
983 /* This is the method that looks for new messages in a folder */
984 static void
985 internal_folder_observer_update (TnyFolderObserver *self, TnyFolderChange *change)
986 {
987         InternalFolderObserver *derived = (InternalFolderObserver *)self;
988         
989         TnyFolderChangeChanged changed;
990
991         changed = tny_folder_change_get_changed (change);
992
993         if (changed & TNY_FOLDER_CHANGE_CHANGED_ADDED_HEADERS) {
994                 TnyList *list;
995
996                 /* Get added headers */
997                 list = tny_simple_list_new ();
998                 tny_folder_change_get_added_headers (change, list);
999
1000                 /* Add them to the folder observer */
1001                 tny_list_foreach (list, foreach_add_item, 
1002                                   derived->new_headers);
1003
1004                 g_object_unref (G_OBJECT (list));
1005         }
1006 }
1007
1008 static void
1009 internal_folder_observer_init (InternalFolderObserver *self) 
1010 {
1011         self->new_headers = tny_simple_list_new ();
1012 }
1013 static void
1014 internal_folder_observer_finalize (GObject *object) 
1015 {
1016         InternalFolderObserver *self;
1017
1018         self = (InternalFolderObserver *) object;
1019         g_object_unref (self->new_headers);
1020
1021         G_OBJECT_CLASS (internal_folder_observer_parent_class)->finalize (object);
1022 }
1023 static void
1024 tny_folder_observer_init (TnyFolderObserverIface *iface) 
1025 {
1026         iface->update_func = internal_folder_observer_update;
1027 }
1028 static void
1029 internal_folder_observer_class_init (InternalFolderObserverClass *klass) 
1030 {
1031         GObjectClass *object_class;
1032
1033         internal_folder_observer_parent_class = g_type_class_peek_parent (klass);
1034         object_class = (GObjectClass*) klass;
1035         object_class->finalize = internal_folder_observer_finalize;
1036 }
1037
1038 /*****************/
1039
1040 static void
1041 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
1042 {
1043         TnyIterator *iter;
1044         TnyList *folders = tny_simple_list_new ();
1045
1046         tny_folder_store_get_folders (store, folders, query, NULL);
1047         iter = tny_list_create_iterator (folders);
1048
1049         while (!tny_iterator_is_done (iter)) {
1050
1051                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
1052                 if (folder) {
1053                         tny_list_prepend (all_folders, G_OBJECT (folder));
1054                         recurse_folders (folder, query, all_folders);    
1055                         g_object_unref (G_OBJECT (folder));
1056                 }
1057
1058                 tny_iterator_next (iter);
1059         }
1060          g_object_unref (G_OBJECT (iter));
1061          g_object_unref (G_OBJECT (folders));
1062 }
1063
1064 /* 
1065  * Issues the "progress-changed" signal. The timer won't be removed,
1066  * so you must call g_source_remove to stop the signal emission
1067  */
1068 static gboolean
1069 idle_notify_progress (gpointer data)
1070 {
1071         ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
1072         ModestMailOperationState *state;
1073
1074         state = modest_mail_operation_clone_state (mail_op);
1075
1076         /* This is a GDK lock because we are an idle callback and
1077          * the handlers of this signal can contain Gtk+ code */
1078
1079         gdk_threads_enter (); /* CHECKED */
1080         g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
1081         gdk_threads_leave (); /* CHECKED */
1082
1083         g_slice_free (ModestMailOperationState, state);
1084         
1085         return TRUE;
1086 }
1087
1088 /* 
1089  * Issues the "progress-changed" signal and removes the timer. It uses
1090  * a lock to ensure that the progress information of the mail
1091  * operation is not modified while there are notifications pending
1092  */
1093 static gboolean
1094 idle_notify_progress_once (gpointer data)
1095 {
1096         ModestPair *pair;
1097
1098         pair = (ModestPair *) data;
1099
1100         /* This is a GDK lock because we are an idle callback and
1101          * the handlers of this signal can contain Gtk+ code */
1102
1103         gdk_threads_enter (); /* CHECKED */
1104         g_signal_emit (G_OBJECT (pair->first), signals[PROGRESS_CHANGED_SIGNAL], 0, pair->second, NULL);
1105         gdk_threads_leave (); /* CHECKED */
1106
1107         /* Free the state and the reference to the mail operation */
1108         g_slice_free (ModestMailOperationState, (ModestMailOperationState*)pair->second);
1109         g_object_unref (pair->first);
1110
1111         return FALSE;
1112 }
1113
1114 /* 
1115  * Used to notify the queue from the main
1116  * loop. We call it inside an idle call to achieve that
1117  */
1118 static gboolean
1119 idle_notify_queue (gpointer data)
1120 {
1121         ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
1122
1123         /* Do not need to block, the notify end will do it for us */    
1124         modest_mail_operation_notify_end (mail_op);
1125         g_object_unref (mail_op);
1126
1127         return FALSE;
1128 }
1129
1130 static int
1131 compare_headers_by_date (gconstpointer a, 
1132                          gconstpointer b)
1133 {
1134         TnyHeader **header1, **header2;
1135         time_t sent1, sent2;
1136
1137         header1 = (TnyHeader **) a;
1138         header2 = (TnyHeader **) b;
1139
1140         sent1 = tny_header_get_date_sent (*header1);
1141         sent2 = tny_header_get_date_sent (*header2);
1142
1143         /* We want the most recent ones (greater time_t) at the
1144            beginning */
1145         if (sent1 < sent2)
1146                 return 1;
1147         else
1148                 return -1;
1149 }
1150
1151 static gboolean 
1152 set_last_updated_idle (gpointer data)
1153 {
1154
1155         /* This is a GDK lock because we are an idle callback and
1156          * modest_account_mgr_set_int can contain Gtk+ code */
1157
1158         gdk_threads_enter (); /* CHECKED - please recheck */
1159
1160         /* It does not matter if the time is not exactly the same than
1161            the time when this idle was called, it's just an
1162            approximation and it won't be very different */
1163
1164         modest_account_mgr_set_int (modest_runtime_get_account_mgr (), 
1165                                     (gchar *) data, 
1166                                     MODEST_ACCOUNT_LAST_UPDATED, 
1167                                     time(NULL), 
1168                                     TRUE);
1169
1170         gdk_threads_leave (); /* CHECKED - please recheck */
1171
1172         return FALSE;
1173 }
1174
1175 static gboolean
1176 idle_update_account_cb (gpointer data)
1177 {
1178         UpdateAccountInfo *idle_info;
1179
1180         idle_info = (UpdateAccountInfo *) data;
1181
1182         /* This is a GDK lock because we are an idle callback and
1183          * idle_info->callback can contain Gtk+ code */
1184
1185         gdk_threads_enter (); /* CHECKED */
1186         idle_info->callback (idle_info->mail_op,
1187                              idle_info->new_headers,
1188                              idle_info->user_data);
1189         gdk_threads_leave (); /* CHECKED */
1190
1191         /* Frees */
1192         g_object_unref (idle_info->mail_op);
1193         if (idle_info->new_headers)
1194                 g_object_unref (idle_info->new_headers);
1195         g_free (idle_info);
1196
1197         return FALSE;
1198 }
1199
1200 static TnyList *
1201 get_all_folders_from_account (TnyStoreAccount *account,
1202                               GError **error)
1203 {
1204         TnyList *all_folders = NULL;
1205         TnyIterator *iter = NULL;
1206         TnyFolderStoreQuery *query = NULL;
1207
1208         all_folders = tny_simple_list_new ();
1209         query = tny_folder_store_query_new ();
1210         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1211         tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
1212                                       all_folders,
1213                                       query,
1214                                       error);
1215
1216         if (*error) {
1217                 if (all_folders)
1218                         g_object_unref (all_folders);
1219                 return NULL;
1220         }
1221
1222         iter = tny_list_create_iterator (all_folders);
1223         while (!tny_iterator_is_done (iter)) {
1224                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
1225                 if (folder) {
1226                         recurse_folders (folder, query, all_folders);
1227                         g_object_unref (folder);
1228                 }
1229                 tny_iterator_next (iter);
1230         }
1231         g_object_unref (G_OBJECT (iter));
1232
1233         return all_folders;
1234 }
1235
1236
1237 static gpointer
1238 update_account_thread (gpointer thr_user_data)
1239 {
1240         static gboolean first_time = TRUE;
1241         UpdateAccountInfo *info = NULL;
1242         TnyList *all_folders = NULL, *new_headers = NULL;
1243         GPtrArray *new_headers_array = NULL;
1244         TnyIterator *iter = NULL;
1245         ModestMailOperationPrivate *priv = NULL;
1246         ModestTnySendQueue *send_queue = NULL;
1247         gint i = 0, timeout = 0;
1248
1249         info = (UpdateAccountInfo *) thr_user_data;
1250         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mail_op);
1251
1252         /* Get account and set it into mail_operation */
1253         priv->account = g_object_ref (info->account);
1254
1255         /* Get all the folders. We can do it synchronously because
1256            we're already running in a different thread than the UI */
1257         all_folders = get_all_folders_from_account (info->account, &(priv->error));
1258         if (!all_folders) {
1259                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1260                 goto out;
1261         }
1262
1263         /* Update status and notify. We need to call the notification
1264            with a source function in order to call it from the main
1265            loop. We need that in order not to get into trouble with
1266            Gtk+. We use a timeout in order to provide more status
1267            information, because the sync tinymail call does not
1268            provide it for the moment */
1269         timeout = g_timeout_add (100, idle_notify_progress, info->mail_op);
1270
1271         new_headers_array = g_ptr_array_new ();
1272         iter = tny_list_create_iterator (all_folders);
1273
1274         while (!tny_iterator_is_done (iter) && !priv->error && 
1275                priv->status != MODEST_MAIL_OPERATION_STATUS_CANCELED) {
1276
1277                 TnyFolderType folder_type;
1278                 TnyFolder *folder = NULL;
1279
1280                 folder = TNY_FOLDER (tny_iterator_get_current (iter));
1281                 folder_type = tny_folder_get_folder_type (folder);
1282
1283                 /* Refresh it only if it's the INBOX */
1284                 if (folder_type == TNY_FOLDER_TYPE_INBOX) {
1285                         InternalFolderObserver *observer = NULL;
1286                         TnyIterator *new_headers_iter = NULL;
1287
1288                         /* Refresh the folder. Our observer receives
1289                          * the new emails during folder refreshes, so
1290                          * we can use observer->new_headers
1291                          */
1292                         observer = g_object_new (internal_folder_observer_get_type (), NULL);
1293                         tny_folder_add_observer (TNY_FOLDER (folder), TNY_FOLDER_OBSERVER (observer));
1294                 
1295                         tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
1296
1297                         new_headers_iter = tny_list_create_iterator (observer->new_headers);
1298                         while (!tny_iterator_is_done (new_headers_iter)) {
1299                                 TnyHeader *header = NULL;
1300
1301                                 header = TNY_HEADER (tny_iterator_get_current (new_headers_iter));
1302                                 /* Apply per-message size limits */
1303                                 if (tny_header_get_message_size (header) < info->max_size)
1304                                         g_ptr_array_add (new_headers_array, g_object_ref (header));
1305                                 
1306                                 g_object_unref (header);
1307                                 tny_iterator_next (new_headers_iter);
1308                         }
1309                         g_object_unref (new_headers_iter);
1310
1311                         tny_folder_remove_observer (TNY_FOLDER (folder), TNY_FOLDER_OBSERVER (observer));
1312                         g_object_unref (observer);
1313                 } else {
1314                         /* We no not need to do it the first time,
1315                            because it's automatically done by the tree
1316                            model */
1317                         if (G_LIKELY (!first_time))
1318                                 tny_folder_poke_status (folder);
1319                 }
1320                 g_object_unref (folder);
1321
1322                 tny_iterator_next (iter);
1323         }
1324         g_object_unref (G_OBJECT (iter));
1325         g_source_remove (timeout);
1326
1327         if (priv->status != MODEST_MAIL_OPERATION_STATUS_CANCELED && 
1328             priv->status != MODEST_MAIL_OPERATION_STATUS_FAILED &&
1329             new_headers_array->len > 0) {
1330                 gint msg_num = 0;
1331
1332                 /* Order by date */
1333                 g_ptr_array_sort (new_headers_array, (GCompareFunc) compare_headers_by_date);
1334
1335                 /* TODO: Ask the user, instead of just failing,
1336                  * showing mail_nc_msg_count_limit_exceeded, with 'Get
1337                  * all' and 'Newest only' buttons. */
1338                 if (new_headers_array->len > info->retrieve_limit) {
1339                         /* TODO */
1340                 }
1341
1342                 /* Should be get only the headers or the message as well? */
1343                 if (g_ascii_strcasecmp (info->retrieve_type, 
1344                                         MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY) != 0) {     
1345                         priv->done = 0;
1346                         priv->total = MIN (new_headers_array->len, info->retrieve_limit);
1347                         while (msg_num < priv->total) {
1348
1349                                 TnyHeader *header = TNY_HEADER (g_ptr_array_index (new_headers_array, msg_num));
1350                                 TnyFolder *folder = tny_header_get_folder (header);
1351                                 TnyMsg *msg       = tny_folder_get_msg (folder, header, NULL);
1352                                 ModestMailOperationState *state;
1353                                 ModestPair* pair;
1354
1355                                 priv->done++;
1356                                 /* We can not just use the mail operation because the
1357                                    values of done and total could change before the
1358                                    idle is called */
1359                                 state = modest_mail_operation_clone_state (info->mail_op);
1360                                 pair = modest_pair_new (g_object_ref (info->mail_op), state, FALSE);
1361                                 g_idle_add_full (G_PRIORITY_HIGH_IDLE, idle_notify_progress_once,
1362                                                  pair, (GDestroyNotify) modest_pair_free);
1363
1364                                 g_object_unref (msg);
1365                                 g_object_unref (folder);
1366
1367                                 msg_num++;
1368                         }
1369                 }
1370         }
1371
1372         if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
1373                 goto out;
1374
1375         /* Copy the headers to a list and free the array */
1376         new_headers = tny_simple_list_new ();
1377         for (i=0; i < new_headers_array->len; i++) {
1378                 TnyHeader *header = TNY_HEADER (g_ptr_array_index (new_headers_array, i));
1379                 tny_list_append (new_headers, G_OBJECT (header));
1380         }
1381         g_ptr_array_foreach (new_headers_array, (GFunc) g_object_unref, NULL);
1382         g_ptr_array_free (new_headers_array, FALSE);
1383         
1384
1385         /* Perform send (if operation was not cancelled) */
1386         priv->done = 0;
1387         priv->total = 0;
1388         if (priv->account != NULL) 
1389                 g_object_unref (priv->account);
1390         priv->account = g_object_ref (info->transport_account);
1391         
1392         send_queue = modest_runtime_get_send_queue (info->transport_account);
1393         if (send_queue) {
1394                 modest_tny_send_queue_try_to_send (send_queue);
1395         } else {
1396                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1397                              MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
1398                              "cannot create a send queue for %s\n", 
1399                              tny_account_get_name (TNY_ACCOUNT (info->transport_account)));
1400                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1401         }
1402         
1403         /* Check if the operation was a success */
1404         if (!priv->error) {
1405                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1406
1407                 /* Update the last updated key */
1408                 g_idle_add_full (G_PRIORITY_HIGH_IDLE, 
1409                                  set_last_updated_idle, 
1410                                  g_strdup (tny_account_get_id (TNY_ACCOUNT (info->account))),
1411                                  (GDestroyNotify) g_free);
1412         }
1413
1414  out:
1415
1416         if (info->callback) {
1417                 UpdateAccountInfo *idle_info;
1418
1419                 /* This thread is not in the main lock */
1420                 idle_info = g_malloc0 (sizeof (UpdateAccountInfo));
1421                 idle_info->mail_op = g_object_ref (info->mail_op);
1422                 idle_info->new_headers = (new_headers) ? g_object_ref (new_headers) : NULL;
1423                 idle_info->callback = info->callback;
1424                 idle_info->user_data = info->user_data;
1425                 g_idle_add (idle_update_account_cb, idle_info);
1426         }
1427
1428         /* Notify about operation end. Note that the info could be
1429            freed before this idle happens, but the mail operation will
1430            be still alive */
1431         g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
1432
1433         /* Frees */
1434         if (new_headers)
1435                 g_object_unref (new_headers);
1436         if (all_folders)
1437                 g_object_unref (all_folders);
1438         g_object_unref (info->account);
1439         g_object_unref (info->transport_account);
1440         g_free (info->retrieve_type);
1441         g_slice_free (UpdateAccountInfo, info);
1442
1443         first_time = FALSE;
1444
1445         return NULL;
1446 }
1447
1448 gboolean
1449 modest_mail_operation_update_account (ModestMailOperation *self,
1450                                       const gchar *account_name,
1451                                       UpdateAccountCallback callback,
1452                                       gpointer user_data)
1453 {
1454         GThread *thread = NULL;
1455         UpdateAccountInfo *info = NULL;
1456         ModestMailOperationPrivate *priv = NULL;
1457         ModestAccountMgr *mgr = NULL;
1458         TnyStoreAccount *store_account = NULL;
1459         TnyTransportAccount *transport_account = NULL;
1460
1461         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
1462         g_return_val_if_fail (account_name, FALSE);
1463
1464         /* Init mail operation. Set total and done to 0, and do not
1465            update them, this way the progress objects will know that
1466            we have no clue about the number of the objects */
1467         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1468         priv->total = 0;
1469         priv->done  = 0;
1470         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1471
1472         /* Get the store account */
1473         store_account = (TnyStoreAccount *)
1474                 modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
1475                                                                      account_name,
1476                                                                      TNY_ACCOUNT_TYPE_STORE);
1477                                                                      
1478         if (!store_account) {
1479                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1480                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1481                              "cannot get tny store account for %s\n", account_name);
1482                 goto error;
1483         }
1484
1485         
1486         /* Get the transport account, we can not do it in the thread
1487            due to some problems with dbus */
1488         transport_account = (TnyTransportAccount *)
1489                 modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
1490                                                                                     account_name);
1491         if (!transport_account) {
1492                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1493                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1494                              "cannot get tny transport account for %s\n", account_name);
1495                 goto error;
1496         }
1497
1498         /* Create the helper object */
1499         info = g_slice_new (UpdateAccountInfo);
1500         info->mail_op = self;
1501         info->account = store_account;
1502         info->transport_account = transport_account;
1503         info->callback = callback;
1504         info->user_data = user_data;
1505
1506         /* Get the message size limit */
1507         info->max_size  = modest_conf_get_int (modest_runtime_get_conf (), 
1508                                                MODEST_CONF_MSG_SIZE_LIMIT, NULL);
1509         if (info->max_size == 0)
1510                 info->max_size = G_MAXINT;
1511         else
1512                 info->max_size = info->max_size * KB;
1513
1514         /* Get per-account retrieval type */
1515         mgr = modest_runtime_get_account_mgr ();
1516         info->retrieve_type = modest_account_mgr_get_string (mgr, account_name, 
1517                                                              MODEST_ACCOUNT_RETRIEVE, FALSE);
1518
1519         /* Get per-account message amount retrieval limit */
1520         info->retrieve_limit = modest_account_mgr_get_int (mgr, account_name, 
1521                                                            MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE);
1522         if (info->retrieve_limit == 0)
1523                 info->retrieve_limit = G_MAXINT;
1524                 
1525         /* printf ("DEBUG: %s: info->retrieve_limit = %d\n", __FUNCTION__, info->retrieve_limit); */
1526
1527         /* Set account busy */
1528         modest_account_mgr_set_account_busy(mgr, account_name, TRUE);
1529         priv->account_name = g_strdup(account_name);
1530         
1531         thread = g_thread_create (update_account_thread, info, FALSE, NULL);
1532
1533         return TRUE;
1534
1535  error:
1536         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1537         if (callback) 
1538                 callback (self, 0, user_data);
1539         modest_mail_operation_notify_end (self);
1540         return FALSE;
1541 }
1542
1543 /* ******************************************************************* */
1544 /* ************************** STORE  ACTIONS ************************* */
1545 /* ******************************************************************* */
1546
1547
1548 TnyFolder *
1549 modest_mail_operation_create_folder (ModestMailOperation *self,
1550                                      TnyFolderStore *parent,
1551                                      const gchar *name)
1552 {
1553         ModestMailOperationPrivate *priv;
1554         TnyFolder *new_folder = NULL;
1555
1556         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
1557         g_return_val_if_fail (name, NULL);
1558         
1559         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1560
1561         /* Check for already existing folder */
1562         if (modest_tny_folder_has_subfolder_with_name (parent, name)) {
1563                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1564                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1565                              MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS,
1566                              _CS("ckdg_ib_folder_already_exists"));
1567         }
1568
1569         /* Check parent */
1570         if (TNY_IS_FOLDER (parent)) {
1571                 /* Check folder rules */
1572                 ModestTnyFolderRules rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
1573                 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
1574                         /* Set status failed and set an error */
1575                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1576                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1577                                      MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1578                                      _("mail_in_ui_folder_create_error"));
1579                 }
1580         }
1581
1582         if (!strcmp (name, " ") || strchr (name, '/')) {
1583                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1584                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1585                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1586                              _("mail_in_ui_folder_create_error"));
1587         }
1588
1589         if (!priv->error) {
1590                 /* Create the folder */
1591                 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
1592                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
1593                 if (!priv->error)
1594                         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1595         }
1596
1597         /* Notify about operation end */
1598         modest_mail_operation_notify_end (self);
1599
1600         return new_folder;
1601 }
1602
1603 void
1604 modest_mail_operation_remove_folder (ModestMailOperation *self,
1605                                      TnyFolder           *folder,
1606                                      gboolean             remove_to_trash)
1607 {
1608         TnyAccount *account;
1609         ModestMailOperationPrivate *priv;
1610         ModestTnyFolderRules rules;
1611
1612         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1613         g_return_if_fail (TNY_IS_FOLDER (folder));
1614         
1615         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1616         
1617         /* Check folder rules */
1618         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
1619         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
1620                 /* Set status failed and set an error */
1621                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1622                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1623                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1624                              _("mail_in_ui_folder_delete_error"));
1625                 goto end;
1626         }
1627
1628         /* Get the account */
1629         account = modest_tny_folder_get_account (folder);
1630         priv->account = g_object_ref(account);
1631
1632         /* Delete folder or move to trash */
1633         if (remove_to_trash) {
1634                 TnyFolder *trash_folder = NULL;
1635                 trash_folder = modest_tny_account_get_special_folder (account,
1636                                                                       TNY_FOLDER_TYPE_TRASH);
1637                 /* TODO: error_handling */
1638                 if (trash_folder) {
1639                         modest_mail_operation_xfer_folder (self, folder,
1640                                                     TNY_FOLDER_STORE (trash_folder), 
1641                                                     TRUE, NULL, NULL);
1642                         g_object_unref (trash_folder);
1643                 }
1644         } else {
1645                 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
1646
1647                 tny_folder_store_remove_folder (parent, folder, &(priv->error));
1648                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
1649
1650                 if (!priv->error)
1651                         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1652
1653                 if (parent)
1654                         g_object_unref (G_OBJECT (parent));
1655         }
1656         g_object_unref (G_OBJECT (account));
1657
1658  end:
1659         /* Notify about operation end */
1660         modest_mail_operation_notify_end (self);
1661 }
1662
1663 static void
1664 transfer_folder_status_cb (GObject *obj,
1665                            TnyStatus *status,
1666                            gpointer user_data)
1667 {
1668         ModestMailOperation *self;
1669         ModestMailOperationPrivate *priv;
1670         ModestMailOperationState *state;
1671         XFerMsgAsyncHelper *helper;
1672
1673         g_return_if_fail (status != NULL);
1674         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_COPY_FOLDER);
1675
1676         helper = (XFerMsgAsyncHelper *) user_data;
1677         g_return_if_fail (helper != NULL);
1678
1679         self = helper->mail_op;
1680         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1681
1682         priv->done = status->position;
1683         priv->total = status->of_total;
1684
1685         state = modest_mail_operation_clone_state (self);
1686
1687         /* This is not a GDK lock because we are a Tinymail callback
1688          * which is already GDK locked by Tinymail */
1689
1690         /* no gdk_threads_enter (), CHECKED */
1691
1692         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL); 
1693
1694         /* no gdk_threads_leave (), CHECKED */
1695
1696         g_slice_free (ModestMailOperationState, state);
1697 }
1698
1699
1700 static void
1701 transfer_folder_cb (TnyFolder *folder, 
1702                     gboolean cancelled, 
1703                     TnyFolderStore *into, 
1704                     TnyFolder *new_folder, 
1705                     GError *err, 
1706                     gpointer user_data)
1707 {
1708         XFerMsgAsyncHelper *helper;
1709         ModestMailOperation *self = NULL;
1710         ModestMailOperationPrivate *priv = NULL;
1711
1712         helper = (XFerMsgAsyncHelper *) user_data;
1713         g_return_if_fail (helper != NULL);       
1714
1715         self = helper->mail_op;
1716         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1717
1718         if (err) {
1719                 priv->error = g_error_copy (err);
1720                 priv->done = 0;
1721                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1722         } else if (cancelled) {
1723                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
1724                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1725                              MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
1726                              _("Transference of %s was cancelled."),
1727                              tny_folder_get_name (folder));
1728         } else {
1729                 priv->done = 1;
1730                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1731         }
1732                 
1733         /* Notify about operation end */
1734         modest_mail_operation_notify_end (self);
1735
1736         /* If user defined callback function was defined, call it */
1737         if (helper->user_callback) {
1738
1739                 /* This is not a GDK lock because we are a Tinymail callback
1740                  * which is already GDK locked by Tinymail */
1741
1742                 /* no gdk_threads_enter (), CHECKED */
1743                 helper->user_callback (priv->source, helper->user_data);
1744                 /* no gdk_threads_leave () , CHECKED */
1745         }
1746
1747         /* Free */
1748         g_object_unref (helper->mail_op);
1749         g_slice_free   (XFerMsgAsyncHelper, helper);
1750 }
1751
1752 /**
1753  *
1754  * This function checks if the new name is a valid name for our local
1755  * folders account. The new name could not be the same than then name
1756  * of any of the mandatory local folders
1757  *
1758  * We can not rely on tinymail because tinymail does not check the
1759  * name of the virtual folders that the account could have in the case
1760  * that we're doing a rename (because it directly calls Camel which
1761  * knows nothing about our virtual folders). 
1762  *
1763  * In the case of an actual copy/move (i.e. move/copy a folder between
1764  * accounts) tinymail uses the tny_folder_store_create_account which
1765  * is reimplemented by our ModestTnyLocalFoldersAccount that indeed
1766  * checks the new name of the folder, so this call in that case
1767  * wouldn't be needed. *But* NOTE that if tinymail changes its
1768  * implementation (if folder transfers within the same account is no
1769  * longer implemented as a rename) this call will allow Modest to work
1770  * perfectly
1771  *
1772  * If the new name is not valid, this function will set the status to
1773  * failed and will set also an error in the mail operation
1774  */
1775 static gboolean
1776 new_name_valid_if_local_account (ModestMailOperationPrivate *priv,
1777                                  TnyFolderStore *into,
1778                                  const gchar *new_name)
1779 {
1780         if (TNY_IS_ACCOUNT (into) && 
1781             modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (into)) &&
1782             modest_tny_local_folders_account_folder_name_in_use (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (into),
1783                                                                  new_name)) {
1784                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1785                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1786                              MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS,
1787                              _("ckdg_ib_folder_already_exists"));
1788                 return FALSE;
1789         } else
1790                 return TRUE;
1791 }
1792
1793 /**
1794  * This function checks if @ancestor is an acestor of @folder and
1795  * returns TRUE in that case
1796  */
1797 static gboolean
1798 folder_is_ancestor (TnyFolder *folder,
1799                     TnyFolderStore *ancestor)
1800 {
1801         TnyFolder *tmp = NULL;
1802         gboolean found = FALSE;
1803
1804         tmp = folder;
1805         while (!found && tmp && !TNY_IS_ACCOUNT (tmp)) {
1806                 TnyFolderStore *folder_store;
1807
1808                 folder_store = tny_folder_get_folder_store (tmp);
1809                 if (ancestor == folder_store)
1810                         found = TRUE;
1811                 else
1812                         tmp = g_object_ref (folder_store);
1813                 g_object_unref (folder_store);
1814         }
1815         return found;
1816 }
1817
1818 void
1819 modest_mail_operation_xfer_folder (ModestMailOperation *self,
1820                                    TnyFolder *folder,
1821                                    TnyFolderStore *parent,
1822                                    gboolean delete_original,
1823                                    XferMsgsAsynUserCallback user_callback,
1824                                    gpointer user_data)
1825 {
1826         ModestMailOperationPrivate *priv = NULL;
1827         ModestTnyFolderRules parent_rules = 0, rules; 
1828         XFerMsgAsyncHelper *helper = NULL;
1829         const gchar *folder_name = NULL;
1830         const gchar *error_msg;
1831
1832         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1833         g_return_if_fail (TNY_IS_FOLDER (folder));
1834         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
1835
1836         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1837         folder_name = tny_folder_get_name (folder);
1838
1839         /* Set the error msg */
1840         error_msg = _("mail_in_ui_folder_move_target_error");
1841
1842         /* Get account and set it into mail_operation */
1843         priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
1844         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1845
1846         /* Get folder rules */
1847         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
1848         if (TNY_IS_FOLDER (parent))
1849                 parent_rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
1850         
1851         /* The moveable restriction is applied also to copy operation */
1852         if ((gpointer) parent == (gpointer) folder ||
1853             (!TNY_IS_FOLDER_STORE (parent)) || 
1854             (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE)) {
1855
1856                 /* Set status failed and set an error */
1857                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1858                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1859                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1860                              error_msg);
1861
1862                 /* Notify the queue */
1863                 modest_mail_operation_notify_end (self);
1864
1865         } else if (TNY_IS_FOLDER (parent) && 
1866                    (parent_rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)) {
1867
1868                 /* Set status failed and set an error */
1869                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1870                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1871                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1872                              error_msg);
1873
1874                 /* Notify the queue */
1875                 modest_mail_operation_notify_end (self);
1876
1877         } else if (TNY_IS_FOLDER (parent) &&
1878                    TNY_IS_FOLDER_STORE (folder) &&
1879                    folder_is_ancestor (TNY_FOLDER (parent), TNY_FOLDER_STORE (folder))) {
1880                 /* Set status failed and set an error */
1881                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1882                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1883                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1884                              error_msg);
1885
1886                 /* Notify the queue */
1887                 modest_mail_operation_notify_end (self);
1888
1889         } else if (TNY_IS_FOLDER_STORE (parent) &&
1890                    modest_tny_folder_has_subfolder_with_name (parent, folder_name)) {
1891                 /* Check that the new folder name is not used by any
1892                     parent subfolder */
1893
1894                 /* Set status failed and set an error */
1895                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1896                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1897                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1898                              error_msg);
1899
1900                 /* Notify the queue */
1901                 modest_mail_operation_notify_end (self);
1902                 
1903         } else if (!(new_name_valid_if_local_account (priv, parent, folder_name))) {
1904                 /* Check that the new folder name is not used by any
1905                    special local folder */
1906
1907                 /* Set status failed and set an error */
1908                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1909                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1910                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1911                              error_msg);
1912
1913                 /* Notify the queue */
1914                 modest_mail_operation_notify_end (self);
1915         } else {
1916                 /* Create the helper */
1917                 helper = g_slice_new0 (XFerMsgAsyncHelper);
1918                 helper->mail_op = g_object_ref(self);
1919                 helper->dest_folder = NULL;
1920                 helper->headers = NULL;
1921                 helper->user_callback = user_callback;
1922                 helper->user_data = user_data;
1923                 
1924                 /* Move/Copy folder */          
1925                 tny_folder_copy_async (folder,
1926                                        parent,
1927                                        tny_folder_get_name (folder),
1928                                        delete_original,
1929                                        transfer_folder_cb,
1930                                        transfer_folder_status_cb,
1931                                        helper);         
1932         } 
1933         
1934 }
1935
1936 void
1937 modest_mail_operation_rename_folder (ModestMailOperation *self,
1938                                      TnyFolder *folder,
1939                                      const gchar *name)
1940 {
1941         ModestMailOperationPrivate *priv;
1942         ModestTnyFolderRules rules;
1943         XFerMsgAsyncHelper *helper;
1944
1945         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1946         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
1947         g_return_if_fail (name);
1948         
1949         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1950
1951         /* Get account and set it into mail_operation */
1952         priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
1953
1954         /* Check folder rules */
1955         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
1956         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
1957                 /* Set status failed and set an error */
1958                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1959                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1960                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1961                              _("FIXME: unable to rename"));
1962
1963                 /* Notify about operation end */
1964                 modest_mail_operation_notify_end (self);
1965         } else if (!strcmp (name, " ") || strchr (name, '/')) {
1966                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1967                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1968                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1969                              _("FIXME: unable to rename"));
1970                 /* Notify about operation end */
1971                 modest_mail_operation_notify_end (self);
1972         } else {
1973                 TnyFolderStore *into;
1974
1975                 into = tny_folder_get_folder_store (folder);    
1976
1977                 /* Check that the new folder name is not used by any
1978                    special local folder */
1979                 if (new_name_valid_if_local_account (priv, into, name)) {
1980                         /* Create the helper */
1981                         helper = g_slice_new0 (XFerMsgAsyncHelper);
1982                         helper->mail_op = g_object_ref(self);
1983                         helper->dest_folder = NULL;
1984                         helper->headers = NULL;
1985                         helper->user_callback = NULL;
1986                         helper->user_data = NULL;
1987                 
1988                         /* Rename. Camel handles folder subscription/unsubscription */
1989                         tny_folder_copy_async (folder, into, name, TRUE,
1990                                                transfer_folder_cb,
1991                                                transfer_folder_status_cb,
1992                                                helper);
1993                 } else {
1994                         modest_mail_operation_notify_end (self);
1995                 }
1996                 g_object_unref (into);
1997         }
1998 }
1999
2000 /* ******************************************************************* */
2001 /* **************************  MSG  ACTIONS  ************************* */
2002 /* ******************************************************************* */
2003
2004 void modest_mail_operation_get_msg (ModestMailOperation *self,
2005                                     TnyHeader *header,
2006                                     GetMsgAsyncUserCallback user_callback,
2007                                     gpointer user_data)
2008 {
2009         GetMsgAsyncHelper *helper = NULL;
2010         TnyFolder *folder;
2011         ModestMailOperationPrivate *priv;
2012         
2013         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2014         g_return_if_fail (TNY_IS_HEADER (header));
2015         
2016         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2017         folder = tny_header_get_folder (header);
2018
2019         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2020
2021         /* Get message from folder */
2022         if (folder) {
2023                 /* Get account and set it into mail_operation */
2024                 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2025                 
2026                 /* Check for cached messages */
2027                 if (tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED)
2028                         priv->op_type = MODEST_MAIL_OPERATION_TYPE_OPEN;
2029                 else 
2030                         priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2031
2032                 helper = g_slice_new0 (GetMsgAsyncHelper);
2033                 helper->mail_op = self;
2034                 helper->user_callback = user_callback;
2035                 helper->user_data = user_data;
2036                 helper->header = g_object_ref (header);
2037
2038                 // The callback's reference so that the mail op is not
2039                 // finalized until the async operation is completed even if
2040                 // the user canceled the request meanwhile.
2041                 g_object_ref (G_OBJECT (helper->mail_op));
2042
2043                 tny_folder_get_msg_async (folder, header, get_msg_cb, get_msg_status_cb, helper);
2044
2045                 g_object_unref (G_OBJECT (folder));
2046         } else {
2047                 /* Set status failed and set an error */
2048                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2049                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2050                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2051                              _("Error trying to get a message. No folder found for header"));
2052
2053                 /* Notify the queue */
2054                 modest_mail_operation_notify_end (self);
2055         }
2056 }
2057
2058 static void
2059 get_msg_cb (TnyFolder *folder, 
2060             gboolean cancelled, 
2061             TnyMsg *msg, 
2062             GError *error, 
2063             gpointer user_data)
2064 {
2065         GetMsgAsyncHelper *helper = NULL;
2066         ModestMailOperation *self = NULL;
2067         ModestMailOperationPrivate *priv = NULL;
2068
2069         helper = (GetMsgAsyncHelper *) user_data;
2070         g_return_if_fail (helper != NULL);       
2071         self = helper->mail_op;
2072         g_return_if_fail (MODEST_IS_MAIL_OPERATION(self));
2073         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2074
2075         /* Check errors and cancel */
2076         if (error) {
2077                 priv->error = g_error_copy (error);
2078                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2079         } else if (cancelled) {
2080                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
2081                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2082                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2083                              _("Error trying to refresh the contents of %s"),
2084                              tny_folder_get_name (folder));
2085         } else {
2086                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2087         }
2088
2089         /* If user defined callback function was defined, call it even
2090            if the operation failed*/
2091         if (helper->user_callback) {
2092                 /* This is not a GDK lock because we are a Tinymail callback
2093                  * which is already GDK locked by Tinymail */
2094
2095                 /* no gdk_threads_enter (), CHECKED */
2096                 helper->user_callback (self, helper->header, msg, helper->user_data);
2097                 /* no gdk_threads_leave (), CHECKED */
2098         }
2099
2100         /* Notify about operation end */
2101         modest_mail_operation_notify_end (self);
2102         /* Free */
2103         g_object_unref (helper->mail_op);
2104         g_object_unref (helper->header);
2105         g_slice_free (GetMsgAsyncHelper, helper);
2106                 
2107 }
2108
2109 static void     
2110 get_msg_status_cb (GObject *obj,
2111                    TnyStatus *status,  
2112                    gpointer user_data)
2113 {
2114         GetMsgAsyncHelper *helper = NULL;
2115         ModestMailOperation *self;
2116         ModestMailOperationPrivate *priv;
2117         ModestMailOperationState *state;
2118
2119         g_return_if_fail (status != NULL);
2120         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_GET_MSG);
2121
2122         helper = (GetMsgAsyncHelper *) user_data;
2123         g_return_if_fail (helper != NULL);       
2124
2125         self = helper->mail_op;
2126         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2127
2128         priv->done = 1;
2129         priv->total = 1;
2130
2131         state = modest_mail_operation_clone_state (self);
2132         state->bytes_done = status->position;
2133         state->bytes_total = status->of_total;
2134
2135         /* This is not a GDK lock because we are a Tinymail callback
2136          * which is already GDK locked by Tinymail */
2137
2138         /* no gdk_threads_enter (), CHECKED */
2139         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
2140         /* no gdk_threads_leave (), CHECKED */
2141
2142         g_slice_free (ModestMailOperationState, state);
2143 }
2144
2145 /****************************************************/
2146 typedef struct {
2147         ModestMailOperation *mail_op;
2148         TnyList *headers;
2149         GetMsgAsyncUserCallback user_callback;
2150         gpointer user_data;
2151         GDestroyNotify notify;
2152 } GetFullMsgsInfo;
2153
2154 typedef struct {
2155         GetMsgAsyncUserCallback user_callback;
2156         TnyHeader *header;
2157         TnyMsg *msg;
2158         gpointer user_data;
2159         ModestMailOperation *mail_op;
2160 } NotifyGetMsgsInfo;
2161
2162
2163 /* 
2164  * Used by get_msgs_full_thread to call the user_callback for each
2165  * message that has been read
2166  */
2167 static gboolean
2168 notify_get_msgs_full (gpointer data)
2169 {
2170         NotifyGetMsgsInfo *info;
2171
2172         info = (NotifyGetMsgsInfo *) data;      
2173
2174         /* This is a GDK lock because we are an idle callback and
2175          * because info->user_callback can contain Gtk+ code */
2176
2177         gdk_threads_enter (); /* CHECKED */
2178         info->user_callback (info->mail_op, info->header, info->msg, info->user_data);
2179         gdk_threads_leave (); /* CHECKED */
2180
2181         g_slice_free (NotifyGetMsgsInfo, info);
2182
2183         return FALSE;
2184 }
2185
2186 /* 
2187  * Used by get_msgs_full_thread to free al the thread resources and to
2188  * call the destroy function for the passed user_data
2189  */
2190 static gboolean
2191 get_msgs_full_destroyer (gpointer data)
2192 {
2193         GetFullMsgsInfo *info;
2194
2195         info = (GetFullMsgsInfo *) data;
2196
2197         if (info->notify) {
2198
2199                 /* This is a GDK lock because we are an idle callback and
2200                  * because info->notify can contain Gtk+ code */
2201
2202                 gdk_threads_enter (); /* CHECKED */
2203                 info->notify (info->user_data);
2204                 gdk_threads_leave (); /* CHECKED */
2205         }
2206
2207         /* free */
2208         g_object_unref (info->headers);
2209         g_slice_free (GetFullMsgsInfo, info);
2210
2211         return FALSE;
2212 }
2213
2214 static gpointer
2215 get_msgs_full_thread (gpointer thr_user_data)
2216 {
2217         GetFullMsgsInfo *info;
2218         ModestMailOperationPrivate *priv = NULL;
2219         TnyIterator *iter = NULL;
2220         
2221         info = (GetFullMsgsInfo *) thr_user_data;       
2222         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
2223
2224         iter = tny_list_create_iterator (info->headers);
2225         while (!tny_iterator_is_done (iter)) { 
2226                 TnyHeader *header;
2227                 TnyFolder *folder;
2228                 
2229                 header = TNY_HEADER (tny_iterator_get_current (iter));
2230                 folder = tny_header_get_folder (header);
2231                                 
2232                 /* Check for cached messages */
2233                 if (tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED)
2234                         priv->op_type = MODEST_MAIL_OPERATION_TYPE_OPEN;
2235                 else 
2236                         priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2237
2238                 /* Get message from folder */
2239                 if (folder) {
2240                         TnyMsg *msg;
2241                         /* The callback will call it per each header */
2242                         msg = tny_folder_get_msg (folder, header, &(priv->error));
2243
2244                         if (msg) {
2245                                 ModestMailOperationState *state;
2246                                 ModestPair *pair;
2247
2248                                 priv->done++;
2249
2250                                 /* notify progress */
2251                                 state = modest_mail_operation_clone_state (info->mail_op);
2252                                 pair = modest_pair_new (g_object_ref (info->mail_op), state, FALSE);
2253                                 g_idle_add_full (G_PRIORITY_HIGH_IDLE, idle_notify_progress_once,
2254                                                  pair, (GDestroyNotify) modest_pair_free);
2255
2256                                 /* The callback is the responsible for
2257                                    freeing the message */
2258                                 if (info->user_callback) {
2259                                         NotifyGetMsgsInfo *info_notify;
2260                                         info_notify = g_slice_new0 (NotifyGetMsgsInfo);
2261                                         info_notify->user_callback = info->user_callback;
2262                                         info_notify->mail_op = info->mail_op;
2263                                         info_notify->header = g_object_ref (header);
2264                                         info_notify->msg = g_object_ref (msg);
2265                                         info_notify->user_data = info->user_data;
2266                                         g_idle_add_full (G_PRIORITY_HIGH_IDLE,
2267                                                          notify_get_msgs_full, 
2268                                                          info_notify, NULL);
2269                                 }
2270                                 g_object_unref (msg);
2271                         } 
2272                 } else {
2273                         /* Set status failed and set an error */
2274                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2275                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2276                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2277                                      "Error trying to get a message. No folder found for header");
2278                 }
2279
2280                 if (header)
2281                         g_object_unref (header);
2282                 
2283                 tny_iterator_next (iter);
2284         }
2285
2286         /* Set operation status */
2287         if (priv->status == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS)
2288                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2289
2290         /* Notify about operation end */
2291         g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
2292
2293         /* Free thread resources. Will be called after all previous idles */
2294         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1, get_msgs_full_destroyer, info, NULL);
2295
2296         return NULL;
2297 }
2298
2299 void 
2300 modest_mail_operation_get_msgs_full (ModestMailOperation *self,
2301                                      TnyList *header_list, 
2302                                      GetMsgAsyncUserCallback user_callback,
2303                                      gpointer user_data,
2304                                      GDestroyNotify notify)
2305 {
2306         TnyHeader *header = NULL;
2307         TnyFolder *folder = NULL;
2308         GThread *thread;
2309         ModestMailOperationPrivate *priv = NULL;
2310         GetFullMsgsInfo *info = NULL;
2311         gboolean size_ok = TRUE;
2312         gint max_size;
2313         TnyIterator *iter = NULL;
2314         
2315         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2316         
2317         /* Init mail operation */
2318         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2319         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2320         priv->done = 0;
2321         priv->total = tny_list_get_length(header_list);
2322
2323         /* Get account and set it into mail_operation */
2324         if (tny_list_get_length (header_list) >= 1) {
2325                 iter = tny_list_create_iterator (header_list);
2326                 header = TNY_HEADER (tny_iterator_get_current (iter));
2327                 if (header) {
2328                         folder = tny_header_get_folder (header);
2329                         if (folder) {           
2330                                 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2331
2332                                 g_object_unref (folder);
2333                         }
2334
2335                         g_object_unref (header);
2336                 }
2337
2338                 if (tny_list_get_length (header_list) == 1) {
2339                         g_object_unref (iter);
2340                         iter = NULL;
2341                 }
2342         }
2343
2344         /* Get msg size limit */
2345         max_size  = modest_conf_get_int (modest_runtime_get_conf (), 
2346                                          MODEST_CONF_MSG_SIZE_LIMIT, 
2347                                          &(priv->error));
2348         if (priv->error) {
2349                 g_clear_error (&(priv->error));
2350                 max_size = G_MAXINT;
2351         } else {
2352                 max_size = max_size * KB;
2353         }
2354
2355         /* Check message size limits. If there is only one message
2356            always retrieve it */
2357         if (iter != NULL) {
2358                 while (!tny_iterator_is_done (iter) && size_ok) {
2359                         header = TNY_HEADER (tny_iterator_get_current (iter));
2360                         if (header) {
2361                                 if (tny_header_get_message_size (header) >= max_size)
2362                                         size_ok = FALSE;
2363                                 g_object_unref (header);
2364                         }
2365
2366                         tny_iterator_next (iter);
2367                 }
2368                 g_object_unref (iter);
2369         }
2370
2371         if (size_ok) {
2372                 /* Create the info */
2373                 info = g_slice_new0 (GetFullMsgsInfo);
2374                 info->mail_op = self;
2375                 info->user_callback = user_callback;
2376                 info->user_data = user_data;
2377                 info->headers = g_object_ref (header_list);
2378                 info->notify = notify;
2379
2380                 thread = g_thread_create (get_msgs_full_thread, info, FALSE, NULL);
2381         } else {
2382                 /* Set status failed and set an error */
2383                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2384                 /* FIXME: the error msg is different for pop */
2385                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2386                              MODEST_MAIL_OPERATION_ERROR_MESSAGE_SIZE_LIMIT,
2387                              _("emev_ni_ui_imap_msg_size_exceed_error"));
2388                 /* Remove from queue and free resources */
2389                 modest_mail_operation_notify_end (self);
2390                 if (notify)
2391                         notify (user_data);
2392         }
2393 }
2394
2395
2396 void 
2397 modest_mail_operation_remove_msg (ModestMailOperation *self,  
2398                                   TnyHeader *header,
2399                                   gboolean remove_to_trash /*ignored*/)
2400 {
2401         TnyFolder *folder;
2402         ModestMailOperationPrivate *priv;
2403
2404         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2405         g_return_if_fail (TNY_IS_HEADER (header));
2406
2407         if (remove_to_trash)
2408                 g_warning ("remove to trash is not implemented");
2409
2410         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2411         folder = tny_header_get_folder (header);
2412
2413         /* Get account and set it into mail_operation */
2414         priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2415
2416         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2417
2418         /* remove message from folder */
2419         tny_folder_remove_msg (folder, header, &(priv->error));
2420         if (!priv->error) {
2421                 tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
2422                 tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
2423
2424                 if (TNY_IS_CAMEL_IMAP_FOLDER (folder))
2425 /*                      tny_folder_sync_async(folder, FALSE, NULL, NULL, NULL); /\* FALSE --> don't expunge *\/ */
2426                         tny_folder_sync (folder, FALSE, &(priv->error)); /* FALSE --> don't expunge */
2427                 else if (TNY_IS_CAMEL_POP_FOLDER (folder))
2428 /*                      tny_folder_sync_async(folder, FALSE, NULL, NULL, NULL); /\* TRUE --> dont expunge *\/ */
2429                         tny_folder_sync (folder, TRUE, &(priv->error)); /* TRUE --> expunge */
2430                 else
2431                         /* local folders */
2432 /*                      tny_folder_sync_async(folder, TRUE, NULL, NULL, NULL); /\* TRUE --> expunge *\/ */
2433                         tny_folder_sync (folder, TRUE, &(priv->error)); /* TRUE --> expunge */
2434         }
2435         
2436         
2437         /* Set status */
2438         if (!priv->error)
2439                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2440         else
2441                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2442
2443         /* Free */
2444         g_object_unref (G_OBJECT (folder));
2445
2446         /* Notify about operation end */
2447         modest_mail_operation_notify_end (self);
2448 }
2449
2450 void 
2451 modest_mail_operation_remove_msgs (ModestMailOperation *self,  
2452                                    TnyList *headers,
2453                                   gboolean remove_to_trash /*ignored*/)
2454 {
2455         TnyFolder *folder;
2456         ModestMailOperationPrivate *priv;
2457         TnyIterator *iter = NULL;
2458         TnyHeader *header = NULL;
2459
2460         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2461         g_return_if_fail (TNY_IS_LIST (headers));
2462
2463         if (remove_to_trash)
2464                 g_warning ("remove to trash is not implemented");
2465
2466         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2467
2468         /* Get folder from first header and sync it */
2469         iter = tny_list_create_iterator (headers);
2470         header = TNY_HEADER (tny_iterator_get_current (iter));
2471         folder = tny_header_get_folder (header);
2472         
2473         /* Get account and set it into mail_operation */
2474         priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2475
2476         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2477
2478         /* remove message from folder */
2479         tny_folder_remove_msgs (folder, headers, &(priv->error));
2480         if (!priv->error) {
2481                 if (TNY_IS_CAMEL_IMAP_FOLDER (folder) || 
2482                     TNY_IS_CAMEL_POP_FOLDER (folder))
2483                         tny_folder_sync_async(folder, FALSE, NULL, NULL, NULL); /* FALSE --> don't expunge */ 
2484                 else
2485                         /* local folders */
2486                         tny_folder_sync_async(folder, TRUE, NULL, NULL, NULL); /* TRUE --> expunge */
2487         }
2488         
2489         
2490         /* Set status */
2491         if (!priv->error)
2492                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2493         else
2494                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2495
2496         /* Free */
2497         g_object_unref (header);
2498         g_object_unref (iter);
2499         g_object_unref (G_OBJECT (folder));
2500
2501         /* Notify about operation end */
2502         modest_mail_operation_notify_end (self);
2503 }
2504
2505
2506 static void
2507 transfer_msgs_status_cb (GObject *obj,
2508                          TnyStatus *status,  
2509                          gpointer user_data)
2510 {
2511         XFerMsgAsyncHelper *helper = NULL;
2512         ModestMailOperation *self;
2513         ModestMailOperationPrivate *priv;
2514         ModestMailOperationState *state;
2515
2516
2517         g_return_if_fail (status != NULL);
2518         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_XFER_MSGS);
2519
2520         helper = (XFerMsgAsyncHelper *) user_data;
2521         g_return_if_fail (helper != NULL);       
2522
2523         self = helper->mail_op;
2524         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2525
2526         priv->done = status->position;
2527         priv->total = status->of_total;
2528
2529         state = modest_mail_operation_clone_state (self);
2530
2531         /* This is not a GDK lock because we are a Tinymail callback and
2532          * Tinymail already acquires the Gdk lock */
2533
2534         /* no gdk_threads_enter (), CHECKED */
2535
2536         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
2537
2538         /* no gdk_threads_leave (), CHECKED */
2539
2540         g_slice_free (ModestMailOperationState, state);
2541 }
2542
2543
2544 static void
2545 transfer_msgs_cb (TnyFolder *folder, gboolean cancelled, GError *err, gpointer user_data)
2546 {
2547         XFerMsgAsyncHelper *helper;
2548         ModestMailOperation *self;
2549         ModestMailOperationPrivate *priv;
2550         TnyIterator *iter = NULL;
2551         TnyHeader *header = NULL;
2552
2553         helper = (XFerMsgAsyncHelper *) user_data;
2554         self = helper->mail_op;
2555
2556         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2557
2558         if (err) {
2559                 priv->error = g_error_copy (err);
2560                 priv->done = 0;
2561                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;     
2562         } else if (cancelled) {
2563                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
2564                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2565                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2566                              _("Error trying to refresh the contents of %s"),
2567                              tny_folder_get_name (folder));
2568         } else {
2569                 priv->done = 1;
2570                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2571
2572                 /* Update folder counts */
2573                 tny_folder_poke_status (folder);                
2574                 tny_folder_poke_status (helper->dest_folder);           
2575         }
2576
2577         
2578         /* Mark headers as deleted and seen */
2579         if ((helper->delete) && 
2580             (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS)) {
2581                 iter = tny_list_create_iterator (helper->headers);
2582                 while (!tny_iterator_is_done (iter)) {
2583                         header = TNY_HEADER (tny_iterator_get_current (iter));
2584                         tny_header_set_flags (header, TNY_HEADER_FLAG_DELETED);
2585                         tny_header_set_flags (header, TNY_HEADER_FLAG_SEEN);
2586                         g_object_unref (header);
2587
2588                         tny_iterator_next (iter);
2589                 }
2590
2591         }
2592                 
2593
2594         /* Notify about operation end */
2595         modest_mail_operation_notify_end (self);
2596
2597         /* If user defined callback function was defined, call it */
2598         if (helper->user_callback) {
2599                 /* This is not a GDK lock because we are a Tinymail callback and
2600                  * Tinymail already acquires the Gdk lock */
2601
2602                 /* no gdk_threads_enter (), CHECKED */
2603                 helper->user_callback (priv->source, helper->user_data);
2604                 /* no gdk_threads_leave (), CHECKED */
2605         }
2606
2607         /* Free */
2608         if (helper->headers)
2609                 g_object_unref (helper->headers);
2610         if (helper->dest_folder)
2611                 g_object_unref (helper->dest_folder);
2612         if (helper->mail_op)
2613                 g_object_unref (helper->mail_op);
2614         if (folder)
2615                 g_object_unref (folder);
2616         if (iter)
2617                 g_object_unref (iter);
2618         g_slice_free   (XFerMsgAsyncHelper, helper);
2619
2620 }
2621
2622 void
2623 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
2624                                  TnyList *headers, 
2625                                  TnyFolder *folder, 
2626                                  gboolean delete_original,
2627                                  XferMsgsAsynUserCallback user_callback,
2628                                  gpointer user_data)
2629 {
2630         ModestMailOperationPrivate *priv = NULL;
2631         TnyIterator *iter = NULL;
2632         TnyFolder *src_folder = NULL;
2633         XFerMsgAsyncHelper *helper = NULL;
2634         TnyHeader *header = NULL;
2635         ModestTnyFolderRules rules = 0;
2636         const gchar *id1 = NULL;
2637         const gchar *id2 = NULL;
2638         gboolean same_folder = FALSE;
2639
2640         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2641         g_return_if_fail (TNY_IS_LIST (headers));
2642         g_return_if_fail (TNY_IS_FOLDER (folder));
2643
2644         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2645         priv->total = 1;
2646         priv->done = 0;
2647         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2648
2649         /* Apply folder rules */
2650         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
2651         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
2652                 /* Set status failed and set an error */
2653                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2654                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2655                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
2656                              _CS("ckct_ib_unable_to_paste_here"));
2657                 /* Notify the queue */
2658                 modest_mail_operation_notify_end (self);
2659                 return;
2660         }
2661                 
2662         /* Get source folder */
2663         iter = tny_list_create_iterator (headers);
2664         header = TNY_HEADER (tny_iterator_get_current (iter));
2665         if (header) {
2666                 src_folder = tny_header_get_folder (header);
2667                 g_object_unref (header);
2668         }
2669
2670         g_object_unref (iter);
2671
2672         /* Check folder source and destination */
2673         id1 = tny_folder_get_id (src_folder);
2674         id2 = tny_folder_get_id (TNY_FOLDER(folder));
2675         same_folder = !g_ascii_strcasecmp (id1, id2);
2676         if (same_folder) {
2677                 /* Set status failed and set an error */
2678                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2679                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2680                              MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
2681                              _("mcen_ib_unable_to_copy_samefolder"));
2682                 
2683                 /* Notify the queue */
2684                 modest_mail_operation_notify_end (self);
2685                 
2686                 /* Free */
2687                 g_object_unref (src_folder);            
2688                 return;
2689         }
2690
2691         /* Create the helper */
2692         helper = g_slice_new0 (XFerMsgAsyncHelper);
2693         helper->mail_op = g_object_ref(self);
2694         helper->dest_folder = g_object_ref(folder);
2695         helper->headers = g_object_ref(headers);
2696         helper->user_callback = user_callback;
2697         helper->user_data = user_data;
2698         helper->delete = delete_original;
2699
2700         /* Get account and set it into mail_operation */
2701         priv->account = modest_tny_folder_get_account (src_folder);
2702
2703         /* Transfer messages */
2704         tny_folder_transfer_msgs_async (src_folder, 
2705                                         headers, 
2706                                         folder, 
2707                                         delete_original, 
2708                                         transfer_msgs_cb, 
2709                                         transfer_msgs_status_cb,
2710                                         helper);
2711 }
2712
2713
2714 static void
2715 on_refresh_folder (TnyFolder   *folder, 
2716                    gboolean     cancelled, 
2717                    GError     *error,
2718                    gpointer     user_data)
2719 {
2720         RefreshAsyncHelper *helper = NULL;
2721         ModestMailOperation *self = NULL;
2722         ModestMailOperationPrivate *priv = NULL;
2723
2724         helper = (RefreshAsyncHelper *) user_data;
2725         self = helper->mail_op;
2726         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2727
2728         g_return_if_fail(priv!=NULL);
2729
2730         if (error) {
2731                 priv->error = g_error_copy (error);
2732                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2733                 printf("DEBUG: %s: Operation error:\n %s", __FUNCTION__,
2734                        error->message);
2735                 goto out;
2736         }
2737
2738         if (cancelled) {
2739                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
2740                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2741                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2742                              _("Error trying to refresh the contents of %s"),
2743                              tny_folder_get_name (folder));
2744                 printf("DEBUG: %s: Operation cancelled.\n", __FUNCTION__);
2745                 goto out;
2746         }
2747
2748         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2749
2750         /* Call user defined callback, if it exists */
2751         if (helper->user_callback) {
2752
2753                 /* This is not a GDK lock because we are a Tinymail callback and
2754                  * Tinymail already acquires the Gdk lock */
2755                 helper->user_callback (self, folder, helper->user_data);
2756         }
2757
2758  out:
2759         /* Free */
2760         g_slice_free   (RefreshAsyncHelper, helper);
2761
2762         /* Notify about operation end */
2763         modest_mail_operation_notify_end (self);
2764         g_object_unref(self);
2765 }
2766
2767 static void
2768 on_refresh_folder_status_update (GObject *obj,
2769                                  TnyStatus *status,
2770                                  gpointer user_data)
2771 {
2772         RefreshAsyncHelper *helper = NULL;
2773         ModestMailOperation *self = NULL;
2774         ModestMailOperationPrivate *priv = NULL;
2775         ModestMailOperationState *state;
2776
2777         g_return_if_fail (user_data != NULL);
2778         g_return_if_fail (status != NULL);
2779         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
2780
2781         helper = (RefreshAsyncHelper *) user_data;
2782         self = helper->mail_op;
2783         g_return_if_fail (MODEST_IS_MAIL_OPERATION(self));
2784
2785         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2786
2787         priv->done = status->position;
2788         priv->total = status->of_total;
2789
2790         state = modest_mail_operation_clone_state (self);
2791
2792         /* This is not a GDK lock because we are a Tinymail callback and
2793          * Tinymail already acquires the Gdk lock */
2794         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
2795
2796         g_slice_free (ModestMailOperationState, state);
2797 }
2798
2799 void 
2800 modest_mail_operation_refresh_folder  (ModestMailOperation *self,
2801                                        TnyFolder *folder,
2802                                        RefreshAsyncUserCallback user_callback,
2803                                        gpointer user_data)
2804 {
2805         ModestMailOperationPrivate *priv = NULL;
2806         RefreshAsyncHelper *helper = NULL;
2807
2808         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2809
2810         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2811
2812         /* Get account and set it into mail_operation */
2813         priv->account = modest_tny_folder_get_account  (folder);
2814
2815         /* Create the helper */
2816         helper = g_slice_new0 (RefreshAsyncHelper);
2817         helper->mail_op = g_object_ref(self);
2818         helper->user_callback = user_callback;
2819         helper->user_data = user_data;
2820
2821         /* Refresh the folder. TODO: tinymail could issue a status
2822            updates before the callback call then this could happen. We
2823            must review the design */
2824         tny_folder_refresh_async (folder,
2825                                   on_refresh_folder,
2826                                   on_refresh_folder_status_update,
2827                                   helper);
2828 }
2829
2830 /**
2831  *
2832  * It's used by the mail operation queue to notify the observers
2833  * attached to that signal that the operation finished. We need to use
2834  * that because tinymail does not give us the progress of a given
2835  * operation when it finishes (it directly calls the operation
2836  * callback).
2837  */
2838 static void
2839 modest_mail_operation_notify_end (ModestMailOperation *self)
2840 {
2841         ModestMailOperationState *state;
2842         ModestMailOperationPrivate *priv = NULL;
2843
2844         g_return_if_fail (self);
2845
2846         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2847
2848         /* Set the account back to not busy */
2849         if (priv->account_name) {
2850                 modest_account_mgr_set_account_busy (modest_runtime_get_account_mgr(), 
2851                                                      priv->account_name, FALSE);
2852                 g_free(priv->account_name);
2853                 priv->account_name = NULL;
2854         }
2855         
2856         /* Notify the observers about the mail operation end */
2857         /* We do not wrapp this emission because we assume that this
2858            function is always called from within the main lock */
2859         state = modest_mail_operation_clone_state (self);
2860         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
2861         g_slice_free (ModestMailOperationState, state);
2862 }