* cleaner version of the patch to make sure no double account-setup-wizards are
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  
30
31 #include "modest-easysetup-wizard.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkseparator.h>
42 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
43 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
44 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
45 #include "widgets/modest-serversecurity-combo-box.h"
46 #include "widgets/modest-secureauth-combo-box.h"
47 #include "widgets/modest-validating-entry.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h"
51 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
52 #include "maemo/modest-connection-specific-smtp-window.h"
53 #include "widgets/modest-ui-constants.h"
54 #include "maemo/modest-account-settings-dialog.h"
55 #include "maemo/modest-maemo-utils.h"
56 #include <gconf/gconf-client.h>
57 #include <string.h> /* For strlen(). */
58
59 /* Include config.h so that _() works: */
60 #ifdef HAVE_CONFIG_H
61 #include <config.h>
62 #endif
63
64 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
65
66 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
67
68 #define WIZARD_DIALOG_GET_PRIVATE(o)                                    \
69         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
70
71 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
72
73
74 typedef enum {
75         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
76         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
77 } ModestEasysetupWizardDialogServerChanges;
78
79 struct _ModestEasysetupWizardDialogPrivate
80 {
81         ModestPresets *presets;
82
83         /* Remember what fields the user edited manually to not prefill them
84          * again. */
85         ModestEasysetupWizardDialogServerChanges server_changes;
86         
87         /* Check if the user changes a field to show a confirmation dialog */
88         gboolean dirty;
89 };
90
91 static void
92 on_easysetup_changed(GtkWidget* widget, ModestEasysetupWizardDialog* wizard)
93 {
94         ModestEasysetupWizardDialogPrivate* priv = WIZARD_DIALOG_GET_PRIVATE(wizard);
95         g_return_if_fail (priv != NULL);
96         priv->dirty = TRUE;
97 }
98
99 static void
100 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
101                                              GValue *value, GParamSpec *pspec)
102 {
103         switch (property_id) {
104         default:
105                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
106         }
107 }
108
109 static void
110 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
111                                              const GValue *value, GParamSpec *pspec)
112 {
113         switch (property_id) {
114         default:
115                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116         }
117 }
118
119 static void
120 modest_easysetup_wizard_dialog_dispose (GObject *object)
121 {
122         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
123                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
124 }
125
126 static void
127 modest_easysetup_wizard_dialog_finalize (GObject *object)
128 {
129         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
130         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
131         
132         if (self->account_manager)
133                 g_object_unref (G_OBJECT (self->account_manager));
134                 
135         if (priv->presets)
136                 modest_presets_destroy (priv->presets);
137                 
138         if (self->specific_window)
139                 gtk_widget_destroy (self->specific_window);
140                 
141         g_free (self->saved_account_name);
142         
143         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
144 }
145
146 static void
147 show_error (GtkWidget *parent_widget, const gchar* text);
148
149 static gboolean
150 create_account (ModestEasysetupWizardDialog *self, gboolean enabled);
151
152 static void
153 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
154
155 static void
156 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
157
158 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
159
160 static gint get_serverport_incoming(ModestPresetsServerType servertype_incoming,
161                                     ModestPresetsSecurity security_incoming)
162 {
163         int serverport_incoming = 0;
164                 /* We don't check for SMTP here as that is impossible for an incoming server. */
165                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
166                         serverport_incoming =
167                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 993 : 143; 
168                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
169                         serverport_incoming =
170                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 995 : 110; 
171                 }
172         return serverport_incoming;
173 }
174
175 static GList* check_for_supported_auth_methods(ModestEasysetupWizardDialog* account_wizard)
176 {
177         GError *error = NULL;
178         const ModestTransportStoreProtocol protocol = 
179           easysetup_servertype_combo_box_get_active_servertype (
180                                                                 EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
181         const gchar* hostname = gtk_entry_get_text(GTK_ENTRY(account_wizard->entry_incomingserver));
182         const gchar* username = gtk_entry_get_text(GTK_ENTRY(account_wizard->entry_user_username));
183         const ModestConnectionProtocol protocol_security_incoming = 
184                                         modest_serversecurity_combo_box_get_active_serversecurity (
185                                         MODEST_SERVERSECURITY_COMBO_BOX (
186                                         account_wizard->combo_incoming_security));
187         int port_num = get_serverport_incoming(protocol, protocol_security_incoming); 
188         GList *list_auth_methods =
189           modest_maemo_utils_get_supported_secure_authentication_methods (
190                                                                       protocol, 
191                                                                       hostname, port_num, username, GTK_WINDOW (account_wizard), &error);
192         if (list_auth_methods) {
193                 /* TODO: Select the correct method */
194                 GList* list = NULL;
195                 GList* method;
196                 for (method = list_auth_methods; method != NULL; method = g_list_next(method)) {
197                         ModestAuthProtocol auth = (ModestAuthProtocol) (GPOINTER_TO_INT(method->data));
198                         if (modest_protocol_info_auth_is_secure(auth)) {
199                                 list = g_list_append(list, GINT_TO_POINTER(auth));
200                         }
201                 }
202                 g_list_free(list_auth_methods);
203                 if (list)
204                         return list;
205         }
206
207         if(error == NULL || error->domain != modest_maemo_utils_get_supported_secure_authentication_error_quark() ||
208                         error->code != MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
209         {
210                 show_error (GTK_WIDGET(account_wizard), _("Could not discover supported secure authentication methods."));
211         }
212
213         if(error != NULL) g_error_free(error);
214         return NULL;
215 }
216
217 static void
218 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
219 {
220         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
221         
222         /* Call the vfunc, which may be overridden by derived classes: */
223         if (klass->enable_buttons) {
224                 GtkNotebook *notebook = NULL;
225                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
226                 
227                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
228                 if (current_page_num == -1)
229                         return;
230                         
231                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
232                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
233         }
234 }
235
236 static void
237 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
238 {
239         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
240         g_assert(self);
241         invoke_enable_buttons_vfunc(self);
242 }
243
244 static void
245 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
246 {
247         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
248         g_assert(self);
249         invoke_enable_buttons_vfunc(self);
250 }
251
252 /** This is a convenience function to create a caption containing a mandatory widget.
253  * When the widget is edited, the enable_buttons() vfunc will be called.
254  */
255 static GtkWidget* create_caption_new_with_asterisk(ModestEasysetupWizardDialog *self,
256                                                   GtkSizeGroup *group,
257                                                   const gchar *value,
258                                                   GtkWidget *control,
259                                                   GtkWidget *icon,
260                                                   HildonCaptionStatus flag)
261 {
262         GtkWidget *caption = NULL;
263   
264         /* Note: Previously, the translated strings already contained the "*",
265          * Comment out this code if they do again.
266          */
267         /* Add a * character to indicate mandatory fields,
268          * as specified in our "Email UI Specification": */
269         if (flag == HILDON_CAPTION_MANDATORY) {
270                 gchar* title = g_strdup_printf("%s*", value);
271                 caption = hildon_caption_new (group, title, control, icon, flag);       
272                 g_free(title);
273         }       
274         else
275                 caption = hildon_caption_new (group, value, control, icon, flag);
276
277         /* Connect to the appropriate changed signal for the widget, 
278          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
279          */
280         if (GTK_IS_ENTRY (control)) {
281                 g_signal_connect (G_OBJECT (control), "changed",
282                                   G_CALLBACK (on_caption_entry_changed), self);
283                 
284         }
285         else if (GTK_IS_COMBO_BOX (control)) {
286                 g_signal_connect (G_OBJECT (control), "changed",
287                                   G_CALLBACK (on_caption_combobox_changed), self);
288         }
289          
290         return caption;
291 }
292            
293 static GtkWidget*
294 create_page_welcome (ModestEasysetupWizardDialog *self)
295 {
296         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
297         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
298         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
299         /* So that it is not truncated: */
300         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
301         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
302         gtk_widget_show (label);
303         gtk_widget_show (GTK_WIDGET (box));
304         return GTK_WIDGET (box);
305 }
306
307 static void
308 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
309 {
310         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
311         g_assert(self);
312         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
313         
314         priv->dirty = TRUE;
315         
316         /* Fill the providers combo, based on the selected country: */
317         GSList *list_mcc_ids = easysetup_country_combo_box_get_active_country_ids (
318                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
319         easysetup_provider_combo_box_fill (
320                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, list_mcc_ids);
321 }
322
323 static void
324 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
325 {
326         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
327         g_assert(self);
328         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
329         
330         priv->dirty = TRUE;
331         
332         /* Fill the providers combo, based on the selected country: */
333         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
334                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
335         
336         gchar* domain_name = NULL;
337         if(provider_id)
338                 domain_name = modest_presets_get_domain (priv->presets, provider_id);
339         
340         if(!domain_name)
341                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
342                 
343         if (self->entry_user_email)
344                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
345                 
346         g_free (domain_name);
347         
348         g_free (provider_id);
349 }
350
351 static void
352 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
353 {
354         /* ModestEasysetupWizardDialog *dialog = MODEST_EASYSETUP_WIZARD_DIALOG (user_data); */
355         show_error (GTK_WIDGET (self), _CS("ckdg_ib_maximum_characters_reached"));
356 }
357
358 static void
359 on_entry_invalid_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data)
360 {
361         /* ModestEasysetupWizardDialog *dialog = MODEST_EASYSETUP_WIZARD_DIALOG (user_data); */
362         
363         const gchar *show_char = NULL;
364         if (character)
365           show_char = character;
366         else {
367           /* TODO: We need a logical ID for this: */
368           show_char = _("whitespace");
369         }
370         
371         /* TODO: Should this show just this one bad character or all the not-allowed characters? */
372         gchar *message = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), show_char);
373         show_error (GTK_WIDGET (self), message);
374 }
375
376 static GtkWidget*
377 create_page_account_details (ModestEasysetupWizardDialog *self)
378 {
379         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
380         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
381         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
382         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
383         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
384         gtk_widget_show (label);
385         
386         /* Create a size group to be used by all captions.
387          * Note that HildonCaption does not create a default size group if we do not specify one.
388          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
389         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
390
391         /* The country widgets: */
392         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
393         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_country"), 
394                                                               self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
395         gtk_widget_show (self->combo_account_country);
396         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
397         gtk_widget_show (caption);
398         
399         /* connect to country combo's changed signal, so we can fill the provider combo: */
400         g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
401                           G_CALLBACK (on_combo_account_country), self);
402             
403         GtkWidget *separator = gtk_hseparator_new ();
404         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
405         gtk_widget_show (separator);
406             
407         /* The service provider widgets: */     
408         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
409         
410         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_serviceprovider"), 
411                                                    self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
412         gtk_widget_show (self->combo_account_serviceprovider);
413         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
414         gtk_widget_show (caption);
415         
416         /* connect to providers combo's changed signal, so we can fill the email address: */
417         g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
418                           G_CALLBACK (on_combo_account_serviceprovider), self);
419         
420         /* TODO: Default to the current country somehow.
421          * But I don't know how to get the information that is specified in the 
422          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
423          *
424          * This is probably not the best choice of gconf key:
425          * This is the  "mcc used in the last pairing", ie. the last connection you made.
426          * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
427          *
428          */
429         GConfClient *client = gconf_client_get_default ();
430         GError *error = NULL;
431         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
432         gint mcc_id = gconf_client_get_int(client, key, &error);
433         
434         if(mcc_id < 0)
435                 mcc_id = 0;
436      
437         if (error) {
438                 g_warning ("Error getting gconf key %s:\n%s", key, error->message);
439                 g_error_free (error);
440                 error = NULL;
441         
442                 mcc_id = 0;
443         }
444     
445         /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
446          * This might just be a Maemo bug.
447          */
448         if (mcc_id == 0) 
449         {
450                 /* For now, we default to Finland when there is nothing better: */
451                 mcc_id = 244;
452         }
453    
454         easysetup_country_combo_box_set_active_country_id (
455                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
456                 
457         
458         /* The description widgets: */  
459         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
460         g_signal_connect(G_OBJECT(self->entry_account_title), "changed",
461                                                                          G_CALLBACK(on_easysetup_changed), self);
462         /* Do use auto-capitalization: */
463         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
464                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
465         
466         /* Set a default account title, choosing one that does not already exist: */
467         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
468         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
469         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
470                 self->account_manager, default_account_name_start);
471         g_free (default_account_name_start);
472         default_account_name_start = NULL;
473         
474         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
475         g_free (default_account_name);
476         default_account_name = NULL;
477
478         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
479                                                    self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
480         gtk_widget_show (self->entry_account_title);
481         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
482         gtk_widget_show (caption);
483         
484         /* Prevent the use of some characters in the account title, 
485          * as required by our UI specification: */
486         GList *list_prevent = NULL;
487         list_prevent = g_list_append (list_prevent, "\\");
488         list_prevent = g_list_append (list_prevent, "/");
489         list_prevent = g_list_append (list_prevent, ":");
490         list_prevent = g_list_append (list_prevent, "*");
491         list_prevent = g_list_append (list_prevent, "?");
492         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
493         list_prevent = g_list_append (list_prevent, "“");
494         list_prevent = g_list_append (list_prevent, "<"); 
495         list_prevent = g_list_append (list_prevent, ">"); 
496         list_prevent = g_list_append (list_prevent, "|");
497         list_prevent = g_list_append (list_prevent, "^");       
498         modest_validating_entry_set_unallowed_characters (
499                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
500         g_list_free (list_prevent);
501         list_prevent = NULL;
502         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_account_title),
503                                                                                                                                          on_entry_invalid_character, self);
504         
505         /* Set max length as in the UI spec:
506          * The UI spec seems to want us to show a dialog if we hit the maximum. */
507         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
508         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
509                                               on_entry_max, self);
510         
511         gtk_widget_show (GTK_WIDGET (box));
512         
513         return GTK_WIDGET (box);
514 }
515
516 static GtkWidget*
517 create_page_user_details (ModestEasysetupWizardDialog *self)
518 {
519         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
520         
521         /* Create a size group to be used by all captions.
522          * Note that HildonCaption does not create a default size group if we do not specify one.
523          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
524         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
525          
526         /* The name widgets: (use auto cap) */
527         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
528         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), 
529                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
530         
531         /* Set max length as in the UI spec:
532          * The UI spec seems to want us to show a dialog if we hit the maximum. */
533         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
534         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
535                                               on_entry_max, self);
536         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
537                                                               _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
538         g_signal_connect(G_OBJECT(self->entry_user_name), "changed", 
539                                                                          G_CALLBACK(on_easysetup_changed), self);
540         gtk_widget_show (self->entry_user_name);
541         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
542         gtk_widget_show (caption);
543         
544         /* Prevent the use of some characters in the name, 
545          * as required by our UI specification: */
546         GList *list_prevent = NULL;
547         list_prevent = g_list_append (list_prevent, "<");
548         list_prevent = g_list_append (list_prevent, ">");
549         modest_validating_entry_set_unallowed_characters (
550                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
551         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_user_name),
552                 on_entry_invalid_character, self);
553         g_list_free (list_prevent);
554         
555         /* The username widgets: */     
556         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
557         /* Auto-capitalization is the default, so let's turn it off: */
558         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
559         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
560                                                    self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
561         gtk_widget_show (self->entry_user_username);
562         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
563         g_signal_connect(G_OBJECT(self->entry_user_username), "changed", 
564                                                                          G_CALLBACK(on_easysetup_changed), self);
565         gtk_widget_show (caption);
566         
567         /* Prevent the use of some characters in the username, 
568          * as required by our UI specification: */
569         modest_validating_entry_set_unallowed_characters_whitespace (
570                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
571         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(self->entry_user_username),
572                 on_entry_invalid_character, self);
573         
574         /* Set max length as in the UI spec:
575          * The UI spec seems to want us to show a dialog if we hit the maximum. */
576         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
577         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
578                                               on_entry_max, self);
579         
580         /* The password widgets: */     
581         self->entry_user_password = gtk_entry_new ();
582         /* Auto-capitalization is the default, so let's turn it off: */
583         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
584                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
585         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
586         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
587         caption = create_caption_new_with_asterisk (self, sizegroup, 
588                                                    _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
589         g_signal_connect(G_OBJECT(self->entry_user_password), "changed", 
590                                                                          G_CALLBACK(on_easysetup_changed), self);
591         gtk_widget_show (self->entry_user_password);
592         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
593         gtk_widget_show (caption);
594         
595         /* The email address widgets: */        
596         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
597         /* Auto-capitalization is the default, so let's turn it off: */
598         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
599         caption = create_caption_new_with_asterisk (self, sizegroup, 
600                                                    _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
601         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
602         gtk_widget_show (self->entry_user_email);
603         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
604         g_signal_connect(G_OBJECT(self->entry_user_email), "changed", 
605                                                                          G_CALLBACK(on_easysetup_changed), self);
606         gtk_widget_show (caption);
607         
608         /* Set max length as in the UI spec:
609          * The UI spec seems to want us to show a dialog if we hit the maximum. */
610         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
611         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
612                                               on_entry_max, self);
613         
614         
615         gtk_widget_show (GTK_WIDGET (box));
616         
617         return GTK_WIDGET (box);
618 }
619
620 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
621 {
622         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
623         
624         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
625         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
626         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
627         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
628         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
629         gtk_widget_show (label);
630         
631         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
632         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
633         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
634         
635         /* The documentation for gtk_label_set_line_wrap() says that we must 
636          * call gtk_widget_set_size_request() with a hard-coded width, 
637          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
638         gtk_widget_set_size_request (label, 400, -1);
639         
640         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
641         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
642         gtk_widget_show (label);
643         
644         gtk_widget_show (GTK_WIDGET (box));
645         return GTK_WIDGET (box);
646 }
647
648 /** Change the caption title for the incoming server, 
649  * as specified in the UI spec:
650  */
651 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
652 {
653         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
654                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
655         const gchar* type = 
656                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
657                  _("mail_fi_emailtype_pop3") : 
658                  _("mail_fi_emailtype_imap") );
659                         
660                 
661         /* Note that this produces a compiler warning, 
662          * because the compiler does not know that the translated string will have a %s in it.
663          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
664         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
665         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
666         g_free(incomingserver_title);
667 }
668
669 /** Change the caption title for the incoming server, 
670  * as specified in the UI spec:
671  */
672 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
673 {
674         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
675                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
676         
677         /* Fill the combo with appropriately titled choices for POP or IMAP. */
678         /* The choices are the same, but the titles are different, as in the UI spec. */
679         modest_serversecurity_combo_box_fill (
680                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
681 }
682
683 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
684 {
685         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
686         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(self);
687         
688         priv->dirty = TRUE;
689         
690         update_incoming_server_title (self);
691         update_incoming_server_security_choices (self);
692
693         set_default_custom_servernames (self);
694 }
695
696 static void on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
697 {
698         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
699         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
700         priv->dirty = TRUE;
701         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
702 }
703
704 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
705 {
706         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
707
708         /* Show note that account type cannot be changed in future: */
709         GtkWidget *label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
710         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
711         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
712         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
713         gtk_widget_show (label);
714         
715         /* Create a size group to be used by all captions.
716          * Note that HildonCaption does not create a default size group if we do not specify one.
717          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
718         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
719          
720         /* The incoming server widgets: */
721         if (!self->combo_incoming_servertype)
722                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
723         easysetup_servertype_combo_box_set_active_servertype (
724                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
725         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
726                                                               _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
727         gtk_widget_show (self->combo_incoming_servertype);
728         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
729         gtk_widget_show (caption);
730         
731         if(!self->entry_incomingserver)
732         {
733                 self->entry_incomingserver = gtk_entry_new ();
734                 g_signal_connect(G_OBJECT(self->entry_incomingserver), "changed",
735                                                                                  G_CALLBACK(on_easysetup_changed), self);
736         }
737         /* Auto-capitalization is the default, so let's turn it off: */
738         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
739         set_default_custom_servernames (self);
740
741         if (self->caption_incoming)
742                 gtk_widget_destroy (self->caption_incoming);
743            
744         /* The caption title will be updated in update_incoming_server_title().
745          * so this default text will never be seen: */
746         /* (Note: Changing the title seems pointless. murrayc) */
747         self->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
748                                                                   "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
749         update_incoming_server_title (self);
750         gtk_widget_show (self->entry_incomingserver);
751         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
752         gtk_widget_show (self->caption_incoming);
753         
754         /* Change the caption title when the servertype changes, 
755          * as in the UI spec: */
756         g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
757                           G_CALLBACK (on_combo_servertype_changed), self);
758
759         /* Remember when the servername was changed manually: */
760         g_signal_connect (G_OBJECT (self->entry_incomingserver), "changed",
761                           G_CALLBACK (on_entry_incoming_servername_changed), self);
762
763         /* The secure connection widgets: */    
764         if (!self->combo_incoming_security)
765                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
766         update_incoming_server_security_choices (self);
767         modest_serversecurity_combo_box_set_active_serversecurity (
768                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
769         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
770                                       self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
771         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed",
772                           G_CALLBACK (on_easysetup_changed), self);
773         gtk_widget_show (self->combo_incoming_security);
774         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
775         gtk_widget_show (caption);
776         
777         if(!self->checkbox_incoming_auth)
778         {
779                 self->checkbox_incoming_auth = gtk_check_button_new ();
780                 g_signal_connect (G_OBJECT (self->checkbox_incoming_auth), "toggled",
781                           G_CALLBACK (on_easysetup_changed), self);
782         }
783         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
784                                       self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
785         
786         gtk_widget_show (self->checkbox_incoming_auth);
787         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
788         gtk_widget_show (caption);
789         
790         gtk_widget_show (GTK_WIDGET (box));
791         
792         return GTK_WIDGET (box);
793 }
794
795 static void
796 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
797 {
798         GtkWidget *widget = GTK_WIDGET (user_data);
799         
800         /* Enable the widget only if the toggle button is active: */
801         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
802         gtk_widget_set_sensitive (widget, enable);
803 }
804
805 /* Make the sensitivity of a widget depend on a toggle button.
806  */
807 static void
808 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
809 {
810         g_signal_connect (G_OBJECT (button), "toggled",
811                           G_CALLBACK (on_toggle_button_changed), widget);
812         
813         /* Set the starting sensitivity: */
814         on_toggle_button_changed (button, widget);
815 }
816
817 static void
818 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
819 {
820         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
821         ModestEasysetupWizardDialogPrivate* priv = WIZARD_DIALOG_GET_PRIVATE(self);
822         
823         /* We set dirty here because setting it depending on the connection specific dialog
824         seems overkill */
825         priv->dirty = TRUE;
826         
827         /* Create the window, if necessary: */
828         if (!(self->specific_window)) {
829                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
830                 modest_connection_specific_smtp_window_fill_with_connections (
831                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
832                         NULL /* account_name, not known yet. */);
833         }
834
835         /* Show the window: */
836         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
837         gtk_widget_show (self->specific_window);
838 }
839
840 static void on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
841 {
842         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
843         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
844         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
845 }
846
847 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
848 {
849         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
850         
851         /* Create a size group to be used by all captions.
852          * Note that HildonCaption does not create a default size group if we do not specify one.
853          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
854         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
855          
856         /* The outgoing server widgets: */
857         if (!self->entry_outgoingserver)
858         {
859                 self->entry_outgoingserver = gtk_entry_new ();
860                 g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
861                           G_CALLBACK (on_easysetup_changed), self);
862         }
863         /* Auto-capitalization is the default, so let's turn it off: */
864         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
865         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
866                                                               _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
867         gtk_widget_show (self->entry_outgoingserver);
868         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
869         gtk_widget_show (caption);
870         set_default_custom_servernames (self);
871         
872         /* The secure connection widgets: */    
873         if (!self->combo_outgoing_security)
874         {
875                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
876                 g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed",
877                           G_CALLBACK (on_easysetup_changed), self);
878         }
879         modest_serversecurity_combo_box_fill (
880                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
881         modest_serversecurity_combo_box_set_active_serversecurity (
882                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
883         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
884                                       self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
885         gtk_widget_show (self->combo_outgoing_security);
886         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
887         gtk_widget_show (caption);
888         
889         /* The secure authentication widgets: */
890         if (!self->combo_outgoing_auth)
891         {
892                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
893                                 g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed",
894                           G_CALLBACK (on_easysetup_changed), self);
895         }
896         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
897                                       self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
898         gtk_widget_show (self->combo_outgoing_auth);
899         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
900         gtk_widget_show (caption);
901         
902         GtkWidget *separator = gtk_hseparator_new ();
903         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
904         gtk_widget_show (separator);
905         
906         /* connection-specific checkbox: */
907         if (!self->checkbox_outgoing_smtp_specific) {
908                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
909                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
910                                               FALSE);
911                 g_signal_connect (G_OBJECT (self->checkbox_outgoing_smtp_specific), "toggled",
912                           G_CALLBACK (on_easysetup_changed), self);
913
914         }
915         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
916                                       self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
917         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
918         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
919         gtk_widget_show (caption);
920         
921         /* Connection-specific SMTP-Severs Edit button: */
922         if (!self->button_outgoing_smtp_servers)
923                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
924         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
925                                       self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
926         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
927         gtk_widget_show (self->button_outgoing_smtp_servers);
928         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
929         gtk_widget_show (caption);
930         
931         /* Only enable the button when the checkbox is checked: */
932         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
933                                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
934                 
935         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
936                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
937
938         g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
939                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
940         
941         
942         gtk_widget_show (GTK_WIDGET (box));
943         
944         return GTK_WIDGET (box);
945 }
946
947 static gboolean
948 show_advanced_edit(gpointer user_data)
949 {
950         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
951         
952         if (!(self->saved_account_name))
953                 return FALSE;
954         
955         /* Show the Account Settings window: */
956         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
957         modest_account_settings_dialog_set_account_name (dialog, self->saved_account_name);
958         
959         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
960         
961         gtk_dialog_run (GTK_DIALOG (dialog));
962
963         gtk_widget_destroy (GTK_WIDGET (dialog));
964         
965         return FALSE; /* Do not call this timeout callback again. */
966 }
967
968 static void
969 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
970 {
971         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
972         
973         /* Save the new account, so we can edit it with ModestAccountSettingsDialog, 
974          * without recoding it to use non-gconf information.
975          * This account will be deleted if Finish is never actually clicked. */
976
977         gboolean saved = TRUE;
978         if (!(self->saved_account_name)) {
979                 saved = create_account (self, FALSE);
980         }
981                 
982         if (!saved)
983                 return;
984                 
985         if (!(self->saved_account_name))
986                 return;
987         
988         /* Show the Account Settings window: */
989         show_advanced_edit(self);
990 }
991 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
992 {
993         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
994         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
995         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
996         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
997         gtk_widget_show (label);
998         
999         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
1000         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1001         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1002         gtk_widget_show (label);
1003         
1004         if (!self->button_edit)
1005                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
1006         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
1007                                                  self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
1008         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
1009         gtk_widget_show (self->button_edit);
1010         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
1011         gtk_widget_show (caption);
1012         
1013         g_signal_connect (G_OBJECT (self->button_edit), "clicked", 
1014                           G_CALLBACK (on_button_edit_advanced_settings), self);
1015         
1016         gtk_widget_show (GTK_WIDGET (box));
1017         return GTK_WIDGET (box);
1018 }
1019
1020
1021 /*
1022  */
1023 static void 
1024 on_response (ModestWizardDialog *wizard_dialog,
1025              gint response_id,
1026              gpointer user_data)
1027 {
1028         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1029         if (response_id == GTK_RESPONSE_CANCEL) {
1030                 /* Remove any temporarily-saved account that will not actually be needed: */
1031                 if (self->saved_account_name) {
1032                         modest_account_mgr_remove_account (self->account_manager,
1033                                                            self->saved_account_name);
1034                 }
1035         }
1036
1037         invoke_enable_buttons_vfunc (self);
1038 }
1039
1040 static void 
1041 on_response_before (ModestWizardDialog *wizard_dialog,
1042                     gint response_id,
1043                     gpointer user_data)
1044 {
1045         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1046         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
1047         if (response_id == GTK_RESPONSE_CANCEL) {
1048                 /* This is mostly copied from
1049                  * src/maemo/modest-account-settings-dialog.c */
1050                 if (priv->dirty)
1051                 {
1052                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
1053                                 _("imum_nc_wizard_confirm_lose_changes")));
1054                         /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1055
1056                         const gint dialog_response = gtk_dialog_run (dialog);
1057                         gtk_widget_destroy (GTK_WIDGET (dialog));
1058
1059                         if (dialog_response != GTK_RESPONSE_OK)
1060                         {
1061                                 /* This is a nasty hack. murrayc. */
1062                                 /* Don't let the dialog close */
1063                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1064                         }
1065                 }
1066         }
1067 }
1068
1069 static void
1070 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1071 {
1072         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1073         
1074         /* Create the notebook to be used by the ModestWizardDialog base class:
1075          * Each page of the notebook will be a page of the wizard: */
1076         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1077         
1078         /* Set the notebook used by the ModestWizardDialog base class: */
1079         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1080     
1081         /* Set the wizard title:
1082          * The actual window title will be a combination of this and the page's tab label title. */
1083         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1084
1085         /* Read in the information about known service providers: */
1086         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1087         
1088         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
1089         priv->presets = modest_presets_new (filepath);
1090         if (!(priv->presets)) {
1091                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
1092         }
1093         
1094         g_assert(priv->presets);
1095
1096         /* The server fields did not have been manually changed yet */
1097         priv->server_changes = 0;
1098
1099         /* Get the account manager object, 
1100          * so we can check for existing accounts,
1101          * and create new accounts: */
1102         self->account_manager = modest_runtime_get_account_mgr ();
1103         g_assert (self->account_manager);
1104         g_object_ref (self->account_manager);
1105         
1106         /* Create the common pages, 
1107          */
1108         self->page_welcome = create_page_welcome (self);
1109         self->page_account_details = create_page_account_details (self);
1110         self->page_user_details = create_page_user_details (self);
1111         
1112         /* Add the common pages: */
1113         gtk_notebook_append_page (notebook, self->page_welcome, 
1114                                   gtk_label_new (_("mcen_ti_emailsetup_welcome")));
1115         gtk_notebook_append_page (notebook, self->page_account_details, 
1116                                   gtk_label_new (_("mcen_ti_accountdetails")));
1117         gtk_notebook_append_page (notebook, self->page_user_details, 
1118                                   gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
1119                 
1120         /* Create and add the easysetup-specific pages,
1121          * because we need _some_ final page to enable the Next and Finish buttons: */
1122         create_subsequent_easysetup_pages (self);
1123
1124         /* Connect to the dialog's response signal so we can enable/disable buttons 
1125          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1126          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1127          * so that we can be enable/disable after ModestWizardDialog has done its own 
1128          * enabling/disabling of buttons.
1129          * 
1130          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1131          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1132          * 
1133          * It's not enough to connect to the notebook's switch-page signal, because 
1134          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1135          * _after_ switching the page (understandably).
1136          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1137          * then gtk_notebook_get_current_page() would return an incorrect value.)
1138          */
1139         g_signal_connect_after (G_OBJECT (self), "response",
1140                                 G_CALLBACK (on_response), self);
1141
1142         /* This is to show a confirmation dialog when the user hits cancel */
1143         g_signal_connect (G_OBJECT (self), "response",
1144                           G_CALLBACK (on_response_before), self);
1145
1146         /* Reset dirty, because there was no user input until now */
1147         priv->dirty = FALSE;
1148         
1149         /* When this window is shown, hibernation should not be possible, 
1150          * because there is no sensible way to save the state: */
1151         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1152                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1153 }
1154
1155 ModestEasysetupWizardDialog*
1156 modest_easysetup_wizard_dialog_new (void)
1157 {       
1158         
1159         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1160 }
1161
1162 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1163 {
1164         GtkNotebook *notebook = NULL;
1165         g_object_get (self, "wizard-notebook", &notebook, NULL);
1166         g_assert(notebook);
1167         
1168         /* Create the custom pages: */
1169         if(!(self->page_custom_incoming)) {
1170                 self->page_custom_incoming = create_page_custom_incoming (self);
1171         }
1172                 
1173         if(!(self->page_custom_outgoing)) {
1174                 self->page_custom_outgoing = create_page_custom_outgoing (self);
1175         }
1176         
1177         if(!(self->page_complete_customsetup)) {
1178                 self->page_complete_customsetup = create_page_complete_custom (self);
1179         }
1180         
1181         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
1182                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
1183                                           gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
1184         
1185         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
1186                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
1187                                           gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
1188                 
1189         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
1190                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
1191                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1192                         
1193         /* This is unnecessary with GTK+ 2.10: */
1194         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1195 }
1196         
1197 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1198 {
1199         GtkNotebook *notebook = NULL;
1200         g_object_get (self, "wizard-notebook", &notebook, NULL);
1201         g_assert(notebook);
1202         
1203         /* Create the easysetup-specific pages: */
1204         if(!self->page_complete_easysetup)
1205                 self->page_complete_easysetup = create_page_complete_easysetup (self);
1206
1207         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
1208                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
1209                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1210                         
1211         /* This is unnecessary with GTK+ 2.10: */
1212         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1213 }
1214 /* After the user details page,
1215  * the following pages depend on whether "Other" was chosen 
1216  * in the provider combobox on the account page
1217  */
1218 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
1219 {
1220         if (easysetup_provider_combo_box_get_active_provider_id (
1221                     EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
1222                 /* "Other..." was selected: */
1223                 
1224                 /* Make sure that the easysetup pages do not exist: */
1225                 if(self->page_complete_easysetup) {
1226                         gtk_widget_destroy (self->page_complete_easysetup);
1227                         self->page_complete_easysetup = NULL;
1228                 }
1229                 
1230                 create_subsequent_customsetup_pages (self);
1231         }       
1232         else {
1233                 /* A specific provider was selected: */
1234                 {
1235                         /* Make sure that the custom pages do not exist:
1236                          * Because they will be used if they exist, when creating the account. */
1237                         if(self->page_custom_incoming) {
1238                                 gtk_widget_destroy (self->page_custom_incoming);
1239                                 self->page_custom_incoming = NULL;
1240                         }
1241                         
1242                         if(self->page_custom_outgoing) {
1243                                 gtk_widget_destroy (self->page_custom_outgoing);
1244                                 self->page_custom_outgoing = NULL;
1245                         }
1246                         
1247                         if(self->page_complete_customsetup) {
1248                                 gtk_widget_destroy (self->page_complete_customsetup);
1249                                 self->page_complete_customsetup = NULL;
1250                         }
1251                 }
1252                 
1253                 /* Create the easysetup pages: */
1254                 create_subsequent_easysetup_pages (self);
1255         }
1256 }
1257
1258
1259 static gchar*
1260 util_get_default_servername_from_email_address (const gchar* email_address, ModestTransportStoreProtocol servertype)
1261 {
1262         if (!email_address)
1263                 return NULL;
1264         
1265         gchar* at = g_utf8_strchr (email_address, -1, '@');
1266         if (!at || (g_utf8_strlen (at, -1) < 2))
1267                 return NULL;
1268                 
1269         gchar* domain = g_utf8_next_char (at);
1270         if(!domain)
1271                 return NULL;
1272                 
1273         const gchar* hostname = NULL;
1274         if (servertype == MODEST_PROTOCOL_STORE_POP)
1275                 hostname = "pop";
1276         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
1277                 hostname = "imap";
1278         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
1279                 hostname = "smtp";
1280         
1281         if (!hostname)
1282                 return NULL;
1283                 
1284         return g_strdup_printf ("%s.%s", hostname, domain);
1285 }
1286
1287 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
1288 {
1289         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(account_wizard);
1290
1291         if (!account_wizard->entry_incomingserver)
1292                 return;
1293                 
1294         /* Set a default domain for the server, based on the email address,
1295          * if no server name was already specified.
1296          */
1297         if (account_wizard->entry_user_email
1298             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1299                 const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
1300                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1301                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1302                 
1303                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
1304
1305                 /* Do not set the INCOMING_CHANGED flag because of this edit */
1306                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1307                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
1308                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1309
1310                 g_free (servername);
1311         }
1312         
1313         /* Set a default domain for the server, based on the email address,
1314          * if no server name was already specified.
1315          */
1316         if (!account_wizard->entry_outgoingserver)
1317                 return;
1318                 
1319         if (account_wizard->entry_user_email
1320             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1321                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1322                 
1323                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
1324
1325                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1326                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1327                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
1328                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1329
1330                 g_free (servername);
1331         }
1332 }
1333
1334 static gchar*
1335 get_entered_account_title (ModestEasysetupWizardDialog *account_wizard)
1336 {
1337         const gchar* account_title = 
1338                 gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
1339         if (!account_title || (strlen (account_title) == 0))
1340                 return NULL;
1341         else {
1342                 /* Strip it of whitespace at the start and end: */
1343                 gchar *result = g_strdup (account_title);
1344                 result = g_strstrip (result);
1345                 
1346                 if (!result)
1347                         return NULL;
1348                         
1349                 if (strlen (result) == 0) {
1350                         g_free (result);
1351                         return NULL;    
1352                 }
1353                 
1354                 return result;
1355         }
1356 }
1357
1358 static gboolean
1359 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1360 {
1361         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1362         
1363         /* Do extra validation that couldn't be done for every key press,
1364          * either because it was too slow,
1365          * or because it requires interaction:
1366          */
1367         if (current_page == account_wizard->page_account_details) {     
1368                 /* Check that the title is not already in use: */
1369                 gchar* account_title = get_entered_account_title (account_wizard);
1370                 if (!account_title)
1371                         return FALSE;
1372                         
1373                 /* Aavoid a clash with an existing display name: */
1374                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1375                         account_wizard->account_manager, account_title);
1376
1377                 if (name_in_use) {
1378                         /* Warn the user via a dialog: */
1379                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1380             
1381                         return FALSE;
1382                 }
1383         }
1384         else if (current_page == account_wizard->page_user_details) {
1385                 /* Check that the email address is valud: */
1386                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1387                 if ((!email_address) || (strlen(email_address) == 0))
1388                         return FALSE;
1389                         
1390                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1391                         /* Warn the user via a dialog: */
1392                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1393                                              
1394                         /* Return focus to the email address entry: */
1395                         gtk_widget_grab_focus (account_wizard->entry_user_email);
1396                         gtk_editable_select_region (GTK_EDITABLE (account_wizard->entry_user_email), 0, -1);
1397
1398                         return FALSE;
1399                 }
1400                 
1401                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1402                 create_subsequent_pages (account_wizard);
1403         }
1404         
1405         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1406          * but does not specify how.
1407          */
1408           
1409         if(next_page == account_wizard->page_custom_incoming) {
1410                 set_default_custom_servernames (account_wizard);
1411         }
1412         else if (next_page == account_wizard->page_custom_outgoing) {
1413                 set_default_custom_servernames (account_wizard);
1414     /* Check if the server supports secure authentication */
1415                 const ModestConnectionProtocol security_incoming = 
1416                         modest_serversecurity_combo_box_get_active_serversecurity (
1417                                                                                                                                                                                                                                                                  MODEST_SERVERSECURITY_COMBO_BOX (
1418                                                                                                                                                                                                                                                                                                                                                                                                         account_wizard->combo_incoming_security));
1419                 if (gtk_toggle_button_get_active (
1420                         GTK_TOGGLE_BUTTON (account_wizard->checkbox_incoming_auth))
1421                                 && !modest_protocol_info_is_secure(security_incoming))
1422                 {
1423                                 GList* methods = check_for_supported_auth_methods(account_wizard);
1424                                 if (!methods)
1425                                 {
1426                                         g_list_free(methods);
1427                                         return FALSE;
1428                                 }
1429                                 g_list_free(methods);
1430                 }
1431         }
1432         
1433         /* If this is the last page, and this is a click on Finish, 
1434          * then attempt to create the dialog.
1435          */
1436         if(!next_page) /* This is NULL when this is a click on Finish. */
1437         {
1438                 if (account_wizard->saved_account_name) {
1439                         /* Just enable the already-saved account (temporarily created when 
1440                          * editing advanced settings): */
1441                         modest_account_mgr_set_enabled (account_wizard->account_manager, 
1442                                                         account_wizard->saved_account_name, TRUE);
1443                 } else {
1444                         create_account (account_wizard, TRUE /* enabled */);
1445                 }
1446         }
1447         
1448         
1449         return TRUE;
1450 }
1451
1452 static gboolean entry_is_empty (GtkWidget *entry)
1453 {
1454         if (!entry)
1455                 return FALSE;
1456                 
1457         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1458         if ((!text) || (strlen(text) == 0))
1459                 return TRUE;
1460         else {
1461                 /* Strip it of whitespace at the start and end: */
1462                 gchar *stripped = g_strdup (text);
1463                 stripped = g_strstrip (stripped);
1464                 
1465                 if (!stripped)
1466                         return TRUE;
1467                         
1468                 const gboolean result = (strlen (stripped) == 0);
1469                 
1470                 g_free (stripped);
1471                 return result;
1472         }
1473 }
1474
1475 static void
1476 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1477 {
1478         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1479         
1480         gboolean enable_next = TRUE;
1481         if (current_page == account_wizard->page_welcome) {
1482                 enable_next = TRUE;
1483         }
1484         else if (current_page == account_wizard->page_account_details) {
1485                 /* The account details title is mandatory: */
1486                 if (entry_is_empty(account_wizard->entry_account_title))
1487                         enable_next = FALSE;
1488         }
1489         else if (current_page == account_wizard->page_user_details) {   
1490                 /* The user details username is mandatory: */
1491                 if (entry_is_empty(account_wizard->entry_user_username))
1492                         enable_next = FALSE;
1493                         
1494                 /* The user details email address is mandatory: */
1495                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1496                         enable_next = FALSE;
1497         }
1498         else if (current_page == account_wizard->page_custom_incoming) {
1499                 /* The custom incoming server is mandatory: */
1500                 if (entry_is_empty(account_wizard->entry_incomingserver))
1501                         enable_next = FALSE;
1502         }
1503                         
1504         /* Enable the buttons, 
1505          * identifying them via their associated response codes: */
1506                                    
1507         /* Disable the Finish button until we are on the last page,
1508          * because HildonWizardDialog enables this for all but the first page: */
1509         GtkNotebook *notebook = NULL;
1510         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1511         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1512         
1513         gint current = gtk_notebook_get_current_page (notebook);
1514         gint last = gtk_notebook_get_n_pages (notebook) - 1;
1515         const gboolean is_last = (current == last);
1516     
1517         if(!is_last) {
1518                 gtk_dialog_set_response_sensitive (dialog_base,
1519                                                    MODEST_WIZARD_DIALOG_FINISH,
1520                                                    FALSE);
1521         } else
1522         {
1523                 /* Disable Next on the last page: */
1524                 enable_next = FALSE;
1525         }
1526         
1527         gtk_dialog_set_response_sensitive (dialog_base,
1528                                            MODEST_WIZARD_DIALOG_NEXT,
1529                                            enable_next);
1530 }
1531
1532 static void
1533 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1534 {
1535         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1536         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1537
1538
1539         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1540         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1541         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1542         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1543         
1544         /* Provide a vfunc implementation so we can decide 
1545          * when to enable/disable the prev/next buttons.
1546          */
1547         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1548         base_klass->before_next = on_before_next;
1549         base_klass->enable_buttons = on_enable_buttons;
1550 }
1551  
1552 static void
1553 show_error (GtkWidget *parent_widget, const gchar* text)
1554 {
1555         //TODO: Apparently this doesn't show anything in Maemo Bora:
1556         hildon_banner_show_information(parent_widget, NULL, text);
1557         
1558 #if 0
1559         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text)); */
1560         /*
1561           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1562           (GtkDialogFlags)0,
1563           GTK_MESSAGE_ERROR,
1564           GTK_BUTTONS_OK,
1565           text ));
1566         */
1567                  
1568         gtk_dialog_run (dialog);
1569         gtk_widget_destroy (GTK_WIDGET (dialog));
1570 #endif
1571 }
1572
1573 /** Attempt to create the account from the information that the user has entered.
1574  * @result: TRUE if the account was successfully created.
1575  */
1576 gboolean
1577 create_account (ModestEasysetupWizardDialog *self, gboolean enabled)
1578 {
1579         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1580         
1581         gchar* display_name = get_entered_account_title (self);
1582
1583         /* Some checks: */
1584         if (!display_name)
1585                 return FALSE;
1586                 
1587         /* We should have checked for this already, 
1588          * and changed that name accordingly, 
1589          * but let's check again just in case:
1590          */
1591         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) {
1592                 g_free (display_name);
1593                 return FALSE;
1594         }
1595
1596         /* Increment the non-user visible name if necessary, 
1597          * based on the display name: */
1598         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1599         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1600                                                                           account_name_start, FALSE /* not a server account */);
1601         g_free (account_name_start);
1602                 
1603         /* username and password (for both incoming and outgoing): */
1604         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1605         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1606         /* Incoming server: */
1607         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
1608          * or modest_account_mgr_add_server_account will fail. */
1609         gchar* servername_incoming = NULL;
1610         guint serverport_incoming = 0;
1611         ModestTransportStoreProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1612         ModestConnectionProtocol protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_NORMAL;
1613         ModestAuthProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1614
1615         /* Get details from the specified presets: */
1616         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1617                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1618         if (provider_id) {
1619                 /* Use presets: */
1620                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1621                                                                  TRUE /* incoming */);
1622                 
1623                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets,
1624                                                                                                    provider_id, 
1625                                                                                                    TRUE /* incoming */);
1626                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets,
1627                                                                                                    provider_id, 
1628                                                                                                    TRUE /* incoming */);
1629
1630                 g_warning ("security incoming: %x", security_incoming);
1631                         
1632                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1633                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
1634                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1635                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
1636                         protocol_incoming = MODEST_PROTOCOL_STORE_POP; 
1637                 }
1638                 serverport_incoming = get_serverport_incoming(servertype_incoming, security_incoming);
1639                 
1640                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1641                         protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1642                 
1643                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1644                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1645         }
1646         else {
1647                 /* Use custom pages because no preset was specified: */
1648                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1649                 
1650                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1651                         EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1652                 
1653                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1654                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1655                 
1656                 /* The UI spec says:
1657                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1658                  * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1659                  */
1660                 
1661                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) &&
1662                                 !modest_protocol_info_is_secure(protocol_security_incoming))
1663                 {
1664                                 GList* methods = check_for_supported_auth_methods(self);
1665                                 if (!methods) {
1666                                         g_free (display_name);
1667                                         return FALSE;
1668                                 }
1669                                 else
1670                                   protocol_authentication_incoming = (ModestAuthProtocol) (GPOINTER_TO_INT(methods->data));
1671                 }
1672                 else
1673                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1674         }
1675         
1676         /* First we add the 2 server accounts, and then we add the account that uses them.
1677          * If we don't do it in this order then we will experience a crash. */
1678          
1679         /* Add a (incoming) server account, to be used by the account: */
1680         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1681         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1682                                                                         store_name_start, TRUE /* server account */);
1683         g_free (store_name_start);
1684         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1685                                                                   store_name,
1686                                                                   servername_incoming,
1687                                                                   serverport_incoming,
1688                                                                   username, password,
1689                                                                   protocol_incoming,
1690                                                                   protocol_security_incoming,
1691                                                                   protocol_authentication_incoming);            
1692                 
1693         g_free (servername_incoming);
1694         
1695         if (!created) {
1696                 /* TODO: Provide a Logical ID for the text: */
1697                 show_error (GTK_WIDGET (self), _("An error occurred while creating the incoming account."));
1698                 g_free (display_name);
1699                 return FALSE;   
1700         }
1701         
1702         /* Outgoing server: */
1703         gchar* servername_outgoing = NULL;
1704         ModestTransportStoreProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1705         ModestConnectionProtocol protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1706         ModestAuthProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1707         guint serverport_outgoing = 0;
1708         
1709         if (provider_id) {
1710                 /* Use presets: */
1711                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1712                                                                  FALSE /* incoming */);
1713                         
1714                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets,
1715                                                                                                    provider_id, 
1716                                                                                                    FALSE /* incoming */);
1717                 
1718                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1719                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1720                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1721                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1722                 
1723                 ModestPresetsSecurity security_outgoing = 
1724                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1725                                                                  FALSE /* incoming */);
1726
1727                 /* TODO: The secure-smtp information in the presets data is currently wrong,
1728                  * so we choose a reasonable default. Remove this when the presets data is corrected: */
1729                 security_outgoing = security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP;
1730
1731                 protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1732                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP) {
1733                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
1734                         protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1735                         serverport_outgoing = 465;
1736                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_PASSWORD;
1737                 } else {
1738                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
1739                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1740                 }
1741         }
1742         else {
1743                 /* Use custom pages because no preset was specified: */
1744                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1745                 
1746                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1747
1748                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1749                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1750                 
1751                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1752                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1753         }
1754             
1755         /* Add a (outgoing) server account to be used by the account: */
1756         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1757         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1758                                                                             transport_name_start, TRUE /* server account */);
1759         g_free (transport_name_start);
1760         created = modest_account_mgr_add_server_account (self->account_manager,
1761                                                          transport_name,
1762                                                          servername_outgoing,
1763                                                          serverport_outgoing,
1764                                                          username, password,
1765                                                          protocol_outgoing,
1766                                                          protocol_security_outgoing,
1767                                                          protocol_authentication_outgoing);
1768                 
1769         g_free (servername_outgoing);
1770                 
1771         if (!created) {
1772                 /* TODO: Provide a Logical ID for the text: */
1773                 show_error (GTK_WIDGET (self), _("An error occurred while creating the outgoing account."));
1774                 g_free (display_name);
1775                 return FALSE;   
1776         }
1777         
1778         
1779         /* Create the account, which will contain the two "server accounts": */
1780         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1781                                                   store_name, /* The name of our POP/IMAP server account. */
1782                                                   transport_name, /* The name of our SMTP server account. */
1783                                                   enabled);
1784         g_free (store_name);
1785         g_free (transport_name);
1786         
1787         if (!created) {
1788                 /* TODO: Provide a Logical ID for the text: */
1789                 show_error (GTK_WIDGET (self), _("An error occurred while creating the account."));
1790                 g_free (display_name);
1791                 return FALSE;   
1792         }
1793
1794         /* Sanity check: */
1795         /* There must be at least one account now: */
1796         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
1797          * fixed in versions after 3.1. */
1798         if(!modest_account_mgr_has_accounts (self->account_manager, FALSE))
1799                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1800                 
1801         /* The user name and email address must be set additionally: */
1802         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1803         modest_account_mgr_set_string (self->account_manager, account_name,
1804                                        MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1805
1806         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1807         modest_account_mgr_set_string (self->account_manager, account_name,
1808                                        MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1809
1810         /* Set the display name: */
1811         modest_account_mgr_set_string (self->account_manager, account_name,
1812                                        MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1813
1814         /* Set retrieve type */ 
1815         const gchar *retrieve = MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1816         modest_account_mgr_set_string (self->account_manager, account_name,
1817                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1818
1819         /* Save the connection-specific SMTP server accounts. */
1820         modest_account_mgr_set_use_connection_specific_smtp(self->account_manager, account_name,
1821                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->checkbox_outgoing_smtp_specific)));
1822         gboolean result = TRUE;
1823         if (self->specific_window)
1824                 result = modest_connection_specific_smtp_window_save_server_accounts (
1825                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1826                         
1827         g_free (self->saved_account_name);
1828         self->saved_account_name = g_strdup (account_name);
1829         
1830         g_free (account_name);
1831         g_free (display_name);
1832         
1833         return result;
1834 }