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