* make modest_text_utils_get_display_date return a ptr to
[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                                 if (data) {
286                                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
287                                                             MODEL_COL_SERVER_ACCOUNT_DATA, data,
288                                                             MODEL_COL_SERVER_NAME, data->hostname,
289                                                             -1);
290                                 } else {
291                                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
292                                                             MODEL_COL_SERVER_ACCOUNT_DATA, NULL,
293                                                             MODEL_COL_SERVER_NAME, NULL,
294                                                             MODEL_COL_SERVER_ACCOUNT_NAME, NULL,
295                                                             -1);
296                                 }
297                         }
298                         else
299                         {
300                                 if (!modest_connection_specific_smtp_edit_window_is_dirty(
301                                                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
302                                 {
303                                         gtk_widget_hide(window);
304                                         dialog_finished = TRUE;
305                                 }
306                                 else
307                                 {
308                                                 
309                                         gint response;
310                                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window), 
311                                                                             _("imum_nc_wizard_confirm_lose_changes"));                   
312                                         if (response == GTK_RESPONSE_OK)
313                                         {
314                                                 gtk_widget_hide(window);
315                                                 dialog_finished = TRUE;
316                                         }
317                                 }
318                         }
319                 }
320         }
321         g_free (connection_name);
322         g_free (id);
323         g_free (server_account_name);
324         update_model_server_names (self);
325 }
326
327 static void
328 on_button_cancel (GtkButton *button, gpointer user_data)
329 {
330         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
331
332         /* Hide the window.
333          * The code that showed it will respond to the hide signal. */  
334         gtk_widget_hide (GTK_WIDGET (self));
335 }
336
337 static void
338 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
339 {
340         ModestConnectionSpecificSmtpWindowPrivate *priv = 
341                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
342
343         GtkTreeModel *model = NULL;
344         GtkTreeIter iter;
345         const gboolean has_selection =
346                 gtk_tree_selection_get_selected (sel, &model, &iter);
347
348         gtk_widget_set_sensitive (priv->button_edit, has_selection);
349 }
350
351 static void
352 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
353 {
354         ModestWindowMgr *mgr;
355
356         /* Specify a default size, because the GtkTreeView's default requested size  
357          * is not big enough: */
358         gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
359         
360         /* This seems to be necessary to make the window show at the front with decoration.
361          * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
362          * then the window will be below the others. */
363         gtk_window_set_type_hint (GTK_WINDOW (self),
364                             GDK_WINDOW_TYPE_HINT_DIALOG);
365                             
366         ModestConnectionSpecificSmtpWindowPrivate *priv = 
367                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
368
369         /* Create a tree model for the tree view:
370          * with a string for the name, a string for the server name, and an int for the ID.
371          * This must match our MODEL_COLS enum constants.
372          */
373         priv->model = GTK_TREE_MODEL (gtk_list_store_new (5, 
374                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
375
376         /* Setup the tree view: */
377         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
378
379         /* Show the column headers,
380          * which does not seem to be the default on Maemo.
381          */                     
382         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
383         
384         /* name column:
385          * The ID model column in not shown in the view. */
386         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
387         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
388         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
389         gtk_tree_view_column_set_attributes (view_column, renderer, 
390         "text", MODEL_COL_NAME, NULL);
391         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
392         gtk_tree_view_append_column (priv->treeview, view_column);
393
394         
395         /* server name column: */
396         view_column = gtk_tree_view_column_new ();
397         renderer = gtk_cell_renderer_text_new ();
398         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
399         gtk_tree_view_column_set_attributes (view_column, renderer, 
400         "text", MODEL_COL_SERVER_NAME, NULL);
401         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
402         gtk_tree_view_append_column (priv->treeview, view_column);
403         
404         /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
405         
406         GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
407
408         /* Introductory note: */
409         /* TODO: For some reason this label does not wrap. It is truncated. */
410         GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
411         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
412         /* So that it is shown without being truncated: */
413         gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
414         /* The documentation for gtk_label_set_line_wrap() says that we must 
415          * call gtk_widget_set_size_request() with a hard-coded width, 
416          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
417         gtk_widget_set_size_request (label, 400, -1);
418         gtk_widget_show (label);
419         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
420         
421         /* Put the treeview in a scrolled window and add it to the box: */
422         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
423         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
424         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
425                 GTK_POLICY_AUTOMATIC);
426         gtk_widget_show (scrolled_window);
427         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
428         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
429         gtk_widget_show (GTK_WIDGET (priv->treeview));
430         
431         /* Add the buttons: */
432         GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
433         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
434         gtk_widget_show (hbox);
435         
436         priv->button_edit = gtk_button_new_from_stock (_("mcen_bd_edit"));
437         gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
438         gtk_widget_show (priv->button_edit);
439         g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
440                 G_CALLBACK (on_button_edit), self);
441         
442         GtkWidget *button_cancel = gtk_button_new_from_stock (_("mcen_bd_close"));
443         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
444         gtk_widget_show (button_cancel);
445         g_signal_connect (G_OBJECT (button_cancel), "clicked",
446                 G_CALLBACK (on_button_cancel), self);
447         
448         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
449         gtk_widget_show (vbox);
450         
451         /* Disable the Edit button when nothing is selected: */
452         GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
453         g_signal_connect (sel, "changed",
454                           G_CALLBACK(on_selection_changed), self);
455         on_selection_changed (sel, self);
456         
457         /* When this window is shown, hibernation should not be possible, 
458          * because there is no sensible way to save the state: */
459         mgr = modest_runtime_get_window_mgr ();
460         modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr, 
461                                                                      GTK_WINDOW (self)); 
462
463         /* Set window title */
464         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
465
466         /* Track key presses to close the window if the Escape is pressed */
467         g_signal_connect (G_OBJECT (self), 
468                           "key-press-event", 
469                           G_CALLBACK (on_key_pressed), NULL);
470 }
471
472 ModestConnectionSpecificSmtpWindow*
473 modest_connection_specific_smtp_window_new (void)
474 {
475         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
476 }
477
478 /** The application should call this when the user changes should be saved.
479  */
480 gboolean
481 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
482 {
483         ModestConnectionSpecificSmtpWindowPrivate *priv = 
484                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
485         
486         
487         /* Get the first iter in the list */
488         GtkTreeIter iter;
489         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
490
491         /* Walk through the list, reading each row */
492         while (valid) {
493                 gchar *id = NULL;
494                 gchar *connection_name = NULL;
495                 gchar *server_account_name = NULL;
496                 ModestServerAccountData *data = NULL;
497                 
498                 gtk_tree_model_get (priv->model, &iter, 
499                                     MODEL_COL_ID, &id, 
500                                     MODEL_COL_NAME, &connection_name, 
501                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
502                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
503                                     -1);
504                                  
505                 gboolean success = TRUE;   
506                 if (id && data) { /* The presence of data suggests that there is something to save. */
507                         if (!server_account_name) {
508                                 /* Add a new server account, building a (non-human-visible) name: */
509                                 gchar *name_start = g_strdup_printf("specific_%s", connection_name);
510                                 server_account_name = modest_account_mgr_get_unused_account_name (
511                                         priv->account_manager, name_start, TRUE /* server account. */);
512                                 g_assert (server_account_name);
513                                 g_free (name_start);
514                                 
515                                 success = modest_account_mgr_add_server_account (priv->account_manager,
516                                                                                  server_account_name,
517                                                                                  data->hostname, 0,
518                                                                                  data->username, data->password,
519                                                                                  MODEST_PROTOCOL_TRANSPORT_SMTP,
520                                                                                  data->security,
521                                                                                  data->secure_auth);
522                                         
523                                 /* associate the specific server account with this connection for this account: */
524                                 success = success && modest_account_mgr_set_connection_specific_smtp (
525                                         priv->account_manager, connection_name, server_account_name);
526         
527                                 /* Save the new name in the treemodel, so it can be edited again later: */
528                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
529                                         MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
530                                 
531                         } else {
532                                 /* Change an existing server account: */
533                                 modest_account_mgr_set_server_account_hostname (priv->account_manager, server_account_name, 
534                                                                                 data->hostname);
535                                                 
536                                 modest_account_mgr_set_server_account_username (priv->account_manager, server_account_name,
537                                         data->username);
538                                                         
539                                 modest_account_mgr_set_server_account_password (priv->account_manager, server_account_name,
540                                         data->password);
541                                                 
542                                 modest_account_mgr_set_server_account_secure_auth (priv->account_manager, server_account_name, 
543                                         data->secure_auth);
544                                                 
545                                 modest_account_mgr_set_server_account_security (priv->account_manager, server_account_name, 
546                                         data->security);
547
548                                 modest_account_mgr_set_server_account_port (priv->account_manager, server_account_name, data->port);
549                         }
550                 } else if (connection_name) {
551                         modest_account_mgr_remove_connection_specific_smtp (priv->account_manager, 
552                                                                             connection_name);
553                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
554                                             MODEL_COL_SERVER_ACCOUNT_NAME, NULL, -1);
555                 }
556                 
557                 g_free (connection_name);
558                 g_free (id);
559                 g_free (server_account_name);
560                 
561                 if (!success)
562                         return FALSE;
563                         
564                 /* Get next row: */
565                 valid = gtk_tree_model_iter_next (priv->model, &iter);
566         }
567         
568         update_model_server_names (self);
569         
570         return TRUE;
571 }
572
573 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
574 {
575         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
576
577         GtkTreeIter iter;
578         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
579         while (valid) {
580                 
581                 gchar *server_account_name = NULL;
582                 ModestServerAccountData *data = NULL;
583                 gtk_tree_model_get (priv->model, &iter, 
584                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
585                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
586                                     -1);
587                 if (server_account_name) {
588                         
589                         /* Get the server hostname and show it in the treemodel: */     
590                         gchar *hostname = modest_account_mgr_get_server_account_hostname (priv->account_manager, 
591                                                                                           server_account_name);
592                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
593                                             MODEL_COL_SERVER_NAME, hostname,
594                                             -1);
595                         g_free (hostname);
596                 } else if (data && data->hostname && (data->hostname[0] != '\0')) {
597                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
598                                             MODEL_COL_SERVER_NAME, data->hostname,
599                                             -1);
600                 } else {
601                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
602                                             MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
603                                             -1);
604                 }
605                         
606                 /* Get next row: */
607                 valid = gtk_tree_model_iter_next (priv->model, &iter);
608         }
609 }
610
611 static gboolean
612 on_key_pressed (GtkWidget *self,
613                 GdkEventKey *event,
614                 gpointer user_data)
615 {
616         if (event->keyval == GDK_Escape) {
617                 /* Simulate a press on Cancel to close the dialog */
618                 on_button_cancel (NULL, self);
619         }
620         
621         return FALSE;
622 }