114cb184fa00d915e81917ecbb3e84bf59267c8a
[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 <tny-status.h>
42 #include <camel/camel-stream-mem.h>
43 #include <glib/gi18n.h>
44 #include <modest-tny-account.h>
45 #include <modest-tny-send-queue.h>
46 #include <modest-runtime.h>
47 #include "modest-text-utils.h"
48 #include "modest-tny-msg.h"
49 #include "modest-tny-folder.h"
50 #include "modest-tny-platform-factory.h"
51 #include "modest-marshal.h"
52 #include "modest-error.h"
53
54 /* 'private'/'protected' functions */
55 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
56 static void modest_mail_operation_init       (ModestMailOperation *obj);
57 static void modest_mail_operation_finalize   (GObject *obj);
58
59 static void     update_folders_cb    (TnyFolderStore *self, 
60                                       TnyList *list, 
61                                       GError **err, 
62                                       gpointer user_data);
63 static void     update_folders_status_cb (GObject *obj,
64                                           TnyStatus *status,  
65                                           gpointer user_data);
66
67 static void     update_process_msg_status_cb (GObject *obj,
68                                               TnyStatus *status,  
69                                           gpointer user_data);
70
71 enum _ModestMailOperationSignals 
72 {
73         PROGRESS_CHANGED_SIGNAL,
74
75         NUM_SIGNALS
76 };
77
78 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
79 struct _ModestMailOperationPrivate {
80         guint                      done;
81         guint                      total;
82         ModestMailOperationStatus  status;      
83         ModestMailOperationId      id;          
84         GError                    *error;
85 };
86
87 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
88                                                    MODEST_TYPE_MAIL_OPERATION, \
89                                                    ModestMailOperationPrivate))
90
91 #define CHECK_EXCEPTION(priv, new_status)  if (priv->error) {\
92                                                    priv->status = new_status;\
93                                                }
94
95 typedef struct _RefreshFolderAsyncHelper
96 {
97         ModestMailOperation *mail_op;
98         TnyIterator *iter;
99         guint failed;
100         guint canceled;
101
102 } RefreshFolderAsyncHelper;
103
104 typedef struct _XFerMsgAsyncHelper
105 {
106         ModestMailOperation *mail_op;
107         TnyList *headers;
108         TnyFolder *dest_folder;
109
110 } XFerMsgAsyncHelper;
111
112
113 /* globals */
114 static GObjectClass *parent_class = NULL;
115
116 static guint signals[NUM_SIGNALS] = {0};
117
118 GType
119 modest_mail_operation_get_type (void)
120 {
121         static GType my_type = 0;
122         if (!my_type) {
123                 static const GTypeInfo my_info = {
124                         sizeof(ModestMailOperationClass),
125                         NULL,           /* base init */
126                         NULL,           /* base finalize */
127                         (GClassInitFunc) modest_mail_operation_class_init,
128                         NULL,           /* class finalize */
129                         NULL,           /* class data */
130                         sizeof(ModestMailOperation),
131                         1,              /* n_preallocs */
132                         (GInstanceInitFunc) modest_mail_operation_init,
133                         NULL
134                 };
135                 my_type = g_type_register_static (G_TYPE_OBJECT,
136                                                   "ModestMailOperation",
137                                                   &my_info, 0);
138         }
139         return my_type;
140 }
141
142 static void
143 modest_mail_operation_class_init (ModestMailOperationClass *klass)
144 {
145         GObjectClass *gobject_class;
146         gobject_class = (GObjectClass*) klass;
147
148         parent_class            = g_type_class_peek_parent (klass);
149         gobject_class->finalize = modest_mail_operation_finalize;
150
151         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
152
153         /**
154          * ModestMailOperation::progress-changed
155          * @self: the #MailOperation that emits the signal
156          * @user_data: user data set when the signal handler was connected
157          *
158          * Emitted when the progress of a mail operation changes
159          */
160         signals[PROGRESS_CHANGED_SIGNAL] = 
161                 g_signal_new ("progress-changed",
162                               G_TYPE_FROM_CLASS (gobject_class),
163                               G_SIGNAL_RUN_FIRST,
164                               G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
165                               NULL, NULL,
166                               g_cclosure_marshal_VOID__VOID,
167                               G_TYPE_NONE, 0);
168 }
169
170 static void
171 modest_mail_operation_init (ModestMailOperation *obj)
172 {
173         ModestMailOperationPrivate *priv;
174
175         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
176
177         priv->status   = MODEST_MAIL_OPERATION_STATUS_INVALID;
178         priv->id       = MODEST_MAIL_OPERATION_ID_UNKNOWN;
179         priv->error    = NULL;
180         priv->done     = 0;
181         priv->total    = 0;
182 }
183
184 static void
185 modest_mail_operation_finalize (GObject *obj)
186 {
187         ModestMailOperationPrivate *priv;
188
189         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
190
191         if (priv->error) {
192                 g_error_free (priv->error);
193                 priv->error = NULL;
194         }
195
196         G_OBJECT_CLASS(parent_class)->finalize (obj);
197 }
198
199 ModestMailOperation*
200 modest_mail_operation_new (ModestMailOperationId id)
201 {
202         ModestMailOperation *obj;
203         ModestMailOperationPrivate *priv;
204
205
206         obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
207         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
208
209         priv->id = id;
210
211         return obj;
212 }
213
214
215
216 ModestMailOperationId
217 modest_mail_operation_get_id (ModestMailOperation *self)
218 {
219         ModestMailOperationPrivate *priv;
220
221         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
222         
223         return priv->id;
224 }
225
226 void
227 modest_mail_operation_send_mail (ModestMailOperation *self,
228                                  TnyTransportAccount *transport_account,
229                                  TnyMsg* msg)
230 {
231         TnySendQueue *send_queue;
232         
233         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
234         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
235         g_return_if_fail (TNY_IS_MSG (msg));
236         
237         send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
238         if (!TNY_IS_SEND_QUEUE(send_queue))
239                 g_printerr ("modest: could not find send queue for account\n");
240         else {
241                 GError *err = NULL;
242                 tny_send_queue_add (send_queue, msg, &err);
243                 if (err) {
244                         g_printerr ("modest: error adding msg to send queue: %s\n",
245                                     err->message);
246                         g_error_free (err);
247                 } else
248                         g_message ("modest: message added to send queue");
249         }
250
251         /* Notify the queue */
252         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
253 }
254
255 void
256 modest_mail_operation_send_new_mail (ModestMailOperation *self,
257                                      TnyTransportAccount *transport_account,
258                                      const gchar *from,  const gchar *to,
259                                      const gchar *cc,  const gchar *bcc,
260                                      const gchar *subject, const gchar *plain_body,
261                                      const gchar *html_body,
262                                      const GList *attachments_list,
263                                      TnyHeaderFlags priority_flags)
264 {
265         TnyMsg *new_msg;
266         ModestMailOperationPrivate *priv = NULL;
267         /* GList *node = NULL; */
268
269         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
270         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
271
272         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
273
274         /* Check parametters */
275         if (to == NULL) {
276                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
277                              MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
278                              _("Error trying to send a mail. You need to set at least one recipient"));
279                 return;
280         }
281
282         if (html_body == NULL) {
283                 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
284         } else {
285                 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
286         }
287         if (!new_msg) {
288                 g_printerr ("modest: failed to create a new msg\n");
289                 return;
290         }
291
292         /* TODO: add priority handling. It's received in the priority_flags operator, and
293            it should have effect in the sending operation */
294
295         /* Call mail operation */
296         modest_mail_operation_send_mail (self, transport_account, new_msg);
297
298         /* Free */
299         g_object_unref (G_OBJECT (new_msg));
300 }
301
302 void
303 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
304                                       TnyTransportAccount *transport_account,
305                                       const gchar *from,  const gchar *to,
306                                       const gchar *cc,  const gchar *bcc,
307                                       const gchar *subject, const gchar *plain_body,
308                                       const gchar *html_body,
309                                       const GList *attachments_list,
310                                       TnyHeaderFlags priority_flags)
311 {
312         TnyMsg *msg = NULL;
313         TnyFolder *folder = NULL;
314         ModestMailOperationPrivate *priv = NULL;
315         GError *err = NULL;
316
317         /* GList *node = NULL; */
318
319         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
320         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
321
322         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
323
324         if (html_body == NULL) {
325                 msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
326         } else {
327                 msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
328         }
329         if (!msg) {
330                 g_printerr ("modest: failed to create a new msg\n");
331                 goto cleanup;
332         }
333
334         folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account), TNY_FOLDER_TYPE_DRAFTS);
335         if (!folder) {
336                 g_printerr ("modest: failed to find Drafts folder\n");
337                 goto cleanup;
338         }
339         
340         tny_folder_add_msg (folder, msg, &err);
341         if (err) {
342                 g_printerr ("modest: error adding msg to Drafts folder: %s",
343                             err->message);
344                 g_error_free (err);
345                 goto cleanup;
346         }
347
348         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
349
350         /* Free */
351 cleanup:
352         if (msg)
353                 g_object_unref (G_OBJECT(msg));
354         if (folder)
355                 g_object_unref (G_OBJECT(folder));
356 }
357
358 static void
359 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
360 {
361         TnyIterator *iter;
362         TnyList *folders = tny_simple_list_new ();
363
364         tny_folder_store_get_folders (store, folders, query, NULL);
365         iter = tny_list_create_iterator (folders);
366
367         while (!tny_iterator_is_done (iter)) {
368
369                 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
370
371                 tny_list_prepend (all_folders, G_OBJECT (folder));
372
373                 recurse_folders (folder, query, all_folders);
374             
375                 g_object_unref (G_OBJECT (folder));
376
377                 tny_iterator_next (iter);
378         }
379          g_object_unref (G_OBJECT (iter));
380          g_object_unref (G_OBJECT (folders));
381 }
382
383 static void
384 update_folders_status_cb (GObject *obj,
385                           TnyStatus *status,  
386                           gpointer user_data)
387 {
388         ModestMailOperation *self;
389         ModestMailOperationPrivate *priv;
390
391         g_return_if_fail (status != NULL);
392         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
393
394         self = MODEST_MAIL_OPERATION (user_data);
395         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
396
397         priv->done = status->position;
398         priv->total = status->of_total;
399
400         if (priv->done == 1 && priv->total == 100)
401                 return;
402
403         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
404 }
405
406 static void
407 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
408 {
409         ModestMailOperation *self;
410         ModestMailOperationPrivate *priv;
411         TnyIterator *iter;
412         TnyList *all_folders;
413         
414         self  = MODEST_MAIL_OPERATION (user_data);
415         priv  = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
416
417         g_message (__FUNCTION__);
418         
419         if (*err) {
420                 priv->error = g_error_copy (*err);
421                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
422                 goto out;
423         }
424
425         /* Get all the folders We can do it synchronously because
426            we're already running in a different thread than the UI */
427         all_folders = tny_list_copy (list);
428         iter = tny_list_create_iterator (all_folders);
429         while (!tny_iterator_is_done (iter)) {
430                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
431
432                 recurse_folders (folder, NULL, all_folders);
433                 tny_iterator_next (iter);
434         }
435         g_object_unref (G_OBJECT (iter));
436
437         /* Refresh folders */
438         iter = tny_list_create_iterator (all_folders);
439         priv->total = tny_list_get_length (all_folders);
440
441         while (!tny_iterator_is_done (iter) && !priv->error) {
442
443                 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
444
445                 /* Refresh the folder */
446                 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
447
448                 if (priv->error) {
449                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
450                 } else {        
451                         /* Update status and notify */
452                         priv->done++;
453                         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
454                 }
455     
456                 g_object_unref (G_OBJECT (folder));
457             
458                 tny_iterator_next (iter);
459         }
460
461         g_object_unref (G_OBJECT (iter));
462  out:
463         g_object_unref (G_OBJECT (list));
464
465         /* Check if the operation was a success */
466         if (priv->done == priv->total && !priv->error)
467                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
468
469         /* Free */
470         g_object_unref (G_OBJECT (folder_store));
471
472         /* Notify the queue */
473         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
474 }
475
476 gboolean
477 modest_mail_operation_update_account (ModestMailOperation *self,
478                                       TnyStoreAccount *store_account)
479 {
480         ModestMailOperationPrivate *priv;
481         TnyList *folders;
482
483         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
484         g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
485
486         /* Pick async call reference */
487         g_object_ref (store_account);
488
489         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
490
491         priv->total = 0;
492         priv->done  = 0;
493         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
494         
495         /* Get subscribed folders & refresh them */
496         folders = TNY_LIST (tny_simple_list_new ());
497
498         g_message ("tny_folder_store_get_folders_async");
499         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
500                                             folders, update_folders_cb, NULL, update_folders_status_cb, self);
501         
502         return TRUE;
503 }
504
505 ModestMailOperationStatus
506 modest_mail_operation_get_status (ModestMailOperation *self)
507 {
508         ModestMailOperationPrivate *priv;
509
510         g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
511         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
512                               MODEST_MAIL_OPERATION_STATUS_INVALID);
513
514         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
515         return priv->status;
516 }
517
518 const GError *
519 modest_mail_operation_get_error (ModestMailOperation *self)
520 {
521         ModestMailOperationPrivate *priv;
522
523         g_return_val_if_fail (self, NULL);
524         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
525
526         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
527         return priv->error;
528 }
529
530 gboolean 
531 modest_mail_operation_cancel (ModestMailOperation *self)
532 {
533         ModestMailOperationPrivate *priv;
534
535         if (!MODEST_IS_MAIL_OPERATION (self)) {
536                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
537                 return FALSE;
538         }
539
540         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
541
542         /* TODO: Tinymail does not support cancel operation  */
543         
544         /* Set new status */
545         priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
546
547         /* Notify the queue */
548         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
549
550         return TRUE;
551 }
552
553 guint 
554 modest_mail_operation_get_task_done (ModestMailOperation *self)
555 {
556         ModestMailOperationPrivate *priv;
557
558         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
559
560         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
561         return priv->done;
562 }
563
564 guint 
565 modest_mail_operation_get_task_total (ModestMailOperation *self)
566 {
567         ModestMailOperationPrivate *priv;
568
569         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
570
571         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
572         return priv->total;
573 }
574
575 gboolean
576 modest_mail_operation_is_finished (ModestMailOperation *self)
577 {
578         ModestMailOperationPrivate *priv;
579         gboolean retval = FALSE;
580
581         if (!MODEST_IS_MAIL_OPERATION (self)) {
582                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
583                 return retval;
584         }
585
586         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
587
588         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
589             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
590             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
591             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
592                 retval = TRUE;
593         } else {
594                 retval = FALSE;
595         }
596
597         return retval;
598 }
599
600 /* ******************************************************************* */
601 /* ************************** STORE  ACTIONS ************************* */
602 /* ******************************************************************* */
603
604
605 TnyFolder *
606 modest_mail_operation_create_folder (ModestMailOperation *self,
607                                      TnyFolderStore *parent,
608                                      const gchar *name)
609 {
610         ModestTnyFolderRules rules;
611         ModestMailOperationPrivate *priv;
612         TnyFolder *new_folder = NULL;
613         gboolean can_create = FALSE;
614
615         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
616         g_return_val_if_fail (name, NULL);
617         
618         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
619
620         /* Check parent */
621         if (!TNY_IS_FOLDER (parent)) {
622                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
623                              MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
624                              _("mail_in_ui_folder_create_error"));
625         } else {
626                 /* Check folder rules */
627                 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
628                 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
629                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
630                                      MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
631                                      _("mail_in_ui_folder_create_error"));
632                 else
633                         can_create = TRUE;              
634         }
635
636         if (can_create) {
637                 /* Create the folder */
638                 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
639                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
640         }
641
642         /* Notify the queue */
643         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
644
645         return new_folder;
646 }
647
648 void
649 modest_mail_operation_remove_folder (ModestMailOperation *self,
650                                      TnyFolder           *folder,
651                                      gboolean             remove_to_trash)
652 {
653         TnyAccount *account;
654         ModestMailOperationPrivate *priv;
655         ModestTnyFolderRules rules;
656
657         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
658         g_return_if_fail (TNY_IS_FOLDER (folder));
659
660         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
661
662         /* Check folder rules */
663         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
664         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
665                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
666                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
667                              _("mail_in_ui_folder_delete_error"));
668                 goto end;
669         }
670
671         /* Get the account */
672         account = tny_folder_get_account (folder);
673
674         /* Delete folder or move to trash */
675         if (remove_to_trash) {
676                 TnyFolder *trash_folder, *new_folder;
677                 trash_folder = modest_tny_account_get_special_folder (account,
678                                                                       TNY_FOLDER_TYPE_TRASH);
679                 /* TODO: error_handling */
680                 new_folder = modest_mail_operation_xfer_folder (self, folder, 
681                                                                 TNY_FOLDER_STORE (trash_folder), TRUE);
682                 g_object_unref (G_OBJECT (new_folder));
683         } else {
684                 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
685
686                 tny_folder_store_remove_folder (parent, folder, &(priv->error));
687                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
688
689                 if (parent)
690                         g_object_unref (G_OBJECT (parent));
691         }
692         g_object_unref (G_OBJECT (account));
693
694  end:
695         /* Notify the queue */
696         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
697 }
698
699 void
700 modest_mail_operation_rename_folder (ModestMailOperation *self,
701                                      TnyFolder *folder,
702                                      const gchar *name)
703 {
704         ModestMailOperationPrivate *priv;
705         ModestTnyFolderRules rules;
706
707         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
708         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
709         g_return_if_fail (name);
710         
711         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
712
713         /* Check folder rules */
714         rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
715         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
716                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
717                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
718                              _("FIXME: unable to rename"));
719         } else {
720                 /* Rename. Camel handles folder subscription/unsubscription */
721                 tny_folder_set_name (folder, name, &(priv->error));
722                 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
723         }
724
725         /* Notify the queue */
726         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
727  }
728
729 TnyFolder *
730 modest_mail_operation_xfer_folder (ModestMailOperation *self,
731                                    TnyFolder *folder,
732                                    TnyFolderStore *parent,
733                                    gboolean delete_original)
734 {
735         ModestMailOperationPrivate *priv;
736         TnyFolder *new_folder = NULL;
737         ModestTnyFolderRules rules;
738
739         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
740         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
741         g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
742
743         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
744
745         /* The moveable restriction is applied also to copy operation */
746         rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
747         if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
748                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
749                              MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
750                              _("FIXME: unable to rename"));
751         } else {
752                 /* Move/Copy folder */
753                 new_folder = tny_folder_copy (folder,
754                                               parent,
755                                               tny_folder_get_name (folder),
756                                               delete_original, 
757                                               &(priv->error));
758         }
759
760         /* Notify the queue */
761         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
762
763         return new_folder;
764 }
765
766
767 /* ******************************************************************* */
768 /* **************************  MSG  ACTIONS  ************************* */
769 /* ******************************************************************* */
770
771 void          modest_mail_operation_process_msg     (ModestMailOperation *self,
772                                                      TnyHeader *header, 
773                                                      guint num_ops,
774                                                      TnyGetMsgCallback user_callback,
775                                                      gpointer user_data)
776 {
777         TnyFolder *folder;
778         ModestMailOperationPrivate *priv;
779         
780         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
781         g_return_if_fail (TNY_IS_HEADER (header));
782         
783         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
784         folder = tny_header_get_folder (header);
785
786         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
787         priv->total = num_ops;
788
789         /* Get message from folder */
790         if (folder) {
791                 /* The callback will call it per each header */
792                 tny_folder_get_msg_async (folder, header, user_callback, update_process_msg_status_cb, user_data);
793                 g_object_unref (G_OBJECT (folder));
794         } else {
795                 /* Set status failed and set an error */
796                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
797                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
798                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
799                              _("Error trying to get a message. No folder found for header"));
800         }
801 }
802
803 static void     
804 update_process_msg_status_cb (GObject *obj,
805                               TnyStatus *status,  
806                               gpointer user_data)
807 {
808         ModestMailOperation *self;
809         ModestMailOperationPrivate *priv;
810
811         g_return_if_fail (status != NULL);
812         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
813
814         self = MODEST_MAIL_OPERATION (user_data);
815         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
816
817         priv->done += status->position;
818
819         if (priv->done == 1 && priv->total == 100)
820                 return;
821
822         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
823 }
824
825
826
827 void 
828 modest_mail_operation_remove_msg (ModestMailOperation *self,
829                                   TnyHeader *header,
830                                   gboolean remove_to_trash)
831 {
832         TnyFolder *folder;
833         ModestMailOperationPrivate *priv;
834
835         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
836         g_return_if_fail (TNY_IS_HEADER (header));
837
838         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
839         folder = tny_header_get_folder (header);
840
841         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
842
843         /* Delete or move to trash */
844         if (remove_to_trash) {
845                 TnyFolder *trash_folder;
846                 TnyStoreAccount *store_account;
847
848                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
849                 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
850                                                                       TNY_FOLDER_TYPE_TRASH);
851                 if (trash_folder) {
852                         TnyList *headers;
853
854                         /* Create list */
855                         headers = tny_simple_list_new ();
856                         tny_list_append (headers, G_OBJECT (header));
857                         g_object_unref (header);
858
859                         /* Move to trash */
860                         modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
861                         g_object_unref (headers);
862 /*                      g_object_unref (trash_folder); */
863                 } else {
864                         ModestMailOperationPrivate *priv;
865
866                         /* Set status failed and set an error */
867                         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
868                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
869                         g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
870                                      MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
871                                      _("Error trying to delete a message. Trash folder not found"));
872                 }
873
874                 g_object_unref (G_OBJECT (store_account));
875         } else {
876                 tny_folder_remove_msg (folder, header, &(priv->error));
877                 if (!priv->error)
878                         tny_folder_sync(folder, TRUE, &(priv->error));
879         }
880
881         /* Set status */
882         if (!priv->error)
883                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
884         else
885                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
886
887         /* Free */
888         g_object_unref (G_OBJECT (folder));
889
890         /* Notify the queue */
891         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
892 }
893
894 static void
895 transfer_msgs_status_cb (GObject *obj,
896                          TnyStatus *status,  
897                          gpointer user_data)
898 {
899 }
900
901
902 static void
903 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
904 {
905         XFerMsgAsyncHelper *helper;
906         ModestMailOperation *self;
907         ModestMailOperationPrivate *priv;
908
909         helper = (XFerMsgAsyncHelper *) user_data;
910         self = helper->mail_op;
911
912         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
913
914         if (*err) {
915                 priv->error = g_error_copy (*err);
916                 priv->done = 0;
917                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
918         } else {
919                 priv->done = 1;
920                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
921         }
922
923         /* Free */
924         g_object_unref (helper->headers);
925         g_object_unref (helper->dest_folder);
926         g_object_unref (folder);
927         g_free (helper);
928
929         /* Notify the queue */
930         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
931 }
932
933 void
934 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
935                                  TnyList *headers, 
936                                  TnyFolder *folder, 
937                                  gboolean delete_original)
938 {
939         ModestMailOperationPrivate *priv;
940         TnyIterator *iter;
941         TnyFolder *src_folder;
942         XFerMsgAsyncHelper *helper;
943         TnyHeader *header;
944
945         g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
946         g_return_if_fail (TNY_IS_LIST (headers));
947         g_return_if_fail (TNY_IS_FOLDER (folder));
948
949         /* Pick references for async calls */
950         g_object_ref (folder);
951
952         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
953         priv->total = 1;
954         priv->done = 0;
955         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
956
957         /* Create the helper */
958         helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
959         helper->mail_op = self;
960         helper->dest_folder = folder;
961         helper->headers = headers;
962
963         /* Get source folder */
964         iter = tny_list_create_iterator (headers);
965         header = TNY_HEADER (tny_iterator_get_current (iter));
966         src_folder = tny_header_get_folder (header);
967         g_object_unref (header);
968         g_object_unref (iter);
969
970         /* Transfer messages */
971         tny_folder_transfer_msgs_async (src_folder, 
972                                         headers, 
973                                         folder, 
974                                         delete_original, 
975                                         transfer_msgs_cb, 
976                                         transfer_msgs_status_cb, 
977                                         helper);
978 }
979
980
981 static void
982 on_refresh_folder (TnyFolder   *folder, 
983                    gboolean     cancelled, 
984                    GError     **error,
985                    gpointer     user_data)
986 {
987         ModestMailOperation *self;
988         ModestMailOperationPrivate *priv;
989
990         self = MODEST_MAIL_OPERATION (user_data);
991         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
992
993         if (*error) {
994                 priv->error = g_error_copy (*error);
995                 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
996                 goto out;
997         }
998
999         if (cancelled) {
1000                 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
1001                 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1002                              MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1003                              _("Error trying to refresh the contents of %s"),
1004                              tny_folder_get_name (folder));
1005                 goto out;
1006         }
1007
1008         priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1009
1010  out:
1011         /* Free */
1012         g_object_unref (folder);
1013
1014         /* Notify the queue */
1015         modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
1016 }
1017
1018 static void
1019 on_refresh_folder_status_update (GObject *obj,
1020                                  TnyStatus *status,
1021                                  gpointer user_data)
1022 {
1023         ModestMailOperation *self;
1024         ModestMailOperationPrivate *priv;
1025
1026         g_return_if_fail (status != NULL);
1027         g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
1028
1029         /* Temporary FIX: useful when tinymail send us status
1030            information *after* calling the function callback */
1031         if (!MODEST_IS_MAIL_OPERATION (user_data))
1032                 return;
1033
1034         self = MODEST_MAIL_OPERATION (user_data);
1035         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1036
1037         priv->done = status->position;
1038         priv->total = status->of_total;
1039
1040         if (priv->done == 1 && priv->total == 100)
1041                 return;
1042
1043         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
1044 }
1045
1046 void 
1047 modest_mail_operation_refresh_folder  (ModestMailOperation *self,
1048                                        TnyFolder *folder)
1049 {
1050         ModestMailOperationPrivate *priv;
1051
1052         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1053
1054         /* Pick a reference */
1055         g_object_ref (folder);
1056
1057         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1058
1059         /* Refresh the folder. TODO: tinymail could issue a status
1060            updates before the callback call then this could happen. We
1061            must review the design */
1062         tny_folder_refresh_async (folder,
1063                                   on_refresh_folder,
1064                                   on_refresh_folder_status_update,
1065                                   self);
1066 }
1067
1068 void
1069 _modest_mail_operation_notify_end (ModestMailOperation *self)
1070 {
1071         g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
1072 }