Work to fix bug NB#81989:
[modest] / src / modest-mail-operation.c
index 4c3efb3..7c522bc 100644 (file)
@@ -489,11 +489,6 @@ modest_mail_operation_cancel (ModestMailOperation *self)
 
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
 
-       /* Note that if we call cancel with an already canceled mail
-          operation the progress changed signal won't be emitted */
-       if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED)
-               return FALSE;
-
        /* Set new status */
        priv->status = MODEST_MAIL_OPERATION_STATUS_CANCELED;
        
@@ -631,6 +626,7 @@ modest_mail_operation_send_mail (ModestMailOperation *self,
                modest_mail_operation_notify_start (self);
 
                tny_send_queue_add_async (send_queue, msg, NULL, NULL, NULL);
+               modest_tny_send_queue_set_requested_send_receive (MODEST_TNY_SEND_QUEUE (send_queue), FALSE);
 
                priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
                modest_mail_operation_notify_end (self);
@@ -741,11 +737,8 @@ modest_mail_operation_create_msg (ModestMailOperation *self,
                                  ModestMailOperationCreateMsgCallback callback,
                                  gpointer userdata)
 {
-       ModestMailOperationPrivate *priv;
        CreateMsgInfo *info = NULL;
 
-       priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
-
        info = g_slice_new0 (CreateMsgInfo);
        info->mail_op = g_object_ref (self);
 
@@ -1188,6 +1181,7 @@ typedef struct
        gboolean poke_all;
        TnyFolderObserver *inbox_observer;
        RetrieveAllCallback retrieve_all_cb;
+       gboolean interactive;
 } UpdateAccountInfo;
 
 
@@ -1230,8 +1224,8 @@ inbox_refreshed_cb (TnyFolder *inbox,
        ModestAccountMgr *mgr;
        ModestAccountRetrieveType retrieve_type;
        TnyList *new_headers = NULL;
-       gboolean headers_only, ignore_limit;
-       TnyTransportAccount *transport_account;
+       gboolean headers_only, ignore_limit, succeeded;
+       TnyTransportAccount *transport_account = NULL;
 
        info = (UpdateAccountInfo *) user_data;
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
@@ -1272,6 +1266,8 @@ inbox_refreshed_cb (TnyFolder *inbox,
 
        /* Update the last updated key, even if we don't have to get new headers */
        modest_account_mgr_set_last_updated (mgr, tny_account_get_id (priv->account), time (NULL));
+       if (!canceled && !err) 
+               modest_account_mgr_set_server_account_username_has_succeeded (mgr, tny_account_get_id (priv->account), TRUE);
        
        if (new_headers_array->len == 0)
                goto send_mail;
@@ -1282,7 +1278,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
                retrieve_limit = G_MAXINT;
        
        /* Get per-account retrieval type */
-       retrieve_type = modest_account_mgr_get_retrieve_type (mgr, info->account_name); 
+       retrieve_type = modest_account_mgr_get_retrieve_type (mgr, info->account_name);
        headers_only = (retrieve_type == MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY);
 
        /* Order by date */
@@ -1341,6 +1337,13 @@ inbox_refreshed_cb (TnyFolder *inbox,
        g_ptr_array_foreach (new_headers_array, (GFunc) g_object_unref, NULL);
        g_ptr_array_free (new_headers_array, FALSE);
 
+       if (priv->error)
+               succeeded = FALSE;
+       else
+               succeeded = TRUE;
+       modest_account_mgr_set_server_account_username_has_succeeded (modest_runtime_get_account_mgr (), 
+                                                                     tny_account_get_name (priv->account), 
+                                                                     succeeded);
  send_mail:
        /* Get the transport account */
        transport_account = (TnyTransportAccount *)
@@ -1353,23 +1356,28 @@ inbox_refreshed_cb (TnyFolder *inbox,
                guint num_messages;
 
                send_queue = modest_runtime_get_send_queue (transport_account);
+               g_object_unref (transport_account);
 
                /* Get outbox folder */
                outbox = tny_send_queue_get_outbox (TNY_SEND_QUEUE (send_queue));
-               num_messages = tny_folder_get_all_count (outbox);
-               g_object_unref (outbox);
-
+               if (outbox) { /* this could fail in some cases */
+                       num_messages = tny_folder_get_all_count (outbox);
+                       g_object_unref (outbox);
+               } else {
+                       g_warning ("%s: could not get outbox", __FUNCTION__);
+                       num_messages = 0;
+               }
+               
                if (num_messages != 0) {
-                       /* Send mails */
-                       g_object_unref (priv->account);
-
                        /* Reenable suspended items */
                        modest_tny_send_queue_wakeup (MODEST_TNY_SEND_QUEUE (send_queue));
 
                        /* Try to send */
                        tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE (send_queue));
+                       modest_tny_send_queue_set_requested_send_receive (MODEST_TNY_SEND_QUEUE (send_queue), 
+                                                                         info->interactive);
                }
-       } 
+       }
 
        /* Check if the operation was a success */
        if (!priv->error)
@@ -1406,7 +1414,8 @@ recurse_folders_async_cb (TnyFolderStore *folder_store,
 
        if (err || canceled) {
                /* Try to continue anyway */
-       } else {
+       } else if (info->poke_all) {
+               /* We're not getting INBOX children if we don't want to poke all */
                TnyIterator *iter = tny_list_create_iterator (list);
                while (!tny_iterator_is_done (iter)) {
                        TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
@@ -1486,6 +1495,7 @@ void
 modest_mail_operation_update_account (ModestMailOperation *self,
                                      const gchar *account_name,
                                      gboolean poke_all,
+                                     gboolean interactive,
                                      RetrieveAllCallback retrieve_all_cb,
                                      UpdateAccountCallback callback,
                                      gpointer user_data)
@@ -1493,7 +1503,6 @@ modest_mail_operation_update_account (ModestMailOperation *self,
        UpdateAccountInfo *info = NULL;
        ModestMailOperationPrivate *priv = NULL;
        ModestTnyAccountStore *account_store = NULL;
-       TnyStoreAccount *store_account = NULL;
        TnyList *folders;
        ModestMailOperationState *state;
 
@@ -1506,11 +1515,28 @@ modest_mail_operation_update_account (ModestMailOperation *self,
 
        /* Get the store account */
        account_store = modest_runtime_get_account_store ();
-       store_account = (TnyStoreAccount *)
+       priv->account =
                modest_tny_account_store_get_server_account (account_store,
                                                             account_name,
                                                             TNY_ACCOUNT_TYPE_STORE);
-       priv->account = g_object_ref (store_account);
+
+       /* The above function could return NULL */
+       if (!priv->account) {
+               /* Check if the operation was a success */
+               g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
+                            MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
+                            "no account");
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+
+               /* Call the user callback */
+               if (callback)
+                       callback (self, NULL, user_data);
+
+               /* Notify about operation end */
+               modest_mail_operation_notify_end (self);
+
+               return;
+       }
 
        /* Create the helper object */
        info = g_slice_new0 (UpdateAccountInfo);
@@ -1518,6 +1544,7 @@ modest_mail_operation_update_account (ModestMailOperation *self,
        info->folders = tny_simple_list_new ();
        info->mail_op = g_object_ref (self);
        info->poke_all = poke_all;
+       info->interactive = interactive;
        info->account_name = g_strdup (account_name);
        info->callback = callback;
        info->user_data = user_data;
@@ -1538,7 +1565,7 @@ modest_mail_operation_update_account (ModestMailOperation *self,
        
        /* Get all folders and continue in the callback */ 
        folders = tny_simple_list_new ();
-       tny_folder_store_get_folders_async (TNY_FOLDER_STORE (store_account),
+       tny_folder_store_get_folders_async (TNY_FOLDER_STORE (priv->account),
                                            folders, NULL,
                                            recurse_folders_async_cb, 
                                            NULL, info);
@@ -1705,7 +1732,6 @@ modest_mail_operation_remove_folder (ModestMailOperation *self,
                                     TnyFolder           *folder,
                                     gboolean             remove_to_trash)
 {
-       TnyAccount *account;
        ModestMailOperationPrivate *priv;
        ModestTnyFolderRules rules;
 
@@ -1726,14 +1752,13 @@ modest_mail_operation_remove_folder (ModestMailOperation *self,
        }
 
        /* Get the account */
-       account = modest_tny_folder_get_account (folder);
-       priv->account = g_object_ref(account);
+       priv->account = modest_tny_folder_get_account (folder);
        priv->op_type = MODEST_MAIL_OPERATION_TYPE_DELETE;
 
        /* Delete folder or move to trash */
        if (remove_to_trash) {
                TnyFolder *trash_folder = NULL;
-               trash_folder = modest_tny_account_get_special_folder (account,
+               trash_folder = modest_tny_account_get_special_folder (priv->account,
                                                                      TNY_FOLDER_TYPE_TRASH);
                /* TODO: error_handling */
                if (trash_folder) {
@@ -1759,7 +1784,6 @@ modest_mail_operation_remove_folder (ModestMailOperation *self,
                } else
                        g_warning ("%s: could not get parent folder", __FUNCTION__);
        }
-       g_object_unref (G_OBJECT (account));
 
  end:
        /* Notify about operation end */
@@ -2158,8 +2182,11 @@ get_msg_async_cb (TnyFolder *folder,
                finished = (priv->done == priv->total) ? TRUE : FALSE;
        }
 
-       /* Check errors */
-       if (canceled || err) {
+       /* If canceled by the user, ignore the error given by Tinymail */
+       if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED) {
+               canceled = TRUE;
+               finished = TRUE;
+       } else if (canceled || err) {
                priv->status = MODEST_MAIL_OPERATION_STATUS_FINISHED_WITH_ERRORS;
                if (err) {
                        priv->error = g_error_copy ((const GError *) err);
@@ -2173,9 +2200,6 @@ get_msg_async_cb (TnyFolder *folder,
        } else if (finished && priv->status == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS) {
                /* Set the success status before calling the user callback */
                priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
-       } else if (priv->status == MODEST_MAIL_OPERATION_STATUS_CANCELED) {
-               canceled = TRUE;
-               finished = TRUE;
        }
 
 
@@ -2226,16 +2250,29 @@ modest_mail_operation_get_msgs_full (ModestMailOperation *self,
        ModestMailOperationPrivate *priv = NULL;
        gint msg_list_size;
        TnyIterator *iter = NULL;
+       gboolean has_uncached_messages;
        
        g_return_if_fail (MODEST_IS_MAIL_OPERATION (self));
 
        /* Init mail operation */
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
        priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS;
-       priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE;
        priv->done = 0;
        priv->total = tny_list_get_length(header_list);
 
+       /* Check uncached messages */
+       for (iter = tny_list_create_iterator (header_list), has_uncached_messages = FALSE;
+            !has_uncached_messages && !tny_iterator_is_done (iter); 
+            tny_iterator_next (iter)) {
+               TnyHeader *header;
+
+               header = (TnyHeader *) tny_iterator_get_current (iter);
+               if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_CACHED))
+                       has_uncached_messages = TRUE;
+               g_object_unref (header);
+       }       
+       priv->op_type = has_uncached_messages?MODEST_MAIL_OPERATION_TYPE_RECEIVE:MODEST_MAIL_OPERATION_TYPE_OPEN;
+
        /* Get account and set it into mail_operation */
        if (tny_list_get_length (header_list) >= 1) {
                TnyIterator *iterator = tny_list_create_iterator (header_list);
@@ -2695,6 +2732,7 @@ modest_mail_operation_xfer_msgs (ModestMailOperation *self,
        TnyHeader *header = NULL;
        ModestTnyFolderRules rules = 0;
        TnyAccount *dst_account = NULL;
+       gboolean leave_on_server;
 
        g_return_if_fail (self && MODEST_IS_MAIL_OPERATION (self));
        g_return_if_fail (headers && TNY_IS_LIST (headers));
@@ -2786,11 +2824,28 @@ modest_mail_operation_xfer_msgs (ModestMailOperation *self,
                g_object_unref (hdr);
        }
 
+       /* If leave_on_server is set to TRUE then don't use
+          delete_original, we always pass FALSE. This is because
+          otherwise tinymail will try to sync the source folder and
+          this could cause an error if we're offline while
+          transferring an already downloaded message from a POP
+          account */
+        if (modest_protocol_info_get_transport_store_protocol (tny_account_get_proto (priv->account)) == 
+           MODEST_PROTOCOL_STORE_POP) {
+               const gchar *account_name;
+
+               account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (priv->account);
+               leave_on_server = modest_account_mgr_get_leave_on_server (modest_runtime_get_account_mgr (),
+                                                                         account_name);
+       } else {
+               leave_on_server = FALSE;
+       }
+
        modest_mail_operation_notify_start (self);
        tny_folder_transfer_msgs_async (src_folder, 
                                        helper->headers, 
                                        folder, 
-                                       delete_original, 
+                                       (leave_on_server) ? FALSE : delete_original, 
                                        transfer_msgs_cb, 
                                        transfer_msgs_status_cb,
                                        helper);
@@ -2904,9 +2959,6 @@ modest_mail_operation_refresh_folder  (ModestMailOperation *self,
        helper->user_callback = user_callback;
        helper->user_data = user_data;
 
-       /* Refresh the folder. TODO: tinymail could issue a status
-          updates before the callback call then this could happen. We
-          must review the design */
        modest_mail_operation_notify_start (self);
        
        /* notify that the operation was started */
@@ -2916,6 +2968,8 @@ modest_mail_operation_refresh_folder  (ModestMailOperation *self,
        state->total = 0;
        g_signal_emit (G_OBJECT (self), signals[PROGRESS_CHANGED_SIGNAL], 
                        0, state, NULL);
+
+       /* FIXME: we're leaking the state here, or? valgrind thinks so */
        
        tny_folder_refresh_async (folder,
                                  on_refresh_folder,