638e5e151b1298243a2ce21da6e35713b6090a2e
[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
63 /* Include config.h so that _() works: */
64 #ifdef HAVE_CONFIG_H
65 #include <config.h>
66 #endif
67
68 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
69
70 #define MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
71                                                     MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, \
72                                                     ModestEasysetupWizardDialogPrivate))
73
74 #define LABELS_WIDTH -1
75
76 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
77
78
79 typedef enum {
80         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
81         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
82 } ModestEasysetupWizardDialogServerChanges;
83
84 struct _ModestEasysetupWizardDialogPrivate
85 {
86         ModestPresets *presets;
87
88         /* Remember what fields the user edited manually to not prefill them
89          * again. */
90         ModestEasysetupWizardDialogServerChanges server_changes;
91         
92         /* Check if the user changes a field to show a confirmation dialog */
93         gboolean dirty;
94
95         /* If we have a pending load of settings or not. */
96         gboolean pending_load_settings;
97         
98         /* Used by derived widgets to query existing accounts,
99          * and to create new accounts: */
100         ModestAccountMgr *account_manager;
101         ModestAccountSettings *settings;
102         
103         /* notebook pages: */
104         GtkWidget *page_welcome;
105         
106         GtkWidget *page_account_details;
107         GtkWidget *account_country_picker;
108         GtkWidget *account_serviceprovider_picker;
109         GtkWidget *entry_account_title;
110         
111         GtkWidget *page_user_details;
112         GtkWidget *entry_user_name;
113         GtkWidget *entry_user_username;
114         GtkWidget *entry_user_password;
115         GtkWidget *entry_user_email;
116         
117         GtkWidget *page_complete_easysetup;
118         
119         GtkWidget *page_custom_incoming;
120         GtkWidget *incoming_servertype_picker;
121         GtkWidget *caption_incoming;
122         GtkWidget *entry_incomingserver;
123
124         /* Security widgets */
125         GtkWidget *incoming_security;
126         GtkWidget *outgoing_security;
127
128         GtkWidget *page_custom_outgoing;
129         GtkWidget *entry_outgoingserver;
130         GtkWidget *checkbox_outgoing_smtp_specific;
131         GtkWidget *button_outgoing_smtp_servers;
132         
133         GtkWidget *page_complete_customsetup;
134
135         ModestProtocolType last_plugin_protocol_selected;
136         GSList *missing_data_signals;
137 };
138
139 static void save_to_settings (ModestEasysetupWizardDialog *self);
140 static void real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next);
141
142 static gboolean
143 on_delete_event (GtkWidget *widget,
144                  GdkEvent *event,
145                  ModestEasysetupWizardDialog *wizard)
146 {
147         gtk_dialog_response (GTK_DIALOG (wizard), GTK_RESPONSE_CANCEL);
148         return TRUE;
149 }
150
151 static void
152 on_easysetup_changed(GtkWidget* widget, ModestEasysetupWizardDialog* wizard)
153 {
154         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard);
155         g_return_if_fail (priv != NULL);
156         priv->dirty = TRUE;
157 }
158
159 static void
160 modest_easysetup_wizard_dialog_dispose (GObject *object)
161 {
162         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
163                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
164 }
165
166 static void
167 modest_easysetup_wizard_dialog_finalize (GObject *object)
168 {
169         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
170         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
171         
172         if (priv->account_manager)
173                 g_object_unref (G_OBJECT (priv->account_manager));
174                 
175         if (priv->presets)
176                 modest_presets_destroy (priv->presets);
177
178         if (priv->settings)
179                 g_object_unref (priv->settings);
180
181         if (priv->missing_data_signals) {
182                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
183                 priv->missing_data_signals = NULL;
184         }
185                 
186         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
187 }
188
189 static void
190 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
191
192 static void
193 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
194
195 static void on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data);
196
197 static gint 
198 get_port_from_protocol (ModestProtocolType server_type,
199                         gboolean alternate_port)
200 {
201         int server_port = 0;
202         ModestProtocol *protocol;
203         ModestProtocolRegistry *protocol_registry;
204
205         protocol_registry = modest_runtime_get_protocol_registry ();
206         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, server_type);
207
208         if (alternate_port) {
209                 server_port = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
210         } else {
211                 server_port = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
212         }
213         return server_port;
214 }
215
216 static void
217 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
218 {
219         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
220         
221         /* Call the vfunc, which may be overridden by derived classes: */
222         if (klass->enable_buttons) {
223                 GtkNotebook *notebook = NULL;
224                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
225                 
226                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
227                 if (current_page_num == -1)
228                         return;
229                         
230                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
231                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
232         }
233 }
234
235 static void
236 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
237 {
238         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
239         g_assert(self);
240         invoke_enable_buttons_vfunc(self);
241 }
242
243 static void
244 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
245 {
246         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
247         g_assert(self);
248         invoke_enable_buttons_vfunc(self);
249 }
250
251 static void
252 on_picker_button_value_changed (HildonPickerButton *widget, gpointer user_data)
253 {
254         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
255         g_assert(self);
256         invoke_enable_buttons_vfunc(self);
257 }
258
259 /** This is a convenience function to create a caption containing a mandatory widget.
260  * When the widget is edited, the enable_buttons() vfunc will be called.
261  */
262 static GtkWidget* 
263 create_captioned (ModestEasysetupWizardDialog *self,
264                   GtkSizeGroup *group,
265                   const gchar *value,
266                   GtkWidget *control)
267 {
268
269         GtkWidget *result;
270         result = modest_maemo_utils_create_captioned (group, value, control);
271
272         /* Connect to the appropriate changed signal for the widget, 
273          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
274          */
275         if (GTK_IS_ENTRY (control)) {
276                 g_signal_connect (G_OBJECT (control), "changed",
277                                   G_CALLBACK (on_caption_entry_changed), self);
278                 
279         }
280         else if (GTK_IS_COMBO_BOX (control)) {
281                 g_signal_connect (G_OBJECT (control), "changed",
282                                   G_CALLBACK (on_caption_combobox_changed), self);
283         }
284          
285         return result;
286 }
287            
288 static GtkWidget*
289 create_page_welcome (ModestEasysetupWizardDialog *self)
290 {
291         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
292         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
293         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
294         /* So that it is not truncated: */
295         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
296         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
297         gtk_widget_show (label);
298         gtk_widget_show (GTK_WIDGET (box));
299         return GTK_WIDGET (box);
300 }
301
302 static void
303 on_account_country_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
304 {
305         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
306         g_assert(self);
307         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
308         
309         priv->dirty = TRUE;
310         
311         /* Fill the providers picker, based on the selected country: */
312         if (priv->presets != NULL) {
313                 gint mcc = modest_country_picker_get_active_country_mcc (
314                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
315                 modest_provider_picker_fill (
316                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker), priv->presets, mcc);
317         }
318 }
319
320 static void
321 on_account_serviceprovider_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
322 {
323         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
324         g_assert(self);
325         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
326         
327         priv->dirty = TRUE;
328         
329         /* Fill the providers combo, based on the selected country: */
330         gchar* provider_id = modest_provider_picker_get_active_provider_id (
331                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
332         
333         gchar* domain_name = NULL;
334         if(provider_id)
335                 domain_name = modest_presets_get_domain (priv->presets, provider_id);
336         
337         if(!domain_name)
338                 domain_name = g_strdup (MODEST_EXAMPLE_EMAIL_ADDRESS);
339                 
340         if (priv->entry_user_email)
341                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), domain_name);
342                 
343         g_free (domain_name);
344         
345         g_free (provider_id);
346 }
347
348 static void
349 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
350 {
351         modest_platform_information_banner (GTK_WIDGET (self), NULL,
352                                             _CS("ckdg_ib_maximum_characters_reached"));
353 }
354
355 static GtkWidget*
356 create_page_account_details (ModestEasysetupWizardDialog *self)
357 {
358         GtkWidget *caption;
359         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
360         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
361         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
362
363         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
364         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
365         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
366         gtk_widget_show (label);
367
368         /* Create a size group to be used by all captions.
369          * Note that HildonCaption does not create a default size group if we do not specify one.
370          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
371         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
372
373         /* The country widgets: */
374         priv->account_country_picker = GTK_WIDGET (modest_country_picker_new (MODEST_EDITABLE_SIZE,
375                                                                               MODEST_EDITABLE_ARRANGEMENT));
376         hildon_button_set_title (HILDON_BUTTON (priv->account_country_picker), _("mcen_fi_country"));
377         g_signal_connect (G_OBJECT (priv->account_country_picker), "value-changed",
378                           G_CALLBACK (on_picker_button_value_changed), self);
379         gtk_box_pack_start (GTK_BOX (box), priv->account_country_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
380         gtk_widget_show (priv->account_country_picker);
381         
382         GtkWidget *separator = gtk_hseparator_new ();
383         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
384         gtk_widget_show (separator);
385             
386         /* The service provider widgets: */     
387         priv->account_serviceprovider_picker = GTK_WIDGET (modest_provider_picker_new (MODEST_EDITABLE_SIZE,
388                                                                                        MODEST_EDITABLE_ARRANGEMENT));
389         hildon_button_set_title (HILDON_BUTTON (priv->account_serviceprovider_picker), _("mcen_fi_serviceprovider"));
390         g_signal_connect (G_OBJECT (priv->account_serviceprovider_picker), "value-changed",
391                           G_CALLBACK (on_picker_button_value_changed), self);
392         gtk_box_pack_start (GTK_BOX (box), priv->account_serviceprovider_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
393         gtk_widget_show (priv->account_serviceprovider_picker);
394         
395         /* The description widgets: */  
396         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
397         g_signal_connect(G_OBJECT(priv->entry_account_title), "changed",
398                          G_CALLBACK(on_easysetup_changed), self);
399         /* Do use auto-capitalization: */
400         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_account_title), 
401                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
402         
403         /* Set a default account title, choosing one that does not already exist: */
404         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
405         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
406         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
407                 priv->account_manager, default_account_name_start);
408         g_free (default_account_name_start);
409         default_account_name_start = NULL;
410         
411         gtk_entry_set_text( GTK_ENTRY (priv->entry_account_title), default_account_name);
412         g_free (default_account_name);
413         default_account_name = NULL;
414
415         caption = create_captioned (self, sizegroup, _("mcen_fi_account_title"), 
416                                     priv->entry_account_title);
417         gtk_widget_show (priv->entry_account_title);
418         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
419         gtk_widget_show (caption);
420         
421         /* Prevent the use of some characters in the account title, 
422          * as required by our UI specification: */
423         GList *list_prevent = NULL;
424         list_prevent = g_list_append (list_prevent, "\\");
425         list_prevent = g_list_append (list_prevent, "/");
426         list_prevent = g_list_append (list_prevent, ":");
427         list_prevent = g_list_append (list_prevent, "*");
428         list_prevent = g_list_append (list_prevent, "?");
429         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
430         list_prevent = g_list_append (list_prevent, "“");
431         list_prevent = g_list_append (list_prevent, "<"); 
432         list_prevent = g_list_append (list_prevent, ">"); 
433         list_prevent = g_list_append (list_prevent, "|");
434         list_prevent = g_list_append (list_prevent, "^");       
435         modest_validating_entry_set_unallowed_characters (
436                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
437         g_list_free (list_prevent);
438         list_prevent = NULL;
439         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
440                                                                                                                                          modest_utils_on_entry_invalid_character, self);
441         
442         /* Set max length as in the UI spec:
443          * The UI spec seems to want us to show a dialog if we hit the maximum. */
444         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
445         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title), 
446                                               on_entry_max, self);
447         
448         gtk_widget_show (GTK_WIDGET (box));
449         
450         return GTK_WIDGET (box);
451 }
452
453 static GtkWidget*
454 create_page_user_details (ModestEasysetupWizardDialog *self)
455 {
456         GtkSizeGroup* sizegroup;
457         GtkWidget *box;
458         ModestEasysetupWizardDialogPrivate *priv;
459
460         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
461         
462         /* Create a size group to be used by all captions.
463          * Note that HildonCaption does not create a default size group if we do not specify one.
464          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
465         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
466         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
467          
468         /* The name widgets: (use auto cap) */
469         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
470         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name), 
471                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
472         
473         /* Set max length as in the UI spec:
474          * The UI spec seems to want us to show a dialog if we hit the maximum. */
475         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
476         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name), 
477                                               on_entry_max, self);
478         GtkWidget *caption = create_captioned (self, sizegroup, 
479                                                _("mcen_li_emailsetup_name"), priv->entry_user_name);
480         g_signal_connect(G_OBJECT(priv->entry_user_name), "changed", 
481                          G_CALLBACK(on_easysetup_changed), self);
482         gtk_widget_show (priv->entry_user_name);
483         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
484         gtk_widget_show (caption);
485         
486         /* Prevent the use of some characters in the name, 
487          * as required by our UI specification: */
488         GList *list_prevent = NULL;
489         list_prevent = g_list_append (list_prevent, "<");
490         list_prevent = g_list_append (list_prevent, ">");
491         modest_validating_entry_set_unallowed_characters (
492                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
493         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
494                 modest_utils_on_entry_invalid_character, self);
495         g_list_free (list_prevent);
496         
497         /* The username widgets: */     
498         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
499         /* Auto-capitalization is the default, so let's turn it off: */
500         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), 
501                                          HILDON_GTK_INPUT_MODE_FULL);
502         caption = create_captioned (self, sizegroup, _("mail_fi_username"), 
503                                     priv->entry_user_username);
504         gtk_widget_show (priv->entry_user_username);
505         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
506         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", 
507                          G_CALLBACK(on_easysetup_changed), self);
508         gtk_widget_show (caption);
509         
510         /* Prevent the use of some characters in the username, 
511          * as required by our UI specification: */
512         modest_validating_entry_set_unallowed_characters_whitespace (
513                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
514         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_username),
515                 modest_utils_on_entry_invalid_character, self);
516         
517         /* Set max length as in the UI spec:
518          * The UI spec seems to want us to show a dialog if we hit the maximum. */
519         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
520         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username), 
521                                               on_entry_max, self);
522         
523         /* The password widgets: */     
524         priv->entry_user_password = gtk_entry_new ();
525         /* Auto-capitalization is the default, so let's turn it off: */
526         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
527                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
528         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
529         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
530         caption = create_captioned (self, sizegroup, 
531                                     _("mail_fi_password"), priv->entry_user_password);
532         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", 
533                          G_CALLBACK(on_easysetup_changed), self);
534         gtk_widget_show (priv->entry_user_password);
535         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
536         gtk_widget_show (caption);
537         
538         /* The email address widgets: */        
539         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
540         /* Auto-capitalization is the default, so let's turn it off: */
541         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
542         caption = create_captioned (self, sizegroup, 
543                                     _("mcen_li_emailsetup_email_address"), priv->entry_user_email);
544         gtk_entry_set_text (GTK_ENTRY (priv->entry_user_email), MODEST_EXAMPLE_EMAIL_ADDRESS); /* Default text. */
545         gtk_widget_show (priv->entry_user_email);
546         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
547         g_signal_connect(G_OBJECT(priv->entry_user_email), "changed", 
548                          G_CALLBACK(on_easysetup_changed), self);
549         gtk_widget_show (caption);
550         
551         /* Set max length as in the UI spec:
552          * The UI spec seems to want us to show a dialog if we hit the maximum. */
553         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
554         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email), 
555                                               on_entry_max, self);
556         
557         
558         gtk_widget_show (GTK_WIDGET (box));
559         
560         return GTK_WIDGET (box);
561 }
562
563 static GtkWidget* 
564 create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
565 {
566         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
567         
568         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
569         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
570         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
571         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
572         /* The documentation for gtk_label_set_line_wrap() says that we must 
573          * call gtk_widget_set_size_request() with a hard-coded width, 
574          * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
575         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
576         gtk_widget_show (label);
577         
578         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
579         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
580         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
581         
582         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
583         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
584         gtk_widget_show (label);
585         
586         gtk_widget_show (GTK_WIDGET (box));
587         return GTK_WIDGET (box);
588 }
589
590 /** Change the caption title for the incoming server, 
591  * as specified in the UI spec:
592  */
593 static void 
594 update_incoming_server_title (ModestEasysetupWizardDialog *self)
595 {
596         ModestEasysetupWizardDialogPrivate* priv; 
597         ModestProtocolType protocol_type; 
598         ModestProtocolRegistry *protocol_registry;
599
600         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
601         protocol_registry = modest_runtime_get_protocol_registry ();
602         
603         protocol_type = modest_servertype_picker_get_active_servertype (
604                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
605
606         /* This could happen when the combo box has still no active iter */
607         if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
608                 gchar* incomingserver_title;
609                 const gchar *protocol_display_name; 
610
611                 protocol_display_name = modest_protocol_get_display_name (modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type));
612                 
613                 incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), protocol_display_name);
614                 g_object_set (G_OBJECT (priv->caption_incoming), "label", incomingserver_title, NULL);
615                 g_free(incomingserver_title);
616         }
617 }
618
619 /** Change the caption title for the incoming server, 
620  * as specified in the UI spec:
621  */
622 static void 
623 update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
624 {
625         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
626         ModestServertypePicker *server_type_picker;
627         ModestProtocolType protocol_type;
628         ModestSecurityOptionsView *view;
629
630         server_type_picker = 
631                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker);
632         protocol_type = 
633                 modest_servertype_picker_get_active_servertype (server_type_picker);
634         
635         /* Fill the combo with appropriately titled choices for all
636            those protocols */
637         view = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
638         modest_security_options_view_set_server_type (view, protocol_type);
639 }
640
641 static void 
642 on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data)
643 {
644         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
645         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
646         ModestServertypePicker *picker;
647         ModestProtocolType protocol_type;
648
649         priv->dirty = TRUE;
650         
651         /* Update title */
652         update_incoming_server_title (self);
653
654         /* Update security options if needed */
655         picker = MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker);
656         protocol_type = modest_servertype_picker_get_active_servertype (picker);
657         update_incoming_server_security_choices (self);
658         gtk_widget_show (priv->incoming_security);
659
660         set_default_custom_servernames (self);
661 }
662
663 static void 
664 on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
665 {
666         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
667         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
668         priv->dirty = TRUE;
669         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
670 }
671
672 static GtkWidget* 
673 create_page_custom_incoming (ModestEasysetupWizardDialog *self)
674 {
675         ModestProtocolRegistry *protocol_registry;
676         ModestEasysetupWizardDialogPrivate* priv; 
677         GtkWidget *box; 
678         GtkWidget *scrolled_window;
679         GtkWidget *label;
680         GtkSizeGroup *sizegroup;
681
682         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
683         protocol_registry = modest_runtime_get_protocol_registry ();
684
685         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
686         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
687
688         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
689                                         GTK_POLICY_NEVER,
690                                         GTK_POLICY_AUTOMATIC);
691
692         /* Show note that account type cannot be changed in future: */
693         label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
694         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
695         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
696         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
697         gtk_widget_show (label);
698         
699         /* Create a size group to be used by all captions.
700          * Note that HildonCaption does not create a default size group if we do not specify one.
701          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
702         sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
703          
704         /* The incoming server widgets: */
705         priv->incoming_servertype_picker = GTK_WIDGET (modest_servertype_picker_new (MODEST_EDITABLE_SIZE,
706                                                                                      MODEST_EDITABLE_ARRANGEMENT,
707                                                                                      TRUE));
708         hildon_button_set_title (HILDON_BUTTON (priv->incoming_servertype_picker), _("mcen_li_emailsetup_type"));
709         g_signal_connect (G_OBJECT (priv->incoming_servertype_picker), "value-changed",
710                           G_CALLBACK (on_picker_button_value_changed), self);
711         gtk_box_pack_start (GTK_BOX (box), priv->incoming_servertype_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
712         gtk_widget_show (priv->incoming_servertype_picker);
713         
714         priv->entry_incomingserver = gtk_entry_new ();
715         g_signal_connect(G_OBJECT(priv->entry_incomingserver), "changed", G_CALLBACK(on_easysetup_changed), self);
716         /* Auto-capitalization is the default, so let's turn it off: */
717         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
718         set_default_custom_servernames (self);
719
720         /* The caption title will be updated in update_incoming_server_title().
721          * so this default text will never be seen: */
722         /* (Note: Changing the title seems pointless. murrayc) */
723         priv->caption_incoming = create_captioned (self, sizegroup, 
724                                                    "Incoming Server", 
725                                                    priv->entry_incomingserver);
726         update_incoming_server_title (self);
727         gtk_widget_show (priv->entry_incomingserver);
728         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
729         gtk_widget_show (priv->caption_incoming);
730         
731         /* Change the caption title when the servertype changes, 
732          * as in the UI spec: */
733         g_signal_connect (G_OBJECT (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (priv->incoming_servertype_picker))), 
734                           "changed",
735                           G_CALLBACK (on_servertype_selector_changed), self);
736
737         /* Remember when the servername was changed manually: */
738         g_signal_connect (G_OBJECT (priv->entry_incomingserver), "changed",
739                           G_CALLBACK (on_entry_incoming_servername_changed), self);
740
741         /* The secure connection widgets. These are only valid for
742            protocols with security */   
743         priv->incoming_security = 
744                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
745                                                         FALSE, sizegroup);
746         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security, 
747                             FALSE, FALSE, MODEST_MARGIN_HALF);
748
749         /* Set default selection */
750         modest_servertype_picker_set_active_servertype (
751                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker), 
752                 MODEST_PROTOCOLS_STORE_POP);
753
754         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), box);
755         gtk_container_set_focus_vadjustment (GTK_CONTAINER (box),
756                                              gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrolled_window)));
757         gtk_widget_show (GTK_WIDGET (box));
758         gtk_widget_show (scrolled_window);
759
760         return GTK_WIDGET (scrolled_window);
761 }
762
763 static void
764 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
765 {
766         GtkWidget *widget = GTK_WIDGET (user_data);
767         
768         /* Enable the widget only if the toggle button is active: */
769         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
770         gtk_widget_set_sensitive (widget, enable);
771 }
772
773 /* Make the sensitivity of a widget depend on a toggle button.
774  */
775 static void
776 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
777 {
778         g_signal_connect (G_OBJECT (button), "toggled",
779                           G_CALLBACK (on_toggle_button_changed), widget);
780         
781         /* Set the starting sensitivity: */
782         on_toggle_button_changed (button, widget);
783 }
784
785 static void
786 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
787 {
788         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
789         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
790         GtkWidget *specific_window;
791         
792         /* We set dirty here because setting it depending on the connection specific dialog
793         seems overkill */
794         priv->dirty = TRUE;
795         
796         /* Create the window, if necessary: */
797         specific_window = (GtkWidget *) modest_connection_specific_smtp_window_new ();
798         modest_connection_specific_smtp_window_fill_with_connections (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), priv->account_manager);
799
800         /* Show the window */
801         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (specific_window), GTK_WINDOW (self));
802         gtk_widget_show (specific_window);
803 }
804
805 static void 
806 on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
807 {
808         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
809         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
810         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
811 }
812
813 static GtkWidget* 
814 create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
815 {
816         ModestEasysetupWizardDialogPrivate *priv;
817         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
818         
819         /* Create a size group to be used by all captions.
820          * Note that HildonCaption does not create a default size group if we do not specify one.
821          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
822         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
823          
824         /* The outgoing server widgets: */
825         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
826         priv->entry_outgoingserver = gtk_entry_new ();
827         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
828                   G_CALLBACK (on_easysetup_changed), self);
829         /* Auto-capitalization is the default, so let's turn it off: */
830         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
831         GtkWidget *caption = create_captioned (self, sizegroup, 
832                                                _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver);
833         gtk_widget_show (priv->entry_outgoingserver);
834         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
835         gtk_widget_show (caption);
836         set_default_custom_servernames (self);
837
838         /* The secure connection widgets. These are only valid for
839            protocols with security */   
840         priv->outgoing_security = 
841                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
842                                                         FALSE, sizegroup);
843         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security, 
844                             FALSE, FALSE, MODEST_MARGIN_HALF);
845         gtk_widget_show (priv->outgoing_security);
846         
847         GtkWidget *separator = gtk_hseparator_new ();
848         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
849         gtk_widget_show (separator);
850         
851         /* connection-specific checkbox: */
852         priv->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
853         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific), 
854                                       FALSE);
855         g_signal_connect (G_OBJECT (priv->checkbox_outgoing_smtp_specific), "toggled",
856                   G_CALLBACK (on_easysetup_changed), self);
857
858         caption = modest_maemo_utils_create_captioned (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
859                                                        priv->checkbox_outgoing_smtp_specific);
860         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
861         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
862         gtk_widget_show (caption);
863         
864         /* Connection-specific SMTP-Severs Edit button: */
865         priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
866         caption = modest_maemo_utils_create_captioned (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
867                                                        priv->button_outgoing_smtp_servers);
868         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
869         gtk_widget_show (priv->button_outgoing_smtp_servers);
870         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
871         gtk_widget_show (caption);
872         
873         /* Only enable the button when the checkbox is checked: */
874         enable_widget_for_togglebutton (priv->button_outgoing_smtp_servers, 
875                                         GTK_TOGGLE_BUTTON (priv->checkbox_outgoing_smtp_specific));
876                 
877         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
878                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
879
880         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
881                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
882         
883         
884         gtk_widget_show (GTK_WIDGET (box));
885         
886         return GTK_WIDGET (box);
887 }
888
889 static gboolean
890 show_advanced_edit(gpointer user_data)
891 {
892         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
893         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
894         gint response;
895         
896         /* Show the Account Settings window: */
897         ModestAccountSettingsDialog *dialog = modest_default_account_settings_dialog_new ();
898         if (priv->pending_load_settings) {
899                 save_to_settings (self);
900                 priv->pending_load_settings = FALSE;
901         }
902         modest_account_settings_dialog_load_settings (dialog, priv->settings);
903         
904         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog), GTK_WINDOW (self));
905         
906         response = gtk_dialog_run (GTK_DIALOG (dialog));
907
908         gtk_widget_destroy (GTK_WIDGET (dialog));
909         
910         return FALSE; /* Do not call this timeout callback again. */
911 }
912
913 static void
914 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
915 {
916         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
917         
918         /* Show the Account Settings window: */
919         show_advanced_edit(self);
920 }
921 static GtkWidget* 
922 create_page_complete_custom (ModestEasysetupWizardDialog *self)
923 {
924         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
925         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
926         GtkWidget *button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
927         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
928         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
929         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
930         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
931         gtk_widget_show (label);
932         
933         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
934         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
935         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
936         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
937         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
938         gtk_widget_show (label);
939         
940         GtkWidget *caption = modest_maemo_utils_create_captioned (NULL, _("mcen_fi_advanced_settings"), 
941                                                                   button_edit);
942         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
943         gtk_widget_show (button_edit);
944         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
945         gtk_widget_show (caption);
946         
947         g_signal_connect (G_OBJECT (button_edit), "clicked", 
948                           G_CALLBACK (on_button_edit_advanced_settings), self);
949         
950         gtk_widget_show (GTK_WIDGET (box));
951         return GTK_WIDGET (box);
952 }
953
954
955 /*
956  */
957 static void 
958 on_response (ModestWizardDialog *wizard_dialog,
959              gint response_id,
960              gpointer user_data)
961 {
962         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
963
964         invoke_enable_buttons_vfunc (self);
965 }
966
967 static void 
968 on_response_before (ModestWizardDialog *wizard_dialog,
969                     gint response_id,
970                     gpointer user_data)
971 {
972         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
973         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
974         if (response_id == GTK_RESPONSE_CANCEL) {
975                 /* This is mostly copied from
976                  * src/maemo/modest-account-settings-dialog.c */
977                 if (priv->dirty) {
978                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
979                                 _("imum_nc_wizard_confirm_lose_changes")));
980                         /* TODO: These button names will be ambiguous, and not
981                          * specified in the UI specification. */
982
983                         const gint dialog_response = gtk_dialog_run (dialog);
984                         gtk_widget_destroy (GTK_WIDGET (dialog));
985
986                         if (dialog_response != GTK_RESPONSE_OK) {
987                                 /* Don't let the dialog close */
988                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
989                         }
990                 }
991         }
992 }
993
994 typedef struct IdleData {
995         ModestEasysetupWizardDialog *dialog;
996         ModestPresets *presets;
997 } IdleData;
998
999 static gboolean
1000 presets_idle (gpointer userdata)
1001 {
1002         IdleData *idle_data = (IdleData *) userdata;
1003         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (idle_data->dialog);
1004         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1005
1006         g_assert (idle_data->presets);
1007
1008         gdk_threads_enter ();
1009
1010         priv->presets = idle_data->presets;
1011
1012         if (MODEST_IS_COUNTRY_PICKER (priv->account_country_picker)) {
1013 /*              gint mcc = get_default_country_code(); */
1014                 gint mcc;
1015                 /* Fill the combo in an idle call, as it takes a lot of time */
1016                 modest_country_picker_load_data(
1017                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1018                 /* connect to country picker's changed signal, so we can fill the provider picker: */
1019                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector 
1020                                             (HILDON_PICKER_BUTTON (priv->account_country_picker))),
1021                                   "changed",
1022                                   G_CALLBACK (on_account_country_selector_changed), self);
1023             
1024                 modest_country_picker_set_active_country_locale (
1025                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1026                 mcc = modest_country_picker_get_active_country_mcc (
1027                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1028                 modest_provider_picker_fill (
1029                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker),
1030                         priv->presets, mcc);
1031                 /* connect to providers picker's changed signal, so we can fill the email address: */
1032                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector 
1033                                             (HILDON_PICKER_BUTTON (priv->account_serviceprovider_picker))),
1034                                   "changed",
1035                                   G_CALLBACK (on_account_serviceprovider_selector_changed), self);
1036                 
1037                 modest_provider_picker_set_others_provider (MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
1038         }
1039
1040         priv->dirty = FALSE;
1041
1042         g_object_unref (idle_data->dialog);
1043         g_free (idle_data);
1044
1045         gdk_threads_leave ();
1046
1047         return FALSE;
1048 }
1049
1050 static gpointer
1051 presets_loader (gpointer userdata)
1052 {
1053         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (userdata);
1054         ModestPresets *presets = NULL;
1055         IdleData *idle_data;
1056
1057         const gchar* path  = NULL;
1058         const gchar* path1 = MODEST_PROVIDER_DATA_FILE;
1059         const gchar* path2 = MODEST_MAEMO_PROVIDER_DATA_FILE;
1060         
1061         if (access(path1, R_OK) == 0) 
1062                 path = path1;
1063         else if (access(path2, R_OK) == 0)
1064                 path = path2;
1065         else {
1066                 g_warning ("%s: neither '%s' nor '%s' is a readable provider data file",
1067                            __FUNCTION__, path1, path2);
1068                 return NULL;
1069         }
1070
1071         presets = modest_presets_new (path);
1072         if (!presets) {
1073                 g_warning ("%s: failed to parse '%s'", __FUNCTION__, path);
1074                 return NULL;
1075         }
1076         
1077         idle_data = g_new0 (IdleData, 1);
1078         idle_data->dialog = self;
1079         idle_data->presets = presets;
1080         
1081         g_idle_add (presets_idle, idle_data);   
1082
1083         return NULL;
1084 }
1085
1086 static void
1087 modest_easysetup_wizard_dialog_append_page (GtkNotebook *notebook,
1088                                             GtkWidget *page,
1089                                             const gchar *label)
1090 {
1091         gint index;
1092         /* Append page and set attributes */
1093         index = gtk_notebook_append_page (notebook, page, gtk_label_new (label));
1094         gtk_container_child_set (GTK_CONTAINER (notebook), page,
1095                                  "tab-expand", TRUE, "tab-fill", TRUE,
1096                                  NULL);
1097         gtk_widget_show (page);
1098 }
1099
1100 static void
1101 init_user_page (ModestEasysetupWizardDialogPrivate *priv)
1102 {
1103         priv->page_user_details = NULL;
1104         priv->entry_user_name = NULL;
1105         priv->entry_user_username = NULL;
1106         priv->entry_user_password = NULL;
1107         priv->entry_user_email = NULL;
1108 }
1109
1110 static void
1111 init_incoming_page (ModestEasysetupWizardDialogPrivate *priv)
1112 {
1113         priv->page_custom_incoming = NULL;
1114         priv->incoming_servertype_picker = NULL;
1115         priv->caption_incoming = NULL;
1116         priv->entry_incomingserver = NULL;
1117         priv->entry_user_email = NULL;
1118         priv->incoming_security = NULL;
1119 }
1120
1121 static void
1122 init_outgoing_page (ModestEasysetupWizardDialogPrivate *priv)
1123 {
1124         priv->page_custom_outgoing = NULL;
1125         priv->entry_outgoingserver = NULL;
1126         priv->checkbox_outgoing_smtp_specific = NULL;
1127         priv->button_outgoing_smtp_servers = NULL;
1128         priv->outgoing_security = NULL;
1129 }
1130
1131 static void
1132 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1133 {
1134         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1135         
1136         /* Create the notebook to be used by the ModestWizardDialog base class:
1137          * Each page of the notebook will be a page of the wizard: */
1138         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1139         
1140         /* Set the notebook used by the ModestWizardDialog base class: */
1141         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1142     
1143         /* Set the wizard title:
1144          * The actual window title will be a combination of this and the page's tab label title. */
1145         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1146
1147         /* Read in the information about known service providers: */
1148         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1149         
1150         /* The server fields did not have been manually changed yet */
1151         priv->server_changes = 0;
1152         priv->pending_load_settings = TRUE;
1153
1154         /* Get the account manager object, 
1155          * so we can check for existing accounts,
1156          * and create new accounts: */
1157         priv->account_manager = modest_runtime_get_account_mgr ();
1158         g_object_ref (priv->account_manager);
1159         
1160         /* Initialize fields */
1161         priv->page_welcome = create_page_welcome (self);
1162         priv->page_account_details = create_page_account_details (self);
1163
1164         init_user_page (priv);
1165         init_incoming_page (priv);
1166         init_outgoing_page (priv);
1167
1168         priv->page_complete_easysetup = NULL;       
1169         priv->page_complete_customsetup = NULL;
1170         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1171         priv->missing_data_signals = NULL;
1172
1173         /* Add the common pages */
1174         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_welcome, 
1175                                                     _("mcen_ti_emailsetup_welcome"));
1176         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_account_details, 
1177                                                     _("mcen_ti_accountdetails"));
1178                 
1179         /* Connect to the dialog's response signal so we can enable/disable buttons 
1180          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1181          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1182          * so that we can be enable/disable after ModestWizardDialog has done its own 
1183          * enabling/disabling of buttons.
1184          * 
1185          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1186          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1187          * 
1188          * It's not enough to connect to the notebook's switch-page signal, because 
1189          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1190          * _after_ switching the page (understandably).
1191          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1192          * then gtk_notebook_get_current_page() would return an incorrect value.)
1193          */
1194         g_signal_connect_after (G_OBJECT (self), "response",
1195                                 G_CALLBACK (on_response), self);
1196
1197         /* This is to show a confirmation dialog when the user hits cancel */
1198         g_signal_connect (G_OBJECT (self), "response",
1199                           G_CALLBACK (on_response_before), self);
1200
1201         g_signal_connect (G_OBJECT (self), "delete-event",
1202                           G_CALLBACK (on_delete_event), self);
1203
1204         /* Reset dirty, because there was no user input until now */
1205         priv->dirty = FALSE;
1206         
1207         /* When this window is shown, hibernation should not be possible, 
1208          * because there is no sensible way to save the state: */
1209         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1210                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1211
1212         /* Load provider presets */
1213         g_object_ref (self);
1214         g_thread_create (presets_loader, self, FALSE, NULL);
1215
1216         hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_wizardwelcome",
1217                                         modest_maemo_utils_get_osso_context()); 
1218
1219         priv->settings = modest_account_settings_new ();
1220 }
1221
1222 ModestEasysetupWizardDialog*
1223 modest_easysetup_wizard_dialog_new (void)
1224 {       
1225         
1226         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1227 }
1228
1229 static void 
1230 create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1231 {
1232         ModestEasysetupWizardDialogPrivate *priv;
1233         GtkNotebook *notebook = NULL;
1234
1235         g_object_get (self, "wizard-notebook", &notebook, NULL);
1236         g_assert(notebook);
1237
1238         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1239
1240         if (!priv->page_user_details) {
1241                 priv->page_user_details = create_page_user_details (self);
1242         }                       
1243
1244         /* Create the custom pages: */
1245         if(!(priv->page_custom_incoming)) {
1246                 priv->page_custom_incoming = create_page_custom_incoming (self);
1247         }
1248                 
1249         /* TODO: only if needed */
1250         if(!(priv->page_custom_outgoing)) {
1251                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1252         }
1253         
1254         if(!(priv->page_complete_customsetup)) {
1255                 priv->page_complete_customsetup = create_page_complete_custom (self);
1256         }
1257
1258         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_user_details)))
1259                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_user_details,
1260                                                             _("mcen_ti_emailsetup_userdetails"));
1261
1262         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1263                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_incoming, 
1264                                                             _("mcen_ti_emailsetup_incomingdetails"));
1265         
1266         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))           
1267                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_outgoing, 
1268                                                             _("mcen_ti_emailsetup_outgoingdetails"));
1269                 
1270         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup)))
1271                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_customsetup, 
1272                                                             _("mcen_ti_emailsetup_complete"));
1273                         
1274         /* This is unnecessary with GTK+ 2.10: */
1275         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1276 }
1277         
1278 static void 
1279 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1280 {
1281         ModestEasysetupWizardDialogPrivate *priv;
1282         GtkNotebook *notebook = NULL;
1283
1284         g_object_get (self, "wizard-notebook", &notebook, NULL);
1285         g_assert(notebook);
1286         
1287         /* Create the easysetup-specific pages: */
1288         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1289         if(!priv->page_complete_easysetup)
1290                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1291
1292         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup)))
1293                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_easysetup, 
1294                                                             _("mcen_ti_emailsetup_complete"));
1295                         
1296         /* This is unnecessary with GTK+ 2.10: */
1297         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1298 }
1299
1300 /* */
1301 static void
1302 remove_non_common_tabs (GtkNotebook *notebook, 
1303                         gboolean remove_user_details) 
1304 {
1305         gint starting_tab;
1306         /* The first 2 tabs are the common ones (welcome tab and the
1307            providers tab), so we always remove starting from the
1308            end */
1309
1310         starting_tab = (remove_user_details) ? 2 : 3;
1311         while (gtk_notebook_get_n_pages (notebook) > starting_tab) 
1312                 gtk_notebook_remove_page (notebook, -1); 
1313 }
1314
1315 static void
1316 on_missing_mandatory_data (ModestAccountProtocol *protocol,
1317                            gboolean missing,
1318                            gpointer user_data)
1319 {
1320         real_enable_buttons (MODEST_WIZARD_DIALOG (user_data), !missing);
1321 }
1322
1323 /* After the user details page,
1324  * the following pages depend on whether "Other" was chosen 
1325  * in the provider combobox on the account page
1326  */
1327 static void 
1328 create_subsequent_pages (ModestEasysetupWizardDialog *self)
1329 {
1330         ModestEasysetupWizardDialogPrivate *priv;
1331         ModestProviderPicker *picker;
1332         ModestProviderPickerIdType id_type;
1333         GtkNotebook *notebook;
1334
1335         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1336         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1337         id_type = modest_provider_picker_get_active_id_type (picker);
1338         g_object_get (self, "wizard-notebook", &notebook, NULL);
1339
1340         if (id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
1341                 /* "Other..." was selected: */
1342
1343                 /* If we come from a rollbacked easysetup */
1344                 if (priv->page_complete_easysetup) {
1345                         remove_non_common_tabs (notebook, FALSE);
1346                         priv->page_complete_easysetup = NULL;
1347                 } 
1348                 
1349                 /* If we come from a rollbacked plugin protocol setup */
1350                 if (priv->last_plugin_protocol_selected != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1351                         remove_non_common_tabs (notebook, TRUE);
1352                         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1353                         modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1354                         priv->missing_data_signals = NULL;
1355                 }
1356
1357                 create_subsequent_customsetup_pages (self);
1358         } else {
1359                 /* If we come from a rollbacked custom setup */
1360                 if (priv->page_custom_incoming) {
1361                         remove_non_common_tabs (notebook, TRUE);
1362                         init_user_page (priv);
1363                         init_incoming_page (priv);
1364                         init_outgoing_page (priv);
1365                         init_user_page (priv);
1366                         priv->page_complete_customsetup = NULL;
1367                 }
1368
1369                 /* It's a pluggable protocol and not a provider with presets */
1370                 if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1371                         ModestProtocol *protocol;
1372                         gchar *proto_name;
1373                         ModestProtocolType proto_type;
1374
1375                         
1376                         /* If we come from a rollbacked easy setup */
1377                         if (priv->last_plugin_protocol_selected == 
1378                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID &&
1379                             priv->page_complete_easysetup) {
1380                                 remove_non_common_tabs (notebook, TRUE);
1381                                 init_user_page (priv);
1382                                 priv->page_complete_easysetup = NULL;
1383                         }
1384                         
1385                         proto_name = modest_provider_picker_get_active_provider_id (picker);
1386                         protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
1387                                                                                   MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1388                                                                                   proto_name);
1389                         proto_type = modest_protocol_get_type_id (protocol);
1390
1391                         if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol) &&
1392                             proto_type != priv->last_plugin_protocol_selected) {
1393                                 ModestPairList *tabs;
1394                                 GSList *tmp;
1395
1396                                 /* Remember the last selected plugin protocol */
1397                                 priv->last_plugin_protocol_selected = proto_type;
1398
1399                                 /* Get tabs */
1400                                 tabs = modest_account_protocol_get_easysetupwizard_tabs (MODEST_ACCOUNT_PROTOCOL (protocol));
1401                                 tmp = (GSList *) tabs;
1402                                 while (tmp) {
1403                                         ModestPair *pair = (ModestPair *) tmp->data;
1404                                         modest_easysetup_wizard_dialog_append_page (notebook,
1405                                                                                     GTK_WIDGET (pair->second),
1406                                                                                     (const gchar *) pair->first);
1407
1408                                         /* Connect signals */
1409                                         priv->missing_data_signals = 
1410                                                 modest_signal_mgr_connect (priv->missing_data_signals, 
1411                                                                            G_OBJECT (pair->second), 
1412                                                                            "missing-mandatory-data",
1413                                                                            G_CALLBACK (on_missing_mandatory_data), 
1414                                                                            self);
1415
1416                                         g_free (pair->first);
1417                                         tmp = g_slist_next (tmp);
1418                                         /* Critical: if you don't show the page then the dialog will ignore it */
1419 /*                                      gtk_widget_show (GTK_WIDGET (pair->second)); */
1420                                 }
1421                                 modest_pair_list_free (tabs);
1422                         }
1423                         g_free (proto_name);
1424                 } else {
1425                         if (priv->last_plugin_protocol_selected != 
1426                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1427                                 remove_non_common_tabs (notebook, TRUE);
1428                                 init_user_page (priv);
1429                                 priv->page_complete_easysetup = NULL;
1430                                 priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1431                                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1432                                 priv->missing_data_signals = NULL;
1433                         }
1434                         if (!priv->page_user_details) {
1435                                 priv->page_user_details = create_page_user_details (self);
1436                                 modest_easysetup_wizard_dialog_append_page (notebook, 
1437                                                                             priv->page_user_details,
1438                                                                             _("mcen_ti_emailsetup_userdetails"));
1439                         }
1440                 }
1441                 
1442                 /* Create the easysetup pages: */
1443                 create_subsequent_easysetup_pages (self);
1444         }
1445 }
1446
1447
1448 static gchar*
1449 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocolType protocol_type)
1450 {
1451         const gchar* hostname = NULL;
1452         gchar* at;
1453         gchar* domain;
1454         ModestProtocolRegistry *protocol_registry;
1455         ModestProtocol *protocol;
1456
1457         if (!email_address)
1458                 return NULL;
1459         
1460         at = g_utf8_strchr (email_address, -1, '@');
1461         if (!at || (g_utf8_strlen (at, -1) < 2))
1462                 return NULL;
1463                 
1464         domain = g_utf8_next_char (at);
1465         if(!domain)
1466                 return NULL;
1467
1468         protocol_registry = modest_runtime_get_protocol_registry ();
1469         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
1470                 
1471         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS) ||
1472             modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
1473                 hostname = modest_protocol_get_name (protocol);
1474         }
1475         
1476         if (!hostname)
1477                 return NULL;
1478                 
1479         return g_strdup_printf ("%s.%s", hostname, domain);
1480 }
1481
1482 static void 
1483 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1484 {
1485         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1486
1487         if (!priv->entry_incomingserver)
1488                 return;
1489                 
1490         /* Set a default domain for the server, based on the email address,
1491          * if no server name was already specified.
1492          */
1493         if (priv->entry_user_email
1494             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1495                 const ModestProtocolType protocol_type = modest_servertype_picker_get_active_servertype (
1496                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
1497
1498                 /* This could happen when the combo box has still no active iter */
1499                 if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1500                         const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));    
1501                         gchar* servername = util_get_default_servername_from_email_address (email_address, 
1502                                                                                             protocol_type);
1503
1504                         /* Do not set the INCOMING_CHANGED flag because of this edit */
1505                         g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1506                         gtk_entry_set_text (GTK_ENTRY (priv->entry_incomingserver), servername);
1507                         g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1508                         
1509                         g_free (servername);
1510                 }
1511         }
1512         
1513         /* Set a default domain for the server, based on the email address,
1514          * if no server name was already specified.
1515          */
1516         if (!priv->entry_outgoingserver)
1517                 return;
1518                 
1519         if (priv->entry_user_email
1520             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1521                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));
1522                 
1523                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOLS_TRANSPORT_SMTP);
1524
1525                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1526                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1527                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), servername);
1528                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1529
1530                 g_free (servername);
1531         }
1532 }
1533
1534 static gchar*
1535 get_entered_account_title (ModestEasysetupWizardDialog *self)
1536 {
1537         ModestEasysetupWizardDialogPrivate *priv;
1538         const gchar* account_title;
1539
1540         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1541         account_title = gtk_entry_get_text (GTK_ENTRY (priv->entry_account_title));
1542
1543         if (!account_title || (g_utf8_strlen (account_title, -1) == 0)) {
1544                 return NULL;
1545         } else {
1546                 /* Strip it of whitespace at the start and end: */
1547                 gchar *result = g_strdup (account_title);
1548                 result = g_strstrip (result);
1549                 
1550                 if (!result)
1551                         return NULL;
1552                         
1553                 if (g_utf8_strlen (result, -1) == 0) {
1554                         g_free (result);
1555                         return NULL;    
1556                 }
1557                 
1558                 return result;
1559         }
1560 }
1561
1562 static gboolean
1563 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1564 {
1565         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1566         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1567         ModestProtocolRegistry *protocol_registry;
1568
1569         protocol_registry = modest_runtime_get_protocol_registry ();
1570
1571         /* if are browsing pages previous to the last one, then we have pending settings in
1572          * this wizard */
1573         if (next_page != NULL)
1574                 priv->pending_load_settings = TRUE;
1575         
1576         /* Do extra validation that couldn't be done for every key press,
1577          * either because it was too slow,
1578          * or because it requires interaction:
1579          */
1580         if (current_page == priv->page_account_details) {       
1581                 /* Check that the title is not already in use: */
1582                 gchar* account_title = get_entered_account_title (self);
1583                 if (!account_title)
1584                         return FALSE;
1585                         
1586                 /* Aavoid a clash with an existing display name: */
1587                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1588                         priv->account_manager, account_title);
1589                 g_free (account_title);
1590
1591                 if (name_in_use) {
1592                         /* Warn the user via a dialog: */
1593                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1594             
1595                         return FALSE;
1596                 }
1597
1598                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1599                 create_subsequent_pages (self);
1600
1601         } else if (current_page == priv->page_user_details) {
1602                 /* Check that the email address is valud: */
1603                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1604                 if ((!email_address) || (g_utf8_strlen (email_address, -1) == 0))
1605                         return FALSE;
1606                         
1607                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1608                         /* Warn the user via a dialog: */
1609                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1610                                              
1611                         /* Return focus to the email address entry: */
1612                         gtk_widget_grab_focus (priv->entry_user_email);
1613                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1614
1615                         return FALSE;
1616                 }
1617         }
1618           
1619         if (next_page == priv->page_custom_incoming) {
1620                 set_default_custom_servernames (self);
1621         } else if (next_page == priv->page_custom_outgoing) {
1622                 set_default_custom_servernames (self);
1623
1624                 /* Check if the server supports secure authentication */
1625 /*              if (modest_security_options_view_auth_check (security_options)) */
1626 /*                      if (!check_has_supported_auth_methods (self)) */
1627 /*                              return FALSE; */
1628                 gtk_widget_show (priv->outgoing_security);
1629         }
1630         
1631         /* If this is the last page, and this is a click on Finish, 
1632          * then attempt to create the dialog.
1633          */
1634         if(!next_page && 
1635            current_page != priv->page_account_details) /* This is NULL when this is a click on Finish. */
1636         {
1637                 if (priv->pending_load_settings) {
1638                         save_to_settings (self);
1639                 }
1640
1641                 /* We check if there's already another account with the same configuration */
1642                 if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
1643                         modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1644                         return FALSE;
1645                 }
1646
1647                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1648         }
1649         
1650         
1651         return TRUE;
1652 }
1653
1654 static gboolean entry_is_empty (GtkWidget *entry)
1655 {
1656         if (!entry)
1657                 return FALSE;
1658                 
1659         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1660         if ((!text) || (g_utf8_strlen (text, -1) == 0))
1661                 return TRUE;
1662         else {
1663                 /* Strip it of whitespace at the start and end: */
1664                 gchar *stripped = g_strdup (text);
1665                 stripped = g_strstrip (stripped);
1666                 
1667                 if (!stripped)
1668                         return TRUE;
1669                         
1670                 const gboolean result = (g_utf8_strlen (stripped, -1) == 0);
1671                 
1672                 g_free (stripped);
1673                 return result;
1674         }
1675 }
1676
1677 static void
1678 real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next)
1679 {               
1680         GtkNotebook *notebook = NULL;
1681         gboolean is_finish_tab;
1682         GtkWidget *current;
1683         ModestEasysetupWizardDialogPrivate *priv;
1684
1685         /* Get data */
1686         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (dialog);
1687         g_object_get (dialog, "wizard-notebook", &notebook, NULL);
1688         
1689         /* Disable the Finish button until we are on the last page,
1690          * because HildonWizardDialog enables this for all but the
1691          * first page */
1692         current = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
1693         is_finish_tab = ((current == priv->page_complete_easysetup) ||
1694                          (current == priv->page_complete_customsetup));
1695
1696         if (is_finish_tab) {
1697                 /* Disable Next on the last page no matter what the
1698                    argument say */
1699                 enable_next = FALSE;
1700         } else {
1701                 /* Not the last one */
1702                 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1703                                                    MODEST_WIZARD_DIALOG_FINISH,
1704                                                    FALSE);
1705         }
1706         
1707         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
1708                                            MODEST_WIZARD_DIALOG_NEXT,
1709                                            enable_next);
1710 }
1711
1712 static void
1713 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1714 {
1715         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1716         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1717         
1718         gboolean enable_next = TRUE;
1719         if (current_page == priv->page_welcome) {
1720                 enable_next = TRUE;
1721         } else if (current_page == priv->page_account_details) {
1722                 /* The account details title is mandatory: */
1723                 if (entry_is_empty(priv->entry_account_title))
1724                         enable_next = FALSE;
1725         } else if (current_page == priv->page_user_details) {   
1726                 /* The user details username is mandatory: */
1727                 if (entry_is_empty(priv->entry_user_username))
1728                         enable_next = FALSE;
1729                         
1730                 /* The user details email address is mandatory: */
1731                 if (enable_next && entry_is_empty (priv->entry_user_email))
1732                         enable_next = FALSE;
1733         } else if (current_page == priv->page_custom_incoming) {
1734                 /* The custom incoming server is mandatory: */
1735                 if (entry_is_empty(priv->entry_incomingserver))
1736                         enable_next = FALSE;
1737         } else if (MODEST_IS_EASYSETUP_WIZARD_PAGE (current_page)) {
1738                 enable_next = !modest_easysetup_wizard_page_validate (
1739                        MODEST_EASYSETUP_WIZARD_PAGE (current_page));
1740         }
1741                         
1742         /* Enable/disable buttons */ 
1743         real_enable_buttons (dialog, enable_next);
1744 }
1745
1746 static void
1747 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1748 {
1749         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1750         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1751
1752
1753         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1754         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1755         
1756         /* Provide a vfunc implementation so we can decide 
1757          * when to enable/disable the prev/next buttons.
1758          */
1759         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1760         base_klass->before_next = on_before_next;
1761         base_klass->enable_buttons = on_enable_buttons;
1762 }
1763
1764 /**
1765  * save_to_settings:
1766  * @self: a #ModestEasysetupWizardDialog
1767  *
1768  * takes information from all the wizard and stores it in settings
1769  */
1770 static void
1771 save_to_settings (ModestEasysetupWizardDialog *self)
1772 {
1773         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1774         guint special_port;
1775         gchar *provider_id = NULL;
1776         gchar* display_name;
1777         const gchar *username, *password;
1778         gchar *store_hostname, *transport_hostname;
1779         guint store_port, transport_port;
1780         ModestProtocolRegistry *protocol_registry;
1781         ModestProtocolType store_protocol, transport_protocol;
1782         ModestProtocolType store_security, transport_security;
1783         ModestProtocolType store_auth_protocol, transport_auth_protocol;
1784         ModestServerAccountSettings *store_settings, *transport_settings;
1785         const gchar *fullname, *email_address;
1786         ModestProviderPicker *picker;
1787         ModestProviderPickerIdType id_type;
1788
1789         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1790         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1791         protocol_registry = modest_runtime_get_protocol_registry ();
1792
1793         /* Get details from the specified presets: */
1794         id_type = modest_provider_picker_get_active_id_type (picker);
1795         provider_id = modest_provider_picker_get_active_provider_id (picker);
1796                 
1797         /* Let the plugin save the settings. We do a return in order
1798            to save an indentation level */
1799         if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1800                 ModestProtocol *protocol;
1801
1802                 protocol = modest_protocol_registry_get_protocol_by_name (
1803                        modest_runtime_get_protocol_registry (),
1804                        MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1805                        provider_id);
1806
1807                 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
1808                         gint n_pages, i = 0;
1809                         GtkNotebook *notebook;
1810                         GList *wizard_pages = NULL;
1811
1812                         g_object_get (self, "wizard-notebook", &notebook, NULL);
1813                         n_pages = gtk_notebook_get_n_pages (notebook);
1814                         for (i = 0; i < n_pages; i++) {
1815                                 GtkWidget *page = gtk_notebook_get_nth_page (notebook, i);
1816                                 if (MODEST_IS_EASYSETUP_WIZARD_PAGE (page))
1817                                         wizard_pages  = g_list_append (wizard_pages, page);
1818                         }
1819                         modest_account_protocol_save_wizard_settings (MODEST_ACCOUNT_PROTOCOL (protocol),
1820                                                                       wizard_pages,
1821                                                                       priv->settings);
1822                         g_list_free (wizard_pages);
1823                 } else {
1824                         g_warning ("The selected protocol is a plugin protocol "//
1825                                    "but it's not a ModestAccountProtocol");
1826                 }
1827
1828                 g_free (provider_id);
1829                 return;
1830         }
1831
1832         /* username and password (for both incoming and outgoing): */
1833         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1834         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1835
1836         store_settings = modest_account_settings_get_store_settings (priv->settings);
1837         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1838
1839         /* Incoming server: */
1840         /* Note: We need something as default for the transport store protocol values, 
1841          * or modest_account_mgr_add_server_account will fail. */
1842         store_port = 0;
1843         store_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1844         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
1845         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1846
1847         if (provider_id) {
1848                 ModestProtocolType store_provider_server_type;
1849                 gboolean store_provider_use_alternate_port;
1850                 /* Use presets: */
1851                 store_hostname = modest_presets_get_server (priv->presets, provider_id, 
1852                                                             TRUE /* store */);
1853                 
1854                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
1855                                                                          provider_id, 
1856                                                                          TRUE /* store */);
1857                 store_security  = modest_presets_get_info_server_security (priv->presets,
1858                                                                                     provider_id, 
1859                                                                                     TRUE /* store */);
1860                 store_auth_protocol  = modest_presets_get_info_server_auth (priv->presets,
1861                                                                                      provider_id, 
1862                                                                                      TRUE /* store */);
1863                 store_provider_use_alternate_port  = modest_presets_get_info_server_use_alternate_port (priv->presets,
1864                                                                                                         provider_id, 
1865                                                                                                         TRUE /* store */);
1866
1867                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1868                 if (store_provider_server_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID)
1869                         store_protocol = MODEST_PROTOCOLS_STORE_POP;
1870                 else
1871                         store_protocol = store_provider_server_type;
1872
1873                 /* we check if there is a *special* port */
1874                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
1875                 if (special_port != 0)
1876                         store_port = special_port;
1877                 else 
1878                         store_port = get_port_from_protocol(store_provider_server_type, store_security);
1879
1880                 modest_server_account_settings_set_security_protocol (store_settings, 
1881                                                                       store_security);
1882                 modest_server_account_settings_set_auth_protocol (store_settings, 
1883                                                                   store_auth_protocol);
1884                 if (store_port != 0)
1885                         modest_server_account_settings_set_port (store_settings, store_port);
1886         } else {
1887                 /* Use custom pages because no preset was specified: */
1888                 store_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver) ));               
1889                 store_protocol = modest_servertype_picker_get_active_servertype (
1890                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));           
1891
1892                 modest_security_options_view_save_settings (
1893                                     MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
1894                                     priv->settings);
1895         }
1896
1897         /* now we store the common store account settings */
1898         modest_server_account_settings_set_hostname (store_settings, store_hostname);
1899         modest_server_account_settings_set_username (store_settings, username);
1900         modest_server_account_settings_set_password (store_settings, password);
1901         modest_server_account_settings_set_protocol (store_settings, store_protocol);
1902
1903         g_object_unref (store_settings);
1904         g_free (store_hostname);
1905         
1906         /* Outgoing server: */
1907         transport_hostname = NULL;
1908         transport_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1909         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
1910         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1911         transport_port = 0;
1912         
1913         if (provider_id) {
1914                 ModestProtocolType transport_provider_server_type;
1915                 ModestProtocolType transport_provider_security;
1916
1917                 /* Use presets */
1918                 transport_hostname = modest_presets_get_server (priv->presets, provider_id, 
1919                                                                 FALSE /* transport */);
1920                         
1921                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
1922                                                                                       provider_id, 
1923                                                                                       FALSE /* transport */);           
1924                 transport_provider_security = modest_presets_get_info_server_security (priv->presets, 
1925                                                                                        provider_id, 
1926                                                                                        FALSE /* transport */);
1927
1928                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1929                 transport_protocol = transport_provider_server_type;
1930                 transport_security = transport_provider_security;
1931                 if (transport_security == MODEST_PROTOCOLS_CONNECTION_SSL) {
1932                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
1933                         /* we check if there is a *special* port */
1934                         special_port = modest_presets_get_port (priv->presets, provider_id,
1935                                                                 FALSE /* transport */);
1936                         if (special_port != 0)
1937                                 transport_port = special_port;
1938                         else 
1939                                 transport_port = 465;
1940                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
1941                 } else {
1942                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
1943                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1944                 }
1945
1946                 modest_server_account_settings_set_security_protocol (transport_settings, 
1947                                                                       transport_security);
1948                 modest_server_account_settings_set_auth_protocol (transport_settings, 
1949                                                                   transport_auth_protocol);
1950                 if (transport_port != 0)
1951                         modest_server_account_settings_set_port (transport_settings, 
1952                                                                  transport_port);
1953         } else {
1954                 ModestProtocolRegistry *registry;
1955                 ModestProtocol *store_proto;
1956
1957                 registry = modest_runtime_get_protocol_registry ();
1958                 /* Use custom pages because no preset was specified: */
1959                 transport_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver) ));
1960
1961                 store_proto = modest_protocol_registry_get_protocol_by_type (registry, 
1962                                                                              store_protocol);
1963
1964                 if (transport_protocol == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1965                         /* fallback to SMTP if none was specified */
1966                         g_warning ("No transport protocol was specified for store %d (%s)",
1967                                    modest_protocol_get_type_id (store_proto),
1968                                    modest_protocol_get_display_name (store_proto));
1969                         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP;
1970                 }
1971
1972                 modest_security_options_view_save_settings (
1973                                     MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security),
1974                                     priv->settings);
1975         }
1976
1977         /* now we store the common transport account settings */
1978         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
1979         modest_server_account_settings_set_username (transport_settings, username);
1980         modest_server_account_settings_set_password (transport_settings, password);
1981         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
1982
1983         g_object_unref (transport_settings);
1984         g_free (transport_hostname);
1985         
1986         fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
1987         email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1988         modest_account_settings_set_fullname (priv->settings, fullname);
1989         modest_account_settings_set_email_address (priv->settings, email_address);
1990         /* we don't set retrieve type to preserve advanced settings if
1991            any. By default account settings are set to headers only */
1992         
1993         /* Save the connection-specific SMTP server accounts. */
1994         modest_account_settings_set_use_connection_specific_smtp 
1995                 (priv->settings, 
1996                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
1997
1998         display_name = get_entered_account_title (self);
1999         modest_account_settings_set_display_name (priv->settings, display_name);
2000         g_free (display_name);
2001         g_free (provider_id);
2002 }
2003