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