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