2007-03-12 [colin] 2.8.1cvs8
[claws.git] / src / wizard.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
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_checkbtn;
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         prefs_account->inbox = g_strdup_printf("#mh/%s/inbox",
689                         g_path_get_basename(gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name))));
690         prefs_account->local_inbox = g_strdup(prefs_account->inbox);
691
692         if (prefs_account->protocol != A_LOCAL)
693                 prefs_account->recv_server = g_strdup(recv_server);
694         else
695                 prefs_account->local_mbox = g_strdup(recv_server);
696
697         g_free(recv_server);
698         g_free(smtp_server);
699
700         prefs_account->userid = g_strdup(
701                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
702         prefs_account->passwd = g_strdup(
703                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
704
705         prefs_account->smtp_userid = g_strdup(
706                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_username)));
707         prefs_account->smtp_passwd = g_strdup(
708                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_password)));
709         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth))) {
710                 prefs_account->use_smtp_auth = TRUE;
711         }
712
713 #ifdef USE_OPENSSL
714         smtp_ssl_type = SSL_NONE;
715         recv_ssl_type = SSL_NONE;       
716
717         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl))) {
718                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_tls)))
719                         smtp_ssl_type = SSL_STARTTLS;
720                 else
721                         smtp_ssl_type = SSL_TUNNEL;
722         }
723         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
724                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_tls)))
725                         recv_ssl_type = SSL_STARTTLS;
726                 else
727                         recv_ssl_type = SSL_TUNNEL;
728         }
729
730         prefs_account->ssl_smtp = smtp_ssl_type;
731
732         if (prefs_account->protocol == A_IMAP4)
733                 prefs_account->ssl_imap = recv_ssl_type;
734         else
735                 prefs_account->ssl_pop = recv_ssl_type;
736
737 #endif
738         if (prefs_account->protocol == A_IMAP4) {
739                 gchar *directory = gtk_editable_get_chars(
740                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
741                 if (directory && strlen(directory)) {
742                         prefs_account->imap_dir = g_strdup(directory);
743                 }
744                 prefs_account->imap_subsonly = 
745                         gtk_toggle_button_get_active(
746                                 GTK_TOGGLE_BUTTON(wizard->subsonly_checkbtn));
747                 g_free(directory);
748         }
749
750         account_list = g_list_append(account_list, prefs_account);
751         prefs_account_write_config_all(account_list);
752         prefs_account_free(prefs_account);
753         account_read_config_all();
754
755         initialize_fonts(wizard);
756         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
757                 write_welcome_email(wizard);
758         
759 #ifndef G_OS_WIN32 
760         plugin_load_standard_plugins();
761 #endif
762         return TRUE;
763 }
764
765 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
766 {
767         GtkWidget *w;
768         GtkWidget *vbox;
769         GtkWidget *hbox;
770         GtkWidget *image;
771         char *title_string;
772
773         vbox = gtk_vbox_new (FALSE, 6);
774         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
775
776         /* create the titlebar */
777         hbox = gtk_hbox_new (FALSE, 12);
778         image = stock_pixmap_widget(wizard->window, 
779                                 STOCK_PIXMAP_CLAWS_MAIL_ICON);
780         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
781         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
782         w = gtk_label_new (title_string);
783         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
784         g_free (title_string);
785         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
786
787         /* pack the titlebar */
788         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
789
790         /* pack the separator */
791         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
792
793         /* pack space */
794         w = gtk_alignment_new (0, 0, 0, 0);
795         gtk_widget_set_size_request (w, 0, 6);
796         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
797
798         return vbox;
799 }
800
801 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
802         GtkWidget *label = gtk_label_new(text);                               \
803         gtk_table_attach(GTK_TABLE(table), label,                             \
804                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
805         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);                     \
806         if (GTK_IS_MISC(label))                                               \
807                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
808         gtk_table_attach(GTK_TABLE(table), entry,                             \
809                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
810 }
811
812 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
813 {
814         if (!strcmp(type, "smtp")) {
815                 if (!tmpl.smtpserver || !strlen(tmpl.smtpserver))
816                         return g_strconcat(type, ".", tmpl.domain, NULL);
817                 else 
818                         return g_strdup(tmpl.smtpserver);
819         } else {
820                 if (!tmpl.recvserver || !strlen(tmpl.recvserver))
821                         return g_strconcat(type, ".", tmpl.domain, NULL);
822                 else 
823                         return g_strdup(tmpl.recvserver);
824         }
825 }
826
827 static gchar *get_default_account(WizardWindow * wizard)
828 {
829         gchar *result = NULL;
830         
831         if (!tmpl.recvuser || !strlen(tmpl.recvuser)) {
832                 result = gtk_editable_get_chars(
833                                 GTK_EDITABLE(wizard->email), 0, -1);
834
835                 if (strstr(result, "@")) {
836                         *(strstr(result,"@")) = '\0';
837                 } 
838         } else {
839                 result = g_strdup(tmpl.recvuser);
840         }
841         return result;
842 }
843
844 static gchar *get_default_smtp_account(WizardWindow * wizard)
845 {
846         gchar *result = NULL;
847         
848         if (!tmpl.smtpuser || !strlen(tmpl.smtpuser)) {
849                 return g_strdup("");
850         } else {
851                 result = g_strdup(tmpl.smtpuser);
852         }
853         return result;
854 }
855
856 static void wizard_email_changed(GtkWidget *widget, gpointer data)
857 {
858         WizardWindow *wizard = (WizardWindow *)data;
859         RecvProtocol protocol;
860         gchar *text;
861         protocol = GPOINTER_TO_INT
862                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
863         
864         text = get_default_server(wizard, "smtp");
865         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
866         g_free(text);
867
868         text = get_default_account(wizard);
869         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
870         g_free(text);
871
872         if (protocol == A_POP3) {
873                 text = get_default_server(wizard, "pop");
874                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
875                 g_free(text);
876         } else if (protocol == A_IMAP4) {
877                 text = get_default_server(wizard, "imap");
878                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
879                 g_free(text);
880         } else if (protocol == A_LOCAL) {
881                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
882         }
883         
884 }
885
886 static GtkWidget* user_page (WizardWindow * wizard)
887 {
888         GtkWidget *table = gtk_table_new(3,2, FALSE);
889         gint i = 0;
890         
891         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
892         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
893
894         wizard->full_name = gtk_entry_new();
895         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), tmpl.name?tmpl.name:"");
896         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your name:</span>"), 
897                              wizard->full_name, i); i++;
898         
899         wizard->email = gtk_entry_new();
900         gtk_entry_set_text(GTK_ENTRY(wizard->email), tmpl.email?tmpl.email:"");
901         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Your email address:</span>"), 
902                              wizard->email, i); i++;
903         
904         wizard->organization = gtk_entry_new();
905         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"),
906                              wizard->organization, i); i++;
907         gtk_entry_set_text(GTK_ENTRY(wizard->organization), tmpl.organization?tmpl.organization:"");
908         
909         g_signal_connect(G_OBJECT(wizard->email), "changed",
910                          G_CALLBACK(wizard_email_changed),
911                          wizard);
912         return table;
913 }
914
915 static GtkWidget* mailbox_page (WizardWindow * wizard)
916 {
917         GtkWidget *table = gtk_table_new(1,2, FALSE);
918         gint i = 0;
919         GtkTooltips *tips = gtk_tooltips_new();
920
921         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
922         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
923
924         wizard->mailbox_name = gtk_entry_new();
925         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), tmpl.mailbox?tmpl.mailbox:"");
926
927         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), wizard->mailbox_name,
928                              _("You can also specify an absolute path, for example: "
929                                "\"/home/john/Documents/Mail\""),
930                              NULL);
931
932         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Mailbox name:</span>"), 
933                              wizard->mailbox_name, i); i++;
934         
935         return table;
936 }
937
938 static void smtp_auth_changed (GtkWidget *btn, gpointer data)
939 {
940         WizardWindow *wizard = (WizardWindow *)data;
941         gboolean do_auth = gtk_toggle_button_get_active(
942                 GTK_TOGGLE_BUTTON(wizard->smtp_auth));
943         gtk_widget_set_sensitive(wizard->smtp_username, do_auth);
944         gtk_widget_set_sensitive(wizard->smtp_username_label, do_auth);
945         gtk_widget_set_sensitive(wizard->smtp_password, do_auth);
946         gtk_widget_set_sensitive(wizard->smtp_password_label, do_auth);
947 }
948
949 static GtkWidget* smtp_page (WizardWindow * wizard)
950 {
951 #ifdef USE_OPENSSL
952         GtkWidget *table = gtk_table_new(6, 2, FALSE);
953 #else
954         GtkWidget *table = gtk_table_new(4, 2, FALSE);
955 #endif
956         GtkTooltips *tips = gtk_tooltips_new();
957         gchar *text;
958         gint i = 0;
959         
960         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
961         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
962
963         wizard->smtp_server = gtk_entry_new();
964         text = get_default_server(wizard, "smtp");
965         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
966         g_free(text);
967
968         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), wizard->smtp_server,
969                              _("You can specify the port number by appending it at the end: "
970                                "\"mail.example.com:25\""),
971                              NULL);
972
973         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">SMTP server address:</span>"), 
974                              wizard->smtp_server, i); i++;
975         wizard->smtp_auth = gtk_check_button_new_with_label(
976                                         _("Use authentication"));
977         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_auth),
978                         tmpl.smtpauth);
979         g_signal_connect(G_OBJECT(wizard->smtp_auth), "toggled",
980                          G_CALLBACK(smtp_auth_changed),
981                          wizard);
982         gtk_table_attach(GTK_TABLE(table), wizard->smtp_auth,      
983                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
984
985         text = get_default_smtp_account(wizard);
986
987         wizard->smtp_username = gtk_entry_new();
988         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_username), text);
989         g_free(text);
990         wizard->smtp_username_label = gtk_label_new(_("SMTP username:\n"
991                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
992         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_username_label), TRUE);
993         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username_label,                               
994                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
995         if (GTK_IS_MISC(wizard->smtp_username_label))                                                 
996                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_username_label), 1, 0.5);        
997         gtk_table_attach(GTK_TABLE(table), wizard->smtp_username,             
998                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
999         i++;
1000         wizard->smtp_password = gtk_entry_new();
1001         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_password), tmpl.smtppass?tmpl.smtppass:""); 
1002         gtk_entry_set_visibility(GTK_ENTRY(wizard->smtp_password), FALSE);
1003         wizard->smtp_password_label = gtk_label_new(_("SMTP password:\n"
1004                                         "<span size=\"small\">(empty to use the same as reception)</span>"));
1005         gtk_label_set_use_markup(GTK_LABEL(wizard->smtp_password_label), TRUE);
1006         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password_label,                               
1007                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1008         if (GTK_IS_MISC(wizard->smtp_password_label))                                                 
1009                 gtk_misc_set_alignment(GTK_MISC(wizard->smtp_password_label), 1, 0.5);        
1010         gtk_table_attach(GTK_TABLE(table), wizard->smtp_password,             
1011                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1012         i++;
1013 #ifdef USE_OPENSSL
1014         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
1015                                         _("Use SSL to connect to SMTP server"));
1016         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl),
1017                         tmpl.smtpssl != 0);
1018         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,     
1019                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); 
1020         i++;
1021         wizard->smtp_use_tls = gtk_check_button_new_with_label(
1022                                         _("Use SSL via STARTTLS"));
1023         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_tls),
1024                         tmpl.smtpssl == 2);
1025         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_tls,      
1026                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 16, 0); 
1027         i++;
1028         SET_TOGGLE_SENSITIVITY (wizard->smtp_use_ssl, wizard->smtp_use_tls);
1029 #endif
1030         smtp_auth_changed(NULL, wizard);
1031         return table;
1032 }
1033
1034 static void wizard_protocol_change(WizardWindow *wizard, RecvProtocol protocol)
1035 {
1036         gchar *text;
1037         
1038         if (protocol == A_POP3) {
1039                 text = get_default_server(wizard, "pop");
1040                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1041                 gtk_widget_hide(wizard->recv_imap_label);
1042                 gtk_widget_hide(wizard->recv_imap_subdir);
1043                 gtk_widget_hide(wizard->subsonly_checkbtn);
1044                 gtk_widget_show(wizard->recv_username);
1045                 gtk_widget_show(wizard->recv_password);
1046                 gtk_widget_show(wizard->recv_username_label);
1047                 gtk_widget_show(wizard->recv_password_label);
1048                 gtk_widget_hide(wizard->no_imap_warning);
1049 #ifdef USE_OPENSSL
1050                 gtk_widget_show(wizard->recv_use_ssl);
1051                 gtk_widget_show(wizard->recv_use_tls);
1052 #endif
1053                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
1054                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1055                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
1056                 g_free(text);
1057         } else if (protocol == A_IMAP4) {
1058 #ifdef HAVE_LIBETPAN
1059                 text = get_default_server(wizard, "imap");
1060                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1061                 gtk_widget_show(wizard->recv_imap_label);
1062                 gtk_widget_show(wizard->recv_imap_subdir);
1063                 gtk_widget_show(wizard->subsonly_checkbtn);
1064                 gtk_widget_show(wizard->recv_username);
1065                 gtk_widget_show(wizard->recv_password);
1066                 gtk_widget_show(wizard->recv_username_label);
1067                 gtk_widget_show(wizard->recv_password_label);
1068                 gtk_widget_hide(wizard->no_imap_warning);
1069 #ifdef USE_OPENSSL
1070                 gtk_widget_show(wizard->recv_use_ssl);
1071                 gtk_widget_show(wizard->recv_use_tls);
1072 #endif
1073                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Server address:</span>"));
1074                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1075                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
1076                 g_free(text);
1077 #else
1078                 gtk_widget_hide(wizard->recv_imap_label);
1079                 gtk_widget_hide(wizard->recv_imap_subdir);
1080                 gtk_widget_hide(wizard->subsonly_checkbtn);
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                 gtk_widget_show(wizard->no_imap_warning);
1086 #ifdef USE_OPENSSL
1087                 gtk_widget_hide(wizard->recv_use_ssl);
1088                 gtk_widget_hide(wizard->recv_use_tls);
1089 #endif
1090                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, FALSE);
1091 #endif
1092         } else if (protocol == A_LOCAL) {
1093                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), tmpl.mboxfile?tmpl.mboxfile:"");
1094                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("<span weight=\"bold\">Local mailbox:</span>"));
1095                 gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1096                 gtk_widget_hide(wizard->no_imap_warning);
1097                 gtk_widget_hide(wizard->recv_imap_label);
1098                 gtk_widget_hide(wizard->recv_imap_subdir);
1099                 gtk_widget_hide(wizard->subsonly_checkbtn);
1100                 gtk_widget_hide(wizard->recv_username);
1101                 gtk_widget_hide(wizard->recv_password);
1102                 gtk_widget_hide(wizard->recv_username_label);
1103                 gtk_widget_hide(wizard->recv_password_label);
1104 #ifdef USE_OPENSSL
1105                 gtk_widget_hide(wizard->recv_use_ssl);
1106                 gtk_widget_hide(wizard->recv_use_tls);
1107 #endif
1108                 gtk_dialog_set_response_sensitive (GTK_DIALOG(wizard->window), GO_FORWARD, TRUE);
1109         }
1110 }
1111
1112 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
1113 {
1114         WizardWindow *wizard = (WizardWindow *)data;
1115         RecvProtocol protocol;
1116         protocol = GPOINTER_TO_INT
1117                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1118
1119         wizard_protocol_change(wizard, protocol);       
1120 }
1121
1122 static GtkWidget* recv_page (WizardWindow * wizard)
1123 {
1124 #ifdef USE_OPENSSL
1125         GtkWidget *table = gtk_table_new(8,2, FALSE);
1126 #else
1127         GtkWidget *table = gtk_table_new(6,2, FALSE);
1128 #endif
1129         GtkWidget *menu = gtk_menu_new();
1130         GtkWidget *menuitem;
1131         GtkTooltips *tips = gtk_tooltips_new();
1132         gchar *text;
1133         gint i = 0;
1134         gint index = 0;
1135
1136         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1137         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1138
1139         wizard->recv_type = gtk_option_menu_new();
1140         
1141         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
1142         g_signal_connect(G_OBJECT(menuitem), "activate",
1143                          G_CALLBACK(wizard_protocol_changed),
1144                          wizard);
1145
1146         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
1147         g_signal_connect(G_OBJECT(menuitem), "activate",
1148                          G_CALLBACK(wizard_protocol_changed),
1149                          wizard);
1150
1151         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
1152         g_signal_connect(G_OBJECT(menuitem), "activate",
1153                          G_CALLBACK(wizard_protocol_changed),
1154                          wizard);
1155
1156         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
1157         switch(tmpl.recvtype) {
1158         case A_POP3: 
1159                 index = 0;
1160                 break;
1161         case A_IMAP4:
1162                 index = 1;
1163                 break;
1164         case A_LOCAL:
1165                 index = 2;
1166                 break;
1167         default:
1168                 index = 0;
1169         }
1170         gtk_option_menu_set_history(GTK_OPTION_MENU (wizard->recv_type), index);
1171         GTK_TABLE_ADD_ROW_AT(table, _("<span weight=\"bold\">Server type:</span>"), 
1172                              wizard->recv_type, i); i++;
1173
1174         wizard->recv_server = gtk_entry_new();
1175         text = get_default_server(wizard, "pop");
1176         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
1177         g_free(text);
1178         
1179         gtk_tooltips_set_tip(GTK_TOOLTIPS(tips), wizard->recv_server,
1180                              _("You can specify the port number by appending it at the end: "
1181                                "\"mail.example.com:110\""),
1182                              NULL);
1183
1184         wizard->recv_label = gtk_label_new(_("<span weight=\"bold\">Server address:</span>"));
1185         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_label), TRUE);
1186         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
1187                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1188         if (GTK_IS_MISC(wizard->recv_label))                                                  
1189                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
1190         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
1191                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1192         i++;
1193         
1194         wizard->recv_username = gtk_entry_new();
1195         wizard->recv_username_label = gtk_label_new(_("<span weight=\"bold\">Username:</span>"));
1196         gtk_label_set_use_markup(GTK_LABEL(wizard->recv_username_label), TRUE);
1197         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
1198                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1199         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
1200                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
1201         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
1202                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1203         i++;
1204         
1205         text = get_default_account(wizard);
1206         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
1207         g_free(text);
1208
1209         wizard->recv_password = gtk_entry_new();
1210         gtk_entry_set_text(GTK_ENTRY(wizard->recv_password), tmpl.recvpass?tmpl.recvpass:"");
1211         wizard->recv_password_label = gtk_label_new(_("Password:"));
1212         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
1213                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1214         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
1215                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
1216         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
1217                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1218         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
1219         i++;
1220         
1221         wizard->recv_imap_subdir = gtk_entry_new();
1222         gtk_entry_set_text(GTK_ENTRY(wizard->recv_imap_subdir), tmpl.imapdir?tmpl.imapdir:"");
1223         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
1224         
1225         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
1226                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1227         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
1228                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
1229         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
1230                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1231
1232         i++;
1233         
1234         wizard->subsonly_checkbtn = gtk_check_button_new_with_label(
1235                         _("Show only subscribed folders"));
1236         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->subsonly_checkbtn),
1237                         tmpl.subsonly);
1238         gtk_table_attach(GTK_TABLE(table), wizard->subsonly_checkbtn,                         
1239                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1240
1241         i++;
1242         
1243         
1244         wizard->no_imap_warning = gtk_label_new(_(
1245                           "<span weight=\"bold\">Warning: this version of Claws Mail\n"
1246                           "has been built without IMAP support.</span>"));
1247         gtk_label_set_use_markup(GTK_LABEL(wizard->no_imap_warning), TRUE);
1248         gtk_table_attach(GTK_TABLE(table), wizard->no_imap_warning,                           
1249                          0,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
1250 #ifdef USE_OPENSSL
1251         wizard->recv_use_ssl = gtk_check_button_new_with_label(
1252                                         _("Use SSL to connect to receiving server"));
1253         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl),
1254                         tmpl.recvssl != 0);
1255         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
1256                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
1257         i++;
1258         wizard->recv_use_tls = gtk_check_button_new_with_label(
1259                                         _("Use SSL via STARTTLS"));
1260         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(wizard->recv_use_tls),
1261                         tmpl.recvssl == 2);
1262         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_tls,      
1263                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 16, 0); 
1264         i++;
1265         SET_TOGGLE_SENSITIVITY (wizard->recv_use_ssl, wizard->recv_use_tls);
1266 #endif  
1267         return table;
1268 }
1269
1270 static void
1271 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
1272 {
1273         WizardWindow * wizard = (WizardWindow *)data;
1274         int current_page, num_pages;
1275         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
1276         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
1277         gint protocol = GPOINTER_TO_INT
1278                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
1279         gboolean skip_mailbox_page = FALSE;
1280         
1281         if (protocol == A_IMAP4) {
1282                 skip_mailbox_page = TRUE;
1283         }
1284         
1285         num_pages = g_slist_length(wizard->pages);
1286
1287         current_page = gtk_notebook_get_current_page (
1288                                 GTK_NOTEBOOK(wizard->notebook));
1289         if (response == CANCEL)
1290         {
1291                 wizard->result = FALSE;
1292                 wizard->finished = TRUE;
1293                 gtk_widget_destroy (GTK_WIDGET(dialog));
1294         }
1295         else if (response == FINISHED)
1296         {
1297                 if (!wizard_write_config(wizard)) {
1298                         current_page = gtk_notebook_get_current_page (
1299                                         GTK_NOTEBOOK(wizard->notebook));
1300                         goto set_sens;
1301                 }
1302                 wizard->result = TRUE;
1303                 wizard->finished = TRUE;
1304                 gtk_widget_destroy (GTK_WIDGET(dialog));
1305         }
1306         else
1307         {
1308                 if (response == GO_BACK)
1309                 {
1310                         if (current_page > 0) {
1311                                 current_page--;
1312                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1313                                         /* mailbox */
1314                                         current_page--;
1315                                 }
1316                                 gtk_notebook_set_current_page (
1317                                         GTK_NOTEBOOK(wizard->notebook), 
1318                                         current_page);
1319                         }
1320                 }
1321                 else if (response == GO_FORWARD)
1322                 {
1323                         if (current_page < (num_pages-1)) {
1324                                 current_page++;
1325                                 if (current_page == MAILBOX_PAGE && skip_mailbox_page) {
1326                                         /* mailbox */
1327                                         current_page++;
1328                                 }
1329                                 gtk_notebook_set_current_page (
1330                                         GTK_NOTEBOOK(wizard->notebook), 
1331                                         current_page);
1332                         }
1333                 }
1334 set_sens:
1335                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
1336                                 current_page > 0);
1337                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
1338                                 current_page < (num_pages - 1));
1339                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
1340                                 current_page == (num_pages - 1));
1341         }
1342 }
1343
1344 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
1345                                  gpointer data)
1346 {
1347         WizardWindow *wizard = (WizardWindow *)data;
1348         wizard->result = FALSE;
1349         wizard->finished = TRUE;
1350         
1351         return FALSE;
1352 }
1353
1354 #define PACK_WARNING(text) {                                            \
1355         label = gtk_label_new(text);                                    \
1356         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);                  \
1357         gtk_box_pack_end(GTK_BOX(widget), label, FALSE, FALSE, 0);      \
1358 }
1359
1360 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
1361         WizardWindow *wizard = g_new0(WizardWindow, 1);
1362         GtkWidget *page;
1363         GtkWidget *widget;
1364         GtkWidget *label;
1365         gchar     *text;
1366         GSList    *cur;
1367         gboolean   result;
1368         gint i = 0;
1369         wizard->mainwin = mainwin;
1370         wizard->create_mailbox = create_mailbox;
1371         
1372         gtk_widget_hide(mainwin->window);
1373         
1374         wizard_read_defaults();
1375         
1376         wizard->window = gtk_dialog_new_with_buttons (_("Claws Mail Setup Wizard"),
1377                         NULL, 0, 
1378                         GTK_STOCK_GO_BACK, GO_BACK,
1379                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
1380                         GTK_STOCK_SAVE, FINISHED,
1381                         GTK_STOCK_CANCEL, CANCEL,
1382                         NULL);
1383
1384         g_signal_connect(wizard->window, "response", 
1385                           G_CALLBACK(wizard_response_cb), wizard);
1386         gtk_widget_realize(wizard->window);
1387         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
1388                         GO_FORWARD);
1389         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1390                         GO_BACK, FALSE);
1391         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1392                         GO_FORWARD, TRUE);
1393         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1394                         FINISHED, FALSE);
1395         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
1396                         CANCEL, TRUE);
1397         
1398         wizard->notebook = gtk_notebook_new();
1399         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
1400         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
1401         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
1402                             wizard->notebook, TRUE, TRUE, 0);
1403         
1404         wizard->pages = NULL;
1405         
1406 /*welcome page: 0 */
1407         WELCOME_PAGE = i;
1408         page = create_page(wizard, _("Welcome to Claws Mail"));
1409         
1410         wizard->pages = g_slist_append(wizard->pages, page);
1411         widget = stock_pixmap_widget(wizard->window, 
1412                                 STOCK_PIXMAP_CLAWS_MAIL_LOGO);
1413
1414         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1415         
1416         text = g_strdup(_("Welcome to the Claws Mail setup wizard.\n\n"
1417                           "We will begin by defining some basic "
1418                           "information about you and your most common "
1419                           "mail options so that you can start to use "
1420                           "Claws Mail in less than five minutes."));
1421         widget = gtk_label_new(text);
1422         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
1423         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1424         g_free(text);
1425
1426 /* user page: 1 */
1427         i++;
1428         USER_PAGE = i;
1429         widget = create_page (wizard, _("About You"));
1430         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
1431         PACK_WARNING(_("Bold fields must be completed"));
1432         
1433         wizard->pages = g_slist_append(wizard->pages, widget);
1434
1435 /* recv+auth page: 2 */
1436         i++;
1437         RECV_PAGE = i;
1438         widget = create_page (wizard, _("Receiving mail"));
1439         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
1440         PACK_WARNING(_("Bold fields must be completed"));
1441         
1442         wizard->pages = g_slist_append(wizard->pages, widget);
1443
1444 /*smtp page: 3 */
1445         i++;
1446         SMTP_PAGE = i;
1447         widget = create_page (wizard, _("Sending mail"));
1448         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
1449         PACK_WARNING(_("Bold fields must be completed"));
1450         
1451         wizard->pages = g_slist_append(wizard->pages, widget);
1452
1453 /* mailbox page: 4 */
1454         if (create_mailbox) {
1455                 i++;
1456                 MAILBOX_PAGE = i;
1457                 widget = create_page (wizard, _("Saving mail on disk"));
1458                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
1459                 PACK_WARNING(_("Bold fields must be completed"));
1460         
1461                 wizard->pages = g_slist_append(wizard->pages, widget);
1462         }
1463
1464 /* done page: 6 */
1465         i++;
1466         DONE_PAGE = i;
1467         page = create_page(wizard, _("Configuration finished"));
1468         
1469         wizard->pages = g_slist_append(wizard->pages, page);
1470         widget = stock_pixmap_widget(wizard->window, 
1471                                 STOCK_PIXMAP_CLAWS_MAIL_LOGO);
1472
1473         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1474         
1475         text = g_strdup(_("Claws Mail is now ready.\n\n"
1476                           "Click Save to start."));
1477         widget = gtk_label_new(text);
1478         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
1479         g_free(text);
1480
1481
1482         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
1483                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
1484                                           GTK_WIDGET(cur->data), NULL);
1485         }
1486         
1487         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
1488                          G_CALLBACK(wizard_close_cb), wizard);
1489         gtk_widget_show_all (wizard->window);
1490
1491         gtk_widget_hide(wizard->recv_imap_label);
1492         gtk_widget_hide(wizard->recv_imap_subdir);
1493         gtk_widget_hide(wizard->subsonly_checkbtn);
1494
1495         wizard_protocol_change(wizard, tmpl.recvtype);
1496
1497         while (!wizard->finished)
1498                 gtk_main_iteration();
1499
1500         result = wizard->result;
1501         
1502         GTK_EVENTS_FLUSH();
1503
1504         gtk_widget_show(mainwin->window);
1505         g_free(wizard);
1506
1507         return result;
1508 }