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