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