* speed up modest_account_mgr_has_accounts by caching the status
[modest] / src / modest-tny-account.c
index 4e18eff..d1695de 100644 (file)
 #include <tny-simple-list.h>
 #include <modest-tny-folder.h>
 #include <modest-tny-outbox-account.h>
+#include <modest-transport-account-decorator.h>
 #include <modest-account-mgr-helpers.h>
 #include <modest-init.h>
 #include <tny-camel-transport-account.h>
 #include <tny-camel-imap-store-account.h>
 #include <tny-camel-pop-store-account.h>
 #include <tny-folder-stats.h>
+#include <modest-debug.h>
 #include <string.h>
 #ifdef MODEST_HAVE_HILDON0_WIDGETS
 #include <hildon-widgets/hildon-file-system-info.h>
 #include <hildon/hildon-file-system-info.h>
 #endif
 
+/* we need these dummy functions, or tinymail will complain */
+static gchar *  get_pass_dummy     (TnyAccount *account, const gchar *prompt, gboolean *cancel);
+static void     forget_pass_dummy  (TnyAccount *account);
+
 TnyFolder *
 modest_tny_account_get_special_folder (TnyAccount *account,
                                       TnyFolderType special_type)
 {
-       TnyList *folders;
-       TnyIterator *iter;
+       TnyList *folders = NULL;
+       TnyIterator *iter = NULL;
        TnyFolder *special_folder = NULL;
-
+       TnyAccount *local_account  = NULL;
+       GError *error = NULL;
        
        g_return_val_if_fail (account, NULL);
        g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM,
                              NULL);
        
-       TnyAccount *local_account  = NULL;
-               
        /* The accounts have already been instantiated by 
         * modest_tny_account_store_get_accounts(), which is the 
         * TnyAccountStore::get_accounts_func() implementation,
@@ -72,134 +77,80 @@ modest_tny_account_get_special_folder (TnyAccount *account,
         
        /* Per-account outbox folders are each in their own on-disk directory: */
        if (special_type == TNY_FOLDER_TYPE_OUTBOX) {
-               const gchar *modest_account_name = 
+
+               gchar *account_id;
+               const gchar *modest_account_name;
+
+               modest_account_name =
                        modest_tny_account_get_parent_modest_account_name_for_server_account (account);
-               
-               if (modest_account_name) {
-                       gchar *account_id = g_strdup_printf (
-                               MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s", 
-                               modest_account_name);
-                       
-                       local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
-                                                                                    MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
-                                                                                    account_id);
-                       if (!local_account) {
-                               g_printerr ("modest: %s: modest_tny_account_store_get_tny_account_by(ID) returned NULL for %s\n", __FUNCTION__, account_id);
+               if (!modest_account_name) {
+                       g_warning ("%s: could not get modest account name", __FUNCTION__);
                        return NULL;
-                       }
-               
-                       g_free (account_id);
-               } else {
-                       g_warning ("%s: modest_account_name was NULL.", __FUNCTION__);
                }
-       } else {
+               
+               account_id = g_strdup_printf (
+                       MODEST_PER_ACCOUNT_LOCAL_OUTBOX_FOLDER_ACCOUNT_ID_PREFIX "%s", 
+                       modest_account_name);
+               
+               local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
+                                                                            MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
+                                                                            account_id);
+               g_free (account_id);            
+       } else 
                /* Other local folders are all in one on-disk directory: */
                local_account = modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
                                                                             MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
                                                                             MODEST_LOCAL_FOLDERS_ACCOUNT_ID);
-       }
-       
        if (!local_account) {
                g_printerr ("modest: cannot get local account\n");
-               return NULL;
+               goto cleanup;
        }
 
        folders = TNY_LIST (tny_simple_list_new ());
-
+       
        /* There is no need to do this _async, as these are local folders. */
        /* TODO: However, this seems to fail sometimes when the network is busy, 
         * returning an empty list. murrayc. */ 
