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