2007-04-03 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6
7 #include "modest-easysetup-wizard.h"
8 #include <glib/gi18n.h>
9 #include <gtk/gtknotebook.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtklabel.h>
12 #include <gtk/gtkcombobox.h>
13 #include <gtk/gtkentry.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkcheckbutton.h>
16 #include <gtk/gtkmessagedialog.h>
17 #include <hildon-widgets/hildon-caption.h>
18 #include "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.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         
675         const gchar* filepath = "/usr/share/operator-wizard/provider-data.keyfile";
676         priv->presets = modest_presets_new (filepath); /* TODO: the actual filepath. */
677         if (!(priv->presets))
678         {
679                 const gchar* filepath_hack = "./src/maemo/easysetup/provider-data-test.keyfile";
680                 g_warning ("Could not locate the official provider data keyfile from %s, "
681                         "so attempting to load it instead from %s", filepath, filepath_hack);
682                 priv->presets = modest_presets_new (filepath_hack); /* TODO: the actual filepath. */
683         }
684         
685         g_assert(priv->presets);
686         
687         
688         /* Create the account manager object, 
689          * so we can check for existing accounts,
690          * and create new accounts: */
691         ModestConf *conf = modest_conf_new (); /* Just a thin wrapper around GConfClient. */
692         self->account_manager = modest_account_mgr_new (conf);
693         g_object_unref (conf);
694         
695     /* Create the common pages, 
696      */
697     self->page_welcome = create_page_welcome (self);
698         self->page_account_details = create_page_account_details (self);
699         self->page_user_details = create_page_user_details (self);
700         
701         /* Add the common pages: */
702         gtk_notebook_append_page (notebook, self->page_welcome, 
703                 gtk_label_new (_("mcen_ti_emailsetup_welcome")));
704         gtk_notebook_append_page (notebook, self->page_account_details, 
705                 gtk_label_new (_("mcen_ti_accountdetails")));
706         gtk_notebook_append_page (notebook, self->page_user_details, 
707                 gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
708                 
709         /* Create and add the easysetup-specific pages,
710          * because we need _some_ final page to enable the Next and Finish buttons: */
711         create_subsequent_easysetup_pages (self);
712
713             
714     /* Connect to the dialog's response signal so we can enable/disable buttons 
715      * for the newly-selected page, because the prev/next buttons cause response to be emitted.
716      * Note that we use g_signal_connect_after() instead of g_signal_connect()
717      * so that we can be enable/disable after ModestWizardDialog has done its own 
718      * enabling/disabling of buttons.
719      * 
720      * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
721      * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
722      * 
723      * It's not enough to connect to the notebook's switch-page signal, because 
724      * ModestWizardDialog's "response" signal handler enables the buttons itself, 
725      * _after_ switching the page (understandably).
726      * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
727      * then gtk_notebook_get_current_page() would return an incorrect value.)
728      */
729     g_signal_connect_after (G_OBJECT (self), "response",
730             G_CALLBACK (on_response), self);       
731 }
732
733 ModestEasysetupWizardDialog*
734 modest_easysetup_wizard_dialog_new (void)
735 {
736         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
737 }
738
739 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
740 {
741         GtkNotebook *notebook = NULL;
742         g_object_get (self, "wizard-notebook", &notebook, NULL);
743         g_assert(notebook);
744         
745         /* Create the custom pages: */
746         if(!(self->page_custom_incoming)) {
747                 self->page_custom_incoming = create_page_custom_incoming (self);
748         }
749                 
750         if(!(self->page_custom_outgoing)) {
751                 self->page_custom_outgoing = create_page_custom_outgoing (self);
752         }
753         
754         if(!(self->page_complete_customsetup)) {
755                 self->page_complete_customsetup = create_page_complete_custom (self);
756         }
757         
758         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
759                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
760                         gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
761         
762         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
763                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
764                         gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
765                 
766         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
767                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
768                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
769 }
770         
771 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
772 {
773         GtkNotebook *notebook = NULL;
774         g_object_get (self, "wizard-notebook", &notebook, NULL);
775         g_assert(notebook);
776         
777         /* Create the easysetup-specific pages: */
778         if(!self->page_complete_easysetup)
779                 self->page_complete_easysetup = create_page_complete_easysetup (self);
780
781         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
782                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
783                         gtk_label_new (_("mcen_ti_emailsetup_complete")));
784                         
785 }
786 /* After the user details page,
787  * the following pages depend on whether "Other" was chosen 
788  * in the provider combobox on the account page
789  */
790 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
791 {
792         if (easysetup_provider_combo_box_get_active_provider_id (
793                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
794                 /* "Other..." was selected: */
795                 
796                 /* Make sure that the easysetup pages do not exist: */
797                 if(self->page_complete_easysetup) {
798                         gtk_widget_destroy (self->page_complete_easysetup);
799                         self->page_complete_easysetup = NULL;
800                 }
801                 
802                 create_subsequent_customsetup_pages (self);
803         }       
804         else {
805                 /* A specific provider was selected: */
806                 {
807                         /* Make sure that the custom pages do not exist:
808                          * Because they will be used if they exist, when creating the account. */
809                         if(self->page_custom_incoming) {
810                                 gtk_widget_destroy (self->page_custom_incoming);
811                                 self->page_custom_incoming = NULL;
812                         }
813                         
814                         if(self->page_custom_outgoing) {
815                                 gtk_widget_destroy (self->page_custom_outgoing);
816                                 self->page_custom_outgoing = NULL;
817                         }
818                         
819                         if(self->page_complete_customsetup) {
820                                 gtk_widget_destroy (self->page_complete_customsetup);
821                                 self->page_complete_customsetup = NULL;
822                         }
823                 }
824                 
825                 /* Create the easysetup pages: */
826                 create_subsequent_easysetup_pages (self);
827         }
828 }
829
830 gchar*
831 util_get_default_servername_from_email_address (const gchar* email_address, ModestProtocol servertype)
832 {
833         if (!email_address)
834                 return NULL;
835         
836         gchar* at = g_utf8_strchr (email_address, -1, '@');
837         if (!at || (g_utf8_strlen (at, -1) < 2))
838                 return NULL;
839                 
840         gchar* domain = g_utf8_next_char (at);
841         if(!domain)
842                 return NULL;
843                 
844         const gchar* hostname = NULL;
845         if (servertype == MODEST_PROTOCOL_STORE_POP)
846                 hostname = "pop";
847         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
848                 hostname = "imap";
849         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
850                 hostname = "smtp";
851         
852         if (!hostname)
853                 return NULL;
854                 
855         return g_strdup_printf ("%s.%s", hostname, domain);
856 }
857
858 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
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* incoming_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_incomingserver));
864         if ((!incoming_existing || (strlen(incoming_existing) == 0)) 
865                 && account_wizard->entry_user_email) {
866                 const ModestProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
867                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
868                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
869                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), util_get_default_servername_from_email_address (email_address, protocol));
870         }
871         
872         /* Set a default domain for the server, based on the email address,
873          * if no server name was already specified.
874          */
875         const gchar* outgoing_existing = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_outgoingserver));
876         if ((!outgoing_existing || (strlen(outgoing_existing) == 0)) 
877                 && account_wizard->entry_user_email) {
878                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
879                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP));
880         }
881 }
882
883 static gboolean
884 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
885 {
886         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
887         
888         /* Do extra validation that couldn't be done for every key press,
889          * either because it was too slow,
890          * or because it requires interaction:
891          */
892         if (current_page == account_wizard->page_account_details) {     
893                 /* Check that the title is not already in use: */
894                 const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
895                 if ((!account_name) || (strlen(account_name) == 0))
896                         return FALSE;
897                         
898                 gboolean name_in_use = FALSE;
899                 name_in_use = modest_account_mgr_account_exists (account_wizard->account_manager,
900                         account_name, TRUE /*  server_account */);
901                 
902                 if (name_in_use) {
903                         /* Warn the user via a dialog: */
904                         /* TODO: The UI spec says we should increment the title,
905                          * as well as warning. This seems contradictory.
906                          */
907                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing."));
908             
909                         return FALSE;
910                 }
911         }
912         else if (current_page == account_wizard->page_user_details) {
913                 /* Check that the email address is valud: */
914                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
915                 if ((!email_address) || (strlen(email_address) == 0))
916                         return FALSE;
917                         
918                 if (!modest_text_utils_validate_email_address (email_address)) {
919                         /* Warn the user via a dialog: */
920                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
921                                              
922             /* Return focus to the email address entry: */
923             gtk_widget_grab_focus (account_wizard->entry_user_email);
924             
925                         return FALSE;
926                 }
927                 
928                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
929                 create_subsequent_pages (account_wizard);
930         }
931         
932         /* TODO: The UI Spec wants us to check that the servernames are valid, 
933          * but does not specify how.
934          */
935           
936         if(next_page == account_wizard->page_custom_incoming) {
937                 set_default_custom_servernames (account_wizard);
938         }
939         else if (next_page == account_wizard->page_custom_outgoing) {
940                 set_default_custom_servernames (account_wizard);
941         }
942         
943         /* If this is the last page, and this is a click on Finish, 
944          * then attempt to create the dialog.
945          */
946         if(!next_page) /* This is NULL when this is a click on Finish. */
947         {
948                 create_account (account_wizard);
949         }
950         
951         
952         return TRUE;
953 }
954
955 static gboolean entry_is_empty (GtkWidget *entry)
956 {
957         if (!entry)
958                 return FALSE;
959                 
960         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
961         if ((!text) || (strlen(text) == 0))
962                 return TRUE;
963         else
964                 return FALSE;
965 }
966
967 static void
968 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
969 {
970         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
971         
972         gboolean enable_next = TRUE;
973         if (current_page == account_wizard->page_welcome) {
974                 enable_next = TRUE;
975         }
976         else if (current_page == account_wizard->page_account_details) {
977                 /* The account details title is mandatory: */
978                 if (entry_is_empty(account_wizard->entry_account_title))
979                         enable_next = FALSE;
980         }
981         else if (current_page == account_wizard->page_user_details) {   
982                 /* The user details username is mandatory: */
983                 if (entry_is_empty(account_wizard->entry_user_username))
984                         enable_next = FALSE;
985                         
986                 /* The user details email address is mandatory: */
987                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
988                         enable_next = FALSE;
989         }
990         else if (current_page == account_wizard->page_custom_incoming) {
991                 /* The custom incoming server is mandatory: */
992                 if (entry_is_empty(account_wizard->entry_incomingserver))
993                         enable_next = FALSE;
994         }
995                         
996         /* Enable the buttons, 
997          * identifying them via their associated response codes:
998          */
999         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1000     gtk_dialog_set_response_sensitive (dialog_base,
1001                                        MODEST_WIZARD_DIALOG_NEXT,
1002                                        enable_next);
1003                                        
1004     /* Disable the Finish button until we are on the last page,
1005      * because HildonWizardDialog enables this for all but the first page: */
1006     GtkNotebook *notebook = NULL;
1007         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1008         
1009     gint current = gtk_notebook_get_current_page (notebook);
1010     gint last = gtk_notebook_get_n_pages (notebook) - 1;
1011     gboolean is_last = (current == last);
1012     
1013     if(!is_last) {
1014         gtk_dialog_set_response_sensitive (dialog_base,
1015                                        MODEST_WIZARD_DIALOG_FINISH,
1016                                        FALSE);
1017     }
1018 }
1019
1020 static void
1021 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1022 {
1023         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1024         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1025
1026
1027         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1028         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1029         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1030         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1031         
1032         /* Provide a vfunc implementation so we can decide 
1033          * when to enable/disable the prev/next buttons.
1034          */
1035         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1036         base_klass->before_next = on_before_next;
1037         base_klass->enable_buttons = on_enable_buttons;
1038 }
1039  
1040 static void
1041 show_error (GtkWindow *parent_window, const gchar* text)
1042 {
1043         GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1044                 (GtkDialogFlags)0,
1045                  GTK_MESSAGE_ERROR,
1046                  GTK_BUTTONS_OK,
1047                  text ));
1048                  
1049                  gtk_dialog_run (dialog);
1050                  gtk_widget_destroy (GTK_WIDGET (dialog));
1051 }
1052
1053 /** Attempt to create the account from the information that the user has entered.
1054  * @result: TRUE if the account was successfully created.
1055  */
1056 gboolean
1057 create_account (ModestEasysetupWizardDialog *self)
1058 {
1059         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1060         
1061         const gchar* account_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1062
1063         /* Some checks: */
1064         if (!account_name)
1065                 return FALSE;
1066                 
1067         /* We should have checked for this already, 
1068          * and changed that name accordingly, 
1069          * but let's check again just in case:
1070          */
1071         if (modest_account_mgr_account_exists (self->account_manager, account_name, FALSE)) 
1072                 return FALSE;
1073                 
1074         /* username and password (for both incoming and outgoing): */
1075         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1076         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1077         
1078         /* Incoming server: */
1079         /* Note: We need something as default for the ModestProtocol values, 
1080          * or modest_account_mgr_add_server_account will fail. */
1081         gchar* servername_incoming = NULL;
1082         ModestProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1083         ModestProtocol protocol_security_incoming = MODEST_PROTOCOL_SECURITY_NONE;
1084         ModestProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1085         
1086         /* Get details from the specified presets: */
1087         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1088                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1089         if(provider_id) {
1090                 /* Use presets: */
1091                 
1092                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1093                         TRUE /* incoming */);
1094                 
1095                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets, provider_id, 
1096                         TRUE /* incoming */);
1097                 
1098         
1099                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1100                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP)
1101                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1102                 else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP)
1103                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1104                                 
1105                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets, provider_id, 
1106                         TRUE /* incoming */);
1107                         
1108                 
1109                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1110                         protocol_security_incoming = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1111                 
1112                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1113                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1114         }
1115         else {
1116                 /* Use custom pages because no preset was specified: */
1117                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1118                 
1119                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1120                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1121                 
1122                 protocol_security_incoming = easysetup_serversecurity_combo_box_get_active_serversecurity (
1123                 EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1124                 
1125                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1126                         ? MODEST_PROTOCOL_AUTH_PASSWORD
1127                         : MODEST_PROTOCOL_AUTH_NONE;
1128                 
1129         }
1130         
1131         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1132                 account_name,
1133                 servername_incoming,
1134                 username, password,
1135                 protocol_incoming,
1136                 protocol_security_incoming,
1137                 protocol_authentication_incoming);              
1138                 
1139         g_free (servername_incoming);
1140         
1141         if (!created) {
1142                 /* TODO: Provide a Logical ID for the text: */
1143                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1144                 return FALSE;   
1145         }
1146         
1147         /* Outgoing server: */
1148         gchar* servername_outgoing = NULL;
1149         ModestProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1150         ModestProtocol protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1151         ModestProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1152         
1153         if(provider_id) {
1154                 /* Use presets: */
1155                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1156                         FALSE /* incoming */);
1157                         
1158                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets, provider_id, 
1159                         FALSE /* incoming */);
1160                 
1161                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1162                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1163                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1164                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* TODO: Is this what we want? */
1165                 
1166                 ModestPresetsSecurity security_outgoing = 
1167                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1168                                 FALSE /* incoming */);
1169                         
1170                 protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_NONE;
1171                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP)
1172                         protocol_security_outgoing = MODEST_PROTOCOL_SECURITY_SSL; /* TODO: Is this what we want? */
1173                 
1174                 protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1175                 /* TODO: There is no SMTP authentication enum for presets. */
1176         }
1177         else {
1178                 /* Use custom pages because no preset was specified: */
1179                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1180                 
1181                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1182
1183                 protocol_security_outgoing = easysetup_serversecurity_combo_box_get_active_serversecurity (
1184                         EASYSETUP_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1185                 
1186                 protocol_authentication_outgoing = easysetup_secureauth_combo_box_get_active_secureauth (
1187                         EASYSETUP_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1188                 
1189                 gboolean specific = gtk_toggle_button_get_active (
1190                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
1191                 
1192         }
1193             
1194         created = modest_account_mgr_add_server_account (self->account_manager,
1195                 account_name,
1196                 servername_outgoing,
1197                 username, password,
1198                 protocol_outgoing,
1199                 protocol_security_outgoing,
1200                 protocol_authentication_outgoing);
1201                 
1202         g_free (servername_outgoing);
1203                 
1204         if (!created) {
1205                 /* TODO: Provide a Logical ID for the text: */
1206                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1207                 return FALSE;   
1208         }            
1209         
1210         return FALSE;
1211 }
1212
1213