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