* updated with dbus debugging info
[modest] / src / modest-tny-account-store.c
index 7a925af..99cbaf7 100644 (file)
@@ -103,8 +103,8 @@ static void    on_vfs_volume_unmounted     (GnomeVFSVolumeMonitor *volume_monito
                                            GnomeVFSVolume *volume, 
                                            gpointer user_data);
 
-static void    modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, 
-                                                                  const gchar *server_account_name);
+static void    forget_password_in_memory (ModestTnyAccountStore *self, 
+                                         const gchar *server_account_name);
 
 static void    add_connection_specific_transport_accounts         (ModestTnyAccountStore *self);
 
@@ -300,7 +300,9 @@ static void
 account_disconnect (TnyAccount *account)
 {
        g_return_if_fail (account && TNY_IS_ACCOUNT(account));
-       tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(account), FALSE, NULL, NULL);
+
+       if (modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
+               tny_camel_account_set_online (TNY_CAMEL_ACCOUNT(account), FALSE, NULL, NULL);
 }
 
 
@@ -434,7 +436,7 @@ on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
 }
 
 /**
- * modest_tny_account_store_forget_password_in_memory
+ * forget_password_in_memory
  * @self: a TnyAccountStore instance
  * @account: A server account.
  * 
@@ -442,9 +444,9 @@ on_vfs_volume_unmounted(GnomeVFSVolumeMonitor *volume_monitor,
  * For instance, this should be called when the user has changed the password in the account settings.
  */
 static void
-modest_tny_account_store_forget_password_in_memory (ModestTnyAccountStore *self, const gchar * server_account_name)
+forget_password_in_memory (ModestTnyAccountStore *self, 
+                          const gchar * server_account_name)
 {
-       /* printf ("DEBUG: %s\n", __FUNCTION__); */
        ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
 
        if (server_account_name && priv->password_hash) {
@@ -501,17 +503,14 @@ on_account_settings_hide (GtkWidget *widget, gpointer user_data)
 }
 
 static void 
-show_password_warning_only ()
+show_password_warning_only (const gchar *msg)
 {
        ModestWindow *main_window = 
                modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE); /* don't create */
        
        /* Show an explanatory temporary banner: */
        if (main_window) 
-               modest_platform_information_banner (NULL, NULL, 
-                                                   _("mcen_ib_username_pw_incorrect"));
-       else
-               g_warning ("%s: %s", __FUNCTION__, _("mcen_ib_username_pw_incorrect"));
+               modest_platform_information_banner (NULL, NULL, msg);
 }
 
 static void 
@@ -550,29 +549,47 @@ show_wrong_password_dialog (TnyAccount *account)
 static gchar*
 get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *cancel)
 {
-       const TnyAccountStore *account_store = NULL;
        ModestTnyAccountStore *self = NULL;
        ModestTnyAccountStorePrivate *priv;
        gchar *username = NULL;
        gchar *pwd = NULL;
        gpointer pwd_ptr = NULL;
        gboolean already_asked = FALSE;
+       const gchar *server_account_name;
+       gchar *url_string;
 
        g_return_val_if_fail (account, NULL);
        
        MODEST_DEBUG_BLOCK(
                g_debug ("%s: prompt (not shown) = %s\n", __FUNCTION__, prompt_not_used);
-       );
-       
-       /* Initialize the output parameter: */
-       if (cancel)
-               *cancel = FALSE;
-               
-       const gchar *server_account_name = tny_account_get_id (account);
-       account_store = TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
-                                                            "account_store"));
+       );              
+
+       /* Get a reference to myself */
+       self = MODEST_TNY_ACCOUNT_STORE (g_object_get_data (G_OBJECT(account), "account_store"));
+       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+       /* Ensure that we still have this account. It could happen
+          that a set_online was requested *before* removing an
+          account, and due to tinymail emits the get_password
+          function using a g_idle the account could be actually
+          removed *before* this function was really called */
+       url_string = tny_account_get_url_string (account);
+       if (url_string) {
+               TnyAccount *tmp_account;
+
+               tmp_account = tny_account_store_find_account (TNY_ACCOUNT_STORE (self), 
+                                                             url_string);
+               g_free (url_string);
+
+               if (!tmp_account) {
+                       *cancel = TRUE;
+                       return NULL;
+               }
+               g_object_unref (tmp_account);
+       }
 
