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