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