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