* remove modest_tny_account_store_get_tny_account_from_server_account
[modest] / src / modest-tny-account-store.c
index ac083fa..feacae2 100644 (file)
@@ -35,6 +35,7 @@
 #include <tny-store-account.h>
 #include <tny-transport-account.h>
 #include <tny-device.h>
+#include <tny-simple-list.h>
 #include <tny-account-store.h>
 #include <tny-camel-transport-account.h>
 #include <tny-camel-imap-store-account.h>
@@ -74,9 +75,9 @@ struct _ModestTnyAccountStorePrivate {
        
        GHashTable         *password_hash;
        TnyDevice          *device;
-       TnyPlatformFactory *platform_fact;
        TnySessionCamel    *tny_session_camel;
 
+       ModestAccountMgr   *account_mgr;
        TnyAccount         *local_folders;
 };
 
@@ -160,8 +161,7 @@ modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
                MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
 
        priv->cache_dir              = NULL;
-       
-       priv->platform_fact          = NULL;
+       priv->account_mgr            = NULL;
        priv->tny_session_camel      = NULL;
        priv->device                 = NULL;
        
@@ -172,8 +172,18 @@ modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
 }
 
 
-
-
+/* we need these dummy functions, or tinymail will complain */
+static gchar*
+get_password_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel)
+{
+       return NULL;
+}
+static void
+forget_password_dummy (TnyAccount *account)
+{
+       return;
+}
+       
 /* create a pseudo-account for our local folders */
 static TnyAccount*
 get_local_folders_account (ModestTnyAccountStore *self)
@@ -190,20 +200,20 @@ get_local_folders_account (ModestTnyAccountStore *self)
                g_printerr ("modest: cannot create account for local folders");
                return NULL;
        }
-
-       maildir = modest_local_folder_info_get_maildir_path ();
        
+       tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),priv->tny_session_camel);
+       
+       maildir = modest_local_folder_info_get_maildir_path ();
        url = camel_url_new ("maildir:", NULL);
        camel_url_set_path (url, maildir);
-
        url_string = camel_url_to_string (url, 0);
-       tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
        
-       tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),
-                                      priv->tny_session_camel);
+       tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string);
        tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); 
        tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); 
-       
+        tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_password_dummy);
+       tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_password_dummy);
+
        camel_url_free (url);
        g_free (maildir);
        g_free (url_string);
@@ -239,17 +249,11 @@ on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean se
 static ModestTnyAccountStore*
 get_account_store_for_account (TnyAccount *account)
 {
-       return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account), "account_store"));
+       return MODEST_TNY_ACCOUNT_STORE(g_object_get_data (G_OBJECT(account),
+                                                          "account_store"));
 }
 
 
