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