2006-08-01 [wwp] 2.4.0cvs10
[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         GtkWidget *smtp_auth;
96         GtkWidget *smtp_username;
97         GtkWidget *smtp_password;
98         GtkWidget *smtp_username_label;
99         GtkWidget *smtp_password_label;
100
101         GtkWidget *recv_type;
102         GtkWidget *recv_label;
103         GtkWidget *recv_server;
104         GtkWidget *recv_username;
105         GtkWidget *recv_password;
106         GtkWidget *recv_username_label;
107         GtkWidget *recv_password_label;
108         GtkWidget *recv_imap_label;
109         GtkWidget *recv_imap_subdir;
110
111 #ifdef USE_OPENSSL
112         GtkWidget *smtp_use_ssl;
113         GtkWidget *recv_use_ssl;
114 #endif
115         
116         gboolean create_mailbox;
117         gboolean finished;
118         gboolean result;
119
120 } WizardWindow;
121
122 typedef struct _AccountTemplate {
123         gchar *name;
124         gchar *domain;
125         gchar *email;
126         gchar *organization;
127         gchar *smtpserver;
128         gboolean smtpauth;
129         gchar *smtpuser;
130         gchar *smtppass;
131         RecvProtocol recvtype;
132         gchar *recvserver;
133         gchar *recvuser;
134         gchar *recvpass;
135         gchar *imapdir;
136         gchar *mboxfile;
137         gchar *mailbox;
138         gboolean smtpssl;
139         gboolean recvssl;
140 } AccountTemplate;
141
142 static AccountTemplate tmpl;
143
144 static PrefParam template_params[] = {
145         {"name", "$USERNAME",
146          &tmpl.name, P_STRING, NULL, NULL, NULL},
147         {"domain", "$DEFAULTDOMAIN",
148          &tmpl.domain, P_STRING, NULL, NULL, NULL},
149         {"email", "$NAME_MAIL@$DOMAIN",
150          &tmpl.email, P_STRING, NULL, NULL, NULL},
151         {"organization", "",
152          &tmpl.organization, P_STRING, NULL, NULL, NULL},
153         {"smtpserver", "smtp.$DOMAIN",
154          &tmpl.smtpserver, P_STRING, NULL, NULL, NULL},
155         {"smtpauth", "FALSE",
156          &tmpl.smtpauth, P_BOOL, NULL, NULL, NULL},
157         {"smtpuser", "",
158          &tmpl.smtpuser, P_STRING, NULL, NULL, NULL},
159         {"smtppass", "",
160          &tmpl.smtppass, P_STRING, NULL, NULL, NULL},
161         {"recvtype", A_POP3,
162          &tmpl.recvtype, P_INT, NULL, NULL, NULL},
163         {"recvserver", "pop.$DOMAIN",
164          &tmpl.recvserver, P_STRING, NULL, NULL, NULL},
165         {"recvuser", "$LOGIN",
166          &tmpl.recvuser, P_STRING, NULL, NULL, NULL},
167         {"recvpass", "",
168          &tmpl.recvpass, P_STRING, NULL, NULL, NULL},
169         {"imapdir", "",
170          &tmpl.imapdir, P_STRING, NULL, NULL, NULL},
171         {"mboxfile", "/var/mail/$LOGIN",
172          &tmpl.mboxfile, P_STRING, NULL, NULL, NULL},
173         {"mailbox", "Mail",
174          &tmpl.mailbox, P_STRING, NULL, NULL, NULL},
175         {"smtpssl", "FALSE",
176          &tmpl.smtpssl, P_BOOL, NULL, NULL, NULL},
177         {"recvssl", "FALSE",
178          &tmpl.recvssl, P_BOOL, NULL, NULL, NULL},
179         {NULL, NULL, NULL, P_INT, NULL, NULL, NULL}
180 };
181
182
183 static gchar *accountrc_tmpl =
184         "[AccountTemplate]\n"
185         "#you can use $DEFAULTDOMAIN here\n"
186         "#domain must be defined before the variables that use it\n"
187         "#by default, domain is extracted from the hostname\n"
188         "#domain=\n"
189         "\n"
190         "#you can use $USERNAME for name (this is the default)\n"
191         "#name=\n"
192         "\n"
193         "#you can use $LOGIN, $NAME_MAIL and $DOMAIN here \n"
194         "#$NAME_MAIL is the name without uppercase and with dots instead\n"
195         "#of spaces\n"
196         "#the default is $NAME_MAIL@$DOMAIN\n"
197         "#email=\n"
198         "\n"
199         "#you can use $DOMAIN here\n"
200         "#the default organization is empty\n"
201         "#organization=\n"
202         "\n"
203         "#you can use $DOMAIN here \n"
204         "#the default is stmp.$DOMAIN\n"
205         "#smtpserver=\n"
206         "\n"
207         "#Whether to use smtp authentication\n"
208         "#the default is 0 (no)\n"
209         "#smtpauth=\n"
210         "\n"
211         "#SMTP username\n"
212         "#you can use $LOGIN, $NAME_MAIL, $DOMAIN or $EMAIL here\n"
213         "#the default is empty (same as reception username)\n"
214         "#smtpuser=\n"
215         "\n"
216         "#SMTP password\n"
217         "#the default is empty (same as reception password)\n"
218         "#smtppass=\n"
219         "\n"
220         "#recvtype can be:\n"
221         "#0 for pop3\n"
222         "#3  for imap\n"
223         "#5  for a local mbox file\n"
224         "#recvtype=\n"
225         "\n"
226         "#you can use $DOMAIN here \n"
227         "#the default is {pop,imap}.$DOMAIN\n"
228         "#recvserver=\n"
229         "\n"
230         "#you can use $LOGIN, $NAME_MAIL, $DOMAIN or $EMAIL here\n"
231         "#default is $LOGIN\n"
232         "#recvuser=\n"
233         "\n"
234         "#default is empty\n"
235         "#recvpass=\n"
236         "\n"
237         "#imap dir if imap (relative to the home on the server\n"
238         "#default is empty\n"
239         "#imapdir=\n"
240         "\n"
241         "#mbox file if local\n"
242         "#you can use $LOGIN here\n"
243         "#default is /var/mail/$LOGIN\n"
244         "#mboxfile=\n"
245         "\n"
246         "#mailbox name if pop3 or local\n"
247         "#relative path from the user's home\n"
248         "#default is \"Mail\"\n"
249         "#mailbox=\n"
250         "\n"
251         "#whether to use ssl on STMP connections\n"
252         "#default is 0\n"
253         "#smtpssl=\n"
254         "\n"
255         "#whether to use ssl on pop or imap connections\n"
256         "#default is 0\n"
257         "#recvssl=\n";
258
259 static gchar *wizard_get_default_domain_name(void)
260 {
261         static gchar *domain_name = NULL;
262         
263         if (domain_name == NULL) {
264                 domain_name = g_strdup(get_domain_name());
265                 if (strchr(domain_name, '.') != strrchr(domain_name, '.')
266                 && strlen(strchr(domain_name, '.')) > 6) {
267                         gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
268                         g_free(domain_name);
269                         domain_name = tmp;
270                 }
271         }
272         return domain_name;
273 }
274
275 static gchar *get_name_for_mail(void)
276 {
277         gchar *name = g_strdup(tmpl.name);
278         if (name == NULL)
279                 return NULL;
280         g_strdown(name);
281         while(strstr(name, " "))
282                 *strstr(name, " ")='.';
283         
284         return name;
285 }
286
287 #define PARSE_DEFAULT(str) {    \
288         gchar *tmp = NULL, *new = NULL; \
289         if (str != NULL) {      \
290                 tmp = g_strdup(str);    \
291                 if (strstr(str, "$USERNAME")) { \
292                         tmp = g_strdup(str);    \
293                         *strstr(tmp, "$USERNAME") = '\0';       \
294                         new = g_strconcat(tmp, g_get_real_name(),       \
295                                 strstr(str, "$USERNAME")+strlen("$USERNAME"),   \
296                                 NULL);  \
297                         g_free(tmp);    \
298                         g_free(str);    \
299                         str = new;      \
300                         new = NULL;     \
301                 }       \
302                 if (strstr(str, "$LOGIN")) {    \
303                         tmp = g_strdup(str);    \
304                         *strstr(tmp, "$LOGIN") = '\0';  \
305                         new = g_strconcat(tmp, g_get_user_name(),       \
306                                 strstr(str, "$LOGIN")+strlen("$LOGIN"),         \
307                                 NULL);  \
308                         g_free(tmp);    \
309                         g_free(str);    \
310                         str = new;      \
311                         new = NULL;     \
312                 }       \
313                 if (strstr(str, "$EMAIL")) {    \
314                         tmp = g_strdup(str);    \
315                         *strstr(tmp, "$EMAIL") = '\0';  \
316                         new = g_strconcat(tmp, tmpl.email,      \
317                                 strstr(str, "$EMAIL")+strlen("$EMAIL"),         \
318                                 NULL);  \
319                         g_free(tmp);    \
320                         g_free(str);    \
321                         str = new;      \
322                         new = NULL;     \
323                 }       \
324                 if (strstr(str, "$NAME_MAIL")) {        \
325                         tmp = g_strdup(str);    \
326                         *strstr(tmp, "$NAME_MAIL") = '\0';      \
327                         new = g_strconcat(tmp, get_name_for_mail(),     \
328                                 strstr(str, "$NAME_MAIL")+strlen("$NAME_MAIL"),         \
329                                 NULL);  \
330                         g_free(tmp);    \
331                         g_free(str);    \
332                         str = new;      \
333                         new = NULL;     \
334                 }       \
335                 if (strstr(str, "$DEFAULTDOMAIN")) {    \
336                         tmp = g_strdup(str);    \
337                         *strstr(tmp, "$DEFAULTDOMAIN") = '\0';  \
338                         new = g_strconcat(tmp, wizard_get_default_domain_name(),        \
339                                 strstr(str, "$DEFAULTDOMAIN")+strlen("$DEFAULTDOMAIN"),         \
340                                 NULL);  \
341                         g_free(tmp);    \
342                         g_free(str);    \
343                         str = new;      \
344                         new = NULL;     \
345                 }       \
346                 if (strstr(str, "$DOMAIN")) {   \
347                         tmp = g_strdup(str);    \
348                         *strstr(tmp, "$DOMAIN") = '\0'; \
349                         new = g_strconcat(tmp, tmpl.domain,     \
350                                 strstr(str, "$DOMAIN")+strlen("$DOMAIN"),       \
351                                 NULL);  \
352                         g_free(tmp);    \
353                         g_free(str);    \
354                         str = new;      \
355                         new = NULL;     \
356                 }       \
357         }       \
358 }
359 static void wizard_read_defaults(void)
360 {
361         gchar *rcpath;
362
363         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "accountrc.tmpl", NULL);
364         if (!is_file_exist(rcpath)) {
365                 str_write_to_file(accountrc_tmpl, rcpath);
366         }
367
368         prefs_read_config(template_params, "AccountTemplate", rcpath, NULL);
369
370         PARSE_DEFAULT(tmpl.domain);
371         PARSE_DEFAULT(tmpl.name);
372         PARSE_DEFAULT(tmpl.email);
373         PARSE_DEFAULT(tmpl.organization);
374         PARSE_DEFAULT(tmpl.smtpserver);
375         PARSE_DEFAULT(tmpl.smtpuser);
376         PARSE_DEFAULT(tmpl.smtppass);
377         PARSE_DEFAULT(tmpl.recvserver);
378         PARSE_DEFAULT(tmpl.recvuser);
379         PARSE_DEFAULT(tmpl.recvpass);
380         PARSE_DEFAULT(tmpl.imapdir);
381         PARSE_DEFAULT(tmpl.mboxfile);
382         PARSE_DEFAULT(tmpl.mailbox);
383 /*
384         printf("defaults:"
385         "%s, %s, %s, %s, %s, %d, %s, %s, %s, %s, %s, %s, %d, %d\n",
386         tmpl.name,tmpl.domain,tmpl.email,tmpl.organization,tmpl.smtpserver,
387         tmpl.recvtype,tmpl.recvserver,tmpl.recvuser,tmpl.recvpass,
388         tmpl.imapdir,tmpl.mboxfile,tmpl.mailbox,tmpl.smtpssl,tmpl.recvssl);
389 */
390         g_free(rcpath);
391 }
392
393
394 static void initialize_fonts(WizardWindow *wizard)
395 {
396         GtkWidget *widget = wizard->email;
397         gint size = pango_font_description_get_size(
398                         widget->style->font_desc)
399                       /PANGO_SCALE;
400         gchar *tmp, *new;
401         
402         tmp = g_strdup(prefs_common.textfont);
403         if (strrchr(tmp, ' ')) {
404                 *(strrchr(tmp, ' ')) = '\0';
405                 new = g_strdup_printf("%s %d", tmp, size);
406                 g_free(prefs_common.textfont);
407                 prefs_common.textfont = new;
408         }
409         g_free(tmp);
410         
411         tmp = g_strdup(prefs_common.smallfont);
412         if (strrchr(tmp, ' ')) {
413                 *(strrchr(tmp, ' ')) = '\0';
414                 new = g_strdup_printf("%s %d", tmp, size);
415                 g_free(prefs_common.smallfont);
416                 prefs_common.smallfont = new;
417         }
418         g_free(tmp);
419         
420         tmp = g_strdup(prefs_common.normalfont);
421         if (strrchr(tmp, ' ')) {
422                 *(strrchr(tmp, ' ')) = '\0';
423                 new = g_strdup_printf("%s %d", tmp, size);
424                 g_free(prefs_common.normalfont);
425                 prefs_common.normalfont = new;
426         }
427         g_free(tmp);
428 }
429
430 #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"
431 #define FACE "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJ1BMVEUTGBYnS3BCUE9KVC9acyRy\n\
432  kC6LjITZdGiumnF/p7yrq6jJzc/5+vf7GI+IAAACZ0lEQVQ4y5XUvW/UMBQA8LRl6VZ3QLqNNhVS\n\
433  YSnKFfExEg9lbJNK3QqVoiPHzgpFnAIs5Do9ORNDVOwuFVIgduZWOfuP4vlyybVVkeCdTpHe7z1/\n\
434  RHYc85dw/gEUxi2gS5ZlDKqbUDcpzarroMEOpDU2VtcAmFKlxn+t2VWQUNa1BmAlGrsCqWKMwetv\n\
435  MMbnFaixFn9rh47DFCuzDs6hxPyrxXuOA7WqWQcp2Fhx7gTOcga6bGHSS6FHCHFouLcMJptuaQbp\n\
436  +kff90P6Yn0TMpXN4DxNVz+FQZ8Gob8DGWSsBUIG23seDXwaBBnU0EJKVhNvqx/6aHTHrmMGhNw9\n\
437  XlqkgbdEX/gh1PUcVj84G4HnuHTXDQ+6Dk3IyqFL/UfEpXvj7RChaoEsUX9rYeGE7o83wp0WcCjS\n\
438  9/01AifhLrj0oINeuuJ53kIP+uF+7uL03eQpGWx5yzDu05fM3Z53AJDkvdODwvOfmbH3uOuwcLz+\n\
439  UBWLR8/N+MHnDsoeG4zecDl5Mhqa74NR90p+VEYmo+ioSEan8WnytANhDB8kX06TJFZJcowvfrZz\n\
440  XIMS2vxMIlHJfHMTRLOqC7TovlJFEVVGF7yafFVTkGgij+I851hZCHP5Tk8BWXObjuxhl2fm8pdu\n\
441  O0wluZDKHgJ91nVMKhuKN6cZQf9uQAs85lrjGDYmwmqzDynwClRKTCF/OwfDub0dQyzHxVUt6DzK\n\
442  eY5NseIxb8abwoVSMpZDhJyL9kJamGAxplC7izkHyaXM5/nZHFiNeRHfBFNwLjhOr+fAmw1G3OYl\n\
443  bwoijGGzD40pdeu3ROv/+Pr8AWPP4vVXbP0VAAAAAElFTkSuQmCC"
444
445 static void write_welcome_email(WizardWindow *wizard)
446 {
447         gchar buf_date[64];
448         gchar *head=NULL;
449         gchar *body=NULL;
450         gchar *msg=NULL;
451         gchar *subj=NULL;
452         const gchar *mailbox = gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name));
453         Folder *folder = folder_find_from_path(mailbox);
454         FolderItem *inbox = folder ? folder->inbox:NULL;
455         gchar *file = get_tmp_file();
456         
457         get_rfc822_date(buf_date, sizeof(buf_date));
458
459         subj = g_strdup_printf(_("Welcome to Sylpheed-Claws "));
460
461         head = g_strdup_printf(
462                 "From: %s <%s>\n"
463                 "To: %s <%s>\n"
464                 "Date: %s\n"
465                 "Subject: %s\n"
466                 "X-Face: %s\n"
467                 "Face: %s\n"
468                 "Content-Type: text/plain; charset=UTF-8\n",
469                 _("Sylpheed-Claws Team"),
470                 USERS_ML_ADDR,
471                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)),
472                 gtk_entry_get_text(GTK_ENTRY(wizard->email)),
473                 buf_date, subj, XFACE, FACE);
474         body = g_strdup_printf(
475                 _("\n"
476                 "Welcome to Sylpheed-Claws\n"
477                 "-------------------------\n"
478                 "\n"
479                 "Now that you have set up your account you can fetch your\n"
480                 "mail by clicking the 'Get Mail' button at the left of the\n"
481                 "toolbar.\n"
482                 "\n"
483                 "You can change your Account Preferences by using the menu\n"
484                 "entry '/Configuration/Preferences for current account'\n"
485                 "and change the general Preferences by using\n"
486                 "'/Configuration/Preferences'.\n"
487                 "\n"
488                 "You can find further information in the Sylpheed-Claws manual,\n"
489                 "which can be accessed by using the menu entry '/Help/Manual'\n"
490                 "or online at the URL given below.\n"
491                 "\n"
492                 "Useful URLs\n"
493                 "-----------\n"
494                 "Homepage:      <%s>\n"
495                 "Manual:        <%s>\n"
496                 "FAQ:          <%s>\n"
497                 "Themes:        <%s>\n"
498                 "Mailing Lists: <%s>\n"
499                 "\n"
500                 "LICENSE\n"
501                 "-------\n"
502                 "Sylpheed-Claws is free software, released under the terms\n"
503                 "of the GNU General Public License, version 2 or later, as\n"
504                 "published by the Free Software Foundation, 51 Franklin Street,\n"
505                 "Fifth Floor, Boston, MA 02110-1301, USA. The license can be\n"
506                 "found at <%s>.\n"
507                 "\n"
508                 "DONATIONS\n"
509                 "---------\n"
510                 "If you wish to donate to the Sylpheed-Claws project you can do\n"
511                 "so at <%s>.\n\n"),
512                 HOMEPAGE_URI, MANUAL_URI, FAQ_URI, THEMES_URI, MAILING_LIST_URI,
513                 GPL_URI, DONATE_URI);
514         
515         msg = g_strconcat(head, body, NULL);
516
517         if (inbox && inbox->total_msgs == 0
518          && str_write_to_file(msg, file) >= 0) {
519                 MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
520                 folder_item_add_msg(inbox, file, &flags, FALSE);
521         }
522         g_free(subj);
523         g_free(head);
524         g_free(body);
525         g_free(msg);
526         g_unlink(file);
527 }
528 #undef XFACE
529
530 static gboolean wizard_write_config(WizardWindow *wizard)
531 {
532         static gboolean mailbox_ok = FALSE;
533         PrefsAccount *prefs_account = prefs_account_new();
534         GList *account_list = NULL;
535         GtkWidget *menu, *menuitem;
536         
537         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
538         menuitem = gtk_menu_get_active(GTK_MENU(menu));
539         prefs_account->protocol = GPOINTER_TO_INT
540                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
541         
542         
543         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4 && 
544             !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)))) {
545                 alertpanel_error(_("Please enter the mailbox name."));
546                 g_free(prefs_account);
547                 gtk_notebook_set_current_page (
548                         GTK_NOTEBOOK(wizard->notebook), 
549                         MAILBOX_PAGE);
550                 return FALSE;
551         }
552
553         if (!mailbox_ok) {
554                 if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
555                         mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
556                                         gtk_entry_get_text(
557                                                 GTK_ENTRY(wizard->mailbox_name)));
558                 } else
559                         mailbox_ok = TRUE;
560         }
561
562         if (!mailbox_ok) {
563                 /* alertpanel done by setup_write_mailbox_path */
564                 g_free(prefs_account);
565                 gtk_notebook_set_current_page (
566                         GTK_NOTEBOOK(wizard->notebook), 
567                         MAILBOX_PAGE);
568                 return FALSE;
569         }
570         
571         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->full_name)))
572         ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->email)))) {
573                 alertpanel_error(_("Please enter your name and email address."));
574                 g_free(prefs_account);
575                 gtk_notebook_set_current_page (
576                         GTK_NOTEBOOK(wizard->notebook), 
577                         USER_PAGE);
578                 return FALSE;
579         }
580         
581         if (prefs_account->protocol != A_LOCAL) {
582                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)))
583                 ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
584                         alertpanel_error(_("Please enter your receiving server "
585                                            "and username."));
586                         g_free(prefs_account);
587                         gtk_notebook_set_current_page (
588                                 GTK_NOTEBOOK(wizard->notebook), 
589                                 RECV_PAGE);
590                         return FALSE;
591                 }
592         } else {
593                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
594                         alertpanel_error(_("Please enter your username."));
595                         g_free(prefs_account);
596                         gtk_notebook_set_current_page (
597                                 GTK_NOTEBOOK(wizard->notebook), 
598                                 RECV_PAGE);
599                         return FALSE;
600                 }
601         }
602         
603         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)))) {
604                 alertpanel_error(_("Please enter your SMTP server."));
605                 g_free(prefs_account);
606                 gtk_notebook_set_current_page (
607                         GTK_NOTEBOOK(wizard->notebook), 
608                         SMTP_PAGE);
609                 return FALSE;
610         }
611
612         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth))) {
613                 if (prefs_account->protocol == A_LOCAL
614                 &&  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->smtp_username)))) {
615                         alertpanel_error(_("Please enter your SMTP username."));
616                         g_free(prefs_account);
617                         gtk_notebook_set_current_page (
618                                 GTK_NOTEBOOK(wizard->notebook), 
619                                 SMTP_PAGE);
620                         return FALSE;           
621                 } /* if it's not local we'll use the reception server */
622         }
623
624         if (prefs_account->protocol != A_LOCAL)
625                 prefs_account->account_name = g_strdup_printf("%s@%s",
626                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
627                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
628         else
629                 prefs_account->account_name = g_strdup_printf("%s",
630                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
631
632         prefs_account->name = g_strdup(
633                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
634         prefs_account->address = g_strdup(
635                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
636         prefs_account->organization = g_strdup(
637                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
638         prefs_account->smtp_server = g_strdup(
639                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
640
641         if (prefs_account->protocol != A_LOCAL)
642                 prefs_account->recv_server = g_strdup(
643                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
644         else
645                 prefs_account->local_mbox = g_strdup(
646                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
647
648         prefs_account->userid = g_strdup(
649                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
650         prefs_account->passwd = g_strdup(
651                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
652
653         prefs_account->smtp_userid = g_strdup(
654                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_username)));
655         prefs_account->smtp_passwd = g_strdup(
656                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_password)));
657         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth))) {
658                 prefs_account->use_smtp_auth = TRUE;
659         }
660
661 #ifdef USE_OPENSSL                      
662         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
663                 prefs_account->ssl_smtp = SSL_TUNNEL;
664         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
665                 if (prefs_account->protocol == A_IMAP4)
666                         prefs_account->ssl_imap = SSL_TUNNEL;
667                 else
668                         prefs_account->ssl_pop = SSL_TUNNEL;
669         }
670 #endif
671         if (prefs_account->protocol == A_IMAP4) {
672                 gchar *directory = gtk_editable_get_chars(
673                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
674                 if (directory && strlen(directory)) {
675                         prefs_account->imap_dir = g_strdup(directory);
676                 }
677                 g_free(directory);
678         }
679
680         account_list = g_list_append(account_list, prefs_account);
681         prefs_account_write_config_all(account_list);
682         prefs_account_free(prefs_account);
683         account_read_config_all();
684
685         initialize_fonts(wizard);
686         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
687                 write_welcome_email(wizard);
688         
689         return TRUE;
690 }
691
692 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
693 {
694         GtkWidget *w;
695         GtkWidget *vbox;
696         GtkWidget *hbox;
697         GtkWidget *image;
698         char *title_string;
699
700         vbox = gtk_vbox_new (FALSE, 6);
701         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
702
703         /* create the titlebar */
704         hbox = gtk_hbox_new (FALSE, 12);
705         image = stock_pixmap_widget(wizard->window, 
706                                 STOCK_PIXMAP_SYLPHEED_CLAWS_ICON);
707         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
708         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
709         w = gtk_label_new (title_string);
710         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
711         g_free (title_string);
712         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
713
714         /* pack the titlebar */
715         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
716
717         /* pack the separator */
718         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
719
720         /* pack space */
721         w = gtk_alignment_new (0, 0, 0, 0);
722         gtk_widget_set_size_request (w, 0, 6);
723         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
724
725         return vbox;
726 }
727
728 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
729         GtkWidget *label = gtk_label_new(text);                               \
730         gtk_table_attach(GTK_TABLE(table), label,                             \
731                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
732         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);                     \
733         if (GTK_IS_MISC(label))                                               \
734                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
735         gtk_table_attach(GTK_TABLE(table), entry,                             \
736                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
737 }
738
739 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
740 {
741         if (!strcmp(type, "smtp")) {
742                 if (!tmpl.smtpserver || !strlen(tmpl.smtpserver))
743                         return g_strconcat(type, ".", tmpl.domain, NULL);
744                 else 
745                         return g_strdup(tmpl.smtpserver);
746         } else {
747                 if (!tmpl.recvserver || !strlen(tmpl.recvserver))
748                         return g_strconcat(type, ".", tmpl.domain, NULL);
749                 else 
750                         return g_strdup(tmpl.recvserver);
751         }
752 }
753
754 static gchar *get_default_account(WizardWindow * wizard)
755 {
756         gchar *result = NULL;
757         
758         if (!tmpl.recvuser || !strlen(tmpl.recvuser)) {
759                 result = gtk_editable_get_chars(
760                                 GTK_EDITABLE(wizard->email), 0, -1);
761
762                 if (strstr(result, "@")) {
763                         *(strstr(result,"@")) = '\0';
764                 } 
765         } else {
766                 result = g_strdup(tmpl.recvuser);
767         }
768         return result;
769 }
770
771 static gchar *get_default_smtp_account(WizardWindow * wizard)
772 {
773         gchar *result = NULL;
774         
775         if (!tmpl.smtpuser || !strlen(tmpl.smtpuser)) {
776                 return g_strdup("");
777         } else {
778                 result = g_strdup(tmpl.smtpuser);
779         }
780         return result;
781 }
782
783 static void wizard_email_changed(GtkWidget *widget, gpointer data)
784 {
785         WizardWindow *wizard = (WizardWindow *)data;
786         RecvProtocol protocol;
787         gchar *text;
788         protocol = GPOINTER_TO_INT
789                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
790         
791         text = get_default_server(wizard, "smtp");
792         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
793         g_free(text);
794
795         text = get_default_account(wizard);
796         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
797         g_free(text);
798
799         if (protocol == A_POP3) {
800                 text = get_default_server(wizard, "pop");
801                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
802                 g_free(text);
803         } else if (protocol == A_IMAP4) {
804                 text = get_default_server(wizard, "imap");
805                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
806                 g_free(text);
807         } else if (protocol == A_LOCAL) {
808                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
809         }
810         
811 }
812
813 static GtkWidget* user_page (WizardWindow * wizard)
814 {
815         GtkWidget *table = gtk_table_new(3,2, FALSE);
816         gint i = 0;
817         
818         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
819         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
820
821         wizard->full_name = gtk_entry_new();
822         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), tmpl.name?tmpl.name:"");
823         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your name:</span>"), 
824                              wizard->full_name, i); i++;
825         
826         wizard->email = gtk_entry_new();
827         gtk_entry_set_text(GTK_ENTRY(wizard->email), tmpl.email?tmpl.email:"");
828         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your email address:</span>"), 
829                              wizard->email, i); i++;
830         
831         wizard->organization = gtk_entry_new();
832         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"),
833                              wizard->organization, i); i++;
834         gtk_entry_set_text(GTK_ENTRY(wizard->organization), tmpl.organization?tmpl.organization:"");
835         
836         g_signal_connect(G_OBJECT(wizard->email), "changed",
837                          G_CALLBACK(wizard_email_changed),
838                          wizard);
839         return table;
840 }
841
842 static GtkWidget* mailbox_page (WizardWindow * wizard)
843 {
844         GtkWidget *table = gtk_table_new(1,2, FALSE);
845         gint i = 0;
846         
847         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
848         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
849
850         wizard->mailbox_name = gtk_entry_new();
851         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), tmpl.mailbox?tmpl.mailbox:"");
852         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Mailbox name:</span>"), 
853                              wizard->mailbox_name, i); i++;
854         
855         return table;
856 }
857
858 static void smtp_auth_changed (GtkWidget *btn, gpointer data)
859 {
860         WizardWindow *wizard = (WizardWindow *)data;
861         gboolean do_auth = gtk_toggle_button_get_active(
862                 GTK_TOGGLE_BUTTON(wizard->smtp_auth));
863         gtk_widget_set_sensitive(wizard->smtp_username, do_auth);
864         gtk_widget_set_sensitive(wizard->smtp_username_label, do_auth);
865         gtk_widget_set_sensitive(wizard->smtp_password, do_auth);
866         gtk_widget_set_sensitive(wizard->smtp_password_label, do_auth);
867 }
868
869 static GtkWidget* smtp_page (WizardWindow * wizard)
870 {
871         GtkWidget *table = gtk_table_new(1,4, FALSE);
872         gchar *text;
873         gint i = 0;
874         
875         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
876         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
877
878         wizard->smtp_server = gtk_entry_new();
879         text = get_default_server(wizard, "smtp");
880         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
881         g_free(text);
882         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">SMTP server address:</span>"), 
883                              wizard->smtp_server, i); i++;
884         wizard->smtp_auth = gtk_check_button_new_with_label(
885                                         _("Use authentication"));
886         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth),
887                         tmpl.smtpauth);
888         g_signal_connect(G_OBJECT(wizard->smtp_auth), "toggled",
889                          G_CALLBACK(smtp_auth_changed),
890                          wizard);
891         gtk_table_attach(GTK_TABLE(table), wizard->smtp_auth,      
892                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
893
894         text = get_default_smtp_account(wizard);
895
896         wizard->smtp_username = gtk_entry_new();
897         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_username), text);
898         g_free(text);
899         wizard->smtp_username_label = gtk_label_new(_("SMTP username:\n"
900                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
901         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_username_label), TRUE);
902         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username_label,                               
903                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
904         if (GTK_IS_MISC(wizard->smtp_username_label))                                                 
905                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_username_label), 1, 0.5);        
906         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username,             
907                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
908         i++;
909         wizard->smtp_password = gtk_entry_new();
910         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_password), tmpl.smtppass?tmpl.smtppass:""); 
911         gtk_entry_set_visibility(GTK_ENTRY(wizard->smtp_password), FALSE);
912         wizard->smtp_password_label = gtk_label_new(_("SMTP password:\n"
913                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
914         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_password_label), TRUE);
915         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password_label,                               
916                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
917         if (GTK_IS_MISC(wizard->smtp_password_label))                                                 
918                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_password_label), 1, 0.5);        
919         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password,             
920                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
921         i++;
922         smtp_auth_changed(NULL, wizard);
923         return table;
924 }
925
926 static void wizard_protocol_change(WizardWindow *wizard, RecvProtocol protocol)
927 {
928         gchar *text;
929         
930         if (protocol == A_POP3) {
931                 text = get_default_server(wizard, "pop");
932                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
933                 gtk_widget_hide(wizard->recv_imap_label);
934                 gtk_widget_hide(wizard->recv_imap_subdir);
935                 gtk_widget_show(wizard->recv_username);
936                 gtk_widget_show(wizard->recv_password);
937                 gtk_widget_show(wizard->recv_username_label);
938                 gtk_widget_show(wizard->recv_password_label);
939                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
940                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
941                 g_free(text);
942         } else if (protocol == A_IMAP4) {
943                 text = get_default_server(wizard, "imap");
944                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
945                 gtk_widget_show(wizard->recv_imap_label);
946                 gtk_widget_show(wizard->recv_imap_subdir);
947                 gtk_widget_show(wizard->recv_username);
948                 gtk_widget_show(wizard->recv_password);
949                 gtk_widget_show(wizard->recv_username_label);
950                 gtk_widget_show(wizard->recv_password_label);
951                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
952                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
953                 g_free(text);
954         } else if (protocol == A_LOCAL) {
955                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
956                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
957                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
958                 gtk_widget_hide(wizard->recv_imap_label);
959                 gtk_widget_hide(wizard->recv_imap_subdir);
960                 gtk_widget_hide(wizard->recv_username);
961                 gtk_widget_hide(wizard->recv_password);
962                 gtk_widget_hide(wizard->recv_username_label);
963                 gtk_widget_hide(wizard->recv_password_label);
964         }
965 }
966
967 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
968 {
969         WizardWindow *wizard = (WizardWindow *)data;
970         RecvProtocol protocol;
971         protocol = GPOINTER_TO_INT
972                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
973
974         wizard_protocol_change(wizard, protocol);       
975 }
976
977 static GtkWidget* recv_page (WizardWindow * wizard)
978 {
979         GtkWidget *table = gtk_table_new(5,2, FALSE);
980         GtkWidget *menu = gtk_menu_new();
981         GtkWidget *menuitem;
982         gchar *text;
983         gint i = 0;
984         gint index = 0;
985
986         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
987         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
988
989         wizard->recv_type = gtk_option_menu_new();
990         
991         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
992         g_signal_connect(G_OBJECT(menuitem), "activate",
993                          G_CALLBACK(wizard_protocol_changed),
994                          wizard);
995 #ifdef HAVE_LIBETPAN
996         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
997         g_signal_connect(G_OBJECT(menuitem), "activate",
998                          G_CALLBACK(wizard_protocol_changed),
999                          wizard);
1000 #endif
1001         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
1002         g_signal_connect(G_OBJECT(menuitem), "activate",
1003                          G_CALLBACK(wizard_protocol_changed),
1004                          wizard);
1005
1006         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
1007         switch(tmpl.recvtype) {
1008         case A_POP3: 
1009                 index = 0;
1010                 break;
1011 #ifdef HAVE_LIBETPAN
1012         case A_IMAP4:
1013                 index = 1;
1014                 break;
1015         case A_LOCAL:
1016                 index = 2;
1017                 break;
1018 #else
1019         case A_LOCAL:
1020                 index = 1;
1021                 break;
1022 #endif
1023         default:
1024                 index = 0;
1025         }
1026         gtk_option_menu_set_history(GTK_OPTION_MENU (wizard->recv_type), index);
1027         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
1028                              wizard->recv_type, i); i++;
1029
1030         wizard->recv_server = gtk_entry_new();
1031         text = get_default_server(wizard, "pop");
1032         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1033         g_free(text);
1034         
1035         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
1036         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1037         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
1038                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1039         if (GTK_IS_MISC(wizard->recv_label))                                                  
1040                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
1041         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
1042                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1043         i++;
1044         
1045         wizard->recv_username = gtk_entry_new();
1046         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
1047         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
1048         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
1049                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1050         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
1051                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
1052         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
1053                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1054         i++;
1055         
1056         text = get_default_account(wizard);
1057         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
1058         g_free(text);
1059
1060         wizard->recv_password = gtk_entry_new();
1061         gtk_entry_set_text(GTK_ENTRY(wizard->recv_password), tmpl.recvpass?tmpl.recvpass:"");
1062         wizard->recv_password_label = gtk_label_new(_("Password:"));
1063         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
1064                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1065         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
1066                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
1067         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
1068                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1069         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
1070         i++;
1071         
1072         wizard->recv_imap_subdir = gtk_entry_new();
1073         gtk_entry_set_text(GTK_ENTRY(wizard->recv_imap_subdir), tmpl.imapdir?tmpl.imapdir:"");
1074         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
1075         
1076         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
1077                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1078         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
1079                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
1080         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
1081                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1082
1083         i++;
1084         
1085         return table;
1086 }
1087
1088 #ifdef USE_OPENSSL
1089 static GtkWidget* ssl_page (WizardWindow * wizard)
1090 {
1091         GtkWidget *table = gtk_table_new(2,2, FALSE);
1092         gint i = 0;
1093         
1094         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1095         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1096
1097         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
1098                                         _("Use SSL to connect to SMTP server"));
1099         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl),
1100                         tmpl.smtpssl);
1101         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
1102                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
1103         
1104         wizard->recv_use_ssl = gtk_check_button_new_with_label(
1105                                         _("Use SSL to connect to receiving server"));
1106         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl),
1107                         tmpl.recvssl);
1108         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
1109                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
1110         
1111         return table;
1112 }
1113 #endif
1114
1115 static void
1116 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
1117 {
1118         WizardWindow * wizard = (WizardWindow *)data;
1119         int current_page, num_pages;
1120         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
1121         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
1122         gint protocol = GPOINTER_TO_INT
1123                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1124         gboolean skip_mailbox_page = FALSE;
1125         
1126         if (protocol == A_IMAP4) {
1127                 skip_mailbox_page = TRUE;
1128         }
1129         
1130         num_pages = g_slist_length(wizard->pages);
1131
1132         current_page = gtk_notebook_get_current_page (
1133                                 GTK_NOTEBOOK(wizard->notebook));
1134         if (response == CANCEL)
1135         {
1136                 wizard->result = FALSE;
1137                 wizard->finished = TRUE;
1138                 gtk_widget_destroy (GTK_WIDGET(dialog));
1139         }
1140         else if (response == FINISHED)
1141         {
1142                 if (!wizard_write_config(wizard)) {
1143                         current_page = gtk_notebook_get_current_page (
1144                                         GTK_NOTEBOOK(wizard->notebook));
1145                         goto set_sens;
1146                 }
1147                 wizard->result = TRUE;
1148                 wizard->finished = TRUE;
1149                 gtk_widget_destroy (GTK_WIDGET(dialog));
1150         }
1151         else
1152         {
1153                 if (response == GO_BACK)
1154                 {
1155                         if (current_page > 0) {
1156                                 current_page--;
1157                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1158                                         /* mailbox */
1159                                         current_page--;
1160                                 }
1161                                 gtk_notebook_set_current_page (
1162                                         GTK_NOTEBOOK(wizard->notebook), 
1163                                         current_page);
1164                         }
1165                 }
1166                 else if (response == GO_FORWARD)
1167                 {
1168                         if (current_page < (num_pages-1)) {
1169                                 current_page++;
1170                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1171                                         /* mailbox */
1172                                         current_page++;
1173                                 }
1174                                 gtk_notebook_set_current_page (
1175                                         GTK_NOTEBOOK(wizard->notebook), 
1176                                         current_page);
1177                         }
1178                 }
1179 set_sens:
1180                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
1181                                 current_page > 0);
1182                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
1183                                 current_page < (num_pages - 1));
1184                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
1185                                 current_page == (num_pages - 1));
1186         }
1187 }
1188
1189 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
1190                                  gpointer data)
1191 {
1192         WizardWindow *wizard = (WizardWindow *)data;
1193         wizard->result = FALSE;
1194         wizard->finished = TRUE;
1195         
1196         return FALSE;
1197 }
1198
1199 #define PACK_WARNING(text) {                                            \
1200         label = gtk_label_new(text);                                    \
1201         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
1202         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
1203 }
1204
1205 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
1206         WizardWindow *wizard = g_new0(WizardWindow, 1);
1207         GtkWidget *page;
1208         GtkWidget *widget;
1209         GtkWidget *label;
1210         gchar     *text;
1211         GSList    *cur;
1212         gboolean   result;
1213         gint i = 0;
1214         wizard->mainwin = mainwin;
1215         wizard->create_mailbox = create_mailbox;
1216         
1217         gtk_widget_hide(mainwin->window);
1218         
1219         wizard_read_defaults();
1220         
1221         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
1222                         NULL, 0, 
1223                         GTK_STOCK_GO_BACK, GO_BACK,
1224                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
1225                         GTK_STOCK_SAVE, FINISHED,
1226                         GTK_STOCK_CANCEL, CANCEL,
1227                         NULL);
1228
1229         g_signal_connect(wizard->window, "response", 
1230                           G_CALLBACK(wizard_response_cb), wizard);
1231         gtk_widget_realize(wizard->window);
1232         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
1233                         GO_FORWARD);
1234         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1235                         GO_BACK, FALSE);
1236         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1237                         GO_FORWARD, TRUE);
1238         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1239                         FINISHED, FALSE);
1240         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1241                         CANCEL, TRUE);
1242         
1243         wizard->notebook = gtk_notebook_new();
1244         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
1245         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
1246         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
1247                             wizard->notebook, TRUE, TRUE, 0);
1248         
1249         wizard->pages = NULL;
1250         
1251 /*welcome page: 0 */
1252         WELCOME_PAGE = i;
1253         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
1254         
1255         wizard->pages = g_slist_append(wizard->pages, page);
1256         widget = stock_pixmap_widget(wizard->window, 
1257                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1258
1259         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1260         
1261         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
1262                           "We will begin by defining some basic "
1263                           "information about you and your most common "
1264                           "mail options so that you can start to use "
1265                           "Sylpheed-Claws in less than five minutes."));
1266         widget = gtk_label_new(text);
1267         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1268         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1269         g_free(text);
1270
1271 /* user page: 1 */
1272         i++;
1273         USER_PAGE = i;
1274         widget = create_page (wizard, _("About You"));
1275         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
1276         PACK_WARNING(_("Bold fields must be completed"));
1277         
1278         wizard->pages = g_slist_append(wizard->pages, widget);
1279
1280 /* recv+auth page: 2 */
1281         i++;
1282         RECV_PAGE = i;
1283         widget = create_page (wizard, _("Receiving mail"));
1284         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
1285         PACK_WARNING(_("Bold fields must be completed"));
1286         
1287         wizard->pages = g_slist_append(wizard->pages, widget);
1288
1289 /*smtp page: 3 */
1290         i++;
1291         SMTP_PAGE = i;
1292         widget = create_page (wizard, _("Sending mail"));
1293         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
1294         PACK_WARNING(_("Bold fields must be completed"));
1295         
1296         wizard->pages = g_slist_append(wizard->pages, widget);
1297
1298 /* mailbox page: 4 */
1299         if (create_mailbox) {
1300                 i++;
1301                 MAILBOX_PAGE = i;
1302                 widget = create_page (wizard, _("Saving mail on disk"));
1303                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
1304                 PACK_WARNING(_("Bold fields must be completed"));
1305         
1306                 wizard->pages = g_slist_append(wizard->pages, widget);
1307         }
1308 /* ssl page: 5 */
1309 #ifdef USE_OPENSSL
1310         i++;
1311         SSL_PAGE = i;
1312         widget = create_page (wizard, _("Security"));
1313         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
1314         PACK_WARNING(_("Bold fields must be completed"));
1315         
1316         wizard->pages = g_slist_append(wizard->pages, widget);
1317 #endif
1318
1319 /* done page: 6 */
1320         i++;
1321         DONE_PAGE = i;
1322         page = create_page(wizard, _("Configuration finished"));
1323         
1324         wizard->pages = g_slist_append(wizard->pages, page);
1325         widget = stock_pixmap_widget(wizard->window, 
1326                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1327
1328         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1329         
1330         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
1331                           "Click Save to start."));
1332         widget = gtk_label_new(text);
1333         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1334         g_free(text);
1335
1336
1337         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
1338                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
1339                                           GTK_WIDGET(cur->data), NULL);
1340         }
1341         
1342         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
1343                          G_CALLBACK(wizard_close_cb), wizard);
1344         gtk_widget_show_all (wizard->window);
1345
1346         gtk_widget_hide(wizard->recv_imap_label);
1347         gtk_widget_hide(wizard->recv_imap_subdir);
1348
1349         wizard_protocol_change(wizard, tmpl.recvtype);
1350
1351         while (!wizard->finished)
1352                 gtk_main_iteration();
1353
1354         result = wizard->result;
1355         
1356         GTK_EVENTS_FLUSH();
1357
1358         gtk_widget_show(mainwin->window);
1359         g_free(wizard);
1360
1361         return result;
1362 }