1 |
/* wptGPGPrefsDlg.cpp - WinPT 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 "wptContext.h" /* for passphrase_s */ |
35 |
#include "wptDlgs.h" |
36 |
#include "wptW32API.h" |
37 |
#include "wptVersion.h" |
38 |
|
39 |
static struct { |
40 |
const char *name; |
41 |
int id; |
42 |
} opts[] = { |
43 |
{"HomeDir", IDC_GPGPREFS_HOMEDIR}, |
44 |
{"gpgProgram", IDC_GPGPREFS_EXEDIR}, |
45 |
{"OptFile", IDC_GPGPREFS_OPTFILE}, |
46 |
{NULL, 0} |
47 |
}; |
48 |
|
49 |
/* Dialog box procedure for the GPG preferences. */ |
50 |
BOOL CALLBACK |
51 |
gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
52 |
{ |
53 |
char exedir[512], homedir[512], optfile[512]; |
54 |
char locale_dir[512]; |
55 |
char *p = NULL, t[256]; |
56 |
const char *s; |
57 |
int have_optfile = 0; |
58 |
gpg_optfile_t opt = NULL; |
59 |
gpg_option_t e; |
60 |
UINT n; |
61 |
|
62 |
switch (msg) { |
63 |
case WM_INITDIALOG: |
64 |
SetWindowText (dlg, _("GnuPG Preferences")); |
65 |
SetDlgItemText( dlg, IDC_GPGPREFS_HOMEINF, |
66 |
_("GnuPG home directory (where both keyrings are located)") ); |
67 |
SetDlgItemText( dlg, IDC_GPGPREFS_OPTINF, |
68 |
_("GnuPG config file (default: use gpg.conf)") ); |
69 |
SetDlgItemText( dlg, IDC_GPGPREFS_EXEINF, |
70 |
_("GnuPG exe file location (full path with added gpg.exe)") ); |
71 |
SetDlgItemText( dlg, IDC_GPGPREFS_LOCALINF, |
72 |
_("Locale directory (to access the translation files)") ); |
73 |
SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign")); |
74 |
SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files")); |
75 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key")); |
76 |
|
77 |
for( n=0; (s = opts[n].name); n++ ) { |
78 |
p = get_reg_entry_gpg( s ); |
79 |
if( p ) { |
80 |
SetDlgItemText( dlg, opts[n].id, p ); |
81 |
free_if_alloc( p ); |
82 |
} |
83 |
} |
84 |
|
85 |
p = get_reg_entry_mo (); |
86 |
if (p) { |
87 |
SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p); |
88 |
free_if_alloc (p); |
89 |
} |
90 |
p = get_gnupg_cfgfile (); |
91 |
if (p) { |
92 |
parse_gpg_options (p, &opt); |
93 |
free_if_alloc (p); |
94 |
if (opt && find_option( opt, "ask-cert-level" ) ) |
95 |
CheckDlgButton( dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED ); |
96 |
if (opt && (e=find_option( opt, "comment" )) ) |
97 |
SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val); |
98 |
if (opt && (e=find_option( opt, "encrypt-to"))) |
99 |
SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val); |
100 |
if (opt) |
101 |
release_gpg_options (opt); |
102 |
} |
103 |
center_window (dlg, NULL); |
104 |
SetForegroundWindow (dlg); |
105 |
return TRUE; |
106 |
|
107 |
case WM_SYSCOMMAND: |
108 |
if( LOWORD( wparam ) == SC_CLOSE ) |
109 |
EndDialog( dlg, TRUE ); |
110 |
return FALSE; |
111 |
|
112 |
case WM_COMMAND: |
113 |
switch ( LOWORD( wparam ) ) { |
114 |
case IDC_GPGPREFS_SAVE: |
115 |
if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) { |
116 |
msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR ); |
117 |
return FALSE; |
118 |
} |
119 |
if( dir_exist_check( homedir ) ) { |
120 |
_snprintf( t, sizeof t - 1, "%s: %s", homedir, winpt_strerror(WPTERR_DIR_OPEN) ); |
121 |
msg_box( dlg, t, _("Preferences"), MB_ERR ); |
122 |
return FALSE; |
123 |
} |
124 |
|
125 |
if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) { |
126 |
if ( file_exist_check( optfile ) ) { |
127 |
n = msg_box(dlg, _("Could not find GPG config file.\n" |
128 |
"Do you want to create a config file?"), |
129 |
_("Preferences"), MB_INFO|MB_YESNO ); |
130 |
if( n == IDNO ) { |
131 |
SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" ); |
132 |
return FALSE; |
133 |
} |
134 |
else if( n == IDYES ) { |
135 |
FILE * fp = fopen( optfile, "wb" ); |
136 |
if( fp ) |
137 |
fclose( fp ); |
138 |
if( file_exist_check( optfile ) ) { |
139 |
log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) ); |
140 |
return FALSE; |
141 |
} |
142 |
have_optfile = 1; |
143 |
} |
144 |
} |
145 |
else |
146 |
have_optfile = 1; |
147 |
if( have_optfile ) { |
148 |
if( set_reg_entry_gpg( "OptFile", optfile ) ) { |
149 |
msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR ); |
150 |
return FALSE; |
151 |
} |
152 |
} |
153 |
} |
154 |
else { |
155 |
char *cfg = get_gnupg_cfgfile (); |
156 |
if (cfg && !file_exist_check (cfg)) |
157 |
set_reg_entry_gpg( "OptFile", cfg); |
158 |
free_if_alloc (cfg); |
159 |
} |
160 |
if ( set_reg_entry_gpg( "HomeDir", homedir ) ) { |
161 |
msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR ); |
162 |
return FALSE; |
163 |
} |
164 |
if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) { |
165 |
msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR ); |
166 |
return FALSE; |
167 |
} |
168 |
if( file_exist_check( exedir ) ) { |
169 |
msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR ); |
170 |
return FALSE; |
171 |
} |
172 |
if( set_reg_entry_gpg("gpgProgram", exedir ) ) { |
173 |
msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR ); |
174 |
return FALSE; |
175 |
} |
176 |
if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) { |
177 |
if (dir_exist_check (locale_dir)) { |
178 |
log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN)); |
179 |
return FALSE; |
180 |
} |
181 |
set_reg_entry_mo (locale_dir); |
182 |
set_gettext_file ("winpt", locale_dir); |
183 |
} |
184 |
else |
185 |
set_reg_entry_mo (""); |
186 |
|
187 |
p = get_gnupg_cfgfile (); |
188 |
if( !p ) { |
189 |
msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR ); |
190 |
EndDialog( dlg, FALSE ); |
191 |
} |
192 |
parse_gpg_options( p, &opt ); |
193 |
|
194 |
if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) { |
195 |
modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL ); |
196 |
reg_prefs.gpg.ask_cert_level = 1; |
197 |
} |
198 |
else { |
199 |
delete_option( opt, "ask-cert-level" ); |
200 |
reg_prefs.gpg.ask_cert_level = 0; |
201 |
} |
202 |
|
203 |
if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) )) |
204 |
modify_entry( opt, ENTRY_MULTI, "comment", t ); |
205 |
else if( n == 0 ) |
206 |
modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" ); |
207 |
else |
208 |
delete_option( opt, "comment" ); |
209 |
|
210 |
if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) ) |
211 |
modify_entry( opt, ENTRY_MULTI, "encrypt-to", t ); |
212 |
else |
213 |
delete_option( opt, "encrypt-to" ); |
214 |
|
215 |
commit_gpg_options( p, opt ); |
216 |
release_gpg_options (opt); |
217 |
EndDialog( dlg, TRUE ); |
218 |
return TRUE; |
219 |
|
220 |
case IDC_GPGPREFS_HOMEDLG: |
221 |
const char * home; |
222 |
home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL); |
223 |
if (home) { |
224 |
SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home); |
225 |
if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0) |
226 |
break; |
227 |
char *name = make_filename (home, "gpg", "exe"); |
228 |
if (file_exist_check (name) == 0) |
229 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name); |
230 |
free_if_alloc (name); |
231 |
name = make_filename (home, "gpg", "conf"); |
232 |
if (file_exist_check (name) == 0) |
233 |
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name); |
234 |
free_if_alloc (name); |
235 |
} |
236 |
break; |
237 |
|
238 |
case IDC_GPGREFS_EXEDLG: |
239 |
const char *bin; |
240 |
bin = get_fileopen_dlg (dlg, _("Choose GPG binary"), |
241 |
_("Executable Files (*.exe)\0*.exe\0\0"), |
242 |
NULL); |
243 |
if (bin) |
244 |
SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, bin); |
245 |
return TRUE; |
246 |
|
247 |
case IDC_GPGPREFS_OPTDLG: |
248 |
const char *optf; |
249 |
optf = get_fileopen_dlg (dlg, _("Choose GPG config file"), |
250 |
NULL, NULL); |
251 |
if (optf) |
252 |
SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, optf); |
253 |
return TRUE; |
254 |
} |
255 |
break; |
256 |
} |
257 |
|
258 |
return FALSE; |
259 |
} |