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