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