-
-static void
-set_account_store_for_account (TnyAccount *account, ModestTnyAccountStore *store)
-{
-       g_object_set_data (G_OBJECT(account), "account_store", (gpointer)store);
-}
-
 static void
 on_password_requested (ModestTnyAccountStore *account_store, 
                       const gchar* account_name,
@@ -301,16 +305,12 @@ on_password_requested (ModestTnyAccountStore *account_store,
                *remember = FALSE;
 
        gtk_widget_destroy (dialog);
-
-       while (gtk_events_pending ())
-               gtk_main_iteration ();
 }
 
 static gchar*
 get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 {
        const gchar *key;
-       ModestAccountMgr *account_mgr;
        const TnyAccountStore *account_store;
        ModestTnyAccountStore *self;
        ModestTnyAccountStorePrivate *priv;
@@ -321,13 +321,10 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
        
        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);
        
-       account_mgr = modest_tny_platform_factory_get_account_mgr_instance
-               (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
-       
        /* 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...*/
@@ -338,7 +335,7 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 
        /* if the password is not already there, try ModestConf */
        if (!already_asked) {
-               pwd  = modest_account_mgr_get_string (account_mgr,
+               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));
@@ -356,7 +353,7 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 
                if (!*cancel) {
                        if (remember)
-                               modest_account_mgr_set_string (account_mgr,
+                               modest_account_mgr_set_string (priv->account_mgr,
                                                               key, MODEST_ACCOUNT_PASSWORD,
                                                               pwd,
                                                               TRUE, NULL);
@@ -378,14 +375,13 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 
 
 static void
-forget_password (TnyAccount *account) {
-
+forget_password (TnyAccount *account)
+{
        ModestTnyAccountStore *self;
        ModestTnyAccountStorePrivate *priv;
        const TnyAccountStore *account_store;
        gchar *pwd;
        const gchar *key;
-       ModestAccountMgr *account_mgr;
        
         account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account));
        self = MODEST_TNY_ACCOUNT_STORE (account_store);
@@ -400,11 +396,8 @@ forget_password (TnyAccount *account) {
                g_hash_table_insert (priv->password_hash, g_strdup (key), NULL);
        }
 
-       account_mgr = modest_tny_platform_factory_get_account_mgr_instance
-               (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
-
        /* Remove from configuration system */
-       modest_account_mgr_unset (account_mgr,
+       modest_account_mgr_unset (priv->account_mgr,
                                  key, MODEST_ACCOUNT_PASSWORD,
                                  TRUE, NULL);
 }
@@ -429,12 +422,6 @@ tny_account_for_proto (ModestProtocol proto)
        else 
                g_return_val_if_reached (NULL);
        
-       if (tny_account)
-               tny_account_set_proto (tny_account,
-                                      modest_protocol_info_get_protocol_name(proto));
-       else
-               g_printerr ("modest: could not get tny account for %d\n",
-                           proto);    
        return tny_account;
 }
 
@@ -469,10 +456,14 @@ get_tny_account_from_server_account (ModestTnyAccountStore *self,
        }
        
        /* Set account store, session and id */
-       set_account_store_for_account (TNY_ACCOUNT(tny_account), self);
        tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),  /* session */
                                       priv->tny_session_camel);
-       tny_account_set_id (tny_account, account_data->account_name); /* id */
+
+       /* Proto */
+       tny_account_set_proto (tny_account,
+                              modest_protocol_info_get_protocol_name(account_data->proto));
+
+       g_object_set_data (G_OBJECT(tny_account), "account_store", (gpointer)self);
 
        /* Options */
        if (account_data->options) {
@@ -483,6 +474,9 @@ get_tny_account_from_server_account (ModestTnyAccountStore *self,
                        tmp = g_slist_next (tmp);
                }
        }
+       /* id */
+       tny_account_set_id (tny_account, account_data->account_name);
+
        /* Hostname & Username */
        if (account_data->username) 
                tny_account_set_user (tny_account, account_data->username);
@@ -491,8 +485,8 @@ get_tny_account_from_server_account (ModestTnyAccountStore *self,
                tny_account_set_hostname (tny_account, account_data->hostname);
 
        /* Password functions */
-       tny_account_set_pass_func (tny_account, get_password);
         tny_account_set_forget_pass_func (tny_account, forget_password);
+       tny_account_set_pass_func (tny_account, get_password);
 
        return tny_account;
 }
@@ -522,6 +516,11 @@ modest_tny_account_store_finalize (GObject *obj)
                g_hash_table_destroy (priv->password_hash);
                priv->password_hash = NULL;
        }
+
+       if (priv->account_mgr) {
+               g_object_unref (priv->account_mgr);
+               priv->account_mgr = NULL;
+       }
        
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -532,21 +531,15 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
 
        GObject *obj;
        ModestTnyAccountStorePrivate *priv;
-       TnyPlatformFactory *pfact;
        
        g_return_val_if_fail (account_mgr, NULL);
 
        obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL));
        priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj);
 
-       pfact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance());
-       if (!pfact) {
-               g_printerr ("modest: cannot get platform factory instance\n");
-               g_object_unref (obj);
-               return NULL;
-       } else
-               priv->platform_fact = pfact;
-
+       priv->account_mgr = account_mgr;
+       g_object_ref (G_OBJECT(priv->account_mgr));
+       
        /* The session needs the platform factory */
        priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj));
        if (!priv->tny_session_camel) {
@@ -554,8 +547,9 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) {
                g_object_unref (obj);
                return NULL;
        }
+       
        tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ());
