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