1 |
werner |
36 |
/* wptPreferencesDlg.cpp - Dialog for the preferences |
2 |
twoaday |
328 |
* Copyright (C) 2001-2003, 2005-2006, 2008-2009 Timo Schulz |
3 |
werner |
36 |
* |
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 |
twoaday |
328 |
|
17 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <shlobj.h> |
23 |
|
|
|
24 |
|
|
#include "wptNLS.h" |
25 |
|
|
#include "wptGPG.h" |
26 |
|
|
#include "wptCommonCtl.h" |
27 |
|
|
#include "wptDlgs.h" |
28 |
|
|
#include "wptTypes.h" |
29 |
|
|
#include "wptErrors.h" |
30 |
|
|
#include "wptRegistry.h" |
31 |
|
|
#include "wptHotkey.h" |
32 |
|
|
#include "wptW32API.h" |
33 |
|
|
#include "wptVersion.h" |
34 |
|
|
#include "wptAgent.h" |
35 |
|
|
#include "wptKeyserver.h" |
36 |
werner |
47 |
#include "resource.h" |
37 |
werner |
36 |
|
38 |
|
|
|
39 |
|
|
/* Dialog IDs of all hotkeys. */ |
40 |
|
|
static int hotkeys[] = { |
41 |
twoaday |
328 |
/* clipboard based */ |
42 |
|
|
// FIXME: why the wrong order leads to a correct storing |
43 |
|
|
// in the registry!? (2,1) should be (1,2) |
44 |
werner |
36 |
IDC_PREFS_CLIP_DECRYPT_VERIFY, |
45 |
twoaday |
328 |
IDC_PREFS_CLIP_ENCRYPT, |
46 |
|
|
IDC_PREFS_CLIP_SIGN, |
47 |
werner |
36 |
IDC_PREFS_CLIP_SIGNENC, |
48 |
twoaday |
328 |
/* current window based */ |
49 |
werner |
36 |
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 |
twoaday |
180 |
check_hotkey (char *key) |
60 |
werner |
36 |
{ |
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 |
twoaday |
32 |
} |
69 |
werner |
36 |
|
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 |
|
|
|
77 |
twoaday |
328 |
for (int i=0; hotkeys[i] != 0; i++) |
78 |
|
|
EnableWindow (GetDlgItem (dlg, hotkeys[i]), mode); |
79 |
werner |
36 |
} |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* En- or disable the backup items in the dialog @dlg. |
84 |
|
|
If val != 0 activate them, disable them otherwise. */ |
85 |
|
|
static void |
86 |
|
|
enable_backup_items (HWND dlg, int val) |
87 |
|
|
{ |
88 |
|
|
int mode = val? TRUE : FALSE; |
89 |
|
|
|
90 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKHOME), mode); |
91 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKUSER), mode); |
92 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), mode); |
93 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), mode); |
94 |
twoaday |
328 |
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSECRING), mode); |
95 |
werner |
36 |
} |
96 |
|
|
|
97 |
twoaday |
176 |
/* Helper function to prepare dialog. */ |
98 |
|
|
static void |
99 |
|
|
on_init_dialog (HWND dlg) |
100 |
|
|
{ |
101 |
|
|
struct gettext_tab_s prefs[] = { |
102 |
|
|
{IDC_PREFS_VIEWER, _("Use clipboard &viewer to display the plaintext")}, |
103 |
twoaday |
328 |
{IDC_PREFS_WWINFO, _("Word wrap cleartext &signatures at column:")}, |
104 |
twoaday |
176 |
{IDC_PREFS_DISABLE_HOTKEYS, _("&Disable hotkeys (Not recommended!)")}, |
105 |
|
|
{IDC_PREFS_TRUST, _("Skip key validation and assume that keys are always fully trusted")}, |
106 |
|
|
{IDC_PREFS_AUTOBACKUP, _("&Automatic keyring backup when WinPT closes")}, |
107 |
|
|
{IDC_PREFS_BAKHOME, _("Backup to &keyring folder")}, |
108 |
|
|
{IDC_PREFS_BAKUSER, _("Backup to:")}, |
109 |
|
|
{IDC_PREFS_KEYLISTINF, _("Select &key list mode")}, |
110 |
|
|
{IDC_PREFS_KSINF, _("Keyserver &config")}, |
111 |
twoaday |
328 |
{IDC_PREFS_CACHEINF, _("Cache &passphrases for 'n' minutes:")}, |
112 |
twoaday |
176 |
{IDC_PREFS_CACHEHINT, _("(CTRL+ALT+F to clear the cache)")}, |
113 |
|
|
{IDC_PREFS_ALLOPTINF, _("General options")}, |
114 |
twoaday |
328 |
{IDC_PREFS_CLIPINF, _("Clipboard hotkeys CTRL+ALT")}, |
115 |
|
|
{IDC_PREFS_CURRINF, _("Current window hotkeys CTRL+SHIFT")}, |
116 |
twoaday |
176 |
{IDC_PREFS_KSELFILE, _("Browse...")}, |
117 |
|
|
{IDC_PREFS_BAKSELECT, _("Browse...")}, |
118 |
twoaday |
328 |
{IDC_PREFS_EXTINF, _("Default extension for encrypted files:")}, |
119 |
twoaday |
222 |
{IDC_PREFS_BAKSECRING, _("&Backup includes secret keyring")}, |
120 |
twoaday |
328 |
|
121 |
|
|
{IDC_PREFS_CLIP_HT_ENC, _("Encrypt")}, |
122 |
|
|
{IDC_PREFS_CLIP_HT_DEC, _("Decrypt/Verify")}, |
123 |
|
|
{IDC_PREFS_CLIP_HT_SIG, _("Sign")}, |
124 |
|
|
{IDC_PREFS_CLIP_HT_SAE, _("Sign && Encrypt")}, |
125 |
|
|
{IDC_PREFS_CW_HT_ENC, _("Encrypt")}, |
126 |
|
|
{IDC_PREFS_CW_HT_DEC, _("Decrypt/Verify")}, |
127 |
|
|
{IDC_PREFS_CW_HT_SIG, _("Sign")}, |
128 |
|
|
{IDC_PREFS_CW_HT_SAE, _("Sign && Encrypt")}, |
129 |
twoaday |
176 |
{IDCANCEL, _("&Cancel")}, |
130 |
twoaday |
328 |
{0, NULL}}; |
131 |
twoaday |
176 |
int i; |
132 |
twoaday |
218 |
HWND cb; |
133 |
twoaday |
176 |
|
134 |
|
|
gettext_localize_dialog (dlg, prefs, _("WinPT Preferences")); |
135 |
twoaday |
180 |
|
136 |
|
|
SetDlgItemInt (dlg, IDC_PREFS_CACHETIME, reg_prefs.cache_time/60, TRUE); |
137 |
twoaday |
176 |
SetDlgItemInt (dlg, IDC_PREFS_WORDWRAP, reg_prefs.word_wrap, TRUE); |
138 |
|
|
if (reg_prefs.backup.path) |
139 |
|
|
SetDlgItemText (dlg, IDC_PREFS_BAKPATH, reg_prefs.backup.path); |
140 |
|
|
|
141 |
twoaday |
180 |
for (i=0; hotkeys[i]; i++) { |
142 |
|
|
if (!wpt_hotkeys[i].enabled) |
143 |
|
|
continue; |
144 |
|
|
char key[2]; |
145 |
|
|
key[0] = wpt_hotkeys[i].key; |
146 |
|
|
key[1] = 0; |
147 |
|
|
SetDlgItemText (dlg, hotkeys[i], key); |
148 |
|
|
} |
149 |
twoaday |
218 |
cb = GetDlgItem (dlg, IDC_PREFS_DEFEXT); |
150 |
twoaday |
222 |
combox_add_string (cb, "GnuPG (.gpg)"); |
151 |
|
|
combox_add_string (cb, "PGP (.pgp)"); |
152 |
twoaday |
218 |
SendMessage (cb, CB_SETCURSEL, 0, 0); |
153 |
|
|
|
154 |
twoaday |
176 |
CheckDlgButton (dlg, IDC_PREFS_DISABLE_HOTKEYS, |
155 |
|
|
reg_prefs.no_hotkeys ? BST_CHECKED : BST_UNCHECKED); |
156 |
|
|
CheckDlgButton (dlg, IDC_PREFS_VIEWER, |
157 |
|
|
reg_prefs.use_viewer ? BST_CHECKED: BST_UNCHECKED); |
158 |
|
|
CheckDlgButton (dlg, IDC_PREFS_TRUST, |
159 |
|
|
reg_prefs.always_trust? BST_CHECKED : BST_UNCHECKED); |
160 |
|
|
CheckDlgButton (dlg, IDC_PREFS_AUTOBACKUP, |
161 |
|
|
reg_prefs.auto_backup? BST_CHECKED : BST_UNCHECKED); |
162 |
|
|
CheckDlgButton (dlg, IDC_PREFS_BAKHOME, |
163 |
|
|
reg_prefs.backup.mode==1? BST_CHECKED : BST_UNCHECKED); |
164 |
|
|
CheckDlgButton (dlg, IDC_PREFS_BAKUSER, |
165 |
|
|
reg_prefs.backup.mode==2? BST_CHECKED : BST_UNCHECKED); |
166 |
twoaday |
222 |
CheckDlgButton (dlg, IDC_PREFS_BAKSECRING, |
167 |
|
|
reg_prefs.backup.include_secr? BST_CHECKED :BST_UNCHECKED); |
168 |
twoaday |
218 |
SendDlgItemMessage (dlg, IDC_PREFS_DEFEXT, CB_SETCURSEL, |
169 |
|
|
reg_prefs.default_ext, 0); |
170 |
twoaday |
176 |
if (reg_prefs.no_hotkeys) |
171 |
|
|
disable_hotkey_items (dlg, 1); |
172 |
|
|
if (!reg_prefs.auto_backup) |
173 |
|
|
enable_backup_items (dlg, 0); |
174 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), |
175 |
|
|
reg_prefs.backup.mode==1?FALSE : TRUE); |
176 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), |
177 |
|
|
reg_prefs.backup.mode==1? FALSE : TRUE); |
178 |
|
|
center_window (dlg, NULL); |
179 |
|
|
SetForegroundWindow (dlg); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
|
183 |
werner |
36 |
/* Dialog box procedure for the WinPT preferences. */ |
184 |
|
|
BOOL CALLBACK |
185 |
|
|
prefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
186 |
|
|
{ |
187 |
|
|
int rc; |
188 |
twoaday |
180 |
char t[3]; |
189 |
twoaday |
176 |
char path[256], *p; |
190 |
werner |
36 |
|
191 |
twoaday |
88 |
switch (msg) { |
192 |
werner |
36 |
case WM_INITDIALOG: |
193 |
twoaday |
176 |
on_init_dialog (dlg); |
194 |
werner |
36 |
return TRUE; |
195 |
|
|
|
196 |
twoaday |
328 |
case WM_DESTROY: |
197 |
|
|
balloon_msg_disable (); |
198 |
|
|
break; |
199 |
|
|
|
200 |
werner |
36 |
case WM_COMMAND: |
201 |
twoaday |
180 |
switch (HIWORD (wparam)) { |
202 |
werner |
36 |
case BN_CLICKED: |
203 |
twoaday |
180 |
switch ((int)LOWORD (wparam)) { |
204 |
werner |
36 |
case IDC_PREFS_DISABLE_HOTKEYS: |
205 |
|
|
reg_prefs.no_hotkeys ^= 1; |
206 |
twoaday |
117 |
disable_hotkey_items (dlg, reg_prefs.no_hotkeys); |
207 |
werner |
36 |
break; |
208 |
|
|
|
209 |
|
|
case IDC_PREFS_TRUST: |
210 |
|
|
reg_prefs.always_trust ^= 1; |
211 |
twoaday |
117 |
if (reg_prefs.always_trust) { |
212 |
werner |
36 |
msg_box (dlg, |
213 |
|
|
_("In most cases it is not a good idea to enable this setting.\n" |
214 |
twoaday |
248 |
"If you know what you are doing, you can enable this flag, otherwise\n" |
215 |
werner |
36 |
"it is safe to leave this flag untouched."), |
216 |
twoaday |
328 |
_("Preferences"), MB_WARN); |
217 |
werner |
36 |
} |
218 |
|
|
break; |
219 |
|
|
|
220 |
|
|
case IDC_PREFS_AUTOBACKUP: |
221 |
|
|
reg_prefs.auto_backup ^= 1; |
222 |
|
|
enable_backup_items (dlg, reg_prefs.auto_backup); |
223 |
|
|
if (reg_prefs.auto_backup == 1) { |
224 |
|
|
if (!IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) && |
225 |
|
|
!IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER)) |
226 |
|
|
CheckDlgButton (dlg, IDC_PREFS_BAKHOME, BST_CHECKED); |
227 |
|
|
} |
228 |
twoaday |
130 |
if (IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME)) |
229 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE); |
230 |
werner |
36 |
break; |
231 |
|
|
|
232 |
|
|
case IDC_PREFS_BAKHOME: |
233 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), FALSE); |
234 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE); |
235 |
|
|
break; |
236 |
|
|
|
237 |
|
|
case IDC_PREFS_BAKUSER: |
238 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), TRUE); |
239 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), TRUE); |
240 |
|
|
break; |
241 |
|
|
} |
242 |
|
|
break; |
243 |
|
|
} |
244 |
twoaday |
180 |
switch (LOWORD (wparam)) { |
245 |
werner |
36 |
case IDC_PREFS_BAKSELECT: |
246 |
|
|
const char *bpath; |
247 |
|
|
bpath = get_folder_dlg (dlg, _("Select GPG backup path"), NULL); |
248 |
|
|
if (bpath) |
249 |
|
|
SetDlgItemText (dlg, IDC_PREFS_BAKPATH, bpath); |
250 |
|
|
break; |
251 |
|
|
|
252 |
|
|
case IDOK: |
253 |
twoaday |
180 |
rc = GetDlgItemInt (dlg, IDC_PREFS_CACHETIME, NULL, FALSE); |
254 |
twoaday |
181 |
if (rc <= 0) { |
255 |
werner |
36 |
reg_prefs.cache_time = 0; |
256 |
twoaday |
181 |
agent_flush_cache (); |
257 |
|
|
} |
258 |
twoaday |
180 |
else if (rc > 720) { |
259 |
twoaday |
328 |
show_balloon_msg (GetDlgItem (dlg, IDC_PREFS_CACHETIME), |
260 |
|
|
_("Please enter a value that is between 1-720.\nIt is not " |
261 |
|
|
"a good idea to cache the passphrase more than 12 hours."), |
262 |
|
|
IDI_ERROR); |
263 |
twoaday |
117 |
SetDlgItemInt (dlg, IDC_PREFS_CACHETIME, 0, FALSE); |
264 |
twoaday |
328 |
return FALSE; |
265 |
werner |
36 |
} |
266 |
|
|
if (reg_prefs.cache_time != rc) |
267 |
|
|
agent_flush_cache (); |
268 |
twoaday |
180 |
reg_prefs.cache_time = rc*60; |
269 |
twoaday |
117 |
rc = GetDlgItemInt (dlg, IDC_PREFS_WORDWRAP, NULL, FALSE); |
270 |
twoaday |
180 |
if (rc <= 0) |
271 |
werner |
36 |
reg_prefs.word_wrap = 0; |
272 |
twoaday |
117 |
else if (rc > 80) { |
273 |
twoaday |
328 |
show_balloon_msg (GetDlgItem (dlg, IDC_PREFS_WORDWRAP), |
274 |
|
|
_("Please enter a value between 1-80."), |
275 |
|
|
IDI_ERROR); |
276 |
|
|
return FALSE; |
277 |
werner |
36 |
} |
278 |
twoaday |
218 |
reg_prefs.default_ext = SendDlgItemMessage (dlg, IDC_PREFS_DEFEXT, CB_GETCURSEL, 0, 0); |
279 |
werner |
36 |
reg_prefs.word_wrap = rc; |
280 |
twoaday |
180 |
reg_prefs.use_viewer = IsDlgButtonChecked (dlg, IDC_PREFS_VIEWER)? 1 : 0; |
281 |
|
|
reg_prefs.backup.mode = IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME ) ? 1 : |
282 |
|
|
IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER)? 2 : 0; |
283 |
twoaday |
222 |
reg_prefs.backup.include_secr = IsDlgButtonChecked (dlg, IDC_PREFS_BAKSECRING)? 1 : 0; |
284 |
twoaday |
180 |
if (reg_prefs.backup.mode == 2) { |
285 |
twoaday |
328 |
if (!GetDlgItemText (dlg, IDC_PREFS_BAKPATH, path, DIM (path)-1) |
286 |
twoaday |
180 |
|| dir_exist_check (path)) { |
287 |
twoaday |
328 |
show_balloon_msg (GetDlgItem (dlg, IDC_PREFS_BAKPATH), |
288 |
|
|
_("The specified backup folder is invalid."), |
289 |
|
|
IDI_ERROR); |
290 |
|
|
return FALSE; |
291 |
werner |
36 |
} |
292 |
|
|
free_if_alloc (reg_prefs.backup.path); |
293 |
|
|
p = reg_prefs.backup.path = m_strdup (path); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
if (IsDlgButtonChecked (dlg, IDC_PREFS_DISABLE_HOTKEYS)) { |
297 |
|
|
hotkeys_unregister (glob_hwnd); |
298 |
|
|
reg_prefs.no_hotkeys = 1; |
299 |
|
|
} |
300 |
|
|
else { |
301 |
|
|
reg_prefs.no_hotkeys = 0; |
302 |
twoaday |
328 |
for (int i = 0; hotkeys[i] != 0; i++) { |
303 |
|
|
rc = GetDlgItemText (dlg, hotkeys[i], t, DIM (t)-1); |
304 |
twoaday |
180 |
if (rc > 0 && check_hotkey (&t[0])) |
305 |
|
|
hotkey_enable (&wpt_hotkeys[i], t); |
306 |
werner |
36 |
else |
307 |
twoaday |
180 |
hotkey_disable (&wpt_hotkeys[i]); |
308 |
werner |
36 |
} |
309 |
|
|
set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "DisableHotkeys", "0"); |
310 |
|
|
reg_prefs.no_hotkeys = 0; |
311 |
|
|
} |
312 |
|
|
|
313 |
twoaday |
180 |
rc = set_reg_winpt_prefs (®_prefs); |
314 |
|
|
if (rc) |
315 |
werner |
36 |
msg_box (dlg, winpt_strerror (rc), _("Preferences"), MB_ERR); |
316 |
|
|
|
317 |
|
|
if (reg_prefs.no_hotkeys == 0) { |
318 |
|
|
hotkeys_unregister (glob_hwnd); |
319 |
twoaday |
180 |
rc = hotkeys_register (glob_hwnd); |
320 |
|
|
if (rc) |
321 |
werner |
36 |
msg_box (NULL, winpt_strerror (rc), _("Hotkeys"), MB_ERR); |
322 |
|
|
} |
323 |
|
|
EndDialog (dlg, TRUE); |
324 |
|
|
return TRUE; |
325 |
|
|
|
326 |
|
|
case IDCANCEL: |
327 |
twoaday |
117 |
/* Reset backup mode if no mode was chosen. */ |
328 |
|
|
if (reg_prefs.auto_backup == 1 && |
329 |
|
|
!IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) && |
330 |
|
|
!IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER)) |
331 |
|
|
reg_prefs.auto_backup = 0; |
332 |
|
|
EndDialog (dlg, FALSE); |
333 |
werner |
36 |
return FALSE; |
334 |
|
|
} |
335 |
|
|
break; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
return FALSE; |
339 |
|
|
} |