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