2007-06-05 Murray Cumming <murrayc@murrayc.com>modest/trunk] > more ChangeLog2
[modest] / src / maemo / easysetup / modest-easysetup-wizard.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  
30
31 #include "modest-easysetup-wizard.h"
32 #include <glib/gi18n.h>
33 #include <gtk/gtknotebook.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkcombobox.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkmessagedialog.h>
41 #include <gtk/gtkseparator.h>
42 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
43 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
44 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
45 #include "widgets/modest-serversecurity-combo-box.h"
46 #include "widgets/modest-secureauth-combo-box.h"
47 #include "widgets/modest-validating-entry.h"
48 #include "modest-text-utils.h"
49 #include "modest-account-mgr.h"
50 #include "modest-account-mgr-helpers.h"
51 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
52 #include "maemo/modest-connection-specific-smtp-window.h"
53 #include "widgets/modest-ui-constants.h"
54 #include "maemo/modest-account-settings-dialog.h"
55 #include "maemo/modest-maemo-utils.h"
56 #include <gconf/gconf-client.h>
57 #include <string.h> /* For strlen(). */
58
59 /* Include config.h so that _() works: */
60 #ifdef HAVE_CONFIG_H
61 #include <config.h>
62 #endif
63
64 #define EXAMPLE_EMAIL_ADDRESS "first.last@provider.com"
65
66 G_DEFINE_TYPE (ModestEasysetupWizardDialog, modest_easysetup_wizard_dialog, MODEST_TYPE_WIZARD_DIALOG);
67
68 #define WIZARD_DIALOG_GET_PRIVATE(o)                                    \
69         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, ModestEasysetupWizardDialogPrivate))
70
71 typedef struct _ModestEasysetupWizardDialogPrivate ModestEasysetupWizardDialogPrivate;
72
73 typedef enum {
74         MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED = 0x01,
75         MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED = 0x02
76 } ModestEasysetupWizardDialogServerChanges;
77
78 struct _ModestEasysetupWizardDialogPrivate
79 {
80         ModestPresets *presets;
81
82         /* Remember what fields the user edited manually to not prefill them
83          * again. */
84         ModestEasysetupWizardDialogServerChanges server_changes;
85 };
86
87 static void
88 modest_easysetup_wizard_dialog_get_property (GObject *object, guint property_id,
89                                              GValue *value, GParamSpec *pspec)
90 {
91         switch (property_id) {
92         default:
93                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94         }
95 }
96
97 static void
98 modest_easysetup_wizard_dialog_set_property (GObject *object, guint property_id,
99                                              const GValue *value, GParamSpec *pspec)
100 {
101         switch (property_id) {
102         default:
103                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104         }
105 }
106
107 static void
108 modest_easysetup_wizard_dialog_dispose (GObject *object)
109 {
110         if (G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose)
111                 G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->dispose (object);
112 }
113
114 static void
115 modest_easysetup_wizard_dialog_finalize (GObject *object)
116 {
117         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (object);
118         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
119         
120         if (self->account_manager)
121                 g_object_unref (G_OBJECT (self->account_manager));
122                 
123         if (priv->presets)
124                 modest_presets_destroy (priv->presets);
125                 
126         if (self->specific_window)
127                 gtk_widget_destroy (self->specific_window);
128                 
129         g_free (self->saved_account_name);
130         
131         G_OBJECT_CLASS (modest_easysetup_wizard_dialog_parent_class)->finalize (object);
132 }
133
134 static void
135 show_error (GtkWindow *parent_window, const gchar* text);
136
137 static gboolean
138 create_account (ModestEasysetupWizardDialog *self, gboolean enabled);
139
140 static void
141 create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self);
142
143 static void
144 set_default_custom_servernames(ModestEasysetupWizardDialog *dialog);
145
146 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data);
147
148 static void
149 invoke_enable_buttons_vfunc (ModestEasysetupWizardDialog *wizard_dialog)
150 {
151         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
152         
153         /* Call the vfunc, which may be overridden by derived classes: */
154         if (klass->enable_buttons) {
155                 GtkNotebook *notebook = NULL;
156                 g_object_get (wizard_dialog, "wizard-notebook", &notebook, NULL);
157                 
158                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
159                 if (current_page_num == -1)
160                         return;
161                         
162                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
163                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (wizard_dialog), current_page_widget);
164         }
165 }
166
167 static void
168 on_caption_entry_changed (GtkEditable *editable, gpointer user_data)
169 {
170         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
171         g_assert(self);
172         invoke_enable_buttons_vfunc(self);
173 }
174
175 static void
176 on_caption_combobox_changed (GtkComboBox *widget, gpointer user_data)
177 {
178         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
179         g_assert(self);
180         invoke_enable_buttons_vfunc(self);
181 }
182
183 /** This is a convenience function to create a caption containing a mandatory widget.
184  * When the widget is edited, the enable_buttons() vfunc will be called.
185  */
186 static GtkWidget* create_caption_new_with_asterisk(ModestEasysetupWizardDialog *self,
187                                                   GtkSizeGroup *group,
188                                                   const gchar *value,
189                                                   GtkWidget *control,
190                                                   GtkWidget *icon,
191                                                   HildonCaptionStatus flag)
192 {
193         GtkWidget *caption = NULL;
194   
195         /* Note: Previously, the translated strings already contained the "*",
196          * Comment out this code if they do again.
197          */
198         /* Add a * character to indicate mandatory fields,
199          * as specified in our "Email UI Specification": */
200         if (flag == HILDON_CAPTION_MANDATORY) {
201                 gchar* title = g_strdup_printf("%s*", value);
202                 caption = hildon_caption_new (group, title, control, icon, flag);       
203                 g_free(title);
204         }       
205         else
206                 caption = hildon_caption_new (group, value, control, icon, flag);
207
208         /* Connect to the appropriate changed signal for the widget, 
209          * so we can ask for the prev/next buttons to be enabled/disabled appropriately:
210          */
211         if (GTK_IS_ENTRY (control)) {
212                 g_signal_connect (G_OBJECT (control), "changed",
213                                   G_CALLBACK (on_caption_entry_changed), self);
214                 
215         }
216         else if (GTK_IS_COMBO_BOX (control)) {
217                 g_signal_connect (G_OBJECT (control), "changed",
218                                   G_CALLBACK (on_caption_combobox_changed), self);
219         }
220          
221         return caption;
222 }
223            
224 static GtkWidget*
225 create_page_welcome (ModestEasysetupWizardDialog *self)
226 {
227         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
228         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_intro"));
229         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
230         /* So that it is not truncated: */
231         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
232         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
233         gtk_widget_show (label);
234         gtk_widget_show (GTK_WIDGET (box));
235         return GTK_WIDGET (box);
236 }
237
238 static void
239 on_combo_account_country (GtkComboBox *widget, gpointer user_data)
240 {
241         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
242         g_assert(self);
243         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
244         
245         /* Fill the providers combo, based on the selected country: */
246         gint mcc_id = easysetup_country_combo_box_get_active_country_id (
247                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country));
248         easysetup_provider_combo_box_fill (
249                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider), priv->presets, mcc_id);
250 }
251
252 static void
253 on_combo_account_serviceprovider (GtkComboBox *widget, gpointer user_data)
254 {
255         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
256         g_assert(self);
257         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
258         
259         /* Fill the providers combo, based on the selected country: */
260         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
261                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
262         
263         gchar* domain_name = NULL;
264         if(provider_id)
265                 domain_name = modest_presets_get_domain (priv->presets, provider_id);
266         
267         if(!domain_name)
268                 domain_name = g_strdup (EXAMPLE_EMAIL_ADDRESS);
269                 
270         if (self->entry_user_email)
271                 gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), domain_name);
272                 
273         g_free (domain_name);
274         
275         g_free (provider_id);
276 }
277
278 static void
279 on_entry_max (ModestValidatingEntry *self, gpointer user_data)
280 {
281         ModestEasysetupWizardDialog *dialog = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
282         show_error (GTK_WINDOW (dialog), _("ckdg_ib_maximum_characters_reached"));
283 }
284
285 static GtkWidget*
286 create_page_account_details (ModestEasysetupWizardDialog *self)
287 {
288         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
289         GtkWidget *label = gtk_label_new(_("mcen_ia_accountdetails"));
290         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
291         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
292         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
293         gtk_widget_show (label);
294         
295         /* Create a size group to be used by all captions.
296          * Note that HildonCaption does not create a default size group if we do not specify one.
297          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
298         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
299
300         /* The country widgets: */
301         self->combo_account_country = GTK_WIDGET (easysetup_country_combo_box_new ());
302         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_country"), 
303                                                               self->combo_account_country, NULL, HILDON_CAPTION_OPTIONAL);
304         gtk_widget_show (self->combo_account_country);
305         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
306         gtk_widget_show (caption);
307         
308         /* connect to country combo's changed signal, so we can fill the provider combo: */
309         g_signal_connect (G_OBJECT (self->combo_account_country), "changed",
310                           G_CALLBACK (on_combo_account_country), self);
311             
312         GtkWidget *separator = gtk_hseparator_new ();
313         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
314         gtk_widget_show (separator);
315             
316         /* The service provider widgets: */     
317         self->combo_account_serviceprovider = GTK_WIDGET (easysetup_provider_combo_box_new ());
318         
319         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_serviceprovider"), 
320                                                    self->combo_account_serviceprovider, NULL, HILDON_CAPTION_OPTIONAL);
321         gtk_widget_show (self->combo_account_serviceprovider);
322         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
323         gtk_widget_show (caption);
324         
325         /* connect to providers combo's changed signal, so we can fill the email address: */
326         g_signal_connect (G_OBJECT (self->combo_account_serviceprovider), "changed",
327                           G_CALLBACK (on_combo_account_serviceprovider), self);
328         
329         /* TODO: Default to the current country somehow.
330          * But I don't know how to get the information that is specified in the 
331          * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
332          *
333          * This is probably not the best choice of gconf key:
334          * This is the  "mcc used in the last pairing", ie. the last connection you made.
335          * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
336          *
337          */
338         GConfClient *client = gconf_client_get_default ();
339         GError *error = NULL;
340         const gchar* key = "/apps/osso/operator-wizard/last_mcc";
341         gint mcc_id = gconf_client_get_int(client, key, &error);
342         
343         if(mcc_id < 0)
344                 mcc_id = 0;
345      
346         if (error) {
347                 g_warning ("Error getting gconf key %s:\n%s", key, error->message);
348                 g_error_free (error);
349                 error = NULL;
350         
351                 mcc_id = 0;
352         }
353     
354         /* Note that gconf_client_get_int() seems to return 0 without an error if the key is not there
355          * This might just be a Maemo bug.
356          */
357         if (mcc_id == 0) 
358         {
359                 /* For now, we default to Finland when there is nothing better: */
360                 mcc_id = 244;
361         }
362    
363         easysetup_country_combo_box_set_active_country_id (
364                 EASYSETUP_COUNTRY_COMBO_BOX (self->combo_account_country), mcc_id);
365                 
366         
367         /* The description widgets: */  
368         self->entry_account_title = GTK_WIDGET (modest_validating_entry_new ());
369         /* Do use auto-capitalization: */
370         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_account_title), 
371                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
372         
373         /* Set a default account title, choosing one that does not already exist: */
374         /* Note that this is irrelevant to the non-user visible name, which we will create later. */
375         gchar* default_account_name_start = g_strdup (_("mcen_ia_emailsetup_defaultname"));
376         gchar* default_account_name = modest_account_mgr_get_unused_account_display_name (
377                 self->account_manager, default_account_name_start);
378         g_free (default_account_name_start);
379         default_account_name_start = NULL;
380         
381         gtk_entry_set_text( GTK_ENTRY (self->entry_account_title), default_account_name);
382         g_free (default_account_name);
383         default_account_name = NULL;
384
385         caption = create_caption_new_with_asterisk (self, sizegroup, _("mcen_fi_account_title"), 
386                                                    self->entry_account_title, NULL, HILDON_CAPTION_MANDATORY);
387         gtk_widget_show (self->entry_account_title);
388         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
389         gtk_widget_show (caption);
390         
391         /* Prevent the use of some characters in the account title, 
392          * as required by our UI specification: */
393         GList *list_prevent = NULL;
394         list_prevent = g_list_append (list_prevent, "\\");
395         list_prevent = g_list_append (list_prevent, "/");
396         list_prevent = g_list_append (list_prevent, ":");
397         list_prevent = g_list_append (list_prevent, "*");
398         list_prevent = g_list_append (list_prevent, "?");
399         list_prevent = g_list_append (list_prevent, "\""); /* The UI spec mentions â€œ, but maybe means ", maybe both. */
400         list_prevent = g_list_append (list_prevent, "“");
401         list_prevent = g_list_append (list_prevent, "<"); 
402         list_prevent = g_list_append (list_prevent, ">"); 
403         list_prevent = g_list_append (list_prevent, "|");
404         list_prevent = g_list_append (list_prevent, "^");       
405         modest_validating_entry_set_unallowed_characters (
406                 MODEST_VALIDATING_ENTRY (self->entry_account_title), list_prevent);
407         g_list_free (list_prevent);
408         
409         /* Set max length as in the UI spec:
410          * 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_account_title), 64);
412         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_account_title), 
413                                               on_entry_max, self);
414         
415         gtk_widget_show (GTK_WIDGET (box));
416         
417         return GTK_WIDGET (box);
418 }
419
420 static GtkWidget*
421 create_page_user_details (ModestEasysetupWizardDialog *self)
422 {
423         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
424         
425         /* Create a size group to be used by all captions.
426          * Note that HildonCaption does not create a default size group if we do not specify one.
427          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
428         GtkSizeGroup* sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
429          
430         /* The name widgets: */
431         self->entry_user_name = GTK_WIDGET (modest_validating_entry_new ());
432         /* Auto-capitalization is the default, so let's turn it off: */
433         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_name), HILDON_GTK_INPUT_MODE_FULL);
434         /* Set max length as in the UI spec:
435          * The UI spec seems to want us to show a dialog if we hit the maximum. */
436         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_name), 64);
437         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_name), 
438                                               on_entry_max, self);
439         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
440                                                               _("mcen_li_emailsetup_name"), self->entry_user_name, NULL, HILDON_CAPTION_OPTIONAL);
441         gtk_widget_show (self->entry_user_name);
442         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
443         gtk_widget_show (caption);
444         
445         /* Prevent the use of some characters in the name, 
446          * as required by our UI specification: */
447         GList *list_prevent = NULL;
448         list_prevent = g_list_append (list_prevent, "<");
449         list_prevent = g_list_append (list_prevent, ">");
450         modest_validating_entry_set_unallowed_characters (
451                 MODEST_VALIDATING_ENTRY (self->entry_user_name), list_prevent);
452         g_list_free (list_prevent);
453         
454         /* The username widgets: */     
455         self->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
456         /* Auto-capitalization is the default, so let's turn it off: */
457         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
458         caption = create_caption_new_with_asterisk (self, sizegroup, _("mail_fi_username"), 
459                                                    self->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
460         gtk_widget_show (self->entry_user_username);
461         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
462         gtk_widget_show (caption);
463         
464         /* Prevent the use of some characters in the username, 
465          * as required by our UI specification: */
466         modest_validating_entry_set_unallowed_characters_whitespace (
467                 MODEST_VALIDATING_ENTRY (self->entry_user_username));
468         
469         /* Set max length as in the UI spec:
470          * The UI spec seems to want us to show a dialog if we hit the maximum. */
471         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_username), 64);
472         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_username), 
473                                               on_entry_max, self);
474         
475         /* The password widgets: */     
476         self->entry_user_password = gtk_entry_new ();
477         /* Auto-capitalization is the default, so let's turn it off: */
478         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_password), 
479                                          HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
480         gtk_entry_set_visibility (GTK_ENTRY (self->entry_user_password), FALSE);
481         /* gtk_entry_set_invisible_char (GTK_ENTRY (self->entry_user_password), '*'); */
482         caption = create_caption_new_with_asterisk (self, sizegroup, 
483                                                    _("mail_fi_password"), self->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
484         gtk_widget_show (self->entry_user_password);
485         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
486         gtk_widget_show (caption);
487         
488         /* The email address widgets: */        
489         self->entry_user_email = GTK_WIDGET (modest_validating_entry_new ());
490         /* Auto-capitalization is the default, so let's turn it off: */
491         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_user_email), HILDON_GTK_INPUT_MODE_FULL);
492         caption = create_caption_new_with_asterisk (self, sizegroup, 
493                                                    _("mcen_li_emailsetup_email_address"), self->entry_user_email, NULL, HILDON_CAPTION_MANDATORY);
494         gtk_entry_set_text (GTK_ENTRY (self->entry_user_email), EXAMPLE_EMAIL_ADDRESS); /* Default text. */
495         gtk_widget_show (self->entry_user_email);
496         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
497         gtk_widget_show (caption);
498         
499         /* Set max length as in the UI spec:
500          * The UI spec seems to want us to show a dialog if we hit the maximum. */
501         gtk_entry_set_max_length (GTK_ENTRY (self->entry_user_email), 64);
502         modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (self->entry_user_email), 
503                                               on_entry_max, self);
504         
505         
506         gtk_widget_show (GTK_WIDGET (box));
507         
508         return GTK_WIDGET (box);
509 }
510
511 static GtkWidget* create_page_complete_easysetup (ModestEasysetupWizardDialog *self)
512 {
513         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
514         
515         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
516         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
517         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
518         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
519         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
520         gtk_widget_show (label);
521         
522         label = gtk_label_new (_("mcen_ia_easysetup_complete"));
523         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
524         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
525         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
526         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
527         gtk_widget_show (label);
528         
529         gtk_widget_show (GTK_WIDGET (box));
530         return GTK_WIDGET (box);
531 }
532
533 /** Change the caption title for the incoming server, 
534  * as specified in the UI spec:
535  */
536 static void update_incoming_server_title (ModestEasysetupWizardDialog *self)
537 {
538         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
539                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
540         const gchar* type = 
541                 (protocol == MODEST_PROTOCOL_STORE_POP ? 
542                  _("mail_fi_emailtype_pop3") : 
543                  _("mail_fi_emailtype_imap") );
544                         
545                 
546         /* Note that this produces a compiler warning, 
547          * because the compiler does not know that the translated string will have a %s in it.
548          * I do not see a way to avoid the warning while still using these Logical IDs. murrayc. */
549         gchar* incomingserver_title = g_strdup_printf(_("mcen_li_emailsetup_servertype"), type);
550         g_object_set (G_OBJECT (self->caption_incoming), "label", incomingserver_title, NULL);
551         g_free(incomingserver_title);
552 }
553
554 /** Change the caption title for the incoming server, 
555  * as specified in the UI spec:
556  */
557 static void update_incoming_server_security_choices (ModestEasysetupWizardDialog *self)
558 {
559         const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
560                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
561         
562         /* Fill the combo with appropriately titled choices for POP or IMAP. */
563         /* The choices are the same, but the titles are different, as in the UI spec. */
564         modest_serversecurity_combo_box_fill (
565                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), protocol);
566 }
567
568 static void on_combo_servertype_changed(GtkComboBox *combobox, gpointer user_data)
569 {
570         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
571         update_incoming_server_title (self);
572         update_incoming_server_security_choices (self);
573
574         set_default_custom_servernames (self);
575 }
576
577 static void on_entry_incoming_servername_changed(GtkEntry *entry, gpointer user_data)
578 {
579         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
580         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
581         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED;
582 }
583
584 static GtkWidget* create_page_custom_incoming (ModestEasysetupWizardDialog *self)
585 {
586         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
587
588         /* Show note that account type cannot be changed in future: */
589         GtkWidget *label = gtk_label_new (_("mcen_ia_emailsetup_account_type"));
590         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
591         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
592         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
593         gtk_widget_show (label);
594         
595         /* Create a size group to be used by all captions.
596          * Note that HildonCaption does not create a default size group if we do not specify one.
597          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
598         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
599          
600         /* The incoming server widgets: */
601         if (!self->combo_incoming_servertype)
602                 self->combo_incoming_servertype = GTK_WIDGET (easysetup_servertype_combo_box_new ());
603         easysetup_servertype_combo_box_set_active_servertype (
604                 EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype), MODEST_PROTOCOL_STORE_POP);
605         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
606                                                               _("mcen_li_emailsetup_type"), self->combo_incoming_servertype, NULL, HILDON_CAPTION_MANDATORY);
607         gtk_widget_show (self->combo_incoming_servertype);
608         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
609         gtk_widget_show (caption);
610         
611         if(!self->entry_incomingserver)
612                 self->entry_incomingserver = gtk_entry_new ();
613         /* Auto-capitalization is the default, so let's turn it off: */
614         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_incomingserver), HILDON_GTK_INPUT_MODE_FULL);
615         set_default_custom_servernames (self);
616
617         if (self->caption_incoming)
618                 gtk_widget_destroy (self->caption_incoming);
619            
620         /* The caption title will be updated in update_incoming_server_title().
621          * so this default text will never be seen: */
622         /* (Note: Changing the title seems pointless. murrayc) */
623         self->caption_incoming = create_caption_new_with_asterisk (self, sizegroup, 
624                                                                   "Incoming Server", self->entry_incomingserver, NULL, HILDON_CAPTION_MANDATORY);
625         update_incoming_server_title (self);
626         gtk_widget_show (self->entry_incomingserver);
627         gtk_box_pack_start (GTK_BOX (box), self->caption_incoming, FALSE, FALSE, MODEST_MARGIN_HALF);
628         gtk_widget_show (self->caption_incoming);
629         
630         /* Change the caption title when the servertype changes, 
631          * as in the UI spec: */
632         g_signal_connect (G_OBJECT (self->combo_incoming_servertype), "changed",
633                           G_CALLBACK (on_combo_servertype_changed), self);
634
635         /* Remember when the servername was changed manually: */
636         g_signal_connect (G_OBJECT (self->entry_incomingserver), "changed",
637                           G_CALLBACK (on_entry_incoming_servername_changed), self);
638
639         /* The secure connection widgets: */    
640         if (!self->combo_incoming_security)
641                 self->combo_incoming_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
642         update_incoming_server_security_choices (self);
643         modest_serversecurity_combo_box_set_active_serversecurity (
644                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
645         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
646                                       self->combo_incoming_security, NULL, HILDON_CAPTION_OPTIONAL);
647         gtk_widget_show (self->combo_incoming_security);
648         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
649         gtk_widget_show (caption);
650         
651         if(!self->checkbox_incoming_auth)
652                 self->checkbox_incoming_auth = gtk_check_button_new ();
653         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
654                                       self->checkbox_incoming_auth, NULL, HILDON_CAPTION_OPTIONAL);
655         gtk_widget_show (self->checkbox_incoming_auth);
656         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
657         gtk_widget_show (caption);
658         
659         gtk_widget_show (GTK_WIDGET (box));
660         
661         return GTK_WIDGET (box);
662 }
663
664 static void
665 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
666 {
667         GtkWidget *widget = GTK_WIDGET (user_data);
668         
669         /* Enable the widget only if the toggle button is active: */
670         const gboolean enable = gtk_toggle_button_get_active (togglebutton);
671         gtk_widget_set_sensitive (widget, enable);
672 }
673
674 /* Make the sensitivity of a widget depend on a toggle button.
675  */
676 static void
677 enable_widget_for_togglebutton (GtkWidget *widget, GtkToggleButton* button)
678 {
679         g_signal_connect (G_OBJECT (button), "toggled",
680                           G_CALLBACK (on_toggle_button_changed), widget);
681         
682         /* Set the starting sensitivity: */
683         on_toggle_button_changed (button, widget);
684 }
685
686 static void
687 on_button_outgoing_smtp_servers (GtkButton *button, gpointer user_data)
688 {
689         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
690         
691         /* Create the window, if necessary: */
692         if (!(self->specific_window)) {
693                 self->specific_window = GTK_WIDGET (modest_connection_specific_smtp_window_new ());
694                 modest_connection_specific_smtp_window_fill_with_connections (
695                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), self->account_manager, 
696                         NULL /* account_name, not known yet. */);
697         }
698
699         /* Show the window: */
700         gtk_window_set_transient_for (GTK_WINDOW (self->specific_window), GTK_WINDOW (self));
701         gtk_widget_show (self->specific_window);
702 }
703
704 static void on_entry_outgoing_servername_changed (GtkEntry *entry, gpointer user_data)
705 {
706         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
707         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
708         priv->server_changes |= MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED;
709 }
710
711 static GtkWidget* create_page_custom_outgoing (ModestEasysetupWizardDialog *self)
712 {
713         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
714         
715         /* Create a size group to be used by all captions.
716          * Note that HildonCaption does not create a default size group if we do not specify one.
717          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
718         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
719          
720         /* The outgoing server widgets: */
721         if (!self->entry_outgoingserver)
722                 self->entry_outgoingserver = gtk_entry_new ();
723         /* Auto-capitalization is the default, so let's turn it off: */
724         hildon_gtk_entry_set_input_mode (GTK_ENTRY (self->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
725         GtkWidget *caption = create_caption_new_with_asterisk (self, sizegroup, 
726                                                               _("mcen_li_emailsetup_smtp"), self->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
727         gtk_widget_show (self->entry_outgoingserver);
728         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
729         gtk_widget_show (caption);
730         set_default_custom_servernames (self);
731         
732         /* The secure connection widgets: */    
733         if (!self->combo_outgoing_security)
734                 self->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
735         modest_serversecurity_combo_box_fill (
736                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
737         modest_serversecurity_combo_box_set_active_serversecurity (
738                 MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
739         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
740                                       self->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
741         gtk_widget_show (self->combo_outgoing_security);
742         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
743         gtk_widget_show (caption);
744         
745         /* The secure authentication widgets: */
746         if (!self->combo_outgoing_auth)
747                 self->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
748         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
749                                       self->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
750         gtk_widget_show (self->combo_outgoing_auth);
751         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
752         gtk_widget_show (caption);
753         
754         GtkWidget *separator = gtk_hseparator_new ();
755         gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, MODEST_MARGIN_HALF);
756         gtk_widget_show (separator);
757         
758         /* connection-specific checkbox: */
759         if (!self->checkbox_outgoing_smtp_specific) {
760                 self->checkbox_outgoing_smtp_specific = gtk_check_button_new ();
761                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific), 
762                                               FALSE);
763         }
764         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_connection_smtp"), 
765                                       self->checkbox_outgoing_smtp_specific, NULL, HILDON_CAPTION_OPTIONAL);
766         gtk_widget_show (self->checkbox_outgoing_smtp_specific);
767         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
768         gtk_widget_show (caption);
769         
770         /* Connection-specific SMTP-Severs Edit button: */
771         if (!self->button_outgoing_smtp_servers)
772                 self->button_outgoing_smtp_servers = gtk_button_new_with_label (_("mcen_bd_edit"));
773         caption = hildon_caption_new (sizegroup, _("mcen_fi_advsetup_optional_smtp"), 
774                                       self->button_outgoing_smtp_servers, NULL, HILDON_CAPTION_OPTIONAL);
775         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
776         gtk_widget_show (self->button_outgoing_smtp_servers);
777         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
778         gtk_widget_show (caption);
779         
780         /* Only enable the button when the checkbox is checked: */
781         enable_widget_for_togglebutton (self->button_outgoing_smtp_servers, 
782                                         GTK_TOGGLE_BUTTON (self->checkbox_outgoing_smtp_specific));
783                 
784         g_signal_connect (G_OBJECT (self->button_outgoing_smtp_servers), "clicked",
785                           G_CALLBACK (on_button_outgoing_smtp_servers), self);
786
787         g_signal_connect (G_OBJECT (self->entry_outgoingserver), "changed",
788                           G_CALLBACK (on_entry_outgoing_servername_changed), self);
789         
790         
791         gtk_widget_show (GTK_WIDGET (box));
792         
793         return GTK_WIDGET (box);
794 }
795
796 static gboolean
797 on_timeout_show_advanced_edit(gpointer user_data)
798 {
799         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
800         
801         if (!(self->saved_account_name))
802                 return FALSE;
803         
804         /* Show the Account Settings window: */
805         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
806         modest_account_settings_dialog_set_account_name (dialog, self->saved_account_name);
807         
808         gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
809         
810         gtk_dialog_run (GTK_DIALOG (dialog));
811
812         /* TODO: The hide() is not necessary.
813          * It is just here to show that it doesn't work,
814          * just as destroy doesn't work.
815          */
816         gtk_widget_hide (GTK_WIDGET(dialog));
817
818         /* TODO: The dialog remains on screen, not allowing any interaction.
819          * But gtk_widget_destroy() should always destroy.
820          */
821         printf("debug: destroying settings dialog\n");
822         gtk_widget_destroy (GTK_WIDGET (dialog));
823         printf("debug: after destroying settings dialog (doesn't seem to work).\n");
824         
825         return FALSE; /* Do not call this timeout callback again. */
826 }
827
828 static void
829 on_button_edit_advanced_settings (GtkButton *button, gpointer user_data)
830 {
831         ModestEasysetupWizardDialog * self = MODEST_EASYSETUP_WIZARD_DIALOG (user_data);
832         
833         /* Save the new account, so we can edit it with ModestAccountSettingsDialog, 
834          * without recoding it to use non-gconf information.
835          * This account will be deleted if Finish is never actually clicked. */
836
837         gboolean saved = TRUE;
838         gboolean was_already_saved = TRUE;
839         if (!(self->saved_account_name)) {
840                 saved = create_account (self, FALSE);
841                 was_already_saved = FALSE;
842         }
843                 
844         if (!saved)
845                 return;
846                 
847         if (!(self->saved_account_name))
848                 return;
849         
850         /* Show the Account Settings window: */
851         if (was_already_saved) {
852                 /* Just show the dialog immediately: */
853                 on_timeout_show_advanced_edit(self);
854         }
855         else
856         {
857                 printf ("debug: waiting for gconf to update its local cache. "
858                         "This is a hack to work around a maemo gconf bug in maemo bora.\n");
859        
860                 g_timeout_add (5000, on_timeout_show_advanced_edit, self);
861         }
862 }
863 static GtkWidget* create_page_complete_custom (ModestEasysetupWizardDialog *self)
864 {
865         GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE);
866         GtkWidget *label = gtk_label_new(_("mcen_ia_emailsetup_setup_complete"));
867         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
868         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
869         gtk_widget_show (label);
870         
871         label = gtk_label_new (_("mcen_ia_customsetup_complete"));
872         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
873         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
874         gtk_widget_show (label);
875         
876         if (!self->button_edit)
877                 self->button_edit = gtk_button_new_with_label (_("mcen_bd_edit"));
878         GtkWidget *caption = hildon_caption_new (NULL, _("mcen_fi_advanced_settings"), 
879                                                  self->button_edit, NULL, HILDON_CAPTION_OPTIONAL);
880         hildon_caption_set_child_expand (HILDON_CAPTION (caption), FALSE);
881         gtk_widget_show (self->button_edit);
882         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
883         gtk_widget_show (caption);
884         
885         g_signal_connect (G_OBJECT (self->button_edit), "clicked", 
886                           G_CALLBACK (on_button_edit_advanced_settings), self);
887         
888         gtk_widget_show (GTK_WIDGET (box));
889         return GTK_WIDGET (box);
890 }
891
892
893 /*
894  */
895 static void 
896 on_response (ModestWizardDialog *wizard_dialog,
897              gint response_id,
898              gpointer user_data)
899 {
900         ModestEasysetupWizardDialog *self = MODEST_EASYSETUP_WIZARD_DIALOG (wizard_dialog);
901         
902         if (response_id == GTK_RESPONSE_CANCEL) {
903                 /* Remove any temporarily-saved account that will not actually be needed: */
904                 if (self->saved_account_name) {
905                         modest_account_mgr_remove_account (self->account_manager,
906                                                            self->saved_account_name, FALSE);
907                 }       
908         }
909         
910         invoke_enable_buttons_vfunc (self);
911 }
912
913 static void
914 modest_easysetup_wizard_dialog_init (ModestEasysetupWizardDialog *self)
915 {
916         gtk_container_set_border_width (GTK_CONTAINER (self), MODEST_MARGIN_HALF);
917         
918         /* Create the notebook to be used by the ModestWizardDialog base class:
919          * Each page of the notebook will be a page of the wizard: */
920         GtkNotebook *notebook = GTK_NOTEBOOK (gtk_notebook_new());
921         
922         /* Set the notebook used by the ModestWizardDialog base class: */
923         g_object_set (G_OBJECT(self), "wizard-notebook", notebook, NULL);
924     
925         /* Set the wizard title:
926          * The actual window title will be a combination of this and the page's tab label title. */
927         g_object_set (G_OBJECT(self), "wizard-name", _("mcen_ti_emailsetup"), NULL);
928
929         /* Read in the information about known service providers: */
930         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
931         
932         const gchar* filepath = MODEST_PROVIDERS_DATA_PATH; /* Defined in config.h */
933         priv->presets = modest_presets_new (filepath);
934         if (!(priv->presets)) {
935                 g_warning ("Could not locate the official provider data keyfile from %s", filepath);
936         }
937         
938         g_assert(priv->presets);
939
940         /* The server fields did not have been manually changed yet */
941         priv->server_changes = 0;
942
943         /* Get the account manager object, 
944          * so we can check for existing accounts,
945          * and create new accounts: */
946         self->account_manager = modest_runtime_get_account_mgr ();
947         g_assert (self->account_manager);
948         g_object_ref (self->account_manager);
949         
950         /* Create the common pages, 
951          */
952         self->page_welcome = create_page_welcome (self);
953         self->page_account_details = create_page_account_details (self);
954         self->page_user_details = create_page_user_details (self);
955         
956         /* Add the common pages: */
957         gtk_notebook_append_page (notebook, self->page_welcome, 
958                                   gtk_label_new (_("mcen_ti_emailsetup_welcome")));
959         gtk_notebook_append_page (notebook, self->page_account_details, 
960                                   gtk_label_new (_("mcen_ti_accountdetails")));
961         gtk_notebook_append_page (notebook, self->page_user_details, 
962                                   gtk_label_new (_("mcen_ti_emailsetup_userdetails")));
963                 
964         /* Create and add the easysetup-specific pages,
965          * because we need _some_ final page to enable the Next and Finish buttons: */
966         create_subsequent_easysetup_pages (self);
967
968         /* Connect to the dialog's response signal so we can enable/disable buttons 
969          * for the newly-selected page, because the prev/next buttons cause response to be emitted.
970          * Note that we use g_signal_connect_after() instead of g_signal_connect()
971          * so that we can be enable/disable after ModestWizardDialog has done its own 
972          * enabling/disabling of buttons.
973          * 
974          * HOWEVER, this doesn't work because ModestWizardDialog's response signal handler 
975          * does g_signal_stop_emission_by_name(), stopping our signal handler from running.
976          * 
977          * It's not enough to connect to the notebook's switch-page signal, because 
978          * ModestWizardDialog's "response" signal handler enables the buttons itself, 
979          * _after_ switching the page (understandably).
980          * (Note that if we had, if we used g_signal_connect() instead of g_signal_connect_after()
981          * then gtk_notebook_get_current_page() would return an incorrect value.)
982          */
983         g_signal_connect_after (G_OBJECT (self), "response",
984                                 G_CALLBACK (on_response), self);
985
986         /* When this window is shown, hibernation should not be possible, 
987          * because there is no sensible way to save the state: */
988         modest_window_mgr_prevent_hibernation_while_window_is_shown (
989                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
990 }
991
992 ModestEasysetupWizardDialog*
993 modest_easysetup_wizard_dialog_new (void)
994 {
995         return g_object_new (MODEST_TYPE_EASYSETUP_WIZARD_DIALOG, NULL);
996 }
997
998 static void create_subsequent_customsetup_pages (ModestEasysetupWizardDialog *self)
999 {
1000         GtkNotebook *notebook = NULL;
1001         g_object_get (self, "wizard-notebook", &notebook, NULL);
1002         g_assert(notebook);
1003         
1004         /* Create the custom pages: */
1005         if(!(self->page_custom_incoming)) {
1006                 self->page_custom_incoming = create_page_custom_incoming (self);
1007         }
1008                 
1009         if(!(self->page_custom_outgoing)) {
1010                 self->page_custom_outgoing = create_page_custom_outgoing (self);
1011         }
1012         
1013         if(!(self->page_complete_customsetup)) {
1014                 self->page_complete_customsetup = create_page_complete_custom (self);
1015         }
1016         
1017         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_incoming)))
1018                 gtk_notebook_append_page (notebook, self->page_custom_incoming,
1019                                           gtk_label_new (_("mcen_ti_emailsetup_incomingdetails")));
1020         
1021         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_custom_outgoing)))           
1022                 gtk_notebook_append_page (notebook, self->page_custom_outgoing,
1023                                           gtk_label_new (_("mcen_ti_emailsetup_outgoingdetails")));
1024                 
1025         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_customsetup)))
1026                 gtk_notebook_append_page (notebook, self->page_complete_customsetup,
1027                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1028                         
1029         /* This is unnecessary with GTK+ 2.10: */
1030         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1031 }
1032         
1033 static void create_subsequent_easysetup_pages (ModestEasysetupWizardDialog *self)
1034 {
1035         GtkNotebook *notebook = NULL;
1036         g_object_get (self, "wizard-notebook", &notebook, NULL);
1037         g_assert(notebook);
1038         
1039         /* Create the easysetup-specific pages: */
1040         if(!self->page_complete_easysetup)
1041                 self->page_complete_easysetup = create_page_complete_easysetup (self);
1042
1043         if (!gtk_widget_get_parent (GTK_WIDGET (self->page_complete_easysetup)))
1044                 gtk_notebook_append_page (notebook, self->page_complete_easysetup, 
1045                                           gtk_label_new (_("mcen_ti_emailsetup_complete")));
1046                         
1047         /* This is unnecessary with GTK+ 2.10: */
1048         modest_wizard_dialog_force_title_update (MODEST_WIZARD_DIALOG(self));
1049 }
1050 /* After the user details page,
1051  * the following pages depend on whether "Other" was chosen 
1052  * in the provider combobox on the account page
1053  */
1054 static void create_subsequent_pages (ModestEasysetupWizardDialog *self)
1055 {
1056         if (easysetup_provider_combo_box_get_active_provider_id (
1057                     EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider)) == 0) {
1058                 /* "Other..." was selected: */
1059                 
1060                 /* Make sure that the easysetup pages do not exist: */
1061                 if(self->page_complete_easysetup) {
1062                         gtk_widget_destroy (self->page_complete_easysetup);
1063                         self->page_complete_easysetup = NULL;
1064                 }
1065                 
1066                 create_subsequent_customsetup_pages (self);
1067         }       
1068         else {
1069                 /* A specific provider was selected: */
1070                 {
1071                         /* Make sure that the custom pages do not exist:
1072                          * Because they will be used if they exist, when creating the account. */
1073                         if(self->page_custom_incoming) {
1074                                 gtk_widget_destroy (self->page_custom_incoming);
1075                                 self->page_custom_incoming = NULL;
1076                         }
1077                         
1078                         if(self->page_custom_outgoing) {
1079                                 gtk_widget_destroy (self->page_custom_outgoing);
1080                                 self->page_custom_outgoing = NULL;
1081                         }
1082                         
1083                         if(self->page_complete_customsetup) {
1084                                 gtk_widget_destroy (self->page_complete_customsetup);
1085                                 self->page_complete_customsetup = NULL;
1086                         }
1087                 }
1088                 
1089                 /* Create the easysetup pages: */
1090                 create_subsequent_easysetup_pages (self);
1091         }
1092 }
1093
1094
1095 static gchar*
1096 util_get_default_servername_from_email_address (const gchar* email_address, ModestTransportStoreProtocol servertype)
1097 {
1098         if (!email_address)
1099                 return NULL;
1100         
1101         gchar* at = g_utf8_strchr (email_address, -1, '@');
1102         if (!at || (g_utf8_strlen (at, -1) < 2))
1103                 return NULL;
1104                 
1105         gchar* domain = g_utf8_next_char (at);
1106         if(!domain)
1107                 return NULL;
1108                 
1109         const gchar* hostname = NULL;
1110         if (servertype == MODEST_PROTOCOL_STORE_POP)
1111                 hostname = "pop";
1112         else if (servertype == MODEST_PROTOCOL_STORE_IMAP)
1113                 hostname = "imap";
1114         else if (servertype == MODEST_PROTOCOL_TRANSPORT_SMTP)
1115                 hostname = "smtp";
1116         
1117         if (!hostname)
1118                 return NULL;
1119                 
1120         return g_strdup_printf ("%s.%s", hostname, domain);
1121 }
1122
1123 static void set_default_custom_servernames (ModestEasysetupWizardDialog *account_wizard)
1124 {
1125         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE(account_wizard);
1126
1127         if (!account_wizard->entry_incomingserver)
1128                 return;
1129                 
1130         /* Set a default domain for the server, based on the email address,
1131          * if no server name was already specified.
1132          */
1133         if (account_wizard->entry_user_email
1134             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_INCOMING_CHANGED) == 0)) {
1135                 const ModestTransportStoreProtocol protocol = easysetup_servertype_combo_box_get_active_servertype (
1136                         EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1137                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1138                 
1139                 gchar* servername = util_get_default_servername_from_email_address (email_address, protocol);
1140
1141                 /* Do not set the INCOMING_CHANGED flag because of this edit */
1142                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1143                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_incomingserver), servername);
1144                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_incomingserver), G_CALLBACK (on_entry_incoming_servername_changed), account_wizard);
1145
1146                 g_free (servername);
1147         }
1148         
1149         /* Set a default domain for the server, based on the email address,
1150          * if no server name was already specified.
1151          */
1152         if (!account_wizard->entry_outgoingserver)
1153                 return;
1154                 
1155         if (account_wizard->entry_user_email
1156             && ((priv->server_changes & MODEST_EASYSETUP_WIZARD_DIALOG_OUTGOING_CHANGED) == 0)) {
1157                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY(account_wizard->entry_user_email));
1158                 
1159                 gchar* servername = util_get_default_servername_from_email_address (email_address, MODEST_PROTOCOL_TRANSPORT_SMTP);
1160
1161                 /* Do not set the OUTGOING_CHANGED flag because of this edit */
1162                 g_signal_handlers_block_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1163                 gtk_entry_set_text (GTK_ENTRY (account_wizard->entry_outgoingserver), servername);
1164                 g_signal_handlers_unblock_by_func (G_OBJECT (account_wizard->entry_outgoingserver), G_CALLBACK (on_entry_outgoing_servername_changed), account_wizard);
1165
1166                 g_free (servername);
1167         }
1168 }
1169
1170 static gboolean
1171 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
1172 {
1173         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1174         
1175         /* Do extra validation that couldn't be done for every key press,
1176          * either because it was too slow,
1177          * or because it requires interaction:
1178          */
1179         if (current_page == account_wizard->page_account_details) {     
1180                 /* Check that the title is not already in use: */
1181                 const gchar* account_title = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_account_title));
1182                 if ((!account_title) || (strlen(account_title) == 0))
1183                         return FALSE;
1184                         
1185                 /* Aavoid a clash with an existing display name: */
1186                 const gboolean name_in_use = modest_account_mgr_account_with_display_name_exists (
1187                         account_wizard->account_manager, account_title);
1188
1189                 if (name_in_use) {
1190                         /* Warn the user via a dialog: */
1191                         show_error (GTK_WINDOW (account_wizard), _("mail_ib_account_name_already_existing"));
1192             
1193                         return FALSE;
1194                 }
1195         }
1196         else if (current_page == account_wizard->page_user_details) {
1197                 /* Check that the email address is valud: */
1198                 const gchar* email_address = gtk_entry_get_text (GTK_ENTRY (account_wizard->entry_user_email));
1199                 if ((!email_address) || (strlen(email_address) == 0))
1200                         return FALSE;
1201                         
1202                 if (!modest_text_utils_validate_email_address (email_address)) {
1203                         /* Warn the user via a dialog: */
1204                         show_error (GTK_WINDOW (account_wizard), _("mcen_ib_invalid_email"));
1205                                              
1206                         /* Return focus to the email address entry: */
1207                         gtk_widget_grab_focus (account_wizard->entry_user_email);
1208             
1209                         return FALSE;
1210                 }
1211                 
1212                 /* Make sure that the subsequent pages are appropriate for the provider choice. */
1213                 create_subsequent_pages (account_wizard);
1214         }
1215         
1216         /* TODO: The UI Spec wants us to check that the servernames are valid, 
1217          * but does not specify how.
1218          */
1219           
1220         if(next_page == account_wizard->page_custom_incoming) {
1221                 set_default_custom_servernames (account_wizard);
1222         }
1223         else if (next_page == account_wizard->page_custom_outgoing) {
1224                 set_default_custom_servernames (account_wizard);
1225     /* Check if the server support secure authentication */
1226     if (gtk_toggle_button_get_active (
1227                         GTK_TOGGLE_BUTTON (account_wizard->checkbox_incoming_auth))) {
1228 #if 0
1229                                 const ModestTransportStoreProtocol protocol = 
1230           easysetup_servertype_combo_box_get_active_servertype (
1231                                                                 EASYSETUP_SERVERTYPE_COMBO_BOX (account_wizard->combo_incoming_servertype));
1232         const gchar* hostname = gtk_entry_get_text(GTK_ENTRY(account_wizard->entry_incomingserver));
1233         /* FIXME: Get correct port */
1234
1235         int port_num = 22; 
1236         GList *list_auth_methods = 
1237           modest_maemo_utils_get_supported_secure_authentication_methods (
1238                                                                       protocol, 
1239                                                                       hostname, port_num, GTK_WINDOW (dialog)); 
1240         if (list_auth_methods) {
1241           /* TODO: Select the correct method */
1242           g_list_free (list_auth_methods);
1243         }
1244         else
1245         {
1246           GtkWidget* error_dialog = gtk_message_dialog_new(GTK_WINDOW(dialog),
1247                                                        GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
1248                                                        GTK_BUTTONS_OK, _("Server does not support secure authentication!"));
1249           gtk_dialog_run(GTK_DIALOG(error_dialog));
1250           gtk_widget_destroy(error_dialog);
1251           return FALSE;
1252         }
1253 #endif
1254                  }
1255         }
1256         
1257         /* If this is the last page, and this is a click on Finish, 
1258          * then attempt to create the dialog.
1259          */
1260         if(!next_page) /* This is NULL when this is a click on Finish. */
1261         {
1262                 if (account_wizard->saved_account_name) {
1263                         /* Just enable the already-saved account (temporarily created when 
1264                          * editing advanced settings): */
1265                         modest_account_mgr_set_enabled (account_wizard->account_manager, 
1266                                                         account_wizard->saved_account_name, TRUE);
1267                 } else {
1268                         create_account (account_wizard, TRUE /* enabled */);
1269                 }
1270         }
1271         
1272         
1273         return TRUE;
1274 }
1275
1276 static gboolean entry_is_empty (GtkWidget *entry)
1277 {
1278         if (!entry)
1279                 return FALSE;
1280                 
1281         const gchar* text = gtk_entry_get_text (GTK_ENTRY (entry));
1282         if ((!text) || (strlen(text) == 0))
1283                 return TRUE;
1284         else
1285                 return FALSE;
1286 }
1287
1288 static void
1289 on_enable_buttons (ModestWizardDialog *dialog, GtkWidget *current_page)
1290 {
1291         ModestEasysetupWizardDialog *account_wizard = MODEST_EASYSETUP_WIZARD_DIALOG (dialog);
1292         
1293         gboolean enable_next = TRUE;
1294         if (current_page == account_wizard->page_welcome) {
1295                 enable_next = TRUE;
1296         }
1297         else if (current_page == account_wizard->page_account_details) {
1298                 /* The account details title is mandatory: */
1299                 if (entry_is_empty(account_wizard->entry_account_title))
1300                         enable_next = FALSE;
1301         }
1302         else if (current_page == account_wizard->page_user_details) {   
1303                 /* The user details username is mandatory: */
1304                 if (entry_is_empty(account_wizard->entry_user_username))
1305                         enable_next = FALSE;
1306                         
1307                 /* The user details email address is mandatory: */
1308                 if (enable_next && entry_is_empty (account_wizard->entry_user_email))
1309                         enable_next = FALSE;
1310         }
1311         else if (current_page == account_wizard->page_custom_incoming) {
1312                 /* The custom incoming server is mandatory: */
1313                 if (entry_is_empty(account_wizard->entry_incomingserver))
1314                         enable_next = FALSE;
1315         }
1316                         
1317         /* Enable the buttons, 
1318          * identifying them via their associated response codes: */
1319                                    
1320         /* Disable the Finish button until we are on the last page,
1321          * because HildonWizardDialog enables this for all but the first page: */
1322         GtkNotebook *notebook = NULL;
1323         GtkDialog *dialog_base = GTK_DIALOG (dialog);
1324         g_object_get (dialog_base, "wizard-notebook", &notebook, NULL);
1325         
1326         gint current = gtk_notebook_get_current_page (notebook);
1327         gint last = gtk_notebook_get_n_pages (notebook) - 1;
1328         const gboolean is_last = (current == last);
1329     
1330         if(!is_last) {
1331                 gtk_dialog_set_response_sensitive (dialog_base,
1332                                                    MODEST_WIZARD_DIALOG_FINISH,
1333                                                    FALSE);
1334         } else
1335         {
1336                 /* Disable Next on the last page: */
1337                 enable_next = FALSE;
1338         }
1339         
1340         gtk_dialog_set_response_sensitive (dialog_base,
1341                                            MODEST_WIZARD_DIALOG_NEXT,
1342                                            enable_next);
1343 }
1344
1345 static void
1346 modest_easysetup_wizard_dialog_class_init (ModestEasysetupWizardDialogClass *klass)
1347 {
1348         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1349         g_type_class_add_private (klass, sizeof (ModestEasysetupWizardDialogPrivate));
1350
1351
1352         object_class->get_property = modest_easysetup_wizard_dialog_get_property;
1353         object_class->set_property = modest_easysetup_wizard_dialog_set_property;
1354         object_class->dispose = modest_easysetup_wizard_dialog_dispose;
1355         object_class->finalize = modest_easysetup_wizard_dialog_finalize;
1356         
1357         /* Provide a vfunc implementation so we can decide 
1358          * when to enable/disable the prev/next buttons.
1359          */
1360         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
1361         base_klass->before_next = on_before_next;
1362         base_klass->enable_buttons = on_enable_buttons;
1363 }
1364  
1365 static void
1366 show_error (GtkWindow *parent_window, const gchar* text)
1367 {
1368         GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, text));
1369         /*
1370           GtkDialog *dialog = GTK_DIALOG (gtk_message_dialog_new (parent_window,
1371           (GtkDialogFlags)0,
1372           GTK_MESSAGE_ERROR,
1373           GTK_BUTTONS_OK,
1374           text ));
1375         */
1376                  
1377         gtk_dialog_run (dialog);
1378         gtk_widget_destroy (GTK_WIDGET (dialog));
1379 }
1380
1381 /** Attempt to create the account from the information that the user has entered.
1382  * @result: TRUE if the account was successfully created.
1383  */
1384 gboolean
1385 create_account (ModestEasysetupWizardDialog *self, gboolean enabled)
1386 {
1387         ModestEasysetupWizardDialogPrivate *priv = WIZARD_DIALOG_GET_PRIVATE (self);
1388         
1389         const gchar* display_name = gtk_entry_get_text (GTK_ENTRY (self->entry_account_title));
1390
1391         /* Some checks: */
1392         if (!display_name)
1393                 return FALSE;
1394                 
1395         /* We should have checked for this already, 
1396          * and changed that name accordingly, 
1397          * but let's check again just in case:
1398          */
1399         if (modest_account_mgr_account_with_display_name_exists (self->account_manager, display_name)) 
1400                 return FALSE;
1401
1402         /* Increment the non-user visible name if necessary, 
1403          * based on the display name: */
1404         gchar *account_name_start = g_strdup_printf ("%sID", display_name);
1405         gchar* account_name = modest_account_mgr_get_unused_account_name (self->account_manager,
1406                                                                           account_name_start, FALSE /* not a server account */);
1407         g_free (account_name_start);
1408                 
1409         /* username and password (for both incoming and outgoing): */
1410         const gchar* username = gtk_entry_get_text (GTK_ENTRY (self->entry_user_username));
1411         const gchar* password = gtk_entry_get_text (GTK_ENTRY (self->entry_user_password));
1412         
1413         /* Incoming server: */
1414         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
1415          * or modest_account_mgr_add_server_account will fail. */
1416         gchar* servername_incoming = NULL;
1417         guint serverport_incoming = 0;
1418         ModestTransportStoreProtocol protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1419         ModestConnectionProtocol protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_NORMAL;
1420         ModestAuthProtocol protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_NONE;
1421         
1422         /* Get details from the specified presets: */
1423         gchar* provider_id = easysetup_provider_combo_box_get_active_provider_id (
1424                 EASYSETUP_PROVIDER_COMBO_BOX (self->combo_account_serviceprovider));
1425         if (provider_id) {
1426                 /* Use presets: */
1427                 servername_incoming = modest_presets_get_server (priv->presets, provider_id, 
1428                                                                  TRUE /* incoming */);
1429                 
1430                 ModestPresetsServerType servertype_incoming = modest_presets_get_info_server_type (priv->presets,
1431                                                                                                    provider_id, 
1432                                                                                                    TRUE /* incoming */);
1433                 ModestPresetsSecurity security_incoming = modest_presets_get_info_server_security (priv->presets,
1434                                                                                                    provider_id, 
1435                                                                                                    TRUE /* incoming */);
1436
1437                 g_warning ("security incoming: %x", security_incoming);
1438                 
1439                 /* We don't check for SMTP here as that is impossible for an incoming server. */
1440                 if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_IMAP) {
1441                         protocol_incoming = MODEST_PROTOCOL_STORE_IMAP;
1442                         serverport_incoming =
1443                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 993 : 143; 
1444                 } else if (servertype_incoming == MODEST_PRESETS_SERVER_TYPE_POP) {
1445                         protocol_incoming = MODEST_PROTOCOL_STORE_POP;
1446                         serverport_incoming =
1447                                 (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING_ALTERNATE_PORT) ? 995 : 110; 
1448                 }
1449                                 
1450                 if (security_incoming & MODEST_PRESETS_SECURITY_SECURE_INCOMING)
1451                         protocol_security_incoming = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1452                 
1453                 if (security_incoming & MODEST_PRESETS_SECURITY_APOP)
1454                         protocol_authentication_incoming = MODEST_PROTOCOL_AUTH_PASSWORD; /* TODO: Is this what we want? */
1455         }
1456         else {
1457                 /* Use custom pages because no preset was specified: */
1458                 servername_incoming = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_incomingserver) ));
1459                 
1460                 protocol_incoming = easysetup_servertype_combo_box_get_active_servertype (
1461                         EASYSETUP_SERVERTYPE_COMBO_BOX (self->combo_incoming_servertype));
1462                 
1463                 protocol_security_incoming = modest_serversecurity_combo_box_get_active_serversecurity (
1464                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_incoming_security));
1465                 
1466                 /* The UI spec says:
1467                  * If secure authentication is unchecked, allow sending username and password also as plain text.
1468                  * If secure authentication is checked, require one of the secure methods during connection: SSL, TLS, CRAM-MD5 etc. 
1469                  * TODO: Do we need to discover which of these (SSL, TLS, CRAM-MD5) is supported?
1470                  */
1471                 protocol_authentication_incoming = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->checkbox_incoming_auth)) 
1472                         ? MODEST_PROTOCOL_AUTH_CRAMMD5
1473                         : MODEST_PROTOCOL_AUTH_PASSWORD;
1474         }
1475         
1476         /* First we add the 2 server accounts, and then we add the account that uses them.
1477          * If we don't do it in this order then we will experience a crash. */
1478          
1479         /* Add a (incoming) server account, to be used by the account: */
1480         gchar *store_name_start = g_strconcat (account_name, "_store", NULL);
1481         gchar *store_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1482                                                                         store_name_start, TRUE /* server account */);
1483         g_free (store_name_start);
1484         gboolean created = modest_account_mgr_add_server_account (self->account_manager,
1485                                                                   store_name,
1486                                                                   servername_incoming,
1487                                                                   serverport_incoming,
1488                                                                   username, password,
1489                                                                   protocol_incoming,
1490                                                                   protocol_security_incoming,
1491                                                                   protocol_authentication_incoming);            
1492                 
1493         g_free (servername_incoming);
1494         
1495         if (!created) {
1496                 /* TODO: Provide a Logical ID for the text: */
1497                 show_error (GTK_WINDOW (self), _("An error occurred while creating the incoming account."));
1498                 return FALSE;   
1499         }
1500         
1501         /* Sanity check: */
1502         /* There must be at least one account now: */
1503         /* Note, when this fails is is caused by a Maemo gconf bug that has been 
1504          * fixed in versions after 3.1. */
1505         GSList *account_names = modest_account_mgr_account_names (self->account_manager, FALSE);
1506         if(!account_names)
1507         {
1508                 g_warning ("modest_account_mgr_account_names() returned NULL after adding an account.");
1509         }
1510         g_slist_free (account_names);
1511         
1512         
1513         /* Outgoing server: */
1514         gchar* servername_outgoing = NULL;
1515         ModestTransportStoreProtocol protocol_outgoing = MODEST_PROTOCOL_STORE_POP;
1516         ModestConnectionProtocol protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1517         ModestAuthProtocol protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1518         guint serverport_outgoing = 0;
1519         
1520         if (provider_id) {
1521                 /* Use presets: */
1522                 servername_outgoing = modest_presets_get_server (priv->presets, provider_id, 
1523                                                                  FALSE /* incoming */);
1524                         
1525                 ModestPresetsServerType servertype_outgoing = modest_presets_get_info_server_type (priv->presets,
1526                                                                                                    provider_id, 
1527                                                                                                    FALSE /* incoming */);
1528                 
1529                 /* Note: We need something as default, or modest_account_mgr_add_server_account will fail. */
1530                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SENDMAIL; 
1531                 if (servertype_outgoing == MODEST_PRESETS_SERVER_TYPE_SMTP)
1532                         protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP;
1533                 
1534                 ModestPresetsSecurity security_outgoing = 
1535                         modest_presets_get_info_server_security (priv->presets, provider_id, 
1536                                                                  FALSE /* incoming */);
1537                         
1538                 protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_NORMAL;
1539                 if (security_outgoing & MODEST_PRESETS_SECURITY_SECURE_SMTP) {
1540                         protocol_security_outgoing = MODEST_PROTOCOL_CONNECTION_SSL; /* TODO: Is this what we want? */
1541                         serverport_outgoing = 465;
1542                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_PASSWORD;
1543                 } else
1544                         protocol_authentication_outgoing = MODEST_PROTOCOL_AUTH_NONE;
1545                 /* TODO: There is no SMTP authentication enum for presets. */
1546         }
1547         else {
1548                 /* Use custom pages because no preset was specified: */
1549                 servername_outgoing = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry_outgoingserver) ));
1550                 
1551                 protocol_outgoing = MODEST_PROTOCOL_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1552
1553                 protocol_security_outgoing = modest_serversecurity_combo_box_get_active_serversecurity (
1554                         MODEST_SERVERSECURITY_COMBO_BOX (self->combo_outgoing_security));
1555                 
1556                 protocol_authentication_outgoing = modest_secureauth_combo_box_get_active_secureauth (
1557                         MODEST_SECUREAUTH_COMBO_BOX (self->combo_outgoing_auth));
1558         }
1559             
1560         /* Add a (outgoing) server account to be used by the account: */
1561         gchar *transport_name_start = g_strconcat (account_name, "_transport", NULL);
1562         gchar *transport_name = modest_account_mgr_get_unused_account_name (self->account_manager, 
1563                                                                             transport_name_start, TRUE /* server account */);
1564         g_free (transport_name_start);
1565         created = modest_account_mgr_add_server_account (self->account_manager,
1566                                                          transport_name,
1567                                                          servername_outgoing,
1568                                                          serverport_outgoing,
1569                                                          username, password,
1570                                                          protocol_outgoing,
1571                                                          protocol_security_outgoing,
1572                                                          protocol_authentication_outgoing);
1573                 
1574         g_free (servername_outgoing);
1575                 
1576         if (!created) {
1577                 /* TODO: Provide a Logical ID for the text: */
1578                 show_error (GTK_WINDOW (self), _("An error occurred while creating the outgoing account."));
1579                 return FALSE;   
1580         }
1581         
1582         
1583         /* Create the account, which will contain the two "server accounts": */
1584         created = modest_account_mgr_add_account (self->account_manager, account_name, 
1585                                                   store_name, /* The name of our POP/IMAP server account. */
1586                                                   transport_name, /* The name of our SMTP server account. */
1587                                                   enabled);
1588         g_free (store_name);
1589         g_free (transport_name);
1590         
1591         if (!created) {
1592                 /* TODO: Provide a Logical ID for the text: */
1593                 show_error (GTK_WINDOW (self), _("An error occurred while creating the account."));
1594                 return FALSE;   
1595         }
1596         
1597         /* The user name and email address must be set additionally: */
1598         const gchar* user_name = gtk_entry_get_text (GTK_ENTRY (self->entry_user_name));
1599         modest_account_mgr_set_string (self->account_manager, account_name,
1600                                        MODEST_ACCOUNT_FULLNAME, user_name, FALSE /* not server account */);
1601
1602         const gchar* emailaddress = gtk_entry_get_text (GTK_ENTRY (self->entry_user_email));
1603         modest_account_mgr_set_string (self->account_manager, account_name,
1604                                        MODEST_ACCOUNT_EMAIL, emailaddress, FALSE /* not server account */);
1605
1606         /* Set the display name: */
1607         modest_account_mgr_set_string (self->account_manager, account_name,
1608                                        MODEST_ACCOUNT_DISPLAY_NAME, display_name, FALSE /* not server account */);
1609
1610         /* Set retrieve type */ 
1611         const gchar *retrieve = MODEST_ACCOUNT_RETRIEVE_VALUE_HEADERS_ONLY;
1612         modest_account_mgr_set_string (self->account_manager, account_name,
1613                 MODEST_ACCOUNT_RETRIEVE, retrieve, FALSE /* not server account */);
1614
1615         /* Save the connection-specific SMTP server accounts. */
1616         gboolean result = TRUE;
1617         if (self->specific_window)
1618                 result = modest_connection_specific_smtp_window_save_server_accounts (
1619                         MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (self->specific_window), account_name);
1620                         
1621         g_free (self->saved_account_name);
1622         self->saved_account_name = g_strdup (account_name);
1623         
1624         g_free (account_name);
1625
1626         return result;
1627 }