36d670ae1320d8334f010a5b41e019563f7be7a5
[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-protocol-info.h>
39
40 #include <string.h>
41
42 /* 'private'/'protected' functions */
43 static void       modest_account_assistant_class_init    (ModestAccountAssistantClass *klass);
44 static void       modest_account_assistant_init          (ModestAccountAssistant *obj);
45 static void       modest_account_assistant_finalize      (GObject *obj);
46
47 /* list my signals */
48 enum {
49         /* MY_SIGNAL_1, */
50         /* MY_SIGNAL_2, */
51         LAST_SIGNAL
52 };
53
54 typedef struct _ModestAccountAssistantPrivate ModestAccountAssistantPrivate;
55 struct _ModestAccountAssistantPrivate {
56
57         ModestAccountMgr *account_mgr;
58
59         GtkWidget *account_name;
60         GtkWidget *fullname;
61         GtkWidget *email;
62        
63         GtkWidget *username;
64         GtkWidget *password;
65         GtkWidget *store_server_widget;
66         GtkWidget *store_protocol_combo;
67         GtkWidget *store_security_combo;
68         GtkWidget *store_secure_auth;
69         
70         GtkWidget *transport_widget;
71
72         GtkWidget *transport_holder;
73
74         ModestPairList *receiving_transport_store_protos;
75         ModestPairList *sending_transport_store_protos;
76         ModestPairList *security_protos;
77 };
78
79 #define MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                       MODEST_TYPE_ACCOUNT_ASSISTANT, \
81                                                       ModestAccountAssistantPrivate))
82 /* globals */
83 static GtkAssistantClass *parent_class = NULL;
84
85 /* uncomment the following if you have defined any signals */
86 /* static guint signals[LAST_SIGNAL] = {0}; */
87
88 GType
89 modest_account_assistant_get_type (void)
90 {
91         static GType my_type = 0;
92         if (!my_type) {
93                 static const GTypeInfo my_info = {
94                         sizeof(ModestAccountAssistantClass),
95                         NULL,           /* base init */
96                         NULL,           /* base finalize */
97                         (GClassInitFunc) modest_account_assistant_class_init,
98                         NULL,           /* class finalize */
99                         NULL,           /* class data */
100                         sizeof(ModestAccountAssistant),
101                         1,              /* n_preallocs */
102                         (GInstanceInitFunc) modest_account_assistant_init,
103                         NULL
104                 };
105                 my_type = g_type_register_static (GTK_TYPE_ASSISTANT,
106                                                   "ModestAccountAssistant",
107                                                   &my_info, 0);
108         }
109         return my_type;
110 }
111
112 static void
113 modest_account_assistant_class_init (ModestAccountAssistantClass *klass)
114 {
115         GObjectClass *gobject_class;
116         gobject_class = (GObjectClass*) klass;
117
118         parent_class            = g_type_class_peek_parent (klass);
119         gobject_class->finalize = modest_account_assistant_finalize;
120
121         g_type_class_add_private (gobject_class, sizeof(ModestAccountAssistantPrivate));
122
123         /* signal definitions go here, e.g.: */
124 /*      signals[MY_SIGNAL_1] = */
125 /*              g_signal_new ("my_signal_1",....); */
126 /*      signals[MY_SIGNAL_2] = */
127 /*              g_signal_new ("my_signal_2",....); */
128 /*      etc. */
129 }
130
131
132
133 static void
134 add_intro_page (ModestAccountAssistant *assistant)
135 {
136         GtkWidget *page, *label;
137         
138         page = gtk_vbox_new (FALSE, 12);
139         
140         label = gtk_label_new (_("mcen_ia_emailsetup_intro"));
141         gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
142         gtk_misc_set_padding (GTK_MISC (label), 12, 12);
143         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
144         gtk_box_pack_start (GTK_BOX(page), label, FALSE, FALSE, 0);
145         gtk_widget_set_size_request (label, 400, -1);
146         gtk_widget_show_all (page);
147         
148         gtk_assistant_append_page (GTK_ASSISTANT(assistant), page);
149                 
150         gtk_assistant_set_page_title (GTK_ASSISTANT(assistant), page,
151                                       _("mcen_ti_emailsetup_welcome"));
152         gtk_assistant_set_page_type (GTK_ASSISTANT(assistant), page,
153                                      GTK_ASSISTANT_PAGE_INTRO);
154         gtk_assistant_set_page_complete (GTK_ASSISTANT(assistant),
155                                          page, TRUE);
156 }
157
158
159 static void
160 set_current_page_complete (ModestAccountAssistant *self, gboolean complete)
161 {
162         GtkWidget *page;
163         gint pageno;
164
165         pageno = gtk_assistant_get_current_page (GTK_ASSISTANT(self));
166
167         if (pageno != -1) {
168                 page   = gtk_assistant_get_nth_page (GTK_ASSISTANT(self), pageno);
169                 gtk_assistant_set_page_complete (GTK_ASSISTANT(self), page, complete);
170         }
171 }
172
173
174 static void
175 identity_page_update_completeness (GtkEditable *editable,
176                                    ModestAccountAssistant *self)
177 {
178         ModestAccountAssistantPrivate *priv;
179         const gchar *txt;
180
181         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
182
183         txt = gtk_entry_get_text (GTK_ENTRY(priv->fullname));
184         if (!txt || strlen(txt) == 0) {
185                 set_current_page_complete (self, FALSE);
186                 return;
187         }
188
189         /* FIXME: regexp check for email address */
190         txt = gtk_entry_get_text (GTK_ENTRY(priv->email));
191         if (!modest_text_utils_validate_email_address (txt, NULL)) {
192                 set_current_page_complete (self, FALSE);
193                 return;
194         }
195
196         txt = gtk_entry_get_text (GTK_ENTRY(priv->username));
197         if (!txt || txt[0] == '\0') {
198                 set_current_page_complete (self, FALSE);
199                 return;
200         }
201         set_current_page_complete (self, TRUE);
202 }
203
204 static GtkWidget *
205 field_name_label (const gchar *text)
206 {
207         GtkWidget *label;
208         gchar *fixed_text;
209
210         fixed_text = g_strconcat (text, ":", NULL);
211         label = gtk_label_new (fixed_text);
212         g_free (fixed_text);
213         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
214
215         return label;
216 }
217
218 static void
219 add_identity_page (ModestAccountAssistant *self)
220 {
221         GtkWidget *page, *label, *table, *frame;
222         GtkWidget *alignment;
223         ModestAccountAssistantPrivate *priv;
224
225         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
226
227         priv->account_name = gtk_entry_new ();
228         gtk_entry_set_max_length (GTK_ENTRY (priv->fullname), 40);
229         priv->fullname = gtk_entry_new ();
230         gtk_entry_set_max_length (GTK_ENTRY (priv->fullname), 40);
231         priv->email    = gtk_entry_new ();
232         gtk_entry_set_width_chars (GTK_ENTRY (priv->email), 40);
233         priv->username = gtk_entry_new ();
234         gtk_entry_set_width_chars (GTK_ENTRY (priv->username), 40);
235         priv->password = gtk_entry_new ();
236         gtk_entry_set_width_chars (GTK_ENTRY (priv->password), 40);
237         gtk_entry_set_visibility (GTK_ENTRY (priv->password), FALSE);
238         
239         page = gtk_vbox_new (FALSE, 24);
240
241         label = gtk_label_new (
242                 _("Please enter below the name for the account you're creating."));
243         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
244         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
245         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
246         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 0, 12, 12);
247         gtk_container_add (GTK_CONTAINER (alignment), label);
248         gtk_box_pack_start (GTK_BOX(page), alignment, FALSE, FALSE, 0);
249         
250         table = gtk_table_new (1,2, FALSE);
251         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
252         gtk_table_set_row_spacings (GTK_TABLE (table), 1);
253         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Account name")),
254                                    0,1,0,1);
255         gtk_table_attach_defaults (GTK_TABLE(table),priv->account_name,
256                                    1,2,0,1);
257         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
258         gtk_container_add (GTK_CONTAINER (alignment), table);
259         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 0, 12, 0);
260         gtk_box_pack_start (GTK_BOX(page), alignment, FALSE, FALSE, 0);
261
262         frame = gtk_frame_new (NULL);
263         label = gtk_label_new (NULL);
264         gtk_label_set_markup (GTK_LABEL (label), _("<b>Public information </b>"));
265         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
266         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
267         table = gtk_table_new (2,2, FALSE);
268         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
269         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
270         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Full name")),
271                                    0,1,0,1);
272         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Email address")),
273                                    0,1,1,2);
274         gtk_table_attach_defaults (GTK_TABLE(table),priv->fullname,
275                                    1,2,0,1);
276         gtk_table_attach_defaults (GTK_TABLE(table),priv->email,
277                                    1,2,1,2);
278         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
279         gtk_container_add (GTK_CONTAINER (alignment), table);
280         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
281         gtk_container_add (GTK_CONTAINER (frame), alignment);
282         gtk_box_pack_start (GTK_BOX(page), frame, FALSE, FALSE, 0);
283
284
285         frame = gtk_frame_new (NULL);
286         label = gtk_label_new (NULL);
287         gtk_label_set_markup (GTK_LABEL (label), _("<b>Server account </b>"));
288         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
289         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
290         table = gtk_table_new (2,2, FALSE);
291         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
292         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
293         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("User name")),
294                                    0,1,0,1);
295         gtk_table_attach_defaults (GTK_TABLE(table),field_name_label (_("Password")),
296                                    0,1,1,2);
297         gtk_table_attach_defaults (GTK_TABLE(table),priv->username,
298                                    1,2,0,1);
299         gtk_table_attach_defaults (GTK_TABLE(table),priv->password,
300                                    1,2,1,2);
301         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
302         gtk_container_add (GTK_CONTAINER (alignment), table);
303         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
304         gtk_container_add (GTK_CONTAINER (frame), alignment);
305         gtk_box_pack_start (GTK_BOX(page), frame, FALSE, FALSE, 0);
306
307         g_signal_connect (G_OBJECT(priv->fullname), "changed",
308                           G_CALLBACK(identity_page_update_completeness),
309                           self);
310         g_signal_connect (G_OBJECT(priv->username), "changed",
311                           G_CALLBACK(identity_page_update_completeness),
312                           self);
313         g_signal_connect (G_OBJECT(priv->password), "changed",
314                           G_CALLBACK(identity_page_update_completeness),
315                           self);
316         g_signal_connect (G_OBJECT(priv->email), "changed",
317                           G_CALLBACK(identity_page_update_completeness),
318                           self);
319         g_signal_connect (G_OBJECT(priv->account_name), "changed",
320                           G_CALLBACK(identity_page_update_completeness),
321                           self);
322         
323         
324         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
325         gtk_container_add (GTK_CONTAINER (alignment), page);
326         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 12, 12, 12, 12);
327         gtk_widget_show_all (alignment);
328         gtk_assistant_append_page (GTK_ASSISTANT(self), alignment);
329         
330         gtk_assistant_set_page_title (GTK_ASSISTANT(self), alignment,
331                                       _("Identity"));
332         gtk_assistant_set_page_type (GTK_ASSISTANT(self), alignment,
333                                      GTK_ASSISTANT_PAGE_CONTENT);
334         gtk_assistant_set_page_complete (GTK_ASSISTANT(self),
335                                          alignment, FALSE);
336 }       
337
338
339 static void
340 receiving_page_update_completeness (GtkEditable *editable,
341                                     ModestAccountAssistant *self)
342 {
343         ModestAccountAssistantPrivate *priv;
344         const gchar *txt;
345
346         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
347
348         txt = gtk_entry_get_text (GTK_ENTRY (priv->store_server_widget));
349         if (!txt || strlen(txt) == 0) {
350                 set_current_page_complete (self, FALSE);
351                 return;
352         }
353         set_current_page_complete (self, TRUE);
354 }
355
356 static void
357 add_receiving_page (ModestAccountAssistant *self)
358 {
359         GtkWidget *page, *vbox;
360         GtkWidget *table, *frame;
361         GtkWidget *alignment;
362         ModestAccountAssistantPrivate *priv;
363         GtkWidget *label;
364
365         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
366         page = gtk_alignment_new (0.5, 0.0, 1.0, 0.0);
367         gtk_alignment_set_padding (GTK_ALIGNMENT (page), 12, 12, 12, 12);
368         vbox = gtk_vbox_new (FALSE, 24);
369         gtk_container_add (GTK_CONTAINER (page), vbox);
370
371         /* Warning label on top */
372         label = gtk_label_new (_("TODO: Note unable to..."));
373         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
374         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
375         gtk_box_pack_start (GTK_BOX(vbox),
376                             label,
377                             FALSE, FALSE, 0);
378
379         /* Note: This ModestPairList* must exist for as long as the combo
380          * that uses it, because the ModestComboBox uses the ID opaquely, 
381          * so it can't know how to manage its memory. */
382         priv->receiving_transport_store_protos = 
383                 modest_protocol_info_get_supported_store_protocol_pair_list ();
384         priv->store_protocol_combo = modest_combo_box_new (priv->receiving_transport_store_protos, g_str_equal);
385         priv->store_server_widget = gtk_entry_new ();
386
387         /* Setup incoming server frame */
388         frame = gtk_frame_new (NULL);
389         label = gtk_label_new (NULL);
390         gtk_label_set_markup (GTK_LABEL (label), _("<b>Incoming server</b>"));
391         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
392         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
393         table = gtk_table_new (2, 2, FALSE);
394         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
395         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
396         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Account type")),
397                           0, 1, 0, 1,
398                           GTK_FILL, 0, 0, 0);
399         alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
400         gtk_container_add (GTK_CONTAINER (alignment), priv->store_protocol_combo);
401         gtk_table_attach (GTK_TABLE (table), alignment,
402                           1, 2, 0, 1,
403                           GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
404         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Incoming server")),
405                           0, 1, 1, 2,
406                           GTK_FILL, GTK_FILL, 0, 0);
407         gtk_table_attach (GTK_TABLE (table), priv->store_server_widget,
408                           1, 2, 1, 2,
409                           GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
410         alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
411         gtk_container_add (GTK_CONTAINER (alignment), table);
412         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
413         gtk_container_add (GTK_CONTAINER (frame), alignment);
414         gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, TRUE, 0);
415
416         /* Setup security information widgets */
417         priv->security_protos = 
418                 modest_protocol_info_get_connection_protocol_pair_list ();
419         priv->store_security_combo = modest_combo_box_new (priv->security_protos, g_str_equal);
420         priv->store_secure_auth = gtk_check_button_new ();
421
422         /* Setup security frame */
423         frame = gtk_frame_new (NULL);
424         label = gtk_label_new (NULL);
425         gtk_label_set_markup (GTK_LABEL (label), _("<b>Security options</b>"));
426         gtk_frame_set_label_widget (GTK_FRAME (frame), label);
427         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
428         table = gtk_table_new (2, 2, FALSE);
429         gtk_table_set_col_spacings (GTK_TABLE (table), 6);
430         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
431         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Secure connection")),
432                           0, 1, 0, 1,
433                           GTK_FILL, 0, 0, 0);
434         gtk_table_attach (GTK_TABLE (table), priv->store_security_combo,
435                           1, 2, 0, 1,
436                           GTK_FILL | GTK_EXPAND, 0, 0, 0);
437         gtk_table_attach (GTK_TABLE (table), field_name_label (_("Use secure authentication")),
438                           0, 1, 1, 2,
439                           GTK_FILL, GTK_FILL, 0, 0);
440         gtk_table_attach_defaults (GTK_TABLE (table), priv->store_secure_auth,
441                                    1, 2, 1, 2);
442         alignment = gtk_alignment_new (0.0, 0.0, 1.0, 0.0);
443         gtk_container_add (GTK_CONTAINER (alignment), table);
444         gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);
445         gtk_container_add (GTK_CONTAINER (frame), alignment);
446         gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, FALSE, 0);
447         
448         /* Setup assistant page */
449         gtk_assistant_append_page (GTK_ASSISTANT(self), page);
450                 
451         gtk_assistant_set_page_title (GTK_ASSISTANT(self), page,
452                                       _("Receiving mail"));
453         gtk_assistant_set_page_type (GTK_ASSISTANT(self), page,
454                                      GTK_ASSISTANT_PAGE_CONTENT);
455         gtk_assistant_set_page_complete (GTK_ASSISTANT(self),
456                                          page, FALSE);
457         gtk_widget_show_all (page);
458 }
459
460
461
462
463 static void
464 on_sending_combo_box_changed (GtkComboBox *combo, ModestAccountAssistant *self)
465 {
466         ModestAccountAssistantPrivate *priv;
467         gchar *chosen;
468         
469         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
470
471         chosen = gtk_combo_box_get_active_text (GTK_COMBO_BOX(combo));
472
473         if (priv->transport_widget)
474                 gtk_container_remove (GTK_CONTAINER(priv->transport_holder),
475                                       priv->transport_widget);
476         priv->transport_widget =
477                 modest_transport_widget_new (modest_protocol_info_get_transport_store_protocol(chosen));
478
479         gtk_container_add (GTK_CONTAINER(priv->transport_holder),
480                            priv->transport_widget);
481
482         gtk_widget_show_all (priv->transport_holder);
483 }
484
485
486
487 static void
488 add_sending_page (ModestAccountAssistant *self)
489 {
490         GtkWidget *page, *box, *combo;
491         ModestAccountAssistantPrivate *priv;
492
493         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
494         page = gtk_vbox_new (FALSE, 6);
495         
496         gtk_box_pack_start (GTK_BOX(page),
497                             gtk_label_new (
498                                     _("Please select among the following options")),
499                             FALSE, FALSE, 0);
500         box = gtk_hbox_new (FALSE, 0);
501         gtk_box_pack_start (GTK_BOX(box),
502                             gtk_label_new(_("Server type")),
503                             FALSE,FALSE,0);
504         
505         /* Note: This ModestPairList* must exist for as long as the combo
506          * that uses it, because the ModestComboBox uses the ID opaquely, 
507          * so it can't know how to manage its memory. */
508         priv->sending_transport_store_protos = modest_protocol_info_get_transport_store_protocol_pair_list ();
509         combo = modest_combo_box_new (priv->sending_transport_store_protos, g_str_equal);
510
511         g_signal_connect (G_OBJECT(combo), "changed",
512                           G_CALLBACK(on_sending_combo_box_changed), self);
513
514         gtk_box_pack_start (GTK_BOX(box), combo, FALSE,FALSE,0);
515         gtk_box_pack_start (GTK_BOX(page), box, FALSE,FALSE, 0);
516
517         gtk_box_pack_start (GTK_BOX(page), gtk_hseparator_new(), FALSE, FALSE, 0);
518
519         priv->transport_holder = gtk_hbox_new (FALSE, 0);
520         gtk_box_pack_start (GTK_BOX(page), priv->transport_holder,
521                             FALSE, FALSE, 0);
522
523         /* Force the selection */
524         on_sending_combo_box_changed (GTK_COMBO_BOX (combo), self);
525
526         gtk_assistant_append_page (GTK_ASSISTANT(self), page);
527                 
528         gtk_assistant_set_page_title (GTK_ASSISTANT(self), page,
529                                       _("Sending mail"));
530         gtk_assistant_set_page_type (GTK_ASSISTANT(self), page,
531                                      GTK_ASSISTANT_PAGE_INTRO);
532         gtk_assistant_set_page_complete (GTK_ASSISTANT(self),
533                                          page, TRUE);
534         gtk_widget_show_all (page);
535 }
536
537
538
539 static void
540 add_final_page (ModestAccountAssistant *self)
541 {
542         GtkWidget *page, *box;
543         ModestAccountAssistantPrivate *priv;
544
545         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
546         page = gtk_vbox_new (FALSE, 6);
547         
548         gtk_box_pack_start (GTK_BOX(page),
549                             gtk_label_new (
550                                     _("We're almost done. Press 'Apply' to store this new account")),
551                             FALSE, FALSE, 6);
552         box = gtk_hbox_new (FALSE, 6);
553         priv->account_name =
554                 gtk_entry_new_with_max_length (40);
555         gtk_entry_set_text (GTK_ENTRY(priv->account_name),
556                             gtk_entry_get_text(GTK_ENTRY(priv->email)));
557         gtk_box_pack_start (GTK_BOX(box),gtk_label_new(_("Account name:")),
558                             FALSE,FALSE,6);
559         gtk_box_pack_start (GTK_BOX(box),priv->account_name , FALSE,FALSE,6);
560         
561         gtk_box_pack_start (GTK_BOX(page), box, FALSE, FALSE, 6);
562         
563         gtk_assistant_append_page (GTK_ASSISTANT(self), page);
564                 
565         gtk_assistant_set_page_title (GTK_ASSISTANT(self), page,
566                                       _("Account Management"));
567         gtk_assistant_set_page_type (GTK_ASSISTANT(self), page,
568                                      GTK_ASSISTANT_PAGE_CONFIRM);
569
570         gtk_assistant_set_page_complete (GTK_ASSISTANT(self),
571                                          page, TRUE);
572         gtk_widget_show_all (page);
573 }
574
575
576 static void
577 modest_account_assistant_init (ModestAccountAssistant *obj)
578 {       
579         ModestAccountAssistantPrivate *priv;    
580         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(obj);       
581
582         priv->account_mgr       = NULL;
583
584         priv->store_server_widget       = NULL;
585         priv->transport_widget  = NULL;
586 }
587
588 static void
589 modest_account_assistant_finalize (GObject *obj)
590 {
591         ModestAccountAssistantPrivate *priv;
592                 
593         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(obj);
594         
595         if (priv->account_mgr) {
596                 g_object_unref (G_OBJECT(priv->account_mgr));
597                 priv->account_mgr = NULL;
598         }
599         
600         /* These had to stay alive for as long as the comboboxes that used them: */
601         modest_pair_list_free (priv->receiving_transport_store_protos);
602         modest_pair_list_free (priv->sending_transport_store_protos);
603
604         G_OBJECT_CLASS(parent_class)->finalize (obj);
605 }
606
607 static void
608 on_cancel (ModestAccountAssistant *self, gpointer user_data)
609 {
610         GtkWidget *label;
611         GtkWidget *dialog;
612         int response;
613         
614         label = gtk_label_new (_("Are you sure you want to cancel\n"
615                                  "setting up a new account?"));
616         
617         dialog = gtk_dialog_new_with_buttons (_("Cancel"),
618                                               GTK_WINDOW(self),
619                                               GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
620                                               GTK_STOCK_YES, GTK_RESPONSE_ACCEPT,
621                                               GTK_STOCK_NO,  GTK_RESPONSE_CANCEL,
622                                               NULL);
623         
624         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox),
625                             label, FALSE, FALSE, 6);
626
627         gtk_widget_show_all ((GTK_DIALOG(dialog)->vbox));
628         
629         gtk_window_set_resizable (GTK_WINDOW(dialog), FALSE);
630         
631         response = gtk_dialog_run (GTK_DIALOG(dialog));
632         gtk_widget_destroy (dialog);
633
634         switch (response) {
635         case GTK_RESPONSE_ACCEPT:
636                 /* close the assistant */
637                 gtk_widget_hide (GTK_WIDGET(self));
638                 break;
639         case GTK_RESPONSE_CANCEL:
640                 /* don't do anything */
641                 break;
642         default: g_assert_not_reached ();
643
644         };                           
645 }
646
647 static const gchar*
648 get_account_name (ModestAccountAssistant *self)
649 {
650         ModestAccountAssistantPrivate *priv;
651         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
652
653         return gtk_entry_get_text (GTK_ENTRY(priv->account_name));
654 }
655
656 static const gchar*
657 get_fullname (ModestAccountAssistant *self)
658 {
659         ModestAccountAssistantPrivate *priv;
660         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
661
662         return gtk_entry_get_text (GTK_ENTRY(priv->fullname));
663 }
664
665
666
667 static const gchar*
668 get_email (ModestAccountAssistant *self)
669 {
670         ModestAccountAssistantPrivate *priv;
671         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
672
673         return gtk_entry_get_text (GTK_ENTRY(priv->email));
674 }
675
676
677 static void
678 on_close (ModestAccountAssistant *self, gpointer user_data)
679 {
680         gtk_widget_hide (GTK_WIDGET (self));
681 }
682
683
684 /*
685  * FIXME: hmmmm this a Camel internal thing, should move this
686  * somewhere else
687  */
688 static gchar*
689 get_account_uri (ModestTransportStoreProtocol proto, const gchar* path)
690 {
691         CamelURL *url;
692         gchar *uri;
693         
694         switch (proto) {
695         case MODEST_PROTOCOL_STORE_MBOX:
696                 url = camel_url_new ("mbox:", NULL); break;
697         case MODEST_PROTOCOL_STORE_MAILDIR:
698                 url = camel_url_new ("maildir:", NULL); break;
699         default:
700                 g_return_val_if_reached (NULL);
701         }
702         camel_url_set_path (url, path);
703         uri = camel_url_to_string (url, 0);
704         camel_url_free (url);
705
706         return uri;     
707 }
708
709 static gchar*
710 get_new_server_account_name (ModestAccountMgr* acc_mgr, ModestTransportStoreProtocol proto,
711                              const gchar* username, const gchar *servername)
712 {
713         gchar *name;
714         gint  i = 0;
715         
716         while (TRUE) {
717                 name = g_strdup_printf ("%s:%d",
718                                         modest_protocol_info_get_transport_store_protocol_name(proto), i++);
719                 if (modest_account_mgr_account_exists (acc_mgr, name, TRUE))
720                         g_free (name);
721                 else
722                         break;
723         }
724         return name;
725 }
726
727
728 static void
729 on_apply (ModestAccountAssistant *self, gpointer user_data)
730 {
731 /*      ModestAccountAssistantPrivate *priv; */
732 /*      ModestTransportStoreProtocol proto = MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN; */
733 /*      ModestAuthProtocol security = MODEST_PROTOCOL_CONNECTION_NORMAL; */
734 /*      ModestConnectionProtocol auth = MODEST_PROTOCOL_AUTH_NONE; */
735 /*      gchar *store_name, *transport_name; */
736 /*      const gchar *account_name, *username, *servername, *path; */
737 /*      ModestTransportWidget *transport; */
738
739 /*      priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self); */
740
741 /*      /\* create server account -> store *\/ */
742 /*      store = MODEST_STORE_WIDGET(priv->store_widget); */
743 /*      proto    = modest_store_widget_get_proto (store); */
744 /*      username = modest_store_widget_get_username (store); */
745 /*      servername = modest_store_widget_get_servername (store); */
746 /*      path       = modest_store_widget_get_path (store); */
747 /*      security = modest_store_widget_get_security (store); */
748 /*      auth = modest_store_widget_get_auth (store); */
749 /*      store_name = get_new_server_account_name (priv->account_mgr, proto,username, servername); */
750
751 /*      if (proto == MODEST_PROTOCOL_STORE_MAILDIR || */
752 /*          proto == MODEST_PROTOCOL_STORE_MBOX) { */
753 /*              gchar *uri = get_account_uri (proto, path); */
754 /*              modest_account_mgr_add_server_account_uri (priv->account_mgr, store_name, proto, uri); */
755 /*              g_free (uri); */
756 /*      } else */
757 /*              modest_account_mgr_add_server_account (priv->account_mgr, */
758 /*                                                     store_name,  */
759 /*                                                     servername, */
760 /*                                                     0, /\* FIXME: does this mean default?*\/ */
761 /*                                                     username, */
762 /*                                                     NULL,  */
763 /*                                                     proto,  */
764 /*                                                     security,  */
765 /*                                                     auth); */
766                 
767 /*      /\* create server account -> transport *\/ */
768 /*      transport = MODEST_TRANSPORT_WIDGET(priv->transport_widget); */
769 /*      proto = modest_transport_widget_get_proto (transport); */
770 /*      username   = NULL; */
771 /*      servername = NULL; */
772 /*      if (proto == MODEST_PROTOCOL_TRANSPORT_SMTP) { */
773 /*              servername = modest_transport_widget_get_servername (transport); */
774 /*              if (modest_transport_widget_get_requires_auth (transport)) */
775 /*                      username = modest_transport_widget_get_username (transport); */
776 /*      } */
777         
778 /*      transport_name = get_new_server_account_name (priv->account_mgr, proto,username, servername); */
779 /*      modest_account_mgr_add_server_account (priv->account_mgr, */
780 /*                                             transport_name,  servername, */
781 /*                                             0, /\* FIXME: does this mean default?*\/ */
782 /*                                             username, NULL, */
783 /*                                             proto, security, auth); */
784
785 /*      /\* create account *\/ */
786 /*      account_name = get_account_name (self); */
787 /*      modest_account_mgr_add_account (priv->account_mgr, */
788 /*                                      account_name, */
789 /*                                      account_name, */
790 /*                                      get_fullname (self), */
791 /*                                      get_email (self), */
792 /*                                      MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY, */
793 /*                                      store_name, */
794 /*                                      transport_name, TRUE); */
795
796 /*      /\* Frees *\/    */
797 /*      g_free (store_name); */
798 /*      g_free (transport_name); */
799 }
800
801
802
803 GtkWidget*
804 modest_account_assistant_new (ModestAccountMgr *account_mgr)
805 {
806         GObject *obj;
807         ModestAccountAssistant *self;
808         ModestAccountAssistantPrivate *priv;
809
810         g_return_val_if_fail (account_mgr, NULL);
811         
812         obj  = g_object_new(MODEST_TYPE_ACCOUNT_ASSISTANT, NULL);
813         self = MODEST_ACCOUNT_ASSISTANT(obj);
814
815         priv = MODEST_ACCOUNT_ASSISTANT_GET_PRIVATE(self);
816
817         g_object_ref (account_mgr);
818         priv->account_mgr = account_mgr;
819
820         add_intro_page (self);
821         add_identity_page (self); 
822         add_receiving_page (self); 
823         add_sending_page (self);
824         add_final_page (self);
825
826         gtk_assistant_set_current_page (GTK_ASSISTANT(self), 0);
827         gtk_window_set_title (GTK_WINDOW(self),
828                               _("Modest Account Wizard"));
829         gtk_window_set_resizable (GTK_WINDOW(self), TRUE);      
830         gtk_window_set_default_size (GTK_WINDOW(self), 400, 400);
831         
832         gtk_window_set_modal (GTK_WINDOW(self), TRUE);
833
834         g_signal_connect (G_OBJECT(self), "apply",
835                           G_CALLBACK(on_apply), NULL);
836         g_signal_connect (G_OBJECT(self), "cancel",
837                           G_CALLBACK(on_cancel), NULL);
838         g_signal_connect (G_OBJECT(self), "close",
839                           G_CALLBACK(on_close), NULL);
840
841         return GTK_WIDGET(self);
842 }