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