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