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