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