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