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