5919ee39af6b0cad34e10774acafaee573d57d1a
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-easysetup-wizard.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <gtk/gtkseparator.h>
18 #include <hildon-widgets/hildon-caption.h>
19 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
20 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
21 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
22 #include "widgets/modest-serversecurity-combo-box.h"
23 #include "widgets/modest-secureauth-combo-box.h"
24 #include "widgets/modest-validating-entry.h"
25 #include "modest-text-utils.h"
26 #include "modest-account-mgr.h"
27 #include "modest-account-mgr-helpers.h"
28 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
29 #include "maemo/modest-connection-specific-smtp-window.h"
30 #include <gconf/gconf-client.h>
31 #include <string.h> /* For strlen(). */
32
33 /* Include config.h so that _() works: */
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
39
40 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
41
42 #define WIZARD_DIALOG_GET_PRIVATE(o) \
43         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
44
45 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
46
47 struct _ModestEasysetupWizardDialogPrivate
48 {
49         ModestPresets *presets;
50 };
51
52 static void
53 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
54                                                                                                                         GValue *value, GParamSpec *pspec)
55 {
56         switch (property_id) {
57         default:
58                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
59         }
60 }
61
62 static void
63 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
64                                                                                                                         const GValue *value, GParamSpec *pspec)
65 {
66         switch (property_id) {
67         default:
68                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
69         }
70 }
71
72 static void
73 modest_easysetup_wizard_dialog_dispose (GObject *object)
74 {
75         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
76                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
77 }
78
79 static void
80 modest_easysetup_wizard_dialog_finalize (GObject *object)
81 {
82         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
83         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
84         
85         if (self->account_manager)
86                 g_object_unref (G_OBJECT (self->account_manager));
87                 
88         if (priv->presets)
89                 modest_presets_destroy (priv->presets);
90                 
91         if (self->specific_window)
92                 gtk_widget_destroy (self->specific_window);
93         
94         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
95 }
96
97 static void
98 show_error (GtkWindow *parent_window, const gchar* text);
99
100 static gboolean
101 create_account (ModestEasysetupWizardDialog *self);
102
103 static void
104 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
105
106 static void
107 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
108
109 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
110
111 static void
112 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
113 {
114         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
115         
116         /* Call the vfunc, which may be overridden by derived classes: */
117         if (klass->enable_buttons) {
118                 GtkNotebook *notebook = NULL;
119                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
120                 
121                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
122                 if (current_page_num == -1)
123                         return;
124                         
125                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
126                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
127         }
128 }
129
130 static void
131 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
132 {
133         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
134         g_assert(self);
135         invoke_enable_buttons_vfunc(self);
136 }
137
138 static void
139 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
140 {
141         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
142         g_assert(self);
143         invoke_enable_buttons_vfunc(self);
144 }
145
146 /** This is a convenience function to create a caption containing a mandatory widget.
147  * When the widget is edited, the enable_buttons() vfunc will be called.
148  */
149 static GtkWidget* create_caption_new_with_asterix(ModestEasysetupWizardDialog *self,
150         GtkSizeGroup *group,
151         const gchar *value,
152         GtkWidget *control,
153         GtkWidget *icon,
154         HildonCaptionStatus flag)
155 {
156   GtkWidget *caption = hildon_caption_new (group, value, control, icon, flag);
157   
158 /* The translated strings seem to already contain the *,
159  * but this code can be used if that is not true in future.
160  */
161 #if 0
162         /* Add a * character to indicate mandatory fields,
163          * as specified in our "Email UI Specification": */
164         if (flag == HILDON_CAPTION_MANDATORY) {
165                 gchar* title = g_strdup_printf("%s*", value);
166                 caption = hildon_caption_new (group, title, control, icon, flag);       
167                 g_free(title);
168         }       
169         else
170                 caption = hildon_caption_new (group, value, control, icon, flag);
171 #endif
172
173         /* Connect to the appropriate changed signal for the widget, 
174          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
175          */
176         if (GTK_IS_ENTRY (control)) {
177                 g_signal_connect (G_OBJECT (control), "changed",
178                 G_CALLBACK (on_caption_entry_changed), self);
179                 
180         }
181         else if (GTK_IS_COMBO_BOX (control)) {
182                 g_signal_connect (G_OBJECT (control), "changed",
183                 G_CALLBACK (on_caption_combobox_changed), self);
184         }
185          
186         return caption;
187 }
188            
189 static GtkWidget*
190 create_page_welcome (ModestEasysetupWizardDialog *self)
191 {
192         GtkWidget *box = gtk_vbox_new (FALSE, 2);
193         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
194         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
195         gtk_widget_show (label);
196         gtk_widget_show (GTK_WIDGET (box));
197         return GTK_WIDGET (box);
198 }
199
200 static void
201 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
202 {
203         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
204         g_assert(self);
205         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
206         
207         /* Fill the providers combo, based on the selected country: */
208         gint mcc_id = easysetup_country_combo_box_get_active_country_id (
209                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
210         easysetup_provider_combo_box_fill (
211                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, mcc_id);
212 }
213
214 static void
215 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
216 {
217         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
218         g_assert(self);
219         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
220         
221         /* Fill the providers combo, based on the selected country: */
222         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
223                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
224         
225         gchar* domain_name = NULL;
226         if(provider_id)
227           domain_name = modest_presets_get_domain (priv->presets, provider_id);
228         
229         if(!domain_name)
230                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
231                 
232         if (self->entry_user_email)
233                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
234                 
235     g_free (domain_name);
236         
237         g_free (provider_id);
238 }
239
240
241 static GtkWidget*
242 create_page_account_details (ModestEasysetupWizardDialog *self)
243 {
244         GtkWidget *box = gtk_vbox_new (FALSE, 2);
245         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
246         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 2);
247         gtk_widget_show (label);
248         
249         /* Create a size group to be used by all captions.
250          * Note that HildonCaption does not create a default size group if we do not specify one.
251          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
252         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
253
254         /* The country widgets: */
255         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
256         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_country"), 
257                 self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
258         gtk_widget_show (self->combo_account_country);
259         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
260         gtk_widget_show (caption);
261         
262         /* connect to country combo's changed signal, so we can fill the provider combo: */
263     g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
264             G_CALLBACK (on_combo_account_country), self);
265             
266         GtkWidget *separator = gtk_hseparator_new ();
267         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, 2);
268         gtk_widget_show (separator);
269             
270         /* The service provider widgets: */     
271         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
272         
273         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_serviceprovider"), 
274                 self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
275         gtk_widget_show (self->combo_account_serviceprovider);
276         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
277         gtk_widget_show (caption);
278         
279         /* connect to providers combo's changed signal, so we can fill the email address: */
280     g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
281             G_CALLBACK (on_combo_account_serviceprovider), self);
282         
283         /* TODO: Default to the current country somehow.
284          * But I don't know how to get the information that is specified in the 
285          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
286          *
287          * This is probably not the best choice of gconf key:
288          * This is the  "mcc used in the last pairing", ie. the last connection you made.
289      * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
290      *
291          */
292         GConfClient *client = gconf_client_get_default ();
293         GError *error = NULL;
294         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
295         gint mcc_id = gconf_client_get_int(client, key, &error);
296         
297         if(mcc_id < 0)
298         mcc_id = 0;
299      
300     if (error) {
301         g_warning ("Error getting gconf key %s:\n%s", key, error->message);
302         g_error_free (error);
303         error = NULL;
304         
305         mcc_id = 0;
306     }
307     
308     /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
309      * This might just be a Maemo bug.
310      */
311     if (mcc_id == 0) 
312     {
313         /* For now, we default to Finland when there is nothing better: */
314         mcc_id = 244;
315     }
316    
317         easysetup_country_combo_box_set_active_country_id (
318                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
319                 
320         
321         /* The description widgets: */  
322         self->entry_account_title = GTK_WIDGET (easysetup_validating_entry_new ());
323         
324         /* Set a default account title, choosing one that does not already exist: */
325         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
326         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
327         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
328                 self->account_manager, default_account_name_start);
329         g_free (default_account_name_start);
330         default_account_name_start = NULL;
331         
332         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
333         g_free (default_account_name);
334         default_account_name = NULL;
335
336         caption = create_caption_new_with_asterix (self, sizegroup, _("mcen_fi_account_title"), 
337                 self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
338         gtk_widget_show (self->entry_account_title);
339         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
340         gtk_widget_show (caption);
341         
342         /* Prevent the use of some characters in the account title, 
343          * as required by our UI specification: */
344         GList *list_prevent = NULL;
345         list_prevent = g_list_append (list_prevent, "\\");
346         list_prevent = g_list_append (list_prevent, "/");
347         list_prevent = g_list_append (list_prevent, ":");
348         list_prevent = g_list_append (list_prevent, "*");
349         list_prevent = g_list_append (list_prevent, "?");
350         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
351         list_prevent = g_list_append (list_prevent, "“");
352         list_prevent = g_list_append (list_prevent, "<"); 
353         list_prevent = g_list_append (list_prevent, ">"); 
354         list_prevent = g_list_append (list_prevent, "|");
355         list_prevent = g_list_append (list_prevent, "^");       
356         easysetup_validating_entry_set_unallowed_characters (
357                 EASYSETUP_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
358         g_list_free (list_prevent);
359         
360         /* Set max length as in the UI spec:
361          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
362         gtk_entry_set_max_length (GTK_ENTRY (self->entry_account_title), 64);
363         
364         gtk_widget_show (GTK_WIDGET (box));
365         
366         return GTK_WIDGET (box);
367 }
368
369 static GtkWidget*
370 create_page_user_details (ModestEasysetupWizardDialog *self)
371 {
372         GtkWidget *box = gtk_vbox_new (FALSE, 2);
373         
374         /* Create a size group to be used by all captions.
375          * Note that HildonCaption does not create a default size group if we do not specify one.
376          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
377         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
378          
379         /* The name widgets: */
380         self->entry_user_name = GTK_WIDGET (easysetup_validating_entry_new ());
381         /* Set max length as in the UI spec:
382          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
383         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
384         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
385                 _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
386         gtk_widget_show (self->entry_user_name);
387         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
388         gtk_widget_show (caption);
389         
390         /* Prevent the use of some characters in the name, 
391          * as required by our UI specification: */
392         GList *list_prevent = NULL;
393         list_prevent = g_list_append (list_prevent, "<");
394         list_prevent = g_list_append (list_prevent, ">");
395         easysetup_validating_entry_set_unallowed_characters (
396                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
397         g_list_free (list_prevent);
398         
399         /* The username widgets: */     
400         self->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
401         caption = create_caption_new_with_asterix (self, sizegroup, _("mail_fi_username"), 
402                 self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
403         gtk_widget_show (self->entry_user_username);
404         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
405         gtk_widget_show (caption);
406         
407         /* Prevent the use of some characters in the username, 
408          * as required by our UI specification: */
409         easysetup_validating_entry_set_unallowed_characters_whitespace (
410                 EASYSETUP_VALIDATING_ENTRY (self->entry_user_username));
411         
412         /* Set max length as in the UI spec:
413          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
414         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
415         
416         /* The password widgets: */     
417         self->entry_user_password = gtk_entry_new ();
418         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
419         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
420         caption = create_caption_new_with_asterix (self, sizegroup, 
421                 _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
422         gtk_widget_show (self->entry_user_password);
423         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
424         gtk_widget_show (caption);
425         
426         /* The email address widgets: */        
427         self->entry_user_email = GTK_WIDGET (easysetup_validating_entry_new ());
428         caption = create_caption_new_with_asterix (self, sizegroup, 
429                 _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
430         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
431         gtk_widget_show (self->entry_user_email);
432         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
433         gtk_widget_show (caption);
434         
435         /* Set max length as in the UI spec:
436          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
437         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
438         
439         
440         gtk_widget_show (GTK_WIDGET (box));
441         
442         return GTK_WIDGET (box);
443 }
444
445 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
446 {
447         GtkWidget *box = gtk_vbox_new (FALSE, 2);
448         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
449         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
450         gtk_widget_show (label);
451         gtk_widget_show (GTK_WIDGET (box));
452         return GTK_WIDGET (box);
453 }
454
455 /** Change the caption title for the incoming server, 
456  * as specified in the UI spec:
457  */
458 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
459 {
460         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
461                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
462         const gchar* type = 
463                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
464                         _("mail_fi_emailtype_pop3") : 
465                         _("mail_fi_emailtype_imap") );
466                         
467                 
468         /* Note that this produces a compiler warning, 
469          * because the compiler does not know that the translated string will have a %s in it.
470          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
471         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
472         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
473         g_free(incomingserver_title);
474 }
475
476 /** Change the caption title for the incoming server, 
477  * as specified in the UI spec:
478  */
479 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
480 {
481         ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
482                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
483         
484         /* Fill the combo with appropriately titled choices for POP or IMAP. */
485         /* The choices are the same, but the titles are different, as in the UI spec. */
486         modest_serversecurity_combo_box_fill (
487                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
488 }
489
490 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
491 {
492         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
493         update_incoming_server_title (self);
494         update_incoming_server_security_choices (self);
495 }
496            
497 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
498 {
499         GtkWidget *box = gtk_vbox_new (FALSE, 2);
500         
501         /* Create a size group to be used by all captions.
502          * Note that HildonCaption does not create a default size group if we do not specify one.
503          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
504         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
505          
506         /* The incoming server widgets: */
507         if (!self->combo_incoming_servertype)
508                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
509         easysetup_servertype_combo_box_set_active_servertype (
510                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
511         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
512                 _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
513         gtk_widget_show (self->combo_incoming_servertype);
514         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
515         gtk_widget_show (caption);
516         
517         if(!self->entry_incomingserver)
518                 self->entry_incomingserver = gtk_entry_new ();
519         set_default_custom_servernames (self);
520
521         if (self->caption_incoming)
522           gtk_widget_destroy (self->caption_incoming);
523            
524         /* The caption title will be updated in update_incoming_server_title().
525          * so this default text will never be seen: */
526         /* (Note: Changing the title seems pointless. murrayc) */
527         self->caption_incoming = create_caption_new_with_asterix (self, sizegroup, 
528                 "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
529         update_incoming_server_title (self);
530         gtk_widget_show (self->entry_incomingserver);
531         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, 2);
532         gtk_widget_show (self->caption_incoming);
533         
534         /* Change the caption title when the servertype changes, 
535          * as in the UI spec: */
536          g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
537                 G_CALLBACK (on_combo_servertype_changed), self);
538         
539         /* The secure connection widgets: */    
540         if (!self->combo_incoming_security)
541                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
542         update_incoming_server_security_choices (self);
543         modest_serversecurity_combo_box_set_active_serversecurity (
544                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_SECURITY_NONE);
545         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
546                 self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
547         gtk_widget_show (self->combo_incoming_security);
548         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
549         gtk_widget_show (caption);
550         
551         if(!self->checkbox_incoming_auth)
552                 self->checkbox_incoming_auth = 
553                         gtk_check_button_new_with_label (_("mcen_li_emailsetup_secure_authentication"));
554         gtk_box_pack_start (GTK_BOX (box), self->checkbox_incoming_auth, FALSE, FALSE, 2);
555         gtk_widget_show (self->checkbox_incoming_auth);
556         
557         gtk_widget_show (GTK_WIDGET (box));
558         
559         return GTK_WIDGET (box);
560 }
561
562 static void
563 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
564 {
565         GtkWidget *widget = GTK_WIDGET (user_data);
566         
567         /* Enable the widget only if the toggle button is active: */
568         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
569         gtk_widget_set_sensitive (widget, enable);
570 }
571
572 /* Make the sensitivity of a widget depend on a toggle button.
573  */
574 static void
575 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
576 {
577         g_signal_connect (G_OBJECT (button), "toggled",
578                 G_CALLBACK (on_toggle_button_changed), widget);
579         
580         /* Set the starting sensitivity: */
581         on_toggle_button_changed (button, widget);
582 }
583
584 static void
585 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
586 {
587         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
588         
589         /* Create the window, if necessary: */
590         if (!(self->specific_window)) {
591                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
592                 modest_connection_specific_smtp_window_fill_with_connections (
593                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
594                         NULL /* account_name, not known yet. */);
595         }
596
597         /* Show the window: */
598         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
599         gtk_widget_show (self->specific_window);
600 }
601
602 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
603 {
604         GtkWidget *box = gtk_vbox_new (FALSE, 2);
605         
606         /* Create a size group to be used by all captions.
607          * Note that HildonCaption does not create a default size group if we do not specify one.
608          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
609         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
610          
611         /* The outgoing server widgets: */
612         if (!self->entry_outgoingserver)
613                 self->entry_outgoingserver = gtk_entry_new ();
614         GtkWidget *caption = create_caption_new_with_asterix (self, sizegroup, 
615                 _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
616         gtk_widget_show (self->entry_outgoingserver);
617         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
618         gtk_widget_show (caption);
619         set_default_custom_servernames (self);
620         
621         /* The secure connection widgets: */    
622         if (!self->combo_outgoing_security)
623                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
624         modest_serversecurity_combo_box_fill (
625                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
626         modest_serversecurity_combo_box_set_active_serversecurity (
627                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_SECURITY_NONE);
628         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
629                 self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
630         gtk_widget_show (self->combo_outgoing_security);
631         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
632         gtk_widget_show (caption);
633         
634         /* The secure authentication widgets: */
635         if (!self->combo_outgoing_auth)
636                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
637         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
638                 self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
639         gtk_widget_show (self->combo_outgoing_auth);
640         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
641         gtk_widget_show (caption);
642         
643         /* connection-specific checkbox: */
644         if (!self->checkbox_outgoing_smtp_specific) {
645                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new_with_label (_("mcen_fi_advsetup_connection_smtp"));
646                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
647                         FALSE);
648         }
649         gtk_box_pack_start (GTK_BOX (box), self->checkbox_outgoing_smtp_specific, FALSE, FALSE, 2);
650         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
651         
652         GtkWidget *separator = gtk_hseparator_new ();
653         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, 2);
654         gtk_widget_show (separator);
655         
656         /* Connection-specific SMTP-Severs Edit button: */
657         if (!self->button_outgoing_smtp_servers)
658                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
659         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
660                 self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
661         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
662         gtk_widget_show (self->button_outgoing_smtp_servers);
663         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
664         gtk_widget_show (caption);
665         
666         /* Only enable the button when the checkbox is checked: */
667         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
668                 GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
669                 
670         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
671                 G_CALLBACK (on_button_outgoing_smtp_servers), self);
672         
673         
674         gtk_widget_show (GTK_WIDGET (box));
675         
676         return GTK_WIDGET (box);
677 }
678
679 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
680 {
681         GtkWidget *box = gtk_vbox_new (FALSE, 2);
682         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
683         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
684         gtk_widget_show (label);
685         
686         if (!self->button_edit)
687                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
688         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
689                 self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
690         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
691         gtk_widget_show (self->button_edit);
692         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
693         gtk_widget_show (caption);
694         
695         gtk_widget_show (GTK_WIDGET (box));
696         return GTK_WIDGET (box);
697 }
698
699
700 /*
701  */
702 static void 
703 on_response (ModestWizardDialog *wizard_dialog,
704         gint response_id,
705         gpointer user_data)
706 {
707         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
708         invoke_enable_buttons_vfunc (self);
709 }
710
711 static void
712 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
713 {
714         /* Create the notebook to be used by the ModestWizardDialog base class:
715          * Each page of the notebook will be a page of the wizard: */
716         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
717         
718     /* Set the notebook used by the ModestWizardDialog base class: */
719     g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
720     
721     /* Set the wizard title:
722      * The actual window title will be a combination of this and the page's tab label title. */
723     g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
724
725         /* Read in the information about known service providers: */
726         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
727         
728         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
729         priv->presets = modest_presets_new (filepath);
730         if (!(priv->presets)) {
731                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
732         }
733         
734         g_assert(priv->presets);
735         
736         
737         /* Get the account manager object, 
738          * so we can check for existing accounts,
739          * and create new accounts: */
740         self->account_manager = modest_runtime_get_account_mgr ();
741         g_assert (self->account_manager);
742         g_object_ref (self->account_manager);
743         
744     /* Create the common pages, 
745      */
746     self->page_welcome = create_page_welcome (self);
747         self->page_account_details = create_page_account_details (self);
748         self->page_user_details = create_page_user_details (self);
749         
750         /* Add the common pages: */
751         gtk_notebook_append_page (notebook, self->page_welcome, 
752                 gtk_label_new (_("mcen_ti_emailsetup_welcome")));
753         gtk_notebook_append_page (notebook, self->page_account_details, 
754                 gtk_label_new (_("mcen_ti_accountdetails")));
755         gtk_notebook_append_page (notebook, self->page_user_details, 
756                 gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
757                 
758         /* Create and add the easysetup-specific pages,
759          * because we need _some_ final page to enable the Next and Finish buttons: */
760         create_subsequent_easysetup_pages (self);
761
762             
763     /* Connect to the dialog's response signal so we can enable/disable buttons 
764      * for the newly-selected page, because the prev/next buttons cause response to be emitted.
765      * Note that we use g_signal_connect_after() instead of g_signal_connect()
766      * so that we can be enable/disable after ModestWizardDialog has done its own 
767      * enabling/disabling of buttons.
768      * 
769      * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
770      * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
771      * 
772      * It's not enough to connect to the notebook's switch-page signal, because 
773      * ModestWizardDialog's "response" signal handler enables the buttons itself, 
774      * _after_ switching the page (understandably).
775      * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
776      * then gtk_notebook_get_current_page() would return an incorrect value.)
777      */
778     g_signal_connect_after (G_OBJECT (self), "response",
779             G_CALLBACK (on_response), self);       
780 }
781
782 ModestEasysetupWizardDialog*
783 modest_easysetup_wizard_dialog_new (void)
784 {
785         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
786 }
787
788 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
789 {
790         GtkNotebook *notebook = NULL;
791         g_object_get (self, "wizard-notebook", &notebook, NULL);
792         g_assert(notebook);
793         
794         /* Create the custom pages: */
795         if(!(self->page_custom_incoming)) {
796                 self->page_custom_incoming = create_page_custom_incoming (self);
797         }
798                 
799         if(!(self->page_custom_outgoing)) {
800                 self->page_custom_outgoing = create_page_custom_outgoing (self);
801         }
802         
803         if(!(self->page_complete_customsetup)) {
804                 self->page_complete_customsetup = create_page_complete_custom (self);
805         }
806         
807         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
808                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
809                         gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
810         
811         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
812                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
813                         gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
814                 
815         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
816                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
817                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
818 }
819         
820 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
821 {
822         GtkNotebook *notebook = NULL;
823         g_object_get (self, "wizard-notebook", &notebook, NULL);
824         g_assert(notebook);
825         
826         /* Create the easysetup-specific pages: */
827         if(!self->page_complete_easysetup)
828                 self->page_complete_easysetup = create_page_complete_easysetup (self);
829
830         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
831                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
832                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
833                         
834 }
835 /* After the user details page,
836  * the following pages depend on whether "Other" was chosen 
837  * in the provider combobox on the account page
838  */
839 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
840 {
841         if (easysetup_provider_combo_box_get_active_provider_id (
842                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
843                 /* "Other..." was selected: */
844                 
845                 /* Make sure that the easysetup pages do not exist: */
846                 if(self->page_complete_easysetup) {
847                         gtk_widget_destroy (self->page_complete_easysetup);
848                         self->page_complete_easysetup = NULL;
849                 }
850                 
851                 create_subsequent_customsetup_pages (self);
852         }       
853         else {
854                 /* A specific provider was selected: */
855                 {
856                         /* Make sure that the custom pages do not exist:
857                          * Because they will be used if they exist, when creating the account. */
858                         if(self->page_custom_incoming) {
859                                 gtk_widget_destroy (self->page_custom_incoming);
860                                 self->page_custom_incoming = NULL;
861                         }
862                         
863                         if(self->page_custom_outgoing) {
864                                 gtk_widget_destroy (self->page_custom_outgoing);
865                                 self->page_custom_outgoing = NULL;
866                         }
867                         
868                         if(self->page_complete_customsetup) {
869                                 gtk_widget_destroy (self->page_complete_customsetup);
870                                 self->page_complete_customsetup = NULL;
871                         }
872                 }
873                 
874                 /* Create the easysetup pages: */
875                 create_subsequent_easysetup_pages (self);
876         }
877 }
878
879
880 static gchar*
881 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
882 {
883         if (!email_address)
884                 return NULL;
885         
886         gchar* at = g_utf8_strchr (email_address, -1, '@');
887         if (!at || (g_utf8_strlen (at, -1) < 2))
888                 return NULL;
889                 
890         gchar* domain = g_utf8_next_char (at);
891         if(!domain)
892                 return NULL;
893                 
894         const gchar* hostname = NULL;
895         if (servertype == MODEST_PROTOCOL_STORE_POP)
896                 hostname = "pop";
897         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
898                 hostname = "imap";
899         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
900                 hostname = "smtp";
901         
902         if (!hostname)
903                 return NULL;
904                 
905         return g_strdup_printf ("%s.%s", hostname, domain);
906 }
907
908 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
909 {
910         if (!account_wizard->entry_incomingserver)
911                 return;
912                 
913         /* Set a default domain for the server, based on the email address,
914          * if no server name was already specified.
915          */
916         const gchar* incoming_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_incomingserver));
917         if ((!incoming_existing || (strlen(incoming_existing) == 0)) 
918                 && account_wizard->entry_user_email) {
919                 const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
920                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
921                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
922                 
923                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
924                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
925                 g_free (servername);
926         }
927         
928         /* Set a default domain for the server, based on the email address,
929          * if no server name was already specified.
930          */
931         if (!account_wizard->entry_outgoingserver)
932                 return;
933                 
934         const gchar* outgoing_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_outgoingserver));
935         if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
936                 && account_wizard->entry_user_email) {
937                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
938                 
939                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
940                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
941                 g_free (servername);
942         }
943 }
944
945 static gboolean
946 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
947 {
948         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
949         
950         /* Do extra validation that couldn't be done for every key press,
951          * either because it was too slow,
952          * or because it requires interaction:
953          */
954         if (current_page == account_wizard->page_account_details) {     
955                 /* Check that the title is not already in use: */
956                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
957                 if ((!account_title) || (strlen(account_title) == 0))
958                         return FALSE;
959                         
960                 /* Aavoid a clash with an existing display name: */
961                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
962                         account_wizard->account_manager, account_title);
963
964                 if (name_in_use) {
965                         /* Warn the user via a dialog: */
966                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
967             
968                         return FALSE;
969                 }
970         }
971         else if (current_page == account_wizard->page_user_details) {
972                 /* Check that the email address is valud: */
973                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
974                 if ((!email_address) || (strlen(email_address) == 0))
975                         return FALSE;
976                         
977                 if (!modest_text_utils_validate_email_address (email_address)) {
978                         /* Warn the user via a dialog: */
979                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
980                                              
981             /* Return focus to the email address entry: */
982             gtk_widget_grab_focus (account_wizard->entry_user_email);
983             
984                         return FALSE;
985                 }
986                 
987                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
988                 create_subsequent_pages (account_wizard);
989         }
990         
991         /* TODO: The UI Spec wants us to check that the servernames are valid, 
992          * but does not specify how.
993          */
994           
995         if(next_page == account_wizard->page_custom_incoming) {
996                 set_default_custom_servernames (account_wizard);
997         }
998         else if (next_page == account_wizard->page_custom_outgoing) {
999                 set_default_custom_servernames (account_wizard);
1000         }
1001         
1002         /* If this is the last page, and this is a click on Finish, 
1003          * then attempt to create the dialog.
1004          */
1005         if(!next_page) /* This is NULL when this is a click on Finish. */
1006         {
1007                 create_account (account_wizard);
1008         }
1009         
1010         
1011         return TRUE;
1012 }
1013
1014 static gboolean entry_is_empty (GtkWidget *entry)
1015 {
1016         if (!entry)
1017                 return FALSE;
1018                 
1019         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1020         if ((!text) || (strlen(text) == 0))
1021                 return TRUE;
1022         else
1023                 return FALSE;
1024 }
1025
1026 static void
1027 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1028 {
1029         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1030         
1031         gboolean enable_next = TRUE;
1032         if (current_page == account_wizard->page_welcome) {
1033                 enable_next = TRUE;
1034         }
1035         else if (current_page == account_wizard->page_account_details) {
1036                 /* The account details title is mandatory: */
1037                 if (entry_is_empty(account_wizard->entry_account_title))
1038                         enable_next = FALSE;
1039         }
1040         else if (current_page == account_wizard->page_user_details) {   
1041                 /* The user details username is mandatory: */
1042                 if (entry_is_empty(account_wizard->entry_user_username))
1043                         enable_next = FALSE;
1044                         
1045                 /* The user details email address is mandatory: */
1046                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1047                         enable_next = FALSE;
1048         }
1049         else if (current_page == account_wizard->page_custom_incoming) {
1050                 /* The custom incoming server is mandatory: */
1051                 if (entry_is_empty(account_wizard->entry_incomingserver))
1052                         enable_next = FALSE;
1053         }
1054                         
1055         /* Enable the buttons, 
1056          * identifying them via their associated response codes:
1057          */
1058         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1059     gtk_dialog_set_response_sensitive (dialog_base,
1060                                        MODEST_WIZARD_DIALOG_NEXT,
1061                                        enable_next);
1062                                        
1063     /* Disable the Finish button until we are on the last page,
1064      * because HildonWizardDialog enables this for all but the first page: */
1065     GtkNotebook *notebook = NULL;
1066         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1067         
1068     gint current = gtk_notebook_get_current_page (notebook);
1069     gint last = gtk_notebook_get_n_pages (notebook) - 1;
1070     gboolean is_last = (current == last);
1071     
1072     if(!is_last) {
1073         gtk_dialog_set_response_sensitive (dialog_base,
1074                                        MODEST_WIZARD_DIALOG_FINISH,
1075                                        FALSE);
1076     }
1077 }
1078
1079 static void
1080 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1081 {
1082         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1083         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1084
1085
1086         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1087         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1088         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1089         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1090         
1091         /* Provide a vfunc implementation so we can decide 
1092          * when to enable/disable the prev/next buttons.
1093          */
1094         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1095         base_klass->before_next = on_before_next;
1096         base_klass->enable_buttons = on_enable_buttons;
1097 }
1098  
1099 static void
1100 show_error (GtkWindow *parent_window, const gchar* text)
1101 {
1102         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1103                 (GtkDialogFlags)0,
1104                  GTK_MESSAGE_ERROR,
1105                  GTK_BUTTONS_OK,
1106                  text ));
1107                  
1108                  gtk_dialog_run (dialog);
1109                  gtk_widget_destroy (GTK_WIDGET (dialog));
1110 }
1111
1112 /** Attempt to create the account from the information that the user has entered.
1113  * @result: TRUE if the account was successfully created.
1114  */
1115 gboolean
1116 create_account (ModestEasysetupWizardDialog *self)
1117 {
1118         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1119         
1120         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1121
1122         /* Some checks: */
1123         if (!display_name)
1124                 return FALSE;
1125                 
1126         /* We should have checked for this already, 
1127          * and changed that name accordingly, 
1128          * but let's check again just in case:
1129          */
1130         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1131                 return FALSE;
1132
1133         /* Increment the non-user visible name if necessary, 
1134          * based on the display name: */
1135         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1136         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1137                 account_name_start, FALSE /* not a server account */);
1138         g_free (account_name_start);
1139                 
1140         /* username and password (for both incoming and outgoing): */
1141         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1142         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1143         
1144         /* Incoming server: */
1145         /* Note: We need something as default for the ModestProtocol values, 
1146          * or modest_account_mgr_add_server_account will fail. */
1147         gchar* servername_incoming = NULL;
1148         ModestProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1149         ModestProtocol protocol_security_incoming = MODEST_PROTOCOL_SECURITY_NONE;
1150         ModestProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1151         
1152         /* Get details from the specified presets: */
1153         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1154                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1155         if(provider_id) {
1156                 /* Use presets: */
1157                 
1158                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1159                         TRUE /* incoming */);
1160                 
1161                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets, provider_id, 
1162                         TRUE /* incoming */);
1163                 
1164         
1165                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1166                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP)
1167                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1168                 else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP)
1169                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1170                                 
1171                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets, provider_id, 
1172                         TRUE /* incoming */);
1173                         
1174                 
1175                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1176                         protocol_security_incoming = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1177                 
1178                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1179                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1180         }
1181         else {
1182                 /* Use custom pages because no preset was specified: */
1183                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1184                 
1185                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1186                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1187                 
1188                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1189                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1190                 
1191                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1192                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1193                         : MODEST_PROTOCOL_AUTH_NONE;
1194                 
1195         }
1196         
1197         /* First we add the 2 server accounts, and then we add the account that uses them.
1198          * If we don't do it in this order then we will experience a crash. */
1199          
1200         /* Add a (incoming) server account, to be used by the account: */
1201         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1202         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1203                 store_name_start, TRUE /* server account */);
1204         g_free (store_name_start);
1205         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1206                 store_name,
1207                 servername_incoming,
1208                 username, password,
1209                 protocol_incoming,
1210                 protocol_security_incoming,
1211                 protocol_authentication_incoming);              
1212                 
1213         g_free (servername_incoming);
1214         
1215         if (!created) {
1216                 /* TODO: Provide a Logical ID for the text: */
1217                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1218                 return FALSE;   
1219         }
1220         
1221         /* Sanity check: */
1222         /* There must be at least one account now: */
1223         GSList *account_names = modest_account_mgr_account_names (self->account_manager);
1224         if(!account_names)
1225         {
1226                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1227         }
1228         g_slist_free (account_names);
1229         
1230         
1231         /* Outgoing server: */
1232         gchar* servername_outgoing = NULL;
1233         ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1234         ModestProtocol protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1235         ModestProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1236         
1237         if(provider_id) {
1238                 /* Use presets: */
1239                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1240                         FALSE /* incoming */);
1241                         
1242                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets, provider_id, 
1243                         FALSE /* incoming */);
1244                 
1245                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1246                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1247                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1248                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* TODO: Is this what we want? */
1249                 
1250                 ModestPresetsSecurity security_outgoing = 
1251                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1252                                 FALSE /* incoming */);
1253                         
1254                 protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1255                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP)
1256                         protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1257                 
1258                 protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1259                 /* TODO: There is no SMTP authentication enum for presets. */
1260         }
1261         else {
1262                 /* Use custom pages because no preset was specified: */
1263                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1264                 
1265                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1266
1267                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1268                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1269                 
1270                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1271                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1272                 
1273                 /* TODO: 
1274                 gboolean specific = gtk_toggle_button_get_active (
1275                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
1276                 */
1277                 
1278         }
1279             
1280         /* Add a (outgoing) server account to be used by the account: */
1281         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1282         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1283                 transport_name_start, TRUE /* server account */);
1284         g_free (transport_name_start);
1285         created = modest_account_mgr_add_server_account (self->account_manager,
1286                 transport_name,
1287                 servername_outgoing,
1288                 username, password,
1289                 protocol_outgoing,
1290                 protocol_security_outgoing,
1291                 protocol_authentication_outgoing);
1292                 
1293         g_free (servername_outgoing);
1294                 
1295         if (!created) {
1296                 /* TODO: Provide a Logical ID for the text: */
1297                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1298                 return FALSE;   
1299         }
1300         
1301         
1302         /* Create the account, which will contain the two "server accounts": */
1303         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1304                 store_name, /* The name of our POP/IMAP server account. */
1305                 transport_name /* The name of our SMTP server account. */);
1306         g_free (store_name);
1307         g_free (transport_name);
1308         
1309         if (!created) {
1310                 /* TODO: Provide a Logical ID for the text: */
1311                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1312                 return FALSE;   
1313         }
1314         
1315         /* The user name and email address must be set additionally: */
1316         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1317         modest_account_mgr_set_string (self->account_manager, account_name,
1318                 MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1319
1320         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1321         modest_account_mgr_set_string (self->account_manager, account_name,
1322                 MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1323
1324         /* Set the display name: */
1325         modest_account_mgr_set_string (self->account_manager, account_name,
1326                 MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1327
1328         /* Save the connection-specific SMTP server accounts. */
1329         if (self->specific_window)
1330                 return modest_connection_specific_smtp_window_save_server_accounts (
1331                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1332                         
1333         g_free (account_name);
1334
1335         return FALSE;
1336 }
1337
1338