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