* all:
[modest] / src / modest-tny-account-store.c
index e4ba8ee..784d77a 100644 (file)
@@ -74,8 +74,10 @@ struct _ModestTnyAccountStorePrivate {
        
        GHashTable         *password_hash;
        TnyDevice          *device;
-       TnyPlatformFactory *platform_fact;
        TnySessionCamel    *tny_session_camel;
+
+       ModestAccountMgr   *account_mgr;
+       TnyAccount         *local_folders;
 };
 
 #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -150,6 +152,7 @@ modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass)
        
 }
 
+
 static void
 modest_tny_account_store_instance_init (ModestTnyAccountStore *obj)
 {
@@ -157,16 +160,56 @@ 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;
        
        priv->password_hash          = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                              g_free, g_free);
+
+       priv->local_folders          = NULL;
+}
+
+
+
+
+/* create a pseudo-account for our local folders */
+static TnyAccount*
+get_local_folders_account (ModestTnyAccountStore *self)
+{
+       TnyStoreAccount *tny_account;
+       CamelURL *url;
+       gchar *maildir, *url_string;
+       ModestTnyAccountStorePrivate *priv;
+
+       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+       
+       tny_account = tny_camel_store_account_new ();
+       if (!tny_account) {
+               g_printerr ("modest: cannot create account for local folders");
+               return NULL;
+       }
+       
+       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_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); 
+       
+       camel_url_free (url);
+       g_free (maildir);
+       g_free (url_string);
+
+       return TNY_ACCOUNT(tny_account);
 }
 
 
+
 static void
 on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account,
                    gpointer user_data)
@@ -193,7 +236,8 @@ 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"));
 }
 
 
@@ -264,7 +308,6 @@ 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;
@@ -275,13 +318,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...*/
@@ -292,7 +332,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));
@@ -310,7 +350,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);
@@ -332,14 +372,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);
@@ -354,11 +393,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);
 }
@@ -476,6 +512,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);
 }
@@ -486,21 +527,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) {
@@ -508,9 +543,10 @@ 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 ());
-
-
+       /* FIXME: unref this in the end? */
+       
        /* Connect signals */
        g_signal_connect (G_OBJECT(account_mgr), "account_changed",
                                       G_CALLBACK (on_account_changed), obj);
@@ -538,43 +574,6 @@ modest_tny_account_store_add_transport_account  (TnyAccountStore *self,
 }
 
 
-/* create a pseudo-account for our local folders */
-static TnyAccount*
-get_local_folder_account (ModestTnyAccountStore *self)
-{
-       TnyStoreAccount *tny_account;
-       CamelURL *url;
-       gchar *maildir, *url_string;
-       ModestTnyAccountStorePrivate *priv;
-
-       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
-       
-       tny_account = tny_camel_store_account_new ();
-       if (!tny_account) {
-               g_printerr ("modest: cannot create account for local folders");
-               return NULL;
-       }
-
-       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_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); 
-       tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); 
-       
-       camel_url_free (url);
-       g_free (maildir);
-       g_free (url_string);
-
-       return TNY_ACCOUNT(tny_account);
-}
-
 
 static TnyAccount*
 get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *account_data,
@@ -604,7 +603,7 @@ get_tny_account_from_account (ModestTnyAccountStore *self, ModestAccountData *ac
        
        if (account_data->display_name)
                tny_account_set_name (tny_account, account_data->display_name); 
-
+       
        return tny_account;
 }
 
@@ -616,27 +615,25 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *account_store, TnyList
        ModestTnyAccountStore        *self;
        ModestTnyAccountStorePrivate *priv;
        GSList                       *accounts, *cursor;
-       ModestAccountMgr             *account_mgr; 
-       TnyAccount                   *local_folder_account;
        
        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);
+               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);
        }
 
-       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);
@@ -644,17 +641,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 */
-       local_folder_account = get_local_folder_account (MODEST_TNY_ACCOUNT_STORE(self));
-       if (!local_folder_account)
-               g_printerr ("modest: failed to add local folders account\n");
-       else
-               tny_list_prepend (list, G_OBJECT(local_folder_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);
 }
 
@@ -684,7 +683,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));
 }
@@ -728,15 +728,23 @@ void
 modest_tny_account_store_set_get_pass_func (ModestTnyAccountStore *self,
                                            ModestTnyGetPassFunc func)
 {
-       g_message (__FUNCTION__);
-       return; /* not implemented, we use signals */
+       /* not implemented, we use signals */
+       g_printerr ("modest: set_get_pass_func not implemented\n");
 }
 
-
-
 TnySessionCamel*
-tny_account_store_get_session    (TnyAccountStore *self)
+tny_account_store_get_session  (TnyAccountStore *self)
 {
        g_return_val_if_fail (self, NULL);      
        return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel;
 }
+
+
+TnyAccount*
+modest_tny_account_store_get_local_folders_account    (ModestTnyAccountStore *self)
+{
+       g_return_val_if_fail (self, NULL);
+       
+       return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->local_folders;
+}
+