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