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