* Fixed some compilation warnings
[modest] / src / modest-tny-account-store.c
index dce26e0..ca0d113 100644 (file)
@@ -28,6 +28,7 @@
  */
 
 #include <string.h>
+#include <glib/gi18n.h>
 
 #include <tny-account.h>
 #include <tny-account-store.h>
 #include <tny-device.h>
 #include <tny-account-store.h>
 #include <tny-camel-transport-account.h>
-#include <tny-camel-store-account.h>
+#include <tny-camel-imap-store-account.h>
+#include <tny-camel-pop-store-account.h>
 #include <modest-marshal.h>
-#include <glib/gi18n.h>
+#include <modest-protocol-info.h>
 #include "modest-account-mgr.h"
 #include "modest-tny-account-store.h"
 #include "modest-tny-platform-factory.h"
+#include <tny-gtk-lockable.h>
+#include <camel/camel.h>
 
 /* 'private'/'protected' functions */
 static void modest_tny_account_store_class_init   (ModestTnyAccountStoreClass *klass);
@@ -140,8 +144,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 +211,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)
 {
@@ -218,8 +221,6 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
        gchar *pwd = NULL;
        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 +228,48 @@ 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); */
-       
-       return 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;
+
+               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;
+               }
+       } else
+               *cancel = FALSE;
+
+       return pwd;
 }
 
 
@@ -263,10 +279,57 @@ 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);
+}
+
+
+
+/* instantiate the correct tny account subclass */
+static TnyAccount*
+tny_account_for_proto (const gchar* proto /* ugly */, ModestProtocolType modest_type) 
+{
+       TnyAccount *tny_account = NULL;
+       
+       /* is it a store or a transport? */
+       if  (modest_type == MODEST_PROTOCOL_TYPE_STORE) {
+               if (strcmp (proto, "pop") == 0) 
+                       tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ());
+               else if (strcmp (proto, "imap") == 0)
+                       tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ());
+
+       }  else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT) {
+               if (strcmp (proto, "sendmail") == 0) 
+                       tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
+               else if (strcmp (proto, "smtp") == 0)
+                       tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
+       }
+       
+       if (tny_account) {
+               tny_account_set_proto (tny_account, proto);
+       } else
+               g_printerr ("modest: could not get tny %s account (%d)\n",
+                           proto, modest_type);
+       
+       return tny_account;
 }
 
 
@@ -288,17 +351,19 @@ tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
        g_return_val_if_fail (server_account, NULL);
 
        priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+       
+       /* proto */
+       val = modest_account_mgr_get_string (priv->account_mgr, server_account,
+                                            MODEST_ACCOUNT_PROTO, TRUE, NULL);
+       if (!val) {
+               g_printerr ("modest: protocol not defined for '%s:%s'\n", 
+                           account, server_account);
+               return NULL;
+       }
 
-       /* is it a store or a transport? */
-       if  (modest_type == MODEST_PROTOCOL_TYPE_STORE) 
-               tny_account = TNY_ACCOUNT(tny_camel_store_account_new ());
-       else if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT)
-               tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ());
-       else
-               g_assert_not_reached ();
-
+       tny_account = tny_account_for_proto (val, modest_type);
        if (!tny_account) {
-               g_printerr ("modest: failed to create new tny account for '%s:%s'\n",
+               g_printerr ("modest: could not create account for '%s:%s'\n",
                            account, server_account);
                return NULL;
        }
@@ -325,19 +390,6 @@ tny_account_from_name (ModestTnyAccountStore *self, const gchar *account,
                return NULL;
        }
 
-       /* proto */
-       val = modest_account_mgr_get_string (priv->account_mgr, server_account,
-                                            MODEST_ACCOUNT_PROTO, TRUE, NULL);
-       if (val) {
-               tny_account_set_proto (tny_account, val);
-               g_free (val);
-       } else {
-               g_printerr ("modest: protocol not defined for '%s:%s'\n", 
-                           account, server_account);
-               g_object_unref (G_OBJECT(tny_account));
-               return NULL;
-       }
-
        /* Options */
        options = modest_account_mgr_get_list (priv->account_mgr,
                                               tny_account_get_id (tny_account),
@@ -403,7 +455,7 @@ modest_tny_account_store_finalize (GObject *obj)
        }
 
        if (priv->tny_session_camel) {
-               // FIXME: how to kill a camel
+               camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel));
                priv->tny_session_camel = NULL;
        }
 
@@ -463,7 +515,6 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
                g_object_unref (obj);
                return NULL;
        }
-       tny_device_force_online (priv->device);
        
        priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
 
@@ -473,6 +524,8 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
                return NULL;
        }
 
+       tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
+
        return MODEST_TNY_ACCOUNT_STORE(obj);
 }
 
@@ -505,7 +558,7 @@ add_account  (TnyAccountStore *self, TnyAccount *account) {
        return modest_account_mgr_add_server_account (priv->account_mgr,
                                                      account_name,
                                                      hostname, username, NULL,
-                                                     proto);
+                                                     modest_protocol_info_get_protocol(proto));
 }
 
 
@@ -593,7 +646,7 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
                modest_type = MODEST_PROTOCOL_TYPE_STORE;
                break;
        case TNY_ACCOUNT_STORE_BOTH:
-               modest_type = MODEST_PROTOCOL_TYPE_ANY;
+               modest_type = MODEST_PROTOCOL_TYPE_UNKNOWN;
                break;
        default:
                g_assert_not_reached ();
@@ -617,7 +670,7 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
                        continue;
                } 
                
-               if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
+               if (modest_type == MODEST_PROTOCOL_TYPE_TRANSPORT || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
                        server_account = get_server_account_for_account (self, account_name,
                                                                         MODEST_PROTOCOL_TYPE_TRANSPORT);
                        if (server_account) {
@@ -636,7 +689,7 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *iface,
                        g_free (server_account);
                }
                
-               if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_ANY) {
+               if (modest_type == MODEST_PROTOCOL_TYPE_STORE || modest_type == MODEST_PROTOCOL_TYPE_UNKNOWN) {
                        server_account = get_server_account_for_account (self, account_name,
                                                                         MODEST_PROTOCOL_TYPE_STORE);
                        if (server_account) {