Do ignore empty recipients when checking names
[modest] / src / gnome / modest-account-assistant.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 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <camel/camel-url.h>
33 #include <widgets/modest-combo-box.h>
34 #include "modest-account-assistant.h"
35 #include "modest-store-widget.h"
36 #include "modest-transport-widget.h"
37 #include "modest-text-utils.h"
38 #include "modest-runtime.h"
39 #include "modest-utils.h"
40 #include "modest-protocol-registry.h"
41 #include "modest-platform.h"
42 #include "gnome/modest-gnome-utils.h"
43
44 #include <string.h>
45
46 /* 'private'/'protected' functions */
47 static void       modest_account_assistant_class_init    (ModestAccountAssistantClass *klass);
48 static void       modest_account_assistant_init          (ModestAccountAssistant *obj);
49 static void       modest_account_assistant_finalize      (GObject *obj);
50 static gboolean   on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page);
51 static void       on_response (ModestWizardDialog *wizard_dialog,
52                                gint response_id,
53                                gpointer user_data);
54 static void       on_response_before (ModestWizardDialog *wizard_dialog,
55                                       gint response_id,
56                                       gpointer user_data);
57
58 /* list my signals */
59 enum {
60         /* MY_SIGNAL_1, */
61         /* MY_SIGNAL_2, */
62         LAST_SIGNAL
63 };
64
65 typedef struct _ModestAccountAssistantPrivate ModestAccountAssistantPrivate;
66 struct _ModestAccountAssistantPrivate {
67
68         ModestAccountMgr *account_mgr;
69         ModestAccountSettings *settings;
70         gboolean   dirty;
71
72         GtkWidget *notebook;
73
74         GtkWidget *account_name;
75         GtkWidget *fullname;
76         GtkWidget *email;
77        
78         GtkWidget *username;
79         GtkWidget *password;
80         GtkWidget *store_server_widget;
81         GtkWidget *store_protocol_combo;
82         GtkWidget *store_security_combo;
83         GtkWidget *store_secure_auth;
84         GtkWidget *transport_server_widget;
85         GtkWidget *transport_security_combo;
86         GtkWidget *transport_secure_auth_combo;
87         
88         GtkWidget *transport_widget;
89
90         GtkWidget *transport_holder;
91
92         ModestPairList *receiving_transport_store_protos;
93         ModestPairList *sending_transport_store_protos;
94         ModestPairList *security_protos;
95         ModestPairList *transport_security_protos;
96         ModestPairList *transport_auth_protos;
97 };
98
99 #define MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
100                                                       MODEST_TYPE_ACCOUNT_ASSISTANT, \
101                                                       ModestAccountAssistantPrivate))
102 /* globals */
103 static GtkAssistantClass *parent_class = NULL;
104
105 /* uncomment the following if you have defined any signals */
106 /* static guint signals[LAST_SIGNAL] = {0}; */
107
108 static void save_to_settings (ModestAccountAssistant *self);
109
110 GType
111 modest_account_assistant_get_type (void)
112 {
113         static GType my_type = 0;
114         if (!my_type) {
115                 static const GTypeInfo my_info = {
116                         sizeof(ModestAccountAssistantClass),
117                         NULL,           /* base init */
118                         NULL,           /* base finalize */
119                         (GClassInitFunc) modest_account_assistant_class_init,
120                         NULL,           /* class finalize */
121                         NULL,           /* class data */
122                         sizeof(ModestAccountAssistant),
123                         1,              /* n_preallocs */
124                         (GInstanceInitFunc) modest_account_assistant_init,
125                         NULL
126                 };
127                 my_type = g_type_register_static (MODEST_TYPE_WIZARD_DIALOG,
128                                                   "ModestAccountAssistant",
129                                                   &my_info, 0);
130         }
131         return my_type;
132 }
133
134 static void
135 modest_account_assistant_class_init (ModestAccountAssistantClass *klass)
136 {
137         GObjectClass *gobject_class;
138         gobject_class = (GObjectClass*) klass;
139
140         parent_class            = g_type_class_peek_parent (klass);
141         gobject_class->finalize = modest_account_assistant_finalize;
142
143         g_type_class_add_private (gobject_class, sizeof(ModestAccountAssistantPrivate));
144
145         ModestWizardDialogClass *base_klass = (ModestWizardDialogClass*)(klass);
146         base_klass->before_next = on_before_next;
147 }
148
149 static gboolean
150 on_delete_event (GtkWidget *widget,
151                  GdkEvent *event,
152                  ModestAccountAssistant *assistant)
153 {
154         gtk_dialog_response (GTK_DIALOG (assistant), GTK_RESPONSE_CANCEL);
155         return TRUE;
156 }
157
158 static void
159 on_assistant_changed(GtkWidget* widget, ModestAccountAssistant* assistant)
160 {
161         ModestAccountAssistantPrivate* priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(assistant);
162         g_return_if_fail (priv != NULL);
163         priv->dirty = TRUE;
164 }
165
166 static void
167 on_incoming_security_changed(GtkWidget* widget, ModestAccountAssistant* assistant)
168 {
169         ModestAccountAssistantPrivate* priv;
170         ModestProtocolType protocol_id;
171         ModestProtocol *protocol_security_incoming;
172         const gchar *name;
173         ModestProtocolRegistry *registry;
174         gboolean is_secure;
175
176         g_return_if_fail (MODEST_IS_ACCOUNT_ASSISTANT (assistant));
177
178         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(assistant);
179         registry = modest_runtime_get_protocol_registry ();
180         name = (const gchar *) modest_combo_box_get_active_id  (MODEST_COMBO_BOX (priv->store_security_combo));
181         protocol_security_incoming = modest_protocol_registry_get_protocol_by_name (registry, 
182                                                                                     MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS,
183                                                                                     name);
184         protocol_id = modest_protocol_get_type_id (protocol_security_incoming);
185         is_secure = modest_protocol_registry_protocol_type_is_secure (registry, protocol_id);
186
187         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->store_secure_auth), is_secure);
188         gtk_widget_set_sensitive (priv->store_secure_auth, !is_secure);
189         
190         on_assistant_changed (widget, assistant);
191 }
192
193 static void
194 invoke_enable_buttons_vfunc (ModestAccountAssistant *assistant)
195 {
196         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (assistant);
197         
198         /* Call the vfunc, which may be overridden by derived classes: */
199         if (klass->enable_buttons) {
200                 GtkNotebook *notebook = NULL;
201                 g_object_get (assistant, "wizard-notebook", &notebook, NULL);
202                 
203                 const gint current_page_num = gtk_notebook_get_current_page (notebook);
204                 if (current_page_num == -1)
205                         return;
206                         
207                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (notebook, current_page_num);
208                 (*(klass->enable_buttons))(MODEST_WIZARD_DIALOG (assistant), current_page_widget);
209         }
210 }
211
212 static void
213 on_entry_changed (GtkEditable *editable, gpointer userdata)
214 {
215         on_assistant_changed (NULL, MODEST_ACCOUNT_ASSISTANT (userdata));
216         invoke_enable_buttons_vfunc(MODEST_ACCOUNT_ASSISTANT (userdata));
217 }
218
219 static void
220 on_combo_changed (GtkComboBox *combo, gpointer userdata)
221 {
222         on_assistant_changed (NULL, MODEST_ACCOUNT_ASSISTANT (userdata));
223         invoke_enable_buttons_vfunc(MODEST_ACCOUNT_ASSISTANT (userdata));
224 }
225
226 static void
227 add_intro_page (ModestAccountAssistant *assistant)
228 {
229         GtkWidget *page, *label;
230         ModestAccountAssistantPrivate *priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE (assistant);
231         
232         page = gtk_vbox_new (FALSE, 12);
233         
234         label = gtk_label_new (_("mcen_ia_emailsetup_intro"));
235         gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
236         gtk_misc_set_padding (GTK_MISC (label), 12, 12);
237         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
238         gtk_box_pack_start (GTK_BOX(page), label, FALSE, FALSE, 0);
239         gtk_widget_set_size_request (label, 400, -1);
240         gtk_widget_show_all (page);
241         
242         gtk_notebook_append_page (GTK_NOTEBOOK(priv->notebook), page, NULL);
243                 
244         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(priv->notebook), page,
245                                       _("mcen_ti_emailsetup_welcome"));
246         /* gtk_notebook_set_page_type (GTK_NOTEBOOK(assistant), page, */
247         /*                           GTK_ASSISTANT_PAGE_INTRO); */
248         /* gtk_notebook_set_page_complete (GTK_ASSISTANT(assistant), */
249         /*                               page, TRUE); */
250 }
251
252
253 static void
254 set_current_page_complete (ModestAccountAssistant *self, gboolean complete)
255 {
256         GtkWidget *page;
257         gint pageno;
258         ModestAccountAssistantPrivate *priv;
259
260         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE (self);
261
262         pageno = gtk_notebook_get_current_page (GTK_NOTEBOOK(priv->notebook));
263
264         if (pageno != -1) {
265                 page   = gtk_notebook_get_nth_page (GTK_NOTEBOOK(priv->notebook), pageno);
266                 /* gtk_assistant_set_page_complete (GTK_NOTEBOOK(priv->notebook), page, complete); */
267         }
268 }
269
270
271 static GtkWidget *
272 field_name_label (const gchar *text)
273 {
274         GtkWidget *label;
275         gchar *fixed_text;
276
277         fixed_text = g_strconcat (text, ":", NULL);
278         label = gtk_label_new (fixed_text);
279         g_free (fixed_text);
280         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
281
282         return label;
283 }
284
285 static void
286 add_identity_page (ModestAccountAssistant *self)
287 {
288         GtkWidget *page, *label, *table, *frame;
289         GtkWidget *alignment;
290         ModestAccountAssistantPrivate *priv;
291
292         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
293
294         priv->account_name = gtk_entry_new ();
295         gtk_entry_set_max_length (GTK_ENTRY (priv->account_name), 40);
296         g_signal_connect (G_OBJECT (priv->account_name), "changed", G_CALLBACK (on_entry_changed), self);
297         priv->fullname = gtk_entry_new ();
298         gtk_entry_set_max_length (GTK_ENTRY (priv->fullname), 40);
299         g_signal_connect (G_OBJECT (priv->fullname), "changed", G_CALLBACK (on_entry_changed), self);
300         priv->email    = gtk_entry_new ();
301         gtk_entry_set_width_chars (GTK_ENTRY (priv->email), 40);
302         g_signal_connect (G_OBJECT (priv->email), "changed", G_CALLBACK (on_entry_changed), self);
303         priv->username = gtk_entry_new ();
304         gtk_entry_set_width_chars (GTK_ENTRY (priv->username), 40);
305         g_signal_connect (G_OBJECT (priv->username), "changed", G_CALLBACK (on_entry_changed), self);
306         priv->password = gtk_entry_new ();
307         gtk_entry_set_width_chars (GTK_ENTRY (priv->password), 40);
308         gtk_entry_set_visibility (GTK_ENTRY (priv->password), FALSE);
309         
310         page = gtk_vbox_new (FALSE, 24);
311
312         label = gtk_label_new (
313                 _("Please enter below the name for the account you're creating."));
314         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
315         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
316         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
317         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 0, 12, 12);
318         gtk_container_add (GTK_CONTAINER (alignment), label);
319         gtk_box_pack_start (GTK_BOX(page), alignment, FALSE, FALSE, 0);
320         
321         table = gtk_table_new (1,2, FALSE);
322         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
323         gtk_table_set_row_spacings (GTK_TABLE (table), 1);
324         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Account name")),
325                                    0,1,0,1);
326         gtk_table_attach_defaults (GTK_TABLE(table),priv->account_name,
327                                    1,2,0,1);
328         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
329         gtk_container_add (GTK_CONTAINER (alignment), table);
330         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 0, 12, 0);
331         gtk_box_pack_start (GTK_BOX(page), alignment, FALSE, FALSE, 0);
332
333         frame = gtk_frame_new (NULL);
334         label = gtk_label_new (NULL);
335         gtk_label_set_markup (GTK_LABEL (label), _("<b>Public information </b>"));
336         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
337         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
338         table = gtk_table_new (2,2, FALSE);
339         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
340         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
341         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Full name")),
342                                    0,1,0,1);
343         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Email address")),
344                                    0,1,1,2);
345         gtk_table_attach_defaults (GTK_TABLE(table),priv->fullname,
346                                    1,2,0,1);
347         gtk_table_attach_defaults (GTK_TABLE(table),priv->email,
348                                    1,2,1,2);
349         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
350         gtk_container_add (GTK_CONTAINER (alignment), table);
351         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
352         gtk_container_add (GTK_CONTAINER (frame), alignment);
353         gtk_box_pack_start (GTK_BOX(page), frame, FALSE, FALSE, 0);
354
355
356         frame = gtk_frame_new (NULL);
357         label = gtk_label_new (NULL);
358         gtk_label_set_markup (GTK_LABEL (label), _("<b>Server account </b>"));
359         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
360         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
361         table = gtk_table_new (2,2, FALSE);
362         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
363         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
364         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("User name")),
365                                    0,1,0,1);
366         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Password")),
367                                    0,1,1,2);
368         gtk_table_attach_defaults (GTK_TABLE(table),priv->username,
369                                    1,2,0,1);
370         gtk_table_attach_defaults (GTK_TABLE(table),priv->password,
371                                    1,2,1,2);
372         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
373         gtk_container_add (GTK_CONTAINER (alignment), table);
374         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
375         gtk_container_add (GTK_CONTAINER (frame), alignment);
376         gtk_box_pack_start (GTK_BOX(page), frame, FALSE, FALSE, 0);
377         
378         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
379         gtk_container_add (GTK_CONTAINER (alignment), page);
380         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
381         gtk_widget_show_all (alignment);
382         gtk_notebook_append_page (GTK_NOTEBOOK(priv->notebook), alignment, NULL);
383         
384         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(priv->notebook), alignment,
385                                       _("Identity"));
386         /* gtk_assistant_set_page_type (GTK_ASSISTANT(self), alignment, */
387         /*                           GTK_ASSISTANT_PAGE_CONTENT); */
388         /* gtk_assistant_set_page_complete (GTK_ASSISTANT(self), */
389         /*                               alignment, FALSE); */
390 }       
391
392
393 static void
394 receiving_page_update_completeness (GtkEditable *editable,
395                                     ModestAccountAssistant *self)
396 {
397         ModestAccountAssistantPrivate *priv;
398         const gchar *txt;
399
400         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
401
402         txt = gtk_entry_get_text (GTK_ENTRY (priv->store_server_widget));
403         if (!txt || strlen(txt) == 0) {
404                 set_current_page_complete (self, FALSE);
405                 return;
406         }
407         set_current_page_complete (self, TRUE);
408 }
409
410 static void
411 add_receiving_page (ModestAccountAssistant *self)
412 {
413         GtkWidget *page, *vbox;
414         GtkWidget *table, *frame;
415         GtkWidget *alignment;
416         ModestAccountAssistantPrivate *priv;
417         GtkWidget *label;
418         const gchar *tag = MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS;
419
420         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
421         page = gtk_alignment_new (0.5, 0.0, 1.0, 0.0);
422         gtk_alignment_set_padding (GTK_ALIGNMENT (page), 12, 12, 12, 12);
423         vbox = gtk_vbox_new (FALSE, 24);
424         gtk_container_add (GTK_CONTAINER (page), vbox);
425
426         /* Warning label on top */
427         label = gtk_label_new (_("Setting details for the incoming mail server."));
428         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
429         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
430         gtk_box_pack_start (GTK_BOX(vbox),
431                             label,
432                             FALSE, FALSE, 0);
433
434         /* Note: This ModestPairList* must exist for as long as the combo
435          * that uses it, because the ModestComboBox uses the ID opaquely, 
436          * so it can't know how to manage its memory. */
437         priv->receiving_transport_store_protos = modest_gnome_utils_get_protocols_pair_list (tag);
438         priv->store_protocol_combo = modest_combo_box_new (priv->receiving_transport_store_protos, g_str_equal);
439         priv->store_server_widget = gtk_entry_new ();
440
441         /* Setup incoming server frame */
442         frame = gtk_frame_new (NULL);
443         label = gtk_label_new (NULL);
444         gtk_label_set_markup (GTK_LABEL (label), _("<b>Incoming server</b>"));
445         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
446         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
447         table = gtk_table_new (2, 2, FALSE);
448         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
449         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
450         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Account type")),
451                           0, 1, 0, 1,
452                           GTK_FILL, 0, 0, 0);
453         alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
454         gtk_container_add (GTK_CONTAINER (alignment), priv->store_protocol_combo);
455         gtk_table_attach (GTK_TABLE (table), alignment,
456                           1, 2, 0, 1,
457                           GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
458         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Incoming server")),
459                           0, 1, 1, 2,
460                           GTK_FILL, GTK_FILL, 0, 0);
461         gtk_table_attach (GTK_TABLE (table), priv->store_server_widget,
462                           1, 2, 1, 2,
463                           GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
464         alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
465         gtk_container_add (GTK_CONTAINER (alignment), table);
466         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
467         gtk_container_add (GTK_CONTAINER (frame), alignment);
468         gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
469
470         /* Setup security information widgets */
471         tag = MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS;
472         priv->security_protos = modest_gnome_utils_get_protocols_pair_list (tag);
473         priv->store_security_combo = modest_combo_box_new (priv->security_protos, g_str_equal);
474         priv->store_secure_auth = gtk_check_button_new ();
475
476         /* Setup security frame */
477         frame = gtk_frame_new (NULL);
478         label = gtk_label_new (NULL);
479         gtk_label_set_markup (GTK_LABEL (label), _("<b>Security options</b>"));
480         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
481         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
482         table = gtk_table_new (2, 2, FALSE);
483         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
484         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
485         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Secure connection")),
486                           0, 1, 0, 1,
487                           GTK_FILL, 0, 0, 0);
488         gtk_table_attach (GTK_TABLE (table), priv->store_security_combo,
489                           1, 2, 0, 1,
490                           GTK_FILL | GTK_EXPAND, 0, 0, 0);
491         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Use secure authentication")),
492                           0, 1, 1, 2,
493                           GTK_FILL, GTK_FILL, 0, 0);
494         gtk_table_attach_defaults (GTK_TABLE (table), priv->store_secure_auth,
495                                    1, 2, 1, 2);
496         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
497         gtk_container_add (GTK_CONTAINER (alignment), table);
498         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
499         gtk_container_add (GTK_CONTAINER (frame), alignment);
500         gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, FALSE, 0);
501         
502         /* Setup assistant page */
503         gtk_notebook_append_page (GTK_NOTEBOOK(priv->notebook), page, NULL);
504                 
505         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(priv->notebook), page,
506                                          _("Incoming details"));
507         gtk_widget_show_all (page);
508 }
509
510
511
512
513 static void
514 on_sending_combo_box_changed (GtkComboBox *combo, ModestAccountAssistant *self)
515 {
516         ModestAccountAssistantPrivate *priv;
517         gchar *chosen;
518         ModestProtocol *proto;
519
520         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
521
522         chosen = gtk_combo_box_get_active_text (GTK_COMBO_BOX(combo));
523
524         if (priv->transport_widget)
525                 gtk_container_remove (GTK_CONTAINER(priv->transport_holder),
526                                       priv->transport_widget);
527
528         proto = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
529                                                                MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
530                                                                chosen);
531         priv->transport_widget = modest_transport_widget_new (modest_protocol_get_type_id (proto));
532
533         gtk_container_add (GTK_CONTAINER(priv->transport_holder),
534                            priv->transport_widget);
535
536         gtk_widget_show_all (priv->transport_holder);
537         on_assistant_changed (NULL, MODEST_ACCOUNT_ASSISTANT (self));
538 }
539
540
541
542 static void
543 add_sending_page (ModestAccountAssistant *self)
544 {
545         GtkWidget *page, *vbox;
546         GtkWidget *table, *frame;
547         GtkWidget *alignment;
548         ModestAccountAssistantPrivate *priv;
549         GtkWidget *label;
550         const gchar *tag = MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS;
551
552         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
553         page = gtk_alignment_new (0.5, 0.0, 1.0, 0.0);
554         gtk_alignment_set_padding (GTK_ALIGNMENT (page), 12, 12, 12, 12);
555         vbox = gtk_vbox_new (FALSE, 24);
556         gtk_container_add (GTK_CONTAINER (page), vbox);
557
558         /* Warning label on top */
559         label = gtk_label_new (_("Settings for the outgoing mail server"));
560         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
561         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
562         gtk_box_pack_start (GTK_BOX(vbox),
563                             label,
564                             FALSE, FALSE, 0);
565
566         priv->transport_server_widget = gtk_entry_new ();
567         /* Setup incoming server frame */
568         frame = gtk_frame_new (NULL);
569         label = gtk_label_new (NULL);
570         gtk_label_set_markup (GTK_LABEL (label), _("<b>Outgoing server</b>"));
571         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
572         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
573         table = gtk_table_new (2, 2, FALSE);
574         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
575         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
576         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Outgoing server (SMTP)")),
577                           0, 1, 0, 1,
578                           GTK_FILL, GTK_FILL, 0, 0);
579         gtk_table_attach (GTK_TABLE (table), priv->transport_server_widget,
580                           1, 2, 0, 1,
581                           GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
582         alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
583         gtk_container_add (GTK_CONTAINER (alignment), table);
584         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
585         gtk_container_add (GTK_CONTAINER (frame), alignment);
586         gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
587
588         /* Setup security information widgets */
589         priv->transport_security_protos = modest_gnome_utils_get_protocols_pair_list (tag);
590         priv->transport_security_combo = modest_combo_box_new (priv->security_protos, g_str_equal);
591         tag = MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS;
592         priv->transport_auth_protos = modest_gnome_utils_get_protocols_pair_list (tag);
593         priv->transport_secure_auth_combo = GTK_WIDGET (modest_combo_box_new (priv->transport_auth_protos, g_str_equal));
594
595         /* Setup security frame */
596         frame = gtk_frame_new (NULL);
597         label = gtk_label_new (NULL);
598         gtk_label_set_markup (GTK_LABEL (label), _("<b>Security options</b>"));
599         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
600         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
601         table = gtk_table_new (2, 2, FALSE);
602         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
603         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
604         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Secure connection")),
605                           0, 1, 0, 1,
606                           GTK_FILL, 0, 0, 0);
607         gtk_table_attach (GTK_TABLE (table), priv->transport_security_combo,
608                           1, 2, 0, 1,
609                           GTK_FILL | GTK_EXPAND, 0, 0, 0);
610         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Secure authentication")),
611                           0, 1, 1, 2,
612                           GTK_FILL, GTK_FILL, 0, 0);
613         gtk_table_attach_defaults (GTK_TABLE (table), priv->transport_secure_auth_combo,
614                                    1, 2, 1, 2);
615         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
616         gtk_container_add (GTK_CONTAINER (alignment), table);
617         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
618         gtk_container_add (GTK_CONTAINER (frame), alignment);
619         gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, FALSE, 0);
620         
621         /* Setup assistant page */
622         gtk_notebook_append_page (GTK_NOTEBOOK(priv->notebook), page, NULL);
623                 
624         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK(priv->notebook), page,
625                                          _("Outgoing details"));
626         gtk_widget_show_all (page);
627
628 }
629
630 static void
631 modest_account_assistant_init (ModestAccountAssistant *obj)
632 {       
633         ModestAccountAssistantPrivate *priv;    
634         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(obj);       
635
636         priv->account_mgr       = NULL;
637
638         priv->store_server_widget       = NULL;
639         priv->transport_widget  = NULL;
640         priv->settings = modest_account_settings_new ();
641         priv->dirty = FALSE;
642
643         priv->notebook = gtk_notebook_new ();
644         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), FALSE);
645
646         g_object_set (obj, "wizard-notebook", priv->notebook, NULL);
647         g_object_set (obj, "wizard-name", _("Account wizard"), NULL);
648
649         add_intro_page (obj);
650         add_identity_page (obj); 
651         add_receiving_page (obj); 
652         add_sending_page (obj);
653
654         gtk_notebook_set_current_page (GTK_NOTEBOOK(priv->notebook), 0);
655         gtk_window_set_resizable (GTK_WINDOW(obj), TRUE);       
656         gtk_window_set_default_size (GTK_WINDOW(obj), 400, 400);
657         
658         gtk_window_set_modal (GTK_WINDOW(obj), TRUE);
659
660         g_signal_connect_after (G_OBJECT (obj), "response",
661                                 G_CALLBACK (on_response), obj);
662
663         /* This is to show a confirmation dialog when the user hits cancel */
664         g_signal_connect (G_OBJECT (obj), "response",
665                           G_CALLBACK (on_response_before), obj);
666
667         g_signal_connect (G_OBJECT (obj), "delete-event",
668                           G_CALLBACK (on_delete_event), obj);
669 }
670
671 static void
672 modest_account_assistant_finalize (GObject *obj)
673 {
674         ModestAccountAssistantPrivate *priv;
675                 
676         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(obj);
677         
678         if (priv->account_mgr) {
679                 g_object_unref (G_OBJECT(priv->account_mgr));
680                 priv->account_mgr = NULL;
681         }
682
683         if (priv->settings) {
684                 g_object_unref (G_OBJECT (priv->settings));
685                 priv->settings = NULL;
686         }
687         
688         /* These had to stay alive for as long as the comboboxes that used them: */
689         modest_pair_list_free (priv->receiving_transport_store_protos);
690         modest_pair_list_free (priv->sending_transport_store_protos);
691
692         G_OBJECT_CLASS(parent_class)->finalize (obj);
693 }
694
695 static const gchar*
696 get_account_name (ModestAccountAssistant *self)
697 {
698         ModestAccountAssistantPrivate *priv;
699         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
700
701         return gtk_entry_get_text (GTK_ENTRY(priv->account_name));
702 }
703
704 static const gchar*
705 get_fullname (ModestAccountAssistant *self)
706 {
707         ModestAccountAssistantPrivate *priv;
708         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
709
710         return gtk_entry_get_text (GTK_ENTRY(priv->fullname));
711 }
712
713
714
715 static const gchar*
716 get_email (ModestAccountAssistant *self)
717 {
718         ModestAccountAssistantPrivate *priv;
719         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
720
721         return gtk_entry_get_text (GTK_ENTRY(priv->email));
722 }
723
724
725
726 /*
727  * FIXME: hmmmm this a Camel internal thing, should move this
728  * somewhere else
729  */
730 static gchar*
731 get_account_uri (ModestProtocolType proto, const gchar* path)
732 {
733         CamelURL *url;
734         gchar *uri;
735         
736         if (proto == modest_protocol_registry_get_mbox_type_id ()) {
737                 url = camel_url_new ("mbox:", NULL);
738         } else {
739                 if (proto == modest_protocol_registry_get_maildir_type_id ())
740                         url = camel_url_new ("maildir:", NULL);
741                 else
742                         g_return_val_if_reached (NULL);
743         }
744
745         camel_url_set_path (url, path);
746         uri = camel_url_to_string (url, 0);
747         camel_url_free (url);
748
749         return uri;     
750 }
751
752 static gchar*
753 get_new_server_account_name (ModestAccountMgr* acc_mgr, 
754                              ModestProtocolType proto_id,
755                              const gchar *username, 
756                              const gchar *servername)
757 {
758         gchar *name;
759         const gchar *proto_name;
760         gint  i = 0;
761         ModestProtocolRegistry *registry;
762         ModestProtocol *proto;
763
764         registry = modest_runtime_get_protocol_registry ();
765         proto = modest_protocol_registry_get_protocol_by_type (registry, proto_id);
766         proto_name = modest_protocol_get_name (proto);
767
768         while (TRUE) {
769                 name = g_strdup_printf ("%s:%d", proto_name, i++);
770
771                 if (modest_account_mgr_account_exists (acc_mgr, name, TRUE))
772                         g_free (name);
773                 else
774                         break;
775         }
776         return name;
777 }
778
779
780
781 GtkWidget*
782 modest_account_assistant_new (ModestAccountMgr *account_mgr)
783 {
784         GObject *obj;
785         ModestAccountAssistant *self;
786         ModestAccountAssistantPrivate *priv;
787
788         g_return_val_if_fail (account_mgr, NULL);
789         
790         obj  = g_object_new(MODEST_TYPE_ACCOUNT_ASSISTANT, NULL);
791         self = MODEST_ACCOUNT_ASSISTANT(obj);
792
793         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
794
795         g_object_ref (account_mgr);
796         priv->account_mgr = account_mgr;
797
798         return GTK_WIDGET(self);
799 }
800
801 static gchar*
802 get_entered_account_title (ModestAccountAssistant *self)
803 {
804         ModestAccountAssistantPrivate *priv;
805         const gchar* account_title;
806
807         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
808         account_title = gtk_entry_get_text (GTK_ENTRY (priv->account_name));
809
810         if (!account_title || (strlen (account_title) == 0)) {
811                 return NULL;
812         } else {
813                 /* Strip it of whitespace at the start and end: */
814                 gchar *result = g_strdup (account_title);
815                 result = g_strstrip (result);
816                 
817                 if (!result)
818                         return NULL;
819                         
820                 if (strlen (result) == 0) {
821                         g_free (result);
822                         return NULL;    
823                 }
824                 
825                 return result;
826         }
827 }
828
829 static gboolean
830 on_before_next (ModestWizardDialog *dialog, GtkWidget *current_page, GtkWidget *next_page)
831 {
832         ModestAccountAssistant *self = MODEST_ACCOUNT_ASSISTANT (dialog);
833         ModestAccountAssistantPrivate *priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE (self);
834
835         /* Do extra validation that couldn't be done for every key press,
836          * either because it was too slow,
837          * or because it requires interaction:
838          */
839         if(!next_page) /* This is NULL when this is a click on Finish. */
840         {
841                 save_to_settings (self);
842                 modest_account_mgr_add_account_from_settings (modest_runtime_get_account_mgr (), priv->settings);
843         }
844         
845         
846         return TRUE;
847 }
848
849 static gint 
850 get_serverport_incoming(ModestProtocolType protocol,
851                         ModestProtocolType security)
852 {
853         int serverport_incoming = 0;
854
855         /* We don't check for SMTP here as that is impossible for an incoming server. */
856         if ((security == modest_protocol_registry_get_none_connection_type_id ()) ||
857             (security == modest_protocol_registry_get_tls_connection_type_id ()) ||
858             (security == modest_protocol_registry_get_tlsop_connection_type_id ())) {
859
860                 if (protocol == MODEST_PROTOCOLS_STORE_IMAP) {
861                         serverport_incoming = 143;
862                 } else if (protocol == MODEST_PROTOCOLS_STORE_POP) {
863                         serverport_incoming = 110;
864                 }
865         } else if (security == modest_protocol_registry_get_ssl_connection_type_id ()) {
866                 if (protocol == MODEST_PROTOCOLS_STORE_IMAP) {
867                         serverport_incoming = 993;
868                 } else if  (protocol == MODEST_PROTOCOLS_STORE_POP) {
869                         serverport_incoming = 995;
870                 }
871         }
872
873         return serverport_incoming;
874 }
875
876 static GList* 
877 check_for_supported_auth_methods (ModestAccountAssistant* self)
878 {
879         GError *error = NULL;
880         ModestProtocolType protocol;
881         const gchar* hostname;
882         const gchar* username;
883         gchar *store_protocol_name, *store_security_name;
884         ModestProtocolType security_protocol;
885         int port_num; 
886         GList *list_auth_methods;
887         ModestAccountAssistantPrivate *priv;
888         ModestProtocolRegistry *registry;
889         ModestProtocol *proto;
890         
891         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE (self);
892         hostname = gtk_entry_get_text(GTK_ENTRY(priv->store_server_widget));
893         username = gtk_entry_get_text(GTK_ENTRY(priv->username));
894         store_protocol_name = gtk_combo_box_get_active_text (GTK_COMBO_BOX (priv->store_protocol_combo));
895         registry = modest_runtime_get_protocol_registry ();
896         proto = modest_protocol_registry_get_protocol_by_name (registry,
897                                                                MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,
898                                                                store_protocol_name);
899         protocol = modest_protocol_get_type_id (proto);
900
901         g_free (store_protocol_name);
902         store_security_name = gtk_combo_box_get_active_text (GTK_COMBO_BOX (priv->store_security_combo));
903
904         proto = modest_protocol_registry_get_protocol_by_name (registry,
905                                                                MODEST_PROTOCOL_REGISTRY_CONNECTION_PROTOCOLS,
906                                                                store_security_name);
907         security_protocol = modest_protocol_get_type_id (proto);
908         g_free (store_security_name);
909         port_num = get_serverport_incoming(protocol, security_protocol); 
910         list_auth_methods = modest_utils_get_supported_secure_authentication_methods (protocol, hostname, port_num, 
911                                                                                       username, GTK_WINDOW (self), &error);
912
913         if (list_auth_methods) {
914                 /* TODO: Select the correct method */
915                 GList* list = NULL;
916                 GList* method;
917                 for (method = list_auth_methods; method != NULL; method = g_list_next(method)) {
918                         ModestProtocolType auth = (ModestProtocolType) (GPOINTER_TO_INT(method->data));
919                         if (modest_protocol_registry_protocol_type_is_secure (registry, auth)) {
920                                 list = g_list_append(list, GINT_TO_POINTER(auth));
921                         }
922                 }
923
924                 g_list_free(list_auth_methods);
925
926                 if (list)
927                         return list;
928         }
929
930         if(error != NULL)
931                 g_error_free(error);
932
933         return NULL;
934 }
935
936 static ModestProtocolType check_first_supported_auth_method(ModestAccountAssistant* self)
937 {
938         ModestProtocolType result = MODEST_PROTOCOLS_AUTH_PASSWORD;
939
940         GList* methods = check_for_supported_auth_methods(self);
941         if (methods)
942         {
943                 /* Use the first one: */
944                 result = (ModestProtocolType) (GPOINTER_TO_INT(methods->data));
945                 g_list_free(methods);
946         }
947
948         return result;
949 }
950
951 /**
952  * save_to_settings:
953  * @self: a #ModestEasysetupWizardDialog
954  *
955  * takes information from all the wizard and stores it in settings
956  */
957 static void
958 save_to_settings (ModestAccountAssistant *self)
959 {
960         ModestAccountAssistantPrivate *priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE (self);
961         gchar* display_name;
962         const gchar *username, *password;
963         gchar *store_hostname, *transport_hostname;
964         guint store_port, transport_port;
965         ModestProtocolType store_protocol, transport_protocol;
966         ModestProtocolType store_security, transport_security;
967         ModestProtocolType store_auth_protocol, transport_auth_protocol;
968         ModestServerAccountSettings *store_settings, *transport_settings;
969         const gchar *fullname, *email_address;
970
971         /* username and password (for both incoming and outgoing): */
972         username = gtk_entry_get_text (GTK_ENTRY (priv->username));
973         password = gtk_entry_get_text (GTK_ENTRY (priv->password));
974
975         /* Incoming server: */
976         /* Note: We need something as default for the ModestTransportStoreProtocol* values, 
977          * or modest_account_mgr_add_server_account will fail. */
978         store_port = 0;
979         store_protocol = MODEST_PROTOCOLS_STORE_POP;
980         store_security = MODEST_PROTOCOLS_CONNECTION_NONE;
981         store_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
982
983         /* Use custom pages because no preset was specified: */
984         store_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->store_server_widget) ));                
985         store_protocol = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->store_protocol_combo));
986         store_security = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->store_security_combo));
987
988         /* The UI spec says: 
989          * If secure authentication is unchecked, allow sending username and password also as plain text.
990          * If secure authentication is checked, require one of the secure methods during 
991          * connection: SSL, TLS, CRAM-MD5 etc. */
992         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->store_secure_auth)) &&
993             !modest_protocol_registry_protocol_type_is_secure(modest_runtime_get_protocol_registry (),
994                                                               store_security)) {
995                 store_auth_protocol = check_first_supported_auth_method (self);
996         } else {
997                 store_auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
998         }
999
1000         /* now we store the store account settings */
1001         store_settings = modest_account_settings_get_store_settings (priv->settings);
1002         modest_server_account_settings_set_hostname (store_settings, store_hostname);
1003         modest_server_account_settings_set_username (store_settings, username);
1004         modest_server_account_settings_set_password (store_settings, password);
1005         modest_server_account_settings_set_protocol (store_settings, store_protocol);
1006         modest_server_account_settings_set_security_protocol (store_settings, store_security);
1007         modest_server_account_settings_set_auth_protocol (store_settings, store_auth_protocol);
1008         if (store_port != 0)
1009                 modest_server_account_settings_set_port (store_settings, store_port);
1010
1011         g_object_unref (store_settings);
1012         g_free (store_hostname);
1013         
1014         /* Outgoing server: */
1015         transport_hostname = NULL;
1016         transport_protocol = MODEST_PROTOCOLS_STORE_POP;
1017         transport_security = MODEST_PROTOCOLS_CONNECTION_NONE;
1018         transport_auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
1019         transport_port = 0;
1020         
1021         transport_hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->transport_server_widget) ));
1022         transport_protocol = MODEST_PROTOCOLS_TRANSPORT_SMTP; /* It's always SMTP for outgoing. */
1023         transport_security = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->transport_security_combo));
1024         transport_auth_protocol = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->transport_secure_auth_combo));
1025             
1026         /* now we transport the transport account settings */
1027         transport_settings = modest_account_settings_get_transport_settings (priv->settings);
1028         modest_server_account_settings_set_hostname (transport_settings, transport_hostname);
1029         modest_server_account_settings_set_username (transport_settings, username);
1030         modest_server_account_settings_set_password (transport_settings, password);
1031         modest_server_account_settings_set_protocol (transport_settings, transport_protocol);
1032         modest_server_account_settings_set_security_protocol (transport_settings, transport_security);
1033         modest_server_account_settings_set_auth_protocol (transport_settings, transport_auth_protocol);
1034         if (transport_port != 0)
1035                 modest_server_account_settings_set_port (transport_settings, transport_port);
1036
1037         g_object_unref (transport_settings);
1038         g_free (transport_hostname);
1039         
1040         fullname = gtk_entry_get_text (GTK_ENTRY (priv->fullname));
1041         email_address = gtk_entry_get_text (GTK_ENTRY (priv->email));
1042         modest_account_settings_set_fullname (priv->settings, fullname);
1043         modest_account_settings_set_email_address (priv->settings, email_address);
1044         /* we don't set retrieve type to preserve advanced settings if any. By default account settings
1045            are set to headers only */
1046         
1047         display_name = get_entered_account_title (self);
1048         modest_account_settings_set_display_name (priv->settings, display_name);
1049         g_free (display_name);
1050
1051 }
1052
1053 static void 
1054 on_response (ModestWizardDialog *wizard_dialog,
1055              gint response_id,
1056              gpointer user_data)
1057 {
1058         ModestAccountAssistant *self = MODEST_ACCOUNT_ASSISTANT (wizard_dialog);
1059
1060         invoke_enable_buttons_vfunc (self);
1061 }
1062
1063 static void 
1064 on_response_before (ModestWizardDialog *wizard_dialog,
1065                     gint response_id,
1066                     gpointer user_data)
1067 {
1068         ModestAccountAssistant *self = MODEST_ACCOUNT_ASSISTANT (wizard_dialog);
1069         ModestAccountAssistantPrivate *priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(wizard_dialog);
1070
1071         if (response_id == GTK_RESPONSE_CANCEL) {
1072                 /* This is mostly copied from
1073                  * src/maemo/modest-account-settings-dialog.c */
1074                 if (priv->dirty) {
1075                         gint dialog_response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
1076                                                                                         _("imum_nc_wizard_confirm_lose_changes"));
1077
1078                         if (dialog_response != GTK_RESPONSE_OK) {
1079                                 /* Don't let the dialog close */
1080                                 g_signal_stop_emission_by_name (wizard_dialog, "response");
1081                         }
1082                 }
1083         }
1084 }
1085