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