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