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