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