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

Contents of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 315 - (show annotations)
Fri May 25 14:45:06 2007 UTC (17 years, 9 months ago) by twoaday
File size: 11964 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 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 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 there is no 'HomeDir' registry value but an existing
125 GPG multipath, we use this as the home dir instead. */
126 if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) {
127 p = multi_gnupg_path (0);
128 if (p && dir_exist_check (p) == 0)
129 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
130 free_if_alloc (p);
131 }
132 }
133
134
135 /* Dialog box procedure for the GPG preferences. */
136 BOOL CALLBACK
137 gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
138 {
139 static int gpg4win = false;
140 char exedir[512];
141 char homedir[512];
142 char locale_dir[512];
143 char *p = NULL, t[256];
144 const char *s;
145 const char *folder;
146 config_file_t opt = NULL;
147 conf_option_t e;
148 UINT n;
149
150 switch (msg) {
151 case WM_INITDIALOG:
152 SetWindowText (dlg, _("GnuPG Preferences"));
153 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
154 _("GnuPG home directory (where both keyrings are located)"));
155 SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
156 _("GnuPG exe file location (full path with added gpg.exe)"));
157 SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,
158 _("Locale directory (to access the translation files)"));
159 SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign"));
160 SetDlgItemText (dlg, IDC_GPGPREFS_ASKEXPIRE, _("Allow to set an expiration date for signatures"));
161 SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
162 SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
163 SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
164 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse..."));
165 SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse..."));
166 SetDlgItemText (dlg, IDC_GPGPREFS_LOCDLG, _("Browse..."));
167 SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings"));
168 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
169
170 gpg4win = load_gpg4win_values (dlg);
171 if (!gpg4win) {
172 load_registry_values (dlg);
173 CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED);
174 }
175
176 p = get_gnupg_cfgfile ();
177 if (p) {
178 parse_config (p, &opt);
179 free_if_alloc (p);
180 if (opt) {
181 if (conf_find_option (opt, "ask-cert-expire"))
182 CheckDlgButton (dlg, IDC_GPGPREFS_ASKEXPIRE, BST_CHECKED);
183 if (conf_find_option (opt, "ask-cert-level"))
184 CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
185 e = conf_find_option (opt, "comment");
186 if (e != NULL)
187 SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
188 e = conf_find_option (opt, "encrypt-to");
189 if (e != NULL)
190 SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
191 release_config (opt);
192 }
193 }
194
195 #ifdef WINPT_MOBILE
196 /* In mobile mode we do not allow to change the default settings
197 because this would not make any sense. */
198 CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_UNCHECKED);
199 EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_OVRDEFAULT), FALSE);
200 activate_items (dlg, FALSE);
201 #endif
202
203 center_window (dlg, NULL);
204 SetForegroundWindow (dlg);
205 return TRUE;
206
207 case WM_DESTROY:
208 gpg4win = false;
209 break;
210
211 case WM_COMMAND:
212 if (HIWORD (wparam) == BN_CLICKED &&
213 LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) {
214 if (IsDlgButtonChecked (dlg, LOWORD (wparam))) {
215 activate_items (dlg, TRUE);
216 gpg4win = false;
217 }
218 else {
219 activate_items (dlg, FALSE);
220 gpg4win = true;
221 }
222 break;
223 }
224
225 switch (LOWORD (wparam)) {
226 case IDC_GPGPREFS_SAVE:
227 if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
228 homedir, DIM (homedir) -1)) {
229 msg_box (dlg, _("Please enter the GnuPG home directory."),
230 _("Preferences"), MB_ERR);
231 return FALSE;
232 }
233 if (dir_exist_check (homedir)) {
234 _snprintf (t, DIM (t) - 1, "%s: %s", homedir,
235 winpt_strerror (WPTERR_DIR_OPEN));
236 msg_box (dlg, t, _("Preferences"), MB_ERR);
237 return FALSE;
238 }
239
240 if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
241 msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
242 _("Preferences"), MB_ERR);
243 return FALSE;
244 }
245 if (!GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
246 exedir, DIM (exedir) -1)) {
247 msg_box (dlg, _("Please enter where GPG.exe is located."),
248 _("Preferences"), MB_ERR);
249 return FALSE;
250 }
251 if (file_exist_check (exedir)) {
252 msg_box (dlg, _("Could not find the GPG program in this directory."),
253 _("Preferences"), MB_ERR);
254 return FALSE;
255 }
256 if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
257 msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
258 _("Preferences"), MB_ERR);
259 return FALSE;
260 }
261 if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE,
262 locale_dir, DIM (locale_dir) -1) > 0) {
263 if (dir_exist_check (locale_dir)) {
264 log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir,
265 winpt_strerror (WPTERR_DIR_OPEN));
266 return FALSE;
267 }
268 if (!gpg4win)
269 set_reg_entry_mo (locale_dir);
270 gettext_set_file ("winpt", locale_dir);
271 }
272 else if (!gpg4win)
273 set_reg_entry_mo ("");
274
275 p = get_gnupg_cfgfile ();
276 if (!p) {
277 msg_box (dlg, _("Could not get GPG config file"),
278 _("Preferences"), MB_ERR);
279 EndDialog (dlg, FALSE);
280 return TRUE;
281 }
282 parse_config (p, &opt);
283
284 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKLEVEL)) {
285 conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-level", NULL);
286 reg_prefs.gpg.ask_cert_level = 1;
287 }
288 else {
289 conf_delete_option (opt, "ask-cert-level");
290 reg_prefs.gpg.ask_cert_level = 0;
291 }
292 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKEXPIRE)) {
293 conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-expire", NULL);
294 reg_prefs.gpg.ask_cert_expire = 1;
295 }
296 else {
297 conf_delete_option (opt, "ask-cert-expire");
298 reg_prefs.gpg.ask_cert_expire = 0;
299 }
300
301 n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, DIM (t)-1);
302 if (n > 0)
303 conf_modify_entry (opt, ENTRY_MULTI, "comment", t);
304 else if (n == 0)
305 conf_modify_entry (opt, ENTRY_MULTI, "comment", "\"\"");
306 else
307 conf_delete_option (opt, "comment");
308
309 n = GetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, t, DIM (t)-1);
310 if (n > 0)
311 conf_modify_entry (opt, ENTRY_MULTI, "encrypt-to", t);
312 else
313 conf_delete_option (opt, "encrypt-to");
314
315 commit_config (p, opt);
316 release_config (opt);
317 /* only return TRUE if the home dir has been changed. */
318 if (SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, EM_GETMODIFY, 0, 0)) {
319 set_gnupg_default_key (NULL);
320 EndDialog (dlg, TRUE);
321 }
322 else
323 EndDialog (dlg, FALSE);
324 return TRUE;
325
326 case IDC_GPGPREFS_HOMEDLG:
327 folder = get_folder_dlg (dlg, _("Choose GPG Home Directory"), NULL);
328 if (folder) {
329 char *name;
330
331 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
332 SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR,
333 EM_SETMODIFY, (WPARAM)(UINT)TRUE, 0);
334 if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
335 exedir, DIM (exedir)-1) > 0)
336 break;
337 name = make_filename (folder, "gpg", "exe");
338 if (file_exist_check (name) == 0)
339 SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
340 free_if_alloc (name);
341 }
342 return TRUE;
343
344 case IDC_GPGPREFS_LOCDLG:
345 folder = get_folder_dlg (dlg, _("Choose Locale Directory"), NULL);
346 if (folder)
347 SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder);
348 return TRUE;
349
350 case IDC_GPGREFS_EXEDLG:
351 n = msg_box (dlg, _("Please be aware that the modifcation of "
352 "the value will affect all installed "
353 "programs which use GPG via the "
354 "GPGME interface library\n\n"
355 "Do you really want to continue?"),
356 _("GPG Warning"), MB_WARN_ASK);
357 if (n != IDYES)
358 return TRUE;
359
360 s = get_fileopen_dlg (dlg, _("Choose GPG Binary"),
361 "Executable Files (*.exe)\0*.exe\0\0",
362 NULL);
363 if (s)
364 SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
365 return TRUE;
366
367 case IDCANCEL:
368 EndDialog (dlg, FALSE);
369 return TRUE;
370 }
371 break;
372 }
373
374 return FALSE;
375 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26