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