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