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