-       if (!server_account_name || !account_store) {
+       server_account_name = tny_account_get_id (account);
+       if (!server_account_name || !self) {
                g_warning ("modest: %s: could not retrieve account_store for account %s",
                           __FUNCTION__, server_account_name ? server_account_name : "<NULL>");
                if (cancel)
@@ -580,9 +597,6 @@ get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *canc
                
                return NULL;
        }
-
-       self = MODEST_TNY_ACCOUNT_STORE (account_store);
-       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
        
        /* This hash map stores passwords, including passwords that are not stored in gconf. */
        /* Is it in the hash? if it's already there, it must be wrong... */
@@ -662,12 +676,20 @@ get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *canc
                pwd = NULL;
                
                if (already_asked) {
-                       /* Show an info banner, before we show the protected password dialog: */
-                       show_password_warning_only();
+                       const gchar *msg;
+                       gboolean username_known = 
+                               modest_account_mgr_get_server_account_username_has_succeeded(priv->account_mgr, 
+                                                                                            server_account_name);
+                       /* If the login has ever succeeded then show a specific message */
+                       if (username_known)
+                               msg = _("ecdg_ib_set_password_incorrect");
+                       else
+                               msg = _("mcen_ib_username_pw_incorrect");
+                       show_password_warning_only (msg);
                }
 
                /* Request password */
-               g_signal_emit (G_OBJECT(account_store), signals[PASSWORD_REQUESTED_SIGNAL], 0,
+               g_signal_emit (G_OBJECT (self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
                               account_id, /* server_account_name */
                               &username, &pwd, cancel, &remember);
 
@@ -1507,6 +1529,10 @@ connection_status_changed (TnyAccount *account,
                           TnyConnectionStatus status, 
                           gpointer data)
 {
+       /* We do this here and not in the connection policy because we
+          don't want to do it for every account, just for the
+          accounts that are interactively added when modest is
+          runnning */
        if (status == TNY_CONNECTION_STATUS_CONNECTED) {
                const gchar *account_name;
                ModestWindow *main_window;
@@ -1519,11 +1545,6 @@ connection_status_changed (TnyAccount *account,
                                                                  G_OBJECT (account),
                                                                  "connection_status_changed");
 
-               /* Set the username as succedded */
-               modest_account_mgr_set_server_account_username_has_succeeded (modest_runtime_get_account_mgr (), 
-                                                                             tny_account_get_id (account), 
-                                                                             TRUE);
-
                /* Perform a send receive */
                account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
                main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
@@ -1551,8 +1572,7 @@ create_tny_account (ModestTnyAccountStore *self,
        if (account) {
                /* Forget any cached password for the account, so that
                   we use a new account if any */
-               modest_tny_account_store_forget_password_in_memory (self, 
-                                                                   tny_account_get_id (account));
+               forget_password_in_memory (self, tny_account_get_id (account));
 
                /* Install a signal handler that will refresh the
                   account the first time it becomes online. Do this
@@ -1716,16 +1736,6 @@ on_account_disconnect_when_removing (TnyCamelAccount *account,
                                                                  "connection_status_changed");
        }
 
-       /* Remove it from the list of accounts */
-       if (TNY_IS_STORE_ACCOUNT (account))
-               tny_list_remove (priv->store_accounts, (GObject *) account);
-       else
-               tny_list_remove (priv->transport_accounts, (GObject *) account);
-
-       /* Notify the observers */
-       g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 
-                      0, account);
-
        /* Cancel all pending operations */
        tny_account_cancel (TNY_ACCOUNT (account));
        
@@ -1758,6 +1768,15 @@ on_account_removed (ModestAccountMgr *acc_mgr,
        /* If there was any problem creating the account, for example,
           with the configuration system this could not exist */
        if (store_account) {
+               /* Forget any cached password for the account */
+               forget_password_in_memory (self, tny_account_get_id (store_account));
+
+               /* Remove it from the list of accounts and notify the
+                  observers. Do not need to wait for account
+                  disconnection */
+               tny_list_remove (priv->store_accounts, (GObject *) store_account);
+               g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
+
                /* Cancel all pending operations */
                tny_account_cancel (TNY_ACCOUNT (store_account));
 
@@ -1775,13 +1794,27 @@ on_account_removed (ModestAccountMgr *acc_mgr,
        if (transport_account) {
                TnyAccount *local_account = NULL;
                TnyFolder *outbox = NULL;
-               ModestTnyAccountStorePrivate *priv = NULL;
+
+               /* Forget any cached password for the account */
+               forget_password_in_memory (self, tny_account_get_id (store_account));
+
+               /* Remove it from the list of accounts and notify the
+                  observers. Do not need to wait for account
+                  disconnection */
+               tny_list_remove (priv->transport_accounts, (GObject *) transport_account);
+               g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
        
                /* Remove the OUTBOX of the account from the global outbox */
-               priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
                outbox = g_hash_table_lookup (priv->outbox_of_transport, transport_account);
 
                if (TNY_IS_FOLDER (outbox)) {
+                       TnyAccount *outbox_account = tny_folder_get_account (outbox);
+
+                       if (outbox_account) {
+                               tny_list_remove (priv->store_accounts_outboxes, G_OBJECT (outbox_account));
+                               g_object_unref (outbox_account);
+                       }
+
                        local_account = modest_tny_account_store_get_local_folders_account (self);
                        modest_tny_local_folders_account_remove_folder_from_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
                                                                                    outbox);