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