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