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