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