* src/gnome/modest-account-view-window.c:
[modest] / src / gnome / modest-account-settings-dialog.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
31 #include "widgets/modest-account-settings-dialog.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkstock.h>
42
43 #include "widgets/modest-serversecurity-combo-box.h"
44 #include "widgets/modest-secureauth-combo-box.h"
45 #include "widgets/modest-validating-entry.h"
46 #include "widgets/modest-retrieve-combo-box.h"
47 #include "widgets/modest-limit-retrieve-combo-box.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
51 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
52 #include "modest-protocol-info.h"
53 /* #include "maemo/modest-connection-specific-smtp-window.h" */
54 /* #include "maemo/modest-signature-editor-dialog.h" */
55 #include <modest-utils.h>
56 #include <modest-platform.h>
57 /* #include "maemo/modest-maemo-utils.h" */
58 #include "widgets/modest-ui-constants.h"
59 #include <tny-account.h>
60 #include <tny-status.h>
61
62 #include <gconf/gconf-client.h>
63 #include <string.h> /* For strlen(). */
64
65 /* Include config.h so that _() works: */
66 #ifdef HAVE_CONFIG_H
67 #include <config.h>
68 #endif
69
70 #define PORT_MIN 1
71 #define PORT_MAX 65535
72
73 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
74
75 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
76         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
77
78 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
79
80 struct _ModestAccountSettingsDialogPrivate
81 {
82 };
83
84 static void
85 enable_buttons (ModestAccountSettingsDialog *self);
86
87 static gboolean
88 save_configuration (ModestAccountSettingsDialog *dialog);
89
90 static const gchar * null_means_empty (const gchar * str);
91
92 static const gchar *
93 null_means_empty (const gchar * str)
94 {
95         return str ? str : "";
96 }
97
98
99 static void
100 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
101                                                                                                                         GValue *value, GParamSpec *pspec)
102 {
103         switch (property_id) {
104         default:
105                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106         }
107 }
108
109 static void
110 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
111                                                                                                                         const GValue *value, GParamSpec *pspec)
112 {
113         switch (property_id) {
114         default:
115                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116         }
117 }
118
119 static void
120 modest_account_settings_dialog_dispose (GObject *object)
121 {
122         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
123                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
124 }
125
126 static void
127 modest_account_settings_dialog_finalize (GObject *object)
128 {
129         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
130         
131         if (self->account_name)
132                 g_free (self->account_name);
133                 
134         if (self->original_account_title)
135                 g_free (self->original_account_title);
136                 
137         if (self->account_manager)
138                 g_object_unref (G_OBJECT (self->account_manager));
139                 
140         if (self->specific_window)
141                 gtk_widget_destroy (self->specific_window);
142                 
143         if (self->signature_dialog)
144                 gtk_widget_destroy (self->signature_dialog);
145
146         if (self->settings) {
147                 g_object_unref (self->settings);
148                 self->settings = NULL;
149         }
150         
151         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
152 }
153
154 static void
155 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
156
157 static void
158 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
159
160 static void
161 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
162 {
163         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
164         self->modified = TRUE;
165 }
166
167 static void
168 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
169 {
170         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
171         self->modified = TRUE;
172 }
173
174 static void
175 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
176 {
177         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
178         self->modified = TRUE;
179 }
180
181 static void
182 on_modified_spin_button_changed (GtkSpinButton *spin_button, gpointer user_data)
183 {
184         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
185         gint value = gtk_spin_button_get_value_as_int (spin_button);
186
187         gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, value > 0);
188         self->modified = TRUE;
189 }
190
191 /* Set a modified boolean whenever the widget is changed, 
192  * so we can check for it later.
193  */
194 static void
195 connect_for_modified (ModestAccountSettingsDialog *self, GtkWidget *widget)
196 {
197         if (GTK_SPIN_BUTTON (widget)) {
198                 g_signal_connect (G_OBJECT (widget), "value_changed",
199                         G_CALLBACK (on_modified_spin_button_changed), self);
200         }
201         else if (GTK_IS_ENTRY (widget)) {
202                 g_signal_connect (G_OBJECT (widget), "changed",
203                         G_CALLBACK (on_modified_entry_changed), self);
204         } else if (GTK_IS_COMBO_BOX (widget)) {
205                 g_signal_connect (G_OBJECT (widget), "changed",
206                         G_CALLBACK (on_modified_combobox_changed), self);       
207         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
208                 g_signal_connect (G_OBJECT (widget), "toggled",
209                         G_CALLBACK (on_modified_checkbox_toggled), self);
210         }
211 }
212
213 static void
214 on_field_entry_changed (GtkEditable *editable, gpointer user_data)
215 {
216         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
217         g_assert(self);
218         enable_buttons(self);
219 }
220
221 static void
222 on_field_combobox_changed (GtkComboBox *widget, gpointer user_data)
223 {
224         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
225         g_assert(self);
226         enable_buttons(self);
227 }
228
229 /** This is a convenience function to create a field containing a mandatory widget.
230  * When the widget is edited, the enable_buttons() vfunc will be called.
231  */
232 static GtkWidget* create_field(ModestAccountSettingsDialog *self,
233                                GtkSizeGroup *group,
234                                const gchar *value,
235                                GtkWidget *control,
236                                GtkWidget *icon,
237                                gboolean mandatory)
238 {
239         GtkWidget *hbox;
240         gchar *title;
241         GtkWidget *label;
242
243         hbox = gtk_hbox_new (FALSE, 12);
244         if (mandatory)
245                 title = g_strdup_printf("%s*:", value);
246         else
247                 title = g_strdup_printf ("%s:", value);
248         label = gtk_label_new (title);
249         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
250         g_free (title);
251
252         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
253         gtk_box_pack_start (GTK_BOX (hbox), control, TRUE, TRUE, 0);
254         gtk_size_group_add_widget (group, label);
255   
256         /* Connect to the appropriate changed signal for the widget, 
257          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
258          */
259         if (GTK_IS_ENTRY (control)) {
260                 g_signal_connect (G_OBJECT (control), "changed",
261                 G_CALLBACK (on_field_entry_changed), self);
262                 
263         }
264         else if (GTK_IS_COMBO_BOX (control)) {
265                 g_signal_connect (G_OBJECT (control), "changed",
266                 G_CALLBACK (on_field_combobox_changed), self);
267         }
268         gtk_widget_show_all (hbox);
269          
270         return hbox;
271 }
272
273 static void
274 on_entry_invalid_account_title_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
275 {
276         gchar *tmp, *msg;
277                         
278         tmp = g_strndup (account_title_forbidden_chars, ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH);
279         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
280
281         modest_platform_information_banner (GTK_WIDGET (self), NULL, msg);
282
283         g_free (msg);
284         g_free (tmp);
285 }
286
287 static void
288 on_entry_invalid_fullname_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
289 {
290         gchar *tmp, *msg;
291                         
292         tmp = g_strndup (user_name_forbidden_chars, USER_NAME_FORBIDDEN_CHARS_LENGTH);
293         msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
294
295         modest_platform_information_banner(GTK_WIDGET (self), NULL, msg);
296
297         g_free (msg);
298         g_free (tmp);
299 }
300
301
302 static void
303 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
304 {
305         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
306                                          _CS("ckdg_ib_maximum_characters_reached"));
307 }
308
309 static GtkWidget*
310 create_page_account_details (ModestAccountSettingsDialog *self)
311 {
312         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
313         GtkAdjustment *focus_adjustment = NULL;
314         GtkAlignment *alignment;
315         
316         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
317         GtkWidget *scrollwin = gtk_scrolled_window_new (NULL, NULL);
318         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
319                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
320            
321         /* The description widgets: */  
322         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
323         GtkWidget *field = create_field (self, sizegroup, _("mcen_fi_account_title"), 
324                 self->entry_account_title, NULL, TRUE);
325         gtk_widget_show (self->entry_account_title);
326         connect_for_modified (self, self->entry_account_title);
327         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
328         gtk_widget_show (field);
329         
330         /* Prevent the use of some characters in the account title, 
331          * as required by our UI specification: */
332         GList *list_prevent = NULL;
333         list_prevent = g_list_append (list_prevent, "\\");
334         list_prevent = g_list_append (list_prevent, "/");
335         list_prevent = g_list_append (list_prevent, ":");
336         list_prevent = g_list_append (list_prevent, "*");
337         list_prevent = g_list_append (list_prevent, "?");
338         list_prevent = g_list_append (list_prevent, "\"");
339         list_prevent = g_list_append (list_prevent, "<"); 
340         list_prevent = g_list_append (list_prevent, ">"); 
341         list_prevent = g_list_append (list_prevent, "|");
342         list_prevent = g_list_append (list_prevent, "^");       
343         modest_validating_entry_set_unallowed_characters (
344                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
345         g_list_free (list_prevent);
346         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_account_title),
347                                          on_entry_invalid_account_title_character, self);
348         
349         /* Set max length as in the UI spec:
350          * The UI spec seems to want us to show a dialog if we hit the maximum. */
351         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
352         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
353                 on_entry_max, self);
354         
355         /* The retrieve combobox: */
356         self->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
357         field = create_field (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
358                               self->combo_retrieve, NULL, TRUE);
359         gtk_widget_show (self->combo_retrieve);
360         connect_for_modified (self, self->combo_retrieve);
361         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
362         gtk_widget_show (field);
363         
364         /* The limit-retrieve combobox: */
365         self->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
366         field = create_field (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
367                               self->combo_limit_retrieve, NULL, TRUE);
368         gtk_widget_show (self->combo_limit_retrieve);
369         connect_for_modified (self, self->combo_limit_retrieve);
370         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
371         gtk_widget_show (field);
372
373         /* The leave-messages widgets: */
374         if(!self->checkbox_leave_messages)
375                 self->checkbox_leave_messages = gtk_check_button_new ();
376         if (!self->caption_leave_messages) {
377                 self->caption_leave_messages = create_field (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
378                                                              self->checkbox_leave_messages, NULL, TRUE);
379         }
380                         
381         gtk_widget_show (self->checkbox_leave_messages);
382         connect_for_modified (self, self->checkbox_leave_messages);
383         gtk_box_pack_start (GTK_BOX (box), self->caption_leave_messages, FALSE, FALSE, MODEST_MARGIN_HALF);
384         gtk_widget_show (self->caption_leave_messages);
385         
386         gtk_widget_show (GTK_WIDGET (box));
387         
388         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollwin), box);
389         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin), GTK_SHADOW_NONE);
390         gtk_viewport_set_shadow_type (GTK_VIEWPORT (gtk_bin_get_child (scrollwin)), GTK_SHADOW_NONE);
391         gtk_widget_show (scrollwin);
392
393         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
394         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment); 
395
396         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
397         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
398         gtk_container_add (GTK_CONTAINER (alignment), scrollwin);
399         gtk_widget_show (alignment);
400         
401         return GTK_WIDGET (alignment);
402 }
403
404 static gchar*
405 get_entered_account_title (ModestAccountSettingsDialog *dialog)
406 {
407         const gchar* account_title = 
408                 gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
409         if (!account_title || (strlen (account_title) == 0))
410                 return NULL;
411         else {
412                 /* Strip it of whitespace at the start and end: */
413                 gchar *result = g_strdup (account_title);
414                 result = g_strstrip (result);
415                 
416                 if (!result)
417                         return NULL;
418                         
419                 if (strlen (result) == 0) {
420                         g_free (result);
421                         return NULL;    
422                 }
423                 
424                 return result;
425         }
426 }
427
428
429 static void
430 on_button_signature (GtkButton *button, gpointer user_data)
431 {
432 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
433         
434 /*      /\* Create the window, if necessary: *\/ */
435 /*      if (!(self->signature_dialog)) { */
436 /*              self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ()); */
437         
438 /*              gboolean use_signature = modest_account_settings_get_use_signature (self->settings); */
439 /*              const gchar *signature = modest_account_settings_get_signature(self->settings); */
440 /*              gchar* account_title = get_entered_account_title (self); */
441 /*              modest_signature_editor_dialog_set_settings ( */
442 /*                      MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog),  */
443 /*                      use_signature, signature, account_title); */
444
445 /*              g_free (account_title); */
446 /*              account_title = NULL; */
447 /*              signature = NULL; */
448 /*      } */
449
450 /*      /\* Show the window: *\/         */
451 /*      gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self)); */
452 /*      gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE); */
453 /*     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog)); */
454 /*     gtk_widget_hide (self->signature_dialog); */
455 /*     if (response != GTK_RESPONSE_OK) { */
456 /*      /\* Destroy the widget now, and its data: *\/ */
457 /*      gtk_widget_destroy (self->signature_dialog); */
458 /*      self->signature_dialog = NULL; */
459 /*     } */
460 /*     else { */
461 /*      /\* Mark modified, so we use the dialog's data later: *\/ */
462 /*      self->modified = TRUE;   */
463 /*     } */
464 }
465
466 static GtkWidget*
467 create_page_user_details (ModestAccountSettingsDialog *self)
468 {
469         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
470         GtkAdjustment *focus_adjustment = NULL;
471         GtkAlignment *alignment;
472         
473         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
474         GtkWidget *scrollwin = gtk_scrolled_window_new (NULL, NULL);
475         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
476                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
477         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin), GTK_SHADOW_NONE);
478          
479         /* The name widgets: */
480         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
481
482         /* Set max length as in the UI spec:
483          * The UI spec seems to want us to show a dialog if we hit the maximum. */
484         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
485         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
486                 on_entry_max, self);
487         GtkWidget *field = create_field (self, sizegroup, 
488                                          _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, FALSE);
489         gtk_widget_show (self->entry_user_name);
490         connect_for_modified (self, self->entry_user_name);
491         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
492         gtk_widget_show (field);
493
494
495         /* Prevent the use of some characters in the name, 
496          * as required by our UI specification: */
497         GList *list_prevent = NULL;
498         list_prevent = g_list_append (list_prevent, "<");
499         list_prevent = g_list_append (list_prevent, ">");
500         modest_validating_entry_set_unallowed_characters (
501                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
502         g_list_free (list_prevent);
503         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_user_name),
504                                          on_entry_invalid_fullname_character, self);
505         
506         /* The username widgets: */     
507         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
508         field = create_field (self, sizegroup, _("mail_fi_username"), 
509                                 self->entry_user_username, NULL, TRUE);
510         gtk_widget_show (self->entry_user_username);
511         connect_for_modified (self, self->entry_user_username);
512         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
513         gtk_widget_show (field);
514         
515         /* Prevent the use of some characters in the username, 
516          * as required by our UI specification: */
517         modest_validating_entry_set_unallowed_characters_whitespace (
518                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
519         
520         /* Set max length as in the UI spec:
521          * The UI spec seems to want us to show a dialog if we hit the maximum. */
522         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
523         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
524                 on_entry_max, self);
525         
526         /* The password widgets: */     
527         self->entry_user_password = gtk_entry_new ();
528         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
529         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
530         field = create_field (self, sizegroup, 
531                               _("mail_fi_password"), self->entry_user_password, NULL, FALSE);
532         gtk_widget_show (self->entry_user_password);
533         connect_for_modified (self, self->entry_user_password);
534         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
535         gtk_widget_show (field);
536         
537         /* The email address widgets: */        
538         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
539         field = create_field (self, sizegroup, 
540                               _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, TRUE);
541         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
542         gtk_widget_show (self->entry_user_email);
543         connect_for_modified (self, self->entry_user_email);
544         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
545         gtk_widget_show (field);
546         
547         /* Set max length as in the UI spec:
548          * The UI spec seems to want us to show a dialog if we hit the maximum. */
549         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
550         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
551                 on_entry_max, self);
552         
553         /* Signature button: */
554         if (!self->button_signature)
555                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
556         field = create_field (self, sizegroup, _("mcen_fi_email_signature"), 
557                 self->button_signature, NULL, FALSE);
558         gtk_widget_show (self->button_signature);
559         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
560         gtk_widget_show (field);
561                 
562         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
563                 G_CALLBACK (on_button_signature), self);
564                 
565         gtk_widget_show (GTK_WIDGET (box));
566         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollwin), box);
567         gtk_viewport_set_shadow_type (GTK_VIEWPORT (gtk_bin_get_child (scrollwin)), GTK_SHADOW_NONE);
568         gtk_widget_show (scrollwin);
569
570         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
571         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment); 
572         
573         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
574         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
575         gtk_container_add (GTK_CONTAINER (alignment), scrollwin);
576         gtk_widget_show (alignment);
577         
578         return GTK_WIDGET (alignment);
579 }
580
581 /** Change the field title for the incoming server, 
582  * as specified in the UI spec:
583  */
584 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
585 {
586         const gchar* type = 
587                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
588                         _("mail_fi_emailtype_pop3") : 
589                         _("mail_fi_emailtype_imap") );
590                         
591                 
592         /* Note that this produces a compiler warning, 
593          * because the compiler does not know that the translated string will have a %s in it.
594          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
595         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
596         
597         /* This is a mandatory field, so add a *. This is usually done by 
598          * create_field() but we can't use that here. */
599         gchar *with_asterisk = g_strconcat (incomingserver_title, "*", NULL);
600         g_free (incomingserver_title);
601         
602         g_object_set (G_OBJECT (self->caption_incoming), "label", with_asterisk, NULL);
603         g_free(with_asterisk);
604 }
605
606 /** Change the field title for the incoming server, 
607  * as specified in the UI spec:
608  */
609 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestTransportStoreProtocol protocol)
610 {
611         /* Fill the combo with appropriately titled choices for POP or IMAP. */
612         /* The choices are the same, but the titles are different, as in the UI spec. */
613         modest_serversecurity_combo_box_fill (
614                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
615 }
616            
617 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
618 {
619         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
620         GtkAlignment *alignment;
621         
622         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
623          
624         /* The incoming server widgets: */
625         if(!self->entry_incomingserver)
626                 self->entry_incomingserver = gtk_entry_new ();
627
628         if (self->caption_incoming)
629           gtk_widget_destroy (self->caption_incoming);
630            
631         /* The field title will be updated in update_incoming_server_title().
632          * so this default text will never be seen: */
633         /* (Note: Changing the title seems pointless. murrayc) */
634         self->caption_incoming = create_field (self, sizegroup, 
635                                              "Incoming Server", self->entry_incomingserver, NULL, TRUE);
636         gtk_widget_show (self->entry_incomingserver);
637         connect_for_modified (self, self->entry_incomingserver);
638         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
639         gtk_widget_show (self->caption_incoming);
640         
641         /* The secure connection widgets: */
642         /* This will be filled by update_incoming_server_security_choices(). */
643         if (!self->combo_incoming_security)
644                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
645         GtkWidget *field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
646                                          self->combo_incoming_security, NULL, FALSE);
647         gtk_widget_show (self->combo_incoming_security);
648         connect_for_modified (self, self->combo_incoming_security);
649         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
650         gtk_widget_show (field);
651         
652         /* Show a default port number when the security method changes, as per the UI spec: */
653         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
654         
655         
656         /* The port widgets: */
657         if (!self->entry_incoming_port)
658                 self->entry_incoming_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble) PORT_MIN, (gdouble) PORT_MAX, 1.0));
659         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
660                 self->entry_incoming_port, NULL, FALSE);
661         gtk_widget_show (self->entry_incoming_port);
662         connect_for_modified (self, self->entry_incoming_port);
663         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
664         gtk_widget_show (field);
665         
666         /* The secure authentication widgets: */
667         if(!self->checkbox_incoming_auth)
668                 self->checkbox_incoming_auth = gtk_check_button_new ();
669         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
670                               self->checkbox_incoming_auth, NULL, FALSE);
671         gtk_widget_show (self->checkbox_incoming_auth);
672         connect_for_modified (self, self->checkbox_incoming_auth);
673         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
674         gtk_widget_show (field);
675         
676         gtk_widget_show (GTK_WIDGET (box));
677         
678         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
679         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
680         gtk_container_add (GTK_CONTAINER (alignment), box);
681         gtk_widget_show (alignment);
682         
683         return GTK_WIDGET (alignment);
684 }
685
686 static void
687 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
688 {
689         GtkWidget *widget = GTK_WIDGET (user_data);
690         
691         /* Enable the widget only if the toggle button is active: */
692         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
693         gtk_widget_set_sensitive (widget, enable);
694 }
695
696 /* Make the sensitivity of a widget depend on a toggle button.
697  */
698 static void
699 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
700 {
701         g_signal_connect (G_OBJECT (button), "toggled",
702                 G_CALLBACK (on_toggle_button_changed), widget);
703         
704         /* Set the starting sensitivity: */
705         on_toggle_button_changed (button, widget);
706 }
707
708 static void
709 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
710 {
711 /*      ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data); */
712         
713 /*      /\* Create the window if necessary: *\/ */
714 /*      if (!(self->specific_window)) { */
715 /*              self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ()); */
716 /*              modest_connection_specific_smtp_window_fill_with_connections ( */
717 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager); */
718 /*      } */
719
720 /*      /\* Show the window: *\/         */
721 /*      gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self)); */
722 /*      gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE); */
723 /*      gtk_widget_show (self->specific_window); */
724 /*      self->modified = TRUE; */
725 }
726
727 static void
728 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
729 {
730         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
731         
732         ModestAuthProtocol protocol_security = 
733                 modest_secureauth_combo_box_get_active_secureauth (
734                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
735         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
736         
737         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
738         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
739 }
740
741 static void
742 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
743 {
744         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
745         
746         const gint port_number = 
747                 modest_serversecurity_combo_box_get_active_serversecurity_port (
748                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
749
750         if(port_number != 0) {
751                 gtk_spin_button_set_value (
752                         GTK_SPIN_BUTTON (self->entry_outgoing_port), (gdouble) port_number);
753         }               
754 }
755
756 static void
757 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
758 {
759         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
760         
761         const gint port_number = 
762                 modest_serversecurity_combo_box_get_active_serversecurity_port (
763                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
764
765         if(port_number != 0) {
766                 gtk_spin_button_set_value (
767                         GTK_SPIN_BUTTON (self->entry_incoming_port), (gdouble) port_number);
768         }               
769 }
770
771
772 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
773 {
774         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
775         GtkAdjustment *focus_adjustment = NULL;
776         GtkAlignment *alignment;
777         
778         /* Put it all in a scrolled window, so that all widgets can be 
779          * accessed even when the on-screen keyboard is visible: */
780         GtkWidget *scrollwin = gtk_scrolled_window_new(NULL, NULL);
781         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin), 
782                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
783         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin), GTK_SHADOW_NONE);
784         
785         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
786          
787         /* The outgoing server widgets: */
788         if (!self->entry_outgoingserver)
789                 self->entry_outgoingserver = gtk_entry_new ();
790         GtkWidget *field = create_field (self, sizegroup, 
791                                          _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, FALSE);
792         gtk_widget_show (self->entry_outgoingserver);
793         connect_for_modified (self, self->entry_outgoingserver);
794         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
795         gtk_widget_show (field);
796         
797         /* The secure authentication widgets: */
798         if (!self->combo_outgoing_auth)
799                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
800         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
801                 self->combo_outgoing_auth, NULL, FALSE);
802         gtk_widget_show (self->combo_outgoing_auth);
803         connect_for_modified (self, self->combo_outgoing_auth);
804         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
805         gtk_widget_show (field);
806         
807         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
808         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
809         
810         /* The username widgets: */     
811         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
812         self->caption_outgoing_username = create_field (self, sizegroup, _("mail_fi_username"), 
813                                                       self->entry_outgoing_username, NULL, TRUE);
814         gtk_widget_show (self->entry_outgoing_username);
815         connect_for_modified (self, self->entry_outgoing_username);
816         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
817         gtk_widget_show (self->caption_outgoing_username);
818         
819         /* Prevent the use of some characters in the username, 
820          * as required by our UI specification: */
821         modest_validating_entry_set_unallowed_characters_whitespace (
822                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
823         
824         /* Set max length as in the UI spec:
825          * The UI spec seems to want us to show a dialog if we hit the maximum. */
826         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
827         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
828                 on_entry_max, self);
829                 
830         /* The password widgets: */     
831         self->entry_outgoing_password = gtk_entry_new ();
832         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
833         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
834         self->caption_outgoing_password = create_field (self, sizegroup, 
835                                                       _("mail_fi_password"), self->entry_outgoing_password, NULL, FALSE);
836         gtk_widget_show (self->entry_outgoing_password);
837         connect_for_modified (self, self->entry_outgoing_password);
838         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
839         gtk_widget_show (self->caption_outgoing_password);
840         
841         /* The secure connection widgets: */
842         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
843          * and modest_serversecurity_combo_box_set_active_serversecurity().
844          */
845         if (!self->combo_outgoing_security)
846                 
847                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
848         field = create_field (self, sizegroup, _("mcen_li_emailsetup_secure_connection"), 
849                               self->combo_outgoing_security, NULL, FALSE);
850         gtk_widget_show (self->combo_outgoing_security);
851         connect_for_modified (self, self->combo_outgoing_security);
852         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
853         gtk_widget_show (field);
854         
855         /* Show a default port number when the security method changes, as per the UI spec: */
856         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
857         
858         /* The port widgets: */
859         if (!self->entry_outgoing_port)
860                 self->entry_outgoing_port = GTK_WIDGET (gtk_spin_button_new_with_range ((gdouble)PORT_MIN, (gdouble) PORT_MAX, 1.0));
861         field = create_field (self, sizegroup, _("mcen_fi_emailsetup_port"), 
862                               self->entry_outgoing_port, NULL, FALSE);
863         gtk_widget_show (self->entry_outgoing_port);
864         connect_for_modified (self, self->entry_outgoing_port);
865         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
866         gtk_widget_show (field);
867         
868         GtkWidget *separator = gtk_hseparator_new ();
869         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
870         gtk_widget_show (separator);
871         
872         /* connection-specific checkbox: */
873         if (!self->checkbox_outgoing_smtp_specific) {
874                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
875                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
876                         FALSE);
877         }
878         field = create_field (self, sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
879                               self->checkbox_outgoing_smtp_specific, NULL, FALSE);
880         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
881         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
882         gtk_widget_show (field);
883         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
884         
885         /* Connection-specific SMTP-Severs Edit button: */
886         if (!self->button_outgoing_smtp_servers)
887                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
888         field = create_field (self, sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
889                               self->button_outgoing_smtp_servers, NULL, FALSE);
890         gtk_widget_show (self->button_outgoing_smtp_servers);
891         gtk_box_pack_start (GTK_BOX (box), field, FALSE, FALSE, MODEST_MARGIN_HALF);
892         gtk_widget_show (field);
893         
894         /* Only enable the button when the checkbox is checked: */
895         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
896                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
897                 
898         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
899                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
900                 
901         gtk_widget_show (GTK_WIDGET (box));
902         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrollwin), box);
903         gtk_viewport_set_shadow_type (GTK_VIEWPORT (gtk_bin_get_child (scrollwin)), GTK_SHADOW_NONE);
904         gtk_widget_show(scrollwin);
905
906         focus_adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollwin));
907         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), focus_adjustment);
908         
909         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
910         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
911         gtk_container_add (GTK_CONTAINER (alignment), scrollwin);
912         gtk_widget_show (alignment);
913         
914         return GTK_WIDGET (alignment);
915 }
916         
917 static gboolean
918 check_data (ModestAccountSettingsDialog *self)
919 {
920         /* Check that the title is not already in use: */
921         gchar* account_title = get_entered_account_title (self);
922         if (!account_title)
923                 return FALSE; /* Should be prevented already anyway. */
924                 
925         if (strcmp(account_title, self->original_account_title) != 0) {
926                 /* Check the changed title: */
927                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
928                         account_title);
929         
930                 if (name_in_use) {
931                         /* Warn the user via a dialog: */
932                         modest_platform_information_banner(NULL, NULL, _("mail_ib_account_name_already_existing"));
933                 
934                 g_free (account_title);
935                         return FALSE;
936                 }
937         }
938         
939         g_free (account_title);
940         account_title  = NULL;
941
942         /* Check that the email address is valid: */
943         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
944         if ((!email_address) || (strlen(email_address) == 0)) {
945                 return FALSE;
946         }
947                         
948         if (!modest_text_utils_validate_email_address (email_address, NULL)) {
949                 /* Warn the user via a dialog: */
950                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_email"));
951                                          
952                 /* Return focus to the email address entry: */
953                 gtk_widget_grab_focus (self->entry_user_email);
954                 gtk_editable_select_region (GTK_EDITABLE (self->entry_user_email), 0, -1);
955                 return FALSE;
956         }
957
958         /* make sure the domain name for the incoming server is valid */
959         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver));
960         if ((!hostname) || (strlen(hostname) == 0)) {
961                 return FALSE;
962         }
963         
964         if (!modest_text_utils_validate_domain_name (hostname)) {
965                 /* Warn the user via a dialog: */
966                 modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
967                                          
968                 /* Return focus to the email address entry: */
969                 gtk_widget_grab_focus (self->entry_incomingserver);
970                 gtk_editable_select_region (GTK_EDITABLE (self->entry_incomingserver), 0, -1);
971                 return FALSE;
972         }
973
974         /* make sure the domain name for the outgoing server is valid */
975         const gchar* hostname2 = gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver));
976         if ((!hostname2) || (strlen(hostname2) == 0)) {
977                 return FALSE;
978         }
979         
980         if (!modest_text_utils_validate_domain_name (hostname2)) {
981                 /* Warn the user via a dialog: */
982                 modest_platform_information_banner (self->entry_outgoingserver, NULL, _("mcen_ib_invalid_servername"));
983
984                 /* Return focus to the email address entry: */
985                 gtk_widget_grab_focus (self->entry_outgoingserver);
986                 gtk_editable_select_region (GTK_EDITABLE (self->entry_outgoingserver), 0, -1);
987                 return FALSE;
988         }
989         
990         /* Find a suitable authentication method when secure authentication is desired */
991
992         const gint port_num = gtk_spin_button_get_value_as_int (
993                         GTK_SPIN_BUTTON (self->entry_incoming_port));
994         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
995
996         /*
997         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
998                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
999         */
1000         /* If we use an encrypted protocol then there is no need to encrypt the password */
1001         /* I don't think this is a good assumption. It overrides the user's request. murrayc: 
1002          *  if (!modest_protocol_info_is_secure(protocol_security_incoming)) */
1003         if (TRUE)
1004         {
1005                 if (gtk_toggle_button_get_active (
1006                                 GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth))) {
1007                         GError *error = NULL;
1008
1009                         GList *list_auth_methods = 
1010                                 modest_utils_get_supported_secure_authentication_methods (self->incoming_protocol, 
1011                                         hostname, port_num, username, GTK_WINDOW (self), &error);
1012                         if (list_auth_methods) {
1013                                 /* Use the first supported method.
1014                                  * TODO: Should we prioritize them, to prefer a particular one? */
1015                                 GList* method;
1016                                 for (method = list_auth_methods; method != NULL; method = g_list_next(method))
1017                                 {
1018                                         ModestAuthProtocol proto = (ModestAuthProtocol)(GPOINTER_TO_INT(method->data));
1019                                         // Allow secure methods, e.g MD5 only
1020                                         if (modest_protocol_info_auth_is_secure(proto))
1021                                         {
1022                                                 self->protocol_authentication_incoming = proto;
1023                                                 break;
1024                                         }
1025                                 }
1026                                 g_list_free (list_auth_methods);
1027                         }
1028
1029                         if (list_auth_methods == NULL || 
1030                                         !modest_protocol_info_auth_is_secure(self->protocol_authentication_incoming))
1031                         {
1032                                 if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
1033                                                 error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
1034                                         modest_platform_information_banner(GTK_WIDGET (self), NULL, 
1035                                                                          _("Could not discover supported secure authentication methods."));
1036
1037                                 if(error != NULL)
1038                                         g_error_free(error);
1039                                         
1040                                 /* This is a nasty hack. jschmid. */
1041                                 /* Don't let the dialog close */
1042                                 /*g_signal_stop_emission_by_name (dialog, "response");*/
1043                                 return FALSE;
1044                         }
1045                 }
1046         }
1047         
1048         return TRUE;
1049 }
1050 /*
1051  */
1052 static void 
1053 on_response (GtkDialog *wizard_dialog,
1054         gint response_id,
1055         gpointer user_data)
1056 {
1057         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
1058         enable_buttons (self);
1059         
1060         gboolean prevent_response = FALSE;
1061
1062         /* Warn about unsaved changes: */
1063         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
1064                 GtkDialog *dialog = GTK_DIALOG (modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
1065                                                                                          _("imum_nc_wizard_confirm_lose_changes")));
1066                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1067                 
1068                 const gint dialog_response = gtk_dialog_run (dialog);
1069                 gtk_widget_destroy (GTK_WIDGET (dialog));
1070                 
1071                 if (dialog_response != GTK_RESPONSE_OK)
1072                         prevent_response = TRUE;
1073         }
1074         /* Check for invalid input: */
1075         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
1076                 prevent_response = TRUE;
1077         }
1078                 
1079         if (prevent_response) {
1080                 /* This is a nasty hack. murrayc. */
1081                 /* Don't let the dialog close */
1082                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1083                 return; 
1084         }
1085                 
1086         if (response_id == GTK_RESPONSE_OK) {
1087                 /* Try to save the changes if modified (NB #59251): */
1088                 if (self->modified)
1089                 {
1090                         const gboolean saved = save_configuration (self);
1091                         if (saved) {
1092                                 /* Do not show the account-saved dialog if we are just saving this 
1093                                  * temporarily, because from the user's point of view it will not 
1094                                  * really be saved (saved + enabled) until later
1095                                  */
1096                                 if (modest_account_settings_get_account_name (self->settings) != NULL) {
1097                                         ModestServerAccountSettings *store_settings;
1098                                         ModestServerAccountSettings *transport_settings;
1099                                         const gchar *store_account_name;
1100                                         const gchar *transport_account_name;
1101
1102
1103                                         store_settings = modest_account_settings_get_store_settings (self->settings);
1104                                         transport_settings = modest_account_settings_get_transport_settings (self->settings);
1105                                         store_account_name = modest_server_account_settings_get_account_name (store_settings);
1106                                         transport_account_name = modest_server_account_settings_get_account_name (transport_settings);
1107                                         
1108                                         if (store_account_name) {
1109                                                 modest_account_mgr_notify_account_update (self->account_manager, 
1110                                                                                           store_account_name);
1111                                         }
1112                                         if (transport_account_name) {
1113                                                 modest_account_mgr_notify_account_update (self->account_manager, 
1114                                                                                           transport_account_name);
1115                                         }
1116                                         g_object_unref (store_settings);
1117                                         g_object_unref (transport_settings);
1118                                         
1119                                         modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1120                                 }
1121                         } else {
1122                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1123                         }
1124                 }
1125         }
1126 }
1127
1128 static void
1129 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
1130 {
1131         /* Create the notebook to be used by the GtkDialog base class:
1132          * Each page of the notebook will be a page of the wizard: */
1133         self->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1134         self->settings = modest_account_settings_new ();
1135
1136         /* Get the account manager object, 
1137          * so we can check for existing accounts,
1138          * and create new accounts: */
1139         self->account_manager = modest_runtime_get_account_mgr ();
1140         g_assert (self->account_manager);
1141         g_object_ref (self->account_manager);
1142         
1143         self->protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1144
1145     /* Create the common pages, 
1146      */
1147         self->page_account_details = create_page_account_details (self);
1148         self->page_user_details = create_page_user_details (self);
1149         self->page_incoming = create_page_incoming (self);
1150         self->page_outgoing = create_page_outgoing (self);
1151         
1152         /* Add the notebook pages: */
1153         gtk_notebook_append_page (self->notebook, self->page_account_details, 
1154                 gtk_label_new (_("mcen_ti_account_settings_account")));
1155         gtk_notebook_append_page (self->notebook, self->page_user_details, 
1156                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1157         gtk_notebook_append_page (self->notebook, self->page_incoming,
1158                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1159         gtk_notebook_append_page (self->notebook, self->page_outgoing,
1160                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1161                 
1162         GtkDialog *dialog = GTK_DIALOG (self);
1163         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (self->notebook));
1164         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1165         gtk_widget_show (GTK_WIDGET (self->notebook));
1166         
1167     /* Add the buttons: */
1168         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1169         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_SAVE, GTK_RESPONSE_OK);
1170     
1171     /* Connect to the dialog's response signal: */
1172     /* We use connect-before 
1173      * so we can stop the signal emission, 
1174      * to stop the default signal handler from closing the dialog.
1175      */
1176     g_signal_connect (G_OBJECT (self), "response",
1177             G_CALLBACK (on_response), self); 
1178             
1179     self->modified = FALSE;
1180
1181     /* When this window is shown, hibernation should not be possible, 
1182          * because there is no sensible way to save the state: */
1183     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1184         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1185
1186     gtk_window_set_default_size (GTK_WINDOW (self), 600, 400);
1187
1188 /*     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings", */
1189 /*                                  modest_maemo_utils_get_osso_context()); */
1190 }
1191
1192 ModestAccountSettingsDialog*
1193 modest_account_settings_dialog_new (void)
1194 {
1195         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
1196 }
1197
1198 /** Update the UI with the stored account details, so they can be edited.
1199  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1200  */
1201 void modest_account_settings_dialog_set_account (ModestAccountSettingsDialog *dialog, ModestAccountSettings *settings)
1202 {
1203         ModestServerAccountSettings *incoming_account;
1204         ModestServerAccountSettings *outgoing_account;
1205         const gchar *account_name;
1206
1207         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1208
1209         incoming_account = modest_account_settings_get_store_settings (settings);
1210         outgoing_account = modest_account_settings_get_transport_settings (settings);
1211
1212         account_name = modest_account_settings_get_account_name (settings);
1213                 
1214         /* Save the account name so we can refer to it later: */
1215         if (dialog->account_name)
1216                 g_free (dialog->account_name);
1217         dialog->account_name = g_strdup (account_name);
1218
1219         if (dialog->settings)
1220                 g_object_unref (dialog->settings);
1221         dialog->settings = g_object_ref (settings);
1222         
1223         /* Save the account title so we can refer to it if the user changes it: */
1224         if (dialog->original_account_title)
1225                 g_free (dialog->original_account_title);
1226         dialog->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1227         
1228         /* Show the account data in the widgets: */
1229         
1230         /* Note that we never show the non-display name in the UI.
1231          * (Though the display name defaults to the non-display name at the start.) */
1232         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1233                             null_means_empty (modest_account_settings_get_display_name (settings)));
1234         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1235                             null_means_empty (modest_account_settings_get_fullname (settings)));
1236         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1237                             null_means_empty (modest_account_settings_get_email_address (settings)));
1238         modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1239                                         modest_server_account_settings_get_protocol (incoming_account));
1240         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1241                                                             modest_account_settings_get_retrieve_type (settings));
1242         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1243                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), 
1244                 modest_account_settings_get_retrieve_limit (settings));
1245         
1246         
1247         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), 
1248                                       modest_account_settings_get_leave_messages_on_server (settings));
1249         
1250         /* Only show the leave-on-server checkbox for POP, 
1251          * as per the UI spec: */
1252         if (modest_server_account_settings_get_protocol (incoming_account) != MODEST_PROTOCOL_STORE_POP) {
1253                 gtk_widget_hide (dialog->caption_leave_messages);
1254         } else {
1255                 gtk_widget_show (dialog->caption_leave_messages);
1256         }
1257         
1258         update_incoming_server_security_choices (dialog, modest_server_account_settings_get_protocol (incoming_account));
1259         if (incoming_account) {
1260                 const gchar *username;
1261                 const gchar *password;
1262                 const gchar *hostname;
1263                 /* Remember this for later: */
1264                 dialog->incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1265                 
1266                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1267                 username = modest_server_account_settings_get_username (incoming_account);
1268                 password = modest_server_account_settings_get_password (incoming_account);
1269                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1270                                     null_means_empty (username));
1271                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1272                                     null_means_empty (password));
1273                         
1274                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1275                                     null_means_empty (hostname));
1276                         
1277                 /* The UI spec says:
1278                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1279          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1280                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1281          */                                                                                                              
1282                 modest_serversecurity_combo_box_set_active_serversecurity (
1283                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), 
1284                         modest_server_account_settings_get_security (incoming_account));
1285                 
1286                 /* Check if we have
1287                  - a secure protocol
1288                  OR
1289                  - use encrypted passwords
1290                 */
1291                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (incoming_account);
1292                 dialog->protocol_authentication_incoming = (secure_auth != MODEST_PROTOCOL_AUTH_NONE)?
1293                         secure_auth:MODEST_PROTOCOL_AUTH_PASSWORD;
1294                 if (modest_protocol_info_auth_is_secure(secure_auth))
1295                 {
1296                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1297                                                      TRUE);
1298                 }
1299                 else
1300                 {
1301                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1302                                                      FALSE);
1303                 };
1304                                         
1305                 update_incoming_server_title (dialog, dialog->incoming_protocol);
1306                 
1307                 const gint port_num = modest_server_account_settings_get_port (incoming_account);
1308                 if (port_num == 0) {
1309                         /* Show the appropriate port number: */
1310                         on_combo_incoming_security_changed (
1311                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1312                 } else {
1313                         /* Keep the user-entered port-number,
1314                          * or the already-appropriate automatic port number: */
1315                         gtk_spin_button_set_value (
1316                                 GTK_SPIN_BUTTON (dialog->entry_incoming_port), (gdouble) port_num);
1317                 }
1318                 g_object_unref (incoming_account);
1319         }
1320         
1321         outgoing_account = modest_account_settings_get_transport_settings (settings);
1322         if (outgoing_account) {
1323                 const gchar *hostname;
1324                 const gchar *username;
1325                 const gchar *password;
1326
1327                 /* Remember this for later: */
1328                 dialog->outgoing_protocol = 
1329                         modest_server_account_settings_get_protocol (outgoing_account);
1330
1331                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1332                 username = modest_server_account_settings_get_username (outgoing_account);
1333                 password = modest_server_account_settings_get_password (outgoing_account);
1334                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1335                                     null_means_empty (hostname));
1336                 
1337                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1338                                     null_means_empty (username));
1339                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1340                                     null_means_empty (password));
1341                 
1342                 /* Get the secure-auth setting: */
1343                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (outgoing_account);
1344                 modest_secureauth_combo_box_set_active_secureauth (
1345                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1346                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1347                 
1348                 modest_serversecurity_combo_box_fill (
1349                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), 
1350                         dialog->outgoing_protocol);
1351                 
1352                 /* Get the security setting: */
1353                 const ModestConnectionProtocol security = modest_server_account_settings_get_security (outgoing_account);
1354                 modest_serversecurity_combo_box_set_active_serversecurity (
1355                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1356                 
1357                 const gint port_num = modest_server_account_settings_get_port (outgoing_account);
1358                 if (port_num == 0) {
1359                         /* Show the appropriate port number: */
1360                         on_combo_outgoing_security_changed (
1361                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1362                 }
1363                 else {
1364                         /* Keep the user-entered port-number,
1365                          * or the already-appropriate automatic port number: */
1366                         gtk_spin_button_set_value (
1367                                 GTK_SPIN_BUTTON (dialog->entry_outgoing_port), (gdouble) port_num);
1368                 }
1369                 
1370                 const gboolean has_specific = 
1371                         modest_account_settings_get_use_connection_specific_smtp (settings);
1372                 gtk_toggle_button_set_active (
1373                         GTK_TOGGLE_BUTTON (dialog->checkbox_outgoing_smtp_specific), 
1374                         has_specific);
1375                 g_object_unref (outgoing_account);
1376         }
1377
1378         /* Set window title according to account: */
1379         /* TODO: Is this the correct way to find a human-readable name for
1380          * the protocol used? */
1381         const gchar* proto_str = modest_protocol_info_get_transport_store_protocol_name (dialog->incoming_protocol);
1382         gchar *proto_name = g_utf8_strup(proto_str, -1);
1383         const gchar *account_title = modest_account_settings_get_display_name(settings);
1384
1385         gchar *title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1386         g_free (proto_name);
1387
1388         gtk_window_set_title (GTK_WINDOW (dialog), title);
1389         g_free (title);
1390
1391         /* account_data->is_enabled,  */
1392         /*account_data->is_default,  */
1393
1394         /* Unset the modified flag so we can detect changes later: */
1395         dialog->modified = FALSE;
1396 }
1397
1398 /** Show the User Info tab.
1399  */
1400 void modest_account_settings_dialog_switch_to_user_info (ModestAccountSettingsDialog *dialog)
1401 {
1402         const gint page_num = gtk_notebook_page_num (dialog->notebook, dialog->page_user_details);
1403         if (page_num == -1) {
1404                 g_warning ("%s: notebook page not found.\n", __FUNCTION__);     
1405         }
1406                 
1407         /* Ensure that the widget is visible so that gtk_notebook_set_current_page() works: */
1408         /* TODO: even this hack (recommened by the GTK+ documentation) doesn't seem to work. */
1409         
1410         gtk_widget_show (dialog->page_user_details);
1411         gtk_widget_show (GTK_WIDGET (dialog->notebook));
1412         gtk_widget_show (GTK_WIDGET (dialog));
1413         gtk_notebook_set_current_page (dialog->notebook, page_num);
1414 }
1415
1416 static gboolean
1417 save_configuration (ModestAccountSettingsDialog *dialog)
1418 {
1419         const gchar* account_name = dialog->account_name;
1420         ModestServerAccountSettings *store_settings;
1421         ModestServerAccountSettings *transport_settings;
1422                 
1423         /* Set the account data from the widgets: */
1424         const gchar* user_fullname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1425         modest_account_settings_set_fullname (dialog->settings, user_fullname);
1426         
1427         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1428         modest_account_settings_set_email_address (dialog->settings, emailaddress);
1429                 
1430 /*      /\* Signature: *\/ */
1431 /*      if (dialog->signature_dialog) { */
1432 /*              gboolean use_signature = FALSE; */
1433 /*              gchar *signature =  */
1434 /*                      modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog), */
1435 /*                                                                   &use_signature); */
1436         
1437 /*              modest_account_settings_set_use_signature (dialog->settings, use_signature); */
1438 /*              modest_account_settings_set_signature (dialog->settings, signature); */
1439 /*      } */
1440         
1441         ModestAccountRetrieveType retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1442                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1443         modest_account_settings_set_retrieve_type (dialog->settings, retrieve_type);
1444         
1445         gint retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1446                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1447         modest_account_settings_set_retrieve_limit (dialog->settings, retrieve_limit);
1448         
1449         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1450         modest_account_settings_set_leave_messages_on_server (dialog->settings, leave_on_server); 
1451
1452         store_settings = modest_account_settings_get_store_settings (dialog->settings);
1453                         
1454         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1455         modest_server_account_settings_set_hostname (store_settings, hostname);
1456                                 
1457         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1458         modest_server_account_settings_set_username (store_settings, username);
1459         
1460         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1461         modest_server_account_settings_set_password (store_settings, password);
1462                         
1463         /* port: */
1464         gint port_num = gtk_spin_button_get_value_as_int (
1465                         GTK_SPIN_BUTTON (dialog->entry_incoming_port));
1466         modest_server_account_settings_set_port (store_settings, port_num);
1467                         
1468         /* The UI spec says:
1469          * If secure authentication is unchecked, allow sending username and password also as plain text.
1470          * If secure authentication is checked, require one of the secure 
1471          * methods during connection: SSL, TLS, CRAM-MD5 etc. 
1472          */
1473         
1474         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1475                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1476         modest_server_account_settings_set_security (store_settings, protocol_security_incoming);       
1477         modest_server_account_settings_set_auth_protocol (store_settings, dialog->protocol_authentication_incoming);
1478
1479         g_object_unref (store_settings);
1480         
1481         /* Outgoing: */
1482         transport_settings = modest_account_settings_get_transport_settings (dialog->settings);
1483         
1484         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1485         modest_server_account_settings_set_hostname (transport_settings, hostname);
1486                 
1487         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1488         modest_server_account_settings_set_username (transport_settings, username);
1489                 
1490         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1491         modest_server_account_settings_set_password (transport_settings, password);
1492         
1493         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1494                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1495         modest_server_account_settings_set_security (transport_settings, protocol_security_outgoing);
1496         
1497         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1498                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1499         modest_server_account_settings_set_auth_protocol (transport_settings, protocol_authentication_outgoing);        
1500         
1501         /* port: */
1502         port_num = gtk_spin_button_get_value_as_int (
1503                         GTK_SPIN_BUTTON (dialog->entry_outgoing_port));
1504         modest_server_account_settings_set_port (transport_settings, port_num);
1505         g_object_unref (transport_settings);
1506         
1507         
1508         /* Set the changed account title last, to simplify the previous code: */
1509         gchar* account_title = get_entered_account_title (dialog);
1510         if (!account_title)
1511                 return FALSE; /* Should be prevented already anyway. */
1512                 
1513 /*      if (strcmp (account_title, account_name) != 0) { */
1514         modest_account_settings_set_display_name (dialog->settings, account_title);
1515 /*      } */
1516         g_free (account_title);
1517         account_title = NULL;
1518         
1519         /* Save connection-specific SMTP server accounts: */
1520         modest_account_settings_set_use_connection_specific_smtp 
1521                 (dialog->settings, 
1522                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbox_outgoing_smtp_specific)));
1523
1524         /* this configuration is not persistent, we should not save */
1525         if (account_name != NULL)
1526                 modest_account_mgr_save_account_settings (dialog->account_manager, dialog->settings);
1527
1528 /*      if (dialog->specific_window) { */
1529 /*              return modest_connection_specific_smtp_window_save_server_accounts ( */
1530 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window)); */
1531 /*      } else { */
1532                 return TRUE;
1533 /*      } */
1534
1535 }
1536
1537 static gboolean entry_is_empty (GtkWidget *entry)
1538 {
1539         if (!entry)
1540                 return FALSE;
1541                 
1542         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1543         if ((!text) || (strlen(text) == 0))
1544                 return TRUE;
1545         else {
1546                 /* Strip it of whitespace at the start and end: */
1547                 gchar *stripped = g_strdup (text);
1548                 stripped = g_strstrip (stripped);
1549                 
1550                 if (!stripped)
1551                         return TRUE;
1552                         
1553                 const gboolean result = (strlen (stripped) == 0);
1554                 
1555                 g_free (stripped);
1556                 return result;
1557         }
1558 }
1559
1560 static void
1561 enable_buttons (ModestAccountSettingsDialog *self)
1562 {
1563         gboolean enable_ok = TRUE;
1564         ModestAuthProtocol outgoing_auth_protocol;
1565         
1566         /* The account details title is mandatory: */
1567         if (entry_is_empty(self->entry_account_title))
1568                 enable_ok = FALSE;
1569
1570         /* The user details username is mandatory: */
1571         if (enable_ok && entry_is_empty(self->entry_user_username))
1572                 enable_ok = FALSE;
1573                 
1574         /* The user details email address is mandatory: */
1575         if (enable_ok && entry_is_empty (self->entry_user_email))
1576                 enable_ok = FALSE;
1577
1578         /* The custom incoming server is mandatory: */
1579         if (enable_ok && entry_is_empty(self->entry_incomingserver))
1580                 enable_ok = FALSE;
1581
1582         /* The custom incoming server is mandatory: */
1583         if (enable_ok && entry_is_empty(self->entry_outgoingserver))
1584                 enable_ok = FALSE;
1585
1586         /* Outgoing username is mandatory if outgoing auth is secure */
1587         if (self->combo_outgoing_auth) {
1588                 outgoing_auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
1589                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1590                 if (enable_ok && 
1591                     outgoing_auth_protocol != MODEST_PROTOCOL_AUTH_NONE &&
1592                     entry_is_empty (self->entry_outgoing_username))
1593                         enable_ok = FALSE;
1594         }
1595                         
1596         /* Enable the buttons, 
1597          * identifying them via their associated response codes:
1598          */
1599         GtkDialog *dialog_base = GTK_DIALOG (self);
1600         gtk_dialog_set_response_sensitive (dialog_base,
1601                                            GTK_RESPONSE_OK,
1602                                            enable_ok);
1603 }
1604
1605 static void
1606 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1607 {
1608         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1609         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1610
1611
1612         object_class->get_property = modest_account_settings_dialog_get_property;
1613         object_class->set_property = modest_account_settings_dialog_set_property;
1614         object_class->dispose = modest_account_settings_dialog_dispose;
1615         object_class->finalize = modest_account_settings_dialog_finalize;
1616 }