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