2006-09-18 [paul] 2.4.0cvs197
[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                 "Sylpheed-Claws has lots of extra features accessible via plugins,\n"
484                 "like anti-spam filtering and learning (via the Bogofilter or\n"
485                 "SpamAssassin plugins), privacy protection (via PGP/Mime), an RSS\n"
486                 "aggregator, a calendar, and much more. You can load them from\n"
487                 "the menu entry '/Configuration/Plugins'.\n"
488                 "\n"
489                 "You can change your Account Preferences by using the menu\n"
490                 "entry '/Configuration/Preferences for current account'\n"
491                 "and change the general Preferences by using\n"
492                 "'/Configuration/Preferences'.\n"
493                 "\n"
494                 "You can find further information in the Sylpheed-Claws manual,\n"
495                 "which can be accessed by using the menu entry '/Help/Manual'\n"
496                 "or online at the URL given below.\n"
497                 "\n"
498                 "Useful URLs\n"
499                 "-----------\n"
500                 "Homepage:      <%s>\n"
501                 "Manual:        <%s>\n"
502                 "FAQ:          <%s>\n"
503                 "Themes:        <%s>\n"
504                 "Mailing Lists: <%s>\n"
505                 "\n"
506                 "LICENSE\n"
507                 "-------\n"
508                 "Sylpheed-Claws is free software, released under the terms\n"
509                 "of the GNU General Public License, version 2 or later, as\n"
510                 "published by the Free Software Foundation, 51 Franklin Street,\n"
511                 "Fifth Floor, Boston, MA 02110-1301, USA. The license can be\n"
512                 "found at <%s>.\n"
513                 "\n"
514                 "DONATIONS\n"
515                 "---------\n"
516                 "If you wish to donate to the Sylpheed-Claws project you can do\n"
517                 "so at <%s>.\n\n"),
518                 HOMEPAGE_URI, MANUAL_URI, FAQ_URI, THEMES_URI, MAILING_LIST_URI,
519                 GPL_URI, DONATE_URI);
520         
521         msg = g_strconcat(head, body, NULL);
522
523         if (inbox && inbox->total_msgs == 0
524          && str_write_to_file(msg, file) >= 0) {
525                 MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
526                 folder_item_add_msg(inbox, file, &flags, FALSE);
527         }
528         g_free(subj);
529         g_free(head);
530         g_free(body);
531         g_free(msg);
532         g_unlink(file);
533 }
534 #undef XFACE
535
536 static gboolean wizard_write_config(WizardWindow *wizard)
537 {
538         static gboolean mailbox_ok = FALSE;
539         PrefsAccount *prefs_account = prefs_account_new();
540         GList *account_list = NULL;
541         GtkWidget *menu, *menuitem;
542         
543         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
544         menuitem = gtk_menu_get_active(GTK_MENU(menu));
545         prefs_account->protocol = GPOINTER_TO_INT
546                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
547         
548         
549         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4 && 
550             !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)))) {
551                 alertpanel_error(_("Please enter the mailbox name."));
552                 g_free(prefs_account);
553                 gtk_notebook_set_current_page (
554                         GTK_NOTEBOOK(wizard->notebook), 
555                         MAILBOX_PAGE);
556                 return FALSE;
557         }
558
559         if (!mailbox_ok) {
560                 if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
561                         mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
562                                         gtk_entry_get_text(
563                                                 GTK_ENTRY(wizard->mailbox_name)));
564                 } else
565                         mailbox_ok = TRUE;
566         }
567
568         if (!mailbox_ok) {
569                 /* alertpanel done by setup_write_mailbox_path */
570                 g_free(prefs_account);
571                 gtk_notebook_set_current_page (
572                         GTK_NOTEBOOK(wizard->notebook), 
573                         MAILBOX_PAGE);
574                 return FALSE;
575         }
576         
577         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->full_name)))
578         ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->email)))) {
579                 alertpanel_error(_("Please enter your name and email address."));
580                 g_free(prefs_account);
581                 gtk_notebook_set_current_page (
582                         GTK_NOTEBOOK(wizard->notebook), 
583                         USER_PAGE);
584                 return FALSE;
585         }
586         
587         if (prefs_account->protocol != A_LOCAL) {
588                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)))
589                 ||  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
590                         alertpanel_error(_("Please enter your receiving server "
591                                            "and username."));
592                         g_free(prefs_account);
593                         gtk_notebook_set_current_page (
594                                 GTK_NOTEBOOK(wizard->notebook), 
595                                 RECV_PAGE);
596                         return FALSE;
597                 }
598         } else {
599                 if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)))) {
600                         alertpanel_error(_("Please enter your username."));
601                         g_free(prefs_account);
602                         gtk_notebook_set_current_page (
603                                 GTK_NOTEBOOK(wizard->notebook), 
604                                 RECV_PAGE);
605                         return FALSE;
606                 }
607         }
608         
609         if (!strlen(gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)))) {
610                 alertpanel_error(_("Please enter your SMTP server."));
611                 g_free(prefs_account);
612                 gtk_notebook_set_current_page (
613                         GTK_NOTEBOOK(wizard->notebook), 
614                         SMTP_PAGE);
615                 return FALSE;
616         }
617
618         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth))) {
619                 if (prefs_account->protocol == A_LOCAL
620                 &&  !strlen(gtk_entry_get_text(GTK_ENTRY(wizard->smtp_username)))) {
621                         alertpanel_error(_("Please enter your SMTP username."));
622                         g_free(prefs_account);
623                         gtk_notebook_set_current_page (
624                                 GTK_NOTEBOOK(wizard->notebook), 
625                                 SMTP_PAGE);
626                         return FALSE;           
627                 } /* if it's not local we'll use the reception server */
628         }
629
630         if (prefs_account->protocol != A_LOCAL)
631                 prefs_account->account_name = g_strdup_printf("%s@%s",
632                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
633                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
634         else
635                 prefs_account->account_name = g_strdup_printf("%s",
636                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
637
638         prefs_account->name = g_strdup(
639                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
640         prefs_account->address = g_strdup(
641                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
642         prefs_account->organization = g_strdup(
643                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
644         prefs_account->smtp_server = g_strdup(
645                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
646
647         if (prefs_account->protocol != A_LOCAL)
648                 prefs_account->recv_server = g_strdup(
649                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
650         else
651                 prefs_account->local_mbox = g_strdup(
652                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
653
654         prefs_account->userid = g_strdup(
655                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
656         prefs_account->passwd = g_strdup(
657                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
658
659         prefs_account->smtp_userid = g_strdup(
660                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_username)));
661         prefs_account->smtp_passwd = g_strdup(
662                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_password)));
663         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth))) {
664                 prefs_account->use_smtp_auth = TRUE;
665         }
666
667 #ifdef USE_OPENSSL                      
668         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
669                 prefs_account->ssl_smtp = SSL_TUNNEL;
670         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
671                 if (prefs_account->protocol == A_IMAP4)
672                         prefs_account->ssl_imap = SSL_TUNNEL;
673                 else
674                         prefs_account->ssl_pop = SSL_TUNNEL;
675         }
676 #endif
677         if (prefs_account->protocol == A_IMAP4) {
678                 gchar *directory = gtk_editable_get_chars(
679                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
680                 if (directory && strlen(directory)) {
681                         prefs_account->imap_dir = g_strdup(directory);
682                 }
683                 g_free(directory);
684         }
685
686         account_list = g_list_append(account_list, prefs_account);
687         prefs_account_write_config_all(account_list);
688         prefs_account_free(prefs_account);
689         account_read_config_all();
690
691         initialize_fonts(wizard);
692         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
693                 write_welcome_email(wizard);
694         
695         return TRUE;
696 }
697
698 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
699 {
700         GtkWidget *w;
701         GtkWidget *vbox;
702         GtkWidget *hbox;
703         GtkWidget *image;
704         char *title_string;
705
706         vbox = gtk_vbox_new (FALSE, 6);
707         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
708
709         /* create the titlebar */
710         hbox = gtk_hbox_new (FALSE, 12);
711         image = stock_pixmap_widget(wizard->window, 
712                                 STOCK_PIXMAP_SYLPHEED_CLAWS_ICON);
713         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
714         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
715         w = gtk_label_new (title_string);
716         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
717         g_free (title_string);
718         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
719
720         /* pack the titlebar */
721         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
722
723         /* pack the separator */
724         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
725
726         /* pack space */
727         w = gtk_alignment_new (0, 0, 0, 0);
728         gtk_widget_set_size_request (w, 0, 6);
729         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
730
731         return vbox;
732 }
733
734 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
735         GtkWidget *label = gtk_label_new(text);                               \
736         gtk_table_attach(GTK_TABLE(table), label,                             \
737                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
738         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);                     \
739         if (GTK_IS_MISC(label))                                               \
740                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
741         gtk_table_attach(GTK_TABLE(table), entry,                             \
742                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
743 }
744
745 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
746 {
747         if (!strcmp(type, "smtp")) {
748                 if (!tmpl.smtpserver || !strlen(tmpl.smtpserver))
749                         return g_strconcat(type, ".", tmpl.domain, NULL);
750                 else 
751                         return g_strdup(tmpl.smtpserver);
752         } else {
753                 if (!tmpl.recvserver || !strlen(tmpl.recvserver))
754                         return g_strconcat(type, ".", tmpl.domain, NULL);
755                 else 
756                         return g_strdup(tmpl.recvserver);
757         }
758 }
759
760 static gchar *get_default_account(WizardWindow * wizard)
761 {
762         gchar *result = NULL;
763         
764         if (!tmpl.recvuser || !strlen(tmpl.recvuser)) {
765                 result = gtk_editable_get_chars(
766                                 GTK_EDITABLE(wizard->email), 0, -1);
767
768                 if (strstr(result, "@")) {
769                         *(strstr(result,"@")) = '\0';
770                 } 
771         } else {
772                 result = g_strdup(tmpl.recvuser);
773         }
774         return result;
775 }
776
777 static gchar *get_default_smtp_account(WizardWindow * wizard)
778 {
779         gchar *result = NULL;
780         
781         if (!tmpl.smtpuser || !strlen(tmpl.smtpuser)) {
782                 return g_strdup("");
783         } else {
784                 result = g_strdup(tmpl.smtpuser);
785         }
786         return result;
787 }
788
789 static void wizard_email_changed(GtkWidget *widget, gpointer data)
790 {
791         WizardWindow *wizard = (WizardWindow *)data;
792         RecvProtocol protocol;
793         gchar *text;
794         protocol = GPOINTER_TO_INT
795                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
796         
797         text = get_default_server(wizard, "smtp");
798         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
799         g_free(text);
800
801         text = get_default_account(wizard);
802         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
803         g_free(text);
804
805         if (protocol == A_POP3) {
806                 text = get_default_server(wizard, "pop");
807                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
808                 g_free(text);
809         } else if (protocol == A_IMAP4) {
810                 text = get_default_server(wizard, "imap");
811                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
812                 g_free(text);
813         } else if (protocol == A_LOCAL) {
814                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
815         }
816         
817 }
818
819 static GtkWidget* user_page (WizardWindow * wizard)
820 {
821         GtkWidget *table = gtk_table_new(3,2, FALSE);
822         gint i = 0;
823         
824         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
825         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
826
827         wizard->full_name = gtk_entry_new();
828         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), tmpl.name?tmpl.name:"");
829         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your name:</span>"), 
830                              wizard->full_name, i); i++;
831         
832         wizard->email = gtk_entry_new();
833         gtk_entry_set_text(GTK_ENTRY(wizard->email), tmpl.email?tmpl.email:"");
834         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your email address:</span>"), 
835                              wizard->email, i); i++;
836         
837         wizard->organization = gtk_entry_new();
838         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"),
839                              wizard->organization, i); i++;
840         gtk_entry_set_text(GTK_ENTRY(wizard->organization), tmpl.organization?tmpl.organization:"");
841         
842         g_signal_connect(G_OBJECT(wizard->email), "changed",
843                          G_CALLBACK(wizard_email_changed),
844                          wizard);
845         return table;
846 }
847
848 static GtkWidget* mailbox_page (WizardWindow * wizard)
849 {
850         GtkWidget *table = gtk_table_new(1,2, FALSE);
851         gint i = 0;
852         
853         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
854         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
855
856         wizard->mailbox_name = gtk_entry_new();
857         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), tmpl.mailbox?tmpl.mailbox:"");
858         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Mailbox name:</span>"), 
859                              wizard->mailbox_name, i); i++;
860         
861         return table;
862 }
863
864 static void smtp_auth_changed (GtkWidget *btn, gpointer data)
865 {
866         WizardWindow *wizard = (WizardWindow *)data;
867         gboolean do_auth = gtk_toggle_button_get_active(
868                 GTK_TOGGLE_BUTTON(wizard->smtp_auth));
869         gtk_widget_set_sensitive(wizard->smtp_username, do_auth);
870         gtk_widget_set_sensitive(wizard->smtp_username_label, do_auth);
871         gtk_widget_set_sensitive(wizard->smtp_password, do_auth);
872         gtk_widget_set_sensitive(wizard->smtp_password_label, do_auth);
873 }
874
875 static GtkWidget* smtp_page (WizardWindow * wizard)
876 {
877         GtkWidget *table = gtk_table_new(1,4, FALSE);
878         gchar *text;
879         gint i = 0;
880         
881         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
882         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
883
884         wizard->smtp_server = gtk_entry_new();
885         text = get_default_server(wizard, "smtp");
886         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
887         g_free(text);
888         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">SMTP server address:</span>"), 
889                              wizard->smtp_server, i); i++;
890         wizard->smtp_auth = gtk_check_button_new_with_label(
891                                         _("Use authentication"));
892         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth),
893                         tmpl.smtpauth);
894         g_signal_connect(G_OBJECT(wizard->smtp_auth), "toggled",
895                          G_CALLBACK(smtp_auth_changed),
896                          wizard);
897         gtk_table_attach(GTK_TABLE(table), wizard->smtp_auth,      
898                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
899
900         text = get_default_smtp_account(wizard);
901
902         wizard->smtp_username = gtk_entry_new();
903         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_username), text);
904         g_free(text);
905         wizard->smtp_username_label = gtk_label_new(_("SMTP username:\n"
906                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
907         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_username_label), TRUE);
908         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username_label,                               
909                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
910         if (GTK_IS_MISC(wizard->smtp_username_label))                                                 
911                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_username_label), 1, 0.5);        
912         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username,             
913                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
914         i++;
915         wizard->smtp_password = gtk_entry_new();
916         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_password), tmpl.smtppass?tmpl.smtppass:""); 
917         gtk_entry_set_visibility(GTK_ENTRY(wizard->smtp_password), FALSE);
918         wizard->smtp_password_label = gtk_label_new(_("SMTP password:\n"
919                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
920         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_password_label), TRUE);
921         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password_label,                               
922                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
923         if (GTK_IS_MISC(wizard->smtp_password_label))                                                 
924                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_password_label), 1, 0.5);        
925         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password,             
926                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
927         i++;
928         smtp_auth_changed(NULL, wizard);
929         return table;
930 }
931
932 static void wizard_protocol_change(WizardWindow *wizard, RecvProtocol protocol)
933 {
934         gchar *text;
935         
936         if (protocol == A_POP3) {
937                 text = get_default_server(wizard, "pop");
938                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
939                 gtk_widget_hide(wizard->recv_imap_label);
940                 gtk_widget_hide(wizard->recv_imap_subdir);
941                 gtk_widget_show(wizard->recv_username);
942                 gtk_widget_show(wizard->recv_password);
943                 gtk_widget_show(wizard->recv_username_label);
944                 gtk_widget_show(wizard->recv_password_label);
945                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
946                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
947                 g_free(text);
948         } else if (protocol == A_IMAP4) {
949                 text = get_default_server(wizard, "imap");
950                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
951                 gtk_widget_show(wizard->recv_imap_label);
952                 gtk_widget_show(wizard->recv_imap_subdir);
953                 gtk_widget_show(wizard->recv_username);
954                 gtk_widget_show(wizard->recv_password);
955                 gtk_widget_show(wizard->recv_username_label);
956                 gtk_widget_show(wizard->recv_password_label);
957                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
958                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
959                 g_free(text);
960         } else if (protocol == A_LOCAL) {
961                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
962                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
963                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
964                 gtk_widget_hide(wizard->recv_imap_label);
965                 gtk_widget_hide(wizard->recv_imap_subdir);
966                 gtk_widget_hide(wizard->recv_username);
967                 gtk_widget_hide(wizard->recv_password);
968                 gtk_widget_hide(wizard->recv_username_label);
969                 gtk_widget_hide(wizard->recv_password_label);
970         }
971 }
972
973 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
974 {
975         WizardWindow *wizard = (WizardWindow *)data;
976         RecvProtocol protocol;
977         protocol = GPOINTER_TO_INT
978                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
979
980         wizard_protocol_change(wizard, protocol);       
981 }
982
983 static GtkWidget* recv_page (WizardWindow * wizard)
984 {
985         GtkWidget *table = gtk_table_new(5,2, FALSE);
986         GtkWidget *menu = gtk_menu_new();
987         GtkWidget *menuitem;
988         gchar *text;
989         gint i = 0;
990         gint index = 0;
991
992         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
993         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
994
995         wizard->recv_type = gtk_option_menu_new();
996         
997         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
998         g_signal_connect(G_OBJECT(menuitem), "activate",
999                          G_CALLBACK(wizard_protocol_changed),
1000                          wizard);
1001 #ifdef HAVE_LIBETPAN
1002         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
1003         g_signal_connect(G_OBJECT(menuitem), "activate",
1004                          G_CALLBACK(wizard_protocol_changed),
1005                          wizard);
1006 #endif
1007         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
1008         g_signal_connect(G_OBJECT(menuitem), "activate",
1009                          G_CALLBACK(wizard_protocol_changed),
1010                          wizard);
1011
1012         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
1013         switch(tmpl.recvtype) {
1014         case A_POP3: 
1015                 index = 0;
1016                 break;
1017 #ifdef HAVE_LIBETPAN
1018         case A_IMAP4:
1019                 index = 1;
1020                 break;
1021         case A_LOCAL:
1022                 index = 2;
1023                 break;
1024 #else
1025         case A_LOCAL:
1026                 index = 1;
1027                 break;
1028 #endif
1029         default:
1030                 index = 0;
1031         }
1032         gtk_option_menu_set_history(GTK_OPTION_MENU (wizard->recv_type), index);
1033         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
1034                              wizard->recv_type, i); i++;
1035
1036         wizard->recv_server = gtk_entry_new();
1037         text = get_default_server(wizard, "pop");
1038         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1039         g_free(text);
1040         
1041         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
1042         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1043         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
1044                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1045         if (GTK_IS_MISC(wizard->recv_label))                                                  
1046                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
1047         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
1048                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1049         i++;
1050         
1051         wizard->recv_username = gtk_entry_new();
1052         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
1053         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
1054         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
1055                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1056         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
1057                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
1058         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
1059                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1060         i++;
1061         
1062         text = get_default_account(wizard);
1063         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
1064         g_free(text);
1065
1066         wizard->recv_password = gtk_entry_new();
1067         gtk_entry_set_text(GTK_ENTRY(wizard->recv_password), tmpl.recvpass?tmpl.recvpass:"");
1068         wizard->recv_password_label = gtk_label_new(_("Password:"));
1069         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
1070                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1071         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
1072                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
1073         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
1074                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1075         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
1076         i++;
1077         
1078         wizard->recv_imap_subdir = gtk_entry_new();
1079         gtk_entry_set_text(GTK_ENTRY(wizard->recv_imap_subdir), tmpl.imapdir?tmpl.imapdir:"");
1080         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
1081         
1082         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
1083                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1084         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
1085                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
1086         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
1087                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1088
1089         i++;
1090         
1091         return table;
1092 }
1093
1094 #ifdef USE_OPENSSL
1095 static GtkWidget* ssl_page (WizardWindow * wizard)
1096 {
1097         GtkWidget *table = gtk_table_new(2,2, FALSE);
1098         gint i = 0;
1099         
1100         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1101         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1102
1103         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
1104                                         _("Use SSL to connect to SMTP server"));
1105         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl),
1106                         tmpl.smtpssl);
1107         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
1108                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
1109         
1110         wizard->recv_use_ssl = gtk_check_button_new_with_label(
1111                                         _("Use SSL to connect to receiving server"));
1112         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl),
1113                         tmpl.recvssl);
1114         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
1115                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
1116         
1117         return table;
1118 }
1119 #endif
1120
1121 static void
1122 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
1123 {
1124         WizardWindow * wizard = (WizardWindow *)data;
1125         int current_page, num_pages;
1126         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
1127         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
1128         gint protocol = GPOINTER_TO_INT
1129                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1130         gboolean skip_mailbox_page = FALSE;
1131         
1132         if (protocol == A_IMAP4) {
1133                 skip_mailbox_page = TRUE;
1134         }
1135         
1136         num_pages = g_slist_length(wizard->pages);
1137
1138         current_page = gtk_notebook_get_current_page (
1139                                 GTK_NOTEBOOK(wizard->notebook));
1140         if (response == CANCEL)
1141         {
1142                 wizard->result = FALSE;
1143                 wizard->finished = TRUE;
1144                 gtk_widget_destroy (GTK_WIDGET(dialog));
1145         }
1146         else if (response == FINISHED)
1147         {
1148                 if (!wizard_write_config(wizard)) {
1149                         current_page = gtk_notebook_get_current_page (
1150                                         GTK_NOTEBOOK(wizard->notebook));
1151                         goto set_sens;
1152                 }
1153                 wizard->result = TRUE;
1154                 wizard->finished = TRUE;
1155                 gtk_widget_destroy (GTK_WIDGET(dialog));
1156         }
1157         else
1158         {
1159                 if (response == GO_BACK)
1160                 {
1161                         if (current_page > 0) {
1162                                 current_page--;
1163                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1164                                         /* mailbox */
1165                                         current_page--;
1166                                 }
1167                                 gtk_notebook_set_current_page (
1168                                         GTK_NOTEBOOK(wizard->notebook), 
1169                                         current_page);
1170                         }
1171                 }
1172                 else if (response == GO_FORWARD)
1173                 {
1174                         if (current_page < (num_pages-1)) {
1175                                 current_page++;
1176                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1177                                         /* mailbox */
1178                                         current_page++;
1179                                 }
1180                                 gtk_notebook_set_current_page (
1181                                         GTK_NOTEBOOK(wizard->notebook), 
1182                                         current_page);
1183                         }
1184                 }
1185 set_sens:
1186                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
1187                                 current_page > 0);
1188                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
1189                                 current_page < (num_pages - 1));
1190                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
1191                                 current_page == (num_pages - 1));
1192         }
1193 }
1194
1195 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
1196                                  gpointer data)
1197 {
1198         WizardWindow *wizard = (WizardWindow *)data;
1199         wizard->result = FALSE;
1200         wizard->finished = TRUE;
1201         
1202         return FALSE;
1203 }
1204
1205 #define PACK_WARNING(text) {                                            \
1206         label = gtk_label_new(text);                                    \
1207         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
1208         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
1209 }
1210
1211 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
1212         WizardWindow *wizard = g_new0(WizardWindow, 1);
1213         GtkWidget *page;
1214         GtkWidget *widget;
1215         GtkWidget *label;
1216         gchar     *text;
1217         GSList    *cur;
1218         gboolean   result;
1219         gint i = 0;
1220         wizard->mainwin = mainwin;
1221         wizard->create_mailbox = create_mailbox;
1222         
1223         gtk_widget_hide(mainwin->window);
1224         
1225         wizard_read_defaults();
1226         
1227         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
1228                         NULL, 0, 
1229                         GTK_STOCK_GO_BACK, GO_BACK,
1230                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
1231                         GTK_STOCK_SAVE, FINISHED,
1232                         GTK_STOCK_CANCEL, CANCEL,
1233                         NULL);
1234
1235         g_signal_connect(wizard->window, "response", 
1236                           G_CALLBACK(wizard_response_cb), wizard);
1237         gtk_widget_realize(wizard->window);
1238         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
1239                         GO_FORWARD);
1240         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1241                         GO_BACK, FALSE);
1242         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1243                         GO_FORWARD, TRUE);
1244         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1245                         FINISHED, FALSE);
1246         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1247                         CANCEL, TRUE);
1248         
1249         wizard->notebook = gtk_notebook_new();
1250         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
1251         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
1252         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
1253                             wizard->notebook, TRUE, TRUE, 0);
1254         
1255         wizard->pages = NULL;
1256         
1257 /*welcome page: 0 */
1258         WELCOME_PAGE = i;
1259         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
1260         
1261         wizard->pages = g_slist_append(wizard->pages, page);
1262         widget = stock_pixmap_widget(wizard->window, 
1263                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1264
1265         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1266         
1267         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
1268                           "We will begin by defining some basic "
1269                           "information about you and your most common "
1270                           "mail options so that you can start to use "
1271                           "Sylpheed-Claws in less than five minutes."));
1272         widget = gtk_label_new(text);
1273         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1274         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1275         g_free(text);
1276
1277 /* user page: 1 */
1278         i++;
1279         USER_PAGE = i;
1280         widget = create_page (wizard, _("About You"));
1281         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
1282         PACK_WARNING(_("Bold fields must be completed"));
1283         
1284         wizard->pages = g_slist_append(wizard->pages, widget);
1285
1286 /* recv+auth page: 2 */
1287         i++;
1288         RECV_PAGE = i;
1289         widget = create_page (wizard, _("Receiving mail"));
1290         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
1291         PACK_WARNING(_("Bold fields must be completed"));
1292         
1293         wizard->pages = g_slist_append(wizard->pages, widget);
1294
1295 /*smtp page: 3 */
1296         i++;
1297         SMTP_PAGE = i;
1298         widget = create_page (wizard, _("Sending mail"));
1299         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
1300         PACK_WARNING(_("Bold fields must be completed"));
1301         
1302         wizard->pages = g_slist_append(wizard->pages, widget);
1303
1304 /* mailbox page: 4 */
1305         if (create_mailbox) {
1306                 i++;
1307                 MAILBOX_PAGE = i;
1308                 widget = create_page (wizard, _("Saving mail on disk"));
1309                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
1310                 PACK_WARNING(_("Bold fields must be completed"));
1311         
1312                 wizard->pages = g_slist_append(wizard->pages, widget);
1313         }
1314 /* ssl page: 5 */
1315 #ifdef USE_OPENSSL
1316         i++;
1317         SSL_PAGE = i;
1318         widget = create_page (wizard, _("Security"));
1319         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
1320         PACK_WARNING(_("Bold fields must be completed"));
1321         
1322         wizard->pages = g_slist_append(wizard->pages, widget);
1323 #endif
1324
1325 /* done page: 6 */
1326         i++;
1327         DONE_PAGE = i;
1328         page = create_page(wizard, _("Configuration finished"));
1329         
1330         wizard->pages = g_slist_append(wizard->pages, page);
1331         widget = stock_pixmap_widget(wizard->window, 
1332                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1333
1334         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1335         
1336         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
1337                           "Click Save to start."));
1338         widget = gtk_label_new(text);
1339         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1340         g_free(text);
1341
1342
1343         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
1344                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
1345                                           GTK_WIDGET(cur->data), NULL);
1346         }
1347         
1348         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
1349                          G_CALLBACK(wizard_close_cb), wizard);
1350         gtk_widget_show_all (wizard->window);
1351
1352         gtk_widget_hide(wizard->recv_imap_label);
1353         gtk_widget_hide(wizard->recv_imap_subdir);
1354
1355         wizard_protocol_change(wizard, tmpl.recvtype);
1356
1357         while (!wizard->finished)
1358                 gtk_main_iteration();
1359
1360         result = wizard->result;
1361         
1362         GTK_EVENTS_FLUSH();
1363
1364         gtk_widget_show(mainwin->window);
1365         g_free(wizard);
1366
1367         return result;
1368 }