b49614104bfa3db6aa180aa2ff0bd7a62286eca3
[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 {
234         TnyMsg *new_msg;
235         ModestMailOperationPrivate *priv = NULL;
236         /* GList *node = NULL; */
237
238         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
239         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
240
241         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
242
243         /* Check parametters */
244         if (to == NULL) {
245                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
246                              MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
247                              _("Error trying to send a mail. You need to set at least one recipient"));
248                 return;
249         }
250
251         if (html_body == NULL) {
252                 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
253         } else {
254                 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
255         }
256         if (!new_msg) {
257                 g_printerr ("modest: failed to create a new msg\n");
258                 return;
259         }
260
261         /* Call mail operation */
262         modest_mail_operation_send_mail (self, transport_account, new_msg);
263
264         /* Free */
265         g_object_unref (G_OBJECT (new_msg));
266 }
267
268 static void
269 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
270 {
271         TnyIterator *iter;
272         TnyList *folders = tny_simple_list_new ();
273
274         tny_folder_store_get_folders (store, folders, query, NULL);
275         iter = tny_list_create_iterator (folders);
276
277         while (!tny_iterator_is_done (iter)) {
278
279                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
280
281                 tny_list_prepend (all_folders, G_OBJECT (folder));
282
283                 recurse_folders (folder, query, all_folders);
284             
285                 g_object_unref (G_OBJECT (folder));
286
287                 tny_iterator_next (iter);
288         }
289          g_object_unref (G_OBJECT (iter));
290          g_object_unref (G_OBJECT (folders));
291 }
292
293 static void
294 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
295 {
296         ModestMailOperation *self;
297         ModestMailOperationPrivate *priv;
298         TnyIterator *iter;
299         TnyList *all_folders;
300         
301         self  = MODEST_MAIL_OPERATION (user_data);
302         priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
303
304         g_message (__FUNCTION__);
305         
306         if (*err) {
307                 priv->error = g_error_copy (*err);
308                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
309                 goto out;
310         }
311
312         /* Get all the folders We can do it synchronously because
313            we're already running in a different thread than the UI */
314         all_folders = tny_list_copy (list);
315         iter = tny_list_create_iterator (all_folders);
316         while (!tny_iterator_is_done (iter)) {
317                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
318
319                 recurse_folders (folder, NULL, all_folders);
320                 tny_iterator_next (iter);
321         }
322         g_object_unref (G_OBJECT (iter));
323
324         /* Refresh folders */
325         iter = tny_list_create_iterator (all_folders);
326         priv->total = tny_list_get_length (all_folders);
327
328         while (!tny_iterator_is_done (iter) && !priv->error) {
329
330                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
331
332                 /* Refresh the folder */
333                 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
334
335                 if (priv->error) {
336                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
337                 } else {        
338                         /* Update status and notify */
339                         priv->done++;
340                         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
341                 }
342     
343                 g_object_unref (G_OBJECT (folder));
344             
345                 tny_iterator_next (iter);
346         }
347
348         g_object_unref (G_OBJECT (iter));
349  out:
350         g_object_unref (G_OBJECT (list));
351
352         /* Check if the operation was a success */
353         if (priv->done == priv->total && !priv->error)
354                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
355
356         /* Free */
357         g_object_unref (G_OBJECT (folder_store));
358
359         /* Notify the queue */
360         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
361 }
362
363 gboolean
364 modest_mail_operation_update_account (ModestMailOperation *self,
365                                       TnyStoreAccount *store_account)
366 {
367         ModestMailOperationPrivate *priv;
368         TnyList *folders;
369
370         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
371         g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
372
373         /* Pick async call reference */
374         g_object_ref (store_account);
375
376         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
377
378         priv->total = 0;
379         priv->done  = 0;
380         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
381         
382         /* Get subscribed folders & refresh them */
383         folders = TNY_LIST (tny_simple_list_new ());
384
385         g_message ("tny_folder_store_get_folders_async");
386         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
387                                             folders, update_folders_cb, NULL, self);
388         
389         return TRUE;
390 }
391
392 ModestMailOperationStatus
393 modest_mail_operation_get_status (ModestMailOperation *self)
394 {
395         ModestMailOperationPrivate *priv;
396
397         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
398         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
399                               MODEST_MAIL_OPERATION_STATUS_INVALID);
400
401         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
402         return priv->status;
403 }
404
405 const GError *
406 modest_mail_operation_get_error (ModestMailOperation *self)
407 {
408         ModestMailOperationPrivate *priv;
409
410         g_return_val_if_fail (self, NULL);
411         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
412
413         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
414         return priv->error;
415 }
416
417 gboolean 
418 modest_mail_operation_cancel (ModestMailOperation *self)
419 {
420         /* TODO */
421         return TRUE;
422 }
423
424 guint 
425 modest_mail_operation_get_task_done (ModestMailOperation *self)
426 {
427         ModestMailOperationPrivate *priv;
428
429         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
430
431         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
432         return priv->done;
433 }
434
435 guint 
436 modest_mail_operation_get_task_total (ModestMailOperation *self)
437 {
438         ModestMailOperationPrivate *priv;
439
440         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
441
442         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
443         return priv->total;
444 }
445
446 gboolean
447 modest_mail_operation_is_finished (ModestMailOperation *self)
448 {
449         ModestMailOperationPrivate *priv;
450         gboolean retval = FALSE;
451
452         if (!MODEST_IS_MAIL_OPERATION (self)) {
453                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
454                 return retval;
455         }
456
457         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
458
459         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
460             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
461             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
462             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
463                 retval = TRUE;
464         } else {
465                 retval = FALSE;
466         }
467
468         return retval;
469 }
470
471 /* ******************************************************************* */
472 /* ************************** STORE  ACTIONS ************************* */
473 /* ******************************************************************* */
474
475
476 TnyFolder *
477 modest_mail_operation_create_folder (ModestMailOperation *self,
478                                      TnyFolderStore *parent,
479                                      const gchar *name)
480 {
481         ModestMailOperationPrivate *priv;
482         TnyFolder *new_folder = NULL;
483
484         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
485         g_return_val_if_fail (name, NULL);
486
487         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
488
489         /* Create the folder */
490         new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
491         CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return NULL);
492
493         /* Notify the queue */
494         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
495
496         return new_folder;
497 }
498
499 void
500 modest_mail_operation_remove_folder (ModestMailOperation *self,
501                                      TnyFolder           *folder,
502                                      gboolean             remove_to_trash)
503 {
504         TnyFolderStore *parent;
505         TnyAccount *account;
506         ModestMailOperationPrivate *priv;
507
508         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
509         g_return_if_fail (TNY_IS_FOLDER (folder));
510
511         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
512
513         /* Get the account */
514         account = tny_folder_get_account (folder);
515
516         /* Delete folder or move to trash */
517         if (remove_to_trash) {
518                 TnyFolder *trash_folder, *new_folder;
519                 trash_folder = modest_tny_account_get_special_folder (account,
520                                                                       TNY_FOLDER_TYPE_TRASH);
521                 /* TODO: error_handling */
522                 new_folder = modest_mail_operation_xfer_folder (self, folder, 
523                                                                 TNY_FOLDER_STORE (trash_folder), TRUE);
524                 g_object_unref (G_OBJECT (new_folder));
525         } else {
526                 parent = tny_folder_get_folder_store (folder);
527
528                 tny_folder_store_remove_folder (parent, folder, &(priv->error));
529                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, );
530
531                 if (parent)
532                         g_object_unref (G_OBJECT (parent));
533         }
534         g_object_unref (G_OBJECT (account));
535
536         /* Notify the queue */
537         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
538 }
539
540 void
541 modest_mail_operation_rename_folder (ModestMailOperation *self,
542                                      TnyFolder *folder,
543                                      const gchar *name)
544 {
545         ModestMailOperationPrivate *priv;
546
547         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
548         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
549         g_return_if_fail (name);
550
551         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
552
553         /* FIXME: better error handling */
554         if (strrchr (name, '/') != NULL)
555                 goto out;
556
557         /* Rename. Camel handles folder subscription/unsubscription */
558         tny_folder_set_name (folder, name, &(priv->error));
559         CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto out);
560
561  out:
562         /* Notify the queue */
563         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
564  }
565
566 TnyFolder *
567 modest_mail_operation_xfer_folder (ModestMailOperation *self,
568                                    TnyFolder *folder,
569                                    TnyFolderStore *parent,
570                                    gboolean delete_original)
571 {
572         ModestMailOperationPrivate *priv;
573         TnyFolder *new_folder;
574
575         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
576         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
577         g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
578
579         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
580
581         new_folder = tny_folder_copy (folder,
582                                       parent,
583                                       tny_folder_get_name (folder),
584                                       delete_original, 
585                                       &(priv->error));
586
587         /* Notify the queue */
588         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
589
590         return new_folder;
591 }
592
593
594 /* ******************************************************************* */
595 /* **************************  MSG  ACTIONS  ************************* */
596 /* ******************************************************************* */
597
598 void 
599 modest_mail_operation_remove_msg (ModestMailOperation *self,
600                                   TnyHeader *header,
601                                   gboolean remove_to_trash)
602 {
603         TnyFolder *folder;
604         ModestMailOperationPrivate *priv;
605
606         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
607         g_return_if_fail (TNY_IS_HEADER (header));
608
609         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
610         folder = tny_header_get_folder (header);
611
612         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
613
614         /* Delete or move to trash */
615         if (remove_to_trash) {
616                 TnyFolder *trash_folder;
617                 TnyStoreAccount *store_account;
618
619                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
620                 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
621                                                                       TNY_FOLDER_TYPE_TRASH);
622                 if (trash_folder) {
623                         modest_mail_operation_xfer_msg (self, header, trash_folder, TRUE);
624 /*                      g_object_unref (trash_folder); */
625                 } else {
626                         ModestMailOperationPrivate *priv;
627
628                         /* Set status failed and set an error */
629                         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
630                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
631                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
632                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
633                                      _("Error trying to delete a message. Trash folder not found"));
634                 }
635
636                 g_object_unref (G_OBJECT (store_account));
637         } else {
638                 tny_folder_remove_msg (folder, header, &(priv->error));
639                 if (!priv->error)
640                         tny_folder_sync(folder, TRUE, &(priv->error));
641         }
642
643         /* Set status */
644         if (!priv->error)
645                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
646         else
647                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
648
649         /* Free */
650         g_object_unref (G_OBJECT (folder));
651
652         /* Notify the queue */
653         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
654 }
655
656 static void
657 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
658 {
659         XFerMsgAsyncHelper *helper;
660         ModestMailOperation *self;
661         ModestMailOperationPrivate *priv;
662
663         helper = (XFerMsgAsyncHelper *) user_data;
664         self = helper->mail_op;
665         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
666
667         if (*err) {
668                 priv->error = g_error_copy (*err);
669                 priv->done = 0;
670                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
671         } else {
672                 priv->done = 1;
673                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
674         }
675
676         /* Free */
677         g_object_unref (helper->headers);
678         g_object_unref (helper->dest_folder);
679         g_object_unref (folder);
680         g_free (helper);
681
682         /* Notify the queue */
683         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
684 }
685
686 void
687 modest_mail_operation_xfer_msg (ModestMailOperation *self,
688                                 TnyHeader *header, 
689                                 TnyFolder *folder, 
690                                 gboolean delete_original)
691 {
692         ModestMailOperationPrivate *priv;
693         TnyFolder *src_folder;
694         TnyList *headers;
695         XFerMsgAsyncHelper *helper;
696
697         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
698         g_return_if_fail (TNY_IS_HEADER (header));
699         g_return_if_fail (TNY_IS_FOLDER (folder));
700
701         /* Pick references for async calls */
702         g_object_ref (folder);
703
704         headers = tny_simple_list_new ();
705         tny_list_prepend (headers, G_OBJECT (header));
706
707         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
708         priv->total = 1;
709         priv->done = 0;
710         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
711
712         /* Create the helper */
713         helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
714         helper->mail_op = self;
715         helper->dest_folder = folder;
716         helper->headers = headers;
717
718         src_folder = tny_header_get_folder (header);
719         tny_folder_transfer_msgs_async (src_folder, 
720                                         headers, 
721                                         folder, 
722                                         delete_original, 
723                                         transfer_msgs_cb, 
724                                         helper);
725 }
726
727 static void
728 on_refresh_folder (TnyFolder   *folder, 
729                    gboolean     cancelled, 
730                    GError     **error,
731                    gpointer     user_data)
732 {
733         ModestMailOperation *self;
734         ModestMailOperationPrivate *priv;
735
736         self = MODEST_MAIL_OPERATION (user_data);
737         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
738
739         if (*error) {
740                 priv->error = g_error_copy (*error);
741                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
742                 goto out;
743         }
744
745         if (cancelled) {
746                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
747                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
748                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
749                              _("Error trying to refresh the contents of %s"),
750                              tny_folder_get_name (folder));
751                 goto out;
752         }
753
754         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
755
756  out:
757         /* Free */
758         g_object_unref (folder);
759
760         /* Notify the queue */
761         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
762 }
763
764 /* static void */
765 /* on_refresh_folder_status_update (TnyFolder *folder, const gchar *msg, */
766 /*                               gint num, gint total,  gpointer user_data) */
767 /* { */
768 /*      ModestMailOperation *self; */
769 /*      ModestMailOperationPrivate *priv; */
770
771 /*      self = MODEST_MAIL_OPERATION (user_data); */
772 /*      priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self); */
773
774 /*      priv->done = num; */
775 /*      priv->total = total; */
776
777 /*      if (num == 1 && total == 100) */
778 /*              return; */
779
780 /*      g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL); */
781 /* } */
782
783 void 
784 modest_mail_operation_refresh_folder  (ModestMailOperation *self,
785                                        TnyFolder *folder)
786 {
787         ModestMailOperationPrivate *priv;
788
789         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
790
791         /* Pick a reference */
792         g_object_ref (folder);
793
794         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
795
796         /* Refresh the folder. TODO: tinymail could issue a status
797            updates before the callback call then this could happen. We
798            must review the design */
799         tny_folder_refresh_async (folder,
800                                   on_refresh_folder,
801 /*                                on_refresh_folder_status_update, */
802                                   NULL,
803                                   self);
804 }
805
806 void
807 _modest_mail_operation_notify_end (ModestMailOperation *self)
808 {
809         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
810 }