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

Annotation of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 10734 byte(s)


1 twoaday 128 /* wptGPGPrefsDlg.cpp - GnuPG Preferences
2 twoaday 328 * Copyright (C) 2001-2006, 2007 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 twoaday 278 activate_items (HWND dlg, BOOL val)
40 twoaday 161 {
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 twoaday 241 path = get_reg_entry_gpg ("HomeDir");
74 twoaday 128 p = multi_gnupg_path (1);
75 twoaday 241 if (path && dir_exist_check (path) == 0 && p && strcmp (path, p)) {
76     /* The 'HomeDir' has a higher priority so if the key exists and is
77     different from the multi user path, we force the use of it. */
78     free_if_alloc (p);
79     p = path;
80     }
81     else
82     free_if_alloc (path);
83 twoaday 128 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     return true;
90     }
91    
92    
93     /* Load the GPG values direct from the registry. */
94     static void
95     load_registry_values (HWND dlg)
96     {
97     char *p;
98    
99     p = get_reg_entry_gpg ("HomeDir");
100     if (p) {
101     SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
102 twoaday 225 free_if_alloc (p);
103 twoaday 128 }
104     p = get_reg_entry_gpg ("gpgProgram");
105     if (p) {
106     SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p);
107 twoaday 225 free_if_alloc (p);
108 twoaday 128 }
109 twoaday 328
110 twoaday 256 /* if there is no 'HomeDir' registry value but an existing
111     GPG multipath, we use this as the home dir instead. */
112 twoaday 128 if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) {
113     p = multi_gnupg_path (0);
114     if (p && dir_exist_check (p) == 0)
115     SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
116     free_if_alloc (p);
117     }
118     }
119    
120    
121 twoaday 41 /* Dialog box procedure for the GPG preferences. */
122 werner 36 BOOL CALLBACK
123     gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
124     {
125 twoaday 128 static int gpg4win = false;
126     char exedir[512];
127     char homedir[512];
128 twoaday 41 char *p = NULL, t[256];
129     const char *s;
130 twoaday 128 const char *folder;
131 twoaday 271 config_file_t opt = NULL;
132     conf_option_t e;
133 werner 36 UINT n;
134    
135 twoaday 41 switch (msg) {
136 werner 36 case WM_INITDIALOG:
137 twoaday 41 SetWindowText (dlg, _("GnuPG Preferences"));
138 twoaday 117 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
139     _("GnuPG home directory (where both keyrings are located)"));
140     SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
141     _("GnuPG exe file location (full path with added gpg.exe)"));
142 twoaday 41 SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign"));
143 twoaday 181 SetDlgItemText (dlg, IDC_GPGPREFS_ASKEXPIRE, _("Allow to set an expiration date for signatures"));
144 twoaday 117 SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
145 twoaday 78 SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
146 twoaday 88 SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
147 twoaday 147 SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse..."));
148     SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse..."));
149 twoaday 150 SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings"));
150 twoaday 177 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
151 twoaday 117
152 twoaday 128 gpg4win = load_gpg4win_values (dlg);
153 twoaday 161 if (!gpg4win) {
154 twoaday 128 load_registry_values (dlg);
155 twoaday 161 CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED);
156     }
157 twoaday 128
158 twoaday 41 p = get_gnupg_cfgfile ();
159     if (p) {
160 twoaday 270 parse_config (p, &opt);
161 twoaday 41 free_if_alloc (p);
162 twoaday 128 if (opt) {
163 twoaday 271 if (conf_find_option (opt, "ask-cert-expire"))
164 twoaday 181 CheckDlgButton (dlg, IDC_GPGPREFS_ASKEXPIRE, BST_CHECKED);
165 twoaday 271 if (conf_find_option (opt, "ask-cert-level"))
166 twoaday 128 CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
167 twoaday 271 e = conf_find_option (opt, "comment");
168 twoaday 177 if (e != NULL)
169     SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
170 twoaday 271 e = conf_find_option (opt, "encrypt-to");
171 twoaday 177 if (e != NULL)
172 twoaday 128 SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
173 twoaday 270 release_config (opt);
174 twoaday 128 }
175 werner 36 }
176 twoaday 117
177 twoaday 278 #ifdef WINPT_MOBILE
178     /* In mobile mode we do not allow to change the default settings
179     because this would not make any sense. */
180     CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_UNCHECKED);
181     EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_OVRDEFAULT), FALSE);
182     activate_items (dlg, FALSE);
183     #endif
184    
185 twoaday 41 center_window (dlg, NULL);
186     SetForegroundWindow (dlg);
187 werner 36 return TRUE;
188 twoaday 128
189     case WM_DESTROY:
190     gpg4win = false;
191     break;
192 werner 36
193     case WM_COMMAND:
194 twoaday 161 if (HIWORD (wparam) == BN_CLICKED &&
195     LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) {
196 twoaday 165 if (IsDlgButtonChecked (dlg, LOWORD (wparam))) {
197 twoaday 278 activate_items (dlg, TRUE);
198 twoaday 165 gpg4win = false;
199     }
200     else {
201 twoaday 278 activate_items (dlg, FALSE);
202 twoaday 165 gpg4win = true;
203     }
204 twoaday 161 break;
205     }
206    
207 twoaday 128 switch (LOWORD (wparam)) {
208 werner 36 case IDC_GPGPREFS_SAVE:
209 twoaday 128 if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
210 twoaday 315 homedir, DIM (homedir) -1)) {
211 twoaday 165 msg_box (dlg, _("Please enter the GnuPG home directory."),
212 twoaday 128 _("Preferences"), MB_ERR);
213 werner 36 return FALSE;
214     }
215 twoaday 117 if (dir_exist_check (homedir)) {
216 twoaday 315 _snprintf (t, DIM (t) - 1, "%s: %s", homedir,
217 twoaday 117 winpt_strerror (WPTERR_DIR_OPEN));
218     msg_box (dlg, t, _("Preferences"), MB_ERR);
219 werner 36 return FALSE;
220     }
221    
222 twoaday 128 if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
223     msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
224     _("Preferences"), MB_ERR);
225 werner 36 return FALSE;
226     }
227 twoaday 256 if (!GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
228 twoaday 315 exedir, DIM (exedir) -1)) {
229 twoaday 256 msg_box (dlg, _("Please enter where GPG.exe is located."),
230 twoaday 128 _("Preferences"), MB_ERR);
231 werner 36 return FALSE;
232     }
233 twoaday 128 if (file_exist_check (exedir)) {
234 twoaday 165 msg_box (dlg, _("Could not find the GPG program in this directory."),
235 twoaday 128 _("Preferences"), MB_ERR);
236 werner 36 return FALSE;
237     }
238 twoaday 128 if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
239 twoaday 165 msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
240 twoaday 128 _("Preferences"), MB_ERR);
241 werner 36 return FALSE;
242     }
243 twoaday 328
244 werner 36 p = get_gnupg_cfgfile ();
245 twoaday 128 if (!p) {
246     msg_box (dlg, _("Could not get GPG config file"),
247     _("Preferences"), MB_ERR);
248     EndDialog (dlg, FALSE);
249 twoaday 225 return TRUE;
250 werner 36 }
251 twoaday 270 parse_config (p, &opt);
252 werner 36
253 twoaday 165 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKLEVEL)) {
254 twoaday 271 conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-level", NULL);
255 twoaday 41 reg_prefs.gpg.ask_cert_level = 1;
256     }
257     else {
258 twoaday 271 conf_delete_option (opt, "ask-cert-level");
259 twoaday 41 reg_prefs.gpg.ask_cert_level = 0;
260     }
261 twoaday 181 if (IsDlgButtonChecked (dlg, IDC_GPGPREFS_ASKEXPIRE)) {
262 twoaday 271 conf_modify_entry (opt, ENTRY_SINGLE, "ask-cert-expire", NULL);
263 twoaday 181 reg_prefs.gpg.ask_cert_expire = 1;
264     }
265     else {
266 twoaday 271 conf_delete_option (opt, "ask-cert-expire");
267 twoaday 181 reg_prefs.gpg.ask_cert_expire = 0;
268     }
269    
270 twoaday 225 n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, DIM (t)-1);
271 twoaday 165 if (n > 0)
272 twoaday 271 conf_modify_entry (opt, ENTRY_MULTI, "comment", t);
273 twoaday 165 else if (n == 0)
274 twoaday 271 conf_modify_entry (opt, ENTRY_MULTI, "comment", "\"\"");
275 werner 36 else
276 twoaday 271 conf_delete_option (opt, "comment");
277 werner 36
278 twoaday 225 n = GetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, t, DIM (t)-1);
279 twoaday 165 if (n > 0)
280 twoaday 271 conf_modify_entry (opt, ENTRY_MULTI, "encrypt-to", t);
281 werner 36 else
282 twoaday 271 conf_delete_option (opt, "encrypt-to");
283 werner 36
284 twoaday 270 commit_config (p, opt);
285     release_config (opt);
286 twoaday 197 /* only return TRUE if the home dir has been changed. */
287     if (SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR, EM_GETMODIFY, 0, 0)) {
288     set_gnupg_default_key (NULL);
289     EndDialog (dlg, TRUE);
290     }
291     else
292     EndDialog (dlg, FALSE);
293 werner 36 return TRUE;
294    
295     case IDC_GPGPREFS_HOMEDLG:
296 twoaday 197 folder = get_folder_dlg (dlg, _("Choose GPG Home Directory"), NULL);
297 twoaday 128 if (folder) {
298     char *name;
299    
300     SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
301 twoaday 197 SendDlgItemMessage (dlg, IDC_GPGPREFS_HOMEDIR,
302     EM_SETMODIFY, (WPARAM)(UINT)TRUE, 0);
303 twoaday 256 if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
304 twoaday 165 exedir, DIM (exedir)-1) > 0)
305 werner 36 break;
306 twoaday 128 name = make_filename (folder, "gpg", "exe");
307 werner 36 if (file_exist_check (name) == 0)
308     SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
309     free_if_alloc (name);
310     }
311 twoaday 128 return TRUE;
312    
313 werner 36 case IDC_GPGREFS_EXEDLG:
314 twoaday 328 // TODO: use gpgme_ctx_set_engine_info instead.
315 twoaday 315 n = msg_box (dlg, _("Please be aware that the modifcation of "
316     "the value will affect all installed "
317     "programs which use GPG via the "
318     "GPGME interface library\n\n"
319     "Do you really want to continue?"),
320 twoaday 328 _("GPG WARNING"), MB_WARN_ASK);
321 twoaday 315 if (n != IDYES)
322     return TRUE;
323    
324 twoaday 197 s = get_fileopen_dlg (dlg, _("Choose GPG Binary"),
325 twoaday 167 "Executable Files (*.exe)\0*.exe\0\0",
326 twoaday 128 NULL);
327     if (s)
328     SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
329 werner 36 return TRUE;
330 twoaday 130
331     case IDCANCEL:
332     EndDialog (dlg, FALSE);
333     return TRUE;
334 werner 36 }
335     break;
336     }
337    
338     return FALSE;
339 twoaday 41 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26