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