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>
42 #include <modest-tny-account.h>
43 #include <modest-tny-send-queue.h>
44 #include <modest-runtime.h>
45 #include "modest-text-utils.h"
46 #include "modest-tny-msg.h"
47 #include "modest-tny-platform-factory.h"
48 #include "modest-marshal.h"
49 #include "modest-formatter.h"
50 #include "modest-error.h"
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);
57 static void status_update_cb (TnyFolder *folder,
62 static void folder_refresh_cb (TnyFolder *folder,
66 static void update_folders_cb (TnyFolderStore *self,
70 static void add_attachments (TnyMsg *msg,
71 GList *attachments_list);
74 static TnyMimePart * add_body_part (TnyMsg *msg,
76 const gchar *content_type,
77 gboolean has_attachments);
80 static void modest_mail_operation_xfer_folder (ModestMailOperation *self,
82 TnyFolderStore *parent,
83 gboolean delete_original);
85 static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self,
88 gboolean delete_original);
90 enum _ModestMailOperationSignals
92 PROGRESS_CHANGED_SIGNAL,
97 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
98 struct _ModestMailOperationPrivate {
101 ModestMailOperationStatus status;
104 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
105 MODEST_TYPE_MAIL_OPERATION, \
106 ModestMailOperationPrivate))
108 typedef struct _RefreshFolderAsyncHelper
110 ModestMailOperation *mail_op;
115 } RefreshFolderAsyncHelper;
117 /* some utility functions */
118 static char * get_content_type(const gchar *s);
119 static gboolean is_ascii(const gchar *s);
122 static GObjectClass *parent_class = NULL;
124 static guint signals[NUM_SIGNALS] = {0};
127 modest_mail_operation_get_type (void)
129 static GType my_type = 0;
131 static const GTypeInfo my_info = {
132 sizeof(ModestMailOperationClass),
133 NULL, /* base init */
134 NULL, /* base finalize */
135 (GClassInitFunc) modest_mail_operation_class_init,
136 NULL, /* class finalize */
137 NULL, /* class data */
138 sizeof(ModestMailOperation),
140 (GInstanceInitFunc) modest_mail_operation_init,
143 my_type = g_type_register_static (G_TYPE_OBJECT,
144 "ModestMailOperation",
151 modest_mail_operation_class_init (ModestMailOperationClass *klass)
153 GObjectClass *gobject_class;
154 gobject_class = (GObjectClass*) klass;
156 parent_class = g_type_class_peek_parent (klass);
157 gobject_class->finalize = modest_mail_operation_finalize;
159 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
162 * ModestMailOperation::progress-changed
163 * @self: the #MailOperation that emits the signal
164 * @user_data: user data set when the signal handler was connected
166 * Emitted when the progress of a mail operation changes
168 signals[PROGRESS_CHANGED_SIGNAL] =
169 g_signal_new ("progress_changed",
170 G_TYPE_FROM_CLASS (gobject_class),
172 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
174 g_cclosure_marshal_VOID__VOID,
179 modest_mail_operation_init (ModestMailOperation *obj)
181 ModestMailOperationPrivate *priv;
183 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
185 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
192 modest_mail_operation_finalize (GObject *obj)
194 ModestMailOperationPrivate *priv;
196 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
199 g_error_free (priv->error);
203 G_OBJECT_CLASS(parent_class)->finalize (obj);
207 modest_mail_operation_new (void)
209 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
214 modest_mail_operation_send_mail (ModestMailOperation *self,
215 TnyTransportAccount *transport_account,
218 TnySendQueue *send_queue;
220 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
221 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
223 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
225 tny_send_queue_add (send_queue, msg);
229 modest_mail_operation_send_new_mail (ModestMailOperation *self,
230 TnyTransportAccount *transport_account,
235 const gchar *subject,
237 const GList *attachments_list)
239 TnyPlatformFactory *fact;
243 ModestMailOperationPrivate *priv = NULL;
245 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
246 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
248 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
250 /* Check parametters */
252 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
253 MODEST_MAIL_OPERATION_ERROR_MISSING_PARAMETER,
254 _("Error trying to send a mail. You need to set almost one a recipient"));
259 fact = modest_tny_platform_factory_get_instance ();
260 new_msg = tny_platform_factory_new_msg (fact);
261 header = tny_platform_factory_new_header (fact);
263 /* WARNING: set the header before assign values to it */
264 tny_msg_set_header (new_msg, header);
265 tny_header_set_from (TNY_HEADER (header), from);
266 tny_header_set_replyto (TNY_HEADER (header), from);
267 tny_header_set_to (TNY_HEADER (header), to);
268 tny_header_set_cc (TNY_HEADER (header), cc);
269 tny_header_set_bcc (TNY_HEADER (header), bcc);
270 tny_header_set_subject (TNY_HEADER (header), subject);
272 content_type = get_content_type(body);
274 /* Add the body of the new mail */
275 add_body_part (new_msg, body, (const gchar *) content_type,
276 (attachments_list == NULL) ? FALSE : TRUE);
278 /* Add attachments */
279 add_attachments (new_msg, (GList*) attachments_list);
282 modest_mail_operation_send_mail (self, transport_account, new_msg);
285 g_object_unref (header);
286 g_object_unref (new_msg);
287 g_free(content_type);
291 add_if_attachment (gpointer data, gpointer user_data)
294 GList *attachments_list;
296 part = TNY_MIME_PART (data);
297 attachments_list = (GList *) user_data;
299 if (tny_mime_part_is_attachment (part))
300 attachments_list = g_list_prepend (attachments_list, part);
305 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
307 TnyPlatformFactory *fact;
309 TnyHeader *new_header, *header;
312 ModestFormatter *formatter;
314 /* Get body from original msg. Always look for the text/plain
315 part of the message to create the reply/forwarded mail */
316 header = tny_msg_get_header (msg);
317 body = modest_tny_msg_find_body_part (msg, FALSE);
319 /* TODO: select the formatter from account prefs */
320 formatter = modest_formatter_new ("text/plain");
322 /* Format message body */
325 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
327 new_msg = modest_formatter_cite (formatter, body, header);
329 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
330 new_msg = modest_formatter_quote (formatter, body, header);
335 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
337 new_msg = modest_formatter_inline (formatter, body, header);
339 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
340 new_msg = modest_formatter_attach (formatter, body, header);
344 g_object_unref (G_OBJECT(formatter));
345 g_object_unref (G_OBJECT(body));
347 /* Fill the header */
348 fact = modest_tny_platform_factory_get_instance ();
349 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
350 tny_msg_set_header (new_msg, new_header);
351 tny_header_set_from (new_header, from);
352 tny_header_set_replyto (new_header, from);
354 /* Change the subject */
356 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
357 (is_reply) ? _("Re:") : _("Fwd:"));
358 tny_header_set_subject (new_header, (const gchar *) new_subject);
359 g_free (new_subject);
362 g_object_unref (G_OBJECT (new_header));
363 g_object_unref (G_OBJECT (header));
369 modest_mail_operation_create_forward_mail (TnyMsg *msg,
371 ModestMailOperationForwardType forward_type)
374 TnyList *parts = NULL;
375 GList *attachments_list = NULL;
377 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
379 /* Add attachments */
380 parts = TNY_LIST (tny_simple_list_new());
381 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
382 tny_list_foreach (parts, add_if_attachment, attachments_list);
383 add_attachments (new_msg, attachments_list);
386 if (attachments_list) g_list_free (attachments_list);
387 g_object_unref (G_OBJECT (parts));
393 modest_mail_operation_create_reply_mail (TnyMsg *msg,
395 ModestMailOperationReplyType reply_type,
396 ModestMailOperationReplyMode reply_mode)
398 TnyMsg *new_msg = NULL;
399 TnyHeader *new_header, *header;
400 const gchar* reply_to;
401 gchar *new_cc = NULL;
402 const gchar *cc = NULL, *bcc = NULL;
405 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
407 /* Fill the header */
408 header = tny_msg_get_header (msg);
409 new_header = tny_msg_get_header (new_msg);
410 reply_to = tny_header_get_replyto (header);
413 tny_header_set_to (new_header, reply_to);
415 tny_header_set_to (new_header, tny_header_get_from (header));
417 switch (reply_mode) {
418 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
419 /* Do not fill neither cc nor bcc */
421 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
424 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
425 /* Concatenate to, cc and bcc */
426 cc = tny_header_get_cc (header);
427 bcc = tny_header_get_bcc (header);
429 tmp = g_string_new (tny_header_get_to (header));
430 if (cc) g_string_append_printf (tmp, ",%s",cc);
431 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
433 /* Remove my own address from the cc list. TODO:
434 remove also the To: of the new message, needed due
435 to the new reply_to feature */
437 modest_text_utils_remove_address ((const gchar *) tmp->str,
439 /* FIXME: remove also the mails from the new To: */
440 tny_header_set_cc (new_header, new_cc);
443 g_string_free (tmp, TRUE);
449 g_object_unref (G_OBJECT (new_header));
450 g_object_unref (G_OBJECT (header));
456 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
458 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
462 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
464 ModestMailOperation *self = NULL;
465 ModestMailOperationPrivate *priv = NULL;
466 RefreshFolderAsyncHelper *helper;
468 helper = (RefreshFolderAsyncHelper *) user_data;
469 self = MODEST_MAIL_OPERATION (helper->mail_op);
470 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
472 if ((canceled && *err) || *err) {
473 priv->error = g_error_copy (*err);
475 } else if (canceled) {
477 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
478 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
479 _("Error trying to refresh folder %s. Operation canceled"),
480 tny_folder_get_name (folder));
485 if (priv->done == priv->total)
486 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
487 else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
488 if (helper->failed == priv->total)
489 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
490 else if (helper->failed == priv->total)
491 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
493 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
495 tny_iterator_next (helper->iter);
496 if (tny_iterator_is_done (helper->iter)) {
498 list = tny_iterator_get_list (helper->iter);
499 g_object_unref (G_OBJECT (helper->iter));
500 g_object_unref (G_OBJECT (list));
501 g_slice_free (RefreshFolderAsyncHelper, helper);
503 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
504 tny_folder_refresh_async (folder, folder_refresh_cb,
507 g_object_unref (G_OBJECT(folder));
509 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
514 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
516 ModestMailOperation *self;
517 ModestMailOperationPrivate *priv;
518 RefreshFolderAsyncHelper *helper;
521 self = MODEST_MAIL_OPERATION (user_data);
522 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
525 priv->error = g_error_copy (*err);
526 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
530 priv->total = tny_list_get_length (list);
532 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
534 helper = g_slice_new0 (RefreshFolderAsyncHelper);
535 helper->mail_op = self;
536 helper->iter = tny_list_create_iterator (list);
538 helper->canceled = 0;
540 /* Async refresh folders */
541 folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
542 tny_folder_refresh_async (folder, folder_refresh_cb,
543 status_update_cb, helper);
544 g_object_unref (G_OBJECT(folder));
548 modest_mail_operation_update_account (ModestMailOperation *self,
549 TnyStoreAccount *store_account)
551 ModestMailOperationPrivate *priv;
553 TnyFolderStoreQuery *query;
555 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
556 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
558 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
560 /* Get subscribed folders & refresh them */
561 folders = TNY_LIST (tny_simple_list_new ());
562 query = tny_folder_store_query_new ();
563 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
564 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
565 folders, update_folders_cb, query, self);
566 g_object_unref (query);
571 ModestMailOperationStatus
572 modest_mail_operation_get_status (ModestMailOperation *self)
574 ModestMailOperationPrivate *priv;
576 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
577 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
578 MODEST_MAIL_OPERATION_STATUS_INVALID);
580 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
585 modest_mail_operation_get_error (ModestMailOperation *self)
587 ModestMailOperationPrivate *priv;
589 g_return_val_if_fail (self, NULL);
590 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
592 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
597 modest_mail_operation_cancel (ModestMailOperation *self)
604 modest_mail_operation_get_task_done (ModestMailOperation *self)
606 ModestMailOperationPrivate *priv;
608 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
610 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
615 modest_mail_operation_get_task_total (ModestMailOperation *self)
617 ModestMailOperationPrivate *priv;
619 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
621 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
626 modest_mail_operation_is_finished (ModestMailOperation *self)
628 ModestMailOperationPrivate *priv;
629 gboolean retval = FALSE;
631 if (!MODEST_IS_MAIL_OPERATION (self)) {
632 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
636 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
638 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
639 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
640 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
641 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
650 /* ******************************************************************* */
651 /* ************************** STORE ACTIONS ************************* */
652 /* ******************************************************************* */
656 modest_mail_operation_create_folder (ModestMailOperation *self,
657 TnyFolderStore *parent,
660 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
661 g_return_val_if_fail (name, NULL);
663 TnyFolder *new_folder = NULL;
664 TnyStoreAccount *store_account;
666 /* Create the folder */
667 new_folder = tny_folder_store_create_folder (parent, name, NULL); /* FIXME */
671 /* Subscribe to folder */
672 if (!tny_folder_is_subscribed (new_folder)) {
673 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
674 tny_store_account_subscribe (store_account, new_folder);
675 g_object_unref (G_OBJECT (store_account));
682 modest_mail_operation_remove_folder (ModestMailOperation *self,
684 gboolean remove_to_trash)
686 TnyFolderStore *folder_store;
688 g_return_if_fail (TNY_IS_FOLDER (folder));
690 /* Get folder store */
691 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
693 /* Delete folder or move to trash */
694 if (remove_to_trash) {
695 TnyFolder *trash_folder;
696 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store),
697 TNY_FOLDER_TYPE_TRASH);
698 /* TODO: error_handling */
699 modest_mail_operation_move_folder (self, folder,
700 TNY_FOLDER_STORE (trash_folder));
702 tny_folder_store_remove_folder (folder_store, folder, NULL); /* FIXME */
703 g_object_unref (G_OBJECT (folder));
707 g_object_unref (G_OBJECT (folder_store));
711 modest_mail_operation_rename_folder (ModestMailOperation *self,
715 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
716 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
717 g_return_if_fail (name);
719 /* FIXME: better error handling */
720 if (strrchr (name, '/') != NULL)
723 /* Rename. Camel handles folder subscription/unsubscription */
724 tny_folder_set_name (folder, name, NULL); /* FIXME */
728 modest_mail_operation_move_folder (ModestMailOperation *self,
730 TnyFolderStore *parent)
732 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
733 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
734 g_return_if_fail (TNY_IS_FOLDER (folder));
736 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
740 modest_mail_operation_copy_folder (ModestMailOperation *self,
742 TnyFolderStore *parent)
744 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
745 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
746 g_return_if_fail (TNY_IS_FOLDER (folder));
748 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
752 modest_mail_operation_xfer_folder (ModestMailOperation *self,
754 TnyFolderStore *parent,
755 gboolean delete_original)
757 const gchar *folder_name;
758 TnyFolder *dest_folder, *child;
760 TnyList *folders, *headers;
762 g_return_if_fail (TNY_IS_FOLDER (folder));
763 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
765 /* Create the destination folder */
766 folder_name = tny_folder_get_name (folder);
767 dest_folder = modest_mail_operation_create_folder (self, 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)) {
780 child = TNY_FOLDER (tny_iterator_get_current (iter));
781 modest_mail_operation_xfer_folder (self, child, TNY_FOLDER_STORE (dest_folder),
783 tny_iterator_next (iter);
784 g_object_unref (G_OBJECT(child));
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 /* ******************************************************************* */
800 /* ************************** MSG ACTIONS ************************* */
801 /* ******************************************************************* */
804 modest_mail_operation_copy_msg (ModestMailOperation *self,
808 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
809 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
811 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
815 modest_mail_operation_move_msg (ModestMailOperation *self,
819 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
820 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
822 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
826 modest_mail_operation_remove_msg (ModestMailOperation *self,
828 gboolean remove_to_trash)
832 g_return_if_fail (TNY_IS_HEADER (header));
834 folder = tny_header_get_folder (header);
836 /* Delete or move to trash */
837 if (remove_to_trash) {
838 TnyFolder *trash_folder;
839 TnyStoreAccount *store_account;
841 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
842 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
843 TNY_FOLDER_TYPE_TRASH);
845 modest_mail_operation_move_msg (self, header, trash_folder);
846 /* g_object_unref (trash_folder); */
848 ModestMailOperationPrivate *priv;
850 /* Set status failed and set an error */
851 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
852 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
853 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
854 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
855 _("Error trying to delete a message. Trash folder not found"));
858 g_object_unref (G_OBJECT (store_account));
860 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
861 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
865 g_object_unref (folder);
869 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
871 ModestMailOperationPrivate *priv;
873 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
875 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
877 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
881 modest_mail_operation_xfer_msg (ModestMailOperation *self,
884 gboolean delete_original)
886 ModestMailOperationPrivate *priv;
887 TnyFolder *src_folder;
890 src_folder = tny_header_get_folder (header);
891 headers = tny_simple_list_new ();
893 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
896 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
898 tny_list_prepend (headers, G_OBJECT (header));
899 tny_folder_transfer_msgs_async (src_folder, headers, folder,
900 delete_original, transfer_msgs_cb, self);
903 g_object_unref (headers);
904 g_object_unref (folder);
910 /* ******************************************************************* */
911 /* ************************* UTILIY FUNCTIONS ************************ */
912 /* ******************************************************************* */
914 is_ascii(const gchar *s)
917 if (s[0] & 128 || s[0] < 32)
925 get_content_type(const gchar *s)
929 type = g_string_new("text/plain");
931 if (g_utf8_validate(s, -1, NULL)) {
932 g_string_append(type, "; charset=\"utf-8\"");
934 /* it should be impossible to reach this, but better safe than sorry */
935 g_warning("invalid utf8 in message");
936 g_string_append(type, "; charset=\"latin1\"");
939 return g_string_free(type, FALSE);
943 add_attachments (TnyMsg *msg, GList *attachments_list)
946 TnyMimePart *attachment_part, *old_attachment;
947 const gchar *attachment_content_type;
948 const gchar *attachment_filename;
949 TnyStream *attachment_stream;
950 TnyPlatformFactory *fact;
952 fact = modest_tny_platform_factory_get_instance ();
953 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
955 old_attachment = pos->data;
956 attachment_filename = tny_mime_part_get_filename (old_attachment);
957 attachment_stream = tny_mime_part_get_stream (old_attachment);
958 attachment_part = tny_platform_factory_new_mime_part (fact);
960 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
962 tny_mime_part_construct_from_stream (attachment_part,
964 attachment_content_type);
965 tny_stream_reset (attachment_stream);
967 tny_mime_part_set_filename (attachment_part, attachment_filename);
969 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
970 /* g_object_unref (attachment_part); */
976 add_body_part (TnyMsg *msg,
978 const gchar *content_type,
979 gboolean has_attachments)
981 TnyMimePart *text_body_part = NULL;
982 TnyStream *text_body_stream;
983 TnyPlatformFactory *fact;
985 fact = modest_tny_platform_factory_get_instance ();
987 /* Create the stream */
988 text_body_stream = TNY_STREAM (tny_camel_stream_new
989 (camel_stream_mem_new_with_buffer
990 (body, strlen(body))));
992 /* Create body part if needed */
994 text_body_part = tny_platform_factory_new_mime_part (fact);
996 text_body_part = TNY_MIME_PART(msg);
998 /* Construct MIME part */
999 tny_stream_reset (text_body_stream);
1000 tny_mime_part_construct_from_stream (text_body_part,
1003 tny_stream_reset (text_body_stream);
1005 /* Add part if needed */
1006 if (has_attachments) {
1007 tny_mime_part_add_part (TNY_MIME_PART (msg), text_body_part);
1008 g_object_unref (G_OBJECT(text_body_part));
1012 g_object_unref (text_body_stream);
1014 return text_body_part;