1 |
/* wptGPGPrefsDlg.cpp - GnuPG Preferences |
2 |
* Copyright (C) 2001-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 |
|
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 |
/* Load the GPG4WIN default values and disabled the |
38 |
dialog items to indicate these are fixed values. |
39 |
Return value: true if GPG4win was found. */ |
40 |
static bool |
41 |
load_gpg4win_values (HWND dlg) |
42 |
{ |
43 |
char *path; |
44 |
char *p; |
45 |
|
46 |
path = get_reg_entry_gpg4win (NULL); |
47 |
if (!path) |
48 |
return false; |
49 |
p = make_filename (path, "gpg", "exe"); |
50 |
if (p) { |
51 |
if (file_exist_check (p) == 0) { |
52 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
53 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_EXEDIR), FALSE); |
54 |
EnableWindow (GetDlgItem (dlg, IDC_GPGREFS_EXEDLG), FALSE); |
55 |
} |
56 |
free_if_alloc (p); |
57 |
} |
58 |
|
59 |
p = get_reg_entry_mo (); |
60 |
if (p) { |
61 |
if (dir_exist_check (p) == 0) { |
62 |
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
63 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCALE), FALSE); |
64 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCDLG), FALSE); |
65 |
} |
66 |
free_if_alloc (p); |
67 |
} |
68 |
|
69 |
p = multi_gnupg_path (1); |
70 |
if (p) { |
71 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
72 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE); |
73 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE); |
74 |
free_if_alloc (p); |
75 |
} |
76 |
|
77 |
return true; |
78 |
} |
79 |
|
80 |
|
81 |
/* Load the GPG values direct from the registry. */ |
82 |
static void |
83 |
load_registry_values (HWND dlg) |
84 |
{ |
85 |
char *p; |
86 |
|
87 |
p = get_reg_entry_gpg ("HomeDir"); |
88 |
if (p) { |
89 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
90 |
free_if_alloc (p); |
91 |
} |
92 |
p = get_reg_entry_gpg ("gpgProgram"); |
93 |
if (p) { |
94 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
95 |
free_if_alloc (p); |
96 |
} |
97 |
p = get_reg_entry_mo (); |
98 |
if (p) { |
99 |
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
100 |
free_if_alloc (p); |
101 |
} |
102 |
if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) { |
103 |
p = multi_gnupg_path (0); |
104 |
if (p && dir_exist_check (p) == 0) |
105 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
106 |
free_if_alloc (p); |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
/* Dialog box procedure for the GPG preferences. */ |
112 |
BOOL CALLBACK |
113 |
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
114 |
{ |
115 |
static int gpg4win = false; |
116 |
char exedir[512]; |
117 |
char homedir[512]; |
118 |
char locale_dir[512]; |
119 |
char *p = NULL, t[256]; |
120 |
const char *s; |
121 |
const char *folder; |
122 |
gpg_optfile_t opt = NULL; |
123 |
gpg_option_t e; |
124 |
UINT n; |
125 |
|
126 |
switch (msg) { |
127 |
case WM_INITDIALOG: |
128 |
SetWindowText (dlg, _("GnuPG Preferences")); |
129 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF, |
130 |
_("GnuPG home directory (where both keyrings are located)")); |
131 |
SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF, |
132 |
_("GnuPG exe file location (full path with added gpg.exe)")); |
133 |
SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF, |
134 |
_("Locale directory (to access the translation files)")); |
135 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign")); |
136 |
SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files")); |
137 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
138 |
SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options")); |
139 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse...")); |
140 |
SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse...")); |
141 |
SetDlgItemText (dlg, IDC_GPGPREFS_LOCDLG, _("Browse...")); |
142 |
SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings")); |
143 |
|
144 |
gpg4win = load_gpg4win_values (dlg); |
145 |
if (!gpg4win) |
146 |
load_registry_values (dlg); |
147 |
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_OVRDEFAULT), |
148 |
gpg4win? TRUE : FALSE); |
149 |
|
150 |
p = get_gnupg_cfgfile (); |
151 |
if (p) { |
152 |
parse_gpg_options (p, &opt); |
153 |
free_if_alloc (p); |
154 |
if (opt) { |
155 |
if (find_option (opt, "ask-cert-level")) |
156 |
CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED); |
157 |
if ((e=find_option (opt, "comment"))) |
158 |
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
159 |
if ((e=find_option (opt, "encrypt-to"))) |
160 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
161 |
release_gpg_options (opt); |
162 |
} |
163 |
} |
164 |
|
165 |
center_window (dlg, NULL); |
166 |
SetForegroundWindow (dlg); |
167 |
return TRUE; |
168 |
|
169 |
case WM_DESTROY: |
170 |
gpg4win = false; |
171 |
break; |
172 |
|
173 |
case WM_SYSCOMMAND: |
174 |
if (LOWORD (wparam) == SC_CLOSE) |
175 |
EndDialog (dlg, TRUE); |
176 |
return FALSE; |
177 |
|
178 |
case WM_COMMAND: |
179 |
switch (LOWORD (wparam)) { |
180 |
case IDC_GPGPREFS_SAVE: |
181 |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, |
182 |
homedir, sizeof (homedir) -1)) { |
183 |
msg_box (dlg, _("Please enter the GnuPG home directory."), |
184 |
_("Preferences"), MB_ERR); |
185 |
return FALSE; |
186 |
} |
187 |
if (dir_exist_check (homedir)) { |
188 |
_snprintf (t, sizeof (t) - 1, "%s: %s", homedir, |
189 |
winpt_strerror (WPTERR_DIR_OPEN)); |
190 |
msg_box (dlg, t, _("Preferences"), MB_ERR); |
191 |
return FALSE; |
192 |
} |
193 |
|
194 |
if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) { |
195 |
msg_box (dlg, _("Could not save 'HomeDir' in the registry."), |
196 |
_("Preferences"), MB_ERR); |
197 |
return FALSE; |
198 |
} |
199 |
if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
200 |
exedir, sizeof (exedir) -1)) { |
201 |
msg_box (dlg, _("Please enter where GPG.exe is located."), |
202 |
_("Preferences"), MB_ERR); |
203 |
return FALSE; |
204 |
} |
205 |
if (file_exist_check (exedir)) { |
206 |
msg_box (dlg, _("Could not find the GPG program in this directory."), |
207 |
_("Preferences"), MB_ERR); |
208 |
return FALSE; |
209 |
} |
210 |
if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) { |
211 |
msg_box (dlg, _("Could not save 'gpgProgram' in the registry"), |
212 |
_("Preferences"), MB_ERR); |
213 |
return FALSE; |
214 |
} |
215 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1)) { |
216 |
if (dir_exist_check (locale_dir)) { |
217 |
log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir, |
218 |
winpt_strerror (WPTERR_DIR_OPEN)); |
219 |
return FALSE; |
220 |
} |
221 |
if (!gpg4win) |
222 |
set_reg_entry_mo (locale_dir); |
223 |
set_gettext_file ("winpt", locale_dir); |
224 |
} |
225 |
else if (!gpg4win) |
226 |
set_reg_entry_mo (""); |
227 |
|
228 |
p = get_gnupg_cfgfile (); |
229 |
if (!p) { |
230 |
msg_box (dlg, _("Could not get GPG config file"), |
231 |
_("Preferences"), MB_ERR); |
232 |
EndDialog (dlg, FALSE); |
233 |
} |
234 |
parse_gpg_options (p, &opt); |
235 |
|
236 |
if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) { |
237 |
modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL ); |
238 |
reg_prefs.gpg.ask_cert_level = 1; |
239 |
} |
240 |
else { |
241 |
delete_option (opt, "ask-cert-level"); |
242 |
reg_prefs.gpg.ask_cert_level = 0; |
243 |
} |
244 |
|
245 |
if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) )) |
246 |
modify_entry( opt, ENTRY_MULTI, "comment", t ); |
247 |
else if( n == 0 ) |
248 |
modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" ); |
249 |
else |
250 |
delete_option( opt, "comment" ); |
251 |
|
252 |
if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) ) |
253 |
modify_entry( opt, ENTRY_MULTI, "encrypt-to", t ); |
254 |
else |
255 |
delete_option( opt, "encrypt-to" ); |
256 |
|
257 |
commit_gpg_options (p, opt); |
258 |
release_gpg_options (opt); |
259 |
EndDialog (dlg, TRUE); |
260 |
return TRUE; |
261 |
|
262 |
case IDC_GPGPREFS_HOMEDLG: |
263 |
folder = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL); |
264 |
if (folder) { |
265 |
char *name; |
266 |
|
267 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder); |
268 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0) |
269 |
break; |
270 |
name = make_filename (folder, "gpg", "exe"); |
271 |
if (file_exist_check (name) == 0) |
272 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
273 |
free_if_alloc (name); |
274 |
name = make_filename (folder, "gpg", "conf"); |
275 |
if (file_exist_check (name) == 0) |
276 |
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name); |
277 |
free_if_alloc (name); |
278 |
} |
279 |
return TRUE; |
280 |
|
281 |
case IDC_GPGPREFS_LOCDLG: |
282 |
folder = get_folder_dlg (dlg, _("Choose locale directory"), NULL); |
283 |
if (folder) |
284 |
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder); |
285 |
return TRUE; |
286 |
|
287 |
case IDC_GPGREFS_EXEDLG: |
288 |
s = get_fileopen_dlg (dlg, _("Choose GPG binary"), |
289 |
_("Executable Files (*.exe)\0*.exe\0\0"), |
290 |
NULL); |
291 |
if (s) |
292 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s); |
293 |
return TRUE; |
294 |
|
295 |
case IDCANCEL: |
296 |
EndDialog (dlg, FALSE); |
297 |
return TRUE; |
298 |
} |
299 |
break; |
300 |
} |
301 |
|
302 |
return FALSE; |
303 |
} |