* src/modest-account-mgr-helpers.c:
[modest] / src / modest-mail-operation.c
index cea772e..b441d2a 100644 (file)
@@ -1223,7 +1223,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
        ModestAccountRetrieveType retrieve_type;
        TnyList *new_headers = NULL;
        gboolean headers_only, ignore_limit;
-       TnyTransportAccount *transport_account;
+       TnyTransportAccount *transport_account = NULL;
 
        info = (UpdateAccountInfo *) user_data;
        priv = MODEST_MAIL_OPERATION_GET_PRIVATE (info->mail_op);
@@ -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;
@@ -1345,6 +1347,7 @@ 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));
@@ -1366,7 +1369,7 @@ inbox_refreshed_cb (TnyFolder *inbox,
                        /* Try to send */
                        tny_camel_send_queue_flush (TNY_CAMEL_SEND_QUEUE (send_queue));
                }
-       } 
+       }
 
        /* Check if the operation was a success */
        if (!priv->error)
@@ -1490,7 +1493,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;
 
@@ -1503,11 +1505,10 @@ 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);
 
        /* Create the helper object */
        info = g_slice_new0 (UpdateAccountInfo);
@@ -1535,7 +1536,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);
@@ -1702,7 +1703,6 @@ modest_mail_operation_remove_folder (ModestMailOperation *self,
                                     TnyFolder           *folder,
                                     gboolean             remove_to_trash)
 {
-       TnyAccount *account;
        ModestMailOperationPrivate *priv;
        ModestTnyFolderRules rules;
 
@@ -1723,14 +1723,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) {
@@ -1756,7 +1755,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 */
@@ -2155,8 +2153,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);
@@ -2170,9 +2171,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;
        }
 
 
@@ -2830,6 +2828,19 @@ on_refresh_folder (TnyFolder   *folder,
 
        g_return_if_fail(priv!=NULL);
 
+       /* If the folder is remote, set the "Last updated" value */
+       if (modest_tny_folder_is_remote_folder (folder)) {
+               TnyAccount *account = modest_tny_folder_get_account (folder);
+               ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
+               const gchar *name;
+               name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+               modest_account_mgr_set_last_updated (mgr, tny_account_get_id (account), time (NULL));
+               if (!cancelled && !error)
+                       modest_account_mgr_set_server_account_username_has_succeeded (mgr, tny_account_get_id (account), TRUE);
+               modest_account_mgr_set_account_busy (mgr, name, FALSE);
+               g_object_unref (account);
+       }
+
        if (error) {
                priv->error = g_error_copy (error);
                priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
@@ -2922,6 +2933,12 @@ modest_mail_operation_refresh_folder  (ModestMailOperation *self,
        /* Refresh the folder. TODO: tinymail could issue a status
           updates before the callback call then this could happen. We
           must review the design */
+       if (modest_tny_folder_is_remote_folder (folder)) {
+               /* If the folder is remote, mark its account as busy */
+               const gchar *name;
+               name = modest_tny_account_get_parent_modest_account_name_for_server_account (priv->account);
+               modest_account_mgr_set_account_busy (modest_runtime_get_account_mgr (), name, TRUE);
+       }
        modest_mail_operation_notify_start (self);
        
        /* notify that the operation was started */