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