2007-04-24 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-easysetup-wizard.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/gtkseparator.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 "widgets/modest-serversecurity-combo-box.h"
23 #include "widgets/modest-secureauth-combo-box.h"
24 #include "widgets/modest-validating-entry.h"
25 #include "modest-text-utils.h"
26 #include "modest-account-mgr.h"
27 #include "modest-account-mgr-helpers.h"
28 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
29 #include "maemo/modest-connection-specific-smtp-window.h"
30 #include "maemo/modest-maemo-ui-constants.h"
31 #include <gconf/gconf-client.h>
32 #include <string.h> /* For strlen(). */
33
34 /* Include config.h so that _() works: */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
40
41 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
42
43 #define WIZARD_DIALOG_GET_PRIVATE(o) \
44         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
45
46 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
47
48 struct _ModestEasysetupWizardDialogPrivate
49 {
50         ModestPresets *presets;
51 };
52
53 static void
54 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
55                                                                                                                         GValue *value, GParamSpec *pspec)
56 {
57         switch (property_id) {
58         default:
59                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
60         }
61 }
62
63 static void
64 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
65                                                                                                                         const GValue *value, GParamSpec *pspec)
66 {
67         switch (property_id) {
68         default:
69                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
70         }
71 }
72
73 static void
74 modest_easysetup_wizard_dialog_dispose (GObject *object)
75 {
76         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
77                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
78 }
79
80 static void
81 modest_easysetup_wizard_dialog_finalize (GObject *object)
82 {
83         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
84         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
85         
86         if (self->account_manager)
87                 g_object_unref (G_OBJECT (self->account_manager));
88                 
89         if (priv->presets)
90                 modest_presets_destroy (priv->presets);
91                 
92         if (self->specific_window)
93                 gtk_widget_destroy (self->specific_window);
94         
95         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
96 }
97
98 static void
99 show_error (GtkWindow *parent_window, const gchar* text);
100
101 static gboolean
102 create_account (ModestEasysetupWizardDialog *self);
103
104 static void
105 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
106
107 static void
108 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
109
110 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
111
112 static void
113 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
114 {
115         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
116         
117         /* Call the vfunc, which may be overridden by derived classes: */
118         if (klass->enable_buttons) {
119                 GtkNotebook *notebook = NULL;
120                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
121                 
122                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
123                 if (current_page_num == -1)
124                         return;
125                         
126                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
127                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
128         }
129 }
130
131 static void
132 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
133 {
134         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
135         g_assert(self);
136         invoke_enable_buttons_vfunc(self);
137 }
138
139 static void
140 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
141 {
142         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
143         g_assert(self);
144         invoke_enable_buttons_vfunc(self);
145 }
146
147 /** This is a convenience function to create a caption containing a mandatory widget.
148  * When the widget is edited, the enable_buttons() vfunc will be called.
149  */
150 static GtkWidget* create_caption_new_with_asterix(ModestEasysetupWizardDialog *self,
151         GtkSizeGroup *group,
152         const gchar *value,
153         GtkWidget *control,
154         GtkWidget *icon,
155         HildonCaptionStatus flag)
156 {
157   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
158   
159 /* The translated strings seem to already contain the *,
160  * but this code can be used if that is not true in future.
161  */
162 #if 0
163         /* Add a * character to indicate mandatory fields,
164          * as specified in our "Email UI Specification": */
165         if (flag == HILDON_CAPTION_MANDATORY) {
166                 gchar* title = g_strdup_printf("%s*", value);
167                 caption = hildon_caption_new (group, title, control, icon, flag);       
168                 g_free(title);
169         }       
170         else
171                 caption = hildon_caption_new (group, value, control, icon, flag);
172 #endif
173
174         /* Connect to the appropriate changed signal for the widget, 
175          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
176          */
177         if (GTK_IS_ENTRY (control)) {
178                 g_signal_connect (G_OBJECT (control), "changed",
179                 G_CALLBACK (on_caption_entry_changed), self);
180                 
181         }
182         else if (GTK_IS_COMBO_BOX (control)) {
183                 g_signal_connect (G_OBJECT (control), "changed",
184                 G_CALLBACK (on_caption_combobox_changed), self);
185         }
186          
187         return caption;
188 }
189            
190 static GtkWidget*
191 create_page_welcome (ModestEasysetupWizardDialog *self)
192 {
193         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
194         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
195         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
196         gtk_widget_show (label);
197         gtk_widget_show (GTK_WIDGET (box));
198         return GTK_WIDGET (box);
199 }
200
201 static void
202 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
203 {
204         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
205         g_assert(self);
206         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
207         
208         /* Fill the providers combo, based on the selected country: */
209         gint mcc_id = easysetup_country_combo_box_get_active_country_id (
210                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
211         easysetup_provider_combo_box_fill (
212                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, mcc_id);
213 }
214
215 static void
216 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
217 {
218         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
219         g_assert(self);
220         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
221         
222         /* Fill the providers combo, based on the selected country: */
223         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
224                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
225         
226         gchar* domain_name = NULL;
227         if(provider_id)
228           domain_name = modest_presets_get_domain (priv->presets, provider_id);
229         
230         if(!domain_name)
231                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
232                 
233         if (self->entry_user_email)
234                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
235                 
236     g_free (domain_name);
237         
238         g_free (provider_id);
239 }
240
241 static void
242 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
243 {
244         ModestEasysetupWizardDialog *dialog = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
245         show_error (GTK_WINDOW (dialog), _("ckdg_ib_maximum_characters_reached"));
246 }
247
248 static GtkWidget*
249 create_page_account_details (ModestEasysetupWizardDialog *self)
250 {
251         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
252         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
253         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
254         gtk_widget_show (label);
255         
256         /* Create a size group to be used by all captions.
257          * Note that HildonCaption does not create a default size group if we do not specify one.
258          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
259         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
260
261         /* The country widgets: */
262         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
263         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_country"), 
264                 self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
265         gtk_widget_show (self->combo_account_country);
266         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
267         gtk_widget_show (caption);
268         
269         /* connect to country combo's changed signal, so we can fill the provider combo: */
270     g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
271             G_CALLBACK (on_combo_account_country), self);
272             
273         GtkWidget *separator = gtk_hseparator_new ();
274         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
275         gtk_widget_show (separator);
276             
277         /* The service provider widgets: */     
278         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
279         
280         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_serviceprovider"), 
281                 self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
282         gtk_widget_show (self->combo_account_serviceprovider);
283         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
284         gtk_widget_show (caption);
285         
286         /* connect to providers combo's changed signal, so we can fill the email address: */
287     g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
288             G_CALLBACK (on_combo_account_serviceprovider), self);
289         
290         /* TODO: Default to the current country somehow.
291          * But I don't know how to get the information that is specified in the 
292          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
293          *
294          * This is probably not the best choice of gconf key:
295          * This is the  "mcc used in the last pairing", ie. the last connection you made.
296      * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
297      *
298          */
299         GConfClient *client = gconf_client_get_default ();
300         GError *error = NULL;
301         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
302         gint mcc_id = gconf_client_get_int(client, key, &error);
303         
304         if(mcc_id < 0)
305         mcc_id = 0;
306      
307     if (error) {
308         g_warning ("Error getting gconf key %s:\n%s", key, error->message);
309         g_error_free (error);
310         error = NULL;
311         
312         mcc_id = 0;
313     }
314     
315     /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
316      * This might just be a Maemo bug.
317      */
318     if (mcc_id == 0) 
319     {
320         /* For now, we default to Finland when there is nothing better: */
321         mcc_id = 244;
322     }
323    
324         easysetup_country_combo_box_set_active_country_id (
325                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
326                 
327         
328         /* The description widgets: */  
329         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
330         /* Do use auto-capitalization: */
331         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
332                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
333         
334         /* Set a default account title, choosing one that does not already exist: */
335         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
336         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
337         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
338                 self->account_manager, default_account_name_start);
339         g_free (default_account_name_start);
340         default_account_name_start = NULL;
341         
342         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
343         g_free (default_account_name);
344         default_account_name = NULL;
345
346         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
347                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
348         gtk_widget_show (self->entry_account_title);
349         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
350         gtk_widget_show (caption);
351         
352         /* Prevent the use of some characters in the account title, 
353          * as required by our UI specification: */
354         GList *list_prevent = NULL;
355         list_prevent = g_list_append (list_prevent, "\\");
356         list_prevent = g_list_append (list_prevent, "/");
357         list_prevent = g_list_append (list_prevent, ":");
358         list_prevent = g_list_append (list_prevent, "*");
359         list_prevent = g_list_append (list_prevent, "?");
360         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
361         list_prevent = g_list_append (list_prevent, "“");
362         list_prevent = g_list_append (list_prevent, "<"); 
363         list_prevent = g_list_append (list_prevent, ">"); 
364         list_prevent = g_list_append (list_prevent, "|");
365         list_prevent = g_list_append (list_prevent, "^");       
366         modest_validating_entry_set_unallowed_characters (
367                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
368         g_list_free (list_prevent);
369         
370         /* Set max length as in the UI spec:
371          * The UI spec seems to want us to show a dialog if we hit the maximum. */
372         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
373         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
374                 on_entry_max, self);
375         
376         gtk_widget_show (GTK_WIDGET (box));
377         
378         return GTK_WIDGET (box);
379 }
380
381 static GtkWidget*
382 create_page_user_details (ModestEasysetupWizardDialog *self)
383 {
384         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
385         
386         /* Create a size group to be used by all captions.
387          * Note that HildonCaption does not create a default size group if we do not specify one.
388          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
389         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
390          
391         /* The name widgets: */
392         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
393         /* Auto-capitalization is the default, so let's turn it off: */
394         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
395         /* Set max length as in the UI spec:
396          * The UI spec seems to want us to show a dialog if we hit the maximum. */
397         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
398         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
399                 on_entry_max, self);
400         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
401                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
402         gtk_widget_show (self->entry_user_name);
403         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
404         gtk_widget_show (caption);
405         
406         /* Prevent the use of some characters in the name, 
407          * as required by our UI specification: */
408         GList *list_prevent = NULL;
409         list_prevent = g_list_append (list_prevent, "<");
410         list_prevent = g_list_append (list_prevent, ">");
411         modest_validating_entry_set_unallowed_characters (
412                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
413         g_list_free (list_prevent);
414         
415         /* The username widgets: */     
416         self->entry_user_username = 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_username), HILDON_GTK_INPUT_MODE_FULL);
419         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
420                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
421         gtk_widget_show (self->entry_user_username);
422         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
423         gtk_widget_show (caption);
424         
425         /* Prevent the use of some characters in the username, 
426          * as required by our UI specification: */
427         modest_validating_entry_set_unallowed_characters_whitespace (
428                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
429         
430         /* Set max length as in the UI spec:
431          * The UI spec seems to want us to show a dialog if we hit the maximum. */
432         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
433         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
434                 on_entry_max, self);
435         
436         /* The password widgets: */     
437         self->entry_user_password = gtk_entry_new ();
438         /* Auto-capitalization is the default, so let's turn it off: */
439         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), HILDON_GTK_INPUT_MODE_FULL);
440         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
441         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
442         caption = create_caption_new_with_asterix (self, sizegroup, 
443                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
444         gtk_widget_show (self->entry_user_password);
445         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
446         gtk_widget_show (caption);
447         
448         /* The email address widgets: */        
449         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
450         /* Auto-capitalization is the default, so let's turn it off: */
451         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
452         caption = create_caption_new_with_asterix (self, sizegroup, 
453                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
454         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
455         gtk_widget_show (self->entry_user_email);
456         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
457         gtk_widget_show (caption);
458         
459         /* Set max length as in the UI spec:
460          * The UI spec seems to want us to show a dialog if we hit the maximum. */
461         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
462         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
463                 on_entry_max, self);
464         
465         
466         gtk_widget_show (GTK_WIDGET (box));
467         
468         return GTK_WIDGET (box);
469 }
470
471 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
472 {
473         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
474         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
475         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
476         gtk_widget_show (label);
477         gtk_widget_show (GTK_WIDGET (box));
478         return GTK_WIDGET (box);
479 }
480
481 /** Change the caption title for the incoming server, 
482  * as specified in the UI spec:
483  */
484 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
485 {
486         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
487                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
488         const gchar* type = 
489                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
490                         _("mail_fi_emailtype_pop3") : 
491                         _("mail_fi_emailtype_imap") );
492                         
493                 
494         /* Note that this produces a compiler warning, 
495          * because the compiler does not know that the translated string will have a %s in it.
496          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
497         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
498         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
499         g_free(incomingserver_title);
500 }
501
502 /** Change the caption title for the incoming server, 
503  * as specified in the UI spec:
504  */
505 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
506 {
507         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
508                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
509         
510         /* Fill the combo with appropriately titled choices for POP or IMAP. */
511         /* The choices are the same, but the titles are different, as in the UI spec. */
512         modest_serversecurity_combo_box_fill (
513                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
514 }
515
516 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
517 {
518         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
519         update_incoming_server_title (self);
520         update_incoming_server_security_choices (self);
521 }
522            
523 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
524 {
525         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
526         
527         /* Create a size group to be used by all captions.
528          * Note that HildonCaption does not create a default size group if we do not specify one.
529          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
530         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
531          
532         /* The incoming server widgets: */
533         if (!self->combo_incoming_servertype)
534                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
535         easysetup_servertype_combo_box_set_active_servertype (
536                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
537         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
538                 _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
539         gtk_widget_show (self->combo_incoming_servertype);
540         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
541         gtk_widget_show (caption);
542         
543         if(!self->entry_incomingserver)
544                 self->entry_incomingserver = gtk_entry_new ();
545         /* Auto-capitalization is the default, so let's turn it off: */
546         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
547         set_default_custom_servernames (self);
548
549         if (self->caption_incoming)
550           gtk_widget_destroy (self->caption_incoming);
551            
552         /* The caption title will be updated in update_incoming_server_title().
553          * so this default text will never be seen: */
554         /* (Note: Changing the title seems pointless. murrayc) */
555         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
556                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
557         update_incoming_server_title (self);
558         gtk_widget_show (self->entry_incomingserver);
559         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
560         gtk_widget_show (self->caption_incoming);
561         
562         /* Change the caption title when the servertype changes, 
563          * as in the UI spec: */
564          g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
565                 G_CALLBACK (on_combo_servertype_changed), self);
566         
567         /* The secure connection widgets: */    
568         if (!self->combo_incoming_security)
569                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
570         update_incoming_server_security_choices (self);
571         modest_serversecurity_combo_box_set_active_serversecurity (
572                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_SECURITY_NONE);
573         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
574                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
575         gtk_widget_show (self->combo_incoming_security);
576         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
577         gtk_widget_show (caption);
578         
579         if(!self->checkbox_incoming_auth)
580                 self->checkbox_incoming_auth = gtk_check_button_new ();
581         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
582                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
583         gtk_widget_show (self->checkbox_incoming_auth);
584         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
585         gtk_widget_show (caption);
586         
587         gtk_widget_show (GTK_WIDGET (box));
588         
589         return GTK_WIDGET (box);
590 }
591
592 static void
593 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
594 {
595         GtkWidget *widget = GTK_WIDGET (user_data);
596         
597         /* Enable the widget only if the toggle button is active: */
598         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
599         gtk_widget_set_sensitive (widget, enable);
600 }
601
602 /* Make the sensitivity of a widget depend on a toggle button.
603  */
604 static void
605 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
606 {
607         g_signal_connect (G_OBJECT (button), "toggled",
608                 G_CALLBACK (on_toggle_button_changed), widget);
609         
610         /* Set the starting sensitivity: */
611         on_toggle_button_changed (button, widget);
612 }
613
614 static void
615 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
616 {
617         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
618         
619         /* Create the window, if necessary: */
620         if (!(self->specific_window)) {
621                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
622                 modest_connection_specific_smtp_window_fill_with_connections (
623                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
624                         NULL /* account_name, not known yet. */);
625         }
626
627         /* Show the window: */
628         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
629         gtk_widget_show (self->specific_window);
630 }
631
632 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
633 {
634         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
635         
636         /* Create a size group to be used by all captions.
637          * Note that HildonCaption does not create a default size group if we do not specify one.
638          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
639         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
640          
641         /* The outgoing server widgets: */
642         if (!self->entry_outgoingserver)
643                 self->entry_outgoingserver = gtk_entry_new ();
644         /* Auto-capitalization is the default, so let's turn it off: */
645         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
646         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
647                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
648         gtk_widget_show (self->entry_outgoingserver);
649         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
650         gtk_widget_show (caption);
651         set_default_custom_servernames (self);
652         
653         /* The secure connection widgets: */    
654         if (!self->combo_outgoing_security)
655                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
656         modest_serversecurity_combo_box_fill (
657                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
658         modest_serversecurity_combo_box_set_active_serversecurity (
659                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_SECURITY_NONE);
660         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
661                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
662         gtk_widget_show (self->combo_outgoing_security);
663         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
664         gtk_widget_show (caption);
665         
666         /* The secure authentication widgets: */
667         if (!self->combo_outgoing_auth)
668                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
669         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
670                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
671         gtk_widget_show (self->combo_outgoing_auth);
672         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
673         gtk_widget_show (caption);
674         
675         /* connection-specific checkbox: */
676         if (!self->checkbox_outgoing_smtp_specific) {
677                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
678                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
679                         FALSE);
680         }
681         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
682                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
683         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
684         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
685         gtk_widget_show (caption);
686         
687         GtkWidget *separator = gtk_hseparator_new ();
688         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
689         gtk_widget_show (separator);
690         
691         /* Connection-specific SMTP-Severs Edit button: */
692         if (!self->button_outgoing_smtp_servers)
693                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
694         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
695                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
696         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
697         gtk_widget_show (self->button_outgoing_smtp_servers);
698         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
699         gtk_widget_show (caption);
700         
701         /* Only enable the button when the checkbox is checked: */
702         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
703                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
704                 
705         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
706                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
707         
708         
709         gtk_widget_show (GTK_WIDGET (box));
710         
711         return GTK_WIDGET (box);
712 }
713
714 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
715 {
716         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
717         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
718         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
719         gtk_widget_show (label);
720         
721         if (!self->button_edit)
722                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
723         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
724                 self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
725         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
726         gtk_widget_show (self->button_edit);
727         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
728         gtk_widget_show (caption);
729         
730         gtk_widget_show (GTK_WIDGET (box));
731         return GTK_WIDGET (box);
732 }
733
734
735 /*
736  */
737 static void 
738 on_response (ModestWizardDialog *wizard_dialog,
739         gint response_id,
740         gpointer user_data)
741 {
742         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
743         invoke_enable_buttons_vfunc (self);
744 }
745
746 static void
747 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
748 {
749         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
750         
751         /* Create the notebook to be used by the ModestWizardDialog base class:
752          * Each page of the notebook will be a page of the wizard: */
753         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
754         
755     /* Set the notebook used by the ModestWizardDialog base class: */
756     g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
757     
758     /* Set the wizard title:
759      * The actual window title will be a combination of this and the page's tab label title. */
760     g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
761
762         /* Read in the information about known service providers: */
763         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
764         
765         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
766         priv->presets = modest_presets_new (filepath);
767         if (!(priv->presets)) {
768                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
769         }
770         
771         g_assert(priv->presets);
772         
773         
774         /* Get the account manager object, 
775          * so we can check for existing accounts,
776          * and create new accounts: */
777         self->account_manager = modest_runtime_get_account_mgr ();
778         g_assert (self->account_manager);
779         g_object_ref (self->account_manager);
780         
781     /* Create the common pages, 
782      */
783     self->page_welcome = create_page_welcome (self);
784         self->page_account_details = create_page_account_details (self);
785         self->page_user_details = create_page_user_details (self);
786         
787         /* Add the common pages: */
788         gtk_notebook_append_page (notebook, self->page_welcome, 
789                 gtk_label_new (_("mcen_ti_emailsetup_welcome")));
790         gtk_notebook_append_page (notebook, self->page_account_details, 
791                 gtk_label_new (_("mcen_ti_accountdetails")));
792         gtk_notebook_append_page (notebook, self->page_user_details, 
793                 gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
794                 
795         /* Create and add the easysetup-specific pages,
796          * because we need _some_ final page to enable the Next and Finish buttons: */
797         create_subsequent_easysetup_pages (self);
798
799             
800     /* Connect to the dialog's response signal so we can enable/disable buttons 
801      * for the newly-selected page, because the prev/next buttons cause response to be emitted.
802      * Note that we use g_signal_connect_after() instead of g_signal_connect()
803      * so that we can be enable/disable after ModestWizardDialog has done its own 
804      * enabling/disabling of buttons.
805      * 
806      * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
807      * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
808      * 
809      * It's not enough to connect to the notebook's switch-page signal, because 
810      * ModestWizardDialog's "response" signal handler enables the buttons itself, 
811      * _after_ switching the page (understandably).
812      * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
813      * then gtk_notebook_get_current_page() would return an incorrect value.)
814      */
815     g_signal_connect_after (G_OBJECT (self), "response",
816             G_CALLBACK (on_response), self);       
817 }
818
819 ModestEasysetupWizardDialog*
820 modest_easysetup_wizard_dialog_new (void)
821 {
822         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
823 }
824
825 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
826 {
827         GtkNotebook *notebook = NULL;
828         g_object_get (self, "wizard-notebook", &notebook, NULL);
829         g_assert(notebook);
830         
831         /* Create the custom pages: */
832         if(!(self->page_custom_incoming)) {
833                 self->page_custom_incoming = create_page_custom_incoming (self);
834         }
835                 
836         if(!(self->page_custom_outgoing)) {
837                 self->page_custom_outgoing = create_page_custom_outgoing (self);
838         }
839         
840         if(!(self->page_complete_customsetup)) {
841                 self->page_complete_customsetup = create_page_complete_custom (self);
842         }
843         
844         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
845                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
846                         gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
847         
848         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
849                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
850                         gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
851                 
852         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
853                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
854                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
855 }
856         
857 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
858 {
859         GtkNotebook *notebook = NULL;
860         g_object_get (self, "wizard-notebook", &notebook, NULL);
861         g_assert(notebook);
862         
863         /* Create the easysetup-specific pages: */
864         if(!self->page_complete_easysetup)
865                 self->page_complete_easysetup = create_page_complete_easysetup (self);
866
867         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
868                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
869                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
870                         
871 }
872 /* After the user details page,
873  * the following pages depend on whether "Other" was chosen 
874  * in the provider combobox on the account page
875  */
876 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
877 {
878         if (easysetup_provider_combo_box_get_active_provider_id (
879                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
880                 /* "Other..." was selected: */
881                 
882                 /* Make sure that the easysetup pages do not exist: */
883                 if(self->page_complete_easysetup) {
884                         gtk_widget_destroy (self->page_complete_easysetup);
885                         self->page_complete_easysetup = NULL;
886                 }
887                 
888                 create_subsequent_customsetup_pages (self);
889         }       
890         else {
891                 /* A specific provider was selected: */
892                 {
893                         /* Make sure that the custom pages do not exist:
894                          * Because they will be used if they exist, when creating the account. */
895                         if(self->page_custom_incoming) {
896                                 gtk_widget_destroy (self->page_custom_incoming);
897                                 self->page_custom_incoming = NULL;
898                         }
899                         
900                         if(self->page_custom_outgoing) {
901                                 gtk_widget_destroy (self->page_custom_outgoing);
902                                 self->page_custom_outgoing = NULL;
903                         }
904                         
905                         if(self->page_complete_customsetup) {
906                                 gtk_widget_destroy (self->page_complete_customsetup);
907                                 self->page_complete_customsetup = NULL;
908                         }
909                 }
910                 
911                 /* Create the easysetup pages: */
912                 create_subsequent_easysetup_pages (self);
913         }
914 }
915
916
917 static gchar*
918 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
919 {
920         if (!email_address)
921                 return NULL;
922         
923         gchar* at = g_utf8_strchr (email_address, -1, '@');
924         if (!at || (g_utf8_strlen (at, -1) < 2))
925                 return NULL;
926                 
927         gchar* domain = g_utf8_next_char (at);
928         if(!domain)
929                 return NULL;
930                 
931         const gchar* hostname = NULL;
932         if (servertype == MODEST_PROTOCOL_STORE_POP)
933                 hostname = "pop";
934         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
935                 hostname = "imap";
936         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
937                 hostname = "smtp";
938         
939         if (!hostname)
940                 return NULL;
941                 
942         return g_strdup_printf ("%s.%s", hostname, domain);
943 }
944
945 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
946 {
947         if (!account_wizard->entry_incomingserver)
948                 return;
949                 
950         /* Set a default domain for the server, based on the email address,
951          * if no server name was already specified.
952          */
953         const gchar* incoming_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_incomingserver));
954         if ((!incoming_existing || (strlen(incoming_existing) == 0)) 
955                 && account_wizard->entry_user_email) {
956                 const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
957                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
958                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
959                 
960                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
961                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
962                 g_free (servername);
963         }
964         
965         /* Set a default domain for the server, based on the email address,
966          * if no server name was already specified.
967          */
968         if (!account_wizard->entry_outgoingserver)
969                 return;
970                 
971         const gchar* outgoing_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_outgoingserver));
972         if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
973                 && account_wizard->entry_user_email) {
974                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
975                 
976                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
977                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
978                 g_free (servername);
979         }
980 }
981
982 static gboolean
983 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
984 {
985         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
986         
987         /* Do extra validation that couldn't be done for every key press,
988          * either because it was too slow,
989          * or because it requires interaction:
990          */
991         if (current_page == account_wizard->page_account_details) {     
992                 /* Check that the title is not already in use: */
993                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
994                 if ((!account_title) || (strlen(account_title) == 0))
995                         return FALSE;
996                         
997                 /* Aavoid a clash with an existing display name: */
998                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
999                         account_wizard->account_manager, account_title);
1000
1001                 if (name_in_use) {
1002                         /* Warn the user via a dialog: */
1003                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
1004             
1005                         return FALSE;
1006                 }
1007         }
1008         else if (current_page == account_wizard->page_user_details) {
1009                 /* Check that the email address is valud: */
1010                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1011                 if ((!email_address) || (strlen(email_address) == 0))
1012                         return FALSE;
1013                         
1014                 if (!modest_text_utils_validate_email_address (email_address)) {
1015                         /* Warn the user via a dialog: */
1016                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
1017                                              
1018             /* Return focus to the email address entry: */
1019             gtk_widget_grab_focus (account_wizard->entry_user_email);
1020             
1021                         return FALSE;
1022                 }
1023                 
1024                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1025                 create_subsequent_pages (account_wizard);
1026         }
1027         
1028         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1029          * but does not specify how.
1030          */
1031           
1032         if(next_page == account_wizard->page_custom_incoming) {
1033                 set_default_custom_servernames (account_wizard);
1034         }
1035         else if (next_page == account_wizard->page_custom_outgoing) {
1036                 set_default_custom_servernames (account_wizard);
1037         }
1038         
1039         /* If this is the last page, and this is a click on Finish, 
1040          * then attempt to create the dialog.
1041          */
1042         if(!next_page) /* This is NULL when this is a click on Finish. */
1043         {
1044                 create_account (account_wizard);
1045         }
1046         
1047         
1048         return TRUE;
1049 }
1050
1051 static gboolean entry_is_empty (GtkWidget *entry)
1052 {
1053         if (!entry)
1054                 return FALSE;
1055                 
1056         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1057         if ((!text) || (strlen(text) == 0))
1058                 return TRUE;
1059         else
1060                 return FALSE;
1061 }
1062
1063 static void
1064 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1065 {
1066         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1067         
1068         gboolean enable_next = TRUE;
1069         if (current_page == account_wizard->page_welcome) {
1070                 enable_next = TRUE;
1071         }
1072         else if (current_page == account_wizard->page_account_details) {
1073                 /* The account details title is mandatory: */
1074                 if (entry_is_empty(account_wizard->entry_account_title))
1075                         enable_next = FALSE;
1076         }
1077         else if (current_page == account_wizard->page_user_details) {   
1078                 /* The user details username is mandatory: */
1079                 if (entry_is_empty(account_wizard->entry_user_username))
1080                         enable_next = FALSE;
1081                         
1082                 /* The user details email address is mandatory: */
1083                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1084                         enable_next = FALSE;
1085         }
1086         else if (current_page == account_wizard->page_custom_incoming) {
1087                 /* The custom incoming server is mandatory: */
1088                 if (entry_is_empty(account_wizard->entry_incomingserver))
1089                         enable_next = FALSE;
1090         }
1091                         
1092         /* Enable the buttons, 
1093          * identifying them via their associated response codes:
1094          */
1095         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1096     gtk_dialog_set_response_sensitive (dialog_base,
1097                                        MODEST_WIZARD_DIALOG_NEXT,
1098                                        enable_next);
1099                                        
1100     /* Disable the Finish button until we are on the last page,
1101      * because HildonWizardDialog enables this for all but the first page: */
1102     GtkNotebook *notebook = NULL;
1103         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1104         
1105     gint current = gtk_notebook_get_current_page (notebook);
1106     gint last = gtk_notebook_get_n_pages (notebook) - 1;
1107     gboolean is_last = (current == last);
1108     
1109     if(!is_last) {
1110         gtk_dialog_set_response_sensitive (dialog_base,
1111                                        MODEST_WIZARD_DIALOG_FINISH,
1112                                        FALSE);
1113     }
1114 }
1115
1116 static void
1117 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1118 {
1119         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1120         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1121
1122
1123         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1124         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1125         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1126         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1127         
1128         /* Provide a vfunc implementation so we can decide 
1129          * when to enable/disable the prev/next buttons.
1130          */
1131         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1132         base_klass->before_next = on_before_next;
1133         base_klass->enable_buttons = on_enable_buttons;
1134 }
1135  
1136 static void
1137 show_error (GtkWindow *parent_window, const gchar* text)
1138 {
1139         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1140                 (GtkDialogFlags)0,
1141                  GTK_MESSAGE_ERROR,
1142                  GTK_BUTTONS_OK,
1143                  text ));
1144                  
1145                  gtk_dialog_run (dialog);
1146                  gtk_widget_destroy (GTK_WIDGET (dialog));
1147 }
1148
1149 /** Attempt to create the account from the information that the user has entered.
1150  * @result: TRUE if the account was successfully created.
1151  */
1152 gboolean
1153 create_account (ModestEasysetupWizardDialog *self)
1154 {
1155         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1156         
1157         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1158
1159         /* Some checks: */
1160         if (!display_name)
1161                 return FALSE;
1162                 
1163         /* We should have checked for this already, 
1164          * and changed that name accordingly, 
1165          * but let's check again just in case:
1166          */
1167         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1168                 return FALSE;
1169
1170         /* Increment the non-user visible name if necessary, 
1171          * based on the display name: */
1172         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1173         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1174                 account_name_start, FALSE /* not a server account */);
1175         g_free (account_name_start);
1176                 
1177         /* username and password (for both incoming and outgoing): */
1178         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1179         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1180         
1181         /* Incoming server: */
1182         /* Note: We need something as default for the ModestProtocol values, 
1183          * or modest_account_mgr_add_server_account will fail. */
1184         gchar* servername_incoming = NULL;
1185         ModestProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1186         ModestProtocol protocol_security_incoming = MODEST_PROTOCOL_SECURITY_NONE;
1187         ModestProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1188         
1189         /* Get details from the specified presets: */
1190         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1191                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1192         if(provider_id) {
1193                 /* Use presets: */
1194                 
1195                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1196                         TRUE /* incoming */);
1197                 
1198                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets, provider_id, 
1199                         TRUE /* incoming */);
1200                 
1201         
1202                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1203                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP)
1204                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1205                 else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP)
1206                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1207                                 
1208                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets, provider_id, 
1209                         TRUE /* incoming */);
1210                         
1211                 
1212                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1213                         protocol_security_incoming = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1214                 
1215                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1216                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1217         }
1218         else {
1219                 /* Use custom pages because no preset was specified: */
1220                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1221                 
1222                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1223                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1224                 
1225                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1226                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1227                 
1228                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1229                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1230                         : MODEST_PROTOCOL_AUTH_NONE;
1231                 
1232         }
1233         
1234         /* First we add the 2 server accounts, and then we add the account that uses them.
1235          * If we don't do it in this order then we will experience a crash. */
1236          
1237         /* Add a (incoming) server account, to be used by the account: */
1238         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1239         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1240                 store_name_start, TRUE /* server account */);
1241         g_free (store_name_start);
1242         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1243                 store_name,
1244                 servername_incoming,
1245                 username, password,
1246                 protocol_incoming,
1247                 protocol_security_incoming,
1248                 protocol_authentication_incoming);              
1249                 
1250         g_free (servername_incoming);
1251         
1252         if (!created) {
1253                 /* TODO: Provide a Logical ID for the text: */
1254                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1255                 return FALSE;   
1256         }
1257         
1258         /* Sanity check: */
1259         /* There must be at least one account now: */
1260         GSList *account_names = modest_account_mgr_account_names (self->account_manager);
1261         if(!account_names)
1262         {
1263                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1264         }
1265         g_slist_free (account_names);
1266         
1267         
1268         /* Outgoing server: */
1269         gchar* servername_outgoing = NULL;
1270         ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1271         ModestProtocol protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1272         ModestProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1273         
1274         if(provider_id) {
1275                 /* Use presets: */
1276                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1277                         FALSE /* incoming */);
1278                         
1279                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets, provider_id, 
1280                         FALSE /* incoming */);
1281                 
1282                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1283                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1284                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1285                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1286                 
1287                 ModestPresetsSecurity security_outgoing = 
1288                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1289                                 FALSE /* incoming */);
1290                         
1291                 protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1292                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP)
1293                         protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1294                 
1295                 protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1296                 /* TODO: There is no SMTP authentication enum for presets. */
1297         }
1298         else {
1299                 /* Use custom pages because no preset was specified: */
1300                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1301                 
1302                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1303
1304                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1305                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1306                 
1307                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1308                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1309         }
1310             
1311         /* Add a (outgoing) server account to be used by the account: */
1312         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1313         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1314                 transport_name_start, TRUE /* server account */);
1315         g_free (transport_name_start);
1316         created = modest_account_mgr_add_server_account (self->account_manager,
1317                 transport_name,
1318                 servername_outgoing,
1319                 username, password,
1320                 protocol_outgoing,
1321                 protocol_security_outgoing,
1322                 protocol_authentication_outgoing);
1323                 
1324         g_free (servername_outgoing);
1325                 
1326         if (!created) {
1327                 /* TODO: Provide a Logical ID for the text: */
1328                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1329                 return FALSE;   
1330         }
1331         
1332         
1333         /* Create the account, which will contain the two "server accounts": */
1334         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1335                 store_name, /* The name of our POP/IMAP server account. */
1336                 transport_name /* The name of our SMTP server account. */);
1337         g_free (store_name);
1338         g_free (transport_name);
1339         
1340         if (!created) {
1341                 /* TODO: Provide a Logical ID for the text: */
1342                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1343                 return FALSE;   
1344         }
1345         
1346         /* The user name and email address must be set additionally: */
1347         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1348         modest_account_mgr_set_string (self->account_manager, account_name,
1349                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1350
1351         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1352         modest_account_mgr_set_string (self->account_manager, account_name,
1353                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1354
1355         /* Set the display name: */
1356         modest_account_mgr_set_string (self->account_manager, account_name,
1357                 MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1358
1359         /* Save the connection-specific SMTP server accounts. */
1360         if (self->specific_window)
1361                 return modest_connection_specific_smtp_window_save_server_accounts (
1362                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1363                         
1364         g_free (account_name);
1365
1366         return FALSE;
1367 }
1368
1369