1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include "modest-mail-operation.h"
31 /* include other impl specific header files */
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-stream.h>
39 #include <tny-simple-list.h>
40 #include <camel/camel-stream-mem.h>
41 #include <glib/gi18n.h>
43 #include "modest-text-utils.h"
44 #include "modest-tny-msg-actions.h"
45 #include "modest-tny-platform-factory.h"
46 #include "modest-marshal.h"
47 #include "modest-formatter.h"
48 #include "modest-error.h"
50 /* 'private'/'protected' functions */
51 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
52 static void modest_mail_operation_init (ModestMailOperation *obj);
53 static void modest_mail_operation_finalize (GObject *obj);
55 static void status_update_cb (TnyFolder *folder,
60 static void folder_refresh_cb (TnyFolder *folder,
64 static void update_folders_cb (TnyFolderStore *self,
68 static void add_attachments (TnyMsg *msg,
69 GList *attachments_list);
72 static TnyMimePart * add_body_part (TnyMsg *msg,
74 const gchar *content_type,
75 gboolean has_attachments);
78 static void modest_mail_operation_xfer_folder (ModestMailOperation *self,
80 TnyFolderStore *parent,
81 gboolean delete_original);
83 static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self,
86 gboolean delete_original);
88 static TnyFolder * modest_mail_operation_find_trash_folder (ModestMailOperation *self,
89 TnyStoreAccount *store_account);
92 enum _ModestMailOperationSignals
94 PROGRESS_CHANGED_SIGNAL,
99 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
100 struct _ModestMailOperationPrivate {
103 ModestMailOperationStatus status;
106 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
107 MODEST_TYPE_MAIL_OPERATION, \
108 ModestMailOperationPrivate))
110 typedef struct _RefreshFolderAsyncHelper
112 ModestMailOperation *mail_op;
117 } RefreshFolderAsyncHelper;
119 /* some utility functions */
120 static char * get_content_type(const gchar *s);
121 static gboolean is_ascii(const gchar *s);
124 static GObjectClass *parent_class = NULL;
126 static guint signals[NUM_SIGNALS] = {0};
129 modest_mail_operation_get_type (void)
131 static GType my_type = 0;
133 static const GTypeInfo my_info = {
134 sizeof(ModestMailOperationClass),
135 NULL, /* base init */
136 NULL, /* base finalize */
137 (GClassInitFunc) modest_mail_operation_class_init,
138 NULL, /* class finalize */
139 NULL, /* class data */
140 sizeof(ModestMailOperation),
142 (GInstanceInitFunc) modest_mail_operation_init,
145 my_type = g_type_register_static (G_TYPE_OBJECT,
146 "ModestMailOperation",
153 modest_mail_operation_class_init (ModestMailOperationClass *klass)
155 GObjectClass *gobject_class;
156 gobject_class = (GObjectClass*) klass;
158 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->finalize = modest_mail_operation_finalize;
161 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
164 * ModestMailOperation::progress-changed
165 * @self: the #MailOperation that emits the signal
166 * @user_data: user data set when the signal handler was connected
168 * Emitted when the progress of a mail operation changes
170 signals[PROGRESS_CHANGED_SIGNAL] =
171 g_signal_new ("progress_changed",
172 G_TYPE_FROM_CLASS (gobject_class),
174 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
176 g_cclosure_marshal_VOID__VOID,
181 modest_mail_operation_init (ModestMailOperation *obj)
183 ModestMailOperationPrivate *priv;
185 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
187 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
194 modest_mail_operation_finalize (GObject *obj)
196 ModestMailOperationPrivate *priv;
198 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
201 g_error_free (priv->error);
205 G_OBJECT_CLASS(parent_class)->finalize (obj);
209 modest_mail_operation_new (void)
211 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
216 modest_mail_operation_send_mail (ModestMailOperation *self,
217 TnyTransportAccount *transport_account,
220 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
221 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
223 tny_transport_account_send (transport_account, msg, NULL); /* FIXME */
227 modest_mail_operation_send_new_mail (ModestMailOperation *self,
228 TnyTransportAccount *transport_account,
233 const gchar *subject,
235 const GList *attachments_list)
237 TnyPlatformFactory *fact;
241 ModestMailOperationPrivate *priv = NULL;
243 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
244 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
246 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
248 /* Check parametters */
250 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
251 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
252 _("Error trying to send a mail. You need to set almost one a recipient"));
257 fact = modest_tny_platform_factory_get_instance ();
258 new_msg = tny_platform_factory_new_msg (fact);
259 header = tny_platform_factory_new_header (fact);
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_replyto (TNY_HEADER (header), from);
265 tny_header_set_to (TNY_HEADER (header), to);
266 tny_header_set_cc (TNY_HEADER (header), cc);
267 tny_header_set_bcc (TNY_HEADER (header), bcc);
268 tny_header_set_subject (TNY_HEADER (header), subject);
270 content_type = get_content_type(body);
272 /* Add the body of the new mail */
273 add_body_part (new_msg, body, (const gchar *) content_type,
274 (attachments_list == NULL) ? FALSE : TRUE);
276 /* Add attachments */
277 add_attachments (new_msg, (GList*) attachments_list);
280 tny_transport_account_send (transport_account, new_msg, NULL); /* FIXME */
283 g_object_unref (header);
284 g_object_unref (new_msg);
285 g_free(content_type);
289 add_if_attachment (gpointer data, gpointer user_data)
292 GList *attachments_list;
294 part = TNY_MIME_PART (data);
295 attachments_list = (GList *) user_data;
297 if (tny_mime_part_is_attachment (part))
298 attachments_list = g_list_prepend (attachments_list, part);
303 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
305 TnyPlatformFactory *fact;
307 TnyHeader *new_header, *header;
310 ModestFormatter *formatter;
312 /* Get body from original msg. Always look for the text/plain
313 part of the message to create the reply/forwarded mail */
314 header = tny_msg_get_header (msg);
315 body = modest_tny_msg_actions_find_body_part (msg, FALSE);
317 /* TODO: select the formatter from account prefs */
318 formatter = modest_formatter_new ("text/plain");
320 /* Format message body */
323 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
325 new_msg = modest_formatter_cite (formatter, body, header);
327 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
328 new_msg = modest_formatter_quote (formatter, body, header);
333 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
335 new_msg = modest_formatter_inline (formatter, body, header);
337 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
338 new_msg = modest_formatter_attach (formatter, body, header);
342 g_object_unref (G_OBJECT (formatter));
344 /* Fill the header */
345 fact = modest_tny_platform_factory_get_instance ();
346 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
347 tny_msg_set_header (new_msg, new_header);
348 tny_header_set_from (new_header, from);
349 tny_header_set_replyto (new_header, from);
351 /* Change the subject */
353 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
354 (is_reply) ? _("Re:") : _("Fwd:"));
355 tny_header_set_subject (new_header, (const gchar *) new_subject);
356 g_free (new_subject);
359 g_object_unref (G_OBJECT (new_header));
360 g_object_unref (G_OBJECT (header));
366 modest_mail_operation_create_forward_mail (TnyMsg *msg,
368 ModestMailOperationForwardType forward_type)
371 TnyList *parts = NULL;
372 GList *attachments_list = NULL;
374 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
376 /* Add attachments */
377 parts = TNY_LIST (tny_simple_list_new());
378 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
379 tny_list_foreach (parts, add_if_attachment, attachments_list);
380 add_attachments (new_msg, attachments_list);
383 if (attachments_list) g_list_free (attachments_list);
384 g_object_unref (G_OBJECT (parts));
390 modest_mail_operation_create_reply_mail (TnyMsg *msg,
392 ModestMailOperationReplyType reply_type,
393 ModestMailOperationReplyMode reply_mode)
395 TnyMsg *new_msg = NULL;
396 TnyHeader *new_header, *header;
397 const gchar* reply_to;
398 gchar *new_cc = NULL;
399 const gchar *cc = NULL, *bcc = NULL;
402 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
404 /* Fill the header */
405 header = tny_msg_get_header (msg);
406 new_header = tny_msg_get_header (new_msg);
407 reply_to = tny_header_get_replyto (header);
409 /* TODO: tinymail returns Invalid and it should return NULL */
410 if (strcmp (reply_to, "Invalid"))
411 tny_header_set_to (new_header, reply_to);
413 tny_header_set_to (new_header, tny_header_get_from (header));
415 switch (reply_mode) {
416 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
417 /* Do not fill neither cc nor bcc */
419 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
422 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
423 /* Concatenate to, cc and bcc */
424 cc = tny_header_get_cc (header);
425 bcc = tny_header_get_bcc (header);
427 tmp = g_string_new (tny_header_get_to (header));
428 if (cc) g_string_append_printf (tmp, ",%s",cc);
429 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
431 /* Remove my own address from the cc list. TODO:
432 remove also the To: of the new message, needed due
433 to the new reply_to feature */
435 modest_text_utils_remove_address ((const gchar *) tmp->str,
437 /* FIXME: remove also the mails from the new To: */
438 tny_header_set_cc (new_header, new_cc);
441 g_string_free (tmp, TRUE);
447 g_object_unref (G_OBJECT (new_header));
448 g_object_unref (G_OBJECT (header));
454 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
456 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
460 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
462 ModestMailOperation *self = NULL;
463 ModestMailOperationPrivate *priv = NULL;
464 RefreshFolderAsyncHelper *helper;
466 helper = (RefreshFolderAsyncHelper *) user_data;
467 self = MODEST_MAIL_OPERATION (helper->mail_op);
468 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
470 if ((canceled && *err) || *err) {
471 priv->error = g_error_copy (*err);
473 } else if (canceled) {
475 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
476 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
477 _("Error trying to refresh folder %s. Operation canceled"),
478 tny_folder_get_name (folder));
483 if (priv->done == priv->total)
484 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
485 else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
486 if (helper->failed == priv->total)
487 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
488 else if (helper->failed == priv->total)
489 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
491 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
493 tny_iterator_next (helper->iter);
494 if (tny_iterator_is_done (helper->iter)) {
496 list = tny_iterator_get_list (helper->iter);
497 g_object_unref (G_OBJECT (helper->iter));
498 g_object_unref (G_OBJECT (list));
499 g_slice_free (RefreshFolderAsyncHelper, helper);
501 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
506 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
511 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
513 ModestMailOperation *self;
514 ModestMailOperationPrivate *priv;
515 RefreshFolderAsyncHelper *helper;
517 self = MODEST_MAIL_OPERATION (user_data);
518 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
521 priv->error = g_error_copy (*err);
522 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
526 priv->total = tny_list_get_length (list);
528 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
530 helper = g_slice_new0 (RefreshFolderAsyncHelper);
531 helper->mail_op = self;
532 helper->iter = tny_list_create_iterator (list);
534 helper->canceled = 0;
536 /* Async refresh folders */
537 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
544 modest_mail_operation_update_account (ModestMailOperation *self,
545 TnyStoreAccount *store_account)
547 ModestMailOperationPrivate *priv;
549 TnyFolderStoreQuery *query;
551 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
552 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
554 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
556 /* Get subscribed folders & refresh them */
557 folders = TNY_LIST (tny_simple_list_new ());
558 query = tny_folder_store_query_new ();
559 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
560 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
561 folders, update_folders_cb, query, self);
562 g_object_unref (query);
567 ModestMailOperationStatus
568 modest_mail_operation_get_status (ModestMailOperation *self)
570 ModestMailOperationPrivate *priv;
572 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
573 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
574 MODEST_MAIL_OPERATION_STATUS_INVALID);
576 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
581 modest_mail_operation_get_error (ModestMailOperation *self)
583 ModestMailOperationPrivate *priv;
585 g_return_val_if_fail (self, NULL);
586 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
588 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
593 modest_mail_operation_cancel (ModestMailOperation *self)
600 modest_mail_operation_get_task_done (ModestMailOperation *self)
602 ModestMailOperationPrivate *priv;
604 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
606 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
611 modest_mail_operation_get_task_total (ModestMailOperation *self)
613 ModestMailOperationPrivate *priv;
615 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
617 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
622 modest_mail_operation_is_finished (ModestMailOperation *self)
624 ModestMailOperationPrivate *priv;
625 gboolean retval = FALSE;
627 if (!MODEST_IS_MAIL_OPERATION (self)) {
628 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
632 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
634 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
635 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
636 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
637 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
646 /* ******************************************************************* */
647 /* ************************** STORE ACTIONS ************************* */
648 /* ******************************************************************* */
652 modest_mail_operation_create_folder (ModestMailOperation *self,
653 TnyFolderStore *parent,
656 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
657 g_return_val_if_fail (name, NULL);
659 TnyFolder *new_folder = NULL;
660 TnyStoreAccount *store_account;
662 /* Create the folder */
663 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
667 /* Subscribe to folder */
668 if (!tny_folder_is_subscribed (new_folder)) {
669 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
670 tny_store_account_subscribe (store_account, new_folder);
671 g_object_unref (G_OBJECT (store_account));
678 modest_mail_operation_remove_folder (ModestMailOperation *self,
680 gboolean remove_to_trash)
682 TnyFolderStore *folder_store;
684 g_return_if_fail (TNY_IS_FOLDER (folder));
686 /* Get folder store */
687 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
689 /* Delete folder or move to trash */
690 if (remove_to_trash) {
691 TnyFolder *trash_folder;
693 trash_folder = modest_mail_operation_find_trash_folder (self,
694 TNY_STORE_ACCOUNT (folder_store));
696 /* TODO: error_handling */
697 modest_mail_operation_move_folder (self,
699 TNY_FOLDER_STORE (trash_folder));
701 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
702 g_object_unref (G_OBJECT (folder));
706 g_object_unref (G_OBJECT (folder_store));
710 modest_mail_operation_rename_folder (ModestMailOperation *self,
714 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
715 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
716 g_return_if_fail (name);
718 /* FIXME: better error handling */
719 if (strrchr (name, '/') != NULL)
722 /* Rename. Camel handles folder subscription/unsubscription */
723 tny_folder_set_name (folder, name, NULL); /* FIXME */
727 modest_mail_operation_move_folder (ModestMailOperation *self,
729 TnyFolderStore *parent)
731 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
732 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
733 g_return_if_fail (TNY_IS_FOLDER (folder));
735 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
739 modest_mail_operation_copy_folder (ModestMailOperation *self,
741 TnyFolderStore *parent)
743 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
744 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
745 g_return_if_fail (TNY_IS_FOLDER (folder));
747 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
751 modest_mail_operation_xfer_folder (ModestMailOperation *self,
753 TnyFolderStore *parent,
754 gboolean delete_original)
756 const gchar *folder_name;
757 TnyFolder *dest_folder, *child;
759 TnyList *folders, *headers;
761 g_return_if_fail (TNY_IS_FOLDER (folder));
762 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
764 /* Create the destination folder */
765 folder_name = tny_folder_get_name (folder);
766 dest_folder = modest_mail_operation_create_folder (self,
767 parent, folder_name);
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 */
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);
779 while (!tny_iterator_is_done (iter)) {
781 child = TNY_FOLDER (tny_iterator_get_current (iter));
782 modest_mail_operation_xfer_folder (self, child,
783 TNY_FOLDER_STORE (dest_folder),
785 tny_iterator_next (iter);
788 /* Delete source folder (if needed) */
790 modest_mail_operation_remove_folder (self, folder, FALSE);
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));
800 /* FIXME: this method should be rewritten when the policy for the
801 Trash folder becomes clearer */
803 modest_mail_operation_find_trash_folder (ModestMailOperation *self,
804 TnyStoreAccount *store_account)
809 /*TnyFolderStoreQuery *query;*/
810 TnyFolder *trash_folder = NULL;
812 /* Look for Trash folder */
813 folders = TNY_LIST (tny_simple_list_new ());
814 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
815 folders, NULL, NULL); /* FIXME */
816 iter = tny_list_create_iterator (folders);
819 while (!tny_iterator_is_done (iter) && !found) {
821 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
822 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
825 tny_iterator_next (iter);
829 g_object_unref (G_OBJECT (folders));
830 g_object_unref (G_OBJECT (iter));
832 /* TODO: better error handling management */
839 /* ******************************************************************* */
840 /* ************************** MSG ACTIONS ************************* */
841 /* ******************************************************************* */
844 modest_mail_operation_copy_msg (ModestMailOperation *self,
848 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
849 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
851 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
855 modest_mail_operation_move_msg (ModestMailOperation *self,
859 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
860 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
862 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
866 modest_mail_operation_remove_msg (ModestMailOperation *self,
868 gboolean remove_to_trash)
872 g_return_if_fail (TNY_IS_HEADER (header));
874 folder = tny_header_get_folder (header);
876 /* Delete or move to trash */
877 if (remove_to_trash) {
878 TnyFolder *trash_folder;
879 TnyStoreAccount *store_account;
881 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
882 trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
885 modest_mail_operation_move_msg (self, header, trash_folder);
886 /* g_object_unref (trash_folder); */
888 ModestMailOperationPrivate *priv;
890 /* Set status failed and set an error */
891 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
892 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
893 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
894 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
895 _("Error trying to delete a message. Trash folder not found"));
898 g_object_unref (G_OBJECT (store_account));
900 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
901 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
905 g_object_unref (folder);
909 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
911 ModestMailOperationPrivate *priv;
913 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
915 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
917 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
921 modest_mail_operation_xfer_msg (ModestMailOperation *self,
924 gboolean delete_original)
926 ModestMailOperationPrivate *priv;
927 TnyFolder *src_folder;
930 src_folder = tny_header_get_folder (header);
931 headers = tny_simple_list_new ();
933 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
936 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
938 tny_list_prepend (headers, G_OBJECT (header));
939 tny_folder_transfer_msgs_async (src_folder, headers, folder,
940 delete_original, transfer_msgs_cb, self);
943 g_object_unref (headers);
944 g_object_unref (folder);
950 /* ******************************************************************* */
951 /* ************************* UTILIY FUNCTIONS ************************ */
952 /* ******************************************************************* */
954 is_ascii(const gchar *s)
957 if (s[0] & 128 || s[0] < 32)
965 get_content_type(const gchar *s)
969 type = g_string_new("text/plain");
971 if (g_utf8_validate(s, -1, NULL)) {
972 g_string_append(type, "; charset=\"utf-8\"");
974 /* it should be impossible to reach this, but better safe than sorry */
975 g_warning("invalid utf8 in message");
976 g_string_append(type, "; charset=\"latin1\"");
979 return g_string_free(type, FALSE);
983 add_attachments (TnyMsg *msg, GList *attachments_list)
986 TnyMimePart *attachment_part, *old_attachment;
987 const gchar *attachment_content_type;
988 const gchar *attachment_filename;
989 TnyStream *attachment_stream;
990 TnyPlatformFactory *fact;
992 fact = modest_tny_platform_factory_get_instance ();
993 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
995 old_attachment = pos->data;
996 attachment_filename = tny_mime_part_get_filename (old_attachment);
997 attachment_stream = tny_mime_part_get_stream (old_attachment);
998 attachment_part = tny_platform_factory_new_mime_part (fact);
1000 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1002 tny_mime_part_construct_from_stream (attachment_part,
1004 attachment_content_type);
1005 tny_stream_reset (attachment_stream);
1007 tny_mime_part_set_filename (attachment_part, attachment_filename);
1009 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1010 /* g_object_unref (attachment_part); */
1015 static TnyMimePart *
1016 add_body_part (TnyMsg *msg,
1018 const gchar *content_type,
1019 gboolean has_attachments)
1021 TnyMimePart *text_body_part = NULL;
1022 TnyStream *text_body_stream;
1023 TnyPlatformFactory *fact;
1025 fact = modest_tny_platform_factory_get_instance ();
1027 /* Create the stream */
1028 text_body_stream = TNY_STREAM (tny_camel_stream_new
1029 (camel_stream_mem_new_with_buffer
1030 (body, strlen(body))));
1032 /* Create body part if needed */
1033 if (has_attachments)
1034 text_body_part = tny_platform_factory_new_mime_part (fact);
1036 text_body_part = TNY_MIME_PART(msg);
1038 /* Construct MIME part */
1039 tny_stream_reset (text_body_stream);
1040 tny_mime_part_construct_from_stream (text_body_part,
1043 tny_stream_reset (text_body_stream);
1045 /* Add part if needed */
1046 if (has_attachments) {
1047 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1048 g_object_unref (G_OBJECT(text_body_part));
1052 g_object_unref (text_body_stream);
1054 return text_body_part;