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