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