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