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