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