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 */
313 header = tny_msg_get_header (msg);
314 body = modest_tny_msg_actions_find_body_part (msg, TRUE);
316 /* TODO: select the formatter from account prefs */
317 formatter = modest_formatter_new ("text/plain");
319 /* Format message body */
322 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
324 new_msg = modest_formatter_cite (formatter, body, header);
326 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
327 new_msg = modest_formatter_quote (formatter, body, header);
332 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
334 new_msg = modest_formatter_inline (formatter, body, header);
336 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
337 new_msg = modest_formatter_attach (formatter, body, header);
341 g_object_unref (G_OBJECT (formatter));
343 /* Fill the header */
344 fact = modest_tny_platform_factory_get_instance ();
345 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
346 tny_msg_set_header (new_msg, new_header);
347 tny_header_set_from (new_header, from);
348 tny_header_set_replyto (new_header, from);
350 /* Change the subject */
352 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
353 (is_reply) ? _("Re:") : _("Fwd:"));
354 tny_header_set_subject (new_header, (const gchar *) new_subject);
355 g_free (new_subject);
358 g_object_unref (G_OBJECT (new_header));
359 g_object_unref (G_OBJECT (header));
365 modest_mail_operation_create_forward_mail (TnyMsg *msg,
367 ModestMailOperationForwardType forward_type)
370 TnyList *parts = NULL;
371 GList *attachments_list = NULL;
373 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
375 /* Add attachments */
376 parts = TNY_LIST (tny_simple_list_new());
377 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
378 tny_list_foreach (parts, add_if_attachment, attachments_list);
379 add_attachments (new_msg, attachments_list);
382 if (attachments_list) g_list_free (attachments_list);
383 g_object_unref (G_OBJECT (parts));
389 modest_mail_operation_create_reply_mail (TnyMsg *msg,
391 ModestMailOperationReplyType reply_type,
392 ModestMailOperationReplyMode reply_mode)
395 TnyHeader *new_header, *header;
396 const gchar* reply_to;
398 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
400 /* Fill the header */
401 header = tny_msg_get_header (msg);
402 new_header = tny_msg_get_header (new_msg);
403 reply_to = tny_header_get_replyto (header);
405 tny_header_set_to (new_header, reply_to);
407 tny_header_set_to (new_header, tny_header_get_from (header));
409 switch (reply_mode) {
410 gchar *new_cc = NULL;
411 const gchar *cc = NULL, *bcc = NULL;
414 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
415 /* Do not fill neither cc nor bcc */
417 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
420 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
421 /* Concatenate to, cc and bcc */
422 cc = tny_header_get_cc (header);
423 bcc = tny_header_get_bcc (header);
425 tmp = g_string_new (tny_header_get_to (header));
426 if (cc) g_string_append_printf (tmp, ",%s",cc);
427 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
429 /* Remove my own address from the cc list. TODO:
430 remove also the To: of the new message, needed due
431 to the new reply_to feature */
433 modest_text_utils_remove_address ((const gchar *) tmp->str,
435 /* FIXME: remove also the mails from the new To: */
436 tny_header_set_cc (new_header, new_cc);
439 g_string_free (tmp, TRUE);
445 g_object_unref (G_OBJECT (new_header));
446 g_object_unref (G_OBJECT (header));
452 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
454 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
458 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
460 ModestMailOperation *self = NULL;
461 ModestMailOperationPrivate *priv = NULL;
462 RefreshFolderAsyncHelper *helper;
464 helper = (RefreshFolderAsyncHelper *) user_data;
465 self = MODEST_MAIL_OPERATION (helper->mail_op);
466 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
468 if ((canceled && *err) || *err) {
469 priv->error = g_error_copy (*err);
471 } else if (canceled) {
473 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
474 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
475 _("Error trying to refresh folder %s. Operation canceled"),
476 tny_folder_get_name (folder));
481 if (priv->done == priv->total)
482 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
483 else if ((priv->done + helper->canceled + helper->failed) == priv->total)
484 if (helper->failed == priv->total)
485 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
486 else if (helper->failed == priv->total)
487 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
489 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
491 tny_iterator_next (helper->iter);
492 if (tny_iterator_is_done (helper->iter)) {
494 list = tny_iterator_get_list (helper->iter);
495 g_object_unref (G_OBJECT (helper->iter));
496 g_object_unref (G_OBJECT (list));
497 g_slice_free (RefreshFolderAsyncHelper, helper);
499 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
504 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
509 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
511 ModestMailOperation *self;
512 ModestMailOperationPrivate *priv;
514 RefreshFolderAsyncHelper *helper;
516 self = MODEST_MAIL_OPERATION (user_data);
517 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
520 priv->error = g_error_copy (*err);
521 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
525 priv->total = tny_list_get_length (list);
527 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
529 helper = g_slice_new0 (RefreshFolderAsyncHelper);
530 helper->mail_op = self;
531 helper->iter = tny_list_create_iterator (list);
533 helper->canceled = 0;
535 /* Async refresh folders */
536 tny_folder_refresh_async (TNY_FOLDER (tny_iterator_get_current (helper->iter)),
543 modest_mail_operation_update_account (ModestMailOperation *self,
544 TnyStoreAccount *store_account)
546 ModestMailOperationPrivate *priv;
548 TnyFolderStoreQuery *query;
550 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
551 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
553 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
555 /* Get subscribed folders & refresh them */
556 folders = TNY_LIST (tny_simple_list_new ());
557 query = tny_folder_store_query_new ();
558 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
559 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
560 folders, update_folders_cb, query, self);
561 g_object_unref (query);
566 ModestMailOperationStatus
567 modest_mail_operation_get_status (ModestMailOperation *self)
569 ModestMailOperationPrivate *priv;
571 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
572 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
573 MODEST_MAIL_OPERATION_STATUS_INVALID);
575 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
580 modest_mail_operation_get_error (ModestMailOperation *self)
582 ModestMailOperationPrivate *priv;
584 g_return_val_if_fail (self, NULL);
585 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
587 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
592 modest_mail_operation_cancel (ModestMailOperation *self)
599 modest_mail_operation_get_task_done (ModestMailOperation *self)
601 ModestMailOperationPrivate *priv;
603 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
605 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
610 modest_mail_operation_get_task_total (ModestMailOperation *self)
612 ModestMailOperationPrivate *priv;
614 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
616 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
621 modest_mail_operation_is_finished (ModestMailOperation *self)
623 ModestMailOperationPrivate *priv;
624 gboolean retval = FALSE;
626 if (!MODEST_IS_MAIL_OPERATION (self)) {
627 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
631 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
633 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
634 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
635 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
636 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
645 /* ******************************************************************* */
646 /* ************************** STORE ACTIONS ************************* */
647 /* ******************************************************************* */
651 modest_mail_operation_create_folder (ModestMailOperation *self,
652 TnyFolderStore *parent,
655 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
656 g_return_val_if_fail (name, NULL);
658 TnyFolder *new_folder = NULL;
659 TnyStoreAccount *store_account;
661 /* Create the folder */
662 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
666 /* Subscribe to folder */
667 if (!tny_folder_is_subscribed (new_folder)) {
668 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
669 tny_store_account_subscribe (store_account, new_folder);
670 g_object_unref (G_OBJECT (store_account));
677 modest_mail_operation_remove_folder (ModestMailOperation *self,
679 gboolean remove_to_trash)
681 TnyFolderStore *folder_store;
683 g_return_if_fail (TNY_IS_FOLDER (folder));
685 /* Get folder store */
686 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
688 /* Delete folder or move to trash */
689 if (remove_to_trash) {
690 TnyFolder *trash_folder;
692 trash_folder = modest_mail_operation_find_trash_folder (self,
693 TNY_STORE_ACCOUNT (folder_store));
695 /* TODO: error_handling */
696 modest_mail_operation_move_folder (self,
698 TNY_FOLDER_STORE (trash_folder));
700 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
701 g_object_unref (G_OBJECT (folder));
705 g_object_unref (G_OBJECT (folder_store));
709 modest_mail_operation_rename_folder (ModestMailOperation *self,
713 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
714 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
715 g_return_if_fail (name);
717 /* FIXME: better error handling */
718 if (strrchr (name, '/') != NULL)
721 /* Rename. Camel handles folder subscription/unsubscription */
722 tny_folder_set_name (folder, name, NULL); /* FIXME */
726 modest_mail_operation_move_folder (ModestMailOperation *self,
728 TnyFolderStore *parent)
730 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
731 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
732 g_return_if_fail (TNY_IS_FOLDER (folder));
734 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
738 modest_mail_operation_copy_folder (ModestMailOperation *self,
740 TnyFolderStore *parent)
742 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
743 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
744 g_return_if_fail (TNY_IS_FOLDER (folder));
746 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
750 modest_mail_operation_xfer_folder (ModestMailOperation *self,
752 TnyFolderStore *parent,
753 gboolean delete_original)
755 const gchar *folder_name;
756 TnyFolder *dest_folder, *child;
758 TnyList *folders, *headers;
760 g_return_if_fail (TNY_IS_FOLDER (folder));
761 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
763 /* Create the destination folder */
764 folder_name = tny_folder_get_name (folder);
765 dest_folder = modest_mail_operation_create_folder (self,
766 parent, folder_name);
768 /* Transfer messages */
769 headers = TNY_LIST (tny_simple_list_new ());
770 tny_folder_get_headers (folder, headers, FALSE, NULL); /* FIXME */
771 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, NULL); /* FIXME */
773 /* Recurse children */
774 folders = TNY_LIST (tny_simple_list_new ());
775 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, NULL ); /* FIXME */
776 iter = tny_list_create_iterator (folders);
778 while (!tny_iterator_is_done (iter)) {
780 child = TNY_FOLDER (tny_iterator_get_current (iter));
781 modest_mail_operation_xfer_folder (self, child,
782 TNY_FOLDER_STORE (dest_folder),
784 tny_iterator_next (iter);
787 /* Delete source folder (if needed) */
789 modest_mail_operation_remove_folder (self, folder, FALSE);
792 g_object_unref (G_OBJECT (dest_folder));
793 g_object_unref (G_OBJECT (headers));
794 g_object_unref (G_OBJECT (folders));
795 g_object_unref (G_OBJECT (iter));
799 /* FIXME: this method should be rewritten when the policy for the
800 Trash folder becomes clearer */
802 modest_mail_operation_find_trash_folder (ModestMailOperation *self,
803 TnyStoreAccount *store_account)
808 /*TnyFolderStoreQuery *query;*/
809 TnyFolder *trash_folder;
811 /* Look for Trash folder */
812 folders = TNY_LIST (tny_simple_list_new ());
813 tny_folder_store_get_folders (TNY_FOLDER_STORE (store_account),
814 folders, NULL, NULL); /* FIXME */
815 iter = tny_list_create_iterator (folders);
818 while (!tny_iterator_is_done (iter) && !found) {
820 trash_folder = TNY_FOLDER (tny_iterator_get_current (iter));
821 if (tny_folder_get_folder_type (trash_folder) == TNY_FOLDER_TYPE_TRASH)
824 tny_iterator_next (iter);
828 g_object_unref (G_OBJECT (folders));
829 g_object_unref (G_OBJECT (iter));
831 /* TODO: better error handling management */
838 /* ******************************************************************* */
839 /* ************************** MSG ACTIONS ************************* */
840 /* ******************************************************************* */
843 modest_mail_operation_copy_msg (ModestMailOperation *self,
847 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
848 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
850 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
854 modest_mail_operation_move_msg (ModestMailOperation *self,
858 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
859 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
861 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
865 modest_mail_operation_remove_msg (ModestMailOperation *self,
867 gboolean remove_to_trash)
871 g_return_if_fail (TNY_IS_HEADER (header));
873 folder = tny_header_get_folder (header);
875 /* Delete or move to trash */
876 if (remove_to_trash) {
877 TnyFolder *trash_folder;
878 TnyStoreAccount *store_account;
880 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
881 trash_folder = modest_mail_operation_find_trash_folder (self, store_account);
884 modest_mail_operation_move_msg (self, header, trash_folder);
885 /* g_object_unref (trash_folder); */
887 ModestMailOperationPrivate *priv;
889 /* Set status failed and set an error */
890 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
891 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
892 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
893 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
894 _("Error trying to delete a message. Trash folder not found"));
897 g_object_unref (G_OBJECT (store_account));
899 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
900 tny_folder_expunge (folder, NULL); /* FIXME */
904 g_object_unref (folder);
908 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
910 ModestMailOperationPrivate *priv;
912 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
914 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
916 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
920 modest_mail_operation_xfer_msg (ModestMailOperation *self,
923 gboolean delete_original)
925 ModestMailOperationPrivate *priv;
926 TnyFolder *src_folder;
929 src_folder = tny_header_get_folder (header);
930 headers = tny_simple_list_new ();
932 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
935 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
937 tny_list_prepend (headers, G_OBJECT (header));
938 tny_folder_transfer_msgs_async (src_folder, headers, folder,
939 delete_original, transfer_msgs_cb, self);
942 g_object_unref (headers);
943 g_object_unref (folder);
949 /* ******************************************************************* */
950 /* ************************* UTILIY FUNCTIONS ************************ */
951 /* ******************************************************************* */
953 is_ascii(const gchar *s)
956 if (s[0] & 128 || s[0] < 32)
964 get_content_type(const gchar *s)
968 type = g_string_new("text/plain");
970 if (g_utf8_validate(s, -1, NULL)) {
971 g_string_append(type, "; charset=\"utf-8\"");
973 /* it should be impossible to reach this, but better safe than sorry */
974 g_warning("invalid utf8 in message");
975 g_string_append(type, "; charset=\"latin1\"");
978 return g_string_free(type, FALSE);
982 add_attachments (TnyMsg *msg, GList *attachments_list)
985 TnyMimePart *attachment_part, *old_attachment;
986 const gchar *attachment_content_type;
987 const gchar *attachment_filename;
988 TnyStream *attachment_stream;
989 TnyPlatformFactory *fact;
991 fact = modest_tny_platform_factory_get_instance ();
992 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
994 old_attachment = pos->data;
995 attachment_filename = tny_mime_part_get_filename (old_attachment);
996 attachment_stream = tny_mime_part_get_stream (old_attachment);
997 attachment_part = tny_platform_factory_new_mime_part (fact);
999 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
1001 tny_mime_part_construct_from_stream (attachment_part,
1003 attachment_content_type);
1004 tny_stream_reset (attachment_stream);
1006 tny_mime_part_set_filename (attachment_part, attachment_filename);
1008 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
1009 /* g_object_unref (attachment_part); */
1014 static TnyMimePart *
1015 add_body_part (TnyMsg *msg,
1017 const gchar *content_type,
1018 gboolean has_attachments)
1020 TnyMimePart *text_body_part = NULL;
1021 TnyStream *text_body_stream;
1022 TnyPlatformFactory *fact;
1024 fact = modest_tny_platform_factory_get_instance ();
1026 /* Create the stream */
1027 text_body_stream = TNY_STREAM (tny_camel_stream_new
1028 (camel_stream_mem_new_with_buffer
1029 (body, strlen(body))));
1031 /* Create body part if needed */
1032 if (has_attachments)
1033 text_body_part = tny_platform_factory_new_mime_part (fact);
1035 text_body_part = TNY_MIME_PART(msg);
1037 /* Construct MIME part */
1038 tny_stream_reset (text_body_stream);
1039 tny_mime_part_construct_from_stream (text_body_part,
1042 tny_stream_reset (text_body_stream);
1044 /* Add part if needed */
1045 if (has_attachments) {
1046 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1047 g_object_unref (G_OBJECT(text_body_part));
1051 g_object_unref (text_body_stream);
1053 return text_body_part;