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