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