* Added a new parameter to modest_platform_check_memory_low in order to show or...
[modest] / src / modest-tny-account-store.c
index 2f5ebbe..a0fd3de 100644 (file)
@@ -521,9 +521,8 @@ show_wrong_password_dialog (TnyAccount *account)
 { 
        /* This is easier than using a struct for the user_data: */
        ModestTnyAccountStore *self = modest_runtime_get_account_store();
-       const gchar *modest_account_name;
        GtkWidget *main_window;
-       GtkWidget *dialog;
+       GtkWidget *dialog = NULL;
 
        main_window = (GtkWidget *) modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
                                                                       FALSE); /* don't create */
@@ -532,16 +531,16 @@ show_wrong_password_dialog (TnyAccount *account)
                return;
        }
 
-       modest_account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
-       if (!modest_account_name) {
-               g_warning ("%s: modest_tny_account_get_parent_modest_account_name_for_server_account() failed.\n", 
-                       __FUNCTION__);
+       if (g_object_get_data (G_OBJECT (account), "connection_specific") != NULL) {
+               modest_ui_actions_on_smtp_servers (NULL, NULL);
+       } else {
+               const gchar *modest_account_name;
+               modest_account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);   
+               dialog = modest_tny_account_store_show_account_settings_dialog (self, modest_account_name);
+               modest_account_settings_dialog_save_password (MODEST_ACCOUNT_SETTINGS_DIALOG (dialog));
        }
-       
-       dialog = modest_tny_account_store_show_account_settings_dialog (self, modest_account_name);
-       modest_account_settings_dialog_save_password (MODEST_ACCOUNT_SETTINGS_DIALOG (dialog));
        /* Show an explanatory temporary banner: */
-       modest_platform_information_banner (GTK_WIDGET(dialog), NULL, _("mcen_ib_username_pw_incorrect"));
+       modest_platform_information_banner (dialog, NULL, _("mcen_ib_username_pw_incorrect"));
 }
 
 /* This callback will be called by Tinymail when it needs the password
@@ -685,7 +684,7 @@ get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *canc
                                                                                             server_account_name);
                        /* If the login has ever succeeded then show a specific message */
                        if (username_known)
-                               msg = _("ecdg_ib_set_password_incorrect");
+                               msg = dgettext ("hildon-common-strings", "ecdg_ib_set_password_incorrect");
                        else
                                msg = _("mcen_ib_username_pw_incorrect");
                        show_password_warning_only (msg);
