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