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