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