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