c092e6da9f3766aced4cde25769612fc19e1ee4c
[modest] / src / hildon2 / 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 #include <config.h>
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/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkseparator.h>
42 #include "modest-country-picker.h"
43 #include "modest-provider-picker.h"
44 #include "modest-servertype-picker.h"
45 #include "widgets/modest-validating-entry.h"
46 #include "modest-text-utils.h"
47 #include "modest-conf.h"
48 #include "modest-defs.h"
49 #include "modest-account-mgr.h"
50 #include "modest-signal-mgr.h"
51 #include "modest-account-mgr-helpers.h"
52 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
53 #include "modest-connection-specific-smtp-window.h"
54 #include "widgets/modest-ui-constants.h"
55 #include "widgets/modest-default-account-settings-dialog.h"
56 #include "widgets/modest-easysetup-wizard-page.h"
57 #include "modest-maemo-utils.h"
58 #include "modest-utils.h"
59 #include "modest-hildon-includes.h"
60 #include "modest-maemo-security-options-view.h"
61 #include "modest-account-protocol.h"
62
63 /* Include config.h so that _() works: */
64 #ifdef HAVE_CONFIG_H
65 #include <config.h>
66 #endif
67
68 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
69
70 #define MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
71                                                     MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, \
72                                                     ModestEasysetupWizardDialogPrivate))
73
74 #define LABELS_WIDTH 480
75 #define DIALOG_WIDTH LABELS_WIDTH + MODEST_MARGIN_DOUBLE
76
77 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
78
79
80 typedef enum {
81         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
82         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
83 } ModestEasysetupWizardDialogServerChanges;
84
85 struct _ModestEasysetupWizardDialogPrivate
86 {
87         ModestPresets *presets;
88
89         /* Remember what fields the user edited manually to not prefill them
90          * again. */
91         ModestEasysetupWizardDialogServerChanges server_changes;
92         
93         /* Check if the user changes a field to show a confirmation dialog */
94         gboolean dirty;
95
96         /* If we have a pending load of settings or not. */
97         gboolean pending_load_settings;
98         
99         /* Used by derived widgets to query existing accounts,
100          * and to create new accounts: */
101         ModestAccountMgr *account_manager;
102         ModestAccountSettings *settings;
103         
104         /* notebook pages: */
105         GtkWidget *page_welcome;
106         GtkWidget *check_support_progress;
107         gboolean  check_support_done;
108         guint check_support_show_progress_id;
109         guint check_support_progress_pulse_id;
110         gint pending_check_support;
111         gboolean destroyed;
112         
113         GtkWidget *page_account_details;
114         GtkWidget *account_country_picker;
115         GtkWidget *account_serviceprovider_picker;
116         GtkWidget *entry_account_title;
117         
118         GtkWidget *page_user_details;
119         GtkWidget *entry_user_name;
120         GtkWidget *entry_user_username;
121         GtkWidget *entry_user_password;
122         GtkWidget *entry_user_email;
123         
124         GtkWidget *page_complete_easysetup;
125         
126         GtkWidget *page_custom_incoming;
127         GtkWidget *incoming_servertype_picker;
128         GtkWidget *caption_incoming;
129         GtkWidget *entry_incomingserver;
130
131         /* Security widgets */
132         GtkWidget *incoming_security;
133         GtkWidget *outgoing_security;
134
135         GtkWidget *page_custom_outgoing;
136         GtkWidget *entry_outgoingserver;
137         GtkWidget *checkbox_outgoing_smtp_specific;
138         GtkWidget *button_outgoing_smtp_servers;
139         
140         GtkWidget *page_complete_customsetup;
141
142         ModestProtocolType last_plugin_protocol_selected;
143         GSList *missing_data_signals;
144 };
145
146 static void save_to_settings (ModestEasysetupWizardDialog *self);
147 static void real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next);
148 static void on_update_model (ModestWizardDialog *dialog);
149 static gboolean on_save (ModestWizardDialog *dialog);
150 static GList* check_for_supported_auth_methods (ModestEasysetupWizardDialog* self);
151 static gboolean check_has_supported_auth_methods(ModestEasysetupWizardDialog* self);
152 static void check_support_callback (ModestAccountProtocol *protocol,
153                                     gboolean supported,
154                                     gpointer userdata);
155 static void check_support_of_protocols (ModestEasysetupWizardDialog *self);
156 static gboolean check_support_show_progress (gpointer userdata);
157 static gboolean check_support_progress_pulse (gpointer userdata);
158
159 static gboolean
160 on_delete_event (GtkWidget *widget,
161                  GdkEvent *event,
162                  ModestEasysetupWizardDialog *wizard)
163 {
164         gtk_dialog_response (GTK_DIALOG (wizard), GTK_RESPONSE_CANCEL);
165         return TRUE;
166 }
167
168 static void
169 on_easysetup_changed(GtkWidget* widget, ModestEasysetupWizardDialog* wizard)
170 {
171         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard);
172         g_return_if_fail (priv != NULL);
173         priv->dirty = TRUE;
174 }
175
176 static void
177 modest_easysetup_wizard_dialog_dispose (GObject *object)
178 {
179         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (object);
180         priv->destroyed = TRUE;
181
182         if (priv->check_support_show_progress_id > 0) {
183                 g_source_remove (priv->check_support_show_progress_id);
184                 priv->check_support_show_progress_id = 0;
185         }
186
187         if (priv->check_support_progress_pulse_id > 0) {
188                 g_source_remove (priv->check_support_progress_pulse_id);
189                 priv->check_support_progress_pulse_id = 0;
190         }
191
192         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
193                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
194 }
195
196 static void
197 modest_easysetup_wizard_dialog_finalize (GObject *object)
198 {
199         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
200         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
201
202         if (priv->account_manager)
203                 g_object_unref (G_OBJECT (priv->account_manager));
204
205         if (priv->presets)
206                 modest_presets_destroy (priv->presets);
207
208         if (priv->settings)
209                 g_object_unref (priv->settings);
210
211         if (priv->missing_data_signals) {
212                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
213                 priv->missing_data_signals = NULL;
214         }
215
216         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
217 }
218
219 static void
220 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
221
222 static void
223 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
224
225 static void on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data);
226
227 static gint 
228 get_port_from_protocol (ModestProtocolType server_type,
229                         gboolean alternate_port)
230 {
231         int server_port = 0;
232         ModestProtocol *protocol;
233         ModestProtocolRegistry *protocol_registry;
234
235         protocol_registry = modest_runtime_get_protocol_registry ();
236         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, server_type);
237
238         if (alternate_port) {
239                 server_port = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
240         } else {
241                 server_port = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
242         }
243         return server_port;
244 }
245
246 static void
247 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
248 {
249         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
250
251         /* Call the vfunc, which may be overridden by derived classes: */
252         if (klass->enable_buttons) {
253                 GtkNotebook *notebook = NULL;
254                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
255
256                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
257                 if (current_page_num == -1)
258                         return;
259
260                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
261                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
262         }
263 }
264
265 static void
266 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
267 {
268         ModestEasysetupWizardDialog *self;
269         gboolean mandatory, empty;
270         ModestEasysetupWizardDialogPrivate *priv;
271
272         g_return_if_fail (user_data);
273
274         self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
275         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
276
277         invoke_enable_buttons_vfunc(self);
278
279         empty = !g_utf8_collate (gtk_entry_get_text (GTK_ENTRY (editable)), "");
280         mandatory = ((GtkWidget *) editable == priv->entry_account_title) ||
281                 ((GtkWidget *) editable == priv->entry_user_username) || 
282                 ((GtkWidget *) editable == priv->entry_user_email);
283
284         /* Show a banner & get focus */
285         if (empty && mandatory) {
286                 modest_platform_information_banner ((GtkWidget *) editable, NULL, 
287                                                     _CS("ckct_ib_enter_some_text"));
288                 gtk_widget_grab_focus (GTK_WIDGET (editable));
289         }
290 }
291
292 static void
293 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
294 {
295         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
296         g_assert(self);
297         invoke_enable_buttons_vfunc(self);
298 }
299
300 static void
301 on_picker_button_value_changed (HildonPickerButton *widget, gpointer user_data)
302 {
303         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
304         g_assert(self);
305         invoke_enable_buttons_vfunc(self);
306 }
307
308 static void
309 on_serviceprovider_picker_button_value_changed (HildonPickerButton *widget, gpointer user_data)
310 {
311         gchar* default_account_name_start;
312         gchar* default_account_name;
313         ModestEasysetupWizardDialog *self;
314         ModestEasysetupWizardDialogPrivate *priv;
315         ModestProviderPickerIdType provider_id_type;
316
317         self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
318         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
319
320         on_picker_button_value_changed (widget, user_data);
321
322         provider_id_type = modest_provider_picker_get_active_id_type (
323                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
324         if (provider_id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
325                 default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
326         } else {
327                 GtkWidget *selector;
328
329                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (widget)));
330                 default_account_name_start = 
331                         g_strdup (hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
332                 
333         }
334         default_account_name = modest_account_mgr_get_unused_account_display_name (
335                 priv->account_manager, default_account_name_start);
336         g_free (default_account_name_start);
337         default_account_name_start = NULL;
338
339         hildon_entry_set_text (HILDON_ENTRY (priv->entry_account_title), default_account_name);
340         g_free (default_account_name);
341 }
342
343 /** This is a convenience function to create a caption containing a mandatory widget.
344  * When the widget is edited, the enable_buttons() vfunc will be called.
345  */
346 static GtkWidget* 
347 create_captioned (ModestEasysetupWizardDialog *self,
348                   GtkSizeGroup *title_size_group,
349                   GtkSizeGroup *value_size_group,
350                   const gchar *value,
351                   gboolean use_markup,
352                   GtkWidget *control)
353 {
354
355         GtkWidget *result;
356         result = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
357                                                       value, use_markup, control);
358
359         /* Connect to the appropriate changed signal for the widget, 
360          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
361          */
362         if (GTK_IS_ENTRY (control)) {
363                 g_signal_connect (G_OBJECT (control), "changed",
364                                   G_CALLBACK (on_caption_entry_changed), self);
365                 
366         }
367         else if (GTK_IS_COMBO_BOX (control)) {
368                 g_signal_connect (G_OBJECT (control), "changed",
369                                   G_CALLBACK (on_caption_combobox_changed), self);
370         }
371          
372         return result;
373 }
374            
375 static GtkWidget*
376 create_page_welcome (ModestEasysetupWizardDialog *self)
377 {
378         GtkWidget *box;
379         GtkWidget *label; 
380         ModestEasysetupWizardDialogPrivate *priv;
381
382         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
383         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
384         label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
385         priv->check_support_progress = gtk_progress_bar_new ();
386         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
387         /* So that it is not truncated: */
388         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
389         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
390         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
391         gtk_box_pack_end (GTK_BOX (box), priv->check_support_progress, FALSE, FALSE, 0);
392         gtk_widget_show (label);
393         gtk_widget_show (GTK_WIDGET (box));
394         return GTK_WIDGET (box);
395 }
396
397 static void
398 on_account_country_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
399 {
400         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
401         g_assert(self);
402         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
403         
404         priv->dirty = TRUE;
405         
406         /* Fill the providers picker, based on the selected country: */
407         if (priv->presets != NULL) {
408                 gint mcc = modest_country_picker_get_active_country_mcc (
409                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
410                 modest_provider_picker_fill (
411                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker), priv->presets, mcc);
412         }
413 }
414
415 static void
416 update_user_email_from_provider (ModestEasysetupWizardDialog *self)
417 {
418         ModestEasysetupWizardDialogPrivate *priv; 
419         gchar* provider_id = NULL, *with_at = NULL;
420
421         g_assert(self);
422         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
423
424         /* Fill the providers combo, based on the selected country: */
425         provider_id = modest_provider_picker_get_active_provider_id (
426                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
427
428         if (provider_id) {
429                 gchar *domain_name = modest_presets_get_domain (priv->presets, provider_id);
430                 with_at = g_strdup_printf ("%c%s", '@', domain_name);
431                 g_free (domain_name);
432         }
433
434         /* The sample address already contains the '@' */
435         if (!with_at)
436                 with_at = g_strdup (MODEST_EXAMPLE_EMAIL_ADDRESS);
437
438         if (priv->entry_user_email)
439                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_email), with_at);
440
441         g_free (with_at);
442         g_free (provider_id);
443 }
444
445 static void
446 on_account_serviceprovider_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
447 {
448         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
449         g_assert(self);
450         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
451         
452         priv->dirty = TRUE;
453
454         update_user_email_from_provider (self);
455 }
456
457 static void
458 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
459 {
460         modest_platform_information_banner (GTK_WIDGET (self), NULL,
461                                             _CS("ckdg_ib_maximum_characters_reached"));
462 }
463
464 static GtkWidget*
465 create_page_account_details (ModestEasysetupWizardDialog *self)
466 {
467         GtkWidget *caption;
468         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
469         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
470         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
471
472         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
473         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
474         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
475         gtk_widget_show (label);
476
477         /* Create a size group to be used by all captions.
478          * Note that HildonCaption does not create a default size group if we do not specify one.
479          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
480         GtkSizeGroup* title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
481         GtkSizeGroup* value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
482
483         /* The country widgets: */
484         priv->account_country_picker = GTK_WIDGET (modest_country_picker_new (MODEST_EDITABLE_SIZE,
485                                                                               HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
486         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup, 
487                                                _("mcen_fi_country"), priv->account_country_picker);
488         g_signal_connect (G_OBJECT (priv->account_country_picker), "value-changed",
489                           G_CALLBACK (on_picker_button_value_changed), self);
490         gtk_box_pack_start (GTK_BOX (box), priv->account_country_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
491         gtk_widget_show (priv->account_country_picker);
492         
493         GtkWidget *separator = gtk_hseparator_new ();
494         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
495         gtk_widget_show (separator);
496             
497         /* The service provider widgets: */     
498         priv->account_serviceprovider_picker = GTK_WIDGET (modest_provider_picker_new (MODEST_EDITABLE_SIZE,
499                                                                                        HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
500         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
501                                                _("mcen_fi_serviceprovider"), 
502                                                priv->account_serviceprovider_picker);
503         g_signal_connect (G_OBJECT (priv->account_serviceprovider_picker), "value-changed",
504                           G_CALLBACK (on_serviceprovider_picker_button_value_changed), self);
505         gtk_box_pack_start (GTK_BOX (box), priv->account_serviceprovider_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
506         gtk_widget_show (priv->account_serviceprovider_picker);
507         
508         /* The description widgets: */  
509         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
510         g_signal_connect(G_OBJECT(priv->entry_account_title), "changed",
511                          G_CALLBACK(on_easysetup_changed), self);
512         /* Do use auto-capitalization: */
513         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_account_title), 
514                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
515         
516         /* Set a default account title, choosing one that does not already exist: */
517         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
518         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
519         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
520                 priv->account_manager, default_account_name_start);
521         g_free (default_account_name_start);
522         default_account_name_start = NULL;
523         
524         hildon_entry_set_text (HILDON_ENTRY (priv->entry_account_title), default_account_name);
525         g_free (default_account_name);
526         default_account_name = NULL;
527
528         caption = create_captioned (self, title_sizegroup, value_sizegroup, _("mcen_fi_account_title"), FALSE,
529                                     priv->entry_account_title);
530         gtk_widget_show (priv->entry_account_title);
531         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
532         gtk_widget_show (caption);
533         
534         /* Prevent the use of some characters in the account title, 
535          * as required by our UI specification: */
536         GList *list_prevent = NULL;
537         list_prevent = g_list_append (list_prevent, "\\");
538         list_prevent = g_list_append (list_prevent, "/");
539         list_prevent = g_list_append (list_prevent, ":");
540         list_prevent = g_list_append (list_prevent, "*");
541         list_prevent = g_list_append (list_prevent, "?");
542         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
543         list_prevent = g_list_append (list_prevent, "“");
544         list_prevent = g_list_append (list_prevent, "<"); 
545         list_prevent = g_list_append (list_prevent, ">"); 
546         list_prevent = g_list_append (list_prevent, "|");
547         list_prevent = g_list_append (list_prevent, "^");       
548         modest_validating_entry_set_unallowed_characters (
549                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
550         g_list_free (list_prevent);
551         list_prevent = NULL;
552         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
553                                                                                                                                          modest_utils_on_entry_invalid_character, self);
554         
555         /* Set max length as in the UI spec:
556          * The UI spec seems to want us to show a dialog if we hit the maximum. */
557         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
558         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title), 
559                                               on_entry_max, self);
560         
561         gtk_widget_show (GTK_WIDGET (box));
562
563         g_object_unref (title_sizegroup);
564         g_object_unref (value_sizegroup);
565         
566         return GTK_WIDGET (box);
567 }
568
569 static GtkWidget*
570 create_page_user_details (ModestEasysetupWizardDialog *self)
571 {
572         GtkSizeGroup* title_sizegroup;
573         GtkSizeGroup* value_sizegroup;
574         GtkWidget *box;
575         ModestEasysetupWizardDialogPrivate *priv;
576
577         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
578         
579         /* Create a size group to be used by all captions.
580          * Note that HildonCaption does not create a default size group if we do not specify one.
581          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
582         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
583         title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
584         value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
585          
586         /* The name widgets: (use auto cap) */
587         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
588         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name), 
589                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
590         
591         /* Set max length as in the UI spec:
592          * The UI spec seems to want us to show a dialog if we hit the maximum. */
593         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
594         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
595                                               on_entry_max, self);
596         GtkWidget *caption = create_captioned (self, title_sizegroup, value_sizegroup,
597                                                _("mcen_li_emailsetup_name"), FALSE, priv->entry_user_name);
598         g_signal_connect(G_OBJECT(priv->entry_user_name), "changed", 
599                          G_CALLBACK(on_easysetup_changed), self);
600         gtk_widget_show (priv->entry_user_name);
601         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
602         gtk_widget_show (caption);
603         
604         /* Prevent the use of some characters in the name, 
605          * as required by our UI specification: */
606         GList *list_prevent = NULL;
607         list_prevent = g_list_append (list_prevent, "<");
608         list_prevent = g_list_append (list_prevent, ">");
609         modest_validating_entry_set_unallowed_characters (
610                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
611         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
612                 modest_utils_on_entry_invalid_character, self);
613         g_list_free (list_prevent);
614         
615         /* The username widgets: */     
616         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
617         /* Auto-capitalization is the default, so let's turn it off: */
618         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), 
619                                          HILDON_GTK_INPUT_MODE_FULL);
620         caption = create_captioned (self, title_sizegroup, value_sizegroup, _("mail_fi_username"), FALSE,
621                                     priv->entry_user_username);
622         gtk_widget_show (priv->entry_user_username);
623         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
624         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", 
625                          G_CALLBACK(on_easysetup_changed), self);
626         gtk_widget_show (caption);
627         
628         /* Prevent the use of some characters in the username, 
629          * as required by our UI specification: */
630         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_username),
631                 modest_utils_on_entry_invalid_character, self);
632         
633         /* Set max length as in the UI spec:
634          * The UI spec seems to want us to show a dialog if we hit the maximum. */
635         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
636         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
637                                               on_entry_max, self);
638         
639         /* The password widgets: */     
640         priv->entry_user_password = hildon_entry_new (MODEST_EDITABLE_SIZE);
641         /* Auto-capitalization is the default, so let's turn it off: */
642         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
643                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
644         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
645         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
646         caption = create_captioned (self, title_sizegroup, value_sizegroup,
647                                     _("mail_fi_password"), FALSE, priv->entry_user_password);
648         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", 
649                          G_CALLBACK(on_easysetup_changed), self);
650         gtk_widget_show (priv->entry_user_password);
651         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
652         gtk_widget_show (caption);
653         
654         /* The email address widgets: */        
655         priv->entry_user_email = 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_email), HILDON_GTK_INPUT_MODE_FULL);
658         caption = create_captioned (self, title_sizegroup, value_sizegroup,
659                                     _("mcen_li_emailsetup_email_address"), FALSE, priv->entry_user_email);
660         update_user_email_from_provider (self);
661         gtk_widget_show (priv->entry_user_email);
662         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
663         g_signal_connect(G_OBJECT(priv->entry_user_email), "changed", 
664                          G_CALLBACK(on_easysetup_changed), self);
665         gtk_widget_show (caption);
666         
667         /* Set max length as in the UI spec:
668          * The UI spec seems to want us to show a dialog if we hit the maximum. */
669         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
670         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
671                                               on_entry_max, self);
672         
673         
674         gtk_widget_show (GTK_WIDGET (box));
675         g_object_unref (title_sizegroup);
676         g_object_unref (value_sizegroup);
677
678         return GTK_WIDGET (box);
679 }
680
681 static GtkWidget*
682 create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
683 {
684         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
685
686         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
687         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
688         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
689         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
690
691         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
692         gtk_widget_show (label);
693
694         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
695         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
696         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
697
698         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
699         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
700         gtk_widget_show (label);
701
702         gtk_widget_show (GTK_WIDGET (box));
703         return GTK_WIDGET (box);
704 }
705
706 /** Change the caption title for the incoming server, 
707  * as specified in the UI spec:
708  */
709 static void 
710 update_incoming_server_title (ModestEasysetupWizardDialog *self)
711 {
712         ModestEasysetupWizardDialogPrivate* priv; 
713         ModestProtocolType protocol_type; 
714         ModestProtocolRegistry *protocol_registry;
715
716         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
717         protocol_registry = modest_runtime_get_protocol_registry ();
718
719         protocol_type = modest_servertype_picker_get_active_servertype (
720                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
721
722         /* This could happen when the combo box has still no active iter */
723         if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
724                 gchar* incomingserver_title;
725                 const gchar *protocol_display_name;
726                 ModestProtocol *protocol;
727
728                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, 
729                                                                           protocol_type);
730                 protocol_display_name = modest_protocol_get_display_name (protocol);
731
732                 incomingserver_title = g_strconcat (_("mcen_li_emailsetup_servertype"), "\n<small>(",
733                                                     protocol_display_name, ")</small>", NULL);
734
735                 modest_maemo_utils_captioned_set_label (priv->caption_incoming, incomingserver_title, TRUE);
736
737                 g_free(incomingserver_title);
738         }
739 }
740
741 /** Change the caption title for the incoming server, 
742  * as specified in the UI spec:
743  */
744 static void 
745 update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
746 {
747         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
748         ModestServertypePicker *server_type_picker;
749         ModestProtocolType protocol_type;
750         ModestSecurityOptionsView *view;
751
752         server_type_picker = 
753                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker);
754         protocol_type = 
755                 modest_servertype_picker_get_active_servertype (server_type_picker);
756         
757         /* Fill the combo with appropriately titled choices for all
758            those protocols */
759         view = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
760         modest_security_options_view_set_server_type (view, protocol_type);
761 }
762
763 static void 
764 on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data)
765 {
766         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
767         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
768         ModestServertypePicker *picker;
769         ModestProtocolType protocol_type;
770
771         priv->dirty = TRUE;
772         
773         /* Update title */
774         update_incoming_server_title (self);
775
776         /* Update security options if needed */
777         picker = MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker);
778         protocol_type = modest_servertype_picker_get_active_servertype (picker);
779         update_incoming_server_security_choices (self);
780         gtk_widget_show (priv->incoming_security);
781
782         set_default_custom_servernames (self);
783 }
784
785 static void 
786 on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
787 {
788         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
789         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
790         priv->dirty = TRUE;
791         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
792 }
793
794 static GtkWidget* 
795 create_page_custom_incoming (ModestEasysetupWizardDialog *self)
796 {
797         ModestProtocolRegistry *protocol_registry;
798         ModestEasysetupWizardDialogPrivate* priv; 
799         GtkWidget *box; 
800         GtkWidget *pannable;
801         GtkWidget *label;
802         GtkSizeGroup *title_sizegroup;
803         GtkSizeGroup *value_sizegroup;
804
805         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
806         protocol_registry = modest_runtime_get_protocol_registry ();
807
808         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
809         pannable = hildon_pannable_area_new ();
810
811         /* Show note that account type cannot be changed in future: */
812         label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
813         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
814         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
815         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
816         gtk_widget_show (label);
817         
818         /* Create a size group to be used by all captions.
819          * Note that HildonCaption does not create a default size group if we do not specify one.
820          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
821         title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
822         value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
823          
824         /* The incoming server widgets: */
825         priv->incoming_servertype_picker = GTK_WIDGET (modest_servertype_picker_new (MODEST_EDITABLE_SIZE,
826                                                                                      HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
827                                                                                      TRUE));
828         hildon_button_set_title (HILDON_BUTTON (priv->incoming_servertype_picker), _("mcen_li_emailsetup_type"));
829         g_signal_connect (G_OBJECT (priv->incoming_servertype_picker), "value-changed",
830                           G_CALLBACK (on_picker_button_value_changed), self);
831         gtk_box_pack_start (GTK_BOX (box), priv->incoming_servertype_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
832         gtk_widget_show (priv->incoming_servertype_picker);
833         
834         priv->entry_incomingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
835         g_signal_connect(G_OBJECT(priv->entry_incomingserver), "changed", G_CALLBACK(on_easysetup_changed), self);
836         /* Auto-capitalization is the default, so let's turn it off: */
837         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
838         set_default_custom_servernames (self);
839
840         /* The caption title will be updated in update_incoming_server_title().
841          * so this default text will never be seen: */
842         priv->caption_incoming = create_captioned (self, title_sizegroup, value_sizegroup,
843                                                    "This will be removed", 
844                                                    FALSE, priv->entry_incomingserver);
845         update_incoming_server_title (self);
846         gtk_widget_show (priv->entry_incomingserver);
847         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
848         gtk_widget_show (priv->caption_incoming);
849         
850         /* Change the caption title when the servertype changes, 
851          * as in the UI spec: */
852         g_signal_connect (G_OBJECT (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (priv->incoming_servertype_picker))), 
853                           "changed",
854                           G_CALLBACK (on_servertype_selector_changed), self);
855
856         /* Remember when the servername was changed manually: */
857         g_signal_connect (G_OBJECT (priv->entry_incomingserver), "changed",
858                           G_CALLBACK (on_entry_incoming_servername_changed), self);
859
860         /* The secure connection widgets. These are only valid for
861            protocols with security */   
862         priv->incoming_security = 
863                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
864                                                         FALSE, title_sizegroup, value_sizegroup);
865         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security, 
866                             FALSE, FALSE, MODEST_MARGIN_HALF);
867         gtk_widget_show_all (priv->incoming_security);
868
869         /* Set default selection */
870         modest_servertype_picker_set_active_servertype (
871                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker), 
872                 MODEST_PROTOCOLS_STORE_POP);
873
874         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), box);
875         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box),
876                                              hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (pannable)));
877         gtk_widget_show (GTK_WIDGET (box));
878         gtk_widget_show (pannable);
879
880         g_object_unref (title_sizegroup);
881         g_object_unref (value_sizegroup);
882
883         return GTK_WIDGET (pannable);
884 }
885
886 static void
887 on_check_button_changed (HildonCheckButton *button, gpointer user_data)
888 {
889         GtkWidget *widget = GTK_WIDGET (user_data);
890         
891         /* Enable the widget only if the check button is active: */
892         const gboolean enable = hildon_check_button_get_active (button);
893         gtk_widget_set_sensitive (widget, enable);
894 }
895
896 /* Make the sensitivity of a widget depend on a check button.
897  */
898 static void
899 enable_widget_for_checkbutton (GtkWidget *widget, HildonCheckButton* button)
900 {
901         g_signal_connect (G_OBJECT (button), "toggled",
902                           G_CALLBACK (on_check_button_changed), widget);
903         
904         /* Set the starting sensitivity: */
905         on_check_button_changed (button, widget);
906 }
907
908 static void
909 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
910 {
911         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
912         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
913         GtkWidget *specific_window;
914         
915         /* We set dirty here because setting it depending on the connection specific dialog
916         seems overkill */
917         priv->dirty = TRUE;
918         
919         /* Create the window, if necessary: */
920         specific_window = (GtkWidget *) modest_connection_specific_smtp_window_new ();
921         modest_connection_specific_smtp_window_fill_with_connections (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), priv->account_manager);
922
923         /* Show the window */
924         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (specific_window), GTK_WINDOW (self));
925         gtk_widget_show (specific_window);
926 }
927
928 static void 
929 on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
930 {
931         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
932         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
933         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
934 }
935
936 static GtkWidget* 
937 create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
938 {
939         ModestEasysetupWizardDialogPrivate *priv;
940         gchar *smtp_caption_label;
941         GtkWidget *pannable;
942         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
943
944         pannable = hildon_pannable_area_new ();
945         
946         /* Create a size group to be used by all captions.
947          * Note that HildonCaption does not create a default size group if we do not specify one.
948          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
949         GtkSizeGroup *title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
950         GtkSizeGroup *value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
951          
952         /* The outgoing server widgets: */
953         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
954         priv->entry_outgoingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
955         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
956                   G_CALLBACK (on_easysetup_changed), self);
957         /* Auto-capitalization is the default, so let's turn it off: */
958         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
959         smtp_caption_label = g_strconcat (_("mcen_li_emailsetup_smtp"), "\n<small>(SMTP)</small>", NULL);
960         GtkWidget *caption = create_captioned (self, title_sizegroup, value_sizegroup,
961                                                smtp_caption_label, TRUE, priv->entry_outgoingserver);
962         g_free (smtp_caption_label);
963         gtk_widget_show (priv->entry_outgoingserver);
964         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
965         gtk_widget_show (caption);
966         set_default_custom_servernames (self);
967
968         /* The secure connection widgets. These are only valid for
969            protocols with security */   
970         priv->outgoing_security = 
971                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
972                                                         FALSE, title_sizegroup, value_sizegroup);
973         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security, 
974                             FALSE, FALSE, MODEST_MARGIN_HALF);
975         gtk_widget_show (priv->outgoing_security);
976
977         GtkWidget *separator = gtk_hseparator_new ();
978         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
979         gtk_widget_show (separator);
980
981         /* connection-specific checkbox: */
982         priv->checkbox_outgoing_smtp_specific = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
983         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->checkbox_outgoing_smtp_specific), 
984                                         FALSE);
985         gtk_button_set_label (GTK_BUTTON (priv->checkbox_outgoing_smtp_specific),
986                               _("mcen_fi_advsetup_connection_smtp"));
987         gtk_button_set_alignment (GTK_BUTTON (priv->checkbox_outgoing_smtp_specific),
988                                   0.0, 0.5);
989         g_signal_connect (G_OBJECT (priv->checkbox_outgoing_smtp_specific), "toggled",
990                   G_CALLBACK (on_easysetup_changed), self);
991
992         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
993         gtk_box_pack_start (GTK_BOX (box), priv->checkbox_outgoing_smtp_specific,
994                             FALSE, FALSE, MODEST_MARGIN_HALF);
995
996         /* Connection-specific SMTP-Severs Edit button: */
997         priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_advsetup_optional_smtp"));
998         hildon_gtk_widget_set_theme_size (priv->button_outgoing_smtp_servers, 
999                                           HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);  
1000         gtk_widget_show (priv->button_outgoing_smtp_servers);
1001         gtk_box_pack_start (GTK_BOX (box), priv->button_outgoing_smtp_servers, 
1002                             FALSE, FALSE, MODEST_MARGIN_HALF);
1003
1004         /* Only enable the button when the checkbox is checked: */
1005         enable_widget_for_checkbutton (priv->button_outgoing_smtp_servers, 
1006                                        HILDON_CHECK_BUTTON (priv->checkbox_outgoing_smtp_specific));
1007
1008         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
1009                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
1010
1011         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
1012                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
1013
1014
1015         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), box);
1016         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box),
1017                                              hildon_pannable_area_get_vadjustment (HILDON_PANNABLE_AREA (pannable)));
1018         gtk_widget_show (GTK_WIDGET (box));
1019         gtk_widget_show (pannable);
1020
1021         g_object_unref (title_sizegroup);
1022         g_object_unref (value_sizegroup);
1023         
1024         return GTK_WIDGET (pannable);
1025 }
1026
1027 static gboolean
1028 show_advanced_edit(gpointer user_data)
1029 {
1030         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1031         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1032         gint response;
1033         
1034         /* Show the Account Settings window: */
1035         ModestAccountSettingsDialog *dialog = modest_default_account_settings_dialog_new ();
1036         if (priv->pending_load_settings) {
1037                 save_to_settings (self);
1038                 priv->pending_load_settings = FALSE;
1039         }
1040         modest_account_settings_dialog_load_settings (dialog, priv->settings);
1041         
1042         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog), GTK_WINDOW (self));
1043         
1044         response = gtk_dialog_run (GTK_DIALOG (dialog));
1045
1046         gtk_widget_destroy (GTK_WIDGET (dialog));
1047         
1048         return FALSE; /* Do not call this timeout callback again. */
1049 }
1050
1051 static void
1052 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
1053 {
1054         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1055         
1056         /* Show the Account Settings window: */
1057         show_advanced_edit(self);
1058 }
1059 static GtkWidget* 
1060 create_page_complete_custom (ModestEasysetupWizardDialog *self)
1061 {
1062         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
1063         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
1064         GtkWidget *button_edit = gtk_button_new_with_label (_("mcen_fi_advanced_settings"));
1065         hildon_gtk_widget_set_theme_size (button_edit, HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
1066         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1067         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
1068         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1069         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1070         gtk_widget_show (label);
1071
1072         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
1073         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1074         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
1075         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1076         gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
1077         gtk_widget_show (label);
1078
1079         gtk_widget_show (button_edit);
1080         gtk_box_pack_start (GTK_BOX (box), button_edit, FALSE, FALSE, MODEST_MARGIN_HALF);
1081
1082         g_signal_connect (G_OBJECT (button_edit), "clicked", 
1083                           G_CALLBACK (on_button_edit_advanced_settings), self);
1084
1085         gtk_widget_show (GTK_WIDGET (box));
1086         return GTK_WIDGET (box);
1087 }
1088
1089
1090 /*
1091  */
1092 static void 
1093 on_response (ModestWizardDialog *wizard_dialog,
1094              gint response_id,
1095              gpointer user_data)
1096 {
1097         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1098
1099         invoke_enable_buttons_vfunc (self);
1100 }
1101
1102 static void 
1103 on_response_before (ModestWizardDialog *wizard_dialog,
1104                     gint response_id,
1105                     gpointer user_data)
1106 {
1107         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1108         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
1109         if (response_id == GTK_RESPONSE_CANCEL) {
1110                 /* This is mostly copied from
1111                  * src/maemo/modest-account-settings-dialog.c */
1112                 if (priv->dirty) {
1113                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
1114                                 _("imum_nc_wizard_confirm_lose_changes")));
1115                         /* TODO: These button names will be ambiguous, and not
1116                          * specified in the UI specification. */
1117
1118                         const gint dialog_response = gtk_dialog_run (dialog);
1119                         gtk_widget_destroy (GTK_WIDGET (dialog));
1120
1121                         if (dialog_response != GTK_RESPONSE_OK) {
1122                                 /* Don't let the dialog close */
1123                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1124                         }
1125                 }
1126         }
1127 }
1128
1129 static void
1130 fill_providers (ModestEasysetupWizardDialog *self)
1131 {
1132         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1133
1134         if (MODEST_IS_COUNTRY_PICKER (priv->account_country_picker)) {
1135 /*              gint mcc = get_default_country_code(); */
1136                 gint mcc;
1137                 /* Fill the combo in an idle call, as it takes a lot of time */
1138                 modest_country_picker_load_data(
1139                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1140                 /* connect to country picker's changed signal, so we can fill the provider picker: */
1141                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector 
1142                                             (HILDON_PICKER_BUTTON (priv->account_country_picker))),
1143                                   "changed",
1144                                   G_CALLBACK (on_account_country_selector_changed), self);
1145             
1146                 modest_country_picker_set_active_country_locale (
1147                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1148                 mcc = modest_country_picker_get_active_country_mcc (
1149                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1150                 modest_provider_picker_fill (
1151                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker),
1152                         priv->presets, mcc);
1153                 /* connect to providers picker's changed signal, so we can fill the email address: */
1154                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector 
1155                                             (HILDON_PICKER_BUTTON (priv->account_serviceprovider_picker))),
1156                                   "changed",
1157                                   G_CALLBACK (on_account_serviceprovider_selector_changed), self);
1158                 
1159                 modest_provider_picker_set_others_provider (MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
1160         }
1161
1162         priv->dirty = FALSE;
1163
1164 }
1165
1166 typedef struct IdleData {
1167         ModestEasysetupWizardDialog *dialog;
1168         ModestPresets *presets;
1169 } IdleData;
1170
1171 static gboolean
1172 presets_idle (gpointer userdata)
1173 {
1174         IdleData *idle_data = (IdleData *) userdata;
1175         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (idle_data->dialog);
1176         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1177
1178         g_assert (idle_data->presets);
1179
1180         gdk_threads_enter ();
1181
1182         priv->presets = idle_data->presets;
1183
1184         if (priv->check_support_done)
1185                 fill_providers (self);
1186
1187         g_object_unref (idle_data->dialog);
1188         g_free (idle_data);
1189
1190         gdk_threads_leave ();
1191
1192         return FALSE;
1193 }
1194
1195 static gpointer
1196 presets_loader (gpointer userdata)
1197 {
1198         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (userdata);
1199         ModestPresets *presets = NULL;
1200         IdleData *idle_data;
1201
1202         const gchar* path  = NULL;
1203         const gchar* path1 = MODEST_PROVIDER_DATA_FILE;
1204         const gchar* path2 = MODEST_MAEMO_PROVIDER_DATA_FILE;
1205         
1206         if (access(path1, R_OK) == 0) 
1207                 path = path1;
1208         else if (access(path2, R_OK) == 0)
1209                 path = path2;
1210         else {
1211                 g_warning ("%s: neither '%s' nor '%s' is a readable provider data file",
1212                            __FUNCTION__, path1, path2);
1213                 return NULL;
1214         }
1215
1216         presets = modest_presets_new (path);
1217         if (!presets) {
1218                 g_warning ("%s: failed to parse '%s'", __FUNCTION__, path);
1219                 return NULL;
1220         }
1221         
1222         idle_data = g_new0 (IdleData, 1);
1223         idle_data->dialog = self;
1224         idle_data->presets = presets;
1225         
1226         g_idle_add (presets_idle, idle_data);   
1227
1228         return NULL;
1229 }
1230
1231 static void
1232 modest_easysetup_wizard_dialog_append_page (GtkNotebook *notebook,
1233                                             GtkWidget *page,
1234                                             const gchar *label)
1235 {
1236         gint index;
1237         /* Append page and set attributes */
1238         index = gtk_notebook_append_page (notebook, page, gtk_label_new (label));
1239         gtk_container_child_set (GTK_CONTAINER (notebook), page,
1240                                  "tab-expand", TRUE, "tab-fill", TRUE,
1241                                  NULL);
1242         gtk_widget_show (page);
1243 }
1244
1245 static void
1246 init_user_page (ModestEasysetupWizardDialogPrivate *priv)
1247 {
1248         priv->page_user_details = NULL;
1249         priv->entry_user_name = NULL;
1250         priv->entry_user_username = NULL;
1251         priv->entry_user_password = NULL;
1252         priv->entry_user_email = NULL;
1253 }
1254
1255 static void
1256 init_incoming_page (ModestEasysetupWizardDialogPrivate *priv)
1257 {
1258         priv->page_custom_incoming = NULL;
1259         priv->incoming_servertype_picker = NULL;
1260         priv->caption_incoming = NULL;
1261         priv->entry_incomingserver = NULL;
1262         priv->entry_user_email = NULL;
1263         priv->incoming_security = NULL;
1264 }
1265
1266 static void
1267 init_outgoing_page (ModestEasysetupWizardDialogPrivate *priv)
1268 {
1269         priv->page_custom_outgoing = NULL;
1270         priv->entry_outgoingserver = NULL;
1271         priv->checkbox_outgoing_smtp_specific = NULL;
1272         priv->button_outgoing_smtp_servers = NULL;
1273         priv->outgoing_security = NULL;
1274 }
1275
1276 static void
1277 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1278 {
1279         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1280         
1281         /* Create the notebook to be used by the ModestWizardDialog base class:
1282          * Each page of the notebook will be a page of the wizard: */
1283         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1284         gtk_widget_set_size_request (GTK_WIDGET (notebook), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
1285         
1286         /* Set the notebook used by the ModestWizardDialog base class: */
1287         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1288     
1289         /* Set the wizard title:
1290          * The actual window title will be a combination of this and the page's tab label title. */
1291         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1292
1293         /* Read in the information about known service providers: */
1294         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1295         
1296         /* The server fields did not have been manually changed yet */
1297         priv->server_changes = 0;
1298         priv->pending_load_settings = TRUE;
1299
1300         /* Get the account manager object, 
1301          * so we can check for existing accounts,
1302          * and create new accounts: */
1303         priv->account_manager = modest_runtime_get_account_mgr ();
1304         g_object_ref (priv->account_manager);
1305         
1306         /* Initialize fields */
1307         priv->check_support_done = FALSE;
1308         priv->check_support_show_progress_id = 0;
1309         priv->check_support_progress_pulse_id = 0;
1310         priv->pending_check_support = 0;
1311         priv->destroyed = FALSE;
1312         priv->page_welcome = create_page_welcome (self);
1313         priv->page_account_details = create_page_account_details (self);
1314
1315         init_user_page (priv);
1316         init_incoming_page (priv);
1317         init_outgoing_page (priv);
1318
1319         priv->page_complete_easysetup = NULL;       
1320         priv->page_complete_customsetup = NULL;
1321         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1322         priv->missing_data_signals = NULL;
1323
1324         /* Add the common pages */
1325         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_welcome, 
1326                                                     _("mcen_ti_emailsetup_welcome"));
1327         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_account_details, 
1328                                                     _("mcen_ti_accountdetails"));
1329                 
1330         /* Connect to the dialog's response signal so we can enable/disable buttons 
1331          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1332          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1333          * so that we can be enable/disable after ModestWizardDialog has done its own 
1334          * enabling/disabling of buttons.
1335          * 
1336          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1337          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1338          * 
1339          * It's not enough to connect to the notebook's switch-page signal, because 
1340          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1341          * _after_ switching the page (understandably).
1342          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1343          * then gtk_notebook_get_current_page() would return an incorrect value.)
1344          */
1345         g_signal_connect_after (G_OBJECT (self), "response",
1346                                 G_CALLBACK (on_response), self);
1347
1348         /* This is to show a confirmation dialog when the user hits cancel */
1349         g_signal_connect (G_OBJECT (self), "response",
1350                           G_CALLBACK (on_response_before), self);
1351
1352         g_signal_connect (G_OBJECT (self), "delete-event",
1353                           G_CALLBACK (on_delete_event), self);
1354
1355         /* Reset dirty, because there was no user input until now */
1356         priv->dirty = FALSE;
1357
1358         /* When this window is shown, hibernation should not be possible, 
1359          * because there is no sensible way to save the state: */
1360         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1361                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1362
1363         /* Load provider presets */
1364         g_object_ref (self);
1365         g_thread_create (presets_loader, self, FALSE, NULL);
1366
1367         priv->settings = modest_account_settings_new ();
1368
1369         check_support_of_protocols (self);
1370 }
1371
1372 ModestEasysetupWizardDialog*
1373 modest_easysetup_wizard_dialog_new (void)
1374 {       
1375         
1376         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1377 }
1378
1379 static void 
1380 create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1381 {
1382         ModestEasysetupWizardDialogPrivate *priv;
1383         GtkNotebook *notebook = NULL;
1384
1385         g_object_get (self, "wizard-notebook", &notebook, NULL);
1386         g_assert(notebook);
1387
1388         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1389
1390         if (!priv->page_user_details) {
1391                 priv->page_user_details = create_page_user_details (self);
1392         }                       
1393
1394         /* Create the custom pages: */
1395         if(!(priv->page_custom_incoming)) {
1396                 priv->page_custom_incoming = create_page_custom_incoming (self);
1397         }
1398                 
1399         /* TODO: only if needed */
1400         if(!(priv->page_custom_outgoing)) {
1401                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1402         }
1403         
1404         if(!(priv->page_complete_customsetup)) {
1405                 priv->page_complete_customsetup = create_page_complete_custom (self);
1406         }
1407
1408         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_user_details)))
1409                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_user_details,
1410                                                             _("mcen_ti_emailsetup_userdetails"));
1411
1412         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1413                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_incoming, 
1414                                                             _("mcen_ti_emailsetup_incomingdetails"));
1415         
1416         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))           
1417                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_outgoing, 
1418                                                             _("mcen_ti_emailsetup_outgoingdetails"));
1419                 
1420         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup)))
1421                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_customsetup, 
1422                                                             _("mcen_ti_emailsetup_complete"));
1423                         
1424         /* This is unnecessary with GTK+ 2.10: */
1425         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1426 }
1427         
1428 static void 
1429 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1430 {
1431         ModestEasysetupWizardDialogPrivate *priv;
1432         GtkNotebook *notebook = NULL;
1433
1434         g_object_get (self, "wizard-notebook", &notebook, NULL);
1435         g_assert(notebook);
1436         
1437         /* Create the easysetup-specific pages: */
1438         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1439         if(!priv->page_complete_easysetup)
1440                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1441
1442         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup)))
1443                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_easysetup, 
1444                                                             _("mcen_ti_emailsetup_complete"));
1445                         
1446         /* This is unnecessary with GTK+ 2.10: */
1447         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1448 }
1449
1450 /* */
1451 static void
1452 remove_non_common_tabs (GtkNotebook *notebook, 
1453                         gboolean remove_user_details) 
1454 {
1455         gint starting_tab;
1456         /* The first 2 tabs are the common ones (welcome tab and the
1457            providers tab), so we always remove starting from the
1458            end */
1459
1460         starting_tab = (remove_user_details) ? 2 : 3;
1461         while (gtk_notebook_get_n_pages (notebook) > starting_tab) 
1462                 gtk_notebook_remove_page (notebook, -1); 
1463 }
1464
1465 static void
1466 on_missing_mandatory_data (ModestAccountProtocol *protocol,
1467                            gboolean missing,
1468                            gpointer user_data)
1469 {
1470         real_enable_buttons (MODEST_WIZARD_DIALOG (user_data), !missing);
1471 }
1472
1473 /* After the user details page,
1474  * the following pages depend on whether "Other" was chosen 
1475  * in the provider combobox on the account page
1476  */
1477 static void 
1478 create_subsequent_pages (ModestEasysetupWizardDialog *self)
1479 {
1480         ModestEasysetupWizardDialogPrivate *priv;
1481         ModestProviderPicker *picker;
1482         ModestProviderPickerIdType id_type;
1483         GtkNotebook *notebook;
1484
1485         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1486         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1487         id_type = modest_provider_picker_get_active_id_type (picker);
1488         g_object_get (self, "wizard-notebook", &notebook, NULL);
1489         modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1490                                                             NULL);
1491         
1492
1493         if (id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
1494                 /* "Other..." was selected: */
1495
1496                 /* If we come from a rollbacked easysetup */
1497                 if (priv->page_complete_easysetup) {
1498                         remove_non_common_tabs (notebook, FALSE);
1499                         priv->page_complete_easysetup = NULL;
1500                 } 
1501                 
1502                 /* If we come from a rollbacked plugin protocol setup */
1503                 if (priv->last_plugin_protocol_selected != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1504                         remove_non_common_tabs (notebook, TRUE);
1505                         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1506                         modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1507                         priv->missing_data_signals = NULL;
1508                 }
1509
1510                 create_subsequent_customsetup_pages (self);
1511         } else {
1512                 /* If we come from a rollbacked custom setup */
1513                 if (priv->page_custom_incoming) {
1514                         remove_non_common_tabs (notebook, TRUE);
1515                         init_user_page (priv);
1516                         init_incoming_page (priv);
1517                         init_outgoing_page (priv);
1518                         init_user_page (priv);
1519                         priv->page_complete_customsetup = NULL;
1520                 }
1521
1522                 /* It's a pluggable protocol and not a provider with presets */
1523                 if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1524                         ModestProtocol *protocol;
1525                         gchar *proto_name;
1526                         ModestProtocolType proto_type;
1527
1528                         
1529                         /* If we come from a rollbacked easy setup */
1530                         if (priv->last_plugin_protocol_selected == 
1531                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID &&
1532                             priv->page_complete_easysetup) {
1533                                 remove_non_common_tabs (notebook, TRUE);
1534                                 init_user_page (priv);
1535                                 priv->page_complete_easysetup = NULL;
1536                         }
1537                         
1538                         proto_name = modest_provider_picker_get_active_provider_id (picker);
1539                         protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
1540                                                                                   MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1541                                                                                   proto_name);
1542                         proto_type = modest_protocol_get_type_id (protocol);
1543
1544                         if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol) &&
1545                             proto_type != priv->last_plugin_protocol_selected) {
1546                                 ModestPairList *tabs;
1547                                 GSList *tmp;
1548                                 gboolean first_page = TRUE;
1549                                 ModestWizardDialogResponseOverrideFunc response_override;
1550
1551                                 /* Remember the last selected plugin protocol */
1552                                 priv->last_plugin_protocol_selected = proto_type;
1553
1554                                 /* Get tabs */
1555                                 tabs = modest_account_protocol_get_easysetupwizard_tabs (MODEST_ACCOUNT_PROTOCOL (protocol));
1556                                 response_override = modest_account_protocol_get_wizard_response_override 
1557                                         (MODEST_ACCOUNT_PROTOCOL (protocol));
1558                                 modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1559                                                                                     response_override);
1560                                 tmp = (GSList *) tabs;
1561                                 while (tmp) {
1562                                         ModestPair *pair = (ModestPair *) tmp->data;
1563                                         modest_easysetup_wizard_dialog_append_page (notebook,
1564                                                                                     GTK_WIDGET (pair->second),
1565                                                                                     (const gchar *) pair->first);
1566                                         if (first_page) {
1567                                                 gtk_container_set_focus_child (GTK_CONTAINER (notebook),
1568                                                                                GTK_WIDGET (pair->second));
1569                                                 first_page = FALSE;
1570                                         }
1571
1572                                         /* Connect signals */
1573                                         if (MODEST_IS_EASYSETUP_WIZARD_PAGE (pair->second)) {
1574                                                 priv->missing_data_signals = 
1575                                                         modest_signal_mgr_connect (priv->missing_data_signals, 
1576                                                                                    G_OBJECT (pair->second), 
1577                                                                                    "missing-mandatory-data",
1578                                                                                    G_CALLBACK (on_missing_mandatory_data), 
1579                                                                                    self);
1580                                         }
1581
1582                                         g_free (pair->first);
1583                                         tmp = g_slist_next (tmp);
1584                                         /* Critical: if you don't show the page then the dialog will ignore it */
1585 /*                                      gtk_widget_show (GTK_WIDGET (pair->second)); */
1586                                 }
1587                                 modest_pair_list_free (tabs);
1588                         }
1589                         g_free (proto_name);
1590                 } else {
1591                         if (priv->last_plugin_protocol_selected != 
1592                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1593                                 remove_non_common_tabs (notebook, TRUE);
1594                                 init_user_page (priv);
1595                                 priv->page_complete_easysetup = NULL;
1596                                 priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1597                                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1598                                 priv->missing_data_signals = NULL;
1599                         }
1600                         if (!priv->page_user_details) {
1601                                 priv->page_user_details = create_page_user_details (self);
1602                                 modest_easysetup_wizard_dialog_append_page (notebook, 
1603                                                                             priv->page_user_details,
1604                                                                             _("mcen_ti_emailsetup_userdetails"));
1605                         }
1606                 }
1607                 
1608                 /* Create the easysetup pages: */
1609                 create_subsequent_easysetup_pages (self);
1610         }
1611 }
1612
1613
1614 static gchar*
1615 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocolType protocol_type)
1616 {
1617         const gchar* hostname = NULL;
1618         gchar* at;
1619         gchar* domain;
1620         ModestProtocolRegistry *protocol_registry;
1621         ModestProtocol *protocol;
1622
1623         if (!email_address)
1624                 return NULL;
1625         
1626         at = g_utf8_strchr (email_address, -1, '@');
1627         if (!at || (g_utf8_strlen (at, -1) < 2))
1628                 return NULL;
1629                 
1630         domain = g_utf8_next_char (at);
1631         if(!domain)
1632                 return NULL;
1633
1634         protocol_registry = modest_runtime_get_protocol_registry ();
1635         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
1636                 
1637         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS) ||
1638             modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
1639                 hostname = modest_protocol_get_name (protocol);
1640         }
1641         
1642         if (!hostname)
1643                 return NULL;
1644                 
1645         return g_strdup_printf ("%s.%s", hostname, domain);
1646 }
1647
1648 static void 
1649 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1650 {
1651         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1652
1653         if (!priv->entry_incomingserver)
1654                 return;
1655                 
1656         /* Set a default domain for the server, based on the email address,
1657          * if no server name was already specified.
1658          */
1659         if (priv->entry_user_email
1660             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1661                 const ModestProtocolType protocol_type = modest_servertype_picker_get_active_servertype (
1662                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
1663
1664                 /* This could happen when the combo box has still no active iter */
1665                 if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1666                         const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));      
1667                         gchar* servername = util_get_default_servername_from_email_address (email_address, 
1668                                                                                             protocol_type);
1669
1670                         /* Do not set the INCOMING_CHANGED flag because of this edit */
1671                         g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1672                         hildon_entry_set_text (HILDON_ENTRY (priv->entry_incomingserver), servername);
1673                         g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1674                         
1675                         g_free (servername);
1676                 }
1677         }
1678         
1679         /* Set a default domain for the server, based on the email address,
1680          * if no server name was already specified.
1681          */
1682         if (!priv->entry_outgoingserver)
1683                 return;
1684                 
1685         if (priv->entry_user_email
1686             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1687                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));
1688                 
1689                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOLS_TRANSPORT_SMTP);
1690
1691                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1692                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1693                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_outgoingserver), servername);
1694                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1695
1696                 g_free (servername);
1697         }
1698 }
1699
1700 static gchar*
1701 get_entered_account_title (ModestEasysetupWizardDialog *self)
1702 {
1703         ModestEasysetupWizardDialogPrivate *priv;
1704         const gchar* account_title;
1705
1706         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1707         account_title = hildon_entry_get_text (HILDON_ENTRY (priv->entry_account_title));
1708
1709         if (!account_title || (g_utf8_strlen (account_title, -1) == 0)) {
1710                 return NULL;
1711         } else {
1712                 /* Strip it of whitespace at the start and end: */
1713                 gchar *result = g_strdup (account_title);
1714                 result = g_strstrip (result);
1715                 
1716                 if (!result)
1717                         return NULL;
1718                         
1719                 if (g_utf8_strlen (result, -1) == 0) {
1720                         g_free (result);
1721                         return NULL;    
1722                 }
1723                 
1724                 return result;
1725         }
1726 }
1727
1728 static gboolean
1729 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1730 {
1731         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1732         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1733         ModestProtocolRegistry *protocol_registry;
1734
1735         protocol_registry = modest_runtime_get_protocol_registry ();
1736
1737         /* if are browsing pages previous to the last one, then we have pending settings in
1738          * this wizard */
1739         if (next_page != NULL)
1740                 priv->pending_load_settings = TRUE;
1741         
1742         /* Do extra validation that couldn't be done for every key press,
1743          * either because it was too slow,
1744          * or because it requires interaction:
1745          */
1746         if (current_page == priv->page_account_details) {       
1747                 /* Check that the title is not already in use: */
1748                 gchar* account_title = get_entered_account_title (self);
1749                 if (!account_title)
1750                         return FALSE;
1751                         
1752                 /* Aavoid a clash with an existing display name: */
1753                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1754                         priv->account_manager, account_title);
1755                 g_free (account_title);
1756
1757                 if (name_in_use) {
1758                         /* Warn the user via a dialog: */
1759                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1760             
1761                         return FALSE;
1762                 }
1763
1764                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1765                 create_subsequent_pages (self);
1766
1767         } else if (current_page == priv->page_user_details) {
1768                 /* Check that the email address is valud: */
1769                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
1770                 if ((!email_address) || (g_utf8_strlen (email_address, -1) == 0))
1771                         return FALSE;
1772                         
1773                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1774                         /* Warn the user via a dialog: */
1775                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1776                                              
1777                         /* Return focus to the email address entry: */
1778                         gtk_widget_grab_focus (priv->entry_user_email);
1779                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1780
1781                         return FALSE;
1782                 }
1783         }
1784           
1785         if (next_page == priv->page_custom_incoming) {
1786                 set_default_custom_servernames (self);
1787         } else if (next_page == priv->page_custom_outgoing) {
1788                 set_default_custom_servernames (self);
1789
1790                 /* Check if the server supports secure authentication */
1791                 if (modest_security_options_view_auth_check (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security)))
1792                         if (!check_has_supported_auth_methods (self))
1793                                 return FALSE;
1794                 gtk_widget_show (priv->outgoing_security);
1795         }
1796         
1797         /* If this is the last page, and this is a click on Finish, 
1798          * then attempt to create the dialog.
1799          */
1800         if(!next_page && 
1801            current_page != priv->page_account_details) /* This is NULL when this is a click on Finish. */
1802         {
1803                 if (priv->pending_load_settings) {
1804                         save_to_settings (self);
1805                 }
1806
1807                 /* We check if there's already another account with the same configuration */
1808                 if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
1809                         modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1810                         return FALSE;
1811                 }
1812
1813                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1814         }
1815         
1816         
1817         return TRUE;
1818 }
1819
1820 static gboolean entry_is_empty (GtkWidget *entry)
1821 {
1822         if (!entry)
1823                 return FALSE;
1824                 
1825         const gchar* text = hildon_entry_get_text (HILDON_ENTRY (entry));
1826         if ((!text) || (g_utf8_strlen (text, -1) == 0))
1827                 return TRUE;
1828         else {
1829                 /* Strip it of whitespace at the start and end: */
1830                 gchar *stripped = g_strdup (text);
1831                 stripped = g_strstrip (stripped);
1832                 
1833                 if (!stripped)
1834                         return TRUE;
1835                         
1836                 const gboolean result = (g_utf8_strlen (stripped, -1) == 0);
1837                 
1838                 g_free (stripped);
1839                 return result;
1840         }
1841 }
1842
1843 static void
1844 real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next)
1845 {               
1846         GtkNotebook *notebook = NULL;
1847         gboolean is_finish_tab;
1848         GtkWidget *current;
1849         ModestEasysetupWizardDialogPrivate *priv;
1850
1851         /* Get data */
1852         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (dialog);
1853         g_object_get (dialog, "wizard-notebook", &notebook, NULL);
1854
1855         /* Disable the Finish button until we are on the last page,
1856          * because HildonWizardDialog enables this for all but the
1857          * first page */
1858         current = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
1859         is_finish_tab = ((current == priv->page_complete_easysetup) ||
1860                          (current == priv->page_complete_customsetup));
1861
1862         if (is_finish_tab) {
1863                 /* Disable Next on the last page no matter what the
1864                    argument say */
1865                 enable_next = FALSE;
1866         } else {
1867                 /* Not the last one */
1868                 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1869                                                    MODEST_WIZARD_DIALOG_FINISH,
1870                                                    FALSE);
1871
1872                 /* If the check support is not done then do not enable
1873                    the wizard to continue */
1874                 enable_next = enable_next && priv->check_support_done;
1875         }
1876
1877         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1878                                            MODEST_WIZARD_DIALOG_NEXT,
1879                                            enable_next);
1880 }
1881
1882 static void
1883 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1884 {
1885         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1886         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1887         
1888         gboolean enable_next = TRUE;
1889         if (current_page == priv->page_welcome) {
1890                 enable_next = TRUE;
1891         } else if (current_page == priv->page_account_details) {
1892                 /* The account details title is mandatory: */
1893                 if (entry_is_empty(priv->entry_account_title))
1894                         enable_next = FALSE;
1895         } else if (current_page == priv->page_user_details) {   
1896                 /* The user details username is mandatory: */
1897                 if (entry_is_empty(priv->entry_user_username))
1898                         enable_next = FALSE;
1899                         
1900                 /* The user details email address is mandatory: */
1901                 if (enable_next && entry_is_empty (priv->entry_user_email))
1902                         enable_next = FALSE;
1903         } else if (current_page == priv->page_custom_incoming) {
1904                 /* The custom incoming server is mandatory: */
1905                 if (entry_is_empty(priv->entry_incomingserver))
1906                         enable_next = FALSE;
1907         } else if (MODEST_IS_EASYSETUP_WIZARD_PAGE (current_page)) {
1908                 enable_next = !modest_easysetup_wizard_page_validate (
1909                        MODEST_EASYSETUP_WIZARD_PAGE (current_page));
1910         }
1911                         
1912         /* Enable/disable buttons */ 
1913         real_enable_buttons (dialog, enable_next);
1914 }
1915
1916 static void
1917 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1918 {
1919         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1920         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1921
1922
1923         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1924         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1925         
1926         /* Provide a vfunc implementation so we can decide 
1927          * when to enable/disable the prev/next buttons.
1928          */
1929         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1930         base_klass->before_next = on_before_next;
1931         base_klass->enable_buttons = on_enable_buttons;
1932         base_klass->update_model = on_update_model;
1933         base_klass->save = on_save;
1934 }
1935
1936 /**
1937  * save_to_settings:
1938  * @self: a #ModestEasysetupWizardDialog
1939  *
1940  * takes information from all the wizard and stores it in settings
1941  */
1942 static void
1943 save_to_settings (ModestEasysetupWizardDialog *self)
1944 {
1945         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1946         guint special_port;
1947         gchar *provider_id = NULL;
1948         gchar* display_name;
1949         const gchar *username, *password;
1950         gchar *store_hostname, *transport_hostname;
1951         guint store_port, transport_port;
1952         ModestProtocolRegistry *protocol_registry;
1953         ModestProtocolType store_protocol, transport_protocol;
1954         ModestProtocolType store_security, transport_security;
1955         ModestProtocolType store_auth_protocol, transport_auth_protocol;
1956         ModestServerAccountSettings *store_settings, *transport_settings;
1957         const gchar *fullname, *email_address;
1958         ModestProviderPicker *picker;
1959         ModestProviderPickerIdType id_type;
1960
1961         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1962         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1963         protocol_registry = modest_runtime_get_protocol_registry ();
1964
1965         /* Get details from the specified presets: */
1966         id_type = modest_provider_picker_get_active_id_type (picker);
1967         provider_id = modest_provider_picker_get_active_provider_id (picker);
1968
1969         /* Let the plugin save the settings. We do a return in order
1970            to save an indentation level */
1971         if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1972                 ModestProtocol *protocol;
1973
1974                 protocol = modest_protocol_registry_get_protocol_by_name (
1975                        modest_runtime_get_protocol_registry (),
1976                        MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1977                        provider_id);
1978
1979                 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1980                         gint n_pages, i = 0;
1981                         GtkNotebook *notebook;
1982                         GList *wizard_pages = NULL;
1983
1984                         g_object_get (self, "wizard-notebook", &notebook, NULL);
1985                         n_pages = gtk_notebook_get_n_pages (notebook);
1986                         for (i = 0; i < n_pages; i++) {
1987                                 GtkWidget *page = gtk_notebook_get_nth_page (notebook, i);
1988                                 if (MODEST_IS_EASYSETUP_WIZARD_PAGE (page))
1989                                         wizard_pages  = g_list_append (wizard_pages, page);
1990                         }
1991                         modest_account_protocol_save_wizard_settings (MODEST_ACCOUNT_PROTOCOL (protocol),
1992                                                                       wizard_pages,
1993                                                                       priv->settings);
1994                         g_list_free (wizard_pages);
1995                 } else {
1996                         g_warning ("The selected protocol is a plugin protocol "//
1997                                    "but it's not a ModestAccountProtocol");
1998                 }
1999
2000                 g_free (provider_id);
2001                 return;
2002         }
2003
2004         /* username and password (for both incoming and outgoing): */
2005         username = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username));
2006         password = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_password));
2007
2008         store_settings = modest_account_settings_get_store_settings (priv->settings);
2009         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
2010
2011         /* Incoming server: */
2012         /* Note: We need something as default for the transport store protocol values, 
2013          * or modest_account_mgr_add_server_account will fail. */
2014         store_port = 0;
2015         store_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2016         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2017         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2018
2019         if (provider_id) {
2020                 ModestProtocolType store_provider_server_type;
2021                 gboolean store_provider_use_alternate_port;
2022                 /* Use presets: */
2023                 store_hostname = modest_presets_get_server (priv->presets, provider_id,
2024                                                             TRUE /* store */);
2025
2026                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2027                                                                          provider_id,
2028                                                                          TRUE /* store */);
2029                 store_security  = modest_presets_get_info_server_security (priv->presets,
2030                                                                            provider_id,
2031                                                                            TRUE /* store */);
2032                 store_auth_protocol  = modest_presets_get_info_server_auth (priv->presets,
2033                                                                             provider_id,
2034                                                                             TRUE /* store */);
2035                 store_provider_use_alternate_port  = modest_presets_get_info_server_use_alternate_port (priv->presets,
2036                                                                                                         provider_id,
2037                                                                                                         TRUE /* store */);
2038
2039                 /* We don't check for SMTP here as that is impossible for an incoming server. */
2040                 if (store_provider_server_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID)
2041                         store_protocol = MODEST_PROTOCOLS_STORE_POP;
2042                 else
2043                         store_protocol = store_provider_server_type;
2044
2045                 /* we check if there is a *special* port */
2046                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
2047                 if (special_port != 0) {
2048                         store_port = special_port;
2049                 } else {
2050                         gboolean use_alternate_port = FALSE;
2051                         if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
2052                                                                               store_security))
2053                                 use_alternate_port = TRUE;
2054                         store_port = get_port_from_protocol(store_provider_server_type, use_alternate_port);
2055                 }
2056
2057                 modest_server_account_settings_set_security_protocol (store_settings, 
2058                                                                       store_security);
2059                 modest_server_account_settings_set_auth_protocol (store_settings, 
2060                                                                   store_auth_protocol);
2061                 if (store_port != 0)
2062                         modest_server_account_settings_set_port (store_settings, store_port);
2063         } else {
2064                 /* Use custom pages because no preset was specified: */
2065                 store_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_incomingserver) ));         
2066                 store_protocol = modest_servertype_picker_get_active_servertype (
2067                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));           
2068
2069                 modest_security_options_view_save_settings (
2070                                     MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
2071                                     priv->settings);
2072         }
2073
2074         /* now we store the common store account settings */
2075         modest_server_account_settings_set_hostname (store_settings, store_hostname);
2076         modest_server_account_settings_set_username (store_settings, username);
2077         modest_server_account_settings_set_password (store_settings, password);
2078         modest_server_account_settings_set_protocol (store_settings, store_protocol);
2079
2080         g_object_unref (store_settings);
2081         g_free (store_hostname);
2082         
2083         /* Outgoing server: */
2084         transport_hostname = NULL;
2085         transport_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2086         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2087         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2088         transport_port = 0;
2089         
2090         if (provider_id) {
2091                 ModestProtocolType transport_provider_server_type;
2092                 ModestProtocolType transport_provider_security;
2093
2094                 /* Use presets */
2095                 transport_hostname = modest_presets_get_server (priv->presets, provider_id, 
2096                                                                 FALSE /* transport */);
2097                         
2098                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2099                                                                                       provider_id, 
2100                                                                                       FALSE /* transport */);           
2101                 transport_provider_security = modest_presets_get_info_server_security (priv->presets, 
2102                                                                                        provider_id, 
2103                                                                                        FALSE /* transport */);
2104
2105                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
2106                 transport_protocol = transport_provider_server_type;
2107                 transport_security = transport_provider_security;
2108                 if (transport_security == MODEST_PROTOCOLS_CONNECTION_SSL) {
2109                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
2110                         /* we check if there is a *special* port */
2111                         special_port = modest_presets_get_port (priv->presets, provider_id,
2112                                                                 FALSE /* transport */);
2113                         if (special_port != 0)
2114                                 transport_port = special_port;
2115                         else 
2116                                 transport_port = 465;
2117                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
2118                 } else {
2119                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
2120                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2121                 }
2122
2123                 modest_server_account_settings_set_security_protocol (transport_settings, 
2124                                                                       transport_security);
2125                 modest_server_account_settings_set_auth_protocol (transport_settings, 
2126                                                                   transport_auth_protocol);
2127                 if (transport_port != 0)
2128                         modest_server_account_settings_set_port (transport_settings, 
2129                                                                  transport_port);
2130         } else {
2131                 ModestProtocolRegistry *registry;
2132                 ModestProtocol *store_proto;
2133
2134                 registry = modest_runtime_get_protocol_registry ();
2135                 /* Use custom pages because no preset was specified: */
2136                 transport_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver) ));
2137
2138                 store_proto = modest_protocol_registry_get_protocol_by_type (registry, 
2139                                                                              store_protocol);
2140
2141                 if (transport_protocol == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
2142                         /* fallback to SMTP if none was specified */
2143                         g_warning ("No transport protocol was specified for store %d (%s)",
2144                                    modest_protocol_get_type_id (store_proto),
2145                                    modest_protocol_get_display_name (store_proto));
2146                         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP;
2147                 }
2148
2149                 modest_security_options_view_save_settings (
2150                                     MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security),
2151                                     priv->settings);
2152         }
2153
2154         /* now we store the common transport account settings */
2155         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
2156         modest_server_account_settings_set_username (transport_settings, username);
2157         modest_server_account_settings_set_password (transport_settings, password);
2158         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
2159
2160         g_object_unref (transport_settings);
2161         g_free (transport_hostname);
2162
2163         fullname = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_name));
2164         email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
2165         modest_account_settings_set_fullname (priv->settings, fullname);
2166         modest_account_settings_set_email_address (priv->settings, email_address);
2167         /* we don't set retrieve type to preserve advanced settings if
2168            any. By default account settings are set to headers only */
2169
2170         /* Save the connection-specific SMTP server accounts. */
2171         if (priv->checkbox_outgoing_smtp_specific) {
2172                 modest_account_settings_set_use_connection_specific_smtp
2173                         (priv->settings,
2174                          hildon_check_button_get_active(HILDON_CHECK_BUTTON(priv->checkbox_outgoing_smtp_specific)));
2175         }
2176
2177         display_name = get_entered_account_title (self);
2178         modest_account_settings_set_display_name (priv->settings, display_name);
2179         g_free (display_name);
2180         g_free (provider_id);
2181 }
2182
2183
2184 static void
2185 on_update_model (ModestWizardDialog *dialog)
2186 {
2187         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2188         save_to_settings (self);
2189
2190 }
2191
2192 static gboolean
2193 on_save (ModestWizardDialog *dialog)
2194 {
2195         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2196         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2197
2198         save_to_settings (self);
2199
2200         if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
2201                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
2202                 return FALSE;
2203         }
2204
2205         return modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
2206
2207 }
2208
2209
2210 static GList*
2211 check_for_supported_auth_methods (ModestEasysetupWizardDialog* self)
2212 {
2213         GError *error = NULL;
2214         ModestProtocolType protocol_type;
2215         const gchar* hostname;
2216         const gchar* username;
2217         ModestProtocolType security_protocol_incoming_type;
2218         ModestProtocolRegistry *registry;
2219         int port_num;
2220         GList *list_auth_methods;
2221         ModestEasysetupWizardDialogPrivate *priv;
2222         
2223         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2224         registry = modest_runtime_get_protocol_registry ();
2225         protocol_type = modest_servertype_picker_get_active_servertype (
2226                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
2227         hostname = gtk_entry_get_text(GTK_ENTRY(priv->entry_incomingserver));
2228         username = gtk_entry_get_text(GTK_ENTRY(priv->entry_user_username));
2229         security_protocol_incoming_type = modest_security_options_view_get_connection_protocol
2230                 (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security));
2231         port_num = get_port_from_protocol(protocol_type, FALSE);
2232         list_auth_methods = modest_utils_get_supported_secure_authentication_methods (protocol_type, hostname, port_num,
2233                                                                                       username, GTK_WINDOW (self), &error);
2234
2235         if (list_auth_methods) {
2236                 /* TODO: Select the correct method */
2237                 GList* list = NULL;
2238                 GList* method;
2239                 for (method = list_auth_methods; method != NULL; method = g_list_next(method)) {
2240                         ModestProtocolType auth_protocol_type = (ModestProtocolType) (GPOINTER_TO_INT(method->data));
2241                         if (modest_protocol_registry_protocol_type_has_tag (registry, auth_protocol_type,
2242                                                                             MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS)) {
2243                                 list = g_list_append(list, GINT_TO_POINTER(auth_protocol_type));
2244                         }
2245                 }
2246
2247                 g_list_free(list_auth_methods);
2248
2249                 if (list)
2250                         return list;
2251         }
2252
2253         if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
2254                         error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
2255         {
2256                 modest_platform_information_banner (GTK_WIDGET(self), NULL,
2257                                                     _("mcen_ib_unableto_discover_auth_methods"));
2258         }
2259
2260         if(error != NULL)
2261                 g_error_free(error);
2262
2263         return NULL;
2264 }
2265
2266 static gboolean 
2267 check_has_supported_auth_methods(ModestEasysetupWizardDialog* self)
2268 {
2269         GList* methods = check_for_supported_auth_methods(self);
2270         if (!methods)
2271         {
2272                 return FALSE;
2273         }
2274
2275         g_list_free(methods);
2276         return TRUE;
2277 }
2278
2279 static gboolean 
2280 check_support_progress_pulse (gpointer userdata)
2281 {
2282         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2283         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2284
2285         if (priv->destroyed) {
2286                 priv->check_support_progress_pulse_id = 0;
2287                 return FALSE;
2288         }
2289
2290         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->check_support_progress));
2291
2292         return TRUE;
2293 }
2294
2295 static gboolean 
2296 check_support_show_progress (gpointer userdata)
2297 {
2298         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2299         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2300
2301         priv->check_support_show_progress_id = 0;
2302
2303         if (priv->destroyed)
2304                 return FALSE;
2305
2306         gtk_widget_show (priv->check_support_progress);
2307         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->check_support_progress));
2308
2309         priv->check_support_progress_pulse_id = g_timeout_add (200, check_support_progress_pulse, self);
2310
2311         return FALSE;
2312 }
2313
2314 static void
2315 check_support_callback (ModestAccountProtocol *protocol,
2316                         gboolean supported,
2317                         gpointer userdata)
2318 {
2319         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2320         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2321
2322         priv->pending_check_support --;
2323
2324         if (priv->check_support_show_progress_id > 0) {
2325                 g_source_remove (priv->check_support_show_progress_id);
2326                 priv->check_support_show_progress_id = 0;
2327         }
2328
2329         if (priv->check_support_progress_pulse_id > 0) {
2330                 g_source_remove (priv->check_support_progress_pulse_id);
2331                 priv->check_support_progress_pulse_id = 0;
2332         }
2333
2334         if (priv->pending_check_support == 0) {
2335                 priv->check_support_done = TRUE;
2336
2337                 if (!priv->destroyed) {
2338                         if (priv->presets)
2339                                 fill_providers (self);
2340                         gtk_widget_hide (priv->check_support_progress);
2341                         invoke_enable_buttons_vfunc (self);
2342                 }
2343         }
2344         g_object_unref (self);
2345 }
2346
2347
2348 static void
2349 check_support_of_protocols (ModestEasysetupWizardDialog *self)
2350 {
2351         ModestProtocolRegistry *registry;
2352         GSList *provider_protos, *node;
2353         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2354
2355         registry = modest_runtime_get_protocol_registry ();
2356         provider_protos = modest_protocol_registry_get_by_tag (registry, 
2357                                                                MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
2358
2359         for (node = provider_protos; node != NULL; node = g_slist_next (node)) {
2360                 ModestProtocol *proto = MODEST_PROTOCOL (node->data);
2361
2362                 if (!modest_protocol_registry_protocol_type_has_tag (registry, 
2363                                                                      modest_protocol_get_type_id (proto),
2364                                                                      MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS))
2365                         continue;
2366
2367                 if (modest_protocol_registry_protocol_type_has_tag 
2368                     (registry,
2369                      modest_protocol_get_type_id (proto),
2370                      MODEST_PROTOCOL_REGISTRY_SINGLETON_PROVIDER_PROTOCOLS)) {
2371                         /* Check if there's already an account configured with this account type */
2372                         if (modest_account_mgr_singleton_protocol_exists (modest_runtime_get_account_mgr (),
2373                                                                           modest_protocol_get_type_id (proto)))
2374                                 continue;
2375                 }
2376
2377                 if (MODEST_ACCOUNT_PROTOCOL (proto)) {
2378                         priv->pending_check_support ++;
2379                         modest_account_protocol_check_support (MODEST_ACCOUNT_PROTOCOL (proto),
2380                                                                check_support_callback,
2381                                                                g_object_ref (self));
2382                 }
2383         }
2384         g_slist_free (provider_protos);
2385
2386         if (priv->pending_check_support > 0) {
2387                 priv->check_support_show_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 1000,
2388                                                                            check_support_show_progress, 
2389                                                                            g_object_ref (self), g_object_unref);
2390         } else {
2391                 priv->check_support_done = TRUE;
2392         }
2393         invoke_enable_buttons_vfunc (self);
2394 }