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