+
+TnyAccount*
+modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self)
+{
+ TnyAccount *account = NULL;
+ ModestTnyAccountStorePrivate *priv;
+ TnyIterator *iter;
+ gboolean found;
+
+ g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ found = FALSE;
+ iter = tny_list_create_iterator (priv->store_accounts);
+ while (!tny_iterator_is_done (iter) && !found) {
+ TnyAccount *tmp_account;
+
+ tmp_account = TNY_ACCOUNT (tny_iterator_get_current (iter));
+ if (modest_tny_account_is_virtual_local_folders (tmp_account)) {
+ account = g_object_ref (tmp_account);
+ found = TRUE;
+ }
+ g_object_unref (tmp_account);
+ tny_iterator_next (iter);
+ }
+ g_object_unref (iter);
+
+ /* Returns a new reference to the account */
+ return account;
+}
+
+/*********************************************************************************/
+static void
+add_existing_accounts (ModestTnyAccountStore *self)
+{
+ GSList *account_names = NULL, *iter = NULL;
+ ModestTnyAccountStorePrivate *priv = NULL;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ /* These are account names, not server_account names */
+ account_names = modest_account_mgr_account_names (priv->account_mgr, FALSE);
+
+ for (iter = account_names; iter != NULL; iter = g_slist_next (iter)) {
+ const gchar *account_name = (const gchar*) iter->data;
+
+ /* Insert all enabled accounts without notifying */
+ if (modest_account_mgr_get_enabled (priv->account_mgr, account_name))
+ insert_account (self, account_name, FALSE);
+ }
+ modest_account_mgr_free_account_names (account_names);
+}
+
+static TnyAccount*
+create_tny_account (ModestTnyAccountStore *self,
+ const gchar *name,
+ TnyAccountType type)
+{
+ TnyAccount *account = NULL;
+ ModestTnyAccountStorePrivate *priv = NULL;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ account = modest_tny_account_new_from_account (priv->account_mgr,
+ name, type,
+ priv->session,
+ get_password,
+ forget_password);
+
+ if (account) {
+ /* Forget any cached password for the account, so that
+ we use a new account if any */
+ modest_tny_account_store_forget_password_in_memory (self,
+ tny_account_get_id (account));
+ /* Set the account store */
+ g_object_set_data (G_OBJECT(account), "account_store", self);
+ } else {
+ g_printerr ("modest: failed to create account for %s\n", name);
+ }
+
+ return account;
+}
+
+/*
+ * This function will be used for both adding new accounts and for the
+ * initialization. In the initialization we do not want to emit
+ * signals so notify will be FALSE, in the case of account additions
+ * we do want to notify the observers
+ */
+static void
+insert_account (ModestTnyAccountStore *self,
+ const gchar *account,
+ gboolean notify)
+{
+ ModestTnyAccountStorePrivate *priv = NULL;
+ TnyAccount *store_account = NULL, *transport_account = NULL;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ /* Get the server and the transport account */
+ store_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_STORE);
+ transport_account = create_tny_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
+
+ /* Add to the list, and notify the observers */
+ if (store_account) {
+ tny_list_append (priv->store_accounts, G_OBJECT (store_account));
+ if (notify)
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
+ g_object_unref (store_account);
+ }
+
+ /* Add to the list, and notify the observers */
+ if (transport_account) {
+ TnyAccount *account_outbox = NULL, *local_account = NULL;
+ TnyFolder *per_account_outbox = NULL;
+ TnyList *folders;
+ TnyIterator *iter_folders;
+
+ /* Add account to the list */
+ tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
+
+ /* Add connection-specific transport accounts */
+ add_connection_specific_transport_accounts (self, account);
+
+ /* Create per account local outbox */
+ account_outbox =
+ modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr,
+ account,
+ priv->session);
+ tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
+
+ /* Get the outbox folder */
+ folders = tny_simple_list_new ();
+ tny_folder_store_get_folders (TNY_FOLDER_STORE (account_outbox),
+ folders, NULL, NULL);
+ g_assert (tny_list_get_length (folders) == 1);
+ iter_folders = tny_list_create_iterator (folders);
+ per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
+ g_object_unref (iter_folders);
+ g_object_unref (account_outbox);
+
+ /* Add the outbox of the new per-account-local-outbox
+ account to the global local merged OUTBOX of the
+ local folders account */
+ local_account = modest_tny_account_store_get_local_folders_account (MODEST_TNY_ACCOUNT_STORE (self));
+ modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
+ per_account_outbox);
+ g_object_unref (local_account);
+ g_object_unref (per_account_outbox);
+
+ /* Notify the observers */
+ if (notify)
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
+ g_object_unref (transport_account);
+ }
+}
+
+static void
+on_account_inserted (ModestAccountMgr *acc_mgr,
+ const gchar *account,
+ gpointer user_data)
+{
+ /* Insert the account and notify the observers */
+ insert_account (MODEST_TNY_ACCOUNT_STORE (user_data), account, TRUE);
+}
+
+static void
+on_account_removed (ModestAccountMgr *acc_mgr,
+ const gchar *account,
+ gpointer user_data)
+{
+ TnyAccount *store_account = NULL, *transport_account = NULL;
+ ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data);
+
+ /* Get the server and the transport account */
+ store_account =
+ modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
+ transport_account =
+ modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_TRANSPORT);
+
+ /* If there was any problem creating the account, for example,
+ with the configuration system this could not exist */
+ if (store_account) {
+ /* Clear the cache */
+ tny_store_account_delete_cache (TNY_STORE_ACCOUNT (store_account));
+
+ /* Notify the observers */
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, store_account);
+ g_object_unref (store_account);
+ } else {
+ g_warning ("There is no store account for account %s\n", account);
+ }
+
+ /* If there was any problem creating the account, for example,
+ with the configuration system this could not exist */
+ if (transport_account) {
+ /* Notify the observers */
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_REMOVED_SIGNAL], 0, transport_account);
+ g_object_unref (transport_account);
+ } else {
+ g_warning ("There is no transport account for account %s\n", account);
+ }
+}
+
+static void
+add_connection_specific_transport_accounts (ModestTnyAccountStore *self,
+ const gchar *account_name)
+{
+ ModestTnyAccountStorePrivate *priv = NULL;
+ GSList *list_specifics = NULL, *iter = NULL;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ list_specifics = modest_account_mgr_get_list (priv->account_mgr,
+ account_name,
+ MODEST_ACCOUNT_CONNECTION_SPECIFIC_SMTP_LIST,
+ MODEST_CONF_VALUE_STRING, FALSE);
+
+ /* Look at each connection-specific transport account for the
+ * modest account: */
+ iter = list_specifics;
+ while (iter) {
+ /* The list alternates between the connection name and the transport name: */
+ iter = g_slist_next (iter);
+ if (iter) {
+ const gchar* transport_account_name = (const gchar*) (iter->data);
+ if (transport_account_name) {
+ TnyAccount * tny_account = NULL;
+ /* Add the account: */
+ tny_account =
+ modest_tny_account_new_from_server_account_name (priv->account_mgr,
+ priv->session,
+ transport_account_name);
+ if (tny_account) {
+ modest_tny_account_set_parent_modest_account_name_for_server_account (tny_account,
+ account_name);
+ g_object_set_data (G_OBJECT(tny_account),
+ "account_store",
+ (gpointer)self);
+
+ tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
+ } else
+ g_printerr ("modest: failed to create smtp-specific account for %s\n",
+ transport_account_name);
+ }
+ }
+ iter = g_slist_next (iter);
+ }
+}