2006-10-27 [colin] 2.5.6cvs10
[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         GtkWidget *no_imap_warning;
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_widget_hide(wizard->no_imap_warning);
949                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
950                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
951                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
952                 g_free(text);
953         } else if (protocol == A_IMAP4) {
954 #ifdef HAVE_LIBETPAN
955                 text = get_default_server(wizard, "imap");
956                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
957                 gtk_widget_show(wizard->recv_imap_label);
958                 gtk_widget_show(wizard->recv_imap_subdir);
959                 gtk_widget_show(wizard->recv_username);
960                 gtk_widget_show(wizard->recv_password);
961                 gtk_widget_show(wizard->recv_username_label);
962                 gtk_widget_show(wizard->recv_password_label);
963                 gtk_widget_hide(wizard->no_imap_warning);
964                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
965                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
966                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
967                 g_free(text);
968 #else
969                 gtk_widget_hide(wizard->recv_imap_label);
970                 gtk_widget_hide(wizard->recv_imap_subdir);
971                 gtk_widget_hide(wizard->recv_username);
972                 gtk_widget_hide(wizard->recv_password);
973                 gtk_widget_hide(wizard->recv_username_label);
974                 gtk_widget_hide(wizard->recv_password_label);
975                 gtk_widget_show(wizard->no_imap_warning);
976                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, FALSE);
977 #endif
978         } else if (protocol == A_LOCAL) {
979                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
980                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
981                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
982                 gtk_widget_hide(wizard->no_imap_warning);
983                 gtk_widget_hide(wizard->recv_imap_label);
984                 gtk_widget_hide(wizard->recv_imap_subdir);
985                 gtk_widget_hide(wizard->recv_username);
986                 gtk_widget_hide(wizard->recv_password);
987                 gtk_widget_hide(wizard->recv_username_label);
988                 gtk_widget_hide(wizard->recv_password_label);
989                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
990         }
991 }
992
993 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
994 {
995         WizardWindow *wizard = (WizardWindow *)data;
996         RecvProtocol protocol;
997         protocol = GPOINTER_TO_INT
998                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
999
1000         wizard_protocol_change(wizard, protocol);       
1001 }
1002
1003 static GtkWidget* recv_page (WizardWindow * wizard)
1004 {
1005         GtkWidget *table = gtk_table_new(5,2, FALSE);
1006         GtkWidget *menu = gtk_menu_new();
1007         GtkWidget *menuitem;
1008         gchar *text;
1009         gint i = 0;
1010         gint index = 0;
1011
1012         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1013         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1014
1015         wizard->recv_type = gtk_option_menu_new();
1016         
1017         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
1018         g_signal_connect(G_OBJECT(menuitem), "activate",
1019                          G_CALLBACK(wizard_protocol_changed),
1020                          wizard);
1021
1022         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
1023         g_signal_connect(G_OBJECT(menuitem), "activate",
1024                          G_CALLBACK(wizard_protocol_changed),
1025                          wizard);
1026
1027         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
1028         g_signal_connect(G_OBJECT(menuitem), "activate",
1029                          G_CALLBACK(wizard_protocol_changed),
1030                          wizard);
1031
1032         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
1033         switch(tmpl.recvtype) {
1034         case A_POP3: 
1035                 index = 0;
1036                 break;
1037         case A_IMAP4:
1038                 index = 1;
1039                 break;
1040         case A_LOCAL:
1041                 index = 2;
1042                 break;
1043         default:
1044                 index = 0;
1045         }
1046         gtk_option_menu_set_history(GTK_OPTION_MENU (wizard->recv_type), index);
1047         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
1048                              wizard->recv_type, i); i++;
1049
1050         wizard->recv_server = gtk_entry_new();
1051         text = get_default_server(wizard, "pop");
1052         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1053         g_free(text);
1054         
1055         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
1056         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1057         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
1058                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1059         if (GTK_IS_MISC(wizard->recv_label))                                                  
1060                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
1061         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
1062                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1063         i++;
1064         
1065         wizard->recv_username = gtk_entry_new();
1066         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
1067         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
1068         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
1069                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1070         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
1071                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
1072         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
1073                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1074         i++;
1075         
1076         text = get_default_account(wizard);
1077         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
1078         g_free(text);
1079
1080         wizard->recv_password = gtk_entry_new();
1081         gtk_entry_set_text(GTK_ENTRY(wizard->recv_password), tmpl.recvpass?tmpl.recvpass:"");
1082         wizard->recv_password_label = gtk_label_new(_("Password:"));
1083         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
1084                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1085         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
1086                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
1087         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
1088                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1089         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
1090         i++;
1091         
1092         wizard->recv_imap_subdir = gtk_entry_new();
1093         gtk_entry_set_text(GTK_ENTRY(wizard->recv_imap_subdir), tmpl.imapdir?tmpl.imapdir:"");
1094         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
1095         
1096         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
1097                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1098         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
1099                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
1100         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
1101                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1102
1103         i++;
1104         
1105         wizard->no_imap_warning = gtk_label_new(_(
1106                           "<span weight=\"bold\">Warning: this version of Sylpheed-Claws\n"
1107                           "has been built without IMAP support.</span>"));
1108         gtk_label_set_use_markup(GTK_LABEL(wizard->no_imap_warning), TRUE);
1109         gtk_table_attach(GTK_TABLE(table), wizard->no_imap_warning,                           
1110                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1111         
1112         return table;
1113 }
1114
1115 #ifdef USE_OPENSSL
1116 static GtkWidget* ssl_page (WizardWindow * wizard)
1117 {
1118         GtkWidget *table = gtk_table_new(2,2, FALSE);
1119         gint i = 0;
1120         
1121         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1122         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1123
1124         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
1125                                         _("Use SSL to connect to SMTP server"));
1126         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl),
1127                         tmpl.smtpssl);
1128         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
1129                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
1130         
1131         wizard->recv_use_ssl = gtk_check_button_new_with_label(
1132                                         _("Use SSL to connect to receiving server"));
1133         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl),
1134                         tmpl.recvssl);
1135         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
1136                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
1137         
1138         return table;
1139 }
1140 #endif
1141
1142 static void
1143 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
1144 {
1145         WizardWindow * wizard = (WizardWindow *)data;
1146         int current_page, num_pages;
1147         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
1148         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
1149         gint protocol = GPOINTER_TO_INT
1150                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1151         gboolean skip_mailbox_page = FALSE;
1152         
1153         if (protocol == A_IMAP4) {
1154                 skip_mailbox_page = TRUE;
1155         }
1156         
1157         num_pages = g_slist_length(wizard->pages);
1158
1159         current_page = gtk_notebook_get_current_page (
1160                                 GTK_NOTEBOOK(wizard->notebook));
1161         if (response == CANCEL)
1162         {
1163                 wizard->result = FALSE;
1164                 wizard->finished = TRUE;
1165                 gtk_widget_destroy (GTK_WIDGET(dialog));
1166         }
1167         else if (response == FINISHED)
1168         {
1169                 if (!wizard_write_config(wizard)) {
1170                         current_page = gtk_notebook_get_current_page (
1171                                         GTK_NOTEBOOK(wizard->notebook));
1172                         goto set_sens;
1173                 }
1174                 wizard->result = TRUE;
1175                 wizard->finished = TRUE;
1176                 gtk_widget_destroy (GTK_WIDGET(dialog));
1177         }
1178         else
1179         {
1180                 if (response == GO_BACK)
1181                 {
1182                         if (current_page > 0) {
1183                                 current_page--;
1184                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1185                                         /* mailbox */
1186                                         current_page--;
1187                                 }
1188                                 gtk_notebook_set_current_page (
1189                                         GTK_NOTEBOOK(wizard->notebook), 
1190                                         current_page);
1191                         }
1192                 }
1193                 else if (response == GO_FORWARD)
1194                 {
1195                         if (current_page < (num_pages-1)) {
1196                                 current_page++;
1197                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1198                                         /* mailbox */
1199                                         current_page++;
1200                                 }
1201                                 gtk_notebook_set_current_page (
1202                                         GTK_NOTEBOOK(wizard->notebook), 
1203                                         current_page);
1204                         }
1205                 }
1206 set_sens:
1207                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
1208                                 current_page > 0);
1209                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
1210                                 current_page < (num_pages - 1));
1211                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
1212                                 current_page == (num_pages - 1));
1213         }
1214 }
1215
1216 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
1217                                  gpointer data)
1218 {
1219         WizardWindow *wizard = (WizardWindow *)data;
1220         wizard->result = FALSE;
1221         wizard->finished = TRUE;
1222         
1223         return FALSE;
1224 }
1225
1226 #define PACK_WARNING(text) {                                            \
1227         label = gtk_label_new(text);                                    \
1228         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
1229         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
1230 }
1231
1232 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
1233         WizardWindow *wizard = g_new0(WizardWindow, 1);
1234         GtkWidget *page;
1235         GtkWidget *widget;
1236         GtkWidget *label;
1237         gchar     *text;
1238         GSList    *cur;
1239         gboolean   result;
1240         gint i = 0;
1241         wizard->mainwin = mainwin;
1242         wizard->create_mailbox = create_mailbox;
1243         
1244         gtk_widget_hide(mainwin->window);
1245         
1246         wizard_read_defaults();
1247         
1248         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
1249                         NULL, 0, 
1250                         GTK_STOCK_GO_BACK, GO_BACK,
1251                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
1252                         GTK_STOCK_SAVE, FINISHED,
1253                         GTK_STOCK_CANCEL, CANCEL,
1254                         NULL);
1255
1256         g_signal_connect(wizard->window, "response", 
1257                           G_CALLBACK(wizard_response_cb), wizard);
1258         gtk_widget_realize(wizard->window);
1259         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
1260                         GO_FORWARD);
1261         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1262                         GO_BACK, FALSE);
1263         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1264                         GO_FORWARD, TRUE);
1265         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1266                         FINISHED, FALSE);
1267         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1268                         CANCEL, TRUE);
1269         
1270         wizard->notebook = gtk_notebook_new();
1271         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
1272         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
1273         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
1274                             wizard->notebook, TRUE, TRUE, 0);
1275         
1276         wizard->pages = NULL;
1277         
1278 /*welcome page: 0 */
1279         WELCOME_PAGE = i;
1280         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
1281         
1282         wizard->pages = g_slist_append(wizard->pages, page);
1283         widget = stock_pixmap_widget(wizard->window, 
1284                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1285
1286         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1287         
1288         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
1289                           "We will begin by defining some basic "
1290                           "information about you and your most common "
1291                           "mail options so that you can start to use "
1292                           "Sylpheed-Claws in less than five minutes."));
1293         widget = gtk_label_new(text);
1294         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1295         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1296         g_free(text);
1297
1298 /* user page: 1 */
1299         i++;
1300         USER_PAGE = i;
1301         widget = create_page (wizard, _("About You"));
1302         gtk_box_pack_start (GTK_BOX(widget), user_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 /* recv+auth page: 2 */
1308         i++;
1309         RECV_PAGE = i;
1310         widget = create_page (wizard, _("Receiving mail"));
1311         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
1312         PACK_WARNING(_("Bold fields must be completed"));
1313         
1314         wizard->pages = g_slist_append(wizard->pages, widget);
1315
1316 /*smtp page: 3 */
1317         i++;
1318         SMTP_PAGE = i;
1319         widget = create_page (wizard, _("Sending mail"));
1320         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
1321         PACK_WARNING(_("Bold fields must be completed"));
1322         
1323         wizard->pages = g_slist_append(wizard->pages, widget);
1324
1325 /* mailbox page: 4 */
1326         if (create_mailbox) {
1327                 i++;
1328                 MAILBOX_PAGE = i;
1329                 widget = create_page (wizard, _("Saving mail on disk"));
1330                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
1331                 PACK_WARNING(_("Bold fields must be completed"));
1332         
1333                 wizard->pages = g_slist_append(wizard->pages, widget);
1334         }
1335 /* ssl page: 5 */
1336 #ifdef USE_OPENSSL
1337         i++;
1338         SSL_PAGE = i;
1339         widget = create_page (wizard, _("Security"));
1340         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
1341         PACK_WARNING(_("Bold fields must be completed"));
1342         
1343         wizard->pages = g_slist_append(wizard->pages, widget);
1344 #endif
1345
1346 /* done page: 6 */
1347         i++;
1348         DONE_PAGE = i;
1349         page = create_page(wizard, _("Configuration finished"));
1350         
1351         wizard->pages = g_slist_append(wizard->pages, page);
1352         widget = stock_pixmap_widget(wizard->window, 
1353                                 STOCK_PIXMAP_SYLPHEED_CLAWS_LOGO);
1354
1355         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1356         
1357         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
1358                           "Click Save to start."));
1359         widget = gtk_label_new(text);
1360         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1361         g_free(text);
1362
1363
1364         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
1365                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
1366                                           GTK_WIDGET(cur->data), NULL);
1367         }
1368         
1369         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
1370                          G_CALLBACK(wizard_close_cb), wizard);
1371         gtk_widget_show_all (wizard->window);
1372
1373         gtk_widget_hide(wizard->recv_imap_label);
1374         gtk_widget_hide(wizard->recv_imap_subdir);
1375
1376         wizard_protocol_change(wizard, tmpl.recvtype);
1377
1378         while (!wizard->finished)
1379                 gtk_main_iteration();
1380
1381         result = wizard->result;
1382         
1383         GTK_EVENTS_FLUSH();
1384
1385         gtk_widget_show(mainwin->window);
1386         g_free(wizard);
1387
1388         return result;
1389 }