+ 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);
+
+ g_assert (store_account);
+ g_assert (transport_account);
+
+ /* Add accounts to the lists */
+ tny_list_append (priv->store_accounts, G_OBJECT (store_account));
+ tny_list_append (priv->transport_accounts, G_OBJECT (transport_account));
+
+ /* Create a new pseudo-account with an outbox for this
+ transport account and add it to the global outbox
+ in the local account */
+ add_outbox_from_transport_account_to_global_outbox (self, account, transport_account);
+
+ /* Notify the observers. We do it after everything is
+ created */
+ if (notify) {
+ TnyAccount *local_account = NULL;
+
+ /* Notify the observers about the new server & transport accounts */
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, store_account);
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_INSERTED_SIGNAL], 0, transport_account);
+
+ /* Notify that the local account changed */
+ local_account = modest_tny_account_store_get_local_folders_account (self);
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
+ g_object_unref (local_account);
+ }
+
+ /* Frees */
+ g_object_unref (store_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) {
+ TnyAccount *local_account = NULL;
+ TnyFolder *outbox = NULL;
+ ModestTnyAccountStorePrivate *priv = NULL;
+
+ /* Remove the OUTBOX of the account from the global outbox */
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+ outbox = g_hash_table_lookup (priv->outbox_of_transport, transport_account);
+
+ if (TNY_IS_FOLDER (outbox)) {
+ local_account = modest_tny_account_store_get_local_folders_account (self);
+ modest_tny_local_folders_account_remove_folder_from_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
+ outbox);
+ g_hash_table_remove (priv->outbox_of_transport, transport_account);
+
+ /* Notify the change in the local account */
+ g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
+ g_object_unref (local_account);
+ } else {
+ g_warning ("Removing a transport account that has no outbox");
+ }
+
+ /* 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)
+{
+ ModestTnyAccountStorePrivate *priv = NULL;
+ GSList *list_specifics = NULL, *iter = NULL;
+
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+ ModestConf *conf = modest_runtime_get_conf ();
+
+ GError *err = NULL;
+ list_specifics = modest_conf_get_list (conf,
+ MODEST_CONF_CONNECTION_SPECIFIC_SMTP_LIST,
+ MODEST_CONF_VALUE_STRING, &err);
+ if (err) {
+ g_printerr ("modest: %s: error getting list: %s\n.", __FUNCTION__, err->message);
+ g_error_free (err);
+ err = NULL;
+ }
+
+ /* 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) {
+ g_object_set_data (G_OBJECT(tny_account),
+ "account_store",
+ (gpointer)self);
+
+ tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
+ g_object_unref (tny_account);
+ } else
+ g_printerr ("modest: failed to create smtp-specific account for %s\n",
+ transport_account_name);
+ }
+ }
+ iter = g_slist_next (iter);
+ }
+}
+
+TnyMsg *
+modest_tny_account_store_find_msg_in_outboxes (ModestTnyAccountStore *self,
+ const gchar *uri,
+ TnyAccount **ac_out)
+{
+ TnyIterator *acc_iter;
+ ModestTnyAccountStorePrivate *priv;
+ TnyMsg *msg = NULL;
+ TnyAccount *msg_account = NULL;
+
+ g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
+ priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self);
+
+ acc_iter = tny_list_create_iterator (priv->store_accounts_outboxes);
+ while (!msg && !tny_iterator_is_done (acc_iter)) {
+ TnyList *folders = tny_simple_list_new ();
+ TnyAccount *account = TNY_ACCOUNT (tny_iterator_get_current (acc_iter));
+ TnyIterator *folders_iter = NULL;
+
+ tny_folder_store_get_folders (TNY_FOLDER_STORE (account), folders, NULL, NULL);
+ folders_iter = tny_list_create_iterator (folders);
+
+ while (msg == NULL && !tny_iterator_is_done (folders_iter)) {
+ TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (folders_iter));
+ msg = tny_folder_find_msg (folder, uri, NULL);
+
+ if (msg)
+ msg_account = g_object_ref (account);
+
+ g_object_unref (folder);
+ tny_iterator_next (folders_iter);
+ }
+
+ g_object_unref (folders);
+ g_object_unref (account);
+ tny_iterator_next (acc_iter);
+ }
+
+ g_object_unref (acc_iter);
+
+ if (ac_out != NULL)
+ *ac_out = msg_account;
+
+ return msg;