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