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