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