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