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