/[winpt]/trunk/Src/wptGPGPrefsDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 241 - (show annotations)
Thu Jun 29 11:15:55 2006 UTC (18 years, 8 months ago) by twoaday
File size: 11396 byte(s)


1 /* wptGPGPrefsDlg.cpp - GnuPG Preferences
2 * Copyright (C) 2001-2006 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 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 /* 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 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 EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCDLG), FALSE);
79 }
80 free_if_alloc (p);
81 }
82
83 path = get_reg_entry_gpg ("HomeDir");
84 p = multi_gnupg_path (1);
85 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 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 free_if_alloc (p);
113 }
114 p = get_reg_entry_gpg ("gpgProgram");
115 if (p) {
116 SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p);
117 free_if_alloc (p);
118 }
119 p = get_reg_entry_mo ();
120 if (p) {
121 SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p);
122 free_if_alloc (p);
123 }
124 if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) {
125 p = multi_gnupg_path (0);
126 if (p && dir_exist_check (p) == 0)
127 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
128 free_if_alloc (p);
129 }
130 }
131
132
133 /* Dialog box procedure for the GPG preferences. */
134 BOOL CALLBACK
135 gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
136 {
137 static int gpg4win = false;
138 char exedir[512];
139 char homedir[512];
140 char locale_dir[512];
141 char *p = NULL, t[256];
142 const char *s;
143 const char *folder;
144 gpg_optfile_t opt = NULL;
145 gpg_option_t e;
146 UINT n;
147
148 switch (msg) {
149 case WM_INITDIALOG:
150 SetWindowText (dlg, _("GnuPG Preferences"));
151 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
152 _("GnuPG home directory (where both keyrings are located)"));
153 SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
154 _("GnuPG exe file location (full path with added gpg.exe)"));
155 SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,
156 _("Locale directory (to access the translation files)"));
157 SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign"));
158 SetDlgItemText (dlg, IDC_GPGPREFS_ASKEXPIRE, _("Allow to set an expiration date for signatures"));
159 SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
160 SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
161 SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
162 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse..."));
163 SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse..."));
164 SetDlgItemText (dlg, IDC_GPGPREFS_LOCDLG, _("Browse..."));
165 SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings"));
166 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
167
168 gpg4win = load_gpg4win_values (dlg);
169 if (!gpg4win) {
170 load_registry_values (dlg);
171 CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED);
172 }
173
174 p = get_gnupg_cfgfile ();
175 if (p) {
176 parse_gpg_options (p, &opt);
177 free_if_alloc (p);
178 if (opt) {
179 if (find_option (opt, "ask-cert-expire"))
180 CheckDlgButton (dlg, IDC_GPGPREFS_ASKEXPIRE, BST_CHECKED);
181 if (find_option (opt, "ask-cert-level"))
182 CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
183 e = find_option (opt, "comment");
184 if (e != NULL)
185 SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
186 e = find_option (opt, "encrypt-to");
187 if (e != NULL)
188 SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
189 release_gpg_options (opt);
190 }
191 }
192
193 center_window (dlg, NULL);
194 SetForegroundWindow (dlg);
195 return TRUE;
196
197 case WM_DESTROY:
198 gpg4win = false;
199 break;
200
201 case WM_COMMAND:
202 if (HIWORD (wparam) == BN_CLICKED &&
203 LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) {
204 if (IsDlgButtonChecked (dlg, LOWORD (wparam))) {
205 enable_disable_items (dlg, TRUE);
206 gpg4win = false;
207 }
208 else {
209 enable_disable_items (dlg, FALSE);
210 gpg4win = true;
211 }
212 break;
213 }
214
215 switch (LOWORD (wparam)) {
216 case IDC_GPGPREFS_SAVE:
217 if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
218 homedir, sizeof (homedir) -1)) {
219 msg_box (dlg, _("Please enter the GnuPG home directory."),
220 _("Preferences"), MB_ERR);
221 return FALSE;
222 }
223 if (dir_exist_check (homedir)) {
224 _snprintf (t, sizeof (t) - 1, "%s: %s", homedir,
225 winpt_strerror (WPTERR_DIR_OPEN));
226 msg_box (dlg, t, _("Preferences"), MB_ERR);
227 return FALSE;
228 }
229
230 if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
231 msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
232 _("Preferences"), MB_ERR);
233 return FALSE;
234 }
235 if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
236 exedir, sizeof (exedir) -1)) {
237 msg_box (dlg, _("Please enter where GPG.exe is located."),
238 _("Preferences"), MB_ERR);
239 return FALSE;
240 }
241 if (file_exist_check (exedir)) {
242 msg_box (dlg, _("Could not find the GPG program in this directory."),
243 _("Preferences"), MB_ERR);
244 return FALSE;
245 }
246 if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
247 msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
248 _("Preferences"), MB_ERR);
249 return FALSE;
250 }
251 if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE,
252 locale_dir, sizeof (locale_dir) -1) > 0) {
253 if (dir_exist_check (locale_dir)) {
254 log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir,
255 winpt_strerror (WPTERR_DIR_OPEN));
256 return FALSE;
257 }
258 if (!gpg4win)
259 set_reg_entry_mo (locale_dir);
260 set_gettext_file ("winpt", locale_dir);
261 }
262 else if (!gpg4win)
263 set_reg_entry_mo ("");
264
265 p = get_gnupg_cfgfile ();
266 if (!p) {
267 msg_box (dlg, _("Could not get GPG config file"),
268 _("Preferences"), MB_ERR);
269 EndDialog (dlg, FALSE);
270 return TRUE;
271 }
272 parse_gpg_options (p, &opt);
273
274 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKLEVEL)) {
275 modify_entry (opt, ENTRY_SINGLE, "ask-cert-level", NULL);
276 reg_prefs.gpg.ask_cert_level = 1;
277 }
278 else {
279 delete_option (opt, "ask-cert-level");
280 reg_prefs.gpg.ask_cert_level = 0;
281 }
282 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKEXPIRE)) {
283 modify_entry (opt, ENTRY_SINGLE, "ask-cert-expire", NULL);
284 reg_prefs.gpg.ask_cert_expire = 1;
285 }
286 else {
287 delete_option (opt, "ask-cert-expire");
288 reg_prefs.gpg.ask_cert_expire = 0;
289 }
290
291 n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, DIM (t)-1);
292 if (n > 0)
293 modify_entry (opt, ENTRY_MULTI, "comment", t);
294 else if (n == 0)
295 modify_entry (opt, ENTRY_MULTI, "comment", "\"\"");
296 else
297 delete_option (opt, "comment");
298
299 n = GetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, t, DIM (t)-1);
300 if (n > 0)
301 modify_entry (opt, ENTRY_MULTI, "encrypt-to", t);
302 else
303 delete_option (opt, "encrypt-to");
304
305 commit_gpg_options (p, opt);
306 release_gpg_options (opt);
307 /* only return TRUE if the home dir has been changed. */
308 if (SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, EM_GETMODIFY, 0, 0)) {
309 set_gnupg_default_key (NULL);
310 EndDialog (dlg, TRUE);
311 }
312 else
313 EndDialog (dlg, FALSE);
314 return TRUE;
315
316 case IDC_GPGPREFS_HOMEDLG:
317 folder = get_folder_dlg (dlg, _("Choose GPG Home Directory"), NULL);
318 if (folder) {
319 char *name;
320
321 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
322 SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR,
323 EM_SETMODIFY, (WPARAM)(UINT)TRUE, 0);
324 if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
325 exedir, DIM (exedir)-1) > 0)
326 break;
327 name = make_filename (folder, "gpg", "exe");
328 if (file_exist_check (name) == 0)
329 SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
330 free_if_alloc (name);
331 name = make_filename (folder, "gpg", "conf");
332 if (file_exist_check (name) == 0)
333 SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);
334 free_if_alloc (name);
335 }
336 return TRUE;
337
338 case IDC_GPGPREFS_LOCDLG:
339 folder = get_folder_dlg (dlg, _("Choose Locale Directory"), NULL);
340 if (folder)
341 SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder);
342 return TRUE;
343
344 case IDC_GPGREFS_EXEDLG:
345 s = get_fileopen_dlg (dlg, _("Choose GPG Binary"),
346 "Executable Files (*.exe)\0*.exe\0\0",
347 NULL);
348 if (s)
349 SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
350 return TRUE;
351
352 case IDCANCEL:
353 EndDialog (dlg, FALSE);
354 return TRUE;
355 }
356 break;
357 }
358
359 return FALSE;
360 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26