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