* check_account-mgr.c:
[modest] / src / modest-tny-account-store.c
index f488124..bce0157 100644 (file)
@@ -140,8 +140,8 @@ modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET(ModestTnyAccountStoreClass, password_requested),
                              NULL, NULL,
-                             modest_marshal_VOID__STRING_POINTER_POINTER,
-                             G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER);
+                             modest_marshal_VOID__STRING_POINTER_POINTER_POINTER,
+                             G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);
        
        signals[ACCOUNT_UPDATE_SIGNAL] =
                g_signal_new ("account_update",
@@ -207,7 +207,6 @@ set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store
        g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
 }
 
-
 static gchar*
 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 {
@@ -219,7 +218,7 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
        gboolean already_asked;
        
        g_return_val_if_fail (account, NULL);
-       
+
        key           = tny_account_get_id (account);
        account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
 
@@ -227,33 +226,51 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
         priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
 
        /* is it in the hash? if it's already there, it must be wrong... */
-       already_asked = g_hash_table_lookup (priv->password_hash, key) != NULL;
+       already_asked = g_hash_table_lookup_extended (priv->password_hash, 
+                                                     key, NULL, (gpointer *) &pwd);
 
        /* if the password is not already there, try ModestConf */
-/*     if (!already_asked)  */
+       if (!already_asked) {
                pwd  = modest_account_mgr_get_string (priv->account_mgr,
                                                      key, MODEST_ACCOUNT_PASSWORD,
                                                      TRUE, NULL);
+               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. we emit a signal to get the password somewhere *\/ */
-/*             const gchar* name = tny_account_get_name (account); */
-/*             *cancel = TRUE; */
-/*             pwd     = NULL; */
-/*             g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0, */
-/*                            name, &pwd, cancel); */
-/*             if (!*cancel) /\* remember the password *\/ */
-/*                     modest_account_mgr_set_string (priv->account_mgr, */
-/*                                                    key, MODEST_ACCOUNT_PASSWORD, */
-/*                                                    pwd, TRUE, NULL); */
-/*     } else */
-/*             *cancel = FALSE; */
-
-/*     g_hash_table_insert (priv->password_hash, key, 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. we emit a signal to get the password somewhere */
+               const gchar* name = tny_account_get_name (account);
+               gboolean remember;
+               pwd = NULL;
+
+               gdk_threads_enter ();
        
-       return pwd; 
+               g_signal_emit (G_OBJECT(self), signals[PASSWORD_REQUESTED_SIGNAL], 0,
+                              name, &pwd, cancel, &remember);
+
+               if (!*cancel) {
+                       if (remember)
+                               modest_account_mgr_set_string (priv->account_mgr,
+                                                              key, MODEST_ACCOUNT_PASSWORD,
+                                                              pwd, 
+                                                              TRUE, NULL);
+                       /* 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;
+               }
+               gdk_threads_leave ();
+       } else
+               *cancel = FALSE;
+
+       return pwd;
 }
 
 
@@ -263,10 +280,26 @@ forget_password (TnyAccount *account) {
        ModestTnyAccountStore *self;
        ModestTnyAccountStorePrivate *priv;
        const TnyAccountStore *account_store;
+       gchar *pwd;
+       const gchar *key;
 
         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);
+       key  = tny_account_get_id (account);
+
+       /* Do not remove the key, this will allow us to detect that we
+          have already asked for it at least once */
+       pwd = g_hash_table_lookup (priv->password_hash, key);
+       if (pwd) {
+               memset (pwd, 0, strlen (pwd));
+               g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
+       }
+
+       /* Remove from configuration system */
+       modest_account_mgr_unset (priv->account_mgr,
+                                 key, MODEST_ACCOUNT_PASSWORD,
+                                 TRUE, NULL);
 }
 
 
@@ -280,7 +313,9 @@ tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
        TnyAccount *tny_account;
        ModestTnyAccountStorePrivate *priv;
        gchar *val;
-
+       GSList *options = NULL;
+       GError *error = NULL;
+       
        g_return_val_if_fail (self, NULL);
        g_return_val_if_fail (account, NULL);
        g_return_val_if_fail (server_account, NULL);
@@ -308,8 +343,20 @@ tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
                                       priv->tny_session_camel);
        
        /* id */
-       tny_account_set_id (tny_account, server_account);
-       tny_account_set_name (tny_account, account);
+       tny_account_set_id   (tny_account, server_account);
+
+       /* name */
+       val = modest_account_mgr_get_string (priv->account_mgr, account,
+                                            MODEST_ACCOUNT_DISPLAY_NAME, FALSE, NULL);
+       if (val) {
+               tny_account_set_name (tny_account, val);
+               g_free (val);
+       } else {
+               g_printerr ("modest: display name not defined for '%s:%s'\n", 
+                           account, server_account);
+               g_object_unref (G_OBJECT(tny_account));
+               return NULL;
+       }
 
        /* proto */
        val = modest_account_mgr_get_string (priv->account_mgr, server_account,
@@ -324,6 +371,28 @@ tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
                return NULL;
        }
 
+       /* Options */
+       options = modest_account_mgr_get_list (priv->account_mgr,
+                                              tny_account_get_id (tny_account),
+                                              MODEST_ACCOUNT_OPTIONS,
+                                              MODEST_CONF_VALUE_STRING,
+                                              TRUE,
+                                              &error);
+       
+       if (error) {
+               g_warning ("Error retrieving account %s options: %s",
+                          tny_account_get_id (tny_account), error->message);
+               g_error_free (error);
+       } else {
+               GSList *tmp = options;
+               while (options) {
+                       tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (tny_account), options->data);
+                       g_free (options->data);
+                       options = g_slist_next (options);
+               }
+               g_slist_free (tmp);
+       }
+
        /* hostname */
        val = modest_account_mgr_get_string (priv->account_mgr, server_account,
                                             MODEST_ACCOUNT_HOSTNAME, TRUE,
@@ -566,12 +635,14 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
        cursor = accounts = modest_account_mgr_account_names (priv->account_mgr, NULL); 
 
        while (cursor) {
-               gchar           *account_name;
-               gchar           *server_account;
-               TnyAccount *account;
-
-               account_name  =  (gchar*)cursor->data;
-               account =  NULL;
+               gchar       *account_name;
+               gchar       *server_account;
+               TnyAccount  *account;
+               gboolean     is_server_account;
+
+               account_name      = (gchar*)cursor->data;
+               account           = NULL;
+               is_server_account = FALSE;
                
                if (!modest_account_mgr_account_get_enabled (priv->account_mgr, account_name)) { 
                        g_free (account_name); 
@@ -582,10 +653,13 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
                if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
                        server_account = get_server_account_for_account (self, account_name,
                                                                         MODEST_PROTOCOL_TYPE_TRANSPORT);
-                       if (server_account)
+                       if (server_account) {
                                account = tny_account_from_name (self, account_name, 
                                                                       server_account,
                                                                       MODEST_PROTOCOL_TYPE_TRANSPORT);
+                               is_server_account = TRUE;
+                       }
+
                        if (!account)
                                g_printerr ("modest: no transport account for '%s'\n",
                                            account_name);
@@ -598,10 +672,13 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
                if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
                        server_account = get_server_account_for_account (self, account_name,
                                                                         MODEST_PROTOCOL_TYPE_STORE);
-                       if (server_account)
+                       if (server_account) {
                                account = tny_account_from_name (self, account_name, 
                                                                       server_account,
                                                                       MODEST_PROTOCOL_TYPE_STORE);
+                               is_server_account = TRUE;
+                       }
+
                        if (!account)
                                g_printerr ("modest: no store account for '%s'\n",
                                            account_name);
@@ -617,8 +694,6 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
        g_slist_free (accounts);
 
        tny_session_camel_set_account_store (priv->tny_session_camel, iface);
-/*     tny_session_camel_set_current_accounts (priv->tny_session_camel, */
-/*                                             list); */
 }