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