2005-12-11 [paul] 1.9.100cvs82
[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 #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         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
640         g_signal_connect(G_OBJECT(menuitem), "activate",
641                          G_CALLBACK(wizard_protocol_changed),
642                          wizard);
643         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
644         g_signal_connect(G_OBJECT(menuitem), "activate",
645                          G_CALLBACK(wizard_protocol_changed),
646                          wizard);
647
648         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
649         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
650                              wizard->recv_type, i); i++;
651
652         wizard->recv_server = gtk_entry_new();
653         text = get_default_server(wizard, "pop");
654         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
655         g_free(text);
656         
657         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
658         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
659         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
660                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
661         if (GTK_IS_MISC(wizard->recv_label))                                                  
662                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
663         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
664                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
665         i++;
666         
667         wizard->recv_username = gtk_entry_new();
668         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
669         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
670         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
671                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
672         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
673                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
674         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
675                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
676         i++;
677         
678         text = get_default_account(wizard);
679         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
680         g_free(text);
681
682         wizard->recv_password = gtk_entry_new();
683         wizard->recv_password_label = gtk_label_new(_("Password:"));
684         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
685                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
686         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
687                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
688         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
689                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
690         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
691         i++;
692         
693         wizard->recv_imap_subdir = gtk_entry_new();
694         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
695         
696         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
697                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
698         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
699                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
700         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
701                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
702
703         i++;
704         
705         return table;
706 }
707
708 #ifdef USE_OPENSSL
709 static GtkWidget* ssl_page (WizardWindow * wizard)
710 {
711         GtkWidget *table = gtk_table_new(2,2, FALSE);
712         gint i = 0;
713         
714         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
715         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
716
717         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
718                                         _("Use SSL to connect to SMTP server"));
719         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
720                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
721         
722         wizard->recv_use_ssl = gtk_check_button_new_with_label(
723                                         _("Use SSL to connect to receiving server"));
724         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
725                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
726         
727         return table;
728 }
729 #endif
730
731 static void
732 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
733 {
734         WizardWindow * wizard = (WizardWindow *)data;
735         int current_page, num_pages;
736         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
737         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
738         gint protocol = GPOINTER_TO_INT
739                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
740         gboolean skip_mailbox_page = FALSE;
741         
742         if (protocol == A_IMAP4) {
743                 skip_mailbox_page = TRUE;
744         }
745         
746         num_pages = g_slist_length(wizard->pages);
747
748         current_page = gtk_notebook_get_current_page (
749                                 GTK_NOTEBOOK(wizard->notebook));
750         if (response == CANCEL)
751         {
752                 wizard->result = FALSE;
753                 wizard->finished = TRUE;
754                 gtk_widget_destroy (GTK_WIDGET(dialog));
755         }
756         else if (response == FINISHED)
757         {
758                 if (!wizard_write_config(wizard)) {
759                         current_page = gtk_notebook_get_current_page (
760                                         GTK_NOTEBOOK(wizard->notebook));
761                         goto set_sens;
762                 }
763                 wizard->result = TRUE;
764                 wizard->finished = TRUE;
765                 gtk_widget_destroy (GTK_WIDGET(dialog));
766         }
767         else
768         {
769                 if (response == GO_BACK)
770                 {
771                         if (current_page > 0) {
772                                 current_page--;
773                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
774                                         /* mailbox */
775                                         current_page--;
776                                 }
777                                 gtk_notebook_set_current_page (
778                                         GTK_NOTEBOOK(wizard->notebook), 
779                                         current_page);
780                         }
781                 }
782                 else if (response == GO_FORWARD)
783                 {
784                         if (current_page < (num_pages-1)) {
785                                 current_page++;
786                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
787                                         /* mailbox */
788                                         current_page++;
789                                 }
790                                 gtk_notebook_set_current_page (
791                                         GTK_NOTEBOOK(wizard->notebook), 
792                                         current_page);
793                         }
794                 }
795 set_sens:
796                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
797                                 current_page > 0);
798                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
799                                 current_page < (num_pages - 1));
800                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
801                                 current_page == (num_pages - 1));
802         }
803 }
804
805 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
806                                  gpointer data)
807 {
808         WizardWindow *wizard = (WizardWindow *)data;
809         wizard->result = FALSE;
810         wizard->finished = TRUE;
811         
812         return FALSE;
813 }
814
815 #define PACK_WARNING(text) {                                            \
816         label = gtk_label_new(text);                                    \
817         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
818         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
819 }
820
821 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
822         WizardWindow *wizard = g_new0(WizardWindow, 1);
823         GtkWidget *page;
824         GtkWidget *widget;
825         GtkWidget *label;
826         gchar     *text;
827         GSList    *cur;
828         gboolean   result;
829         gint i = 0;
830         wizard->mainwin = mainwin;
831         wizard->create_mailbox = create_mailbox;
832         
833         gtk_widget_hide(mainwin->window);
834         
835         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
836                         NULL, 0, 
837                         GTK_STOCK_GO_BACK, GO_BACK,
838                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
839                         GTK_STOCK_SAVE, FINISHED,
840                         GTK_STOCK_CANCEL, CANCEL,
841                         NULL);
842
843         g_signal_connect(wizard->window, "response", 
844                           G_CALLBACK(wizard_response_cb), wizard);
845         gtk_widget_realize(wizard->window);
846         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
847                         GO_FORWARD);
848         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
849                         GO_BACK, FALSE);
850         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
851                         GO_FORWARD, TRUE);
852         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
853                         FINISHED, FALSE);
854         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
855                         CANCEL, TRUE);
856         
857         wizard->notebook = gtk_notebook_new();
858         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
859         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
860         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
861                             wizard->notebook, TRUE, TRUE, 0);
862         
863         wizard->pages = NULL;
864         
865 /*welcome page: 0 */
866         WELCOME_PAGE = i;
867         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
868         
869         wizard->pages = g_slist_append(wizard->pages, page);
870         widget = stock_pixmap_widget(wizard->window, 
871                                 STOCK_PIXMAP_SYLPHEED_LOGO);
872
873         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
874         
875         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
876                           "We will begin by defining some basic "
877                           "information about you and your most common "
878                           "mail options so that you can start to use "
879                           "Sylpheed-Claws in less than five minutes."));
880         widget = gtk_label_new(text);
881         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
882         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
883         g_free(text);
884
885 /* user page: 1 */
886         i++;
887         USER_PAGE = i;
888         widget = create_page (wizard, _("About You"));
889         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
890         PACK_WARNING(_("Bold fields must be completed"));
891         
892         wizard->pages = g_slist_append(wizard->pages, widget);
893
894 /*smtp page: 2 */
895         i++;
896         SMTP_PAGE = i;
897         widget = create_page (wizard, _("Sending mail"));
898         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
899         PACK_WARNING(_("Bold fields must be completed"));
900         
901         wizard->pages = g_slist_append(wizard->pages, widget);
902
903 /* recv+auth page: 3 */
904         i++;
905         RECV_PAGE = i;
906         widget = create_page (wizard, _("Receiving mail"));
907         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
908         PACK_WARNING(_("Bold fields must be completed"));
909         
910         wizard->pages = g_slist_append(wizard->pages, widget);
911
912 /* mailbox page: 4 */
913         if (create_mailbox) {
914                 i++;
915                 MAILBOX_PAGE = i;
916                 widget = create_page (wizard, _("Saving mail on disk"));
917                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
918                 PACK_WARNING(_("Bold fields must be completed"));
919         
920                 wizard->pages = g_slist_append(wizard->pages, widget);
921         }
922 /* ssl page: 5 */
923 #ifdef USE_OPENSSL
924         i++;
925         SSL_PAGE = i;
926         widget = create_page (wizard, _("Security"));
927         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
928         PACK_WARNING(_("Bold fields must be completed"));
929         
930         wizard->pages = g_slist_append(wizard->pages, widget);
931 #endif
932
933 /* done page: 6 */
934         i++;
935         DONE_PAGE = i;
936         page = create_page(wizard, _("Configuration finished"));
937         
938         wizard->pages = g_slist_append(wizard->pages, page);
939         widget = stock_pixmap_widget(wizard->window, 
940                                 STOCK_PIXMAP_SYLPHEED_LOGO);
941
942         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
943         
944         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
945                           "Click Save to start."));
946         widget = gtk_label_new(text);
947         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
948         g_free(text);
949
950
951         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
952                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
953                                           GTK_WIDGET(cur->data), NULL);
954         }
955         
956         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
957                          G_CALLBACK(wizard_close_cb), wizard);
958         gtk_widget_show_all (wizard->window);
959
960         gtk_widget_hide(wizard->recv_imap_label);
961         gtk_widget_hide(wizard->recv_imap_subdir);
962
963         while (!wizard->finished)
964                 gtk_main_iteration();
965
966         result = wizard->result;
967         
968         GTK_EVENTS_FLUSH();
969
970         gtk_widget_show(mainwin->window);
971         g_free(wizard);
972
973         return result;
974 }