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