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