* src/maemo/modest-msg-edit-window.c
[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 "widgets/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_complete_easysetup = NULL;       
1258         priv->page_custom_incoming = NULL;
1259         priv->combo_incoming_servertype = NULL;
1260         priv->caption_incoming = NULL;
1261         priv->entry_incomingserver = NULL;
1262         priv->combo_incoming_security = NULL;
1263         priv->checkbox_incoming_auth = NULL;
1264         priv->page_custom_outgoing = NULL;
1265         priv->entry_outgoingserver = NULL;
1266         priv->combo_outgoing_security = NULL;
1267         priv->combo_outgoing_auth = NULL;
1268         priv->checkbox_outgoing_smtp_specific = NULL;
1269         priv->button_outgoing_smtp_servers = NULL;
1270         priv->page_complete_customsetup = NULL;
1271         priv->specific_window = NULL;
1272
1273         /* Add the common pages: */
1274         gtk_notebook_append_page (notebook, priv->page_welcome, 
1275                                   gtk_label_new (_("mcen_ti_emailsetup_welcome")));
1276         gtk_container_child_set (GTK_CONTAINER (notebook), 
1277                                  priv->page_welcome,
1278                                  "tab-expand", TRUE,
1279                                  "tab-fill", TRUE,
1280                                  NULL);
1281
1282         gtk_notebook_append_page (notebook, priv->page_account_details, 
1283                                   gtk_label_new (_("mcen_ti_accountdetails")));
1284         gtk_container_child_set (GTK_CONTAINER (notebook), 
1285                                  priv->page_account_details,
1286                                  "tab-expand", TRUE,
1287                                  "tab-fill", TRUE,
1288                                  NULL);
1289
1290         gtk_notebook_append_page (notebook, priv->page_user_details, 
1291                                   gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
1292         gtk_container_child_set (GTK_CONTAINER (notebook), 
1293                                  priv->page_user_details,
1294                                  "tab-expand", TRUE,
1295                                  "tab-fill", TRUE,
1296                                  NULL);
1297                 
1298         /* Create and add the easysetup-specific pages,
1299          * because we need _some_ final page to enable the Next and Finish buttons: */
1300         create_subsequent_easysetup_pages (self);
1301
1302         /* Connect to the dialog's response signal so we can enable/disable buttons 
1303          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1304          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1305          * so that we can be enable/disable after ModestWizardDialog has done its own 
1306          * enabling/disabling of buttons.
1307          * 
1308          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1309          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1310          * 
1311          * It's not enough to connect to the notebook's switch-page signal, because 
1312          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1313          * _after_ switching the page (understandably).
1314          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1315          * then gtk_notebook_get_current_page() would return an incorrect value.)
1316          */
1317         g_signal_connect_after (G_OBJECT (self), "response",
1318                                 G_CALLBACK (on_response), self);
1319
1320         /* This is to show a confirmation dialog when the user hits cancel */
1321         g_signal_connect (G_OBJECT (self), "response",
1322                           G_CALLBACK (on_response_before), self);
1323
1324         g_signal_connect (G_OBJECT (self), "delete-event",
1325                           G_CALLBACK (on_delete_event), self);
1326
1327         /* Reset dirty, because there was no user input until now */
1328         priv->dirty = FALSE;
1329         
1330         /* When this window is shown, hibernation should not be possible, 
1331          * because there is no sensible way to save the state: */
1332         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1333                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1334
1335         /* Load provider presets */
1336         g_object_ref (self);
1337         g_thread_create (presets_loader, self, FALSE, NULL);
1338
1339         hildon_help_dialog_help_enable (GTK_DIALOG(self), "applications_email_wizardwelcome",
1340                                         modest_maemo_utils_get_osso_context()); 
1341
1342         priv->settings = modest_account_settings_new ();
1343 }
1344
1345 ModestEasysetupWizardDialog*
1346 modest_easysetup_wizard_dialog_new (void)
1347 {       
1348         
1349         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1350 }
1351
1352 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1353 {
1354         ModestEasysetupWizardDialogPrivate *priv;
1355         GtkNotebook *notebook = NULL;
1356
1357         g_object_get (self, "wizard-notebook", &notebook, NULL);
1358         g_assert(notebook);
1359
1360         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1361         
1362         /* Create the custom pages: */
1363         if(!(priv->page_custom_incoming)) {
1364                 priv->page_custom_incoming = create_page_custom_incoming (self);
1365         }
1366                 
1367         if(!(priv->page_custom_outgoing)) {
1368                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1369         }
1370         
1371         if(!(priv->page_complete_customsetup)) {
1372                 priv->page_complete_customsetup = create_page_complete_custom (self);
1373         }
1374         
1375         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1376                 gtk_notebook_append_page (notebook, priv->page_custom_incoming,
1377                                           gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
1378         
1379         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))           
1380                 gtk_notebook_append_page (notebook, priv->page_custom_outgoing,
1381                                           gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
1382                 
1383         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup))) {
1384                 gtk_notebook_append_page (notebook, priv->page_complete_customsetup,
1385                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1386                 gtk_container_child_set (GTK_CONTAINER (notebook), 
1387                                          priv->page_complete_customsetup,
1388                                          "tab-expand", TRUE,
1389                                          "tab-fill", TRUE,
1390                                          NULL);
1391         }
1392                         
1393         /* This is unnecessary with GTK+ 2.10: */
1394         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1395 }
1396         
1397 static void 
1398 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1399 {
1400         ModestEasysetupWizardDialogPrivate *priv;
1401         GtkNotebook *notebook = NULL;
1402
1403         g_object_get (self, "wizard-notebook", &notebook, NULL);
1404         g_assert(notebook);
1405         
1406         /* Create the easysetup-specific pages: */
1407         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1408         if(!priv->page_complete_easysetup)
1409                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1410
1411         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup))) {
1412                 gtk_notebook_append_page (notebook, priv->page_complete_easysetup, 
1413                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1414                 gtk_container_child_set (GTK_CONTAINER (notebook),
1415                                          priv->page_complete_easysetup,
1416                                          "tab-expand", TRUE,
1417                                          "tab-fill", TRUE,
1418                                          NULL);
1419         }
1420                         
1421         /* This is unnecessary with GTK+ 2.10: */
1422         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1423 }
1424 /* After the user details page,
1425  * the following pages depend on whether "Other" was chosen 
1426  * in the provider combobox on the account page
1427  */
1428 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
1429 {
1430         ModestEasysetupWizardDialogPrivate *priv;
1431
1432         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1433
1434         if (easysetup_provider_combo_box_get_active_provider_id (
1435                     EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider)) == 0) {
1436                 /* "Other..." was selected: */
1437                 
1438                 /* Make sure that the easysetup pages do not exist: */
1439                 if(priv->page_complete_easysetup) {
1440                         gtk_widget_destroy (priv->page_complete_easysetup);
1441                         priv->page_complete_easysetup = NULL;
1442                 }
1443                 
1444                 create_subsequent_customsetup_pages (self);
1445         }       
1446         else {
1447                 /* A specific provider was selected: */
1448                 {
1449                         /* Make sure that the custom pages do not exist:
1450                          * Because they will be used if they exist, when creating the account. */
1451                         if(priv->page_custom_incoming) {
1452                                 gtk_widget_destroy (priv->page_custom_incoming);
1453                                 priv->page_custom_incoming = NULL;
1454                                 priv->combo_incoming_servertype = NULL;
1455                                 priv->caption_incoming = NULL;
1456                                 priv->entry_incomingserver = NULL;
1457                                 priv->combo_incoming_security = NULL;
1458                                 priv->checkbox_incoming_auth = NULL;
1459                         }
1460                         
1461                         if(priv->page_custom_outgoing) {
1462                                 gtk_widget_destroy (priv->page_custom_outgoing);
1463                                 priv->page_custom_outgoing = NULL;
1464                                 priv->entry_outgoingserver = NULL;
1465                                 priv->combo_outgoing_security = NULL;
1466                                 priv->combo_outgoing_auth = NULL;
1467                                 priv->checkbox_outgoing_smtp_specific = NULL;
1468                                 priv->button_outgoing_smtp_servers = NULL;
1469                         }
1470                         
1471                         if(priv->page_complete_customsetup) {
1472                                 gtk_widget_destroy (priv->page_complete_customsetup);
1473                                 priv->page_complete_customsetup = NULL;
1474                         }
1475                 }
1476                 
1477                 /* Create the easysetup pages: */
1478                 create_subsequent_easysetup_pages (self);
1479         }
1480 }
1481
1482
1483 static gchar*
1484 util_get_default_servername_from_email_address (const gchar* email_address, ModestTransportStoreProtocol servertype)
1485 {
1486         if (!email_address)
1487                 return NULL;
1488         
1489         gchar* at = g_utf8_strchr (email_address, -1, '@');
1490         if (!at || (g_utf8_strlen (at, -1) < 2))
1491                 return NULL;
1492                 
1493         gchar* domain = g_utf8_next_char (at);
1494         if(!domain)
1495                 return NULL;
1496                 
1497         const gchar* hostname = NULL;
1498         if (servertype == MODEST_PROTOCOL_STORE_POP)
1499                 hostname = "pop";
1500         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
1501                 hostname = "imap";
1502         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
1503                 hostname = "smtp";
1504         
1505         if (!hostname)
1506                 return NULL;
1507                 
1508         return g_strdup_printf ("%s.%s", hostname, domain);
1509 }
1510
1511 static void 
1512 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1513 {
1514         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1515
1516         if (!priv->entry_incomingserver)
1517                 return;
1518                 
1519         /* Set a default domain for the server, based on the email address,
1520          * if no server name was already specified.
1521          */
1522         if (priv->entry_user_email
1523             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1524                 const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
1525                         EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype));
1526                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));
1527                 
1528                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
1529
1530                 /* Do not set the INCOMING_CHANGED flag because of this edit */
1531                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1532                 gtk_entry_set_text (GTK_ENTRY (priv->entry_incomingserver), servername);
1533                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1534
1535                 g_free (servername);
1536         }
1537         
1538         /* Set a default domain for the server, based on the email address,
1539          * if no server name was already specified.
1540          */
1541         if (!priv->entry_outgoingserver)
1542                 return;
1543                 
1544         if (priv->entry_user_email
1545             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1546                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(priv->entry_user_email));
1547                 
1548                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
1549
1550                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1551                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1552                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), servername);
1553                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1554
1555                 g_free (servername);
1556         }
1557 }
1558
1559 static gchar*
1560 get_entered_account_title (ModestEasysetupWizardDialog *self)
1561 {
1562         ModestEasysetupWizardDialogPrivate *priv;
1563         const gchar* account_title;
1564
1565         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1566         account_title = gtk_entry_get_text (GTK_ENTRY (priv->entry_account_title));
1567
1568         if (!account_title || (strlen (account_title) == 0)) {
1569                 return NULL;
1570         } else {
1571                 /* Strip it of whitespace at the start and end: */
1572                 gchar *result = g_strdup (account_title);
1573                 result = g_strstrip (result);
1574                 
1575                 if (!result)
1576                         return NULL;
1577                         
1578                 if (strlen (result) == 0) {
1579                         g_free (result);
1580                         return NULL;    
1581                 }
1582                 
1583                 return result;
1584         }
1585 }
1586
1587 static gboolean
1588 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1589 {
1590         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1591         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1592
1593         /* if are browsing pages previous to the last one, then we have pending settings in
1594          * this wizard */
1595         if (next_page != NULL)
1596                 priv->pending_load_settings = TRUE;
1597         
1598         /* Do extra validation that couldn't be done for every key press,
1599          * either because it was too slow,
1600          * or because it requires interaction:
1601          */
1602         if (current_page == priv->page_account_details) {       
1603                 /* Check that the title is not already in use: */
1604                 gchar* account_title = get_entered_account_title (self);
1605                 if (!account_title)
1606                         return FALSE;
1607                         
1608                 /* Aavoid a clash with an existing display name: */
1609                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1610                         priv->account_manager, account_title);
1611
1612                 if (name_in_use) {
1613                         /* Warn the user via a dialog: */
1614                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1615             
1616                         return FALSE;
1617                 }
1618         }
1619         else if (current_page == priv->page_user_details) {
1620                 /* Check that the email address is valud: */
1621                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1622                 if ((!email_address) || (strlen(email_address) == 0))
1623                         return FALSE;
1624                         
1625                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1626                         /* Warn the user via a dialog: */
1627                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1628                                              
1629                         /* Return focus to the email address entry: */
1630                         gtk_widget_grab_focus (priv->entry_user_email);
1631                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1632
1633                         return FALSE;
1634                 }
1635                 
1636                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1637                 create_subsequent_pages (self);
1638         }
1639         
1640         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1641          * but does not specify how.
1642          */
1643           
1644         if(next_page == priv->page_custom_incoming) {
1645                 set_default_custom_servernames (self);
1646         }
1647         else if (next_page == priv->page_custom_outgoing) {
1648                 set_default_custom_servernames (self);
1649
1650                 /* Check if the server supports secure authentication */
1651                 const ModestConnectionProtocol security_incoming = 
1652                         modest_serversecurity_combo_box_get_active_serversecurity (
1653                                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security));
1654                 if (gtk_toggle_button_get_active (
1655                         GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth))
1656                                 && !modest_protocol_info_is_secure(security_incoming))
1657                 {
1658                                 if (!check_has_supported_auth_methods (self))
1659                                         return FALSE;
1660                 }
1661         }
1662         
1663         /* If this is the last page, and this is a click on Finish, 
1664          * then attempt to create the dialog.
1665          */
1666         if(!next_page) /* This is NULL when this is a click on Finish. */
1667         {
1668                 if (priv->pending_load_settings) {
1669                         save_to_settings (self);
1670                 }
1671                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1672         }
1673         
1674         
1675         return TRUE;
1676 }
1677
1678 static gboolean entry_is_empty (GtkWidget *entry)
1679 {
1680         if (!entry)
1681                 return FALSE;
1682                 
1683         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1684         if ((!text) || (strlen(text) == 0))
1685                 return TRUE;
1686         else {
1687                 /* Strip it of whitespace at the start and end: */
1688                 gchar *stripped = g_strdup (text);
1689                 stripped = g_strstrip (stripped);
1690                 
1691                 if (!stripped)
1692                         return TRUE;
1693                         
1694                 const gboolean result = (strlen (stripped) == 0);
1695                 
1696                 g_free (stripped);
1697                 return result;
1698         }
1699 }
1700
1701 static void
1702 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1703 {
1704         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1705         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1706         
1707         gboolean enable_next = TRUE;
1708         if (current_page == priv->page_welcome) {
1709                 enable_next = TRUE;
1710         }
1711         else if (current_page == priv->page_account_details) {
1712                 /* The account details title is mandatory: */
1713                 if (entry_is_empty(priv->entry_account_title))
1714                         enable_next = FALSE;
1715         }
1716         else if (current_page == priv->page_user_details) {     
1717                 /* The user details username is mandatory: */
1718                 if (entry_is_empty(priv->entry_user_username))
1719                         enable_next = FALSE;
1720                         
1721                 /* The user details email address is mandatory: */
1722                 if (enable_next && entry_is_empty (priv->entry_user_email))
1723                         enable_next = FALSE;
1724         }
1725         else if (current_page == priv->page_custom_incoming) {
1726                 /* The custom incoming server is mandatory: */
1727                 if (entry_is_empty(priv->entry_incomingserver))
1728                         enable_next = FALSE;
1729         }
1730                         
1731         /* Enable the buttons, 
1732          * identifying them via their associated response codes: */
1733                                    
1734         /* Disable the Finish button until we are on the last page,
1735          * because HildonWizardDialog enables this for all but the first page: */
1736         GtkNotebook *notebook = NULL;
1737         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1738         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1739         
1740         gint current = gtk_notebook_get_current_page (notebook);
1741         gint last = gtk_notebook_get_n_pages (notebook) - 1;
1742         const gboolean is_last = (current == last);
1743     
1744         if(!is_last) {
1745                 gtk_dialog_set_response_sensitive (dialog_base,
1746                                                    MODEST_WIZARD_DIALOG_FINISH,
1747                                                    FALSE);
1748         } else
1749         {
1750                 /* Disable Next on the last page: */
1751                 enable_next = FALSE;
1752         }
1753         
1754         gtk_dialog_set_response_sensitive (dialog_base,
1755                                            MODEST_WIZARD_DIALOG_NEXT,
1756                                            enable_next);
1757 }
1758
1759 static void
1760 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1761 {
1762         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1763         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1764
1765
1766         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1767         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1768         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1769         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1770         
1771         /* Provide a vfunc implementation so we can decide 
1772          * when to enable/disable the prev/next buttons.
1773          */
1774         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1775         base_klass->before_next = on_before_next;
1776         base_klass->enable_buttons = on_enable_buttons;
1777 }
1778  
1779 static void
1780 show_error (GtkWidget *parent_widget, const gchar* text)
1781 {
1782         //TODO: Apparently this doesn't show anything in Maemo Bora:
1783         hildon_banner_show_information(parent_widget, NULL, text);      
1784 }
1785
1786
1787 /**
1788  * save_to_settings:
1789  * @self: a #ModestEasysetupWizardDialog
1790  *
1791  * takes information from all the wizard and stores it in settings
1792  */
1793 static void
1794 save_to_settings (ModestEasysetupWizardDialog *self)
1795 {
1796         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1797         guint special_port;
1798         gchar *provider_id;
1799         gchar* display_name;
1800         const gchar *username, *password;
1801         gchar *store_hostname, *transport_hostname;
1802         guint store_port, transport_port;
1803         ModestTransportStoreProtocol store_protocol, transport_protocol;
1804         ModestConnectionProtocol store_security, transport_security;
1805         ModestAuthProtocol store_auth_protocol, transport_auth_protocol;
1806         ModestServerAccountSettings *store_settings, *transport_settings;
1807         const gchar *fullname, *email_address;
1808
1809         /* Get details from the specified presets: */
1810         provider_id = easysetup_provider_combo_box_get_active_provider_id (
1811                 EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider));
1812                 
1813         /* username and password (for both incoming and outgoing): */
1814         username = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username));
1815         password = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password));
1816
1817         /* Incoming server: */
1818         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
1819          * or modest_account_mgr_add_server_account will fail. */
1820         store_port = 0;
1821         store_protocol = MODEST_PROTOCOL_STORE_POP;
1822         store_security = MODEST_PROTOCOL_CONNECTION_NORMAL;
1823         store_auth_protocol = MODEST_PROTOCOL_AUTH_NONE;
1824
1825         if (provider_id) {
1826                 ModestPresetsServerType store_provider_server_type;
1827                 ModestPresetsSecurity store_provider_security;
1828                 /* Use presets: */
1829                 store_hostname = modest_presets_get_server (priv->presets, provider_id, 
1830                                                             TRUE /* store */);
1831                 
1832                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
1833                                                                          provider_id, 
1834                                                                          TRUE /* store */);
1835                 store_provider_security  = modest_presets_get_info_server_security (priv->presets,
1836                                                                                     provider_id, 
1837                                                                                     TRUE /* store */);
1838
1839                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1840                 if (store_provider_server_type == MODEST_PRESETS_SERVER_TYPE_IMAP)
1841                         store_protocol = MODEST_PROTOCOL_STORE_IMAP;
1842                 else if (store_provider_server_type == MODEST_PRESETS_SERVER_TYPE_POP)
1843                         store_protocol = MODEST_PROTOCOL_STORE_POP;
1844                 else /* fallback */
1845                         store_protocol = MODEST_PROTOCOL_STORE_POP;
1846
1847                 /* we check if there is a *special* port */
1848                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
1849                 if (special_port != 0)
1850                         store_port = special_port;
1851                 else 
1852                         store_port = get_serverport_incoming(store_provider_server_type, store_provider_security);
1853                 
1854                 if (store_provider_security & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1855                         store_security = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1856                 
1857                 if (store_provider_security & MODEST_PRESETS_SECURITY_APOP)
1858                         store_auth_protocol = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1859         } else {
1860                 /* Use custom pages because no preset was specified: */
1861                 store_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_incomingserver) ));               
1862                 store_protocol = easysetup_servertype_combo_box_get_active_servertype (
1863                         EASYSETUP_SERVERTYPE_COMBO_BOX (priv->combo_incoming_servertype));              
1864                 store_security = modest_serversecurity_combo_box_get_active_serversecurity (
1865                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_incoming_security));
1866                 
1867                 /* The UI spec says: 
1868                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1869                  * If secure authentication is checked, require one of the secure methods during 
1870                  * connection: SSL, TLS, CRAM-MD5 etc. */
1871                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->checkbox_incoming_auth)) &&
1872                     !modest_protocol_info_is_secure(store_security)) {
1873                         store_auth_protocol = check_first_supported_auth_method (self);
1874                 } else {
1875                         store_auth_protocol = MODEST_PROTOCOL_AUTH_PASSWORD;
1876                 }
1877         }
1878
1879         /* now we store the store account settings */
1880         store_settings = modest_account_settings_get_store_settings (priv->settings);
1881         modest_server_account_settings_set_hostname (store_settings, store_hostname);
1882         modest_server_account_settings_set_username (store_settings, username);
1883         modest_server_account_settings_set_password (store_settings, password);
1884         modest_server_account_settings_set_protocol (store_settings, store_protocol);
1885         modest_server_account_settings_set_security (store_settings, store_security);
1886         modest_server_account_settings_set_auth_protocol (store_settings, store_auth_protocol);
1887         if (store_port != 0)
1888                 modest_server_account_settings_set_port (store_settings, store_port);
1889
1890         g_object_unref (store_settings);
1891         g_free (store_hostname);
1892         
1893         /* Outgoing server: */
1894         transport_hostname = NULL;
1895         transport_protocol = MODEST_PROTOCOL_STORE_POP;
1896         transport_security = MODEST_PROTOCOL_CONNECTION_NORMAL;
1897         transport_auth_protocol = MODEST_PROTOCOL_AUTH_NONE;
1898         transport_port = 0;
1899         
1900         if (provider_id) {
1901                 ModestPresetsServerType transport_provider_server_type;
1902                 ModestPresetsSecurity transport_provider_security;
1903
1904                 /* Use presets: */
1905                 transport_hostname = modest_presets_get_server (priv->presets, provider_id, 
1906                                                                 FALSE /* transport */);
1907                         
1908                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
1909                                                                                       provider_id, 
1910                                                                                       FALSE /* transport */);           
1911                 transport_provider_security = modest_presets_get_info_server_security (priv->presets, 
1912                                                                                        provider_id, 
1913                                                                                        FALSE /* transport */);
1914
1915                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1916                 transport_protocol = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1917                 if (transport_provider_server_type == MODEST_PRESETS_SERVER_TYPE_SMTP)
1918                         transport_protocol = MODEST_PROTOCOL_TRANSPORT_SMTP;
1919
1920                 transport_security = MODEST_PROTOCOL_CONNECTION_NORMAL;
1921                 if (transport_provider_security & MODEST_PRESETS_SECURITY_SECURE_SMTP) {
1922                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
1923                         transport_security = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1924                         /* we check if there is a *special* port */
1925                         special_port = modest_presets_get_port (priv->presets, provider_id,
1926                                                                 FALSE /* transport */);
1927                         if (special_port != 0)
1928                                 transport_port = special_port;
1929                         else 
1930                                 transport_port = 465;
1931                         transport_auth_protocol = MODEST_PROTOCOL_AUTH_PASSWORD;
1932                 } else {
1933                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
1934                         transport_auth_protocol = MODEST_PROTOCOL_AUTH_NONE;
1935                 }
1936         } else {
1937                 /* Use custom pages because no preset was specified: */
1938                 transport_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver) ));
1939                 transport_protocol = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1940                 transport_security = modest_serversecurity_combo_box_get_active_serversecurity (
1941                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));               
1942                 transport_auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
1943                         MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
1944         }
1945             
1946         /* now we transport the transport account settings */
1947         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1948         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
1949         modest_server_account_settings_set_username (transport_settings, username);
1950         modest_server_account_settings_set_password (transport_settings, password);
1951         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
1952         modest_server_account_settings_set_security (transport_settings, transport_security);
1953         modest_server_account_settings_set_auth_protocol (transport_settings, transport_auth_protocol);
1954         if (transport_port != 0)
1955                 modest_server_account_settings_set_port (transport_settings, transport_port);
1956
1957         g_object_unref (transport_settings);
1958         g_free (transport_hostname);
1959         
1960         fullname = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_name));
1961         email_address = gtk_entry_get_text (GTK_ENTRY (priv->entry_user_email));
1962         modest_account_settings_set_fullname (priv->settings, fullname);
1963         modest_account_settings_set_email_address (priv->settings, email_address);
1964         /* we don't set retrieve type to preserve advanced settings if any. By default account settings
1965            are set to headers only */
1966         
1967         /* Save the connection-specific SMTP server accounts. */
1968         modest_account_settings_set_use_connection_specific_smtp 
1969                 (priv->settings, 
1970                  gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox_outgoing_smtp_specific)));
1971
1972         display_name = get_entered_account_title (self);
1973         modest_account_settings_set_display_name (priv->settings, display_name);
1974         g_free (display_name);
1975
1976         if (priv->specific_window)
1977                 modest_connection_specific_smtp_window_save_server_accounts (
1978                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (priv->specific_window));
1979 }
1980