@@ -700,7 +699,13 @@ get_password (TnyAccount *account, const gchar * prompt_not_used, gboolean *canc
                if (!*cancel) {
                        /* The password will be returned as the result,
                         * but we need to tell tinymail about the username too: */
-                       tny_account_set_user (account, username);
+
+                       /* WARNING: I disabled setting username as this can cause locks. Anyway,
+                        * as now we have the password dialog username entry always dimmed
+                        * this shouldn't be a problem */
+
+                       /* if (username) */
+                       /*      tny_account_set_user (account, username); */
                        
                        /* Do not save the password in gconf, because
                         * the UI spec says "The password will never
@@ -964,16 +969,8 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr,
           global OUTBOX hosted in the local account */
        add_existing_accounts (MODEST_TNY_ACCOUNT_STORE (obj));
        
-       /* FIXME: I'm doing this (adding an "if (FALSE)"because this
-          stuff is not working properly and could cause SIGSEVs, for
-          example one send queue will be created for each connection
-          specific SMTP server, so when tinymail asks for the outbox
-          it will return NULL because there is no outbox folder for
-          this specific transport accounts, and it's a must that the
-          send queue returns an outbox */
-       if (TRUE)
-               /* Add connection-specific transport accounts */
-               add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE(obj));
+       /* Add connection-specific transport accounts */
+       add_connection_specific_transport_accounts (MODEST_TNY_ACCOUNT_STORE(obj));
        
        /* This is a singleton, so it does not need to be unrefed. */
        if (volume_path_is_mounted (MODEST_MCC1_VOLUMEPATH)) {
@@ -1002,7 +999,7 @@ modest_tny_account_store_get_accounts  (TnyAccountStore *self,
                tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
                break;
        case TNY_ACCOUNT_STORE_STORE_ACCOUNTS:
-               tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
+               tny_list_foreach (priv->store_accounts, foreach_account_append_to_list, list);
                break;
        case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS:
                tny_list_foreach (priv->transport_accounts, foreach_account_append_to_list, list);
@@ -1067,18 +1064,16 @@ modest_tny_account_store_alert (TnyAccountStore *self,
 {
        ModestTransportStoreProtocol proto =
                MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN; 
-       const gchar* server_name = NULL;
+       const gchar* server_name = "";
        gchar *prompt = NULL;
        gboolean retval;
 
-
-       g_return_val_if_fail (account, FALSE);
+       /* NOTE: account may be NULL in some cases */
        g_return_val_if_fail (error, FALSE);
        
        /* Get the server name: */
-       server_name = tny_account_get_hostname (account);
-
        if (account) {
+               server_name = tny_account_get_hostname (account);
                const gchar *proto_name = tny_account_get_proto (account);
                if (proto_name)
                        proto = modest_protocol_info_get_transport_store_protocol (proto_name);
@@ -1598,54 +1593,92 @@ create_tny_account (ModestTnyAccountStore *self,
        return account;
 }
 
+typedef struct _AddOutboxInfo {
+       ModestTnyAccountStore *account_store;
+       TnyAccount *transport_account;
+} AddOutboxInfo;
 
 static void
-add_outbox_from_transport_account_to_global_outbox (ModestTnyAccountStore *self,
-                                                   const gchar *account_name,
-                                                   TnyAccount *transport_account)
+add_outbox_from_transport_account_to_global_outbox_get_folders_cb (TnyFolderStore *folder_store,
+                                                                  gboolean cancelled,
+                                                                  TnyList *list,
+                                                                  GError *err,
+                                                                  gpointer userdata)
 {
-       TnyList *folders = NULL;
-       TnyIterator *iter_folders = NULL;
-       TnyAccount *local_account = NULL, *account_outbox = NULL;
-       TnyFolder *per_account_outbox = NULL;
+       TnyIterator *iter_folders;
+       TnyFolder *per_account_outbox;
+       TnyAccount *local_account = NULL;
+       AddOutboxInfo *info = (AddOutboxInfo *) userdata;
        ModestTnyAccountStorePrivate *priv = NULL;
+       ModestTnyAccountStore *self;
 
+       self = MODEST_TNY_ACCOUNT_STORE (info->account_store);
        priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
-
-       /* Create per account local outbox */
-       account_outbox = 
-               modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr, 
-                                                                           account_name, 
-                                                                           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);
-       if (tny_list_get_length (folders) != 1) {
+       
+       if (tny_list_get_length (list) != 1) {
                g_warning ("%s: > 1 outbox found (%d)?!", __FUNCTION__,
-                          tny_list_get_length (folders));
+                          tny_list_get_length (list));
        }
                        
-       iter_folders = tny_list_create_iterator (folders);
+       iter_folders = tny_list_create_iterator (list);
        per_account_outbox = TNY_FOLDER (tny_iterator_get_current (iter_folders));
        g_object_unref (iter_folders);
-       g_object_unref (folders);
-       g_object_unref (account_outbox);
+       g_object_unref (list);
 
        /* 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 (self);
+       local_account = modest_tny_account_store_get_local_folders_account (info->account_store);
        modest_tny_local_folders_account_add_folder_to_outbox (MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (local_account),
                                                               per_account_outbox);
        /* Add the pair to the hash table */
        g_hash_table_insert (priv->outbox_of_transport,
-                            transport_account,
+                            info->transport_account,
                             per_account_outbox);
        
+       /* Notify that the local account changed */
+       g_signal_emit (G_OBJECT (self), signals [ACCOUNT_CHANGED_SIGNAL], 0, local_account);
+
+       g_object_unref (info->transport_account);
        g_object_unref (local_account);
        g_object_unref (per_account_outbox);
+       g_slice_free (AddOutboxInfo, info);
+}
+                                                                  
+
+static void
+add_outbox_from_transport_account_to_global_outbox (ModestTnyAccountStore *self,
+                                                   const gchar *account_name,
+                                                   TnyAccount *transport_account)
+{
+       TnyList *folders = NULL;
+       TnyAccount *account_outbox = NULL;
+       ModestTnyAccountStorePrivate *priv = NULL;
+       AddOutboxInfo *info;
+
+       priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self);
+
+       /* Create per account local outbox */
+       account_outbox = 
+               modest_tny_account_new_for_per_account_local_outbox_folder (priv->account_mgr, 
+                                                                           account_name, 
+                                                                           priv->session);
+
+       if (!G_IS_OBJECT (account_outbox)) {
+               g_warning ("%s: could not create per account local outbox folder", __FUNCTION__);
+               return;
+       }
+
+       tny_list_append (priv->store_accounts_outboxes, G_OBJECT (account_outbox));
+       
+       /* Get the outbox folder */
+       folders = tny_simple_list_new ();
+       info = g_slice_new0 (AddOutboxInfo);
+       info->account_store = self;
+       info->transport_account = g_object_ref (transport_account);
+       tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account_outbox), folders, NULL, 
+                                           add_outbox_from_transport_account_to_global_outbox_get_folders_cb, NULL, (gpointer) info);
+       g_object_unref (account_outbox);
 }
 
 /*
@@ -1685,21 +1718,13 @@ insert_account (ModestTnyAccountStore *self,
        /* 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);
-       
+       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 */
@@ -1746,8 +1771,19 @@ on_account_disconnect_when_removing (TnyCamelAccount *account,
        g_object_unref (account);
 
        /* Clear the cache if it's an store account */
-       if (TNY_IS_STORE_ACCOUNT (account))
+       if (TNY_IS_STORE_ACCOUNT (account)) {
                tny_store_account_delete_cache (TNY_STORE_ACCOUNT (account));
+       } else if (TNY_IS_TRANSPORT_ACCOUNT (account)) {
+               ModestTnySendQueue* send_queue;
+               send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (account), FALSE);
+               if (send_queue) {
+                       if (modest_tny_send_queue_sending_in_progress (send_queue))
+                               tny_send_queue_cancel (TNY_SEND_QUEUE (send_queue),
+                                                      TNY_SEND_QUEUE_CANCEL_ACTION_REMOVE, 
+                                                      NULL);
+                       modest_runtime_remove_send_queue (TNY_TRANSPORT_ACCOUNT (account));
+               }
+       }
 }
 
 static void
@@ -1764,13 +1800,15 @@ on_account_removed (ModestAccountMgr *acc_mgr,
 
        /* Get the server and the transport account */
        store_account = 
-               modest_tny_account_store_get_server_account (self, account, TNY_ACCOUNT_TYPE_STORE);
+               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);
+               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) {
+       if (TNY_IS_STORE_ACCOUNT(store_account)) {
                /* Forget any cached password for the account */
                forget_password_in_memory (self, tny_account_get_id (store_account));
 
@@ -1789,17 +1827,18 @@ on_account_removed (ModestAccountMgr *acc_mgr,
                tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account), FALSE,
                                              on_account_disconnect_when_removing, self);
        } else {
-               g_warning ("There is no store account for account %s\n", account);
+               g_warning ("%s: no store account for account %s\n", 
+                          __FUNCTION__, account);
        }
 
        /* If there was any problem creating the account, for example,
           with the configuration system this could not exist */
