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