version and bug fix info update for week 27, 2009 second release
[modest] / src / hildon2 / 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 #include <config.h>
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/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkseparator.h>
42 #include "modest-country-picker.h"
43 #include "modest-provider-picker.h"
44 #include "modest-servertype-picker.h"
45 #include "widgets/modest-validating-entry.h"
46 #include "modest-text-utils.h"
47 #include "modest-conf.h"
48 #include "modest-defs.h"
49 #include "modest-account-mgr.h"
50 #include "modest-signal-mgr.h"
51 #include "modest-account-mgr-helpers.h"
52 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
53 #include "modest-connection-specific-smtp-window.h"
54 #include "widgets/modest-ui-constants.h"
55 #include "widgets/modest-default-account-settings-dialog.h"
56 #include "widgets/modest-easysetup-wizard-page.h"
57 #include "modest-maemo-utils.h"
58 #include "modest-utils.h"
59 #include "modest-hildon-includes.h"
60 #include "modest-maemo-security-options-view.h"
61 #include "modest-account-protocol.h"
62 #include "modest-address-book.h"
63
64 /* Include config.h so that _() works: */
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
70
71 #define MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
72                                                     MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, \
73                                                     ModestEasysetupWizardDialogPrivate))
74
75 #define LABELS_WIDTH 520
76 #define DIALOG_WIDTH LABELS_WIDTH + MODEST_MARGIN_DOUBLE
77
78 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
79
80
81 typedef enum {
82         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
83         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
84 } ModestEasysetupWizardDialogServerChanges;
85
86 struct _ModestEasysetupWizardDialogPrivate
87 {
88         ModestPresets *presets;
89
90         /* Remember what fields the user edited manually to not prefill them
91          * again. */
92         ModestEasysetupWizardDialogServerChanges server_changes;
93
94         /* Check if the user changes a field to show a confirmation dialog */
95         gboolean dirty;
96
97         /* If we have a pending load of settings or not. */
98         gboolean pending_load_settings;
99
100         /* Used by derived widgets to query existing accounts,
101          * and to create new accounts: */
102         ModestAccountMgr *account_manager;
103         ModestAccountSettings *settings;
104
105         /* notebook pages: */
106         GtkWidget *page_welcome;
107         GtkWidget *check_support_progress;
108         gboolean  check_support_done;
109         guint check_support_show_progress_id;
110         guint check_support_progress_pulse_id;
111         gint pending_check_support;
112         gboolean destroyed;
113
114         GtkWidget *page_account_details;
115         GtkWidget *account_country_picker;
116         GtkWidget *account_serviceprovider_picker;
117         GtkWidget *entry_account_title;
118         GtkWidget *caption_account_title;
119
120         GtkWidget *page_user_details;
121         GtkWidget *entry_user_name;
122         GtkWidget *entry_user_username;
123         GtkWidget *entry_user_password;
124         GtkWidget *entry_user_email;
125
126         GtkWidget *page_complete_easysetup;
127
128         GtkWidget *page_custom_incoming;
129         GtkWidget *incoming_servertype_picker;
130         GtkWidget *caption_incoming;
131         GtkWidget *entry_incomingserver;
132
133         /* Security widgets */
134         GtkWidget *incoming_security;
135         GtkWidget *outgoing_security;
136
137         GtkWidget *page_custom_outgoing;
138         GtkWidget *entry_outgoingserver;
139         GtkWidget *checkbox_outgoing_smtp_specific;
140         GtkWidget *button_outgoing_smtp_servers;
141
142         GtkWidget *page_complete_customsetup;
143
144         ModestProtocolType last_plugin_protocol_selected;
145         GSList *missing_data_signals;
146 };
147
148 static void save_to_settings (ModestEasysetupWizardDialog *self);
149 static void real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next);
150 static void on_update_model (ModestWizardDialog *dialog);
151 static gboolean on_save (ModestWizardDialog *dialog);
152 static GList* check_for_supported_auth_methods (ModestEasysetupWizardDialog* self);
153 static gboolean check_has_supported_auth_methods(ModestEasysetupWizardDialog* self);
154 static void check_support_callback (ModestAccountProtocol *protocol,
155                                     gboolean supported,
156                                     gpointer userdata);
157 static void check_support_of_protocols (ModestEasysetupWizardDialog *self);
158 static gboolean check_support_show_progress (gpointer userdata);
159 static gboolean check_support_progress_pulse (gpointer userdata);
160
161 static gboolean
162 on_delete_event (GtkWidget *widget,
163                  GdkEvent *event,
164                  ModestEasysetupWizardDialog *wizard)
165 {
166         gtk_dialog_response (GTK_DIALOG (wizard), GTK_RESPONSE_CANCEL);
167         return TRUE;
168 }
169
170 static void
171 on_easysetup_changed(GtkWidget* widget, ModestEasysetupWizardDialog* wizard)
172 {
173         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard);
174         g_return_if_fail (priv != NULL);
175         priv->dirty = TRUE;
176 }
177
178 static void
179 modest_easysetup_wizard_dialog_dispose (GObject *object)
180 {
181         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (object);
182         priv->destroyed = TRUE;
183
184         if (priv->check_support_show_progress_id > 0) {
185                 g_source_remove (priv->check_support_show_progress_id);
186                 priv->check_support_show_progress_id = 0;
187         }
188
189         if (priv->check_support_progress_pulse_id > 0) {
190                 g_source_remove (priv->check_support_progress_pulse_id);
191                 priv->check_support_progress_pulse_id = 0;
192         }
193
194         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
195                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
196 }
197
198 static void
199 modest_easysetup_wizard_dialog_finalize (GObject *object)
200 {
201         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
202         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
203
204         if (priv->account_manager)
205                 g_object_unref (G_OBJECT (priv->account_manager));
206
207         if (priv->presets)
208                 modest_presets_destroy (priv->presets);
209
210         if (priv->settings)
211                 g_object_unref (priv->settings);
212
213         if (priv->missing_data_signals) {
214                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
215                 priv->missing_data_signals = NULL;
216         }
217
218         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
219 }
220
221 static void
222 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
223
224 static void
225 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
226
227 static void on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data);
228
229 static gint
230 get_port_from_protocol (ModestProtocolType server_type,
231                         gboolean alternate_port)
232 {
233         int server_port = 0;
234         ModestProtocol *protocol;
235         ModestProtocolRegistry *protocol_registry;
236
237         protocol_registry = modest_runtime_get_protocol_registry ();
238         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, server_type);
239
240         if (alternate_port) {
241                 server_port = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
242         } else {
243                 server_port = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
244         }
245         return server_port;
246 }
247
248 static void
249 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
250 {
251         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
252
253         /* Call the vfunc, which may be overridden by derived classes: */
254         if (klass->enable_buttons) {
255                 GtkNotebook *notebook = NULL;
256                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
257
258                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
259                 if (current_page_num == -1)
260                         return;
261
262                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
263                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
264         }
265 }
266
267 static void
268 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
269 {
270         ModestEasysetupWizardDialog *self;
271
272         g_return_if_fail (user_data);
273
274         self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
275
276         invoke_enable_buttons_vfunc(self);
277 }
278
279 static void
280 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
281 {
282         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
283         g_assert(self);
284         invoke_enable_buttons_vfunc(self);
285 }
286
287 static void
288 on_picker_button_value_changed (HildonPickerButton *widget, gpointer user_data)
289 {
290         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
291         g_assert(self);
292         invoke_enable_buttons_vfunc(self);
293 }
294
295 static void
296 on_serviceprovider_picker_button_value_changed (HildonPickerButton *widget, gpointer user_data)
297 {
298         gchar* default_account_name_start;
299         gchar* default_account_name;
300         ModestEasysetupWizardDialog *self;
301         ModestEasysetupWizardDialogPrivate *priv;
302         ModestProviderPickerIdType provider_id_type;
303         ModestProtocol *protocol;
304         gchar *proto_name;
305         ModestProtocolType proto_type;
306         gboolean hide_account_title;
307
308         self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
309         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
310
311         on_picker_button_value_changed (widget, user_data);
312
313         provider_id_type = modest_provider_picker_get_active_id_type (
314                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
315         if (provider_id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
316                 default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
317         } else {
318                 GtkWidget *selector;
319
320                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (widget)));
321                 default_account_name_start =
322                         g_strdup (hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
323
324         }
325         default_account_name = modest_account_mgr_get_unused_account_display_name (
326                 priv->account_manager, default_account_name_start);
327         g_free (default_account_name_start);
328         default_account_name_start = NULL;
329
330         hide_account_title = FALSE;
331         proto_name = modest_provider_picker_get_active_provider_id (MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
332         if (proto_name != NULL) {
333                 protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
334                                                                           MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
335                                                                           proto_name);
336                 if (protocol != NULL) {
337                         proto_type = modest_protocol_get_type_id (protocol);
338
339                         hide_account_title = (modest_protocol_registry_protocol_type_has_tag
340                                               (modest_runtime_get_protocol_registry (),
341                                                proto_type,
342                                                MODEST_PROTOCOL_REGISTRY_SINGLETON_PROVIDER_PROTOCOLS) ||
343                                               modest_protocol_registry_protocol_type_has_tag
344                                               (modest_runtime_get_protocol_registry (),
345                                                proto_type,
346                                                MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS));
347                 }
348         }
349
350         if (hide_account_title)
351                 gtk_widget_hide (priv->caption_account_title);
352         else
353                 gtk_widget_show (priv->caption_account_title);
354
355         hildon_entry_set_text (HILDON_ENTRY (priv->entry_account_title), default_account_name);
356         g_free (default_account_name);
357 }
358
359 /** This is a convenience function to create a caption containing a mandatory widget.
360  * When the widget is edited, the enable_buttons() vfunc will be called.
361  */
362 static GtkWidget*
363 create_captioned (ModestEasysetupWizardDialog *self,
364                   GtkSizeGroup *title_size_group,
365                   GtkSizeGroup *value_size_group,
366                   const gchar *value,
367                   gboolean use_markup,
368                   GtkWidget *control)
369 {
370
371         GtkWidget *result;
372         result = modest_maemo_utils_create_captioned_with_size_type (title_size_group, value_size_group,
373                                                                      value, use_markup, control,
374                                                                      MODEST_EDITABLE_SIZE);
375
376         /* Connect to the appropriate changed signal for the widget,
377          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
378          */
379         if (GTK_IS_ENTRY (control)) {
380                 g_signal_connect (G_OBJECT (control), "changed",
381                                   G_CALLBACK (on_caption_entry_changed), self);
382
383         }
384         else if (GTK_IS_COMBO_BOX (control)) {
385                 g_signal_connect (G_OBJECT (control), "changed",
386                                   G_CALLBACK (on_caption_combobox_changed), self);
387         }
388
389         return result;
390 }
391
392 static GtkWidget*
393 create_page_welcome (ModestEasysetupWizardDialog *self)
394 {
395         GtkWidget *box;
396         GtkWidget *align;
397         GtkWidget *label;
398         ModestEasysetupWizardDialogPrivate *priv;
399
400         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
401         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
402         label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
403         priv->check_support_progress = gtk_progress_bar_new ();
404         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
405         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
406         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
407         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
408         /* So that it is not truncated: */
409         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
410         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
411         gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
412         gtk_box_pack_end (GTK_BOX (box), priv->check_support_progress, FALSE, FALSE, 0);
413         gtk_container_add (GTK_CONTAINER (align), box);
414         gtk_widget_show (label);
415         gtk_widget_show (GTK_WIDGET (box));
416         return GTK_WIDGET (align);
417 }
418
419 static void
420 on_account_country_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
421 {
422         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
423         g_assert(self);
424         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
425
426         priv->dirty = TRUE;
427
428         /* Fill the providers picker, based on the selected country: */
429         if (priv->presets != NULL) {
430                 gint mcc = modest_country_picker_get_active_country_mcc (
431                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
432                 modest_provider_picker_fill (
433                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker), priv->presets, mcc);
434         }
435 }
436
437 static void
438 update_user_email_from_provider (ModestEasysetupWizardDialog *self)
439 {
440         ModestEasysetupWizardDialogPrivate *priv;
441         gchar* provider_id = NULL, *with_at = NULL;
442
443         g_assert(self);
444         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
445
446         /* Fill the providers combo, based on the selected country: */
447         provider_id = modest_provider_picker_get_active_provider_id (
448                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
449
450         if (provider_id) {
451                 gchar *domain_name = modest_presets_get_domain (priv->presets, provider_id);
452                 with_at = g_strdup_printf ("%c%s", '@', domain_name);
453                 g_free (domain_name);
454         }
455
456         /* The sample address already contains the '@' */
457         if (!with_at)
458                 with_at = g_strdup (MODEST_EXAMPLE_EMAIL_ADDRESS);
459
460         if (priv->entry_user_email)
461                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_email), with_at);
462
463         g_free (with_at);
464         g_free (provider_id);
465 }
466
467 static void
468 on_account_serviceprovider_selector_changed (HildonTouchSelector *widget, gint column, gpointer user_data)
469 {
470         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
471         g_assert(self);
472         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
473
474         priv->dirty = TRUE;
475
476         update_user_email_from_provider (self);
477 }
478
479 static void
480 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
481 {
482         modest_platform_information_banner (GTK_WIDGET (self), NULL,
483                                             _CS("ckdg_ib_maximum_characters_reached"));
484 }
485
486 static GtkWidget*
487 create_page_account_details (ModestEasysetupWizardDialog *self)
488 {
489         GtkWidget *align;
490         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
491         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
492         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
493
494         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
495         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
496         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
497         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
498         gtk_widget_show (label);
499
500         /* Create a size group to be used by all captions.
501          * Note that HildonCaption does not create a default size group if we do not specify one.
502          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
503         GtkSizeGroup* title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
504         GtkSizeGroup* value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
505
506         /* The country widgets: */
507         priv->account_country_picker = GTK_WIDGET (modest_country_picker_new (MODEST_EDITABLE_SIZE,
508                                                                               HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
509         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
510                                                _("mcen_fi_country"), priv->account_country_picker);
511         g_signal_connect (G_OBJECT (priv->account_country_picker), "value-changed",
512                           G_CALLBACK (on_picker_button_value_changed), self);
513         gtk_box_pack_start (GTK_BOX (box), priv->account_country_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
514         gtk_widget_show (priv->account_country_picker);
515
516         /* The service provider widgets: */
517         priv->account_serviceprovider_picker = GTK_WIDGET (modest_provider_picker_new (MODEST_EDITABLE_SIZE,
518                                                                                        HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
519         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
520                                                _("mcen_fi_serviceprovider"),
521                                                priv->account_serviceprovider_picker);
522         g_signal_connect (G_OBJECT (priv->account_serviceprovider_picker), "value-changed",
523                           G_CALLBACK (on_serviceprovider_picker_button_value_changed), self);
524         gtk_box_pack_start (GTK_BOX (box), priv->account_serviceprovider_picker, FALSE, FALSE, 0);
525         gtk_widget_show (priv->account_serviceprovider_picker);
526
527         /* The description widgets: */
528         priv->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
529
530         /* Do use auto-capitalization: */
531         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_account_title),
532                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
533
534         /* Set a default account title, choosing one that does not already exist: */
535         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
536         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
537         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
538                 priv->account_manager, default_account_name_start);
539         g_free (default_account_name_start);
540         default_account_name_start = NULL;
541
542         hildon_entry_set_text (HILDON_ENTRY (priv->entry_account_title), default_account_name);
543         g_free (default_account_name);
544         default_account_name = NULL;
545
546         /* Connect signal after changing the text in the entry */
547         g_signal_connect(G_OBJECT(priv->entry_account_title), "changed",
548                          G_CALLBACK(on_easysetup_changed), self);
549
550         priv->caption_account_title = create_captioned (self, title_sizegroup, value_sizegroup,
551                                                         _("mcen_fi_account_title"), FALSE,
552                                                         priv->entry_account_title);
553         gtk_widget_show (priv->entry_account_title);
554         gtk_box_pack_start (GTK_BOX (box), priv->caption_account_title, FALSE, FALSE, 0);
555         gtk_widget_show (priv->caption_account_title);
556         gtk_widget_set_no_show_all (priv->caption_account_title, TRUE);
557
558         /* Prevent the use of some characters in the account title,
559          * as required by our UI specification: */
560         GList *list_prevent = NULL;
561         list_prevent = g_list_append (list_prevent, "\\");
562         list_prevent = g_list_append (list_prevent, "/");
563         list_prevent = g_list_append (list_prevent, ":");
564         list_prevent = g_list_append (list_prevent, "*");
565         list_prevent = g_list_append (list_prevent, "?");
566         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
567         list_prevent = g_list_append (list_prevent, "“");
568         list_prevent = g_list_append (list_prevent, "<");
569         list_prevent = g_list_append (list_prevent, ">");
570         list_prevent = g_list_append (list_prevent, "|");
571         list_prevent = g_list_append (list_prevent, "^");
572         modest_validating_entry_set_unallowed_characters (
573                 MODEST_VALIDATING_ENTRY (priv->entry_account_title), list_prevent);
574         g_list_free (list_prevent);
575         list_prevent = NULL;
576         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_account_title),
577                                                                                                                                          modest_utils_on_entry_invalid_character, self);
578
579         /* Set max length as in the UI spec:
580          * The UI spec seems to want us to show a dialog if we hit the maximum. */
581         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_account_title), 64);
582         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_account_title),
583                                               on_entry_max, self);
584
585         gtk_widget_show (GTK_WIDGET (box));
586
587         g_object_unref (title_sizegroup);
588         g_object_unref (value_sizegroup);
589
590         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
591         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE, 0);
592         gtk_widget_show (align);
593         gtk_container_add (GTK_CONTAINER (align), box);
594
595         return GTK_WIDGET (align);
596 }
597
598 static void
599 on_user_username_changed(GtkWidget* widget, ModestEasysetupWizardDialog *self)
600 {
601         ModestEasysetupWizardDialogPrivate* priv;
602         gchar* provider_id = NULL;
603
604         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
605
606         /* Work out the user email address */
607         provider_id = modest_provider_picker_get_active_provider_id (
608                 MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
609
610         if (provider_id) {
611                 const gchar *current_username;
612                 gchar *email_address;
613                 gchar *domain_name = modest_presets_get_domain (priv->presets, provider_id);
614
615                 current_username = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username));
616
617                 if (current_username && strstr (current_username, "@")) {
618                         email_address = g_strdup (current_username);
619                 } else {
620                         email_address = g_strdup_printf ("%s@%s",
621                                                          current_username,
622                                                          domain_name);
623                 }
624
625                 /* Update the email address */
626                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_email), email_address);
627
628                 g_free (email_address);
629                 g_free (domain_name);
630                 g_free (provider_id);
631         }
632
633         /* Update state */
634         on_easysetup_changed(widget, self);
635 }
636
637
638 static GtkWidget*
639 create_page_user_details (ModestEasysetupWizardDialog *self)
640 {
641         GtkSizeGroup* title_sizegroup;
642         GtkSizeGroup* value_sizegroup;
643         GtkWidget *box;
644         ModestEasysetupWizardDialogPrivate *priv;
645         GtkWidget *align;
646         const gchar *my_name;
647
648         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
649
650         /* Create a size group to be used by all captions.
651          * Note that HildonCaption does not create a default size group if we do not specify one.
652          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
653         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
654         title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
655         value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
656
657         /* The name widgets: (use auto cap) */
658         priv->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
659         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_name),
660                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
661
662         /* Set max length as in the UI spec:
663          * The UI spec seems to want us to show a dialog if we hit the maximum. */
664         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_name), 64);
665         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_name),
666                                               on_entry_max, self);
667         GtkWidget *caption = create_captioned (self, title_sizegroup, value_sizegroup,
668                                                _("mcen_li_emailsetup_name"), FALSE, priv->entry_user_name);
669
670         /* Try to initialize the user name with my own contact */
671         my_name = modest_address_book_get_my_name ();
672         if (my_name)
673                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_name), my_name);
674
675         g_signal_connect(G_OBJECT(priv->entry_user_name), "changed",
676                          G_CALLBACK(on_easysetup_changed), self);
677         gtk_widget_show (priv->entry_user_name);
678         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 0);
679         gtk_widget_show (caption);
680
681         /* Prevent the use of some characters in the name,
682          * as required by our UI specification: */
683         GList *list_prevent = NULL;
684         list_prevent = g_list_append (list_prevent, "<");
685         list_prevent = g_list_append (list_prevent, ">");
686         modest_validating_entry_set_unallowed_characters (
687                 MODEST_VALIDATING_ENTRY (priv->entry_user_name), list_prevent);
688         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_name),
689                 modest_utils_on_entry_invalid_character, self);
690         g_list_free (list_prevent);
691
692         /* The username widgets: */
693         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
694         /* Auto-capitalization is the default, so let's turn it off: */
695         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username),
696                                          HILDON_GTK_INPUT_MODE_FULL);
697         caption = create_captioned (self, title_sizegroup, value_sizegroup, _("mail_fi_username"), FALSE,
698                                     priv->entry_user_username);
699         gtk_widget_show (priv->entry_user_username);
700         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 0);
701         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed",
702                          G_CALLBACK(on_user_username_changed), self);
703         gtk_widget_show (caption);
704
705         /* Prevent the use of some characters in the username,
706          * as required by our UI specification: */
707         modest_validating_entry_set_func(MODEST_VALIDATING_ENTRY(priv->entry_user_username),
708                 modest_utils_on_entry_invalid_character, self);
709
710         /* Set max length as in the UI spec:
711          * The UI spec seems to want us to show a dialog if we hit the maximum. */
712         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
713         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_username),
714                                               on_entry_max, self);
715
716         /* The password widgets: */
717         priv->entry_user_password = hildon_entry_new (MODEST_EDITABLE_SIZE);
718         /* Auto-capitalization is the default, so let's turn it off: */
719         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password),
720                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
721         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
722         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
723         caption = create_captioned (self, title_sizegroup, value_sizegroup,
724                                     _("mail_fi_password"), FALSE, priv->entry_user_password);
725         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed",
726                          G_CALLBACK(on_easysetup_changed), self);
727         gtk_widget_show (priv->entry_user_password);
728         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 0);
729         gtk_widget_show (caption);
730
731         /* The email address widgets: */
732         priv->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
733         /* Auto-capitalization is the default, so let's turn it off: */
734         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
735         caption = create_captioned (self, title_sizegroup, value_sizegroup,
736                                     _("mcen_li_emailsetup_email_address"), FALSE, priv->entry_user_email);
737         update_user_email_from_provider (self);
738         gtk_widget_show (priv->entry_user_email);
739         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 0);
740         g_signal_connect(G_OBJECT(priv->entry_user_email), "changed",
741                          G_CALLBACK(on_easysetup_changed), self);
742         gtk_widget_show (caption);
743
744         /* Set max length as in the UI spec:
745          * The UI spec seems to want us to show a dialog if we hit the maximum. */
746         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_email), 64);
747         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (priv->entry_user_email),
748                                               on_entry_max, self);
749
750
751         gtk_widget_show (GTK_WIDGET (box));
752         g_object_unref (title_sizegroup);
753         g_object_unref (value_sizegroup);
754
755         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
756         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE, 0);
757         gtk_widget_show (align);
758         gtk_container_add (GTK_CONTAINER (align), box);
759
760         return GTK_WIDGET (align);
761 }
762
763 static GtkWidget*
764 create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
765 {
766         GtkWidget *align;
767         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
768
769         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
770         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
771         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
772         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
773         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
774
775         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
776         gtk_widget_show (label);
777
778         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
779         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
780         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
781
782         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
783         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
784         gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
785         gtk_widget_show (label);
786
787         gtk_widget_show (GTK_WIDGET (box));
788
789         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
790         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE, 0);
791         gtk_widget_show (align);
792         gtk_container_add (GTK_CONTAINER (align), box);
793
794         return GTK_WIDGET (align);
795 }
796
797 /** Change the caption title for the incoming server,
798  * as specified in the UI spec:
799  */
800 static void
801 update_incoming_server_title (ModestEasysetupWizardDialog *self)
802 {
803         ModestEasysetupWizardDialogPrivate* priv;
804         ModestProtocolType protocol_type;
805         ModestProtocolRegistry *protocol_registry;
806
807         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
808         protocol_registry = modest_runtime_get_protocol_registry ();
809
810         protocol_type = modest_servertype_picker_get_active_servertype (
811                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
812
813         /* This could happen when the combo box has still no active iter */
814         if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
815                 gchar* incomingserver_title;
816                 const gchar *protocol_display_name;
817                 ModestProtocol *protocol;
818
819                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry,
820                                                                           protocol_type);
821                 protocol_display_name = modest_protocol_get_display_name (protocol);
822
823                 incomingserver_title = g_strconcat (_("mcen_li_emailsetup_servertype"), "\n<small>(",
824                                                     protocol_display_name, ")</small>", NULL);
825
826                 modest_maemo_utils_captioned_set_label (priv->caption_incoming, incomingserver_title, TRUE);
827
828                 g_free(incomingserver_title);
829         }
830 }
831
832 /** Change the caption title for the incoming server,
833  * as specified in the UI spec:
834  */
835 static void
836 update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
837 {
838         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
839         ModestServertypePicker *server_type_picker;
840         ModestProtocolType protocol_type;
841         ModestSecurityOptionsView *view;
842
843         server_type_picker =
844                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker);
845         protocol_type =
846                 modest_servertype_picker_get_active_servertype (server_type_picker);
847
848         /* Fill the combo with appropriately titled choices for all
849            those protocols */
850         view = MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security);
851         modest_security_options_view_set_server_type (view, protocol_type);
852 }
853
854 static void
855 on_servertype_selector_changed(HildonTouchSelector *selector, gint column, gpointer user_data)
856 {
857         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
858         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
859
860         priv->dirty = TRUE;
861
862         /* Update title */
863         update_incoming_server_title (self);
864
865         /* Update security options if needed */
866         update_incoming_server_security_choices (self);
867         gtk_widget_show (priv->incoming_security);
868
869         set_default_custom_servernames (self);
870 }
871
872 static void
873 on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
874 {
875         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
876         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
877         priv->dirty = TRUE;
878         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
879 }
880
881 static GtkWidget*
882 create_page_custom_incoming (ModestEasysetupWizardDialog *self)
883 {
884         ModestProtocolRegistry *protocol_registry;
885         ModestEasysetupWizardDialogPrivate* priv;
886         GtkWidget *box;
887         GtkWidget *align;
888         GtkWidget *pannable;
889         GtkWidget *label;
890         GtkSizeGroup *title_sizegroup;
891         GtkSizeGroup *value_sizegroup;
892
893         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
894         protocol_registry = modest_runtime_get_protocol_registry ();
895
896         box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
897         pannable = hildon_pannable_area_new ();
898
899         /* Show note that account type cannot be changed in future: */
900         label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
901         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
902         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
903         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
904         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
905         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
906         gtk_widget_show (label);
907
908         /* Create a size group to be used by all captions.
909          * Note that HildonCaption does not create a default size group if we do not specify one.
910          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
911         title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
912         value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
913
914         /* The incoming server widgets: */
915         priv->incoming_servertype_picker = GTK_WIDGET (modest_servertype_picker_new (MODEST_EDITABLE_SIZE,
916                                                                                      HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
917                                                                                      TRUE));
918         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
919                                                _("mcen_li_emailsetup_type"),
920                                                priv->incoming_servertype_picker);
921         g_signal_connect (G_OBJECT (priv->incoming_servertype_picker), "value-changed",
922                           G_CALLBACK (on_picker_button_value_changed), self);
923         gtk_box_pack_start (GTK_BOX (box), priv->incoming_servertype_picker, FALSE, FALSE, 0);
924         gtk_widget_show (priv->incoming_servertype_picker);
925
926         priv->entry_incomingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
927
928         /* Auto-capitalization is the default, so let's turn it off: */
929         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
930         set_default_custom_servernames (self);
931
932         /* The caption title will be updated in update_incoming_server_title().
933          * so this default text will never be seen: */
934         priv->caption_incoming = create_captioned (self, title_sizegroup, value_sizegroup,
935                                                    "This will be removed",
936                                                    FALSE, priv->entry_incomingserver);
937         update_incoming_server_title (self);
938         gtk_widget_show (priv->entry_incomingserver);
939         gtk_box_pack_start (GTK_BOX (box), priv->caption_incoming, FALSE, FALSE, 0);
940         gtk_widget_show (priv->caption_incoming);
941
942         /* Remember when the servername was changed manually: */
943         g_signal_connect (G_OBJECT (priv->entry_incomingserver), "changed",
944                           G_CALLBACK (on_entry_incoming_servername_changed), self);
945
946         /* The secure connection widgets. These are only valid for
947            protocols with security */
948         priv->incoming_security =
949                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_INCOMING,
950                                                         FALSE, title_sizegroup, value_sizegroup);
951         gtk_box_pack_start (GTK_BOX (box), priv->incoming_security,
952                             FALSE, FALSE, 0);
953         gtk_widget_show (priv->incoming_security);
954
955         /* Set default selection */
956         modest_servertype_picker_set_active_servertype (
957                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker),
958                 MODEST_PROTOCOLS_STORE_POP);
959         update_incoming_server_title (self);
960         update_incoming_server_security_choices (self);
961         set_default_custom_servernames (self);
962
963         /* Change the caption title when the servertype changes,
964          * as in the UI spec: */
965         g_signal_connect (G_OBJECT (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (priv->incoming_servertype_picker))),
966                           "changed",
967                           G_CALLBACK (on_servertype_selector_changed), self);
968
969         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
970         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
971         gtk_widget_show (align);
972         gtk_container_add (GTK_CONTAINER (align), box);
973
974         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), align);
975         gtk_widget_show (GTK_WIDGET (box));
976         gtk_widget_show (pannable);
977
978         g_object_unref (title_sizegroup);
979         g_object_unref (value_sizegroup);
980
981         /* Connect changed signals. We do it here after initializing everything */
982         g_signal_connect (G_OBJECT(priv->entry_incomingserver), "changed",
983                           G_CALLBACK(on_easysetup_changed), self);
984
985
986         return GTK_WIDGET (pannable);
987 }
988
989 static void
990 on_check_button_changed (HildonCheckButton *button, gpointer user_data)
991 {
992         GtkWidget *widget = GTK_WIDGET (user_data);
993
994         /* Enable the widget only if the check button is active: */
995         const gboolean enable = hildon_check_button_get_active (button);
996         gtk_widget_set_sensitive (widget, enable);
997 }
998
999 /* Make the sensitivity of a widget depend on a check button.
1000  */
1001 static void
1002 enable_widget_for_checkbutton (GtkWidget *widget, HildonCheckButton* button)
1003 {
1004         g_signal_connect (G_OBJECT (button), "toggled",
1005                           G_CALLBACK (on_check_button_changed), widget);
1006
1007         /* Set the starting sensitivity: */
1008         on_check_button_changed (button, widget);
1009 }
1010
1011 static void
1012 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
1013 {
1014         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1015         ModestEasysetupWizardDialogPrivate* priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1016         GtkWidget *specific_window;
1017
1018         /* We set dirty here because setting it depending on the connection specific dialog
1019         seems overkill */
1020         priv->dirty = TRUE;
1021
1022         /* Create the window, if necessary: */
1023         specific_window = (GtkWidget *) modest_connection_specific_smtp_window_new ();
1024         modest_connection_specific_smtp_window_fill_with_connections (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (specific_window), priv->account_manager);
1025
1026         /* Show the window */
1027         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (specific_window), GTK_WINDOW (self));
1028         gtk_widget_show (specific_window);
1029 }
1030
1031 static void
1032 on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
1033 {
1034         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1035         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1036         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
1037 }
1038
1039 static GtkWidget*
1040 create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
1041 {
1042         ModestEasysetupWizardDialogPrivate *priv;
1043         gchar *smtp_caption_label;
1044         GtkWidget *pannable;
1045         GtkWidget *align;
1046         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
1047
1048         pannable = hildon_pannable_area_new ();
1049
1050         /* Create a size group to be used by all captions.
1051          * Note that HildonCaption does not create a default size group if we do not specify one.
1052          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
1053         GtkSizeGroup *title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1054         GtkSizeGroup *value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1055
1056         /* The outgoing server widgets: */
1057         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1058         priv->entry_outgoingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
1059
1060         /* Auto-capitalization is the default, so let's turn it off: */
1061         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
1062         smtp_caption_label = g_strconcat (_("mcen_li_emailsetup_smtp"), "\n<small>(SMTP)</small>", NULL);
1063         GtkWidget *caption = create_captioned (self, title_sizegroup, value_sizegroup,
1064                                                smtp_caption_label, TRUE, priv->entry_outgoingserver);
1065         g_free (smtp_caption_label);
1066         gtk_widget_show (priv->entry_outgoingserver);
1067         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 0);
1068         gtk_widget_show (caption);
1069         set_default_custom_servernames (self);
1070
1071         /* The secure connection widgets. These are only valid for
1072            protocols with security */
1073         priv->outgoing_security =
1074                 modest_maemo_security_options_view_new (MODEST_SECURITY_OPTIONS_OUTGOING,
1075                                                         FALSE, title_sizegroup, value_sizegroup);
1076         gtk_box_pack_start (GTK_BOX (box), priv->outgoing_security,
1077                             FALSE, FALSE, 0);
1078         gtk_widget_show (priv->outgoing_security);
1079
1080         GtkWidget *separator = gtk_hseparator_new ();
1081         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, 0);
1082         gtk_widget_show (separator);
1083
1084         /* connection-specific checkbox: */
1085         priv->checkbox_outgoing_smtp_specific = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
1086         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->checkbox_outgoing_smtp_specific),
1087                                         FALSE);
1088         gtk_button_set_label (GTK_BUTTON (priv->checkbox_outgoing_smtp_specific),
1089                               _("mcen_fi_advsetup_connection_smtp"));
1090         gtk_button_set_alignment (GTK_BUTTON (priv->checkbox_outgoing_smtp_specific),
1091                                   0.0, 0.5);
1092
1093         gtk_widget_show (priv->checkbox_outgoing_smtp_specific);
1094         gtk_box_pack_start (GTK_BOX (box), priv->checkbox_outgoing_smtp_specific,
1095                             FALSE, FALSE, 0);
1096
1097         /* Connection-specific SMTP-Severs Edit button: */
1098         priv->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_advsetup_optional_smtp"));
1099         hildon_gtk_widget_set_theme_size (priv->button_outgoing_smtp_servers,
1100                                           HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
1101         gtk_widget_show (priv->button_outgoing_smtp_servers);
1102         gtk_box_pack_start (GTK_BOX (box), priv->button_outgoing_smtp_servers,
1103                             FALSE, FALSE, 0);
1104
1105         /* Only enable the button when the checkbox is checked: */
1106         enable_widget_for_checkbutton (priv->button_outgoing_smtp_servers,
1107                                        HILDON_CHECK_BUTTON (priv->checkbox_outgoing_smtp_specific));
1108
1109         g_signal_connect (G_OBJECT (priv->button_outgoing_smtp_servers), "clicked",
1110                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
1111
1112         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
1113                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
1114
1115
1116         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
1117         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
1118         gtk_widget_show (align);
1119         gtk_container_add (GTK_CONTAINER (align), box);
1120
1121         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), align);
1122         gtk_widget_show (GTK_WIDGET (box));
1123         gtk_widget_show (pannable);
1124
1125         g_object_unref (title_sizegroup);
1126         g_object_unref (value_sizegroup);
1127
1128         /* Connect changed signals. We do it here after initializing everything */
1129         g_signal_connect (G_OBJECT (priv->entry_outgoingserver), "changed",
1130                           G_CALLBACK (on_easysetup_changed), self);
1131         g_signal_connect (G_OBJECT (priv->checkbox_outgoing_smtp_specific), "toggled",
1132                           G_CALLBACK (on_easysetup_changed), self);
1133
1134         return GTK_WIDGET (pannable);
1135 }
1136
1137 static gboolean
1138 show_advanced_edit(gpointer user_data)
1139 {
1140         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1141         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1142         gint response;
1143
1144         /* Show the Account Settings window: */
1145         ModestAccountSettingsDialog *dialog = modest_default_account_settings_dialog_new ();
1146         if (priv->pending_load_settings) {
1147                 save_to_settings (self);
1148                 priv->pending_load_settings = FALSE;
1149         }
1150         modest_account_settings_dialog_load_settings (dialog, priv->settings);
1151
1152         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog), GTK_WINDOW (self));
1153
1154         response = gtk_dialog_run (GTK_DIALOG (dialog));
1155
1156         gtk_widget_destroy (GTK_WIDGET (dialog));
1157
1158         return FALSE; /* Do not call this timeout callback again. */
1159 }
1160
1161 static void
1162 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
1163 {
1164         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
1165
1166         /* Show the Account Settings window: */
1167         show_advanced_edit(self);
1168 }
1169 static GtkWidget*
1170 create_page_complete_custom (ModestEasysetupWizardDialog *self)
1171 {
1172         gchar *complete_string;
1173         GtkWidget *align;
1174         GtkWidget *box;
1175         GtkWidget *label;
1176         GtkWidget *button_edit;
1177
1178         box = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
1179         complete_string = g_strconcat (_("mcen_ia_emailsetup_setup_complete"), " ",
1180                                        _("mcen_ia_customsetup_complete"), NULL);
1181         label = gtk_label_new(complete_string);
1182         g_free (complete_string);
1183         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
1184         gtk_widget_set_size_request (label, LABELS_WIDTH, -1);
1185         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1186         gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
1187         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
1188         gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
1189         gtk_widget_show (label);
1190
1191         button_edit = gtk_button_new_with_label (_("mcen_fi_advanced_settings"));
1192         hildon_gtk_widget_set_theme_size (button_edit, HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
1193         gtk_widget_show (button_edit);
1194         gtk_box_pack_start (GTK_BOX (box), button_edit, FALSE, FALSE, 0);
1195
1196         g_signal_connect (G_OBJECT (button_edit), "clicked",
1197                           G_CALLBACK (on_button_edit_advanced_settings), self);
1198
1199         gtk_widget_show (GTK_WIDGET (box));
1200
1201         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
1202         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE, 0);
1203         gtk_widget_show (align);
1204         gtk_container_add (GTK_CONTAINER (align), box);
1205
1206         return GTK_WIDGET (align);
1207 }
1208
1209
1210 /*
1211  */
1212 static void
1213 on_response (ModestWizardDialog *wizard_dialog,
1214              gint response_id,
1215              gpointer user_data)
1216 {
1217         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1218
1219         invoke_enable_buttons_vfunc (self);
1220 }
1221
1222 static void
1223 on_response_before (ModestWizardDialog *wizard_dialog,
1224                     gint response_id,
1225                     gpointer user_data)
1226 {
1227         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
1228         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(wizard_dialog);
1229         if (response_id == GTK_RESPONSE_CANCEL) {
1230                 /* This is mostly copied from
1231                  * src/maemo/modest-account-settings-dialog.c */
1232                 if (priv->dirty) {
1233                         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_confirmation (GTK_WINDOW (self),
1234                                 _("imum_nc_wizard_confirm_lose_changes")));
1235                         /* TODO: These button names will be ambiguous, and not
1236                          * specified in the UI specification. */
1237
1238                         const gint dialog_response = gtk_dialog_run (dialog);
1239                         gtk_widget_destroy (GTK_WIDGET (dialog));
1240
1241                         if (dialog_response != GTK_RESPONSE_OK) {
1242                                 /* Don't let the dialog close */
1243                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1244                         }
1245                 }
1246         }
1247 }
1248
1249 static void
1250 fill_providers (ModestEasysetupWizardDialog *self)
1251 {
1252         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1253
1254         if (MODEST_IS_COUNTRY_PICKER (priv->account_country_picker)) {
1255 /*              gint mcc = get_default_country_code(); */
1256                 gint mcc;
1257                 /* Fill the combo in an idle call, as it takes a lot of time */
1258                 modest_country_picker_load_data(
1259                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1260                 /* connect to country picker's changed signal, so we can fill the provider picker: */
1261                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector
1262                                             (HILDON_PICKER_BUTTON (priv->account_country_picker))),
1263                                   "changed",
1264                                   G_CALLBACK (on_account_country_selector_changed), self);
1265
1266                 modest_country_picker_set_active_country_locale (
1267                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1268                 mcc = modest_country_picker_get_active_country_mcc (
1269                         MODEST_COUNTRY_PICKER (priv->account_country_picker));
1270                 modest_provider_picker_fill (
1271                         MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker),
1272                         priv->presets, mcc);
1273                 /* connect to providers picker's changed signal, so we can fill the email address: */
1274                 g_signal_connect (G_OBJECT (hildon_picker_button_get_selector
1275                                             (HILDON_PICKER_BUTTON (priv->account_serviceprovider_picker))),
1276                                   "changed",
1277                                   G_CALLBACK (on_account_serviceprovider_selector_changed), self);
1278
1279                 modest_provider_picker_set_others_provider (MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker));
1280         }
1281
1282         priv->dirty = FALSE;
1283
1284 }
1285
1286 typedef struct IdleData {
1287         ModestEasysetupWizardDialog *dialog;
1288         ModestPresets *presets;
1289 } IdleData;
1290
1291 static gboolean
1292 presets_idle (gpointer userdata)
1293 {
1294         IdleData *idle_data = (IdleData *) userdata;
1295         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (idle_data->dialog);
1296         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1297
1298         g_assert (idle_data->presets);
1299
1300         gdk_threads_enter ();
1301
1302         priv->presets = idle_data->presets;
1303
1304         if (priv->check_support_done)
1305                 fill_providers (self);
1306
1307         g_object_unref (idle_data->dialog);
1308         g_free (idle_data);
1309
1310         gdk_threads_leave ();
1311
1312         return FALSE;
1313 }
1314
1315 static gpointer
1316 presets_loader (gpointer userdata)
1317 {
1318         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (userdata);
1319         ModestPresets *presets = NULL;
1320         IdleData *idle_data;
1321
1322         const gchar* path  = NULL;
1323         const gchar* path1 = MODEST_PROVIDER_DATA_FILE;
1324         const gchar* path2 = MODEST_FALLBACK_PROVIDER_DATA_FILE;
1325
1326         if (access(path1, R_OK) == 0)
1327                 path = path1;
1328         else if (access(path2, R_OK) == 0)
1329                 path = path2;
1330         else {
1331                 g_warning ("%s: neither '%s' nor '%s' is a readable provider data file",
1332                            __FUNCTION__, path1, path2);
1333                 return NULL;
1334         }
1335
1336         presets = modest_presets_new (path);
1337         if (!presets) {
1338                 g_warning ("%s: failed to parse '%s'", __FUNCTION__, path);
1339                 return NULL;
1340         }
1341
1342         idle_data = g_new0 (IdleData, 1);
1343         idle_data->dialog = self;
1344         idle_data->presets = presets;
1345
1346         g_idle_add (presets_idle, idle_data);
1347
1348         return NULL;
1349 }
1350
1351 static void
1352 modest_easysetup_wizard_dialog_append_page (GtkNotebook *notebook,
1353                                             GtkWidget *page,
1354                                             const gchar *label)
1355 {
1356         gint index;
1357
1358         /* Append page and set attributes */
1359         index = gtk_notebook_append_page (notebook, page, gtk_label_new (label));
1360         gtk_container_child_set (GTK_CONTAINER (notebook), page,
1361                                  "tab-expand", TRUE, "tab-fill", TRUE,
1362                                  NULL);
1363
1364         /* Give focus to page and show it */
1365         gtk_container_set_focus_child (GTK_CONTAINER (notebook), page);
1366         gtk_widget_show (page);
1367 }
1368
1369 static void
1370 init_user_page (ModestEasysetupWizardDialogPrivate *priv)
1371 {
1372         priv->page_user_details = NULL;
1373         priv->entry_user_name = NULL;
1374         priv->entry_user_username = NULL;
1375         priv->entry_user_password = NULL;
1376         priv->entry_user_email = NULL;
1377 }
1378
1379 static void
1380 init_incoming_page (ModestEasysetupWizardDialogPrivate *priv)
1381 {
1382         priv->page_custom_incoming = NULL;
1383         priv->incoming_servertype_picker = NULL;
1384         priv->caption_incoming = NULL;
1385         priv->entry_incomingserver = NULL;
1386         priv->entry_user_email = NULL;
1387         priv->incoming_security = NULL;
1388 }
1389
1390 static void
1391 init_outgoing_page (ModestEasysetupWizardDialogPrivate *priv)
1392 {
1393         priv->page_custom_outgoing = NULL;
1394         priv->entry_outgoingserver = NULL;
1395         priv->checkbox_outgoing_smtp_specific = NULL;
1396         priv->button_outgoing_smtp_servers = NULL;
1397         priv->outgoing_security = NULL;
1398 }
1399
1400 static void
1401 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
1402 {
1403         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
1404
1405         /* Create the notebook to be used by the ModestWizardDialog base class:
1406          * Each page of the notebook will be a page of the wizard: */
1407         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
1408         gtk_widget_set_size_request (GTK_WIDGET (notebook), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
1409
1410         /* Set the notebook used by the ModestWizardDialog base class: */
1411         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
1412
1413         /* Set the wizard title:
1414          * The actual window title will be a combination of this and the page's tab label title. */
1415         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
1416
1417         /* Read in the information about known service providers: */
1418         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1419
1420         /* The server fields did not have been manually changed yet */
1421         priv->server_changes = 0;
1422         priv->pending_load_settings = TRUE;
1423
1424         /* Get the account manager object,
1425          * so we can check for existing accounts,
1426          * and create new accounts: */
1427         priv->account_manager = modest_runtime_get_account_mgr ();
1428         g_object_ref (priv->account_manager);
1429
1430         /* Initialize fields */
1431         priv->check_support_done = FALSE;
1432         priv->check_support_show_progress_id = 0;
1433         priv->check_support_progress_pulse_id = 0;
1434         priv->pending_check_support = 0;
1435         priv->destroyed = FALSE;
1436         priv->page_welcome = create_page_welcome (self);
1437         gtk_widget_show (priv->page_welcome);
1438         priv->page_account_details = create_page_account_details (self);
1439
1440         init_user_page (priv);
1441         init_incoming_page (priv);
1442         init_outgoing_page (priv);
1443
1444         priv->page_complete_easysetup = NULL;
1445         priv->page_complete_customsetup = NULL;
1446         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1447         priv->missing_data_signals = NULL;
1448
1449         /* Add the common pages */
1450         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_welcome,
1451                                                     _("mcen_ti_emailsetup_welcome"));
1452         modest_easysetup_wizard_dialog_append_page (notebook, priv->page_account_details,
1453                                                     _("mcen_ti_accountdetails"));
1454
1455         /* Connect to the dialog's response signal so we can enable/disable buttons
1456          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
1457          * Note that we use g_signal_connect_after() instead of g_signal_connect()
1458          * so that we can be enable/disable after ModestWizardDialog has done its own
1459          * enabling/disabling of buttons.
1460          *
1461          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler
1462          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
1463          *
1464          * It's not enough to connect to the notebook's switch-page signal, because
1465          * ModestWizardDialog's "response" signal handler enables the buttons itself,
1466          * _after_ switching the page (understandably).
1467          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
1468          * then gtk_notebook_get_current_page() would return an incorrect value.)
1469          */
1470         g_signal_connect_after (G_OBJECT (self), "response",
1471                                 G_CALLBACK (on_response), self);
1472
1473         /* This is to show a confirmation dialog when the user hits cancel */
1474         g_signal_connect (G_OBJECT (self), "response",
1475                           G_CALLBACK (on_response_before), self);
1476
1477         g_signal_connect (G_OBJECT (self), "delete-event",
1478                           G_CALLBACK (on_delete_event), self);
1479
1480         /* Reset dirty, because there was no user input until now */
1481         priv->dirty = FALSE;
1482
1483         /* When this window is shown, hibernation should not be possible,
1484          * because there is no sensible way to save the state: */
1485         modest_window_mgr_prevent_hibernation_while_window_is_shown (
1486                 modest_runtime_get_window_mgr (), GTK_WINDOW (self));
1487
1488         /* Load provider presets */
1489         g_object_ref (self);
1490         g_thread_create (presets_loader, self, FALSE, NULL);
1491
1492         priv->settings = modest_account_settings_new ();
1493
1494         check_support_of_protocols (self);
1495 }
1496
1497 ModestEasysetupWizardDialog*
1498 modest_easysetup_wizard_dialog_new (void)
1499 {
1500
1501         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
1502 }
1503
1504 static void
1505 create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
1506 {
1507         ModestEasysetupWizardDialogPrivate *priv;
1508         GtkNotebook *notebook = NULL;
1509
1510         g_object_get (self, "wizard-notebook", &notebook, NULL);
1511         g_assert(notebook);
1512
1513         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1514
1515         if (!priv->page_user_details) {
1516                 priv->page_user_details = create_page_user_details (self);
1517         }
1518
1519         /* Create the custom pages: */
1520         if(!(priv->page_custom_incoming)) {
1521                 priv->page_custom_incoming = create_page_custom_incoming (self);
1522         }
1523
1524         /* TODO: only if needed */
1525         if(!(priv->page_custom_outgoing)) {
1526                 priv->page_custom_outgoing = create_page_custom_outgoing (self);
1527         }
1528
1529         if(!(priv->page_complete_customsetup)) {
1530                 priv->page_complete_customsetup = create_page_complete_custom (self);
1531         }
1532
1533         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_user_details)))
1534                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_user_details,
1535                                                             _("mcen_ti_emailsetup_userdetails"));
1536
1537         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_incoming)))
1538                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_incoming,
1539                                                             _("mcen_ti_emailsetup_incomingdetails"));
1540
1541         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_custom_outgoing)))
1542                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_custom_outgoing,
1543                                                             _("mcen_ti_emailsetup_outgoingdetails"));
1544
1545         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_customsetup)))
1546                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_customsetup,
1547                                                             _("mcen_ti_emailsetup_complete"));
1548
1549         /* This is unnecessary with GTK+ 2.10: */
1550         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1551 }
1552
1553 static void
1554 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1555 {
1556         ModestEasysetupWizardDialogPrivate *priv;
1557         GtkNotebook *notebook = NULL;
1558
1559         g_object_get (self, "wizard-notebook", &notebook, NULL);
1560         g_assert(notebook);
1561
1562         /* Create the easysetup-specific pages: */
1563         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1564         if(!priv->page_complete_easysetup)
1565                 priv->page_complete_easysetup = create_page_complete_easysetup (self);
1566
1567         if (!gtk_widget_get_parent (GTK_WIDGET (priv->page_complete_easysetup)))
1568                 modest_easysetup_wizard_dialog_append_page (notebook, priv->page_complete_easysetup,
1569                                                             _("mcen_ti_emailsetup_complete"));
1570
1571         /* This is unnecessary with GTK+ 2.10: */
1572         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1573 }
1574
1575 /* */
1576 static void
1577 remove_non_common_tabs (GtkNotebook *notebook,
1578                         gboolean remove_user_details)
1579 {
1580         gint starting_tab;
1581         /* The first 2 tabs are the common ones (welcome tab and the
1582            providers tab), so we always remove starting from the
1583            end */
1584
1585         starting_tab = (remove_user_details) ? 2 : 3;
1586         while (gtk_notebook_get_n_pages (notebook) > starting_tab)
1587                 gtk_notebook_remove_page (notebook, -1);
1588 }
1589
1590 static void
1591 on_missing_mandatory_data (ModestAccountProtocol *protocol,
1592                            gboolean missing,
1593                            gpointer user_data)
1594 {
1595         real_enable_buttons (MODEST_WIZARD_DIALOG (user_data), !missing);
1596 }
1597
1598 /* After the user details page,
1599  * the following pages depend on whether "Other" was chosen
1600  * in the provider combobox on the account page
1601  */
1602 static void
1603 create_subsequent_pages (ModestEasysetupWizardDialog *self)
1604 {
1605         ModestEasysetupWizardDialogPrivate *priv;
1606         ModestProviderPicker *picker;
1607         ModestProviderPickerIdType id_type;
1608         GtkNotebook *notebook;
1609
1610         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1611         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
1612         id_type = modest_provider_picker_get_active_id_type (picker);
1613         g_object_get (self, "wizard-notebook", &notebook, NULL);
1614         modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1615                                                             NULL);
1616
1617
1618         if (id_type == MODEST_PROVIDER_PICKER_ID_OTHER) {
1619                 /* "Other..." was selected: */
1620
1621                 /* If we come from a rollbacked easysetup */
1622                 if (priv->page_complete_easysetup) {
1623                         remove_non_common_tabs (notebook, FALSE);
1624                         priv->page_complete_easysetup = NULL;
1625                 }
1626
1627                 /* If we come from a rollbacked plugin protocol setup */
1628                 if (priv->last_plugin_protocol_selected != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1629                         remove_non_common_tabs (notebook, TRUE);
1630                         priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1631                         modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1632                         priv->missing_data_signals = NULL;
1633                 }
1634
1635                 create_subsequent_customsetup_pages (self);
1636         } else {
1637                 /* If we come from a rollbacked custom setup */
1638                 if (priv->page_custom_incoming) {
1639                         remove_non_common_tabs (notebook, TRUE);
1640                         init_user_page (priv);
1641                         init_incoming_page (priv);
1642                         init_outgoing_page (priv);
1643                         init_user_page (priv);
1644                         priv->page_complete_customsetup = NULL;
1645                 }
1646
1647                 /* It's a pluggable protocol and not a provider with presets */
1648                 if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
1649                         ModestProtocol *protocol;
1650                         gchar *proto_name;
1651                         ModestProtocolType proto_type;
1652
1653                         /* Get protocol data */
1654                         proto_name = modest_provider_picker_get_active_provider_id (picker);
1655                         protocol = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
1656                                                                                   MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
1657                                                                                   proto_name);
1658                         proto_type = modest_protocol_get_type_id (protocol);
1659
1660
1661                         /* If we come from a rollbacked easy setup */
1662                         if (priv->last_plugin_protocol_selected != proto_type &&
1663                             priv->page_complete_easysetup) {
1664                                 remove_non_common_tabs (notebook, TRUE);
1665                                 init_user_page (priv);
1666                                 priv->page_complete_easysetup = NULL;
1667                         }
1668
1669                         if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol) &&
1670                             proto_type != priv->last_plugin_protocol_selected) {
1671                                 ModestPairList *tabs;
1672                                 GSList *tmp;
1673                                 gboolean first_page = TRUE;
1674                                 ModestWizardDialogResponseOverrideFunc response_override;
1675
1676                                 /* Remember the last selected plugin protocol */
1677                                 priv->last_plugin_protocol_selected = proto_type;
1678
1679                                 /* Get tabs */
1680                                 tabs = modest_account_protocol_get_easysetupwizard_tabs (MODEST_ACCOUNT_PROTOCOL (protocol));
1681                                 response_override = modest_account_protocol_get_wizard_response_override
1682                                         (MODEST_ACCOUNT_PROTOCOL (protocol));
1683                                 modest_wizard_dialog_set_response_override_handler (MODEST_WIZARD_DIALOG (self),
1684                                                                                     response_override);
1685                                 tmp = (GSList *) tabs;
1686                                 while (tmp) {
1687                                         ModestPair *pair = (ModestPair *) tmp->data;
1688                                         modest_easysetup_wizard_dialog_append_page (notebook,
1689                                                                                     GTK_WIDGET (pair->second),
1690                                                                                     (const gchar *) pair->first);
1691                                         if (first_page) {
1692                                                 gtk_container_set_focus_child (GTK_CONTAINER (notebook),
1693                                                                                GTK_WIDGET (pair->second));
1694                                                 first_page = FALSE;
1695                                         }
1696
1697                                         /* Connect signals */
1698                                         if (MODEST_IS_EASYSETUP_WIZARD_PAGE (pair->second)) {
1699                                                 priv->missing_data_signals =
1700                                                         modest_signal_mgr_connect (priv->missing_data_signals,
1701                                                                                    G_OBJECT (pair->second),
1702                                                                                    "missing-mandatory-data",
1703                                                                                    G_CALLBACK (on_missing_mandatory_data),
1704                                                                                    self);
1705                                         }
1706
1707                                         g_free (pair->first);
1708                                         tmp = g_slist_next (tmp);
1709                                         /* Critical: if you don't show the page then the dialog will ignore it */
1710 /*                                      gtk_widget_show (GTK_WIDGET (pair->second)); */
1711                                 }
1712                                 modest_pair_list_free (tabs);
1713                         }
1714                         g_free (proto_name);
1715                 } else {
1716                         if (priv->last_plugin_protocol_selected !=
1717                             MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1718                                 remove_non_common_tabs (notebook, TRUE);
1719                                 init_user_page (priv);
1720                                 priv->page_complete_easysetup = NULL;
1721                                 priv->last_plugin_protocol_selected = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
1722                                 modest_signal_mgr_disconnect_all_and_destroy (priv->missing_data_signals);
1723                                 priv->missing_data_signals = NULL;
1724                         }
1725                         if (!priv->page_user_details) {
1726                                 priv->page_user_details = create_page_user_details (self);
1727                                 modest_easysetup_wizard_dialog_append_page (notebook,
1728                                                                             priv->page_user_details,
1729                                                                             _("mcen_ti_emailsetup_userdetails"));
1730                         }
1731                 }
1732
1733                 /* Create the easysetup pages: */
1734                 create_subsequent_easysetup_pages (self);
1735         }
1736 }
1737
1738
1739 static gchar*
1740 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocolType protocol_type)
1741 {
1742         const gchar* hostname = NULL;
1743         gchar* at;
1744         gchar* domain;
1745         ModestProtocolRegistry *protocol_registry;
1746         ModestProtocol *protocol;
1747
1748         if (!email_address)
1749                 return NULL;
1750
1751         at = g_utf8_strchr (email_address, -1, '@');
1752         if (!at || (g_utf8_strlen (at, -1) < 2))
1753                 return NULL;
1754
1755         domain = g_utf8_next_char (at);
1756         if(!domain)
1757                 return NULL;
1758
1759         protocol_registry = modest_runtime_get_protocol_registry ();
1760         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
1761
1762         if (modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS) ||
1763             modest_protocol_registry_protocol_type_has_tag (protocol_registry, protocol_type, MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS)) {
1764                 hostname = modest_protocol_get_name (protocol);
1765         }
1766
1767         if (!hostname)
1768                 return NULL;
1769
1770         return g_strdup_printf ("%s.%s", hostname, domain);
1771 }
1772
1773 static void
1774 set_default_custom_servernames (ModestEasysetupWizardDialog *self)
1775 {
1776         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1777
1778         if (!priv->entry_incomingserver)
1779                 return;
1780
1781         /* Set a default domain for the server, based on the email address,
1782          * if no server name was already specified.
1783          */
1784         if (priv->entry_user_email
1785             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1786                 const ModestProtocolType protocol_type = modest_servertype_picker_get_active_servertype (
1787                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
1788
1789                 /* This could happen when the combo box has still no active iter */
1790                 if (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
1791                         const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));
1792                         gchar* servername = util_get_default_servername_from_email_address (email_address,
1793                                                                                             protocol_type);
1794
1795                         /* Do not set the INCOMING_CHANGED flag because of this edit */
1796                         g_signal_handlers_block_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1797                         hildon_entry_set_text (HILDON_ENTRY (priv->entry_incomingserver), servername);
1798                         g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), self);
1799
1800                         g_free (servername);
1801                 }
1802         }
1803
1804         /* Set a default domain for the server, based on the email address,
1805          * if no server name was already specified.
1806          */
1807         if (!priv->entry_outgoingserver)
1808                 return;
1809
1810         if (priv->entry_user_email
1811             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1812                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY(priv->entry_user_email));
1813
1814                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOLS_TRANSPORT_SMTP);
1815
1816                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1817                 g_signal_handlers_block_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1818                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_outgoingserver), servername);
1819                 g_signal_handlers_unblock_by_func (G_OBJECT (priv->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), self);
1820
1821                 g_free (servername);
1822         }
1823 }
1824
1825 static gchar*
1826 get_entered_account_title (ModestEasysetupWizardDialog *self)
1827 {
1828         ModestEasysetupWizardDialogPrivate *priv;
1829         const gchar* account_title;
1830
1831         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE(self);
1832         account_title = hildon_entry_get_text (HILDON_ENTRY (priv->entry_account_title));
1833
1834         if (!account_title || (g_utf8_strlen (account_title, -1) == 0)) {
1835                 return NULL;
1836         } else {
1837                 /* Strip it of whitespace at the start and end: */
1838                 gchar *result = g_strdup (account_title);
1839                 result = g_strstrip (result);
1840
1841                 if (!result)
1842                         return NULL;
1843
1844                 if (g_utf8_strlen (result, -1) == 0) {
1845                         g_free (result);
1846                         return NULL;
1847                 }
1848
1849                 return result;
1850         }
1851 }
1852
1853 static gboolean
1854 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1855 {
1856         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1857         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
1858         ModestProtocolRegistry *protocol_registry;
1859
1860         protocol_registry = modest_runtime_get_protocol_registry ();
1861
1862         /* if are browsing pages previous to the last one, then we have pending settings in
1863          * this wizard */
1864         if (next_page != NULL)
1865                 priv->pending_load_settings = TRUE;
1866
1867         /* Do extra validation that couldn't be done for every key press,
1868          * either because it was too slow,
1869          * or because it requires interaction:
1870          */
1871         if (current_page == priv->page_account_details) {
1872                 /* Check that the title is not already in use: */
1873                 gchar* account_title = get_entered_account_title (self);
1874                 if (!account_title)
1875                         return FALSE;
1876
1877                 /* Aavoid a clash with an existing display name: */
1878                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1879                         priv->account_manager, account_title);
1880                 g_free (account_title);
1881
1882                 if (name_in_use) {
1883                         /* Warn the user via a dialog: */
1884                         hildon_banner_show_information(NULL, NULL, _("mail_ib_account_name_already_existing"));
1885
1886                         return FALSE;
1887                 }
1888
1889                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1890                 create_subsequent_pages (self);
1891
1892         } else if (current_page == priv->page_user_details) {
1893                 /* Check that the email address is valud: */
1894                 const gchar* email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
1895                 if ((!email_address) || (g_utf8_strlen (email_address, -1) == 0))
1896                         return FALSE;
1897
1898                 if (!modest_text_utils_validate_email_address (email_address, NULL)) {
1899                         /* Warn the user via a dialog: */
1900                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_email"));
1901
1902                         /* Return focus to the email address entry: */
1903                         gtk_widget_grab_focus (priv->entry_user_email);
1904                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_user_email), 0, -1);
1905
1906                         return FALSE;
1907                 }
1908         } else if (current_page == priv->page_custom_incoming ||
1909                    current_page == priv->page_custom_outgoing) {
1910
1911                 const gchar *hostname;
1912                 GtkWidget *entry = (current_page == priv->page_custom_incoming) ?
1913                         priv->entry_incomingserver :
1914                         priv->entry_outgoingserver;
1915
1916                 hostname = gtk_entry_get_text (GTK_ENTRY (entry));
1917                 if (!modest_text_utils_validate_domain_name (hostname)) {
1918                         /* Show error */
1919                         modest_platform_information_banner (NULL, NULL, _("mcen_ib_invalid_servername"));
1920
1921                         /* Return focus to the email address entry: */
1922                         gtk_widget_grab_focus (entry);
1923                         gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
1924
1925                         return FALSE;
1926                 }
1927         }
1928
1929         if (next_page == priv->page_custom_incoming) {
1930                 set_default_custom_servernames (self);
1931         } else if (next_page == priv->page_custom_outgoing) {
1932                 set_default_custom_servernames (self);
1933
1934                 /* Check if the server supports secure authentication */
1935                 if (modest_security_options_view_auth_check (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security)))
1936                         if (!check_has_supported_auth_methods (self))
1937                                 return FALSE;
1938                 gtk_widget_show (priv->outgoing_security);
1939         }
1940
1941         /* If this is the last page, and this is a click on Finish,
1942          * then attempt to create the dialog.
1943          */
1944         if(!next_page &&
1945            current_page != priv->page_account_details) /* This is NULL when this is a click on Finish. */
1946         {
1947                 if (priv->pending_load_settings) {
1948                         save_to_settings (self);
1949                 }
1950
1951                 /* We check if there's already another account with the same configuration */
1952                 if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
1953                         modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
1954                         return FALSE;
1955                 }
1956
1957                 modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
1958         }
1959
1960
1961         return TRUE;
1962 }
1963
1964 static gboolean entry_is_empty (GtkWidget *entry)
1965 {
1966         if (!entry)
1967                 return FALSE;
1968
1969         const gchar* text = hildon_entry_get_text (HILDON_ENTRY (entry));
1970         if ((!text) || (g_utf8_strlen (text, -1) == 0))
1971                 return TRUE;
1972         else {
1973                 /* Strip it of whitespace at the start and end: */
1974                 gchar *stripped = g_strdup (text);
1975                 stripped = g_strstrip (stripped);
1976
1977                 if (!stripped)
1978                         return TRUE;
1979
1980                 const gboolean result = (g_utf8_strlen (stripped, -1) == 0);
1981
1982                 g_free (stripped);
1983                 return result;
1984         }
1985 }
1986
1987 static void
1988 real_enable_buttons (ModestWizardDialog *dialog, gboolean enable_next)
1989 {
1990         GtkNotebook *notebook = NULL;
1991         gboolean is_finish_tab;
1992         GtkWidget *current;
1993         ModestEasysetupWizardDialogPrivate *priv;
1994
1995         /* Get data */
1996         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (dialog);
1997         g_object_get (dialog, "wizard-notebook", &notebook, NULL);
1998
1999         /* Disable the Finish button until we are on the last page,
2000          * because HildonWizardDialog enables this for all but the
2001          * first page */
2002         current = gtk_notebook_get_nth_page (notebook, gtk_notebook_get_current_page (notebook));
2003         is_finish_tab = ((current == priv->page_complete_easysetup) ||
2004                          (current == priv->page_complete_customsetup));
2005
2006         if (is_finish_tab) {
2007                 /* Disable Next on the last page no matter what the
2008                    argument say */
2009                 enable_next = FALSE;
2010         } else {
2011                 /* Not the last one */
2012                 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
2013                                                    MODEST_WIZARD_DIALOG_FINISH,
2014                                                    FALSE);
2015
2016                 /* If the check support is not done then do not enable
2017                    the wizard to continue */
2018                 enable_next = enable_next && priv->check_support_done;
2019         }
2020
2021         gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
2022                                            MODEST_WIZARD_DIALOG_NEXT,
2023                                            enable_next);
2024 }
2025
2026 static void
2027 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
2028 {
2029         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2030         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2031
2032         gboolean enable_next = TRUE;
2033         if (current_page == priv->page_welcome) {
2034                 enable_next = TRUE;
2035         } else if (current_page == priv->page_account_details) {
2036                 /* The account details title is mandatory: */
2037                 if (entry_is_empty(priv->entry_account_title))
2038                         enable_next = FALSE;
2039         } else if (current_page == priv->page_user_details) {
2040                 /* The user details username is mandatory: */
2041                 if (entry_is_empty(priv->entry_user_username))
2042                         enable_next = FALSE;
2043
2044                 /* The user details email address is mandatory: */
2045                 if (enable_next && entry_is_empty (priv->entry_user_email))
2046                         enable_next = FALSE;
2047         } else if (current_page == priv->page_custom_incoming) {
2048                 /* The custom incoming server is mandatory: */
2049                 if (entry_is_empty(priv->entry_incomingserver))
2050                         enable_next = FALSE;
2051         } else if (MODEST_IS_EASYSETUP_WIZARD_PAGE (current_page)) {
2052                 enable_next = !modest_easysetup_wizard_page_validate (
2053                        MODEST_EASYSETUP_WIZARD_PAGE (current_page));
2054         }
2055
2056         /* Enable/disable buttons */
2057         real_enable_buttons (dialog, enable_next);
2058 }
2059
2060 static void
2061 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
2062 {
2063         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2064         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
2065
2066
2067         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
2068         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
2069
2070         /* Provide a vfunc implementation so we can decide
2071          * when to enable/disable the prev/next buttons.
2072          */
2073         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
2074         base_klass->before_next = on_before_next;
2075         base_klass->enable_buttons = on_enable_buttons;
2076         base_klass->update_model = on_update_model;
2077         base_klass->save = on_save;
2078 }
2079
2080 /**
2081  * save_to_settings:
2082  * @self: a #ModestEasysetupWizardDialog
2083  *
2084  * takes information from all the wizard and stores it in settings
2085  */
2086 static void
2087 save_to_settings (ModestEasysetupWizardDialog *self)
2088 {
2089         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2090         guint special_port;
2091         gchar *provider_id = NULL;
2092         gchar* display_name;
2093         const gchar *username, *password;
2094         gchar *store_hostname, *transport_hostname;
2095         guint store_port, transport_port;
2096         ModestProtocolRegistry *protocol_registry;
2097         ModestProtocolType store_protocol, transport_protocol;
2098         ModestProtocolType store_security, transport_security;
2099         ModestProtocolType store_auth_protocol, transport_auth_protocol;
2100         ModestServerAccountSettings *store_settings, *transport_settings;
2101         const gchar *fullname, *email_address;
2102         ModestProviderPicker *picker;
2103         ModestProviderPickerIdType id_type;
2104
2105         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2106         picker = MODEST_PROVIDER_PICKER (priv->account_serviceprovider_picker);
2107         protocol_registry = modest_runtime_get_protocol_registry ();
2108
2109         /* Get details from the specified presets: */
2110         id_type = modest_provider_picker_get_active_id_type (picker);
2111         provider_id = modest_provider_picker_get_active_provider_id (picker);
2112
2113         /* Let the plugin save the settings. We do a return in order
2114            to save an indentation level */
2115         if (id_type == MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL) {
2116                 ModestProtocol *protocol;
2117
2118                 protocol = modest_protocol_registry_get_protocol_by_name (
2119                        modest_runtime_get_protocol_registry (),
2120                        MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS,
2121                        provider_id);
2122
2123                 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
2124                         gint n_pages, i = 0;
2125                         GtkNotebook *notebook;
2126                         GList *wizard_pages = NULL;
2127
2128                         g_object_get (self, "wizard-notebook", &notebook, NULL);
2129                         n_pages = gtk_notebook_get_n_pages (notebook);
2130                         for (i = 0; i < n_pages; i++) {
2131                                 GtkWidget *page = gtk_notebook_get_nth_page (notebook, i);
2132                                 if (MODEST_IS_EASYSETUP_WIZARD_PAGE (page))
2133                                         wizard_pages  = g_list_append (wizard_pages, page);
2134                         }
2135                         modest_account_protocol_save_wizard_settings (MODEST_ACCOUNT_PROTOCOL (protocol),
2136                                                                       wizard_pages,
2137                                                                       priv->settings);
2138                         g_list_free (wizard_pages);
2139                 } else {
2140                         g_debug ("The selected protocol is a plugin protocol "//
2141                                  "but it's not a ModestAccountProtocol");
2142                 }
2143
2144                 g_free (provider_id);
2145                 return;
2146         }
2147
2148         /* username and password (for both incoming and outgoing): */
2149         username = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username));
2150         password = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_password));
2151
2152         store_settings = modest_account_settings_get_store_settings (priv->settings);
2153         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
2154
2155         /* Incoming server: */
2156         /* Note: We need something as default for the transport store protocol values,
2157          * or modest_account_mgr_add_server_account will fail. */
2158         store_port = 0;
2159         store_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2160         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2161         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2162
2163         if (provider_id) {
2164                 ModestProtocolType store_provider_server_type;
2165                 gboolean store_provider_use_alternate_port;
2166                 /* Use presets: */
2167                 store_hostname = modest_presets_get_server (priv->presets, provider_id,
2168                                                             TRUE /* store */);
2169
2170                 store_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2171                                                                          provider_id,
2172                                                                          TRUE /* store */);
2173                 store_security  = modest_presets_get_info_server_security (priv->presets,
2174                                                                            provider_id,
2175                                                                            TRUE /* store */);
2176                 store_auth_protocol  = modest_presets_get_info_server_auth (priv->presets,
2177                                                                             provider_id,
2178                                                                             TRUE /* store */);
2179                 store_provider_use_alternate_port  = modest_presets_get_info_server_use_alternate_port (priv->presets,
2180                                                                                                         provider_id,
2181                                                                                                         TRUE /* store */);
2182
2183                 /* We don't check for SMTP here as that is impossible for an incoming server. */
2184                 if (store_provider_server_type == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID)
2185                         store_protocol = MODEST_PROTOCOLS_STORE_POP;
2186                 else
2187                         store_protocol = store_provider_server_type;
2188
2189                 /* we check if there is a *special* port */
2190                 special_port = modest_presets_get_port (priv->presets, provider_id, TRUE /* incoming */);
2191                 if (special_port != 0) {
2192                         store_port = special_port;
2193                 } else {
2194                         gboolean use_alternate_port = FALSE;
2195                         if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
2196                                                                               store_security))
2197                                 use_alternate_port = TRUE;
2198                         store_port = get_port_from_protocol(store_provider_server_type, use_alternate_port);
2199                 }
2200
2201                 modest_server_account_settings_set_security_protocol (store_settings,
2202                                                                       store_security);
2203                 modest_server_account_settings_set_auth_protocol (store_settings,
2204                                                                   store_auth_protocol);
2205                 if (store_port != 0)
2206                         modest_server_account_settings_set_port (store_settings, store_port);
2207         } else {
2208                 /* Use custom pages because no preset was specified: */
2209                 store_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_incomingserver) ));
2210                 store_protocol = modest_servertype_picker_get_active_servertype (
2211                         MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
2212
2213                 modest_security_options_view_save_settings (
2214                                     MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security),
2215                                     priv->settings);
2216         }
2217
2218         /* now we store the common store account settings */
2219         modest_server_account_settings_set_hostname (store_settings, store_hostname);
2220         modest_server_account_settings_set_username (store_settings, username);
2221         modest_server_account_settings_set_password (store_settings, password);
2222         modest_server_account_settings_set_protocol (store_settings, store_protocol);
2223
2224         g_object_unref (store_settings);
2225         g_free (store_hostname);
2226
2227         /* Outgoing server: */
2228         transport_hostname = NULL;
2229         transport_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
2230         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
2231         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2232         transport_port = 0;
2233
2234         if (provider_id) {
2235                 ModestProtocolType transport_provider_server_type;
2236                 ModestProtocolType transport_provider_security;
2237                 gboolean is_secure;
2238
2239                 /* Use presets */
2240                 transport_hostname = modest_presets_get_server (priv->presets, provider_id,
2241                                                                 FALSE /* transport */);
2242
2243                 transport_provider_server_type = modest_presets_get_info_server_type (priv->presets,
2244                                                                                       provider_id,
2245                                                                                       FALSE /* transport */);
2246                 transport_provider_security = modest_presets_get_info_server_security (priv->presets,
2247                                                                                        provider_id,
2248                                                                                        FALSE /* transport */);
2249
2250                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
2251                 transport_protocol = transport_provider_server_type;
2252                 transport_security = transport_provider_security;
2253                 is_secure = modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
2254                                                                               transport_security);
2255
2256                 /* we check if there is a *special* port */
2257                 special_port = modest_presets_get_port (priv->presets, provider_id,
2258                                                         FALSE /* transport */);
2259                 if (special_port != 0) {
2260                         transport_port = special_port;
2261                 } else {
2262                         gboolean use_alternate_port = FALSE;
2263                         if (transport_security == MODEST_PROTOCOLS_CONNECTION_SSL)
2264                                 use_alternate_port = TRUE;
2265                         transport_port = get_port_from_protocol(transport_provider_server_type, use_alternate_port);
2266                 }
2267
2268                 if (is_secure)
2269                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
2270                 else
2271                         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
2272
2273                 modest_server_account_settings_set_security_protocol (transport_settings,
2274                                                                       transport_security);
2275                 modest_server_account_settings_set_auth_protocol (transport_settings,
2276                                                                   transport_auth_protocol);
2277                 if (transport_port != 0)
2278                         modest_server_account_settings_set_port (transport_settings,
2279                                                                  transport_port);
2280         } else {
2281                 ModestProtocolRegistry *registry;
2282                 ModestProtocol *store_proto;
2283
2284                 registry = modest_runtime_get_protocol_registry ();
2285                 /* Use custom pages because no preset was specified: */
2286                 transport_hostname = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver) ));
2287
2288                 store_proto = modest_protocol_registry_get_protocol_by_type (registry,
2289                                                                              store_protocol);
2290
2291                 if (transport_protocol == MODEST_PROTOCOL_REGISTRY_TYPE_INVALID) {
2292                         /* fallback to SMTP if none was specified */
2293                         g_warning ("No transport protocol was specified for store %d (%s)",
2294                                    modest_protocol_get_type_id (store_proto),
2295                                    modest_protocol_get_display_name (store_proto));
2296                         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP;
2297                 }
2298
2299                 modest_security_options_view_save_settings (
2300                                     MODEST_SECURITY_OPTIONS_VIEW (priv->outgoing_security),
2301                                     priv->settings);
2302         }
2303
2304         /* now we store the common transport account settings */
2305         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
2306         modest_server_account_settings_set_username (transport_settings, username);
2307         modest_server_account_settings_set_password (transport_settings, password);
2308         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
2309
2310         g_object_unref (transport_settings);
2311         g_free (transport_hostname);
2312
2313         fullname = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_name));
2314         email_address = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_email));
2315         modest_account_settings_set_fullname (priv->settings, fullname);
2316         modest_account_settings_set_email_address (priv->settings, email_address);
2317         /* we don't set retrieve type to preserve advanced settings if
2318            any. By default account settings are set to headers only */
2319
2320         /* Save the connection-specific SMTP server accounts. */
2321         if (priv->checkbox_outgoing_smtp_specific) {
2322                 modest_account_settings_set_use_connection_specific_smtp
2323                         (priv->settings,
2324                          hildon_check_button_get_active(HILDON_CHECK_BUTTON(priv->checkbox_outgoing_smtp_specific)));
2325         }
2326
2327         display_name = get_entered_account_title (self);
2328         modest_account_settings_set_display_name (priv->settings, display_name);
2329         g_free (display_name);
2330         g_free (provider_id);
2331
2332         /* This values are hardcoded for Fremantle */
2333         modest_account_settings_set_retrieve_type (priv->settings,
2334                                                    MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS);
2335         modest_account_settings_set_retrieve_limit (priv->settings, 20);
2336 }
2337
2338
2339 static void
2340 on_update_model (ModestWizardDialog *dialog)
2341 {
2342         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2343         save_to_settings (self);
2344
2345 }
2346
2347 static gboolean
2348 on_save (ModestWizardDialog *dialog)
2349 {
2350         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
2351         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2352
2353         save_to_settings (self);
2354
2355         if (modest_account_mgr_check_already_configured_account (priv->account_manager, priv->settings)) {
2356                 modest_platform_information_banner (NULL, NULL, _("mail_ib_setting_failed"));
2357                 return FALSE;
2358         }
2359
2360         return modest_account_mgr_add_account_from_settings (priv->account_manager, priv->settings);
2361
2362 }
2363
2364
2365 static GList*
2366 check_for_supported_auth_methods (ModestEasysetupWizardDialog* self)
2367 {
2368         GError *error = NULL;
2369         ModestProtocolType protocol_type;
2370         const gchar* hostname;
2371         const gchar* username;
2372         ModestProtocolType security_protocol_incoming_type;
2373         ModestProtocolRegistry *registry;
2374         int port_num;
2375         GList *list_auth_methods;
2376         ModestEasysetupWizardDialogPrivate *priv;
2377
2378         priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2379         registry = modest_runtime_get_protocol_registry ();
2380         protocol_type = modest_servertype_picker_get_active_servertype (
2381                 MODEST_SERVERTYPE_PICKER (priv->incoming_servertype_picker));
2382         hostname = gtk_entry_get_text(GTK_ENTRY(priv->entry_incomingserver));
2383         username = gtk_entry_get_text(GTK_ENTRY(priv->entry_user_username));
2384         security_protocol_incoming_type = modest_security_options_view_get_connection_protocol
2385                 (MODEST_SECURITY_OPTIONS_VIEW (priv->incoming_security));
2386         port_num = get_port_from_protocol(protocol_type, FALSE);
2387         list_auth_methods = modest_utils_get_supported_secure_authentication_methods (protocol_type, hostname, port_num,
2388                                                                                       username, GTK_WINDOW (self), &error);
2389
2390         if (list_auth_methods) {
2391                 /* TODO: Select the correct method */
2392                 GList* list = NULL;
2393                 GList* method;
2394                 for (method = list_auth_methods; method != NULL; method = g_list_next(method)) {
2395                         ModestProtocolType auth_protocol_type = (ModestProtocolType) (GPOINTER_TO_INT(method->data));
2396                         if (modest_protocol_registry_protocol_type_has_tag (registry, auth_protocol_type,
2397                                                                             MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS)) {
2398                                 list = g_list_append(list, GINT_TO_POINTER(auth_protocol_type));
2399                         }
2400                 }
2401
2402                 g_list_free(list_auth_methods);
2403
2404                 if (list)
2405                         return list;
2406         }
2407
2408         if(error == NULL || error->domain != modest_utils_get_supported_secure_authentication_error_quark() ||
2409                         error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED)
2410         {
2411                 modest_platform_information_banner (GTK_WIDGET(self), NULL,
2412                                                     _("mcen_ib_unableto_discover_auth_methods"));
2413         }
2414
2415         if(error != NULL)
2416                 g_error_free(error);
2417
2418         return NULL;
2419 }
2420
2421 static gboolean
2422 check_has_supported_auth_methods(ModestEasysetupWizardDialog* self)
2423 {
2424         GList* methods = check_for_supported_auth_methods(self);
2425         if (!methods)
2426         {
2427                 return FALSE;
2428         }
2429
2430         g_list_free(methods);
2431         return TRUE;
2432 }
2433
2434 static gboolean
2435 check_support_progress_pulse (gpointer userdata)
2436 {
2437         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2438         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2439
2440         if (priv->destroyed) {
2441                 priv->check_support_progress_pulse_id = 0;
2442                 return FALSE;
2443         }
2444
2445         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->check_support_progress));
2446
2447         return TRUE;
2448 }
2449
2450 static gboolean
2451 check_support_show_progress (gpointer userdata)
2452 {
2453         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2454         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2455
2456         priv->check_support_show_progress_id = 0;
2457
2458         if (priv->destroyed)
2459                 return FALSE;
2460
2461         gtk_widget_show (priv->check_support_progress);
2462         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->check_support_progress));
2463
2464         priv->check_support_progress_pulse_id = g_timeout_add (200, check_support_progress_pulse, self);
2465
2466         return FALSE;
2467 }
2468
2469 static void
2470 check_support_callback (ModestAccountProtocol *protocol,
2471                         gboolean supported,
2472                         gpointer userdata)
2473 {
2474         ModestEasysetupWizardDialog *self = (ModestEasysetupWizardDialog *) userdata;
2475         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2476
2477         priv->pending_check_support --;
2478
2479         if (priv->check_support_show_progress_id > 0) {
2480                 g_source_remove (priv->check_support_show_progress_id);
2481                 priv->check_support_show_progress_id = 0;
2482         }
2483
2484         if (priv->check_support_progress_pulse_id > 0) {
2485                 g_source_remove (priv->check_support_progress_pulse_id);
2486                 priv->check_support_progress_pulse_id = 0;
2487         }
2488
2489         if (priv->pending_check_support == 0) {
2490                 priv->check_support_done = TRUE;
2491
2492                 if (!priv->destroyed) {
2493                         if (priv->presets)
2494                                 fill_providers (self);
2495                         gtk_widget_hide (priv->check_support_progress);
2496                         invoke_enable_buttons_vfunc (self);
2497                 }
2498         }
2499         g_object_unref (self);
2500 }
2501
2502
2503 static void
2504 check_support_of_protocols (ModestEasysetupWizardDialog *self)
2505 {
2506         ModestProtocolRegistry *registry;
2507         GSList *provider_protos, *node;
2508         ModestEasysetupWizardDialogPrivate *priv = MODEST_EASYSETUP_WIZARD_DIALOG_GET_PRIVATE (self);
2509
2510         registry = modest_runtime_get_protocol_registry ();
2511         provider_protos = modest_protocol_registry_get_by_tag (registry,
2512                                                                MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
2513
2514         for (node = provider_protos; node != NULL; node = g_slist_next (node)) {
2515                 ModestProtocol *proto = MODEST_PROTOCOL (node->data);
2516
2517                 if (!modest_protocol_registry_protocol_type_has_tag (registry,
2518                                                                      modest_protocol_get_type_id (proto),
2519                                                                      MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS))
2520                         continue;
2521
2522                 if (modest_protocol_registry_protocol_type_has_tag
2523                     (registry,
2524                      modest_protocol_get_type_id (proto),
2525                      MODEST_PROTOCOL_REGISTRY_SINGLETON_PROVIDER_PROTOCOLS)) {
2526                         /* Check if there's already an account configured with this account type */
2527                         if (modest_account_mgr_singleton_protocol_exists (modest_runtime_get_account_mgr (),
2528                                                                           modest_protocol_get_type_id (proto)))
2529                                 continue;
2530                 }
2531
2532                 if (MODEST_ACCOUNT_PROTOCOL (proto)) {
2533                         priv->pending_check_support ++;
2534                         modest_account_protocol_check_support (MODEST_ACCOUNT_PROTOCOL (proto),
2535                                                                check_support_callback,
2536                                                                g_object_ref (self));
2537                 }
2538         }
2539         g_slist_free (provider_protos);
2540
2541         if (priv->pending_check_support > 0) {
2542                 priv->check_support_show_progress_id = g_timeout_add_full (G_PRIORITY_DEFAULT, 1000,
2543                                                                            check_support_show_progress,
2544                                                                            g_object_ref (self), g_object_unref);
2545         } else {
2546                 priv->check_support_done = TRUE;
2547         }
2548         invoke_enable_buttons_vfunc (self);
2549 }