-       if (transport_account) {
+       if (TNY_IS_TRANSPORT_ACCOUNT(transport_account)) {
                TnyAccount *local_account = NULL;
                TnyFolder *outbox = NULL;
 
                /* Forget any cached password for the account */
-               forget_password_in_memory (self, tny_account_get_id (store_account));
+               forget_password_in_memory (self, tny_account_get_id (transport_account));
 
                /* Remove it from the list of accounts and notify the
                   observers. Do not need to wait for account
@@ -1815,12 +1854,15 @@ on_account_removed (ModestAccountMgr *acc_mgr,
 
                        if (outbox_account) {
                                tny_list_remove (priv->store_accounts_outboxes, G_OBJECT (outbox_account));
+                               /* Remove existing emails to send */
+                               tny_store_account_delete_cache (TNY_STORE_ACCOUNT (outbox_account));
                                g_object_unref (outbox_account);
                        }
 
                        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 */
@@ -1837,7 +1879,8 @@ on_account_removed (ModestAccountMgr *acc_mgr,
                tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account), FALSE,
                                              on_account_disconnect_when_removing, self);
        } else {
-               g_warning ("There is no transport account for account %s\n", account);
+               g_warning ("%s: no transport account for account %s\n", 
+                          __FUNCTION__, account);
        }
 }
 