-       GError *error = NULL;
-       tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account),
-                                     folders, NULL, &error);
+       tny_folder_store_get_folders (TNY_FOLDER_STORE (local_account), folders, NULL, &error);
        if (error) {
-               g_warning ("%s: tny_folder_store_get_folders() failed:\n  error=%s\n", 
-                       __FUNCTION__, error->message);
+               g_warning ("%s: tny_folder_store_get_folders() failed:%s\n", __FUNCTION__, error->message);
+               g_error_free (error);
+               goto cleanup;
        }
                                      
        if (tny_list_get_length (folders) == 0) {
                gchar* url_string = tny_account_get_url_string (local_account);
-               g_printerr ("modest: %s: tny_folder_store_get_folders() returned an empty list for account with URL '%s'\n", 
-                       __FUNCTION__, url_string);
+               g_printerr ("modest: %s: tny_folder_store_get_folders(%s) returned an empty list\n", 
+                           __FUNCTION__, url_string);
                g_free (url_string);
+               goto cleanup;
        }
        
        iter = tny_list_create_iterator (folders);
 
        while (!tny_iterator_is_done (iter)) {
-               TnyFolder *folder =
-                       TNY_FOLDER (tny_iterator_get_current (iter));
+               TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
                if (folder) {
                        if (modest_tny_folder_get_local_or_mmc_folder_type (folder) == special_type) {
                                special_folder = folder;
                                break; /* Leaving a ref for the special_folder return value. */
                        }
-               
-                       g_object_unref (G_OBJECT(folder));
+                       g_object_unref (folder);
                }
-
                tny_iterator_next (iter);
        }
-       
-       g_object_unref (G_OBJECT (folders));
-       g_object_unref (G_OBJECT (iter));
-       g_object_unref (G_OBJECT (local_account));
 
-       /*
-       if (!special_folder) {
-               g_warning ("%s: Returning NULL.", __FUNCTION__);        
-       }
-       */
+cleanup:
+       if (folders)
+               g_object_unref (folders);
+       if (iter)
+               g_object_unref (iter);
+       if (local_account)
+               g_object_unref (local_account);
        
        return special_folder;
 }
 
