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