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_with_id (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);
208 modest_mail_operation_new ()
210 return MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
214 ModestMailOperationId
215 modest_mail_operation_get_id (ModestMailOperation *self)
217 ModestMailOperationPrivate *priv;
219 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
225 modest_mail_operation_send_mail (ModestMailOperation *self,
226 TnyTransportAccount *transport_account,
229 TnySendQueue *send_queue;
231 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
232 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
233 g_return_if_fail (TNY_IS_MSG (msg));
235 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
236 if (!TNY_IS_SEND_QUEUE(send_queue))
237 g_printerr ("modest: could not find send queue for account\n");
240 tny_send_queue_add (send_queue, msg, &err);
242 g_printerr ("modest: error adding msg to send queue: %s\n",
246 g_message ("modest: message added to send queue");
249 /* Notify the queue */
250 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
254 modest_mail_operation_send_new_mail (ModestMailOperation *self,
255 TnyTransportAccount *transport_account,
256 const gchar *from, const gchar *to,
257 const gchar *cc, const gchar *bcc,
258 const gchar *subject, const gchar *plain_body,
259 const gchar *html_body,
260 const GList *attachments_list,
261 TnyHeaderFlags priority_flags)
264 ModestMailOperationPrivate *priv = NULL;
265 /* GList *node = NULL; */
267 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
268 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
270 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
272 /* Check parametters */
274 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
275 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
276 _("Error trying to send a mail. You need to set at least one recipient"));
280 if (html_body == NULL) {
281 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
283 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
286 g_printerr ("modest: failed to create a new msg\n");
290 /* TODO: add priority handling. It's received in the priority_flags operator, and
291 it should have effect in the sending operation */
293 /* Call mail operation */
294 modest_mail_operation_send_mail (self, transport_account, new_msg);
297 g_object_unref (G_OBJECT (new_msg));
301 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
304 TnyList *folders = tny_simple_list_new ();
306 tny_folder_store_get_folders (store, folders, query, NULL);
307 iter = tny_list_create_iterator (folders);
309 while (!tny_iterator_is_done (iter)) {
311 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
313 tny_list_prepend (all_folders, G_OBJECT (folder));
315 recurse_folders (folder, query, all_folders);
317 g_object_unref (G_OBJECT (folder));
319 tny_iterator_next (iter);
321 g_object_unref (G_OBJECT (iter));
322 g_object_unref (G_OBJECT (folders));
326 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
328 ModestMailOperation *self;
329 ModestMailOperationPrivate *priv;
331 TnyList *all_folders;
333 self = MODEST_MAIL_OPERATION (user_data);
334 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
336 g_message (__FUNCTION__);
339 priv->error = g_error_copy (*err);
340 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
344 /* Get all the folders We can do it synchronously because
345 we're already running in a different thread than the UI */
346 all_folders = tny_list_copy (list);
347 iter = tny_list_create_iterator (all_folders);
348 while (!tny_iterator_is_done (iter)) {
349 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
351 recurse_folders (folder, NULL, all_folders);
352 tny_iterator_next (iter);
354 g_object_unref (G_OBJECT (iter));
356 /* Refresh folders */
357 iter = tny_list_create_iterator (all_folders);
358 priv->total = tny_list_get_length (all_folders);
360 while (!tny_iterator_is_done (iter) && !priv->error) {
362 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
364 /* Refresh the folder */
365 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
368 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
370 /* Update status and notify */
372 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
375 g_object_unref (G_OBJECT (folder));
377 tny_iterator_next (iter);
380 g_object_unref (G_OBJECT (iter));
382 g_object_unref (G_OBJECT (list));
384 /* Check if the operation was a success */
385 if (priv->done == priv->total && !priv->error)
386 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
389 g_object_unref (G_OBJECT (folder_store));
391 /* Notify the queue */
392 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
396 modest_mail_operation_update_account (ModestMailOperation *self,
397 TnyStoreAccount *store_account)
399 ModestMailOperationPrivate *priv;
402 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
403 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
405 /* Pick async call reference */
406 g_object_ref (store_account);
408 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
412 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
414 /* Get subscribed folders & refresh them */
415 folders = TNY_LIST (tny_simple_list_new ());
417 g_message ("tny_folder_store_get_folders_async");
418 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
419 folders, update_folders_cb, NULL, self);
424 ModestMailOperationStatus
425 modest_mail_operation_get_status (ModestMailOperation *self)
427 ModestMailOperationPrivate *priv;
429 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
430 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
431 MODEST_MAIL_OPERATION_STATUS_INVALID);
433 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
438 modest_mail_operation_get_error (ModestMailOperation *self)
440 ModestMailOperationPrivate *priv;
442 g_return_val_if_fail (self, NULL);
443 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
445 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
450 modest_mail_operation_cancel (ModestMailOperation *self)
457 modest_mail_operation_get_task_done (ModestMailOperation *self)
459 ModestMailOperationPrivate *priv;
461 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
463 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
468 modest_mail_operation_get_task_total (ModestMailOperation *self)
470 ModestMailOperationPrivate *priv;
472 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
474 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
479 modest_mail_operation_is_finished (ModestMailOperation *self)
481 ModestMailOperationPrivate *priv;
482 gboolean retval = FALSE;
484 if (!MODEST_IS_MAIL_OPERATION (self)) {
485 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
489 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
491 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
492 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
493 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
494 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
503 /* ******************************************************************* */
504 /* ************************** STORE ACTIONS ************************* */
505 /* ******************************************************************* */
509 modest_mail_operation_create_folder (ModestMailOperation *self,
510 TnyFolderStore *parent,
513 ModestTnyFolderRules rules;
514 ModestMailOperationPrivate *priv;
515 TnyFolder *new_folder = NULL;
516 gboolean can_create = FALSE;
518 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
519 g_return_val_if_fail (name, NULL);
521 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
524 if (!TNY_IS_FOLDER (parent)) {
525 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
526 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
527 _("mail_in_ui_folder_create_error"));
529 /* Check folder rules */
530 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
531 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
532 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
533 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
534 _("mail_in_ui_folder_create_error"));
540 /* Create the folder */
541 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
542 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
545 /* Notify the queue */
546 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
552 modest_mail_operation_remove_folder (ModestMailOperation *self,
554 gboolean remove_to_trash)
557 ModestMailOperationPrivate *priv;
558 ModestTnyFolderRules rules;
560 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
561 g_return_if_fail (TNY_IS_FOLDER (folder));
563 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
565 /* Check folder rules */
566 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
567 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
568 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
569 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
570 _("mail_in_ui_folder_delete_error"));
574 /* Get the account */
575 account = tny_folder_get_account (folder);
577 /* Delete folder or move to trash */
578 if (remove_to_trash) {
579 TnyFolder *trash_folder, *new_folder;
580 trash_folder = modest_tny_account_get_special_folder (account,
581 TNY_FOLDER_TYPE_TRASH);
582 /* TODO: error_handling */
583 new_folder = modest_mail_operation_xfer_folder (self, folder,
584 TNY_FOLDER_STORE (trash_folder), TRUE);
585 g_object_unref (G_OBJECT (new_folder));
587 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
589 tny_folder_store_remove_folder (parent, folder, &(priv->error));
590 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
593 g_object_unref (G_OBJECT (parent));
595 g_object_unref (G_OBJECT (account));
598 /* Notify the queue */
599 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
603 modest_mail_operation_rename_folder (ModestMailOperation *self,
607 ModestMailOperationPrivate *priv;
608 ModestTnyFolderRules rules;
610 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
611 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
612 g_return_if_fail (name);
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_RENAMEABLE) {
619 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
620 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
621 _("FIXME: unable to rename"));
623 /* Rename. Camel handles folder subscription/unsubscription */
624 tny_folder_set_name (folder, name, &(priv->error));
625 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
628 /* Notify the queue */
629 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
633 modest_mail_operation_xfer_folder (ModestMailOperation *self,
635 TnyFolderStore *parent,
636 gboolean delete_original)
638 ModestMailOperationPrivate *priv;
639 TnyFolder *new_folder = NULL;
640 ModestTnyFolderRules rules;
642 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
643 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
644 g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
646 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
648 /* The moveable restriction is applied also to copy operation */
649 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
650 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
651 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
652 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
653 _("FIXME: unable to rename"));
655 /* Move/Copy folder */
656 new_folder = tny_folder_copy (folder,
658 tny_folder_get_name (folder),
663 /* Notify the queue */
664 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
670 /* ******************************************************************* */
671 /* ************************** MSG ACTIONS ************************* */
672 /* ******************************************************************* */
675 modest_mail_operation_remove_msg (ModestMailOperation *self,
677 gboolean remove_to_trash)
680 ModestMailOperationPrivate *priv;
682 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
683 g_return_if_fail (TNY_IS_HEADER (header));
685 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
686 folder = tny_header_get_folder (header);
688 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
690 /* Delete or move to trash */
691 if (remove_to_trash) {
692 TnyFolder *trash_folder;
693 TnyStoreAccount *store_account;
695 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
696 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
697 TNY_FOLDER_TYPE_TRASH);
702 headers = tny_simple_list_new ();
703 tny_list_append (headers, G_OBJECT (header));
704 g_object_unref (header);
707 modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
708 g_object_unref (headers);
709 /* g_object_unref (trash_folder); */
711 ModestMailOperationPrivate *priv;
713 /* Set status failed and set an error */
714 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
715 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
716 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
717 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
718 _("Error trying to delete a message. Trash folder not found"));
721 g_object_unref (G_OBJECT (store_account));
723 tny_folder_remove_msg (folder, header, &(priv->error));
725 tny_folder_sync(folder, TRUE, &(priv->error));
730 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
732 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
735 g_object_unref (G_OBJECT (folder));
737 /* Notify the queue */
738 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
742 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
744 XFerMsgAsyncHelper *helper;
745 ModestMailOperation *self;
746 ModestMailOperationPrivate *priv;
748 helper = (XFerMsgAsyncHelper *) user_data;
749 self = helper->mail_op;
750 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
753 priv->error = g_error_copy (*err);
755 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
758 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
762 g_object_unref (helper->headers);
763 g_object_unref (helper->dest_folder);
764 g_object_unref (folder);
767 /* Notify the queue */
768 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
772 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
775 gboolean delete_original)
777 ModestMailOperationPrivate *priv;
779 TnyFolder *src_folder;
780 XFerMsgAsyncHelper *helper;
783 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
784 g_return_if_fail (TNY_IS_LIST (headers));
785 g_return_if_fail (TNY_IS_FOLDER (folder));
787 /* Pick references for async calls */
788 g_object_ref (folder);
790 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
793 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
795 /* Create the helper */
796 helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
797 helper->mail_op = self;
798 helper->dest_folder = folder;
799 helper->headers = headers;
801 /* Get source folder */
802 iter = tny_list_create_iterator (headers);
803 header = TNY_HEADER (tny_iterator_get_current (iter));
804 src_folder = tny_header_get_folder (header);
805 g_object_unref (header);
806 g_object_unref (iter);
808 /* Transfer messages */
809 tny_folder_transfer_msgs_async (src_folder,
818 on_refresh_folder (TnyFolder *folder,
823 ModestMailOperation *self;
824 ModestMailOperationPrivate *priv;
826 self = MODEST_MAIL_OPERATION (user_data);
827 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
830 priv->error = g_error_copy (*error);
831 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
836 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
837 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
838 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
839 _("Error trying to refresh the contents of %s"),
840 tny_folder_get_name (folder));
844 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
848 g_object_unref (folder);
850 /* Notify the queue */
851 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
855 on_refresh_folder_status_update (GObject *obj,
859 ModestMailOperation *self;
860 ModestMailOperationPrivate *priv;
862 g_return_if_fail (status != NULL);
863 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
865 self = MODEST_MAIL_OPERATION (user_data);
866 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
868 priv->done = status->position;
869 priv->total = status->of_total;
871 if (priv->done == 1 && priv->total == 100)
874 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
878 modest_mail_operation_refresh_folder (ModestMailOperation *self,
881 ModestMailOperationPrivate *priv;
883 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
885 /* Pick a reference */
886 g_object_ref (folder);
888 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
890 /* Refresh the folder. TODO: tinymail could issue a status
891 updates before the callback call then this could happen. We
892 must review the design */
893 tny_folder_refresh_async (folder,
895 on_refresh_folder_status_update,
901 _modest_mail_operation_notify_end (ModestMailOperation *self)
903 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);