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