* src/modest-mail-operation.c:
[modest] / src / modest-mail-operation.c
index 7aa6282..1f49f12 100644 (file)
@@ -1222,7 +1222,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
        ModestAccountMgr *mgr;
        ModestAccountRetrieveType retrieve_type;
        TnyList *new_headers = NULL;
-       gboolean headers_only, ignore_limit;
+       gboolean headers_only, ignore_limit, succeeded;
        TnyTransportAccount *transport_account = NULL;
 
        info = (UpdateAccountInfo *) user_data;
@@ -1264,6 +1264,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;
@@ -1274,7 +1276,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 */
@@ -1333,6 +1335,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 *)
@@ -1358,9 +1367,6 @@ inbox_refreshed_cb (TnyFolder *inbox,
                }
                
                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));
 
@@ -1404,7 +1410,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);
@@ -1508,6 +1515,24 @@ modest_mail_operation_update_account (ModestMailOperation *self,
                                                             account_name,
                                                             TNY_ACCOUNT_TYPE_STORE);
 
+       /* 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);
        info->pending_calls = 1;
@@ -2151,8 +2176,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);
@@ -2166,9 +2194,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;
        }
 
 
@@ -2219,16 +2244,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);
@@ -2915,9 +2953,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 */