59eb8afea81bd7636c13e6be06ad4c49a7dccbce
[modest] / src / maemo / modest-connection-specific-smtp-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "modest-connection-specific-smtp-window.h"
31 #include "modest-connection-specific-smtp-edit-window.h"
32 #include <modest-account-mgr-helpers.h>
33 #include "widgets/modest-ui-constants.h"
34
35 #include <modest-runtime.h>
36 #include <tny-maemo-conic-device.h>
37
38 #include <gtk/gtktreeview.h>
39 #include <gtk/gtkcellrenderertext.h>
40 #include <gtk/gtkliststore.h>
41 #include <gtk/gtkscrolledwindow.h>
42 #include <gtk/gtkbutton.h>
43 #include <gtk/gtkhbox.h>
44 #include <gtk/gtkvbox.h>
45 #include <gtk/gtkstock.h>
46
47 #include "modest-hildon-includes.h"
48 #include "modest-platform.h"
49
50 #include <glib/gi18n.h>
51
52 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
53
54 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
55         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
56
57 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
58
59 struct _ModestConnectionSpecificSmtpWindowPrivate
60 {
61         GtkTreeView *treeview;
62         GtkTreeModel *model;
63         GtkWidget *button_edit;
64         
65         ModestAccountMgr *account_manager;
66         gchar* account_name;
67 };
68
69 static void
70 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
71                                                                                                                         GValue *value, GParamSpec *pspec)
72 {
73         switch (property_id) {
74         default:
75                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76         }
77 }
78
79 static void
80 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
81                                                                                                                         const GValue *value, GParamSpec *pspec)
82 {
83         switch (property_id) {
84         default:
85                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86         }
87 }
88
89 static void
90 modest_connection_specific_smtp_window_dispose (GObject *object)
91 {
92         if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
93                 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
94 }
95
96 enum MODEL_COLS {
97         MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
98         MODEL_COL_ID = 1, /* libconic IAP ID: a string */
99         MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
100         MODEL_COL_SERVER_NAME = 3, /* a string */
101         MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
102 };
103
104
105 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
106
107 static void
108 modest_connection_specific_smtp_window_finalize (GObject *object)
109 {
110         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
111         
112         /* Free all the data items from the treemodel: */
113         GtkTreeIter iter;
114         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
115         while (valid) {
116                 ModestServerAccountData *data = NULL;
117                 
118                 gtk_tree_model_get (priv->model, &iter, 
119                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
120                                     -1);
121                                  
122                 if (data)
123                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
124                         
125                 /* Get next row: */
126                 valid = gtk_tree_model_iter_next (priv->model, &iter);
127         }
128         
129         g_object_unref (G_OBJECT (priv->model));
130         g_free (priv->account_name);
131         
132         G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
133 }
134
135 static void
136 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
137 {
138         GObjectClass *object_class = G_OBJECT_CLASS (klass);
139
140         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
141
142         object_class->get_property = modest_connection_specific_smtp_window_get_property;
143         object_class->set_property = modest_connection_specific_smtp_window_set_property;
144         object_class->dispose = modest_connection_specific_smtp_window_dispose;
145         object_class->finalize = modest_connection_specific_smtp_window_finalize;
146 }
147
148 /* libconic does not return a list of connections in scratchbox,
149  * so enable this to put a fake row in the list,
150  * so we can test other parts of the code. */
151 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
152
153 void
154 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
155         const gchar* account_name)
156 {
157         ModestConnectionSpecificSmtpWindowPrivate *priv = 
158                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
159         priv->account_manager = account_manager;
160         priv->account_name = account_name ? g_strdup (account_name) : NULL;
161         
162         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
163         
164         TnyDevice *device = modest_runtime_get_device ();
165         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
166         
167         /* Get the list of Internet Access Points: */
168         #ifdef DEBUG_WITHOUT_LIBCONIC
169         GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
170         #else
171         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
172         GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
173         #endif
174         
175         /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
176         
177         GSList* iter = list_iaps;
178         while (iter) {
179                 ConIcIap *iap = (ConIcIap*)iter->data;
180                 if (iap) {
181                         #ifdef DEBUG_WITHOUT_LIBCONIC
182                         const gchar *name = "debug name";
183                         const gchar *id = "debug id";
184                         #else
185                         const gchar *name = con_ic_iap_get_name (iap);
186                         const gchar *id = con_ic_iap_get_id (iap);
187                         #endif
188                         
189                         printf ("debug: iac name=%s, id=%s\n", name, id);
190                         
191                         /* Get any already-associated connection-specific server account: */
192                         gchar *server_account_name = NULL;
193                         if (priv->account_name)
194                                 server_account_name = modest_account_mgr_get_connection_specific_smtp (
195                                         priv->account_manager, priv->account_name, name);
196                                         
197                         /* Add the row to the model: */
198                         GtkTreeIter iter;
199                         gtk_list_store_append (liststore, &iter);
200                         gtk_list_store_set(liststore, &iter, 
201                                 MODEL_COL_ID, id, 
202                                 MODEL_COL_NAME, name,
203                                 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
204                                 -1);
205                                 
206                         g_free (server_account_name);
207                 }
208                 
209                 iter = g_slist_next (iter);     
210         }
211                 
212         #ifndef DEBUG_WITHOUT_LIBCONIC
213         if (list_iaps)
214                 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
215         #endif
216                 
217         update_model_server_names (self);
218 }
219         
220 static void
221 on_button_edit (GtkButton *button, gpointer user_data)
222 {
223         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
224         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
225         
226         gchar *id = NULL;
227         gchar *connection_name = NULL;
228         gchar *server_account_name = NULL;
229         ModestServerAccountData *data = NULL;
230         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
231         GtkTreeIter iter;
232         GtkTreeModel *model = 0;
233         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
234                 gtk_tree_model_get (priv->model, &iter, 
235                                     MODEL_COL_ID, &id, 
236                                     MODEL_COL_NAME, &connection_name, 
237                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
238                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
239                                     -1);
240         
241                 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
242                 /* TODO: Is 0 an allowed libconic IAP ID? 
243                  * If not then we should check for it. */
244                 
245                 /* Get existing server account data if a server account is already specified: */
246                 gboolean data_was_retrieved = FALSE;
247                 if (server_account_name && !data) {
248                         data = modest_account_mgr_get_server_account_data (priv->account_manager, 
249                                 server_account_name);
250                         if (data)
251                                 data_was_retrieved = TRUE;
252                 }
253                 
254                 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
255                 modest_connection_specific_smtp_edit_window_set_connection (
256                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
257                         
258                 /* Delete data, unless it was data from the rowmodel: */
259                 if (data_was_retrieved) {
260                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
261                         data = NULL;
262                 }
263                         
264                 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
265                 
266                 gboolean dialog_finished = FALSE;
267                 while (!dialog_finished)
268                 {
269                         gint response = gtk_dialog_run (GTK_DIALOG (window));
270                         if (response == GTK_RESPONSE_OK) {
271                                 gtk_widget_hide (window);
272                                 dialog_finished = TRUE;
273                                 /* Delete any previous data for this row: */
274                                 if (data) 
275                                 {
276                                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
277                                         data = NULL;
278                                 }
279                                 
280                                 /* Get the new account data and save it in the row for later:
281                                  * We free this in finalize(),
282                                  * and save it to our configuration in 
283                                  * modest_connection_specific_smtp_window_save_server_accounts(). */
284                                 data = modest_connection_specific_smtp_edit_window_get_settings (
285                                                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
286                                                         priv->account_manager);
287                                 
288                                 const gchar* server_name = data ? data->hostname : NULL;
289                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
290                                                 MODEL_COL_SERVER_ACCOUNT_DATA, data,
291                                                 MODEL_COL_SERVER_NAME, server_name,
292                                                 -1);
293                         }
294                         else
295                         {
296                                 if (!modest_connection_specific_smtp_edit_window_is_dirty(
297                                                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
298                                 {
299                                         gtk_widget_hide(window);
300                                         dialog_finished = TRUE;
301                                 }
302                                 else
303                                 {
304                                                 
305                                         gint response;
306                                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window), 
307                                                                             _("imum_nc_wizard_confirm_lose_changes"));                   
308                                         if (response == GTK_RESPONSE_OK)
309                                         {
310                                                 gtk_widget_hide(window);
311                                                 dialog_finished = TRUE;
312                                         }
313                                 }
314                         }
315                 }
316         }
317         g_free (connection_name);
318         g_free (id);
319         g_free (server_account_name);
320 }
321
322 static void
323 on_button_cancel (GtkButton *button, gpointer user_data)
324 {
325         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
326
327         /* Hide the window.
328          * The code that showed it will respond to the hide signal. */  
329         gtk_widget_hide (GTK_WIDGET (self));
330 }
331
332 static void
333 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
334 {
335         ModestConnectionSpecificSmtpWindowPrivate *priv = 
336                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
337
338         GtkTreeModel *model = NULL;
339         GtkTreeIter iter;
340         const gboolean has_selection =
341                 gtk_tree_selection_get_selected (sel, &model, &iter);
342
343         gtk_widget_set_sensitive (priv->button_edit, has_selection);
344 }
345
346 static void
347 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
348 {
349         ModestWindowMgr *mgr;
350
351         /* Specify a default size, because the GtkTreeView's default requested size  
352          * is not big enough: */
353         gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
354         
355         /* This seems to be necessary to make the window show at the front with decoration.
356          * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
357          * then the window will be below the others. */
358         gtk_window_set_type_hint (GTK_WINDOW (self),
359                             GDK_WINDOW_TYPE_HINT_DIALOG);
360                             
361         ModestConnectionSpecificSmtpWindowPrivate *priv = 
362                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
363
364         /* Create a tree model for the tree view:
365          * with a string for the name, a string for the server name, and an int for the ID.
366          * This must match our MODEL_COLS enum constants.
367          */
368         priv->model = GTK_TREE_MODEL (gtk_list_store_new (5, 
369                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
370
371         /* Setup the tree view: */
372         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
373
374         /* Show the column headers,
375          * which does not seem to be the default on Maemo.
376          */                     
377         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
378         
379         /* name column:
380          * The ID model column in not shown in the view. */
381         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
382         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
383         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
384         gtk_tree_view_column_set_attributes (view_column, renderer, 
385         "text", MODEL_COL_NAME, NULL);
386         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
387         gtk_tree_view_append_column (priv->treeview, view_column);
388
389         
390         /* server name column: */
391         view_column = gtk_tree_view_column_new ();
392         renderer = gtk_cell_renderer_text_new ();
393         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
394         gtk_tree_view_column_set_attributes (view_column, renderer, 
395         "text", MODEL_COL_SERVER_NAME, NULL);
396         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
397         gtk_tree_view_append_column (priv->treeview, view_column);
398         
399         /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
400         
401         GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
402
403         /* Introductory note: */
404         /* TODO: For some reason this label does not wrap. It is truncated. */
405         GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
406         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
407         /* So that it is shown without being truncated: */
408         gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
409         /* The documentation for gtk_label_set_line_wrap() says that we must 
410          * call gtk_widget_set_size_request() with a hard-coded width, 
411          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
412         gtk_widget_set_size_request (label, 400, -1);
413         gtk_widget_show (label);
414         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
415         
416         /* Put the treeview in a scrolled window and add it to the box: */
417         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
418         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
419         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
420                 GTK_POLICY_AUTOMATIC);
421         gtk_widget_show (scrolled_window);
422         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
423         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
424         gtk_widget_show (GTK_WIDGET (priv->treeview));
425         
426         /* Add the buttons: */
427         GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
428         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
429         gtk_widget_show (hbox);
430         
431         priv->button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
432         gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
433         gtk_widget_show (priv->button_edit);
434         g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
435                 G_CALLBACK (on_button_edit), self);
436         
437         GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
438         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
439         gtk_widget_show (button_cancel);
440         g_signal_connect (G_OBJECT (button_cancel), "clicked",
441                 G_CALLBACK (on_button_cancel), self);
442         
443         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
444         gtk_widget_show (vbox);
445         
446         /* Disable the Edit button when nothing is selected: */
447         GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
448         g_signal_connect (sel, "changed",
449                           G_CALLBACK(on_selection_changed), self);
450         on_selection_changed (sel, self);
451         
452         /* When this window is shown, hibernation should not be possible, 
453          * because there is no sensible way to save the state: */
454         mgr = modest_runtime_get_window_mgr ();
455         modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr, 
456                                                                      GTK_WINDOW (self)); 
457
458         /* Set window title */
459         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
460 }
461
462 ModestConnectionSpecificSmtpWindow*
463 modest_connection_specific_smtp_window_new (void)
464 {
465         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
466 }
467
468 /** The application should call this when the user changes should be saved.
469  * @account_name: Specify this again in case it was not previously known.
470  */
471 gboolean
472 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self, 
473         const gchar* account_name)
474 {
475         ModestConnectionSpecificSmtpWindowPrivate *priv = 
476                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
477         
478         
479         /* Get the first iter in the list */
480         GtkTreeIter iter;
481         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
482
483         /* Walk through the list, reading each row */
484         while (valid) {
485         gchar *id = NULL;
486                 gchar *connection_name = NULL;
487                 gchar *server_account_name = NULL;
488                 ModestServerAccountData *data = NULL;
489                 
490                 gtk_tree_model_get (priv->model, &iter, 
491                                     MODEL_COL_ID, &id, 
492                                     MODEL_COL_NAME, &connection_name, 
493                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
494                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
495                                     -1);
496                                  
497                 gboolean success = TRUE;   
498                 if (id && data) { /* The presence of data suggests that there is something to save. */
499                         if (!server_account_name) {
500                                 /* Add a new server account, building a (non-human-visible) name: */
501                                 gchar *name_start = g_strdup_printf("%s_specific_%s", 
502                                         priv->account_name, connection_name);
503                                 server_account_name = modest_account_mgr_get_unused_account_name (
504                                         priv->account_manager, name_start, TRUE /* server account. */);
505                                 g_assert (server_account_name);
506                                 g_free (name_start);
507                                 
508                                 success = modest_account_mgr_add_server_account (priv->account_manager,
509                                                                                  server_account_name,
510                                                                                  data->hostname, 0,
511                                                                                  data->username, data->password,
512                                                                                  MODEST_PROTOCOL_TRANSPORT_SMTP,
513                                                                                  data->security,
514                                                                                  data->secure_auth);
515                                         
516                                 /* associate the specific server account with this connection for this account: */
517                                 success = success && modest_account_mgr_set_connection_specific_smtp (
518                                         priv->account_manager, priv->account_name,
519                                          connection_name, server_account_name);
520         
521                                 /* Save the new name in the treemodel, so it can be edited again later: */
522                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
523                                         MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
524                                 
525                         } else {
526                                 /* Change an existing server account: */
527                                 success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
528                                         MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
529                                                 
530                                 modest_server_account_set_username (priv->account_manager, server_account_name,
531                                         data->username);
532                                                         
533                                 modest_server_account_set_password (priv->account_manager, server_account_name,
534                                         data->password);
535                                                 
536                                 modest_server_account_set_secure_auth (priv->account_manager, server_account_name, 
537                                         data->secure_auth);
538                                                 
539                                 modest_server_account_set_security (priv->account_manager, server_account_name, 
540                                         data->security);
541                                 
542                                 modest_account_mgr_set_int (priv->account_manager, server_account_name,
543                                                 MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
544                         }
545                 }
546                 
547                 g_free (connection_name);
548                 g_free (id);
549                 g_free (server_account_name);
550                 
551                 if (!success)
552                         return FALSE;
553                         
554                 /* Get next row: */
555                 valid = gtk_tree_model_iter_next (priv->model, &iter);
556         }
557         
558         update_model_server_names (self);
559         
560         return TRUE;
561 }
562
563 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
564 {
565         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
566
567         GtkTreeIter iter;
568         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
569         while (valid) {
570                 
571                 gchar *server_account_name = NULL;
572                 gtk_tree_model_get (priv->model, &iter, 
573                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
574                                     -1);
575                                  
576                 if (server_account_name) {
577                         /* Get the server hostname and show it in the treemodel: */     
578                         gchar *hostname = modest_account_mgr_get_string (priv->account_manager, 
579                                 server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
580                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
581                                             MODEL_COL_SERVER_NAME, hostname,
582                                             -1);
583                         g_free (hostname);
584                 } else {
585                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
586                                             MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
587                                             -1);
588                 }
589                         
590                 /* Get next row: */
591                 valid = gtk_tree_model_iter_next (priv->model, &iter);
592         }
593 }
594