Fixes a crash added in a recent commit
[modest] / src / maemo / modest-connection-specific-smtp-window.c
index bfeaf36..8c31398 100644 (file)
@@ -1,4 +1,31 @@
-/* connection-specific-smtp-window.c */
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include "modest-connection-specific-smtp-window.h"
 #include "modest-connection-specific-smtp-edit-window.h"
 #include <gtk/gtkstock.h>
 
 #include "modest-hildon-includes.h"
+#include "modest-platform.h"
+#include "modest-maemo-utils.h"
 
 #include <glib/gi18n.h>
+#include <string.h>
 
-G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
+G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window,
+              GTK_TYPE_DIALOG);
 
 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
        (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
@@ -35,9 +66,14 @@ struct _ModestConnectionSpecificSmtpWindowPrivate
        GtkWidget *button_edit;
        
        ModestAccountMgr *account_manager;
-       gchar* account_name;
 };
 
+static void on_response (GtkDialog *dialog, 
+                        gint response, 
+                        gpointer user_data);
+
+/* static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data); */
+
 static void
 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
                                                                                                                        GValue *value, GParamSpec *pspec)
@@ -70,7 +106,7 @@ enum MODEL_COLS {
        MODEL_COL_ID = 1, /* libconic IAP ID: a string */
        MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
        MODEL_COL_SERVER_NAME = 3, /* a string */
-       MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
+       MODEL_COL_SERVER_ACCOUNT_SETTINGS = 4 /* a gpointer */
 };
 
 
@@ -85,21 +121,20 @@ modest_connection_specific_smtp_window_finalize (GObject *object)
        GtkTreeIter iter;
        gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
        while (valid) {
-               ModestServerAccountData *data = NULL;
+               ModestServerAccountSettings *server_settings = NULL;
                
                gtk_tree_model_get (priv->model, &iter, 
-                                   MODEL_COL_SERVER_ACCOUNT_DATA, &data,
+                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
                                    -1);
                                 
-               if (data)
-                       modest_account_mgr_free_server_account_data (priv->account_manager, data);
+               if (server_settings)
+                       g_object_unref (server_settings);
                        
                /* Get next row: */
                valid = gtk_tree_model_iter_next (priv->model, &iter);
        }
        
        g_object_unref (G_OBJECT (priv->model));
-       g_free (priv->account_name);
        
        G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
 }
@@ -123,13 +158,13 @@ modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpW
 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
 
 void
-modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
-       const gchar* account_name)
+modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self,
+                                                             ModestAccountMgr *account_manager)
 {
+#ifdef MODEST_HAVE_CONIC
        ModestConnectionSpecificSmtpWindowPrivate *priv = 
                CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
        priv->account_manager = account_manager;
-       priv->account_name = account_name ? g_strdup (account_name) : NULL;
        
        GtkListStore *liststore = GTK_LIST_STORE (priv->model);
        
@@ -151,27 +186,26 @@ modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSp
                ConIcIap *iap = (ConIcIap*)iter->data;
                if (iap) {
                        #ifdef DEBUG_WITHOUT_LIBCONIC
-                       const gchar *name = "debug name";
-                       const gchar *id = "debug id";
+                       const gchar *connection_name = "debug name";
+                       const gchar *connection_id = "debug id";
                        #else
-                       const gchar *name = con_ic_iap_get_name (iap);
-                       const gchar *id = con_ic_iap_get_id (iap);
+                       const gchar *connection_name = con_ic_iap_get_name (iap);
+                       const gchar *connection_id = con_ic_iap_get_id (iap);
                        #endif
                        
-                       printf ("debug: iac name=%s, id=%s\n", name, id);
+                       printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
                        
                        /* Get any already-associated connection-specific server account: */
                        gchar *server_account_name = NULL;
-                       if (priv->account_name)
-                               server_account_name = modest_account_mgr_get_connection_specific_smtp (
-                                       priv->account_manager, priv->account_name, name);
+                       server_account_name = modest_account_mgr_get_connection_specific_smtp (
+                               priv->account_manager, connection_id);
                                        
                        /* Add the row to the model: */
                        GtkTreeIter iter;
                        gtk_list_store_append (liststore, &iter);
                        gtk_list_store_set(liststore, &iter, 
-                               MODEL_COL_ID, id, 
-                               MODEL_COL_NAME, name,
+                               MODEL_COL_ID, connection_id, 
+                               MODEL_COL_NAME, connection_name,
                                MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
                                -1);
                                
@@ -187,123 +221,89 @@ modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSp
        #endif
                
        update_model_server_names (self);
+#endif /*MODEST_HAVE_CONIC */
 }
-       
+       
 static void
-on_button_edit (GtkButton *button, gpointer user_data)
+on_button_edit (ModestConnectionSpecificSmtpWindow *self)
 {
-       ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
        ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
+       ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
        
        gchar *id = NULL;
        gchar *connection_name = NULL;
        gchar *server_account_name = NULL;
-       ModestServerAccountData *data = NULL;
+       ModestServerAccountSettings *server_settings = NULL;
        GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
        GtkTreeIter iter;
        GtkTreeModel *model = 0;
        if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
-               gtk_tree_model_get (priv->model, &iter, 
-                                   MODEL_COL_ID, &id, 
-                                   MODEL_COL_NAME, &connection_name, 
+               gtk_tree_model_get (priv->model, &iter,
+                                   MODEL_COL_ID, &id,
+                                   MODEL_COL_NAME, &connection_name,
                                    MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
-                                   MODEL_COL_SERVER_ACCOUNT_DATA, &data,
+                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
                                    -1);
        
                /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
-               /* TODO: Is 0 an allowed libconic IAP ID? 
+               /* TODO: Is 0 an allowed libconic IAP ID?
                 * If not then we should check for it. */
                
                /* Get existing server account data if a server account is already specified: */
-               gboolean data_was_retrieved = FALSE;
-               if (server_account_name && !data) {
-                       data = modest_account_mgr_get_server_account_data (priv->account_manager, 
-                               server_account_name);
-                       if (data)
-                               data_was_retrieved = TRUE;
+               gboolean settings_were_retrieved = FALSE;
+               if (server_account_name && !server_settings) {
+                       server_settings = modest_account_mgr_load_server_settings(mgr, server_account_name);
+                       if (server_settings)
+                               settings_were_retrieved = TRUE;
                }
                
                GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
                modest_connection_specific_smtp_edit_window_set_connection (
-                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
+                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, server_settings);
                        
                /* Delete data, unless it was data from the rowmodel: */
-               if (data_was_retrieved) {
-                       modest_account_mgr_free_server_account_data (priv->account_manager, data);
-                       data = NULL;
+               if (settings_were_retrieved) {
+                       g_object_unref (server_settings);
+                       server_settings = NULL;
                }
                        
-               gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
+               modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (window));
                
-               gboolean dialog_finished = FALSE;
-               while (!dialog_finished)
-               {
-                       gint response = gtk_dialog_run (GTK_DIALOG (window));
-                       if (response == GTK_RESPONSE_OK) {
-                               gtk_widget_hide (window);
-                               dialog_finished = TRUE;
-                               /* Delete any previous data for this row: */
-                               if (data) 
-                               {
-                                       modest_account_mgr_free_server_account_data (priv->account_manager, data);
-                                       data = NULL;
-                               }
-                               
-                               /* Get the new account data and save it in the row for later:
-                                * We free this in finalize(),
-                                * and save it to our configuration in 
-                                * modest_connection_specific_smtp_window_save_server_accounts(). */
-                               data = modest_connection_specific_smtp_edit_window_get_settings (
-                                                       MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
-                                                       priv->account_manager);
-                               
-                               const gchar* server_name = data ? data->hostname : NULL;
-                               gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
-                                               MODEL_COL_SERVER_ACCOUNT_DATA, data,
-                                               MODEL_COL_SERVER_NAME, server_name,
-                                               -1);
+               gint response = gtk_dialog_run (GTK_DIALOG (window));
+               if (response == GTK_RESPONSE_OK) {
+
+                       /* Delete any previous data for this row: */
+                       if (server_settings) {
+                               g_object_unref (server_settings);
+                               server_settings = NULL;
                        }
-                       else
-                       {
-                               if (!modest_connection_specific_smtp_edit_window_is_dirty(
-                                               MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
-                               {
-                                       gtk_widget_hide(window);
-                                       dialog_finished = TRUE;
-                               }
-                               else
-                               {
-                                               
-                                       /* FIXME: Add a logical ID here */
-                                       GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (window), 
-                                               _("All changes made to the SMTP settings will be lost. Continue anyway?")));
-                                       /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
-                        
-                                       const gint dialog_response = gtk_dialog_run (dialog);
-                                       gtk_widget_destroy (GTK_WIDGET (dialog));
-                        
-                                       if (dialog_response == GTK_RESPONSE_OK)
-                                       {
-                                               gtk_widget_hide(window);
-                                               dialog_finished = TRUE;
-                                       }
-                               }
+                       
+                       /* Get the new account data and save it in the row for later:
+                        * We free this in finalize(),
+                        * and save it to our configuration in
+                        * modest_connection_specific_smtp_window_save_server_accounts(). */
+                       server_settings = modest_connection_specific_smtp_edit_window_get_settings (
+                                                                                                   MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window));
+                       
+                       if (server_settings) {
+                               gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
+                                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, server_settings,
+                                                   MODEL_COL_SERVER_NAME, modest_server_account_settings_get_hostname (server_settings),
+                                                   -1);
+                       } else {
+                               gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
+                                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, NULL,
+                                                   MODEL_COL_SERVER_NAME, NULL,
+                                                   MODEL_COL_SERVER_ACCOUNT_NAME, NULL,
+                                                   -1);
                        }
                }
+               gtk_widget_destroy (window);
        }
        g_free (connection_name);
        g_free (id);
        g_free (server_account_name);
-}
-
-static void
-on_button_cancel (GtkButton *button, gpointer user_data)
-{
-       ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
-
-       /* Hide the window.
-        * The code that showed it will respond to the hide signal. */  
-       gtk_widget_hide (GTK_WIDGET (self));
+       update_model_server_names (self);
 }
 
 static void
@@ -350,7 +350,7 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
 
        /* Show the column headers,
         * which does not seem to be the default on Maemo.
-        */                     
+        */
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
        
        /* name column:
@@ -375,13 +375,19 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
        
        /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
        
-       GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
+       GtkWidget *vbox = GTK_DIALOG(self)->vbox;
+       //gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
 
        /* Introductory note: */
+       /* TODO: For some reason this label does not wrap. It is truncated. */
        GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
        gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
        /* So that it is shown without being truncated: */
-       gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
+       gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
+       /* The documentation for gtk_label_set_line_wrap() says that we must 
+        * call gtk_widget_set_size_request() with a hard-coded width, 
+        * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
+       gtk_widget_set_size_request (label, 400, -1);
        gtk_widget_show (label);
        gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
        
@@ -394,27 +400,12 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
        gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
        gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
        gtk_widget_show (GTK_WIDGET (priv->treeview));
-       
-       /* Add the buttons: */
-       GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
-       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
-       gtk_widget_show (hbox);
-       
-       priv->button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
-       gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
-       gtk_widget_show (priv->button_edit);
-       g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
-               G_CALLBACK (on_button_edit), self);
-       
-       GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
-       gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
-       gtk_widget_show (button_cancel);
-       g_signal_connect (G_OBJECT (button_cancel), "clicked",
-               G_CALLBACK (on_button_cancel), self);
-       
-       gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
        gtk_widget_show (vbox);
        
