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