2007-04-24 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 <hildon-widgets/hildon-caption.h>
19 #include <hildon-widgets/hildon-number-editor.h>
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 <maemo/modest-maemo-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_HALF);
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         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
243                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
244         gtk_widget_show (self->entry_account_title);
245         connect_for_modified (self, self->entry_account_title);
246         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
247         gtk_widget_show (caption);
248         
249         /* Prevent the use of some characters in the account title, 
250          * as required by our UI specification: */
251         GList *list_prevent = NULL;
252         list_prevent = g_list_append (list_prevent, "\\");
253         list_prevent = g_list_append (list_prevent, "/");
254         list_prevent = g_list_append (list_prevent, ":");
255         list_prevent = g_list_append (list_prevent, "*");
256         list_prevent = g_list_append (list_prevent, "?");
257         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
258         list_prevent = g_list_append (list_prevent, "“");
259         list_prevent = g_list_append (list_prevent, "<"); 
260         list_prevent = g_list_append (list_prevent, ">"); 
261         list_prevent = g_list_append (list_prevent, "|");
262         list_prevent = g_list_append (list_prevent, "^");       
263         modest_validating_entry_set_unallowed_characters (
264                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
265         g_list_free (list_prevent);
266         
267         /* Set max length as in the UI spec:
268          * The UI spec seems to want us to show a dialog if we hit the maximum. */
269         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
270         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
271                 on_entry_max, self);
272         
273         /* The retrieve combobox: */
274         self->combo_retrieve = GTK_WIDGET (modest_retrieve_combo_box_new ());
275         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_retrievetype"), 
276                 self->combo_retrieve, NULL, HILDON_CAPTION_MANDATORY);
277         gtk_widget_show (self->combo_retrieve);
278         connect_for_modified (self, self->combo_retrieve);
279         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
280         gtk_widget_show (caption);
281         
282         /* The limit-retrieve combobox: */
283         self->combo_limit_retrieve = GTK_WIDGET (modest_limit_retrieve_combo_box_new ());
284         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_limit_retrieve"), 
285                 self->combo_limit_retrieve, NULL, HILDON_CAPTION_MANDATORY);
286         gtk_widget_show (self->combo_limit_retrieve);
287         connect_for_modified (self, self->combo_limit_retrieve);
288         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
289         gtk_widget_show (caption);
290
291         /* The leave-messages widgets: */
292         if(!self->checkbox_leave_messages)
293                 self->checkbox_leave_messages = gtk_check_button_new ();
294         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_advsetup_leave_on_server"), 
295                 self->checkbox_leave_messages, NULL, HILDON_CAPTION_MANDATORY);
296         gtk_widget_show (self->checkbox_leave_messages);
297         connect_for_modified (self, self->checkbox_leave_messages);
298         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
299         gtk_widget_show (caption);
300         
301         gtk_widget_show (GTK_WIDGET (box));
302         
303         return GTK_WIDGET (box);
304 }
305
306 static void
307 on_button_signature (GtkButton *button, gpointer user_data)
308 {
309         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
310         
311         /* Create the window, if necessary: */
312         if (!(self->signature_dialog)) {
313                 self->signature_dialog = GTK_WIDGET (modest_signature_editor_dialog_new ());
314         
315                 gboolean use_signature = FALSE;
316                 gchar *signature = modest_account_mgr_get_signature(self->account_manager, self->account_name, 
317                         &use_signature);
318                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
319                 modest_signature_editor_dialog_set_settings (
320                         MODEST_SIGNATURE_EDITOR_DIALOG (self->signature_dialog), 
321                         use_signature, signature, account_title);
322                 g_free (signature);
323                 signature = NULL;
324         }
325
326         /* Show the window: */  
327         gtk_window_set_transient_for (GTK_WINDOW (self->signature_dialog), GTK_WINDOW (self));
328         gtk_window_set_modal (GTK_WINDOW (self->signature_dialog), TRUE);
329     const gint response = gtk_dialog_run (GTK_DIALOG (self->signature_dialog));
330     gtk_widget_hide (self->signature_dialog);
331     if (response != GTK_RESPONSE_OK) {
332         /* Destroy the widget now, and its data: */
333         gtk_widget_destroy (self->signature_dialog);
334         self->signature_dialog = NULL;
335     }
336     else {
337         /* Mark modified, so we use the dialog's data later: */
338         self->modified = TRUE;  
339     }
340 }
341
342 static GtkWidget*
343 create_page_user_details (ModestAccountSettingsDialog *self)
344 {
345         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
346         
347         /* Create a size group to be used by all captions.
348          * Note that HildonCaption does not create a default size group if we do not specify one.
349          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
350         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
351          
352         /* The name widgets: */
353         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
354         /* Auto-capitalization is the default, so let's turn it off: */
355         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
356         /* Set max length as in the UI spec:
357          * The UI spec seems to want us to show a dialog if we hit the maximum. */
358         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
359         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
360                 on_entry_max, self);
361         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
362                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
363         gtk_widget_show (self->entry_user_name);
364         connect_for_modified (self, self->entry_user_name);
365         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
366         gtk_widget_show (caption);
367         
368         /* Prevent the use of some characters in the name, 
369          * as required by our UI specification: */
370         GList *list_prevent = NULL;
371         list_prevent = g_list_append (list_prevent, "<");
372         list_prevent = g_list_append (list_prevent, ">");
373         modest_validating_entry_set_unallowed_characters (
374                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
375         g_list_free (list_prevent);
376         
377         /* The username widgets: */     
378         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
379         /* Auto-capitalization is the default, so let's turn it off: */
380         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
381         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
382                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
383         gtk_widget_show (self->entry_user_username);
384         connect_for_modified (self, self->entry_user_username);
385         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
386         gtk_widget_show (caption);
387         
388         /* Prevent the use of some characters in the username, 
389          * as required by our UI specification: */
390         modest_validating_entry_set_unallowed_characters_whitespace (
391                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
392         
393         /* Set max length as in the UI spec:
394          * The UI spec seems to want us to show a dialog if we hit the maximum. */
395         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
396         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
397                 on_entry_max, self);
398         
399         /* The password widgets: */     
400         self->entry_user_password = gtk_entry_new ();
401         /* Auto-capitalization is the default, so let's turn it off: */
402         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), HILDON_GTK_INPUT_MODE_FULL);
403         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
404         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
405         caption = create_caption_new_with_asterix (self, sizegroup, 
406                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
407         gtk_widget_show (self->entry_user_password);
408         connect_for_modified (self, self->entry_user_password);
409         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
410         gtk_widget_show (caption);
411         
412         /* The email address widgets: */        
413         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
414         /* Auto-capitalization is the default, so let's turn it off: */
415         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
416         caption = create_caption_new_with_asterix (self, sizegroup, 
417                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
418         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
419         gtk_widget_show (self->entry_user_email);
420         connect_for_modified (self, self->entry_user_email);
421         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
422         gtk_widget_show (caption);
423         
424         /* Set max length as in the UI spec:
425          * The UI spec seems to want us to show a dialog if we hit the maximum. */
426         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
427         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
428                 on_entry_max, self);
429         
430         /* Signature button: */
431         if (!self->button_signature)
432                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_edit"));
433         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
434                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
435         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
436         gtk_widget_show (self->button_signature);
437         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
438         gtk_widget_show (caption);
439                 
440         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
441                 G_CALLBACK (on_button_signature), self);
442                 
443         gtk_widget_show (GTK_WIDGET (box));
444         
445         return GTK_WIDGET (box);
446 }
447
448 /** Change the caption title for the incoming server, 
449  * as specified in the UI spec:
450  */
451 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
452 {
453         const gchar* type = 
454                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
455                         _("mail_fi_emailtype_pop3") : 
456                         _("mail_fi_emailtype_imap") );
457                         
458                 
459         /* Note that this produces a compiler warning, 
460          * because the compiler does not know that the translated string will have a %s in it.
461          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
462         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
463         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
464         g_free(incomingserver_title);
465 }
466
467 /** Change the caption title for the incoming server, 
468  * as specified in the UI spec:
469  */
470 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
471 {
472         /* Fill the combo with appropriately titled choices for POP or IMAP. */
473         /* The choices are the same, but the titles are different, as in the UI spec. */
474         modest_serversecurity_combo_box_fill (
475                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
476 }
477            
478 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
479 {
480         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
481         
482         /* Create a size group to be used by all captions.
483          * Note that HildonCaption does not create a default size group if we do not specify one.
484          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
485         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
486          
487         /* The incoming server widgets: */
488         if(!self->entry_incomingserver)
489                 self->entry_incomingserver = gtk_entry_new ();
490         /* Auto-capitalization is the default, so let's turn it off: */
491         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
492
493         if (self->caption_incoming)
494           gtk_widget_destroy (self->caption_incoming);
495            
496         /* The caption title will be updated in update_incoming_server_title().
497          * so this default text will never be seen: */
498         /* (Note: Changing the title seems pointless. murrayc) */
499         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
500                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
501         gtk_widget_show (self->entry_incomingserver);
502         connect_for_modified (self, self->entry_incomingserver);
503         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
504         gtk_widget_show (self->caption_incoming);
505         
506         /* The secure connection widgets: */
507         /* This will be filled by update_incoming_server_security_choices(). */
508         if (!self->combo_incoming_security)
509                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
510         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
511                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
512         gtk_widget_show (self->combo_incoming_security);
513         connect_for_modified (self, self->combo_incoming_security);
514         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
515         gtk_widget_show (caption);
516         
517         /* Show a default port number when the security method changes, as per the UI spec: */
518         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed", (GCallback)on_combo_incoming_security_changed, self);
519         
520         
521         /* The port widgets: */
522         /* TODO: There are various rules about this in the UI spec. */
523         if (!self->entry_incoming_port)
524                 self->entry_incoming_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
525         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
526                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
527         gtk_widget_show (self->entry_incoming_port);
528         connect_for_modified (self, self->entry_incoming_port);
529         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
530         gtk_widget_show (caption);
531         
532         /* The secure authentication widgets: */
533         if(!self->checkbox_incoming_auth)
534                 self->checkbox_incoming_auth = gtk_check_button_new ();
535         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
536                 self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
537         gtk_widget_show (self->checkbox_incoming_auth);
538         connect_for_modified (self, self->checkbox_incoming_auth);
539         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
540         gtk_widget_show (caption);
541         
542         gtk_widget_show (GTK_WIDGET (box));
543         
544         return GTK_WIDGET (box);
545 }
546
547 static void
548 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
549 {
550         GtkWidget *widget = GTK_WIDGET (user_data);
551         
552         /* Enable the widget only if the toggle button is active: */
553         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
554         gtk_widget_set_sensitive (widget, enable);
555 }
556
557 /* Make the sensitivity of a widget depend on a toggle button.
558  */
559 static void
560 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
561 {
562         g_signal_connect (G_OBJECT (button), "toggled",
563                 G_CALLBACK (on_toggle_button_changed), widget);
564         
565         /* Set the starting sensitivity: */
566         on_toggle_button_changed (button, widget);
567 }
568
569 static void
570 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
571 {
572         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
573         
574         /* Create the window if necessary: */
575         if (!(self->specific_window)) {
576                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
577                 modest_connection_specific_smtp_window_fill_with_connections (
578                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
579                         self->account_name);
580         }
581
582         /* Show the window: */  
583         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
584         gtk_window_set_modal (GTK_WINDOW (self->specific_window), TRUE);
585     gtk_widget_show (self->specific_window);
586 }
587
588 static void
589 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
590 {
591         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
592         
593         ModestProtocol protocol_security = 
594                 modest_secureauth_combo_box_get_active_secureauth (
595                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
596         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
597         
598         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
599         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
600 }
601
602 static void
603 on_combo_outgoing_security_changed (GtkComboBox *widget, gpointer user_data)
604 {
605         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
606         
607         const gint port_number = 
608                 modest_serversecurity_combo_box_get_active_serversecurity_port (
609                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
610
611         if(port_number != 0) {
612                 hildon_number_editor_set_value (
613                         HILDON_NUMBER_EDITOR (self->entry_outgoing_port), port_number);
614         }               
615 }
616
617 static void
618 on_combo_incoming_security_changed (GtkComboBox *widget, gpointer user_data)
619 {
620         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
621         
622         const gint port_number = 
623                 modest_serversecurity_combo_box_get_active_serversecurity_port (
624                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
625
626         if(port_number != 0) {
627                 hildon_number_editor_set_value (
628                         HILDON_NUMBER_EDITOR (self->entry_incoming_port), port_number);
629         }               
630 }
631
632
633 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
634 {
635         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
636         
637         /* Create a size group to be used by all captions.
638          * Note that HildonCaption does not create a default size group if we do not specify one.
639          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
640         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
641          
642         /* The outgoing server widgets: */
643         if (!self->entry_outgoingserver)
644                 self->entry_outgoingserver = gtk_entry_new ();
645         /* Auto-capitalization is the default, so let's turn it off: */
646         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
647         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
648                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
649         gtk_widget_show (self->entry_outgoingserver);
650         connect_for_modified (self, self->entry_outgoingserver);
651         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
652         gtk_widget_show (caption);
653         
654         /* The secure authentication widgets: */
655         if (!self->combo_outgoing_auth)
656                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
657         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
658                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
659         gtk_widget_show (self->combo_outgoing_auth);
660         connect_for_modified (self, self->combo_outgoing_auth);
661         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
662         gtk_widget_show (caption);
663         
664         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
665         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
666         
667         /* The username widgets: */     
668         self->entry_outgoing_username = GTK_WIDGET (modest_validating_entry_new ());
669         /* Auto-capitalization is the default, so let's turn it off: */
670         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_username), HILDON_GTK_INPUT_MODE_FULL);
671         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
672                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
673         gtk_widget_show (self->entry_outgoing_username);
674         connect_for_modified (self, self->entry_outgoing_username);
675         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, MODEST_MARGIN_HALF);
676         gtk_widget_show (self->caption_outgoing_username);
677         
678         /* Prevent the use of some characters in the username, 
679          * as required by our UI specification: */
680         modest_validating_entry_set_unallowed_characters_whitespace (
681                 MODEST_VALIDATING_ENTRY (self->entry_outgoing_username));
682         
683         /* Set max length as in the UI spec:
684          * The UI spec seems to want us to show a dialog if we hit the maximum. */
685         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
686         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_outgoing_username), 
687                 on_entry_max, self);
688                 
689         /* The password widgets: */     
690         self->entry_outgoing_password = gtk_entry_new ();
691         /* Auto-capitalization is the default, so let's turn it off: */
692         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoing_password), HILDON_GTK_INPUT_MODE_FULL);
693         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
694         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
695         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
696                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
697         gtk_widget_show (self->entry_outgoing_password);
698         connect_for_modified (self, self->entry_outgoing_password);
699         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, MODEST_MARGIN_HALF);
700         gtk_widget_show (self->caption_outgoing_password);
701         
702         /* The secure connection widgets: */
703         /* This will be filled and set with modest_serversecurity_combo_box_fill() 
704          * and modest_serversecurity_combo_box_set_active_serversecurity().
705          */
706         if (!self->combo_outgoing_security)
707                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
708         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
709                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
710         gtk_widget_show (self->combo_outgoing_security);
711         connect_for_modified (self, self->combo_outgoing_security);
712         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
713         gtk_widget_show (caption);
714         
715         /* Show a default port number when the security method changes, as per the UI spec: */
716         g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed", (GCallback)on_combo_outgoing_security_changed, self);
717         
718         /* The port widgets: */
719         if (!self->entry_outgoing_port)
720                 self->entry_outgoing_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
721         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
722                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
723         gtk_widget_show (self->entry_outgoing_port);
724         connect_for_modified (self, self->entry_outgoing_port);
725         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
726         gtk_widget_show (caption);
727         
728         GtkWidget *separator = gtk_hseparator_new ();
729         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
730         gtk_widget_show (separator);
731         
732         /* connection-specific checkbox: */
733         if (!self->checkbox_outgoing_smtp_specific) {
734                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
735                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
736                         FALSE);
737         }
738         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
739                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
740         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
741         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
742         gtk_widget_show (caption);
743         connect_for_modified (self, self->checkbox_outgoing_smtp_specific);
744         
745         /* Connection-specific SMTP-Severs Edit button: */
746         if (!self->button_outgoing_smtp_servers)
747                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
748         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
749                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
750         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
751         gtk_widget_show (self->button_outgoing_smtp_servers);
752         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
753         gtk_widget_show (caption);
754         
755         /* Only enable the button when the checkbox is checked: */
756         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
757                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
758                 
759         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
760                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
761         
762         
763         gtk_widget_show (GTK_WIDGET (box));
764         
765         return GTK_WIDGET (box);
766 }
767
768 static gboolean
769 check_data (ModestAccountSettingsDialog *self)
770 {
771         /* Check that the title is not already in use: */
772         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
773         if ((!account_title) || (strlen(account_title) == 0))
774                 return FALSE; /* Should be prevented already anyway. */
775                 
776         if (strcmp(account_title, self->original_account_title) != 0) {
777                 /* Check the changed title: */
778                 const gboolean name_in_use  = modest_account_mgr_account_with_display_name_exists (self->account_manager,
779                         account_title);
780         
781                 if (name_in_use) {
782                         /* Warn the user via a dialog: */
783                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing"));
784                 
785                         return FALSE;
786                 }
787         }
788
789         /* Check that the email address is valud: */
790         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
791         if ((!email_address) || (strlen(email_address) == 0))
792                 return FALSE;
793                         
794         if (!modest_text_utils_validate_email_address (email_address)) {
795                 /* Warn the user via a dialog: */
796                 show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
797                                          
798         /* Return focus to the email address entry: */
799         gtk_widget_grab_focus (self->entry_user_email);
800         
801                 return FALSE;
802         }
803         
804         /* TODO: The UI Spec wants us to check that the servernames are valid, 
805          * but does not specify how.
806          */
807          
808         return TRUE;
809 }
810 /*
811  */
812 static void 
813 on_response (GtkDialog *wizard_dialog,
814         gint response_id,
815         gpointer user_data)
816 {
817         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
818         enable_buttons (self);
819         
820         gboolean prevent_response = FALSE;
821         
822         /* Warn about unsaved changes: */
823         if (response_id == GTK_RESPONSE_CANCEL && self->modified) {
824                 GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (self),
825                 (GtkDialogFlags)0,
826                  GTK_MESSAGE_INFO,
827                  GTK_BUTTONS_OK_CANCEL, /* TODO: These button names are ambiguous, and not specified in the UI specification. */
828                  _("imum_nc_wizard_confirm_lose_changes") ));
829                  
830                  const gint dialog_response = gtk_dialog_run (dialog);
831                  gtk_widget_destroy (GTK_WIDGET (dialog));
832                  
833                 if (dialog_response != GTK_RESPONSE_OK)
834                         prevent_response = TRUE;
835         }
836         /* Check for invalid input: */
837         else if (!check_data (self)) {
838                 prevent_response = TRUE;
839         }
840                 
841         if (prevent_response) {
842                 /* This is a nasty hack. murrayc. */
843                 /* Don't let the dialog close */
844         g_signal_stop_emission_by_name (wizard_dialog, "response");
845                 return; 
846         }
847                 
848                 
849         if (response_id == GTK_RESPONSE_OK) {
850                 /* Try to save the changes: */  
851                 const gboolean saved = save_configuration (self);
852                 if (saved)
853                         show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
854                 else
855                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
856         }
857 }
858
859 static void
860 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
861 {
862         /* Create the notebook to be used by the GtkDialog base class:
863          * Each page of the notebook will be a page of the wizard: */
864         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
865
866     
867     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
868         
869         /* Get the account manager object, 
870          * so we can check for existing accounts,
871          * and create new accounts: */
872         self->account_manager = modest_runtime_get_account_mgr ();
873         g_assert (self->account_manager);
874         g_object_ref (self->account_manager);
875         
876     /* Create the common pages, 
877      */
878         self->page_account_details = create_page_account_details (self);
879         self->page_user_details = create_page_user_details (self);
880         self->page_incoming = create_page_incoming (self);
881         self->page_outgoing = create_page_outgoing (self);
882         
883         /* Add the notebook pages: */
884         gtk_notebook_append_page (notebook, self->page_account_details, 
885                 gtk_label_new (_("mcen_ti_account_settings_account")));
886         gtk_notebook_append_page (notebook, self->page_user_details, 
887                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
888         gtk_notebook_append_page (notebook, self->page_incoming,
889                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
890         gtk_notebook_append_page (notebook, self->page_outgoing,
891                 gtk_label_new (_("mcen_ti_advsetup_sending")));
892                 
893         GtkDialog *dialog = GTK_DIALOG (self);
894         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
895         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
896         gtk_widget_show (GTK_WIDGET (notebook));
897         
898     /* Add the buttons: */
899     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
900     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
901     
902     /* Connect to the dialog's response signal: */
903     /* We use connect-before 
904      * so we can stop the signal emission, 
905      * to stop the default signal handler from closing the dialog.
906      */
907     g_signal_connect (G_OBJECT (self), "response",
908             G_CALLBACK (on_response), self); 
909             
910     self->modified = FALSE;      
911 }
912
913 ModestAccountSettingsDialog*
914 modest_account_settings_dialog_new (void)
915 {
916         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
917 }
918
919 /** Update the UI with the stored account details, so they can be edited.
920  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
921  */
922 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
923 {
924         if (!account_name)
925                 return;
926                 
927         /* Save the account name so we can refer to it later: */
928         if (dialog->account_name)
929                 g_free (dialog->account_name);
930         dialog->account_name = g_strdup (account_name);
931         
932                 
933         /* Get the account data for this account name: */
934         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
935                 account_name);
936         if (!account_data) {
937                 g_printerr ("modest: failed to get account data for %s\n", account_name);
938                 return;
939         }
940         
941         /* Save the account title so we can refer to it if the user changes it: */
942         if (dialog->original_account_title)
943                 g_free (dialog->original_account_title);
944         dialog->original_account_title = g_strdup (account_data->display_name);
945         
946
947         if (!(account_data->store_account)) {
948                 g_printerr ("modest: account has no stores: %s\n", account_name);
949                 return;
950         }
951                 
952         /* Show the account data in the widgets: */
953         
954         /* Note that we never show the non-display name in the UI.
955          * (Though the display name defaults to the non-display name at the start.) */
956         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
957                 account_data->display_name ? account_data->display_name : "");
958                 
959         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
960                 account_data->fullname ? account_data->fullname : "");
961         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
962                 account_data->email ? account_data->email : "");
963                 
964         ModestServerAccountData *incoming_account = account_data->store_account;
965                 
966         if (incoming_account)
967                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
968         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
969                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
970         if (!retrieve) {
971                 /* Default to something, though no default is specified in the UI spec: */
972                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
973         }
974         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
975         g_free (retrieve);
976         
977         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
978                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
979         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
980         
981         
982         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
983                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
984         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
985         
986         /* Only show the leave-on-server checkbox for POP, 
987          * as per the UI spec: */
988         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
989                 gtk_widget_hide (dialog->checkbox_leave_messages);
990         } else {
991                 gtk_widget_show (dialog->checkbox_leave_messages);
992         }
993                 
994         if (incoming_account) {
995                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
996                         incoming_account->username ? incoming_account->username : "");
997                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
998                         incoming_account->password ? incoming_account->password : "");
999                         
1000                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1001                         incoming_account->hostname ? incoming_account->hostname : "");
1002                         
1003                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
1004                         dialog->account_manager, incoming_account->account_name);
1005                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1006                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
1007                         
1008                 update_incoming_server_title (dialog, incoming_account->proto);
1009                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1010                 
1011                 const ModestProtocol security = modest_server_account_get_security (
1012                         dialog->account_manager, incoming_account->account_name);
1013                 modest_serversecurity_combo_box_set_active_serversecurity (
1014                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1015                 
1016                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1017                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1018                 hildon_number_editor_set_value (
1019                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1020         
1021         }
1022         
1023         ModestServerAccountData *outgoing_account = account_data->transport_account;
1024         if (outgoing_account) {
1025                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1026                         outgoing_account->hostname ? outgoing_account->hostname : "");
1027                 
1028                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1029                         outgoing_account->username ? outgoing_account->username : "");
1030                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1031                         outgoing_account->password ? outgoing_account->password : "");
1032                 
1033                 /* Get the secure-auth setting: */
1034                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
1035                         dialog->account_manager, outgoing_account->account_name);
1036                 modest_secureauth_combo_box_set_active_secureauth (
1037                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1038                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1039                 
1040                 modest_serversecurity_combo_box_fill (
1041                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1042                 
1043                 /* Get the security setting: */
1044                 const ModestProtocol security = modest_server_account_get_security (
1045                         dialog->account_manager, outgoing_account->account_name);
1046                 modest_serversecurity_combo_box_set_active_serversecurity (
1047                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1048                 
1049                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1050                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1051                 hildon_number_editor_set_value (
1052                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1053         }
1054         
1055         /* account_data->is_enabled,  */
1056         /*account_data->is_default,  */
1057
1058         /* account_data->store_account->proto */
1059
1060         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1061         
1062         /* Unset the modified flag so we can detect changes later: */
1063         dialog->modified = FALSE;
1064 }
1065
1066 static gboolean
1067 save_configuration (ModestAccountSettingsDialog *dialog)
1068 {
1069         g_assert (dialog->account_name);
1070         
1071         const gchar* account_name = dialog->account_name;
1072                 
1073         /* Set the account data from the widgets: */
1074         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1075         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1076                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1077         if (!test)
1078                 return FALSE;
1079                 
1080         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1081         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1082                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1083         if (!test)
1084                 return FALSE;
1085                 
1086         /* Signature: */
1087         if (dialog->signature_dialog) {
1088                 gboolean use_signature = FALSE;
1089         gchar *signature = modest_signature_editor_dialog_get_settings (
1090                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1091                 &use_signature);
1092         
1093         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1094                 signature, use_signature);
1095         g_free (signature);
1096     }
1097         
1098         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1099                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1100         modest_account_mgr_set_string (dialog->account_manager, account_name,
1101                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1102         g_free (retrieve);
1103         
1104         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1105                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1106         modest_account_mgr_set_int (dialog->account_manager, account_name,
1107                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1108         
1109         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1110         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1111                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1112         if (!test)
1113                 return FALSE;
1114                         
1115         /* Incoming: */
1116         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1117                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1118         g_assert (incoming_account_name);
1119         
1120         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1121         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1122                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1123         if (!test)
1124                 return FALSE;
1125                                 
1126         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1127         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1128                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1129         if (!test)
1130                 return FALSE;
1131                                 
1132         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1133         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1134                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1135         if (!test)
1136                 return FALSE;
1137                         
1138         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
1139                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
1140                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1141                         : MODEST_PROTOCOL_AUTH_NONE;
1142         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1143                         
1144         const ModestProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1145                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1146         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1147         
1148         /* port: */
1149         gint port_num = hildon_number_editor_get_value (
1150                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1151         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1152                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1153                 
1154         g_free (incoming_account_name);
1155         
1156         /* Outgoing: */
1157         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1158                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1159         g_assert (outgoing_account_name);
1160         
1161         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1162         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1163                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1164         if (!test)
1165                 return FALSE;
1166                 
1167         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1168         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1169                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1170         if (!test)
1171                 return FALSE;
1172                 
1173         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1174         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1175                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1176         if (!test)
1177                 return FALSE;
1178         
1179         const ModestProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1180                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1181         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1182         
1183         const ModestProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1184                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1185         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1186                 
1187         /* port: */
1188         port_num = hildon_number_editor_get_value (
1189                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1190         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1191                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1192                         
1193         g_free (outgoing_account_name);
1194         
1195         
1196         /* Set the changed account title last, to simplify the previous code: */
1197         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1198         if ((!account_title) || (strlen(account_title) == 0))
1199                 return FALSE; /* Should be prevented already anyway. */
1200                 
1201         if (strcmp(account_title, account_name) != 0) {
1202                 /* Change the title: */
1203                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1204                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1205                 if (!test)
1206                         return FALSE;
1207         }
1208         
1209         /* Save connection-specific SMTP server accounts: */
1210         if (dialog->specific_window)
1211                 return modest_connection_specific_smtp_window_save_server_accounts (
1212                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1213         else
1214                 return TRUE;
1215 }
1216
1217 static gboolean entry_is_empty (GtkWidget *entry)
1218 {
1219         if (!entry)
1220                 return FALSE;
1221                 
1222         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1223         if ((!text) || (strlen(text) == 0))
1224                 return TRUE;
1225         else
1226                 return FALSE;
1227 }
1228
1229 static void
1230 enable_buttons (ModestAccountSettingsDialog *self)
1231 {
1232         gboolean enable_ok = TRUE;
1233         
1234         /* The account details title is mandatory: */
1235         if (entry_is_empty(self->entry_account_title))
1236                         enable_ok = FALSE;
1237
1238         /* The user details username is mandatory: */
1239         if (entry_is_empty(self->entry_user_username))
1240                 enable_ok = FALSE;
1241                 
1242         /* The user details email address is mandatory: */
1243         if (enable_ok && entry_is_empty (self->entry_user_email))
1244                 enable_ok = FALSE;
1245
1246         /* The custom incoming server is mandatory: */
1247         if (entry_is_empty(self->entry_incomingserver))
1248                 enable_ok = FALSE;
1249                         
1250         /* Enable the buttons, 
1251          * identifying them via their associated response codes:
1252          */
1253         GtkDialog *dialog_base = GTK_DIALOG (self);
1254     gtk_dialog_set_response_sensitive (dialog_base,
1255                                        GTK_RESPONSE_OK,
1256                                        enable_ok);
1257 }
1258
1259 static void
1260 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1261 {
1262         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1263         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1264
1265
1266         object_class->get_property = modest_account_settings_dialog_get_property;
1267         object_class->set_property = modest_account_settings_dialog_set_property;
1268         object_class->dispose = modest_account_settings_dialog_dispose;
1269         object_class->finalize = modest_account_settings_dialog_finalize;
1270 }
1271  
1272 static void
1273 show_error (GtkWindow *parent_window, const gchar* text)
1274 {
1275         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1276                 (GtkDialogFlags)0,
1277                  GTK_MESSAGE_ERROR,
1278                  GTK_BUTTONS_OK,
1279                  text ));
1280                  
1281                  gtk_dialog_run (dialog);
1282                  gtk_widget_destroy (GTK_WIDGET (dialog));
1283 }
1284
1285 static void
1286 show_ok (GtkWindow *parent_window, const gchar* text)
1287 {
1288         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1289                 (GtkDialogFlags)0,
1290                  GTK_MESSAGE_INFO,
1291                  GTK_BUTTONS_OK,
1292                  text ));
1293                  
1294                  gtk_dialog_run (dialog);
1295                  gtk_widget_destroy (GTK_WIDGET (dialog));
1296 }
1297
1298
1299