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