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