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         
331         /* Set a default account title, choosing one that does not already exist: */
332         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
333         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
334         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
335                 self->account_manager, default_account_name_start);
336         g_free (default_account_name_start);
337         default_account_name_start = NULL;
338         
339         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
340         g_free (default_account_name);
341         default_account_name = NULL;
342
343         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
344                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
345         gtk_widget_show (self->entry_account_title);
346         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
347         gtk_widget_show (caption);
348         
349         /* Prevent the use of some characters in the account title, 
350          * as required by our UI specification: */
351         GList *list_prevent = NULL;
352         list_prevent = g_list_append (list_prevent, "\\");
353         list_prevent = g_list_append (list_prevent, "/");
354         list_prevent = g_list_append (list_prevent, ":");
355         list_prevent = g_list_append (list_prevent, "*");
356         list_prevent = g_list_append (list_prevent, "?");
357         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
358         list_prevent = g_list_append (list_prevent, "“");
359         list_prevent = g_list_append (list_prevent, "<"); 
360         list_prevent = g_list_append (list_prevent, ">"); 
361         list_prevent = g_list_append (list_prevent, "|");
362         list_prevent = g_list_append (list_prevent, "^");       
363         modest_validating_entry_set_unallowed_characters (
364                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
365         g_list_free (list_prevent);
366         
367         /* Set max length as in the UI spec:
368          * The UI spec seems to want us to show a dialog if we hit the maximum. */
369         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
370         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
371                 on_entry_max, self);
372         
373         gtk_widget_show (GTK_WIDGET (box));
374         
375         return GTK_WIDGET (box);
376 }
377
378 static GtkWidget*
379 create_page_user_details (ModestEasysetupWizardDialog *self)
380 {
381         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
382         
383         /* Create a size group to be used by all captions.
384          * Note that HildonCaption does not create a default size group if we do not specify one.
385          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
386         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
387          
388         /* The name widgets: */
389         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
390         /* Auto-capitalization is the default, so let's turn it off: */
391         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
392         /* Set max length as in the UI spec:
393          * The UI spec seems to want us to show a dialog if we hit the maximum. */
394         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
395         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
396                 on_entry_max, self);
397         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
398                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
399         gtk_widget_show (self->entry_user_name);
400         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
401         gtk_widget_show (caption);
402         
403         /* Prevent the use of some characters in the name, 
404          * as required by our UI specification: */
405         GList *list_prevent = NULL;
406         list_prevent = g_list_append (list_prevent, "<");
407         list_prevent = g_list_append (list_prevent, ">");
408         modest_validating_entry_set_unallowed_characters (
409                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
410         g_list_free (list_prevent);
411         
412         /* The username widgets: */     
413         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
414         /* Auto-capitalization is the default, so let's turn it off: */
415         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
416         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
417                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
418         gtk_widget_show (self->entry_user_username);
419         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
420         gtk_widget_show (caption);
421         
422         /* Prevent the use of some characters in the username, 
423          * as required by our UI specification: */
424         modest_validating_entry_set_unallowed_characters_whitespace (
425                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
426         
427         /* Set max length as in the UI spec:
428          * The UI spec seems to want us to show a dialog if we hit the maximum. */
429         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
430         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
431                 on_entry_max, self);
432         
433         /* The password widgets: */     
434         self->entry_user_password = gtk_entry_new ();
435         /* Auto-capitalization is the default, so let's turn it off: */
436         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), HILDON_GTK_INPUT_MODE_FULL);
437         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
438         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
439         caption = create_caption_new_with_asterix (self, sizegroup, 
440                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
441         gtk_widget_show (self->entry_user_password);
442         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
443         gtk_widget_show (caption);
444         
445         /* The email address widgets: */        
446         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
447         /* Auto-capitalization is the default, so let's turn it off: */
448         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
449         caption = create_caption_new_with_asterix (self, sizegroup, 
450                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
451         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
452         gtk_widget_show (self->entry_user_email);
453         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
454         gtk_widget_show (caption);
455         
456         /* Set max length as in the UI spec:
457          * The UI spec seems to want us to show a dialog if we hit the maximum. */
458         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
459         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
460                 on_entry_max, self);
461         
462         
463         gtk_widget_show (GTK_WIDGET (box));
464         
465         return GTK_WIDGET (box);
466 }
467
468 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
469 {
470         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
471         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
472         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
473         gtk_widget_show (label);
474         gtk_widget_show (GTK_WIDGET (box));
475         return GTK_WIDGET (box);
476 }
477
478 /** Change the caption title for the incoming server, 
479  * as specified in the UI spec:
480  */
481 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
482 {
483         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
484                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
485         const gchar* type = 
486                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
487                         _("mail_fi_emailtype_pop3") : 
488                         _("mail_fi_emailtype_imap") );
489                         
490                 
491         /* Note that this produces a compiler warning, 
492          * because the compiler does not know that the translated string will have a %s in it.
493          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
494         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
495         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
496         g_free(incomingserver_title);
497 }
498
499 /** Change the caption title for the incoming server, 
500  * as specified in the UI spec:
501  */
502 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
503 {
504         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
505                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
506         
507         /* Fill the combo with appropriately titled choices for POP or IMAP. */
508         /* The choices are the same, but the titles are different, as in the UI spec. */
509         modest_serversecurity_combo_box_fill (
510                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
511 }
512
513 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
514 {
515         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
516         update_incoming_server_title (self);
517         update_incoming_server_security_choices (self);
518 }
519            
520 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
521 {
522         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
523         
524         /* Create a size group to be used by all captions.
525          * Note that HildonCaption does not create a default size group if we do not specify one.
526          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
527         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
528          
529         /* The incoming server widgets: */
530         if (!self->combo_incoming_servertype)
531                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
532         easysetup_servertype_combo_box_set_active_servertype (
533                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
534         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
535                 _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
536         gtk_widget_show (self->combo_incoming_servertype);
537         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
538         gtk_widget_show (caption);
539         
540         if(!self->entry_incomingserver)
541                 self->entry_incomingserver = gtk_entry_new ();
542         /* Auto-capitalization is the default, so let's turn it off: */
543         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
544         set_default_custom_servernames (self);
545
546         if (self->caption_incoming)
547           gtk_widget_destroy (self->caption_incoming);
548            
549         /* The caption title will be updated in update_incoming_server_title().
550          * so this default text will never be seen: */
551         /* (Note: Changing the title seems pointless. murrayc) */
552         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
553                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
554         update_incoming_server_title (self);
555         gtk_widget_show (self->entry_incomingserver);
556         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
557         gtk_widget_show (self->caption_incoming);
558         
559         /* Change the caption title when the servertype changes, 
560          * as in the UI spec: */
561          g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
562                 G_CALLBACK (on_combo_servertype_changed), self);
563         
564         /* The secure connection widgets: */    
565         if (!self->combo_incoming_security)
566                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
567         update_incoming_server_security_choices (self);
568         modest_serversecurity_combo_box_set_active_serversecurity (
569                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_SECURITY_NONE);
570         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
571                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
572         gtk_widget_show (self->combo_incoming_security);
573         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
574         gtk_widget_show (caption);
575         
576         if(!self->checkbox_incoming_auth)
577                 self->checkbox_incoming_auth = gtk_check_button_new ();
578         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
579                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
580         gtk_widget_show (self->checkbox_incoming_auth);
581         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
582         gtk_widget_show (caption);
583         
584         gtk_widget_show (GTK_WIDGET (box));
585         
586         return GTK_WIDGET (box);
587 }
588
589 static void
590 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
591 {
592         GtkWidget *widget = GTK_WIDGET (user_data);
593         
594         /* Enable the widget only if the toggle button is active: */
595         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
596         gtk_widget_set_sensitive (widget, enable);
597 }
598
599 /* Make the sensitivity of a widget depend on a toggle button.
600  */
601 static void
602 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
603 {
604         g_signal_connect (G_OBJECT (button), "toggled",
605                 G_CALLBACK (on_toggle_button_changed), widget);
606         
607         /* Set the starting sensitivity: */
608         on_toggle_button_changed (button, widget);
609 }
610
611 static void
612 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
613 {
614         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
615         
616         /* Create the window, if necessary: */
617         if (!(self->specific_window)) {
618                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
619                 modest_connection_specific_smtp_window_fill_with_connections (
620                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
621                         NULL /* account_name, not known yet. */);
622         }
623
624         /* Show the window: */
625         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
626         gtk_widget_show (self->specific_window);
627 }
628
629 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
630 {
631         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
632         
633         /* Create a size group to be used by all captions.
634          * Note that HildonCaption does not create a default size group if we do not specify one.
635          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
636         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
637          
638         /* The outgoing server widgets: */
639         if (!self->entry_outgoingserver)
640                 self->entry_outgoingserver = gtk_entry_new ();
641         /* Auto-capitalization is the default, so let's turn it off: */
642         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
643         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
644                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
645         gtk_widget_show (self->entry_outgoingserver);
646         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
647         gtk_widget_show (caption);
648         set_default_custom_servernames (self);
649         
650         /* The secure connection widgets: */    
651         if (!self->combo_outgoing_security)
652                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
653         modest_serversecurity_combo_box_fill (
654                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
655         modest_serversecurity_combo_box_set_active_serversecurity (
656                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_SECURITY_NONE);
657         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
658                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
659         gtk_widget_show (self->combo_outgoing_security);
660         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
661         gtk_widget_show (caption);
662         
663         /* The secure authentication widgets: */
664         if (!self->combo_outgoing_auth)
665                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
666         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
667                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
668         gtk_widget_show (self->combo_outgoing_auth);
669         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
670         gtk_widget_show (caption);
671         
672         /* connection-specific checkbox: */
673         if (!self->checkbox_outgoing_smtp_specific) {
674                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
675                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
676                         FALSE);
677         }
678         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
679                 self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
680         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
681         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
682         gtk_widget_show (caption);
683         
684         GtkWidget *separator = gtk_hseparator_new ();
685         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
686         gtk_widget_show (separator);
687         
688         /* Connection-specific SMTP-Severs Edit button: */
689         if (!self->button_outgoing_smtp_servers)
690                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
691         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
692                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
693         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
694         gtk_widget_show (self->button_outgoing_smtp_servers);
695         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
696         gtk_widget_show (caption);
697         
698         /* Only enable the button when the checkbox is checked: */
699         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
700                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
701                 
702         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
703                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
704         
705         
706         gtk_widget_show (GTK_WIDGET (box));
707         
708         return GTK_WIDGET (box);
709 }
710
711 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
712 {
713         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
714         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
715         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
716         gtk_widget_show (label);
717         
718         if (!self->button_edit)
719                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
720         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
721                 self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
722         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
723         gtk_widget_show (self->button_edit);
724         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
725         gtk_widget_show (caption);
726         
727         gtk_widget_show (GTK_WIDGET (box));
728         return GTK_WIDGET (box);
729 }
730
731
732 /*
733  */
734 static void 
735 on_response (ModestWizardDialog *wizard_dialog,
736         gint response_id,
737         gpointer user_data)
738 {
739         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
740         invoke_enable_buttons_vfunc (self);
741 }
742
743 static void
744 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
745 {
746         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
747         
748         /* Create the notebook to be used by the ModestWizardDialog base class:
749          * Each page of the notebook will be a page of the wizard: */
750         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
751         
752     /* Set the notebook used by the ModestWizardDialog base class: */
753     g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
754     
755     /* Set the wizard title:
756      * The actual window title will be a combination of this and the page's tab label title. */
757     g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
758
759         /* Read in the information about known service providers: */
760         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
761         
762         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
763         priv->presets = modest_presets_new (filepath);
764         if (!(priv->presets)) {
765                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
766         }
767         
768         g_assert(priv->presets);
769         
770         
771         /* Get the account manager object, 
772          * so we can check for existing accounts,
773          * and create new accounts: */
774         self->account_manager = modest_runtime_get_account_mgr ();
775         g_assert (self->account_manager);
776         g_object_ref (self->account_manager);
777         
778     /* Create the common pages, 
779      */
780     self->page_welcome = create_page_welcome (self);
781         self->page_account_details = create_page_account_details (self);
782         self->page_user_details = create_page_user_details (self);
783         
784         /* Add the common pages: */
785         gtk_notebook_append_page (notebook, self->page_welcome, 
786                 gtk_label_new (_("mcen_ti_emailsetup_welcome")));
787         gtk_notebook_append_page (notebook, self->page_account_details, 
788                 gtk_label_new (_("mcen_ti_accountdetails")));
789         gtk_notebook_append_page (notebook, self->page_user_details, 
790                 gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
791                 
792         /* Create and add the easysetup-specific pages,
793          * because we need _some_ final page to enable the Next and Finish buttons: */
794         create_subsequent_easysetup_pages (self);
795
796             
797     /* Connect to the dialog's response signal so we can enable/disable buttons 
798      * for the newly-selected page, because the prev/next buttons cause response to be emitted.
799      * Note that we use g_signal_connect_after() instead of g_signal_connect()
800      * so that we can be enable/disable after ModestWizardDialog has done its own 
801      * enabling/disabling of buttons.
802      * 
803      * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
804      * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
805      * 
806      * It's not enough to connect to the notebook's switch-page signal, because 
807      * ModestWizardDialog's "response" signal handler enables the buttons itself, 
808      * _after_ switching the page (understandably).
809      * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
810      * then gtk_notebook_get_current_page() would return an incorrect value.)
811      */
812     g_signal_connect_after (G_OBJECT (self), "response",
813             G_CALLBACK (on_response), self);       
814 }
815
816 ModestEasysetupWizardDialog*
817 modest_easysetup_wizard_dialog_new (void)
818 {
819         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
820 }
821
822 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
823 {
824         GtkNotebook *notebook = NULL;
825         g_object_get (self, "wizard-notebook", &notebook, NULL);
826         g_assert(notebook);
827         
828         /* Create the custom pages: */
829         if(!(self->page_custom_incoming)) {
830                 self->page_custom_incoming = create_page_custom_incoming (self);
831         }
832                 
833         if(!(self->page_custom_outgoing)) {
834                 self->page_custom_outgoing = create_page_custom_outgoing (self);
835         }
836         
837         if(!(self->page_complete_customsetup)) {
838                 self->page_complete_customsetup = create_page_complete_custom (self);
839         }
840         
841         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
842                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
843                         gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
844         
845         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
846                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
847                         gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
848                 
849         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
850                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
851                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
852 }
853         
854 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
855 {
856         GtkNotebook *notebook = NULL;
857         g_object_get (self, "wizard-notebook", &notebook, NULL);
858         g_assert(notebook);
859         
860         /* Create the easysetup-specific pages: */
861         if(!self->page_complete_easysetup)
862                 self->page_complete_easysetup = create_page_complete_easysetup (self);
863
864         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
865                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
866                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
867                         
868 }
869 /* After the user details page,
870  * the following pages depend on whether "Other" was chosen 
871  * in the provider combobox on the account page
872  */
873 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
874 {
875         if (easysetup_provider_combo_box_get_active_provider_id (
876                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
877                 /* "Other..." was selected: */
878                 
879                 /* Make sure that the easysetup pages do not exist: */
880                 if(self->page_complete_easysetup) {
881                         gtk_widget_destroy (self->page_complete_easysetup);
882                         self->page_complete_easysetup = NULL;
883                 }
884                 
885                 create_subsequent_customsetup_pages (self);
886         }       
887         else {
888                 /* A specific provider was selected: */
889                 {
890                         /* Make sure that the custom pages do not exist:
891                          * Because they will be used if they exist, when creating the account. */
892                         if(self->page_custom_incoming) {
893                                 gtk_widget_destroy (self->page_custom_incoming);
894                                 self->page_custom_incoming = NULL;
895                         }
896                         
897                         if(self->page_custom_outgoing) {
898                                 gtk_widget_destroy (self->page_custom_outgoing);
899                                 self->page_custom_outgoing = NULL;
900                         }
901                         
902                         if(self->page_complete_customsetup) {
903                                 gtk_widget_destroy (self->page_complete_customsetup);
904                                 self->page_complete_customsetup = NULL;
905                         }
906                 }
907                 
908                 /* Create the easysetup pages: */
909                 create_subsequent_easysetup_pages (self);
910         }
911 }
912
913
914 static gchar*
915 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
916 {
917         if (!email_address)
918                 return NULL;
919         
920         gchar* at = g_utf8_strchr (email_address, -1, '@');
921         if (!at || (g_utf8_strlen (at, -1) < 2))
922                 return NULL;
923                 
924         gchar* domain = g_utf8_next_char (at);
925         if(!domain)
926                 return NULL;
927                 
928         const gchar* hostname = NULL;
929         if (servertype == MODEST_PROTOCOL_STORE_POP)
930                 hostname = "pop";
931         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
932                 hostname = "imap";
933         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
934                 hostname = "smtp";
935         
936         if (!hostname)
937                 return NULL;
938                 
939         return g_strdup_printf ("%s.%s", hostname, domain);
940 }
941
942 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
943 {
944         if (!account_wizard->entry_incomingserver)
945                 return;
946                 
947         /* Set a default domain for the server, based on the email address,
948          * if no server name was already specified.
949          */
950         const gchar* incoming_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_incomingserver));
951         if ((!incoming_existing || (strlen(incoming_existing) == 0)) 
952                 && account_wizard->entry_user_email) {
953                 const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
954                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
955                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
956                 
957                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
958                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
959                 g_free (servername);
960         }
961         
962         /* Set a default domain for the server, based on the email address,
963          * if no server name was already specified.
964          */
965         if (!account_wizard->entry_outgoingserver)
966                 return;
967                 
968         const gchar* outgoing_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_outgoingserver));
969         if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
970                 && account_wizard->entry_user_email) {
971                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
972                 
973                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
974                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
975                 g_free (servername);
976         }
977 }
978
979 static gboolean
980 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
981 {
982         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
983         
984         /* Do extra validation that couldn't be done for every key press,
985          * either because it was too slow,
986          * or because it requires interaction:
987          */
988         if (current_page == account_wizard->page_account_details) {     
989                 /* Check that the title is not already in use: */
990                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
991                 if ((!account_title) || (strlen(account_title) == 0))
992                         return FALSE;
993                         
994                 /* Aavoid a clash with an existing display name: */
995                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
996                         account_wizard->account_manager, account_title);
997
998                 if (name_in_use) {
999                         /* Warn the user via a dialog: */
1000                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
1001             
1002                         return FALSE;
1003                 }
1004         }
1005         else if (current_page == account_wizard->page_user_details) {
1006                 /* Check that the email address is valud: */
1007                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1008                 if ((!email_address) || (strlen(email_address) == 0))
1009                         return FALSE;
1010                         
1011                 if (!modest_text_utils_validate_email_address (email_address)) {
1012                         /* Warn the user via a dialog: */
1013                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
1014                                              
1015             /* Return focus to the email address entry: */
1016             gtk_widget_grab_focus (account_wizard->entry_user_email);
1017             
1018                         return FALSE;
1019                 }
1020                 
1021                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1022                 create_subsequent_pages (account_wizard);
1023         }
1024         
1025         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1026          * but does not specify how.
1027          */
1028           
1029         if(next_page == account_wizard->page_custom_incoming) {
1030                 set_default_custom_servernames (account_wizard);
1031         }
1032         else if (next_page == account_wizard->page_custom_outgoing) {
1033                 set_default_custom_servernames (account_wizard);
1034         }
1035         
1036         /* If this is the last page, and this is a click on Finish, 
1037          * then attempt to create the dialog.
1038          */
1039         if(!next_page) /* This is NULL when this is a click on Finish. */
1040         {
1041                 create_account (account_wizard);
1042         }
1043         
1044         
1045         return TRUE;
1046 }
1047
1048 static gboolean entry_is_empty (GtkWidget *entry)
1049 {
1050         if (!entry)
1051                 return FALSE;
1052                 
1053         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1054         if ((!text) || (strlen(text) == 0))
1055                 return TRUE;
1056         else
1057                 return FALSE;
1058 }
1059
1060 static void
1061 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1062 {
1063         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1064         
1065         gboolean enable_next = TRUE;
1066         if (current_page == account_wizard->page_welcome) {
1067                 enable_next = TRUE;
1068         }
1069         else if (current_page == account_wizard->page_account_details) {
1070                 /* The account details title is mandatory: */
1071                 if (entry_is_empty(account_wizard->entry_account_title))
1072                         enable_next = FALSE;
1073         }
1074         else if (current_page == account_wizard->page_user_details) {   
1075                 /* The user details username is mandatory: */
1076                 if (entry_is_empty(account_wizard->entry_user_username))
1077                         enable_next = FALSE;
1078                         
1079                 /* The user details email address is mandatory: */
1080                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1081                         enable_next = FALSE;
1082         }
1083         else if (current_page == account_wizard->page_custom_incoming) {
1084                 /* The custom incoming server is mandatory: */
1085                 if (entry_is_empty(account_wizard->entry_incomingserver))
1086                         enable_next = FALSE;
1087         }
1088                         
1089         /* Enable the buttons, 
1090          * identifying them via their associated response codes:
1091          */
1092         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1093     gtk_dialog_set_response_sensitive (dialog_base,
1094                                        MODEST_WIZARD_DIALOG_NEXT,
1095                                        enable_next);
1096                                        
1097     /* Disable the Finish button until we are on the last page,
1098      * because HildonWizardDialog enables this for all but the first page: */
1099     GtkNotebook *notebook = NULL;
1100         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1101         
1102     gint current = gtk_notebook_get_current_page (notebook);
1103     gint last = gtk_notebook_get_n_pages (notebook) - 1;
1104     gboolean is_last = (current == last);
1105     
1106     if(!is_last) {
1107         gtk_dialog_set_response_sensitive (dialog_base,
1108                                        MODEST_WIZARD_DIALOG_FINISH,
1109                                        FALSE);
1110     }
1111 }
1112
1113 static void
1114 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1115 {
1116         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1117         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1118
1119
1120         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1121         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1122         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1123         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1124         
1125         /* Provide a vfunc implementation so we can decide 
1126          * when to enable/disable the prev/next buttons.
1127          */
1128         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1129         base_klass->before_next = on_before_next;
1130         base_klass->enable_buttons = on_enable_buttons;
1131 }
1132  
1133 static void
1134 show_error (GtkWindow *parent_window, const gchar* text)
1135 {
1136         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1137                 (GtkDialogFlags)0,
1138                  GTK_MESSAGE_ERROR,
1139                  GTK_BUTTONS_OK,
1140                  text ));
1141                  
1142                  gtk_dialog_run (dialog);
1143                  gtk_widget_destroy (GTK_WIDGET (dialog));
1144 }
1145
1146 /** Attempt to create the account from the information that the user has entered.
1147  * @result: TRUE if the account was successfully created.
1148  */
1149 gboolean
1150 create_account (ModestEasysetupWizardDialog *self)
1151 {
1152         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1153         
1154         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1155
1156         /* Some checks: */
1157         if (!display_name)
1158                 return FALSE;
1159                 
1160         /* We should have checked for this already, 
1161          * and changed that name accordingly, 
1162          * but let's check again just in case:
1163          */
1164         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1165                 return FALSE;
1166
1167         /* Increment the non-user visible name if necessary, 
1168          * based on the display name: */
1169         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1170         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1171                 account_name_start, FALSE /* not a server account */);
1172         g_free (account_name_start);
1173                 
1174         /* username and password (for both incoming and outgoing): */
1175         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1176         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1177         
1178         /* Incoming server: */
1179         /* Note: We need something as default for the ModestProtocol values, 
1180          * or modest_account_mgr_add_server_account will fail. */
1181         gchar* servername_incoming = NULL;
1182         ModestProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1183         ModestProtocol protocol_security_incoming = MODEST_PROTOCOL_SECURITY_NONE;
1184         ModestProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1185         
1186         /* Get details from the specified presets: */
1187         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1188                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1189         if(provider_id) {
1190                 /* Use presets: */
1191                 
1192                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1193                         TRUE /* incoming */);
1194                 
1195                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets, provider_id, 
1196                         TRUE /* incoming */);
1197                 
1198         
1199                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1200                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP)
1201                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1202                 else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP)
1203                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1204                                 
1205                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets, provider_id, 
1206                         TRUE /* incoming */);
1207                         
1208                 
1209                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1210                         protocol_security_incoming = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1211                 
1212                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1213                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1214         }
1215         else {
1216                 /* Use custom pages because no preset was specified: */
1217                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1218                 
1219                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1220                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1221                 
1222                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1223                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1224                 
1225                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1226                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1227                         : MODEST_PROTOCOL_AUTH_NONE;
1228                 
1229         }
1230         
1231         /* First we add the 2 server accounts, and then we add the account that uses them.
1232          * If we don't do it in this order then we will experience a crash. */
1233          
1234         /* Add a (incoming) server account, to be used by the account: */
1235         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1236         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1237                 store_name_start, TRUE /* server account */);
1238         g_free (store_name_start);
1239         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1240                 store_name,
1241                 servername_incoming,
1242                 username, password,
1243                 protocol_incoming,
1244                 protocol_security_incoming,
1245                 protocol_authentication_incoming);              
1246                 
1247         g_free (servername_incoming);
1248         
1249         if (!created) {
1250                 /* TODO: Provide a Logical ID for the text: */
1251                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1252                 return FALSE;   
1253         }
1254         
1255         /* Sanity check: */
1256         /* There must be at least one account now: */
1257         GSList *account_names = modest_account_mgr_account_names (self->account_manager);
1258         if(!account_names)
1259         {
1260                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1261         }
1262         g_slist_free (account_names);
1263         
1264         
1265         /* Outgoing server: */
1266         gchar* servername_outgoing = NULL;
1267         ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1268         ModestProtocol protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1269         ModestProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1270         
1271         if(provider_id) {
1272                 /* Use presets: */
1273                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1274                         FALSE /* incoming */);
1275                         
1276                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets, provider_id, 
1277                         FALSE /* incoming */);
1278                 
1279                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1280                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1281                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1282                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1283                 
1284                 ModestPresetsSecurity security_outgoing = 
1285                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1286                                 FALSE /* incoming */);
1287                         
1288                 protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1289                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP)
1290                         protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1291                 
1292                 protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1293                 /* TODO: There is no SMTP authentication enum for presets. */
1294         }
1295         else {
1296                 /* Use custom pages because no preset was specified: */
1297                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1298                 
1299                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1300
1301                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1302                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1303                 
1304                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1305                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1306         }
1307             
1308         /* Add a (outgoing) server account to be used by the account: */
1309         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1310         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1311                 transport_name_start, TRUE /* server account */);
1312         g_free (transport_name_start);
1313         created = modest_account_mgr_add_server_account (self->account_manager,
1314                 transport_name,
1315                 servername_outgoing,
1316                 username, password,
1317                 protocol_outgoing,
1318                 protocol_security_outgoing,
1319                 protocol_authentication_outgoing);
1320                 
1321         g_free (servername_outgoing);
1322                 
1323         if (!created) {
1324                 /* TODO: Provide a Logical ID for the text: */
1325                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1326                 return FALSE;   
1327         }
1328         
1329         
1330         /* Create the account, which will contain the two "server accounts": */
1331         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1332                 store_name, /* The name of our POP/IMAP server account. */
1333                 transport_name /* The name of our SMTP server account. */);
1334         g_free (store_name);
1335         g_free (transport_name);
1336         
1337         if (!created) {
1338                 /* TODO: Provide a Logical ID for the text: */
1339                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1340                 return FALSE;   
1341         }
1342         
1343         /* The user name and email address must be set additionally: */
1344         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1345         modest_account_mgr_set_string (self->account_manager, account_name,
1346                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1347
1348         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1349         modest_account_mgr_set_string (self->account_manager, account_name,
1350                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1351
1352         /* Set the display name: */
1353         modest_account_mgr_set_string (self->account_manager, account_name,
1354                 MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1355
1356         /* Save the connection-specific SMTP server accounts. */
1357         if (self->specific_window)
1358                 return modest_connection_specific_smtp_window_save_server_accounts (
1359                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1360                         
1361         g_free (account_name);
1362
1363         return FALSE;
1364 }
1365
1366