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

Contents of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 167 - (show annotations)
Thu Jan 26 10:17:17 2006 UTC (19 years, 1 month ago) by twoaday
File size: 10318 byte(s)
2006-01-25  Timo Schulz  <ts@g10code.com>
 
        * wptRegistry.cpp (get_reg_entry_gpg): Return NULL if
        the key exist with no value.
        * wptMDSumDlg.cpp (mdsum_dlg_proc): Translate string.
        * wptKeysignDlg.cpp (do_fill_seclist): Select the
        default key if possible.
        * wptFirstRunDlg.cpp (firstrun_dlg_proc): Directly
        return the choice.
        * wptKeylist.cpp (get_key_desc): New.
        (keylist_upd_key): Free memory.
        * wptKeyCache.cpp (gpg_keycache_get_default_key): New.
        (gpg_keycache_set_default_key): New.
        * WinPT.cpp (gpg_prefs_ok): New.
        (WinMain): Only start gpg prefs if needed.
         


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26