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