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

Annotation of /trunk/Src/wptPreferencesDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 11422 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 twoaday 2 /* wptPreferencesDlg.cpp - Dialog for the preferences
2 twoaday 22 * Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz
3 twoaday 2 *
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     #include <windows.h>
22     #include <shlobj.h>
23    
24     #include "wptNLS.h"
25     #include "wptGPG.h"
26     #include "wptCommonCtl.h"
27     #include "wptDlgs.h"
28     #include "wptTypes.h"
29     #include "wptErrors.h"
30     #include "wptRegistry.h"
31     #include "wptHotkey.h"
32     #include "wptW32API.h"
33     #include "wptVersion.h"
34     #include "wptAgent.h"
35     #include "wptKeyserver.h"
36     #include "../resource.h"
37    
38    
39     static int hotkeys[] = {
40     IDC_PREFS_CLIP_ENCRYPT,
41     IDC_PREFS_CLIP_DECRYPT_VERIFY,
42     IDC_PREFS_CLIP_SIGN,
43     IDC_PREFS_CLIP_SIGNENC,
44     IDC_PREFS_CWS_ENCRYPT,
45     IDC_PREFS_CWS_DECRYPT_VERIFY,
46     IDC_PREFS_CWS_SIGN,
47     IDC_PREFS_CWS_SIGNENC,
48     0
49     };
50    
51    
52     static int
53 twoaday 22 check_hotkey (char * key)
54 twoaday 2 {
55 twoaday 22 if (*key >= 'A' && *key <= 'Z')
56 twoaday 2 return 1;
57 twoaday 22 else if (*key >= 'a' && *key <= 'z') {
58 twoaday 2 *key = *key - 32;
59     return 1;
60 twoaday 22 }
61 twoaday 2 return 0;
62     } /* check_hotkey */
63    
64    
65     static void
66 twoaday 22 disable_hotkey_items (HWND dlg, int val)
67 twoaday 2 {
68 twoaday 22 int mode = val? FALSE : TRUE;
69 twoaday 2 int i, id;
70    
71 twoaday 22 for (i=0; (id = hotkeys[i]); i++)
72 twoaday 2 EnableWindow( GetDlgItem( dlg, id ), mode );
73     } /* disable_hotkey_items */
74    
75    
76     static void
77 twoaday 22 enable_backup_items (HWND dlg, int val)
78 twoaday 2 {
79 twoaday 22 int mode = val? TRUE : FALSE;
80 twoaday 2
81 twoaday 22 EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKHOME), mode);
82     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKUSER), mode);
83     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), mode);
84     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), mode);
85 twoaday 2 } /* enable_backup_items */
86    
87    
88     static void
89 twoaday 22 set_keylist_mode (HWND dlg)
90 twoaday 2 {
91 twoaday 22 HWND cb = GetDlgItem (dlg, IDC_PREFS_LISTMODE);
92     combox_add_string (cb, (char *)"NORMAL");
93     combox_add_string (cb, (char *)"MINIMAL");
94     SendMessage (cb, CB_SETCURSEL, (WPARAM)reg_prefs.keylist_mode, 0);
95 twoaday 2 } /* set_keylist_mode */
96    
97    
98     static void
99 twoaday 22 set_wipe_mode (HWND dlg)
100 twoaday 2 {
101 twoaday 22 HWND cb = GetDlgItem (dlg, IDC_PREFS_WIPEMODE);
102     combox_add_string (cb, (char *)"Simple");
103     combox_add_string (cb, (char *)"DoD");
104     combox_add_string (cb, (char *)"Gutmann");
105     SendMessage (cb, CB_SETCURSEL, (WPARAM)reg_prefs.wipe_mode, 0);
106 twoaday 2 } /* set_wipe_mode */
107    
108    
109     BOOL CALLBACK
110 twoaday 22 prefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
111 twoaday 2 {
112     int rc;
113     int i, id;
114     char t[2];
115     char path[256], * p;
116     HWND cb;
117    
118     switch ( msg ) {
119     case WM_INITDIALOG:
120     #ifndef LANG_DE
121     SetWindowText( dlg, _("WinPT Preferences") );
122     SetDlgItemText( dlg, IDC_PREFS_SECMODE, _("Do not use any &temporary files") );
123     SetDlgItemText( dlg, IDC_PREFS_VIEWER, _("Use clipboard &viewer to display the plaintext") );
124     SetDlgItemText( dlg, IDC_PREFS_WWINFO, _("Word wrap cleartext &signatures at column") );
125     SetDlgItemText( dlg, IDC_PREFS_DISABLE_HOTKEYS, _("&Disable hotkeys (Not recommended!)") );
126     #endif
127     SetDlgItemInt( dlg, IDC_PREFS_CACHETIME, reg_prefs.cache_time, TRUE );
128     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     if (reg_prefs.kserv_conf)
132     SetDlgItemText (dlg, IDC_PREFS_KSERVER, reg_prefs.kserv_conf);
133    
134 twoaday 22 for (i=0; (id=hotkeys[i]); i++)
135     SetDlgItemText (dlg, id, reg_hotkeys[i].key);
136 twoaday 2 CheckDlgButton( dlg, IDC_PREFS_DISABLE_HOTKEYS,
137     reg_prefs.no_hotkeys ? BST_CHECKED : BST_UNCHECKED );
138     CheckDlgButton( dlg, IDC_PREFS_SECMODE,
139     reg_prefs.use_tmpfiles ? BST_UNCHECKED : BST_CHECKED );
140     CheckDlgButton( dlg, IDC_PREFS_VIEWER,
141     reg_prefs.use_viewer ? BST_CHECKED: BST_UNCHECKED );
142     CheckDlgButton( dlg, IDC_PREFS_TRUST,
143     reg_prefs.always_trust? BST_CHECKED : BST_UNCHECKED );
144     CheckDlgButton( dlg, IDC_PREFS_AUTOBACKUP,
145     reg_prefs.auto_backup? BST_CHECKED : BST_UNCHECKED );
146     CheckDlgButton( dlg, IDC_PREFS_BAKHOME,
147     reg_prefs.backup.mode==1? BST_CHECKED : BST_UNCHECKED );
148     CheckDlgButton( dlg, IDC_PREFS_BAKUSER,
149     reg_prefs.backup.mode==2? BST_CHECKED : BST_UNCHECKED );
150     if( reg_prefs.no_hotkeys )
151     disable_hotkey_items( dlg, 1 );
152     if( !reg_prefs.auto_backup )
153     enable_backup_items( dlg, 0 );
154     EnableWindow( GetDlgItem( dlg, IDC_PREFS_BAKPATH ),
155     reg_prefs.backup.mode==1?FALSE : TRUE );
156     EnableWindow( GetDlgItem( dlg, IDC_PREFS_BAKSELECT ),
157     reg_prefs.backup.mode==1? FALSE : TRUE );
158     set_keylist_mode( dlg );
159     set_wipe_mode( dlg );
160 twoaday 23 center_window( dlg, NULL );
161 twoaday 2 SetForegroundWindow( dlg );
162     return TRUE;
163    
164     case WM_SYSCOMMAND:
165     if( LOWORD( wparam ) == SC_CLOSE )
166     EndDialog( dlg, TRUE );
167     return FALSE;
168    
169     case WM_COMMAND:
170     switch( HIWORD(wparam) ) {
171     case BN_CLICKED:
172     switch( (int)LOWORD( wparam ) ) {
173     case IDC_PREFS_DISABLE_HOTKEYS:
174     reg_prefs.no_hotkeys ^= 1;
175     disable_hotkey_items( dlg, reg_prefs.no_hotkeys );
176     break;
177    
178 twoaday 22 case IDC_PREFS_TRUST:
179 twoaday 2 reg_prefs.always_trust ^= 1;
180     if( reg_prefs.always_trust ) {
181 twoaday 22 msg_box (dlg,
182 twoaday 2 _("In most cases it is not a good idea to enable this setting.\n"
183     "If you know what you are doing let this flag enabled, otherwise\n"
184     "it is safe to leave this flag untouched."),
185     _("Preferences"), MB_ICONWARNING|MB_OK );
186     }
187     break;
188    
189     case IDC_PREFS_AUTOBACKUP:
190     reg_prefs.auto_backup ^= 1;
191 twoaday 22 enable_backup_items (dlg, reg_prefs.auto_backup);
192     if (reg_prefs.auto_backup == 1) {
193     if (!IsDlgButtonChecked (dlg, IDC_PREFS_BAKHOME) &&
194     !IsDlgButtonChecked (dlg, IDC_PREFS_BAKUSER))
195     CheckDlgButton (dlg, IDC_PREFS_BAKHOME, BST_CHECKED);
196     }
197 twoaday 2 break;
198    
199     case IDC_PREFS_BAKHOME:
200 twoaday 22 EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), FALSE);
201     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), FALSE);
202 twoaday 2 break;
203    
204     case IDC_PREFS_BAKUSER:
205 twoaday 22 EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKSELECT), TRUE);
206     EnableWindow (GetDlgItem (dlg, IDC_PREFS_BAKPATH), TRUE);
207 twoaday 2 break;
208     }
209     break;
210     }
211     switch( LOWORD( wparam ) ) {
212     case IDC_PREFS_BAKSELECT:
213 twoaday 22 const char *bpath;
214     bpath = get_folder_dlg (dlg, _("Select GPG backup path"), NULL);
215     if (bpath)
216     SetDlgItemText (dlg, IDC_PREFS_BAKPATH, bpath);
217 twoaday 2 break;
218    
219     case IDC_PREFS_KSELFILE: {
220     const char * name;
221 twoaday 23 name = get_filename_dlg (dlg, FILE_OPEN, _("Please select a keyserver.conf file"),
222 twoaday 2 NULL, NULL);
223 twoaday 23 if (name != NULL)
224 twoaday 2 SetDlgItemText (dlg, IDC_PREFS_KSERVER, name);
225     break; }
226    
227     case IDOK:
228     rc = GetDlgItemInt( dlg, IDC_PREFS_CACHETIME, NULL, FALSE );
229     if( !rc )
230     reg_prefs.cache_time = 0;
231     else if( rc > 3600 ) {
232     msg_box( dlg, _("Please enter a value that is between 1-3600.\nIt is not "
233     "a good idea to cache the passphrase more than one hour."),
234     _("Preferences"), MB_ERR );
235     SetDlgItemInt( dlg, IDC_PREFS_CACHETIME, 0, FALSE );
236     return FALSE;
237     }
238     if (reg_prefs.cache_time != rc)
239     agent_flush_cache ();
240     reg_prefs.cache_time = rc;
241     rc = GetDlgItemInt( dlg, IDC_PREFS_WORDWRAP, NULL, FALSE );
242     if( !rc )
243     reg_prefs.word_wrap = 0;
244     else if( rc > 80 ) {
245     msg_box( dlg, _("Please enter a value between 1-80."), _("Preferences"), MB_ERR );
246     return FALSE;
247     }
248     reg_prefs.word_wrap = rc;
249     reg_prefs.use_tmpfiles = IsDlgButtonChecked( dlg, IDC_PREFS_SECMODE )? 0 : 1;
250     reg_prefs.use_viewer = IsDlgButtonChecked( dlg, IDC_PREFS_VIEWER )? 1 : 0;
251     reg_prefs.backup.mode = IsDlgButtonChecked( dlg, IDC_PREFS_BAKHOME ) ? 1 :
252     IsDlgButtonChecked( dlg, IDC_PREFS_BAKUSER )? 2 : 0;
253     if( reg_prefs.backup.mode == 2 ) {
254     if( !GetDlgItemText( dlg, IDC_PREFS_BAKPATH, path, sizeof (path)-1 )
255     || dir_exist_check( path ) ) {
256     msg_box( dlg, _("The specified backup folder is invalid."),
257     _("Preferences"), MB_ERR );
258     return FALSE;
259     }
260     free_if_alloc (reg_prefs.backup.path);
261     p = reg_prefs.backup.path = m_strdup (path);
262     if (!p)
263     BUG (0);
264     }
265     free_if_alloc (reg_prefs.kserv_conf);
266     if (!GetDlgItemText (dlg, IDC_PREFS_KSERVER, path, sizeof (path)-1)
267     || file_exist_check (path)) {
268     rc = msg_box (dlg, _("The specified keyserver config file is invalid.\n\n"
269     "Create new default config file?"),
270     _("Preferences"), MB_ERR|MB_YESNO);
271     if (rc == IDNO)
272     return FALSE;
273     else
274     {
275     char cwd[256], * fn;
276     FILE * fp = fopen ("keyserver.conf", "wb");
277 twoaday 22 if (fp) {
278 twoaday 2 fprintf (fp, "%s\r\n", DEF_HKP_KEYSERVER);
279     fclose (fp);
280     }
281     GetCurrentDirectory (DIM (cwd)-1, cwd);
282     fn = make_filename (cwd, "keyserver", "conf");
283     memset (path, 0, sizeof( path));
284     strncpy (path, fn, DIM (path)-1);
285     free_if_alloc (fn);
286     }
287     }
288     p = reg_prefs.kserv_conf = m_strdup (path);
289     if (!p)
290     BUG (0);
291    
292     cb = GetDlgItem( dlg, IDC_PREFS_LISTMODE );
293     reg_prefs.keylist_mode = SendMessage( cb, CB_GETCURSEL, 0, 0 );
294    
295     cb = GetDlgItem( dlg, IDC_PREFS_WIPEMODE );
296     reg_prefs.wipe_mode = SendMessage( cb, CB_GETCURSEL, 0, 0 );
297    
298     if (IsDlgButtonChecked (dlg, IDC_PREFS_DISABLE_HOTKEYS)) {
299     hotkeys_unregister (glob_hwnd);
300     reg_prefs.no_hotkeys = 1;
301     }
302     else {
303     reg_prefs.no_hotkeys = 0;
304     for( i = 0; (id = hotkeys[i]); i++ ) {
305     rc = GetDlgItemText( dlg, id, t, 2 );
306     if( rc && check_hotkey( &t[0] ) )
307     hotkey_enable( &reg_hotkeys[i], t );
308     else
309     hotkey_disable( &reg_hotkeys[i] );
310     }
311     set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "DisableHotkeys", "0");
312     reg_prefs.no_hotkeys = 0;
313     }
314    
315     if ((rc = set_reg_winpt_prefs (&reg_prefs)))
316     msg_box (dlg, winpt_strerror (rc), _("Preferences"), MB_ERR);
317    
318     if (reg_prefs.no_hotkeys == 0) {
319     hotkeys_unregister (glob_hwnd);
320     hotkeys_modify ();
321     if ((rc = hotkeys_register (glob_hwnd)))
322     msg_box (NULL, winpt_strerror (rc), _("Hotkeys"), MB_ERR);
323     }
324     EndDialog (dlg, TRUE);
325     return TRUE;
326    
327     case IDCANCEL:
328     EndDialog( dlg, FALSE );
329     return FALSE;
330     }
331     break;
332     }
333    
334     return FALSE;
335     } /* prefs_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26