* src/modest-address-book.h:
[modest] / src / modest-mail-operation.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
32 #include <string.h>
33 #include <stdarg.h>
34 #include <tny-mime-part.h>
35 #include <tny-store-account.h>
36 #include <tny-folder-store.h>
37 #include <tny-folder-store-query.h>
38 #include <tny-camel-stream.h>
39 #include <tny-simple-list.h>
40 #include <tny-send-queue.h>
41 #include <camel/camel-stream-mem.h>
42 #include <glib/gi18n.h>
43 #include <modest-tny-account.h>
44 #include <modest-tny-send-queue.h>
45 #include <modest-runtime.h>
46 #include "modest-text-utils.h"
47 #include "modest-tny-msg.h"
48 #include "modest-tny-platform-factory.h"
49 #include "modest-marshal.h"
50 #include "modest-error.h"
51
52 /* 'private'/'protected' functions */
53 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
54 static void modest_mail_operation_init       (ModestMailOperation *obj);
55 static void modest_mail_operation_finalize   (GObject *obj);
56
57 static void     update_folders_cb    (TnyFolderStore *self, 
58                                       TnyList *list, 
59                                       GError **err, 
60                                       gpointer user_data);
61
62 enum _ModestMailOperationSignals 
63 {
64         PROGRESS_CHANGED_SIGNAL,
65
66         NUM_SIGNALS
67 };
68
69 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
70 struct _ModestMailOperationPrivate {
71         guint                      done;
72         guint                      total;
73         ModestMailOperationStatus  status;
74         GError                    *error;
75 };
76
77 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
78                                                    MODEST_TYPE_MAIL_OPERATION, \
79                                                    ModestMailOperationPrivate))
80
81 #define CHECK_EXCEPTION(priv, new_status, op)  if (priv->error) {\
82                                                    priv->status = new_status;\
83                                                    op;\
84                                                }
85
86 typedef struct _RefreshFolderAsyncHelper
87 {
88         ModestMailOperation *mail_op;
89         TnyIterator *iter;
90         guint failed;
91         guint canceled;
92
93 } RefreshFolderAsyncHelper;
94
95 typedef struct _XFerMsgAsyncHelper
96 {
97         ModestMailOperation *mail_op;
98         TnyList *headers;
99         TnyFolder *dest_folder;
100
101 } XFerMsgAsyncHelper;
102
103
104 /* globals */
105 static GObjectClass *parent_class = NULL;
106
107 static guint signals[NUM_SIGNALS] = {0};
108
109 GType
110 modest_mail_operation_get_type (void)
111 {
112         static GType my_type = 0;
113         if (!my_type) {
114                 static const GTypeInfo my_info = {
115                         sizeof(ModestMailOperationClass),
116                         NULL,           /* base init */
117                         NULL,           /* base finalize */
118                         (GClassInitFunc) modest_mail_operation_class_init,
119                         NULL,           /* class finalize */
120                         NULL,           /* class data */
121                         sizeof(ModestMailOperation),
122                         1,              /* n_preallocs */
123                         (GInstanceInitFunc) modest_mail_operation_init,
124                         NULL
125                 };
126                 my_type = g_type_register_static (G_TYPE_OBJECT,
127                                                   "ModestMailOperation",
128                                                   &my_info, 0);
129         }
130         return my_type;
131 }
132
133 static void
134 modest_mail_operation_class_init (ModestMailOperationClass *klass)
135 {
136         GObjectClass *gobject_class;
137         gobject_class = (GObjectClass*) klass;
138
139         parent_class            = g_type_class_peek_parent (klass);
140         gobject_class->finalize = modest_mail_operation_finalize;
141
142         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
143
144         /**
145          * ModestMailOperation::progress-changed
146          * @self: the #MailOperation that emits the signal
147          * @user_data: user data set when the signal handler was connected
148          *
149          * Emitted when the progress of a mail operation changes
150          */
151         signals[PROGRESS_CHANGED_SIGNAL] = 
152                 g_signal_new ("progress-changed",
153                               G_TYPE_FROM_CLASS (gobject_class),
154                               G_SIGNAL_RUN_FIRST,
155                               G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
156                               NULL, NULL,
157                               g_cclosure_marshal_VOID__VOID,
158                               G_TYPE_NONE, 0);
159 }
160
161 static void
162 modest_mail_operation_init (ModestMailOperation *obj)
163 {
164         ModestMailOperationPrivate *priv;
165
166         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
167
168         priv->status   = MODEST_MAIL_OPERATION_STATUS_INVALID;
169         priv->error    = NULL;
170         priv->done     = 0;
171         priv->total    = 0;
172 }
173
174 static void
175 modest_mail_operation_finalize (GObject *obj)
176 {
177         ModestMailOperationPrivate *priv;
178
179         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
180
181         if (priv->error) {
182                 g_error_free (priv->error);
183                 priv->error = NULL;
184         }
185
186         G_OBJECT_CLASS(parent_class)->finalize (obj);
187 }
188
189 ModestMailOperation*
190 modest_mail_operation_new (void)
191 {
192         return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
193 }
194
195
196 void
197 modest_mail_operation_send_mail (ModestMailOperation *self,
198                                  TnyTransportAccount *transport_account,
199                                  TnyMsg* msg)
200 {
201         TnySendQueue *send_queue;
202         
203         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
204         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
205         g_return_if_fail (TNY_IS_MSG (msg));
206         
207         send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
208         if (!TNY_IS_SEND_QUEUE(send_queue))
209                 g_printerr ("modest: could not find send queue for account\n");
210         else {
211                 GError *err = NULL;
212                 tny_send_queue_add (send_queue, msg, &err);
213                 if (err) {
214                         g_printerr ("modest: error adding msg to send queue: %s\n",
215                                     err->message);
216                         g_error_free (err);
217                 } else
218                         g_message ("modest: message added to send queue");
219         }
220
221         /* Notify the queue */
222         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
223 }
224
225 void
226 modest_mail_operation_send_new_mail (ModestMailOperation *self,
227                                      TnyTransportAccount *transport_account,
228                                      const gchar *from,  const gchar *to,
229                                      const gchar *cc,  const gchar *bcc,
230                                      const gchar *subject, const gchar *plain_body,
231                                      const gchar *html_body,
232                                      const GList *attachments_list,
233                                      TnyHeaderFlags priority_flags)
234 {
235         TnyMsg *new_msg;
236         ModestMailOperationPrivate *priv = NULL;
237         /* GList *node = NULL; */
238
239         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
240         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
241
242         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
243
244         /* Check parametters */
245         if (to == NULL) {
246                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
247                              MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
248                              _("Error trying to send a mail. You need to set at least one recipient"));
249                 return;
250         }
251
252         if (html_body == NULL) {
253                 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
254         } else {
255                 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
256         }
257         if (!new_msg) {
258                 g_printerr ("modest: failed to create a new msg\n");
259                 return;
260         }
261
262         /* TODO: add priority handling. It's received in the priority_flags operator, and
263            it should have effect in the sending operation */
264
265         /* Call mail operation */
266         modest_mail_operation_send_mail (self, transport_account, new_msg);
267
268         /* Free */
269         g_object_unref (G_OBJECT (new_msg));
270 }
271
272 static void
273 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
274 {
275         TnyIterator *iter;
276         TnyList *folders = tny_simple_list_new ();
277
278         tny_folder_store_get_folders (store, folders, query, NULL);
279         iter = tny_list_create_iterator (folders);
280
281         while (!tny_iterator_is_done (iter)) {
282
283                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
284
285                 tny_list_prepend (all_folders, G_OBJECT (folder));
286
287                 recurse_folders (folder, query, all_folders);
288             
289                 g_object_unref (G_OBJECT (folder));
290
291                 tny_iterator_next (iter);
292         }
293          g_object_unref (G_OBJECT (iter));
294          g_object_unref (G_OBJECT (folders));
295 }
296
297 static void
298 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
299 {
300         ModestMailOperation *self;
301         ModestMailOperationPrivate *priv;
302         TnyIterator *iter;
303         TnyList *all_folders;
304         
305         self  = MODEST_MAIL_OPERATION (user_data);
306         priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
307
308         g_message (__FUNCTION__);
309         
310         if (*err) {
311                 priv->error = g_error_copy (*err);
312                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
313                 goto out;
314         }
315
316         /* Get all the folders We can do it synchronously because
317            we're already running in a different thread than the UI */
318         all_folders = tny_list_copy (list);
319         iter = tny_list_create_iterator (all_folders);
320         while (!tny_iterator_is_done (iter)) {
321                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
322
323                 recurse_folders (folder, NULL, all_folders);
324                 tny_iterator_next (iter);
325         }
326         g_object_unref (G_OBJECT (iter));
327
328         /* Refresh folders */
329         iter = tny_list_create_iterator (all_folders);
330         priv->total = tny_list_get_length (all_folders);
331
332         while (!tny_iterator_is_done (iter) && !priv->error) {
333
334                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
335
336                 /* Refresh the folder */
337                 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
338
339                 if (priv->error) {
340                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
341                 } else {        
342                         /* Update status and notify */
343                         priv->done++;
344                         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
345                 }
346     
347                 g_object_unref (G_OBJECT (folder));
348             
349                 tny_iterator_next (iter);
350         }
351
352         g_object_unref (G_OBJECT (iter));
353  out:
354         g_object_unref (G_OBJECT (list));
355
356         /* Check if the operation was a success */
357         if (priv->done == priv->total && !priv->error)
358                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
359
360         /* Free */
361         g_object_unref (G_OBJECT (folder_store));
362
363         /* Notify the queue */
364         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
365 }
366
367 gboolean
368 modest_mail_operation_update_account (ModestMailOperation *self,
369                                       TnyStoreAccount *store_account)
370 {
371         ModestMailOperationPrivate *priv;
372         TnyList *folders;
373
374         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
375         g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
376
377         /* Pick async call reference */
378         g_object_ref (store_account);
379
380         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
381
382         priv->total = 0;
383         priv->done  = 0;
384         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
385         
386         /* Get subscribed folders & refresh them */
387         folders = TNY_LIST (tny_simple_list_new ());
388
389         g_message ("tny_folder_store_get_folders_async");
390         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
391                                             folders, update_folders_cb, NULL, self);
392         
393         return TRUE;
394 }
395
396 ModestMailOperationStatus
397 modest_mail_operation_get_status (ModestMailOperation *self)
398 {
399         ModestMailOperationPrivate *priv;
400
401         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
402         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
403                               MODEST_MAIL_OPERATION_STATUS_INVALID);
404
405         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
406         return priv->status;
407 }
408
409 const GError *
410 modest_mail_operation_get_error (ModestMailOperation *self)
411 {
412         ModestMailOperationPrivate *priv;
413
414         g_return_val_if_fail (self, NULL);
415         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
416
417         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
418         return priv->error;
419 }
420
421 gboolean 
422 modest_mail_operation_cancel (ModestMailOperation *self)
423 {
424         /* TODO */
425         return TRUE;
426 }
427
428 guint 
429 modest_mail_operation_get_task_done (ModestMailOperation *self)
430 {
431         ModestMailOperationPrivate *priv;
432
433         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
434
435         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
436         return priv->done;
437 }
438
439 guint 
440 modest_mail_operation_get_task_total (ModestMailOperation *self)
441 {
442         ModestMailOperationPrivate *priv;
443
444         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
445
446         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
447         return priv->total;
448 }
449
450 gboolean
451 modest_mail_operation_is_finished (ModestMailOperation *self)
452 {
453         ModestMailOperationPrivate *priv;
454         gboolean retval = FALSE;
455
456         if (!MODEST_IS_MAIL_OPERATION (self)) {
457                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
458                 return retval;
459         }
460
461         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
462
463         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
464             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
465             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
466             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
467                 retval = TRUE;
468         } else {
469                 retval = FALSE;
470         }
471
472         return retval;
473 }
474
475 /* ******************************************************************* */
476 /* ************************** STORE  ACTIONS ************************* */
477 /* ******************************************************************* */
478
479
480 TnyFolder *
481 modest_mail_operation_create_folder (ModestMailOperation *self,
482                                      TnyFolderStore *parent,
483                                      const gchar *name)
484 {
485         ModestMailOperationPrivate *priv;
486         TnyFolder *new_folder = NULL;
487
488         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
489         g_return_val_if_fail (name, NULL);
490
491         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
492
493         /* Create the folder */
494         new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
495         CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return NULL);
496
497         /* Notify the queue */
498         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
499
500         return new_folder;
501 }
502
503 void
504 modest_mail_operation_remove_folder (ModestMailOperation *self,
505                                      TnyFolder           *folder,
506                                      gboolean             remove_to_trash)
507 {
508         TnyFolderStore *parent;
509         TnyAccount *account;
510         ModestMailOperationPrivate *priv;
511
512         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
513         g_return_if_fail (TNY_IS_FOLDER (folder));
514
515         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
516
517         /* Get the account */
518         account = tny_folder_get_account (folder);
519
520         /* Delete folder or move to trash */
521         if (remove_to_trash) {
522                 TnyFolder *trash_folder, *new_folder;
523                 trash_folder = modest_tny_account_get_special_folder (account,
524                                                                       TNY_FOLDER_TYPE_TRASH);
525                 /* TODO: error_handling */
526                 new_folder = modest_mail_operation_xfer_folder (self, folder, 
527                                                                 TNY_FOLDER_STORE (trash_folder), TRUE);
528                 g_object_unref (G_OBJECT (new_folder));
529         } else {
530                 parent = tny_folder_get_folder_store (folder);
531
532                 tny_folder_store_remove_folder (parent, folder, &(priv->error));
533                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, );
534
535                 if (parent)
536                         g_object_unref (G_OBJECT (parent));
537         }
538         g_object_unref (G_OBJECT (account));
539
540         /* Notify the queue */
541         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
542 }
543
544 void
545 modest_mail_operation_rename_folder (ModestMailOperation *self,
546                                      TnyFolder *folder,
547                                      const gchar *name)
548 {
549         ModestMailOperationPrivate *priv;
550
551         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
552         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
553         g_return_if_fail (name);
554
555         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
556
557         /* FIXME: better error handling */
558         if (strrchr (name, '/') != NULL)
559                 goto out;
560
561         /* Rename. Camel handles folder subscription/unsubscription */
562         tny_folder_set_name (folder, name, &(priv->error));
563         CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto out);
564
565  out:
566         /* Notify the queue */
567         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
568  }
569
570 TnyFolder *
571 modest_mail_operation_xfer_folder (ModestMailOperation *self,
572                                    TnyFolder *folder,
573                                    TnyFolderStore *parent,
574                                    gboolean delete_original)
575 {
576         ModestMailOperationPrivate *priv;
577         TnyFolder *new_folder;
578
579         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
580         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
581         g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
582
583         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
584
585         new_folder = tny_folder_copy (folder,
586                                       parent,
587                                       tny_folder_get_name (folder),
588                                       delete_original, 
589                                       &(priv->error));
590
591         /* Notify the queue */
592         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
593
594         return new_folder;
595 }
596
597
598 /* ******************************************************************* */
599 /* **************************  MSG  ACTIONS  ************************* */
600 /* ******************************************************************* */
601
602 void 
603 modest_mail_operation_remove_msg (ModestMailOperation *self,
604                                   TnyHeader *header,
605                                   gboolean remove_to_trash)
606 {
607         TnyFolder *folder;
608         ModestMailOperationPrivate *priv;
609
610         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
611         g_return_if_fail (TNY_IS_HEADER (header));
612
613         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
614         folder = tny_header_get_folder (header);
615
616         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
617
618         /* Delete or move to trash */
619         if (remove_to_trash) {
620                 TnyFolder *trash_folder;
621                 TnyStoreAccount *store_account;
622
623                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
624                 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
625                                                                       TNY_FOLDER_TYPE_TRASH);
626                 if (trash_folder) {
627                         modest_mail_operation_xfer_msg (self, header, trash_folder, TRUE);
628 /*                      g_object_unref (trash_folder); */
629                 } else {
630                         ModestMailOperationPrivate *priv;
631
632                         /* Set status failed and set an error */
633                         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
634                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
635                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
636                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
637                                      _("Error trying to delete a message. Trash folder not found"));
638                 }
639
640                 g_object_unref (G_OBJECT (store_account));
641         } else {
642                 tny_folder_remove_msg (folder, header, &(priv->error));
643                 if (!priv->error)
644                         tny_folder_sync(folder, TRUE, &(priv->error));
645         }
646
647         /* Set status */
648         if (!priv->error)
649                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
650         else
651                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
652
653         /* Free */
654         g_object_unref (G_OBJECT (folder));
655
656         /* Notify the queue */
657         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
658 }
659
660 static void
661 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
662 {
663         XFerMsgAsyncHelper *helper;
664         ModestMailOperation *self;
665         ModestMailOperationPrivate *priv;
666
667         helper = (XFerMsgAsyncHelper *) user_data;
668         self = helper->mail_op;
669         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
670
671         if (*err) {
672                 priv->error = g_error_copy (*err);
673                 priv->done = 0;
674                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
675         } else {
676                 priv->done = 1;
677                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
678         }
679
680         /* Free */
681         g_object_unref (helper->headers);
682         g_object_unref (helper->dest_folder);
683         g_object_unref (folder);
684         g_free (helper);
685
686         /* Notify the queue */
687         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
688 }
689
690 void
691 modest_mail_operation_xfer_msg (ModestMailOperation *self,
692                                 TnyHeader *header, 
693                                 TnyFolder *folder, 
694                                 gboolean delete_original)
695 {
696         ModestMailOperationPrivate *priv;
697         TnyFolder *src_folder;
698         TnyList *headers;
699         XFerMsgAsyncHelper *helper;
700
701         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
702         g_return_if_fail (TNY_IS_HEADER (header));
703         g_return_if_fail (TNY_IS_FOLDER (folder));
704
705         /* Pick references for async calls */
706         g_object_ref (folder);
707
708         headers = tny_simple_list_new ();
709         tny_list_prepend (headers, G_OBJECT (header));
710
711         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
712         priv->total = 1;
713         priv->done = 0;
714         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
715
716         /* Create the helper */
717         helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
718         helper->mail_op = self;
719         helper->dest_folder = folder;
720         helper->headers = headers;
721
722         src_folder = tny_header_get_folder (header);
723         tny_folder_transfer_msgs_async (src_folder, 
724                                         headers, 
725                                         folder, 
726                                         delete_original, 
727                                         transfer_msgs_cb, 
728                                         helper);
729 }
730
731 static void
732 on_refresh_folder (TnyFolder   *folder, 
733                    gboolean     cancelled, 
734                    GError     **error,
735                    gpointer     user_data)
736 {
737         ModestMailOperation *self;
738         ModestMailOperationPrivate *priv;
739
740         self = MODEST_MAIL_OPERATION (user_data);
741         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
742
743         if (*error) {
744                 priv->error = g_error_copy (*error);
745                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
746                 goto out;
747         }
748
749         if (cancelled) {
750                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
751                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
752                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
753                              _("Error trying to refresh the contents of %s"),
754                              tny_folder_get_name (folder));
755                 goto out;
756         }
757
758         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
759
760  out:
761         /* Free */
762         g_object_unref (folder);
763
764         /* Notify the queue */
765         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
766 }
767
768 /* static void */
769 /* on_refresh_folder_status_update (TnyFolder *folder, const gchar *msg, */
770 /*                               gint num, gint total,  gpointer user_data) */
771 /* { */
772 /*      ModestMailOperation *self; */
773 /*      ModestMailOperationPrivate *priv; */
774
775 /*      self = MODEST_MAIL_OPERATION (user_data); */
776 /*      priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self); */
777
778 /*      priv->done = num; */
779 /*      priv->total = total; */
780
781 /*      if (num == 1 && total == 100) */
782 /*              return; */
783
784 /*      g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL); */
785 /* } */
786
787 void 
788 modest_mail_operation_refresh_folder  (ModestMailOperation *self,
789                                        TnyFolder *folder)
790 {
791         ModestMailOperationPrivate *priv;
792
793         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
794
795         /* Pick a reference */
796         g_object_ref (folder);
797
798         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
799
800         /* Refresh the folder. TODO: tinymail could issue a status
801            updates before the callback call then this could happen. We
802            must review the design */
803         tny_folder_refresh_async (folder,
804                                   on_refresh_folder,
805 /*                                on_refresh_folder_status_update, */
806                                   NULL,
807                                   self);
808 }
809
810 void
811 _modest_mail_operation_notify_end (ModestMailOperation *self)
812 {
813         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
814 }