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