1 |
/* wptGPGPrefsDlg.cpp - GnuPG Preferences |
2 |
* Copyright (C) 2001-2006, 2007 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 |
|
27 |
#include "resource.h" |
28 |
#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 |
/* Enable all edit items if @val is TRUE, disable them otherwise. */ |
38 |
static void |
39 |
activate_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 |
/* 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 |
free_if_alloc (path); |
72 |
|
73 |
path = get_reg_entry_gpg ("HomeDir"); |
74 |
p = multi_gnupg_path (1); |
75 |
if (path && dir_exist_check (path) == 0 && p && strcmp (path, p)) { |
76 |
/* The 'HomeDir' has a higher priority so if the key exists and is |
77 |
different from the multi user path, we force the use of it. */ |
78 |
free_if_alloc (p); |
79 |
p = path; |
80 |
} |
81 |
else |
82 |
free_if_alloc (path); |
83 |
if (p) { |
84 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
85 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE); |
86 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE); |
87 |
free_if_alloc (p); |
88 |
} |
89 |
return true; |
90 |
} |
91 |
|
92 |
|
93 |
/* Load the GPG values direct from the registry. */ |
94 |
static void |
95 |
load_registry_values (HWND dlg) |
96 |
{ |
97 |
char *p; |
98 |
|
99 |
p = get_reg_entry_gpg ("HomeDir"); |
100 |
if (p) { |
101 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
102 |
free_if_alloc (p); |
103 |
} |
104 |
p = get_reg_entry_gpg ("gpgProgram"); |
105 |
if (p) { |
106 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
107 |
free_if_alloc (p); |
108 |
} |
109 |
|
110 |
/* if there is no 'HomeDir' registry value but an existing |
111 |
GPG multipath, we use this as the home dir instead. */ |
112 |
if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) { |
113 |
p = multi_gnupg_path (0); |
114 |
if (p && dir_exist_check (p) == 0) |
115 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
116 |
free_if_alloc (p); |
117 |
} |
118 |
} |
119 |
|
120 |
|
121 |
/* Dialog box procedure for the GPG preferences. */ |
122 |
BOOL CALLBACK |
123 |
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
124 |
{ |
125 |
static int gpg4win = false; |
126 |
char exedir[512]; |
127 |
char homedir[512]; |
128 |
char *p = NULL, t[256]; |
129 |
const char *s; |
130 |
const char *folder; |
131 |
config_file_t opt = NULL; |
132 |
conf_option_t e; |
133 |
UINT n; |
134 |
|
135 |
switch (msg) { |
136 |
case WM_INITDIALOG: |
137 |
SetWindowText (dlg, _("GnuPG Preferences")); |
138 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF, |
139 |
_("GnuPG home directory (where both keyrings are located)")); |
140 |
SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF, |
141 |
_("GnuPG exe file location (full path with added gpg.exe)")); |
142 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign")); |
143 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKEXPIRE, _("Allow to set an expiration date for signatures")); |
144 |
SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files")); |
145 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
146 |
SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options")); |
147 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse...")); |
148 |
SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse...")); |
149 |
SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings")); |
150 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
151 |
|
152 |
gpg4win = load_gpg4win_values (dlg); |
153 |
if (!gpg4win) { |
154 |
load_registry_values (dlg); |
155 |
CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED); |
156 |
} |
157 |
|
158 |
p = get_gnupg_cfgfile (); |
159 |
if (p) { |
160 |
parse_config (p, &opt); |
161 |
free_if_alloc (p); |
162 |
if (opt) { |
163 |
if (conf_find_option (opt, "ask-cert-expire")) |
164 |
CheckDlgButton (dlg, IDC_GPGPREFS_ASKEXPIRE, BST_CHECKED); |
165 |
if (conf_find_option (opt, "ask-cert-level")) |
166 |
CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED); |
167 |
e = conf_find_option (opt, "comment"); |
168 |
if (e != NULL) |
169 |
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
170 |
e = conf_find_option (opt, "encrypt-to"); |
171 |
if (e != NULL) |
172 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
173 |
release_config (opt); |
174 |
} |
175 |
} |
176 |
|
177 |
#ifdef WINPT_MOBILE |
178 |
/* In mobile mode we do not allow to change the default settings |
179 |
because this would not make any sense. */ |
180 |
CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_UNCHECKED); |
181 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_OVRDEFAULT), FALSE); |
182 |
activate_items (dlg, FALSE); |
183 |
#endif |
184 |
|
185 |
center_window (dlg, NULL); |
186 |
SetForegroundWindow (dlg); |
187 |
return TRUE; |
188 |
|
189 |
case WM_DESTROY: |
190 |
gpg4win = false; |
191 |
break; |
192 |
|
193 |
case WM_COMMAND: |
194 |
if (HIWORD (wparam) == BN_CLICKED && |
195 |
LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) { |
196 |
if (IsDlgButtonChecked (dlg, LOWORD (wparam))) { |
197 |
activate_items (dlg, TRUE); |
198 |
gpg4win = false; |
199 |
} |
200 |
else { |
201 |
activate_items (dlg, FALSE); |
202 |
gpg4win = true; |
203 |
} |
204 |
break; |
205 |
} |
206 |
|
207 |
switch (LOWORD (wparam)) { |
208 |
case IDC_GPGPREFS_SAVE: |
209 |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, |
210 |
homedir, DIM (homedir) -1)) { |
211 |
msg_box (dlg, _("Please enter the GnuPG home directory."), |
212 |
_("Preferences"), MB_ERR); |
213 |
return FALSE; |
214 |
} |
215 |
if (dir_exist_check (homedir)) { |
216 |
_snprintf (t, DIM (t) - 1, "%s: %s", homedir, |
217 |
winpt_strerror (WPTERR_DIR_OPEN)); |
218 |
msg_box (dlg, t, _("Preferences"), MB_ERR); |
219 |
return FALSE; |
220 |
} |
221 |
|
222 |
if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) { |
223 |
msg_box (dlg, _("Could not save 'HomeDir' in the registry."), |
224 |
_("Preferences"), MB_ERR); |
225 |
return FALSE; |
226 |
} |
227 |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
228 |
exedir, DIM (exedir) -1)) { |
229 |
msg_box (dlg, _("Please enter where GPG.exe is located."), |
230 |
_("Preferences"), MB_ERR); |
231 |
return FALSE; |
232 |
} |
233 |
if (file_exist_check (exedir)) { |
234 |
msg_box (dlg, _("Could not find the GPG program in this directory."), |
235 |
_("Preferences"), MB_ERR); |
236 |
return FALSE; |
237 |
} |
238 |
if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) { |
239 |
msg_box (dlg, _("Could not save 'gpgProgram' in the registry"), |
240 |
_("Preferences"), MB_ERR); |
241 |
return FALSE; |
242 |
} |
243 |
|
244 |
p = get_gnupg_cfgfile (); |
245 |
if (!p) { |
246 |
msg_box (dlg, _("Could not get GPG config file"), |
247 |
_("Preferences"), MB_ERR); |
248 |
EndDialog (dlg, FALSE); |
249 |
return TRUE; |
250 |
} |
251 |
parse_config (p, &opt); |
252 |
|
253 |
if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKLEVEL)) { |
254 |
conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-level", NULL); |
255 |
reg_prefs.gpg.ask_cert_level = 1; |
256 |
} |
257 |
else { |
258 |
conf_delete_option (opt, "ask-cert-level"); |
259 |
reg_prefs.gpg.ask_cert_level = 0; |
260 |
} |
261 |
if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKEXPIRE)) { |
262 |
conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-expire", NULL); |
263 |
reg_prefs.gpg.ask_cert_expire = 1; |
264 |
} |
265 |
else { |
266 |
conf_delete_option (opt, "ask-cert-expire"); |
267 |
reg_prefs.gpg.ask_cert_expire = 0; |
268 |
} |
269 |
|
270 |
n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, DIM (t)-1); |
271 |
if (n > 0) |
272 |
conf_modify_entry (opt, ENTRY_MULTI, "comment", t); |
273 |
else if (n == 0) |
274 |
conf_modify_entry (opt, ENTRY_MULTI, "comment", "\"\""); |
275 |
else |
276 |
conf_delete_option (opt, "comment"); |
277 |
|
278 |
n = GetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, t, DIM (t)-1); |
279 |
if (n > 0) |
280 |
conf_modify_entry (opt, ENTRY_MULTI, "encrypt-to", t); |
281 |
else |
282 |
conf_delete_option (opt, "encrypt-to"); |
283 |
|
284 |
commit_config (p, opt); |
285 |
release_config (opt); |
286 |
/* only return TRUE if the home dir has been changed. */ |
287 |
if (SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, EM_GETMODIFY, 0, 0)) { |
288 |
set_gnupg_default_key (NULL); |
289 |
EndDialog (dlg, TRUE); |
290 |
} |
291 |
else |
292 |
EndDialog (dlg, FALSE); |
293 |
return TRUE; |
294 |
|
295 |
case IDC_GPGPREFS_HOMEDLG: |
296 |
folder = get_folder_dlg (dlg, _("Choose GPG Home Directory"), NULL); |
297 |
if (folder) { |
298 |
char *name; |
299 |
|
300 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder); |
301 |
SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, |
302 |
EM_SETMODIFY, (WPARAM)(UINT)TRUE, 0); |
303 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
304 |
exedir, DIM (exedir)-1) > 0) |
305 |
break; |
306 |
name = make_filename (folder, "gpg", "exe"); |
307 |
if (file_exist_check (name) == 0) |
308 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
309 |
free_if_alloc (name); |
310 |
} |
311 |
return TRUE; |
312 |
|
313 |
case IDC_GPGREFS_EXEDLG: |
314 |
// TODO: use gpgme_ctx_set_engine_info instead. |
315 |
n = msg_box (dlg, _("Please be aware that the modifcation of " |
316 |
"the value will affect all installed " |
317 |
"programs which use GPG via the " |
318 |
"GPGME interface library\n\n" |
319 |
"Do you really want to continue?"), |
320 |
_("GPG WARNING"), MB_WARN_ASK); |
321 |
if (n != IDYES) |
322 |
return TRUE; |
323 |
|
324 |
s = get_fileopen_dlg (dlg, _("Choose GPG Binary"), |
325 |
"Executable Files (*.exe)\0*.exe\0\0", |
326 |
NULL); |
327 |
if (s) |
328 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s); |
329 |
return TRUE; |
330 |
|
331 |
case IDCANCEL: |
332 |
EndDialog (dlg, FALSE); |
333 |
return TRUE; |
334 |
} |
335 |
break; |
336 |
} |
337 |
|
338 |
return FALSE; |
339 |
} |