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

Annotation of /trunk/Src/wptPreferencesDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (hide annotations)
Fri Dec 8 10:22:17 2006 UTC (18 years, 2 months ago) by twoaday
File size: 11212 byte(s)


1 werner 36 /* wptPreferencesDlg.cpp - Dialog for the preferences
2 twoaday 180 * Copyright (C) 2001, 2002, 2003, 2005, 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     #ifdef HAVE_CONFIG_H
21     #include <config.h>
22     #endif
23    
24     #include <windows.h>
25     #include <shlobj.h>
26    
27     #include "wptNLS.h"
28     #include "wptGPG.h"
29     #include "wptCommonCtl.h"
30     #include "wptDlgs.h"
31     #include "wptTypes.h"
32     #include "wptErrors.h"
33     #include "wptRegistry.h"
34     #include "wptHotkey.h"
35     #include "wptW32API.h"
36     #include "wptVersion.h"
37     #include "wptAgent.h"
38     #include "wptKeyserver.h"
39 werner 47 #include "resource.h"
40 werner 36
41    
42     /* Dialog IDs of all hotkeys. */
43     static int hotkeys[] = {
44     IDC_PREFS_CLIP_ENCRYPT,
45     IDC_PREFS_CLIP_DECRYPT_VERIFY,
46     IDC_PREFS_CLIP_SIGNENC,
47 twoaday 180 IDC_PREFS_CLIP_SIGN,
48 werner 36 IDC_PREFS_CWS_ENCRYPT,
49     IDC_PREFS_CWS_DECRYPT_VERIFY,
50     IDC_PREFS_CWS_SIGN,
51     IDC_PREFS_CWS_SIGNENC,
52     0
53     };
54    
55    
56     /* Check that the given hotkey is in A..Z or a..z. */
57     static int
58 twoaday 180 check_hotkey (char *key)
59 werner 36 {
60     if (*key >= 'A' && *key <= 'Z')
61     return 1;
62     else if (*key >= 'a' && *key <= 'z') {
63     *key = *key - 32;
64     return 1;
65     }
66     return 0;
67 twoaday 32 }
68 werner 36
69    
70     /* Disable all hotkey controls in the dialog @dlg if val is 1. */
71     static void
72     disable_hotkey_items (HWND dlg, int val)
73     {
74     int mode = val? FALSE : TRUE;
75     int i, id;
76    
77     for (i=0; (id = hotkeys[i]); i++)
78     EnableWindow (GetDlgItem (dlg, id), mode);
79     }
80    
81    
82    
83     /* En- or disable the backup items in the dialog @dlg.
84     If val != 0 activate them, disable them otherwise. */
85     static void
86     enable_backup_items (HWND dlg, int val)
87     {
88     int mode = val? TRUE : FALSE;
89    
90     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKHOME), mode);
91     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKUSER), mode);
92     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), mode);
93     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), mode);
94     }
95    
96 twoaday 176 /* Helper function to prepare dialog. */
97     static void
98     on_init_dialog (HWND dlg)
99     {
100     struct gettext_tab_s prefs[] = {
101     {IDC_PREFS_SECMODE, _("Do not use any &temporary files")},
102     {IDC_PREFS_VIEWER, _("Use clipboard &viewer to display the plaintext")},
103     {IDC_PREFS_WWINFO, _("Word wrap cleartext &signatures at column")},
104     {IDC_PREFS_DISABLE_HOTKEYS, _("&Disable hotkeys (Not recommended!)")},
105     {IDC_PREFS_TRUST, _("Skip key validation and assume that keys are always fully trusted")},
106     {IDC_PREFS_AUTOBACKUP, _("&Automatic keyring backup when WinPT closes")},
107     {IDC_PREFS_BAKHOME, _("Backup to &keyring folder")},
108     {IDC_PREFS_BAKUSER, _("Backup to:")},
109     {IDC_PREFS_KEYLISTINF, _("Select &key list mode")},
110     {IDC_PREFS_KSINF, _("Keyserver &config")},
111 twoaday 180 {IDC_PREFS_CACHEINF, _("Cache &passphrases for 'n' minutes")},
112 twoaday 176 {IDC_PREFS_CACHEHINT, _("(CTRL+ALT+F to clear the cache)")},
113     {IDC_PREFS_ALLOPTINF, _("General options")},
114     {IDC_PREFS_CLIPINF, _("Clipboard hotkeys")},
115     {IDC_PREFS_CURRINF, _("Current window hotkeys")},
116     {IDC_PREFS_KSELFILE, _("Browse...")},
117     {IDC_PREFS_BAKSELECT, _("Browse...")},
118 twoaday 218 {IDC_PREFS_EXTINF, _("Default extension for encrypted files")},
119 twoaday 222 {IDC_PREFS_BAKSECRING, _("&Backup includes secret keyring")},
120 twoaday 176 {IDCANCEL, _("&Cancel")},
121     {0, NULL}};
122     int i;
123 twoaday 218 HWND cb;
124 twoaday 176
125     gettext_localize_dialog (dlg, prefs, _("WinPT Preferences"));
126 twoaday 180
127     SetDlgItemInt (dlg, IDC_PREFS_CACHETIME, reg_prefs.cache_time/60, TRUE);
128 twoaday 176 SetDlgItemInt (dlg, IDC_PREFS_WORDWRAP, reg_prefs.word_wrap, TRUE);
129     if (reg_prefs.backup.path)
130     SetDlgItemText (dlg, IDC_PREFS_BAKPATH, reg_prefs.backup.path);
131    
132 twoaday 180 for (i=0; hotkeys[i]; i++) {
133     if (!wpt_hotkeys[i].enabled)
134     continue;
135     char key[2];
136     key[0] = wpt_hotkeys[i].key;
137     key[1] = 0;
138     SetDlgItemText (dlg, hotkeys[i], key);
139     }
140 twoaday 218 cb = GetDlgItem (dlg, IDC_PREFS_DEFEXT);
141 twoaday 222 combox_add_string (cb, "GnuPG (.gpg)");
142     combox_add_string (cb, "PGP (.pgp)");
143 twoaday 218 SendMessage (cb, CB_SETCURSEL, 0, 0);
144    
145 twoaday 176 CheckDlgButton (dlg, IDC_PREFS_DISABLE_HOTKEYS,
146     reg_prefs.no_hotkeys ? BST_CHECKED : BST_UNCHECKED);
147     CheckDlgButton (dlg, IDC_PREFS_VIEWER,
148     reg_prefs.use_viewer ? BST_CHECKED: BST_UNCHECKED);
149     CheckDlgButton (dlg, IDC_PREFS_TRUST,
150     reg_prefs.always_trust? BST_CHECKED : BST_UNCHECKED);
151     CheckDlgButton (dlg, IDC_PREFS_AUTOBACKUP,
152     reg_prefs.auto_backup? BST_CHECKED : BST_UNCHECKED);
153     CheckDlgButton (dlg, IDC_PREFS_BAKHOME,
154     reg_prefs.backup.mode==1? BST_CHECKED : BST_UNCHECKED);
155     CheckDlgButton (dlg, IDC_PREFS_BAKUSER,
156     reg_prefs.backup.mode==2? BST_CHECKED : BST_UNCHECKED);
157 twoaday 222 CheckDlgButton (dlg, IDC_PREFS_BAKSECRING,
158     reg_prefs.backup.include_secr? BST_CHECKED :BST_UNCHECKED);
159 twoaday 218 SendDlgItemMessage (dlg, IDC_PREFS_DEFEXT, CB_SETCURSEL,
160     reg_prefs.default_ext, 0);
161 twoaday 176 if (reg_prefs.no_hotkeys)
162     disable_hotkey_items (dlg, 1);
163     if (!reg_prefs.auto_backup)
164     enable_backup_items (dlg, 0);
165     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH),
166     reg_prefs.backup.mode==1?FALSE : TRUE);
167     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT),
168     reg_prefs.backup.mode==1? FALSE : TRUE);
169     center_window (dlg, NULL);
170     SetForegroundWindow (dlg);
171     }
172    
173    
174 werner 36 /* Dialog box procedure for the WinPT preferences. */
175     BOOL CALLBACK
176     prefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
177     {
178     int rc;
179 twoaday 176 int i;
180 twoaday 180 char t[3];
181 twoaday 176 char path[256], *p;
182 werner 36
183 twoaday 88 switch (msg) {
184 werner 36 case WM_INITDIALOG:
185 twoaday 176 on_init_dialog (dlg);
186 werner 36 return TRUE;
187    
188     case WM_SYSCOMMAND:
189     if (LOWORD (wparam) == SC_CLOSE)
190 twoaday 176 EndDialog (dlg, FALSE);
191 werner 36 return FALSE;
192    
193     case WM_COMMAND:
194 twoaday 180 switch (HIWORD (wparam)) {
195 werner 36 case BN_CLICKED:
196 twoaday 180 switch ((int)LOWORD (wparam)) {
197 werner 36 case IDC_PREFS_DISABLE_HOTKEYS:
198     reg_prefs.no_hotkeys ^= 1;
199 twoaday 117 disable_hotkey_items (dlg, reg_prefs.no_hotkeys);
200 werner 36 break;
201    
202     case IDC_PREFS_TRUST:
203     reg_prefs.always_trust ^= 1;
204 twoaday 117 if (reg_prefs.always_trust) {
205 werner 36 msg_box (dlg,
206     _("In most cases it is not a good idea to enable this setting.\n"
207 twoaday 248 "If you know what you are doing, you can enable this flag, otherwise\n"
208 werner 36 "it is safe to leave this flag untouched."),
209 twoaday 117 _("Preferences"), MB_ICONWARNING|MB_OK);
210 werner 36 }
211     break;
212    
213     case IDC_PREFS_AUTOBACKUP:
214     reg_prefs.auto_backup ^= 1;
215     enable_backup_items (dlg, reg_prefs.auto_backup);
216     if (reg_prefs.auto_backup == 1) {
217     if (!IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) &&
218     !IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER))
219     CheckDlgButton (dlg, IDC_PREFS_BAKHOME, BST_CHECKED);
220     }
221 twoaday 130 if (IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME))
222     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE);
223 werner 36 break;
224    
225     case IDC_PREFS_BAKHOME:
226     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), FALSE);
227     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE);
228     break;
229    
230     case IDC_PREFS_BAKUSER:
231     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), TRUE);
232     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), TRUE);
233     break;
234     }
235     break;
236     }
237 twoaday 180 switch (LOWORD (wparam)) {
238 werner 36 case IDC_PREFS_BAKSELECT:
239     const char *bpath;
240     bpath = get_folder_dlg (dlg, _("Select GPG backup path"), NULL);
241     if (bpath)
242     SetDlgItemText (dlg, IDC_PREFS_BAKPATH, bpath);
243     break;
244    
245     case IDOK:
246 twoaday 180 rc = GetDlgItemInt (dlg, IDC_PREFS_CACHETIME, NULL, FALSE);
247 twoaday 181 if (rc <= 0) {
248 werner 36 reg_prefs.cache_time = 0;
249 twoaday 181 agent_flush_cache ();
250     }
251 twoaday 180 else if (rc > 720) {
252     msg_box( dlg, _("Please enter a value that is between 1-720.\nIt is not "
253     "a good idea to cache the passphrase more than 12 hours."),
254 twoaday 117 _("Preferences"), MB_ERR);
255     SetDlgItemInt (dlg, IDC_PREFS_CACHETIME, 0, FALSE);
256 werner 36 return TRUE;
257     }
258     if (reg_prefs.cache_time != rc)
259     agent_flush_cache ();
260 twoaday 180 reg_prefs.cache_time = rc*60;
261 twoaday 117 rc = GetDlgItemInt (dlg, IDC_PREFS_WORDWRAP, NULL, FALSE);
262 twoaday 180 if (rc <= 0)
263 werner 36 reg_prefs.word_wrap = 0;
264 twoaday 117 else if (rc > 80) {
265 twoaday 176 msg_box (dlg, _("Please enter a value between 1-80."),
266     _("Preferences"), MB_ERR);
267 werner 36 return TRUE;
268     }
269 twoaday 218 reg_prefs.default_ext = SendDlgItemMessage (dlg, IDC_PREFS_DEFEXT, CB_GETCURSEL, 0, 0);
270 werner 36 reg_prefs.word_wrap = rc;
271 twoaday 180 reg_prefs.use_viewer = IsDlgButtonChecked (dlg, IDC_PREFS_VIEWER)? 1 : 0;
272     reg_prefs.backup.mode = IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME ) ? 1 :
273     IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER)? 2 : 0;
274 twoaday 222 reg_prefs.backup.include_secr = IsDlgButtonChecked (dlg, IDC_PREFS_BAKSECRING)? 1 : 0;
275 twoaday 273 #ifdef WINPT_MOBILE
276     /* We cannot allow backups to a specific path outside
277     the USB disk, so we disable it. */
278 twoaday 180 if (reg_prefs.backup.mode == 2) {
279 twoaday 273 reg_prefs.backup.mode = 1;
280     reg_prefs.backup.include_secr = 0;
281     }
282     #endif
283     if (reg_prefs.backup.mode == 2) {
284 twoaday 180 if (!GetDlgItemText (dlg, IDC_PREFS_BAKPATH, path, sizeof (path)-1)
285     || dir_exist_check (path)) {
286     msg_box (dlg, _("The specified backup folder is invalid."),
287     _("Preferences"), MB_ERR);
288 werner 36 return TRUE;
289     }
290     free_if_alloc (reg_prefs.backup.path);
291     p = reg_prefs.backup.path = m_strdup (path);
292     if (!p)
293     BUG (0);
294     }
295    
296     if (IsDlgButtonChecked (dlg, IDC_PREFS_DISABLE_HOTKEYS)) {
297     hotkeys_unregister (glob_hwnd);
298     reg_prefs.no_hotkeys = 1;
299     }
300     else {
301     reg_prefs.no_hotkeys = 0;
302 twoaday 180 for (i = 0; hotkeys[i]; i++) {
303     rc = GetDlgItemText (dlg, hotkeys[i], t, sizeof (t)-1);
304     if (rc > 0 && check_hotkey (&t[0]))
305     hotkey_enable (&wpt_hotkeys[i], t);
306 werner 36 else
307 twoaday 180 hotkey_disable (&wpt_hotkeys[i]);
308 werner 36 }
309     set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "DisableHotkeys", "0");
310     reg_prefs.no_hotkeys = 0;
311     }
312    
313 twoaday 180 rc = set_reg_winpt_prefs (&reg_prefs);
314     if (rc)
315 werner 36 msg_box (dlg, winpt_strerror (rc), _("Preferences"), MB_ERR);
316    
317     if (reg_prefs.no_hotkeys == 0) {
318     hotkeys_unregister (glob_hwnd);
319 twoaday 180 rc = hotkeys_register (glob_hwnd);
320     if (rc)
321 werner 36 msg_box (NULL, winpt_strerror (rc), _("Hotkeys"), MB_ERR);
322     }
323     EndDialog (dlg, TRUE);
324     return TRUE;
325    
326     case IDCANCEL:
327 twoaday 117 /* Reset backup mode if no mode was chosen. */
328     if (reg_prefs.auto_backup == 1 &&
329     !IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) &&
330     !IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER))
331     reg_prefs.auto_backup = 0;
332     EndDialog (dlg, FALSE);
333 werner 36 return FALSE;
334     }
335     break;
336     }
337    
338     return FALSE;
339     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26