bf30db6e914c3f02d4ebf9e90e1ce7b18b1f2e8c
[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 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkvbox.h>
27 #include <gtk/gtkbox.h>
28 #include <gtk/gtktable.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtklabel.h>
31 #include <gtk/gtknotebook.h>
32 #include <gtk/gtktogglebutton.h>
33 #include <gtk/gtkcheckbutton.h>
34 #include <gtk/gtk.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 #ifdef HAVE_CONFIG_H
42 #  include "config.h"
43 #endif
44
45 #include "utils.h"
46 #include "gtk/menu.h"
47 #include "account.h"
48 #include "prefs_account.h"
49 #include "mainwindow.h"
50 #include "stock_pixmap.h"
51 #include "setup.h"
52 #include "folder.h"
53 #ifdef USE_OPENSSL                      
54 #include "ssl.h"
55 #endif
56 typedef enum
57 {
58         GO_BACK,
59         GO_FORWARD,
60         CANCEL,
61         FINISHED
62 } PageNavigation;
63
64 typedef struct
65 {
66         GtkWidget *window;
67         GSList    *pages;
68         GtkWidget *notebook;
69
70         MainWindow *mainwin;
71         
72         GtkWidget *email;
73         GtkWidget *full_name;
74         GtkWidget *organization;
75
76         GtkWidget *mailbox_name;
77         
78         GtkWidget *smtp_server;
79
80         GtkWidget *recv_type;
81         GtkWidget *recv_server;
82         GtkWidget *recv_username;
83         GtkWidget *recv_password;
84
85 #ifdef USE_OPENSSL
86         GtkWidget *smtp_use_ssl;
87         GtkWidget *recv_use_ssl;
88 #endif
89         
90         gboolean create_mailbox;
91         gboolean finished;
92         gboolean result;
93
94 } WizardWindow;
95
96 static void wizard_write_config(WizardWindow *wizard)
97 {
98         gboolean mailbox_ok = FALSE;
99         PrefsAccount *prefs_account = prefs_account_new();
100         GList *account_list = NULL;
101         GtkWidget *menu, *menuitem;
102         
103         if (wizard->create_mailbox) {
104                 mailbox_ok = setup_write_mailbox_path(wizard->mainwin, 
105                                 gtk_entry_get_text(GTK_ENTRY(wizard->mailbox_name)));
106         }
107
108         prefs_account->account_name = g_strdup_printf("%s@%s",
109                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)),
110                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
111         prefs_account->name = g_strdup(
112                                 gtk_entry_get_text(GTK_ENTRY(wizard->full_name)));
113         prefs_account->address = g_strdup(
114                                 gtk_entry_get_text(GTK_ENTRY(wizard->email)));
115         prefs_account->organization = g_strdup(
116                                 gtk_entry_get_text(GTK_ENTRY(wizard->organization)));
117         prefs_account->smtp_server = g_strdup(
118                                 gtk_entry_get_text(GTK_ENTRY(wizard->smtp_server)));
119         prefs_account->recv_server = g_strdup(
120                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_server)));
121         prefs_account->userid = g_strdup(
122                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_username)));
123         prefs_account->passwd = g_strdup(
124                                 gtk_entry_get_text(GTK_ENTRY(wizard->recv_password)));
125
126         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(wizard->recv_type));
127         menuitem = gtk_menu_get_active(GTK_MENU(menu));
128         prefs_account->protocol = GPOINTER_TO_INT
129                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
130
131 #ifdef USE_OPENSSL                      
132         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->smtp_use_ssl)))
133                 prefs_account->ssl_smtp = SSL_TUNNEL;
134         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wizard->recv_use_ssl))) {
135                 if (prefs_account->protocol == A_IMAP4)
136                         prefs_account->ssl_imap = SSL_TUNNEL;
137                 else
138                         prefs_account->ssl_pop = SSL_TUNNEL;
139         }
140 #endif
141
142         account_list = g_list_append(account_list, prefs_account);
143         prefs_account_write_config_all(account_list);
144         prefs_account_free(prefs_account);
145         account_read_config_all();
146 }
147
148 static GtkWidget* create_page (WizardWindow *wizard, const char * title)
149 {
150         GtkWidget *w;
151         GtkWidget *vbox;
152         GtkWidget *hbox;
153         GtkWidget *image;
154         char *title_string;
155
156         vbox = gtk_vbox_new (FALSE, 6);
157         gtk_container_set_border_width  (GTK_CONTAINER(vbox), 10);
158
159         /* create the titlebar */
160         hbox = gtk_hbox_new (FALSE, 12);
161         image = stock_pixmap_widget(wizard->window, 
162                                 STOCK_PIXMAP_SYLPHEED_ICON);
163         gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 0);
164         title_string = g_strconcat ("<span size=\"xx-large\" weight=\"ultrabold\">", title ? title : "", "</span>", NULL);
165         w = gtk_label_new (title_string);
166         gtk_label_set_use_markup (GTK_LABEL(w), TRUE);
167         g_free (title_string);
168         gtk_box_pack_start (GTK_BOX(hbox), w, FALSE, FALSE, 0);
169
170         /* pack the titlebar */
171         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
172
173         /* pack the separator */
174         gtk_box_pack_start (GTK_BOX(vbox), gtk_hseparator_new(), FALSE, FALSE, 0);
175
176         /* pack space */
177         w = gtk_alignment_new (0, 0, 0, 0);
178         gtk_widget_set_size_request (w, 0, 6);
179         gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
180
181         return vbox;
182 }
183
184 #define GTK_TABLE_ADD_ROW_AT(table,text,entry,i) {                            \
185         GtkWidget *label = gtk_label_new(text);                               \
186         gtk_table_attach(GTK_TABLE(table), label,                             \
187                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
188         if (GTK_IS_MISC(label))                                               \
189                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);              \
190         gtk_table_attach(GTK_TABLE(table), entry,                             \
191                          1,2,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);            \
192 }
193
194 static gchar *get_default_email_addr(void)
195 {
196         gchar *domain_name = g_strdup(get_domain_name());
197         gchar *result;
198         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
199         && strlen(strchr(domain_name, '.')) > 6) {
200                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
201                 g_free(domain_name);
202                 domain_name = tmp;
203         }
204         result = g_strdup_printf("%s@%s",
205                                 g_get_user_name(),
206                                 domain_name);
207         g_free(domain_name);
208         return result;
209 }
210
211 static gchar *get_default_server(WizardWindow * wizard, const gchar *type)
212 {
213         gchar *domain_name = g_strdup(get_domain_name());
214         gchar *result;
215         
216         if (strchr(domain_name, '.') != strrchr(domain_name, '.')
217         && strlen(strchr(domain_name, '.')) > 6) {
218                 gchar *tmp = g_strdup(strchr(domain_name, '.')+1);
219                 g_free(domain_name);
220                 domain_name = tmp;
221         } else if (strchr(domain_name, '.') == NULL) {
222                 /* only hostname found, use email suffix */
223                 gchar *mail;
224                 mail = gtk_editable_get_chars(GTK_EDITABLE(wizard->email), 0, -1);
225
226                 if (strlen (mail) && strstr(mail, "@")) {
227                         g_free(domain_name);
228                         domain_name = g_strdup(strstr(mail, "@")+1);
229                 }
230                 g_free(mail);
231         }
232         result = g_strdup_printf("%s.%s",
233                                 type, domain_name);
234         g_free(domain_name);
235         return result;
236 }
237
238 static void wizard_email_changed(GtkWidget *widget, gpointer data)
239 {
240         WizardWindow *wizard = (WizardWindow *)data;
241         RecvProtocol protocol;
242         gchar *text;
243         protocol = GPOINTER_TO_INT
244                 (g_object_get_data(G_OBJECT(wizard->recv_type), MENU_VAL_ID));
245         
246         text = get_default_server(wizard, "smtp");
247         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
248         g_free(text);
249
250         if (protocol == A_POP3) {
251                 text = get_default_server(wizard, "pop");
252                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
253                 g_free(text);
254         } else {
255                 text = get_default_server(wizard, "imap");
256                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
257                 g_free(text);
258         }
259 }
260
261 static GtkWidget* user_page (WizardWindow * wizard)
262 {
263         GtkWidget *table = gtk_table_new(3,2, FALSE);
264         gchar *text;
265         gint i = 0;
266         
267         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
268         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
269
270         wizard->full_name = gtk_entry_new();
271         gtk_entry_set_text(GTK_ENTRY(wizard->full_name), g_get_real_name());
272         GTK_TABLE_ADD_ROW_AT(table, _("Your name:"), 
273                              wizard->full_name, i); i++;
274         
275         wizard->email = gtk_entry_new();
276         text = get_default_email_addr();
277         gtk_entry_set_text(GTK_ENTRY(wizard->email), text);
278         g_free(text);
279         GTK_TABLE_ADD_ROW_AT(table, _("Your email address:"), 
280                              wizard->email, i); i++;
281         
282         wizard->organization = gtk_entry_new();
283         GTK_TABLE_ADD_ROW_AT(table, _("Your organization:"), 
284                              wizard->organization, i); i++;
285         
286         g_signal_connect(G_OBJECT(wizard->email), "changed",
287                          G_CALLBACK(wizard_email_changed),
288                          wizard);
289         return table;
290 }
291
292 static GtkWidget* mailbox_page (WizardWindow * wizard)
293 {
294         GtkWidget *table = gtk_table_new(1,2, FALSE);
295         gint i = 0;
296         
297         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
298         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
299
300         wizard->mailbox_name = gtk_entry_new();
301         gtk_entry_set_text(GTK_ENTRY(wizard->mailbox_name), "Mail");
302         GTK_TABLE_ADD_ROW_AT(table, _("Mailbox name:"), 
303                              wizard->mailbox_name, i); i++;
304         
305         return table;
306 }
307
308 static GtkWidget* smtp_page (WizardWindow * wizard)
309 {
310         GtkWidget *table = gtk_table_new(1,2, FALSE);
311         gchar *text;
312         gint i = 0;
313         
314         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
315         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
316
317         wizard->smtp_server = gtk_entry_new();
318         text = get_default_server(wizard, "smtp");
319         gtk_entry_set_text(GTK_ENTRY(wizard->smtp_server), text);
320         g_free(text);
321         GTK_TABLE_ADD_ROW_AT(table, _("SMTP server address:"), 
322                              wizard->smtp_server, i); i++;
323         return table;
324 }
325
326 static void wizard_protocol_changed(GtkMenuItem *menuitem, gpointer data)
327 {
328         WizardWindow *wizard = (WizardWindow *)data;
329         RecvProtocol protocol;
330         gchar *text;
331         protocol = GPOINTER_TO_INT
332                 (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
333         
334         if (protocol == A_POP3) {
335                 text = get_default_server(wizard, "pop");
336                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
337                 g_free(text);
338         } else {
339                 text = get_default_server(wizard, "imap");
340                 gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
341                 g_free(text);
342         }
343 }
344
345 static GtkWidget* recv_page (WizardWindow * wizard)
346 {
347         GtkWidget *table = gtk_table_new(5,2, FALSE);
348         GtkWidget *menu = gtk_menu_new();
349         GtkWidget *menuitem;
350         gchar *text;
351         gint i = 0;
352         
353         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
354         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
355
356         wizard->recv_type = gtk_option_menu_new();
357         
358         MENUITEM_ADD (menu, menuitem, _("POP3"), A_POP3);
359         g_signal_connect(G_OBJECT(menuitem), "activate",
360                          G_CALLBACK(wizard_protocol_changed),
361                          wizard);
362         MENUITEM_ADD (menu, menuitem, _("IMAP"), A_IMAP4);
363         g_signal_connect(G_OBJECT(menuitem), "activate",
364                          G_CALLBACK(wizard_protocol_changed),
365                          wizard);
366         gtk_option_menu_set_menu (GTK_OPTION_MENU (wizard->recv_type), menu);
367         GTK_TABLE_ADD_ROW_AT(table, _("Server type:"), 
368                              wizard->recv_type, i); i++;
369
370         wizard->recv_server = gtk_entry_new();
371         text = get_default_server(wizard, "pop");
372         gtk_entry_set_text(GTK_ENTRY(wizard->recv_server), text);
373         g_free(text);
374         GTK_TABLE_ADD_ROW_AT(table, _("Server address:"), 
375                              wizard->recv_server, i); i++;
376         
377         wizard->recv_username = gtk_entry_new();
378         gtk_entry_set_text(GTK_ENTRY(wizard->recv_username), g_get_user_name());
379         GTK_TABLE_ADD_ROW_AT(table, _("Username:"), 
380                              wizard->recv_username, i); i++;
381         
382         wizard->recv_password = gtk_entry_new();
383         gtk_entry_set_visibility(GTK_ENTRY(wizard->recv_password), FALSE);
384         GTK_TABLE_ADD_ROW_AT(table, _("Password:"), 
385                              wizard->recv_password, i); i++;
386         
387         return table;
388 }
389
390 #ifdef USE_OPENSSL
391 static GtkWidget* ssl_page (WizardWindow * wizard)
392 {
393         GtkWidget *table = gtk_table_new(2,2, FALSE);
394         gint i = 0;
395         
396         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
397         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
398
399         wizard->smtp_use_ssl = gtk_check_button_new_with_label(
400                                         _("Use SSL to connect to SMTP server"));
401         gtk_table_attach(GTK_TABLE(table), wizard->smtp_use_ssl,      
402                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0); i++;
403         
404         wizard->recv_use_ssl = gtk_check_button_new_with_label(
405                                         _("Use SSL to connect to receiving server"));
406         gtk_table_attach(GTK_TABLE(table), wizard->recv_use_ssl,      
407                          0,1,i,i+1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
408         
409         return table;
410 }
411 #endif
412
413 static void
414 wizard_response_cb (GtkDialog * dialog, int response, gpointer data)
415 {
416         WizardWindow * wizard = (WizardWindow *)data;
417         int current_page, num_pages;
418         
419         num_pages = g_slist_length(wizard->pages);
420         current_page = gtk_notebook_get_current_page (
421                                 GTK_NOTEBOOK(wizard->notebook));
422         if (response == CANCEL)
423         {
424                 wizard->finished = TRUE;
425                 wizard->result = FALSE;
426                 gtk_widget_destroy (GTK_WIDGET(dialog));
427         }
428         else if (response == FINISHED)
429         {
430                 wizard_write_config(wizard);
431                 wizard->finished = TRUE;
432                 wizard->result = TRUE;
433                 gtk_widget_destroy (GTK_WIDGET(dialog));
434         }
435         else
436         {
437                 if (response == GO_BACK)
438                 {
439                         if (current_page > 0)
440                                 gtk_notebook_set_current_page (
441                                         GTK_NOTEBOOK(wizard->notebook), 
442                                         --current_page);
443                 }
444                 else if (response == GO_FORWARD)
445                 {
446                         if (current_page < (num_pages-1))
447                                 gtk_notebook_set_current_page (
448                                         GTK_NOTEBOOK(wizard->notebook), 
449                                         ++current_page);
450                 }
451
452                 gtk_dialog_set_response_sensitive (dialog, GO_BACK, 
453                                 current_page > 0);
454                 gtk_dialog_set_response_sensitive (dialog, GO_FORWARD, 
455                                 current_page < (num_pages - 1));
456                 gtk_dialog_set_response_sensitive (dialog, FINISHED, 
457                                 current_page == (num_pages - 1));
458         }
459 }
460
461
462 gboolean run_wizard(MainWindow *mainwin, gboolean create_mailbox) {
463         WizardWindow *wizard = g_new0(WizardWindow, 1);
464         GtkWidget *page;
465         GtkWidget *widget;
466         gchar     *text;
467         GSList    *cur;
468         gboolean   result;
469         
470         wizard->mainwin = mainwin;
471         wizard->create_mailbox = create_mailbox;
472         
473         gtk_widget_hide(mainwin->window);
474         
475         wizard->window = gtk_dialog_new_with_buttons (_("New User"),
476                         NULL, 0, 
477                         GTK_STOCK_GO_BACK, GO_BACK,
478                         GTK_STOCK_GO_FORWARD, GO_FORWARD,
479                         GTK_STOCK_SAVE, FINISHED,
480                         GTK_STOCK_QUIT, CANCEL,
481                         NULL);
482
483         g_signal_connect(wizard->window, "response", 
484                           G_CALLBACK(wizard_response_cb), wizard);
485         gtk_widget_realize(wizard->window);
486         gtk_dialog_set_default_response(GTK_DIALOG(wizard->window), 
487                         GO_FORWARD);
488         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
489                         GO_BACK, FALSE);
490         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
491                         GO_FORWARD, TRUE);
492         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
493                         FINISHED, FALSE);
494         gtk_dialog_set_response_sensitive(GTK_DIALOG(wizard->window), 
495                         CANCEL, TRUE);
496         
497         wizard->notebook = gtk_notebook_new();
498         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(wizard->notebook), FALSE);
499         gtk_notebook_set_show_border(GTK_NOTEBOOK(wizard->notebook), FALSE);
500         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(wizard->window)->vbox), 
501                             wizard->notebook, TRUE, TRUE, 0);
502         
503         wizard->pages = NULL;
504         
505 /*welcome page */
506         page = create_page(wizard, _("Welcome to Sylpheed-Claws."));
507         
508         wizard->pages = g_slist_append(wizard->pages, page);
509         widget = stock_pixmap_widget(wizard->window, 
510                                 STOCK_PIXMAP_SYLPHEED_LOGO);
511
512         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
513         
514         text = g_strdup(_("Welcome to Sylpheed-Claws.\n\n"
515                           "It looks like it's the first time you use \n"
516                           "Sylpheed-Claws. So, we'll now define some basic\n"
517                           "information about yourself and your most common\n"
518                           "mail parameters; so that you can begin to use\n"
519                           "Sylpheed-Claws in less than five minutes."));
520         widget = gtk_label_new(text);
521         gtk_box_pack_start (GTK_BOX(page), widget, FALSE, FALSE, 0);
522         g_free(text);
523
524 /* user page */
525         widget = create_page (wizard, _("About You"));
526         gtk_box_pack_start (GTK_BOX(widget), user_page(wizard), FALSE, FALSE, 0);
527         wizard->pages = g_slist_append(wizard->pages, widget);
528
529 /* mailbox page */
530         if (create_mailbox) {
531                 widget = create_page (wizard, _("Saving mail on disk"));
532                 gtk_box_pack_start (GTK_BOX(widget), mailbox_page(wizard), FALSE, FALSE, 0);
533                 wizard->pages = g_slist_append(wizard->pages, widget);
534         }
535 /*smtp page */
536         widget = create_page (wizard, _("Sending mail"));
537         gtk_box_pack_start (GTK_BOX(widget), smtp_page(wizard), FALSE, FALSE, 0);
538         wizard->pages = g_slist_append(wizard->pages, widget);
539
540 /* recv+auth page */
541         widget = create_page (wizard, _("Receiving mail"));
542         gtk_box_pack_start (GTK_BOX(widget), recv_page(wizard), FALSE, FALSE, 0);
543         wizard->pages = g_slist_append(wizard->pages, widget);
544
545 /* ssl page */
546 #ifdef USE_OPENSSL
547         widget = create_page (wizard, _("Security"));
548         gtk_box_pack_start (GTK_BOX(widget), ssl_page(wizard), FALSE, FALSE, 0);
549         wizard->pages = g_slist_append(wizard->pages, widget);
550 #endif
551         
552         for (cur = wizard->pages; cur && cur->data; cur = cur->next) {
553                 gtk_notebook_append_page (GTK_NOTEBOOK(wizard->notebook), 
554                                           GTK_WIDGET(cur->data), NULL);
555         }
556         
557         gtk_widget_show_all (wizard->window);
558
559         while (!wizard->finished)
560                 gtk_main_iteration();
561
562         result = wizard->result;
563         
564         GTK_EVENTS_FLUSH();
565
566         gtk_widget_show(mainwin->window);
567         g_free(wizard);
568
569         return result;
570 }