2007-07-05 Johannes Schmid <johannes.schmid@openismus.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 GtkWidget*
362 create_page_account_details (ModestEasysetupWizardDialog *self)
363 {
364         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
365         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
366         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
367         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
368         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
369         gtk_widget_show (label);
370         
371         /* Create a size group to be used by all captions.
372          * Note that HildonCaption does not create a default size group if we do not specify one.
373          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
374         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
375
376         /* The country widgets: */
377         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
378         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_country"), 
379                                                               self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
380         gtk_widget_show (self->combo_account_country);
381         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
382         gtk_widget_show (caption);
383         
384         /* connect to country combo's changed signal, so we can fill the provider combo: */
385         g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
386                           G_CALLBACK (on_combo_account_country), self);
387             
388         GtkWidget *separator = gtk_hseparator_new ();
389         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
390         gtk_widget_show (separator);
391             
392         /* The service provider widgets: */     
393         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
394         
395         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_serviceprovider"), 
396                                                    self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
397         gtk_widget_show (self->combo_account_serviceprovider);
398         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
399         gtk_widget_show (caption);
400         
401         /* connect to providers combo's changed signal, so we can fill the email address: */
402         g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
403                           G_CALLBACK (on_combo_account_serviceprovider), self);
404         
405         /* TODO: Default to the current country somehow.
406          * But I don't know how to get the information that is specified in the 
407          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
408          *
409          * This is probably not the best choice of gconf key:
410          * This is the  "mcc used in the last pairing", ie. the last connection you made.
411          * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
412          *
413          */
414         GConfClient *client = gconf_client_get_default ();
415         GError *error = NULL;
416         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
417         gint mcc_id = gconf_client_get_int(client, key, &error);
418         
419         if(mcc_id < 0)
420                 mcc_id = 0;
421      
422         if (error) {
423                 g_warning ("Error getting gconf key %s:\n%s", key, error->message);
424                 g_error_free (error);
425                 error = NULL;
426         
427                 mcc_id = 0;
428         }
429     
430         /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
431          * This might just be a Maemo bug.
432          */
433         if (mcc_id == 0) 
434         {
435                 /* For now, we default to Finland when there is nothing better: */
436                 mcc_id = 244;
437         }
438    
439         easysetup_country_combo_box_set_active_country_id (
440                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
441                 
442         
443         /* The description widgets: */  
444         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
445         g_signal_connect(G_OBJECT(self->entry_account_title), "changed",
446                                                                          G_CALLBACK(on_easysetup_changed), self);
447         /* Do use auto-capitalization: */
448         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
449                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
450         
451         /* Set a default account title, choosing one that does not already exist: */
452         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
453         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
454         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
455                 self->account_manager, default_account_name_start);
456         g_free (default_account_name_start);
457         default_account_name_start = NULL;
458         
459         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
460         g_free (default_account_name);
461         default_account_name = NULL;
462
463         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
464                                                    self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
465         gtk_widget_show (self->entry_account_title);
466         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
467         gtk_widget_show (caption);
468         
469         /* Prevent the use of some characters in the account title, 
470          * as required by our UI specification: */
471         GList *list_prevent = NULL;
472         list_prevent = g_list_append (list_prevent, "\\");
473         list_prevent = g_list_append (list_prevent, "/");
474         list_prevent = g_list_append (list_prevent, ":");
475         list_prevent = g_list_append (list_prevent, "*");
476         list_prevent = g_list_append (list_prevent, "?");
477         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
478         list_prevent = g_list_append (list_prevent, "“");
479         list_prevent = g_list_append (list_prevent, "<"); 
480         list_prevent = g_list_append (list_prevent, ">"); 
481         list_prevent = g_list_append (list_prevent, "|");
482         list_prevent = g_list_append (list_prevent, "^");       
483         modest_validating_entry_set_unallowed_characters (
484                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
485         g_list_free (list_prevent);
486         
487         /* Set max length as in the UI spec:
488          * The UI spec seems to want us to show a dialog if we hit the maximum. */
489         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
490         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
491                                               on_entry_max, self);
492         
493         gtk_widget_show (GTK_WIDGET (box));
494         
495         return GTK_WIDGET (box);
496 }
497
498 static GtkWidget*
499 create_page_user_details (ModestEasysetupWizardDialog *self)
500 {
501         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
502         
503         /* Create a size group to be used by all captions.
504          * Note that HildonCaption does not create a default size group if we do not specify one.
505          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
506         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
507          
508         /* The name widgets: */
509         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
510         /* Auto-capitalization is the default, so let's turn it off: */
511         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
512         /* Set max length as in the UI spec:
513          * The UI spec seems to want us to show a dialog if we hit the maximum. */
514         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
515         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
516                                               on_entry_max, self);
517         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
518                                                               _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
519         g_signal_connect(G_OBJECT(self->entry_user_name), "changed", 
520                                                                          G_CALLBACK(on_easysetup_changed), self);
521         gtk_widget_show (self->entry_user_name);
522         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
523         gtk_widget_show (caption);
524         
525         /* Prevent the use of some characters in the name, 
526          * as required by our UI specification: */
527         GList *list_prevent = NULL;
528         list_prevent = g_list_append (list_prevent, "<");
529         list_prevent = g_list_append (list_prevent, ">");
530         modest_validating_entry_set_unallowed_characters (
531                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
532         g_list_free (list_prevent);
533         
534         /* The username widgets: */     
535         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
536         /* Auto-capitalization is the default, so let's turn it off: */
537         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
538         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
539                                                    self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
540         gtk_widget_show (self->entry_user_username);
541         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
542         g_signal_connect(G_OBJECT(self->entry_user_username), "changed", 
543                                                                          G_CALLBACK(on_easysetup_changed), self);
544         gtk_widget_show (caption);
545         
546         /* Prevent the use of some characters in the username, 
547          * as required by our UI specification: */
548         modest_validating_entry_set_unallowed_characters_whitespace (
549                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
550         
551         /* Set max length as in the UI spec:
552          * The UI spec seems to want us to show a dialog if we hit the maximum. */
553         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
554         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
555                                               on_entry_max, self);
556         
557         /* The password widgets: */     
558         self->entry_user_password = gtk_entry_new ();
559         /* Auto-capitalization is the default, so let's turn it off: */
560         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
561                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
562         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
563         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
564         caption = create_caption_new_with_asterisk (self, sizegroup, 
565                                                    _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
566         g_signal_connect(G_OBJECT(self->entry_user_password), "changed", 
567                                                                          G_CALLBACK(on_easysetup_changed), self);
568         gtk_widget_show (self->entry_user_password);
569         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
570         gtk_widget_show (caption);
571         
572         /* The email address widgets: */        
573         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
574         /* Auto-capitalization is the default, so let's turn it off: */
575         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
576         caption = create_caption_new_with_asterisk (self, sizegroup, 
577                                                    _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
578         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
579         gtk_widget_show (self->entry_user_email);
580         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
581         g_signal_connect(G_OBJECT(self->entry_user_email), "changed", 
582                                                                          G_CALLBACK(on_easysetup_changed), self);
583         gtk_widget_show (caption);
584         
585         /* Set max length as in the UI spec:
586          * The UI spec seems to want us to show a dialog if we hit the maximum. */
587         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
588         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
589                                               on_entry_max, self);
590         
591         
592         gtk_widget_show (GTK_WIDGET (box));
593         
594         return GTK_WIDGET (box);
595 }
596
597 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
598 {
599         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
600         
601         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
602         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
603         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
604         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
605         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
606         gtk_widget_show (label);
607         
608         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
609         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
610         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
611         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
612         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
613         gtk_widget_show (label);
614         
615         gtk_widget_show (GTK_WIDGET (box));
616         return GTK_WIDGET (box);
617 }
618
619 /** Change the caption title for the incoming server, 
620  * as specified in the UI spec:
621  */
622 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
623 {
624         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
625                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
626         const gchar* type = 
627                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
628                  _("mail_fi_emailtype_pop3") : 
629                  _("mail_fi_emailtype_imap") );
630                         
631                 
632         /* Note that this produces a compiler warning, 
633          * because the compiler does not know that the translated string will have a %s in it.
634          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
635         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
636         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
637         g_free(incomingserver_title);
638 }
639
640 /** Change the caption title for the incoming server, 
641  * as specified in the UI spec:
642  */
643 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
644 {
645         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
646                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
647         
648         /* Fill the combo with appropriately titled choices for POP or IMAP. */
649         /* The choices are the same, but the titles are different, as in the UI spec. */
650         modest_serversecurity_combo_box_fill (
651                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
652 }
653
654 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
655 {
656         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
657         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(self);
658         
659         priv->dirty = TRUE;
660         
661         update_incoming_server_title (self);
662         update_incoming_server_security_choices (self);
663
664         set_default_custom_servernames (self);
665 }
666
667 static void on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
668 {
669         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
670         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
671         priv->dirty = TRUE;
672         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
673 }
674
675 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
676 {
677         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
678
679         /* Show note that account type cannot be changed in future: */
680         GtkWidget *label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
681         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
682         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
683         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
684         gtk_widget_show (label);
685         
686         /* Create a size group to be used by all captions.
687          * Note that HildonCaption does not create a default size group if we do not specify one.
688          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
689         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
690          
691         /* The incoming server widgets: */
692         if (!self->combo_incoming_servertype)
693                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
694         easysetup_servertype_combo_box_set_active_servertype (
695                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
696         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
697                                                               _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
698         gtk_widget_show (self->combo_incoming_servertype);
699         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
700         gtk_widget_show (caption);
701         
702         if(!self->entry_incomingserver)
703         {
704                 self->entry_incomingserver = gtk_entry_new ();
705                 g_signal_connect(G_OBJECT(self->entry_incomingserver), "changed",
706                                                                                  G_CALLBACK(on_easysetup_changed), self);
707         }
708         /* Auto-capitalization is the default, so let's turn it off: */
709         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
710         set_default_custom_servernames (self);
711
712         if (self->caption_incoming)
713                 gtk_widget_destroy (self->caption_incoming);
714            
715         /* The caption title will be updated in update_incoming_server_title().
716          * so this default text will never be seen: */
717         /* (Note: Changing the title seems pointless. murrayc) */
718         self->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
719                                                                   "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
720         update_incoming_server_title (self);
721         gtk_widget_show (self->entry_incomingserver);
722         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
723         gtk_widget_show (self->caption_incoming);
724         
725         /* Change the caption title when the servertype changes, 
726          * as in the UI spec: */
727         g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
728                           G_CALLBACK (on_combo_servertype_changed), self);
729
730         /* Remember when the servername was changed manually: */
731         g_signal_connect (G_OBJECT (self->entry_incomingserver), "changed",
732                           G_CALLBACK (on_entry_incoming_servername_changed), self);
733
734         /* The secure connection widgets: */    
735         if (!self->combo_incoming_security)
736                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
737         update_incoming_server_security_choices (self);
738         modest_serversecurity_combo_box_set_active_serversecurity (
739                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
740         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
741                                       self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
742         g_signal_connect (G_OBJECT (self->combo_incoming_security), "changed",
743                           G_CALLBACK (on_easysetup_changed), self);
744         gtk_widget_show (self->combo_incoming_security);
745         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
746         gtk_widget_show (caption);
747         
748         if(!self->checkbox_incoming_auth)
749         {
750                 self->checkbox_incoming_auth = gtk_check_button_new ();
751                 g_signal_connect (G_OBJECT (self->checkbox_incoming_auth), "toggled",
752                           G_CALLBACK (on_easysetup_changed), self);
753         }
754         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
755                                       self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
756         
757         gtk_widget_show (self->checkbox_incoming_auth);
758         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
759         gtk_widget_show (caption);
760         
761         gtk_widget_show (GTK_WIDGET (box));
762         
763         return GTK_WIDGET (box);
764 }
765
766 static void
767 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
768 {
769         GtkWidget *widget = GTK_WIDGET (user_data);
770         
771         /* Enable the widget only if the toggle button is active: */
772         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
773         gtk_widget_set_sensitive (widget, enable);
774 }
775
776 /* Make the sensitivity of a widget depend on a toggle button.
777  */
778 static void
779 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
780 {
781         g_signal_connect (G_OBJECT (button), "toggled",
782                           G_CALLBACK (on_toggle_button_changed), widget);
783         
784         /* Set the starting sensitivity: */
785         on_toggle_button_changed (button, widget);
786 }
787
788 static void
789 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
790 {
791         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
792         ModestEasysetupWizardDialogPrivate* priv = WIZARD_DIALOG_GET_PRIVATE(self);
793         
794         /* We set dirty here because setting it depending on the connection specific dialog
795         seems overkill */
796         priv->dirty = TRUE;
797         
798         /* Create the window, if necessary: */
799         if (!(self->specific_window)) {
800                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
801                 modest_connection_specific_smtp_window_fill_with_connections (
802                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
803                         NULL /* account_name, not known yet. */);
804         }
805
806         /* Show the window: */
807         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
808         gtk_widget_show (self->specific_window);
809 }
810
811 static void on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
812 {
813         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
814         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
815         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
816 }
817
818 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
819 {
820         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
821         
822         /* Create a size group to be used by all captions.
823          * Note that HildonCaption does not create a default size group if we do not specify one.
824          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
825         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
826          
827         /* The outgoing server widgets: */
828         if (!self->entry_outgoingserver)
829         {
830                 self->entry_outgoingserver = gtk_entry_new ();
831                 g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
832                           G_CALLBACK (on_easysetup_changed), self);
833         }
834         /* Auto-capitalization is the default, so let's turn it off: */
835         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
836         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
837                                                               _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
838         gtk_widget_show (self->entry_outgoingserver);
839         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
840         gtk_widget_show (caption);
841         set_default_custom_servernames (self);
842         
843         /* The secure connection widgets: */    
844         if (!self->combo_outgoing_security)
845         {
846                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
847                 g_signal_connect (G_OBJECT (self->combo_outgoing_security), "changed",
848                           G_CALLBACK (on_easysetup_changed), self);
849         }
850         modest_serversecurity_combo_box_fill (
851                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
852         modest_serversecurity_combo_box_set_active_serversecurity (
853                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
854         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
855                                       self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
856         gtk_widget_show (self->combo_outgoing_security);
857         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
858         gtk_widget_show (caption);
859         
860         /* The secure authentication widgets: */
861         if (!self->combo_outgoing_auth)
862         {
863                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
864                                 g_signal_connect (G_OBJECT (self->combo_outgoing_auth), "changed",
865                           G_CALLBACK (on_easysetup_changed), self);
866         }
867         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
868                                       self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
869         gtk_widget_show (self->combo_outgoing_auth);
870         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
871         gtk_widget_show (caption);
872         
873         GtkWidget *separator = gtk_hseparator_new ();
874         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
875         gtk_widget_show (separator);
876         
877         /* connection-specific checkbox: */
878         if (!self->checkbox_outgoing_smtp_specific) {
879                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
880                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
881                                               FALSE);
882                 g_signal_connect (G_OBJECT (self->checkbox_outgoing_smtp_specific), "toggled",
883                           G_CALLBACK (on_easysetup_changed), self);
884
885         }
886         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
887                                       self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
888         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
889         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
890         gtk_widget_show (caption);
891         
892         /* Connection-specific SMTP-Severs Edit button: */
893         if (!self->button_outgoing_smtp_servers)
894                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
895         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
896                                       self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
897         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
898         gtk_widget_show (self->button_outgoing_smtp_servers);
899         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
900         gtk_widget_show (caption);
901         
902         /* Only enable the button when the checkbox is checked: */
903         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
904                                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
905                 
906         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
907                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
908
909         g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
910                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
911         
912         
913         gtk_widget_show (GTK_WIDGET (box));
914         
915         return GTK_WIDGET (box);
916 }
917
918 static gboolean
919 show_advanced_edit(gpointer user_data)
920 {
921         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
922         
923         if (!(self->saved_account_name))
924                 return FALSE;
925         
926         /* Show the Account Settings window: */
927         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
928         modest_account_settings_dialog_set_account_name (dialog, self->saved_account_name);
929         
930         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
931         
932         gtk_dialog_run (GTK_DIALOG (dialog));
933
934         gtk_widget_destroy (GTK_WIDGET (dialog));
935         
936         return FALSE; /* Do not call this timeout callback again. */
937 }
938
939 static void
940 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
941 {
942         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
943         
944         /* Save the new account, so we can edit it with ModestAccountSettingsDialog, 
945          * without recoding it to use non-gconf information.
946          * This account will be deleted if Finish is never actually clicked. */
947
948         gboolean saved = TRUE;
949         if (!(self->saved_account_name)) {
950                 saved = create_account (self, FALSE);
951         }
952                 
953         if (!saved)
954                 return;
955                 
956         if (!(self->saved_account_name))
957                 return;
958         
959         /* Show the Account Settings window: */
960         show_advanced_edit(self);
961 }
962 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
963 {
964         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
965         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
966         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
967         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
968         gtk_widget_show (label);
969         
970         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
971         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
972         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
973         gtk_widget_show (label);
974         
975         if (!self->button_edit)
976                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
977         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
978                                                  self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
979         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
980         gtk_widget_show (self->button_edit);
981         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
982         gtk_widget_show (caption);
983         
984         g_signal_connect (G_OBJECT (self->button_edit), "clicked", 
985                           G_CALLBACK (on_button_edit_advanced_settings), self);
986         
987         gtk_widget_show (GTK_WIDGET (box));
988         return GTK_WIDGET (box);
989 }
990
991
992 /*
993  */
994 static void 
995 on_response (ModestWizardDialog *wizard_dialog,
996              gint response_id,
997              gpointer user_data)
998 {
999         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1000         if (response_id == GTK_RESPONSE_CANCEL) {
1001                 /* Remove any temporarily-saved account that will not actually be needed: */
1002                 if (self->saved_account_name) {
1003                         modest_account_mgr_remove_account (self->account_manager,
1004                                                            self->saved_account_name, FALSE);
1005                 }
1006         }
1007
1008         invoke_enable_buttons_vfunc (self);
1009 }
1010
1011 static void 
1012 on_response_before (ModestWizardDialog *wizard_dialog,
1013                     gint response_id,
1014                     gpointer user_data)
1015 {
1016         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1017         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
1018         if (response_id == GTK_RESPONSE_CANCEL) {
1019                 /* This is mostly copied from
1020                  * src/maemo/modest-account-settings-dialog.c */
1021                 if (priv->dirty)
1022                 {
1023                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self), 
1024                                 _("imum_nc_wizard_confirm_lose_changes")));
1025                         /* TODO: These button names will be ambiguous, and not specified in the UI specification. */
1026
1027                         const gint dialog_response = gtk_dialog_run (dialog);
1028                         gtk_widget_destroy (GTK_WIDGET (dialog));
1029
1030                         if (dialog_response != GTK_RESPONSE_OK)
1031                         {
1032                                 /* This is a nasty hack. murrayc. */
1033                                 /* Don't let the dialog close */
1034                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1035                         }
1036                 }
1037         }
1038 }
1039
1040 static void
1041 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1042 {
1043         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1044         
1045         /* Create the notebook to be used by the ModestWizardDialog base class:
1046          * Each page of the notebook will be a page of the wizard: */
1047         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1048         
1049         /* Set the notebook used by the ModestWizardDialog base class: */
1050         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1051     
1052         /* Set the wizard title:
1053          * The actual window title will be a combination of this and the page's tab label title. */
1054         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1055
1056         /* Read in the information about known service providers: */
1057         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1058         
1059         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
1060         priv->presets = modest_presets_new (filepath);
1061         if (!(priv->presets)) {
1062                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
1063         }
1064         
1065         g_assert(priv->presets);
1066
1067         /* The server fields did not have been manually changed yet */
1068         priv->server_changes = 0;
1069
1070         /* Get the account manager object, 
1071          * so we can check for existing accounts,
1072          * and create new accounts: */
1073         self->account_manager = modest_runtime_get_account_mgr ();
1074         g_assert (self->account_manager);
1075         g_object_ref (self->account_manager);
1076         
1077         /* Create the common pages, 
1078          */
1079         self->page_welcome = create_page_welcome (self);
1080         self->page_account_details = create_page_account_details (self);
1081         self->page_user_details = create_page_user_details (self);
1082         
1083         /* Add the common pages: */
1084         gtk_notebook_append_page (notebook, self->page_welcome, 
1085                                   gtk_label_new (_("mcen_ti_emailsetup_welcome")));
1086         gtk_notebook_append_page (notebook, self->page_account_details, 
1087                                   gtk_label_new (_("mcen_ti_accountdetails")));
1088         gtk_notebook_append_page (notebook, self->page_user_details, 
1089                                   gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
1090                 
1091         /* Create and add the easysetup-specific pages,
1092          * because we need _some_ final page to enable the Next and Finish buttons: */
1093         create_subsequent_easysetup_pages (self);
1094
1095         /* Connect to the dialog's response signal so we can enable/disable buttons 
1096          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1097          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1098          * so that we can be enable/disable after ModestWizardDialog has done its own 
1099          * enabling/disabling of buttons.
1100          * 
1101          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
1102          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1103          * 
1104          * It's not enough to connect to the notebook's switch-page signal, because 
1105          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
1106          * _after_ switching the page (understandably).
1107          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1108          * then gtk_notebook_get_current_page() would return an incorrect value.)
1109          */
1110         g_signal_connect_after (G_OBJECT (self), "response",
1111                                 G_CALLBACK (on_response), self);
1112
1113         /* This is to show a confirmation dialog when the user hits cancel */
1114         g_signal_connect (G_OBJECT (self), "response",
1115                           G_CALLBACK (on_response_before), self);
1116
1117         /* Reset dirty, because there was no user input until now */
1118         priv->dirty = FALSE;
1119         
1120         /* When this window is shown, hibernation should not be possible, 
1121          * because there is no sensible way to save the state: */
1122         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1123                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
1124 }
1125
1126 ModestEasysetupWizardDialog*
1127 modest_easysetup_wizard_dialog_new (void)
1128 {
1129         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1130 }
1131
1132 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1133 {
1134         GtkNotebook *notebook = NULL;
1135         g_object_get (self, "wizard-notebook", &notebook, NULL);
1136         g_assert(notebook);
1137         
1138         /* Create the custom pages: */
1139         if(!(self->page_custom_incoming)) {
1140                 self->page_custom_incoming = create_page_custom_incoming (self);
1141         }
1142                 
1143         if(!(self->page_custom_outgoing)) {
1144                 self->page_custom_outgoing = create_page_custom_outgoing (self);
1145         }
1146         
1147         if(!(self->page_complete_customsetup)) {
1148                 self->page_complete_customsetup = create_page_complete_custom (self);
1149         }
1150         
1151         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
1152                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
1153                                           gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
1154         
1155         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
1156                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
1157                                           gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
1158                 
1159         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
1160                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
1161                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1162                         
1163         /* This is unnecessary with GTK+ 2.10: */
1164         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1165 }
1166         
1167 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1168 {
1169         GtkNotebook *notebook = NULL;
1170         g_object_get (self, "wizard-notebook", &notebook, NULL);
1171         g_assert(notebook);
1172         
1173         /* Create the easysetup-specific pages: */
1174         if(!self->page_complete_easysetup)
1175                 self->page_complete_easysetup = create_page_complete_easysetup (self);
1176
1177         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
1178                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
1179                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1180                         
1181         /* This is unnecessary with GTK+ 2.10: */
1182         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1183 }
1184 /* After the user details page,
1185  * the following pages depend on whether "Other" was chosen 
1186  * in the provider combobox on the account page
1187  */
1188 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
1189 {
1190         if (easysetup_provider_combo_box_get_active_provider_id (
1191                     EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
1192                 /* "Other..." was selected: */
1193                 
1194                 /* Make sure that the easysetup pages do not exist: */
1195                 if(self->page_complete_easysetup) {
1196                         gtk_widget_destroy (self->page_complete_easysetup);
1197                         self->page_complete_easysetup = NULL;
1198                 }
1199                 
1200                 create_subsequent_customsetup_pages (self);
1201         }       
1202         else {
1203                 /* A specific provider was selected: */
1204                 {
1205                         /* Make sure that the custom pages do not exist:
1206                          * Because they will be used if they exist, when creating the account. */
1207                         if(self->page_custom_incoming) {
1208                                 gtk_widget_destroy (self->page_custom_incoming);
1209                                 self->page_custom_incoming = NULL;
1210                         }
1211                         
1212                         if(self->page_custom_outgoing) {
1213                                 gtk_widget_destroy (self->page_custom_outgoing);
1214                                 self->page_custom_outgoing = NULL;
1215                         }
1216                         
1217                         if(self->page_complete_customsetup) {
1218                                 gtk_widget_destroy (self->page_complete_customsetup);
1219                                 self->page_complete_customsetup = NULL;
1220                         }
1221                 }
1222                 
1223                 /* Create the easysetup pages: */
1224                 create_subsequent_easysetup_pages (self);
1225         }
1226 }
1227
1228
1229 static gchar*
1230 util_get_default_servername_from_email_address (const gchar* email_address, ModestTransportStoreProtocol servertype)
1231 {
1232         if (!email_address)
1233                 return NULL;
1234         
1235         gchar* at = g_utf8_strchr (email_address, -1, '@');
1236         if (!at || (g_utf8_strlen (at, -1) < 2))
1237                 return NULL;
1238                 
1239         gchar* domain = g_utf8_next_char (at);
1240         if(!domain)
1241                 return NULL;
1242                 
1243         const gchar* hostname = NULL;
1244         if (servertype == MODEST_PROTOCOL_STORE_POP)
1245                 hostname = "pop";
1246         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
1247                 hostname = "imap";
1248         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
1249                 hostname = "smtp";
1250         
1251         if (!hostname)
1252                 return NULL;
1253                 
1254         return g_strdup_printf ("%s.%s", hostname, domain);
1255 }
1256
1257 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
1258 {
1259         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(account_wizard);
1260
1261         if (!account_wizard->entry_incomingserver)
1262                 return;
1263                 
1264         /* Set a default domain for the server, based on the email address,
1265          * if no server name was already specified.
1266          */
1267         if (account_wizard->entry_user_email
1268             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1269                 const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
1270                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1271                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1272                 
1273                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
1274
1275                 /* Do not set the INCOMING_CHANGED flag because of this edit */
1276                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1277                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
1278                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1279
1280                 g_free (servername);
1281         }
1282         
1283         /* Set a default domain for the server, based on the email address,
1284          * if no server name was already specified.
1285          */
1286         if (!account_wizard->entry_outgoingserver)
1287                 return;
1288                 
1289         if (account_wizard->entry_user_email
1290             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1291                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1292                 
1293                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
1294
1295                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1296                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1297                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
1298                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1299
1300                 g_free (servername);
1301         }
1302 }
1303
1304 static gboolean
1305 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1306 {
1307         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1308         
1309         /* Do extra validation that couldn't be done for every key press,
1310          * either because it was too slow,
1311          * or because it requires interaction:
1312          */
1313         if (current_page == account_wizard->page_account_details) {     
1314                 /* Check that the title is not already in use: */
1315                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
1316                 if ((!account_title) || (strlen(account_title) == 0))
1317                         return FALSE;
1318                         
1319                 /* Aavoid a clash with an existing display name: */
1320                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1321                         account_wizard->account_manager, account_title);
1322
1323                 if (name_in_use) {
1324                         /* Warn the user via a dialog: */
1325                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1326             
1327                         return FALSE;
1328                 }
1329         }
1330         else if (current_page == account_wizard->page_user_details) {
1331                 /* Check that the email address is valud: */
1332                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1333                 if ((!email_address) || (strlen(email_address) == 0))
1334                         return FALSE;
1335                         
1336                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1337                         /* Warn the user via a dialog: */
1338                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1339                                              
1340                         /* Return focus to the email address entry: */
1341                         gtk_widget_grab_focus (account_wizard->entry_user_email);
1342                         gtk_editable_select_region (GTK_EDITABLE (account_wizard->entry_user_email), 0, -1);
1343
1344                         return FALSE;
1345                 }
1346                 
1347                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1348                 create_subsequent_pages (account_wizard);
1349         }
1350         
1351         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1352          * but does not specify how.
1353          */
1354           
1355         if(next_page == account_wizard->page_custom_incoming) {
1356                 set_default_custom_servernames (account_wizard);
1357         }
1358         else if (next_page == account_wizard->page_custom_outgoing) {
1359                 set_default_custom_servernames (account_wizard);
1360     /* Check if the server supports secure authentication */
1361                 const ModestConnectionProtocol security_incoming = 
1362                         modest_serversecurity_combo_box_get_active_serversecurity (
1363                                                                                                                                                                                                                                                                  MODEST_SERVERSECURITY_COMBO_BOX (
1364                                                                                                                                                                                                                                                                                                                                                                                                         account_wizard->combo_incoming_security));
1365                 if (gtk_toggle_button_get_active (
1366                         GTK_TOGGLE_BUTTON (account_wizard->checkbox_incoming_auth))
1367                                 && !modest_protocol_info_is_secure(security_incoming))
1368                 {
1369                                 GList* methods = check_for_supported_auth_methods(account_wizard);
1370                                 if (!methods)
1371                                 {
1372                                         g_list_free(methods);
1373                                         return FALSE;
1374                                 }
1375                                 g_list_free(methods);
1376                 }
1377         }
1378         
1379         /* If this is the last page, and this is a click on Finish, 
1380          * then attempt to create the dialog.
1381          */
1382         if(!next_page) /* This is NULL when this is a click on Finish. */
1383         {
1384                 if (account_wizard->saved_account_name) {
1385                         /* Just enable the already-saved account (temporarily created when 
1386                          * editing advanced settings): */
1387                         modest_account_mgr_set_enabled (account_wizard->account_manager, 
1388                                                         account_wizard->saved_account_name, TRUE);
1389                 } else {
1390                         create_account (account_wizard, TRUE /* enabled */);
1391                 }
1392         }
1393         
1394         
1395         return TRUE;
1396 }
1397
1398 static gboolean entry_is_empty (GtkWidget *entry)
1399 {
1400         if (!entry)
1401                 return FALSE;
1402                 
1403         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1404         if ((!text) || (strlen(text) == 0))
1405                 return TRUE;
1406         else
1407                 return FALSE;
1408 }
1409
1410 static void
1411 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1412 {
1413         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1414         
1415         gboolean enable_next = TRUE;
1416         if (current_page == account_wizard->page_welcome) {
1417                 enable_next = TRUE;
1418         }
1419         else if (current_page == account_wizard->page_account_details) {
1420                 /* The account details title is mandatory: */
1421                 if (entry_is_empty(account_wizard->entry_account_title))
1422                         enable_next = FALSE;
1423         }
1424         else if (current_page == account_wizard->page_user_details) {   
1425                 /* The user details username is mandatory: */
1426                 if (entry_is_empty(account_wizard->entry_user_username))
1427                         enable_next = FALSE;
1428                         
1429                 /* The user details email address is mandatory: */
1430                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1431                         enable_next = FALSE;
1432         }
1433         else if (current_page == account_wizard->page_custom_incoming) {
1434                 /* The custom incoming server is mandatory: */
1435                 if (entry_is_empty(account_wizard->entry_incomingserver))
1436                         enable_next = FALSE;
1437         }
1438                         
1439         /* Enable the buttons, 
1440          * identifying them via their associated response codes: */
1441                                    
1442         /* Disable the Finish button until we are on the last page,
1443          * because HildonWizardDialog enables this for all but the first page: */
1444         GtkNotebook *notebook = NULL;
1445         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1446         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1447         
1448         gint current = gtk_notebook_get_current_page (notebook);
1449         gint last = gtk_notebook_get_n_pages (notebook) - 1;
1450         const gboolean is_last = (current == last);
1451     
1452         if(!is_last) {
1453                 gtk_dialog_set_response_sensitive (dialog_base,
1454                                                    MODEST_WIZARD_DIALOG_FINISH,
1455                                                    FALSE);
1456         } else
1457         {
1458                 /* Disable Next on the last page: */
1459                 enable_next = FALSE;
1460         }
1461         
1462         gtk_dialog_set_response_sensitive (dialog_base,
1463                                            MODEST_WIZARD_DIALOG_NEXT,
1464                                            enable_next);
1465 }
1466
1467 static void
1468 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1469 {
1470         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1471         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1472
1473
1474         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1475         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1476         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1477         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1478         
1479         /* Provide a vfunc implementation so we can decide 
1480          * when to enable/disable the prev/next buttons.
1481          */
1482         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1483         base_klass->before_next = on_before_next;
1484         base_klass->enable_buttons = on_enable_buttons;
1485 }
1486  
1487 static void
1488 show_error (GtkWindow *parent_window, const gchar* text)
1489 {
1490         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1491         /*
1492           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1493           (GtkDialogFlags)0,
1494           GTK_MESSAGE_ERROR,
1495           GTK_BUTTONS_OK,
1496           text ));
1497         */
1498                  
1499         gtk_dialog_run (dialog);
1500         gtk_widget_destroy (GTK_WIDGET (dialog));
1501 }
1502
1503 /** Attempt to create the account from the information that the user has entered.
1504  * @result: TRUE if the account was successfully created.
1505  */
1506 gboolean
1507 create_account (ModestEasysetupWizardDialog *self, gboolean enabled)
1508 {
1509         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1510         
1511         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1512
1513         /* Some checks: */
1514         if (!display_name)
1515                 return FALSE;
1516                 
1517         /* We should have checked for this already, 
1518          * and changed that name accordingly, 
1519          * but let's check again just in case:
1520          */
1521         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1522                 return FALSE;
1523
1524         /* Increment the non-user visible name if necessary, 
1525          * based on the display name: */
1526         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1527         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1528                                                                           account_name_start, FALSE /* not a server account */);
1529         g_free (account_name_start);
1530                 
1531         /* username and password (for both incoming and outgoing): */
1532         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1533         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1534         /* Incoming server: */
1535         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
1536          * or modest_account_mgr_add_server_account will fail. */
1537         gchar* servername_incoming = NULL;
1538         guint serverport_incoming = 0;
1539         ModestTransportStoreProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1540         ModestConnectionProtocol protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_NORMAL;
1541         ModestAuthProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1542
1543         /* Get details from the specified presets: */
1544         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1545                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1546         if (provider_id) {
1547                 /* Use presets: */
1548                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1549                                                                  TRUE /* incoming */);
1550                 
1551                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets,
1552                                                                                                    provider_id, 
1553                                                                                                    TRUE /* incoming */);
1554                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets,
1555                                                                                                    provider_id, 
1556                                                                                                    TRUE /* incoming */);
1557
1558                 g_warning ("security incoming: %x", security_incoming);
1559                         
1560                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1561                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
1562                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1563                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
1564                         protocol_incoming = MODEST_PROTOCOL_STORE_POP; 
1565                 }
1566                 serverport_incoming = get_serverport_incoming(servertype_incoming, security_incoming);
1567                 
1568                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1569                         protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1570                 
1571                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1572                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1573         }
1574         else {
1575                 /* Use custom pages because no preset was specified: */
1576                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1577                 
1578                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1579                         EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1580                 
1581                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1582                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1583                 
1584                 /* The UI spec says:
1585                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1586                  * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1587                  */
1588                 
1589                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) &&
1590                                 !modest_protocol_info_is_secure(protocol_security_incoming))
1591                 {
1592                                 GList* methods = check_for_supported_auth_methods(self);
1593                                 if (!methods)
1594                                         return FALSE;
1595                                 else
1596                                   protocol_authentication_incoming = (ModestAuthProtocol) (GPOINTER_TO_INT(methods->data));
1597                 }
1598                 else
1599                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD;
1600         }
1601         
1602         /* First we add the 2 server accounts, and then we add the account that uses them.
1603          * If we don't do it in this order then we will experience a crash. */
1604          
1605         /* Add a (incoming) server account, to be used by the account: */
1606         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1607         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1608                                                                         store_name_start, TRUE /* server account */);
1609         g_free (store_name_start);
1610         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1611                                                                   store_name,
1612                                                                   servername_incoming,
1613                                                                   serverport_incoming,
1614                                                                   username, password,
1615                                                                   protocol_incoming,
1616                                                                   protocol_security_incoming,
1617                                                                   protocol_authentication_incoming);            
1618                 
1619         g_free (servername_incoming);
1620         
1621         if (!created) {
1622                 /* TODO: Provide a Logical ID for the text: */
1623                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1624                 return FALSE;   
1625         }
1626         
1627         /* Outgoing server: */
1628         gchar* servername_outgoing = NULL;
1629         ModestTransportStoreProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1630         ModestConnectionProtocol protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1631         ModestAuthProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1632         guint serverport_outgoing = 0;
1633         
1634         if (provider_id) {
1635                 /* Use presets: */
1636                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1637                                                                  FALSE /* incoming */);
1638                         
1639                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets,
1640                                                                                                    provider_id, 
1641                                                                                                    FALSE /* incoming */);
1642                 
1643                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1644                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1645                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1646                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1647                 
1648                 ModestPresetsSecurity security_outgoing = 
1649                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1650                                                                  FALSE /* incoming */);
1651
1652                 /* TODO: There is no SMTP authentication enum for presets, 
1653                    so we should probably check what the server supports. */
1654                 protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1655                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP) {
1656                         /* printf("DEBUG: %s: using secure SMTP\n", __FUNCTION__); */
1657                         protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1658                         serverport_outgoing = 465;
1659                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_PASSWORD;
1660                 } else {
1661                         /* printf("DEBUG: %s: using non-secure SMTP\n", __FUNCTION__); */
1662                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1663                 }
1664         }
1665         else {
1666                 /* Use custom pages because no preset was specified: */
1667                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1668                 
1669                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1670
1671                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1672                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1673                 
1674                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1675                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1676         }
1677             
1678         /* Add a (outgoing) server account to be used by the account: */
1679         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1680         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1681                                                                             transport_name_start, TRUE /* server account */);
1682         g_free (transport_name_start);
1683         created = modest_account_mgr_add_server_account (self->account_manager,
1684                                                          transport_name,
1685                                                          servername_outgoing,
1686                                                          serverport_outgoing,
1687                                                          username, password,
1688                                                          protocol_outgoing,
1689                                                          protocol_security_outgoing,
1690                                                          protocol_authentication_outgoing);
1691                 
1692         g_free (servername_outgoing);
1693                 
1694         if (!created) {
1695                 /* TODO: Provide a Logical ID for the text: */
1696                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1697                 return FALSE;   
1698         }
1699         
1700         
1701         /* Create the account, which will contain the two "server accounts": */
1702         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1703                                                   store_name, /* The name of our POP/IMAP server account. */
1704                                                   transport_name, /* The name of our SMTP server account. */
1705                                                   enabled);
1706         g_free (store_name);
1707         g_free (transport_name);
1708         
1709         if (!created) {
1710                 /* TODO: Provide a Logical ID for the text: */
1711                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1712                 return FALSE;   
1713         }
1714
1715         /* Sanity check: */
1716         /* There must be at least one account now: */
1717         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
1718          * fixed in versions after 3.1. */
1719         if(!modest_account_mgr_has_accounts (self->account_manager, FALSE))
1720                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1721                 
1722         /* The user name and email address must be set additionally: */
1723         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1724         modest_account_mgr_set_string (self->account_manager, account_name,
1725                                        MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1726
1727         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1728         modest_account_mgr_set_string (self->account_manager, account_name,
1729                                        MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1730
1731         /* Set the display name: */
1732         modest_account_mgr_set_string (self->account_manager, account_name,
1733                                        MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1734
1735         /* Set retrieve type */ 
1736         const gchar *retrieve = MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1737         modest_account_mgr_set_string (self->account_manager, account_name,
1738                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1739
1740         /* Save the connection-specific SMTP server accounts. */
1741         gboolean result = TRUE;
1742         if (self->specific_window)
1743                 result = modest_connection_specific_smtp_window_save_server_accounts (
1744                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1745                         
1746         g_free (self->saved_account_name);
1747         self->saved_account_name = g_strdup (account_name);
1748         
1749         g_free (account_name);
1750
1751         return result;
1752 }