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

Annotation of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26