2007-06-13 Johannes Schmid <johannes.schmid@openismus.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  
30
31 #include "modest-easysetup-wizard.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkseparator.h>
42 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
43 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
44 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
45 #include "widgets/modest-serversecurity-combo-box.h"
46 #include "widgets/modest-secureauth-combo-box.h"
47 #include "widgets/modest-validating-entry.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h"
51 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
52 #include "maemo/modest-connection-specific-smtp-window.h"
53 #include "widgets/modest-ui-constants.h"
54 #include "maemo/modest-account-settings-dialog.h"
55 #include "maemo/modest-maemo-utils.h"
56 #include <gconf/gconf-client.h>
57 #include <string.h> /* For strlen(). */
58
59 /* Include config.h so that _() works: */
60 #ifdef HAVE_CONFIG_H
61 #include <config.h>
62 #endif
63
64 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
65
66 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
67
68 #define WIZARD_DIALOG_GET_PRIVATE(o)                                    \
69         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
70
71 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
72
73 typedef enum {
74         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
75         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
76 } ModestEasysetupWizardDialogServerChanges;
77
78 struct _ModestEasysetupWizardDialogPrivate
79 {
80         ModestPresets *presets;
81
82         /* Remember what fields the user edited manually to not prefill them
83          * again. */
84         ModestEasysetupWizardDialogServerChanges server_changes;
85 };
86
87 static void
88 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
89                                              GValue *value, GParamSpec *pspec)
90 {
91         switch (property_id) {
92         default:
93                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94         }
95 }
96
97 static void
98 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
99                                              const GValue *value, GParamSpec *pspec)
100 {
101         switch (property_id) {
102         default:
103                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104         }
105 }
106
107 static void
108 modest_easysetup_wizard_dialog_dispose (GObject *object)
109 {
110         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
111                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
112 }
113
114 static void
115 modest_easysetup_wizard_dialog_finalize (GObject *object)
116 {
117         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
118         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
119         
120         if (self->account_manager)
121                 g_object_unref (G_OBJECT (self->account_manager));
122                 
123         if (priv->presets)
124                 modest_presets_destroy (priv->presets);
125                 
126         if (self->specific_window)
127                 gtk_widget_destroy (self->specific_window);
128                 
129         g_free (self->saved_account_name);
130         
131         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
132 }
133
134 static void
135 show_error (GtkWindow *parent_window, const gchar* text);
136
137 static gboolean
138 create_account (ModestEasysetupWizardDialog *self, gboolean enabled);
139
140 static void
141 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
142
143 static void
144 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
145
146 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
147
148 static gint get_serverport_incoming(ModestPresetsServerType servertype_incoming,
149                                                                                                                                                 ModestPresetsSecurity security_incoming)
150 {
151         int serverport_incoming = 0;
152                 /* We don't check for SMTP here as that is impossible for an incoming server. */
153                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
154                         serverport_incoming =
155                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 993 : 143; 
156                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
157                         serverport_incoming =
158                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 995 : 110; 
159                 }
160         return serverport_incoming;
161 }
162
163 static void
164 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
165 {
166         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
167         
168         /* Call the vfunc, which may be overridden by derived classes: */
169         if (klass->enable_buttons) {
170                 GtkNotebook *notebook = NULL;
171                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
172                 
173                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
174                 if (current_page_num == -1)
175                         return;
176                         
177                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
178                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
179         }
180 }
181
182 static void
183 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
184 {
185         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
186         g_assert(self);
187         invoke_enable_buttons_vfunc(self);
188 }
189
190 static void
191 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
192 {
193         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
194         g_assert(self);
195         invoke_enable_buttons_vfunc(self);
196 }
197
198 /** This is a convenience function to create a caption containing a mandatory widget.
199  * When the widget is edited, the enable_buttons() vfunc will be called.
200  */
201 static GtkWidget* create_caption_new_with_asterisk(ModestEasysetupWizardDialog *self,
202                                                   GtkSizeGroup *group,
203                                                   const gchar *value,
204                                                   GtkWidget *control,
205                                                   GtkWidget *icon,
206                                                   HildonCaptionStatus flag)
207 {
208         GtkWidget *caption = NULL;
209   
210         /* Note: Previously, the translated strings already contained the "*",
211          * Comment out this code if they do again.
212          */
213         /* Add a * character to indicate mandatory fields,
214          * as specified in our "Email UI Specification": */
215         if (flag == HILDON_CAPTION_MANDATORY) {
216                 gchar* title = g_strdup_printf("%s*", value);
217                 caption = hildon_caption_new (group, title, control, icon, flag);       
218                 g_free(title);
219         }       
220         else
221                 caption = hildon_caption_new (group, value, control, icon, flag);
222
223         /* Connect to the appropriate changed signal for the widget, 
224          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
225          */
226         if (GTK_IS_ENTRY (control)) {
227                 g_signal_connect (G_OBJECT (control), "changed",
228                                   G_CALLBACK (on_caption_entry_changed), self);
229                 
230         }
231         else if (GTK_IS_COMBO_BOX (control)) {
232                 g_signal_connect (G_OBJECT (control), "changed",
233                                   G_CALLBACK (on_caption_combobox_changed), self);
234         }
235          
236         return caption;
237 }
238            
239 static GtkWidget*
240 create_page_welcome (ModestEasysetupWizardDialog *self)
241 {
242         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
243         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
244         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
245         /* So that it is not truncated: */
246         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
247         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
248         gtk_widget_show (label);
249         gtk_widget_show (GTK_WIDGET (box));
250         return GTK_WIDGET (box);
251 }
252
253 static void
254 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
255 {
256         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
257         g_assert(self);
258         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
259         
260         /* Fill the providers combo, based on the selected country: */
261         GSList *list_mcc_ids = easysetup_country_combo_box_get_active_country_ids (
262                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
263         easysetup_provider_combo_box_fill (
264                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, list_mcc_ids);
265 }
266
267 static void
268 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
269 {
270         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
271         g_assert(self);
272         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
273         
274         /* Fill the providers combo, based on the selected country: */
275         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
276                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
277         
278         gchar* domain_name = NULL;
279         if(provider_id)
280                 domain_name = modest_presets_get_domain (priv->presets, provider_id);
281         
282         if(!domain_name)
283                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
284                 
285         if (self->entry_user_email)
286                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
287                 
288         g_free (domain_name);
289         
290         g_free (provider_id);
291 }
292
293 static void
294 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
295 {
296         ModestEasysetupWizardDialog *dialog = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
297         show_error (GTK_WINDOW (dialog), _("ckdg_ib_maximum_characters_reached"));
298 }
299
300 static GtkWidget*
301 create_page_account_details (ModestEasysetupWizardDialog *self)
302 {
303         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
304         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
305         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
306         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
307         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
308         gtk_widget_show (label);
309         
310         /* Create a size group to be used by all captions.
311          * Note that HildonCaption does not create a default size group if we do not specify one.
312          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
313         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
314
315         /* The country widgets: */
316         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
317         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_country"), 
318                                                               self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
319         gtk_widget_show (self->combo_account_country);
320         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
321         gtk_widget_show (caption);
322         
323         /* connect to country combo's changed signal, so we can fill the provider combo: */
324         g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
325                           G_CALLBACK (on_combo_account_country), self);
326             
327         GtkWidget *separator = gtk_hseparator_new ();
328         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
329         gtk_widget_show (separator);
330             
331         /* The service provider widgets: */     
332         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
333         
334         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_serviceprovider"), 
335                                                    self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
336         gtk_widget_show (self->combo_account_serviceprovider);
337         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
338         gtk_widget_show (caption);
339         
340         /* connect to providers combo's changed signal, so we can fill the email address: */
341         g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
342                           G_CALLBACK (on_combo_account_serviceprovider), self);
343         
344         /* TODO: Default to the current country somehow.
345          * But I don't know how to get the information that is specified in the 
346          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
347          *
348          * This is probably not the best choice of gconf key:
349          * This is the  "mcc used in the last pairing", ie. the last connection you made.
350          * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
351          *
352          */
353         GConfClient *client = gconf_client_get_default ();
354         GError *error = NULL;
355         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
356         gint mcc_id = gconf_client_get_int(client, key, &error);
357         
358         if(mcc_id < 0)
359                 mcc_id = 0;
360      
361         if (error) {
362                 g_warning ("Error getting gconf key %s:\n%s", key, error->message);
363                 g_error_free (error);
364                 error = NULL;
365         
366                 mcc_id = 0;
367         }
368     
369         /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
370          * This might just be a Maemo bug.
371          */
372         if (mcc_id == 0) 
373         {
374                 /* For now, we default to Finland when there is nothing better: */
375                 mcc_id = 244;
376         }
377    
378         easysetup_country_combo_box_set_active_country_id (
379                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
380                 
381         
382         /* The description widgets: */  
383         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
384         /* Do use auto-capitalization: */
385         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
386                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
387         
388         /* Set a default account title, choosing one that does not already exist: */
389         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
390         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
391         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
392                 self->account_manager, default_account_name_start);
393         g_free (default_account_name_start);
394         default_account_name_start = NULL;
395         
396         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
397         g_free (default_account_name);
398         default_account_name = NULL;
399
400         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
401                                                    self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
402         gtk_widget_show (self->entry_account_title);
403         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
404         gtk_widget_show (caption);
405         
406         /* Prevent the use of some characters in the account title, 
407          * as required by our UI specification: */
408         GList *list_prevent = NULL;
409         list_prevent = g_list_append (list_prevent, "\\");
410         list_prevent = g_list_append (list_prevent, "/");
411         list_prevent = g_list_append (list_prevent, ":");
412         list_prevent = g_list_append (list_prevent, "*");
413         list_prevent = g_list_append (list_prevent, "?");
414         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
415         list_prevent = g_list_append (list_prevent, "“");
416         list_prevent = g_list_append (list_prevent, "<"); 
417         list_prevent = g_list_append (list_prevent, ">"); 
418         list_prevent = g_list_append (list_prevent, "|");
419         list_prevent = g_list_append (list_prevent, "^");       
420         modest_validating_entry_set_unallowed_characters (
421                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
422         g_list_free (list_prevent);
423         
424         /* Set max length as in the UI spec:
425          * The UI spec seems to want us to show a dialog if we hit the maximum. */
426         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
427         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
428                                               on_entry_max, self);
429         
430         gtk_widget_show (GTK_WIDGET (box));
431         
432         return GTK_WIDGET (box);
433 }
434
435 static GtkWidget*
436 create_page_user_details (ModestEasysetupWizardDialog *self)
437 {
438         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
439         
440         /* Create a size group to be used by all captions.
441          * Note that HildonCaption does not create a default size group if we do not specify one.
442          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
443         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
444          
445         /* The name widgets: */
446         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
447         /* Auto-capitalization is the default, so let's turn it off: */
448         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
449         /* Set max length as in the UI spec:
450          * The UI spec seems to want us to show a dialog if we hit the maximum. */
451         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
452         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
453                                               on_entry_max, self);
454         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
455                                                               _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
456         gtk_widget_show (self->entry_user_name);
457         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
458         gtk_widget_show (caption);
459         
460         /* Prevent the use of some characters in the name, 
461          * as required by our UI specification: */
462         GList *list_prevent = NULL;
463         list_prevent = g_list_append (list_prevent, "<");
464         list_prevent = g_list_append (list_prevent, ">");
465         modest_validating_entry_set_unallowed_characters (
466                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
467         g_list_free (list_prevent);
468         
469         /* The username widgets: */     
470         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
471         /* Auto-capitalization is the default, so let's turn it off: */
472         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
473         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
474                                                    self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
475         gtk_widget_show (self->entry_user_username);
476         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
477         gtk_widget_show (caption);
478         
479         /* Prevent the use of some characters in the username, 
480          * as required by our UI specification: */
481         modest_validating_entry_set_unallowed_characters_whitespace (
482                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
483         
484         /* Set max length as in the UI spec:
485          * The UI spec seems to want us to show a dialog if we hit the maximum. */
486         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
487         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
488                                               on_entry_max, self);
489         
490         /* The password widgets: */     
491         self->entry_user_password = gtk_entry_new ();
492         /* Auto-capitalization is the default, so let's turn it off: */
493         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
494                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
495         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
496         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
497         caption = create_caption_new_with_asterisk (self, sizegroup, 
498                                                    _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
499         gtk_widget_show (self->entry_user_password);
500         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
501         gtk_widget_show (caption);
502         
503         /* The email address widgets: */        
504         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
505         /* Auto-capitalization is the default, so let's turn it off: */
506         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
507         caption = create_caption_new_with_asterisk (self, sizegroup, 
508                                                    _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
509         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
510         gtk_widget_show (self->entry_user_email);
511         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
512         gtk_widget_show (caption);
513         
514         /* Set max length as in the UI spec:
515          * The UI spec seems to want us to show a dialog if we hit the maximum. */
516         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
517         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
518                                               on_entry_max, self);
519         
520         
521         gtk_widget_show (GTK_WIDGET (box));
522         
523         return GTK_WIDGET (box);
524 }
525
526 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
527 {
528         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
529         
530         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
531         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
532         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
533         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
534         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
535         gtk_widget_show (label);
536         
537         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
538         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
539         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
540         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
541         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
542         gtk_widget_show (label);
543         
544         gtk_widget_show (GTK_WIDGET (box));
545         return GTK_WIDGET (box);
546 }
547
548 /** Change the caption title for the incoming server, 
549  * as specified in the UI spec:
550  */
551 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
552 {
553         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
554                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
555         const gchar* type = 
556                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
557                  _("mail_fi_emailtype_pop3") : 
558                  _("mail_fi_emailtype_imap") );
559                         
560                 
561         /* Note that this produces a compiler warning, 
562          * because the compiler does not know that the translated string will have a %s in it.
563          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
564         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
565         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
566         g_free(incomingserver_title);
567 }
568
569 /** Change the caption title for the incoming server, 
570  * as specified in the UI spec:
571  */
572 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
573 {
574         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
575                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
576         
577         /* Fill the combo with appropriately titled choices for POP or IMAP. */
578         /* The choices are the same, but the titles are different, as in the UI spec. */
579         modest_serversecurity_combo_box_fill (
580                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
581 }
582
583 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
584 {
585         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
586         update_incoming_server_title (self);
587         update_incoming_server_security_choices (self);
588
589         set_default_custom_servernames (self);
590 }
591
592 static void on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
593 {
594         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
595         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
596         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
597 }
598
599 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
600 {
601         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
602
603         /* Show note that account type cannot be changed in future: */
604         GtkWidget *label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
605         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
606         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
607         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
608         gtk_widget_show (label);
609         
610         /* Create a size group to be used by all captions.
611          * Note that HildonCaption does not create a default size group if we do not specify one.
612          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
613         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
614          
615         /* The incoming server widgets: */
616         if (!self->combo_incoming_servertype)
617                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
618         easysetup_servertype_combo_box_set_active_servertype (
619                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
620         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
621                                                               _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
622         gtk_widget_show (self->combo_incoming_servertype);
623         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
624         gtk_widget_show (caption);
625         
626         if(!self->entry_incomingserver)
627                 self->entry_incomingserver = gtk_entry_new ();
628         /* Auto-capitalization is the default, so let's turn it off: */
629         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
630         set_default_custom_servernames (self);
631
632         if (self->caption_incoming)
633                 gtk_widget_destroy (self->caption_incoming);
634            
635         /* The caption title will be updated in update_incoming_server_title().
636          * so this default text will never be seen: */
637         /* (Note: Changing the title seems pointless. murrayc) */
638         self->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
639                                                                   "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
640         update_incoming_server_title (self);
641         gtk_widget_show (self->entry_incomingserver);
642         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
643         gtk_widget_show (self->caption_incoming);
644         
645         /* Change the caption title when the servertype changes, 
646          * as in the UI spec: */
647         g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
648                           G_CALLBACK (on_combo_servertype_changed), self);
649
650         /* Remember when the servername was changed manually: */
651         g_signal_connect (G_OBJECT (self->entry_incomingserver), "changed",
652                           G_CALLBACK (on_entry_incoming_servername_changed), self);
653
654         /* The secure connection widgets: */    
655         if (!self->combo_incoming_security)
656                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
657         update_incoming_server_security_choices (self);
658         modest_serversecurity_combo_box_set_active_serversecurity (
659                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
660         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
661                                       self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
662         gtk_widget_show (self->combo_incoming_security);
663         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
664         gtk_widget_show (caption);
665         
666         if(!self->checkbox_incoming_auth)
667                 self->checkbox_incoming_auth = gtk_check_button_new ();
668         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
669                                       self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
670         gtk_widget_show (self->checkbox_incoming_auth);
671         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
672         gtk_widget_show (caption);
673         
674         gtk_widget_show (GTK_WIDGET (box));
675         
676         return GTK_WIDGET (box);
677 }
678
679 static void
680 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
681 {
682         GtkWidget *widget = GTK_WIDGET (user_data);
683         
684         /* Enable the widget only if the toggle button is active: */
685         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
686         gtk_widget_set_sensitive (widget, enable);
687 }
688
689 /* Make the sensitivity of a widget depend on a toggle button.
690  */
691 static void
692 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
693 {
694         g_signal_connect (G_OBJECT (button), "toggled",
695                           G_CALLBACK (on_toggle_button_changed), widget);
696         
697         /* Set the starting sensitivity: */
698         on_toggle_button_changed (button, widget);
699 }
700
701 static void
702 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
703 {
704         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
705         
706         /* Create the window, if necessary: */
707         if (!(self->specific_window)) {
708                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
709                 modest_connection_specific_smtp_window_fill_with_connections (
710                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
711                         NULL /* account_name, not known yet. */);
712         }
713
714         /* Show the window: */
715         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
716         gtk_widget_show (self->specific_window);
717 }
718
719 static void on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
720 {
721         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
722         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
723         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
724 }
725
726 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
727 {
728         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
729         
730         /* Create a size group to be used by all captions.
731          * Note that HildonCaption does not create a default size group if we do not specify one.
732          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
733         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
734          
735         /* The outgoing server widgets: */
736         if (!self->entry_outgoingserver)
737                 self->entry_outgoingserver = gtk_entry_new ();
738         /* Auto-capitalization is the default, so let's turn it off: */
739         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
740         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
741                                                               _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
742         gtk_widget_show (self->entry_outgoingserver);
743         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
744         gtk_widget_show (caption);
745         set_default_custom_servernames (self);
746         
747         /* The secure connection widgets: */    
748         if (!self->combo_outgoing_security)
749                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
750         modest_serversecurity_combo_box_fill (
751                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
752         modest_serversecurity_combo_box_set_active_serversecurity (
753                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
754         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
755                                       self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
756         gtk_widget_show (self->combo_outgoing_security);
757         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
758         gtk_widget_show (caption);
759         
760         /* The secure authentication widgets: */
761         if (!self->combo_outgoing_auth)
762                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
763         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
764                                       self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
765         gtk_widget_show (self->combo_outgoing_auth);
766         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
767         gtk_widget_show (caption);
768         
769         GtkWidget *separator = gtk_hseparator_new ();
770         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
771         gtk_widget_show (separator);
772         
773         /* connection-specific checkbox: */
774         if (!self->checkbox_outgoing_smtp_specific) {
775                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
776                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
777                                               FALSE);
778         }
779         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
780                                       self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
781         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
782         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
783         gtk_widget_show (caption);
784         
785         /* Connection-specific SMTP-Severs Edit button: */
786         if (!self->button_outgoing_smtp_servers)
787                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
788         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
789                                       self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
790         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
791         gtk_widget_show (self->button_outgoing_smtp_servers);
792         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
793         gtk_widget_show (caption);
794         
795         /* Only enable the button when the checkbox is checked: */
796         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
797                                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
798                 
799         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
800                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
801
802         g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
803                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
804         
805         
806         gtk_widget_show (GTK_WIDGET (box));
807         
808         return GTK_WIDGET (box);
809 }
810
811 static gboolean
812 show_advanced_edit(gpointer user_data)
813 {
814         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
815         
816         if (!(self->saved_account_name))
817                 return FALSE;
818         
819         /* Show the Account Settings window: */
820         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
821         modest_account_settings_dialog_set_account_name (dialog, self->saved_account_name);
822         
823         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
824         
825         gtk_dialog_run (GTK_DIALOG (dialog));
826
827         gtk_widget_destroy (GTK_WIDGET (dialog));
828         
829         return FALSE; /* Do not call this timeout callback again. */
830 }
831
832 static void
833 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
834 {
835         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
836         
837         /* Save the new account, so we can edit it with ModestAccountSettingsDialog, 
838          * without recoding it to use non-gconf information.
839          * This account will be deleted if Finish is never actually clicked. */
840
841         gboolean saved = TRUE;
842         if (!(self->saved_account_name)) {
843                 saved = create_account (self, FALSE);
844         }
845                 
846         if (!saved)
847                 return;
848                 
849         if (!(self->saved_account_name))
850                 return;
851         
852         /* Show the Account Settings window: */
853         show_advanced_edit(self);
854 }
855 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
856 {
857         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
858         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
859         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
860         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
861         gtk_widget_show (label);
862         
863         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
864         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
865         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
866         gtk_widget_show (label);
867         
868         if (!self->button_edit)
869                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
870         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
871                                                  self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
872         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
873         gtk_widget_show (self->button_edit);
874         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
875         gtk_widget_show (caption);
876         
877         g_signal_connect (G_OBJECT (self->button_edit), "clicked", 
878                           G_CALLBACK (on_button_edit_advanced_settings), self);
879         
880         gtk_widget_show (GTK_WIDGET (box));
881         return GTK_WIDGET (box);
882 }
883
884
885 /*
886  */
887 static void 
888 on_response (ModestWizardDialog *wizard_dialog,
889              gint response_id,
890              gpointer user_data)
891 {
892         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
893         
894         if (response_id == GTK_RESPONSE_CANCEL) {
895                 /* Remove any temporarily-saved account that will not actually be needed: */
896                 if (self->saved_account_name) {
897                         modest_account_mgr_remove_account (self->account_manager,
898                                                            self->saved_account_name, FALSE);
899                 }       
900         }
901         
902         invoke_enable_buttons_vfunc (self);
903 }
904
905 static void
906 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
907 {
908         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
909         
910         /* Create the notebook to be used by the ModestWizardDialog base class:
911          * Each page of the notebook will be a page of the wizard: */
912         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
913         
914         /* Set the notebook used by the ModestWizardDialog base class: */
915         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
916     
917         /* Set the wizard title:
918          * The actual window title will be a combination of this and the page's tab label title. */
919         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
920
921         /* Read in the information about known service providers: */
922         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
923         
924         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
925         priv->presets = modest_presets_new (filepath);
926         if (!(priv->presets)) {
927                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
928         }
929         
930         g_assert(priv->presets);
931
932         /* The server fields did not have been manually changed yet */
933         priv->server_changes = 0;
934
935         /* Get the account manager object, 
936          * so we can check for existing accounts,
937          * and create new accounts: */
938         self->account_manager = modest_runtime_get_account_mgr ();
939         g_assert (self->account_manager);
940         g_object_ref (self->account_manager);
941         
942         /* Create the common pages, 
943          */
944         self->page_welcome = create_page_welcome (self);
945         self->page_account_details = create_page_account_details (self);
946         self->page_user_details = create_page_user_details (self);
947         
948         /* Add the common pages: */
949         gtk_notebook_append_page (notebook, self->page_welcome, 
950                                   gtk_label_new (_("mcen_ti_emailsetup_welcome")));
951         gtk_notebook_append_page (notebook, self->page_account_details, 
952                                   gtk_label_new (_("mcen_ti_accountdetails")));
953         gtk_notebook_append_page (notebook, self->page_user_details, 
954                                   gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
955                 
956         /* Create and add the easysetup-specific pages,
957          * because we need _some_ final page to enable the Next and Finish buttons: */
958         create_subsequent_easysetup_pages (self);
959
960         /* Connect to the dialog's response signal so we can enable/disable buttons 
961          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
962          * Note that we use g_signal_connect_after() instead of g_signal_connect()
963          * so that we can be enable/disable after ModestWizardDialog has done its own 
964          * enabling/disabling of buttons.
965          * 
966          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
967          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
968          * 
969          * It's not enough to connect to the notebook's switch-page signal, because 
970          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
971          * _after_ switching the page (understandably).
972          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
973          * then gtk_notebook_get_current_page() would return an incorrect value.)
974          */
975         g_signal_connect_after (G_OBJECT (self), "response",
976                                 G_CALLBACK (on_response), self);
977
978         /* When this window is shown, hibernation should not be possible, 
979          * because there is no sensible way to save the state: */
980         modest_window_mgr_prevent_hibernation_while_window_is_shown (
981                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
982 }
983
984 ModestEasysetupWizardDialog*
985 modest_easysetup_wizard_dialog_new (void)
986 {
987         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
988 }
989
990 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
991 {
992         GtkNotebook *notebook = NULL;
993         g_object_get (self, "wizard-notebook", &notebook, NULL);
994         g_assert(notebook);
995         
996         /* Create the custom pages: */
997         if(!(self->page_custom_incoming)) {
998                 self->page_custom_incoming = create_page_custom_incoming (self);
999         }
1000                 
1001         if(!(self->page_custom_outgoing)) {
1002                 self->page_custom_outgoing = create_page_custom_outgoing (self);
1003         }
1004         
1005         if(!(self->page_complete_customsetup)) {
1006                 self->page_complete_customsetup = create_page_complete_custom (self);
1007         }
1008         
1009         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
1010                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
1011                                           gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
1012         
1013         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
1014                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
1015                                           gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
1016                 
1017         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
1018                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
1019                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1020                         
1021         /* This is unnecessary with GTK+ 2.10: */
1022         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1023 }
1024         
1025 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1026 {
1027         GtkNotebook *notebook = NULL;
1028         g_object_get (self, "wizard-notebook", &notebook, NULL);
1029         g_assert(notebook);
1030         
1031         /* Create the easysetup-specific pages: */
1032         if(!self->page_complete_easysetup)
1033                 self->page_complete_easysetup = create_page_complete_easysetup (self);
1034
1035         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
1036                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
1037                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1038                         
1039         /* This is unnecessary with GTK+ 2.10: */
1040         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1041 }
1042 /* After the user details page,
1043  * the following pages depend on whether "Other" was chosen 
1044  * in the provider combobox on the account page
1045  */
1046 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
1047 {
1048         if (easysetup_provider_combo_box_get_active_provider_id (
1049                     EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
1050                 /* "Other..." was selected: */
1051                 
1052                 /* Make sure that the easysetup pages do not exist: */
1053                 if(self->page_complete_easysetup) {
1054                         gtk_widget_destroy (self->page_complete_easysetup);
1055                         self->page_complete_easysetup = NULL;
1056                 }
1057                 
1058                 create_subsequent_customsetup_pages (self);
1059         }       
1060         else {
1061                 /* A specific provider was selected: */
1062                 {
1063                         /* Make sure that the custom pages do not exist:
1064                          * Because they will be used if they exist, when creating the account. */
1065                         if(self->page_custom_incoming) {
1066                                 gtk_widget_destroy (self->page_custom_incoming);
1067                                 self->page_custom_incoming = NULL;
1068                         }
1069                         
1070                         if(self->page_custom_outgoing) {
1071                                 gtk_widget_destroy (self->page_custom_outgoing);
1072                                 self->page_custom_outgoing = NULL;
1073                         }
1074                         
1075                         if(self->page_complete_customsetup) {
1076                                 gtk_widget_destroy (self->page_complete_customsetup);
1077                                 self->page_complete_customsetup = NULL;
1078                         }
1079                 }
1080                 
1081                 /* Create the easysetup pages: */
1082                 create_subsequent_easysetup_pages (self);
1083         }
1084 }
1085
1086
1087 static gchar*
1088 util_get_default_servername_from_email_address (const gchar* email_address, ModestTransportStoreProtocol servertype)
1089 {
1090         if (!email_address)
1091                 return NULL;
1092         
1093         gchar* at = g_utf8_strchr (email_address, -1, '@');
1094         if (!at || (g_utf8_strlen (at, -1) < 2))
1095                 return NULL;
1096                 
1097         gchar* domain = g_utf8_next_char (at);
1098         if(!domain)
1099                 return NULL;
1100                 
1101         const gchar* hostname = NULL;
1102         if (servertype == MODEST_PROTOCOL_STORE_POP)
1103                 hostname = "pop";
1104         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
1105                 hostname = "imap";
1106         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
1107                 hostname = "smtp";
1108         
1109         if (!hostname)
1110                 return NULL;
1111                 
1112         return g_strdup_printf ("%s.%s", hostname, domain);
1113 }
1114
1115 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
1116 {
1117         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(account_wizard);
1118
1119         if (!account_wizard->entry_incomingserver)
1120                 return;
1121                 
1122         /* Set a default domain for the server, based on the email address,
1123          * if no server name was already specified.
1124          */
1125         if (account_wizard->entry_user_email
1126             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1127                 const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
1128                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1129                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1130                 
1131                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
1132
1133                 /* Do not set the INCOMING_CHANGED flag because of this edit */
1134                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1135                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
1136                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1137
1138                 g_free (servername);
1139         }
1140         
1141         /* Set a default domain for the server, based on the email address,
1142          * if no server name was already specified.
1143          */
1144         if (!account_wizard->entry_outgoingserver)
1145                 return;
1146                 
1147         if (account_wizard->entry_user_email
1148             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1149                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1150                 
1151                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
1152
1153                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1154                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1155                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
1156                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1157
1158                 g_free (servername);
1159         }
1160 }
1161
1162 static gboolean
1163 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1164 {
1165         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1166         
1167         /* Do extra validation that couldn't be done for every key press,
1168          * either because it was too slow,
1169          * or because it requires interaction:
1170          */
1171         if (current_page == account_wizard->page_account_details) {     
1172                 /* Check that the title is not already in use: */
1173                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
1174                 if ((!account_title) || (strlen(account_title) == 0))
1175                         return FALSE;
1176                         
1177                 /* Aavoid a clash with an existing display name: */
1178                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1179                         account_wizard->account_manager, account_title);
1180
1181                 if (name_in_use) {
1182                         /* Warn the user via a dialog: */
1183                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
1184             
1185                         return FALSE;
1186                 }
1187         }
1188         else if (current_page == account_wizard->page_user_details) {
1189                 /* Check that the email address is valud: */
1190                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1191                 if ((!email_address) || (strlen(email_address) == 0))
1192                         return FALSE;
1193                         
1194                 if (!modest_text_utils_validate_email_address (email_address)) {
1195                         /* Warn the user via a dialog: */
1196                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
1197                                              
1198                         /* Return focus to the email address entry: */
1199                         gtk_widget_grab_focus (account_wizard->entry_user_email);
1200             
1201                         return FALSE;
1202                 }
1203                 
1204                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1205                 create_subsequent_pages (account_wizard);
1206         }
1207         
1208         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1209          * but does not specify how.
1210          */
1211           
1212         if(next_page == account_wizard->page_custom_incoming) {
1213                 set_default_custom_servernames (account_wizard);
1214         }
1215         else if (next_page == account_wizard->page_custom_outgoing) {
1216                 set_default_custom_servernames (account_wizard);
1217     /* Check if the server support secure authentication */
1218                 const ModestConnectionProtocol security_incoming = 
1219                         modest_serversecurity_combo_box_get_active_serversecurity (
1220                                                                                                                                                                                                                                                                  MODEST_SERVERSECURITY_COMBO_BOX (
1221                                                                                                                                                                                                                                                                                                                                                                                                         account_wizard->combo_incoming_security));
1222     if (gtk_toggle_button_get_active (
1223                         GTK_TOGGLE_BUTTON (account_wizard->checkbox_incoming_auth))
1224                                 && !modest_protocol_info_is_secure(security_incoming))
1225                 {
1226                                 const ModestTransportStoreProtocol protocol = 
1227           easysetup_servertype_combo_box_get_active_servertype (
1228                                                                 EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1229         const gchar* hostname = gtk_entry_get_text(GTK_ENTRY(account_wizard->entry_incomingserver));
1230                                 const ModestConnectionProtocol protocol_security_incoming = 
1231                                         modest_serversecurity_combo_box_get_active_serversecurity (
1232                                                                                                                                                                                                                                                                                  MODEST_SERVERSECURITY_COMBO_BOX (
1233                                                                                                                                                                                                                                                                                                                                                                                                                         account_wizard->combo_incoming_security));
1234         int port_num = get_serverport_incoming(protocol, protocol_security_incoming); 
1235         GList *list_auth_methods = 
1236           modest_maemo_utils_get_supported_secure_authentication_methods (
1237                                                                       protocol, 
1238                                                                       hostname, port_num, GTK_WINDOW (account_wizard)); 
1239         if (list_auth_methods) {
1240           /* TODO: Select the correct method */
1241           g_list_free (list_auth_methods);
1242         }
1243         else
1244         {
1245           GtkWidget* error_dialog = gtk_message_dialog_new(GTK_WINDOW(dialog),
1246                                                        GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
1247                                                        GTK_BUTTONS_OK, _("Server does not support secure authentication!"));
1248           gtk_dialog_run(GTK_DIALOG(error_dialog));
1249           gtk_widget_destroy(error_dialog);
1250           return FALSE;
1251         }
1252                  }
1253         }
1254         
1255         /* If this is the last page, and this is a click on Finish, 
1256          * then attempt to create the dialog.
1257          */
1258         if(!next_page) /* This is NULL when this is a click on Finish. */
1259         {
1260                 if (account_wizard->saved_account_name) {
1261                         /* Just enable the already-saved account (temporarily created when 
1262                          * editing advanced settings): */
1263                         modest_account_mgr_set_enabled (account_wizard->account_manager, 
1264                                                         account_wizard->saved_account_name, TRUE);
1265                 } else {
1266                         create_account (account_wizard, TRUE /* enabled */);
1267                 }
1268         }
1269         
1270         
1271         return TRUE;
1272 }
1273
1274 static gboolean entry_is_empty (GtkWidget *entry)
1275 {
1276         if (!entry)
1277                 return FALSE;
1278                 
1279         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1280         if ((!text) || (strlen(text) == 0))
1281                 return TRUE;
1282         else
1283                 return FALSE;
1284 }
1285
1286 static void
1287 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1288 {
1289         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1290         
1291         gboolean enable_next = TRUE;
1292         if (current_page == account_wizard->page_welcome) {
1293                 enable_next = TRUE;
1294         }
1295         else if (current_page == account_wizard->page_account_details) {
1296                 /* The account details title is mandatory: */
1297                 if (entry_is_empty(account_wizard->entry_account_title))
1298                         enable_next = FALSE;
1299         }
1300         else if (current_page == account_wizard->page_user_details) {   
1301                 /* The user details username is mandatory: */
1302                 if (entry_is_empty(account_wizard->entry_user_username))
1303                         enable_next = FALSE;
1304                         
1305                 /* The user details email address is mandatory: */
1306                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1307                         enable_next = FALSE;
1308         }
1309         else if (current_page == account_wizard->page_custom_incoming) {
1310                 /* The custom incoming server is mandatory: */
1311                 if (entry_is_empty(account_wizard->entry_incomingserver))
1312                         enable_next = FALSE;
1313         }
1314                         
1315         /* Enable the buttons, 
1316          * identifying them via their associated response codes: */
1317                                    
1318         /* Disable the Finish button until we are on the last page,
1319          * because HildonWizardDialog enables this for all but the first page: */
1320         GtkNotebook *notebook = NULL;
1321         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1322         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1323         
1324         gint current = gtk_notebook_get_current_page (notebook);
1325         gint last = gtk_notebook_get_n_pages (notebook) - 1;
1326         const gboolean is_last = (current == last);
1327     
1328         if(!is_last) {
1329                 gtk_dialog_set_response_sensitive (dialog_base,
1330                                                    MODEST_WIZARD_DIALOG_FINISH,
1331                                                    FALSE);
1332         } else
1333         {
1334                 /* Disable Next on the last page: */
1335                 enable_next = FALSE;
1336         }
1337         
1338         gtk_dialog_set_response_sensitive (dialog_base,
1339                                            MODEST_WIZARD_DIALOG_NEXT,
1340                                            enable_next);
1341 }
1342
1343 static void
1344 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1345 {
1346         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1347         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1348
1349
1350         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1351         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1352         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1353         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1354         
1355         /* Provide a vfunc implementation so we can decide 
1356          * when to enable/disable the prev/next buttons.
1357          */
1358         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1359         base_klass->before_next = on_before_next;
1360         base_klass->enable_buttons = on_enable_buttons;
1361 }
1362  
1363 static void
1364 show_error (GtkWindow *parent_window, const gchar* text)
1365 {
1366         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1367         /*
1368           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1369           (GtkDialogFlags)0,
1370           GTK_MESSAGE_ERROR,
1371           GTK_BUTTONS_OK,
1372           text ));
1373         */
1374                  
1375         gtk_dialog_run (dialog);
1376         gtk_widget_destroy (GTK_WIDGET (dialog));
1377 }
1378
1379 /** Attempt to create the account from the information that the user has entered.
1380  * @result: TRUE if the account was successfully created.
1381  */
1382 gboolean
1383 create_account (ModestEasysetupWizardDialog *self, gboolean enabled)
1384 {
1385         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1386         
1387         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1388
1389         /* Some checks: */
1390         if (!display_name)
1391                 return FALSE;
1392                 
1393         /* We should have checked for this already, 
1394          * and changed that name accordingly, 
1395          * but let's check again just in case:
1396          */
1397         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1398                 return FALSE;
1399
1400         /* Increment the non-user visible name if necessary, 
1401          * based on the display name: */
1402         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1403                                                                           display_name, FALSE /* not a server account */);
1404                 
1405         /* username and password (for both incoming and outgoing): */
1406         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1407         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1408         
1409         /* Incoming server: */
1410         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
1411          * or modest_account_mgr_add_server_account will fail. */
1412         gchar* servername_incoming = NULL;
1413         guint serverport_incoming = 0;
1414         ModestTransportStoreProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1415         ModestConnectionProtocol protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_NORMAL;
1416         ModestAuthProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1417         
1418         /* Get details from the specified presets: */
1419         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1420                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1421         if (provider_id) {
1422                 /* Use presets: */
1423                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1424                                                                  TRUE /* incoming */);
1425                 
1426                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets,
1427                                                                                                    provider_id, 
1428                                                                                                    TRUE /* incoming */);
1429                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets,
1430                                                                                                    provider_id, 
1431                                                                                                    TRUE /* incoming */);
1432
1433                 g_warning ("security incoming: %x", security_incoming);
1434                         
1435                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1436                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
1437                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1438                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
1439                         protocol_incoming = MODEST_PROTOCOL_STORE_POP; 
1440                 }
1441                 serverport_incoming = get_serverport_incoming(servertype_incoming, security_incoming);
1442                 
1443                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1444                         protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1445                 
1446                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1447                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1448         }
1449         else {
1450                 /* Use custom pages because no preset was specified: */
1451                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1452                 
1453                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1454                         EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1455                 
1456                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1457                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1458                 
1459                 /* The UI spec says:
1460                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1461                  * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1462                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1463                  */
1464                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1465                         ? MODEST_PROTOCOL_AUTH_CRAMMD5
1466                         : MODEST_PROTOCOL_AUTH_PASSWORD;
1467         }
1468         
1469         /* First we add the 2 server accounts, and then we add the account that uses them.
1470          * If we don't do it in this order then we will experience a crash. */
1471          
1472         /* Add a (incoming) server account, to be used by the account: */
1473         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1474         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1475                                                                         store_name_start, TRUE /* server account */);
1476         g_free (store_name_start);
1477         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1478                                                                   store_name,
1479                                                                   servername_incoming,
1480                                                                   serverport_incoming,
1481                                                                   username, password,
1482                                                                   protocol_incoming,
1483                                                                   protocol_security_incoming,
1484                                                                   protocol_authentication_incoming);            
1485                 
1486         g_free (servername_incoming);
1487         
1488         if (!created) {
1489                 /* TODO: Provide a Logical ID for the text: */
1490                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1491                 return FALSE;   
1492         }
1493         
1494         /* Outgoing server: */
1495         gchar* servername_outgoing = NULL;
1496         ModestTransportStoreProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1497         ModestConnectionProtocol protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1498         ModestAuthProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1499         guint serverport_outgoing = 0;
1500         
1501         if (provider_id) {
1502                 /* Use presets: */
1503                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1504                                                                  FALSE /* incoming */);
1505                         
1506                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets,
1507                                                                                                    provider_id, 
1508                                                                                                    FALSE /* incoming */);
1509                 
1510                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1511                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1512                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1513                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1514                 
1515                 ModestPresetsSecurity security_outgoing = 
1516                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1517                                                                  FALSE /* incoming */);
1518
1519                 /* TODO: There is no SMTP authentication enum for presets, 
1520                    so we should probably check what the server supports. */
1521                 protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1522                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP) {
1523                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
1524                         protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1525                         serverport_outgoing = 465;
1526                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_PASSWORD;
1527                 } else {
1528                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
1529                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1530                 }
1531         }
1532         else {
1533                 /* Use custom pages because no preset was specified: */
1534                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1535                 
1536                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1537
1538                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1539                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1540                 
1541                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1542                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1543         }
1544             
1545         /* Add a (outgoing) server account to be used by the account: */
1546         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1547         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1548                                                                             transport_name_start, TRUE /* server account */);
1549         g_free (transport_name_start);
1550         created = modest_account_mgr_add_server_account (self->account_manager,
1551                                                          transport_name,
1552                                                          servername_outgoing,
1553                                                          serverport_outgoing,
1554                                                          username, password,
1555                                                          protocol_outgoing,
1556                                                          protocol_security_outgoing,
1557                                                          protocol_authentication_outgoing);
1558                 
1559         g_free (servername_outgoing);
1560                 
1561         if (!created) {
1562                 /* TODO: Provide a Logical ID for the text: */
1563                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1564                 return FALSE;   
1565         }
1566         
1567         
1568         /* Create the account, which will contain the two "server accounts": */
1569         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1570                                                   store_name, /* The name of our POP/IMAP server account. */
1571                                                   transport_name, /* The name of our SMTP server account. */
1572                                                   enabled);
1573         g_free (store_name);
1574         g_free (transport_name);
1575         
1576         if (!created) {
1577                 /* TODO: Provide a Logical ID for the text: */
1578                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1579                 return FALSE;   
1580         }
1581
1582
1583         /* Sanity check: */
1584         /* There must be at least one account now: */
1585         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
1586          * fixed in versions after 3.1. */
1587         GSList *account_names = modest_account_mgr_account_names (self->account_manager, FALSE);
1588         if(!account_names)
1589         {
1590                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1591         }
1592         g_slist_free (account_names);
1593
1594
1595         
1596         /* The user name and email address must be set additionally: */
1597         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1598         modest_account_mgr_set_string (self->account_manager, account_name,
1599                                        MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1600
1601         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1602         modest_account_mgr_set_string (self->account_manager, account_name,
1603                                        MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1604
1605         /* Set the display name: */
1606         modest_account_mgr_set_string (self->account_manager, account_name,
1607                                        MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1608
1609         /* Set retrieve type */ 
1610         const gchar *retrieve = MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1611         modest_account_mgr_set_string (self->account_manager, account_name,
1612                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1613
1614         /* Save the connection-specific SMTP server accounts. */
1615         gboolean result = TRUE;
1616         if (self->specific_window)
1617                 result = modest_connection_specific_smtp_window_save_server_accounts (
1618                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1619                         
1620         g_free (self->saved_account_name);
1621         self->saved_account_name = g_strdup (account_name);
1622         
1623         g_free (account_name);
1624
1625         return result;
1626 }