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,
68 enum _ModestMailOperationSignals
70 PROGRESS_CHANGED_SIGNAL,
75 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
76 struct _ModestMailOperationPrivate {
79 ModestMailOperationStatus status;
80 ModestMailOperationId id;
84 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
85 MODEST_TYPE_MAIL_OPERATION, \
86 ModestMailOperationPrivate))
88 #define CHECK_EXCEPTION(priv, new_status) if (priv->error) {\
89 priv->status = new_status;\
92 typedef struct _RefreshFolderAsyncHelper
94 ModestMailOperation *mail_op;
99 } RefreshFolderAsyncHelper;
101 typedef struct _XFerMsgAsyncHelper
103 ModestMailOperation *mail_op;
105 TnyFolder *dest_folder;
107 } XFerMsgAsyncHelper;
111 static GObjectClass *parent_class = NULL;
113 static guint signals[NUM_SIGNALS] = {0};
116 modest_mail_operation_get_type (void)
118 static GType my_type = 0;
120 static const GTypeInfo my_info = {
121 sizeof(ModestMailOperationClass),
122 NULL, /* base init */
123 NULL, /* base finalize */
124 (GClassInitFunc) modest_mail_operation_class_init,
125 NULL, /* class finalize */
126 NULL, /* class data */
127 sizeof(ModestMailOperation),
129 (GInstanceInitFunc) modest_mail_operation_init,
132 my_type = g_type_register_static (G_TYPE_OBJECT,
133 "ModestMailOperation",
140 modest_mail_operation_class_init (ModestMailOperationClass *klass)
142 GObjectClass *gobject_class;
143 gobject_class = (GObjectClass*) klass;
145 parent_class = g_type_class_peek_parent (klass);
146 gobject_class->finalize = modest_mail_operation_finalize;
148 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
151 * ModestMailOperation::progress-changed
152 * @self: the #MailOperation that emits the signal
153 * @user_data: user data set when the signal handler was connected
155 * Emitted when the progress of a mail operation changes
157 signals[PROGRESS_CHANGED_SIGNAL] =
158 g_signal_new ("progress-changed",
159 G_TYPE_FROM_CLASS (gobject_class),
161 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
163 g_cclosure_marshal_VOID__VOID,
168 modest_mail_operation_init (ModestMailOperation *obj)
170 ModestMailOperationPrivate *priv;
172 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
174 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
175 priv->id = MODEST_MAIL_OPERATION_ID_UNKNOWN;
182 modest_mail_operation_finalize (GObject *obj)
184 ModestMailOperationPrivate *priv;
186 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
189 g_error_free (priv->error);
193 G_OBJECT_CLASS(parent_class)->finalize (obj);
197 modest_mail_operation_new (ModestMailOperationId id)
199 ModestMailOperation *obj;
200 ModestMailOperationPrivate *priv;
203 obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
204 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
213 ModestMailOperationId
214 modest_mail_operation_get_id (ModestMailOperation *self)
216 ModestMailOperationPrivate *priv;
218 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
224 modest_mail_operation_send_mail (ModestMailOperation *self,
225 TnyTransportAccount *transport_account,
228 TnySendQueue *send_queue;
230 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
231 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
232 g_return_if_fail (TNY_IS_MSG (msg));
234 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
235 if (!TNY_IS_SEND_QUEUE(send_queue))
236 g_printerr ("modest: could not find send queue for account\n");
239 tny_send_queue_add (send_queue, msg, &err);
241 g_printerr ("modest: error adding msg to send queue: %s\n",
245 g_message ("modest: message added to send queue");
248 /* Notify the queue */
249 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
253 modest_mail_operation_send_new_mail (ModestMailOperation *self,
254 TnyTransportAccount *transport_account,
255 const gchar *from, const gchar *to,
256 const gchar *cc, const gchar *bcc,
257 const gchar *subject, const gchar *plain_body,
258 const gchar *html_body,
259 const GList *attachments_list,
260 TnyHeaderFlags priority_flags)
263 ModestMailOperationPrivate *priv = NULL;
264 /* GList *node = NULL; */
266 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
267 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
269 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
271 /* Check parametters */
273 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
274 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
275 _("Error trying to send a mail. You need to set at least one recipient"));
279 if (html_body == NULL) {
280 new_msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
282 new_msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
285 g_printerr ("modest: failed to create a new msg\n");
289 /* TODO: add priority handling. It's received in the priority_flags operator, and
290 it should have effect in the sending operation */
292 /* Call mail operation */
293 modest_mail_operation_send_mail (self, transport_account, new_msg);
296 g_object_unref (G_OBJECT (new_msg));
300 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
301 TnyTransportAccount *transport_account,
302 const gchar *from, const gchar *to,
303 const gchar *cc, const gchar *bcc,
304 const gchar *subject, const gchar *plain_body,
305 const gchar *html_body,
306 const GList *attachments_list,
307 TnyHeaderFlags priority_flags)
310 TnyFolder *folder = NULL;
311 ModestMailOperationPrivate *priv = NULL;
314 /* GList *node = NULL; */
316 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
317 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
319 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
321 if (html_body == NULL) {
322 msg = modest_tny_msg_new (to, from, cc, bcc, subject, plain_body, (GSList *) attachments_list); /* FIXME: attachments */
324 msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
327 g_printerr ("modest: failed to create a new msg\n");
331 folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account), TNY_FOLDER_TYPE_DRAFTS);
333 g_printerr ("modest: failed to find Drafts folder\n");
337 tny_folder_add_msg (folder, msg, &err);
339 g_printerr ("modest: error adding msg to Drafts folder: %s",
345 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
350 g_object_unref (G_OBJECT(msg));
352 g_object_unref (G_OBJECT(folder));
356 recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
359 TnyList *folders = tny_simple_list_new ();
361 tny_folder_store_get_folders (store, folders, query, NULL);
362 iter = tny_list_create_iterator (folders);
364 while (!tny_iterator_is_done (iter)) {
366 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
368 tny_list_prepend (all_folders, G_OBJECT (folder));
370 recurse_folders (folder, query, all_folders);
372 g_object_unref (G_OBJECT (folder));
374 tny_iterator_next (iter);
376 g_object_unref (G_OBJECT (iter));
377 g_object_unref (G_OBJECT (folders));
381 update_folders_status_cb (GObject *obj,
388 update_folders_cb (TnyFolderStore *folder_store, TnyList *list, GError **err, gpointer user_data)
390 ModestMailOperation *self;
391 ModestMailOperationPrivate *priv;
393 TnyList *all_folders;
395 self = MODEST_MAIL_OPERATION (user_data);
396 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
398 g_message (__FUNCTION__);
401 priv->error = g_error_copy (*err);
402 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
406 /* Get all the folders We can do it synchronously because
407 we're already running in a different thread than the UI */
408 all_folders = tny_list_copy (list);
409 iter = tny_list_create_iterator (all_folders);
410 while (!tny_iterator_is_done (iter)) {
411 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
413 recurse_folders (folder, NULL, all_folders);
414 tny_iterator_next (iter);
416 g_object_unref (G_OBJECT (iter));
418 /* Refresh folders */
419 iter = tny_list_create_iterator (all_folders);
420 priv->total = tny_list_get_length (all_folders);
422 while (!tny_iterator_is_done (iter) && !priv->error) {
424 TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
426 /* Refresh the folder */
427 tny_folder_refresh (TNY_FOLDER (folder), &(priv->error));
430 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
432 /* Update status and notify */
434 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
437 g_object_unref (G_OBJECT (folder));
439 tny_iterator_next (iter);
442 g_object_unref (G_OBJECT (iter));
444 g_object_unref (G_OBJECT (list));
446 /* Check if the operation was a success */
447 if (priv->done == priv->total && !priv->error)
448 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
451 g_object_unref (G_OBJECT (folder_store));
453 /* Notify the queue */
454 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
458 modest_mail_operation_update_account (ModestMailOperation *self,
459 TnyStoreAccount *store_account)
461 ModestMailOperationPrivate *priv;
464 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), FALSE);
465 g_return_val_if_fail (TNY_IS_STORE_ACCOUNT(store_account), FALSE);
467 /* Pick async call reference */
468 g_object_ref (store_account);
470 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
474 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
476 /* Get subscribed folders & refresh them */
477 folders = TNY_LIST (tny_simple_list_new ());
479 g_message ("tny_folder_store_get_folders_async");
480 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
481 folders, update_folders_cb, NULL, update_folders_status_cb, self);
486 ModestMailOperationStatus
487 modest_mail_operation_get_status (ModestMailOperation *self)
489 ModestMailOperationPrivate *priv;
491 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
492 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
493 MODEST_MAIL_OPERATION_STATUS_INVALID);
495 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
500 modest_mail_operation_get_error (ModestMailOperation *self)
502 ModestMailOperationPrivate *priv;
504 g_return_val_if_fail (self, NULL);
505 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
507 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
512 modest_mail_operation_cancel (ModestMailOperation *self)
514 ModestMailOperationPrivate *priv;
516 if (!MODEST_IS_MAIL_OPERATION (self)) {
517 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
521 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
523 /* TODO: Tinymail does not support cancel operation */
526 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
528 /* Notify the queue */
529 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
535 modest_mail_operation_get_task_done (ModestMailOperation *self)
537 ModestMailOperationPrivate *priv;
539 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
541 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
546 modest_mail_operation_get_task_total (ModestMailOperation *self)
548 ModestMailOperationPrivate *priv;
550 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), 0);
552 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
557 modest_mail_operation_is_finished (ModestMailOperation *self)
559 ModestMailOperationPrivate *priv;
560 gboolean retval = FALSE;
562 if (!MODEST_IS_MAIL_OPERATION (self)) {
563 g_warning ("%s: invalid parametter", G_GNUC_FUNCTION);
567 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
569 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
570 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
571 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
572 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
581 /* ******************************************************************* */
582 /* ************************** STORE ACTIONS ************************* */
583 /* ******************************************************************* */
587 modest_mail_operation_create_folder (ModestMailOperation *self,
588 TnyFolderStore *parent,
591 ModestTnyFolderRules rules;
592 ModestMailOperationPrivate *priv;
593 TnyFolder *new_folder = NULL;
594 gboolean can_create = FALSE;
596 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
597 g_return_val_if_fail (name, NULL);
599 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
602 if (!TNY_IS_FOLDER (parent)) {
603 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
604 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
605 _("mail_in_ui_folder_create_error"));
607 /* Check folder rules */
608 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
609 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)
610 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
611 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
612 _("mail_in_ui_folder_create_error"));
618 /* Create the folder */
619 new_folder = tny_folder_store_create_folder (parent, 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);
630 modest_mail_operation_remove_folder (ModestMailOperation *self,
632 gboolean remove_to_trash)
635 ModestMailOperationPrivate *priv;
636 ModestTnyFolderRules rules;
638 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
639 g_return_if_fail (TNY_IS_FOLDER (folder));
641 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
643 /* Check folder rules */
644 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
645 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
646 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
647 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
648 _("mail_in_ui_folder_delete_error"));
652 /* Get the account */
653 account = tny_folder_get_account (folder);
655 /* Delete folder or move to trash */
656 if (remove_to_trash) {
657 TnyFolder *trash_folder, *new_folder;
658 trash_folder = modest_tny_account_get_special_folder (account,
659 TNY_FOLDER_TYPE_TRASH);
660 /* TODO: error_handling */
661 new_folder = modest_mail_operation_xfer_folder (self, folder,
662 TNY_FOLDER_STORE (trash_folder), TRUE);
663 g_object_unref (G_OBJECT (new_folder));
665 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
667 tny_folder_store_remove_folder (parent, folder, &(priv->error));
668 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
671 g_object_unref (G_OBJECT (parent));
673 g_object_unref (G_OBJECT (account));
676 /* Notify the queue */
677 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
681 modest_mail_operation_rename_folder (ModestMailOperation *self,
685 ModestMailOperationPrivate *priv;
686 ModestTnyFolderRules rules;
688 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
689 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
690 g_return_if_fail (name);
692 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
694 /* Check folder rules */
695 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
696 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
697 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
698 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
699 _("FIXME: unable to rename"));
701 /* Rename. Camel handles folder subscription/unsubscription */
702 tny_folder_set_name (folder, name, &(priv->error));
703 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
706 /* Notify the queue */
707 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
711 modest_mail_operation_xfer_folder (ModestMailOperation *self,
713 TnyFolderStore *parent,
714 gboolean delete_original)
716 ModestMailOperationPrivate *priv;
717 TnyFolder *new_folder = NULL;
718 ModestTnyFolderRules rules;
720 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
721 g_return_val_if_fail (TNY_IS_FOLDER_STORE (parent), NULL);
722 g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
724 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
726 /* The moveable restriction is applied also to copy operation */
727 rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
728 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE) {
729 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
730 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
731 _("FIXME: unable to rename"));
733 /* Move/Copy folder */
734 new_folder = tny_folder_copy (folder,
736 tny_folder_get_name (folder),
741 /* Notify the queue */
742 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
748 /* ******************************************************************* */
749 /* ************************** MSG ACTIONS ************************* */
750 /* ******************************************************************* */
752 void modest_mail_operation_process_msg (ModestMailOperation *self,
754 TnyGetMsgCallback user_callback,
758 ModestMailOperationPrivate *priv;
760 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
761 g_return_if_fail (TNY_IS_HEADER (header));
763 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
764 folder = tny_header_get_folder (header);
766 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
768 /* Get message from folder */
770 /* The callback will call it per each header */
771 tny_folder_get_msg_async (folder, header, user_callback, NULL, user_data);
772 g_object_unref (G_OBJECT (folder));
774 /* Set status failed and set an error */
775 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
776 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
777 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
778 _("Error trying to get a message. No folder found for header"));
785 modest_mail_operation_remove_msg (ModestMailOperation *self,
787 gboolean remove_to_trash)
790 ModestMailOperationPrivate *priv;
792 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
793 g_return_if_fail (TNY_IS_HEADER (header));
795 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
796 folder = tny_header_get_folder (header);
798 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
800 /* Delete or move to trash */
801 if (remove_to_trash) {
802 TnyFolder *trash_folder;
803 TnyStoreAccount *store_account;
805 store_account = TNY_STORE_ACCOUNT (tny_folder_get_account (folder));
806 trash_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT(store_account),
807 TNY_FOLDER_TYPE_TRASH);
812 headers = tny_simple_list_new ();
813 tny_list_append (headers, G_OBJECT (header));
814 g_object_unref (header);
817 modest_mail_operation_xfer_msgs (self, headers, trash_folder, TRUE);
818 g_object_unref (headers);
819 /* g_object_unref (trash_folder); */
821 ModestMailOperationPrivate *priv;
823 /* Set status failed and set an error */
824 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
825 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
826 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
827 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
828 _("Error trying to delete a message. Trash folder not found"));
831 g_object_unref (G_OBJECT (store_account));
833 tny_folder_remove_msg (folder, header, &(priv->error));
835 tny_folder_sync(folder, TRUE, &(priv->error));
840 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
842 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
845 g_object_unref (G_OBJECT (folder));
847 /* Notify the queue */
848 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
852 transfer_msgs_status_cb (GObject *obj,
860 transfer_msgs_cb (TnyFolder *folder, GError **err, gpointer user_data)
862 XFerMsgAsyncHelper *helper;
863 ModestMailOperation *self;
864 ModestMailOperationPrivate *priv;
866 helper = (XFerMsgAsyncHelper *) user_data;
867 self = helper->mail_op;
869 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
872 priv->error = g_error_copy (*err);
874 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
877 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
881 g_object_unref (helper->headers);
882 g_object_unref (helper->dest_folder);
883 g_object_unref (folder);
886 /* Notify the queue */
887 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
891 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
894 gboolean delete_original)
896 ModestMailOperationPrivate *priv;
898 TnyFolder *src_folder;
899 XFerMsgAsyncHelper *helper;
902 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
903 g_return_if_fail (TNY_IS_LIST (headers));
904 g_return_if_fail (TNY_IS_FOLDER (folder));
906 /* Pick references for async calls */
907 g_object_ref (folder);
909 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
912 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
914 /* Create the helper */
915 helper = g_malloc0 (sizeof (XFerMsgAsyncHelper));
916 helper->mail_op = self;
917 helper->dest_folder = folder;
918 helper->headers = headers;
920 /* Get source folder */
921 iter = tny_list_create_iterator (headers);
922 header = TNY_HEADER (tny_iterator_get_current (iter));
923 src_folder = tny_header_get_folder (header);
924 g_object_unref (header);
925 g_object_unref (iter);
927 /* Transfer messages */
928 tny_folder_transfer_msgs_async (src_folder,
933 transfer_msgs_status_cb,
939 on_refresh_folder (TnyFolder *folder,
944 ModestMailOperation *self;
945 ModestMailOperationPrivate *priv;
947 self = MODEST_MAIL_OPERATION (user_data);
948 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
951 priv->error = g_error_copy (*error);
952 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
957 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
958 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
959 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
960 _("Error trying to refresh the contents of %s"),
961 tny_folder_get_name (folder));
965 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
969 g_object_unref (folder);
971 /* Notify the queue */
972 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (), self);
976 on_refresh_folder_status_update (GObject *obj,
980 ModestMailOperation *self;
981 ModestMailOperationPrivate *priv;
983 g_return_if_fail (status != NULL);
984 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
986 self = MODEST_MAIL_OPERATION (user_data);
987 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
989 priv->done = status->position;
990 priv->total = status->of_total;
992 if (priv->done == 1 && priv->total == 100)
995 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);
999 modest_mail_operation_refresh_folder (ModestMailOperation *self,
1002 ModestMailOperationPrivate *priv;
1004 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1006 /* Pick a reference */
1007 g_object_ref (folder);
1009 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1011 /* Refresh the folder. TODO: tinymail could issue a status
1012 updates before the callback call then this could happen. We
1013 must review the design */
1014 tny_folder_refresh_async (folder,
1016 on_refresh_folder_status_update,
1022 _modest_mail_operation_notify_end (ModestMailOperation *self)
1024 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, NULL);