7f32814817f01e1c6726cfa087dfec4e9a24163f
[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 void initialize_fonts(WizardWindow *wizard)
109 {
110         GtkWidget *widget = wizard->email;
111         gint size = pango_font_description_get_size(
112                         widget->style->font_desc)
113                       /PANGO_SCALE;
114         gchar *tmp, *new;
115         
116         tmp = g_strdup(prefs_common.textfont);
117         if (strrchr(tmp, ' ')) {
118                 *(strrchr(tmp, ' ')) = '\0';
119                 new = g_strdup_printf("%s %d", tmp, size);
120                 g_free(prefs_common.textfont);
121                 prefs_common.textfont = new;
122         }
123         g_free(tmp);
124         
125         tmp = g_strdup(prefs_common.smallfont);
126         if (strrchr(tmp, ' ')) {
127                 *(strrchr(tmp, ' ')) = '\0';
128                 new = g_strdup_printf("%s %d", tmp, size);
129                 g_free(prefs_common.smallfont);
130                 prefs_common.smallfont = new;
131         }
132         g_free(tmp);
133         
134         tmp = g_strdup(prefs_common.normalfont);
135         if (strrchr(tmp, ' ')) {
136                 *(strrchr(tmp, ' ')) = '\0';
137                 new = g_strdup_printf("%s %d", tmp, size);
138                 g_free(prefs_common.normalfont);
139                 prefs_common.normalfont = new;
140         }
141         g_free(tmp);
142 }
143
144 static void write_welcome_email(WizardWindow *wizard)
145 {
146         gchar buf_date[64];
147         gchar *body=NULL;
148         const gchar *mailbox = gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name));
149         Folder *folder = folder_find_from_path(mailbox);
150         FolderItem *inbox = folder ? folder->inbox:NULL;
151         gchar *file = get_tmp_file();
152         
153         get_rfc822_date(buf_date, sizeof(buf_date));
154
155         body = g_strdup_printf(
156                 "From: Sylpheed-Claws Team <sylpheed-claws-users@lists.sf.net>\n"
157                 "To: %s <%s>\n"
158                 "Date: %s\n"
159                 "Subject: Welcome to Sylpheed-Claws.\n"
160                 "\n"
161                 "Welcome!\n", 
162                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)),
163                 gtk_entry_get_text(GTK_ENTRY(wizard->email)),
164                 buf_date);
165         
166         if (inbox && inbox->total_msgs == 0
167          && str_write_to_file(body, file) >= 0) {
168                 MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
169                 folder_item_add_msg(inbox, file, &flags, FALSE);
170         }
171         g_free(body);
172         g_unlink(file); 
173 }
174 static gboolean wizard_write_config(WizardWindow *wizard)
175 {
176         gboolean mailbox_ok = FALSE;
177         PrefsAccount *prefs_account = prefs_account_new();
178         GList *account_list = NULL;
179         GtkWidget *menu, *menuitem;
180         
181         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
182         menuitem = gtk_menu_get_active(GTK_MENU(menu));
183         prefs_account->protocol = GPOINTER_TO_INT
184                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
185         
186         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
187                 mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
188                                 gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)));
189         } else
190                 mailbox_ok = TRUE;
191
192         if (!mailbox_ok) {
193                 gtk_notebook_set_current_page (
194                         GTK_NOTEBOOK(wizard->notebook), 
195                         4);
196                 return FALSE;
197         }
198         
199         if (prefs_account->protocol != A_LOCAL)
200                 prefs_account->account_name = g_strdup_printf("%s@%s",
201                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
202                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
203         else
204                 prefs_account->account_name = g_strdup_printf("%s",
205                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
206
207         prefs_account->name = g_strdup(
208                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
209         prefs_account->address = g_strdup(
210                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
211         prefs_account->organization = g_strdup(
212                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
213         prefs_account->smtp_server = g_strdup(
214                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
215
216         if (prefs_account->protocol != A_LOCAL)
217                 prefs_account->recv_server = g_strdup(
218                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
219         else
220                 prefs_account->local_mbox = g_strdup(
221                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
222
223         prefs_account->userid = g_strdup(
224                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
225         prefs_account->passwd = g_strdup(
226                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
227
228 #ifdef USE_OPENSSL                      
229         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
230                 prefs_account->ssl_smtp = SSL_TUNNEL;
231         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
232                 if (prefs_account->protocol == A_IMAP4)
233                         prefs_account->ssl_imap = SSL_TUNNEL;
234                 else
235                         prefs_account->ssl_pop = SSL_TUNNEL;
236         }
237 #endif
238         if (prefs_account->protocol == A_IMAP4) {
239                 gchar *directory = gtk_editable_get_chars(
240                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
241                 if (directory && strlen(directory)) {
242                         prefs_account->imap_dir = g_strdup(directory);
243                 }
244                 g_free(directory);
245         }
246
247         account_list = g_list_append(account_list, prefs_account);
248         prefs_account_write_config_all(account_list);
249         prefs_account_free(prefs_account);
250         account_read_config_all();
251
252         initialize_fonts(wizard);
253         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
254                 write_welcome_email(wizard);
255         
256         return TRUE;
257 }
258
259 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
260 {
261         GtkWidget *w;
262         GtkWidget *vbox;
263         GtkWidget *hbox;
264         GtkWidget *image;
265         char *title_string;
266
267         vbox = gtk_vbox_new (FALSE, 6);
268         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
269
270         /* create the titlebar */
271         hbox = gtk_hbox_new (FALSE, 12);
272         image = stock_pixmap_widget(wizard->window, 
273                                 STOCK_PIXMAP_SYLPHEED_ICON);
274         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
275         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
276         w = gtk_label_new (title_string);
277         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
278         g_free (title_string);
279         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
280
281         /* pack the titlebar */
282         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
283
284         /* pack the separator */
285         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
286
287         /* pack space */
288         w = gtk_alignment_new (0, 0, 0, 0);
289         gtk_widget_set_size_request (w, 0, 6);
290         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
291
292         return vbox;
293 }
294
295 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
296         GtkWidget *label = gtk_label_new(text);                               \
297         gtk_table_attach(GTK_TABLE(table), label,                             \
298                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
299         if (GTK_IS_MISC(label))                                               \
300                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
301         gtk_table_attach(GTK_TABLE(table), entry,                             \
302                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
303 }
304
305 static gchar *get_default_email_addr(void)
306 {
307         gchar *domain_name = g_strdup(get_domain_name());
308         gchar *result;
309         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
310         && strlen(strchr(domain_name, '.')) > 6) {
311                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
312                 g_free(domain_name);
313                 domain_name = tmp;
314         }
315         result = g_strdup_printf("%s@%s",
316                                 g_get_user_name(),
317                                 domain_name);
318         g_free(domain_name);
319         return result;
320 }
321
322 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
323 {
324         gchar *domain_name = g_strdup(get_domain_name());
325         gchar *result;
326         
327         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
328         && strlen(strchr(domain_name, '.')) > 6) {
329                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
330                 g_free(domain_name);
331                 domain_name = tmp;
332         } else if (strchr(domain_name, '.') == NULL) {
333                 /* only hostname found, use email suffix */
334                 gchar *mail;
335                 mail = gtk_editable_get_chars(GTK_EDITABLE(wizard->email), 0, -1);
336
337                 if (strlen (mail) && strstr(mail, "@")) {
338                         g_free(domain_name);
339                         domain_name = g_strdup(strstr(mail, "@")+1);
340                 }
341                 g_free(mail);
342         }
343         result = g_strdup_printf("%s.%s",
344                                 type, domain_name);
345         g_free(domain_name);
346         return result;
347 }
348
349 static void wizard_email_changed(GtkWidget *widget, gpointer data)
350 {
351         WizardWindow *wizard = (WizardWindow *)data;
352         RecvProtocol protocol;
353         gchar *text;
354         protocol = GPOINTER_TO_INT
355                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
356         
357         text = get_default_server(wizard, "smtp");
358         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
359         g_free(text);
360
361         if (protocol == A_POP3) {
362                 text = get_default_server(wizard, "pop");
363                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
364                 g_free(text);
365         } else if (protocol == A_IMAP4) {
366                 text = get_default_server(wizard, "imap");
367                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
368                 g_free(text);
369         } else if (protocol == A_LOCAL) {
370                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
371                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
372                 g_free(mbox);
373         }
374 }
375
376 static GtkWidget* user_page (WizardWindow * wizard)
377 {
378         GtkWidget *table = gtk_table_new(3,2, FALSE);
379         gchar *text;
380         gint i = 0;
381         
382         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
383         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
384
385         wizard->full_name = gtk_entry_new();
386         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), g_get_real_name());
387         GTK_TABLE_ADD_ROW_AT(table, _("Your name:"), 
388                              wizard->full_name, i); i++;
389         
390         wizard->email = gtk_entry_new();
391         text = get_default_email_addr();
392         gtk_entry_set_text(GTK_ENTRY(wizard->email), text);
393         g_free(text);
394         GTK_TABLE_ADD_ROW_AT(table, _("Your email address:"), 
395                              wizard->email, i); i++;
396         
397         wizard->organization = gtk_entry_new();
398         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"), 
399                              wizard->organization, i); i++;
400         
401         g_signal_connect(G_OBJECT(wizard->email), "changed",
402                          G_CALLBACK(wizard_email_changed),
403                          wizard);
404         return table;
405 }
406
407 static GtkWidget* mailbox_page (WizardWindow * wizard)
408 {
409         GtkWidget *table = gtk_table_new(1,2, FALSE);
410         gint i = 0;
411         
412         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
413         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
414
415         wizard->mailbox_name = gtk_entry_new();
416         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), "Mail");
417         GTK_TABLE_ADD_ROW_AT(table, _("Mailbox name:"), 
418                              wizard->mailbox_name, i); i++;
419         
420         return table;
421 }
422
423 static GtkWidget* smtp_page (WizardWindow * wizard)
424 {
425         GtkWidget *table = gtk_table_new(1,2, FALSE);
426         gchar *text;
427         gint i = 0;
428         
429         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
430         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
431
432         wizard->smtp_server = gtk_entry_new();
433         text = get_default_server(wizard, "smtp");
434         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
435         g_free(text);
436         GTK_TABLE_ADD_ROW_AT(table, _("SMTP server address:"), 
437                              wizard->smtp_server, i); i++;
438         return table;
439 }
440
441 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
442 {
443         WizardWindow *wizard = (WizardWindow *)data;
444         RecvProtocol protocol;
445         gchar *text;
446         protocol = GPOINTER_TO_INT
447                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
448         
449         if (protocol == A_POP3) {
450                 text = get_default_server(wizard, "pop");
451                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
452                 gtk_widget_hide(wizard->recv_imap_label);
453                 gtk_widget_hide(wizard->recv_imap_subdir);
454                 gtk_widget_show(wizard->recv_username);
455                 gtk_widget_show(wizard->recv_password);
456                 gtk_widget_show(wizard->recv_username_label);
457                 gtk_widget_show(wizard->recv_password_label);
458                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
459                 g_free(text);
460         } else if (protocol == A_IMAP4) {
461                 text = get_default_server(wizard, "imap");
462                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
463                 gtk_widget_show(wizard->recv_imap_label);
464                 gtk_widget_show(wizard->recv_imap_subdir);
465                 gtk_widget_show(wizard->recv_username);
466                 gtk_widget_show(wizard->recv_password);
467                 gtk_widget_show(wizard->recv_username_label);
468                 gtk_widget_show(wizard->recv_password_label);
469                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
470                 g_free(text);
471         } else if (protocol == A_LOCAL) {
472                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
473                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
474                 g_free(mbox);
475                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Local mailbox:"));
476                 gtk_widget_hide(wizard->recv_imap_label);
477                 gtk_widget_hide(wizard->recv_imap_subdir);
478                 gtk_widget_hide(wizard->recv_username);
479                 gtk_widget_hide(wizard->recv_password);
480                 gtk_widget_hide(wizard->recv_username_label);
481                 gtk_widget_hide(wizard->recv_password_label);
482         }
483 }
484
485 static GtkWidget* recv_page (WizardWindow * wizard)
486 {
487         GtkWidget *table = gtk_table_new(5,2, FALSE);
488         GtkWidget *menu = gtk_menu_new();
489         GtkWidget *menuitem;
490         gchar *text;
491         gint i = 0;
492         
493         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
494         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
495
496         wizard->recv_type = gtk_option_menu_new();
497         
498         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
499         g_signal_connect(G_OBJECT(menuitem), "activate",
500                          G_CALLBACK(wizard_protocol_changed),
501                          wizard);
502         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
503         g_signal_connect(G_OBJECT(menuitem), "activate",
504                          G_CALLBACK(wizard_protocol_changed),
505                          wizard);
506         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
507         g_signal_connect(G_OBJECT(menuitem), "activate",
508                          G_CALLBACK(wizard_protocol_changed),
509                          wizard);
510
511         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
512         GTK_TABLE_ADD_ROW_AT(table, _("Server type:"), 
513                              wizard->recv_type, i); i++;
514
515         wizard->recv_server = gtk_entry_new();
516         text = get_default_server(wizard, "pop");
517         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
518         g_free(text);
519         
520         wizard->recv_label = gtk_label_new(_("Server address:"));
521         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
522                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
523         if (GTK_IS_MISC(wizard->recv_label))                                                  
524                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
525         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
526                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
527         i++;
528         
529         wizard->recv_username = gtk_entry_new();
530         wizard->recv_username_label = gtk_label_new(_("Username:"));
531         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
532                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
533         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
534                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
535         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
536                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
537         i++;
538         
539         wizard->recv_password = gtk_entry_new();
540         wizard->recv_password_label = gtk_label_new(_("Password:"));
541         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
542                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
543         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
544                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
545         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
546                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
547         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
548         i++;
549         
550         wizard->recv_imap_subdir = gtk_entry_new();
551         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
552         
553         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
554                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
555         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
556                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
557         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
558                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
559
560         i++;
561         
562         return table;
563 }
564
565 #ifdef USE_OPENSSL
566 static GtkWidget* ssl_page (WizardWindow * wizard)
567 {
568         GtkWidget *table = gtk_table_new(2,2, FALSE);
569         gint i = 0;
570         
571         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
572         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
573
574         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
575                                         _("Use SSL to connect to SMTP server"));
576         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
577                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
578         
579         wizard->recv_use_ssl = gtk_check_button_new_with_label(
580                                         _("Use SSL to connect to receiving server"));
581         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
582                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
583         
584         return table;
585 }
586 #endif
587
588 static void
589 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
590 {
591         WizardWindow * wizard = (WizardWindow *)data;
592         int current_page, num_pages;
593         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
594         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
595         gint protocol = GPOINTER_TO_INT
596                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
597         gboolean skip_mailbox_page = FALSE;
598         
599         if (protocol == A_IMAP4) {
600                 skip_mailbox_page = TRUE;
601         }
602         
603         num_pages = g_slist_length(wizard->pages);
604
605         current_page = gtk_notebook_get_current_page (
606                                 GTK_NOTEBOOK(wizard->notebook));
607         if (response == CANCEL)
608         {
609                 wizard->result = FALSE;
610                 wizard->finished = TRUE;
611                 gtk_widget_destroy (GTK_WIDGET(dialog));
612         }
613         else if (response == FINISHED)
614         {
615                 if (!wizard_write_config(wizard)) {
616                         current_page = gtk_notebook_get_current_page (
617                                         GTK_NOTEBOOK(wizard->notebook));
618                         goto set_sens;
619                 }
620                 wizard->result = TRUE;
621                 wizard->finished = TRUE;
622                 gtk_widget_destroy (GTK_WIDGET(dialog));
623         }
624         else
625         {
626                 if (response == GO_BACK)
627                 {
628                         if (current_page > 0) {
629                                 current_page--;
630                                 if (current_page == 4 && skip_mailbox_page) {
631                                         /* mailbox */
632                                         current_page--;
633                                 }
634                                 gtk_notebook_set_current_page (
635                                         GTK_NOTEBOOK(wizard->notebook), 
636                                         current_page);
637                         }
638                 }
639                 else if (response == GO_FORWARD)
640                 {
641                         if (current_page < (num_pages-1)) {
642                                 current_page++;
643                                 if (current_page == 4 && skip_mailbox_page) {
644                                         /* mailbox */
645                                         current_page++;
646                                 }
647                                 gtk_notebook_set_current_page (
648                                         GTK_NOTEBOOK(wizard->notebook), 
649                                         current_page);
650                         }
651                 }
652 set_sens:
653                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
654                                 current_page > 0);
655                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
656                                 current_page < (num_pages - 1));
657                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
658                                 current_page == (num_pages - 1));
659                 gtk_dialog_set_response_sensitive (dialog, CANCEL, 
660                                 current_page != (num_pages - 1));
661         }
662 }
663
664 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
665                                  gpointer data)
666 {
667         WizardWindow *wizard = (WizardWindow *)data;
668         wizard->result = FALSE;
669         wizard->finished = TRUE;
670         
671         return FALSE;
672 }
673
674 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
675         WizardWindow *wizard = g_new0(WizardWindow, 1);
676         GtkWidget *page;
677         GtkWidget *widget;
678         gchar     *text;
679         GSList    *cur;
680         gboolean   result;
681         
682         wizard->mainwin = mainwin;
683         wizard->create_mailbox = create_mailbox;
684         
685         gtk_widget_hide(mainwin->window);
686         
687         wizard->window = gtk_dialog_new_with_buttons (_("New User"),
688                         NULL, 0, 
689                         GTK_STOCK_GO_BACK, GO_BACK,
690                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
691                         GTK_STOCK_SAVE, FINISHED,
692                         GTK_STOCK_QUIT, CANCEL,
693                         NULL);
694
695         g_signal_connect(wizard->window, "response", 
696                           G_CALLBACK(wizard_response_cb), wizard);
697         gtk_widget_realize(wizard->window);
698         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
699                         GO_FORWARD);
700         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
701                         GO_BACK, FALSE);
702         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
703                         GO_FORWARD, TRUE);
704         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
705                         FINISHED, FALSE);
706         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
707                         CANCEL, TRUE);
708         
709         wizard->notebook = gtk_notebook_new();
710         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
711         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
712         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
713                             wizard->notebook, TRUE, TRUE, 0);
714         
715         wizard->pages = NULL;
716         
717 /*welcome page: 0 */
718         page = create_page(wizard, _("Welcome to Sylpheed-Claws."));
719         
720         wizard->pages = g_slist_append(wizard->pages, page);
721         widget = stock_pixmap_widget(wizard->window, 
722                                 STOCK_PIXMAP_SYLPHEED_LOGO);
723
724         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
725         
726         text = g_strdup(_("Welcome to Sylpheed-Claws.\n\n"
727                           "It looks like it's the first time you use \n"
728                           "Sylpheed-Claws. So, we'll now define some basic\n"
729                           "information about yourself and your most common\n"
730                           "mail parameters; so that you can begin to use\n"
731                           "Sylpheed-Claws in less than five minutes."));
732         widget = gtk_label_new(text);
733         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
734         g_free(text);
735
736 /* user page: 1 */
737         widget = create_page (wizard, _("About You"));
738         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
739         wizard->pages = g_slist_append(wizard->pages, widget);
740
741 /*smtp page: 2 */
742         widget = create_page (wizard, _("Sending mail"));
743         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
744         wizard->pages = g_slist_append(wizard->pages, widget);
745
746 /* recv+auth page: 3 */
747         widget = create_page (wizard, _("Receiving mail"));
748         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
749         wizard->pages = g_slist_append(wizard->pages, widget);
750
751 /* mailbox page: 4 */
752         if (create_mailbox) {
753                 widget = create_page (wizard, _("Saving mail on disk"));
754                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
755                 wizard->pages = g_slist_append(wizard->pages, widget);
756         }
757 /* ssl page: 5 */
758 #ifdef USE_OPENSSL
759         widget = create_page (wizard, _("Security"));
760         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
761         wizard->pages = g_slist_append(wizard->pages, widget);
762 #endif
763
764 /* done page: 6 */
765         page = create_page(wizard, _("Configuration finished."));
766         
767         wizard->pages = g_slist_append(wizard->pages, page);
768         widget = stock_pixmap_widget(wizard->window, 
769                                 STOCK_PIXMAP_SYLPHEED_LOGO);
770
771         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
772         
773         text = g_strdup(_("Sylpheed-Claws is now ready to run.\n\n"
774                           "Click Save to start."));
775         widget = gtk_label_new(text);
776         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
777         g_free(text);
778
779
780         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
781                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
782                                           GTK_WIDGET(cur->data), NULL);
783         }
784         
785         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
786                          G_CALLBACK(wizard_close_cb), wizard);
787         gtk_widget_show_all (wizard->window);
788
789         gtk_widget_hide(wizard->recv_imap_label);
790         gtk_widget_hide(wizard->recv_imap_subdir);
791
792         while (!wizard->finished)
793                 gtk_main_iteration();
794
795         result = wizard->result;
796         
797         GTK_EVENTS_FLUSH();
798
799         gtk_widget_show(mainwin->window);
800         g_free(wizard);
801
802         return result;
803 }