* Replaced tny_folder_get_msg by its async version
[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-msg.h>
39 #include <tny-camel-header.h>
40 #include <tny-camel-stream.h>
41 #include <tny-camel-mime-part.h>
42 #include <tny-simple-list.h>
43 #include <camel/camel-stream-mem.h>
44 #include <glib/gi18n.h>
45
46 #include "modest-text-utils.h"
47 #include "modest-tny-msg-actions.h"
48 #include "modest-tny-platform-factory.h"
49 #include "modest-marshal.h"
50 #include "modest-formatter.h"
51
52 /* 'private'/'protected' functions */
53 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
54 static void modest_mail_operation_init       (ModestMailOperation *obj);
55 static void modest_mail_operation_finalize   (GObject *obj);
56
57 #define MODEST_ERROR modest_error_quark ()
58
59 typedef enum _ModestMailOperationErrorCode ModestMailOperationErrorCode;
60 enum _ModestMailOperationErrorCode {
61         MODEST_MAIL_OPERATION_ERROR_BAD_ACCOUNT,
62         MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
63         MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
64
65         MODEST_MAIL_OPERATION_NUM_ERROR_CODES
66 };
67
68 static void     set_error            (ModestMailOperation *mail_operation, 
69                                       ModestMailOperationErrorCode error_code,
70                                       const gchar *fmt, ...);
71 static void     status_update_cb     (TnyFolder *folder, 
72                                       const gchar *what, 
73                                       gint status, 
74                                       gint oftotal,
75                                       gpointer user_data);
76 static void     folder_refresh_cb    (TnyFolder *folder, 
77                                       gboolean canceled,
78                                       GError **err,
79                                       gpointer user_data);
80 static void     update_folders_cb    (TnyFolderStore *self, 
81                                       TnyList *list, 
82                                       GError **err, 
83                                       gpointer user_data);
84 static void     add_attachments      (TnyMsg *msg, 
85                                       GList *attachments_list);
86
87
88 static TnyMimePart *         add_body_part    (TnyMsg *msg, 
89                                                const gchar *body,
90                                                const gchar *content_type, 
91                                                gboolean has_attachments);
92
93
94 static void modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
95                                                TnyFolder *folder,
96                                                TnyFolderStore *parent,
97                                                gboolean delete_original);
98
99 static void modest_mail_operation_xfer_msg    (ModestMailOperation *mail_op,
100                                                TnyHeader *header, 
101                                                TnyFolder *folder, 
102                                                gboolean delete_original);
103
104 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
105                                                             TnyStoreAccount *store_account);
106
107
108 enum _ModestMailOperationSignals 
109 {
110         PROGRESS_CHANGED_SIGNAL,
111
112         NUM_SIGNALS
113 };
114
115 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
116 struct _ModestMailOperationPrivate {
117         guint                      failed;
118         guint                      canceled;
119         guint                      done;
120         guint                      total;
121         GMutex                    *cb_lock;
122         ModestMailOperationStatus  status;
123         GError                    *error;
124 };
125 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
126                                                    MODEST_TYPE_MAIL_OPERATION, \
127                                                    ModestMailOperationPrivate))
128
129 /* some utility functions */
130 static char * get_content_type(const gchar *s);
131 static gboolean is_ascii(const gchar *s);
132
133 /* globals */
134 static GObjectClass *parent_class = NULL;
135
136 static guint signals[NUM_SIGNALS] = {0};
137
138 GType
139 modest_mail_operation_get_type (void)
140 {
141         static GType my_type = 0;
142         if (!my_type) {
143                 static const GTypeInfo my_info = {
144                         sizeof(ModestMailOperationClass),
145                         NULL,           /* base init */
146                         NULL,           /* base finalize */
147                         (GClassInitFunc) modest_mail_operation_class_init,
148                         NULL,           /* class finalize */
149                         NULL,           /* class data */
150                         sizeof(ModestMailOperation),
151                         1,              /* n_preallocs */
152                         (GInstanceInitFunc) modest_mail_operation_init,
153                         NULL
154                 };
155                 my_type = g_type_register_static (G_TYPE_OBJECT,
156                                                   "ModestMailOperation",
157                                                   &my_info, 0);
158         }
159         return my_type;
160 }
161
162 static void
163 modest_mail_operation_class_init (ModestMailOperationClass *klass)
164 {
165         GObjectClass *gobject_class;
166         gobject_class = (GObjectClass*) klass;
167
168         parent_class            = g_type_class_peek_parent (klass);
169         gobject_class->finalize = modest_mail_operation_finalize;
170
171         g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
172
173         /* signal definitions go here, e.g.: */
174         signals[PROGRESS_CHANGED_SIGNAL] = 
175                 g_signal_new ("progress_changed",
176                               G_TYPE_FROM_CLASS (gobject_class),
177                               G_SIGNAL_RUN_FIRST,
178                               G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
179                               NULL, NULL,
180                               g_cclosure_marshal_VOID__VOID,
181                               G_TYPE_NONE, 0);
182 }
183
184 static void
185 modest_mail_operation_init (ModestMailOperation *obj)
186 {
187         ModestMailOperationPrivate *priv;
188
189         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
190
191         priv->status   = MODEST_MAIL_OPERATION_STATUS_INVALID;
192         priv->error    = NULL;
193         priv->done     = 0;
194         priv->failed   = 0;
195         priv->canceled = 0;
196         priv->total    = 0;
197         priv->cb_lock  = g_mutex_new ();
198 }
199
200 static void
201 modest_mail_operation_finalize (GObject *obj)
202 {
203         ModestMailOperationPrivate *priv;
204
205         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
206
207         if (priv->error) {
208                 g_error_free (priv->error);
209                 priv->error = NULL;
210         }
211
212         g_mutex_free (priv->cb_lock);
213
214         G_OBJECT_CLASS(parent_class)->finalize (obj);
215 }
216
217 ModestMailOperation*
218 modest_mail_operation_new (void)
219 {
220         return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
221 }
222
223
224 void
225 modest_mail_operation_send_mail (ModestMailOperation *mail_op,
226                                  TnyTransportAccount *transport_account,
227                                  TnyMsg* msg)
228 {
229         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
230         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
231
232         tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
233 }
234
235 void
236 modest_mail_operation_send_new_mail (ModestMailOperation *mail_op,
237                                      TnyTransportAccount *transport_account,
238                                      const gchar *from,
239                                      const gchar *to,
240                                      const gchar *cc,
241                                      const gchar *bcc,
242                                      const gchar *subject,
243                                      const gchar *body,
244                                      const GList *attachments_list)
245 {
246         TnyMsg *new_msg;
247         TnyHeader *header;
248         gchar *content_type;
249
250         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
251         g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
252
253         /* Check parametters */
254         if (to == NULL) {
255                 set_error (mail_op,
256                            MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
257                            _("Error trying to send a mail. You need to set almost one a recipient"));
258                 return;
259         }
260
261         /* Create new msg */
262         new_msg          = TNY_MSG (tny_camel_msg_new ());
263         header           = TNY_HEADER (tny_camel_header_new ());
264
265         /* WARNING: set the header before assign values to it */
266         tny_msg_set_header (new_msg, header);
267         tny_header_set_from (TNY_HEADER (header), from);
268         tny_header_set_replyto (TNY_HEADER (header), from);
269         tny_header_set_to (TNY_HEADER (header), to);
270         tny_header_set_cc (TNY_HEADER (header), cc);
271         tny_header_set_bcc (TNY_HEADER (header), bcc);
272         tny_header_set_subject (TNY_HEADER (header), subject);
273
274         content_type = get_content_type(body);
275
276         /* Add the body of the new mail */      
277         add_body_part (new_msg, body, (const gchar *) content_type,
278                        (attachments_list == NULL) ? FALSE : TRUE);
279
280         /* Add attachments */
281         add_attachments (new_msg, (GList*) attachments_list);
282
283         /* Send mail */
284         tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
285
286         /* Clean */
287         g_object_unref (header);
288         g_object_unref (new_msg);
289         g_free(content_type);
290 }
291
292 static void
293 add_if_attachment (gpointer data, gpointer user_data)
294 {
295         TnyMimePart *part;
296         GList *attachments_list;
297
298         part = TNY_MIME_PART (data);
299         attachments_list = (GList *) user_data;
300
301         if (tny_mime_part_is_attachment (part))
302                 attachments_list = g_list_prepend (attachments_list, part);
303 }
304
305
306 static TnyMsg *
307 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
308 {
309         TnyMsg *new_msg;
310         TnyHeader *new_header, *header;
311         gchar *new_subject;
312         TnyMimePart *body;
313         ModestFormatter *formatter;
314
315         /* Get body from original msg */
316         header = tny_msg_get_header (msg);
317         body   = modest_tny_msg_actions_find_body_part (msg, TRUE);
318
319         /* TODO: select the formatter from account prefs */
320         formatter = modest_formatter_new ("text/plain");
321
322         /* Format message body */
323         if (is_reply) {
324                 switch (type) {
325                 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
326                 default:
327                         new_msg = modest_formatter_cite  (formatter, body, header);
328                         break;
329                 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
330                         new_msg = modest_formatter_quote (formatter, body, header);
331                         break;
332                 }
333         } else {
334                 switch (type) {
335                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
336                 default:
337                         new_msg = modest_formatter_inline  (formatter, body, header);
338                         break;
339                 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
340                         new_msg = modest_formatter_attach (formatter, body, header);
341                         break;
342                 }
343         }
344         g_object_unref (G_OBJECT (formatter));
345
346         /* Fill the header */
347         new_header = TNY_HEADER (tny_camel_header_new ());
348         tny_msg_set_header  (new_msg, new_header);
349         tny_header_set_from (new_header, from);
350         tny_header_set_replyto (new_header, from);
351
352         /* Change the subject */
353         new_subject = (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header), 
354                                                                    (is_reply) ? _("Re:") : _("Fwd:"));
355         tny_header_set_subject (new_header, (const gchar *) new_subject);
356         g_free (new_subject);
357
358         /* Clean */
359         g_object_unref (G_OBJECT (new_header));
360         g_object_unref (G_OBJECT (header));
361
362         return new_msg;
363 }
364
365 /**
366  * modest_mail_operation_create_forward_mail:
367  * @msg: a valid #TnyMsg instance
368  * @forward_type: the type of forwarded message
369  * 
370  * creates a forwarded message from an existing one
371  * 
372  * Returns: a new #TnyMsg, or NULL in case of error
373  **/
374 TnyMsg* 
375 modest_mail_operation_create_forward_mail (TnyMsg *msg, 
376                                            const gchar *from,
377                                            ModestMailOperationForwardType forward_type)
378 {
379         TnyMsg *new_msg;
380         TnyList *parts = NULL;
381         GList *attachments_list = NULL;
382
383         new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
384
385         /* Add attachments */
386         parts = TNY_LIST (tny_simple_list_new());
387         tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
388         tny_list_foreach (parts, add_if_attachment, attachments_list);
389         add_attachments (new_msg, attachments_list);
390
391         /* Clean */
392         if (attachments_list) g_list_free (attachments_list);
393         g_object_unref (G_OBJECT (parts));
394
395         return new_msg;
396 }
397
398 /**
399  * modest_mail_operation_create_reply_mail:
400  * @msg: a valid #TnyMsg instance
401  * @reply_type: the format of the new message
402  * @reply_mode: the mode of reply, to the sender only, to a mail list or to all
403  * 
404  * creates a new message to reply to an existing one
405  * 
406  * Returns: Returns: a new #TnyMsg, or NULL in case of error
407  **/
408 TnyMsg* 
409 modest_mail_operation_create_reply_mail (TnyMsg *msg, 
410                                          const gchar *from,
411                                          ModestMailOperationReplyType reply_type,
412                                          ModestMailOperationReplyMode reply_mode)
413 {
414         TnyMsg *new_msg;
415         TnyHeader *new_header, *header;
416         const gchar* reply_to;
417
418         new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
419
420         /* Fill the header */
421         header = tny_msg_get_header (msg);
422         new_header = tny_msg_get_header (new_msg);
423         reply_to = tny_header_get_replyto (header);
424         if (reply_to)
425                 tny_header_set_to (new_header, reply_to);
426         else
427                 tny_header_set_to (new_header, tny_header_get_from (header));
428
429         switch (reply_mode) {
430                 gchar *new_cc = NULL;
431                 const gchar *cc = NULL, *bcc = NULL;
432                 GString *tmp = NULL;
433
434         case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
435                 /* Do not fill neither cc nor bcc */
436                 break;
437         case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
438                 /* TODO */
439                 break;
440         case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
441                 /* Concatenate to, cc and bcc */
442                 cc = tny_header_get_cc (header);
443                 bcc = tny_header_get_bcc (header);
444
445                 tmp = g_string_new (tny_header_get_to (header));
446                 if (cc)  g_string_append_printf (tmp, ",%s",cc);
447                 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
448
449
450                /* Remove my own address from the cc list. TODO:
451                   remove also the To: of the new message, needed due
452                   to the new reply_to feature */
453                 new_cc = (gchar *) 
454                         modest_text_utils_remove_address ((const gchar *) tmp->str, 
455                                                           from);
456                 /* FIXME: remove also the mails from the new To: */
457                 tny_header_set_cc (new_header, new_cc);
458
459                 /* Clean */
460                 g_string_free (tmp, TRUE);
461                 g_free (new_cc);
462                 break;
463         }
464
465         /* Clean */
466         g_object_unref (G_OBJECT (new_header));
467         g_object_unref (G_OBJECT (header));
468
469         return new_msg;
470 }
471
472 static void
473 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data) 
474 {
475         g_print ("%s status: %d, of total %d\n", what, status, oftotal);
476 }
477
478 static void
479 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
480 {
481         ModestMailOperation *mail_op = NULL;
482         ModestMailOperationPrivate *priv = NULL;
483
484         mail_op = MODEST_MAIL_OPERATION (user_data);
485         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
486
487         g_mutex_lock (priv->cb_lock);
488
489         if ((canceled && *err) || *err) {
490                 priv->error = g_error_copy (*err);
491                 priv->failed++;
492         } else if (canceled) {
493                 priv->canceled++;
494                 set_error (mail_op,
495                            MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
496                            _("Error trying to refresh folder %s. Operation canceled"),
497                            tny_folder_get_name (folder));
498         } else {
499                 priv->done++;
500         }
501
502         if (priv->done == priv->total)
503                 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
504         else if ((priv->done + priv->canceled + priv->failed) == priv->total)
505                 if (priv->failed == priv->total)
506                         priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
507                 else if (priv->failed == priv->total)
508                         priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
509                 else
510                         priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
511
512         g_mutex_unlock (priv->cb_lock);
513
514         g_signal_emit (G_OBJECT (mail_op), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
515 }
516
517
518 static void
519 update_folders_cb (TnyFolderStore *self, TnyList *list, GError **err, gpointer user_data)
520 {
521         ModestMailOperation *mail_op;
522         ModestMailOperationPrivate *priv;
523         TnyList *folders;
524         TnyIterator *ifolders;
525         TnyFolder *cur_folder;
526
527         mail_op = MODEST_MAIL_OPERATION (user_data);
528         priv    = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
529
530         ifolders = tny_list_create_iterator (list);
531         priv->total = tny_list_get_length (list);
532         priv->done = 0;
533         priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
534
535         /* Async refresh folders. Reference the mail_op because
536            tinymail destroys the user_data */   
537         for (tny_iterator_first (ifolders); 
538              !tny_iterator_is_done (ifolders); 
539              tny_iterator_next (ifolders)) {
540                 
541                 cur_folder = TNY_FOLDER (tny_iterator_get_current (ifolders));
542                 tny_folder_refresh_async (cur_folder, 
543                                           folder_refresh_cb,
544                                           status_update_cb, g_object_ref (mail_op));
545         }
546         
547         g_object_unref (G_OBJECT (ifolders));
548         g_object_unref (G_OBJECT (list));
549 }
550
551 gboolean
552 modest_mail_operation_update_account (ModestMailOperation *mail_op,
553                                       TnyStoreAccount *store_account)
554 {
555         ModestMailOperationPrivate *priv;
556         TnyList *folders;
557         TnyFolderStoreQuery *query;
558
559         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
560         g_return_if_fail (TNY_IS_STORE_ACCOUNT(store_account));
561
562         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
563
564         /* Get subscribed folders & refresh them */
565         folders = TNY_LIST (tny_simple_list_new ());
566         query = tny_folder_store_query_new ();
567         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
568         tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
569                                             folders, update_folders_cb, query, mail_op);
570         g_object_unref (query);
571
572         return TRUE;
573 }
574
575 ModestMailOperationStatus
576 modest_mail_operation_get_status (ModestMailOperation *mail_op)
577 {
578         ModestMailOperationPrivate *priv;
579
580 /*      g_return_val_if_fail (mail_op, MODEST_MAIL_OPERATION_STATUS_INVALID); */
581 /*      g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op),  */
582 /*                            MODEST_MAIL_OPERATION_STATUS_INVALID); */
583
584         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
585         return priv->status;
586 }
587
588 const GError *
589 modest_mail_operation_get_error (ModestMailOperation *mail_op)
590 {
591         ModestMailOperationPrivate *priv;
592
593 /*      g_return_val_if_fail (mail_op, NULL); */
594 /*      g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), NULL); */
595
596         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
597         return priv->error;
598 }
599
600 void 
601 modest_mail_operation_cancel (ModestMailOperation *mail_op)
602 {
603         /* TODO */
604 }
605
606 guint 
607 modest_mail_operation_get_task_done (ModestMailOperation *mail_op)
608 {
609         ModestMailOperationPrivate *priv;
610
611         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
612
613         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
614         return priv->done;
615 }
616
617 guint 
618 modest_mail_operation_get_task_total (ModestMailOperation *mail_op)
619 {
620         ModestMailOperationPrivate *priv;
621
622         g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (mail_op), 0);
623
624         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
625         return priv->total;
626 }
627
628 gboolean
629 modest_mail_operation_is_finished (ModestMailOperation *mail_op)
630 {
631         ModestMailOperationPrivate *priv;
632         gboolean retval = FALSE;
633
634         if (!MODEST_IS_MAIL_OPERATION (mail_op)) {
635                 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
636                 return retval;
637         }
638
639         priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
640
641         g_mutex_lock (priv->cb_lock);
642
643         if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS   ||
644             priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED    ||
645             priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED  ||
646             priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
647                 retval = TRUE;
648         } else {
649                 retval = FALSE;
650         }
651         g_mutex_unlock (priv->cb_lock);
652
653         return retval;
654 }
655
656 /* ******************************************************************* */
657 /* ************************** STORE  ACTIONS ************************* */
658 /* ******************************************************************* */
659
660
661 TnyFolder *
662 modest_mail_operation_create_folder (ModestMailOperation *mail_op,
663                                      TnyFolderStore *parent,
664                                      const gchar *name)
665 {
666         g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
667         g_return_val_if_fail (name, NULL);
668
669         TnyFolder *new_folder = NULL;
670         TnyStoreAccount *store_account;
671
672         /* Create the folder */
673         new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
674         if (!new_folder) 
675                 return NULL;
676
677         /* Subscribe to folder */
678         if (!tny_folder_is_subscribed (new_folder)) {
679                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
680                 tny_store_account_subscribe (store_account, new_folder);
681                 g_object_unref (G_OBJECT (store_account));
682         }
683
684         return new_folder;
685 }
686
687 void
688 modest_mail_operation_remove_folder (ModestMailOperation *mail_op,
689                                      TnyFolder *folder,
690                                      gboolean remove_to_trash)
691 {
692         TnyFolderStore *folder_store;
693
694         g_return_if_fail (TNY_IS_FOLDER (folder));
695
696         /* Get folder store */
697         folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
698
699         /* Delete folder or move to trash */
700         if (remove_to_trash) {
701                 TnyFolder *trash_folder;
702
703                 trash_folder = modest_mail_operation_find_trash_folder (mail_op,
704                                                                         TNY_STORE_ACCOUNT (folder_store));
705
706                 /* TODO: error_handling */
707                 modest_mail_operation_move_folder (mail_op, 
708                                                    folder, 
709                                                    TNY_FOLDER_STORE (trash_folder));
710         } else {
711                 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
712                 g_object_unref (G_OBJECT (folder));
713         }
714
715         /* Free instances */
716         g_object_unref (G_OBJECT (folder_store));
717 }
718
719 void
720 modest_mail_operation_rename_folder (ModestMailOperation *mail_op,
721                                      TnyFolder *folder,
722                                      const gchar *name)
723 {
724         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
725         g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
726
727         /* FIXME: better error handling */
728         if (strrchr (name, '/') != NULL)
729                 return;
730
731         /* Rename. Camel handles folder subscription/unsubscription */
732         tny_folder_set_name (folder, name, NULL); /* FIXME */
733  }
734
735 void
736 modest_mail_operation_move_folder (ModestMailOperation *mail_op,
737                                    TnyFolder *folder,
738                                    TnyFolderStore *parent)
739 {
740         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
741         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
742         g_return_if_fail (TNY_IS_FOLDER (folder));
743         
744         modest_mail_operation_xfer_folder (mail_op, folder, parent, TRUE);
745 }
746
747 void
748 modest_mail_operation_copy_folder (ModestMailOperation *mail_op,
749                                    TnyFolder *folder,
750                                    TnyFolderStore *parent)
751 {
752         g_return_if_fail (MODEST_IS_MAIL_OPERATION (mail_op));
753         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
754         g_return_if_fail (TNY_IS_FOLDER (folder));
755
756         modest_mail_operation_xfer_folder (mail_op, folder, parent, FALSE);
757 }
758
759 static void
760 modest_mail_operation_xfer_folder (ModestMailOperation *mail_op,
761                                    TnyFolder *folder,
762                                    TnyFolderStore *parent,
763                                    gboolean delete_original)
764 {
765         const gchar *folder_name;
766         TnyFolder *dest_folder, *child;
767         TnyIterator *iter;
768         TnyList *folders, *headers;
769
770         g_return_if_fail (TNY_IS_FOLDER (folder));
771         g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
772
773         /* Create the destination folder */
774         folder_name = tny_folder_get_name (folder);
775         dest_folder = modest_mail_operation_create_folder (mail_op, 
776                                                            parent, folder_name);
777
778         /* Transfer messages */
779         headers = TNY_LIST (tny_simple_list_new ());
780         tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
781         tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
782
783         /* Recurse children */
784         folders = TNY_LIST (tny_simple_list_new ());
785         tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
786         iter = tny_list_create_iterator (folders);
787
788         while (!tny_iterator_is_done (iter)) {
789
790                 child = TNY_FOLDER (tny_iterator_get_current (iter));
791                 modest_mail_operation_xfer_folder (mail_op, child,
792                                                    TNY_FOLDER_STORE (dest_folder),
793                                                    delete_original);
794                 tny_iterator_next (iter);
795         }
796
797         /* Delete source folder (if needed) */
798         if (delete_original)
799                 modest_mail_operation_remove_folder (mail_op, folder, FALSE);
800
801         /* Clean up */
802         g_object_unref (G_OBJECT (dest_folder));
803         g_object_unref (G_OBJECT (headers));
804         g_object_unref (G_OBJECT (folders));
805         g_object_unref (G_OBJECT (iter));
806 }
807
808 static TnyFolder *
809 modest_mail_operation_find_trash_folder (ModestMailOperation *mail_op,
810                                          TnyStoreAccount *store_account)
811 {
812         TnyList *folders;
813         TnyIterator *iter;
814         gboolean found;
815         /*TnyFolderStoreQuery *query;*/
816         TnyFolder *trash_folder;
817
818         /* Look for Trash folder */
819         folders = TNY_LIST (tny_simple_list_new ());
820         tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
821                                       folders, NULL, NULL); /* FIXME */
822         iter = tny_list_create_iterator (folders);
823
824         found = FALSE;
825         while (!tny_iterator_is_done (iter) && !found) {
826
827                 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
828                 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
829                         found = TRUE;
830                 else
831                         tny_iterator_next (iter);
832         }
833
834         /* Clean up */
835         g_object_unref (G_OBJECT (folders));
836         g_object_unref (G_OBJECT (iter));
837
838         /* TODO: better error handling management */
839         if (!found) 
840                 return NULL;
841         else
842                 return trash_folder;
843 }
844
845 /* ******************************************************************* */
846 /* **************************  MSG  ACTIONS  ************************* */
847 /* ******************************************************************* */
848
849 void 
850 modest_mail_operation_copy_msg (ModestMailOperation *mail_op,
851                                 TnyHeader *header, 
852                                 TnyFolder *folder)
853 {
854         g_return_if_fail (TNY_IS_HEADER (header));
855         g_return_if_fail (TNY_IS_FOLDER (folder));
856
857         modest_mail_operation_xfer_msg (mail_op, header, folder, FALSE);
858 }
859
860 void 
861 modest_mail_operation_move_msg (ModestMailOperation *mail_op,
862                                 TnyHeader *header, 
863                                 TnyFolder *folder)
864 {
865         g_return_if_fail (TNY_IS_HEADER (header));
866         g_return_if_fail (TNY_IS_FOLDER (folder));
867
868         modest_mail_operation_xfer_msg (mail_op, header, folder, TRUE);
869 }
870
871 void 
872 modest_mail_operation_remove_msg (ModestMailOperation *mail_op,
873                                   TnyHeader *header,
874                                   gboolean remove_to_trash)
875 {
876         TnyFolder *folder;
877
878         g_return_if_fail (TNY_IS_HEADER (header));
879
880         folder = tny_header_get_folder (header);
881
882         /* Delete or move to trash */
883         if (remove_to_trash) {
884                 TnyFolder *trash_folder;
885                 TnyStoreAccount *store_account;
886
887                 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
888                 trash_folder = modest_mail_operation_find_trash_folder (mail_op, store_account);
889
890                 modest_mail_operation_move_msg (mail_op, header, trash_folder);
891
892                 g_object_unref (G_OBJECT (store_account));
893         } else {
894                 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
895                 tny_folder_expunge (folder, NULL); /* FIXME */
896         }
897
898         /* Free */
899         g_object_unref (folder);
900 }
901
902 static void
903 modest_mail_operation_xfer_msg (ModestMailOperation *mail_op,
904                                 TnyHeader *header, 
905                                 TnyFolder *folder, 
906                                 gboolean delete_original)
907 {
908         TnyFolder *src_folder;
909         TnyList *headers;
910
911         src_folder = tny_header_get_folder (header);
912         headers = tny_simple_list_new ();
913
914         /* Move */
915         tny_list_prepend (headers, G_OBJECT (header));
916         tny_folder_transfer_msgs (src_folder, headers, folder, delete_original, NULL); /* FIXME */
917
918         /* Free */
919         g_object_unref (headers);
920         g_object_unref (folder);
921 }
922
923
924 /* ******************************************************************* */
925 /* ************************* UTILIY FUNCTIONS ************************ */
926 /* ******************************************************************* */
927 static gboolean
928 is_ascii(const gchar *s)
929 {
930         while (s[0]) {
931                 if (s[0] & 128 || s[0] < 32)
932                         return FALSE;
933                 s++;
934         }
935         return TRUE;
936 }
937
938 static char *
939 get_content_type(const gchar *s)
940 {
941         GString *type;
942         
943         type = g_string_new("text/plain");
944         if (!is_ascii(s)) {
945                 if (g_utf8_validate(s, -1, NULL)) {
946                         g_string_append(type, "; charset=\"utf-8\"");
947                 } else {
948                         /* it should be impossible to reach this, but better safe than sorry */
949                         g_warning("invalid utf8 in message");
950                         g_string_append(type, "; charset=\"latin1\"");
951                 }
952         }
953         return g_string_free(type, FALSE);
954 }
955
956 static GQuark 
957 modest_error_quark (void)
958 {
959         static GQuark err_q = 0;
960         
961         if (err_q == 0)
962                 err_q = g_quark_from_static_string ("modest-error-quark");
963         
964         return err_q;
965 }
966
967
968 static void 
969 set_error (ModestMailOperation *mail_op, 
970            ModestMailOperationErrorCode error_code,
971            const gchar *fmt, ...)
972 {
973         ModestMailOperationPrivate *priv;
974         GError* error;
975         va_list args;
976         gchar* orig;
977
978         priv = MODEST_MAIL_OPERATION_GET_PRIVATE(mail_op);
979
980         va_start (args, fmt);
981
982         orig = g_strdup_vprintf(fmt, args);
983         error = g_error_new (MODEST_ERROR, error_code, orig);
984
985         va_end (args);
986
987         if (priv->error)
988                 g_object_unref (priv->error);
989
990         priv->error = error;
991 }
992
993 static void
994 add_attachments (TnyMsg *msg, GList *attachments_list)
995 {
996         GList *pos;
997         TnyMimePart *attachment_part, *old_attachment;
998         const gchar *attachment_content_type;
999         const gchar *attachment_filename;
1000         TnyStream *attachment_stream;
1001
1002         for (pos = (GList *)attachments_list; pos; pos = pos->next) {
1003
1004                 old_attachment = pos->data;
1005                 attachment_filename = tny_mime_part_get_filename (old_attachment);
1006                 attachment_stream = tny_mime_part_get_stream (old_attachment);
1007                 attachment_part = TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1008                 
1009                 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1010                                  
1011                 tny_mime_part_construct_from_stream (attachment_part,
1012                                                      attachment_stream,
1013                                                      attachment_content_type);
1014                 tny_stream_reset (attachment_stream);
1015                 
1016                 tny_mime_part_set_filename (attachment_part, attachment_filename);
1017                 
1018                 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1019 /*              g_object_unref (attachment_part); */
1020         }
1021 }
1022
1023
1024 static TnyMimePart *
1025 add_body_part (TnyMsg *msg, 
1026                const gchar *body,
1027                const gchar *content_type,
1028                gboolean has_attachments)
1029 {
1030         TnyMimePart *text_body_part = NULL;
1031         TnyStream *text_body_stream;
1032
1033         /* Create the stream */
1034         text_body_stream = TNY_STREAM (tny_camel_stream_new
1035                                        (camel_stream_mem_new_with_buffer
1036                                         (body, strlen(body))));
1037
1038         /* Create body part if needed */
1039         if (has_attachments)
1040                 text_body_part = 
1041                         TNY_MIME_PART (tny_camel_mime_part_new (camel_mime_part_new()));
1042         else
1043                 text_body_part = TNY_MIME_PART(msg);
1044
1045         /* Construct MIME part */
1046         tny_stream_reset (text_body_stream);
1047         tny_mime_part_construct_from_stream (text_body_part,
1048                                              text_body_stream,
1049                                              content_type);
1050         tny_stream_reset (text_body_stream);
1051
1052         /* Add part if needed */
1053         if (has_attachments) {
1054                 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1055                 g_object_unref (G_OBJECT(text_body_part));
1056         }
1057
1058         /* Clean */
1059         g_object_unref (text_body_stream);
1060
1061         return text_body_part;
1062 }