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