* The "account-changed" signal is now properly managed in the ModestTnyAccountStore...
[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 };
67
68 static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data);
69
70 static void
71 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
72                                                                                                                         GValue *value, GParamSpec *pspec)
73 {
74         switch (property_id) {
75         default:
76                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77         }
78 }
79
80 static void
81 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
82                                                                                                                         const GValue *value, GParamSpec *pspec)
83 {
84         switch (property_id) {
85         default:
86                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
87         }
88 }
89
90 static void
91 modest_connection_specific_smtp_window_dispose (GObject *object)
92 {
93         if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
94                 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
95 }
96
97 enum MODEL_COLS {
98         MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
99         MODEL_COL_ID = 1, /* libconic IAP ID: a string */
100         MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
101         MODEL_COL_SERVER_NAME = 3, /* a string */
102         MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
103 };
104
105
106 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
107
108 static void
109 modest_connection_specific_smtp_window_finalize (GObject *object)
110 {
111         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
112         
113         /* Free all the data items from the treemodel: */
114         GtkTreeIter iter;
115         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
116         while (valid) {
117                 ModestServerAccountData *data = NULL;
118                 
119                 gtk_tree_model_get (priv->model, &iter, 
120                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
121                                     -1);
122                                  
123                 if (data)
124                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
125                         
126                 /* Get next row: */
127                 valid = gtk_tree_model_iter_next (priv->model, &iter);
128         }
129         
130         g_object_unref (G_OBJECT (priv->model));
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 {
156         ModestConnectionSpecificSmtpWindowPrivate *priv = 
157                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
158         priv->account_manager = account_manager;
159         
160         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
161         
162         TnyDevice *device = modest_runtime_get_device ();
163         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
164         
165         /* Get the list of Internet Access Points: */
166         #ifdef DEBUG_WITHOUT_LIBCONIC
167         GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
168         #else
169         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
170         GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
171         #endif
172         
173         /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
174         
175         GSList* iter = list_iaps;
176         while (iter) {
177                 ConIcIap *iap = (ConIcIap*)iter->data;
178                 if (iap) {
179                         #ifdef DEBUG_WITHOUT_LIBCONIC
180                         const gchar *connection_name = "debug name";
181                         const gchar *connection_id = "debug id";
182                         #else
183                         const gchar *connection_name = con_ic_iap_get_name (iap);
184                         const gchar *connection_id = con_ic_iap_get_id (iap);
185                         #endif
186                         
187                         printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
188                         
189                         /* Get any already-associated connection-specific server account: */
190                         gchar *server_account_name = NULL;
191                         server_account_name = modest_account_mgr_get_connection_specific_smtp (
192                                 priv->account_manager, connection_name);
193                                         
194                         /* Add the row to the model: */
195                         GtkTreeIter iter;
196                         gtk_list_store_append (liststore, &iter);
197                         gtk_list_store_set(liststore, &iter, 
198                                 MODEL_COL_ID, connection_id, 
199                                 MODEL_COL_NAME, connection_name,
200                                 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
201                                 -1);
202                                 
203                         g_free (server_account_name);
204                 }
205                 
206                 iter = g_slist_next (iter);     
207         }
208                 
209         #ifndef DEBUG_WITHOUT_LIBCONIC
210         if (list_iaps)
211                 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
212         #endif
213                 
214         update_model_server_names (self);
215 }
216         
217 static void
218 on_button_edit (GtkButton *button, gpointer user_data)
219 {
220         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
221         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
222         
223         gchar *id = NULL;
224         gchar *connection_name = NULL;
225         gchar *server_account_name = NULL;
226         ModestServerAccountData *data = NULL;
227         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
228         GtkTreeIter iter;
229         GtkTreeModel *model = 0;
230         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
231                 gtk_tree_model_get (priv->model, &iter, 
232                                     MODEL_COL_ID, &id, 
233                                     MODEL_COL_NAME, &connection_name, 
234                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
235                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
236                                     -1);
237         
238                 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
239                 /* TODO: Is 0 an allowed libconic IAP ID? 
240                  * If not then we should check for it. */
241                 
242                 /* Get existing server account data if a server account is already specified: */
243                 gboolean data_was_retrieved = FALSE;
244                 if (server_account_name && !data) {
245                         data = modest_account_mgr_get_server_account_data (priv->account_manager, 
246                                 server_account_name);
247                         if (data)
248                                 data_was_retrieved = TRUE;
249                 }
250                 
251                 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
252                 modest_connection_specific_smtp_edit_window_set_connection (
253                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
254                         
255                 /* Delete data, unless it was data from the rowmodel: */
256                 if (data_was_retrieved) {
257                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
258                         data = NULL;
259                 }
260                         
261                 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
262                 
263                 gboolean dialog_finished = FALSE;
264                 while (!dialog_finished)
265                 {
266                         gint response = gtk_dialog_run (GTK_DIALOG (window));
267                         if (response == GTK_RESPONSE_OK) {
268                                 gtk_widget_hide (window);
269                                 dialog_finished = TRUE;
270                                 /* Delete any previous data for this row: */
271                                 if (data) 
272                                 {
273                                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
274                                         data = NULL;
275                                 }
276                                 
277                                 /* Get the new account data and save it in the row for later:
278                                  * We free this in finalize(),
279                                  * and save it to our configuration in 
280                                  * modest_connection_specific_smtp_window_save_server_accounts(). */
281                                 data = modest_connection_specific_smtp_edit_window_get_settings (
282                                                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
283                                                         priv->account_manager);
284                                 
285                                 const gchar* server_name = data ? data->hostname : NULL;
286                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
287                                                 MODEL_COL_SERVER_ACCOUNT_DATA, data,
288                                                 MODEL_COL_SERVER_NAME, server_name,
289                                                 -1);
290                         }
291                         else
292                         {
293                                 if (!modest_connection_specific_smtp_edit_window_is_dirty(
294                                                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
295                                 {
296                                         gtk_widget_hide(window);
297                                         dialog_finished = TRUE;
298                                 }
299                                 else
300                                 {
301                                                 
302                                         gint response;
303                                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window), 
304                                                                             _("imum_nc_wizard_confirm_lose_changes"));                   
305                                         if (response == GTK_RESPONSE_OK)
306                                         {
307                                                 gtk_widget_hide(window);
308                                                 dialog_finished = TRUE;
309                                         }
310                                 }
311                         }
312                 }
313         }
314         g_free (connection_name);
315         g_free (id);
316         g_free (server_account_name);
317 }
318
319 static void
320 on_button_cancel (GtkButton *button, gpointer user_data)
321 {
322         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
323
324         /* Hide the window.
325          * The code that showed it will respond to the hide signal. */  
326         gtk_widget_hide (GTK_WIDGET (self));
327 }
328
329 static void
330 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
331 {
332         ModestConnectionSpecificSmtpWindowPrivate *priv = 
333                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
334
335         GtkTreeModel *model = NULL;
336         GtkTreeIter iter;
337         const gboolean has_selection =
338                 gtk_tree_selection_get_selected (sel, &model, &iter);
339
340         gtk_widget_set_sensitive (priv->button_edit, has_selection);
341 }
342
343 static void
344 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
345 {
346         ModestWindowMgr *mgr;
347
348         /* Specify a default size, because the GtkTreeView's default requested size  
349          * is not big enough: */
350         gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
351         
352         /* This seems to be necessary to make the window show at the front with decoration.
353          * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
354          * then the window will be below the others. */
355         gtk_window_set_type_hint (GTK_WINDOW (self),
356                             GDK_WINDOW_TYPE_HINT_DIALOG);
357                             
358         ModestConnectionSpecificSmtpWindowPrivate *priv = 
359                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
360
361         /* Create a tree model for the tree view:
362          * with a string for the name, a string for the server name, and an int for the ID.
363          * This must match our MODEL_COLS enum constants.
364          */
365         priv->model = GTK_TREE_MODEL (gtk_list_store_new (5, 
366                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
367
368         /* Setup the tree view: */
369         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
370
371         /* Show the column headers,
372          * which does not seem to be the default on Maemo.
373          */                     
374         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
375         
376         /* name column:
377          * The ID model column in not shown in the view. */
378         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
379         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
380         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
381         gtk_tree_view_column_set_attributes (view_column, renderer, 
382         "text", MODEL_COL_NAME, NULL);
383         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
384         gtk_tree_view_append_column (priv->treeview, view_column);
385
386         
387         /* server name column: */
388         view_column = gtk_tree_view_column_new ();
389         renderer = gtk_cell_renderer_text_new ();
390         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
391         gtk_tree_view_column_set_attributes (view_column, renderer, 
392         "text", MODEL_COL_SERVER_NAME, NULL);
393         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
394         gtk_tree_view_append_column (priv->treeview, view_column);
395         
396         /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
397         
398         GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
399
400         /* Introductory note: */
401         /* TODO: For some reason this label does not wrap. It is truncated. */
402         GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
403         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
404         /* So that it is shown without being truncated: */
405         gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
406         /* The documentation for gtk_label_set_line_wrap() says that we must 
407          * call gtk_widget_set_size_request() with a hard-coded width, 
408          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
409         gtk_widget_set_size_request (label, 400, -1);
410         gtk_widget_show (label);
411         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
412         
413         /* Put the treeview in a scrolled window and add it to the box: */
414         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
415         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
416         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
417                 GTK_POLICY_AUTOMATIC);
418         gtk_widget_show (scrolled_window);
419         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
420         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
421         gtk_widget_show (GTK_WIDGET (priv->treeview));
422         
423         /* Add the buttons: */
424         GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
425         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
426         gtk_widget_show (hbox);
427         
428         priv->button_edit = gtk_button_new_from_stock (_("mcen_bd_edit"));
429         gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
430         gtk_widget_show (priv->button_edit);
431         g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
432                 G_CALLBACK (on_button_edit), self);
433         
434         GtkWidget *button_cancel = gtk_button_new_from_stock (_("mcen_bd_close"));
435         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
436         gtk_widget_show (button_cancel);
437         g_signal_connect (G_OBJECT (button_cancel), "clicked",
438                 G_CALLBACK (on_button_cancel), self);
439         
440         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
441         gtk_widget_show (vbox);
442         
443         /* Disable the Edit button when nothing is selected: */
444         GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
445         g_signal_connect (sel, "changed",
446                           G_CALLBACK(on_selection_changed), self);
447         on_selection_changed (sel, self);
448         
449         /* When this window is shown, hibernation should not be possible, 
450          * because there is no sensible way to save the state: */
451         mgr = modest_runtime_get_window_mgr ();
452         modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr, 
453                                                                      GTK_WINDOW (self)); 
454
455         /* Set window title */
456         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
457
458         /* Track key presses to close the window if the Escape is pressed */
459         g_signal_connect (G_OBJECT (self), 
460                           "key-press-event", 
461                           G_CALLBACK (on_key_pressed), NULL);
462 }
463
464 ModestConnectionSpecificSmtpWindow*
465 modest_connection_specific_smtp_window_new (void)
466 {
467         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
468 }
469
470 /** The application should call this when the user changes should be saved.
471  */
472 gboolean
473 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
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("specific_%s", connection_name);
502                                 server_account_name = modest_account_mgr_get_unused_account_name (
503                                         priv->account_manager, name_start, TRUE /* server account. */);
504                                 g_assert (server_account_name);
505                                 g_free (name_start);
506                                 
507                                 success = modest_account_mgr_add_server_account (priv->account_manager,
508                                                                                  server_account_name,
509                                                                                  data->hostname, 0,
510                                                                                  data->username, data->password,
511                                                                                  MODEST_PROTOCOL_TRANSPORT_SMTP,
512                                                                                  data->security,
513                                                                                  data->secure_auth);
514                                         
515                                 /* associate the specific server account with this connection for this account: */
516                                 success = success && modest_account_mgr_set_connection_specific_smtp (
517                                         priv->account_manager, connection_name, server_account_name);
518         
519                                 /* Save the new name in the treemodel, so it can be edited again later: */
520                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
521                                         MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
522                                 
523                         } else {
524                                 /* Change an existing server account: */
525                                 modest_account_mgr_set_server_account_hostname (priv->account_manager, server_account_name, 
526                                                                                 data->hostname);
527                                                 
528                                 modest_account_mgr_set_server_account_username (priv->account_manager, server_account_name,
529                                         data->username);
530                                                         
531                                 modest_account_mgr_set_server_account_password (priv->account_manager, server_account_name,
532                                         data->password);
533                                                 
534                                 modest_account_mgr_set_server_account_secure_auth (priv->account_manager, server_account_name, 
535                                         data->secure_auth);
536                                                 
537                                 modest_account_mgr_set_server_account_security (priv->account_manager, server_account_name, 
538                                         data->security);
539
540                                 modest_account_mgr_set_server_account_port (priv->account_manager, server_account_name, data->port);
541                         }
542                 }
543                 
544                 g_free (connection_name);
545                 g_free (id);
546                 g_free (server_account_name);
547                 
548                 if (!success)
549                         return FALSE;
550                         
551                 /* Get next row: */
552                 valid = gtk_tree_model_iter_next (priv->model, &iter);
553         }
554         
555         update_model_server_names (self);
556         
557         return TRUE;
558 }
559
560 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
561 {
562         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
563
564         GtkTreeIter iter;
565         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
566         while (valid) {
567                 
568                 gchar *server_account_name = NULL;
569                 gtk_tree_model_get (priv->model, &iter, 
570                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
571                                     -1);
572                                  
573                 if (server_account_name) {
574                         /* Get the server hostname and show it in the treemodel: */     
575                         gchar *hostname = modest_account_mgr_get_server_account_hostname (priv->account_manager, 
576                                                                                           server_account_name);
577                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
578                                             MODEL_COL_SERVER_NAME, hostname,
579                                             -1);
580                         g_free (hostname);
581                 } else {
582                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
583                                             MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
584                                             -1);
585                 }
586                         
587                 /* Get next row: */
588                 valid = gtk_tree_model_iter_next (priv->model, &iter);
589         }
590 }
591
592 static gboolean
593 on_key_pressed (GtkWidget *self,
594                 GdkEventKey *event,
595                 gpointer user_data)
596 {
597         if (event->keyval == GDK_Escape) {
598                 /* Simulate a press on Cancel to close the dialog */
599                 on_button_cancel (NULL, self);
600         }
601         
602         return FALSE;
603 }