-       priv->local_folders = get_local_folders_account (obj); /* FIXME: unref this in the end? */
+       /* FIXME: unref this in the end? */
        
        /* Connect signals */
        g_signal_connect (G_OBJECT(account_mgr), "account_changed",
@@ -625,28 +619,26 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *account_store, TnyList
        ModestTnyAccountStore        *self;
        ModestTnyAccountStorePrivate *priv;
        GSList                       *accounts, *cursor;
-       ModestAccountMgr             *account_mgr; 
        
        g_return_if_fail (account_store);
        g_return_if_fail (TNY_IS_LIST(list));
 
        self        = MODEST_TNY_ACCOUNT_STORE(account_store);
        priv        = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
-       account_mgr = modest_tny_platform_factory_get_account_mgr_instance
-               (MODEST_TNY_PLATFORM_FACTORY(priv->platform_fact));
        
        if (type == TNY_ACCOUNT_STORE_BOTH) {
                modest_tny_account_store_get_accounts (account_store, list,
                                                       TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
                modest_tny_account_store_get_accounts (account_store, list,
                                                       TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
+               return;
        }
 
-       accounts = modest_account_mgr_account_names (account_mgr, NULL); 
+       accounts = modest_account_mgr_account_names (priv->account_mgr, NULL); 
        for (cursor = accounts; cursor; cursor = cursor->next) {
                TnyAccount *tny_account = NULL;
                ModestAccountData *account_data =
-                       modest_account_mgr_get_account_data (account_mgr, 
+                       modest_account_mgr_get_account_data (priv->account_mgr, 
                                                             (gchar*)cursor->data);
                if (account_data && account_data->enabled) {
                        tny_account = get_tny_account_from_account (self, account_data, type);
@@ -654,18 +646,19 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *account_store, TnyList
                                tny_list_prepend (list, G_OBJECT(tny_account));
                }
                g_free (cursor->data);
-               modest_account_mgr_free_account_data (account_mgr, account_data);
+               modest_account_mgr_free_account_data (priv->account_mgr, account_data);
        }
        g_slist_free (accounts);
 
        /* also, add the local folder pseudo-account */
        if (type != TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) {
                if (!priv->local_folders)
+                       priv->local_folders = get_local_folders_account (self);
+               if (!priv->local_folders)
                        g_printerr ("modest: no local folders account\n");
                else
                        tny_list_prepend (list, G_OBJECT(priv->local_folders));
        }
-       tny_session_camel_set_account_store (priv->tny_session_camel, account_store);
 }
 
 static const gchar*
@@ -694,7 +687,8 @@ modest_tny_account_store_get_device (TnyAccountStore *self)
        priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
 
        if (!priv->device) 
-               priv->device = tny_platform_factory_new_device (priv->platform_fact);
+               priv->device = tny_platform_factory_new_device
+                       (modest_tny_platform_factory_get_instance());
        
        return g_object_ref (G_OBJECT(priv->device));
 }
@@ -705,9 +699,33 @@ static gboolean
 modest_tny_account_store_alert (TnyAccountStore *self, TnyAlertType type,
                                const gchar *prompt)
 {
-       g_printerr ("modest: alert_func not implemented (%d, %s)\n",
-                   type, prompt);
-       return TRUE;
+       GtkMessageType gtktype;
+       gboolean retval = FALSE;
+       GtkWidget *dialog;
+
+       switch (type)
+       {
+               case TNY_ALERT_TYPE_INFO:
+               gtktype = GTK_MESSAGE_INFO;
+               break;
+               case TNY_ALERT_TYPE_WARNING:
+               gtktype = GTK_MESSAGE_WARNING;
+               break;
+               case TNY_ALERT_TYPE_ERROR:
+               default:
+               gtktype = GTK_MESSAGE_ERROR;
+               break;
+       }
+
+       dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
+               gtktype, GTK_BUTTONS_YES_NO, prompt);
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
+               retval = TRUE;
+
+       gtk_widget_destroy (dialog);
+
+       return retval;
 }
 
 
@@ -750,7 +768,6 @@ tny_account_store_get_session  (TnyAccountStore *self)
 }
 
 
-
 TnyAccount*
 modest_tny_account_store_get_local_folders_account    (ModestTnyAccountStore *self)
 {
@@ -758,23 +775,3 @@ modest_tny_account_store_get_local_folders_account    (ModestTnyAccountStore *se
        
        return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->local_folders;
 }
-
-
-/* for now, ignore the account ===> the special folders are the same,
- * local folders for all accounts
- * this might change, ie, IMAP might have server-side sent-items
- */
-TnyFolder *
-modest_tny_account_store_get_special_folder (ModestTnyAccountStore*self, TnyAccount *account,
-                                            TnyFolderType special_type)
-{
-       g_return_val_if_fail (self, 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 =
-       //      tny_account_store_get_local_folders_account (self);
-
-       /* FIXME: implement this */
-       return NULL;
-}