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