48f488523bcd4c793c1d9790f42bdd1529ca6bc4
[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 "plugin.h"
53 #include "account.h"
54 #include "prefs_account.h"
55 #include "mainwindow.h"
56 #include "stock_pixmap.h"
57 #include "setup.h"
58 #include "folder.h"
59 #include "alertpanel.h"
60 #ifdef USE_OPENSSL                      
61 #include "ssl.h"
62 #endif
63 #include "prefs_common.h"
64
65 typedef enum
66 {
67         GO_BACK,
68         GO_FORWARD,
69         CANCEL,
70         FINISHED
71 } PageNavigation;
72
73 int WELCOME_PAGE = -1;
74 int USER_PAGE = -1;
75 int SMTP_PAGE = -1;
76 int RECV_PAGE = -1;
77 int MAILBOX_PAGE = -1;
78 int SSL_PAGE = -1;
79 int DONE_PAGE = -1;
80
81 typedef struct
82 {
83         GtkWidget *window;
84         GSList    *pages;
85         GtkWidget *notebook;
86
87         MainWindow *mainwin;
88         
89         GtkWidget *email;
90         GtkWidget *full_name;
91         GtkWidget *organization;
92
93         GtkWidget *mailbox_name;
94         
95         GtkWidget *smtp_server;
96         GtkWidget *smtp_auth;
97         GtkWidget *smtp_username;
98         GtkWidget *smtp_password;
99         GtkWidget *smtp_username_label;
100         GtkWidget *smtp_password_label;
101
102         GtkWidget *recv_type;
103         GtkWidget *recv_label;
104         GtkWidget *recv_server;
105         GtkWidget *recv_username;
106         GtkWidget *recv_password;
107         GtkWidget *recv_username_label;
108         GtkWidget *recv_password_label;
109         GtkWidget *recv_imap_label;
110         GtkWidget *recv_imap_subdir;
111
112 #ifdef USE_OPENSSL
113         GtkWidget *smtp_use_ssl;
114         GtkWidget *recv_use_ssl;
115 #endif
116         
117         gboolean create_mailbox;
118         gboolean finished;
119         gboolean result;
120
121 } WizardWindow;
122
123 typedef struct _AccountTemplate {
124         gchar *name;
125         gchar *domain;
126         gchar *email;
127         gchar *organization;
128         gchar *smtpserver;
129         gboolean smtpauth;
130         gchar *smtpuser;
131         gchar *smtppass;
132         RecvProtocol recvtype;
133         gchar *recvserver;
134         gchar *recvuser;
135         gchar *recvpass;
136         gchar *imapdir;
137         gchar *mboxfile;
138         gchar *mailbox;
139         gboolean smtpssl;
140         gboolean recvssl;
141 } AccountTemplate;
142
143 static AccountTemplate tmpl;
144
145 static PrefParam template_params[] = {
146         {"name", "$USERNAME",
147          &tmpl.name, P_STRING, NULL, NULL, NULL},
148         {"domain", "$DEFAULTDOMAIN",
149          &tmpl.domain, P_STRING, NULL, NULL, NULL},
150         {"email", "$NAME_MAIL@$DOMAIN",
151          &tmpl.email, P_STRING, NULL, NULL, NULL},
152         {"organization", "",
153          &tmpl.organization, P_STRING, NULL, NULL, NULL},
154         {"smtpserver", "smtp.$DOMAIN",
155          &tmpl.smtpserver, P_STRING, NULL, NULL, NULL},
156         {"smtpauth", "FALSE",
157          &tmpl.smtpauth, P_BOOL, NULL, NULL, NULL},
158         {"smtpuser", "",
159          &tmpl.smtpuser, P_STRING, NULL, NULL, NULL},
160         {"smtppass", "",
161          &tmpl.smtppass, P_STRING, NULL, NULL, NULL},
162         {"recvtype", A_POP3,
163          &tmpl.recvtype, P_INT, NULL, NULL, NULL},
164         {"recvserver", "pop.$DOMAIN",
165          &tmpl.recvserver, P_STRING, NULL, NULL, NULL},
166         {"recvuser", "$LOGIN",
167          &tmpl.recvuser, P_STRING, NULL, NULL, NULL},
168         {"recvpass", "",
169          &tmpl.recvpass, P_STRING, NULL, NULL, NULL},
170         {"imapdir", "",
171          &tmpl.imapdir, P_STRING, NULL, NULL, NULL},
172         {"mboxfile", "/var/mail/$LOGIN",
173          &tmpl.mboxfile, P_STRING, NULL, NULL, NULL},
174         {"mailbox", "Mail",
175          &tmpl.mailbox, P_STRING, NULL, NULL, NULL},
176         {"smtpssl", "FALSE",
177          &tmpl.smtpssl, P_BOOL, NULL, NULL, NULL},
178         {"recvssl", "FALSE",
179          &tmpl.recvssl, P_BOOL, NULL, NULL, NULL},
180         {NULL, NULL, NULL, P_INT, NULL, NULL, NULL}
181 };
182
183
184 static gchar *accountrc_tmpl =
185         "[AccountTemplate]\n"
186         "#you can use $DEFAULTDOMAIN here\n"
187         "#domain must be defined before the variables that use it\n"
188         "#by default, domain is extracted from the hostname\n"
189         "#domain=\n"
190         "\n"
191         "#you can use $USERNAME for name (this is the default)\n"
192         "#name=\n"
193         "\n"
194         "#you can use $LOGIN, $NAME_MAIL and $DOMAIN here \n"
195         "#$NAME_MAIL is the name without uppercase and with dots instead\n"
196         "#of spaces\n"
197         "#the default is $NAME_MAIL@$DOMAIN\n"
198         "#email=\n"
199         "\n"
200         "#you can use $DOMAIN here\n"
201         "#the default organization is empty\n"
202         "#organization=\n"
203         "\n"
204         "#you can use $DOMAIN here \n"
205         "#the default is stmp.$DOMAIN\n"
206         "#smtpserver=\n"
207         "\n"
208         "#Whether to use smtp authentication\n"
209         "#the default is 0 (no)\n"
210         "#smtpauth=\n"
211         "\n"
212         "#SMTP username\n"
213         "#you can use $LOGIN, $NAME_MAIL, $DOMAIN or $EMAIL here\n"
214         "#the default is empty (same as reception username)\n"
215         "#smtpuser=\n"
216         "\n"
217         "#SMTP password\n"
218         "#the default is empty (same as reception password)\n"
219         "#smtppass=\n"
220         "\n"
221         "#recvtype can be:\n"
222         "#0 for pop3\n"
223         "#3  for imap\n"
224         "#5  for a local mbox file\n"
225         "#recvtype=\n"
226         "\n"
227         "#you can use $DOMAIN here \n"
228         "#the default is {pop,imap}.$DOMAIN\n"
229         "#recvserver=\n"
230         "\n"
231         "#you can use $LOGIN, $NAME_MAIL, $DOMAIN or $EMAIL here\n"
232         "#default is $LOGIN\n"
233         "#recvuser=\n"
234         "\n"
235         "#default is empty\n"
236         "#recvpass=\n"
237         "\n"
238         "#imap dir if imap (relative to the home on the server\n"
239         "#default is empty\n"
240         "#imapdir=\n"
241         "\n"
242         "#mbox file if local\n"
243         "#you can use $LOGIN here\n"
244         "#default is /var/mail/$LOGIN\n"
245         "#mboxfile=\n"
246         "\n"
247         "#mailbox name if pop3 or local\n"
248         "#relative path from the user's home\n"
249         "#default is \"Mail\"\n"
250         "#mailbox=\n"
251         "\n"
252         "#whether to use ssl on STMP connections\n"
253         "#default is 0\n"
254         "#smtpssl=\n"
255         "\n"
256         "#whether to use ssl on pop or imap connections\n"
257         "#default is 0\n"
258         "#recvssl=\n";
259
260 static gchar *wizard_get_default_domain_name(void)
261 {
262         static gchar *domain_name = NULL;
263         
264         if (domain_name == NULL) {
265                 domain_name = g_strdup(get_domain_name());
266                 if (strchr(domain_name, '.') != strrchr(domain_name, '.')
267                 && strlen(strchr(domain_name, '.')) > 6) {
268                         gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
269                         g_free(domain_name);
270                         domain_name = tmp;
271                 }
272         }
273         return domain_name;
274 }
275
276 static gchar *get_name_for_mail(void)
277 {
278         gchar *name = g_strdup(tmpl.name);
279         if (name == NULL)
280                 return NULL;
281         g_strdown(name);
282         while(strstr(name, " "))
283                 *strstr(name, " ")='.';
284         
285         return name;
286 }
287
288 #define PARSE_DEFAULT(str) {    \
289         gchar *tmp = NULL, *new = NULL; \
290         if (str != NULL) {      \
291                 tmp = g_strdup(str);    \
292                 if (strstr(str, "$USERNAME")) { \
293                         tmp = g_strdup(str);    \
294                         *strstr(tmp, "$USERNAME") = '\0';       \
295                         new = g_strconcat(tmp, g_get_real_name(),       \
296                                 strstr(str, "$USERNAME")+strlen("$USERNAME"),   \
297                                 NULL);  \
298                         g_free(tmp);    \
299                         g_free(str);    \
300                         str = new;      \
301                         new = NULL;     \
302                 }       \
303                 if (strstr(str, "$LOGIN")) {    \
304                         tmp = g_strdup(str);    \
305                         *strstr(tmp, "$LOGIN") = '\0';  \
306                         new = g_strconcat(tmp, g_get_user_name(),       \
307                                 strstr(str, "$LOGIN")+strlen("$LOGIN"),         \
308                                 NULL);  \
309                         g_free(tmp);    \
310                         g_free(str);    \
311                         str = new;      \
312                         new = NULL;     \
313                 }       \
314                 if (strstr(str, "$EMAIL")) {    \
315                         tmp = g_strdup(str);    \
316                         *strstr(tmp, "$EMAIL") = '\0';  \
317                         new = g_strconcat(tmp, tmpl.email,      \
318                                 strstr(str, "$EMAIL")+strlen("$EMAIL"),         \
319                                 NULL);  \
320                         g_free(tmp);    \
321                         g_free(str);    \
322                         str = new;      \
323                         new = NULL;     \
324                 }       \
325                 if (strstr(str, "$NAME_MAIL")) {        \
326                         tmp = g_strdup(str);    \
327                         *strstr(tmp, "$NAME_MAIL") = '\0';      \
328                         new = g_strconcat(tmp, get_name_for_mail(),     \
329                                 strstr(str, "$NAME_MAIL")+strlen("$NAME_MAIL"),         \
330                                 NULL);  \
331                         g_free(tmp);    \
332                         g_free(str);    \
333                         str = new;      \
334                         new = NULL;     \
335                 }       \
336                 if (strstr(str, "$DEFAULTDOMAIN")) {    \
337                         tmp = g_strdup(str);    \
338                         *strstr(tmp, "$DEFAULTDOMAIN") = '\0';  \
339                         new = g_strconcat(tmp, wizard_get_default_domain_name(),        \
340                                 strstr(str, "$DEFAULTDOMAIN")+strlen("$DEFAULTDOMAIN"),         \
341                                 NULL);  \
342                         g_free(tmp);    \
343                         g_free(str);    \
344                         str = new;      \
345                         new = NULL;     \
346                 }       \
347                 if (strstr(str, "$DOMAIN")) {   \
348                         tmp = g_strdup(str);    \
349                         *strstr(tmp, "$DOMAIN") = '\0'; \
350                         new = g_strconcat(tmp, tmpl.domain,     \
351                                 strstr(str, "$DOMAIN")+strlen("$DOMAIN"),       \
352                                 NULL);  \
353                         g_free(tmp);    \
354                         g_free(str);    \
355                         str = new;      \
356                         new = NULL;     \
357                 }       \
358         }       \
359 }
360 static void wizard_read_defaults(void)
361 {
362         gchar *rcpath;
363
364         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "accountrc.tmpl", NULL);
365         if (!is_file_exist(rcpath)) {
366                 str_write_to_file(accountrc_tmpl, rcpath);
367         }
368
369         prefs_read_config(template_params, "AccountTemplate", rcpath, NULL);
370
371         PARSE_DEFAULT(tmpl.domain);
372         PARSE_DEFAULT(tmpl.name);
373         PARSE_DEFAULT(tmpl.email);
374         PARSE_DEFAULT(tmpl.organization);
375         PARSE_DEFAULT(tmpl.smtpserver);
376         PARSE_DEFAULT(tmpl.smtpuser);
377         PARSE_DEFAULT(tmpl.smtppass);
378         PARSE_DEFAULT(tmpl.recvserver);
379         PARSE_DEFAULT(tmpl.recvuser);
380         PARSE_DEFAULT(tmpl.recvpass);
381         PARSE_DEFAULT(tmpl.imapdir);
382         PARSE_DEFAULT(tmpl.mboxfile);
383         PARSE_DEFAULT(tmpl.mailbox);
384 /*
385         printf("defaults:"
386         "%s, %s, %s, %s, %s, %d, %s, %s, %s, %s, %s, %s, %d, %d\n",
387         tmpl.name,tmpl.domain,tmpl.email,tmpl.organization,tmpl.smtpserver,
388         tmpl.recvtype,tmpl.recvserver,tmpl.recvuser,tmpl.recvpass,
389         tmpl.imapdir,tmpl.mboxfile,tmpl.mailbox,tmpl.smtpssl,tmpl.recvssl);
390 */
391         g_free(rcpath);
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 #ifndef G_OS_WIN32 
696         plugin_load_standard_plugins();
697 #endif
698         return TRUE;
699 }
700
701 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
702 {
703         GtkWidget *w;
704         GtkWidget *vbox;
705         GtkWidget *hbox;
706         GtkWidget *image;
707         char *title_string;
708
709         vbox = gtk_vbox_new (FALSE, 6);
710         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
711
712         /* create the titlebar */
713         hbox = gtk_hbox_new (FALSE, 12);
714         image = stock_pixmap_widget(wizard->window, 
715                                 STOCK_PIXMAP_SYLPHEED_CLAWS_ICON);
716         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
717         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
718         w = gtk_label_new (title_string);
719         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
720         g_free (title_string);
721         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
722
723         /* pack the titlebar */
724         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
725
726         /* pack the separator */
727         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
728
729         /* pack space */
730         w = gtk_alignment_new (0, 0, 0, 0);
731         gtk_widget_set_size_request (w, 0, 6);
732         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
733
734         return vbox;
735 }
736
737 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
738         GtkWidget *label = gtk_label_new(text);                               \
739         gtk_table_attach(GTK_TABLE(table), label,                             \
740                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
741         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);                     \
742         if (GTK_IS_MISC(label))                                               \
743                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
744         gtk_table_attach(GTK_TABLE(table), entry,                             \
745                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
746 }
747
748 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
749 {
750         if (!strcmp(type, "smtp")) {
751                 if (!tmpl.smtpserver || !strlen(tmpl.smtpserver))
752                         return g_strconcat(type, ".", tmpl.domain, NULL);
753                 else 
754                         return g_strdup(tmpl.smtpserver);
755         } else {
756                 if (!tmpl.recvserver || !strlen(tmpl.recvserver))
757                         return g_strconcat(type, ".", tmpl.domain, NULL);
758                 else 
759                         return g_strdup(tmpl.recvserver);
760         }
761 }
762
763 static gchar *get_default_account(WizardWindow * wizard)
764 {
765         gchar *result = NULL;
766         
767         if (!tmpl.recvuser || !strlen(tmpl.recvuser)) {
768                 result = gtk_editable_get_chars(
769                                 GTK_EDITABLE(wizard->email), 0, -1);
770
771                 if (strstr(result, "@")) {
772                         *(strstr(result,"@")) = '\0';
773                 } 
774         } else {
775                 result = g_strdup(tmpl.recvuser);
776         }
777         return result;
778 }
779
780 static gchar *get_default_smtp_account(WizardWindow * wizard)
781 {
782         gchar *result = NULL;
783         
784         if (!tmpl.smtpuser || !strlen(tmpl.smtpuser)) {
785                 return g_strdup("");
786         } else {
787                 result = g_strdup(tmpl.smtpuser);
788         }
789         return result;
790 }
791
792 static void wizard_email_changed(GtkWidget *widget, gpointer data)
793 {
794         WizardWindow *wizard = (WizardWindow *)data;
795         RecvProtocol protocol;
796         gchar *text;
797         protocol = GPOINTER_TO_INT
798                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
799         
800         text = get_default_server(wizard, "smtp");
801         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
802         g_free(text);
803
804         text = get_default_account(wizard);
805         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
806         g_free(text);
807
808         if (protocol == A_POP3) {
809                 text = get_default_server(wizard, "pop");
810                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
811                 g_free(text);
812         } else if (protocol == A_IMAP4) {
813                 text = get_default_server(wizard, "imap");
814                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
815                 g_free(text);
816         } else if (protocol == A_LOCAL) {
817                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
818         }
819         
820 }
821
822 static GtkWidget* user_page (WizardWindow * wizard)
823 {
824         GtkWidget *table = gtk_table_new(3,2, FALSE);
825         gint i = 0;
826         
827         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
828         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
829
830         wizard->full_name = gtk_entry_new();
831         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), tmpl.name?tmpl.name:"");
832         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your name:</span>"), 
833                              wizard->full_name, i); i++;
834         
835         wizard->email = gtk_entry_new();
836         gtk_entry_set_text(GTK_ENTRY(wizard->email), tmpl.email?tmpl.email:"");
837         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your email address:</span>"), 
838                              wizard->email, i); i++;
839         
840         wizard->organization = gtk_entry_new();
841         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"),
842                              wizard->organization, i); i++;
843         gtk_entry_set_text(GTK_ENTRY(wizard->organization), tmpl.organization?tmpl.organization:"");
844         
845         g_signal_connect(G_OBJECT(wizard->email), "changed",
846                          G_CALLBACK(wizard_email_changed),
847                          wizard);
848         return table;
849 }
850
851 static GtkWidget* mailbox_page (WizardWindow * wizard)
852 {
853         GtkWidget *table = gtk_table_new(1,2, FALSE);
854         gint i = 0;
855         
856         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
857         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
858
859         wizard->mailbox_name = gtk_entry_new();
860         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), tmpl.mailbox?tmpl.mailbox:"");
861         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Mailbox name:</span>"), 
862                              wizard->mailbox_name, i); i++;
863         
864         return table;
865 }
866
867 static void smtp_auth_changed (GtkWidget *btn, gpointer data)
868 {
869         WizardWindow *wizard = (WizardWindow *)data;
870         gboolean do_auth = gtk_toggle_button_get_active(
871                 GTK_TOGGLE_BUTTON(wizard->smtp_auth));
872         gtk_widget_set_sensitive(wizard->smtp_username, do_auth);
873         gtk_widget_set_sensitive(wizard->smtp_username_label, do_auth);
874         gtk_widget_set_sensitive(wizard->smtp_password, do_auth);
875         gtk_widget_set_sensitive(wizard->smtp_password_label, do_auth);
876 }
877
878 static GtkWidget* smtp_page (WizardWindow * wizard)
879 {
880         GtkWidget *table = gtk_table_new(1,4, FALSE);
881         gchar *text;
882         gint i = 0;
883         
884         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
885         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
886
887         wizard->smtp_server = gtk_entry_new();
888         text = get_default_server(wizard, "smtp");
889         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
890         g_free(text);
891         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">SMTP server address:</span>"), 
892                              wizard->smtp_server, i); i++;
893         wizard->smtp_auth = gtk_check_button_new_with_label(
894                                         _("Use authentication"));
895         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth),
896                         tmpl.smtpauth);
897         g_signal_connect(G_OBJECT(wizard->smtp_auth), "toggled",
898                          G_CALLBACK(smtp_auth_changed),
899                          wizard);
900         gtk_table_attach(GTK_TABLE(table), wizard->smtp_auth,      
901                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
902
903         text = get_default_smtp_account(wizard);
904
905         wizard->smtp_username = gtk_entry_new();
906         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_username), text);
907         g_free(text);
908         wizard->smtp_username_label = gtk_label_new(_("SMTP username:\n"
909                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
910         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_username_label), TRUE);
911         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username_label,                               
912                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
913         if (GTK_IS_MISC(wizard->smtp_username_label))                                                 
914                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_username_label), 1, 0.5);        
915         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username,             
916                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
917         i++;
918         wizard->smtp_password = gtk_entry_new();
919         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_password), tmpl.smtppass?tmpl.smtppass:""); 
920         gtk_entry_set_visibility(GTK_ENTRY(wizard->smtp_password), FALSE);
921         wizard->smtp_password_label = gtk_label_new(_("SMTP password:\n"
922                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
923         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_password_label), TRUE);
924         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password_label,                               
925                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
926         if (GTK_IS_MISC(wizard->smtp_password_label))                                                 
927                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_password_label), 1, 0.5);        
928         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password,             
929                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
930         i++;
931         smtp_auth_changed(NULL, wizard);
932         return table;
933 }
934
935 static void wizard_protocol_change(WizardWindow *wizard, RecvProtocol protocol)
936 {
937         gchar *text;
938         
939         if (protocol == A_POP3) {
940                 text = get_default_server(wizard, "pop");
941                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
942                 gtk_widget_hide(wizard->recv_imap_label);
943                 gtk_widget_hide(wizard->recv_imap_subdir);
944                 gtk_widget_show(wizard->recv_username);
945                 gtk_widget_show(wizard->recv_password);
946                 gtk_widget_show(wizard->recv_username_label);
947                 gtk_widget_show(wizard->recv_password_label);
948                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
949                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
950                 g_free(text);
951         } else if (protocol == A_IMAP4) {
952                 text = get_default_server(wizard, "imap");
953                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
954                 gtk_widget_show(wizard->recv_imap_label);
955                 gtk_widget_show(wizard->recv_imap_subdir);
956                 gtk_widget_show(wizard->recv_username);
957                 gtk_widget_show(wizard->recv_password);
958                 gtk_widget_show(wizard->recv_username_label);
959                 gtk_widget_show(wizard->recv_password_label);
960                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
961                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
962                 g_free(text);
963         } else if (protocol == A_LOCAL) {
964                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
965                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
966                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
967                 gtk_widget_hide(wizard->recv_imap_label);
968                 gtk_widget_hide(wizard->recv_imap_subdir);
969                 gtk_widget_hide(wizard->recv_username);
970                 gtk_widget_hide(wizard->recv_password);
971                 gtk_widget_hide(wizard->recv_username_label);
972                 gtk_widget_hide(wizard->recv_password_label);
973         }
974 }
975
976 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
977 {
978         WizardWindow *wizard = (WizardWindow *)data;
979         RecvProtocol protocol;
980         protocol = GPOINTER_TO_INT
981                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
982
983         wizard_protocol_change(wizard, protocol);       
984 }
985
986 static GtkWidget* recv_page (WizardWindow * wizard)
987 {
988         GtkWidget *table = gtk_table_new(5,2, FALSE);
989         GtkWidget *menu = gtk_menu_new();
990         GtkWidget *menuitem;
991         gchar *text;
992         gint i = 0;
993         gint index = 0;
994
995         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
996         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
997
998         wizard->recv_type = gtk_option_menu_new();
999         
1000         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
1001         g_signal_connect(G_OBJECT(menuitem), "activate",
1002                          G_CALLBACK(wizard_protocol_changed),
1003                          wizard);
1004 #ifdef HAVE_LIBETPAN
1005         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
1006         g_signal_connect(G_OBJECT(menuitem), "activate",
1007                          G_CALLBACK(wizard_protocol_changed),
1008                          wizard);
1009 #endif
1010         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
1011         g_signal_connect(G_OBJECT(menuitem), "activate",
1012                          G_CALLBACK(wizard_protocol_changed),
1013                          wizard);
1014
1015         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
1016         switch(tmpl.recvtype) {
1017         case A_POP3: 
1018                 index = 0;
1019                 break;
1020 #ifdef HAVE_LIBETPAN
1021         case A_IMAP4:
1022                 index = 1;
1023                 break;
1024         case A_LOCAL:
1025                 index = 2;
1026                 break;
1027 #else
1028         case A_LOCAL:
1029                 index = 1;
1030                 break;
1031 #endif
1032         default:
1033                 index = 0;
1034         }
1035         gtk_option_menu_set_history(GTK_OPTION_MENU (wizard->recv_type), index);
1036         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
1037                              wizard->recv_type, i); i++;
1038
1039         wizard->recv_server = gtk_entry_new();
1040         text = get_default_server(wizard, "pop");
1041         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1042         g_free(text);
1043         
1044         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
1045         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1046         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
1047                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1048         if (GTK_IS_MISC(wizard->recv_label))                                                  
1049                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
1050         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
1051                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1052         i++;
1053         
1054         wizard->recv_username = gtk_entry_new();
1055         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
1056         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
1057         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
1058                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1059         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
1060                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
1061         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
1062                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1063         i++;
1064         
1065         text = get_default_account(wizard);
1066         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
1067         g_free(text);
1068
1069         wizard->recv_password = gtk_entry_new();
1070         gtk_entry_set_text(GTK_ENTRY(wizard->recv_password), tmpl.recvpass?tmpl.recvpass:"");
1071         wizard->recv_password_label = gtk_label_new(_("Password:"));
1072         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
1073                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1074         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
1075                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
1076         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
1077                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1078         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
1079         i++;
1080         
1081         wizard->recv_imap_subdir = gtk_entry_new();
1082         gtk_entry_set_text(GTK_ENTRY(wizard->recv_imap_subdir), tmpl.imapdir?tmpl.imapdir:"");
1083         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
1084         
1085         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
1086                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1087         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
1088                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
1089         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
1090                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1091
1092         i++;
1093         
1094         return table;
1095 }
1096
1097 #ifdef USE_OPENSSL
1098 static GtkWidget* ssl_page (WizardWindow * wizard)
1099 {
1100         GtkWidget *table = gtk_table_new(2,2, FALSE);
1101         gint i = 0;
1102         
1103         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1104         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1105
1106         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
1107                                         _("Use SSL to connect to SMTP server"));
1108         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl),
1109                         tmpl.smtpssl);
1110         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
1111                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
1112         
1113         wizard->recv_use_ssl = gtk_check_button_new_with_label(
1114                                         _("Use SSL to connect to receiving server"));
1115         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl),
1116                         tmpl.recvssl);
1117         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
1118                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
1119         
1120         return table;
1121 }
1122 #endif
1123
1124 static void
1125 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
1126 {
1127         WizardWindow * wizard = (WizardWindow *)data;
1128         int current_page, num_pages;
1129         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
1130         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
1131         gint protocol = GPOINTER_TO_INT
1132                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1133         gboolean skip_mailbox_page = FALSE;
1134         
1135         if (protocol == A_IMAP4) {
1136                 skip_mailbox_page = TRUE;
1137         }
1138         
1139         num_pages = g_slist_length(wizard->pages);
1140
1141         current_page = gtk_notebook_get_current_page (
1142                                 GTK_NOTEBOOK(wizard->notebook));
1143         if (response == CANCEL)
1144         {
1145                 wizard->result = FALSE;
1146                 wizard->finished = TRUE;
1147                 gtk_widget_destroy (GTK_WIDGET(dialog));
1148         }
1149         else if (response == FINISHED)
1150         {
1151                 if (!wizard_write_config(wizard)) {
1152                         current_page = gtk_notebook_get_current_page (
1153                                         GTK_NOTEBOOK(wizard->notebook));
1154                         goto set_sens;
1155                 }
1156                 wizard->result = TRUE;
1157                 wizard->finished = TRUE;
1158                 gtk_widget_destroy (GTK_WIDGET(dialog));
1159         }
1160         else
1161         {
1162                 if (response == GO_BACK)
1163                 {
1164                         if (current_page > 0) {
1165                                 current_page--;
1166                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1167                                         /* mailbox */
1168                                         current_page--;
1169                                 }
1170                                 gtk_notebook_set_current_page (
1171                                         GTK_NOTEBOOK(wizard->notebook), 
1172                                         current_page);
1173                         }
1174                 }
1175                 else if (response == GO_FORWARD)
1176                 {
1177                         if (current_page < (num_pages-1)) {
1178                                 current_page++;
1179                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1180                                         /* mailbox */
1181                                         current_page++;
1182                                 }
1183                                 gtk_notebook_set_current_page (
1184                                         GTK_NOTEBOOK(wizard->notebook), 
1185                                         current_page);
1186                         }
1187                 }
1188 set_sens:
1189                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
1190                                 current_page > 0);
1191                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
1192                                 current_page < (num_pages - 1));
1193                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
1194                                 current_page == (num_pages - 1));
1195         }
1196 }
1197
1198 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
1199                                  gpointer data)
1200 {
1201         WizardWindow *wizard = (WizardWindow *)data;
1202         wizard->result = FALSE;
1203         wizard->finished = TRUE;
1204         
1205         return FALSE;
1206 }
1207
1208 #define PACK_WARNING(text) {                                            \
1209         label = gtk_label_new(text);                                    \
1210         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
1211         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
1212 }
1213
1214 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
1215         WizardWindow *wizard = g_new0(WizardWindow, 1);
1216         GtkWidget *page;
1217         GtkWidget *widget;
1218         GtkWidget *label;
1219         gchar     *text;
1220         GSList    *cur;
1221         gboolean   result;
1222         gint i = 0;
1223         wizard->mainwin = mainwin;
1224         wizard->create_mailbox = create_mailbox;
1225         
1226         gtk_widget_hide(mainwin->window);
1227         
1228         wizard_read_defaults();
1229         
1230         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
1231                         NULL, 0, 
1232                         GTK_STOCK_GO_BACK, GO_BACK,
1233                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
1234                         GTK_STOCK_SAVE, FINISHED,
1235                         GTK_STOCK_CANCEL, CANCEL,
1236                         NULL);
1237
1238         g_signal_connect(wizard->window, "response", 
1239                           G_CALLBACK(wizard_response_cb), wizard);
1240         gtk_widget_realize(wizard->window);
1241         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
1242                         GO_FORWARD);
1243         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1244                         GO_BACK, FALSE);
1245         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1246                         GO_FORWARD, TRUE);
1247         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1248                         FINISHED, FALSE);
1249         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1250                         CANCEL, TRUE);
1251         
1252         wizard->notebook = gtk_notebook_new();
1253         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
1254         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
1255         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
1256                             wizard->notebook, TRUE, TRUE, 0);
1257         
1258         wizard->pages = NULL;
1259         
1260 /*welcome page: 0 */
1261         WELCOME_PAGE = i;
1262         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
1263         
1264         wizard->pages = g_slist_append(wizard->pages, page);
1265         widget = stock_pixmap_widget(wizard->window, 
1266                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1267
1268         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1269         
1270         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
1271                           "We will begin by defining some basic "
1272                           "information about you and your most common "
1273                           "mail options so that you can start to use "
1274                           "Sylpheed-Claws in less than five minutes."));
1275         widget = gtk_label_new(text);
1276         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1277         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1278         g_free(text);
1279
1280 /* user page: 1 */
1281         i++;
1282         USER_PAGE = i;
1283         widget = create_page (wizard, _("About You"));
1284         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
1285         PACK_WARNING(_("Bold fields must be completed"));
1286         
1287         wizard->pages = g_slist_append(wizard->pages, widget);
1288
1289 /* recv+auth page: 2 */
1290         i++;
1291         RECV_PAGE = i;
1292         widget = create_page (wizard, _("Receiving mail"));
1293         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
1294         PACK_WARNING(_("Bold fields must be completed"));
1295         
1296         wizard->pages = g_slist_append(wizard->pages, widget);
1297
1298 /*smtp page: 3 */
1299         i++;
1300         SMTP_PAGE = i;
1301         widget = create_page (wizard, _("Sending mail"));
1302         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
1303         PACK_WARNING(_("Bold fields must be completed"));
1304         
1305         wizard->pages = g_slist_append(wizard->pages, widget);
1306
1307 /* mailbox page: 4 */
1308         if (create_mailbox) {
1309                 i++;
1310                 MAILBOX_PAGE = i;
1311                 widget = create_page (wizard, _("Saving mail on disk"));
1312                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
1313                 PACK_WARNING(_("Bold fields must be completed"));
1314         
1315                 wizard->pages = g_slist_append(wizard->pages, widget);
1316         }
1317 /* ssl page: 5 */
1318 #ifdef USE_OPENSSL
1319         i++;
1320         SSL_PAGE = i;
1321         widget = create_page (wizard, _("Security"));
1322         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
1323         PACK_WARNING(_("Bold fields must be completed"));
1324         
1325         wizard->pages = g_slist_append(wizard->pages, widget);
1326 #endif
1327
1328 /* done page: 6 */
1329         i++;
1330         DONE_PAGE = i;
1331         page = create_page(wizard, _("Configuration finished"));
1332         
1333         wizard->pages = g_slist_append(wizard->pages, page);
1334         widget = stock_pixmap_widget(wizard->window, 
1335                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1336
1337         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1338         
1339         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
1340                           "Click Save to start."));
1341         widget = gtk_label_new(text);
1342         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1343         g_free(text);
1344
1345
1346         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
1347                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
1348                                           GTK_WIDGET(cur->data), NULL);
1349         }
1350         
1351         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
1352                          G_CALLBACK(wizard_close_cb), wizard);
1353         gtk_widget_show_all (wizard->window);
1354
1355         gtk_widget_hide(wizard->recv_imap_label);
1356         gtk_widget_hide(wizard->recv_imap_subdir);
1357
1358         wizard_protocol_change(wizard, tmpl.recvtype);
1359
1360         while (!wizard->finished)
1361                 gtk_main_iteration();
1362
1363         result = wizard->result;
1364         
1365         GTK_EVENTS_FLUSH();
1366
1367         gtk_widget_show(mainwin->window);
1368         g_free(wizard);
1369
1370         return result;
1371 }