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