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