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