2007-05-14 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-account-settings-dialog.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-account-settings-dialog.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <gtk/gtkstock.h>
18 #include "modest-hildon-includes.h"
19
20 #include "widgets/modest-serversecurity-combo-box.h"
21 #include "widgets/modest-secureauth-combo-box.h"
22 #include "widgets/modest-validating-entry.h"
23 #include "widgets/modest-retrieve-combo-box.h"
24 #include "widgets/modest-limit-retrieve-combo-box.h"
25 #include "modest-text-utils.h"
26 #include "modest-account-mgr.h"
27 #include "modest-account-mgr-helpers.h" /* For modest_account_mgr_get_account_data(). */
28 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
29 #include "maemo/modest-connection-specific-smtp-window.h"
30 #include "maemo/modest-signature-editor-dialog.h"
31 #include "widgets/modest-ui-constants.h"
32 #include <gconf/gconf-client.h>
33 #include <string.h> /* For strlen(). */
34
35 /* Include config.h so that _() works: */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
41
42 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
43
44 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
45         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
46
47 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
48
49 struct _ModestAccountSettingsDialogPrivate
50 {
51 };
52
53 static void
54 enable_buttons (ModestAccountSettingsDialog *self);
55
56 static gboolean
57 save_configuration (ModestAccountSettingsDialog *dialog);
58
59 static void
60 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
61                                                                                                                         GValue *value, GParamSpec *pspec)
62 {
63         switch (property_id) {
64         default:
65                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66         }
67 }
68
69 static void
70 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
71                                                                                                                         const GValue *value, GParamSpec *pspec)
72 {
73         switch (property_id) {
74         default:
75                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76         }
77 }
78
79 static void
80 modest_account_settings_dialog_dispose (GObject *object)
81 {
82         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
83                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
84 }
85
86 static void
87 modest_account_settings_dialog_finalize (GObject *object)
88 {
89         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
90         
91         if (self->account_name)
92                 g_free (self->account_name);
93                 
94         if (self->original_account_title)
95                 g_free (self->original_account_title);
96                 
97         if (self->account_manager)
98                 g_object_unref (G_OBJECT (self->account_manager));
99                 
100         if (self->specific_window)
101                 gtk_widget_destroy (self->specific_window);
102                 
103         if (self->signature_dialog)
104                 gtk_widget_destroy (self->signature_dialog);
105         
106         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
107 }
108
109 static void
110 show_error (GtkWindow *parent_window, const gchar* text);
111
112 static void
113 show_ok (GtkWindow *parent_window, const gchar* text);
114
115 static void
116 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data);
117
118 static void
119 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data);
120
121 static void
122 on_modified_combobox_changed (GtkComboBox *widget, gpointer user_data)
123 {
124         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
125         self->modified = TRUE;
126 }
127
128 static void
129 on_modified_entry_changed (GtkEditable *editable, gpointer user_data)
130 {
131         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
132         self->modified = TRUE;
133 }
134
135 static void
136 on_modified_checkbox_toggled (GtkToggleButton *togglebutton, gpointer user_data)
137 {
138         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
139         self->modified = TRUE;
140 }
141
142 /* Set a modified boolean whenever the widget is changed, 
143  * so we can check for it later.
144  */
145 static void
146 connect_for_modified (ModestAccountSettingsDialog *self, GtkWidget *widget)
147 {
148         if (GTK_IS_ENTRY (widget)) {
149           g_signal_connect (G_OBJECT (widget), "changed",
150                 G_CALLBACK (on_modified_entry_changed), self);  
151         } else if (GTK_IS_COMBO_BOX (widget)) {
152                 g_signal_connect (G_OBJECT (widget), "changed",
153                 G_CALLBACK (on_modified_combobox_changed), self);       
154         } else if (GTK_IS_TOGGLE_BUTTON (widget)) {
155                 g_signal_connect (G_OBJECT (widget), "toggled",
156                 G_CALLBACK (on_modified_checkbox_toggled), self);
157         }
158 }
159
160 static void
161 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
162 {
163         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
164         g_assert(self);
165         enable_buttons(self);
166 }
167
168 static void
169 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
170 {
171         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
172         g_assert(self);
173         enable_buttons(self);
174 }
175
176 /** This is a convenience function to create a caption containing a mandatory widget.
177  * When the widget is edited, the enable_buttons() vfunc will be called.
178  */
179 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
180         GtkSizeGroup *group,
181         const gchar *value,
182         GtkWidget *control,
183         GtkWidget *icon,
184         HildonCaptionStatus flag)
185 {
186   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
187   
188 /* The translated strings seem to already contain the *,
189  * but this code can be used if that is not true in future.
190  */
191 #if 0
192         /* Add a * character to indicate mandatory fields,
193          * as specified in our "Email UI Specification": */
194         if (flag == HILDON_CAPTION_MANDATORY) {
195                 gchar* title = g_strdup_printf("%s*", value);
196                 caption = hildon_caption_new (group, title, control, icon, flag);       
197                 g_free(title);
198         }       
199         else
200                 caption = hildon_caption_new (group, value, control, icon, flag);
201 #endif
202
203         /* Connect to the appropriate changed signal for the widget, 
204          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
205          */
206         if (GTK_IS_ENTRY (control)) {
207                 g_signal_connect (G_OBJECT (control), "changed",
208                 G_CALLBACK (on_caption_entry_changed), self);
209                 
210         }
211         else if (GTK_IS_COMBO_BOX (control)) {
212                 g_signal_connect (G_OBJECT (control), "changed",
213                 G_CALLBACK (on_caption_combobox_changed), self);
214         }
215          
216         return caption;
217 }
218
219
220 static void
221 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
222 {
223         ModestAccountSettingsDialog *dialog = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
224         show_error (GTK_WINDOW (dialog), _("ckdg_ib_maximum_characters_reached"));
225 }
226
227 static GtkWidget*
228 create_page_account_details (ModestAccountSettingsDialog *self)
229 {
230         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
231         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
232         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
233         gtk_widget_show (label);
234         
235         /* Create a size group to be used by all captions.
236          * Note that HildonCaption does not create a default size group if we do not specify one.
237          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
238         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
239            
240         /* The description widgets: */  
241         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
242         /* Do use auto-capitalization: */
243         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
244                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
245         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
246                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
247         gtk_widget_show (self->entry_account_title);
248         connect_for_modified (self, self->entry_account_title);
249         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
250         gtk_widget_show (caption);
251         
252         /* Prevent the use of some characters in the account title, 
253          * as required by our UI specification: */
254         GList *list_prevent = NULL;
255         list_prevent = g_list_append (list_prevent, "\\");
256         list_prevent = g_list_append (list_prevent, "/");
257         list_prevent = g_list_append (list_prevent, ":");
258         list_prevent = g_list_append (list_prevent, "*");
259         list_prevent = g_list_append (list_prevent, "?");
260         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
261         list_prevent = g_list_append (list_prevent, "“");
262         list_prevent = g_list_append (list_prevent, "<"); 
263         list_prevent = g_list_append (list_prevent, ">"); 
264         list_prevent = g_list_append (list_prevent, "|");
265         list_prevent = g_list_append (list_prevent, "^");       
266         modest_validating_entry_set_unallowed_characters (
267                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
268         g_list_free (list_prevent);
269         
270         /* Set max length as in the UI spec:
271          * The UI spec seems to want us to show a dialog if we hit the maximum. */
272         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
273         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
274                 on_entry_max, self);
275         
276         /* The retrieve combobox: */
277         self->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
278         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
279                 self->combo_retrieve, NULL, HILDON_CAPTION_MANDATORY);
280         gtk_widget_show (self->combo_retrieve);
281         connect_for_modified (self, self->combo_retrieve);
282         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
283         gtk_widget_show (caption);
284         
285         /* The limit-retrieve combobox: */
286         self->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
287         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
288                 self->combo_limit_retrieve, NULL, HILDON_CAPTION_MANDATORY);
289         gtk_widget_show (self->combo_limit_retrieve);
290         connect_for_modified (self, self->combo_limit_retrieve);
291         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
292         gtk_widget_show (caption);
293
294         /* The leave-messages widgets: */
295         if(!self->checkbox_leave_messages)
296                 self->checkbox_leave_messages = gtk_check_button_new ();
297         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
298                 self->checkbox_leave_messages, NULL, HILDON_CAPTION_MANDATORY);
299         gtk_widget_show (self->checkbox_leave_messages);
300         connect_for_modified (self, self->checkbox_leave_messages);
301         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
302         gtk_widget_show (caption);
303         
304         gtk_widget_show (GTK_WIDGET (box));
305         
306         return GTK_WIDGET (box);
307 }
308
309 static void
310 on_button_signature (GtkButton *button, gpointer user_data)
311 {
312         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
313         
314         /* Create the window, if necessary: */
315         if (!(self->signature_dialog)) {
316                 self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ());
317         
318                 gboolean use_signature = FALSE;
319                 gchar *signature = modest_account_mgr_get_signature(self->account_manager, self->account_name, 
320                         &use_signature);
321                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
322                 modest_signature_editor_dialog_set_settings (
323                         MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog), 
324                         use_signature, signature, account_title);
325                 g_free (signature);
326                 signature = NULL;
327         }
328
329         /* Show the window: */  
330         gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self));
331         gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE);
332     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog));
333     gtk_widget_hide (self->signature_dialog);
334     if (response != GTK_RESPONSE_OK) {
335         /* Destroy the widget now, and its data: */
336         gtk_widget_destroy (self->signature_dialog);
337         self->signature_dialog = NULL;
338     }
339     else {
340         /* Mark modified, so we use the dialog's data later: */
341         self->modified = TRUE;  
342     }
343 }
344
345 static GtkWidget*
346 create_page_user_details (ModestAccountSettingsDialog *self)
347 {
348         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
349         
350         /* Create a size group to be used by all captions.
351          * Note that HildonCaption does not create a default size group if we do not specify one.
352          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
353         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
354          
355         /* The name widgets: */
356         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
357         /* Auto-capitalization is the default, so let's turn it off: */
358         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
359         /* Set max length as in the UI spec:
360          * The UI spec seems to want us to show a dialog if we hit the maximum. */
361         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
362         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
363                 on_entry_max, self);
364         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
365                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
366         gtk_widget_show (self->entry_user_name);
367         connect_for_modified (self, self->entry_user_name);
368         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
369         gtk_widget_show (caption);
370         
371         /* Prevent the use of some characters in the name, 
372          * as required by our UI specification: */
373         GList *list_prevent = NULL;
374         list_prevent = g_list_append (list_prevent, "<");
375         list_prevent = g_list_append (list_prevent, ">");
376         modest_validating_entry_set_unallowed_characters (
377                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
378         g_list_free (list_prevent);
379         
380         /* The username widgets: */     
381         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
382         /* Auto-capitalization is the default, so let's turn it off: */
383         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
384         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
385                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
386         gtk_widget_show (self->entry_user_username);
387         connect_for_modified (self, self->entry_user_username);
388         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
389         gtk_widget_show (caption);
390         
391         /* Prevent the use of some characters in the username, 
392          * as required by our UI specification: */
393         modest_validating_entry_set_unallowed_characters_whitespace (
394                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
395         
396         /* Set max length as in the UI spec:
397          * The UI spec seems to want us to show a dialog if we hit the maximum. */
398         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
399         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
400                 on_entry_max, self);
401         
402         /* The password widgets: */     
403         self->entry_user_password = gtk_entry_new ();
404         /* Auto-capitalization is the default, so let's turn it off: */
405         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
406                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
407         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
408         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
409         caption = create_caption_new_with_asterix (self, sizegroup, 
410                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
411         gtk_widget_show (self->entry_user_password);
412         connect_for_modified (self, self->entry_user_password);
413         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
414         gtk_widget_show (caption);
415         
416         /* The email address widgets: */        
417         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
418         /* Auto-capitalization is the default, so let's turn it off: */
419         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
420         caption = create_caption_new_with_asterix (self, sizegroup, 
421                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
422         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
423         gtk_widget_show (self->entry_user_email);
424         connect_for_modified (self, self->entry_user_email);
425         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
426         gtk_widget_show (caption);
427         
428         /* Set max length as in the UI spec:
429          * The UI spec seems to want us to show a dialog if we hit the maximum. */
430         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
431         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
432                 on_entry_max, self);
433         
434         /* Signature button: */
435         if (!self->button_signature)
436                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
437         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
438                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
439         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
440         gtk_widget_show (self->button_signature);
441         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
442         gtk_widget_show (caption);
443                 
444         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
445                 G_CALLBACK (on_button_signature), self);
446                 
447         gtk_widget_show (GTK_WIDGET (box));
448         
449         return GTK_WIDGET (box);
450 }
451
452 /** Change the caption title for the incoming server, 
453  * as specified in the UI spec:
454  */
455 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
456 {
457         const gchar* type = 
458                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
459                         _("mail_fi_emailtype_pop3") : 
460                         _("mail_fi_emailtype_imap") );
461                         
462                 
463         /* Note that this produces a compiler warning, 
464          * because the compiler does not know that the translated string will have a %s in it.
465          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
466         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
467         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
468         g_free(incomingserver_title);
469 }
470
471 /** Change the caption title for the incoming server, 
472  * as specified in the UI spec:
473  */
474 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
475 {
476         /* Fill the combo with appropriately titled choices for POP or IMAP. */
477         /* The choices are the same, but the titles are different, as in the UI spec. */
478         modest_serversecurity_combo_box_fill (
479                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
480 }
481            
482 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
483 {
484         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
485         
486         /* Create a size group to be used by all captions.
487          * Note that HildonCaption does not create a default size group if we do not specify one.
488          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
489         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
490          
491         /* The incoming server widgets: */
492         if(!self->entry_incomingserver)
493                 self->entry_incomingserver = gtk_entry_new ();
494         /* Auto-capitalization is the default, so let's turn it off: */
495         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
496
497         if (self->caption_incoming)
498           gtk_widget_destroy (self->caption_incoming);
499            
500         /* The caption title will be updated in update_incoming_server_title().
501          * so this default text will never be seen: */
502         /* (Note: Changing the title seems pointless. murrayc) */
503         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
504                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
505         gtk_widget_show (self->entry_incomingserver);
506         connect_for_modified (self, self->entry_incomingserver);
507         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
508         gtk_widget_show (self->caption_incoming);
509         
510         /* The secure connection widgets: */
511         /* This will be filled by update_incoming_server_security_choices(). */
512         if (!self->combo_incoming_security)
513                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
514         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
515                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
516         gtk_widget_show (self->combo_incoming_security);
517         connect_for_modified (self, self->combo_incoming_security);
518         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
519         gtk_widget_show (caption);
520         
521         /* Show a default port number when the security method changes, as per the UI spec: */
522         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
523         
524         
525         /* The port widgets: */
526         /* TODO: There are various rules about this in the UI spec. */
527         if (!self->entry_incoming_port)
528                 self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
529         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
530                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
531         gtk_widget_show (self->entry_incoming_port);
532         connect_for_modified (self, self->entry_incoming_port);
533         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
534         gtk_widget_show (caption);
535         
536         /* The secure authentication widgets: */
537         if(!self->checkbox_incoming_auth)
538                 self->checkbox_incoming_auth = gtk_check_button_new ();
539         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
540                 self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
541         gtk_widget_show (self->checkbox_incoming_auth);
542         connect_for_modified (self, self->checkbox_incoming_auth);
543         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
544         gtk_widget_show (caption);
545         
546         gtk_widget_show (GTK_WIDGET (box));
547         
548         return GTK_WIDGET (box);
549 }
550
551 static void
552 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
553 {
554         GtkWidget *widget = GTK_WIDGET (user_data);
555         
556         /* Enable the widget only if the toggle button is active: */
557         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
558         gtk_widget_set_sensitive (widget, enable);
559 }
560
561 /* Make the sensitivity of a widget depend on a toggle button.
562  */
563 static void
564 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
565 {
566         g_signal_connect (G_OBJECT (button), "toggled",
567                 G_CALLBACK (on_toggle_button_changed), widget);
568         
569         /* Set the starting sensitivity: */
570         on_toggle_button_changed (button, widget);
571 }
572
573 static void
574 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
575 {
576         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
577         
578         /* Create the window if necessary: */
579         if (!(self->specific_window)) {
580                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
581                 modest_connection_specific_smtp_window_fill_with_connections (
582                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
583                         self->account_name);
584         }
585
586         /* Show the window: */  
587         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
588         gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE);
589     gtk_widget_show (self->specific_window);
590 }
591
592 static void
593 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
594 {
595         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
596         
597         ModestSecureAuthentication protocol_security = 
598                 modest_secureauth_combo_box_get_active_secureauth (
599                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
600         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
601         
602         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
603         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
604 }
605
606 static void
607 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
608 {
609         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
610         
611         const gint port_number = 
612                 modest_serversecurity_combo_box_get_active_serversecurity_port (
613                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
614
615         if(port_number != 0) {
616                 hildon_number_editor_set_value (
617                         HILDON_NUMBER_EDITOR (self->entry_outgoing_port), port_number);
618         }               
619 }
620
621 static void
622 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
623 {
624         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
625         
626         const gint port_number = 
627                 modest_serversecurity_combo_box_get_active_serversecurity_port (
628                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
629
630         if(port_number != 0) {
631                 hildon_number_editor_set_value (
632                         HILDON_NUMBER_EDITOR (self->entry_incoming_port), port_number);
633         }               
634 }
635
636
637 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
638 {
639         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
640         
641         /* Create a size group to be used by all captions.
642          * Note that HildonCaption does not create a default size group if we do not specify one.
643          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
644         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
645          
646         /* The outgoing server widgets: */
647         if (!self->entry_outgoingserver)
648                 self->entry_outgoingserver = gtk_entry_new ();
649         /* Auto-capitalization is the default, so let's turn it off: */
650         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
651         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
652                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
653         gtk_widget_show (self->entry_outgoingserver);
654         connect_for_modified (self, self->entry_outgoingserver);
655         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
656         gtk_widget_show (caption);
657         
658         /* The secure authentication widgets: */
659         if (!self->combo_outgoing_auth)
660                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
661         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
662                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
663         gtk_widget_show (self->combo_outgoing_auth);
664         connect_for_modified (self, self->combo_outgoing_auth);
665         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
666         gtk_widget_show (caption);
667         
668         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
669         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
670         
671         /* The username widgets: */     
672         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
673         /* Auto-capitalization is the default, so let's turn it off: */
674         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_username), HILDON_GTK_INPUT_MODE_FULL);
675         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
676                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
677         gtk_widget_show (self->entry_outgoing_username);
678         connect_for_modified (self, self->entry_outgoing_username);
679         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
680         gtk_widget_show (self->caption_outgoing_username);
681         
682         /* Prevent the use of some characters in the username, 
683          * as required by our UI specification: */
684         modest_validating_entry_set_unallowed_characters_whitespace (
685                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
686         
687         /* Set max length as in the UI spec:
688          * The UI spec seems to want us to show a dialog if we hit the maximum. */
689         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
690         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
691                 on_entry_max, self);
692                 
693         /* The password widgets: */     
694         self->entry_outgoing_password = gtk_entry_new ();
695         /* Auto-capitalization is the default, so let's turn it off: */
696         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_password), 
697                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
698         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
699         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
700         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
701                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
702         gtk_widget_show (self->entry_outgoing_password);
703         connect_for_modified (self, self->entry_outgoing_password);
704         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
705         gtk_widget_show (self->caption_outgoing_password);
706         
707         /* The secure connection widgets: */
708         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
709          * and modest_serversecurity_combo_box_set_active_serversecurity().
710          */
711         if (!self->combo_outgoing_security)
712                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
713         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
714                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
715         gtk_widget_show (self->combo_outgoing_security);
716         connect_for_modified (self, self->combo_outgoing_security);
717         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
718         gtk_widget_show (caption);
719         
720         /* Show a default port number when the security method changes, as per the UI spec: */
721         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
722         
723         /* The port widgets: */
724         if (!self->entry_outgoing_port)
725                 self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
726         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
727                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
728         gtk_widget_show (self->entry_outgoing_port);
729         connect_for_modified (self, self->entry_outgoing_port);
730         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
731         gtk_widget_show (caption);
732         
733         GtkWidget *separator = gtk_hseparator_new ();
734         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
735         gtk_widget_show (separator);
736         
737         /* connection-specific checkbox: */
738         if (!self->checkbox_outgoing_smtp_specific) {
739                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
740                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
741                         FALSE);
742         }
743         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
744                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
745         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
746         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
747         gtk_widget_show (caption);
748         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
749         
750         /* Connection-specific SMTP-Severs Edit button: */
751         if (!self->button_outgoing_smtp_servers)
752                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
753         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
754                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
755         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
756         gtk_widget_show (self->button_outgoing_smtp_servers);
757         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
758         gtk_widget_show (caption);
759         
760         /* Only enable the button when the checkbox is checked: */
761         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
762                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
763                 
764         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
765                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
766         
767         
768         gtk_widget_show (GTK_WIDGET (box));
769         
770         return GTK_WIDGET (box);
771 }
772
773 static gboolean
774 check_data (ModestAccountSettingsDialog *self)
775 {
776         /* Check that the title is not already in use: */
777         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
778         if ((!account_title) || (strlen(account_title) == 0))
779                 return FALSE; /* Should be prevented already anyway. */
780                 
781         if (strcmp(account_title, self->original_account_title) != 0) {
782                 /* Check the changed title: */
783                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
784                         account_title);
785         
786                 if (name_in_use) {
787                         /* Warn the user via a dialog: */
788                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
789                 
790                         return FALSE;
791                 }
792         }
793
794         /* Check that the email address is valud: */
795         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
796         if ((!email_address) || (strlen(email_address) == 0))
797                 return FALSE;
798                         
799         if (!modest_text_utils_validate_email_address (email_address)) {
800                 /* Warn the user via a dialog: */
801                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
802                                          
803         /* Return focus to the email address entry: */
804         gtk_widget_grab_focus (self->entry_user_email);
805         
806                 return FALSE;
807         }
808         
809         /* TODO: The UI Spec wants us to check that the servernames are valid, 
810          * but does not specify how.
811          */
812          
813         return TRUE;
814 }
815 /*
816  */
817 static void 
818 on_response (GtkDialog *wizard_dialog,
819         gint response_id,
820         gpointer user_data)
821 {
822         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
823         enable_buttons (self);
824         
825         gboolean prevent_response = FALSE;
826         
827         /* Warn about unsaved changes: */
828         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
829                 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
830                         _("imum_nc_wizard_confirm_lose_changes")));
831                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
832                  
833                  const gint dialog_response = gtk_dialog_run (dialog);
834                  gtk_widget_destroy (GTK_WIDGET (dialog));
835                  
836                 if (dialog_response != GTK_RESPONSE_OK)
837                         prevent_response = TRUE;
838         }
839         /* Check for invalid input: */
840         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
841                 prevent_response = TRUE;
842         }
843                 
844         if (prevent_response) {
845                 /* This is a nasty hack. murrayc. */
846                 /* Don't let the dialog close */
847         g_signal_stop_emission_by_name (wizard_dialog, "response");
848                 return; 
849         }
850                 
851         if (response_id == GTK_RESPONSE_OK) {
852                 /* Try to save the changes: */  
853                 const gboolean saved = save_configuration (self);
854                 if (saved) {
855                         /* Do not show the account-saved dialog if we are just saving this 
856                          * temporarily, because from the user's point of view it will not 
857                          * really be saved (saved + enabled) until later.
858                          */
859                         const gboolean enabled = 
860                                 modest_account_mgr_get_enabled (self->account_manager, self->account_name);
861                         if (enabled)
862                                 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
863                 }
864                 else
865                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
866         }
867 }
868
869 static void
870 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
871 {
872         /* Create the notebook to be used by the GtkDialog base class:
873          * Each page of the notebook will be a page of the wizard: */
874         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
875
876     
877     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
878         
879         /* Get the account manager object, 
880          * so we can check for existing accounts,
881          * and create new accounts: */
882         self->account_manager = modest_runtime_get_account_mgr ();
883         g_assert (self->account_manager);
884         g_object_ref (self->account_manager);
885         
886     /* Create the common pages, 
887      */
888         self->page_account_details = create_page_account_details (self);
889         self->page_user_details = create_page_user_details (self);
890         self->page_incoming = create_page_incoming (self);
891         self->page_outgoing = create_page_outgoing (self);
892         
893         /* Add the notebook pages: */
894         gtk_notebook_append_page (notebook, self->page_account_details, 
895                 gtk_label_new (_("mcen_ti_account_settings_account")));
896         gtk_notebook_append_page (notebook, self->page_user_details, 
897                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
898         gtk_notebook_append_page (notebook, self->page_incoming,
899                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
900         gtk_notebook_append_page (notebook, self->page_outgoing,
901                 gtk_label_new (_("mcen_ti_advsetup_sending")));
902                 
903         GtkDialog *dialog = GTK_DIALOG (self);
904         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
905         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
906         gtk_widget_show (GTK_WIDGET (notebook));
907         
908     /* Add the buttons: */
909     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
910     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
911     
912     /* Connect to the dialog's response signal: */
913     /* We use connect-before 
914      * so we can stop the signal emission, 
915      * to stop the default signal handler from closing the dialog.
916      */
917     g_signal_connect (G_OBJECT (self), "response",
918             G_CALLBACK (on_response), self); 
919             
920     self->modified = FALSE;
921     
922     /* When this window is shown, hibernation should not be possible, 
923          * because there is no sensible way to save the state: */
924     modest_window_mgr_prevent_hibernation_while_window_is_shown (
925         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
926 }
927
928 ModestAccountSettingsDialog*
929 modest_account_settings_dialog_new (void)
930 {
931         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
932 }
933
934 /** Update the UI with the stored account details, so they can be edited.
935  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
936  */
937 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
938 {
939         if (!account_name)
940                 return;
941                 
942         /* Save the account name so we can refer to it later: */
943         if (dialog->account_name)
944                 g_free (dialog->account_name);
945         dialog->account_name = g_strdup (account_name);
946         
947                 
948         /* Get the account data for this account name: */
949         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
950                 account_name);
951         if (!account_data) {
952                 g_printerr ("modest: failed to get account data for %s\n", account_name);
953                 return;
954         }
955         
956         /* Save the account title so we can refer to it if the user changes it: */
957         if (dialog->original_account_title)
958                 g_free (dialog->original_account_title);
959         dialog->original_account_title = g_strdup (account_data->display_name);
960         
961
962         if (!(account_data->store_account)) {
963                 g_printerr ("modest: account has no stores: %s\n", account_name);
964                 return;
965         }
966                 
967         /* Show the account data in the widgets: */
968         
969         /* Note that we never show the non-display name in the UI.
970          * (Though the display name defaults to the non-display name at the start.) */
971         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
972                 account_data->display_name ? account_data->display_name : "");
973                 
974         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
975                 account_data->fullname ? account_data->fullname : "");
976         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
977                 account_data->email ? account_data->email : "");
978                 
979         ModestServerAccountData *incoming_account = account_data->store_account;
980                 
981         if (incoming_account)
982                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
983         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
984                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
985         if (!retrieve) {
986                 /* Default to something, though no default is specified in the UI spec: */
987                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
988         }
989         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
990         g_free (retrieve);
991         
992         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
993                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
994         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
995         
996         
997         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
998                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
999         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
1000         
1001         /* Only show the leave-on-server checkbox for POP, 
1002          * as per the UI spec: */
1003         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
1004                 gtk_widget_hide (dialog->checkbox_leave_messages);
1005         } else {
1006                 gtk_widget_show (dialog->checkbox_leave_messages);
1007         }
1008                 
1009         if (incoming_account) {
1010                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1011                         incoming_account->username ? incoming_account->username : "");
1012                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1013                         incoming_account->password ? incoming_account->password : "");
1014                         
1015                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1016                         incoming_account->hostname ? incoming_account->hostname : "");
1017                         
1018                 /* The UI spec says:
1019                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1020          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1021                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1022          */
1023                 const ModestSecureAuthentication secure_auth = modest_server_account_get_secure_auth(
1024                         dialog->account_manager, incoming_account->account_name);
1025                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1026                         secure_auth != MODEST_PROTOCOL_AUTH_PASSWORD);
1027                 /* Note that MODEST_PROTOCOL_AUTH_PLAIN should probably never be used. */
1028                         
1029                 update_incoming_server_title (dialog, incoming_account->proto);
1030                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1031                 
1032                 const ModestSecureConnection security = modest_server_account_get_security (
1033                         dialog->account_manager, incoming_account->account_name);
1034                 modest_serversecurity_combo_box_set_active_serversecurity (
1035                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1036                 
1037                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1038                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1039                 hildon_number_editor_set_value (
1040                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1041         
1042         }
1043         
1044         ModestServerAccountData *outgoing_account = account_data->transport_account;
1045         if (outgoing_account) {
1046                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1047                         outgoing_account->hostname ? outgoing_account->hostname : "");
1048                 
1049                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1050                         outgoing_account->username ? outgoing_account->username : "");
1051                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1052                         outgoing_account->password ? outgoing_account->password : "");
1053                 
1054                 /* Get the secure-auth setting: */
1055                 const ModestSecureAuthentication secure_auth = modest_server_account_get_secure_auth(
1056                         dialog->account_manager, outgoing_account->account_name);
1057                 modest_secureauth_combo_box_set_active_secureauth (
1058                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1059                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1060                 
1061                 modest_serversecurity_combo_box_fill (
1062                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1063                 
1064                 /* Get the security setting: */
1065                 const ModestSecureConnection security = modest_server_account_get_security (
1066                         dialog->account_manager, outgoing_account->account_name);
1067                 modest_serversecurity_combo_box_set_active_serversecurity (
1068                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1069                 
1070                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1071                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1072                 hildon_number_editor_set_value (
1073                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1074         }
1075         
1076         /* account_data->is_enabled,  */
1077         /*account_data->is_default,  */
1078
1079         /* account_data->store_account->proto */
1080
1081         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1082         
1083         /* Unset the modified flag so we can detect changes later: */
1084         dialog->modified = FALSE;
1085 }
1086
1087 static gboolean
1088 save_configuration (ModestAccountSettingsDialog *dialog)
1089 {
1090         g_assert (dialog->account_name);
1091         
1092         const gchar* account_name = dialog->account_name;
1093                 
1094         /* Set the account data from the widgets: */
1095         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1096         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1097                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1098         if (!test)
1099                 return FALSE;
1100                 
1101         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1102         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1103                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1104         if (!test)
1105                 return FALSE;
1106                 
1107         /* Signature: */
1108         if (dialog->signature_dialog) {
1109                 gboolean use_signature = FALSE;
1110         gchar *signature = modest_signature_editor_dialog_get_settings (
1111                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1112                 &use_signature);
1113         
1114         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1115                 signature, use_signature);
1116         g_free (signature);
1117     }
1118         
1119         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1120                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1121         modest_account_mgr_set_string (dialog->account_manager, account_name,
1122                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1123         g_free (retrieve);
1124         
1125         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1126                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1127         modest_account_mgr_set_int (dialog->account_manager, account_name,
1128                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1129         
1130         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1131         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1132                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1133         if (!test)
1134                 return FALSE;
1135                         
1136         /* Incoming: */
1137         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1138                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1139         g_assert (incoming_account_name);
1140         
1141         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1142         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1143                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1144         if (!test)
1145                 return FALSE;
1146                                 
1147         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1148         modest_server_account_set_username (dialog->account_manager, incoming_account_name, username);
1149         
1150         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1151         modest_server_account_set_password (dialog->account_manager, incoming_account_name, password);
1152                         
1153         /* The UI spec says:
1154          * If secure authentication is unchecked, allow sending username and password also as plain text.
1155          * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1156          * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1157      */
1158         const ModestSecureAuthentication protocol_authentication_incoming = gtk_toggle_button_get_active 
1159                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
1160                         ? MODEST_PROTOCOL_AUTH_CRAMMD5
1161                         : MODEST_PROTOCOL_AUTH_PASSWORD;
1162         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1163                         
1164         const ModestSecureConnection protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1165                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1166         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1167         
1168         /* port: */
1169         gint port_num = hildon_number_editor_get_value (
1170                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1171         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1172                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1173                 
1174         g_free (incoming_account_name);
1175         
1176         /* Outgoing: */
1177         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1178                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1179         g_assert (outgoing_account_name);
1180         
1181         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1182         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1183                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1184         if (!test)
1185                 return FALSE;
1186                 
1187         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1188         modest_server_account_set_username (dialog->account_manager, outgoing_account_name,
1189                 username);
1190                 
1191         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1192         modest_server_account_set_password (dialog->account_manager, outgoing_account_name,
1193                 password);
1194         
1195         const ModestSecureConnection protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1196                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1197         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1198         
1199         const ModestSecureAuthentication protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1200                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1201         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1202                 
1203         /* port: */
1204         port_num = hildon_number_editor_get_value (
1205                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1206         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1207                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1208                         
1209         g_free (outgoing_account_name);
1210         
1211         
1212         /* Set the changed account title last, to simplify the previous code: */
1213         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1214         if ((!account_title) || (strlen(account_title) == 0))
1215                 return FALSE; /* Should be prevented already anyway. */
1216                 
1217         if (strcmp(account_title, account_name) != 0) {
1218                 /* Change the title: */
1219                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1220                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1221                 if (!test)
1222                         return FALSE;
1223         }
1224         
1225         /* Save connection-specific SMTP server accounts: */
1226         if (dialog->specific_window)
1227                 return modest_connection_specific_smtp_window_save_server_accounts (
1228                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1229         else
1230                 return TRUE;
1231 }
1232
1233 static gboolean entry_is_empty (GtkWidget *entry)
1234 {
1235         if (!entry)
1236                 return FALSE;
1237                 
1238         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1239         if ((!text) || (strlen(text) == 0))
1240                 return TRUE;
1241         else
1242                 return FALSE;
1243 }
1244
1245 static void
1246 enable_buttons (ModestAccountSettingsDialog *self)
1247 {
1248         gboolean enable_ok = TRUE;
1249         
1250         /* The account details title is mandatory: */
1251         if (entry_is_empty(self->entry_account_title))
1252                         enable_ok = FALSE;
1253
1254         /* The user details username is mandatory: */
1255         if (entry_is_empty(self->entry_user_username))
1256                 enable_ok = FALSE;
1257                 
1258         /* The user details email address is mandatory: */
1259         if (enable_ok && entry_is_empty (self->entry_user_email))
1260                 enable_ok = FALSE;
1261
1262         /* The custom incoming server is mandatory: */
1263         if (entry_is_empty(self->entry_incomingserver))
1264                 enable_ok = FALSE;
1265                         
1266         /* Enable the buttons, 
1267          * identifying them via their associated response codes:
1268          */
1269         GtkDialog *dialog_base = GTK_DIALOG (self);
1270     gtk_dialog_set_response_sensitive (dialog_base,
1271                                        GTK_RESPONSE_OK,
1272                                        enable_ok);
1273 }
1274
1275 static void
1276 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1277 {
1278         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1279         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1280
1281
1282         object_class->get_property = modest_account_settings_dialog_get_property;
1283         object_class->set_property = modest_account_settings_dialog_set_property;
1284         object_class->dispose = modest_account_settings_dialog_dispose;
1285         object_class->finalize = modest_account_settings_dialog_finalize;
1286 }
1287  
1288 static void
1289 show_error (GtkWindow *parent_window, const gchar* text)
1290 {
1291         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1292         /*
1293         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1294                 (GtkDialogFlags)0,
1295                  GTK_MESSAGE_ERROR,
1296                  GTK_BUTTONS_OK,
1297                  text ));
1298         */       
1299         
1300         gtk_dialog_run (dialog);
1301         gtk_widget_destroy (GTK_WIDGET (dialog));
1302 }
1303
1304 static void
1305 show_ok (GtkWindow *parent_window, const gchar* text)
1306 {
1307         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1308         /*
1309         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1310                 (GtkDialogFlags)0,
1311                  GTK_MESSAGE_INFO,
1312                  GTK_BUTTONS_OK,
1313                  text ));
1314         */
1315                  
1316         gtk_dialog_run (dialog);
1317         gtk_widget_destroy (GTK_WIDGET (dialog));
1318 }
1319
1320
1321