9ec09ef0fa2baeef9ab1554022bb3a49bc27b204
[modest] / src / maemo / modest-account-settings-dialog.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-account-settings-dialog.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <gtk/gtkstock.h>
18 #include <hildon-widgets/hildon-caption.h>
19 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
20 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
21 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
22 #include "maemo/easysetup/modest-easysetup-serversecurity-combo-box.h"
23 #include "maemo/easysetup/modest-easysetup-secureauth-combo-box.h"
24 #include "maemo/easysetup/modest-validating-entry.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 <gconf/gconf-client.h>
31 #include <string.h> /* For strlen(). */
32
33 /* Include config.h so that _() works: */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
39
40 G_DEFINE_TYPE (ModestAccountSettingsDialog, modest_account_settings_dialog, GTK_TYPE_DIALOG);
41
42 #define ACCOUNT_SETTINGS_DIALOG_GET_PRIVATE(o) \
43         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, ModestAccountSettingsDialogPrivate))
44
45 typedef struct _ModestAccountSettingsDialogPrivate ModestAccountSettingsDialogPrivate;
46
47 struct _ModestAccountSettingsDialogPrivate
48 {
49 };
50
51 static void
52 enable_buttons (ModestAccountSettingsDialog *self);
53
54 static void
55 save_configuration (ModestAccountSettingsDialog *dialog);
56
57 static void
58 modest_account_settings_dialog_get_property (GObject *object, guint property_id,
59                                                                                                                         GValue *value, GParamSpec *pspec)
60 {
61         switch (property_id) {
62         default:
63                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64         }
65 }
66
67 static void
68 modest_account_settings_dialog_set_property (GObject *object, guint property_id,
69                                                                                                                         const GValue *value, GParamSpec *pspec)
70 {
71         switch (property_id) {
72         default:
73                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
74         }
75 }
76
77 static void
78 modest_account_settings_dialog_dispose (GObject *object)
79 {
80         if (G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose)
81                 G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->dispose (object);
82 }
83
84 static void
85 modest_account_settings_dialog_finalize (GObject *object)
86 {
87         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (object);
88         
89         if (self->original_account_name)
90                 g_free (self->original_account_name);
91                 
92         if (self->account_manager)
93                 g_object_unref (G_OBJECT (self->account_manager));
94         
95         G_OBJECT_CLASS (modest_account_settings_dialog_parent_class)->finalize (object);
96 }
97
98 #if 0
99 static void
100 show_error (GtkWindow *parent_window, const gchar* text);
101 #endif
102
103
104 static void
105 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
106 {
107         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
108         g_assert(self);
109         enable_buttons(self);
110 }
111
112 static void
113 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
114 {
115         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
116         g_assert(self);
117         enable_buttons(self);
118 }
119
120 /** This is a convenience function to create a caption containing a mandatory widget.
121  * When the widget is edited, the enable_buttons() vfunc will be called.
122  */
123 static GtkWidget* create_caption_new_with_asterix(ModestAccountSettingsDialog *self,
124         GtkSizeGroup *group,
125         const gchar *value,
126         GtkWidget *control,
127         GtkWidget *icon,
128         HildonCaptionStatus flag)
129 {
130   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
131   
132 /* The translated strings seem to already contain the *,
133  * but this code can be used if that is not true in future.
134  */
135 #if 0
136         /* Add a * character to indicate mandatory fields,
137          * as specified in our "Email UI Specification": */
138         if (flag == HILDON_CAPTION_MANDATORY) {
139                 gchar* title = g_strdup_printf("%s*", value);
140                 caption = hildon_caption_new (group, title, control, icon, flag);       
141                 g_free(title);
142         }       
143         else
144                 caption = hildon_caption_new (group, value, control, icon, flag);
145 #endif
146
147         /* Connect to the appropriate changed signal for the widget, 
148          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
149          */
150         if (GTK_IS_ENTRY (control)) {
151                 g_signal_connect (G_OBJECT (control), "changed",
152                 G_CALLBACK (on_caption_entry_changed), self);
153                 
154         }
155         else if (GTK_IS_COMBO_BOX (control)) {
156                 g_signal_connect (G_OBJECT (control), "changed",
157                 G_CALLBACK (on_caption_combobox_changed), self);
158         }
159          
160         return caption;
161 }
162
163 static GtkWidget*
164 create_page_account_details (ModestAccountSettingsDialog *self)
165 {
166         GtkWidget *box = gtk_vbox_new (FALSE, 2);
167         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
168         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
169         gtk_widget_show (label);
170         
171         /* Create a size group to be used by all captions.
172          * Note that HildonCaption does not create a default size group if we do not specify one.
173          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
174         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
175            
176         /* The description widgets: */  
177         self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
178         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
179                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
180         gtk_widget_show (self->entry_account_title);
181         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
182         gtk_widget_show (caption);
183         
184         /* Prevent the use of some characters in the account title, 
185          * as required by our UI specification: */
186         GList *list_prevent = NULL;
187         list_prevent = g_list_append (list_prevent, "\\");
188         list_prevent = g_list_append (list_prevent, "/");
189         list_prevent = g_list_append (list_prevent, ":");
190         list_prevent = g_list_append (list_prevent, "*");
191         list_prevent = g_list_append (list_prevent, "?");
192         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions “, but maybe means ", maybe both. */
193         list_prevent = g_list_append (list_prevent, "“");
194         list_prevent = g_list_append (list_prevent, "<"); 
195         list_prevent = g_list_append (list_prevent, ">"); 
196         list_prevent = g_list_append (list_prevent, "|");
197         list_prevent = g_list_append (list_prevent, "^");       
198         easysetup_validating_entry_set_unallowed_characters (
199                 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
200         g_list_free (list_prevent);
201         
202         /* Set max length as in the UI spec:
203          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
204         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
205         
206         /* TODO: The Retrieve and Limit Retrieve combo boxes. */
207         
208         /* The leave-messages widgets: */
209         if(!self->checkbox_leave_messages)
210                 self->checkbox_leave_messages = 
211                         gtk_check_button_new_with_label (_("mcen_fi_advsetup_leave_on_server"));
212         gtk_box_pack_start (GTK_BOX (box), self->checkbox_leave_messages, FALSE, FALSE, 2);
213         gtk_widget_show (self->checkbox_leave_messages);
214         
215         
216         gtk_widget_show (GTK_WIDGET (box));
217         
218         return GTK_WIDGET (box);
219 }
220
221 static void
222 on_button_signature (GtkButton *button, gpointer user_data)
223 {
224         
225 }
226
227 static GtkWidget*
228 create_page_user_details (ModestAccountSettingsDialog *self)
229 {
230         GtkWidget *box = gtk_vbox_new (FALSE, 2);
231         
232         /* Create a size group to be used by all captions.
233          * Note that HildonCaption does not create a default size group if we do not specify one.
234          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
235         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
236          
237         /* The name widgets: */
238         self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
239         /* Set max length as in the UI spec:
240          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
241         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
242         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
243                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
244         gtk_widget_show (self->entry_user_name);
245         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
246         gtk_widget_show (caption);
247         
248         /* Prevent the use of some characters in the name, 
249          * as required by our UI specification: */
250         GList *list_prevent = NULL;
251         list_prevent = g_list_append (list_prevent, "<");
252         list_prevent = g_list_append (list_prevent, ">");
253         easysetup_validating_entry_set_unallowed_characters (
254                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
255         g_list_free (list_prevent);
256         
257         /* The username widgets: */     
258         self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
259         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
260                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
261         gtk_widget_show (self->entry_user_username);
262         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
263         gtk_widget_show (caption);
264         
265         /* Prevent the use of some characters in the username, 
266          * as required by our UI specification: */
267         easysetup_validating_entry_set_unallowed_characters_whitespace (
268                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
269         
270         /* Set max length as in the UI spec:
271          * TODO: 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_user_username), 64);
273         
274         /* The password widgets: */     
275         self->entry_user_password = gtk_entry_new ();
276         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
277         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
278         caption = create_caption_new_with_asterix (self, sizegroup, 
279                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
280         gtk_widget_show (self->entry_user_password);
281         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
282         gtk_widget_show (caption);
283         
284         /* The email address widgets: */        
285         self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
286         caption = create_caption_new_with_asterix (self, sizegroup, 
287                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
288         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
289         gtk_widget_show (self->entry_user_email);
290         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
291         gtk_widget_show (caption);
292         
293         /* Set max length as in the UI spec:
294          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
295         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
296         
297         
298         /* Signature button: */
299         if (!self->button_signature)
300                 self->button_signature = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
301         caption = hildon_caption_new (sizegroup, _("mcen_fi_email_signature"), 
302                 self->button_signature, NULL, HILDON_CAPTION_OPTIONAL);
303         gtk_widget_show (self->button_signature);
304         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
305         gtk_widget_show (caption);
306                 
307         g_signal_connect (G_OBJECT (self->button_signature), "clicked",
308                 G_CALLBACK (on_button_signature), self);
309                 
310         gtk_widget_show (GTK_WIDGET (box));
311         
312         return GTK_WIDGET (box);
313 }
314
315 /** Change the caption title for the incoming server, 
316  * as specified in the UI spec:
317  */
318 static void update_incoming_server_title (ModestAccountSettingsDialog *self, ModestProtocol protocol)
319 {
320         const gchar* type = 
321                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
322                         _("mail_fi_emailtype_pop3") : 
323                         _("mail_fi_emailtype_imap") );
324                         
325                 
326         /* Note that this produces a compiler warning, 
327          * because the compiler does not know that the translated string will have a %s in it.
328          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
329         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
330         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
331         g_free(incomingserver_title);
332 }
333
334 /** Change the caption title for the incoming server, 
335  * as specified in the UI spec:
336  */
337 static void update_incoming_server_security_choices (ModestAccountSettingsDialog *self, ModestProtocol protocol)
338 {
339         /* Fill the combo with appropriately titled choices for POP or IMAP. */
340         /* The choices are the same, but the titles are different, as in the UI spec. */
341         easysetup_serversecurity_combo_box_fill (
342                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
343 }
344            
345 static GtkWidget* create_page_incoming (ModestAccountSettingsDialog *self)
346 {
347         GtkWidget *box = gtk_vbox_new (FALSE, 2);
348         
349         /* Create a size group to be used by all captions.
350          * Note that HildonCaption does not create a default size group if we do not specify one.
351          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
352         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
353          
354         /* The incoming server widgets: */
355         if(!self->entry_incomingserver)
356                 self->entry_incomingserver = gtk_entry_new ();
357
358         if (self->caption_incoming)
359           gtk_widget_destroy (self->caption_incoming);
360            
361         /* The caption title will be updated in update_incoming_server_title().
362          * so this default text will never be seen: */
363         /* (Note: Changing the title seems pointless. murrayc) */
364         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
365                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
366         gtk_widget_show (self->entry_incomingserver);
367         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
368         gtk_widget_show (self->caption_incoming);
369         
370         /* The secure connection widgets: */
371         /* This will be filled by update_incoming_server_security_choices(). */
372         if (!self->combo_incoming_security)
373                 self->combo_incoming_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
374         GtkWidget *caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
375                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
376         gtk_widget_show (self->combo_incoming_security);
377         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
378         gtk_widget_show (caption);
379         
380         /* The port widgets: */
381         if (!self->entry_incoming_port)
382                 self->entry_incoming_port = GTK_WIDGET (gtk_entry_new ());
383         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
384                 self->entry_incoming_port, NULL, HILDON_CAPTION_OPTIONAL);
385         gtk_widget_show (self->entry_incoming_port);
386         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
387         gtk_widget_show (caption);
388         
389         /* The secure authentication widgets: */
390         if(!self->checkbox_incoming_auth)
391                 self->checkbox_incoming_auth = 
392                         gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
393         gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
394         gtk_widget_show (self->checkbox_incoming_auth);
395         
396         gtk_widget_show (GTK_WIDGET (box));
397         
398         return GTK_WIDGET (box);
399 }
400
401 static void
402 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
403 {
404         GtkWidget *widget = GTK_WIDGET (user_data);
405         
406         /* Enable the widget only if the toggle button is active: */
407         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
408         gtk_widget_set_sensitive (widget, enable);
409 }
410
411 /* Make the sensitivity of a widget depend on a toggle button.
412  */
413 static void
414 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
415 {
416         g_signal_connect (G_OBJECT (button), "toggled",
417                 G_CALLBACK (on_toggle_button_changed), widget);
418         
419         /* Set the starting sensitivity: */
420         on_toggle_button_changed (button, widget);
421 }
422         
423 static void
424 on_smtp_servers_window_hide (GtkWindow *window, gpointer user_data)
425 {
426         /* Destroy the window when it is closed: */
427         gtk_widget_destroy (GTK_WIDGET (window));
428 }
429
430 static void
431 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
432 {
433
434         ModestAccountSettingsDialog * self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
435         
436         /* Show the window: */
437         /* TODO: Retrieve the chosen settings,
438          * so we can supply them when creating the connection somehow.
439          */
440         GtkWidget *window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
441         gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
442         g_signal_connect (G_OBJECT (window), "hide",
443                 G_CALLBACK (on_smtp_servers_window_hide), self);
444     gtk_widget_show (window);
445 }
446
447 static void
448 on_combo_outgoing_auth_changed (GtkComboBox *widget, gpointer user_data)
449 {
450         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (user_data);
451         
452         ModestProtocol protocol_security = 
453                 easysetup_secureauth_combo_box_get_active_secureauth (
454                         EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
455         const gboolean secureauth_used = protocol_security != MODEST_PROTOCOL_AUTH_NONE;
456         
457         gtk_widget_set_sensitive (self->caption_outgoing_username, secureauth_used);
458         gtk_widget_set_sensitive (self->caption_outgoing_password, secureauth_used);
459 }
460
461 static GtkWidget* create_page_outgoing (ModestAccountSettingsDialog *self)
462 {
463         GtkWidget *box = gtk_vbox_new (FALSE, 2);
464         
465         /* Create a size group to be used by all captions.
466          * Note that HildonCaption does not create a default size group if we do not specify one.
467          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
468         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
469          
470         /* The outgoing server widgets: */
471         if (!self->entry_outgoingserver)
472                 self->entry_outgoingserver = gtk_entry_new ();
473         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
474                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
475         gtk_widget_show (self->entry_outgoingserver);
476         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
477         gtk_widget_show (caption);
478         
479         /* The secure authentication widgets: */
480         if (!self->combo_outgoing_auth)
481                 self->combo_outgoing_auth = GTK_WIDGET (easysetup_secureauth_combo_box_new ());
482         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
483                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
484         gtk_widget_show (self->combo_outgoing_auth);
485         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
486         gtk_widget_show (caption);
487         
488         /* Dim the outgoing username and password when no secure authentication is used, as per the UI spec: */
489         g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed", (GCallback)on_combo_outgoing_auth_changed, self);
490         
491         /* The username widgets: */     
492         self->entry_outgoing_username = GTK_WIDGET (easysetup_validating_entry_new ());
493         self->caption_outgoing_username = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
494                 self->entry_outgoing_username, NULL, HILDON_CAPTION_MANDATORY);
495         gtk_widget_show (self->entry_outgoing_username);
496         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_username, FALSE, FALSE, 2);
497         gtk_widget_show (self->caption_outgoing_username);
498         
499         /* Prevent the use of some characters in the username, 
500          * as required by our UI specification: */
501         easysetup_validating_entry_set_unallowed_characters_whitespace (
502                 EASYSETUP_VALIDATING_ENTRY (self->entry_outgoing_username));
503         
504         /* Set max length as in the UI spec:
505          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
506         gtk_entry_set_max_length (GTK_ENTRY (self->entry_outgoing_username), 64);
507         
508         /* The password widgets: */     
509         self->entry_outgoing_password = gtk_entry_new ();
510         gtk_entry_set_visibility (GTK_ENTRY (self->entry_outgoing_password), FALSE);
511         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_outgoing_password), '*'); */
512         self->caption_outgoing_password = create_caption_new_with_asterix (self, sizegroup, 
513                 _("mail_fi_password"), self->entry_outgoing_password, NULL, HILDON_CAPTION_OPTIONAL);
514         gtk_widget_show (self->entry_outgoing_password);
515         gtk_box_pack_start (GTK_BOX (box), self->caption_outgoing_password, FALSE, FALSE, 2);
516         gtk_widget_show (self->caption_outgoing_password);
517         
518         /* The secure connection widgets: */
519         /* This will be filled and set with easysetup_serversecurity_combo_box_fill() 
520          * and easysetup_serversecurity_combo_box_set_active_serversecurity().
521          */
522         if (!self->combo_outgoing_security)
523                 self->combo_outgoing_security = GTK_WIDGET (easysetup_serversecurity_combo_box_new ());
524         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
525                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
526         gtk_widget_show (self->combo_outgoing_security);
527         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
528         gtk_widget_show (caption);      
529         
530         /* The port widgets: */
531         if (!self->entry_outgoing_port)
532                 self->entry_outgoing_port = GTK_WIDGET (gtk_entry_new ());
533         caption = hildon_caption_new (sizegroup, _("mcen_fi_emailsetup_port"), 
534                 self->entry_outgoing_port, NULL, HILDON_CAPTION_OPTIONAL);
535         gtk_widget_show (self->entry_outgoing_port);
536         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
537         gtk_widget_show (caption);
538         
539         /* connection-specific checkbox: */
540         if (!self->checkbox_outgoing_smtp_specific) {
541                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
542                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
543                         FALSE);
544         }
545         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
546         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
547         
548         /* Connection-specific SMTP-Severs Edit button: */
549         if (!self->button_outgoing_smtp_servers)
550                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_emailsetup_edit"));
551         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
552                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
553         gtk_widget_show (self->button_outgoing_smtp_servers);
554         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
555         gtk_widget_show (caption);
556         
557         /* Only enable the button when the checkbox is checked: */
558         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
559                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
560                 
561         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
562                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
563         
564         
565         gtk_widget_show (GTK_WIDGET (box));
566         
567         return GTK_WIDGET (box);
568 }
569
570 /*
571  */
572 static void 
573 on_response (GtkDialog *wizard_dialog,
574         gint response_id,
575         gpointer user_data)
576 {
577         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (wizard_dialog);
578         enable_buttons (self);
579         
580         /* TODO: Prevent the OK response if the data is invalid. */
581         
582         if (response_id == GTK_RESPONSE_OK)
583           save_configuration (self);
584 }
585
586 static void
587 modest_account_settings_dialog_init (ModestAccountSettingsDialog *self)
588 {
589         /* Create the notebook to be used by the GtkDialog base class:
590          * Each page of the notebook will be a page of the wizard: */
591         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
592
593     
594     gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup"));
595         
596         /* Get the account manager object, 
597          * so we can check for existing accounts,
598          * and create new accounts: */
599         self->account_manager = modest_runtime_get_account_mgr ();
600         g_assert (self->account_manager);
601         g_object_ref (self->account_manager);
602         
603     /* Create the common pages, 
604      */
605         self->page_account_details = create_page_account_details (self);
606         self->page_user_details = create_page_user_details (self);
607         self->page_incoming = create_page_incoming (self);
608         self->page_outgoing = create_page_outgoing (self);
609         
610         /* Add the notebook pages: */
611         gtk_notebook_append_page (notebook, self->page_account_details, 
612                 gtk_label_new (_("mcen_ti_account_settings_account")));
613         gtk_notebook_append_page (notebook, self->page_user_details, 
614                 gtk_label_new (_("mcen_ti_account_settings_userinfo")));
615         gtk_notebook_append_page (notebook, self->page_incoming,
616                 gtk_label_new (_("mcen_ti_advsetup_retrieval")));
617         gtk_notebook_append_page (notebook, self->page_outgoing,
618                 gtk_label_new (_("mcen_ti_advsetup_sending")));
619                 
620         GtkDialog *dialog = GTK_DIALOG (self);
621         gtk_container_add (GTK_CONTAINER (dialog->vbox), GTK_WIDGET (notebook));
622         gtk_widget_show (GTK_WIDGET (notebook));
623         
624     /* Add the buttons: */
625     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_OK, GTK_RESPONSE_OK);
626     gtk_dialog_add_button (GTK_DIALOG(self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
627     
628     /* Connect to the dialog's response signal: */
629     g_signal_connect_after (G_OBJECT (self), "response",
630             G_CALLBACK (on_response), self);       
631 }
632
633 ModestAccountSettingsDialog*
634 modest_account_settings_dialog_new (void)
635 {
636         return g_object_new (MODEST_TYPE_ACCOUNT_SETTINGS_DIALOG, NULL);
637 }
638
639 /** Update the UI with the stored account details, so they can be edited.
640  * @account_name: Name of the account, which should contain incoming and outgoing server accounts.
641  */
642 void modest_account_settings_dialog_set_account_name (ModestAccountSettingsDialog *dialog, const gchar* account_name)
643 {
644         if (!account_name)
645                 return;
646                 
647         /* Save the account name so we can refer to it if the user changes it: */
648         if (dialog->original_account_name)
649                 g_free (dialog->original_account_name);
650         dialog->original_account_name = g_strdup (account_name);
651         
652         /* Get the account data for this account name: */
653         ModestAccountData *account_data = modest_account_mgr_get_account_data (dialog->account_manager, 
654                 account_name);
655         if (!account_data) {
656                 g_printerr ("modest: failed to get account data for %s\n", account_name);
657                 return;
658         }
659
660         if (!(account_data->store_account)) {
661                 g_printerr ("modest: account has no stores: %s\n", account_name);
662                 return;
663         }
664                 
665         /* Show the account data in the widgets: */
666         gtk_entry_set_text( GTK_ENTRY (dialog->entry_account_title),
667                 account_name ? account_name : "");
668         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_name), 
669                 account_data->fullname ? account_data->fullname : "");
670         gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_email), 
671                 account_data->email ? account_data->email : "");
672                 
673         const gboolean leave_on_server = modest_account_mgr_get_bool (dialog->account_manager, account_name,
674                 MODEST_ACCOUNT_LEAVE_ON_SERVER, FALSE /* not server account */);
675         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages), leave_on_server);
676                 
677                 
678         ModestServerAccountData *incoming_account = account_data->store_account;
679         if (incoming_account) {
680                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_username),
681                         incoming_account->username ? incoming_account->username : "");
682                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_user_password), 
683                         incoming_account->password ? incoming_account->password : "");
684                         
685                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_incomingserver), 
686                         incoming_account->hostname ? incoming_account->hostname : "");
687                         
688                 easysetup_serversecurity_combo_box_set_active_serversecurity (
689                         EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_incoming_security), incoming_account->proto);
690                 update_incoming_server_title (dialog, incoming_account->proto);
691                 update_incoming_server_security_choices (dialog, incoming_account->proto);
692                 
693                 /* TODO:
694         gchar            *uri;
695         ModestProtocol    proto;
696         gchar            *password;
697         time_t            last_updated;
698         GSList           *options;
699         */
700         
701         }
702         
703         ModestServerAccountData *outgoing_account = account_data->transport_account;
704         if (outgoing_account) {
705                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoingserver), 
706                         outgoing_account->hostname ? outgoing_account->hostname : "");
707                 
708                 /* TODO: Dim these if secure authentication is None, as per the UI spec: */
709                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_username), 
710                         outgoing_account->username ? outgoing_account->username : "");
711                 gtk_entry_set_text( GTK_ENTRY (dialog->entry_outgoing_password), 
712                         outgoing_account->password ? outgoing_account->password : "");
713                 
714                 /* TODO: How do we get the auth setting from the server account struct?: */
715                 /* This seems to be in ->options, with hard-coded option names.
716                  * This will need new API in ModestAccountMgr. */
717                 easysetup_secureauth_combo_box_set_active_secureauth (
718                         EASYSETUP_SECUREAUTH_COMBO_BOX (dialog->combo_outgoing_auth), MODEST_PROTOCOL_AUTH_NONE);
719                 on_combo_outgoing_auth_changed (GTK_COMBO_BOX (dialog->combo_outgoing_auth), dialog);
720                 
721                 easysetup_serversecurity_combo_box_fill (
722                 EASYSETUP_SERVERSECURITY_COMBO_BOX (dialog->combo_outgoing_security), outgoing_account->proto);
723                 
724                 
725                 
726                 /* TODO: set port. */
727         }
728         
729         /* TODO: account_data->display_name */
730         /* account_data->is_enabled,  */
731         /*account_data->is_default,  */
732
733         /* account_data->store_account->proto */
734
735         modest_account_mgr_free_account_data (dialog->account_manager, account_data);
736 }
737
738 static void
739 save_configuration (ModestAccountSettingsDialog *dialog)
740 {
741         g_assert (dialog->original_account_name);
742         
743         const gchar* account_name = dialog->original_account_name;
744                 
745         /* Set the account data from the widgets: */
746         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_name));
747         modest_account_mgr_set_string (dialog->account_manager, account_name,
748                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
749                 
750         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_email));
751         modest_account_mgr_set_string (dialog->account_manager, account_name,
752                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
753                 
754         /* TODO: Change name: */
755         /* Possibly the account name may never change, but that should be hidden, 
756          * and the display name may change, defaulting to the account name.
757          */
758         
759         const gboolean leave_on_server = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->checkbox_leave_messages));
760         modest_account_mgr_set_bool (dialog->account_manager, account_name,
761                 MODEST_ACCOUNT_LEAVE_ON_SERVER, leave_on_server, FALSE /* not server account */);
762         
763         /* Incoming: */
764         gchar* incoming_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
765                 MODEST_ACCOUNT_STORE_ACCOUNT, FALSE /* not server account */);
766         g_assert (incoming_account_name);
767         
768         const gchar* hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_incomingserver));
769         modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
770                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
771                 
772         const gchar* username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_username));
773         modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
774                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
775                 
776         const gchar* password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_user_password));
777         modest_account_mgr_set_string (dialog->account_manager, incoming_account_name,
778                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
779         
780         /* TODO: How can we set these in the server account?:   
781         ModestProtocol protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
782                         ? MODEST_PROTOCOL_AUTH_PASSWORD
783                         : MODEST_PROTOCOL_AUTH_NONE;
784                         
785         ModestProtocol protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
786                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
787         
788         */
789         
790         g_free (incoming_account_name);
791         
792         /* Outgoing: */
793         gchar* outgoing_account_name = modest_account_mgr_get_string (dialog->account_manager, account_name,
794                 MODEST_ACCOUNT_TRANSPORT_ACCOUNT, FALSE /* not server account */);
795         g_assert (outgoing_account_name);
796         
797         hostname = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoingserver));
798         modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
799                 MODEST_ACCOUNT_HOSTNAME, hostname, TRUE /* server account */);
800                 
801         username = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_username));
802         modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
803                 MODEST_ACCOUNT_USERNAME, username, TRUE /* server account */);
804                 
805         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
806         modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
807                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
808                         
809         password = gtk_entry_get_text (GTK_ENTRY (dialog->entry_outgoing_password));
810         modest_account_mgr_set_string (dialog->account_manager, outgoing_account_name,
811                 MODEST_ACCOUNT_PASSWORD, password, TRUE /*  server account */);
812         
813         /* TODO: How do we set these in the account data?:
814         ModestProtocol protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
815                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
816         
817         ModestProtocol protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
818                 EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
819          */
820                 
821         g_free (outgoing_account_name);
822         
823 }
824
825         
826 #if 0
827 static gboolean
828 on_before_next (GtkDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
829 {
830         ModestAccountSettingsDialog *self = MODEST_ACCOUNT_SETTINGS_DIALOG (dialog);
831         
832         /* Do extra validation that couldn't be done for every key press,
833          * either because it was too slow,
834          * or because it requires interaction:
835          */
836         if (current_page == self->page_account_details) {       
837                 /* Check that the title is not already in use: */
838                 const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
839                 if ((!account_name) || (strlen(account_name) == 0))
840                         return FALSE;
841                         
842                 gboolean name_in_use = FALSE;
843                 name_in_use = modest_account_mgr_account_exists (self->account_manager,
844                         account_name, FALSE /*  server_account */);
845                 
846                 if (name_in_use) {
847                         /* Warn the user via a dialog: */
848                         show_error (GTK_WINDOW (self), _("mail_ib_account_name_already_existing."));
849             
850                         return FALSE;
851                 }
852         }
853         else if (current_page == self->page_user_details) {
854                 /* Check that the email address is valud: */
855                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
856                 if ((!email_address) || (strlen(email_address) == 0))
857                         return FALSE;
858                         
859                 if (!modest_text_utils_validate_email_address (email_address)) {
860                         /* Warn the user via a dialog: */
861                         show_error (GTK_WINDOW (self), _("mcen_ib_invalid_email"));
862                                              
863             /* Return focus to the email address entry: */
864             gtk_widget_grab_focus (self->entry_user_email);
865             
866                         return FALSE;
867                 }
868                 
869                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
870                 create_subsequent_pages (self);
871         }
872         
873         /* TODO: The UI Spec wants us to check that the servernames are valid, 
874          * but does not specify how.
875          */
876           
877         if(next_page == self->page_incoming) {
878                 set_default_custom_servernames (self);
879         }
880         else if (next_page == self->page_outgoing) {
881                 set_default_custom_servernames (self);
882         }
883         
884         /* If this is the last page, and this is a click on Finish, 
885          * then attempt to create the dialog.
886          */
887         if(!next_page) /* This is NULL when this is a click on Finish. */
888         {
889                 create_account (self);
890         }
891         
892         
893         return TRUE;
894 }
895 #endif
896
897 static gboolean entry_is_empty (GtkWidget *entry)
898 {
899         if (!entry)
900                 return FALSE;
901                 
902         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
903         if ((!text) || (strlen(text) == 0))
904                 return TRUE;
905         else
906                 return FALSE;
907 }
908
909 static void
910 enable_buttons (ModestAccountSettingsDialog *self)
911 {
912         gboolean enable_ok = TRUE;
913         
914         /* The account details title is mandatory: */
915         if (entry_is_empty(self->entry_account_title))
916                         enable_ok = FALSE;
917
918         /* The user details username is mandatory: */
919         if (entry_is_empty(self->entry_user_username))
920                 enable_ok = FALSE;
921                 
922         /* The user details email address is mandatory: */
923         if (enable_ok && entry_is_empty (self->entry_user_email))
924                 enable_ok = FALSE;
925
926         /* The custom incoming server is mandatory: */
927         if (entry_is_empty(self->entry_incomingserver))
928                 enable_ok = FALSE;
929                         
930         /* Enable the buttons, 
931          * identifying them via their associated response codes:
932          */
933         GtkDialog *dialog_base = GTK_DIALOG (self);
934     gtk_dialog_set_response_sensitive (dialog_base,
935                                        GTK_RESPONSE_OK,
936                                        enable_ok);
937 }
938
939 static void
940 modest_account_settings_dialog_class_init (ModestAccountSettingsDialogClass *klass)
941 {
942         GObjectClass *object_class = G_OBJECT_CLASS (klass);
943         g_type_class_add_private (klass, sizeof (ModestAccountSettingsDialogPrivate));
944
945
946         object_class->get_property = modest_account_settings_dialog_get_property;
947         object_class->set_property = modest_account_settings_dialog_set_property;
948         object_class->dispose = modest_account_settings_dialog_dispose;
949         object_class->finalize = modest_account_settings_dialog_finalize;
950 }
951  
952 #if 0
953 static void
954 show_error (GtkWindow *parent_window, const gchar* text)
955 {
956         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
957                 (GtkDialogFlags)0,
958                  GTK_MESSAGE_ERROR,
959                  GTK_BUTTONS_OK,
960                  text ));
961                  
962                  gtk_dialog_run (dialog);
963                  gtk_widget_destroy (GTK_WIDGET (dialog));
964 }
965 #endif
966
967
968