1 |
/* wptGPGPrefsDlg.cpp - WinPT GnuPG Preferences |
/* wptGPGPrefsDlg.cpp - GnuPG Preferences |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
#include <config.h> |
23 |
#endif |
#endif |
31 |
#include "wptGPG.h" |
#include "wptGPG.h" |
32 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
33 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
|
#include "wptContext.h" /* for passphrase_s */ |
|
|
#include "wptDlgs.h" |
|
34 |
#include "wptW32API.h" |
#include "wptW32API.h" |
|
#include "wptVersion.h" |
|
35 |
|
|
36 |
static struct { |
|
37 |
const char *name; |
/* Load the GPG4WIN default values and disabled the |
38 |
int id; |
dialog items to indicate these are fixed values. |
39 |
} opts[] = { |
Return value: true if GPG4win was found. */ |
40 |
{"HomeDir", IDC_GPGPREFS_HOMEDIR}, |
static bool |
41 |
{"gpgProgram", IDC_GPGPREFS_EXEDIR}, |
load_gpg4win_values (HWND dlg) |
42 |
{"OptFile", IDC_GPGPREFS_OPTFILE}, |
{ |
43 |
{NULL, 0} |
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 |
|
} |
65 |
|
free_if_alloc (p); |
66 |
|
} |
67 |
|
|
68 |
|
p = multi_gnupg_path (1); |
69 |
|
if (p) { |
70 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
71 |
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE); |
72 |
|
EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE); |
73 |
|
free_if_alloc (p); |
74 |
|
} |
75 |
|
|
76 |
|
return true; |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
/* Load the GPG values direct from the registry. */ |
81 |
|
static void |
82 |
|
load_registry_values (HWND dlg) |
83 |
|
{ |
84 |
|
char *p; |
85 |
|
|
86 |
|
p = get_reg_entry_gpg ("HomeDir"); |
87 |
|
if (p) { |
88 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
89 |
|
free_if_alloc (p); |
90 |
|
} |
91 |
|
p = get_reg_entry_gpg ("gpgProgram"); |
92 |
|
if (p) { |
93 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p); |
94 |
|
free_if_alloc (p); |
95 |
|
} |
96 |
|
p = get_reg_entry_mo (); |
97 |
|
if (p) { |
98 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
99 |
|
free_if_alloc (p); |
100 |
|
} |
101 |
|
if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) { |
102 |
|
p = multi_gnupg_path (0); |
103 |
|
if (p && dir_exist_check (p) == 0) |
104 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p); |
105 |
|
free_if_alloc (p); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
/* Dialog box procedure for the GPG preferences. */ |
/* Dialog box procedure for the GPG preferences. */ |
111 |
BOOL CALLBACK |
BOOL CALLBACK |
112 |
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
113 |
{ |
{ |
114 |
char exedir[512], homedir[512], optfile[512]; |
static int gpg4win = false; |
115 |
|
char exedir[512]; |
116 |
|
char homedir[512]; |
117 |
char locale_dir[512]; |
char locale_dir[512]; |
118 |
char *p = NULL, t[256]; |
char *p = NULL, t[256]; |
119 |
const char *s; |
const char *s; |
120 |
const char *file; |
const char *folder; |
|
int have_optfile = 0; |
|
121 |
gpg_optfile_t opt = NULL; |
gpg_optfile_t opt = NULL; |
122 |
gpg_option_t e; |
gpg_option_t e; |
123 |
UINT n; |
UINT n; |
127 |
SetWindowText (dlg, _("GnuPG Preferences")); |
SetWindowText (dlg, _("GnuPG Preferences")); |
128 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF, |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF, |
129 |
_("GnuPG home directory (where both keyrings are located)")); |
_("GnuPG home directory (where both keyrings are located)")); |
|
SetDlgItemText ( dlg, IDC_GPGPREFS_OPTINF, |
|
|
_("GnuPG config file (default: use gpg.conf)")); |
|
130 |
SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF, |
SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF, |
131 |
_("GnuPG exe file location (full path with added gpg.exe)")); |
_("GnuPG exe file location (full path with added gpg.exe)")); |
132 |
SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF, |
SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF, |
136 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
137 |
SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options")); |
SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options")); |
138 |
|
|
139 |
for (n=0; (s = opts[n].name); n++) { |
gpg4win = load_gpg4win_values (dlg); |
140 |
p = get_reg_entry_gpg (s); |
if (!gpg4win) |
141 |
if (p) { |
load_registry_values (dlg); |
142 |
SetDlgItemText (dlg, opts[n].id, p); |
|
|
free_if_alloc (p); |
|
|
} |
|
|
} |
|
|
|
|
|
p = get_reg_entry_mo (); |
|
|
if (p) { |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
|
|
free_if_alloc (p); |
|
|
} |
|
143 |
p = get_gnupg_cfgfile (); |
p = get_gnupg_cfgfile (); |
144 |
if (p) { |
if (p) { |
145 |
parse_gpg_options (p, &opt); |
parse_gpg_options (p, &opt); |
146 |
free_if_alloc (p); |
free_if_alloc (p); |
147 |
if (opt && find_option( opt, "ask-cert-level" ) ) |
if (opt) { |
148 |
CheckDlgButton( dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED ); |
if (find_option (opt, "ask-cert-level")) |
149 |
if (opt && (e=find_option( opt, "comment" )) ) |
CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED); |
150 |
|
if ((e=find_option (opt, "comment"))) |
151 |
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
152 |
if (opt && (e=find_option( opt, "encrypt-to"))) |
if ((e=find_option (opt, "encrypt-to"))) |
153 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
|
if (opt) |
|
154 |
release_gpg_options (opt); |
release_gpg_options (opt); |
155 |
|
} |
156 |
} |
} |
157 |
|
|
158 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
159 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
160 |
return TRUE; |
return TRUE; |
161 |
|
|
162 |
|
case WM_DESTROY: |
163 |
|
gpg4win = false; |
164 |
|
break; |
165 |
|
|
166 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
167 |
if( LOWORD( wparam ) == SC_CLOSE ) |
if (LOWORD (wparam) == SC_CLOSE) |
168 |
EndDialog( dlg, TRUE ); |
EndDialog (dlg, TRUE); |
169 |
return FALSE; |
return FALSE; |
170 |
|
|
171 |
case WM_COMMAND: |
case WM_COMMAND: |
172 |
switch ( LOWORD( wparam ) ) { |
switch (LOWORD (wparam)) { |
173 |
case IDC_GPGPREFS_SAVE: |
case IDC_GPGPREFS_SAVE: |
174 |
if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) { |
if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, |
175 |
msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR ); |
homedir, sizeof (homedir) -1)) { |
176 |
|
msg_box (dlg, _("Please enter the GnuPG home directory."), |
177 |
|
_("Preferences"), MB_ERR); |
178 |
return FALSE; |
return FALSE; |
179 |
} |
} |
180 |
if (dir_exist_check (homedir)) { |
if (dir_exist_check (homedir)) { |
184 |
return FALSE; |
return FALSE; |
185 |
} |
} |
186 |
|
|
187 |
if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) { |
if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) { |
188 |
if ( file_exist_check( optfile ) ) { |
msg_box (dlg, _("Could not save 'HomeDir' in the registry."), |
189 |
n = msg_box(dlg, _("Could not find GPG config file.\n" |
_("Preferences"), MB_ERR); |
|
"Do you want to create a config file?"), |
|
|
_("Preferences"), MB_INFO|MB_YESNO ); |
|
|
if( n == IDNO ) { |
|
|
SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" ); |
|
|
return FALSE; |
|
|
} |
|
|
else if( n == IDYES ) { |
|
|
FILE * fp = fopen( optfile, "wb" ); |
|
|
if( fp ) |
|
|
fclose( fp ); |
|
|
if( file_exist_check( optfile ) ) { |
|
|
log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) ); |
|
|
return FALSE; |
|
|
} |
|
|
have_optfile = 1; |
|
|
} |
|
|
} |
|
|
else |
|
|
have_optfile = 1; |
|
|
if( have_optfile ) { |
|
|
if( set_reg_entry_gpg( "OptFile", optfile ) ) { |
|
|
msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR ); |
|
|
return FALSE; |
|
|
} |
|
|
} |
|
|
} |
|
|
else { |
|
|
char *cfg = get_gnupg_cfgfile (); |
|
|
if (cfg && !file_exist_check (cfg)) |
|
|
set_reg_entry_gpg( "OptFile", cfg); |
|
|
free_if_alloc (cfg); |
|
|
} |
|
|
if ( set_reg_entry_gpg( "HomeDir", homedir ) ) { |
|
|
msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR ); |
|
190 |
return FALSE; |
return FALSE; |
191 |
} |
} |
192 |
if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) { |
if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, |
193 |
msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR ); |
exedir, sizeof (exedir) -1)) { |
194 |
|
msg_box (dlg, _("Please enter where GPG.exe is located."), |
195 |
|
_("Preferences"), MB_ERR); |
196 |
return FALSE; |
return FALSE; |
197 |
} |
} |
198 |
if( file_exist_check( exedir ) ) { |
if (file_exist_check (exedir)) { |
199 |
msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR ); |
msg_box (dlg, _("Could not find the GPG program in this directory."), |
200 |
|
_("Preferences"), MB_ERR); |
201 |
return FALSE; |
return FALSE; |
202 |
} |
} |
203 |
if( set_reg_entry_gpg("gpgProgram", exedir ) ) { |
if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) { |
204 |
msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR ); |
msg_box (dlg, _("Could not save 'gpgProgram' in the registry"), |
205 |
|
_("Preferences"), MB_ERR); |
206 |
return FALSE; |
return FALSE; |
207 |
} |
} |
208 |
if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) { |
if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1)) { |
209 |
if (dir_exist_check (locale_dir)) { |
if (dir_exist_check (locale_dir)) { |
210 |
log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN)); |
log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir, |
211 |
|
winpt_strerror (WPTERR_DIR_OPEN)); |
212 |
return FALSE; |
return FALSE; |
213 |
} |
} |
214 |
set_reg_entry_mo (locale_dir); |
if (!gpg4win) |
215 |
|
set_reg_entry_mo (locale_dir); |
216 |
set_gettext_file ("winpt", locale_dir); |
set_gettext_file ("winpt", locale_dir); |
217 |
} |
} |
218 |
else |
else if (!gpg4win) |
219 |
set_reg_entry_mo (""); |
set_reg_entry_mo (""); |
220 |
|
|
221 |
p = get_gnupg_cfgfile (); |
p = get_gnupg_cfgfile (); |
222 |
if( !p ) { |
if (!p) { |
223 |
msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR ); |
msg_box (dlg, _("Could not get GPG config file"), |
224 |
EndDialog( dlg, FALSE ); |
_("Preferences"), MB_ERR); |
225 |
|
EndDialog (dlg, FALSE); |
226 |
} |
} |
227 |
parse_gpg_options( p, &opt ); |
parse_gpg_options (p, &opt); |
228 |
|
|
229 |
if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) { |
if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) { |
230 |
modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL ); |
modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL ); |
231 |
reg_prefs.gpg.ask_cert_level = 1; |
reg_prefs.gpg.ask_cert_level = 1; |
232 |
} |
} |
233 |
else { |
else { |
234 |
delete_option( opt, "ask-cert-level" ); |
delete_option (opt, "ask-cert-level"); |
235 |
reg_prefs.gpg.ask_cert_level = 0; |
reg_prefs.gpg.ask_cert_level = 0; |
236 |
} |
} |
237 |
|
|
247 |
else |
else |
248 |
delete_option( opt, "encrypt-to" ); |
delete_option( opt, "encrypt-to" ); |
249 |
|
|
250 |
commit_gpg_options( p, opt ); |
commit_gpg_options (p, opt); |
251 |
release_gpg_options (opt); |
release_gpg_options (opt); |
252 |
EndDialog( dlg, TRUE ); |
EndDialog (dlg, TRUE); |
253 |
return TRUE; |
return TRUE; |
254 |
|
|
255 |
case IDC_GPGPREFS_HOMEDLG: |
case IDC_GPGPREFS_HOMEDLG: |
256 |
const char *home; |
folder = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL); |
257 |
home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL); |
if (folder) { |
258 |
if (home) { |
char *name; |
259 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home); |
|
260 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder); |
261 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0) |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0) |
262 |
break; |
break; |
263 |
char *name = make_filename (home, "gpg", "exe"); |
name = make_filename (folder, "gpg", "exe"); |
264 |
if (file_exist_check (name) == 0) |
if (file_exist_check (name) == 0) |
265 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
266 |
free_if_alloc (name); |
free_if_alloc (name); |
267 |
name = make_filename (home, "gpg", "conf"); |
name = make_filename (folder, "gpg", "conf"); |
268 |
if (file_exist_check (name) == 0) |
if (file_exist_check (name) == 0) |
269 |
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name); |
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name); |
270 |
free_if_alloc (name); |
free_if_alloc (name); |
271 |
} |
} |
272 |
break; |
return TRUE; |
273 |
|
|
274 |
|
case IDC_GPGPREFS_LOCDLG: |
275 |
|
folder = get_folder_dlg (dlg, _("Choose locale directory"), NULL); |
276 |
|
if (folder) |
277 |
|
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder); |
278 |
|
return TRUE; |
279 |
|
|
280 |
case IDC_GPGREFS_EXEDLG: |
case IDC_GPGREFS_EXEDLG: |
281 |
file = get_fileopen_dlg (dlg, _("Choose GPG binary"), |
s = get_fileopen_dlg (dlg, _("Choose GPG binary"), |
282 |
_("Executable Files (*.exe)\0*.exe\0\0"), |
_("Executable Files (*.exe)\0*.exe\0\0"), |
283 |
NULL); |
NULL); |
284 |
if (file) |
if (s) |
285 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, file); |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s); |
|
return TRUE; |
|
|
|
|
|
case IDC_GPGPREFS_OPTDLG: |
|
|
file = get_fileopen_dlg (dlg, _("Choose GPG config file"), |
|
|
NULL, NULL); |
|
|
if (file) |
|
|
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, file); |
|
286 |
return TRUE; |
return TRUE; |
287 |
} |
} |
288 |
break; |
break; |