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