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