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,
63 static void update_folders_status_cb (GObject *obj,
67 static void update_process_msg_status_cb (GObject *obj,
71 enum _ModestMailOperationSignals
73 PROGRESS_CHANGED_SIGNAL,
78 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
79 struct _ModestMailOperationPrivate {
82 ModestMailOperationStatus status;
83 ModestMailOperationId id;
87 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
88 MODEST_TYPE_MAIL_OPERATION, \
89 ModestMailOperationPrivate))
91 #define CHECK_EXCEPTION(priv, new_status) if (priv->error) {\
92 priv->status = new_status;\
95 typedef struct _RefreshFolderAsyncHelper
97 ModestMailOperation *mail_op;
102 } RefreshFolderAsyncHelper;
104 typedef struct _XFerMsgAsyncHelper
106 ModestMailOperation *mail_op;
108 TnyFolder *dest_folder;
110 } XFerMsgAsyncHelper;
114 static GObjectClass *parent_class = NULL;
116 static guint signals[NUM_SIGNALS] = {0};
119 modest_mail_operation_get_type (void)
121 static GType my_type = 0;
123 static const GTypeInfo my_info = {
124 sizeof(ModestMailOperationClass),
125 NULL, /* base init */
126 NULL, /* base finalize */
127 (GClassInitFunc) modest_mail_operation_class_init,
128 NULL, /* class finalize */
129 NULL, /* class data */
130 sizeof(ModestMailOperation),
132 (GInstanceInitFunc) modest_mail_operation_init,
135 my_type = g_type_register_static (G_TYPE_OBJECT,
136 "ModestMailOperation",
143 modest_mail_operation_class_init (ModestMailOperationClass *klass)
145 GObjectClass *gobject_class;
146 gobject_class = (GObjectClass*) klass;
148 parent_class = g_type_class_peek_parent (klass);
149 gobject_class->finalize = modest_mail_operation_finalize;
151 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
154 * ModestMailOperation::progress-changed
155 * @self: the #MailOperation that emits the signal
156 * @user_data: user data set when the signal handler was connected
158 * Emitted when the progress of a mail operation changes
160 signals[PROGRESS_CHANGED_SIGNAL] =
161 g_signal_new ("progress-changed",
162 G_TYPE_FROM_CLASS (gobject_class),
164 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
166 g_cclosure_marshal_VOID__VOID,
171 modest_mail_operation_init (ModestMailOperation *obj)
173 ModestMailOperationPrivate *priv;
175 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
177 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
178 priv->id = MODEST_MAIL_OPERATION_ID_UNKNOWN;
185 modest_mail_operation_finalize (GObject *obj)
187 ModestMailOperationPrivate *priv;
189 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
192 g_error_free (priv->error);
196 G_OBJECT_CLASS(parent_class)->finalize (obj);
200 modest_mail_operation_new (ModestMailOperationId id)
202 ModestMailOperation *obj;
203 ModestMailOperationPrivate *priv;
206 obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
207 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
216 ModestMailOperationId
217 modest_mail_operation_get_id (ModestMailOperation *self)
219 ModestMailOperationPrivate *priv;
221 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
227 modest_mail_operation_send_mail (ModestMailOperation *self,
228 TnyTransportAccount *transport_account,
231 TnySendQueue *send_queue;
233 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
234 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
235 g_return_if_fail (TNY_IS_MSG (msg));
237 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
238 if (!TNY_IS_SEND_QUEUE(send_queue))
239 g_printerr ("modest: could not find send queue for account\n");
242 tny_send_queue_add (send_queue, msg, &err);
244 g_printerr ("modest: error adding msg to send queue: %s\n",
248 g_message ("modest: message added to send queue");
251 /* Notify the queue */
252 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
256 modest_mail_operation_send_new_mail (ModestMailOperation *self,
257 TnyTransportAccount *transport_account,
258 const gchar *from, const gchar *to,
259 const gchar *cc, const gchar *bcc,
260 const gchar *subject, const gchar *plain_body,
261 const gchar *html_body,
262 const GList *attachments_list,
263 TnyHeaderFlags priority_flags)
266 ModestMailOperationPrivate *priv = NULL;
267 /* GList *node = NULL; */
269 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
270 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
272 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
274 /* Check parametters */
276 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
277 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
278 _("Error trying to send a mail. You need to set at least one recipient"));
282 if (html_body == NULL) {
283 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
285 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
288 g_printerr ("modest: failed to create a new msg\n");
292 /* TODO: add priority handling. It's received in the priority_flags operator, and
293 it should have effect in the sending operation */
295 /* Call mail operation */
296 modest_mail_operation_send_mail (self, transport_account, new_msg);
299 g_object_unref (G_OBJECT (new_msg));
303 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
304 TnyTransportAccount *transport_account,
305 const gchar *from, const gchar *to,
306 const gchar *cc, const gchar *bcc,
307 const gchar *subject, const gchar *plain_body,
308 const gchar *html_body,
309 const GList *attachments_list,
310 TnyHeaderFlags priority_flags)
313 TnyFolder *folder = NULL;
314 ModestMailOperationPrivate *priv = NULL;
317 /* GList *node = NULL; */
319 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
320 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
322 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
324 if (html_body == NULL) {
325 msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
327 msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
330 g_printerr ("modest: failed to create a new msg\n");
334 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account), TNY_FOLDER_TYPE_DRAFTS);
336 g_printerr ("modest: failed to find Drafts folder\n");
340 tny_folder_add_msg (folder, msg, &err);
342 g_printerr ("modest: error adding msg to Drafts folder: %s",
348 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
353 g_object_unref (G_OBJECT(msg));
355 g_object_unref (G_OBJECT(folder));
359 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
362 TnyList *folders = tny_simple_list_new ();
364 tny_folder_store_get_folders (store, folders, query, NULL);
365 iter = tny_list_create_iterator (folders);
367 while (!tny_iterator_is_done (iter)) {
369 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
371 tny_list_prepend (all_folders, G_OBJECT (folder));
373 recurse_folders (folder, query, all_folders);
375 g_object_unref (G_OBJECT (folder));
377 tny_iterator_next (iter);
379 g_object_unref (G_OBJECT (iter));
380 g_object_unref (G_OBJECT (folders));
384 update_folders_status_cb (GObject *obj,
388 ModestMailOperation *self;
389 ModestMailOperationPrivate *priv;
391 g_return_if_fail (status != NULL);
392 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
394 self = MODEST_MAIL_OPERATION (user_data);
395 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
397 priv->done = status->position;
398 priv->total = status->of_total;
400 if (priv->done == 1 && priv->total == 100)
403 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
407 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
409 ModestMailOperation *self;
410 ModestMailOperationPrivate *priv;
412 TnyList *all_folders;
414 self = MODEST_MAIL_OPERATION (user_data);
415 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
417 g_message (__FUNCTION__);
420 priv->error = g_error_copy (*err);
421 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
425 /* Get all the folders We can do it synchronously because
426 we're already running in a different thread than the UI */
427 all_folders = tny_list_copy (list);
428 iter = tny_list_create_iterator (all_folders);
429 while (!tny_iterator_is_done (iter)) {
430 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
432 recurse_folders (folder, NULL, all_folders);
433 tny_iterator_next (iter);
435 g_object_unref (G_OBJECT (iter));
437 /* Refresh folders */
438 iter = tny_list_create_iterator (all_folders);
439 priv->total = tny_list_get_length (all_folders);
441 while (!tny_iterator_is_done (iter) && !priv->error) {
443 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
445 /* Refresh the folder */
446 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
449 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
451 /* Update status and notify */
453 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
456 g_object_unref (G_OBJECT (folder));
458 tny_iterator_next (iter);
461 g_object_unref (G_OBJECT (iter));
463 g_object_unref (G_OBJECT (list));
465 /* Check if the operation was a success */
466 if (priv->done == priv->total && !priv->error)
467 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
470 g_object_unref (G_OBJECT (folder_store));
472 /* Notify the queue */
473 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
477 modest_mail_operation_update_account (ModestMailOperation *self,
478 TnyStoreAccount *store_account)
480 ModestMailOperationPrivate *priv;
483 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
484 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
486 /* Pick async call reference */
487 g_object_ref (store_account);
489 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
493 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
495 /* Get subscribed folders & refresh them */
496 folders = TNY_LIST (tny_simple_list_new ());
498 g_message ("tny_folder_store_get_folders_async");
499 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
500 folders, update_folders_cb, NULL, update_folders_status_cb, self);
505 ModestMailOperationStatus
506 modest_mail_operation_get_status (ModestMailOperation *self)
508 ModestMailOperationPrivate *priv;
510 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
511 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
512 MODEST_MAIL_OPERATION_STATUS_INVALID);
514 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
519 modest_mail_operation_get_error (ModestMailOperation *self)
521 ModestMailOperationPrivate *priv;
523 g_return_val_if_fail (self, NULL);
524 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
526 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
531 modest_mail_operation_cancel (ModestMailOperation *self)
533 ModestMailOperationPrivate *priv;
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 /* TODO: Tinymail does not support cancel operation */
545 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
547 /* Notify the queue */
548 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
554 modest_mail_operation_get_task_done (ModestMailOperation *self)
556 ModestMailOperationPrivate *priv;
558 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
560 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
565 modest_mail_operation_get_task_total (ModestMailOperation *self)
567 ModestMailOperationPrivate *priv;
569 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
571 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
576 modest_mail_operation_is_finished (ModestMailOperation *self)
578 ModestMailOperationPrivate *priv;
579 gboolean retval = FALSE;
581 if (!MODEST_IS_MAIL_OPERATION (self)) {
582 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
586 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
588 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
589 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
590 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
591 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
600 /* ******************************************************************* */
601 /* ************************** STORE ACTIONS ************************* */
602 /* ******************************************************************* */
606 modest_mail_operation_create_folder (ModestMailOperation *self,
607 TnyFolderStore *parent,
610 ModestTnyFolderRules rules;
611 ModestMailOperationPrivate *priv;
612 TnyFolder *new_folder = NULL;
613 gboolean can_create = FALSE;
615 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
616 g_return_val_if_fail (name, NULL);
618 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
621 if (!TNY_IS_FOLDER (parent)) {
622 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
623 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
624 _("mail_in_ui_folder_create_error"));
626 /* Check folder rules */
627 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
628 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
629 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
630 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
631 _("mail_in_ui_folder_create_error"));
637 /* Create the folder */
638 new_folder = tny_folder_store_create_folder (parent, name, &(priv->error));
639 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
642 /* Notify the queue */
643 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
649 modest_mail_operation_remove_folder (ModestMailOperation *self,
651 gboolean remove_to_trash)
654 ModestMailOperationPrivate *priv;
655 ModestTnyFolderRules rules;
657 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
658 g_return_if_fail (TNY_IS_FOLDER (folder));
660 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
662 /* Check folder rules */
663 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
664 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
665 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
666 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
667 _("mail_in_ui_folder_delete_error"));
671 /* Get the account */
672 account = tny_folder_get_account (folder);
674 /* Delete folder or move to trash */
675 if (remove_to_trash) {
676 TnyFolder *trash_folder, *new_folder;
677 trash_folder = modest_tny_account_get_special_folder (account,
678 TNY_FOLDER_TYPE_TRASH);
679 /* TODO: error_handling */
680 new_folder = modest_mail_operation_xfer_folder (self, folder,
681 TNY_FOLDER_STORE (trash_folder), TRUE);
682 g_object_unref (G_OBJECT (new_folder));
684 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
686 tny_folder_store_remove_folder (parent, folder, &(priv->error));
687 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
690 g_object_unref (G_OBJECT (parent));
692 g_object_unref (G_OBJECT (account));
695 /* Notify the queue */
696 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
700 modest_mail_operation_rename_folder (ModestMailOperation *self,
704 ModestMailOperationPrivate *priv;
705 ModestTnyFolderRules rules;
707 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
708 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
709 g_return_if_fail (name);
711 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
713 /* Check folder rules */
714 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
715 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
716 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
717 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
718 _("FIXME: unable to rename"));
720 /* Rename. Camel handles folder subscription/unsubscription */
721 tny_folder_set_name (folder, name, &(priv->error));
722 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
725 /* Notify the queue */
726 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
730 modest_mail_operation_xfer_folder (ModestMailOperation *self,
732 TnyFolderStore *parent,
733 gboolean delete_original)
735 ModestMailOperationPrivate *priv;
736 TnyFolder *new_folder = NULL;
737 ModestTnyFolderRules rules;
739 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
740 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
741 g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
743 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
745 /* The moveable restriction is applied also to copy operation */
746 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
747 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
748 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
749 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
750 _("FIXME: unable to rename"));
752 /* Move/Copy folder */
753 new_folder = tny_folder_copy (folder,
755 tny_folder_get_name (folder),
760 /* Notify the queue */
761 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
767 /* ******************************************************************* */
768 /* ************************** MSG ACTIONS ************************* */
769 /* ******************************************************************* */
771 void modest_mail_operation_process_msg (ModestMailOperation *self,
774 TnyGetMsgCallback user_callback,
778 ModestMailOperationPrivate *priv;
780 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
781 g_return_if_fail (TNY_IS_HEADER (header));
783 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
784 folder = tny_header_get_folder (header);
786 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
787 priv->total = num_ops;
789 /* Get message from folder */
791 /* The callback will call it per each header */
792 tny_folder_get_msg_async (folder, header, user_callback, update_process_msg_status_cb, user_data);
793 g_object_unref (G_OBJECT (folder));
795 /* Set status failed and set an error */
796 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
797 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
798 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
799 _("Error trying to get a message. No folder found for header"));
804 update_process_msg_status_cb (GObject *obj,
808 ModestMailOperation *self;
809 ModestMailOperationPrivate *priv;
811 g_return_if_fail (status != NULL);
812 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
814 self = MODEST_MAIL_OPERATION (user_data);
815 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
817 priv->done += status->position;
819 if (priv->done == 1 && priv->total == 100)
822 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
828 modest_mail_operation_remove_msg (ModestMailOperation *self,
830 gboolean remove_to_trash)
833 ModestMailOperationPrivate *priv;
835 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
836 g_return_if_fail (TNY_IS_HEADER (header));
838 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
839 folder = tny_header_get_folder (header);
841 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
843 /* Delete or move to trash */
844 if (remove_to_trash) {
845 TnyFolder *trash_folder;
846 TnyStoreAccount *store_account;
848 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
849 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
850 TNY_FOLDER_TYPE_TRASH);
855 headers = tny_simple_list_new ();
856 tny_list_append (headers, G_OBJECT (header));
857 g_object_unref (header);
860 modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
861 g_object_unref (headers);
862 /* g_object_unref (trash_folder); */
864 ModestMailOperationPrivate *priv;
866 /* Set status failed and set an error */
867 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
868 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
869 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
870 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
871 _("Error trying to delete a message. Trash folder not found"));
874 g_object_unref (G_OBJECT (store_account));
876 tny_folder_remove_msg (folder, header, &(priv->error));
878 tny_folder_sync(folder, TRUE, &(priv->error));
883 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
885 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
888 g_object_unref (G_OBJECT (folder));
890 /* Notify the queue */
891 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
895 transfer_msgs_status_cb (GObject *obj,
903 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
905 XFerMsgAsyncHelper *helper;
906 ModestMailOperation *self;
907 ModestMailOperationPrivate *priv;
909 helper = (XFerMsgAsyncHelper *) user_data;
910 self = helper->mail_op;
912 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
915 priv->error = g_error_copy (*err);
917 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
920 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
924 g_object_unref (helper->headers);
925 g_object_unref (helper->dest_folder);
926 g_object_unref (folder);
929 /* Notify the queue */
930 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
934 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
937 gboolean delete_original)
939 ModestMailOperationPrivate *priv;
941 TnyFolder *src_folder;
942 XFerMsgAsyncHelper *helper;
945 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
946 g_return_if_fail (TNY_IS_LIST (headers));
947 g_return_if_fail (TNY_IS_FOLDER (folder));
949 /* Pick references for async calls */
950 g_object_ref (folder);
952 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
955 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
957 /* Create the helper */
958 helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
959 helper->mail_op = self;
960 helper->dest_folder = folder;
961 helper->headers = headers;
963 /* Get source folder */
964 iter = tny_list_create_iterator (headers);
965 header = TNY_HEADER (tny_iterator_get_current (iter));
966 src_folder = tny_header_get_folder (header);
967 g_object_unref (header);
968 g_object_unref (iter);
970 /* Transfer messages */
971 tny_folder_transfer_msgs_async (src_folder,
976 transfer_msgs_status_cb,
982 on_refresh_folder (TnyFolder *folder,
987 ModestMailOperation *self;
988 ModestMailOperationPrivate *priv;
990 self = MODEST_MAIL_OPERATION (user_data);
991 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
994 priv->error = g_error_copy (*error);
995 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1000 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
1001 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1002 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1003 _("Error trying to refresh the contents of %s"),
1004 tny_folder_get_name (folder));
1008 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1012 g_object_unref (folder);
1014 /* Notify the queue */
1015 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
1019 on_refresh_folder_status_update (GObject *obj,
1023 ModestMailOperation *self;
1024 ModestMailOperationPrivate *priv;
1026 g_return_if_fail (status != NULL);
1027 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
1029 /* Temporary FIX: useful when tinymail send us status
1030 information *after* calling the function callback */
1031 if (!MODEST_IS_MAIL_OPERATION (user_data))
1034 self = MODEST_MAIL_OPERATION (user_data);
1035 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1037 priv->done = status->position;
1038 priv->total = status->of_total;
1040 if (priv->done == 1 && priv->total == 100)
1043 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
1047 modest_mail_operation_refresh_folder (ModestMailOperation *self,
1050 ModestMailOperationPrivate *priv;
1052 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1054 /* Pick a reference */
1055 g_object_ref (folder);
1057 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1059 /* Refresh the folder. TODO: tinymail could issue a status
1060 updates before the callback call then this could happen. We
1061 must review the design */
1062 tny_folder_refresh_async (folder,
1064 on_refresh_folder_status_update,
1069 _modest_mail_operation_notify_end (ModestMailOperation *self)
1071 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);