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