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.
32 #include <tny-mime-part.h>
33 #include <tny-store-account.h>
34 #include <tny-folder-store.h>
35 #include <tny-folder-store-query.h>
36 #include <tny-camel-stream.h>
37 #include <tny-camel-pop-store-account.h>
38 #include <tny-camel-pop-folder.h>
39 #include <tny-camel-imap-folder.h>
40 #include <tny-camel-mem-stream.h>
41 #include <tny-simple-list.h>
42 #include <tny-send-queue.h>
43 #include <tny-status.h>
44 #include <tny-folder-observer.h>
45 #include <camel/camel-stream-mem.h>
46 #include <glib/gi18n.h>
47 #include "modest-platform.h"
48 #include "modest-account-mgr-helpers.h"
49 #include <modest-tny-account.h>
50 #include <modest-tny-send-queue.h>
51 #include <modest-runtime.h>
52 #include "modest-text-utils.h"
53 #include "modest-tny-msg.h"
54 #include "modest-tny-folder.h"
55 #include "modest-tny-account-store.h"
56 #include "modest-tny-platform-factory.h"
57 #include "modest-marshal.h"
58 #include "modest-error.h"
59 #include "modest-mail-operation.h"
60 #include <modest-count-stream.h>
61 #include <libgnomevfs/gnome-vfs.h>
62 #include "modest-utils.h"
67 * Remove all these #ifdef stuff when the tinymail's idle calls become
70 #define TINYMAIL_IDLES_NOT_LOCKED_YET 1
72 /* 'private'/'protected' functions */
73 static void modest_mail_operation_class_init (ModestMailOperationClass *klass);
74 static void modest_mail_operation_init (ModestMailOperation *obj);
75 static void modest_mail_operation_finalize (GObject *obj);
77 static void get_msg_async_cb (TnyFolder *folder,
83 static void get_msg_status_cb (GObject *obj,
87 static void modest_mail_operation_notify_start (ModestMailOperation *self);
88 static void modest_mail_operation_notify_end (ModestMailOperation *self);
90 static void notify_progress_of_multiple_messages (ModestMailOperation *self,
92 gint *last_total_bytes,
93 gint *sum_total_bytes,
95 gboolean increment_done);
97 static guint compute_message_list_size (TnyList *headers);
99 static guint compute_message_array_size (GPtrArray *headers);
101 static int compare_headers_by_date (gconstpointer a,
104 enum _ModestMailOperationSignals
106 PROGRESS_CHANGED_SIGNAL,
107 OPERATION_STARTED_SIGNAL,
108 OPERATION_FINISHED_SIGNAL,
112 typedef struct _ModestMailOperationPrivate ModestMailOperationPrivate;
113 struct _ModestMailOperationPrivate {
119 ErrorCheckingUserCallback error_checking;
120 gpointer error_checking_user_data;
121 ErrorCheckingUserDataDestroyer error_checking_user_data_destroyer;
122 ModestMailOperationStatus status;
123 ModestMailOperationTypeOperation op_type;
126 #define MODEST_MAIL_OPERATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
127 MODEST_TYPE_MAIL_OPERATION, \
128 ModestMailOperationPrivate))
130 #define CHECK_EXCEPTION(priv, new_status) if (priv->error) {\
131 priv->status = new_status;\
136 GetMsgAsyncUserCallback user_callback;
138 TnyIterator *more_msgs;
140 ModestMailOperation *mail_op;
141 GDestroyNotify destroy_notify;
142 gint last_total_bytes;
143 gint sum_total_bytes;
147 typedef struct _RefreshAsyncHelper {
148 ModestMailOperation *mail_op;
149 RefreshAsyncUserCallback user_callback;
151 } RefreshAsyncHelper;
153 typedef struct _XFerMsgsAsyncHelper
155 ModestMailOperation *mail_op;
157 TnyIterator *more_msgs;
158 TnyFolder *dest_folder;
159 XferMsgsAsyncUserCallback user_callback;
162 gint last_total_bytes;
163 gint sum_total_bytes;
165 } XFerMsgsAsyncHelper;
167 typedef struct _XFerFolderAsyncHelper
169 ModestMailOperation *mail_op;
170 XferFolderAsyncUserCallback user_callback;
172 } XFerFolderAsyncHelper;
174 typedef void (*ModestMailOperationCreateMsgCallback) (ModestMailOperation *mail_op,
178 static void modest_mail_operation_create_msg (ModestMailOperation *self,
179 const gchar *from, const gchar *to,
180 const gchar *cc, const gchar *bcc,
181 const gchar *subject, const gchar *plain_body,
182 const gchar *html_body, const GList *attachments_list,
183 const GList *images_list,
184 TnyHeaderFlags priority_flags,
185 ModestMailOperationCreateMsgCallback callback,
188 static gboolean idle_notify_queue (gpointer data);
191 ModestMailOperation *mail_op;
199 GList *attachments_list;
201 TnyHeaderFlags priority_flags;
202 ModestMailOperationCreateMsgCallback callback;
208 ModestMailOperation *mail_op;
210 ModestMailOperationCreateMsgCallback callback;
215 static GObjectClass *parent_class = NULL;
217 static guint signals[NUM_SIGNALS] = {0};
220 modest_mail_operation_get_type (void)
222 static GType my_type = 0;
224 static const GTypeInfo my_info = {
225 sizeof(ModestMailOperationClass),
226 NULL, /* base init */
227 NULL, /* base finalize */
228 (GClassInitFunc) modest_mail_operation_class_init,
229 NULL, /* class finalize */
230 NULL, /* class data */
231 sizeof(ModestMailOperation),
233 (GInstanceInitFunc) modest_mail_operation_init,
236 my_type = g_type_register_static (G_TYPE_OBJECT,
237 "ModestMailOperation",
244 modest_mail_operation_class_init (ModestMailOperationClass *klass)
246 GObjectClass *gobject_class;
247 gobject_class = (GObjectClass*) klass;
249 parent_class = g_type_class_peek_parent (klass);
250 gobject_class->finalize = modest_mail_operation_finalize;
252 g_type_class_add_private (gobject_class, sizeof(ModestMailOperationPrivate));
255 * ModestMailOperation::progress-changed
256 * @self: the #MailOperation that emits the signal
257 * @user_data: user data set when the signal handler was connected
259 * Emitted when the progress of a mail operation changes
261 signals[PROGRESS_CHANGED_SIGNAL] =
262 g_signal_new ("progress-changed",
263 G_TYPE_FROM_CLASS (gobject_class),
265 G_STRUCT_OFFSET (ModestMailOperationClass, progress_changed),
267 g_cclosure_marshal_VOID__POINTER,
268 G_TYPE_NONE, 1, G_TYPE_POINTER);
272 * This signal is issued whenever a mail operation starts, and
273 * starts mean when the tinymail operation is issued. This
274 * means that it could happen that something wrong happens and
275 * the tinymail function is never called. In this situation a
276 * operation-finished will be issued without any
279 signals[OPERATION_STARTED_SIGNAL] =
280 g_signal_new ("operation-started",
281 G_TYPE_FROM_CLASS (gobject_class),
283 G_STRUCT_OFFSET (ModestMailOperationClass, operation_started),
285 g_cclosure_marshal_VOID__VOID,
290 * This signal is issued whenever a mail operation
291 * finishes. Note that this signal could be issued without any
292 * previous "operation-started" signal, because this last one
293 * is only issued when the tinymail operation is successfully
296 signals[OPERATION_FINISHED_SIGNAL] =
297 g_signal_new ("operation-finished",
298 G_TYPE_FROM_CLASS (gobject_class),
300 G_STRUCT_OFFSET (ModestMailOperationClass, operation_finished),
302 g_cclosure_marshal_VOID__VOID,
307 modest_mail_operation_init (ModestMailOperation *obj)
309 ModestMailOperationPrivate *priv;
311 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
313 priv->account = NULL;
314 priv->status = MODEST_MAIL_OPERATION_STATUS_INVALID;
315 priv->op_type = MODEST_MAIL_OPERATION_TYPE_UNKNOWN;
320 priv->error_checking = NULL;
321 priv->error_checking_user_data = NULL;
325 modest_mail_operation_finalize (GObject *obj)
327 ModestMailOperationPrivate *priv;
329 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
334 g_error_free (priv->error);
338 g_object_unref (priv->source);
342 g_object_unref (priv->account);
343 priv->account = NULL;
347 G_OBJECT_CLASS(parent_class)->finalize (obj);
351 modest_mail_operation_new (GObject *source)
353 ModestMailOperation *obj;
354 ModestMailOperationPrivate *priv;
356 obj = MODEST_MAIL_OPERATION(g_object_new(MODEST_TYPE_MAIL_OPERATION, NULL));
357 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
360 priv->source = g_object_ref(source);
366 modest_mail_operation_new_with_error_handling (GObject *source,
367 ErrorCheckingUserCallback error_handler,
369 ErrorCheckingUserDataDestroyer error_handler_destroyer)
371 ModestMailOperation *obj;
372 ModestMailOperationPrivate *priv;
374 obj = modest_mail_operation_new (source);
375 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(obj);
377 g_return_val_if_fail (error_handler != NULL, obj);
378 priv->error_checking = error_handler;
379 priv->error_checking_user_data = user_data;
380 priv->error_checking_user_data_destroyer = error_handler_destroyer;
386 modest_mail_operation_execute_error_handler (ModestMailOperation *self)
388 ModestMailOperationPrivate *priv;
390 g_return_if_fail (self && MODEST_IS_MAIL_OPERATION(self));
392 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
393 g_return_if_fail(priv->status != MODEST_MAIL_OPERATION_STATUS_SUCCESS);
395 /* Call the user callback */
396 if (priv->error_checking != NULL)
397 priv->error_checking (self, priv->error_checking_user_data);
401 ModestMailOperationTypeOperation
402 modest_mail_operation_get_type_operation (ModestMailOperation *self)
404 ModestMailOperationPrivate *priv;
406 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
407 MODEST_MAIL_OPERATION_TYPE_UNKNOWN);
409 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
411 return priv->op_type;
415 modest_mail_operation_is_mine (ModestMailOperation *self,
418 ModestMailOperationPrivate *priv;
420 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
423 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
424 if (priv->source == NULL) return FALSE;
426 return priv->source == me;
430 modest_mail_operation_get_source (ModestMailOperation *self)
432 ModestMailOperationPrivate *priv;
434 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
437 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
439 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
443 return (priv->source) ? g_object_ref (priv->source) : NULL;
446 ModestMailOperationStatus
447 modest_mail_operation_get_status (ModestMailOperation *self)
449 ModestMailOperationPrivate *priv;
451 g_return_val_if_fail (self, MODEST_MAIL_OPERATION_STATUS_INVALID);
452 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self),
453 MODEST_MAIL_OPERATION_STATUS_INVALID);
455 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
457 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
458 return MODEST_MAIL_OPERATION_STATUS_INVALID;
465 modest_mail_operation_get_error (ModestMailOperation *self)
467 ModestMailOperationPrivate *priv;
469 g_return_val_if_fail (self, NULL);
470 g_return_val_if_fail (MODEST_IS_MAIL_OPERATION (self), NULL);
472 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
475 g_warning ("BUG: %s: priv == NULL", __FUNCTION__);
483 modest_mail_operation_cancel (ModestMailOperation *self)
485 ModestMailOperationPrivate *priv;
486 gboolean canceled = FALSE;
488 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION (self), FALSE);
490 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
493 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
495 /* Cancel the mail operation */
496 g_return_val_if_fail (priv->account, FALSE);
497 tny_account_cancel (priv->account);
499 if (priv->op_type == MODEST_MAIL_OPERATION_TYPE_SEND) {
500 ModestTnySendQueue *queue;
501 queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (priv->account));
503 /* Cancel the sending of the following next messages */
504 tny_send_queue_cancel (TNY_SEND_QUEUE (queue), TNY_SEND_QUEUE_CANCEL_ACTION_SUSPEND, NULL);
511 modest_mail_operation_get_task_done (ModestMailOperation *self)
513 ModestMailOperationPrivate *priv;
515 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
518 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
523 modest_mail_operation_get_task_total (ModestMailOperation *self)
525 ModestMailOperationPrivate *priv;
527 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
530 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
535 modest_mail_operation_is_finished (ModestMailOperation *self)
537 ModestMailOperationPrivate *priv;
538 gboolean retval = FALSE;
540 g_return_val_if_fail (self && MODEST_IS_MAIL_OPERATION(self),
543 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
545 if (priv->status == MODEST_MAIL_OPERATION_STATUS_SUCCESS ||
546 priv->status == MODEST_MAIL_OPERATION_STATUS_FAILED ||
547 priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED ||
548 priv->status == MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS) {
558 * Creates an image of the current state of a mail operation, the
559 * caller must free it
561 static ModestMailOperationState *
562 modest_mail_operation_clone_state (ModestMailOperation *self)
564 ModestMailOperationState *state;
565 ModestMailOperationPrivate *priv;
567 /* FIXME: this should be fixed properly
569 * in some cases, priv was NULL, so checking here to
572 g_return_val_if_fail (self, NULL);
573 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
574 g_return_val_if_fail (priv, NULL);
579 state = g_slice_new (ModestMailOperationState);
581 state->status = priv->status;
582 state->op_type = priv->op_type;
583 state->done = priv->done;
584 state->total = priv->total;
585 state->finished = modest_mail_operation_is_finished (self);
586 state->bytes_done = 0;
587 state->bytes_total = 0;
592 /* ******************************************************************* */
593 /* ************************** SEND ACTIONS ************************* */
594 /* ******************************************************************* */
597 modest_mail_operation_send_mail (ModestMailOperation *self,
598 TnyTransportAccount *transport_account,
601 TnySendQueue *send_queue = NULL;
602 ModestMailOperationPrivate *priv;
604 g_return_if_fail (self && MODEST_IS_MAIL_OPERATION (self));
605 g_return_if_fail (transport_account && TNY_IS_TRANSPORT_ACCOUNT (transport_account));
606 g_return_if_fail (msg && TNY_IS_MSG (msg));
608 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
610 /* Get account and set it into mail_operation */
611 priv->account = g_object_ref (transport_account);
612 priv->op_type = MODEST_MAIL_OPERATION_TYPE_SEND;
616 send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account));
617 if (!TNY_IS_SEND_QUEUE(send_queue)) {
618 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
619 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
620 "modest: could not find send queue for account\n");
621 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
622 modest_mail_operation_notify_end (self);
625 /* Add the msg to the queue */
626 modest_mail_operation_notify_start (self);
628 tny_send_queue_add_async (send_queue, msg, NULL, NULL, NULL);
630 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
631 modest_mail_operation_notify_end (self);
638 idle_create_msg_cb (gpointer idle_data)
640 CreateMsgIdleInfo *info = (CreateMsgIdleInfo *) idle_data;
642 /* This is a GDK lock because we are an idle callback and
643 * info->callback can contain Gtk+ code */
645 gdk_threads_enter (); /* CHECKED */
646 info->callback (info->mail_op, info->msg, info->userdata);
648 g_object_unref (info->mail_op);
650 g_object_unref (info->msg);
651 g_slice_free (CreateMsgIdleInfo, info);
652 gdk_threads_leave (); /* CHECKED */
658 create_msg_thread (gpointer thread_data)
660 CreateMsgInfo *info = (CreateMsgInfo *) thread_data;
661 TnyMsg *new_msg = NULL;
662 ModestMailOperationPrivate *priv;
664 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mail_op);
665 if (info->html_body == NULL) {
666 new_msg = modest_tny_msg_new (info->to, info->from, info->cc,
667 info->bcc, info->subject, info->plain_body,
668 info->attachments_list);
670 new_msg = modest_tny_msg_new_html_plain (info->to, info->from, info->cc,
671 info->bcc, info->subject, info->html_body,
672 info->plain_body, info->attachments_list,
679 /* Set priority flags in message */
680 header = tny_msg_get_header (new_msg);
681 tny_header_set_flag (header, info->priority_flags);
683 /* Set attachment flags in message */
684 if (info->attachments_list != NULL)
685 tny_header_set_flag (header, TNY_HEADER_FLAG_ATTACHMENTS);
687 g_object_unref (G_OBJECT(header));
689 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
690 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
691 MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
692 "modest: failed to create a new msg\n");
700 g_free (info->plain_body);
701 g_free (info->html_body);
702 g_free (info->subject);
703 g_list_foreach (info->attachments_list, (GFunc) g_object_unref, NULL);
704 g_list_free (info->attachments_list);
705 g_list_foreach (info->images_list, (GFunc) g_object_unref, NULL);
706 g_list_free (info->images_list);
708 if (info->callback) {
709 CreateMsgIdleInfo *idle_info;
710 idle_info = g_slice_new0 (CreateMsgIdleInfo);
711 idle_info->mail_op = g_object_ref (info->mail_op);
712 idle_info->msg = (new_msg) ? g_object_ref (new_msg) : NULL;
713 idle_info->callback = info->callback;
714 idle_info->userdata = info->userdata;
715 g_idle_add (idle_create_msg_cb, idle_info);
717 g_idle_add (idle_notify_queue, g_object_ref (info->mail_op));
720 g_object_unref (info->mail_op);
721 g_slice_free (CreateMsgInfo, info);
722 if (new_msg) g_object_unref(new_msg);
728 modest_mail_operation_create_msg (ModestMailOperation *self,
729 const gchar *from, const gchar *to,
730 const gchar *cc, const gchar *bcc,
731 const gchar *subject, const gchar *plain_body,
732 const gchar *html_body,
733 const GList *attachments_list,
734 const GList *images_list,
735 TnyHeaderFlags priority_flags,
736 ModestMailOperationCreateMsgCallback callback,
739 CreateMsgInfo *info = NULL;
741 info = g_slice_new0 (CreateMsgInfo);
742 info->mail_op = g_object_ref (self);
744 info->from = g_strdup (from);
745 info->to = g_strdup (to);
746 info->cc = g_strdup (cc);
747 info->bcc = g_strdup (bcc);
748 info->subject = g_strdup (subject);
749 info->plain_body = g_strdup (plain_body);
750 info->html_body = g_strdup (html_body);
751 info->attachments_list = g_list_copy ((GList *) attachments_list);
752 g_list_foreach (info->attachments_list, (GFunc) g_object_ref, NULL);
753 info->images_list = g_list_copy ((GList *) images_list);
754 g_list_foreach (info->images_list, (GFunc) g_object_ref, NULL);
755 info->priority_flags = priority_flags;
757 info->callback = callback;
758 info->userdata = userdata;
760 g_thread_create (create_msg_thread, info, FALSE, NULL);
765 TnyTransportAccount *transport_account;
770 modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
774 ModestMailOperationPrivate *priv = NULL;
775 SendNewMailInfo *info = (SendNewMailInfo *) userdata;
776 TnyFolder *draft_folder = NULL;
777 TnyFolder *outbox_folder = NULL;
778 TnyHeader *header = NULL;
785 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
788 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
789 modest_mail_operation_notify_end (self);
793 /* Call mail operation */
794 modest_mail_operation_send_mail (self, info->transport_account, msg);
796 if (info->draft_msg != NULL) {
797 TnyFolder *folder = NULL;
798 TnyFolder *src_folder = NULL;
799 TnyFolderType folder_type;
800 TnyTransportAccount *transport_account = NULL;
802 /* To remove the old mail from its source folder, we need to get the
803 * transport account of the original draft message (the transport account
804 * might have been changed by the user) */
805 header = tny_msg_get_header (info->draft_msg);
806 transport_account = modest_tny_account_store_get_transport_account_from_outbox_header(
807 modest_runtime_get_account_store(), header);
808 if (transport_account == NULL)
809 transport_account = g_object_ref(info->transport_account);
810 draft_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account),
811 TNY_FOLDER_TYPE_DRAFTS);
812 outbox_folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account),
813 TNY_FOLDER_TYPE_OUTBOX);
814 g_object_unref(transport_account);
817 g_warning ("%s: modest_tny_account_get_special_folder(..) returned a NULL drafts folder",
821 if (!outbox_folder) {
822 g_warning ("%s: modest_tny_account_get_special_folder(..) returned a NULL outbox folder",
827 folder = tny_msg_get_folder (info->draft_msg);
828 if (folder == NULL) goto end;
829 folder_type = modest_tny_folder_guess_folder_type (folder);
831 if (folder_type == TNY_FOLDER_TYPE_INVALID)
832 g_warning ("%s: BUG: folder of type TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
834 if (folder_type == TNY_FOLDER_TYPE_OUTBOX)
835 src_folder = outbox_folder;
837 src_folder = draft_folder;
839 /* Note: This can fail (with a warning) if the message is not really already in a folder,
840 * because this function requires it to have a UID. */
841 tny_folder_remove_msg (src_folder, header, NULL);
843 tny_folder_sync (folder, TRUE, &err); /* FALSE --> don't expunge */
844 /* tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL); /\* expunge *\/ */
846 g_object_unref (folder);
851 g_object_unref (header);
855 g_object_unref (info->draft_msg);
857 g_object_unref (draft_folder);
859 g_object_unref (outbox_folder);
860 if (info->transport_account)
861 g_object_unref (info->transport_account);
862 g_slice_free (SendNewMailInfo, info);
866 modest_mail_operation_send_new_mail (ModestMailOperation *self,
867 TnyTransportAccount *transport_account,
869 const gchar *from, const gchar *to,
870 const gchar *cc, const gchar *bcc,
871 const gchar *subject, const gchar *plain_body,
872 const gchar *html_body,
873 const GList *attachments_list,
874 const GList *images_list,
875 TnyHeaderFlags priority_flags)
877 ModestMailOperationPrivate *priv = NULL;
878 SendNewMailInfo *info;
880 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
881 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
883 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
884 priv->op_type = MODEST_MAIL_OPERATION_TYPE_SEND;
885 priv->account = TNY_ACCOUNT (g_object_ref (transport_account));
886 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
888 /* Check parametters */
890 /* Set status failed and set an error */
891 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
892 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
893 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
894 _("Error trying to send a mail. You need to set at least one recipient"));
897 info = g_slice_new0 (SendNewMailInfo);
898 info->transport_account = transport_account;
899 if (transport_account)
900 g_object_ref (transport_account);
901 info->draft_msg = draft_msg;
903 g_object_ref (draft_msg);
906 modest_mail_operation_notify_start (self);
907 modest_mail_operation_create_msg (self, from, to, cc, bcc, subject, plain_body, html_body,
908 attachments_list, images_list, priority_flags,
909 modest_mail_operation_send_new_mail_cb, info);
915 TnyTransportAccount *transport_account;
917 SaveToDraftstCallback callback;
921 ModestMailOperation *mailop;
922 } SaveToDraftsAddMsgInfo;
925 modest_mail_operation_save_to_drafts_add_msg_cb(TnyFolder *self,
930 ModestMailOperationPrivate *priv = NULL;
931 SaveToDraftsAddMsgInfo *info = (SaveToDraftsAddMsgInfo *) userdata;
933 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(info->mailop);
936 g_warning ("%s: priv->error != NULL", __FUNCTION__);
937 g_error_free(priv->error);
940 priv->error = (err == NULL) ? NULL : g_error_copy(err);
942 if ((!priv->error) && (info->draft_msg != NULL)) {
943 TnyHeader *header = tny_msg_get_header (info->draft_msg);
944 TnyFolder *src_folder = tny_header_get_folder (header);
946 /* Remove the old draft */
947 tny_folder_remove_msg (src_folder, header, NULL);
949 /* Synchronize to expunge and to update the msg counts */
950 tny_folder_sync_async (info->drafts, TRUE, NULL, NULL, NULL);
951 tny_folder_sync_async (src_folder, TRUE, NULL, NULL, NULL);
953 g_object_unref (G_OBJECT(header));
954 g_object_unref (G_OBJECT(src_folder));
958 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
960 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
962 /* Call the user callback */
964 info->callback (info->mailop, info->msg, info->user_data);
966 if (info->transport_account)
967 g_object_unref (G_OBJECT(info->transport_account));
969 g_object_unref (G_OBJECT (info->draft_msg));
971 g_object_unref (G_OBJECT(info->drafts));
973 g_object_unref (G_OBJECT (info->msg));
975 modest_mail_operation_notify_end (info->mailop);
976 g_object_unref(info->mailop);
977 g_slice_free (SaveToDraftsAddMsgInfo, info);
982 TnyTransportAccount *transport_account;
984 SaveToDraftstCallback callback;
989 modest_mail_operation_save_to_drafts_cb (ModestMailOperation *self,
993 TnyFolder *drafts = NULL;
994 ModestMailOperationPrivate *priv = NULL;
995 SaveToDraftsInfo *info = (SaveToDraftsInfo *) userdata;
997 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1000 if (!(priv->error)) {
1001 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1002 MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
1003 "modest: failed to create a new msg\n");
1006 drafts = modest_tny_account_get_special_folder (TNY_ACCOUNT (info->transport_account),
1007 TNY_FOLDER_TYPE_DRAFTS);
1008 if (!drafts && !(priv->error)) {
1009 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1010 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
1011 "modest: failed to create a new msg\n");
1016 SaveToDraftsAddMsgInfo *cb_info = g_slice_new(SaveToDraftsAddMsgInfo);
1017 cb_info->transport_account = g_object_ref(info->transport_account);
1018 cb_info->draft_msg = info->draft_msg ? g_object_ref(info->draft_msg) : NULL;
1019 cb_info->callback = info->callback;
1020 cb_info->user_data = info->user_data;
1021 cb_info->drafts = g_object_ref(drafts);
1022 cb_info->msg = g_object_ref(msg);
1023 cb_info->mailop = g_object_ref(self);
1024 tny_folder_add_msg_async(drafts, msg, modest_mail_operation_save_to_drafts_add_msg_cb,
1027 /* Call the user callback */
1028 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1030 info->callback (self, msg, info->user_data);
1031 modest_mail_operation_notify_end (self);
1035 g_object_unref (G_OBJECT(drafts));
1036 if (info->draft_msg)
1037 g_object_unref (G_OBJECT (info->draft_msg));
1038 if (info->transport_account)
1039 g_object_unref (G_OBJECT(info->transport_account));
1040 g_slice_free (SaveToDraftsInfo, info);
1044 modest_mail_operation_save_to_drafts (ModestMailOperation *self,
1045 TnyTransportAccount *transport_account,
1047 const gchar *from, const gchar *to,
1048 const gchar *cc, const gchar *bcc,
1049 const gchar *subject, const gchar *plain_body,
1050 const gchar *html_body,
1051 const GList *attachments_list,
1052 const GList *images_list,
1053 TnyHeaderFlags priority_flags,
1054 SaveToDraftstCallback callback,
1057 ModestMailOperationPrivate *priv = NULL;
1058 SaveToDraftsInfo *info = NULL;
1060 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1061 g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account));
1063 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1065 /* Get account and set it into mail_operation */
1066 priv->account = g_object_ref (transport_account);
1067 priv->op_type = MODEST_MAIL_OPERATION_TYPE_INFO;
1069 info = g_slice_new0 (SaveToDraftsInfo);
1070 info->transport_account = g_object_ref (transport_account);
1071 info->draft_msg = (draft_msg) ? g_object_ref (draft_msg) : NULL;
1072 info->callback = callback;
1073 info->user_data = user_data;
1075 modest_mail_operation_notify_start (self);
1076 modest_mail_operation_create_msg (self, from, to, cc, bcc, subject, plain_body, html_body,
1077 attachments_list, images_list, priority_flags,
1078 modest_mail_operation_save_to_drafts_cb, info);
1083 ModestMailOperation *mail_op;
1084 TnyMimePart *mime_part;
1086 GetMimePartSizeCallback callback;
1088 } GetMimePartSizeInfo;
1090 /***** I N T E R N A L F O L D E R O B S E R V E R *****/
1091 /* We use this folder observer to track the headers that have been
1092 * added to a folder */
1095 TnyList *new_headers;
1096 } InternalFolderObserver;
1099 GObjectClass parent;
1100 } InternalFolderObserverClass;
1102 static void tny_folder_observer_init (TnyFolderObserverIface *idace);
1104 G_DEFINE_TYPE_WITH_CODE (InternalFolderObserver,
1105 internal_folder_observer,
1107 G_IMPLEMENT_INTERFACE(TNY_TYPE_FOLDER_OBSERVER, tny_folder_observer_init));
1111 foreach_add_item (gpointer header, gpointer user_data)
1113 tny_list_prepend (TNY_LIST (user_data),
1114 g_object_ref (G_OBJECT (header)));
1117 /* This is the method that looks for new messages in a folder */
1119 internal_folder_observer_update (TnyFolderObserver *self, TnyFolderChange *change)
1121 InternalFolderObserver *derived = (InternalFolderObserver *)self;
1123 TnyFolderChangeChanged changed;
1125 changed = tny_folder_change_get_changed (change);
1127 if (changed & TNY_FOLDER_CHANGE_CHANGED_ADDED_HEADERS) {
1130 /* Get added headers */
1131 list = tny_simple_list_new ();
1132 tny_folder_change_get_added_headers (change, list);
1134 /* Add them to the folder observer */
1135 tny_list_foreach (list, foreach_add_item,
1136 derived->new_headers);
1138 g_object_unref (G_OBJECT (list));
1143 internal_folder_observer_init (InternalFolderObserver *self)
1145 self->new_headers = tny_simple_list_new ();
1148 internal_folder_observer_finalize (GObject *object)
1150 InternalFolderObserver *self;
1152 self = (InternalFolderObserver *) object;
1153 g_object_unref (self->new_headers);
1155 G_OBJECT_CLASS (internal_folder_observer_parent_class)->finalize (object);
1158 tny_folder_observer_init (TnyFolderObserverIface *iface)
1160 iface->update = internal_folder_observer_update;
1163 internal_folder_observer_class_init (InternalFolderObserverClass *klass)
1165 GObjectClass *object_class;
1167 internal_folder_observer_parent_class = g_type_class_peek_parent (klass);
1168 object_class = (GObjectClass*) klass;
1169 object_class->finalize = internal_folder_observer_finalize;
1174 ModestMailOperation *mail_op;
1175 gchar *account_name;
1176 UpdateAccountCallback callback;
1181 TnyFolderObserver *inbox_observer;
1182 RetrieveAllCallback retrieve_all_cb;
1183 } UpdateAccountInfo;
1187 destroy_update_account_info (UpdateAccountInfo *info)
1189 g_free (info->account_name);
1190 g_object_unref (info->folders);
1191 g_object_unref (info->mail_op);
1192 g_slice_free (UpdateAccountInfo, info);
1196 update_account_get_msg_async_cb (TnyFolder *folder,
1202 GetMsgInfo *msg_info = (GetMsgInfo *) user_data;
1204 /* Just delete the helper. Don't do anything with the new
1205 msg. There is also no need to check for errors */
1206 g_object_unref (msg_info->mail_op);
1207 g_object_unref (msg_info->header);
1208 g_slice_free (GetMsgInfo, msg_info);
1212 inbox_refreshed_cb (TnyFolder *inbox,
1217 UpdateAccountInfo *info;
1218 ModestMailOperationPrivate *priv;
1219 TnyIterator *new_headers_iter;
1220 GPtrArray *new_headers_array = NULL;
1221 gint max_size, retrieve_limit, i;
1222 ModestAccountMgr *mgr;
1223 ModestAccountRetrieveType retrieve_type;
1224 TnyList *new_headers = NULL;
1225 gboolean headers_only, ignore_limit;
1226 TnyTransportAccount *transport_account = NULL;
1228 info = (UpdateAccountInfo *) user_data;
1229 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
1230 mgr = modest_runtime_get_account_mgr ();
1232 if (canceled || err || !inbox) {
1233 /* Try to send anyway */
1237 /* Get the message max size */
1238 max_size = modest_conf_get_int (modest_runtime_get_conf (),
1239 MODEST_CONF_MSG_SIZE_LIMIT, NULL);
1241 max_size = G_MAXINT;
1243 max_size = max_size * KB;
1245 /* Create the new headers array. We need it to sort the
1246 new headers by date */
1247 new_headers_array = g_ptr_array_new ();
1248 new_headers_iter = tny_list_create_iterator (((InternalFolderObserver *) info->inbox_observer)->new_headers);
1249 while (!tny_iterator_is_done (new_headers_iter)) {
1250 TnyHeader *header = NULL;
1252 header = TNY_HEADER (tny_iterator_get_current (new_headers_iter));
1253 /* Apply per-message size limits */
1254 if (tny_header_get_message_size (header) < max_size)
1255 g_ptr_array_add (new_headers_array, g_object_ref (header));
1257 g_object_unref (header);
1258 tny_iterator_next (new_headers_iter);
1260 g_object_unref (new_headers_iter);
1261 tny_folder_remove_observer (inbox, info->inbox_observer);
1262 g_object_unref (info->inbox_observer);
1263 info->inbox_observer = NULL;
1265 /* Update the last updated key, even if we don't have to get new headers */
1266 modest_account_mgr_set_last_updated (mgr, tny_account_get_id (priv->account), time (NULL));
1267 if (!canceled && !err)
1268 modest_account_mgr_set_server_account_username_has_succeeded (mgr, tny_account_get_id (priv->account), TRUE);
1270 if (new_headers_array->len == 0)
1273 /* Get per-account message amount retrieval limit */
1274 retrieve_limit = modest_account_mgr_get_retrieve_limit (mgr, info->account_name);
1275 if (retrieve_limit == 0)
1276 retrieve_limit = G_MAXINT;
1278 /* Get per-account retrieval type */
1279 retrieve_type = modest_account_mgr_get_retrieve_type (mgr, info->account_name);
1280 headers_only = (retrieve_type == MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY);
1283 g_ptr_array_sort (new_headers_array, (GCompareFunc) compare_headers_by_date);
1285 /* Ask the users if they want to retrieve all the messages
1286 even though the limit was exceeded */
1287 ignore_limit = FALSE;
1288 if (new_headers_array->len > retrieve_limit) {
1289 /* Ask the user if a callback has been specified and
1290 if the mail operation has a source (this means that
1291 was invoked by the user and not automatically by a
1293 if (info->retrieve_all_cb && priv->source)
1294 ignore_limit = info->retrieve_all_cb (priv->source,
1295 new_headers_array->len,
1299 if (!headers_only) {
1301 const gint msg_list_size = compute_message_array_size (new_headers_array);
1305 priv->total = new_headers_array->len;
1307 priv->total = MIN (new_headers_array->len, retrieve_limit);
1308 while (msg_num < priv->total) {
1309 TnyHeader *header = TNY_HEADER (g_ptr_array_index (new_headers_array, msg_num));
1310 TnyFolder *folder = tny_header_get_folder (header);
1311 GetMsgInfo *msg_info;
1313 /* Create the message info */
1314 msg_info = g_slice_new0 (GetMsgInfo);
1315 msg_info->mail_op = g_object_ref (info->mail_op);
1316 msg_info->header = g_object_ref (header);
1317 msg_info->total_bytes = msg_list_size;
1319 /* Get message in an async way */
1320 tny_folder_get_msg_async (folder, header, update_account_get_msg_async_cb,
1321 get_msg_status_cb, msg_info);
1323 g_object_unref (folder);
1329 /* Copy the headers to a list and free the array */
1330 new_headers = tny_simple_list_new ();
1331 for (i=0; i < new_headers_array->len; i++) {
1332 TnyHeader *header = TNY_HEADER (g_ptr_array_index (new_headers_array, i));
1333 tny_list_append (new_headers, G_OBJECT (header));
1335 g_ptr_array_foreach (new_headers_array, (GFunc) g_object_unref, NULL);
1336 g_ptr_array_free (new_headers_array, FALSE);
1339 /* Get the transport account */
1340 transport_account = (TnyTransportAccount *)
1341 modest_tny_account_store_get_transport_account_for_open_connection (modest_runtime_get_account_store(),
1342 info->account_name);
1344 if (transport_account) {
1345 ModestTnySendQueue *send_queue;
1349 send_queue = modest_runtime_get_send_queue (transport_account);
1350 g_object_unref (transport_account);
1352 /* Get outbox folder */
1353 outbox = tny_send_queue_get_outbox (TNY_SEND_QUEUE (send_queue));
1354 if (outbox) { /* this could fail in some cases */
1355 num_messages = tny_folder_get_all_count (outbox);
1356 g_object_unref (outbox);
1358 g_warning ("%s: could not get outbox", __FUNCTION__);
1362 if (num_messages != 0) {
1364 g_object_unref (priv->account);
1366 /* Reenable suspended items */
1367 modest_tny_send_queue_wakeup (MODEST_TNY_SEND_QUEUE (send_queue));
1370 tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE (send_queue));
1374 /* Check if the operation was a success */
1376 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1378 /* Set the account back to not busy */
1379 modest_account_mgr_set_account_busy (mgr, info->account_name, FALSE);
1381 /* Call the user callback */
1383 info->callback (info->mail_op, new_headers, info->user_data);
1385 /* Notify about operation end */
1386 modest_mail_operation_notify_end (info->mail_op);
1390 g_object_unref (new_headers);
1391 destroy_update_account_info (info);
1395 recurse_folders_async_cb (TnyFolderStore *folder_store,
1401 UpdateAccountInfo *info;
1402 ModestMailOperationPrivate *priv;
1404 info = (UpdateAccountInfo *) user_data;
1405 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
1407 if (err || canceled) {
1408 /* Try to continue anyway */
1410 TnyIterator *iter = tny_list_create_iterator (list);
1411 while (!tny_iterator_is_done (iter)) {
1412 TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
1413 TnyList *folders = tny_simple_list_new ();
1415 /* Add to the list of all folders */
1416 tny_list_append (info->folders, (GObject *) folder);
1418 /* Add pending call */
1419 info->pending_calls++;
1421 tny_folder_store_get_folders_async (folder, folders, NULL,
1422 recurse_folders_async_cb,
1425 g_object_unref (G_OBJECT (folder));
1427 tny_iterator_next (iter);
1429 g_object_unref (G_OBJECT (iter));
1430 g_object_unref (G_OBJECT (list));
1433 /* Remove my own pending call */
1434 info->pending_calls--;
1436 /* This means that we have all the folders */
1437 if (info->pending_calls == 0) {
1438 TnyIterator *iter_all_folders;
1439 TnyFolder *inbox = NULL;
1441 iter_all_folders = tny_list_create_iterator (info->folders);
1443 /* Do a poke status over all folders */
1444 while (!tny_iterator_is_done (iter_all_folders) &&
1445 priv->status == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS) {
1446 TnyFolder *folder = NULL;
1448 folder = TNY_FOLDER (tny_iterator_get_current (iter_all_folders));
1450 if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX) {
1451 /* Get a reference to the INBOX */
1452 inbox = g_object_ref (folder);
1454 /* Issue a poke status over the folder */
1456 tny_folder_poke_status (folder);
1459 /* Free and go to next */
1460 g_object_unref (folder);
1461 tny_iterator_next (iter_all_folders);
1463 g_object_unref (iter_all_folders);
1465 /* Refresh the INBOX */
1467 /* Refresh the folder. Our observer receives
1468 * the new emails during folder refreshes, so
1469 * we can use observer->new_headers
1471 info->inbox_observer = g_object_new (internal_folder_observer_get_type (), NULL);
1472 tny_folder_add_observer (inbox, info->inbox_observer);
1474 /* Refresh the INBOX */
1475 tny_folder_refresh_async (inbox, inbox_refreshed_cb, NULL, info);
1476 g_object_unref (inbox);
1478 /* We could not perform the inbox refresh but
1479 we'll try to send mails anyway */
1480 inbox_refreshed_cb (inbox, FALSE, NULL, info);
1486 modest_mail_operation_update_account (ModestMailOperation *self,
1487 const gchar *account_name,
1489 RetrieveAllCallback retrieve_all_cb,
1490 UpdateAccountCallback callback,
1493 UpdateAccountInfo *info = NULL;
1494 ModestMailOperationPrivate *priv = NULL;
1495 ModestTnyAccountStore *account_store = NULL;
1497 ModestMailOperationState *state;
1499 /* Init mail operation */
1500 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1503 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1504 priv->op_type = MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE;
1506 /* Get the store account */
1507 account_store = modest_runtime_get_account_store ();
1509 modest_tny_account_store_get_server_account (account_store,
1511 TNY_ACCOUNT_TYPE_STORE);
1513 /* Create the helper object */
1514 info = g_slice_new0 (UpdateAccountInfo);
1515 info->pending_calls = 1;
1516 info->folders = tny_simple_list_new ();
1517 info->mail_op = g_object_ref (self);
1518 info->poke_all = poke_all;
1519 info->account_name = g_strdup (account_name);
1520 info->callback = callback;
1521 info->user_data = user_data;
1522 info->retrieve_all_cb = retrieve_all_cb;
1524 /* Set account busy */
1525 modest_account_mgr_set_account_busy (modest_runtime_get_account_mgr (), account_name, TRUE);
1526 modest_mail_operation_notify_start (self);
1528 /* notify about the start of the operation */
1529 state = modest_mail_operation_clone_state (self);
1533 /* Start notifying progress */
1534 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
1535 g_slice_free (ModestMailOperationState, state);
1537 /* Get all folders and continue in the callback */
1538 folders = tny_simple_list_new ();
1539 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (priv->account),
1541 recurse_folders_async_cb,
1546 * Used to notify the queue from the main
1547 * loop. We call it inside an idle call to achieve that
1550 idle_notify_queue (gpointer data)
1552 ModestMailOperation *mail_op = MODEST_MAIL_OPERATION (data);
1554 gdk_threads_enter ();
1555 modest_mail_operation_notify_end (mail_op);
1556 gdk_threads_leave ();
1557 g_object_unref (mail_op);
1563 compare_headers_by_date (gconstpointer a,
1566 TnyHeader **header1, **header2;
1567 time_t sent1, sent2;
1569 header1 = (TnyHeader **) a;
1570 header2 = (TnyHeader **) b;
1572 sent1 = tny_header_get_date_sent (*header1);
1573 sent2 = tny_header_get_date_sent (*header2);
1575 /* We want the most recent ones (greater time_t) at the
1584 /* ******************************************************************* */
1585 /* ************************** STORE ACTIONS ************************* */
1586 /* ******************************************************************* */
1589 ModestMailOperation *mail_op;
1590 CreateFolderUserCallback callback;
1596 create_folder_cb (TnyFolderStore *parent_folder,
1598 TnyFolder *new_folder,
1602 ModestMailOperationPrivate *priv;
1603 CreateFolderInfo *info;
1605 info = (CreateFolderInfo *) user_data;
1606 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
1608 if (canceled || err) {
1609 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1611 priv->error = g_error_copy (err);
1613 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1614 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
1617 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1620 /* The user will unref the new_folder */
1622 info->callback (info->mail_op, parent_folder,
1623 new_folder, info->user_data);
1625 /* Notify about operation end */
1626 modest_mail_operation_notify_end (info->mail_op);
1629 g_object_unref (info->mail_op);
1630 g_slice_free (CreateFolderInfo, info);
1634 modest_mail_operation_create_folder (ModestMailOperation *self,
1635 TnyFolderStore *parent,
1637 CreateFolderUserCallback callback,
1640 ModestMailOperationPrivate *priv;
1642 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
1643 g_return_if_fail (name);
1645 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1646 priv->op_type = MODEST_MAIL_OPERATION_TYPE_INFO;
1647 priv->account = (TNY_IS_ACCOUNT (parent)) ?
1648 g_object_ref (parent) :
1649 modest_tny_folder_get_account (TNY_FOLDER (parent));
1651 /* Check for already existing folder */
1652 if (modest_tny_folder_has_subfolder_with_name (parent, name, TRUE)) {
1653 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1654 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1655 MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS,
1656 _CS("ckdg_ib_folder_already_exists"));
1660 if (TNY_IS_FOLDER (parent)) {
1661 /* Check folder rules */
1662 ModestTnyFolderRules rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
1663 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
1664 /* Set status failed and set an error */
1665 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1666 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1667 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1668 _("mail_in_ui_folder_create_error"));
1672 if (!strcmp (name, " ") || strchr (name, '/')) {
1673 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1674 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1675 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1676 _("mail_in_ui_folder_create_error"));
1680 CreateFolderInfo *info;
1682 info = g_slice_new0 (CreateFolderInfo);
1683 info->mail_op = g_object_ref (self);
1684 info->callback = callback;
1685 info->user_data = user_data;
1687 modest_mail_operation_notify_start (self);
1689 /* Create the folder */
1690 tny_folder_store_create_folder_async (parent, name, create_folder_cb,
1693 /* Call the user callback anyway */
1695 callback (self, parent, NULL, user_data);
1696 /* Notify about operation end */
1697 modest_mail_operation_notify_end (self);
1702 modest_mail_operation_remove_folder (ModestMailOperation *self,
1704 gboolean remove_to_trash)
1706 ModestMailOperationPrivate *priv;
1707 ModestTnyFolderRules rules;
1709 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1710 g_return_if_fail (TNY_IS_FOLDER (folder));
1712 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1714 /* Check folder rules */
1715 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
1716 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_DELETABLE) {
1717 /* Set status failed and set an error */
1718 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1719 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1720 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1721 _("mail_in_ui_folder_delete_error"));
1725 /* Get the account */
1726 priv->account = modest_tny_folder_get_account (folder);
1727 priv->op_type = MODEST_MAIL_OPERATION_TYPE_DELETE;
1729 /* Delete folder or move to trash */
1730 if (remove_to_trash) {
1731 TnyFolder *trash_folder = NULL;
1732 trash_folder = modest_tny_account_get_special_folder (priv->account,
1733 TNY_FOLDER_TYPE_TRASH);
1734 /* TODO: error_handling */
1736 modest_mail_operation_notify_start (self);
1737 modest_mail_operation_xfer_folder (self, folder,
1738 TNY_FOLDER_STORE (trash_folder),
1740 g_object_unref (trash_folder);
1742 g_warning ("%s: modest_tny_account_get_special_folder(..) returned a NULL trash folder", __FUNCTION__);
1745 TnyFolderStore *parent = tny_folder_get_folder_store (folder);
1747 modest_mail_operation_notify_start (self);
1748 tny_folder_store_remove_folder (parent, folder, &(priv->error));
1749 CHECK_EXCEPTION (priv, MODEST_MAIL_OPERATION_STATUS_FAILED);
1752 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1754 g_object_unref (parent);
1756 g_warning ("%s: could not get parent folder", __FUNCTION__);
1760 /* Notify about operation end */
1761 modest_mail_operation_notify_end (self);
1765 transfer_folder_status_cb (GObject *obj,
1769 ModestMailOperation *self;
1770 ModestMailOperationPrivate *priv;
1771 ModestMailOperationState *state;
1772 XFerFolderAsyncHelper *helper;
1774 g_return_if_fail (status != NULL);
1775 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_COPY_FOLDER);
1777 helper = (XFerFolderAsyncHelper *) user_data;
1778 g_return_if_fail (helper != NULL);
1780 self = helper->mail_op;
1781 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1783 priv->done = status->position;
1784 priv->total = status->of_total;
1786 state = modest_mail_operation_clone_state (self);
1788 /* This is not a GDK lock because we are a Tinymail callback
1789 * which is already GDK locked by Tinymail */
1791 /* no gdk_threads_enter (), CHECKED */
1793 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
1795 /* no gdk_threads_leave (), CHECKED */
1797 g_slice_free (ModestMailOperationState, state);
1802 transfer_folder_cb (TnyFolder *folder,
1804 TnyFolderStore *into,
1805 TnyFolder *new_folder,
1809 XFerFolderAsyncHelper *helper;
1810 ModestMailOperation *self = NULL;
1811 ModestMailOperationPrivate *priv = NULL;
1813 helper = (XFerFolderAsyncHelper *) user_data;
1814 g_return_if_fail (helper != NULL);
1816 self = helper->mail_op;
1817 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
1820 priv->error = g_error_copy (err);
1822 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1823 } else if (cancelled) {
1824 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
1825 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1826 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
1827 _("Transference of %s was cancelled."),
1828 tny_folder_get_name (folder));
1831 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
1834 /* Notify about operation end */
1835 modest_mail_operation_notify_end (self);
1837 /* If user defined callback function was defined, call it */
1838 if (helper->user_callback) {
1840 /* This is not a GDK lock because we are a Tinymail callback
1841 * which is already GDK locked by Tinymail */
1843 /* no gdk_threads_enter (), CHECKED */
1844 helper->user_callback (self, new_folder, helper->user_data);
1845 /* no gdk_threads_leave () , CHECKED */
1849 g_object_unref (helper->mail_op);
1850 g_slice_free (XFerFolderAsyncHelper, helper);
1855 * This function checks if the new name is a valid name for our local
1856 * folders account. The new name could not be the same than then name
1857 * of any of the mandatory local folders
1859 * We can not rely on tinymail because tinymail does not check the
1860 * name of the virtual folders that the account could have in the case
1861 * that we're doing a rename (because it directly calls Camel which
1862 * knows nothing about our virtual folders).
1864 * In the case of an actual copy/move (i.e. move/copy a folder between
1865 * accounts) tinymail uses the tny_folder_store_create_account which
1866 * is reimplemented by our ModestTnyLocalFoldersAccount that indeed
1867 * checks the new name of the folder, so this call in that case
1868 * wouldn't be needed. *But* NOTE that if tinymail changes its
1869 * implementation (if folder transfers within the same account is no
1870 * longer implemented as a rename) this call will allow Modest to work
1873 * If the new name is not valid, this function will set the status to
1874 * failed and will set also an error in the mail operation
1877 new_name_valid_if_local_account (ModestMailOperationPrivate *priv,
1878 TnyFolderStore *into,
1879 const gchar *new_name)
1881 if (TNY_IS_ACCOUNT (into) &&
1882 modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (into)) &&
1883 modest_tny_local_folders_account_folder_name_in_use (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (into),
1885 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1886 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1887 MODEST_MAIL_OPERATION_ERROR_FOLDER_EXISTS,
1888 _CS("ckdg_ib_folder_already_exists"));
1895 modest_mail_operation_xfer_folder (ModestMailOperation *self,
1897 TnyFolderStore *parent,
1898 gboolean delete_original,
1899 XferFolderAsyncUserCallback user_callback,
1902 ModestMailOperationPrivate *priv = NULL;
1903 ModestTnyFolderRules parent_rules = 0, rules;
1904 XFerFolderAsyncHelper *helper = NULL;
1905 const gchar *folder_name = NULL;
1906 const gchar *error_msg;
1908 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
1909 g_return_if_fail (TNY_IS_FOLDER (folder));
1910 g_return_if_fail (TNY_IS_FOLDER_STORE (parent));
1912 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
1913 folder_name = tny_folder_get_name (folder);
1915 /* Set the error msg */
1916 error_msg = _("mail_in_ui_folder_move_target_error");
1918 /* Get account and set it into mail_operation */
1919 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
1920 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
1921 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
1923 /* Get folder rules */
1924 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
1925 if (TNY_IS_FOLDER (parent))
1926 parent_rules = modest_tny_folder_get_rules (TNY_FOLDER (parent));
1928 /* Apply operation constraints */
1929 if ((gpointer) parent == (gpointer) folder ||
1930 (!TNY_IS_FOLDER_STORE (parent)) ||
1931 (rules & MODEST_FOLDER_RULES_FOLDER_NON_MOVEABLE)) {
1934 } else if (TNY_IS_FOLDER (parent) &&
1935 (parent_rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE)) {
1939 } else if (TNY_IS_FOLDER (parent) &&
1940 TNY_IS_FOLDER_STORE (folder) &&
1941 modest_tny_folder_is_ancestor (TNY_FOLDER (parent),
1942 TNY_FOLDER_STORE (folder))) {
1943 /* Do not move a parent into a child */
1945 } else if (TNY_IS_FOLDER_STORE (parent) &&
1946 modest_tny_folder_has_subfolder_with_name (parent, folder_name, TRUE)) {
1947 /* Check that the new folder name is not used by any
1950 } else if (!(new_name_valid_if_local_account (priv, parent, folder_name))) {
1951 /* Check that the new folder name is not used by any
1952 special local folder */
1955 /* Create the helper */
1956 helper = g_slice_new0 (XFerFolderAsyncHelper);
1957 helper->mail_op = g_object_ref (self);
1958 helper->user_callback = user_callback;
1959 helper->user_data = user_data;
1961 /* Move/Copy folder */
1962 modest_mail_operation_notify_start (self);
1963 tny_folder_copy_async (folder,
1965 tny_folder_get_name (folder),
1968 transfer_folder_status_cb,
1974 /* Set status failed and set an error */
1975 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
1976 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
1977 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
1980 /* Call the user callback if exists */
1982 user_callback (self, NULL, user_data);
1984 /* Notify the queue */
1985 modest_mail_operation_notify_end (self);
1989 modest_mail_operation_rename_folder (ModestMailOperation *self,
1992 XferFolderAsyncUserCallback user_callback,
1995 ModestMailOperationPrivate *priv;
1996 ModestTnyFolderRules rules;
1997 XFerFolderAsyncHelper *helper;
1999 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2000 g_return_if_fail (TNY_IS_FOLDER_STORE (folder));
2001 g_return_if_fail (name);
2003 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2005 /* Get account and set it into mail_operation */
2006 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2007 priv->op_type = MODEST_MAIL_OPERATION_TYPE_INFO;
2009 /* Check folder rules */
2010 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
2011 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_RENAMEABLE) {
2013 } else if (!strcmp (name, " ") || strchr (name, '/')) {
2016 TnyFolderStore *into;
2018 into = tny_folder_get_folder_store (folder);
2020 /* Check that the new folder name is not used by any
2021 special local folder */
2022 if (new_name_valid_if_local_account (priv, into, name)) {
2023 /* Create the helper */
2024 helper = g_slice_new0 (XFerFolderAsyncHelper);
2025 helper->mail_op = g_object_ref(self);
2026 helper->user_callback = user_callback;
2027 helper->user_data = user_data;
2029 /* Rename. Camel handles folder subscription/unsubscription */
2030 modest_mail_operation_notify_start (self);
2031 tny_folder_copy_async (folder, into, name, TRUE,
2033 transfer_folder_status_cb,
2038 g_object_unref (into);
2043 /* Set status failed and set an error */
2044 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2045 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2046 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
2047 _("FIXME: unable to rename"));
2050 user_callback (self, NULL, user_data);
2052 /* Notify about operation end */
2053 modest_mail_operation_notify_end (self);
2056 /* ******************************************************************* */
2057 /* ************************** MSG ACTIONS ************************* */
2058 /* ******************************************************************* */
2061 modest_mail_operation_get_msg (ModestMailOperation *self,
2063 GetMsgAsyncUserCallback user_callback,
2066 GetMsgInfo *helper = NULL;
2068 ModestMailOperationPrivate *priv;
2070 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2071 g_return_if_fail (TNY_IS_HEADER (header));
2073 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2074 folder = tny_header_get_folder (header);
2076 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2080 /* Get account and set it into mail_operation */
2081 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2083 /* Check for cached messages */
2084 if (tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED)
2085 priv->op_type = MODEST_MAIL_OPERATION_TYPE_OPEN;
2087 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2089 /* Create the helper */
2090 helper = g_slice_new0 (GetMsgInfo);
2091 helper->header = g_object_ref (header);
2092 helper->mail_op = g_object_ref (self);
2093 helper->user_callback = user_callback;
2094 helper->user_data = user_data;
2095 helper->destroy_notify = NULL;
2096 helper->last_total_bytes = 0;
2097 helper->sum_total_bytes = 0;
2098 helper->total_bytes = tny_header_get_message_size (header);
2100 modest_mail_operation_notify_start (self);
2102 /* notify about the start of the operation */
2103 ModestMailOperationState *state;
2104 state = modest_mail_operation_clone_state (self);
2107 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL],
2110 tny_folder_get_msg_async (folder, header, get_msg_async_cb, get_msg_status_cb, helper);
2112 g_object_unref (G_OBJECT (folder));
2116 get_msg_status_cb (GObject *obj,
2120 GetMsgInfo *helper = NULL;
2122 g_return_if_fail (status != NULL);
2123 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_GET_MSG);
2125 helper = (GetMsgInfo *) user_data;
2126 g_return_if_fail (helper != NULL);
2128 /* Notify progress */
2129 notify_progress_of_multiple_messages (helper->mail_op, status, &(helper->last_total_bytes),
2130 &(helper->sum_total_bytes), helper->total_bytes, FALSE);
2134 get_msg_async_cb (TnyFolder *folder,
2140 GetMsgInfo *info = NULL;
2141 ModestMailOperationPrivate *priv = NULL;
2144 info = (GetMsgInfo *) user_data;
2146 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
2149 if (info->more_msgs) {
2150 tny_iterator_next (info->more_msgs);
2151 finished = (tny_iterator_is_done (info->more_msgs));
2153 finished = (priv->done == priv->total) ? TRUE : FALSE;
2156 /* If canceled by the user, ignore the error given by Tinymail */
2157 if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED) {
2160 } else if (canceled || err) {
2161 priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
2163 priv->error = g_error_copy ((const GError *) err);
2164 priv->error->domain = MODEST_MAIL_OPERATION_ERROR;
2167 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2168 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2171 } else if (finished && priv->status == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS) {
2172 /* Set the success status before calling the user callback */
2173 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2177 /* Call the user callback */
2178 if (info->user_callback)
2179 info->user_callback (info->mail_op, info->header, canceled,
2180 msg, err, info->user_data);
2182 /* Notify about operation end if this is the last callback */
2184 /* Free user data */
2185 if (info->destroy_notify)
2186 info->destroy_notify (info->user_data);
2188 /* Notify about operation end */
2189 modest_mail_operation_notify_end (info->mail_op);
2192 if (info->more_msgs)
2193 g_object_unref (info->more_msgs);
2194 g_object_unref (info->header);
2195 g_object_unref (info->mail_op);
2196 g_slice_free (GetMsgInfo, info);
2197 } else if (info->more_msgs) {
2198 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (info->more_msgs));
2199 TnyFolder *folder = tny_header_get_folder (header);
2201 g_object_unref (info->header);
2202 info->header = g_object_ref (header);
2204 /* Retrieve the next message */
2205 tny_folder_get_msg_async (folder, header, get_msg_async_cb, get_msg_status_cb, info);
2207 g_object_unref (header);
2208 g_object_unref (folder);
2210 g_warning ("%s: finished != TRUE but no messages left", __FUNCTION__);
2215 modest_mail_operation_get_msgs_full (ModestMailOperation *self,
2216 TnyList *header_list,
2217 GetMsgAsyncUserCallback user_callback,
2219 GDestroyNotify notify)
2221 ModestMailOperationPrivate *priv = NULL;
2223 TnyIterator *iter = NULL;
2225 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2227 /* Init mail operation */
2228 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2229 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2230 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2232 priv->total = tny_list_get_length(header_list);
2234 /* Get account and set it into mail_operation */
2235 if (tny_list_get_length (header_list) >= 1) {
2236 TnyIterator *iterator = tny_list_create_iterator (header_list);
2237 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iterator));
2239 TnyFolder *folder = tny_header_get_folder (header);
2241 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2242 g_object_unref (folder);
2244 g_object_unref (header);
2246 g_object_unref (iterator);
2249 msg_list_size = compute_message_list_size (header_list);
2251 modest_mail_operation_notify_start (self);
2252 iter = tny_list_create_iterator (header_list);
2253 if (!tny_iterator_is_done (iter)) {
2254 /* notify about the start of the operation */
2255 ModestMailOperationState *state;
2256 state = modest_mail_operation_clone_state (self);
2259 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL],
2262 GetMsgInfo *msg_info = NULL;
2263 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
2264 TnyFolder *folder = tny_header_get_folder (header);
2266 /* Create the message info */
2267 msg_info = g_slice_new0 (GetMsgInfo);
2268 msg_info->mail_op = g_object_ref (self);
2269 msg_info->header = g_object_ref (header);
2270 msg_info->more_msgs = g_object_ref (iter);
2271 msg_info->user_callback = user_callback;
2272 msg_info->user_data = user_data;
2273 msg_info->destroy_notify = notify;
2274 msg_info->last_total_bytes = 0;
2275 msg_info->sum_total_bytes = 0;
2276 msg_info->total_bytes = msg_list_size;
2278 /* The callback will call it per each header */
2279 tny_folder_get_msg_async (folder, header, get_msg_async_cb, get_msg_status_cb, msg_info);
2281 /* Free and go on */
2282 g_object_unref (header);
2283 g_object_unref (folder);
2284 g_slice_free (ModestMailOperationState, state);
2286 g_object_unref (iter);
2291 modest_mail_operation_remove_msg (ModestMailOperation *self,
2293 gboolean remove_to_trash /*ignored*/)
2296 ModestMailOperationPrivate *priv;
2298 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2299 g_return_if_fail (TNY_IS_HEADER (header));
2301 if (remove_to_trash)
2302 g_warning ("remove to trash is not implemented");
2304 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2305 folder = tny_header_get_folder (header);
2307 /* Get account and set it into mail_operation */
2308 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2309 priv->op_type = MODEST_MAIL_OPERATION_TYPE_DELETE;
2310 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2312 /* remove message from folder */
2313 tny_folder_remove_msg (folder, header, &(priv->error));
2315 gboolean expunge, leave_on_server;
2316 const gchar *account_name;
2317 TnyAccount *account;
2318 ModestTransportStoreProtocol account_proto;
2320 tny_header_set_flag (header, TNY_HEADER_FLAG_DELETED);
2321 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
2323 modest_mail_operation_notify_start (self);
2325 /* Get leave on server setting */
2326 account = tny_folder_get_account (folder);
2327 account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
2329 modest_account_mgr_get_leave_on_server (modest_runtime_get_account_mgr (),
2332 account_proto = modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (account));
2334 if (((account_proto == MODEST_PROTOCOL_STORE_POP) && !leave_on_server) ||
2335 modest_tny_folder_is_remote_folder (folder) == FALSE)
2341 tny_folder_sync_async(folder, expunge, NULL, NULL, NULL);
2344 g_object_unref (account);
2350 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2352 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2355 g_object_unref (G_OBJECT (folder));
2357 /* Notify about operation end */
2358 modest_mail_operation_notify_end (self);
2362 modest_mail_operation_remove_msgs (ModestMailOperation *self,
2364 gboolean remove_to_trash /*ignored*/)
2366 TnyFolder *folder = NULL;
2367 ModestMailOperationPrivate *priv;
2368 TnyIterator *iter = NULL;
2369 TnyHeader *header = NULL;
2370 TnyList *remove_headers = NULL;
2371 TnyFolderType folder_type = TNY_FOLDER_TYPE_UNKNOWN;
2373 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2374 g_return_if_fail (TNY_IS_LIST (headers));
2376 if (remove_to_trash)
2377 g_warning ("remove to trash is not implemented");
2379 if (tny_list_get_length(headers) == 0) {
2380 g_warning ("%s: list of headers is empty\n", __FUNCTION__);
2381 goto cleanup; /* nothing to do */
2384 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2385 remove_headers = g_object_ref(headers);
2387 /* Get folder from first header and sync it */
2388 iter = tny_list_create_iterator (headers);
2389 header = TNY_HEADER (tny_iterator_get_current (iter));
2391 folder = tny_header_get_folder (header);
2392 if (!TNY_IS_FOLDER(folder)) {
2393 g_warning ("%s: could not get folder for header\n", __FUNCTION__);
2397 /* Don't remove messages that are being sent */
2398 if (modest_tny_folder_is_local_folder (folder)) {
2399 folder_type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
2401 if (folder_type == TNY_FOLDER_TYPE_OUTBOX) {
2402 TnyTransportAccount *traccount = NULL;
2403 ModestTnyAccountStore *accstore = modest_runtime_get_account_store();
2404 traccount = modest_tny_account_store_get_transport_account_from_outbox_header(accstore, header);
2406 ModestTnySendQueueStatus status;
2407 ModestTnySendQueue *send_queue = modest_runtime_get_send_queue(traccount);
2408 TnyIterator *iter = tny_list_create_iterator(headers);
2409 g_object_unref(remove_headers);
2410 remove_headers = TNY_LIST(tny_simple_list_new());
2411 while (!tny_iterator_is_done(iter)) {
2413 TnyHeader *hdr = TNY_HEADER(tny_iterator_get_current(iter));
2414 msg_id = modest_tny_send_queue_get_msg_id (hdr);
2415 status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
2416 if (status != MODEST_TNY_SEND_QUEUE_SENDING) {
2417 tny_list_append(remove_headers, G_OBJECT(hdr));
2419 g_object_unref(hdr);
2421 tny_iterator_next(iter);
2423 g_object_unref(iter);
2424 g_object_unref(traccount);
2428 /* Get account and set it into mail_operation */
2429 priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder));
2430 priv->op_type = MODEST_MAIL_OPERATION_TYPE_DELETE;
2431 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2433 /* remove message from folder */
2434 modest_mail_operation_notify_start (self);
2436 tny_folder_remove_msgs (folder, remove_headers, &(priv->error));
2438 gboolean expunge, leave_on_server;
2439 const gchar *account_name;
2441 TnyAccount *account;
2442 ModestTransportStoreProtocol account_proto = MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN;
2444 account = tny_folder_get_account (folder);
2445 account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
2447 modest_account_mgr_get_leave_on_server (modest_runtime_get_account_mgr (),
2450 proto = tny_account_get_proto (account);
2452 account_proto = modest_protocol_info_get_transport_store_protocol (proto);
2455 if (((account_proto == MODEST_PROTOCOL_STORE_POP) && !leave_on_server) ||
2456 modest_tny_folder_is_remote_folder (folder) == FALSE)
2462 tny_folder_sync_async(folder, expunge, NULL, NULL, NULL);
2464 g_object_unref (account);
2470 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2472 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2477 g_object_unref (remove_headers);
2479 g_object_unref (header);
2481 g_object_unref (iter);
2483 g_object_unref (folder);
2485 /* Notify about operation end */
2486 modest_mail_operation_notify_end (self);
2490 notify_progress_of_multiple_messages (ModestMailOperation *self,
2492 gint *last_total_bytes,
2493 gint *sum_total_bytes,
2495 gboolean increment_done)
2497 ModestMailOperationPrivate *priv;
2498 ModestMailOperationState *state;
2499 gboolean is_num_bytes = FALSE;
2501 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2503 /* We know that tinymail sends us information about
2504 * transferred bytes with this particular message
2506 * (FIXME: this is very ugly, and no I (djcb) didn't write this code,
2507 * I just added the 'if' so we don't get runtime warning)
2509 if (status->message)
2510 is_num_bytes = (g_ascii_strcasecmp (status->message, "Retrieving message") == 0);
2512 state = modest_mail_operation_clone_state (self);
2513 if (is_num_bytes && !((status->position == 1) && (status->of_total == 100))) {
2514 /* We know that we're in a different message when the
2515 total number of bytes to transfer is different. Of
2516 course it could fail if we're transferring messages
2517 of the same size, but this is a workarround */
2518 if (status->of_total != *last_total_bytes) {
2519 /* We need to increment the done when there is
2520 no information about each individual
2521 message, we need to do this in message
2522 transfers, and we don't do it for getting
2526 *sum_total_bytes += *last_total_bytes;
2527 *last_total_bytes = status->of_total;
2529 state->bytes_done += status->position + *sum_total_bytes;
2530 state->bytes_total = total_bytes;
2532 /* Notify the status change. Only notify about changes
2533 referred to bytes */
2534 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL],
2538 g_slice_free (ModestMailOperationState, state);
2542 transfer_msgs_status_cb (GObject *obj,
2546 XFerMsgsAsyncHelper *helper;
2548 g_return_if_fail (status != NULL);
2549 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_XFER_MSGS);
2551 helper = (XFerMsgsAsyncHelper *) user_data;
2552 g_return_if_fail (helper != NULL);
2554 /* Notify progress */
2555 notify_progress_of_multiple_messages (helper->mail_op, status, &(helper->last_total_bytes),
2556 &(helper->sum_total_bytes), helper->total_bytes, TRUE);
2561 transfer_msgs_cb (TnyFolder *folder, gboolean cancelled, GError *err, gpointer user_data)
2563 XFerMsgsAsyncHelper *helper;
2564 ModestMailOperation *self;
2565 ModestMailOperationPrivate *priv;
2566 gboolean finished = TRUE;
2568 helper = (XFerMsgsAsyncHelper *) user_data;
2569 self = helper->mail_op;
2571 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2574 priv->error = g_error_copy (err);
2576 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2577 } else if (cancelled) {
2578 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
2579 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2580 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2581 _("Error trying to refresh the contents of %s"),
2582 tny_folder_get_name (folder));
2583 } else if (priv->status != MODEST_MAIL_OPERATION_STATUS_CANCELED) {
2584 if (helper->more_msgs) {
2585 /* We'll transfer the next message in the list */
2586 tny_iterator_next (helper->more_msgs);
2587 if (!tny_iterator_is_done (helper->more_msgs)) {
2588 GObject *next_header;
2589 g_object_unref (helper->headers);
2590 helper->headers = tny_simple_list_new ();
2591 next_header = tny_iterator_get_current (helper->more_msgs);
2592 tny_list_append (helper->headers, next_header);
2593 g_object_unref (next_header);
2600 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2606 /* Update folder counts */
2607 tny_folder_poke_status (folder);
2608 tny_folder_poke_status (helper->dest_folder);
2610 /* Notify about operation end */
2611 modest_mail_operation_notify_end (self);
2613 /* If user defined callback function was defined, call it */
2614 if (helper->user_callback) {
2615 /* This is not a GDK lock because we are a Tinymail callback and
2616 * Tinymail already acquires the Gdk lock */
2618 /* no gdk_threads_enter (), CHECKED */
2619 helper->user_callback (self, helper->user_data);
2620 /* no gdk_threads_leave (), CHECKED */
2624 if (helper->more_msgs)
2625 g_object_unref (helper->more_msgs);
2626 if (helper->headers)
2627 g_object_unref (helper->headers);
2628 if (helper->dest_folder)
2629 g_object_unref (helper->dest_folder);
2630 if (helper->mail_op)
2631 g_object_unref (helper->mail_op);
2632 g_slice_free (XFerMsgsAsyncHelper, helper);
2634 /* Transfer more messages */
2635 tny_folder_transfer_msgs_async (folder,
2637 helper->dest_folder,
2640 transfer_msgs_status_cb,
2646 compute_message_list_size (TnyList *headers)
2651 iter = tny_list_create_iterator (headers);
2652 while (!tny_iterator_is_done (iter)) {
2653 TnyHeader *header = TNY_HEADER (tny_iterator_get_current (iter));
2654 size += tny_header_get_message_size (header);
2655 g_object_unref (header);
2656 tny_iterator_next (iter);
2658 g_object_unref (iter);
2664 compute_message_array_size (GPtrArray *headers)
2669 for (i = 0; i < headers->len; i++) {
2670 TnyHeader *header = TNY_HEADER (g_ptr_array_index (headers, i));
2671 size += tny_header_get_message_size (header);
2679 modest_mail_operation_xfer_msgs (ModestMailOperation *self,
2682 gboolean delete_original,
2683 XferMsgsAsyncUserCallback user_callback,
2686 ModestMailOperationPrivate *priv = NULL;
2687 TnyIterator *iter = NULL;
2688 TnyFolder *src_folder = NULL;
2689 XFerMsgsAsyncHelper *helper = NULL;
2690 TnyHeader *header = NULL;
2691 ModestTnyFolderRules rules = 0;
2692 TnyAccount *dst_account = NULL;
2693 gboolean leave_on_server;
2695 g_return_if_fail (self && MODEST_IS_MAIL_OPERATION (self));
2696 g_return_if_fail (headers && TNY_IS_LIST (headers));
2697 g_return_if_fail (folder && TNY_IS_FOLDER (folder));
2699 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2700 priv->total = tny_list_get_length (headers);
2702 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2703 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2705 /* Apply folder rules */
2706 rules = modest_tny_folder_get_rules (TNY_FOLDER (folder));
2707 if (rules & MODEST_FOLDER_RULES_FOLDER_NON_WRITEABLE) {
2708 /* Set status failed and set an error */
2709 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2710 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2711 MODEST_MAIL_OPERATION_ERROR_FOLDER_RULES,
2712 _CS("ckct_ib_unable_to_paste_here"));
2713 /* Notify the queue */
2714 modest_mail_operation_notify_end (self);
2718 /* Get source folder */
2719 iter = tny_list_create_iterator (headers);
2720 header = TNY_HEADER (tny_iterator_get_current (iter));
2722 src_folder = tny_header_get_folder (header);
2723 g_object_unref (header);
2725 g_object_unref (iter);
2727 if (src_folder == NULL) {
2728 /* Notify the queue */
2729 modest_mail_operation_notify_end (self);
2731 g_warning ("%s: cannot find folder from header", __FUNCTION__);
2736 /* Check folder source and destination */
2737 if (src_folder == folder) {
2738 /* Set status failed and set an error */
2739 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2740 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2741 MODEST_MAIL_OPERATION_ERROR_BAD_PARAMETER,
2742 _("mail_in_ui_folder_copy_target_error"));
2744 /* Notify the queue */
2745 modest_mail_operation_notify_end (self);
2748 g_object_unref (src_folder);
2752 /* Create the helper */
2753 helper = g_slice_new0 (XFerMsgsAsyncHelper);
2754 helper->mail_op = g_object_ref(self);
2755 helper->dest_folder = g_object_ref(folder);
2756 helper->user_callback = user_callback;
2757 helper->user_data = user_data;
2758 helper->delete = delete_original;
2759 helper->last_total_bytes = 0;
2760 helper->sum_total_bytes = 0;
2761 helper->total_bytes = compute_message_list_size (headers);
2763 /* Get account and set it into mail_operation */
2764 priv->account = modest_tny_folder_get_account (src_folder);
2765 dst_account = modest_tny_folder_get_account (folder);
2767 if (priv->account == dst_account) {
2768 /* Transfer all messages at once using the fast
2769 * method. Note that depending on the server this
2770 * might not be that fast, and might not be
2771 * user-cancellable either */
2772 helper->headers = g_object_ref (headers);
2773 helper->more_msgs = NULL;
2775 /* Transfer messages one by one so the user can cancel
2778 helper->headers = tny_simple_list_new ();
2779 helper->more_msgs = tny_list_create_iterator (headers);
2780 hdr = tny_iterator_get_current (helper->more_msgs);
2781 tny_list_append (helper->headers, hdr);
2782 g_object_unref (hdr);
2785 /* If leave_on_server is set to TRUE then don't use
2786 delete_original, we always pass FALSE. This is because
2787 otherwise tinymail will try to sync the source folder and
2788 this could cause an error if we're offline while
2789 transferring an already downloaded message from a POP
2791 if (modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (priv->account)) ==
2792 MODEST_PROTOCOL_STORE_POP) {
2793 const gchar *account_name;
2795 account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (priv->account);
2796 leave_on_server = modest_account_mgr_get_leave_on_server (modest_runtime_get_account_mgr (),
2799 leave_on_server = FALSE;
2802 modest_mail_operation_notify_start (self);
2803 tny_folder_transfer_msgs_async (src_folder,
2806 (leave_on_server) ? FALSE : delete_original,
2808 transfer_msgs_status_cb,
2810 g_object_unref (src_folder);
2811 g_object_unref (dst_account);
2816 on_refresh_folder (TnyFolder *folder,
2821 RefreshAsyncHelper *helper = NULL;
2822 ModestMailOperation *self = NULL;
2823 ModestMailOperationPrivate *priv = NULL;
2825 helper = (RefreshAsyncHelper *) user_data;
2826 self = helper->mail_op;
2827 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2829 g_return_if_fail(priv!=NULL);
2831 /* If the folder is remote, set the "Last updated" value */
2832 if (modest_tny_folder_is_remote_folder (folder)) {
2833 TnyAccount *account = modest_tny_folder_get_account (folder);
2834 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
2836 name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
2837 modest_account_mgr_set_last_updated (mgr, tny_account_get_id (account), time (NULL));
2838 if (!cancelled && !error)
2839 modest_account_mgr_set_server_account_username_has_succeeded (mgr, tny_account_get_id (account), TRUE);
2840 modest_account_mgr_set_account_busy (mgr, name, FALSE);
2841 g_object_unref (account);
2845 priv->error = g_error_copy (error);
2846 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
2851 priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
2852 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
2853 MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
2854 _("Error trying to refresh the contents of %s"),
2855 tny_folder_get_name (folder));
2859 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2862 /* Call user defined callback, if it exists */
2863 if (helper->user_callback) {
2865 /* This is not a GDK lock because we are a Tinymail callback and
2866 * Tinymail already acquires the Gdk lock */
2867 helper->user_callback (self, folder, helper->user_data);
2871 g_slice_free (RefreshAsyncHelper, helper);
2873 /* Notify about operation end */
2874 modest_mail_operation_notify_end (self);
2875 g_object_unref(self);
2879 on_refresh_folder_status_update (GObject *obj,
2883 RefreshAsyncHelper *helper = NULL;
2884 ModestMailOperation *self = NULL;
2885 ModestMailOperationPrivate *priv = NULL;
2886 ModestMailOperationState *state;
2888 g_return_if_fail (user_data != NULL);
2889 g_return_if_fail (status != NULL);
2890 g_return_if_fail (status->code == TNY_FOLDER_STATUS_CODE_REFRESH);
2892 helper = (RefreshAsyncHelper *) user_data;
2893 self = helper->mail_op;
2894 g_return_if_fail (MODEST_IS_MAIL_OPERATION(self));
2896 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2898 priv->done = status->position;
2899 priv->total = status->of_total;
2901 state = modest_mail_operation_clone_state (self);
2903 /* This is not a GDK lock because we are a Tinymail callback and
2904 * Tinymail already acquires the Gdk lock */
2905 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 0, state, NULL);
2907 g_slice_free (ModestMailOperationState, state);
2911 modest_mail_operation_refresh_folder (ModestMailOperation *self,
2913 RefreshAsyncUserCallback user_callback,
2916 ModestMailOperationPrivate *priv = NULL;
2917 RefreshAsyncHelper *helper = NULL;
2919 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
2921 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2923 /* Get account and set it into mail_operation */
2924 priv->account = modest_tny_folder_get_account (folder);
2925 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
2927 /* Create the helper */
2928 helper = g_slice_new0 (RefreshAsyncHelper);
2929 helper->mail_op = g_object_ref(self);
2930 helper->user_callback = user_callback;
2931 helper->user_data = user_data;
2933 /* Refresh the folder. TODO: tinymail could issue a status
2934 updates before the callback call then this could happen. We
2935 must review the design */
2936 if (modest_tny_folder_is_remote_folder (folder)) {
2937 /* If the folder is remote, mark its account as busy */
2939 name = modest_tny_account_get_parent_modest_account_name_for_server_account (priv->account);
2940 modest_account_mgr_set_account_busy (modest_runtime_get_account_mgr (), name, TRUE);
2942 modest_mail_operation_notify_start (self);
2944 /* notify that the operation was started */
2945 ModestMailOperationState *state;
2946 state = modest_mail_operation_clone_state (self);
2949 g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL],
2952 /* FIXME: we're leaking the state here, or? valgrind thinks so */
2954 tny_folder_refresh_async (folder,
2956 on_refresh_folder_status_update,
2961 run_queue_stop (ModestTnySendQueue *queue,
2962 ModestMailOperation *self)
2964 ModestMailOperationPrivate *priv;
2966 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2967 g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE (queue));
2968 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2970 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
2972 modest_mail_operation_notify_end (self);
2973 g_signal_handlers_disconnect_by_func (queue, run_queue_stop, self);
2974 g_object_unref (self);
2977 modest_mail_operation_run_queue (ModestMailOperation *self,
2978 ModestTnySendQueue *queue)
2980 ModestMailOperationPrivate *priv;
2982 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
2983 g_return_if_fail (MODEST_IS_TNY_SEND_QUEUE (queue));
2984 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
2986 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
2987 priv->account = TNY_ACCOUNT (tny_camel_send_queue_get_transport_account (TNY_CAMEL_SEND_QUEUE (queue)));
2988 priv->op_type = MODEST_MAIL_OPERATION_TYPE_RUN_QUEUE;
2990 modest_mail_operation_notify_start (self);
2991 g_object_ref (self);
2992 g_signal_connect ((gpointer) queue, "queue-stop", G_CALLBACK (run_queue_stop), (gpointer) self);
2996 sync_folder_finish_callback (TnyFolder *self, gboolean cancelled, GError *err, ModestMailOperation *mail_op)
2998 ModestMailOperationPrivate *priv;
3000 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op);
3002 g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
3003 MODEST_MAIL_OPERATION_ERROR_OPERATION_CANCELED,
3005 priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
3007 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
3009 modest_mail_operation_notify_end (mail_op);
3010 g_object_unref (mail_op);
3014 modest_mail_operation_sync_folder (ModestMailOperation *self,
3015 TnyFolder *folder, gboolean expunge)
3017 ModestMailOperationPrivate *priv;
3019 g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
3020 g_return_if_fail (TNY_IS_FOLDER (folder));
3021 priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
3023 priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
3024 priv->account = TNY_ACCOUNT (tny_folder_get_account (folder));
3025 priv->op_type = MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER;
3027 modest_mail_operation_notify_start (self);
3028 g_object_ref (self);
3029 tny_folder_sync_async (folder, expunge, (TnyFolderCallback) sync_folder_finish_callback, NULL, self);
3033 modest_mail_operation_notify_start (ModestMailOperation *self)
3035 ModestMailOperationPrivate *priv = NULL;
3037 g_return_if_fail (self);
3039 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
3041 /* Ensure that all the fields are filled correctly */
3042 g_return_if_fail (priv->op_type != MODEST_MAIL_OPERATION_TYPE_UNKNOWN);
3044 /* Notify the observers about the mail operation. We do not
3045 wrapp this emission because we assume that this function is
3046 always called from within the main lock */
3047 g_signal_emit (G_OBJECT (self), signals[OPERATION_STARTED_SIGNAL], 0, NULL);
3052 * It's used by the mail operation queue to notify the observers
3053 * attached to that signal that the operation finished. We need to use
3054 * that because tinymail does not give us the progress of a given
3055 * operation when it finishes (it directly calls the operation
3059 modest_mail_operation_notify_end (ModestMailOperation *self)
3061 ModestMailOperationPrivate *priv = NULL;
3063 g_return_if_fail (self);
3065 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
3067 /* Notify the observers about the mail operation end. We do
3068 not wrapp this emission because we assume that this
3069 function is always called from within the main lock */
3070 g_signal_emit (G_OBJECT (self), signals[OPERATION_FINISHED_SIGNAL], 0, NULL);
3072 /* Remove the error user data */
3073 if (priv->error_checking_user_data && priv->error_checking_user_data_destroyer)
3074 priv->error_checking_user_data_destroyer (priv->error_checking_user_data);
3078 modest_mail_operation_get_account (ModestMailOperation *self)
3080 ModestMailOperationPrivate *priv = NULL;
3082 g_return_val_if_fail (self, NULL);
3084 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
3086 return (priv->account) ? g_object_ref (priv->account) : NULL;
3090 modest_mail_operation_noop (ModestMailOperation *self)
3092 ModestMailOperationPrivate *priv = NULL;
3094 g_return_if_fail (self);
3096 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
3097 priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
3098 priv->op_type = MODEST_MAIL_OPERATION_TYPE_INFO;
3102 /* This mail operation does nothing actually */
3103 modest_mail_operation_notify_start (self);
3104 modest_mail_operation_notify_end (self);
3109 modest_mail_operation_to_string (ModestMailOperation *self)
3111 const gchar *type, *status, *account_id;
3112 ModestMailOperationPrivate *priv = NULL;
3114 g_return_val_if_fail (self, NULL);
3116 priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
3118 /* new operations don't have anything interesting */
3119 if (priv->op_type == MODEST_MAIL_OPERATION_TYPE_UNKNOWN)
3120 return g_strdup_printf ("%p <new operation>", self);
3122 switch (priv->op_type) {
3123 case MODEST_MAIL_OPERATION_TYPE_SEND: type= "SEND"; break;
3124 case MODEST_MAIL_OPERATION_TYPE_RECEIVE: type= "RECEIVE"; break;
3125 case MODEST_MAIL_OPERATION_TYPE_OPEN: type= "OPEN"; break;
3126 case MODEST_MAIL_OPERATION_TYPE_DELETE: type= "DELETE"; break;
3127 case MODEST_MAIL_OPERATION_TYPE_INFO: type= "INFO"; break;
3128 case MODEST_MAIL_OPERATION_TYPE_RUN_QUEUE: type= "RUN-QUEUE"; break;
3129 case MODEST_MAIL_OPERATION_TYPE_SYNC_FOLDER: type= "SYNC-FOLDER"; break;
3130 case MODEST_MAIL_OPERATION_TYPE_UNKNOWN: type= "UNKNOWN"; break;
3131 default: type = "UNEXPECTED"; break;
3134 switch (priv->status) {
3135 case MODEST_MAIL_OPERATION_STATUS_INVALID: status= "INVALID"; break;
3136 case MODEST_MAIL_OPERATION_STATUS_SUCCESS: status= "SUCCESS"; break;
3137 case MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS: status= "FINISHED-WITH-ERRORS"; break;
3138 case MODEST_MAIL_OPERATION_STATUS_FAILED: status= "FAILED"; break;
3139 case MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS: status= "IN-PROGRESS"; break;
3140 case MODEST_MAIL_OPERATION_STATUS_CANCELED: status= "CANCELLED"; break;
3141 default: status= "UNEXPECTED"; break;
3144 account_id = priv->account ? tny_account_get_id (priv->account) : "";
3146 return g_strdup_printf ("%p \"%s\" (%s) [%s] {%d/%d} '%s'", self, account_id,type, status,
3147 priv->done, priv->total,
3148 priv->error && priv->error->message ? priv->error->message : "");