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