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