1 |
/* wptPreferencesDlg.cpp - Dialog for the preferences |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT 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 |
* WinPT 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 WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <shlobj.h> |
27 |
|
28 |
#include "wptNLS.h" |
29 |
#include "wptGPG.h" |
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptDlgs.h" |
32 |
#include "wptTypes.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptRegistry.h" |
35 |
#include "wptHotkey.h" |
36 |
#include "wptW32API.h" |
37 |
#include "wptVersion.h" |
38 |
#include "wptAgent.h" |
39 |
#include "wptKeyserver.h" |
40 |
#include "resource.h" |
41 |
|
42 |
|
43 |
/* Dialog IDs of all hotkeys. */ |
44 |
static int hotkeys[] = { |
45 |
IDC_PREFS_CLIP_ENCRYPT, |
46 |
IDC_PREFS_CLIP_DECRYPT_VERIFY, |
47 |
IDC_PREFS_CLIP_SIGN, |
48 |
IDC_PREFS_CLIP_SIGNENC, |
49 |
IDC_PREFS_CWS_ENCRYPT, |
50 |
IDC_PREFS_CWS_DECRYPT_VERIFY, |
51 |
IDC_PREFS_CWS_SIGN, |
52 |
IDC_PREFS_CWS_SIGNENC, |
53 |
0 |
54 |
}; |
55 |
|
56 |
|
57 |
/* Check that the given hotkey is in A..Z or a..z. */ |
58 |
static int |
59 |
check_hotkey (char * key) |
60 |
{ |
61 |
if (*key >= 'A' && *key <= 'Z') |
62 |
return 1; |
63 |
else if (*key >= 'a' && *key <= 'z') { |
64 |
*key = *key - 32; |
65 |
return 1; |
66 |
} |
67 |
return 0; |
68 |
} |
69 |
|
70 |
|
71 |
/* Disable all hotkey controls in the dialog @dlg if val is 1. */ |
72 |
static void |
73 |
disable_hotkey_items (HWND dlg, int val) |
74 |
{ |
75 |
int mode = val? FALSE : TRUE; |
76 |
int i, id; |
77 |
|
78 |
for (i=0; (id = hotkeys[i]); i++) |
79 |
EnableWindow (GetDlgItem (dlg, id), mode); |
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
/* En- or disable the backup items in the dialog @dlg. |
85 |
If val != 0 activate them, disable them otherwise. */ |
86 |
static void |
87 |
enable_backup_items (HWND dlg, int val) |
88 |
{ |
89 |
int mode = val? TRUE : FALSE; |
90 |
|
91 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKHOME), mode); |
92 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKUSER), mode); |
93 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), mode); |
94 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), mode); |
95 |
} |
96 |
|
97 |
|
98 |
/* Initialize the combox in the dialog @dlg with the valid list modes. */ |
99 |
static void |
100 |
init_keylist_modes (HWND dlg) |
101 |
{ |
102 |
HWND cb = GetDlgItem (dlg, IDC_PREFS_LISTMODE); |
103 |
combox_add_string (cb, (char *)"NORMAL"); |
104 |
combox_add_string (cb, (char *)"MINIMAL"); |
105 |
SendMessage (cb, CB_SETCURSEL, (WPARAM)reg_prefs.keylist_mode, 0); |
106 |
} |
107 |
|
108 |
|
109 |
/* Initialize the combobox in the dialog @dlg with the valid wipe modes. */ |
110 |
static void |
111 |
init_wipe_modes (HWND dlg) |
112 |
{ |
113 |
HWND cb = GetDlgItem (dlg, IDC_PREFS_WIPEMODE); |
114 |
combox_add_string (cb, (char *)"Simple"); |
115 |
combox_add_string (cb, (char *)"DoD"); |
116 |
combox_add_string (cb, (char *)"Gutmann"); |
117 |
SendMessage (cb, CB_SETCURSEL, (WPARAM)reg_prefs.wipe_mode, 0); |
118 |
} |
119 |
|
120 |
|
121 |
/* Dialog box procedure for the WinPT preferences. */ |
122 |
BOOL CALLBACK |
123 |
prefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
124 |
{ |
125 |
int rc; |
126 |
int i, id; |
127 |
char t[2]; |
128 |
char path[256], * p; |
129 |
HWND cb; |
130 |
|
131 |
switch ( msg ) { |
132 |
case WM_INITDIALOG: |
133 |
#ifndef LANG_DE |
134 |
SetWindowText (dlg, _("WinPT Preferences")); |
135 |
SetDlgItemText (dlg, IDC_PREFS_SECMODE, _("Do not use any &temporary files")); |
136 |
SetDlgItemText (dlg, IDC_PREFS_VIEWER, _("Use clipboard &viewer to display the plaintext")); |
137 |
SetDlgItemText (dlg, IDC_PREFS_WWINFO, _("Word wrap cleartext &signatures at column")); |
138 |
SetDlgItemText (dlg, IDC_PREFS_DISABLE_HOTKEYS, _("&Disable hotkeys (Not recommended!)")); |
139 |
SetDlgItemText (dlg, IDC_PREFS_TRUST, _("Skip key validation and assume that keys are always fully trusted")); |
140 |
SetDlgItemText (dlg, IDC_PREFS_AUTOBACKUP, _("&Automatic keyring backup when WinPT closes")); |
141 |
SetDlgItemText (dlg, IDC_PREFS_BAKHOME, _("Backup to GPG &home folder")); |
142 |
SetDlgItemText (dlg, IDC_PREFS_BAKUSER, _("Backup to:")); |
143 |
#endif |
144 |
SetDlgItemInt( dlg, IDC_PREFS_CACHETIME, reg_prefs.cache_time, TRUE ); |
145 |
SetDlgItemInt( dlg, IDC_PREFS_WORDWRAP, reg_prefs.word_wrap, TRUE ); |
146 |
if (reg_prefs.backup.path) |
147 |
SetDlgItemText (dlg, IDC_PREFS_BAKPATH, reg_prefs.backup.path); |
148 |
if (reg_prefs.kserv_conf) |
149 |
SetDlgItemText (dlg, IDC_PREFS_KSERVER, reg_prefs.kserv_conf); |
150 |
|
151 |
for (i=0; (id=hotkeys[i]); i++) |
152 |
SetDlgItemText (dlg, id, reg_hotkeys[i].key); |
153 |
CheckDlgButton( dlg, IDC_PREFS_DISABLE_HOTKEYS, |
154 |
reg_prefs.no_hotkeys ? BST_CHECKED : BST_UNCHECKED ); |
155 |
CheckDlgButton( dlg, IDC_PREFS_SECMODE, |
156 |
reg_prefs.use_tmpfiles ? BST_UNCHECKED : BST_CHECKED ); |
157 |
CheckDlgButton( dlg, IDC_PREFS_VIEWER, |
158 |
reg_prefs.use_viewer ? BST_CHECKED: BST_UNCHECKED ); |
159 |
CheckDlgButton( dlg, IDC_PREFS_TRUST, |
160 |
reg_prefs.always_trust? BST_CHECKED : BST_UNCHECKED ); |
161 |
CheckDlgButton( dlg, IDC_PREFS_AUTOBACKUP, |
162 |
reg_prefs.auto_backup? BST_CHECKED : BST_UNCHECKED ); |
163 |
CheckDlgButton( dlg, IDC_PREFS_BAKHOME, |
164 |
reg_prefs.backup.mode==1? BST_CHECKED : BST_UNCHECKED ); |
165 |
CheckDlgButton( dlg, IDC_PREFS_BAKUSER, |
166 |
reg_prefs.backup.mode==2? BST_CHECKED : BST_UNCHECKED ); |
167 |
if( reg_prefs.no_hotkeys ) |
168 |
disable_hotkey_items( dlg, 1 ); |
169 |
if (!reg_prefs.auto_backup) |
170 |
enable_backup_items (dlg, 0); |
171 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), |
172 |
reg_prefs.backup.mode==1?FALSE : TRUE); |
173 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), |
174 |
reg_prefs.backup.mode==1? FALSE : TRUE); |
175 |
init_keylist_modes (dlg); |
176 |
init_wipe_modes (dlg); |
177 |
center_window (dlg, NULL); |
178 |
SetForegroundWindow (dlg); |
179 |
return TRUE; |
180 |
|
181 |
case WM_SYSCOMMAND: |
182 |
if (LOWORD (wparam) == SC_CLOSE) |
183 |
EndDialog (dlg, TRUE); |
184 |
return FALSE; |
185 |
|
186 |
case WM_COMMAND: |
187 |
switch( HIWORD(wparam) ) { |
188 |
case BN_CLICKED: |
189 |
switch( (int)LOWORD( wparam ) ) { |
190 |
case IDC_PREFS_DISABLE_HOTKEYS: |
191 |
reg_prefs.no_hotkeys ^= 1; |
192 |
disable_hotkey_items( dlg, reg_prefs.no_hotkeys ); |
193 |
break; |
194 |
|
195 |
case IDC_PREFS_TRUST: |
196 |
reg_prefs.always_trust ^= 1; |
197 |
if( reg_prefs.always_trust ) { |
198 |
msg_box (dlg, |
199 |
_("In most cases it is not a good idea to enable this setting.\n" |
200 |
"If you know what you are doing let this flag enabled, otherwise\n" |
201 |
"it is safe to leave this flag untouched."), |
202 |
_("Preferences"), MB_ICONWARNING|MB_OK ); |
203 |
} |
204 |
break; |
205 |
|
206 |
case IDC_PREFS_AUTOBACKUP: |
207 |
reg_prefs.auto_backup ^= 1; |
208 |
enable_backup_items (dlg, reg_prefs.auto_backup); |
209 |
if (reg_prefs.auto_backup == 1) { |
210 |
if (!IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) && |
211 |
!IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER)) |
212 |
CheckDlgButton (dlg, IDC_PREFS_BAKHOME, BST_CHECKED); |
213 |
} |
214 |
break; |
215 |
|
216 |
case IDC_PREFS_BAKHOME: |
217 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), FALSE); |
218 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE); |
219 |
break; |
220 |
|
221 |
case IDC_PREFS_BAKUSER: |
222 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), TRUE); |
223 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), TRUE); |
224 |
break; |
225 |
} |
226 |
break; |
227 |
} |
228 |
switch( LOWORD( wparam ) ) { |
229 |
case IDC_PREFS_BAKSELECT: |
230 |
const char *bpath; |
231 |
bpath = get_folder_dlg (dlg, _("Select GPG backup path"), NULL); |
232 |
if (bpath) |
233 |
SetDlgItemText (dlg, IDC_PREFS_BAKPATH, bpath); |
234 |
break; |
235 |
|
236 |
case IDC_PREFS_KSELFILE: { |
237 |
const char * name; |
238 |
name = get_fileopen_dlg (dlg, _("Please select a keyserver.conf file"), |
239 |
NULL, NULL); |
240 |
if (name != NULL) |
241 |
SetDlgItemText (dlg, IDC_PREFS_KSERVER, name); |
242 |
break; } |
243 |
|
244 |
case IDOK: |
245 |
rc = GetDlgItemInt( dlg, IDC_PREFS_CACHETIME, NULL, FALSE ); |
246 |
if( !rc ) |
247 |
reg_prefs.cache_time = 0; |
248 |
else if( rc > 3600 ) { |
249 |
msg_box( dlg, _("Please enter a value that is between 1-3600.\nIt is not " |
250 |
"a good idea to cache the passphrase more than one hour."), |
251 |
_("Preferences"), MB_ERR ); |
252 |
SetDlgItemInt( dlg, IDC_PREFS_CACHETIME, 0, FALSE ); |
253 |
return TRUE; |
254 |
} |
255 |
if (reg_prefs.cache_time != rc) |
256 |
agent_flush_cache (); |
257 |
reg_prefs.cache_time = rc; |
258 |
rc = GetDlgItemInt( dlg, IDC_PREFS_WORDWRAP, NULL, FALSE ); |
259 |
if( !rc ) |
260 |
reg_prefs.word_wrap = 0; |
261 |
else if( rc > 80 ) { |
262 |
msg_box( dlg, _("Please enter a value between 1-80."), _("Preferences"), MB_ERR ); |
263 |
return TRUE; |
264 |
} |
265 |
reg_prefs.word_wrap = rc; |
266 |
reg_prefs.use_tmpfiles = IsDlgButtonChecked( dlg, IDC_PREFS_SECMODE )? 0 : 1; |
267 |
reg_prefs.use_viewer = IsDlgButtonChecked( dlg, IDC_PREFS_VIEWER )? 1 : 0; |
268 |
reg_prefs.backup.mode = IsDlgButtonChecked( dlg, IDC_PREFS_BAKHOME ) ? 1 : |
269 |
IsDlgButtonChecked( dlg, IDC_PREFS_BAKUSER )? 2 : 0; |
270 |
if( reg_prefs.backup.mode == 2 ) { |
271 |
if( !GetDlgItemText( dlg, IDC_PREFS_BAKPATH, path, sizeof (path)-1 ) |
272 |
|| dir_exist_check( path ) ) { |
273 |
msg_box( dlg, _("The specified backup folder is invalid."), |
274 |
_("Preferences"), MB_ERR ); |
275 |
return TRUE; |
276 |
} |
277 |
free_if_alloc (reg_prefs.backup.path); |
278 |
p = reg_prefs.backup.path = m_strdup (path); |
279 |
if (!p) |
280 |
BUG (0); |
281 |
} |
282 |
free_if_alloc (reg_prefs.kserv_conf); |
283 |
if (!GetDlgItemText (dlg, IDC_PREFS_KSERVER, path, sizeof (path)-1) |
284 |
|| file_exist_check (path)) { |
285 |
rc = msg_box (dlg, _("The specified keyserver config file is invalid.\n\n" |
286 |
"Create new default config file?"), |
287 |
_("Preferences"), MB_ERR|MB_YESNO); |
288 |
if (rc == IDNO) |
289 |
return TRUE; |
290 |
else |
291 |
{ |
292 |
char cwd[256], * fn; |
293 |
FILE * fp = fopen ("keyserver.conf", "wb"); |
294 |
if (fp) { |
295 |
fprintf (fp, "%s\r\n", DEF_HKP_KEYSERVER); |
296 |
fclose (fp); |
297 |
} |
298 |
GetCurrentDirectory (DIM (cwd)-1, cwd); |
299 |
fn = make_filename (cwd, "keyserver", "conf"); |
300 |
memset (path, 0, sizeof( path)); |
301 |
strncpy (path, fn, DIM (path)-1); |
302 |
free_if_alloc (fn); |
303 |
} |
304 |
} |
305 |
p = reg_prefs.kserv_conf = m_strdup (path); |
306 |
if (!p) |
307 |
BUG (0); |
308 |
|
309 |
cb = GetDlgItem( dlg, IDC_PREFS_LISTMODE ); |
310 |
reg_prefs.keylist_mode = SendMessage( cb, CB_GETCURSEL, 0, 0 ); |
311 |
|
312 |
cb = GetDlgItem( dlg, IDC_PREFS_WIPEMODE ); |
313 |
reg_prefs.wipe_mode = SendMessage( cb, CB_GETCURSEL, 0, 0 ); |
314 |
|
315 |
if (IsDlgButtonChecked (dlg, IDC_PREFS_DISABLE_HOTKEYS)) { |
316 |
hotkeys_unregister (glob_hwnd); |
317 |
reg_prefs.no_hotkeys = 1; |
318 |
} |
319 |
else { |
320 |
reg_prefs.no_hotkeys = 0; |
321 |
for( i = 0; (id = hotkeys[i]); i++ ) { |
322 |
rc = GetDlgItemText( dlg, id, t, 2 ); |
323 |
if( rc && check_hotkey( &t[0] ) ) |
324 |
hotkey_enable( ®_hotkeys[i], t ); |
325 |
else |
326 |
hotkey_disable( ®_hotkeys[i] ); |
327 |
} |
328 |
set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "DisableHotkeys", "0"); |
329 |
reg_prefs.no_hotkeys = 0; |
330 |
} |
331 |
|
332 |
if ((rc = set_reg_winpt_prefs (®_prefs))) |
333 |
msg_box (dlg, winpt_strerror (rc), _("Preferences"), MB_ERR); |
334 |
|
335 |
if (reg_prefs.no_hotkeys == 0) { |
336 |
hotkeys_unregister (glob_hwnd); |
337 |
hotkeys_modify (); |
338 |
if ((rc = hotkeys_register (glob_hwnd))) |
339 |
msg_box (NULL, winpt_strerror (rc), _("Hotkeys"), MB_ERR); |
340 |
} |
341 |
EndDialog (dlg, TRUE); |
342 |
return TRUE; |
343 |
|
344 |
case IDCANCEL: |
345 |
EndDialog( dlg, FALSE ); |
346 |
return FALSE; |
347 |
} |
348 |
break; |
349 |
} |
350 |
|
351 |
return FALSE; |
352 |
} |