-static void
-on_connection_status_changed (TnyAccount *account, TnyConnectionStatus status, gpointer user_data)
-{
-       printf ("DEBUG: %s: status=%d\n", __FUNCTION__, status);
-       
-       if (status == TNY_CONNECTION_STATUS_DISCONNECTED) {
-               /* A tinymail network operation failed, and tinymail then noticed that 
-                * the account is offline, because our TnyDevice is offline,
-                * because libconic says we are offline.
-                * So ask the user to go online again.
-                * 
-                * Note that this signal will not be emitted if the account was offline 
-                * when the network operation was first attempted. For those cases, 
-                * the application must do its own explicit checks.
-                *
-                * We make sure that this UI is shown in the main thread, to avoid races,
-                * because tinymail does not guarantee that this signal handler will be called 
-                * in the main thread.
-                */
-               /* TODO: Commented out, because this causes hangs, probably related to 
-                * our use of mainloops:
-                * modest_platform_connect_and_wait (NULL);
-                */
-       } else if (status == TNY_CONNECTION_STATUS_CONNECTED_BROKEN) {
-               printf ("DEBUG: %s: Connection broken. Forcing TnyDevice offline.\n", 
-                       __FUNCTION__);
-                       
-               /* Something went wrong during some network operation.
-                * Stop trying to use the network now,
-                * by forcing accounts into offline mode:
-                * 
-                * When libconic reconnects, it will set the device back online again,
-                * regardless of it being forced offline before.
-                */
-               /* TODO: Find out when this is falsely being emitted. */
-               printf ("  DEBUG: %s: Not forcing offline because tinymail is sometimes reporting false connection breaks.\n", 
-                       __FUNCTION__);
-               /*
-               TnyDevice *device = modest_runtime_get_device ();
-               tny_device_force_offline (device);
-               */
-       }
-}
-
-
-
 /**
  * create_tny_account:
  * @account_mgr: a valid account mgr instance
@@ -216,23 +167,19 @@ create_tny_account (ModestAccountMgr *account_mgr,
                    TnySessionCamel *session,
                    ModestServerAccountData *account_data)
 {
+       TnyAccount *tny_account = NULL;
+       const gchar* proto_name;
+       
        g_return_val_if_fail (account_mgr, NULL);
        g_return_val_if_fail (session, NULL);
        g_return_val_if_fail (account_data, NULL);
-
-       /* sanity checks */
-       if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN) {
-               g_printerr ("modest: '%s' does not provide a protocol\n",
-                           account_data->account_name);
-               return NULL;
-       }
-
-       TnyAccount *tny_account = NULL;
+       g_return_val_if_fail (account_data->proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN,
+                             NULL);
        
        switch (account_data->proto) {
        case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
        case MODEST_PROTOCOL_TRANSPORT_SMTP:
-               tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
+               tny_account = TNY_ACCOUNT (modest_transport_account_decorator_new ()); break;
        case MODEST_PROTOCOL_STORE_POP:
                tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
        case MODEST_PROTOCOL_STORE_IMAP:
@@ -246,9 +193,10 @@ create_tny_account (ModestAccountMgr *account_mgr,
        default:
                g_return_val_if_reached (NULL);
        }
+
        if (!tny_account) {
-               g_printerr ("modest: could not create tny account for '%s'\n",
-                           account_data->account_name);
+               g_printerr ("modest: %s: could not create tny account for '%s'\n",
+                           __FUNCTION__, account_data->account_name);
                return NULL;
        }
        tny_account_set_id (tny_account, account_data->account_name);
@@ -256,14 +204,8 @@ create_tny_account (ModestAccountMgr *account_mgr,
        /* This must be set quite early, or other set() functions will fail. */
        tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (tny_account), session);
     
-       /* Handle connection requests:
-        * This (badly-named) signal will be called when we try to use an offline account. */
-       g_signal_connect (G_OBJECT (tny_account), "connection-status-changed",
-                       G_CALLBACK (on_connection_status_changed), NULL);
-
        /* Proto */
-       const gchar* proto_name =
-               modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
+       proto_name =  modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
        tny_account_set_proto (tny_account, proto_name);
 
        return tny_account;
@@ -323,24 +265,25 @@ static gboolean
 update_tny_account (TnyAccount *tny_account, ModestAccountMgr *account_mgr,
                    ModestServerAccountData *account_data)
 {
-       gchar *url = NULL;
-       
        g_return_val_if_fail (account_mgr, FALSE);
        g_return_val_if_fail (account_data, FALSE);
+       g_return_val_if_fail (account_data->account_name, FALSE);
        g_return_val_if_fail (tny_account, FALSE);
-
+       
        tny_account_set_id (tny_account, account_data->account_name);
-              
+       
        /* mbox and maildir accounts use a URI instead of the rest:
         * Note that this is not where we create the special local folders account.
         * We do that in modest_tny_account_new_for_local_folders() instead. */
        if (account_data->uri)  
                tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri);
        else {
-               /* Set camel-specific options: */
-               
+               /* Set camel-specific options: */               
                /* Enable secure connection settings: */
                const gchar* option_security = NULL;
+               const gchar* auth_mech_name = NULL;
+
+
                switch (account_data->security) {
                case MODEST_PROTOCOL_CONNECTION_NORMAL:
                        option_security = MODEST_ACCOUNT_OPTION_SSL "=" MODEST_ACCOUNT_OPTION_SSL_NEVER;
@@ -366,8 +309,6 @@ update_tny_account (TnyAccount *tny_account, ModestAccountMgr *account_mgr,
                                                      option_security);
                
                /* Secure authentication: */
-
-               const gchar* auth_mech_name = NULL;
                switch (account_data->secure_auth) {
                case MODEST_PROTOCOL_AUTH_NONE:
                        /* IMAP and POP need at least a password,
@@ -400,7 +341,7 @@ update_tny_account (TnyAccount *tny_account, ModestAccountMgr *account_mgr,
                        
                default:
                        g_warning ("%s: Unhandled secure authentication setting %d for "
-                               "account=%s (%s)", __FUNCTION__, account_data->secure_auth,
+                                  "account_name=%s (%s)", __FUNCTION__, account_data->secure_auth,
                                   account_data->account_name, account_data->hostname);
                        break;
                }
@@ -427,19 +368,21 @@ update_tny_account (TnyAccount *tny_account, ModestAccountMgr *account_mgr,
                        tny_account_set_port (tny_account, account_data->port);
        }
 
-       /* FIXME: for debugging. 
-        * Let's keep this because it is very useful for debugging. */
-       url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
-       g_debug ("%s:\n  account-url: %s\n", __FUNCTION__, url);
-
-       g_free (url);
+       MODEST_DEBUG_BLOCK (
+               gchar *url = tny_account_get_url_string (TNY_ACCOUNT(tny_account));
+               g_debug ("%s:\n  account-url: %s\n", __FUNCTION__, url);
+               g_free (url);
+       );
+       
        return TRUE;
 }
 
 TnyAccount*
 modest_tny_account_new_from_server_account_name (ModestAccountMgr *account_mgr,
                                                 TnySessionCamel *session,
-                                                const gchar *server_account_name)
+                                                const gchar *server_account_name,
+                                                TnyGetPassFunc get_pass_func,
+                                                TnyForgetPassFunc forget_pass_func)
 {
        ModestServerAccountData *account_data;
        TnyAccount *tny_account;
@@ -452,11 +395,27 @@ modest_tny_account_new_from_server_account_name (ModestAccountMgr *account_mgr,
        if (!account_data)
                return NULL;
 
-       tny_account = create_tny_account (account_mgr, session, account_data);
+       tny_account = TNY_ACCOUNT (tny_camel_transport_account_new ());
+
+       if (tny_account) {
+               const gchar* proto_name = NULL;
+               tny_account_set_id (tny_account, account_data->account_name);
+               tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (tny_account), session);
+               proto_name = modest_protocol_info_get_transport_store_protocol_name(account_data->proto);
+               tny_account_set_proto (tny_account, proto_name);
+               modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account, server_account_name);
+       }
+
        if (!tny_account)
                g_warning ("%s: failed to create tny_account", __FUNCTION__);
        else if (!update_tny_account (tny_account, account_mgr, account_data))
                g_warning ("%s: failed to initialize tny_account", __FUNCTION__);
+       else {
+               tny_account_set_forget_pass_func (tny_account,
+                                                 forget_pass_func ? forget_pass_func : forget_pass_dummy);
+               tny_account_set_pass_func (tny_account,
+                                          get_pass_func ? get_pass_func: get_pass_dummy);
+       }
        
        modest_account_mgr_free_server_account_data (account_mgr, account_data);
        
@@ -465,80 +424,6 @@ modest_tny_account_new_from_server_account_name (ModestAccountMgr *account_mgr,
 
 
 
-gboolean
-modest_tny_account_update_from_server_account_name (TnyAccount *tny_account,
-                                                   ModestAccountMgr *account_mgr,
-                                                   const gchar *server_account_name)
-{
-       ModestServerAccountData *account_data;
-       gboolean valid_account_type;
-       
-       g_return_val_if_fail (tny_account, FALSE);
-       g_return_val_if_fail (server_account_name, FALSE);
-       
-       account_data =  modest_account_mgr_get_server_account_data (account_mgr, 
-                                                                   server_account_name);
-       if (!account_data) {
-               g_warning ("%s: failed to get server account data for %s",
-                          __FUNCTION__, server_account_name);
-               return FALSE;
-       }
-
-       valid_account_type = FALSE;
-
-       /* you cannot change the protocol type of an existing account;
-        * so double check we don't even try
-        */
-       switch (account_data->proto) {
-       case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
-       case MODEST_PROTOCOL_TRANSPORT_SMTP:
-               if (!TNY_IS_CAMEL_TRANSPORT_ACCOUNT(tny_account))
-                       g_warning ("%s: expecting transport account", __FUNCTION__);
-               else
-                       valid_account_type = TRUE;
-               break;
-       case MODEST_PROTOCOL_STORE_POP:
-               if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT(tny_account))
-                       g_warning ("%s: expecting pop account", __FUNCTION__);
-               else
-                       valid_account_type = TRUE;
-               break;
-       case MODEST_PROTOCOL_STORE_IMAP:
-               if (!TNY_IS_CAMEL_IMAP_STORE_ACCOUNT(tny_account))
-                       g_warning ("%s: expecting imap account", __FUNCTION__);
-               else
-                       valid_account_type = TRUE;
-               break;
-       case MODEST_PROTOCOL_STORE_MAILDIR:
-       case MODEST_PROTOCOL_STORE_MBOX:
-               if (!TNY_IS_CAMEL_STORE_ACCOUNT(tny_account))
-                       g_warning ("%s: expecting store account", __FUNCTION__);
-               else
-                       valid_account_type = TRUE;
-               break;
-       default:
-               g_warning ("invalid account type");
-       }
-
-       if (!valid_account_type) {
-               g_warning ("%s: protocol type cannot be changed", __FUNCTION__);
-               modest_account_mgr_free_server_account_data (account_mgr, account_data);
-               return FALSE;
-       }
-       
-       if (!update_tny_account (tny_account, account_mgr, account_data)) {
-               g_warning ("%s: failed to update account", __FUNCTION__);
-               modest_account_mgr_free_server_account_data (account_mgr, account_data);
-               return FALSE;
-       }
-
-       modest_account_mgr_free_server_account_data (account_mgr, account_data);
-       return TRUE;
-}
-
-
-
-
 
 /* we need these dummy functions, or tinymail will complain */
 static gchar*
@@ -553,20 +438,32 @@ forget_pass_dummy (TnyAccount *account)
 }
 
 
+static void
+set_online_callback (TnyCamelAccount *account, gboolean canceled, GError *err, gpointer user_data)
+{
+       /* MODEST TODO: Show a real error message here, this is a significant error!
+        * Perhaps show the account's settings dialog again?! Reconnecting after 
+        * changing the settings of an account failed in this situation. */
+
+       if (err && !canceled)
+               g_warning ("err: %s", err->message);
+}
 
 gboolean
-modest_tny_account_update_from_account (TnyAccount *tny_account, ModestAccountMgr *account_mgr,
-                                       const gchar *account_name, TnyAccountType type) 
+modest_tny_account_update_from_account (TnyAccount *tny_account) 
 {
        ModestAccountData *account_data = NULL;
        ModestServerAccountData *server_data = NULL;
-       
+       TnyConnectionStatus conn_status;
+       ModestAccountMgr *account_mgr;
+       const gchar *account_name;
+       TnyAccountType type;
+
        g_return_val_if_fail (tny_account, FALSE);
-       g_return_val_if_fail (account_mgr, FALSE);
-       g_return_val_if_fail (account_name, FALSE);
-       g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT,
-                             FALSE);
 
+       account_mgr = modest_runtime_get_account_mgr ();
+       account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (tny_account);
+       type = tny_account_get_account_type (tny_account);
        account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
        if (!account_data) {
                g_printerr ("modest: %s: cannot get account data for account %s\n",
@@ -596,6 +493,26 @@ modest_tny_account_update_from_account (TnyAccount *tny_account, ModestAccountMg
 
        modest_account_mgr_free_account_data (account_mgr, account_data);
 
+       /* If the account was online, reconnect to apply the changes */
+       conn_status = tny_account_get_connection_status (tny_account);
+       if (conn_status != TNY_CONNECTION_STATUS_DISCONNECTED) {
+               TnyAccountStore *account_store = NULL;
+
+               /* The callback will have an error for you if the reconnect
+                * failed. Please handle it (this is TODO). */
+
+               account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(tny_account),
+                                                            "account_store"));
+
+               if (account_store) {
+                       modest_tny_account_store_forget_already_asked (MODEST_TNY_ACCOUNT_STORE (account_store), 
+                                                               tny_account);
+               }
+
+               tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(tny_account), TRUE, 
+                       set_online_callback,  "online");
+       }
+
        return TRUE;
 }
 
