2005-11-10 [wwp] 1.9.100cvs6
[claws.git] / src / wizard.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 "account.h"
53 #include "prefs_account.h"
54 #include "mainwindow.h"
55 #include "stock_pixmap.h"
56 #include "setup.h"
57 #include "folder.h"
58 #ifdef USE_OPENSSL                      
59 #include "ssl.h"
60 #endif
61 #include "prefs_common.h"
62
63 typedef enum
64 {
65         GO_BACK,
66         GO_FORWARD,
67         CANCEL,
68         FINISHED
69 } PageNavigation;
70
71 typedef struct
72 {
73         GtkWidget *window;
74         GSList    *pages;
75         GtkWidget *notebook;
76
77         MainWindow *mainwin;
78         
79         GtkWidget *email;
80         GtkWidget *full_name;
81         GtkWidget *organization;
82
83         GtkWidget *mailbox_name;
84         
85         GtkWidget *smtp_server;
86
87         GtkWidget *recv_type;
88         GtkWidget *recv_label;
89         GtkWidget *recv_server;
90         GtkWidget *recv_username;
91         GtkWidget *recv_password;
92         GtkWidget *recv_username_label;
93         GtkWidget *recv_password_label;
94         GtkWidget *recv_imap_label;
95         GtkWidget *recv_imap_subdir;
96
97 #ifdef USE_OPENSSL
98         GtkWidget *smtp_use_ssl;
99         GtkWidget *recv_use_ssl;
100 #endif
101         
102         gboolean create_mailbox;
103         gboolean finished;
104         gboolean result;
105
106 } WizardWindow;
107
108 static void initialize_fonts(WizardWindow *wizard)
109 {
110         GtkWidget *widget = wizard->email;
111         gint size = pango_font_description_get_size(
112                         widget->style->font_desc)
113                       /PANGO_SCALE;
114         gchar *tmp, *new;
115         
116         tmp = g_strdup(prefs_common.textfont);
117         if (strrchr(tmp, ' ')) {
118                 *(strrchr(tmp, ' ')) = '\0';
119                 new = g_strdup_printf("%s %d", tmp, size);
120                 g_free(prefs_common.textfont);
121                 prefs_common.textfont = new;
122         }
123         g_free(tmp);
124         
125         tmp = g_strdup(prefs_common.smallfont);
126         if (strrchr(tmp, ' ')) {
127                 *(strrchr(tmp, ' ')) = '\0';
128                 new = g_strdup_printf("%s %d", tmp, size);
129                 g_free(prefs_common.smallfont);
130                 prefs_common.smallfont = new;
131         }
132         g_free(tmp);
133         
134         tmp = g_strdup(prefs_common.normalfont);
135         if (strrchr(tmp, ' ')) {
136                 *(strrchr(tmp, ' ')) = '\0';
137                 new = g_strdup_printf("%s %d", tmp, size);
138                 g_free(prefs_common.normalfont);
139                 prefs_common.normalfont = new;
140         }
141         g_free(tmp);
142 }
143
144 #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"
145
146 static void write_welcome_email(WizardWindow *wizard)
147 {
148         gchar buf_date[64];
149         gchar *head=NULL;
150         gchar *body=NULL;
151         gchar *msg=NULL;
152         gchar *subj=NULL;
153         const gchar *mailbox = gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name));
154         Folder *folder = folder_find_from_path(mailbox);
155         FolderItem *inbox = folder ? folder->inbox:NULL;
156         gchar *file = get_tmp_file();
157         
158         get_rfc822_date(buf_date, sizeof(buf_date));
159
160         subj = g_strdup_printf(_("Welcome to Sylpheed-Claws"));
161
162         head = g_strdup_printf(
163                 "From: %s <sylpheed-claws-users@lists.sf.net>\n"
164                 "To: %s <%s>\n"
165                 "Date: %s\n"
166                 "Subject: %s\n"
167                 "X-Face: %s\n"
168                 "Content-Type: text/plain; charset=UTF-8\n",
169                 _("Sylpheed-Claws Team"),
170                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)),
171                 gtk_entry_get_text(GTK_ENTRY(wizard->email)),
172                 buf_date, subj, XFACE);
173         body = g_strdup_printf(
174                 _("\n"
175                 "Welcome to Sylpheed-Claws\n"
176                 "-------------------------\n"
177                 "\n"
178                 "Now that you have set up your account you can fetch your\n"
179                 "mail by clicking the 'Get Mail' button at the left of the\n"
180                 "toolbar.\n"
181                 "\n"
182                 "You can change your Account Preferences by using the menu\n"
183                 "entry '/Configuration/Preferences for current account'\n"
184                 "and change the general Preferences by using\n"
185                 "'/Configuration/Preferences'.\n"
186                 "\n"
187                 "You can find further information in the Sylpheed-Claws manual,\n"
188                 "which can be accessed by using the menu entry '/Help/Manual'\n"
189                 "or online at the URL given below.\n"
190                 "\n"
191                 "Useful URLs\n"
192                 "-----------\n"
193                 "Homepage:      <%s>\n"
194                 "Manual:        <%s>\n"
195                 "FAQ:          <%s>\n"
196                 "Themes:        <%s>\n"
197                 "Mailing Lists: <%s>\n"
198                 "\n"
199                 "LICENSE\n"
200                 "-------\n"
201                 "Sylpheed-Claws is free software, released under the terms\n"
202                 "of the GNU General Public License, version 2 or later, as\n"
203                 "published by the Free Software Foundation, 51 Franklin Street,\n"
204                 "Fifth Floor, Boston, MA 02110-1301, USA. The license can be\n"
205                 "found at <%s>.\n"
206                 "\n"
207                 "DONATIONS\n"
208                 "---------\n"
209                 "If you wish to donate to the Sylpheed-Claws project you can do\n"
210                 "so at <%s>.\n\n"),
211                 HOMEPAGE_URI, MANUAL_URI, FAQ_URI, THEMES_URI, MAILING_LIST_URI,
212                 GPL_URI, DONATE_URI);
213         
214         msg = g_strconcat(head, body, NULL);
215
216         if (inbox && inbox->total_msgs == 0
217          && str_write_to_file(msg, file) >= 0) {
218                 MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
219                 folder_item_add_msg(inbox, file, &flags, FALSE);
220         }
221         g_free(subj);
222         g_free(head);
223         g_free(body);
224         g_free(msg);
225         g_unlink(file);
226 }
227 #undef XFACE
228
229 static gboolean wizard_write_config(WizardWindow *wizard)
230 {
231         gboolean mailbox_ok = FALSE;
232         PrefsAccount *prefs_account = prefs_account_new();
233         GList *account_list = NULL;
234         GtkWidget *menu, *menuitem;
235         
236         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
237         menuitem = gtk_menu_get_active(GTK_MENU(menu));
238         prefs_account->protocol = GPOINTER_TO_INT
239                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
240         
241         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4) {
242                 mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
243                                 gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)));
244         } else
245                 mailbox_ok = TRUE;
246
247         if (!mailbox_ok) {
248                 gtk_notebook_set_current_page (
249                         GTK_NOTEBOOK(wizard->notebook), 
250                         4);
251                 return FALSE;
252         }
253         
254         if (prefs_account->protocol != A_LOCAL)
255                 prefs_account->account_name = g_strdup_printf("%s@%s",
256                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
257                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
258         else
259                 prefs_account->account_name = g_strdup_printf("%s",
260                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
261
262         prefs_account->name = g_strdup(
263                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
264         prefs_account->address = g_strdup(
265                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
266         prefs_account->organization = g_strdup(
267                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
268         prefs_account->smtp_server = g_strdup(
269                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
270
271         if (prefs_account->protocol != A_LOCAL)
272                 prefs_account->recv_server = g_strdup(
273                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
274         else
275                 prefs_account->local_mbox = g_strdup(
276                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
277
278         prefs_account->userid = g_strdup(
279                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
280         prefs_account->passwd = g_strdup(
281                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
282
283 #ifdef USE_OPENSSL                      
284         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
285                 prefs_account->ssl_smtp = SSL_TUNNEL;
286         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
287                 if (prefs_account->protocol == A_IMAP4)
288                         prefs_account->ssl_imap = SSL_TUNNEL;
289                 else
290                         prefs_account->ssl_pop = SSL_TUNNEL;
291         }
292 #endif
293         if (prefs_account->protocol == A_IMAP4) {
294                 gchar *directory = gtk_editable_get_chars(
295                         GTK_EDITABLE(wizard->recv_imap_subdir), 0, -1);
296                 if (directory && strlen(directory)) {
297                         prefs_account->imap_dir = g_strdup(directory);
298                 }
299                 g_free(directory);
300         }
301
302         account_list = g_list_append(account_list, prefs_account);
303         prefs_account_write_config_all(account_list);
304         prefs_account_free(prefs_account);
305         account_read_config_all();
306
307         initialize_fonts(wizard);
308         if (wizard->create_mailbox && prefs_account->protocol != A_IMAP4)
309                 write_welcome_email(wizard);
310         
311         return TRUE;
312 }
313
314 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
315 {
316         GtkWidget *w;
317         GtkWidget *vbox;
318         GtkWidget *hbox;
319         GtkWidget *image;
320         char *title_string;
321
322         vbox = gtk_vbox_new (FALSE, 6);
323         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
324
325         /* create the titlebar */
326         hbox = gtk_hbox_new (FALSE, 12);
327         image = stock_pixmap_widget(wizard->window, 
328                                 STOCK_PIXMAP_SYLPHEED_ICON);
329         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
330         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
331         w = gtk_label_new (title_string);
332         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
333         g_free (title_string);
334         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
335
336         /* pack the titlebar */
337         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
338
339         /* pack the separator */
340         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
341
342         /* pack space */
343         w = gtk_alignment_new (0, 0, 0, 0);
344         gtk_widget_set_size_request (w, 0, 6);
345         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
346
347         return vbox;
348 }
349
350 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
351         GtkWidget *label = gtk_label_new(text);                               \
352         gtk_table_attach(GTK_TABLE(table), label,                             \
353                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
354         if (GTK_IS_MISC(label))                                               \
355                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
356         gtk_table_attach(GTK_TABLE(table), entry,                             \
357                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
358 }
359
360 static gchar *get_default_email_addr(void)
361 {
362         gchar *domain_name = g_strdup(get_domain_name());
363         gchar *result;
364         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
365         && strlen(strchr(domain_name, '.')) > 6) {
366                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
367                 g_free(domain_name);
368                 domain_name = tmp;
369         }
370         result = g_strdup_printf("%s@%s",
371                                 g_get_user_name(),
372                                 domain_name);
373         g_free(domain_name);
374         return result;
375 }
376
377 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
378 {
379         gchar *domain_name = NULL;
380         gchar *tmp = gtk_editable_get_chars(
381                         GTK_EDITABLE(wizard->email), 0, -1);
382         gchar *result;
383         
384         if (strstr(tmp, "@")) {
385                 domain_name = g_strdup(strstr(tmp,"@")+1);
386         } else {
387                 domain_name = g_strdup(get_domain_name());
388         }
389         
390         g_free(tmp);
391
392         result = g_strdup_printf("%s.%s",
393                                 type, domain_name);
394         g_free(domain_name);
395         return result;
396 }
397
398 static gchar *get_default_account(WizardWindow * wizard)
399 {
400         gchar *result = gtk_editable_get_chars(
401                         GTK_EDITABLE(wizard->email), 0, -1);
402         
403         if (strstr(result, "@")) {
404                 *(strstr(result,"@")) = '\0';
405         } 
406
407         return result;
408 }
409
410 static void wizard_email_changed(GtkWidget *widget, gpointer data)
411 {
412         WizardWindow *wizard = (WizardWindow *)data;
413         RecvProtocol protocol;
414         gchar *text;
415         protocol = GPOINTER_TO_INT
416                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
417         
418         text = get_default_server(wizard, "smtp");
419         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
420         g_free(text);
421
422         text = get_default_account(wizard);
423         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
424         g_free(text);
425
426         if (protocol == A_POP3) {
427                 text = get_default_server(wizard, "pop");
428                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
429                 g_free(text);
430         } else if (protocol == A_IMAP4) {
431                 text = get_default_server(wizard, "imap");
432                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
433                 g_free(text);
434         } else if (protocol == A_LOCAL) {
435                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
436                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
437                 g_free(mbox);
438         }
439         
440 }
441
442 static GtkWidget* user_page (WizardWindow * wizard)
443 {
444         GtkWidget *table = gtk_table_new(3,2, FALSE);
445         gchar *text;
446         gint i = 0;
447         
448         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
449         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
450
451         wizard->full_name = gtk_entry_new();
452         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), g_get_real_name());
453         GTK_TABLE_ADD_ROW_AT(table, _("Your name:"), 
454                              wizard->full_name, i); i++;
455         
456         wizard->email = gtk_entry_new();
457         text = get_default_email_addr();
458         gtk_entry_set_text(GTK_ENTRY(wizard->email), text);
459         g_free(text);
460         GTK_TABLE_ADD_ROW_AT(table, _("Your email address:"), 
461                              wizard->email, i); i++;
462         
463         wizard->organization = gtk_entry_new();
464         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"), 
465                              wizard->organization, i); i++;
466         
467         g_signal_connect(G_OBJECT(wizard->email), "changed",
468                          G_CALLBACK(wizard_email_changed),
469                          wizard);
470         return table;
471 }
472
473 static GtkWidget* mailbox_page (WizardWindow * wizard)
474 {
475         GtkWidget *table = gtk_table_new(1,2, FALSE);
476         gint i = 0;
477         
478         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
479         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
480
481         wizard->mailbox_name = gtk_entry_new();
482         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), "Mail");
483         GTK_TABLE_ADD_ROW_AT(table, _("Mailbox name:"), 
484                              wizard->mailbox_name, i); i++;
485         
486         return table;
487 }
488
489 static GtkWidget* smtp_page (WizardWindow * wizard)
490 {
491         GtkWidget *table = gtk_table_new(1,2, FALSE);
492         gchar *text;
493         gint i = 0;
494         
495         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
496         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
497
498         wizard->smtp_server = gtk_entry_new();
499         text = get_default_server(wizard, "smtp");
500         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
501         g_free(text);
502         GTK_TABLE_ADD_ROW_AT(table, _("SMTP server address:"), 
503                              wizard->smtp_server, i); i++;
504         return table;
505 }
506
507 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
508 {
509         WizardWindow *wizard = (WizardWindow *)data;
510         RecvProtocol protocol;
511         gchar *text;
512         protocol = GPOINTER_TO_INT
513                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
514         
515         if (protocol == A_POP3) {
516                 text = get_default_server(wizard, "pop");
517                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
518                 gtk_widget_hide(wizard->recv_imap_label);
519                 gtk_widget_hide(wizard->recv_imap_subdir);
520                 gtk_widget_show(wizard->recv_username);
521                 gtk_widget_show(wizard->recv_password);
522                 gtk_widget_show(wizard->recv_username_label);
523                 gtk_widget_show(wizard->recv_password_label);
524                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
525                 g_free(text);
526         } else if (protocol == A_IMAP4) {
527                 text = get_default_server(wizard, "imap");
528                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
529                 gtk_widget_show(wizard->recv_imap_label);
530                 gtk_widget_show(wizard->recv_imap_subdir);
531                 gtk_widget_show(wizard->recv_username);
532                 gtk_widget_show(wizard->recv_password);
533                 gtk_widget_show(wizard->recv_username_label);
534                 gtk_widget_show(wizard->recv_password_label);
535                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Server address:"));
536                 g_free(text);
537         } else if (protocol == A_LOCAL) {
538                 gchar *mbox = g_strdup_printf("/var/mail/%s", g_get_user_name());
539                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), mbox);
540                 g_free(mbox);
541                 gtk_label_set_text(GTK_LABEL(wizard->recv_label), _("Local mailbox:"));
542                 gtk_widget_hide(wizard->recv_imap_label);
543                 gtk_widget_hide(wizard->recv_imap_subdir);
544                 gtk_widget_hide(wizard->recv_username);
545                 gtk_widget_hide(wizard->recv_password);
546                 gtk_widget_hide(wizard->recv_username_label);
547                 gtk_widget_hide(wizard->recv_password_label);
548         }
549 }
550
551 static GtkWidget* recv_page (WizardWindow * wizard)
552 {
553         GtkWidget *table = gtk_table_new(5,2, FALSE);
554         GtkWidget *menu = gtk_menu_new();
555         GtkWidget *menuitem;
556         gchar *text;
557         gint i = 0;
558         
559         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
560         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
561
562         wizard->recv_type = gtk_option_menu_new();
563         
564         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
565         g_signal_connect(G_OBJECT(menuitem), "activate",
566                          G_CALLBACK(wizard_protocol_changed),
567                          wizard);
568         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
569         g_signal_connect(G_OBJECT(menuitem), "activate",
570                          G_CALLBACK(wizard_protocol_changed),
571                          wizard);
572         MENUITEM_ADD (menu, menuitem, _("Local mbox file"), A_LOCAL);
573         g_signal_connect(G_OBJECT(menuitem), "activate",
574                          G_CALLBACK(wizard_protocol_changed),
575                          wizard);
576
577         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
578         GTK_TABLE_ADD_ROW_AT(table, _("Server type:"), 
579                              wizard->recv_type, i); i++;
580
581         wizard->recv_server = gtk_entry_new();
582         text = get_default_server(wizard, "pop");
583         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
584         g_free(text);
585         
586         wizard->recv_label = gtk_label_new(_("Server address:"));
587         gtk_table_attach(GTK_TABLE(table), wizard->recv_label,                        
588                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
589         if (GTK_IS_MISC(wizard->recv_label))                                                  
590                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_label), 1, 0.5);         
591         gtk_table_attach(GTK_TABLE(table), wizard->recv_server,       
592                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
593         i++;
594         
595         wizard->recv_username = gtk_entry_new();
596         wizard->recv_username_label = gtk_label_new(_("Username:"));
597         gtk_table_attach(GTK_TABLE(table), wizard->recv_username_label,                               
598                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
599         if (GTK_IS_MISC(wizard->recv_username_label))                                                 
600                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_username_label), 1, 0.5);        
601         gtk_table_attach(GTK_TABLE(table), wizard->recv_username,             
602                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
603         i++;
604         
605         text = get_default_account(wizard);
606         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), text);
607         g_free(text);
608
609         wizard->recv_password = gtk_entry_new();
610         wizard->recv_password_label = gtk_label_new(_("Password:"));
611         gtk_table_attach(GTK_TABLE(table), wizard->recv_password_label,                               
612                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
613         if (GTK_IS_MISC(wizard->recv_password_label))                                                 
614                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_password_label), 1, 0.5);        
615         gtk_table_attach(GTK_TABLE(table), wizard->recv_password,             
616                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
617         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
618         i++;
619         
620         wizard->recv_imap_subdir = gtk_entry_new();
621         wizard->recv_imap_label = gtk_label_new(_("IMAP server directory:"));
622         
623         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_label,                           
624                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
625         if (GTK_IS_MISC(wizard->recv_imap_label))                                                     
626                 gtk_misc_set_alignment(GTK_MISC(wizard->recv_imap_label), 1, 0.5);            
627         gtk_table_attach(GTK_TABLE(table), wizard->recv_imap_subdir,          
628                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            
629
630         i++;
631         
632         return table;
633 }
634
635 #ifdef USE_OPENSSL
636 static GtkWidget* ssl_page (WizardWindow * wizard)
637 {
638         GtkWidget *table = gtk_table_new(2,2, FALSE);
639         gint i = 0;
640         
641         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
642         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
643
644         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
645                                         _("Use SSL to connect to SMTP server"));
646         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
647                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
648         
649         wizard->recv_use_ssl = gtk_check_button_new_with_label(
650                                         _("Use SSL to connect to receiving server"));
651         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
652                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
653         
654         return table;
655 }
656 #endif
657
658 static void
659 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
660 {
661         WizardWindow * wizard = (WizardWindow *)data;
662         int current_page, num_pages;
663         GtkWidget *menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
664         GtkWidget *menuitem = gtk_menu_get_active(GTK_MENU(menu));
665         gint protocol = GPOINTER_TO_INT
666                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
667         gboolean skip_mailbox_page = FALSE;
668         
669         if (protocol == A_IMAP4) {
670                 skip_mailbox_page = TRUE;
671         }
672         
673         num_pages = g_slist_length(wizard->pages);
674
675         current_page = gtk_notebook_get_current_page (
676                                 GTK_NOTEBOOK(wizard->notebook));
677         if (response == CANCEL)
678         {
679                 wizard->result = FALSE;
680                 wizard->finished = TRUE;
681                 gtk_widget_destroy (GTK_WIDGET(dialog));
682         }
683         else if (response == FINISHED)
684         {
685                 if (!wizard_write_config(wizard)) {
686                         current_page = gtk_notebook_get_current_page (
687                                         GTK_NOTEBOOK(wizard->notebook));
688                         goto set_sens;
689                 }
690                 wizard->result = TRUE;
691                 wizard->finished = TRUE;
692                 gtk_widget_destroy (GTK_WIDGET(dialog));
693         }
694         else
695         {
696                 if (response == GO_BACK)
697                 {
698                         if (current_page > 0) {
699                                 current_page--;
700                                 if (current_page == 4 && skip_mailbox_page) {
701                                         /* mailbox */
702                                         current_page--;
703                                 }
704                                 gtk_notebook_set_current_page (
705                                         GTK_NOTEBOOK(wizard->notebook), 
706                                         current_page);
707                         }
708                 }
709                 else if (response == GO_FORWARD)
710                 {
711                         if (current_page < (num_pages-1)) {
712                                 current_page++;
713                                 if (current_page == 4 && skip_mailbox_page) {
714                                         /* mailbox */
715                                         current_page++;
716                                 }
717                                 gtk_notebook_set_current_page (
718                                         GTK_NOTEBOOK(wizard->notebook), 
719                                         current_page);
720                         }
721                 }
722 set_sens:
723                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
724                                 current_page > 0);
725                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
726                                 current_page < (num_pages - 1));
727                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
728                                 current_page == (num_pages - 1));
729         }
730 }
731
732 static gint wizard_close_cb(GtkWidget *widget, GdkEventAny *event,
733                                  gpointer data)
734 {
735         WizardWindow *wizard = (WizardWindow *)data;
736         wizard->result = FALSE;
737         wizard->finished = TRUE;
738         
739         return FALSE;
740 }
741
742 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
743         WizardWindow *wizard = g_new0(WizardWindow, 1);
744         GtkWidget *page;
745         GtkWidget *widget;
746         gchar     *text;
747         GSList    *cur;
748         gboolean   result;
749         
750         wizard->mainwin = mainwin;
751         wizard->create_mailbox = create_mailbox;
752         
753         gtk_widget_hide(mainwin->window);
754         
755         wizard->window = gtk_dialog_new_with_buttons (_("Sylpheed-Claws Setup Wizard"),
756                         NULL, 0, 
757                         GTK_STOCK_GO_BACK, GO_BACK,
758                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
759                         GTK_STOCK_SAVE, FINISHED,
760                         GTK_STOCK_CANCEL, CANCEL,
761                         NULL);
762
763         g_signal_connect(wizard->window, "response", 
764                           G_CALLBACK(wizard_response_cb), wizard);
765         gtk_widget_realize(wizard->window);
766         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
767                         GO_FORWARD);
768         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
769                         GO_BACK, FALSE);
770         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
771                         GO_FORWARD, TRUE);
772         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
773                         FINISHED, FALSE);
774         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
775                         CANCEL, TRUE);
776         
777         wizard->notebook = gtk_notebook_new();
778         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
779         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
780         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
781                             wizard->notebook, TRUE, TRUE, 0);
782         
783         wizard->pages = NULL;
784         
785 /*welcome page: 0 */
786         page = create_page(wizard, _("Welcome to Sylpheed-Claws"));
787         
788         wizard->pages = g_slist_append(wizard->pages, page);
789         widget = stock_pixmap_widget(wizard->window, 
790                                 STOCK_PIXMAP_SYLPHEED_LOGO);
791
792         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
793         
794         text = g_strdup(_("Welcome to the Sylpheed-Claws setup wizard.\n\n"
795                           "We will begin by defining some basic "
796                           "information about you and your most common "
797                           "mail options so that you can start to use "
798                           "Sylpheed-Claws in less than five minutes."));
799         widget = gtk_label_new(text);
800         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);
801         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
802         g_free(text);
803
804 /* user page: 1 */
805         widget = create_page (wizard, _("About You"));
806         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
807         wizard->pages = g_slist_append(wizard->pages, widget);
808
809 /*smtp page: 2 */
810         widget = create_page (wizard, _("Sending mail"));
811         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
812         wizard->pages = g_slist_append(wizard->pages, widget);
813
814 /* recv+auth page: 3 */
815         widget = create_page (wizard, _("Receiving mail"));
816         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
817         wizard->pages = g_slist_append(wizard->pages, widget);
818
819 /* mailbox page: 4 */
820         if (create_mailbox) {
821                 widget = create_page (wizard, _("Saving mail on disk"));
822                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
823                 wizard->pages = g_slist_append(wizard->pages, widget);
824         }
825 /* ssl page: 5 */
826 #ifdef USE_OPENSSL
827         widget = create_page (wizard, _("Security"));
828         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
829         wizard->pages = g_slist_append(wizard->pages, widget);
830 #endif
831
832 /* done page: 6 */
833         page = create_page(wizard, _("Configuration finished"));
834         
835         wizard->pages = g_slist_append(wizard->pages, page);
836         widget = stock_pixmap_widget(wizard->window, 
837                                 STOCK_PIXMAP_SYLPHEED_LOGO);
838
839         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
840         
841         text = g_strdup(_("Sylpheed-Claws is now ready.\n\n"
842                           "Click Save to start."));
843         widget = gtk_label_new(text);
844         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
845         g_free(text);
846
847
848         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
849                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
850                                           GTK_WIDGET(cur->data), NULL);
851         }
852         
853         g_signal_connect(G_OBJECT(wizard->window), "delete_event",
854                          G_CALLBACK(wizard_close_cb), wizard);
855         gtk_widget_show_all (wizard->window);
856
857         gtk_widget_hide(wizard->recv_imap_label);
858         gtk_widget_hide(wizard->recv_imap_subdir);
859
860         while (!wizard->finished)
861                 gtk_main_iteration();
862
863         result = wizard->result;
864         
865         GTK_EVENTS_FLUSH();
866
867         gtk_widget_show(mainwin->window);
868         g_free(wizard);
869
870         return result;
871 }