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