@@ -678,9 +595,9 @@ typedef struct
 
 /* Gets the memory card name: */
 static void 
-on_modest_file_system_info(HildonFileSystemInfoHandle *handle,
-                             HildonFileSystemInfo *info,
-                             const GError *error, gpointer data)
+on_modest_file_system_info (HildonFileSystemInfoHandle *handle,
+                           HildonFileSystemInfo *info,
+                           const GError *error, gpointer data)
 {
        GetMmcAccountNameData *callback_data = (GetMmcAccountNameData*)data;
 
@@ -699,7 +616,10 @@ on_modest_file_system_info(HildonFileSystemInfoHandle *handle,
        }
                 
        /* printf ("DEBUG: %s: display name=%s\n", __FUNCTION__,  display_name); */
-       tny_account_set_name (account, display_name);
+       if (display_name && previous_display_name && 
+               (strcmp (display_name, previous_display_name) != 0)) {
+               tny_account_set_name (account, display_name);
+       }
                
        /* Inform the application that the name is now ready: */
        if (callback_data->callback)
@@ -980,7 +900,6 @@ modest_tny_folder_store_get_folder_count (TnyFolderStore *self)
        /* Create helper */
        helper = g_malloc0 (sizeof (RecurseFoldersHelper));
        helper->task = TASK_GET_FOLDER_COUNT;
-       helper->sum = 0;
        helper->folders = 0;
 
        recurse_folders (self, NULL, helper);
@@ -1003,7 +922,8 @@ modest_tny_folder_store_get_message_count (TnyFolderStore *self)
        /* Create helper */
        helper = g_malloc0 (sizeof (RecurseFoldersHelper));
        helper->task = TASK_GET_ALL_COUNT;
-       helper->sum = 0;
+       if (TNY_IS_FOLDER (self))
+               helper->sum = tny_folder_get_all_count (TNY_FOLDER (self));
 
        recurse_folders (self, NULL, helper);
 
@@ -1025,7 +945,8 @@ modest_tny_folder_store_get_local_size (TnyFolderStore *self)
        /* Create helper */
        helper = g_malloc0 (sizeof (RecurseFoldersHelper));
        helper->task = TASK_GET_LOCAL_SIZE;
-       helper->sum = 0;
+       if (TNY_IS_FOLDER (self))
+               helper->sum = tny_folder_get_local_size (TNY_FOLDER (self));
 
        recurse_folders (self, NULL, helper);