1 |
twoaday |
128 |
/* wptGPGPrefsDlg.cpp - GnuPG Preferences |
2 |
twoaday |
241 |
* Copyright (C) 2001-2006 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 |
|
|
* 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 |
twoaday |
128 |
|
21 |
werner |
42 |
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
werner |
36 |
#include <windows.h> |
26 |
|
|
|
27 |
werner |
47 |
#include "resource.h" |
28 |
werner |
36 |
#include "wptNLS.h" |
29 |
|
|
#include "wptTypes.h" |
30 |
|
|
#include "wptErrors.h" |
31 |
|
|
#include "wptGPG.h" |
32 |
|
|
#include "wptRegistry.h" |
33 |
|
|
#include "wptCommonCtl.h" |
34 |
|
|
#include "wptW32API.h" |
35 |
|
|
|
36 |
|
|
|
37 |
twoaday |
161 |
/* Enable all edit items if @val is TRUE, disable them otherwise. */ |
38 |
|
|
static void |
39 |
|
|
enable_disable_items (HWND dlg, BOOL val) |
40 |
|
|
{ |
41 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_EXEDIR), val); |
42 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGREFS_EXEDLG), val); |
43 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCALE), val); |
44 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCDLG), val); |
45 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), val); |
46 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), val); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
twoaday |
128 |
/* Load the GPG4WIN default values and disabled the |
51 |
|
|
dialog items to indicate these are fixed values. |
52 |
|
|
Return value: true if GPG4win was found. */ |
53 |
|
|
static bool |
54 |
|
|
load_gpg4win_values (HWND dlg) |
55 |
|
|
{ |
56 |
|
|
char *path; |
57 |
|
|
char *p; |
58 |
|
|
|
59 |
|
|
path = get_reg_entry_gpg4win (NULL); |
60 |
|
|
if (!path) |
61 |
|
|
return false; |
62 |
|
|
p = make_filename (path, "gpg", "exe"); |
63 |
|
|
if (p) { |
64 |
|
|
if (file_exist_check (p) == 0) { |
65 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
66 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_EXEDIR), FALSE); |
67 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGREFS_EXEDLG), FALSE); |
68 |
|
|
} |
69 |
|
|
free_if_alloc (p); |
70 |
|
|
} |
71 |
twoaday |
241 |
free_if_alloc (path); |
72 |
twoaday |
128 |
|
73 |
|
|
p = get_reg_entry_mo (); |
74 |
|
|
if (p) { |
75 |
|
|
if (dir_exist_check (p) == 0) { |
76 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
77 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCALE), FALSE); |
78 |
twoaday |
139 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCDLG), FALSE); |
79 |
twoaday |
128 |
} |
80 |
|
|
free_if_alloc (p); |
81 |
|
|
} |
82 |
twoaday |
241 |
|
83 |
|
|
path = get_reg_entry_gpg ("HomeDir"); |
84 |
twoaday |
128 |
p = multi_gnupg_path (1); |
85 |
twoaday |
241 |
if (path && dir_exist_check (path) == 0 && p && strcmp (path, p)) { |
86 |
|
|
/* The 'HomeDir' has a higher priority so if the key exists and is |
87 |
|
|
different from the multi user path, we force the use of it. */ |
88 |
|
|
free_if_alloc (p); |
89 |
|
|
p = path; |
90 |
|
|
} |
91 |
|
|
else |
92 |
|
|
free_if_alloc (path); |
93 |
twoaday |
128 |
if (p) { |
94 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
95 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE); |
96 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE); |
97 |
|
|
free_if_alloc (p); |
98 |
|
|
} |
99 |
|
|
return true; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/* Load the GPG values direct from the registry. */ |
104 |
|
|
static void |
105 |
|
|
load_registry_values (HWND dlg) |
106 |
|
|
{ |
107 |
|
|
char *p; |
108 |
|
|
|
109 |
|
|
p = get_reg_entry_gpg ("HomeDir"); |
110 |
|
|
if (p) { |
111 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
112 |
twoaday |
225 |
free_if_alloc (p); |
113 |
twoaday |
128 |
} |
114 |
|
|
p = get_reg_entry_gpg ("gpgProgram"); |
115 |
|
|
if (p) { |
116 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
117 |
twoaday |
225 |
free_if_alloc (p); |
118 |
twoaday |
128 |
} |
119 |
|
|
p = get_reg_entry_mo (); |
120 |
|
|
if (p) { |
121 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
122 |
twoaday |
225 |
free_if_alloc (p); |
123 |
twoaday |
128 |
} |
124 |
twoaday |
256 |
/* if there is no 'HomeDir' registry value but an existing |
125 |
|
|
GPG multipath, we use this as the home dir instead. */ |
126 |
twoaday |
128 |
if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) { |
127 |
|
|
p = multi_gnupg_path (0); |
128 |
|
|
if (p && dir_exist_check (p) == 0) |
129 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
130 |
|
|
free_if_alloc (p); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
twoaday |
41 |
/* Dialog box procedure for the GPG preferences. */ |
136 |
werner |
36 |
BOOL CALLBACK |
137 |
|
|
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
138 |
|
|
{ |
139 |
twoaday |
128 |
static int gpg4win = false; |
140 |
|
|
char exedir[512]; |
141 |
|
|
char homedir[512]; |
142 |
twoaday |
41 |
char locale_dir[512]; |
143 |
|
|
char *p = NULL, t[256]; |
144 |
|
|
const char *s; |
145 |
twoaday |
128 |
const char *folder; |
146 |
werner |
36 |
gpg_optfile_t opt = NULL; |
147 |
|
|
gpg_option_t e; |
148 |
|
|
UINT n; |
149 |
|
|
|
150 |
twoaday |
41 |
switch (msg) { |
151 |
werner |
36 |
case WM_INITDIALOG: |
152 |
twoaday |
41 |
SetWindowText (dlg, _("GnuPG Preferences")); |
153 |
twoaday |
117 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF, |
154 |
|
|
_("GnuPG home directory (where both keyrings are located)")); |
155 |
|
|
SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF, |
156 |
|
|
_("GnuPG exe file location (full path with added gpg.exe)")); |
157 |
|
|
SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF, |
158 |
|
|
_("Locale directory (to access the translation files)")); |
159 |
twoaday |
41 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign")); |
160 |
twoaday |
181 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKEXPIRE, _("Allow to set an expiration date for signatures")); |
161 |
twoaday |
117 |
SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files")); |
162 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
163 |
twoaday |
88 |
SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options")); |
164 |
twoaday |
147 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse...")); |
165 |
|
|
SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse...")); |
166 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCDLG, _("Browse...")); |
167 |
twoaday |
150 |
SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings")); |
168 |
twoaday |
177 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
169 |
twoaday |
117 |
|
170 |
twoaday |
128 |
gpg4win = load_gpg4win_values (dlg); |
171 |
twoaday |
161 |
if (!gpg4win) { |
172 |
twoaday |
128 |
load_registry_values (dlg); |
173 |
twoaday |
161 |
CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED); |
174 |
|
|
} |
175 |
twoaday |
128 |
|
176 |
twoaday |
41 |
p = get_gnupg_cfgfile (); |
177 |
|
|
if (p) { |
178 |
twoaday |
270 |
parse_config (p, &opt); |
179 |
twoaday |
41 |
free_if_alloc (p); |
180 |
twoaday |
128 |
if (opt) { |
181 |
twoaday |
181 |
if (find_option (opt, "ask-cert-expire")) |
182 |
|
|
CheckDlgButton (dlg, IDC_GPGPREFS_ASKEXPIRE, BST_CHECKED); |
183 |
twoaday |
128 |
if (find_option (opt, "ask-cert-level")) |
184 |
|
|
CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED); |
185 |
twoaday |
177 |
e = find_option (opt, "comment"); |
186 |
|
|
if (e != NULL) |
187 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
188 |
|
|
e = find_option (opt, "encrypt-to"); |
189 |
|
|
if (e != NULL) |
190 |
twoaday |
128 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
191 |
twoaday |
270 |
release_config (opt); |
192 |
twoaday |
128 |
} |
193 |
werner |
36 |
} |
194 |
twoaday |
117 |
|
195 |
twoaday |
41 |
center_window (dlg, NULL); |
196 |
|
|
SetForegroundWindow (dlg); |
197 |
werner |
36 |
return TRUE; |
198 |
twoaday |
128 |
|
199 |
|
|
case WM_DESTROY: |
200 |
|
|
gpg4win = false; |
201 |
|
|
break; |
202 |
werner |
36 |
|
203 |
|
|
case WM_COMMAND: |
204 |
twoaday |
161 |
if (HIWORD (wparam) == BN_CLICKED && |
205 |
|
|
LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) { |
206 |
twoaday |
165 |
if (IsDlgButtonChecked (dlg, LOWORD (wparam))) { |
207 |
twoaday |
161 |
enable_disable_items (dlg, TRUE); |
208 |
twoaday |
165 |
gpg4win = false; |
209 |
|
|
} |
210 |
|
|
else { |
211 |
twoaday |
161 |
enable_disable_items (dlg, FALSE); |
212 |
twoaday |
165 |
gpg4win = true; |
213 |
|
|
} |
214 |
twoaday |
161 |
break; |
215 |
|
|
} |
216 |
|
|
|
217 |
twoaday |
128 |
switch (LOWORD (wparam)) { |
218 |
werner |
36 |
case IDC_GPGPREFS_SAVE: |
219 |
twoaday |
128 |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, |
220 |
|
|
homedir, sizeof (homedir) -1)) { |
221 |
twoaday |
165 |
msg_box (dlg, _("Please enter the GnuPG home directory."), |
222 |
twoaday |
128 |
_("Preferences"), MB_ERR); |
223 |
werner |
36 |
return FALSE; |
224 |
|
|
} |
225 |
twoaday |
117 |
if (dir_exist_check (homedir)) { |
226 |
twoaday |
165 |
_snprintf (t, sizeof (t) - 1, "%s: %s", homedir, |
227 |
twoaday |
117 |
winpt_strerror (WPTERR_DIR_OPEN)); |
228 |
|
|
msg_box (dlg, t, _("Preferences"), MB_ERR); |
229 |
werner |
36 |
return FALSE; |
230 |
|
|
} |
231 |
|
|
|
232 |
twoaday |
128 |
if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) { |
233 |
|
|
msg_box (dlg, _("Could not save 'HomeDir' in the registry."), |
234 |
|
|
_("Preferences"), MB_ERR); |
235 |
werner |
36 |
return FALSE; |
236 |
|
|
} |
237 |
twoaday |
256 |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
238 |
twoaday |
128 |
exedir, sizeof (exedir) -1)) { |
239 |
twoaday |
256 |
msg_box (dlg, _("Please enter where GPG.exe is located."), |
240 |
twoaday |
128 |
_("Preferences"), MB_ERR); |
241 |
werner |
36 |
return FALSE; |
242 |
|
|
} |
243 |
twoaday |
128 |
if (file_exist_check (exedir)) { |
244 |
twoaday |
165 |
msg_box (dlg, _("Could not find the GPG program in this directory."), |
245 |
twoaday |
128 |
_("Preferences"), MB_ERR); |
246 |
werner |
36 |
return FALSE; |
247 |
|
|
} |
248 |
twoaday |
128 |
if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) { |
249 |
twoaday |
165 |
msg_box (dlg, _("Could not save 'gpgProgram' in the registry"), |
250 |
twoaday |
128 |
_("Preferences"), MB_ERR); |
251 |
werner |
36 |
return FALSE; |
252 |
|
|
} |
253 |
twoaday |
256 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, |
254 |
twoaday |
165 |
locale_dir, sizeof (locale_dir) -1) > 0) { |
255 |
werner |
36 |
if (dir_exist_check (locale_dir)) { |
256 |
twoaday |
128 |
log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir, |
257 |
|
|
winpt_strerror (WPTERR_DIR_OPEN)); |
258 |
werner |
36 |
return FALSE; |
259 |
|
|
} |
260 |
twoaday |
128 |
if (!gpg4win) |
261 |
|
|
set_reg_entry_mo (locale_dir); |
262 |
werner |
36 |
set_gettext_file ("winpt", locale_dir); |
263 |
|
|
} |
264 |
twoaday |
128 |
else if (!gpg4win) |
265 |
werner |
36 |
set_reg_entry_mo (""); |
266 |
|
|
|
267 |
|
|
p = get_gnupg_cfgfile (); |
268 |
twoaday |
128 |
if (!p) { |
269 |
|
|
msg_box (dlg, _("Could not get GPG config file"), |
270 |
|
|
_("Preferences"), MB_ERR); |
271 |
|
|
EndDialog (dlg, FALSE); |
272 |
twoaday |
225 |
return TRUE; |
273 |
werner |
36 |
} |
274 |
twoaday |
270 |
parse_config (p, &opt); |
275 |
werner |
36 |
|
276 |
twoaday |
165 |
if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKLEVEL)) { |
277 |
|
|
modify_entry (opt, ENTRY_SINGLE, "ask-cert-level", NULL); |
278 |
twoaday |
41 |
reg_prefs.gpg.ask_cert_level = 1; |
279 |
|
|
} |
280 |
|
|
else { |
281 |
twoaday |
128 |
delete_option (opt, "ask-cert-level"); |
282 |
twoaday |
41 |
reg_prefs.gpg.ask_cert_level = 0; |
283 |
|
|
} |
284 |
twoaday |
181 |
if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKEXPIRE)) { |
285 |
|
|
modify_entry (opt, ENTRY_SINGLE, "ask-cert-expire", NULL); |
286 |
|
|
reg_prefs.gpg.ask_cert_expire = 1; |
287 |
|
|
} |
288 |
|
|
else { |
289 |
|
|
delete_option (opt, "ask-cert-expire"); |
290 |
|
|
reg_prefs.gpg.ask_cert_expire = 0; |
291 |
|
|
} |
292 |
|
|
|
293 |
twoaday |
225 |
n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, DIM (t)-1); |
294 |
twoaday |
165 |
if (n > 0) |
295 |
|
|
modify_entry (opt, ENTRY_MULTI, "comment", t); |
296 |
|
|
else if (n == 0) |
297 |
|
|
modify_entry (opt, ENTRY_MULTI, "comment", "\"\""); |
298 |
werner |
36 |
else |
299 |
twoaday |
165 |
delete_option (opt, "comment"); |
300 |
werner |
36 |
|
301 |
twoaday |
225 |
n = GetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, t, DIM (t)-1); |
302 |
twoaday |
165 |
if (n > 0) |
303 |
|
|
modify_entry (opt, ENTRY_MULTI, "encrypt-to", t); |
304 |
werner |
36 |
else |
305 |
twoaday |
165 |
delete_option (opt, "encrypt-to"); |
306 |
werner |
36 |
|
307 |
twoaday |
270 |
commit_config (p, opt); |
308 |
|
|
release_config (opt); |
309 |
twoaday |
197 |
/* only return TRUE if the home dir has been changed. */ |
310 |
|
|
if (SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, EM_GETMODIFY, 0, 0)) { |
311 |
|
|
set_gnupg_default_key (NULL); |
312 |
|
|
EndDialog (dlg, TRUE); |
313 |
|
|
} |
314 |
|
|
else |
315 |
|
|
EndDialog (dlg, FALSE); |
316 |
werner |
36 |
return TRUE; |
317 |
|
|
|
318 |
|
|
case IDC_GPGPREFS_HOMEDLG: |
319 |
twoaday |
197 |
folder = get_folder_dlg (dlg, _("Choose GPG Home Directory"), NULL); |
320 |
twoaday |
128 |
if (folder) { |
321 |
|
|
char *name; |
322 |
|
|
|
323 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder); |
324 |
twoaday |
197 |
SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, |
325 |
|
|
EM_SETMODIFY, (WPARAM)(UINT)TRUE, 0); |
326 |
twoaday |
256 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
327 |
twoaday |
165 |
exedir, DIM (exedir)-1) > 0) |
328 |
werner |
36 |
break; |
329 |
twoaday |
128 |
name = make_filename (folder, "gpg", "exe"); |
330 |
werner |
36 |
if (file_exist_check (name) == 0) |
331 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
332 |
|
|
free_if_alloc (name); |
333 |
|
|
} |
334 |
twoaday |
128 |
return TRUE; |
335 |
werner |
36 |
|
336 |
twoaday |
128 |
case IDC_GPGPREFS_LOCDLG: |
337 |
twoaday |
193 |
folder = get_folder_dlg (dlg, _("Choose Locale Directory"), NULL); |
338 |
twoaday |
128 |
if (folder) |
339 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder); |
340 |
|
|
return TRUE; |
341 |
|
|
|
342 |
werner |
36 |
case IDC_GPGREFS_EXEDLG: |
343 |
twoaday |
197 |
s = get_fileopen_dlg (dlg, _("Choose GPG Binary"), |
344 |
twoaday |
167 |
"Executable Files (*.exe)\0*.exe\0\0", |
345 |
twoaday |
128 |
NULL); |
346 |
|
|
if (s) |
347 |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s); |
348 |
werner |
36 |
return TRUE; |
349 |
twoaday |
130 |
|
350 |
|
|
case IDCANCEL: |
351 |
|
|
EndDialog (dlg, FALSE); |
352 |
|
|
return TRUE; |
353 |
werner |
36 |
} |
354 |
|
|
break; |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
return FALSE; |
358 |
twoaday |
41 |
} |