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