@@ -1861,6 +1904,9 @@ modest_tny_account_store_new_connection_specific_transport_account (ModestTnyAcc
                g_object_set_data (G_OBJECT(tny_account), 
                                   "account_store", 
                                   (gpointer)self);
+               g_object_set_data (G_OBJECT(tny_account), 
+                                  "connection_specific", 
+                                  GINT_TO_POINTER (TRUE));
                
                tny_list_append (priv->transport_accounts, G_OBJECT (tny_account));
                add_outbox_from_transport_account_to_global_outbox (self, 
@@ -1966,7 +2012,7 @@ modest_tny_account_store_get_transport_account_from_outbox_header(ModestTnyAccou
        TnyIterator *acc_iter;
        ModestTnyAccountStorePrivate *priv;
        TnyTransportAccount *header_acc = NULL;
-       const gchar *msg_id;
+       gchar *msg_id;
 
        g_return_val_if_fail (MODEST_IS_TNY_ACCOUNT_STORE (self), NULL);
        g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
@@ -1978,7 +2024,7 @@ modest_tny_account_store_get_transport_account_from_outbox_header(ModestTnyAccou
                TnyTransportAccount *account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current (acc_iter));
                ModestTnySendQueue *send_queue;
                ModestTnySendQueueStatus status;
-               send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account));
+               send_queue = modest_runtime_get_send_queue(TNY_TRANSPORT_ACCOUNT(account), TRUE);
                status = modest_tny_send_queue_get_msg_status(send_queue, msg_id);
                if (status != MODEST_TNY_SEND_QUEUE_UNKNOWN) {
                        header_acc = g_object_ref(account);
@@ -1987,6 +2033,7 @@ modest_tny_account_store_get_transport_account_from_outbox_header(ModestTnyAccou
                tny_iterator_next (acc_iter);
        }
        g_object_unref(acc_iter);
+       g_free (msg_id);
 
        /* New reference */
        return header_acc;
@@ -2004,9 +2051,10 @@ modest_tny_account_store_show_account_settings_dialog (ModestTnyAccountStore *se
        found = g_hash_table_lookup_extended (priv->account_settings_dialog_hash,
                                              account_name, NULL, (gpointer*)&dialog_as_gpointer);
 
-       if (found)
+       if (found) {
+               modest_account_settings_dialog_check_allow_changes ((ModestAccountSettingsDialog *) dialog_as_gpointer);
                return (GtkWidget *) dialog_as_gpointer;
-       else {
+       } else {
                ModestAccountSettings *settings;
                GtkWidget *dialog;
                dialog = (GtkWidget *) modest_account_settings_dialog_new ();
@@ -2014,6 +2062,7 @@ modest_tny_account_store_show_account_settings_dialog (ModestTnyAccountStore *se
                modest_account_settings_dialog_set_account (MODEST_ACCOUNT_SETTINGS_DIALOG (dialog), settings);
                g_object_unref (settings);
                modest_account_settings_dialog_switch_to_user_info (MODEST_ACCOUNT_SETTINGS_DIALOG (dialog));
+               modest_account_settings_dialog_check_allow_changes (MODEST_ACCOUNT_SETTINGS_DIALOG (dialog));
                modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog));
                
                g_hash_table_insert (priv->account_settings_dialog_hash, g_strdup (account_name), dialog);