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