4717b1b31b8d74023c0a24fa79ca3e08df2d8148
[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 (GTK_BIN (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                                         if (!self->save_password)
1120                                                 modest_platform_information_banner(NULL, NULL, _("mcen_ib_advsetup_settings_saved"));
1121                                 }
1122                         } else {
1123                                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1124                         }
1125                 }
1126         }
1127 }
1128
1129 static void
1130 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
1131 {
1132         /* Create the notebook to be used by the GtkDialog base class:
1133          * Each page of the notebook will be a page of the wizard: */
1134         self->notebook = GTK_NOTEBOOK (gtk_notebook_new());
1135         self->settings = modest_account_settings_new ();
1136
1137         /* Get the account manager object, 
1138          * so we can check for existing accounts,
1139          * and create new accounts: */
1140         self->account_manager = modest_runtime_get_account_mgr ();
1141         g_assert (self->account_manager);
1142         g_object_ref (self->account_manager);
1143         
1144         self->protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1145
1146     /* Create the common pages, 
1147      */
1148         self->page_account_details = create_page_account_details (self);
1149         self->page_user_details = create_page_user_details (self);
1150         self->page_incoming = create_page_incoming (self);
1151         self->page_outgoing = create_page_outgoing (self);
1152         
1153         /* Add the notebook pages: */
1154         gtk_notebook_append_page (self->notebook, self->page_account_details, 
1155                 gtk_label_new (_("mcen_ti_account_settings_account")));
1156         gtk_notebook_append_page (self->notebook, self->page_user_details, 
1157                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
1158         gtk_notebook_append_page (self->notebook, self->page_incoming,
1159                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
1160         gtk_notebook_append_page (self->notebook, self->page_outgoing,
1161                 gtk_label_new (_("mcen_ti_advsetup_sending")));
1162                 
1163         GtkDialog *dialog = GTK_DIALOG (self);
1164         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (self->notebook));
1165         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
1166         gtk_widget_show (GTK_WIDGET (self->notebook));
1167         
1168     /* Add the buttons: */
1169         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1170         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_SAVE, GTK_RESPONSE_OK);
1171     
1172     /* Connect to the dialog's response signal: */
1173     /* We use connect-before 
1174      * so we can stop the signal emission, 
1175      * to stop the default signal handler from closing the dialog.
1176      */
1177     g_signal_connect (G_OBJECT (self), "response",
1178             G_CALLBACK (on_response), self); 
1179             
1180     self->modified = FALSE;
1181     self->save_password;
1182
1183     /* When this window is shown, hibernation should not be possible, 
1184          * because there is no sensible way to save the state: */
1185     modest_window_mgr_prevent_hibernation_while_window_is_shown (
1186         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1187
1188     gtk_window_set_default_size (GTK_WINDOW (self), 600, 400);
1189
1190 /*     hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_accountsettings", */
1191 /*                                  modest_maemo_utils_get_osso_context()); */
1192 }
1193
1194 ModestAccountSettingsDialog*
1195 modest_account_settings_dialog_new (void)
1196 {
1197         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
1198 }
1199
1200 /** Update the UI with the stored account details, so they can be edited.
1201  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
1202  */
1203 void modest_account_settings_dialog_set_account (ModestAccountSettingsDialog *dialog, ModestAccountSettings *settings)
1204 {
1205         ModestServerAccountSettings *incoming_account;
1206         ModestServerAccountSettings *outgoing_account;
1207         const gchar *account_name;
1208
1209         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS (settings));
1210
1211         incoming_account = modest_account_settings_get_store_settings (settings);
1212         outgoing_account = modest_account_settings_get_transport_settings (settings);
1213
1214         account_name = modest_account_settings_get_account_name (settings);
1215                 
1216         /* Save the account name so we can refer to it later: */
1217         if (dialog->account_name)
1218                 g_free (dialog->account_name);
1219         dialog->account_name = g_strdup (account_name);
1220
1221         if (dialog->settings)
1222                 g_object_unref (dialog->settings);
1223         dialog->settings = g_object_ref (settings);
1224         
1225         /* Save the account title so we can refer to it if the user changes it: */
1226         if (dialog->original_account_title)
1227                 g_free (dialog->original_account_title);
1228         dialog->original_account_title = g_strdup (modest_account_settings_get_display_name (settings));
1229         
1230         /* Show the account data in the widgets: */
1231         
1232         /* Note that we never show the non-display name in the UI.
1233          * (Though the display name defaults to the non-display name at the start.) */
1234         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
1235                             null_means_empty (modest_account_settings_get_display_name (settings)));
1236         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
1237                             null_means_empty (modest_account_settings_get_fullname (settings)));
1238         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
1239                             null_means_empty (modest_account_settings_get_email_address (settings)));
1240         modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1241                                         modest_server_account_settings_get_protocol (incoming_account));
1242         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), 
1243                                                             modest_account_settings_get_retrieve_type (settings));
1244         modest_limit_retrieve_combo_box_set_active_limit_retrieve (
1245                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), 
1246                 modest_account_settings_get_retrieve_limit (settings));
1247         
1248         
1249         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), 
1250                                       modest_account_settings_get_leave_messages_on_server (settings));
1251         
1252         /* Only show the leave-on-server checkbox for POP, 
1253          * as per the UI spec: */
1254         if (modest_server_account_settings_get_protocol (incoming_account) != MODEST_PROTOCOL_STORE_POP) {
1255                 gtk_widget_hide (dialog->caption_leave_messages);
1256         } else {
1257                 gtk_widget_show (dialog->caption_leave_messages);
1258         }
1259         
1260         update_incoming_server_security_choices (dialog, modest_server_account_settings_get_protocol (incoming_account));
1261         if (incoming_account) {
1262                 const gchar *username;
1263                 const gchar *password;
1264                 const gchar *hostname;
1265                 /* Remember this for later: */
1266                 dialog->incoming_protocol = modest_server_account_settings_get_protocol (incoming_account);;
1267                 
1268                 hostname = modest_server_account_settings_get_hostname (incoming_account);
1269                 username = modest_server_account_settings_get_username (incoming_account);
1270                 password = modest_server_account_settings_get_password (incoming_account);
1271                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1272                                     null_means_empty (username));
1273                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1274                                     null_means_empty (password));
1275                         
1276                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1277                                     null_means_empty (hostname));
1278                         
1279                 /* The UI spec says:
1280                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1281          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1282                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1283          */                                                                                                              
1284                 modest_serversecurity_combo_box_set_active_serversecurity (
1285                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), 
1286                         modest_server_account_settings_get_security (incoming_account));
1287                 
1288                 /* Check if we have
1289                  - a secure protocol
1290                  OR
1291                  - use encrypted passwords
1292                 */
1293                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (incoming_account);
1294                 dialog->protocol_authentication_incoming = (secure_auth != MODEST_PROTOCOL_AUTH_NONE)?
1295                         secure_auth:MODEST_PROTOCOL_AUTH_PASSWORD;
1296                 if (modest_protocol_info_auth_is_secure(secure_auth))
1297                 {
1298                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1299                                                      TRUE);
1300                 }
1301                 else
1302                 {
1303                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1304                                                      FALSE);
1305                 };
1306                                         
1307                 update_incoming_server_title (dialog, dialog->incoming_protocol);
1308                 
1309                 const gint port_num = modest_server_account_settings_get_port (incoming_account);
1310                 if (port_num == 0) {
1311                         /* Show the appropriate port number: */
1312                         on_combo_incoming_security_changed (
1313                                 GTK_COMBO_BOX (dialog->combo_incoming_security), dialog);
1314                 } else {
1315                         /* Keep the user-entered port-number,
1316                          * or the already-appropriate automatic port number: */
1317                         gtk_spin_button_set_value (
1318                                 GTK_SPIN_BUTTON (dialog->entry_incoming_port), (gdouble) port_num);
1319                 }
1320                 g_object_unref (incoming_account);
1321         }
1322         
1323         outgoing_account = modest_account_settings_get_transport_settings (settings);
1324         if (outgoing_account) {
1325                 const gchar *hostname;
1326                 const gchar *username;
1327                 const gchar *password;
1328
1329                 /* Remember this for later: */
1330                 dialog->outgoing_protocol = 
1331                         modest_server_account_settings_get_protocol (outgoing_account);
1332
1333                 hostname = modest_server_account_settings_get_hostname (outgoing_account);
1334                 username = modest_server_account_settings_get_username (outgoing_account);
1335                 password = modest_server_account_settings_get_password (outgoing_account);
1336                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1337                                     null_means_empty (hostname));
1338                 
1339                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1340                                     null_means_empty (username));
1341                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1342                                     null_means_empty (password));
1343                 
1344                 /* Get the secure-auth setting: */
1345                 const ModestAuthProtocol secure_auth = modest_server_account_settings_get_auth_protocol (outgoing_account);
1346                 modest_secureauth_combo_box_set_active_secureauth (
1347                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1348                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1349                 
1350                 modest_serversecurity_combo_box_fill (
1351                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), 
1352                         dialog->outgoing_protocol);
1353                 
1354                 /* Get the security setting: */
1355                 const ModestConnectionProtocol security = modest_server_account_settings_get_security (outgoing_account);
1356                 modest_serversecurity_combo_box_set_active_serversecurity (
1357                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1358                 
1359                 const gint port_num = modest_server_account_settings_get_port (outgoing_account);
1360                 if (port_num == 0) {
1361                         /* Show the appropriate port number: */
1362                         on_combo_outgoing_security_changed (
1363                                 GTK_COMBO_BOX (dialog->combo_outgoing_security), dialog);
1364                 }
1365                 else {
1366                         /* Keep the user-entered port-number,
1367                          * or the already-appropriate automatic port number: */
1368                         gtk_spin_button_set_value (
1369                                 GTK_SPIN_BUTTON (dialog->entry_outgoing_port), (gdouble) port_num);
1370                 }
1371                 
1372                 const gboolean has_specific = 
1373                         modest_account_settings_get_use_connection_specific_smtp (settings);
1374                 gtk_toggle_button_set_active (
1375                         GTK_TOGGLE_BUTTON (dialog->checkbox_outgoing_smtp_specific), 
1376                         has_specific);
1377                 g_object_unref (outgoing_account);
1378         }
1379
1380         /* Set window title according to account: */
1381         /* TODO: Is this the correct way to find a human-readable name for
1382          * the protocol used? */
1383         const gchar* proto_str = modest_protocol_info_get_transport_store_protocol_name (dialog->incoming_protocol);
1384         gchar *proto_name = g_utf8_strup(proto_str, -1);
1385         const gchar *account_title = modest_account_settings_get_display_name(settings);
1386
1387         gchar *title = g_strdup_printf(_("mcen_ti_account_settings"), proto_name, account_title);
1388         g_free (proto_name);
1389
1390         gtk_window_set_title (GTK_WINDOW (dialog), title);
1391         g_free (title);
1392
1393         /* account_data->is_enabled,  */
1394         /*account_data->is_default,  */
1395
1396         /* Unset the modified flag so we can detect changes later: */
1397         dialog->modified = FALSE;
1398 }
1399
1400 /** Show the User Info tab.
1401  */
1402 void modest_account_settings_dialog_switch_to_user_info (ModestAccountSettingsDialog *dialog)
1403 {
1404         const gint page_num = gtk_notebook_page_num (dialog->notebook, dialog->page_user_details);
1405         if (page_num == -1) {
1406                 g_warning ("%s: notebook page not found.\n", __FUNCTION__);     
1407         }
1408                 
1409         /* Ensure that the widget is visible so that gtk_notebook_set_current_page() works: */
1410         /* TODO: even this hack (recommened by the GTK+ documentation) doesn't seem to work. */
1411         
1412         gtk_widget_show (dialog->page_user_details);
1413         gtk_widget_show (GTK_WIDGET (dialog->notebook));
1414         gtk_widget_show (GTK_WIDGET (dialog));
1415         gtk_notebook_set_current_page (dialog->notebook, page_num);
1416 }
1417
1418 static gboolean
1419 save_configuration (ModestAccountSettingsDialog *dialog)
1420 {
1421         const gchar* account_name = dialog->account_name;
1422         ModestServerAccountSettings *store_settings;
1423         ModestServerAccountSettings *transport_settings;
1424                 
1425         /* Set the account data from the widgets: */
1426         const gchar* user_fullname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1427         modest_account_settings_set_fullname (dialog->settings, user_fullname);
1428         
1429         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1430         modest_account_settings_set_email_address (dialog->settings, emailaddress);
1431                 
1432 /*      /\* Signature: *\/ */
1433 /*      if (dialog->signature_dialog) { */
1434 /*              gboolean use_signature = FALSE; */
1435 /*              gchar *signature =  */
1436 /*                      modest_signature_editor_dialog_get_settings (MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog), */
1437 /*                                                                   &use_signature); */
1438         
1439 /*              modest_account_settings_set_use_signature (dialog->settings, use_signature); */
1440 /*              modest_account_settings_set_signature (dialog->settings, signature); */
1441 /*      } */
1442         
1443         ModestAccountRetrieveType retrieve_type = modest_retrieve_combo_box_get_active_retrieve_conf (
1444                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1445         modest_account_settings_set_retrieve_type (dialog->settings, retrieve_type);
1446         
1447         gint retrieve_limit = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1448                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1449         modest_account_settings_set_retrieve_limit (dialog->settings, retrieve_limit);
1450         
1451         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1452         modest_account_settings_set_leave_messages_on_server (dialog->settings, leave_on_server); 
1453
1454         store_settings = modest_account_settings_get_store_settings (dialog->settings);
1455                         
1456         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1457         modest_server_account_settings_set_hostname (store_settings, hostname);
1458                                 
1459         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1460         modest_server_account_settings_set_username (store_settings, username);
1461         
1462         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1463         modest_server_account_settings_set_password (store_settings, password);
1464                         
1465         /* port: */
1466         gint port_num = gtk_spin_button_get_value_as_int (
1467                         GTK_SPIN_BUTTON (dialog->entry_incoming_port));
1468         modest_server_account_settings_set_port (store_settings, port_num);
1469                         
1470         /* The UI spec says:
1471          * If secure authentication is unchecked, allow sending username and password also as plain text.
1472          * If secure authentication is checked, require one of the secure 
1473          * methods during connection: SSL, TLS, CRAM-MD5 etc. 
1474          */
1475         
1476         const ModestConnectionProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1477                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1478         modest_server_account_settings_set_security (store_settings, protocol_security_incoming);       
1479         modest_server_account_settings_set_auth_protocol (store_settings, dialog->protocol_authentication_incoming);
1480
1481         g_object_unref (store_settings);
1482         
1483         /* Outgoing: */
1484         transport_settings = modest_account_settings_get_transport_settings (dialog->settings);
1485         
1486         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1487         modest_server_account_settings_set_hostname (transport_settings, hostname);
1488                 
1489         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1490         modest_server_account_settings_set_username (transport_settings, username);
1491                 
1492         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1493         modest_server_account_settings_set_password (transport_settings, password);
1494         
1495         const ModestConnectionProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1496                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1497         modest_server_account_settings_set_security (transport_settings, protocol_security_outgoing);
1498         
1499         const ModestAuthProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1500                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1501         modest_server_account_settings_set_auth_protocol (transport_settings, protocol_authentication_outgoing);        
1502         
1503         /* port: */
1504         port_num = gtk_spin_button_get_value_as_int (
1505                         GTK_SPIN_BUTTON (dialog->entry_outgoing_port));
1506         modest_server_account_settings_set_port (transport_settings, port_num);
1507         g_object_unref (transport_settings);
1508         
1509         
1510         /* Set the changed account title last, to simplify the previous code: */
1511         gchar* account_title = get_entered_account_title (dialog);
1512         if (!account_title)
1513                 return FALSE; /* Should be prevented already anyway. */
1514                 
1515 /*      if (strcmp (account_title, account_name) != 0) { */
1516         modest_account_settings_set_display_name (dialog->settings, account_title);
1517 /*      } */
1518         g_free (account_title);
1519         account_title = NULL;
1520         
1521         /* Save connection-specific SMTP server accounts: */
1522         modest_account_settings_set_use_connection_specific_smtp 
1523                 (dialog->settings, 
1524                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->checkbox_outgoing_smtp_specific)));
1525
1526         /* this configuration is not persistent, we should not save */
1527         if (account_name != NULL)
1528                 modest_account_mgr_save_account_settings (dialog->account_manager, dialog->settings);
1529
1530 /*      if (dialog->specific_window) { */
1531 /*              return modest_connection_specific_smtp_window_save_server_accounts ( */
1532 /*                      MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window)); */
1533 /*      } else { */
1534                 return TRUE;
1535 /*      } */
1536
1537 }
1538
1539 static gboolean entry_is_empty (GtkWidget *entry)
1540 {
1541         if (!entry)
1542                 return FALSE;
1543                 
1544         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1545         if ((!text) || (strlen(text) == 0))
1546                 return TRUE;
1547         else {
1548                 /* Strip it of whitespace at the start and end: */
1549                 gchar *stripped = g_strdup (text);
1550                 stripped = g_strstrip (stripped);
1551                 
1552                 if (!stripped)
1553                         return TRUE;
1554                         
1555                 const gboolean result = (strlen (stripped) == 0);
1556                 
1557                 g_free (stripped);
1558                 return result;
1559         }
1560 }
1561
1562 static void
1563 enable_buttons (ModestAccountSettingsDialog *self)
1564 {
1565         gboolean enable_ok = TRUE;
1566         ModestAuthProtocol outgoing_auth_protocol;
1567         
1568         /* The account details title is mandatory: */
1569         if (entry_is_empty(self->entry_account_title))
1570                 enable_ok = FALSE;
1571
1572         /* The user details username is mandatory: */
1573         if (enable_ok && entry_is_empty(self->entry_user_username))
1574                 enable_ok = FALSE;
1575                 
1576         /* The user details email address is mandatory: */
1577         if (enable_ok && entry_is_empty (self->entry_user_email))
1578                 enable_ok = FALSE;
1579
1580         /* The custom incoming server is mandatory: */
1581         if (enable_ok && entry_is_empty(self->entry_incomingserver))
1582                 enable_ok = FALSE;
1583
1584         /* The custom incoming server is mandatory: */
1585         if (enable_ok && entry_is_empty(self->entry_outgoingserver))
1586                 enable_ok = FALSE;
1587
1588         /* Outgoing username is mandatory if outgoing auth is secure */
1589         if (self->combo_outgoing_auth) {
1590                 outgoing_auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
1591                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1592                 if (enable_ok && 
1593                     outgoing_auth_protocol != MODEST_PROTOCOL_AUTH_NONE &&
1594                     entry_is_empty (self->entry_outgoing_username))
1595                         enable_ok = FALSE;
1596         }
1597                         
1598         /* Enable the buttons, 
1599          * identifying them via their associated response codes:
1600          */
1601         GtkDialog *dialog_base = GTK_DIALOG (self);
1602         gtk_dialog_set_response_sensitive (dialog_base,
1603                                            GTK_RESPONSE_OK,
1604                                            enable_ok);
1605 }
1606
1607 void
1608 modest_account_settings_dialog_check_allow_changes (ModestAccountSettingsDialog *self)
1609 {
1610         ModestServerAccountSettings *incoming_settings;
1611         const gchar *server_account_name;
1612         gboolean username_known;
1613
1614         if (!G_IS_OBJECT (self->settings))
1615                 return;
1616
1617         incoming_settings = modest_account_settings_get_store_settings (self->settings);
1618         server_account_name = modest_server_account_settings_get_account_name (incoming_settings);
1619
1620         username_known = modest_account_mgr_get_server_account_username_has_succeeded (self->account_manager, 
1621                                                                                        server_account_name);
1622
1623         /* Enable or disable widgets */
1624         gtk_widget_set_sensitive (self->entry_user_username, !username_known);
1625         gtk_widget_set_sensitive (self->entry_incomingserver, !username_known);
1626         gtk_widget_set_sensitive (self->entry_outgoingserver, !username_known);
1627         gtk_widget_set_sensitive (self->entry_outgoing_username, !username_known);
1628 }
1629
1630 void
1631 modest_account_settings_dialog_save_password (ModestAccountSettingsDialog *dialog)
1632 {
1633         g_return_if_fail (MODEST_IS_ACCOUNT_SETTINGS_DIALOG (dialog));
1634
1635         dialog->save_password = TRUE;
1636         dialog->modified = TRUE;
1637 }
1638
1639 static void
1640 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1641 {
1642         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1643         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1644
1645
1646         object_class->get_property = modest_account_settings_dialog_get_property;
1647         object_class->set_property = modest_account_settings_dialog_set_property;
1648         object_class->dispose = modest_account_settings_dialog_dispose;
1649         object_class->finalize = modest_account_settings_dialog_finalize;
1650 }