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