2007-05-02 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 "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_NONE);
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_NONE);
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_NONE);
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_NONE);
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 (hildon_note_new_confirmation (GTK_WINDOW (self), 
828                         _("imum_nc_wizard_confirm_lose_changes")));
829                 /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
830                  
831                  const gint dialog_response = gtk_dialog_run (dialog);
832                  gtk_widget_destroy (GTK_WIDGET (dialog));
833                  
834                 if (dialog_response != GTK_RESPONSE_OK)
835                         prevent_response = TRUE;
836         }
837         /* Check for invalid input: */
838         else if (response_id != GTK_RESPONSE_CANCEL && !check_data (self)) {
839                 prevent_response = TRUE;
840         }
841                 
842         if (prevent_response) {
843                 /* This is a nasty hack. murrayc. */
844                 /* Don't let the dialog close */
845         g_signal_stop_emission_by_name (wizard_dialog, "response");
846                 return; 
847         }
848                 
849         if (response_id == GTK_RESPONSE_OK) {
850                 /* Try to save the changes: */  
851                 const gboolean saved = save_configuration (self);
852                 if (saved) {
853                         /* Do not show the account-saved dialog if we are just saving this 
854                          * temporarily, because from the user's point of view it will not 
855                          * really be saved (saved + enabled) until later.
856                          */
857                         const gboolean enabled = 
858                                 modest_account_mgr_get_enabled (self->account_manager, self->account_name);
859                         if (enabled)
860                                 show_ok (GTK_WINDOW (self), _("mcen_ib_advsetup_settings_saved"));
861                 }
862                 else
863                         show_error (GTK_WINDOW (self), _("mail_ib_setting_failed"));
864         }
865 }
866
867 static void
868 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
869 {
870         /* Create the notebook to be used by the GtkDialog base class:
871          * Each page of the notebook will be a page of the wizard: */
872         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
873
874     
875     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
876         
877         /* Get the account manager object, 
878          * so we can check for existing accounts,
879          * and create new accounts: */
880         self->account_manager = modest_runtime_get_account_mgr ();
881         g_assert (self->account_manager);
882         g_object_ref (self->account_manager);
883         
884     /* Create the common pages, 
885      */
886         self->page_account_details = create_page_account_details (self);
887         self->page_user_details = create_page_user_details (self);
888         self->page_incoming = create_page_incoming (self);
889         self->page_outgoing = create_page_outgoing (self);
890         
891         /* Add the notebook pages: */
892         gtk_notebook_append_page (notebook, self->page_account_details, 
893                 gtk_label_new (_("mcen_ti_account_settings_account")));
894         gtk_notebook_append_page (notebook, self->page_user_details, 
895                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
896         gtk_notebook_append_page (notebook, self->page_incoming,
897                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
898         gtk_notebook_append_page (notebook, self->page_outgoing,
899                 gtk_label_new (_("mcen_ti_advsetup_sending")));
900                 
901         GtkDialog *dialog = GTK_DIALOG (self);
902         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
903         gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), MODEST_MARGIN_HALF);
904         gtk_widget_show (GTK_WIDGET (notebook));
905         
906     /* Add the buttons: */
907     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
908     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
909     
910     /* Connect to the dialog's response signal: */
911     /* We use connect-before 
912      * so we can stop the signal emission, 
913      * to stop the default signal handler from closing the dialog.
914      */
915     g_signal_connect (G_OBJECT (self), "response",
916             G_CALLBACK (on_response), self); 
917             
918     self->modified = FALSE;      
919 }
920
921 ModestAccountSettingsDialog*
922 modest_account_settings_dialog_new (void)
923 {
924         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
925 }
926
927 /** Update the UI with the stored account details, so they can be edited.
928  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
929  */
930 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
931 {
932         if (!account_name)
933                 return;
934                 
935         /* Save the account name so we can refer to it later: */
936         if (dialog->account_name)
937                 g_free (dialog->account_name);
938         dialog->account_name = g_strdup (account_name);
939         
940                 
941         /* Get the account data for this account name: */
942         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
943                 account_name);
944         if (!account_data) {
945                 g_printerr ("modest: failed to get account data for %s\n", account_name);
946                 return;
947         }
948         
949         /* Save the account title so we can refer to it if the user changes it: */
950         if (dialog->original_account_title)
951                 g_free (dialog->original_account_title);
952         dialog->original_account_title = g_strdup (account_data->display_name);
953         
954
955         if (!(account_data->store_account)) {
956                 g_printerr ("modest: account has no stores: %s\n", account_name);
957                 return;
958         }
959                 
960         /* Show the account data in the widgets: */
961         
962         /* Note that we never show the non-display name in the UI.
963          * (Though the display name defaults to the non-display name at the start.) */
964         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
965                 account_data->display_name ? account_data->display_name : "");
966                 
967         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
968                 account_data->fullname ? account_data->fullname : "");
969         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
970                 account_data->email ? account_data->email : "");
971                 
972         ModestServerAccountData *incoming_account = account_data->store_account;
973                 
974         if (incoming_account)
975                 modest_retrieve_combo_box_fill (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), incoming_account->proto);
976         gchar *retrieve = modest_account_mgr_get_string (dialog->account_manager, account_name,
977                 MODEST_ACCOUNT_RETRIEVE, FALSE /* not server account */);
978         if (!retrieve) {
979                 /* Default to something, though no default is specified in the UI spec: */
980                 retrieve = g_strdup (MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY);
981         }
982         modest_retrieve_combo_box_set_active_retrieve_conf (MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve), retrieve);
983         g_free (retrieve);
984         
985         const gint limit_retrieve = modest_account_mgr_get_int (dialog->account_manager, account_name,
986                 MODEST_ACCOUNT_LIMIT_RETRIEVE, FALSE /* not server account */);
987         modest_limit_retrieve_combo_box_set_active_limit_retrieve (MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve), limit_retrieve);
988         
989         
990         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
991                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
992         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);    
993         
994         /* Only show the leave-on-server checkbox for POP, 
995          * as per the UI spec: */
996         if (incoming_account->proto != MODEST_PROTOCOL_STORE_POP) {
997                 gtk_widget_hide (dialog->checkbox_leave_messages);
998         } else {
999                 gtk_widget_show (dialog->checkbox_leave_messages);
1000         }
1001                 
1002         if (incoming_account) {
1003                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
1004                         incoming_account->username ? incoming_account->username : "");
1005                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
1006                         incoming_account->password ? incoming_account->password : "");
1007                         
1008                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
1009                         incoming_account->hostname ? incoming_account->hostname : "");
1010                         
1011                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
1012                         dialog->account_manager, incoming_account->account_name);
1013                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth), 
1014                         secure_auth == MODEST_PROTOCOL_AUTH_PASSWORD);
1015                         
1016                 update_incoming_server_title (dialog, incoming_account->proto);
1017                 update_incoming_server_security_choices (dialog, incoming_account->proto);
1018                 
1019                 const ModestProtocol security = modest_server_account_get_security (
1020                         dialog->account_manager, incoming_account->account_name);
1021                 modest_serversecurity_combo_box_set_active_serversecurity (
1022                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), security);
1023                 
1024                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, incoming_account->account_name,
1025                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1026                 hildon_number_editor_set_value (
1027                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port), port_num);
1028         
1029         }
1030         
1031         ModestServerAccountData *outgoing_account = account_data->transport_account;
1032         if (outgoing_account) {
1033                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
1034                         outgoing_account->hostname ? outgoing_account->hostname : "");
1035                 
1036                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
1037                         outgoing_account->username ? outgoing_account->username : "");
1038                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
1039                         outgoing_account->password ? outgoing_account->password : "");
1040                 
1041                 /* Get the secure-auth setting: */
1042                 const ModestProtocol secure_auth = modest_server_account_get_secure_auth(
1043                         dialog->account_manager, outgoing_account->account_name);
1044                 modest_secureauth_combo_box_set_active_secureauth (
1045                         MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), secure_auth);
1046                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
1047                 
1048                 modest_serversecurity_combo_box_fill (
1049                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
1050                 
1051                 /* Get the security setting: */
1052                 const ModestProtocol security = modest_server_account_get_security (
1053                         dialog->account_manager, outgoing_account->account_name);
1054                 modest_serversecurity_combo_box_set_active_serversecurity (
1055                         MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), security);
1056                 
1057                 const gint port_num = modest_account_mgr_get_int (dialog->account_manager, outgoing_account->account_name,
1058                         MODEST_ACCOUNT_PORT, TRUE /* server account */);
1059                 hildon_number_editor_set_value (
1060                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port), port_num);
1061         }
1062         
1063         /* account_data->is_enabled,  */
1064         /*account_data->is_default,  */
1065
1066         /* account_data->store_account->proto */
1067
1068         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
1069         
1070         /* Unset the modified flag so we can detect changes later: */
1071         dialog->modified = FALSE;
1072 }
1073
1074 static gboolean
1075 save_configuration (ModestAccountSettingsDialog *dialog)
1076 {
1077         g_assert (dialog->account_name);
1078         
1079         const gchar* account_name = dialog->account_name;
1080                 
1081         /* Set the account data from the widgets: */
1082         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
1083         gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1084                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1085         if (!test)
1086                 return FALSE;
1087                 
1088         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
1089         test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1090                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1091         if (!test)
1092                 return FALSE;
1093                 
1094         /* Signature: */
1095         if (dialog->signature_dialog) {
1096                 gboolean use_signature = FALSE;
1097         gchar *signature = modest_signature_editor_dialog_get_settings (
1098                 MODEST_SIGNATURE_EDITOR_DIALOG (dialog->signature_dialog),
1099                 &use_signature);
1100         
1101         modest_account_mgr_set_signature(dialog->account_manager, account_name, 
1102                 signature, use_signature);
1103         g_free (signature);
1104     }
1105         
1106         gchar *retrieve = modest_retrieve_combo_box_get_active_retrieve_conf (
1107                 MODEST_RETRIEVE_COMBO_BOX (dialog->combo_retrieve));
1108         modest_account_mgr_set_string (dialog->account_manager, account_name,
1109                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1110         g_free (retrieve);
1111         
1112         const gint limit_retrieve = modest_limit_retrieve_combo_box_get_active_limit_retrieve (
1113                 MODEST_LIMIT_RETRIEVE_COMBO_BOX (dialog->combo_limit_retrieve));
1114         modest_account_mgr_set_int (dialog->account_manager, account_name,
1115                 MODEST_ACCOUNT_LIMIT_RETRIEVE, limit_retrieve, FALSE /* not server account */);
1116         
1117         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
1118         test = modest_account_mgr_set_bool (dialog->account_manager, account_name,
1119                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
1120         if (!test)
1121                 return FALSE;
1122                         
1123         /* Incoming: */
1124         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1125                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
1126         g_assert (incoming_account_name);
1127         
1128         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
1129         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1130                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1131         if (!test)
1132                 return FALSE;
1133                                 
1134         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
1135         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1136                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1137         if (!test)
1138                 return FALSE;
1139                                 
1140         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
1141         test = modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
1142                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1143         if (!test)
1144                 return FALSE;
1145                         
1146         const ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active 
1147                 (GTK_TOGGLE_BUTTON (dialog->checkbox_incoming_auth)) 
1148                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1149                         : MODEST_PROTOCOL_AUTH_NONE;
1150         modest_server_account_set_secure_auth (dialog->account_manager, incoming_account_name, protocol_authentication_incoming);
1151                         
1152         const ModestProtocol protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1153                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security));
1154         modest_server_account_set_security (dialog->account_manager, incoming_account_name, protocol_security_incoming);
1155         
1156         /* port: */
1157         gint port_num = hildon_number_editor_get_value (
1158                         HILDON_NUMBER_EDITOR (dialog->entry_incoming_port));
1159         modest_account_mgr_set_int (dialog->account_manager, incoming_account_name,
1160                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1161                 
1162         g_free (incoming_account_name);
1163         
1164         /* Outgoing: */
1165         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
1166                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
1167         g_assert (outgoing_account_name);
1168         
1169         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
1170         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1171                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
1172         if (!test)
1173                 return FALSE;
1174                 
1175         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
1176         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1177                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
1178         if (!test)
1179                 return FALSE;
1180                 
1181         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
1182         test = modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
1183                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
1184         if (!test)
1185                 return FALSE;
1186         
1187         const ModestProtocol protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1188                 MODEST_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security));
1189         modest_server_account_set_security (dialog->account_manager, outgoing_account_name, protocol_security_outgoing);
1190         
1191         const ModestProtocol protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1192                 MODEST_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth));
1193         modest_server_account_set_secure_auth (dialog->account_manager, outgoing_account_name, protocol_authentication_outgoing);       
1194                 
1195         /* port: */
1196         port_num = hildon_number_editor_get_value (
1197                         HILDON_NUMBER_EDITOR (dialog->entry_outgoing_port));
1198         modest_account_mgr_set_int (dialog->account_manager, outgoing_account_name,
1199                         MODEST_ACCOUNT_PORT, port_num, TRUE /* server account */);
1200                         
1201         g_free (outgoing_account_name);
1202         
1203         
1204         /* Set the changed account title last, to simplify the previous code: */
1205         const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (dialog->entry_account_title));
1206         if ((!account_title) || (strlen(account_title) == 0))
1207                 return FALSE; /* Should be prevented already anyway. */
1208                 
1209         if (strcmp(account_title, account_name) != 0) {
1210                 /* Change the title: */
1211                 gboolean test = modest_account_mgr_set_string (dialog->account_manager, account_name,
1212                 MODEST_ACCOUNT_DISPLAY_NAME, account_title, FALSE /* not server account */);
1213                 if (!test)
1214                         return FALSE;
1215         }
1216         
1217         /* Save connection-specific SMTP server accounts: */
1218         if (dialog->specific_window)
1219                 return modest_connection_specific_smtp_window_save_server_accounts (
1220                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog->specific_window), account_name);
1221         else
1222                 return TRUE;
1223 }
1224
1225 static gboolean entry_is_empty (GtkWidget *entry)
1226 {
1227         if (!entry)
1228                 return FALSE;
1229                 
1230         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1231         if ((!text) || (strlen(text) == 0))
1232                 return TRUE;
1233         else
1234                 return FALSE;
1235 }
1236
1237 static void
1238 enable_buttons (ModestAccountSettingsDialog *self)
1239 {
1240         gboolean enable_ok = TRUE;
1241         
1242         /* The account details title is mandatory: */
1243         if (entry_is_empty(self->entry_account_title))
1244                         enable_ok = FALSE;
1245
1246         /* The user details username is mandatory: */
1247         if (entry_is_empty(self->entry_user_username))
1248                 enable_ok = FALSE;
1249                 
1250         /* The user details email address is mandatory: */
1251         if (enable_ok && entry_is_empty (self->entry_user_email))
1252                 enable_ok = FALSE;
1253
1254         /* The custom incoming server is mandatory: */
1255         if (entry_is_empty(self->entry_incomingserver))
1256                 enable_ok = FALSE;
1257                         
1258         /* Enable the buttons, 
1259          * identifying them via their associated response codes:
1260          */
1261         GtkDialog *dialog_base = GTK_DIALOG (self);
1262     gtk_dialog_set_response_sensitive (dialog_base,
1263                                        GTK_RESPONSE_OK,
1264                                        enable_ok);
1265 }
1266
1267 static void
1268 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
1269 {
1270         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1271         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
1272
1273
1274         object_class->get_property = modest_account_settings_dialog_get_property;
1275         object_class->set_property = modest_account_settings_dialog_set_property;
1276         object_class->dispose = modest_account_settings_dialog_dispose;
1277         object_class->finalize = modest_account_settings_dialog_finalize;
1278 }
1279  
1280 static void
1281 show_error (GtkWindow *parent_window, const gchar* text)
1282 {
1283                 printf("debug: show_error\n");
1284         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1285         /*
1286         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1287                 (GtkDialogFlags)0,
1288                  GTK_MESSAGE_ERROR,
1289                  GTK_BUTTONS_OK,
1290                  text ));
1291         */       
1292         
1293         gtk_dialog_run (dialog);
1294         gtk_widget_destroy (GTK_WIDGET (dialog));
1295 }
1296
1297 static void
1298 show_ok (GtkWindow *parent_window, const gchar* text)
1299 {
1300         printf("debug: show_ok\n");
1301         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1302         /*
1303         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1304                 (GtkDialogFlags)0,
1305                  GTK_MESSAGE_INFO,
1306                  GTK_BUTTONS_OK,
1307                  text ));
1308         */
1309                  
1310         gtk_dialog_run (dialog);
1311         gtk_widget_destroy (GTK_WIDGET (dialog));
1312 }
1313
1314
1315