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 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
299 TnyList *folders = tny_simple_list_new ();
301 tny_folder_store_get_folders (store, folders, query, NULL);
302 iter = tny_list_create_iterator (folders);
304 while (!tny_iterator_is_done (iter)) {
306 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
308 tny_list_prepend (all_folders, G_OBJECT (folder));
310 recurse_folders (folder, query, all_folders);
312 g_object_unref (G_OBJECT (folder));
314 tny_iterator_next (iter);
316 g_object_unref (G_OBJECT (iter));
317 g_object_unref (G_OBJECT (folders));
321 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
323 ModestMailOperation *self;
324 ModestMailOperationPrivate *priv;
326 TnyList *all_folders;
328 self = MODEST_MAIL_OPERATION (user_data);
329 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
331 g_message (__FUNCTION__);
334 priv->error = g_error_copy (*err);
335 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
339 /* Get all the folders We can do it synchronously because
340 we're already running in a different thread than the UI */
341 all_folders = tny_list_copy (list);
342 iter = tny_list_create_iterator (all_folders);
343 while (!tny_iterator_is_done (iter)) {
344 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
346 recurse_folders (folder, NULL, all_folders);
347 tny_iterator_next (iter);
349 g_object_unref (G_OBJECT (iter));
351 /* Refresh folders */
352 iter = tny_list_create_iterator (all_folders);
353 priv->total = tny_list_get_length (all_folders);
355 while (!tny_iterator_is_done (iter) && !priv->error) {
357 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
359 /* Refresh the folder */
360 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
363 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
365 /* Update status and notify */
367 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
370 g_object_unref (G_OBJECT (folder));
372 tny_iterator_next (iter);
375 g_object_unref (G_OBJECT (iter));
377 g_object_unref (G_OBJECT (list));
379 /* Check if the operation was a success */
380 if (priv->done == priv->total && !priv->error)
381 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
384 g_object_unref (G_OBJECT (folder_store));
386 /* Notify the queue */
387 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
391 modest_mail_operation_update_account (ModestMailOperation *self,
392 TnyStoreAccount *store_account)
394 ModestMailOperationPrivate *priv;
397 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
398 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
400 /* Pick async call reference */
401 g_object_ref (store_account);
403 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
407 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
409 /* Get subscribed folders & refresh them */
410 folders = TNY_LIST (tny_simple_list_new ());
412 g_message ("tny_folder_store_get_folders_async");
413 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
414 folders, update_folders_cb, NULL, self);
419 ModestMailOperationStatus
420 modest_mail_operation_get_status (ModestMailOperation *self)
422 ModestMailOperationPrivate *priv;
424 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
425 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
426 MODEST_MAIL_OPERATION_STATUS_INVALID);
428 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
433 modest_mail_operation_get_error (ModestMailOperation *self)
435 ModestMailOperationPrivate *priv;
437 g_return_val_if_fail (self, NULL);
438 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
440 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
445 modest_mail_operation_cancel (ModestMailOperation *self)
452 modest_mail_operation_get_task_done (ModestMailOperation *self)
454 ModestMailOperationPrivate *priv;
456 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
458 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
463 modest_mail_operation_get_task_total (ModestMailOperation *self)
465 ModestMailOperationPrivate *priv;
467 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
469 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
474 modest_mail_operation_is_finished (ModestMailOperation *self)
476 ModestMailOperationPrivate *priv;
477 gboolean retval = FALSE;
479 if (!MODEST_IS_MAIL_OPERATION (self)) {
480 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
484 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
486 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
487 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
488 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
489 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
498 /* ******************************************************************* */
499 /* ************************** STORE ACTIONS ************************* */
500 /* ******************************************************************* */
504 modest_mail_operation_create_folder (ModestMailOperation *self,
505 TnyFolderStore *parent,
508 ModestTnyFolderRules rules;
509 ModestMailOperationPrivate *priv;
510 TnyFolder *new_folder = NULL;
511 gboolean can_create = FALSE;
513 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
514 g_return_val_if_fail (name, NULL);
516 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
519 if (!TNY_IS_FOLDER (parent)) {
520 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
521 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
522 _("mail_in_ui_folder_create_error"));
524 /* Check folder rules */
525 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
526 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
527 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
528 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
529 _("mail_in_ui_folder_create_error"));
535 /* Create the folder */
536 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
537 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
540 /* Notify the queue */
541 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
547 modest_mail_operation_remove_folder (ModestMailOperation *self,
549 gboolean remove_to_trash)
552 ModestMailOperationPrivate *priv;
553 ModestTnyFolderRules rules;
555 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
556 g_return_if_fail (TNY_IS_FOLDER (folder));
558 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
560 /* Check folder rules */
561 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
562 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
563 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
564 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
565 _("mail_in_ui_folder_delete_error"));
569 /* Get the account */
570 account = tny_folder_get_account (folder);
572 /* Delete folder or move to trash */
573 if (remove_to_trash) {
574 TnyFolder *trash_folder, *new_folder;
575 trash_folder = modest_tny_account_get_special_folder (account,
576 TNY_FOLDER_TYPE_TRASH);
577 /* TODO: error_handling */
578 new_folder = modest_mail_operation_xfer_folder (self, folder,
579 TNY_FOLDER_STORE (trash_folder), TRUE);
580 g_object_unref (G_OBJECT (new_folder));
582 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
584 tny_folder_store_remove_folder (parent, folder, &(priv->error));
585 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
588 g_object_unref (G_OBJECT (parent));
590 g_object_unref (G_OBJECT (account));
593 /* Notify the queue */
594 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
598 modest_mail_operation_rename_folder (ModestMailOperation *self,
602 ModestMailOperationPrivate *priv;
603 ModestTnyFolderRules rules;
605 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
606 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
607 g_return_if_fail (name);
609 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
611 /* Check folder rules */
612 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
613 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
614 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
615 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
616 _("FIXME: unable to rename"));
618 /* Rename. Camel handles folder subscription/unsubscription */
619 tny_folder_set_name (folder, name, &(priv->error));
620 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
623 /* Notify the queue */
624 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
628 modest_mail_operation_xfer_folder (ModestMailOperation *self,
630 TnyFolderStore *parent,
631 gboolean delete_original)
633 ModestMailOperationPrivate *priv;
634 TnyFolder *new_folder = NULL;
635 ModestTnyFolderRules rules;
637 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
638 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
639 g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
641 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
643 /* The moveable restriction is applied also to copy operation */
644 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
645 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
646 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
647 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
648 _("FIXME: unable to rename"));
650 /* Move/Copy folder */
651 new_folder = tny_folder_copy (folder,
653 tny_folder_get_name (folder),
658 /* Notify the queue */
659 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
665 /* ******************************************************************* */
666 /* ************************** MSG ACTIONS ************************* */
667 /* ******************************************************************* */
670 modest_mail_operation_remove_msg (ModestMailOperation *self,
672 gboolean remove_to_trash)
675 ModestMailOperationPrivate *priv;
677 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
678 g_return_if_fail (TNY_IS_HEADER (header));
680 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
681 folder = tny_header_get_folder (header);
683 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
685 /* Delete or move to trash */
686 if (remove_to_trash) {
687 TnyFolder *trash_folder;
688 TnyStoreAccount *store_account;
690 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
691 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
692 TNY_FOLDER_TYPE_TRASH);
697 headers = tny_simple_list_new ();
698 tny_list_append (headers, G_OBJECT (header));
699 g_object_unref (header);
702 modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
703 g_object_unref (headers);
704 /* g_object_unref (trash_folder); */
706 ModestMailOperationPrivate *priv;
708 /* Set status failed and set an error */
709 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
710 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
711 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
712 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
713 _("Error trying to delete a message. Trash folder not found"));
716 g_object_unref (G_OBJECT (store_account));
718 tny_folder_remove_msg (folder, header, &(priv->error));
720 tny_folder_sync(folder, TRUE, &(priv->error));
725 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
727 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
730 g_object_unref (G_OBJECT (folder));
732 /* Notify the queue */
733 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
737 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
739 XFerMsgAsyncHelper *helper;
740 ModestMailOperation *self;
741 ModestMailOperationPrivate *priv;
743 helper = (XFerMsgAsyncHelper *) user_data;
744 self = helper->mail_op;
745 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
748 priv->error = g_error_copy (*err);
750 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
753 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
757 g_object_unref (helper->headers);
758 g_object_unref (helper->dest_folder);
759 g_object_unref (folder);
762 /* Notify the queue */
763 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
767 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
770 gboolean delete_original)
772 ModestMailOperationPrivate *priv;
774 TnyFolder *src_folder;
775 XFerMsgAsyncHelper *helper;
778 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
779 g_return_if_fail (TNY_IS_LIST (headers));
780 g_return_if_fail (TNY_IS_FOLDER (folder));
782 /* Pick references for async calls */
783 g_object_ref (folder);
785 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
788 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
790 /* Create the helper */
791 helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
792 helper->mail_op = self;
793 helper->dest_folder = folder;
794 helper->headers = headers;
796 /* Get source folder */
797 iter = tny_list_create_iterator (headers);
798 header = TNY_HEADER (tny_iterator_get_current (iter));
799 src_folder = tny_header_get_folder (header);
800 g_object_unref (header);
801 g_object_unref (iter);
803 /* Transfer messages */
804 tny_folder_transfer_msgs_async (src_folder,
813 on_refresh_folder (TnyFolder *folder,
818 ModestMailOperation *self;
819 ModestMailOperationPrivate *priv;
821 self = MODEST_MAIL_OPERATION (user_data);
822 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
825 priv->error = g_error_copy (*error);
826 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
831 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
832 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
833 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
834 _("Error trying to refresh the contents of %s"),
835 tny_folder_get_name (folder));
839 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
843 g_object_unref (folder);
845 /* Notify the queue */
846 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
850 on_refresh_folder_status_update (GObject *obj,
854 ModestMailOperation *self;
855 ModestMailOperationPrivate *priv;
857 g_return_if_fail (status != NULL);
858 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
860 self = MODEST_MAIL_OPERATION (user_data);
861 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
863 priv->done = status->position;
864 priv->total = status->of_total;
866 if (priv->done == 1 && priv->total == 100)
869 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
873 modest_mail_operation_refresh_folder (ModestMailOperation *self,
876 ModestMailOperationPrivate *priv;
878 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
880 /* Pick a reference */
881 g_object_ref (folder);
883 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
885 /* Refresh the folder. TODO: tinymail could issue a status
886 updates before the callback call then this could happen. We
887 must review the design */
888 tny_folder_refresh_async (folder,
890 on_refresh_folder_status_update,
896 _modest_mail_operation_notify_end (ModestMailOperation *self)
898 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);