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