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);
73 static void modest_mail_operation_xfer_folder (ModestMailOperation *self,
75 TnyFolderStore *parent,
76 gboolean delete_original);
78 static gboolean modest_mail_operation_xfer_msg (ModestMailOperation *self,
81 gboolean delete_original);
83 enum _ModestMailOperationSignals
85 PROGRESS_CHANGED_SIGNAL,
90 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
91 struct _ModestMailOperationPrivate {
94 ModestMailOperationStatus status;
98 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99 MODEST_TYPE_MAIL_OPERATION, \
100 ModestMailOperationPrivate))
102 #define CHECK_EXCEPTION(priv, new_status, op) if (priv->error) {\
103 priv->status = new_status;\
107 typedef struct _RefreshFolderAsyncHelper
109 ModestMailOperation *mail_op;
114 } RefreshFolderAsyncHelper;
117 static GObjectClass *parent_class = NULL;
119 static guint signals[NUM_SIGNALS] = {0};
122 modest_mail_operation_get_type (void)
124 static GType my_type = 0;
126 static const GTypeInfo my_info = {
127 sizeof(ModestMailOperationClass),
128 NULL, /* base init */
129 NULL, /* base finalize */
130 (GClassInitFunc) modest_mail_operation_class_init,
131 NULL, /* class finalize */
132 NULL, /* class data */
133 sizeof(ModestMailOperation),
135 (GInstanceInitFunc) modest_mail_operation_init,
138 my_type = g_type_register_static (G_TYPE_OBJECT,
139 "ModestMailOperation",
146 modest_mail_operation_class_init (ModestMailOperationClass *klass)
148 GObjectClass *gobject_class;
149 gobject_class = (GObjectClass*) klass;
151 parent_class = g_type_class_peek_parent (klass);
152 gobject_class->finalize = modest_mail_operation_finalize;
154 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
157 * ModestMailOperation::progress-changed
158 * @self: the #MailOperation that emits the signal
159 * @user_data: user data set when the signal handler was connected
161 * Emitted when the progress of a mail operation changes
163 signals[PROGRESS_CHANGED_SIGNAL] =
164 g_signal_new ("progress_changed",
165 G_TYPE_FROM_CLASS (gobject_class),
167 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
169 g_cclosure_marshal_VOID__VOID,
174 modest_mail_operation_init (ModestMailOperation *obj)
176 ModestMailOperationPrivate *priv;
178 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
180 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
187 modest_mail_operation_finalize (GObject *obj)
189 ModestMailOperationPrivate *priv;
191 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
194 g_error_free (priv->error);
198 G_OBJECT_CLASS(parent_class)->finalize (obj);
202 modest_mail_operation_new (void)
204 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
209 modest_mail_operation_send_mail (ModestMailOperation *self,
210 TnyTransportAccount *transport_account,
213 TnySendQueue *send_queue;
215 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
216 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
218 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
219 if (!TNY_IS_SEND_QUEUE(send_queue))
220 g_printerr ("modest: could not find send queue for account\n");
223 tny_send_queue_add (send_queue, msg, &err);
225 g_printerr ("modest: error adding msg to send queue: %s\n",
233 modest_mail_operation_send_new_mail (ModestMailOperation *self,
234 TnyTransportAccount *transport_account,
235 const gchar *from, const gchar *to,
236 const gchar *cc, const gchar *bcc,
237 const gchar *subject, const gchar *body,
238 const GList *attachments_list)
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 at least one recipient"));
256 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, body, NULL); /* FIXME: attachments */
258 g_printerr ("modest: failed to create a new msg\n");
262 modest_mail_operation_send_mail (self, transport_account, new_msg);
264 g_object_unref (G_OBJECT(new_msg));
268 add_if_attachment (gpointer data, gpointer user_data)
271 GList *attachments_list;
273 part = TNY_MIME_PART (data);
274 attachments_list = (GList *) user_data;
276 if (tny_mime_part_is_attachment (part))
277 attachments_list = g_list_prepend (attachments_list, part);
282 create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type)
284 TnyPlatformFactory *fact;
286 TnyHeader *new_header, *header;
289 ModestFormatter *formatter;
291 /* Get body from original msg. Always look for the text/plain
292 part of the message to create the reply/forwarded mail */
293 header = tny_msg_get_header (msg);
294 body = modest_tny_msg_find_body_part (msg, FALSE);
296 /* TODO: select the formatter from account prefs */
297 formatter = modest_formatter_new ("text/plain");
299 /* Format message body */
302 case MODEST_MAIL_OPERATION_REPLY_TYPE_CITE:
304 new_msg = modest_formatter_cite (formatter, body, header);
306 case MODEST_MAIL_OPERATION_REPLY_TYPE_QUOTE:
307 new_msg = modest_formatter_quote (formatter, body, header);
312 case MODEST_MAIL_OPERATION_FORWARD_TYPE_INLINE:
314 new_msg = modest_formatter_inline (formatter, body, header);
316 case MODEST_MAIL_OPERATION_FORWARD_TYPE_ATTACHMENT:
317 new_msg = modest_formatter_attach (formatter, body, header);
321 g_object_unref (G_OBJECT(formatter));
322 g_object_unref (G_OBJECT(body));
324 /* Fill the header */
325 fact = modest_tny_platform_factory_get_instance ();
326 new_header = TNY_HEADER (tny_platform_factory_new_header (fact));
327 tny_msg_set_header (new_msg, new_header);
328 tny_header_set_from (new_header, from);
329 tny_header_set_replyto (new_header, from);
331 /* Change the subject */
333 (gchar *) modest_text_utils_derived_subject (tny_header_get_subject(header),
334 (is_reply) ? _("Re:") : _("Fwd:"));
335 tny_header_set_subject (new_header, (const gchar *) new_subject);
336 g_free (new_subject);
339 g_object_unref (G_OBJECT (new_header));
340 g_object_unref (G_OBJECT (header));
346 modest_mail_operation_create_forward_mail (TnyMsg *msg,
348 ModestMailOperationForwardType forward_type)
351 TnyList *parts = NULL;
352 GList *attachments_list = NULL;
354 new_msg = create_reply_forward_mail (msg, from, FALSE, forward_type);
356 /* Add attachments */
357 parts = TNY_LIST (tny_simple_list_new());
358 tny_mime_part_get_parts (TNY_MIME_PART (msg), parts);
359 tny_list_foreach (parts, add_if_attachment, attachments_list);
360 add_attachments (new_msg, attachments_list);
363 if (attachments_list)
364 g_list_free (attachments_list);
365 g_object_unref (G_OBJECT (parts));
371 modest_mail_operation_create_reply_mail (TnyMsg *msg,
373 ModestMailOperationReplyType reply_type,
374 ModestMailOperationReplyMode reply_mode)
376 TnyMsg *new_msg = NULL;
377 TnyHeader *new_header, *header;
378 const gchar* reply_to;
379 gchar *new_cc = NULL;
380 const gchar *cc = NULL, *bcc = NULL;
383 new_msg = create_reply_forward_mail (msg, from, TRUE, reply_type);
385 /* Fill the header */
386 header = tny_msg_get_header (msg);
387 new_header = tny_msg_get_header (new_msg);
388 reply_to = tny_header_get_replyto (header);
391 tny_header_set_to (new_header, reply_to);
393 tny_header_set_to (new_header, tny_header_get_from (header));
395 switch (reply_mode) {
396 case MODEST_MAIL_OPERATION_REPLY_MODE_SENDER:
397 /* Do not fill neither cc nor bcc */
399 case MODEST_MAIL_OPERATION_REPLY_MODE_LIST:
402 case MODEST_MAIL_OPERATION_REPLY_MODE_ALL:
403 /* Concatenate to, cc and bcc */
404 cc = tny_header_get_cc (header);
405 bcc = tny_header_get_bcc (header);
407 tmp = g_string_new (tny_header_get_to (header));
408 if (cc) g_string_append_printf (tmp, ",%s",cc);
409 if (bcc) g_string_append_printf (tmp, ",%s",bcc);
411 /* Remove my own address from the cc list. TODO:
412 remove also the To: of the new message, needed due
413 to the new reply_to feature */
415 modest_text_utils_remove_address ((const gchar *) tmp->str,
417 /* FIXME: remove also the mails from the new To: */
418 tny_header_set_cc (new_header, new_cc);
421 g_string_free (tmp, TRUE);
427 g_object_unref (G_OBJECT (new_header));
428 g_object_unref (G_OBJECT (header));
434 status_update_cb (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
436 g_print ("%s status: %d, of total %d\n", what, status, oftotal);
440 folder_refresh_cb (TnyFolder *folder, gboolean canceled, GError **err, gpointer user_data)
442 ModestMailOperation *self = NULL;
443 ModestMailOperationPrivate *priv = NULL;
444 RefreshFolderAsyncHelper *helper;
446 helper = (RefreshFolderAsyncHelper *) user_data;
447 self = MODEST_MAIL_OPERATION (helper->mail_op);
448 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
450 if ((canceled && *err) || *err) {
451 priv->error = g_error_copy (*err);
453 } else if (canceled) {
455 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
456 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
457 _("Error trying to refresh folder %s. Operation canceled"),
458 tny_folder_get_name (folder));
463 if (priv->done == priv->total)
464 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
465 else if ((priv->done + helper->canceled + helper->failed) == priv->total) {
466 if (helper->failed == priv->total)
467 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
468 else if (helper->failed == priv->total)
469 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
471 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
473 tny_iterator_next (helper->iter);
474 if (tny_iterator_is_done (helper->iter)) {
476 list = tny_iterator_get_list (helper->iter);
477 g_object_unref (G_OBJECT (helper->iter));
478 g_object_unref (G_OBJECT (list));
479 g_slice_free (RefreshFolderAsyncHelper, helper);
481 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
482 tny_folder_refresh_async (folder, folder_refresh_cb,
485 g_object_unref (G_OBJECT(folder));
487 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
492 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
494 ModestMailOperation *self;
495 ModestMailOperationPrivate *priv;
496 RefreshFolderAsyncHelper *helper;
499 self = MODEST_MAIL_OPERATION (user_data);
500 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
503 priv->error = g_error_copy (*err);
504 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
508 priv->total = tny_list_get_length (list);
510 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
512 helper = g_slice_new0 (RefreshFolderAsyncHelper);
513 helper->mail_op = self;
514 helper->iter = tny_list_create_iterator (list);
516 helper->canceled = 0;
518 /* Async refresh folders */
519 folder = TNY_FOLDER (tny_iterator_get_current (helper->iter));
520 tny_folder_refresh_async (folder, folder_refresh_cb,
521 status_update_cb, helper);
522 g_object_unref (G_OBJECT(folder));
526 modest_mail_operation_update_account (ModestMailOperation *self,
527 TnyStoreAccount *store_account)
529 ModestMailOperationPrivate *priv;
531 TnyFolderStoreQuery *query;
533 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
534 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
536 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
538 /* Get subscribed folders & refresh them */
539 folders = TNY_LIST (tny_simple_list_new ());
540 query = tny_folder_store_query_new ();
541 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
542 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
543 folders, update_folders_cb, query, self);
544 g_object_unref (query);
549 ModestMailOperationStatus
550 modest_mail_operation_get_status (ModestMailOperation *self)
552 ModestMailOperationPrivate *priv;
554 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
555 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
556 MODEST_MAIL_OPERATION_STATUS_INVALID);
558 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
563 modest_mail_operation_get_error (ModestMailOperation *self)
565 ModestMailOperationPrivate *priv;
567 g_return_val_if_fail (self, NULL);
568 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
570 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
575 modest_mail_operation_cancel (ModestMailOperation *self)
582 modest_mail_operation_get_task_done (ModestMailOperation *self)
584 ModestMailOperationPrivate *priv;
586 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
588 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
593 modest_mail_operation_get_task_total (ModestMailOperation *self)
595 ModestMailOperationPrivate *priv;
597 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
599 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
604 modest_mail_operation_is_finished (ModestMailOperation *self)
606 ModestMailOperationPrivate *priv;
607 gboolean retval = FALSE;
609 if (!MODEST_IS_MAIL_OPERATION (self)) {
610 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
614 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
616 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
617 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
618 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
619 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
628 /* ******************************************************************* */
629 /* ************************** STORE ACTIONS ************************* */
630 /* ******************************************************************* */
634 modest_mail_operation_create_folder (ModestMailOperation *self,
635 TnyFolderStore *parent,
638 ModestMailOperationPrivate *priv;
639 TnyFolder *new_folder = NULL;
640 TnyStoreAccount *store_account;
642 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
643 g_return_val_if_fail (name, NULL);
645 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
647 /* Create the folder */
648 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
649 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return NULL);
651 /* Subscribe to folder */
652 if (!tny_folder_is_subscribed (new_folder)) {
653 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (TNY_FOLDER (parent)));
654 tny_store_account_subscribe (store_account, new_folder);
655 g_object_unref (G_OBJECT (store_account));
662 modest_mail_operation_remove_folder (ModestMailOperation *self,
664 gboolean remove_to_trash)
666 TnyFolderStore *folder_store;
667 ModestMailOperationPrivate *priv;
669 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
670 g_return_if_fail (TNY_IS_FOLDER (folder));
672 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
674 /* Get folder store */
675 folder_store = TNY_FOLDER_STORE (tny_folder_get_account (folder));
677 /* Delete folder or move to trash */
678 if (remove_to_trash) {
679 TnyFolder *trash_folder;
680 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(folder_store),
681 TNY_FOLDER_TYPE_TRASH);
682 /* TODO: error_handling */
683 modest_mail_operation_move_folder (self, folder,
684 TNY_FOLDER_STORE (trash_folder));
686 tny_folder_store_remove_folder (folder_store, folder, &(priv->error));
687 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, );
689 g_object_unref (G_OBJECT (folder));
693 g_object_unref (G_OBJECT (folder_store));
697 modest_mail_operation_rename_folder (ModestMailOperation *self,
701 ModestMailOperationPrivate *priv;
703 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
704 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
705 g_return_if_fail (name);
707 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
709 /* FIXME: better error handling */
710 if (strrchr (name, '/') != NULL)
713 /* Rename. Camel handles folder subscription/unsubscription */
714 tny_folder_set_name (folder, name, &(priv->error));
715 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, return);
719 modest_mail_operation_move_folder (ModestMailOperation *self,
721 TnyFolderStore *parent)
723 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
724 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
725 g_return_if_fail (TNY_IS_FOLDER (folder));
727 modest_mail_operation_xfer_folder (self, folder, parent, TRUE);
731 modest_mail_operation_copy_folder (ModestMailOperation *self,
733 TnyFolderStore *parent)
735 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
736 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
737 g_return_if_fail (TNY_IS_FOLDER (folder));
739 modest_mail_operation_xfer_folder (self, folder, parent, FALSE);
743 modest_mail_operation_xfer_folder (ModestMailOperation *self,
745 TnyFolderStore *parent,
746 gboolean delete_original)
748 ModestMailOperationPrivate *priv;
749 const gchar *folder_name;
750 TnyFolder *dest_folder = NULL, *child = NULL;
751 TnyIterator *iter = NULL;
752 TnyList *folders = NULL, *headers = NULL;
754 g_return_if_fail (TNY_IS_FOLDER (folder));
755 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
757 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
759 /* Create the destination folder */
760 folder_name = tny_folder_get_name (folder);
761 dest_folder = modest_mail_operation_create_folder (self, parent, folder_name);
762 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
764 /* Transfer messages */
765 headers = TNY_LIST (tny_simple_list_new ());
766 tny_folder_get_headers (folder, headers, FALSE, &(priv->error));
767 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
769 tny_folder_transfer_msgs (folder, headers, dest_folder, delete_original, &(priv->error));
770 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
772 /* Recurse children */
773 folders = TNY_LIST (tny_simple_list_new ());
774 tny_folder_store_get_folders (TNY_FOLDER_STORE (folder), folders, NULL, &(priv->error));
775 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED, goto clean);
777 iter = tny_list_create_iterator (folders);
778 while (!tny_iterator_is_done (iter)) {
779 child = TNY_FOLDER (tny_iterator_get_current (iter));
780 modest_mail_operation_xfer_folder (self, child, TNY_FOLDER_STORE (dest_folder),
782 tny_iterator_next (iter);
783 g_object_unref (G_OBJECT(child));
786 /* Delete source folder (if needed) */
788 modest_mail_operation_remove_folder (self, folder, FALSE);
793 g_object_unref (G_OBJECT (dest_folder));
795 g_object_unref (G_OBJECT (headers));
797 g_object_unref (G_OBJECT (folders));
799 g_object_unref (G_OBJECT (iter));
803 /* ******************************************************************* */
804 /* ************************** MSG ACTIONS ************************* */
805 /* ******************************************************************* */
808 modest_mail_operation_copy_msg (ModestMailOperation *self,
812 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
813 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
815 return modest_mail_operation_xfer_msg (self, header, folder, FALSE);
819 modest_mail_operation_move_msg (ModestMailOperation *self,
823 g_return_val_if_fail (TNY_IS_HEADER (header), FALSE);
824 g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE);
826 return modest_mail_operation_xfer_msg (self, header, folder, TRUE);
830 modest_mail_operation_remove_msg (ModestMailOperation *self,
832 gboolean remove_to_trash)
836 g_return_if_fail (TNY_IS_HEADER (header));
838 folder = tny_header_get_folder (header);
840 /* Delete or move to trash */
841 if (remove_to_trash) {
842 TnyFolder *trash_folder;
843 TnyStoreAccount *store_account;
845 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
846 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
847 TNY_FOLDER_TYPE_TRASH);
849 modest_mail_operation_move_msg (self, header, trash_folder);
850 /* g_object_unref (trash_folder); */
852 ModestMailOperationPrivate *priv;
854 /* Set status failed and set an error */
855 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
856 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
857 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
858 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
859 _("Error trying to delete a message. Trash folder not found"));
862 g_object_unref (G_OBJECT (store_account));
864 tny_folder_remove_msg (folder, header, NULL); /* FIXME */
865 tny_folder_sync(folder, TRUE, NULL); /* FIXME */
869 g_object_unref (folder);
873 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
875 ModestMailOperationPrivate *priv;
877 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(user_data);
879 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
881 g_signal_emit (G_OBJECT (user_data), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
885 modest_mail_operation_xfer_msg (ModestMailOperation *self,
888 gboolean delete_original)
890 ModestMailOperationPrivate *priv;
891 TnyFolder *src_folder;
894 src_folder = tny_header_get_folder (header);
895 headers = tny_simple_list_new ();
897 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
900 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
902 tny_list_prepend (headers, G_OBJECT (header));
903 tny_folder_transfer_msgs_async (src_folder, headers, folder,
904 delete_original, transfer_msgs_cb, self);
907 g_object_unref (headers);
908 g_object_unref (folder);
914 /* ******************************************************************* */
915 /* ************************* UTILIY FUNCTIONS ************************ */
916 /* ******************************************************************* */
919 add_attachments (TnyMsg *msg, GList *attachments_list)
922 TnyMimePart *attachment_part, *old_attachment;
923 const gchar *attachment_content_type;
924 const gchar *attachment_filename;
925 TnyStream *attachment_stream;
926 TnyPlatformFactory *fact;
928 fact = modest_tny_platform_factory_get_instance ();
929 for (pos = (GList *)attachments_list; pos; pos = pos->next) {
931 old_attachment = pos->data;
932 attachment_filename = tny_mime_part_get_filename (old_attachment);
933 attachment_stream = tny_mime_part_get_stream (old_attachment);
934 attachment_part = tny_platform_factory_new_mime_part (fact);
936 attachment_content_type = tny_mime_part_get_content_type (old_attachment);
938 tny_mime_part_construct_from_stream (attachment_part,
940 attachment_content_type);
941 tny_stream_reset (attachment_stream);
943 tny_mime_part_set_filename (attachment_part, attachment_filename);
945 tny_mime_part_add_part (TNY_MIME_PART (msg), attachment_part);
946 /* g_object_unref (attachment_part); */