2006-03-07 [paul] 2.0.0cvs118
[claws.git] / src / wizard.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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 #include "alertpanel.h"
59 #ifdef USE_OPENSSL                      
60 #include "ssl.h"
61 #endif
62 #include "prefs_common.h"
63
64 typedef enum
65 {
66         GO_BACK,
67         GO_FORWARD,
68         CANCEL,
69         FINISHED
70 } PageNavigation;
71
72 int WELCOME_PAGE = -1;
73 int USER_PAGE = -1;
74 int SMTP_PAGE = -1;
75 int RECV_PAGE = -1;
76 int MAILBOX_PAGE = -1;
77 int SSL_PAGE = -1;
78 int DONE_PAGE = -1;
79
80 typedef struct
81 {
82         GtkWidget *window;
83         GSList    *pages;
84         GtkWidget *notebook;
85
86         MainWindow *mainwin;
87         
88         GtkWidget *email;
89         GtkWidget *full_name;
90         GtkWidget *organization;
91
92         GtkWidget *mailbox_name;
93         
94         GtkWidget *smtp_server;
95
96         GtkWidget *recv_type;
97         GtkWidget *recv_label;
98         GtkWidget *recv_server;
99         GtkWidget *recv_username;
100         GtkWidget *recv_password;
101         GtkWidget *recv_username_label;
102         GtkWidget *recv_password_label;
103         GtkWidget *recv_imap_label;
104         GtkWidget *recv_imap_subdir;
105
106 #ifdef USE_OPENSSL
107         GtkWidget *smtp_use_ssl;
108         GtkWidget *recv_use_ssl;
109 #endif
110         
111         gboolean create_mailbox;
112         gboolean finished;
113         gboolean result;
114
115 } WizardWindow;
116
117 static void initialize_fonts(WizardWindow *wizard)
118 {
119         GtkWidget *widget = wizard->email;
120         gint size = pango_font_description_get_size(
121                         widget->style->font_desc)
122                       /PANGO_SCALE;
123         gchar *tmp, *new;
124         
125         tmp = g_strdup(prefs_common.textfont);
126         if (strrchr(tmp, ' ')) {
127                 *(strrchr(tmp, ' ')) = '\0';
128                 new = g_strdup_printf("%s %d", tmp, size);
129                 g_free(prefs_common.textfont);
130                 prefs_common.textfont = new;
131         }
132         g_free(tmp);
133         
134         tmp = g_strdup(prefs_common.smallfont);
135         if (strrchr(tmp, ' ')) {
136                 *(strrchr(tmp, ' ')) = '\0';
137                 new = g_strdup_printf("%s %d", tmp, size);
138                 g_free(prefs_common.smallfont);
139                 prefs_common.smallfont = new;
140         }
141         g_free(tmp);
142         
143         tmp = g_strdup(prefs_common.normalfont);
144         if (strrchr(tmp, ' ')) {
145                 *(strrchr(tmp, ' ')) = '\0';
146                 new = g_strdup_printf("%s %d", tmp, size);
147                 g_free(prefs_common.normalfont);
148                 prefs_common.normalfont = new;
149         }
150         g_free(tmp);
151 }
152
153 #define XFACE "+}Axz@~a,-Yx?0Ysa|q}CLRH=89Y]\"')DSX^<6p\"d)'81yx5%G#u^o*7JG&[aPU0h1Ux.vb2yIjH83{5`/bVo|~nn/i83vE^E)qk-4W)_E.4Y=D*qvf/,Ci_=P<iY<M6"
154
155 static void write_welcome_email(WizardWindow *wizard)
156 {
157         gchar buf_date[64];
158         gchar *head=NULL;
159         gchar *body=NULL;
160         gchar *msg=NULL;
161         gchar *subj=NULL;
162         const gchar *mailbox = gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name));
163         Folder *folder = folder_find_from_path(mailbox);
164         FolderItem *inbox = folder ? folder->inbox:NULL;
165         gchar *file = get_tmp_file();
166         
167         get_rfc822_date(buf_date, sizeof(buf_date));
168
169         subj = g_strdup_printf(_("Welcome to Sylpheed-Claws"));
170
171         head = g_strdup_printf(
172                 "From: %s <sylpheed-claws-users@lists.sf.net>\n"
173                 "To: %s <%s>\n"
174                 "Date: %s\n"
175                 "Subject: %s\n"
176                 "X-Face: %s\n"
177                 "Content-Type: text/plain; charset=UTF-8\n",
178                 _("Sylpheed-Claws Team"),
179                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)),
180                 gtk_entry_get_text(GTK_ENTRY(wizard->email)),
181                 buf_date, subj, XFACE);
182         body = g_strdup_printf(
183                 _("\n"
184                 "Welcome to Sylpheed-Claws\n"
185                 "-------------------------\n"
186                 "\n"
187                 "Now that you have set up your account you can fetch your\n"
188                 "mail by clicking the 'Get Mail' button at the left of the\n"
189                 "toolbar.\n"
190                 "\n"
191                 "You can change your Account Preferences by using the menu\n"
192                 "entry '/Configuration/Preferences for current account'\n"
193                 "and change the general Preferences by using\n"
194                 "'/Configuration/Preferences'.\n"
195                 "\n"
196                 "You can find further information in the Sylpheed-Claws manual,\n"
197                 "which can be accessed by using the menu entry '/Help/Manual'\n"
198                 "or online at the URL given below.\n"
199                 "\n"
200                 "Useful URLs\n"
201                 "-----------\n"
202                 "Homepage:      <%s>\n"
203                 "Manual:        <%s>\n"
204                 "FAQ:          <%s>\n"
205                 "Themes:        <%s>\n"
206                 "Mailing Lists: <%s>\n"
207                 "\n"
208                 "LICENSE\n"
209                 "-------\n"
210                 "Sylpheed-Claws is free software, released under the terms\n"
211                 "of the GNU General Public License, version 2 or later, as\n"
212                 "published by the Free Software Foundation, 51 Franklin Street,\n"
213                 "Fifth Floor, Boston, MA 02110-1301, USA. The license can be\n"
214                 "found at <%s>.\n"
215                 "\n"
216                 "DONATIONS\n"
217                 "---------\n"
218                 "If you wish to donate to the Sylpheed-Claws project you can do\n"
219                 "so at <%s>.\n\n"),
220                 HOMEPAGE_URI, MANUAL_URI, FAQ_URI, THEMES_URI, MAILING_LIST_URI,
221                 GPL_URI, DONATE_URI);
222         
223         msg = g_strconcat(head, body, NULL);
224
225         if (inbox && inbox->total_msgs == 0
226          && str_write_to_file(msg, file) >= 0) {
227                 MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
228                 folder_item_add_msg(inbox, file, &flags, FALSE);
229         }
230         g_free(subj);
231         g_free(head);
232         g_free(body);
233         g_free(msg);
234         g_unlink(file);
235 }
236 #undef XFACE
237
238 static gboolean wizard_write_config(WizardWindow *wizard)
239 {
240         static gboolean mailbox_ok = FALSE;
241         PrefsAccount *prefs_account = prefs_account_new();
242         GList *account_list = NULL;
243         GtkWidget *menu, *menuitem;
244         
245         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
246         menuitem = gtk_menu_get_active(GTK_MENU(menu));
247         prefs_account->protocol = GPOINTER_TO_INT
248                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
249         
250         
251         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4 && 
252             !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)))) {
253                 alertpanel_error(_("Please enter the mailbox name."));
254                 g_free(prefs_account);
255                 gtk_notebook_set_current_page (
256                         GTK_NOTEBOOK(wizard->notebook), 
257                         MAILBOX_PAGE);
258                 return FALSE;
259         }
260
261         if (!mailbox_ok) {
262                 if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
263                         mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
264                                         gtk_entry_get_text(
265                                                 GTK_ENTRY(wizard->mailbox_name)));
266                 } else
267                         mailbox_ok = TRUE;
268         }
269
270         if (!mailbox_ok) {
271                 /* alertpanel done by setup_write_mailbox_path */
272                 g_free(prefs_account);
273                 gtk_notebook_set_current_page (
274                         GTK_NOTEBOOK(wizard->notebook), 
275                         MAILBOX_PAGE);
276                 return FALSE;
277         }
278         
279         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->full_name)))
280         ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->email)))) {
281                 alertpanel_error(_("Please enter your name and email address."));
282                 g_free(prefs_account);
283                 gtk_notebook_set_current_page (
284                         GTK_NOTEBOOK(wizard->notebook), 
285                         USER_PAGE);
286                 return FALSE;
287         }
288         
289         if (prefs_account->protocol != A_LOCAL) {
290                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)))
291                 ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
292                         alertpanel_error(_("Please enter your receiving server "
293                                            "and username."));
294                         g_free(prefs_account);
295                         gtk_notebook_set_current_page (
296                                 GTK_NOTEBOOK(wizard->notebook), 
297                                 RECV_PAGE);
298                         return FALSE;
299                 }
300         } else {
301                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
302                         alertpanel_error(_("Please enter your username."));
303                         g_free(prefs_account);
304                         gtk_notebook_set_current_page (
305                                 GTK_NOTEBOOK(wizard->notebook), 
306                                 RECV_PAGE);
307                         return FALSE;
308                 }
309         }
310
311         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)))) {
312                 alertpanel_error(_("Please enter your SMTP server."));
313                 g_free(prefs_account);
314                 gtk_notebook_set_current_page (
315                         GTK_NOTEBOOK(wizard->notebook), 
316                         SMTP_PAGE);
317                 return FALSE;
318
319         }
320
321         if (prefs_account->protocol != A_LOCAL)
322                 prefs_account->account_name = g_strdup_printf("%s@%s",
323                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
324                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
325         else
326                 prefs_account->account_name = g_strdup_printf("%s",
327                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
328
329         prefs_account->name = g_strdup(
330                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
331         prefs_account->address = g_strdup(
332                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
333         prefs_account->organization = g_strdup(
334                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
335         prefs_account->smtp_server = g_strdup(
336                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
337
338         if (prefs_account->protocol != A_LOCAL)
339                 prefs_account->recv_server = g_strdup(
340                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
341         else
342                 prefs_account->local_mbox = g_strdup(
343                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
344
345         prefs_account->userid = g_strdup(
346                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
347         prefs_account->passwd = g_strdup(
348                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
349
350 #ifdef USE_OPENSSL                      
351         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
352                 prefs_account->ssl_smtp = SSL_TUNNEL;
353         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
354                 if (prefs_account->protocol == A_IMAP4)
355                         prefs_account->ssl_imap = SSL_TUNNEL;
356                 else
357                         prefs_account->ssl_pop = SSL_TUNNEL;
358         }
359 #endif
360         if (prefs_account->protocol == A_IMAP4) {
361                 gchar *directory = gtk_editable_get_chars(
362                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
363                 if (directory && strlen(directory)) {
364                         prefs_account->imap_dir = g_strdup(directory);
365                 }
366                 g_free(directory);
367         }
368
369         account_list = g_list_append(account_list, prefs_account);
370         prefs_account_write_config_all(account_list);
371         prefs_account_free(prefs_account);
372         account_read_config_all();
373
374         initialize_fonts(wizard);
375         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
376                 write_welcome_email(wizard);
377         
378         return TRUE;
379 }
380
381 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
382 {
383         GtkWidget *w;
384         GtkWidget *vbox;
385         GtkWidget *hbox;
386         GtkWidget *image;
387         char *title_string;
388
389         vbox = gtk_vbox_new (FALSE, 6);
390         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
391
392         /* create the titlebar */
393         hbox = gtk_hbox_new (FALSE, 12);
394         image = stock_pixmap_widget(wizard->window, 
395                                 STOCK_PIXMAP_SYLPHEED_ICON);
396         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
397         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
398         w = gtk_label_new (title_string);
399         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
400         g_free (title_string);
401         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
402
403         /* pack the titlebar */
404         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
405
406         /* pack the separator */
407         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
408
409         /* pack space */
410         w = gtk_alignment_new (0, 0, 0, 0);
411         gtk_widget_set_size_request (w, 0, 6);
412         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
413
414         return vbox;
415 }
416
417 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
418         GtkWidget *label = gtk_label_new(text);                               \
419         gtk_table_attach(GTK_TABLE(table), label,                             \
420                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
421         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);                     \
422         if (GTK_IS_MISC(label))                                               \
423                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
424         gtk_table_attach(GTK_TABLE(table), entry,                             \
425                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
426 }
427
428 static gchar *get_default_email_addr(void)
429 {
430         gchar *domain_name = g_strdup(get_domain_name());
431         gchar *result;
432         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
433         && strlen(strchr(domain_name, '.')) > 6) {
434                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
435                 g_free(domain_name);
436                 domain_name = tmp;
437         }
438         result = g_strdup_printf("%s@%s",
439                                 g_get_user_name(),
440                                 domain_name);
441         g_free(domain_name);
442         return result;
443 }
444
445 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
446 {
447         gchar *domain_name = NULL;
448         gchar *tmp = gtk_editable_get_chars(
449                         GTK_EDITABLE(wizard->email), 0, -1);
450         gchar *result;
451         
452         if (strstr(tmp, "@")) {
453                 domain_name = g_strdup(strstr(tmp,"@")+1);
454         } else {
455                 domain_name = g_strdup(get_domain_name());
456         }
457         
458         g_free(tmp);
459
460         result = g_strdup_printf("%s.%s",
461                                 type, domain_name);
462         g_free(domain_name);
463         return result;
464 }
465
466 static gchar *get_default_account(WizardWindow * wizard)
467 {
468         gchar *result = gtk_editable_get_chars(
469                         GTK_EDITABLE(wizard->email), 0, -1);
470         
471         if (strstr(result, "@")) {
472                 *(strstr(result,"@")) = '\0';
473         } 
474
475         return result;
476 }
477
478 static void wizard_email_changed(GtkWidget *widget, gpointer data)
479 {
480         WizardWindow *wizard = (WizardWindow *)data;
481         RecvProtocol protocol;
482         gchar *text;
483         protocol = GPOINTER_TO_INT
484                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
485         
486         text = get_default_server(wizard, "smtp");
487         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
488         g_free(text);
489
490         text = get_default_account(wizard);
491         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
492         g_free(text);
493
494         if (protocol == A_POP3) {
495                 text = get_default_server(wizard, "pop");
496                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
497                 g_free(text);
498         } else if (protocol == A_IMAP4) {
499                 text = get_default_server(wizard, "imap");
500                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
501                 g_free(text);
502         } else if (protocol == A_LOCAL) {
503                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
504                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
505                 g_free(mbox);
506         }
507         
508 }
509
510 static GtkWidget* user_page (WizardWindow * wizard)
511 {
512         GtkWidget *table = gtk_table_new(3,2, FALSE);
513         gchar *text;
514         gint i = 0;
515         
516         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
517         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
518
519         wizard->full_name = gtk_entry_new();
520         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), g_get_real_name());
521         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your name:</span>"), 
522                              wizard->full_name, i); i++;
523         
524         wizard->email = gtk_entry_new();
525         text = get_default_email_addr();
526         gtk_entry_set_text(GTK_ENTRY(wizard->email), text);
527         g_free(text);
528         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your email address:</span>"), 
529                              wizard->email, i); i++;
530         
531         wizard->organization = gtk_entry_new();
532         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"),
533                              wizard->organization, i); i++;
534         
535         g_signal_connect(G_OBJECT(wizard->email), "changed",
536                          G_CALLBACK(wizard_email_changed),
537                          wizard);
538         return table;
539 }
540
541 static GtkWidget* mailbox_page (WizardWindow * wizard)
542 {
543         GtkWidget *table = gtk_table_new(1,2, FALSE);
544         gint i = 0;
545         
546         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
547         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
548
549         wizard->mailbox_name = gtk_entry_new();
550         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), "Mail");
551         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Mailbox name:</span>"), 
552                              wizard->mailbox_name, i); i++;
553         
554         return table;
555 }
556
557 static GtkWidget* smtp_page (WizardWindow * wizard)
558 {
559         GtkWidget *table = gtk_table_new(1,2, FALSE);
560         gchar *text;
561         gint i = 0;
562         
563         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
564         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
565
566         wizard->smtp_server = gtk_entry_new();
567         text = get_default_server(wizard, "smtp");
568         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
569         g_free(text);
570         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">SMTP server address:</span>"), 
571                              wizard->smtp_server, i); i++;
572         return table;
573 }
574
575 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
576 {
577         WizardWindow *wizard = (WizardWindow *)data;
578         RecvProtocol protocol;
579         gchar *text;
580         protocol = GPOINTER_TO_INT
581                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
582         
583         if (protocol == A_POP3) {
584                 text = get_default_server(wizard, "pop");
585                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
586                 gtk_widget_hide(wizard->recv_imap_label);
587                 gtk_widget_hide(wizard->recv_imap_subdir);
588                 gtk_widget_show(wizard->recv_username);
589                 gtk_widget_show(wizard->recv_password);
590                 gtk_widget_show(wizard->recv_username_label);
591                 gtk_widget_show(wizard->recv_password_label);
592                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
593                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
594                 g_free(text);
595         } else if (protocol == A_IMAP4) {
596                 text = get_default_server(wizard, "imap");
597                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
598                 gtk_widget_show(wizard->recv_imap_label);
599                 gtk_widget_show(wizard->recv_imap_subdir);
600                 gtk_widget_show(wizard->recv_username);
601                 gtk_widget_show(wizard->recv_password);
602                 gtk_widget_show(wizard->recv_username_label);
603                 gtk_widget_show(wizard->recv_password_label);
604                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
605                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
606                 g_free(text);
607         } else if (protocol == A_LOCAL) {
608                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
609                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
610                 g_free(mbox);
611                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
612                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
613                 gtk_widget_hide(wizard->recv_imap_label);
614                 gtk_widget_hide(wizard->recv_imap_subdir);
615                 gtk_widget_hide(wizard->recv_username);
616                 gtk_widget_hide(wizard->recv_password);
617                 gtk_widget_hide(wizard->recv_username_label);
618                 gtk_widget_hide(wizard->recv_password_label);
619         }
620 }
621
622 static GtkWidget* recv_page (WizardWindow * wizard)
623 {
624         GtkWidget *table = gtk_table_new(5,2, FALSE);
625         GtkWidget *menu = gtk_menu_new();
626         GtkWidget *menuitem;
627         gchar *text;
628         gint i = 0;
629         
630         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
631         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
632
633         wizard->recv_type = gtk_option_menu_new();
634         
635         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
636         g_signal_connect(G_OBJECT(menuitem), "activate",
637                          G_CALLBACK(wizard_protocol_changed),
638                          wizard);
639 #ifdef HAVE_LIBETPAN
640         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
641         g_signal_connect(G_OBJECT(menuitem), "activate",
642                          G_CALLBACK(wizard_protocol_changed),
643                          wizard);
644 #endif
645         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
646         g_signal_connect(G_OBJECT(menuitem), "activate",
647                          G_CALLBACK(wizard_protocol_changed),
648                          wizard);
649
650         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
651         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
652                              wizard->recv_type, i); i++;
653
654         wizard->recv_server = gtk_entry_new();
655         text = get_default_server(wizard, "pop");
656         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
657         g_free(text);
658         
659         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
660         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
661         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
662                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
663         if (GTK_IS_MISC(wizard->recv_label))                                                  
664                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
665         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
666                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
667         i++;
668         
669         wizard->recv_username = gtk_entry_new();
670         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
671         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
672         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
673                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
674         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
675                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
676         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
677                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
678         i++;
679         
680         text = get_default_account(wizard);
681         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
682         g_free(text);
683
684         wizard->recv_password = gtk_entry_new();
685         wizard->recv_password_label = gtk_label_new(_("Password:"));
686         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
687                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
688         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
689                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
690         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
691                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
692         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
693         i++;
694         
695         wizard->recv_imap_subdir = gtk_entry_new();
696         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
697         
698         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
699                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
700         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
701                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
702         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
703                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
704
705         i++;
706         
707         return table;
708 }
709
710 #ifdef USE_OPENSSL
711 static GtkWidget* ssl_page (WizardWindow * wizard)
712 {
713         GtkWidget *table = gtk_table_new(2,2, FALSE);
714         gint i = 0;
715         
716         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
717         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
718
719         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
720                                         _("Use SSL to connect to SMTP server"));
721         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
722                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
723         
724         wizard->recv_use_ssl = gtk_check_button_new_with_label(
725                                         _("Use SSL to connect to receiving server"));
726         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
727                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
728         
729         return table;
730 }
731 #endif
732
733 static void
734 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
735 {
736         WizardWindow * wizard = (WizardWindow *)data;
737         int current_page, num_pages;
738         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
739         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
740         gint protocol = GPOINTER_TO_INT
741                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
742         gboolean skip_mailbox_page = FALSE;
743         
744         if (protocol == A_IMAP4) {
745                 skip_mailbox_page = TRUE;
746         }
747         
748         num_pages = g_slist_length(wizard->pages);
749
750         current_page = gtk_notebook_get_current_page (
751                                 GTK_NOTEBOOK(wizard->notebook));
752         if (response == CANCEL)
753         {
754                 wizard->result = FALSE;
755                 wizard->finished = TRUE;
756                 gtk_widget_destroy (GTK_WIDGET(dialog));
757         }
758         else if (response == FINISHED)
759         {
760                 if (!wizard_write_config(wizard)) {
761                         current_page = gtk_notebook_get_current_page (
762                                         GTK_NOTEBOOK(wizard->notebook));
763                         goto set_sens;
764                 }
765                 wizard->result = TRUE;
766                 wizard->finished = TRUE;
767                 gtk_widget_destroy (GTK_WIDGET(dialog));
768         }
769         else
770         {
771                 if (response == GO_BACK)
772                 {
773                         if (current_page > 0) {
774                                 current_page--;
775                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
776                                         /* mailbox */
777                                         current_page--;
778                                 }
779                                 gtk_notebook_set_current_page (
780                                         GTK_NOTEBOOK(wizard->notebook), 
781                                         current_page);
782                         }
783                 }
784                 else if (response == GO_FORWARD)
785                 {
786                         if (current_page < (num_pages-1)) {
787                                 current_page++;
788                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
789                                         /* mailbox */
790                                         current_page++;
791                                 }
792                                 gtk_notebook_set_current_page (
793                                         GTK_NOTEBOOK(wizard->notebook), 
794                                         current_page);
795                         }
796                 }
797 set_sens:
798                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
799                                 current_page > 0);
800                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
801                                 current_page < (num_pages - 1));
802                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
803                                 current_page == (num_pages - 1));
804         }
805 }
806
807 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
808                                  gpointer data)
809 {
810         WizardWindow *wizard = (WizardWindow *)data;
811         wizard->result = FALSE;
812         wizard->finished = TRUE;
813         
814         return FALSE;
815 }
816
817 #define PACK_WARNING(text) {                                            \
818         label = gtk_label_new(text);                                    \
819         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
820         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
821 }
822
823 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
824         WizardWindow *wizard = g_new0(WizardWindow, 1);
825         GtkWidget *page;
826         GtkWidget *widget;
827         GtkWidget *label;
828         gchar     *text;
829         GSList    *cur;
830         gboolean   result;
831         gint i = 0;
832         wizard->mainwin = mainwin;
833         wizard->create_mailbox = create_mailbox;
834         
835         gtk_widget_hide(mainwin->window);
836         
837         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
838                         NULL, 0, 
839                         GTK_STOCK_GO_BACK, GO_BACK,
840                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
841                         GTK_STOCK_SAVE, FINISHED,
842                         GTK_STOCK_CANCEL, CANCEL,
843                         NULL);
844
845         g_signal_connect(wizard->window, "response", 
846                           G_CALLBACK(wizard_response_cb), wizard);
847         gtk_widget_realize(wizard->window);
848         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
849                         GO_FORWARD);
850         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
851                         GO_BACK, FALSE);
852         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
853                         GO_FORWARD, TRUE);
854         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
855                         FINISHED, FALSE);
856         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
857                         CANCEL, TRUE);
858         
859         wizard->notebook = gtk_notebook_new();
860         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
861         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
862         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
863                             wizard->notebook, TRUE, TRUE, 0);
864         
865         wizard->pages = NULL;
866         
867 /*welcome page: 0 */
868         WELCOME_PAGE = i;
869         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
870         
871         wizard->pages = g_slist_append(wizard->pages, page);
872         widget = stock_pixmap_widget(wizard->window, 
873                                 STOCK_PIXMAP_SYLPHEED_LOGO);
874
875         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
876         
877         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
878                           "We will begin by defining some basic "
879                           "information about you and your most common "
880                           "mail options so that you can start to use "
881                           "Sylpheed-Claws in less than five minutes."));
882         widget = gtk_label_new(text);
883         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
884         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
885         g_free(text);
886
887 /* user page: 1 */
888         i++;
889         USER_PAGE = i;
890         widget = create_page (wizard, _("About You"));
891         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
892         PACK_WARNING(_("Bold fields must be completed"));
893         
894         wizard->pages = g_slist_append(wizard->pages, widget);
895
896 /*smtp page: 2 */
897         i++;
898         SMTP_PAGE = i;
899         widget = create_page (wizard, _("Sending mail"));
900         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
901         PACK_WARNING(_("Bold fields must be completed"));
902         
903         wizard->pages = g_slist_append(wizard->pages, widget);
904
905 /* recv+auth page: 3 */
906         i++;
907         RECV_PAGE = i;
908         widget = create_page (wizard, _("Receiving mail"));
909         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
910         PACK_WARNING(_("Bold fields must be completed"));
911         
912         wizard->pages = g_slist_append(wizard->pages, widget);
913
914 /* mailbox page: 4 */
915         if (create_mailbox) {
916                 i++;
917                 MAILBOX_PAGE = i;
918                 widget = create_page (wizard, _("Saving mail on disk"));
919                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
920                 PACK_WARNING(_("Bold fields must be completed"));
921         
922                 wizard->pages = g_slist_append(wizard->pages, widget);
923         }
924 /* ssl page: 5 */
925 #ifdef USE_OPENSSL
926         i++;
927         SSL_PAGE = i;
928         widget = create_page (wizard, _("Security"));
929         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
930         PACK_WARNING(_("Bold fields must be completed"));
931         
932         wizard->pages = g_slist_append(wizard->pages, widget);
933 #endif
934
935 /* done page: 6 */
936         i++;
937         DONE_PAGE = i;
938         page = create_page(wizard, _("Configuration finished"));
939         
940         wizard->pages = g_slist_append(wizard->pages, page);
941         widget = stock_pixmap_widget(wizard->window, 
942                                 STOCK_PIXMAP_SYLPHEED_LOGO);
943
944         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
945         
946         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
947                           "Click Save to start."));
948         widget = gtk_label_new(text);
949         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
950         g_free(text);
951
952
953         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
954                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
955                                           GTK_WIDGET(cur->data), NULL);
956         }
957         
958         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
959                          G_CALLBACK(wizard_close_cb), wizard);
960         gtk_widget_show_all (wizard->window);
961
962         gtk_widget_hide(wizard->recv_imap_label);
963         gtk_widget_hide(wizard->recv_imap_subdir);
964
965         while (!wizard->finished)
966                 gtk_main_iteration();
967
968         result = wizard->result;
969         
970         GTK_EVENTS_FLUSH();
971
972         gtk_widget_show(mainwin->window);
973         g_free(wizard);
974
975         return result;
976 }