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