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