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