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