2005-10-16 [colin] 1.9.15cvs56
[claws.git] / src / wizard.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2004 Colin Leroy
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtkwidget.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkbox.h>
33 #include <gtk/gtktable.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtknotebook.h>
37 #include <gtk/gtktogglebutton.h>
38 #include <gtk/gtkcheckbutton.h>
39 #include <gtk/gtk.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45
46 #ifdef HAVE_CONFIG_H
47 #  include "config.h"
48 #endif
49
50 #include "utils.h"
51 #include "gtk/menu.h"
52 #include "account.h"
53 #include "prefs_account.h"
54 #include "mainwindow.h"
55 #include "stock_pixmap.h"
56 #include "setup.h"
57 #include "folder.h"
58 #ifdef USE_OPENSSL                      
59 #include "ssl.h"
60 #endif
61 #include "prefs_common.h"
62
63 typedef enum
64 {
65         GO_BACK,
66         GO_FORWARD,
67         CANCEL,
68         FINISHED
69 } PageNavigation;
70
71 typedef struct
72 {
73         GtkWidget *window;
74         GSList    *pages;
75         GtkWidget *notebook;
76
77         MainWindow *mainwin;
78         
79         GtkWidget *email;
80         GtkWidget *full_name;
81         GtkWidget *organization;
82
83         GtkWidget *mailbox_name;
84         
85         GtkWidget *smtp_server;
86
87         GtkWidget *recv_type;
88         GtkWidget *recv_label;
89         GtkWidget *recv_server;
90         GtkWidget *recv_username;
91         GtkWidget *recv_password;
92         GtkWidget *recv_username_label;
93         GtkWidget *recv_password_label;
94         GtkWidget *recv_imap_label;
95         GtkWidget *recv_imap_subdir;
96
97 #ifdef USE_OPENSSL
98         GtkWidget *smtp_use_ssl;
99         GtkWidget *recv_use_ssl;
100 #endif
101         
102         gboolean create_mailbox;
103         gboolean finished;
104         gboolean result;
105
106 } WizardWindow;
107
108 static gboolean wizard_write_config(WizardWindow *wizard)
109 {
110         gboolean mailbox_ok = FALSE;
111         PrefsAccount *prefs_account = prefs_account_new();
112         GList *account_list = NULL;
113         GtkWidget *menu, *menuitem;
114         
115         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
116         menuitem = gtk_menu_get_active(GTK_MENU(menu));
117         prefs_account->protocol = GPOINTER_TO_INT
118                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
119
120         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
121                 mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
122                                 gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)));
123         } else
124                 mailbox_ok = TRUE;
125
126         if (!mailbox_ok) {
127                 gtk_notebook_set_current_page (
128                         GTK_NOTEBOOK(wizard->notebook), 
129                         4);
130                 return FALSE;
131         }
132         
133         if (prefs_account->protocol != A_LOCAL)
134                 prefs_account->account_name = g_strdup_printf("%s@%s",
135                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
136                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
137         else
138                 prefs_account->account_name = g_strdup_printf("%s",
139                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
140
141         prefs_account->name = g_strdup(
142                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
143         prefs_account->address = g_strdup(
144                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
145         prefs_account->organization = g_strdup(
146                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
147         prefs_account->smtp_server = g_strdup(
148                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
149
150         if (prefs_account->protocol != A_LOCAL)
151                 prefs_account->recv_server = g_strdup(
152                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
153         else
154                 prefs_account->local_mbox = g_strdup(
155                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
156
157         prefs_account->userid = g_strdup(
158                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
159         prefs_account->passwd = g_strdup(
160                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
161
162 #ifdef USE_OPENSSL                      
163         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
164                 prefs_account->ssl_smtp = SSL_TUNNEL;
165         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
166                 if (prefs_account->protocol == A_IMAP4)
167                         prefs_account->ssl_imap = SSL_TUNNEL;
168                 else
169                         prefs_account->ssl_pop = SSL_TUNNEL;
170         }
171 #endif
172         if (prefs_account->protocol == A_IMAP4) {
173                 gchar *directory = gtk_editable_get_chars(
174                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
175                 if (directory && strlen(directory)) {
176                         prefs_account->imap_dir = g_strdup(directory);
177                 }
178                 g_free(directory);
179         }
180
181         account_list = g_list_append(account_list, prefs_account);
182         prefs_account_write_config_all(account_list);
183         prefs_account_free(prefs_account);
184         account_read_config_all();
185         
186         return TRUE;
187 }
188
189 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
190 {
191         GtkWidget *w;
192         GtkWidget *vbox;
193         GtkWidget *hbox;
194         GtkWidget *image;
195         char *title_string;
196
197         vbox = gtk_vbox_new (FALSE, 6);
198         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
199
200         /* create the titlebar */
201         hbox = gtk_hbox_new (FALSE, 12);
202         image = stock_pixmap_widget(wizard->window, 
203                                 STOCK_PIXMAP_SYLPHEED_ICON);
204         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
205         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
206         w = gtk_label_new (title_string);
207         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
208         g_free (title_string);
209         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
210
211         /* pack the titlebar */
212         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
213
214         /* pack the separator */
215         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
216
217         /* pack space */
218         w = gtk_alignment_new (0, 0, 0, 0);
219         gtk_widget_set_size_request (w, 0, 6);
220         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
221
222         return vbox;
223 }
224
225 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
226         GtkWidget *label = gtk_label_new(text);                               \
227         gtk_table_attach(GTK_TABLE(table), label,                             \
228                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
229         if (GTK_IS_MISC(label))                                               \
230                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
231         gtk_table_attach(GTK_TABLE(table), entry,                             \
232                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
233 }
234
235 static gchar *get_default_email_addr(void)
236 {
237         gchar *domain_name = g_strdup(get_domain_name());
238         gchar *result;
239         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
240         && strlen(strchr(domain_name, '.')) > 6) {
241                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
242                 g_free(domain_name);
243                 domain_name = tmp;
244         }
245         result = g_strdup_printf("%s@%s",
246                                 g_get_user_name(),
247                                 domain_name);
248         g_free(domain_name);
249         return result;
250 }
251
252 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
253 {
254         gchar *domain_name = g_strdup(get_domain_name());
255         gchar *result;
256         
257         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
258         && strlen(strchr(domain_name, '.')) > 6) {
259                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
260                 g_free(domain_name);
261                 domain_name = tmp;
262         } else if (strchr(domain_name, '.') == NULL) {
263                 /* only hostname found, use email suffix */
264                 gchar *mail;
265                 mail = gtk_editable_get_chars(GTK_EDITABLE(wizard->email), 0, -1);
266
267                 if (strlen (mail) && strstr(mail, "@")) {
268                         g_free(domain_name);
269                         domain_name = g_strdup(strstr(mail, "@")+1);
270                 }
271                 g_free(mail);
272         }
273         result = g_strdup_printf("%s.%s",
274                                 type, domain_name);
275         g_free(domain_name);
276         return result;
277 }
278
279 static void wizard_email_changed(GtkWidget *widget, gpointer data)
280 {
281         WizardWindow *wizard = (WizardWindow *)data;
282         RecvProtocol protocol;
283         gchar *text;
284         protocol = GPOINTER_TO_INT
285                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
286         
287         text = get_default_server(wizard, "smtp");
288         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
289         g_free(text);
290
291         if (protocol == A_POP3) {
292                 text = get_default_server(wizard, "pop");
293                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
294                 g_free(text);
295         } else if (protocol == A_IMAP4) {
296                 text = get_default_server(wizard, "imap");
297                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
298                 g_free(text);
299         } else if (protocol == A_LOCAL) {
300                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
301                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
302                 g_free(mbox);
303         }
304 }
305
306 static GtkWidget* user_page (WizardWindow * wizard)
307 {
308         GtkWidget *table = gtk_table_new(3,2, FALSE);
309         gchar *text;
310         gint i = 0;
311         
312         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
313         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
314
315         wizard->full_name = gtk_entry_new();
316         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), g_get_real_name());
317         GTK_TABLE_ADD_ROW_AT(table, _("Your name:"), 
318                              wizard->full_name, i); i++;
319         
320         wizard->email = gtk_entry_new();
321         text = get_default_email_addr();
322         gtk_entry_set_text(GTK_ENTRY(wizard->email), text);
323         g_free(text);
324         GTK_TABLE_ADD_ROW_AT(table, _("Your email address:"), 
325                              wizard->email, i); i++;
326         
327         wizard->organization = gtk_entry_new();
328         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"), 
329                              wizard->organization, i); i++;
330         
331         g_signal_connect(G_OBJECT(wizard->email), "changed",
332                          G_CALLBACK(wizard_email_changed),
333                          wizard);
334         return table;
335 }
336
337 static GtkWidget* mailbox_page (WizardWindow * wizard)
338 {
339         GtkWidget *table = gtk_table_new(1,2, FALSE);
340         gint i = 0;
341         
342         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
343         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
344
345         wizard->mailbox_name = gtk_entry_new();
346         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), "Mail");
347         GTK_TABLE_ADD_ROW_AT(table, _("Mailbox name:"), 
348                              wizard->mailbox_name, i); i++;
349         
350         return table;
351 }
352
353 static GtkWidget* smtp_page (WizardWindow * wizard)
354 {
355         GtkWidget *table = gtk_table_new(1,2, FALSE);
356         gchar *text;
357         gint i = 0;
358         
359         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
360         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
361
362         wizard->smtp_server = gtk_entry_new();
363         text = get_default_server(wizard, "smtp");
364         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
365         g_free(text);
366         GTK_TABLE_ADD_ROW_AT(table, _("SMTP server address:"), 
367                              wizard->smtp_server, i); i++;
368         return table;
369 }
370
371 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
372 {
373         WizardWindow *wizard = (WizardWindow *)data;
374         RecvProtocol protocol;
375         gchar *text;
376         protocol = GPOINTER_TO_INT
377                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
378         
379         if (protocol == A_POP3) {
380                 text = get_default_server(wizard, "pop");
381                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
382                 gtk_widget_hide(wizard->recv_imap_label);
383                 gtk_widget_hide(wizard->recv_imap_subdir);
384                 gtk_widget_show(wizard->recv_username);
385                 gtk_widget_show(wizard->recv_password);
386                 gtk_widget_show(wizard->recv_username_label);
387                 gtk_widget_show(wizard->recv_password_label);
388                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
389                 g_free(text);
390         } else if (protocol == A_IMAP4) {
391                 text = get_default_server(wizard, "imap");
392                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
393                 gtk_widget_show(wizard->recv_imap_label);
394                 gtk_widget_show(wizard->recv_imap_subdir);
395                 gtk_widget_show(wizard->recv_username);
396                 gtk_widget_show(wizard->recv_password);
397                 gtk_widget_show(wizard->recv_username_label);
398                 gtk_widget_show(wizard->recv_password_label);
399                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
400                 g_free(text);
401         } else if (protocol == A_LOCAL) {
402                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
403                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
404                 g_free(mbox);
405                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Local mailbox:"));
406                 gtk_widget_hide(wizard->recv_imap_label);
407                 gtk_widget_hide(wizard->recv_imap_subdir);
408                 gtk_widget_hide(wizard->recv_username);
409                 gtk_widget_hide(wizard->recv_password);
410                 gtk_widget_hide(wizard->recv_username_label);
411                 gtk_widget_hide(wizard->recv_password_label);
412         }
413 }
414
415 static GtkWidget* recv_page (WizardWindow * wizard)
416 {
417         GtkWidget *table = gtk_table_new(5,2, FALSE);
418         GtkWidget *menu = gtk_menu_new();
419         GtkWidget *menuitem;
420         gchar *text;
421         gint i = 0;
422         
423         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
424         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
425
426         wizard->recv_type = gtk_option_menu_new();
427         
428         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
429         g_signal_connect(G_OBJECT(menuitem), "activate",
430                          G_CALLBACK(wizard_protocol_changed),
431                          wizard);
432         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
433         g_signal_connect(G_OBJECT(menuitem), "activate",
434                          G_CALLBACK(wizard_protocol_changed),
435                          wizard);
436         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
437         g_signal_connect(G_OBJECT(menuitem), "activate",
438                          G_CALLBACK(wizard_protocol_changed),
439                          wizard);
440
441         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
442         GTK_TABLE_ADD_ROW_AT(table, _("Server type:"), 
443                              wizard->recv_type, i); i++;
444
445         wizard->recv_server = gtk_entry_new();
446         text = get_default_server(wizard, "pop");
447         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
448         g_free(text);
449         
450         wizard->recv_label = gtk_label_new(_("Server address:"));
451         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
452                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
453         if (GTK_IS_MISC(wizard->recv_label))                                                  
454                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
455         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
456                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
457         i++;
458         
459         wizard->recv_username = gtk_entry_new();
460         wizard->recv_username_label = gtk_label_new(_("Username:"));
461         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
462                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
463         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
464                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
465         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
466                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
467         i++;
468         
469         wizard->recv_password = gtk_entry_new();
470         wizard->recv_password_label = gtk_label_new(_("Password:"));
471         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
472                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
473         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
474                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
475         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
476                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
477         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
478         i++;
479         
480         wizard->recv_imap_subdir = gtk_entry_new();
481         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
482         
483         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
484                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
485         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
486                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
487         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
488                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
489
490         i++;
491         
492         return table;
493 }
494
495 #ifdef USE_OPENSSL
496 static GtkWidget* ssl_page (WizardWindow * wizard)
497 {
498         GtkWidget *table = gtk_table_new(2,2, FALSE);
499         gint i = 0;
500         
501         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
502         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
503
504         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
505                                         _("Use SSL to connect to SMTP server"));
506         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
507                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
508         
509         wizard->recv_use_ssl = gtk_check_button_new_with_label(
510                                         _("Use SSL to connect to receiving server"));
511         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
512                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
513         
514         return table;
515 }
516 #endif
517
518 static void initialize_fonts(WizardWindow *wizard)
519 {
520         GtkWidget *widget = wizard->email;
521         gint size = pango_font_description_get_size(
522                         widget->style->font_desc)
523                       /PANGO_SCALE;
524         gchar *tmp, *new;
525         
526         tmp = g_strdup(prefs_common.textfont);
527         if (strrchr(tmp, ' ')) {
528                 *(strrchr(tmp, ' ')) = '\0';
529                 new = g_strdup_printf("%s %d", tmp, size);
530                 g_free(prefs_common.textfont);
531                 prefs_common.textfont = new;
532         }
533         g_free(tmp);
534         
535         tmp = g_strdup(prefs_common.smallfont);
536         if (strrchr(tmp, ' ')) {
537                 *(strrchr(tmp, ' ')) = '\0';
538                 new = g_strdup_printf("%s %d", tmp, size);
539                 g_free(prefs_common.smallfont);
540                 prefs_common.smallfont = new;
541         }
542         g_free(tmp);
543         
544         tmp = g_strdup(prefs_common.normalfont);
545         if (strrchr(tmp, ' ')) {
546                 *(strrchr(tmp, ' ')) = '\0';
547                 new = g_strdup_printf("%s %d", tmp, size);
548                 g_free(prefs_common.normalfont);
549                 prefs_common.normalfont = new;
550         }
551         g_free(tmp);
552         
553         
554         printf("size %d\n", size);
555         
556 }
557
558 static void
559 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
560 {
561         WizardWindow * wizard = (WizardWindow *)data;
562         int current_page, num_pages;
563         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
564         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
565         gint protocol = GPOINTER_TO_INT
566                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
567         gboolean skip_mailbox_page = FALSE;
568         
569         if (protocol == A_IMAP4) {
570                 skip_mailbox_page = TRUE;
571         }
572         
573         num_pages = g_slist_length(wizard->pages);
574
575         current_page = gtk_notebook_get_current_page (
576                                 GTK_NOTEBOOK(wizard->notebook));
577         if (response == CANCEL)
578         {
579                 wizard->result = FALSE;
580                 wizard->finished = TRUE;
581                 gtk_widget_destroy (GTK_WIDGET(dialog));
582         }
583         else if (response == FINISHED)
584         {
585                 if (!wizard_write_config(wizard)) {
586                         current_page = gtk_notebook_get_current_page (
587                                         GTK_NOTEBOOK(wizard->notebook));
588                         goto set_sens;
589                 }
590                 wizard->result = TRUE;
591                 wizard->finished = TRUE;
592                 initialize_fonts(wizard);
593                 gtk_widget_destroy (GTK_WIDGET(dialog));
594         }
595         else
596         {
597                 if (response == GO_BACK)
598                 {
599                         if (current_page > 0) {
600                                 current_page--;
601                                 if (current_page == 4 && skip_mailbox_page) {
602                                         /* mailbox */
603                                         current_page--;
604                                 }
605                                 gtk_notebook_set_current_page (
606                                         GTK_NOTEBOOK(wizard->notebook), 
607                                         current_page);
608                         }
609                 }
610                 else if (response == GO_FORWARD)
611                 {
612                         if (current_page < (num_pages-1)) {
613                                 current_page++;
614                                 if (current_page == 4 && skip_mailbox_page) {
615                                         /* mailbox */
616                                         current_page++;
617                                 }
618                                 gtk_notebook_set_current_page (
619                                         GTK_NOTEBOOK(wizard->notebook), 
620                                         current_page);
621                         }
622                 }
623 set_sens:
624                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
625                                 current_page > 0);
626                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
627                                 current_page < (num_pages - 1));
628                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
629                                 current_page == (num_pages - 1));
630                 gtk_dialog_set_response_sensitive (dialog, CANCEL, 
631                                 current_page != (num_pages - 1));
632         }
633 }
634
635 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
636                                  gpointer data)
637 {
638         WizardWindow *wizard = (WizardWindow *)data;
639         wizard->result = FALSE;
640         wizard->finished = TRUE;
641         
642         return FALSE;
643 }
644
645 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
646         WizardWindow *wizard = g_new0(WizardWindow, 1);
647         GtkWidget *page;
648         GtkWidget *widget;
649         gchar     *text;
650         GSList    *cur;
651         gboolean   result;
652         
653         wizard->mainwin = mainwin;
654         wizard->create_mailbox = create_mailbox;
655         
656         gtk_widget_hide(mainwin->window);
657         
658         wizard->window = gtk_dialog_new_with_buttons (_("New User"),
659                         NULL, 0, 
660                         GTK_STOCK_GO_BACK, GO_BACK,
661                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
662                         GTK_STOCK_SAVE, FINISHED,
663                         GTK_STOCK_QUIT, CANCEL,
664                         NULL);
665
666         g_signal_connect(wizard->window, "response", 
667                           G_CALLBACK(wizard_response_cb), wizard);
668         gtk_widget_realize(wizard->window);
669         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
670                         GO_FORWARD);
671         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
672                         GO_BACK, FALSE);
673         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
674                         GO_FORWARD, TRUE);
675         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
676                         FINISHED, FALSE);
677         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
678                         CANCEL, TRUE);
679         
680         wizard->notebook = gtk_notebook_new();
681         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
682         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
683         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
684                             wizard->notebook, TRUE, TRUE, 0);
685         
686         wizard->pages = NULL;
687         
688 /*welcome page: 0 */
689         page = create_page(wizard, _("Welcome to Sylpheed-Claws."));
690         
691         wizard->pages = g_slist_append(wizard->pages, page);
692         widget = stock_pixmap_widget(wizard->window, 
693                                 STOCK_PIXMAP_SYLPHEED_LOGO);
694
695         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
696         
697         text = g_strdup(_("Welcome to Sylpheed-Claws.\n\n"
698                           "It looks like it's the first time you use \n"
699                           "Sylpheed-Claws. So, we'll now define some basic\n"
700                           "information about yourself and your most common\n"
701                           "mail parameters; so that you can begin to use\n"
702                           "Sylpheed-Claws in less than five minutes."));
703         widget = gtk_label_new(text);
704         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
705         g_free(text);
706
707 /* user page: 1 */
708         widget = create_page (wizard, _("About You"));
709         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
710         wizard->pages = g_slist_append(wizard->pages, widget);
711
712 /*smtp page: 2 */
713         widget = create_page (wizard, _("Sending mail"));
714         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
715         wizard->pages = g_slist_append(wizard->pages, widget);
716
717 /* recv+auth page: 3 */
718         widget = create_page (wizard, _("Receiving mail"));
719         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
720         wizard->pages = g_slist_append(wizard->pages, widget);
721
722 /* mailbox page: 4 */
723         if (create_mailbox) {
724                 widget = create_page (wizard, _("Saving mail on disk"));
725                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
726                 wizard->pages = g_slist_append(wizard->pages, widget);
727         }
728 /* ssl page: 5 */
729 #ifdef USE_OPENSSL
730         widget = create_page (wizard, _("Security"));
731         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
732         wizard->pages = g_slist_append(wizard->pages, widget);
733 #endif
734
735 /* done page: 6 */
736         page = create_page(wizard, _("Configuration finished."));
737         
738         wizard->pages = g_slist_append(wizard->pages, page);
739         widget = stock_pixmap_widget(wizard->window, 
740                                 STOCK_PIXMAP_SYLPHEED_LOGO);
741
742         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
743         
744         text = g_strdup(_("Sylpheed-Claws is now ready to run.\n\n"
745                           "Click Save to start."));
746         widget = gtk_label_new(text);
747         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
748         g_free(text);
749
750
751         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
752                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
753                                           GTK_WIDGET(cur->data), NULL);
754         }
755         
756         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
757                          G_CALLBACK(wizard_close_cb), wizard);
758         gtk_widget_show_all (wizard->window);
759
760         gtk_widget_hide(wizard->recv_imap_label);
761         gtk_widget_hide(wizard->recv_imap_subdir);
762
763         while (!wizard->finished)
764                 gtk_main_iteration();
765
766         result = wizard->result;
767         
768         GTK_EVENTS_FLUSH();
769
770         gtk_widget_show(mainwin->window);
771         g_free(wizard);
772
773         return result;
774 }