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