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