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 <tny-send-queue.h>
41 #include <tny-status.h>
42 #include <camel/camel-stream-mem.h>
43 #include <glib/gi18n.h>
44 #include <modest-tny-account.h>
45 #include <modest-tny-send-queue.h>
46 #include <modest-runtime.h>
47 #include "modest-text-utils.h"
48 #include "modest-tny-msg.h"
49 #include "modest-tny-folder.h"
50 #include "modest-tny-platform-factory.h"
51 #include "modest-marshal.h"
52 #include "modest-error.h"
54 /* 'private'/'protected' functions */
55 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
56 static void modest_mail_operation_init (ModestMailOperation *obj);
57 static void modest_mail_operation_finalize (GObject *obj);
59 static void update_folders_cb (TnyFolderStore *self,
64 enum _ModestMailOperationSignals
66 PROGRESS_CHANGED_SIGNAL,
71 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
72 struct _ModestMailOperationPrivate {
75 ModestMailOperationStatus status;
76 ModestMailOperationId id;
80 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
81 MODEST_TYPE_MAIL_OPERATION, \
82 ModestMailOperationPrivate))
84 #define CHECK_EXCEPTION(priv, new_status) if (priv->error) {\
85 priv->status = new_status;\
88 typedef struct _RefreshFolderAsyncHelper
90 ModestMailOperation *mail_op;
95 } RefreshFolderAsyncHelper;
97 typedef struct _XFerMsgAsyncHelper
99 ModestMailOperation *mail_op;
101 TnyFolder *dest_folder;
103 } XFerMsgAsyncHelper;
107 static GObjectClass *parent_class = NULL;
109 static guint signals[NUM_SIGNALS] = {0};
112 modest_mail_operation_get_type (void)
114 static GType my_type = 0;
116 static const GTypeInfo my_info = {
117 sizeof(ModestMailOperationClass),
118 NULL, /* base init */
119 NULL, /* base finalize */
120 (GClassInitFunc) modest_mail_operation_class_init,
121 NULL, /* class finalize */
122 NULL, /* class data */
123 sizeof(ModestMailOperation),
125 (GInstanceInitFunc) modest_mail_operation_init,
128 my_type = g_type_register_static (G_TYPE_OBJECT,
129 "ModestMailOperation",
136 modest_mail_operation_class_init (ModestMailOperationClass *klass)
138 GObjectClass *gobject_class;
139 gobject_class = (GObjectClass*) klass;
141 parent_class = g_type_class_peek_parent (klass);
142 gobject_class->finalize = modest_mail_operation_finalize;
144 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
147 * ModestMailOperation::progress-changed
148 * @self: the #MailOperation that emits the signal
149 * @user_data: user data set when the signal handler was connected
151 * Emitted when the progress of a mail operation changes
153 signals[PROGRESS_CHANGED_SIGNAL] =
154 g_signal_new ("progress-changed",
155 G_TYPE_FROM_CLASS (gobject_class),
157 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
159 g_cclosure_marshal_VOID__VOID,
164 modest_mail_operation_init (ModestMailOperation *obj)
166 ModestMailOperationPrivate *priv;
168 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
170 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
171 priv->id = MODEST_MAIL_OPERATION_ID_UNKNOWN;
178 modest_mail_operation_finalize (GObject *obj)
180 ModestMailOperationPrivate *priv;
182 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
185 g_error_free (priv->error);
189 G_OBJECT_CLASS(parent_class)->finalize (obj);
193 modest_mail_operation_new (ModestMailOperationId id)
195 ModestMailOperation *obj;
196 ModestMailOperationPrivate *priv;
199 obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
200 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
209 ModestMailOperationId
210 modest_mail_operation_get_id (ModestMailOperation *self)
212 ModestMailOperationPrivate *priv;
214 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
220 modest_mail_operation_send_mail (ModestMailOperation *self,
221 TnyTransportAccount *transport_account,
224 TnySendQueue *send_queue;
226 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
227 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
228 g_return_if_fail (TNY_IS_MSG (msg));
230 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
231 if (!TNY_IS_SEND_QUEUE(send_queue))
232 g_printerr ("modest: could not find send queue for account\n");
235 tny_send_queue_add (send_queue, msg, &err);
237 g_printerr ("modest: error adding msg to send queue: %s\n",
241 g_message ("modest: message added to send queue");
244 /* Notify the queue */
245 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
249 modest_mail_operation_send_new_mail (ModestMailOperation *self,
250 TnyTransportAccount *transport_account,
251 const gchar *from, const gchar *to,
252 const gchar *cc, const gchar *bcc,
253 const gchar *subject, const gchar *plain_body,
254 const gchar *html_body,
255 const GList *attachments_list,
256 TnyHeaderFlags priority_flags)
259 ModestMailOperationPrivate *priv = NULL;
260 /* GList *node = NULL; */
262 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
263 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
265 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
267 /* Check parametters */
269 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
270 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
271 _("Error trying to send a mail. You need to set at least one recipient"));
275 if (html_body == NULL) {
276 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
278 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
281 g_printerr ("modest: failed to create a new msg\n");
285 /* TODO: add priority handling. It's received in the priority_flags operator, and
286 it should have effect in the sending operation */
288 /* Call mail operation */
289 modest_mail_operation_send_mail (self, transport_account, new_msg);
292 g_object_unref (G_OBJECT (new_msg));
296 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
297 TnyTransportAccount *transport_account,
298 const gchar *from, const gchar *to,
299 const gchar *cc, const gchar *bcc,
300 const gchar *subject, const gchar *plain_body,
301 const gchar *html_body,
302 const GList *attachments_list,
303 TnyHeaderFlags priority_flags)
306 TnyFolder *folder = NULL;
307 ModestMailOperationPrivate *priv = NULL;
310 /* GList *node = NULL; */
312 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
313 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
315 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
317 if (html_body == NULL) {
318 msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
320 msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
323 g_printerr ("modest: failed to create a new msg\n");
327 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account), TNY_FOLDER_TYPE_DRAFTS);
329 g_printerr ("modest: failed to find Drafts folder\n");
333 tny_folder_add_msg (folder, msg, &err);
335 g_printerr ("modest: error adding msg to Drafts folder: %s",
341 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
346 g_object_unref (G_OBJECT(msg));
348 g_object_unref (G_OBJECT(folder));
352 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
355 TnyList *folders = tny_simple_list_new ();
357 tny_folder_store_get_folders (store, folders, query, NULL);
358 iter = tny_list_create_iterator (folders);
360 while (!tny_iterator_is_done (iter)) {
362 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
364 tny_list_prepend (all_folders, G_OBJECT (folder));
366 recurse_folders (folder, query, all_folders);
368 g_object_unref (G_OBJECT (folder));
370 tny_iterator_next (iter);
372 g_object_unref (G_OBJECT (iter));
373 g_object_unref (G_OBJECT (folders));
377 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
379 ModestMailOperation *self;
380 ModestMailOperationPrivate *priv;
382 TnyList *all_folders;
384 self = MODEST_MAIL_OPERATION (user_data);
385 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
387 g_message (__FUNCTION__);
390 priv->error = g_error_copy (*err);
391 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
395 /* Get all the folders We can do it synchronously because
396 we're already running in a different thread than the UI */
397 all_folders = tny_list_copy (list);
398 iter = tny_list_create_iterator (all_folders);
399 while (!tny_iterator_is_done (iter)) {
400 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
402 recurse_folders (folder, NULL, all_folders);
403 tny_iterator_next (iter);
405 g_object_unref (G_OBJECT (iter));
407 /* Refresh folders */
408 iter = tny_list_create_iterator (all_folders);
409 priv->total = tny_list_get_length (all_folders);
411 while (!tny_iterator_is_done (iter) && !priv->error) {
413 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
415 /* Refresh the folder */
416 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
419 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
421 /* Update status and notify */
423 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
426 g_object_unref (G_OBJECT (folder));
428 tny_iterator_next (iter);
431 g_object_unref (G_OBJECT (iter));
433 g_object_unref (G_OBJECT (list));
435 /* Check if the operation was a success */
436 if (priv->done == priv->total && !priv->error)
437 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
440 g_object_unref (G_OBJECT (folder_store));
442 /* Notify the queue */
443 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
447 modest_mail_operation_update_account (ModestMailOperation *self,
448 TnyStoreAccount *store_account)
450 ModestMailOperationPrivate *priv;
453 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
454 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
456 /* Pick async call reference */
457 g_object_ref (store_account);
459 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
463 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
465 /* Get subscribed folders & refresh them */
466 folders = TNY_LIST (tny_simple_list_new ());
468 g_message ("tny_folder_store_get_folders_async");
469 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
470 folders, update_folders_cb, NULL, self);
475 ModestMailOperationStatus
476 modest_mail_operation_get_status (ModestMailOperation *self)
478 ModestMailOperationPrivate *priv;
480 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
481 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
482 MODEST_MAIL_OPERATION_STATUS_INVALID);
484 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
489 modest_mail_operation_get_error (ModestMailOperation *self)
491 ModestMailOperationPrivate *priv;
493 g_return_val_if_fail (self, NULL);
494 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
496 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
501 modest_mail_operation_cancel (ModestMailOperation *self)
508 modest_mail_operation_get_task_done (ModestMailOperation *self)
510 ModestMailOperationPrivate *priv;
512 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
514 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
519 modest_mail_operation_get_task_total (ModestMailOperation *self)
521 ModestMailOperationPrivate *priv;
523 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
525 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
530 modest_mail_operation_is_finished (ModestMailOperation *self)
532 ModestMailOperationPrivate *priv;
533 gboolean retval = FALSE;
535 if (!MODEST_IS_MAIL_OPERATION (self)) {
536 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
540 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
542 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
543 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
544 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
545 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
554 /* ******************************************************************* */
555 /* ************************** STORE ACTIONS ************************* */
556 /* ******************************************************************* */
560 modest_mail_operation_create_folder (ModestMailOperation *self,
561 TnyFolderStore *parent,
564 ModestTnyFolderRules rules;
565 ModestMailOperationPrivate *priv;
566 TnyFolder *new_folder = NULL;
567 gboolean can_create = FALSE;
569 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
570 g_return_val_if_fail (name, NULL);
572 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
575 if (!TNY_IS_FOLDER (parent)) {
576 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
577 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
578 _("mail_in_ui_folder_create_error"));
580 /* Check folder rules */
581 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
582 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
583 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
584 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
585 _("mail_in_ui_folder_create_error"));
591 /* Create the folder */
592 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
593 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
596 /* Notify the queue */
597 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
603 modest_mail_operation_remove_folder (ModestMailOperation *self,
605 gboolean remove_to_trash)
608 ModestMailOperationPrivate *priv;
609 ModestTnyFolderRules rules;
611 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
612 g_return_if_fail (TNY_IS_FOLDER (folder));
614 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
616 /* Check folder rules */
617 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
618 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
619 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
620 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
621 _("mail_in_ui_folder_delete_error"));
625 /* Get the account */
626 account = tny_folder_get_account (folder);
628 /* Delete folder or move to trash */
629 if (remove_to_trash) {
630 TnyFolder *trash_folder, *new_folder;
631 trash_folder = modest_tny_account_get_special_folder (account,
632 TNY_FOLDER_TYPE_TRASH);
633 /* TODO: error_handling */
634 new_folder = modest_mail_operation_xfer_folder (self, folder,
635 TNY_FOLDER_STORE (trash_folder), TRUE);
636 g_object_unref (G_OBJECT (new_folder));
638 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
640 tny_folder_store_remove_folder (parent, folder, &(priv->error));
641 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
644 g_object_unref (G_OBJECT (parent));
646 g_object_unref (G_OBJECT (account));
649 /* Notify the queue */
650 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
654 modest_mail_operation_rename_folder (ModestMailOperation *self,
658 ModestMailOperationPrivate *priv;
659 ModestTnyFolderRules rules;
661 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
662 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
663 g_return_if_fail (name);
665 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
667 /* Check folder rules */
668 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
669 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
670 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
671 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
672 _("FIXME: unable to rename"));
674 /* Rename. Camel handles folder subscription/unsubscription */
675 tny_folder_set_name (folder, name, &(priv->error));
676 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
679 /* Notify the queue */
680 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
684 modest_mail_operation_xfer_folder (ModestMailOperation *self,
686 TnyFolderStore *parent,
687 gboolean delete_original)
689 ModestMailOperationPrivate *priv;
690 TnyFolder *new_folder = NULL;
691 ModestTnyFolderRules rules;
693 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
694 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
695 g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
697 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
699 /* The moveable restriction is applied also to copy operation */
700 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
701 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
702 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
703 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
704 _("FIXME: unable to rename"));
706 /* Move/Copy folder */
707 new_folder = tny_folder_copy (folder,
709 tny_folder_get_name (folder),
714 /* Notify the queue */
715 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
721 /* ******************************************************************* */
722 /* ************************** MSG ACTIONS ************************* */
723 /* ******************************************************************* */
726 modest_mail_operation_remove_msg (ModestMailOperation *self,
728 gboolean remove_to_trash)
731 ModestMailOperationPrivate *priv;
733 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
734 g_return_if_fail (TNY_IS_HEADER (header));
736 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
737 folder = tny_header_get_folder (header);
739 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
741 /* Delete or move to trash */
742 if (remove_to_trash) {
743 TnyFolder *trash_folder;
744 TnyStoreAccount *store_account;
746 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
747 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
748 TNY_FOLDER_TYPE_TRASH);
753 headers = tny_simple_list_new ();
754 tny_list_append (headers, G_OBJECT (header));
755 g_object_unref (header);
758 modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
759 g_object_unref (headers);
760 /* g_object_unref (trash_folder); */
762 ModestMailOperationPrivate *priv;
764 /* Set status failed and set an error */
765 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
766 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
767 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
768 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
769 _("Error trying to delete a message. Trash folder not found"));
772 g_object_unref (G_OBJECT (store_account));
774 tny_folder_remove_msg (folder, header, &(priv->error));
776 tny_folder_sync(folder, TRUE, &(priv->error));
781 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
783 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
786 g_object_unref (G_OBJECT (folder));
788 /* Notify the queue */
789 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
793 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
795 XFerMsgAsyncHelper *helper;
796 ModestMailOperation *self;
797 ModestMailOperationPrivate *priv;
799 helper = (XFerMsgAsyncHelper *) user_data;
800 self = helper->mail_op;
801 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
804 priv->error = g_error_copy (*err);
806 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
809 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
813 g_object_unref (helper->headers);
814 g_object_unref (helper->dest_folder);
815 g_object_unref (folder);
818 /* Notify the queue */
819 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
823 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
826 gboolean delete_original)
828 ModestMailOperationPrivate *priv;
830 TnyFolder *src_folder;
831 XFerMsgAsyncHelper *helper;
834 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
835 g_return_if_fail (TNY_IS_LIST (headers));
836 g_return_if_fail (TNY_IS_FOLDER (folder));
838 /* Pick references for async calls */
839 g_object_ref (folder);
841 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
844 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
846 /* Create the helper */
847 helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
848 helper->mail_op = self;
849 helper->dest_folder = folder;
850 helper->headers = headers;
852 /* Get source folder */
853 iter = tny_list_create_iterator (headers);
854 header = TNY_HEADER (tny_iterator_get_current (iter));
855 src_folder = tny_header_get_folder (header);
856 g_object_unref (header);
857 g_object_unref (iter);
859 /* Transfer messages */
860 tny_folder_transfer_msgs_async (src_folder,
869 on_refresh_folder (TnyFolder *folder,
874 ModestMailOperation *self;
875 ModestMailOperationPrivate *priv;
877 self = MODEST_MAIL_OPERATION (user_data);
878 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
881 priv->error = g_error_copy (*error);
882 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
887 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
888 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
889 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
890 _("Error trying to refresh the contents of %s"),
891 tny_folder_get_name (folder));
895 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
899 g_object_unref (folder);
901 /* Notify the queue */
902 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
906 on_refresh_folder_status_update (GObject *obj,
910 ModestMailOperation *self;
911 ModestMailOperationPrivate *priv;
913 g_return_if_fail (status != NULL);
914 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
916 self = MODEST_MAIL_OPERATION (user_data);
917 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
919 priv->done = status->position;
920 priv->total = status->of_total;
922 if (priv->done == 1 && priv->total == 100)
925 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
929 modest_mail_operation_refresh_folder (ModestMailOperation *self,
932 ModestMailOperationPrivate *priv;
934 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
936 /* Pick a reference */
937 g_object_ref (folder);
939 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
941 /* Refresh the folder. TODO: tinymail could issue a status
942 updates before the callback call then this could happen. We
943 must review the design */
944 tny_folder_refresh_async (folder,
946 on_refresh_folder_status_update,
952 _modest_mail_operation_notify_end (ModestMailOperation *self)
954 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);