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