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