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