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