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