+       /* Hack: we use the response apply to identify the click on the edit button */
+       priv->button_edit = gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_edit"), GTK_RESPONSE_APPLY);
+       gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
+       
        /* Disable the Edit button when nothing is selected: */
        GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
        g_signal_connect (sel, "changed",
@@ -429,6 +420,12 @@ modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow
 
        /* Set window title */
        gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
+
+       g_signal_connect (self, "response", G_CALLBACK (on_response), NULL);
+       
+       hildon_help_dialog_help_enable (GTK_DIALOG(self),
+                                       "email_connectionsspecificsmtpconf",
+                                       modest_maemo_utils_get_osso_context());
 }
 
 ModestConnectionSpecificSmtpWindow*
@@ -438,12 +435,11 @@ modest_connection_specific_smtp_window_new (void)
 }
 
 /** The application should call this when the user changes should be saved.
- * @account_name: Specify this again in case it was not previously known.
  */
 gboolean
-modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self, 
-       const gchar* account_name)
+modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
 {
+       ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
        ModestConnectionSpecificSmtpWindowPrivate *priv = 
                CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
        
@@ -454,71 +450,63 @@ modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpe
 
        /* Walk through the list, reading each row */
        while (valid) {
-       gchar *id = NULL;
+               gchar *id = NULL;
                gchar *connection_name = NULL;
                gchar *server_account_name = NULL;
-               ModestServerAccountData *data = NULL;
+               gchar *server_name = NULL;
+               ModestServerAccountSettings *server_settings = NULL;
                
                gtk_tree_model_get (priv->model, &iter, 
                                    MODEL_COL_ID, &id, 
                                    MODEL_COL_NAME, &connection_name, 
+                                   MODEL_COL_SERVER_NAME, &server_name,
                                    MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
-                                   MODEL_COL_SERVER_ACCOUNT_DATA, &data,
+                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
                                    -1);
                                 
                gboolean success = TRUE;   
-               if (id && data) { /* The presence of data suggests that there is something to save. */
+               if (id && server_settings) { /* The presence of data suggests that there is something to save. */
                        if (!server_account_name) {
                                /* Add a new server account, building a (non-human-visible) name: */
-                               gchar *name_start = g_strdup_printf("%s_specific_%s", 
-                                       priv->account_name, connection_name);
+                               gchar *name_start = g_strdup_printf("specific_%s", connection_name);
                                server_account_name = modest_account_mgr_get_unused_account_name (
                                        priv->account_manager, name_start, TRUE /* server account. */);
                                g_assert (server_account_name);
                                g_free (name_start);
                                
-                               success = modest_account_mgr_add_server_account (priv->account_manager,
-                                                                                server_account_name,
-                                                                                data->hostname, 0,
-                                                                                data->username, data->password,
-                                                                                MODEST_PROTOCOL_TRANSPORT_SMTP,
-                                                                                data->security,
-                                                                                data->secure_auth);
-                                       
+                               modest_server_account_settings_set_account_name (server_settings, server_account_name);
+                               success = modest_account_mgr_save_server_settings (mgr, server_settings);
+                               if (success) {
+                                       TnyAccount *account = TNY_ACCOUNT (modest_tny_account_store_new_connection_specific_transport_account 
+                                                                          (modest_runtime_get_account_store (),
+                                                                           server_account_name));
+                                       if (account)
+                                               g_object_unref (account);
+                               }
+                               
                                /* associate the specific server account with this connection for this account: */
                                success = success && modest_account_mgr_set_connection_specific_smtp (
-                                       priv->account_manager, priv->account_name,
-                                        connection_name, server_account_name);
-       
+                                       priv->account_manager, id, server_account_name);
+                               
                                /* Save the new name in the treemodel, so it can be edited again later: */
                                gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
                                        MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
                                
                        } else {
-                               /* Change an existing server account: */
-                               success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
-                                       MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
-                                               
-                               modest_server_account_set_username (priv->account_manager, server_account_name,
-                                       data->username);
-                                                       
-                               modest_server_account_set_password (priv->account_manager, server_account_name,
-                                       data->password);
-                                               
-                               modest_server_account_set_secure_auth (priv->account_manager, server_account_name, 
-                                       data->secure_auth);
-                                               
-                               modest_server_account_set_security (priv->account_manager, server_account_name, 
-                                       data->security);
-                               
-                               modest_account_mgr_set_int (priv->account_manager, server_account_name,
-                                               MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
+                               modest_account_mgr_save_server_settings (mgr, server_settings);
                        }
+               } else if (id && server_name && 
+                          !strcmp (server_name, _("mcen_ia_optionalsmtp_notdefined"))) {
+                       modest_account_mgr_remove_connection_specific_smtp (priv->account_manager, 
+                                                                           id);
+                       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
+                                           MODEL_COL_SERVER_ACCOUNT_NAME, NULL, -1);
                }
                
                g_free (connection_name);
                g_free (id);
                g_free (server_account_name);
+               g_free (server_name);
                
                if (!success)
                        return FALSE;
@@ -541,14 +529,21 @@ void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
        while (valid) {
                
                gchar *server_account_name = NULL;
+               ModestServerAccountSettings *server_settings = NULL;
                gtk_tree_model_get (priv->model, &iter, 
                                    MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
-                                   -1);
-                                
-               if (server_account_name) {
+                                   MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
+                                   -1);        
+               if (server_settings && modest_server_account_settings_get_hostname (server_settings)
+                   && (modest_server_account_settings_get_hostname (server_settings) [0] != '\0')) {
+                       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
+                                           MODEL_COL_SERVER_NAME, modest_server_account_settings_get_hostname (server_settings),
+                                           -1);
+               } else if (server_account_name) {
+                       
                        /* Get the server hostname and show it in the treemodel: */     
-                       gchar *hostname = modest_account_mgr_get_string (priv->account_manager, 
-                               server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
+                       gchar *hostname = modest_account_mgr_get_server_account_hostname (priv->account_manager, 
+                                                                                         server_account_name);
                        gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
                                            MODEL_COL_SERVER_NAME, hostname,
                                            -1);
@@ -564,3 +559,23 @@ void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
        }
 }
 
+static void
+on_response (GtkDialog *dialog,
+            gint response,
+            gpointer user_data)
+{
+       switch (response) {
+       case GTK_RESPONSE_APPLY:
+               /* We use it for the edit button */
+               on_button_edit (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog));
+               g_signal_stop_emission_by_name (dialog, "response");
+               break;
+       case GTK_RESPONSE_CLOSE:
+       case GTK_RESPONSE_NONE:
+       case GTK_RESPONSE_DELETE_EVENT:
+               /* Generated as a response to delete-event, i.e,
+                  pressin Esc, or by pressing the Close button */
+               modest_connection_specific_smtp_window_save_server_accounts (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog));
+               gtk_widget_destroy (GTK_WIDGET (dialog));
+       }
+}