Fixes NB#130999, fixes a memory leak saving attachments
[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, gint column, 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 (G_OBJECT (hildon_picker_button_get_selector
1257                                             (HILDON_PICKER_BUTTON (priv->account_country_picker))),
1258                                   "changed",
1259                                   G_CALLBACK (on_account_country_selector_changed), self);
1260
1261                 modest_country_picker_set_active_country_locale (
1262                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1263                 mcc = modest_country_picker_get_active_country_mcc (
1264                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1265                 modest_provider_picker_fill (
1266                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker),
1267                         priv->presets, mcc);
1268                 /* connect to providers picker's changed signal, so we can fill the email address: */
1269                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector
1270                                             (HILDON_PICKER_BUTTON (priv->account_serviceprovider_picker))),
1271                                   "changed",
1272                                   G_CALLBACK (on_account_serviceprovider_selector_changed), self);
1273
1274                 modest_provider_picker_set_others_provider (MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
1275         }
1276
1277         priv->dirty = FALSE;
1278
1279 }
1280
1281 typedef struct IdleData {
1282         ModestEasysetupWizardDialog *dialog;
1283         ModestPresets *presets;
1284 } IdleData;
1285
1286 static gboolean
1287 presets_idle (gpointer userdata)
1288 {
1289         IdleData *idle_data = (IdleData *) userdata;
1290         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (idle_data->dialog);
1291         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1292
1293         g_assert (idle_data->presets);
1294
1295         gdk_threads_enter ();
1296
1297         priv->presets = idle_data->presets;
1298
1299         if (priv->check_support_done)
1300                 fill_providers (self);
1301
1302         g_object_unref (idle_data->dialog);
1303         g_free (idle_data);
1304
1305         gdk_threads_leave ();
1306
1307         return FALSE;
1308 }
1309
1310 static gpointer
1311 presets_loader (gpointer userdata)
1312 {
1313         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (userdata);
1314         ModestPresets *presets = NULL;
1315         IdleData *idle_data;
1316
1317         const gchar* path  = NULL;
1318         const gchar* path1 = MODEST_PROVIDER_DATA_FILE;
1319         const gchar* path2 = MODEST_FALLBACK_PROVIDER_DATA_FILE;
1320
1321         if (access(path1, R_OK) == 0)
1322                 path = path1;
1323         else if (access(path2, R_OK) == 0)
1324                 path = path2;
1325         else {
1326                 g_warning ("%s: neither '%s' nor '%s' is a readable provider data file",
1327                            __FUNCTION__, path1, path2);
1328                 return NULL;
1329         }
1330
1331         presets = modest_presets_new (path);
1332         if (!presets) {
1333                 g_warning ("%s: failed to parse '%s'", __FUNCTION__, path);
1334                 return NULL;
1335         }
1336
1337         idle_data = g_new0 (IdleData, 1);
1338         idle_data->dialog = self;
1339         idle_data->presets = presets;
1340
1341         g_idle_add (presets_idle, idle_data);
1342
1343         return NULL;
1344 }
1345
1346 static void
1347 modest_easysetup_wizard_dialog_append_page (GtkNotebook *notebook,
1348                                             GtkWidget *page,
1349                                             const gchar *label)
1350 {
1351         gint index;
1352
1353         /* Append page and set attributes */
1354         index = gtk_notebook_append_page (notebook, page, gtk_label_new (label));
1355         gtk_container_child_set (GTK_CONTAINER (notebook), page,
1356                                  "tab-expand", TRUE, "tab-fill", TRUE,
1357                                  NULL);
1358
1359         /* Give focus to page and show it */
1360         gtk_container_set_focus_child (GTK_CONTAINER (notebook), page);
1361         gtk_widget_show (page);
1362 }
1363
1364 static void
1365 init_user_page (ModestEasysetupWizardDialogPrivate *priv)
1366 {
1367         priv->page_user_details = NULL;
1368         priv->entry_user_name = NULL;
1369         priv->entry_user_username = NULL;
1370         priv->entry_user_password = NULL;
1371         priv->entry_user_email = NULL;
1372 }
1373
1374 static void
1375 init_incoming_page (ModestEasysetupWizardDialogPrivate *priv)
1376 {
1377         priv->page_custom_incoming = NULL;
1378         priv->incoming_servertype_picker = NULL;
1379         priv->caption_incoming = NULL;
1380         priv->entry_incomingserver = NULL;
1381         priv->entry_user_email = NULL;
1382         priv->incoming_security = NULL;
1383 }
1384
1385 static void
1386 init_outgoing_page (ModestEasysetupWizardDialogPrivate *priv)
1387 {
1388         priv->page_custom_outgoing = NULL;
1389         priv->entry_outgoingserver = NULL;
1390         priv->checkbox_outgoing_smtp_specific = NULL;
1391         priv->button_outgoing_smtp_servers = NULL;
1392         priv->outgoing_security = NULL;
1393 }
1394
1395 static void
1396 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1397 {
1398         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1399
1400         /* Create the notebook to be used by the ModestWizardDialog base class:
1401          * Each page of the notebook will be a page of the wizard: */
1402         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1403         gtk_widget_set_size_request (GTK_WIDGET (notebook), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
1404
1405         /* Set the notebook used by the ModestWizardDialog base class: */
1406         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1407
1408         /* Set the wizard title:
1409          * The actual window title will be a combination of this and the page's tab label title. */
1410         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1411
1412         /* Read in the information about known service providers: */
1413         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1414
1415         /* The server fields did not have been manually changed yet */
1416         priv->server_changes = 0;
1417         priv->pending_load_settings = TRUE;
1418
1419         /* Get the account manager object,
1420          * so we can check for existing accounts,
1421          * and create new accounts: */
1422         priv->account_manager = modest_runtime_get_account_mgr ();
1423         g_object_ref (priv->account_manager);
1424
1425         /* Initialize fields */
1426         priv->check_support_done = FALSE;
1427         priv->check_support_show_progress_id = 0;
1428         priv->pending_check_support = 0;
1429         priv->destroyed = FALSE;
1430         priv->page_welcome = create_page_welcome (self);
1431         gtk_widget_show (priv->page_welcome);
1432         priv->page_account_details = create_page_account_details (self);
1433
1434         init_user_page (priv);
1435         init_incoming_page (priv);
1436         init_outgoing_page (priv);
1437
1438         priv->page_complete_easysetup = NULL;
1439         priv->page_complete_customsetup = NULL;
1440         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1441         priv->missing_data_signals = NULL;
1442
1443         /* Add the common pages */
1444         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_welcome,
1445                                                     _("mcen_ti_emailsetup_welcome"));
1446         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_account_details,
1447                                                     _("mcen_ti_accountdetails"));
1448
1449         /* Connect to the dialog's response signal so we can enable/disable buttons
1450          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1451          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1452          * so that we can be enable/disable after ModestWizardDialog has done its own
1453          * enabling/disabling of buttons.
1454          *
1455          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler
1456          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1457          *
1458          * It's not enough to connect to the notebook's switch-page signal, because
1459          * ModestWizardDialog's "response" signal handler enables the buttons itself,
1460          * _after_ switching the page (understandably).
1461          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1462          * then gtk_notebook_get_current_page() would return an incorrect value.)
1463          */
1464         g_signal_connect_after (G_OBJECT (self), "response",
1465                                 G_CALLBACK (on_response), self);
1466
1467         /* This is to show a confirmation dialog when the user hits cancel */
1468         g_signal_connect (G_OBJECT (self), "response",
1469                           G_CALLBACK (on_response_before), self);
1470
1471         g_signal_connect (G_OBJECT (self), "delete-event",
1472                           G_CALLBACK (on_delete_event), self);
1473
1474         /* Reset dirty, because there was no user input until now */
1475         priv->dirty = FALSE;
1476
1477         /* When this window is shown, hibernation should not be possible,
1478          * because there is no sensible way to save the state: */
1479         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1480                 modest_runtime_get_window_mgr (), GTK_WINDOW (self));
1481
1482         /* Load provider presets */
1483         g_object_ref (self);
1484         g_thread_create (presets_loader, self, FALSE, NULL);
1485
1486         priv->settings = modest_account_settings_new ();
1487
1488         check_support_of_protocols (self);
1489 }
1490
1491 ModestEasysetupWizardDialog*
1492 modest_easysetup_wizard_dialog_new (void)
1493 {
1494
1495         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1496 }
1497
1498 static void
1499 create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1500 {
1501         ModestEasysetupWizardDialogPrivate *priv;
1502         GtkNotebook *notebook = NULL;
1503
1504         g_object_get (self, "wizard-notebook", &notebook, NULL);
1505         g_assert(notebook);
1506
1507         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1508
1509         if (!priv->page_user_details) {
1510                 priv->page_user_details = create_page_user_details (self);
1511         }
1512
1513         /* Create the custom pages: */
1514         if(!(priv->page_custom_incoming)) {
1515                 priv->page_custom_incoming = create_page_custom_incoming (self);
1516         }
1517
1518         /* TODO: only if needed */
1519         if(!(priv->page_custom_outgoing)) {
1520                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1521         }
1522
1523         if(!(priv->page_complete_customsetup)) {
1524                 priv->page_complete_customsetup = create_page_complete_custom (self);
1525         }
1526
1527         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_user_details)))
1528                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_user_details,
1529                                                             _("mcen_ti_emailsetup_userdetails"));
1530
1531         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1532                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_incoming,
1533                                                             _("mcen_ti_emailsetup_incomingdetails"));
1534
1535         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))
1536                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_outgoing,
1537                                                             _("mcen_ti_emailsetup_outgoingdetails"));
1538
1539         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup)))
1540                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_customsetup,
1541                                                             _("mcen_ti_emailsetup_complete"));
1542
1543         /* This is unnecessary with GTK+ 2.10: */
1544         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1545 }
1546
1547 static void
1548 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1549 {
1550         ModestEasysetupWizardDialogPrivate *priv;
1551         GtkNotebook *notebook = NULL;
1552
1553         g_object_get (self, "wizard-notebook", &notebook, NULL);
1554         g_assert(notebook);
1555
1556         /* Create the easysetup-specific pages: */
1557         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1558         if(!priv->page_complete_easysetup)
1559                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1560
1561         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup)))
1562                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_easysetup,
1563                                                             _("mcen_ti_emailsetup_complete"));
1564
1565         /* This is unnecessary with GTK+ 2.10: */
1566         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1567 }
1568
1569 /* */
1570 static void
1571 remove_non_common_tabs (GtkNotebook *notebook,
1572                         gboolean remove_user_details)
1573 {
1574         gint starting_tab;
1575         /* The first 2 tabs are the common ones (welcome tab and the
1576            providers tab), so we always remove starting from the
1577            end */
1578
1579         starting_tab = (remove_user_details) ? 2 : 3;
1580         while (gtk_notebook_get_n_pages (notebook) > starting_tab)
1581                 gtk_notebook_remove_page (notebook, -1);
1582 }
1583
1584 static void
1585 on_missing_mandatory_data (ModestAccountProtocol *protocol,
1586                            gboolean missing,
1587                            gpointer user_data)
1588 {
1589         real_enable_buttons (MODEST_WIZARD_DIALOG (user_data), !missing);
1590 }
1591
1592 /* After the user details page,
1593  * the following pages depend on whether "Other" was chosen
1594  * in the provider combobox on the account page
1595  */
1596 static void
1597 create_subsequent_pages (ModestEasysetupWizardDialog *self)
1598 {
1599         ModestEasysetupWizardDialogPrivate *priv;
1600         ModestProviderPicker *picker;
1601         ModestProviderPickerIdType id_type;
1602         GtkNotebook *notebook;
1603
1604         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1605         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1606         id_type = modest_provider_picker_get_active_id_type (picker);
1607         g_object_get (self, "wizard-notebook", &notebook, NULL);
1608         modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1609                                                             NULL);
1610
1611
1612         if (id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
1613                 /* "Other..." was selected: */
1614
1615                 /* If we come from a rollbacked easysetup */
1616                 if (priv->page_complete_easysetup) {
1617                         remove_non_common_tabs (notebook, FALSE);
1618                         priv->page_complete_easysetup = NULL;
1619                 }
1620
1621                 /* If we come from a rollbacked plugin protocol setup */
1622                 if (priv->last_plugin_protocol_selected != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1623                         remove_non_common_tabs (notebook, TRUE);
1624                         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1625                         modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1626                         priv->missing_data_signals = NULL;
1627                 }
1628
1629                 create_subsequent_customsetup_pages (self);
1630         } else {
1631                 /* If we come from a rollbacked custom setup */
1632                 if (priv->page_custom_incoming) {
1633                         remove_non_common_tabs (notebook, TRUE);
1634                         init_user_page (priv);
1635                         init_incoming_page (priv);
1636                         init_outgoing_page (priv);
1637                         init_user_page (priv);
1638                         priv->page_complete_customsetup = NULL;
1639                 }
1640
1641                 /* It's a pluggable protocol and not a provider with presets */
1642                 if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1643                         ModestProtocol *protocol;
1644                         gchar *proto_name;
1645                         ModestProtocolType proto_type;
1646
1647                         /* Get protocol data */
1648                         proto_name = modest_provider_picker_get_active_provider_id (picker);
1649                         protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
1650                                                                                   MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1651                                                                                   proto_name);
1652                         proto_type = modest_protocol_get_type_id (protocol);
1653
1654
1655                         /* If we come from a rollbacked easy setup */
1656                         if (priv->last_plugin_protocol_selected != proto_type &&
1657                             priv->page_complete_easysetup) {
1658                                 remove_non_common_tabs (notebook, TRUE);
1659                                 init_user_page (priv);
1660                                 priv->page_complete_easysetup = NULL;
1661                         }
1662
1663                         if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol) &&
1664                             proto_type != priv->last_plugin_protocol_selected) {
1665                                 ModestPairList *tabs;
1666                                 GSList *tmp;
1667                                 gboolean first_page = TRUE;
1668                                 ModestWizardDialogResponseOverrideFunc response_override;
1669
1670                                 /* Remember the last selected plugin protocol */
1671                                 priv->last_plugin_protocol_selected = proto_type;
1672
1673                                 /* Get tabs */
1674                                 tabs = modest_account_protocol_get_easysetupwizard_tabs (MODEST_ACCOUNT_PROTOCOL (protocol));
1675                                 response_override = modest_account_protocol_get_wizard_response_override
1676                                         (MODEST_ACCOUNT_PROTOCOL (protocol));
1677                                 modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1678                                                                                     response_override);
1679                                 tmp = (GSList *) tabs;
1680                                 while (tmp) {
1681                                         ModestPair *pair = (ModestPair *) tmp->data;
1682                                         modest_easysetup_wizard_dialog_append_page (notebook,
1683                                                                                     GTK_WIDGET (pair->second),
1684                                                                                     (const gchar *) pair->first);
1685                                         if (first_page) {
1686                                                 gtk_container_set_focus_child (GTK_CONTAINER (notebook),
1687                                                                                GTK_WIDGET (pair->second));
1688                                                 first_page = FALSE;
1689                                         }
1690
1691                                         /* Connect signals */
1692                                         if (MODEST_IS_EASYSETUP_WIZARD_PAGE (pair->second)) {
1693                                                 priv->missing_data_signals =
1694                                                         modest_signal_mgr_connect (priv->missing_data_signals,
1695                                                                                    G_OBJECT (pair->second),
1696                                                                                    "missing-mandatory-data",
1697                                                                                    G_CALLBACK (on_missing_mandatory_data),
1698                                                                                    self);
1699                                         }
1700
1701                                         g_free (pair->first);
1702                                         tmp = g_slist_next (tmp);
1703                                         /* Critical: if you don't show the page then the dialog will ignore it */
1704 /*                                      gtk_widget_show (GTK_WIDGET (pair->second)); */
1705                                 }
1706                                 modest_pair_list_free (tabs);
1707                         }
1708                         g_free (proto_name);
1709                 } else {
1710                         if (priv->last_plugin_protocol_selected !=
1711                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1712                                 remove_non_common_tabs (notebook, TRUE);
1713                                 init_user_page (priv);
1714                                 priv->page_complete_easysetup = NULL;
1715                                 priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1716                                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1717                                 priv->missing_data_signals = NULL;
1718                         }
1719                         if (!priv->page_user_details) {
1720                                 priv->page_user_details = create_page_user_details (self);
1721                                 modest_easysetup_wizard_dialog_append_page (notebook,
1722                                                                             priv->page_user_details,
1723                                                                             _("mcen_ti_emailsetup_userdetails"));
1724                         }
1725                 }
1726
1727                 /* Create the easysetup pages: */
1728                 create_subsequent_easysetup_pages (self);
1729         }
1730 }
1731
1732
1733 static gchar*
1734 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocolType protocol_type)
1735 {
1736         const gchar* hostname = NULL;
1737         gchar* at;
1738         gchar* domain;
1739         ModestProtocolRegistry *protocol_registry;
1740         ModestProtocol *protocol;
1741
1742         if (!email_address)
1743                 return NULL;
1744
1745         at = g_utf8_strchr (email_address, -1, '@');
1746         if (!at || (g_utf8_strlen (at, -1) < 2))
1747                 return NULL;
1748
1749         domain = g_utf8_next_char (at);
1750         if(!domain)
1751                 return NULL;
1752
1753         protocol_registry = modest_runtime_get_protocol_registry ();
1754         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
1755
1756         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS) ||
1757             modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
1758                 hostname = modest_protocol_get_name (protocol);
1759         }
1760
1761         if (!hostname)
1762                 return NULL;
1763
1764         return g_strdup_printf ("%s.%s", hostname, domain);
1765 }
1766
1767 static void
1768 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1769 {
1770         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1771
1772         if (!priv->entry_incomingserver)
1773                 return;
1774
1775         /* Set a default domain for the server, based on the email address,
1776          * if no server name was already specified.
1777          */
1778         if (priv->entry_user_email
1779             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1780                 const ModestProtocolType protocol_type = modest_servertype_picker_get_active_servertype (
1781                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
1782
1783                 /* This could happen when the combo box has still no active iter */
1784                 if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1785                         const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));
1786                         gchar* servername = util_get_default_servername_from_email_address (email_address,
1787                                                                                             protocol_type);
1788
1789                         /* Do not set the INCOMING_CHANGED flag because of this edit */
1790                         g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1791                         hildon_entry_set_text (HILDON_ENTRY (priv->entry_incomingserver), servername);
1792                         g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1793
1794                         g_free (servername);
1795                 }
1796         }
1797
1798         /* Set a default domain for the server, based on the email address,
1799          * if no server name was already specified.
1800          */
1801         if (!priv->entry_outgoingserver)
1802                 return;
1803
1804         if (priv->entry_user_email
1805             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1806                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));
1807
1808                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOLS_TRANSPORT_SMTP);
1809
1810                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1811                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1812                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_outgoingserver), servername);
1813                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1814
1815                 g_free (servername);
1816         }
1817 }
1818
1819 static gchar*
1820 get_entered_account_title (ModestEasysetupWizardDialog *self)
1821 {
1822         ModestEasysetupWizardDialogPrivate *priv;
1823         const gchar* account_title;
1824
1825         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1826         account_title = hildon_entry_get_text (HILDON_ENTRY (priv->entry_account_title));
1827
1828         if (!account_title || (g_utf8_strlen (account_title, -1) == 0)) {
1829                 return NULL;
1830         } else {
1831                 /* Strip it of whitespace at the start and end: */
1832                 gchar *result = g_strdup (account_title);
1833                 result = g_strstrip (result);
1834
1835                 if (!result)
1836                         return NULL;
1837
1838                 if (g_utf8_strlen (result, -1) == 0) {
1839                         g_free (result);
1840                         return NULL;
1841                 }
1842
1843                 return result;
1844         }
1845 }
1846
1847 static gboolean
1848 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1849 {
1850         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1851         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1852         ModestProtocolRegistry *protocol_registry;
1853
1854         protocol_registry = modest_runtime_get_protocol_registry ();
1855
1856         /* if are browsing pages previous to the last one, then we have pending settings in
1857          * this wizard */
1858         if (next_page != NULL)
1859                 priv->pending_load_settings = TRUE;
1860
1861         /* Do extra validation that couldn't be done for every key press,
1862          * either because it was too slow,
1863          * or because it requires interaction:
1864          */
1865         if (current_page == priv->page_account_details) {
1866                 /* Check that the title is not already in use: */
1867                 gchar* account_title = get_entered_account_title (self);
1868                 if (!account_title)
1869                         return FALSE;
1870
1871                 /* Aavoid a clash with an existing display name: */
1872                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1873                         priv->account_manager, account_title);
1874                 g_free (account_title);
1875
1876                 if (name_in_use) {
1877                         /* Warn the user via a dialog: */
1878                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1879
1880                         return FALSE;
1881                 }
1882
1883                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1884                 create_subsequent_pages (self);
1885
1886         } else if (current_page == priv->page_user_details) {
1887                 /* Check that the email address is valud: */
1888                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
1889                 if ((!email_address) || (g_utf8_strlen (email_address, -1) == 0))
1890                         return FALSE;
1891
1892                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1893                         /* Warn the user via a dialog: */
1894                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1895
1896                         /* Return focus to the email address entry: */
1897                         gtk_widget_grab_focus (priv->entry_user_email);
1898                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1899
1900                         return FALSE;
1901                 }
1902         } else if (current_page == priv->page_custom_incoming ||
1903                    current_page == priv->page_custom_outgoing) {
1904
1905                 const gchar *hostname;
1906                 GtkWidget *entry = (current_page == priv->page_custom_incoming) ?
1907                         priv->entry_incomingserver :
1908                         priv->entry_outgoingserver;
1909
1910                 hostname = gtk_entry_get_text (GTK_ENTRY (entry));
1911                 if (!modest_text_utils_validate_domain_name (hostname)) {
1912                         /* Show error */
1913                         modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
1914
1915                         /* Return focus to the email address entry: */
1916                         gtk_widget_grab_focus (entry);
1917                         gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1918
1919                         return FALSE;
1920                 }
1921         }
1922
1923         if (next_page == priv->page_custom_incoming) {
1924                 set_default_custom_servernames (self);
1925         } else if (next_page == priv->page_custom_outgoing) {
1926                 set_default_custom_servernames (self);
1927
1928                 /* Check if the server supports secure authentication */
1929                 if (modest_security_options_view_auth_check (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security)))
1930                         if (!check_has_supported_auth_methods (self))
1931                                 return FALSE;
1932                 gtk_widget_show (priv->outgoing_security);
1933         }
1934
1935         /* If this is the last page, and this is a click on Finish,
1936          * then attempt to create the dialog.
1937          */
1938         if(!next_page &&
1939            current_page != priv->page_account_details) /* This is NULL when this is a click on Finish. */
1940         {
1941                 if (priv->pending_load_settings) {
1942                         save_to_settings (self);
1943                 }
1944
1945                 /* We check if there's already another account with the same configuration */
1946                 if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
1947                         modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1948                         return FALSE;
1949                 }
1950
1951                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1952         }
1953
1954
1955         return TRUE;
1956 }
1957
1958 static gboolean entry_is_empty (GtkWidget *entry)
1959 {
1960         if (!entry)
1961                 return FALSE;
1962
1963         const gchar* text = hildon_entry_get_text (HILDON_ENTRY (entry));
1964         if ((!text) || (g_utf8_strlen (text, -1) == 0))
1965                 return TRUE;
1966         else {
1967                 /* Strip it of whitespace at the start and end: */
1968                 gchar *stripped = g_strdup (text);
1969                 stripped = g_strstrip (stripped);
1970
1971                 if (!stripped)
1972                         return TRUE;
1973
1974                 const gboolean result = (g_utf8_strlen (stripped, -1) == 0);
1975
1976                 g_free (stripped);
1977                 return result;
1978         }
1979 }
1980
1981 static void
1982 real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next)
1983 {
1984         GtkNotebook *notebook = NULL;
1985         gboolean is_finish_tab;
1986         GtkWidget *current;
1987         ModestEasysetupWizardDialogPrivate *priv;
1988
1989         /* Get data */
1990         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (dialog);
1991         g_object_get (dialog, "wizard-notebook", &notebook, NULL);
1992
1993         /* Disable the Finish button until we are on the last page,
1994          * because HildonWizardDialog enables this for all but the
1995          * first page */
1996         current = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
1997         is_finish_tab = ((current == priv->page_complete_easysetup) ||
1998                          (current == priv->page_complete_customsetup));
1999
2000         if (is_finish_tab) {
2001                 /* Disable Next on the last page no matter what the
2002                    argument say */
2003                 enable_next = FALSE;
2004         } else {
2005                 /* Not the last one */
2006                 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
2007                                                    MODEST_WIZARD_DIALOG_FINISH,
2008                                                    FALSE);
2009
2010                 /* If the check support is not done then do not enable
2011                    the wizard to continue */
2012                 enable_next = enable_next && priv->check_support_done;
2013         }
2014
2015         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
2016                                            MODEST_WIZARD_DIALOG_NEXT,
2017                                            enable_next);
2018 }
2019
2020 static void
2021 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
2022 {
2023         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2024         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2025
2026         gboolean enable_next = TRUE;
2027         if (current_page == priv->page_welcome) {
2028                 enable_next = TRUE;
2029         } else if (current_page == priv->page_account_details) {
2030                 /* The account details title is mandatory: */
2031                 if (entry_is_empty(priv->entry_account_title))
2032                         enable_next = FALSE;
2033         } else if (current_page == priv->page_user_details) {
2034                 /* The user details username is mandatory: */
2035                 if (entry_is_empty(priv->entry_user_username))
2036                         enable_next = FALSE;
2037
2038                 /* The user details email address is mandatory: */
2039                 if (enable_next && entry_is_empty (priv->entry_user_email))
2040                         enable_next = FALSE;
2041         } else if (current_page == priv->page_custom_incoming) {
2042                 /* The custom incoming server is mandatory: */
2043                 if (entry_is_empty(priv->entry_incomingserver))
2044                         enable_next = FALSE;
2045         } else if (MODEST_IS_EASYSETUP_WIZARD_PAGE (current_page)) {
2046                 enable_next = !modest_easysetup_wizard_page_validate (
2047                        MODEST_EASYSETUP_WIZARD_PAGE (current_page));
2048         }
2049
2050         /* Enable/disable buttons */
2051         real_enable_buttons (dialog, enable_next);
2052 }
2053
2054 static void
2055 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
2056 {
2057         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2058         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
2059
2060
2061         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
2062         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
2063
2064         /* Provide a vfunc implementation so we can decide
2065          * when to enable/disable the prev/next buttons.
2066          */
2067         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
2068         base_klass->before_next = on_before_next;
2069         base_klass->enable_buttons = on_enable_buttons;
2070         base_klass->update_model = on_update_model;
2071         base_klass->save = on_save;
2072 }
2073
2074 /**
2075  * save_to_settings:
2076  * @self: a #ModestEasysetupWizardDialog
2077  *
2078  * takes information from all the wizard and stores it in settings
2079  */
2080 static void
2081 save_to_settings (ModestEasysetupWizardDialog *self)
2082 {
2083         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2084         guint special_port;
2085         gchar *provider_id = NULL;
2086         gchar* display_name;
2087         const gchar *username, *password;
2088         gchar *store_hostname, *transport_hostname;
2089         guint store_port, transport_port;
2090         ModestProtocolRegistry *protocol_registry;
2091         ModestProtocolType store_protocol, transport_protocol;
2092         ModestProtocolType store_security, transport_security;
2093         ModestProtocolType store_auth_protocol, transport_auth_protocol;
2094         ModestServerAccountSettings *store_settings, *transport_settings;
2095         const gchar *fullname, *email_address;
2096         ModestProviderPicker *picker;
2097         ModestProviderPickerIdType id_type;
2098
2099         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2100         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
2101         protocol_registry = modest_runtime_get_protocol_registry ();
2102
2103         /* Get details from the specified presets: */
2104         id_type = modest_provider_picker_get_active_id_type (picker);
2105         provider_id = modest_provider_picker_get_active_provider_id (picker);
2106
2107         /* Let the plugin save the settings. We do a return in order
2108            to save an indentation level */
2109         if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
2110                 ModestProtocol *protocol;
2111
2112                 protocol = modest_protocol_registry_get_protocol_by_name (
2113                        modest_runtime_get_protocol_registry (),
2114                        MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
2115                        provider_id);
2116
2117                 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
2118                         gint n_pages, i = 0;
2119                         GtkNotebook *notebook;
2120                         GList *wizard_pages = NULL;
2121
2122                         g_object_get (self, "wizard-notebook", &notebook, NULL);
2123                         n_pages = gtk_notebook_get_n_pages (notebook);
2124                         for (i = 0; i < n_pages; i++) {
2125                                 GtkWidget *page = gtk_notebook_get_nth_page (notebook, i);
2126                                 if (MODEST_IS_EASYSETUP_WIZARD_PAGE (page))
2127                                         wizard_pages  = g_list_append (wizard_pages, page);
2128                         }
2129                         modest_account_protocol_save_wizard_settings (MODEST_ACCOUNT_PROTOCOL (protocol),
2130                                                                       wizard_pages,
2131                                                                       priv->settings);
2132                         g_list_free (wizard_pages);
2133                 } else {
2134                         g_debug ("The selected protocol is a plugin protocol "//
2135                                  "but it's not a ModestAccountProtocol");
2136                 }
2137
2138                 g_free (provider_id);
2139                 return;
2140         }
2141
2142         /* username and password (for both incoming and outgoing): */
2143         username = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username));
2144         password = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_password));
2145
2146         store_settings = modest_account_settings_get_store_settings (priv->settings);
2147         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
2148
2149         /* Incoming server: */
2150         /* Note: We need something as default for the transport store protocol values,
2151          * or modest_account_mgr_add_server_account will fail. */
2152         store_port = 0;
2153         store_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2154         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2155         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2156
2157         if (provider_id) {
2158                 ModestProtocolType store_provider_server_type;
2159                 gboolean store_provider_use_alternate_port;
2160                 /* Use presets: */
2161                 store_hostname = modest_presets_get_server (priv->presets, provider_id,
2162                                                             TRUE /* store */);
2163
2164                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2165                                                                          provider_id,
2166                                                                          TRUE /* store */);
2167                 store_security  = modest_presets_get_info_server_security (priv->presets,
2168                                                                            provider_id,
2169                                                                            TRUE /* store */);
2170                 store_auth_protocol  = modest_presets_get_info_server_auth (priv->presets,
2171                                                                             provider_id,
2172                                                                             TRUE /* store */);
2173                 store_provider_use_alternate_port  = modest_presets_get_info_server_use_alternate_port (priv->presets,
2174                                                                                                         provider_id,
2175                                                                                                         TRUE /* store */);
2176
2177                 /* We don't check for SMTP here as that is impossible for an incoming server. */
2178                 if (store_provider_server_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID)
2179                         store_protocol = MODEST_PROTOCOLS_STORE_POP;
2180                 else
2181                         store_protocol = store_provider_server_type;
2182
2183                 /* we check if there is a *special* port */
2184                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
2185                 if (special_port != 0) {
2186                         store_port = special_port;
2187                 } else {
2188                         gboolean use_alternate_port = FALSE;
2189                         if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
2190                                                                               store_security))
2191                                 use_alternate_port = TRUE;
2192                         store_port = get_port_from_protocol(store_provider_server_type, use_alternate_port);
2193                 }
2194
2195                 modest_server_account_settings_set_security_protocol (store_settings,
2196                                                                       store_security);
2197                 modest_server_account_settings_set_auth_protocol (store_settings,
2198                                                                   store_auth_protocol);
2199                 if (store_port != 0)
2200                         modest_server_account_settings_set_port (store_settings, store_port);
2201         } else {
2202                 /* Use custom pages because no preset was specified: */
2203                 store_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_incomingserver) ));
2204                 store_protocol = modest_servertype_picker_get_active_servertype (
2205                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
2206
2207                 modest_security_options_view_save_settings (
2208                                     MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
2209                                     priv->settings);
2210         }
2211
2212         /* now we store the common store account settings */
2213         modest_server_account_settings_set_hostname (store_settings, store_hostname);
2214         modest_server_account_settings_set_username (store_settings, username);
2215         modest_server_account_settings_set_password (store_settings, password);
2216         modest_server_account_settings_set_protocol (store_settings, store_protocol);
2217
2218         g_object_unref (store_settings);
2219         g_free (store_hostname);
2220
2221         /* Outgoing server: */
2222         transport_hostname = NULL;
2223         transport_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2224         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2225         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2226         transport_port = 0;
2227
2228         if (provider_id) {
2229                 ModestProtocolType transport_provider_server_type;
2230                 ModestProtocolType transport_provider_security;
2231                 gboolean is_secure;
2232
2233                 /* Use presets */
2234                 transport_hostname = modest_presets_get_server (priv->presets, provider_id,
2235                                                                 FALSE /* transport */);
2236
2237                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2238                                                                                       provider_id,
2239                                                                                       FALSE /* transport */);
2240                 transport_provider_security = modest_presets_get_info_server_security (priv->presets,
2241                                                                                        provider_id,
2242                                                                                        FALSE /* transport */);
2243
2244                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
2245                 transport_protocol = transport_provider_server_type;
2246                 transport_security = transport_provider_security;
2247                 is_secure = modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
2248                                                                               transport_security);
2249
2250                 /* we check if there is a *special* port */
2251                 special_port = modest_presets_get_port (priv->presets, provider_id,
2252                                                         FALSE /* transport */);
2253                 if (special_port != 0) {
2254                         transport_port = special_port;
2255                 } else {
2256                         gboolean use_alternate_port = FALSE;
2257                         if (transport_security == MODEST_PROTOCOLS_CONNECTION_SSL)
2258                                 use_alternate_port = TRUE;
2259                         transport_port = get_port_from_protocol(transport_provider_server_type, use_alternate_port);
2260                 }
2261
2262                 if (is_secure)
2263                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
2264                 else
2265                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2266
2267                 modest_server_account_settings_set_security_protocol (transport_settings,
2268                                                                       transport_security);
2269                 modest_server_account_settings_set_auth_protocol (transport_settings,
2270                                                                   transport_auth_protocol);
2271                 if (transport_port != 0)
2272                         modest_server_account_settings_set_port (transport_settings,
2273                                                                  transport_port);
2274         } else {
2275                 ModestProtocolRegistry *registry;
2276                 ModestProtocol *store_proto;
2277
2278                 registry = modest_runtime_get_protocol_registry ();
2279                 /* Use custom pages because no preset was specified: */
2280                 transport_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver) ));
2281
2282                 store_proto = modest_protocol_registry_get_protocol_by_type (registry,
2283                                                                              store_protocol);
2284
2285                 if (transport_protocol == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
2286                         /* fallback to SMTP if none was specified */
2287                         g_warning ("No transport protocol was specified for store %d (%s)",
2288                                    modest_protocol_get_type_id (store_proto),
2289                                    modest_protocol_get_display_name (store_proto));
2290                         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP;
2291                 }
2292
2293                 modest_security_options_view_save_settings (
2294                                     MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security),
2295                                     priv->settings);
2296         }
2297
2298         /* now we store the common transport account settings */
2299         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
2300         modest_server_account_settings_set_username (transport_settings, username);
2301         modest_server_account_settings_set_password (transport_settings, password);
2302         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
2303
2304         g_object_unref (transport_settings);
2305         g_free (transport_hostname);
2306
2307         fullname = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_name));
2308         email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
2309         modest_account_settings_set_fullname (priv->settings, fullname);
2310         modest_account_settings_set_email_address (priv->settings, email_address);
2311         /* we don't set retrieve type to preserve advanced settings if
2312            any. By default account settings are set to headers only */
2313
2314         /* Save the connection-specific SMTP server accounts. */
2315         if (priv->checkbox_outgoing_smtp_specific) {
2316                 modest_account_settings_set_use_connection_specific_smtp
2317                         (priv->settings,
2318                          hildon_check_button_get_active(HILDON_CHECK_BUTTON(priv->checkbox_outgoing_smtp_specific)));
2319         }
2320
2321         display_name = get_entered_account_title (self);
2322         modest_account_settings_set_display_name (priv->settings, display_name);
2323         g_free (display_name);
2324         g_free (provider_id);
2325
2326         /* This values are hardcoded for Fremantle */
2327         modest_account_settings_set_retrieve_type (priv->settings,
2328                                                    MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS);
2329         modest_account_settings_set_retrieve_limit (priv->settings, 20);
2330 }
2331
2332
2333 static void
2334 on_update_model (ModestWizardDialog *dialog)
2335 {
2336         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2337         save_to_settings (self);
2338
2339 }
2340
2341 static gboolean
2342 on_save (ModestWizardDialog *dialog)
2343 {
2344         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2345         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2346
2347         save_to_settings (self);
2348
2349         if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
2350                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
2351                 return FALSE;
2352         }
2353
2354         return modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
2355
2356 }
2357
2358
2359 static GList*
2360 check_for_supported_auth_methods (ModestEasysetupWizardDialog* self)
2361 {
2362         GError *error = NULL;
2363         ModestProtocolType protocol_type;
2364         const gchar* hostname;
2365         const gchar* username;
2366         ModestProtocolType security_protocol_incoming_type;
2367         ModestProtocolRegistry *registry;
2368         int port_num;
2369         GList *list_auth_methods;
2370         ModestEasysetupWizardDialogPrivate *priv;
2371
2372         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2373         registry = modest_runtime_get_protocol_registry ();
2374         protocol_type = modest_servertype_picker_get_active_servertype (
2375                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
2376         hostname = gtk_entry_get_text(GTK_ENTRY(priv->entry_incomingserver));
2377         username = gtk_entry_get_text(GTK_ENTRY(priv->entry_user_username));
2378         security_protocol_incoming_type = modest_security_options_view_get_connection_protocol
2379                 (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security));
2380         port_num = get_port_from_protocol(protocol_type, FALSE);
2381         list_auth_methods = modest_utils_get_supported_secure_authentication_methods (protocol_type, hostname, port_num,
2382                                                                                       username, GTK_WINDOW (self), &error);
2383
2384         if (list_auth_methods) {
2385                 /* TODO: Select the correct method */
2386                 GList* list = NULL;
2387                 GList* method;
2388                 for (method = list_auth_methods; method != NULL; method = g_list_next(method)) {
2389                         ModestProtocolType auth_protocol_type = (ModestProtocolType) (GPOINTER_TO_INT(method->data));
2390                         if (modest_protocol_registry_protocol_type_has_tag (registry, auth_protocol_type,
2391                                                                             MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS)) {
2392                                 list = g_list_append(list, GINT_TO_POINTER(auth_protocol_type));
2393                         }
2394                 }
2395
2396                 g_list_free(list_auth_methods);
2397
2398                 if (list)
2399                         return list;
2400         }
2401
2402         if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
2403                         error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
2404         {
2405                 modest_platform_information_banner (GTK_WIDGET(self), NULL,
2406                                                     _("mcen_ib_unableto_discover_auth_methods"));
2407         }
2408
2409         if(error != NULL)
2410                 g_error_free(error);
2411
2412         return NULL;
2413 }
2414
2415 static gboolean
2416 check_has_supported_auth_methods(ModestEasysetupWizardDialog* self)
2417 {
2418         GList* methods = check_for_supported_auth_methods(self);
2419         if (!methods)
2420         {
2421                 return FALSE;
2422         }
2423
2424         g_list_free(methods);
2425         return TRUE;
2426 }
2427
2428 static gboolean
2429 check_support_show_progress (gpointer userdata)
2430 {
2431         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2432         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2433
2434         priv->check_support_show_progress_id = 0;
2435
2436         if (priv->destroyed)
2437                 return FALSE;
2438
2439         hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
2440
2441         return FALSE;
2442 }
2443
2444 static void
2445 check_support_callback (ModestAccountProtocol *protocol,
2446                         gboolean supported,
2447                         gpointer userdata)
2448 {
2449         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2450         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2451
2452         priv->pending_check_support --;
2453
2454         if (priv->check_support_show_progress_id > 0) {
2455                 g_source_remove (priv->check_support_show_progress_id);
2456                 priv->check_support_show_progress_id = 0;
2457         }
2458
2459         if (priv->pending_check_support == 0) {
2460                 priv->check_support_done = TRUE;
2461
2462                 if (!priv->destroyed) {
2463                         if (priv->presets)
2464                                 fill_providers (self);
2465                         hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), FALSE);
2466                         invoke_enable_buttons_vfunc (self);
2467                 }
2468         }
2469         g_object_unref (self);
2470 }
2471
2472
2473 static void
2474 check_support_of_protocols (ModestEasysetupWizardDialog *self)
2475 {
2476         ModestProtocolRegistry *registry;
2477         GSList *provider_protos, *node;
2478         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2479
2480         registry = modest_runtime_get_protocol_registry ();
2481         provider_protos = modest_protocol_registry_get_by_tag (registry,
2482                                                                MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
2483
2484         for (node = provider_protos; node != NULL; node = g_slist_next (node)) {
2485                 ModestProtocol *proto = MODEST_PROTOCOL (node->data);
2486
2487                 if (!modest_protocol_registry_protocol_type_has_tag (registry,
2488                                                                      modest_protocol_get_type_id (proto),
2489                                                                      MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS))
2490                         continue;
2491
2492                 if (modest_protocol_registry_protocol_type_has_tag
2493                     (registry,
2494                      modest_protocol_get_type_id (proto),
2495                      MODEST_PROTOCOL_REGISTRY_SINGLETON_PROVIDER_PROTOCOLS)) {
2496                         /* Check if there's already an account configured with this account type */
2497                         if (modest_account_mgr_singleton_protocol_exists (modest_runtime_get_account_mgr (),
2498                                                                           modest_protocol_get_type_id (proto)))
2499                                 continue;
2500                 }
2501
2502                 if (MODEST_ACCOUNT_PROTOCOL (proto)) {
2503                         priv->pending_check_support ++;
2504                         modest_account_protocol_check_support (MODEST_ACCOUNT_PROTOCOL (proto),
2505                                                                check_support_callback,
2506                                                                g_object_ref (self));
2507                 }
2508         }
2509         g_slist_free (provider_protos);
2510
2511         if (priv->pending_check_support > 0) {
2512                 priv->check_support_show_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 1000,
2513                                                                            check_support_show_progress,
2514                                                                            g_object_ref (self), g_object_unref);
2515         } else {
2516                 priv->check_support_done = TRUE;
2517         }
2518         invoke_enable_buttons_vfunc (self);
2519 }