+static ModestTnyAccountStore*
+get_account_store_for_account (TnyAccount *account)
+{
+ return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
+ "account_store"));
+}
+
+/* This callback will be called by Tinymail when it needs the password
+ * from the user, for instance if the password was not remembered.
+ * Note that TnyAccount here will be the server account. */
+static gchar*
+get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
+{
+ /* Initialize the output parameter: */
+ if (cancel)
+ *cancel = FALSE;
+
+ const gchar *key;
+ const TnyAccountStore *account_store;
+ ModestTnyAccountStore *self;
+ ModestTnyAccountStorePrivate *priv;
+ gchar *username = NULL;
+ gchar *pwd = NULL;
+ gpointer pwd_ptr;
+ gboolean already_asked;
+
+ key = tny_account_get_id (account);
+ account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
+
+ 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... */
+ pwd_ptr = (gpointer)&pwd; /* pwd_ptr so the compiler does not complained about
+ * type-punned ptrs...*/
+ already_asked = g_hash_table_lookup_extended (priv->password_hash,
+ key,
+ NULL,
+ (gpointer*)&pwd_ptr);
+
+ /* if the password is not already there, try ModestConf */
+ if (!already_asked) {
+ pwd = modest_account_mgr_get_string (priv->account_mgr,
+ key, MODEST_ACCOUNT_PASSWORD, TRUE);
+ g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup (pwd));
+ }
+
+ /* if it was already asked, it must have been wrong, so ask again */
+ if (already_asked || !pwd || strlen(pwd) == 0) {
+ /* we don't have it yet. Get the password from the user */
+ const gchar* account_id = tny_account_get_id (account);
+ gboolean remember = FALSE;
+ pwd = NULL;
+
+ /* Note that we ignore the returned username here,
+ * because it is enough that it will be stored in gconf
+ * by the signal handler. */
+ g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
+ account_id, /* server_account_name */
+ &username, &pwd, cancel, &remember);
+
+ if (!*cancel) {
+ if (remember)
+ modest_account_mgr_set_string (priv->account_mgr,key,
+ MODEST_ACCOUNT_PASSWORD,
+ pwd, TRUE);
+ /* We need to dup the string even knowing that
+ it's already a dup of the contents of an
+ entry, because it if it's wrong, then camel
+ will free it */
+ g_hash_table_insert (priv->password_hash, g_strdup (key), g_strdup(pwd));
+ } else {
+ g_hash_table_remove (priv->password_hash, key);
+
+ g_free (pwd);
+ pwd = NULL;
+ }
+
+ g_free (username);
+ username = NULL;
+ } else
+ *cancel = FALSE;
+
+ /* printf(" DEBUG: %s: returning %s\n", __FUNCTION__, pwd); */
+
+ return pwd;
+}
+
+/* tinymail calls this if the connection failed due to an incorrect password.
+ * And it seems to call this for any general connection failure. */
+static void
+forget_password (TnyAccount *account)