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