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