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

Diff of /trunk/Src/wptGPGPrefsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 117 by twoaday, Thu Dec 8 09:26:32 2005 UTC revision 161 by twoaday, Thu Jan 19 16:10:37 2006 UTC
# Line 1  Line 1 
1  /* wptGPGPrefsDlg.cpp - WinPT GnuPG Preferences  /* wptGPGPrefsDlg.cpp - GnuPG Preferences
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
# Line 17  Line 17 
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    
21  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
22  #include <config.h>  #include <config.h>
23  #endif  #endif
# Line 30  Line 31 
31  #include "wptGPG.h"  #include "wptGPG.h"
32  #include "wptRegistry.h"  #include "wptRegistry.h"
33  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
 #include "wptContext.h" /* for passphrase_s */  
 #include "wptDlgs.h"  
34  #include "wptW32API.h"  #include "wptW32API.h"
 #include "wptVersion.h"  
35    
36  static struct {  
37      const char *name;  /* Enable all edit items if @val is TRUE, disable them otherwise. */
38      int id;  static void
39  } opts[] = {  enable_disable_items (HWND dlg, BOOL val)
40      {"HomeDir",    IDC_GPGPREFS_HOMEDIR},  {
41      {"gpgProgram", IDC_GPGPREFS_EXEDIR},      EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_EXEDIR), val);
42      {"OptFile",    IDC_GPGPREFS_OPTFILE},      EnableWindow (GetDlgItem (dlg, IDC_GPGREFS_EXEDLG), val);
43      {NULL, 0}      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. */  /* Dialog box procedure for the GPG preferences. */
125  BOOL CALLBACK  BOOL CALLBACK
126  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
127  {  {
128      char exedir[512], homedir[512], optfile[512];      static int gpg4win = false;
129        char exedir[512];
130        char homedir[512];
131      char locale_dir[512];      char locale_dir[512];
132      char *p = NULL, t[256];      char *p = NULL, t[256];
133      const char *s;      const char *s;
134      const char *file;      const char *folder;
     int have_optfile = 0;  
135      gpg_optfile_t opt = NULL;      gpg_optfile_t opt = NULL;
136      gpg_option_t e;      gpg_option_t e;
137      UINT n;      UINT n;
# Line 64  gpgprefs_dlg_proc (HWND dlg, UINT msg, W Line 141  gpgprefs_dlg_proc (HWND dlg, UINT msg, W
141          SetWindowText (dlg, _("GnuPG Preferences"));          SetWindowText (dlg, _("GnuPG Preferences"));
142          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
143                         _("GnuPG home directory  (where both keyrings are located)"));                         _("GnuPG home directory  (where both keyrings are located)"));
         SetDlgItemText ( dlg, IDC_GPGPREFS_OPTINF,  
                         _("GnuPG config file (default: use gpg.conf)"));  
144          SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,          SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
145                          _("GnuPG exe file location (full path with added gpg.exe)"));                          _("GnuPG exe file location (full path with added gpg.exe)"));
146          SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,          SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,
# Line 74  gpgprefs_dlg_proc (HWND dlg, UINT msg, W Line 149  gpgprefs_dlg_proc (HWND dlg, UINT msg, W
149          SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));          SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
150          SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));          SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
151          SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));          SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
152            SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDLG, _("Browse..."));
153          for (n=0; (s = opts[n].name); n++) {          SetDlgItemText (dlg, IDC_GPGREFS_EXEDLG, _("Browse..."));
154              p = get_reg_entry_gpg (s);          SetDlgItemText (dlg, IDC_GPGPREFS_LOCDLG, _("Browse..."));
155              if (p) {          SetDlgItemText (dlg, IDC_GPGPREFS_OVRDEFAULT, _("&Overwrite default settings"));
156                  SetDlgItemText (dlg, opts[n].id, p);  
157                  free_if_alloc (p);          gpg4win = load_gpg4win_values (dlg);
158              }          if (!gpg4win) {
159                load_registry_values (dlg);
160                CheckDlgButton (dlg, IDC_GPGPREFS_OVRDEFAULT, BST_CHECKED);
161          }          }
162            
         p = get_reg_entry_mo ();  
         if (p) {  
             SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p);  
             free_if_alloc (p);  
         }  
163          p = get_gnupg_cfgfile ();          p = get_gnupg_cfgfile ();
164          if (p) {          if (p) {
165              parse_gpg_options (p, &opt);              parse_gpg_options (p, &opt);
166              free_if_alloc (p);              free_if_alloc (p);
167              if (opt && find_option( opt, "ask-cert-level" ) )              if (opt) {
168                  CheckDlgButton( dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED );                  if (find_option (opt, "ask-cert-level"))
169              if (opt && (e=find_option( opt, "comment" )) )                      CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
170                    if ((e=find_option (opt, "comment")))
171                  SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);                  SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
172              if (opt && (e=find_option( opt, "encrypt-to")))                  if ((e=find_option (opt, "encrypt-to")))
173                  SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);                      SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
             if (opt)  
174                  release_gpg_options (opt);                  release_gpg_options (opt);
175                }
176          }          }
177    
178          center_window (dlg, NULL);          center_window (dlg, NULL);
179          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
180          return TRUE;          return TRUE;
181            
182        case WM_DESTROY:
183            gpg4win = false;
184            break;
185    
186      case WM_SYSCOMMAND:      case WM_SYSCOMMAND:
187          if( LOWORD( wparam ) == SC_CLOSE )          if (LOWORD (wparam) == SC_CLOSE)
188              EndDialog( dlg, TRUE );              EndDialog (dlg, TRUE);
189          return FALSE;          return FALSE;
190                    
191      case WM_COMMAND:      case WM_COMMAND:
192          switch ( LOWORD( wparam ) ) {          if (HIWORD (wparam) == BN_CLICKED &&
193                LOWORD (wparam) == IDC_GPGPREFS_OVRDEFAULT) {
194                if (IsDlgButtonChecked (dlg, LOWORD (wparam)))
195                    enable_disable_items (dlg, TRUE);
196                else
197                    enable_disable_items (dlg, FALSE);
198                break;
199            }
200    
201            switch (LOWORD (wparam)) {
202          case IDC_GPGPREFS_SAVE:          case IDC_GPGPREFS_SAVE:
203              if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) {              if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
204                  msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR );                                   homedir, sizeof (homedir) -1)) {
205                    msg_box (dlg, _("Please enter the GnuPG home directory."),
206                             _("Preferences"), MB_ERR);
207                  return FALSE;                  return FALSE;
208              }              }
209              if (dir_exist_check (homedir)) {              if (dir_exist_check (homedir)) {
# Line 125  gpgprefs_dlg_proc (HWND dlg, UINT msg, W Line 213  gpgprefs_dlg_proc (HWND dlg, UINT msg, W
213                  return FALSE;                  return FALSE;
214              }              }
215                            
216              if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) {              if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
217                  if ( file_exist_check( optfile ) ) {                  msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
218                      n = msg_box(dlg, _("Could not find GPG config file.\n"                           _("Preferences"), MB_ERR);
                                        "Do you want to create a config file?"),  
                                 _("Preferences"), MB_INFO|MB_YESNO );  
                     if( n == IDNO ) {  
                         SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" );  
                         return FALSE;  
                     }  
                     else if( n == IDYES ) {  
                         FILE * fp = fopen( optfile, "wb" );  
                         if( fp )  
                             fclose( fp );  
                         if( file_exist_check( optfile ) ) {  
                             log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) );  
                             return FALSE;  
                         }  
                         have_optfile = 1;  
                     }  
                 }  
                 else  
                     have_optfile = 1;  
                 if( have_optfile ) {  
                     if( set_reg_entry_gpg( "OptFile", optfile ) ) {  
                         msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR );  
                         return FALSE;  
                     }  
                 }  
             }  
             else {  
                 char *cfg = get_gnupg_cfgfile ();  
                 if (cfg && !file_exist_check (cfg))  
                     set_reg_entry_gpg( "OptFile", cfg);  
                 free_if_alloc (cfg);  
             }  
             if ( set_reg_entry_gpg( "HomeDir", homedir ) ) {                      
                 msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR );  
219                  return FALSE;                  return FALSE;
220              }              }
221              if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) {              if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
222                  msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR );                                   exedir, sizeof (exedir) -1)) {
223                    msg_box (dlg, _("Please enter where GPG.exe is located."),
224                             _("Preferences"), MB_ERR);
225                  return FALSE;                  return FALSE;
226              }              }
227              if( file_exist_check( exedir ) ) {              if (file_exist_check (exedir)) {
228                  msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR );                  msg_box (dlg, _("Could not find the GPG program in this directory."),
229                             _("Preferences"), MB_ERR);
230                  return FALSE;                  return FALSE;
231              }              }
232              if( set_reg_entry_gpg("gpgProgram", exedir ) ) {              if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
233                  msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR );                  msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
234                             _("Preferences"), MB_ERR);
235                  return FALSE;                  return FALSE;
236              }              }
237              if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) {              if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1)) {
238                  if (dir_exist_check (locale_dir)) {                  if (dir_exist_check (locale_dir)) {
239                      log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN));                      log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir,
240                                 winpt_strerror (WPTERR_DIR_OPEN));
241                      return FALSE;                      return FALSE;
242                  }                  }
243                  set_reg_entry_mo (locale_dir);                  if (!gpg4win)
244                        set_reg_entry_mo (locale_dir);
245                  set_gettext_file ("winpt", locale_dir);                  set_gettext_file ("winpt", locale_dir);
246              }              }
247              else              else if (!gpg4win)
248                  set_reg_entry_mo ("");                  set_reg_entry_mo ("");
249    
250              p = get_gnupg_cfgfile ();              p = get_gnupg_cfgfile ();
251              if( !p ) {              if (!p) {
252                  msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR );                  msg_box (dlg, _("Could not get GPG config file"),
253                  EndDialog( dlg, FALSE );                           _("Preferences"), MB_ERR);
254                    EndDialog (dlg, FALSE);
255              }              }
256              parse_gpg_options( p, &opt );              parse_gpg_options (p, &opt);
257    
258              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) {              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) {
259                  modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL );                  modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL );
260                  reg_prefs.gpg.ask_cert_level = 1;                  reg_prefs.gpg.ask_cert_level = 1;
261              }              }
262              else {              else {
263                  delete_option( opt, "ask-cert-level" );                  delete_option (opt, "ask-cert-level");
264                  reg_prefs.gpg.ask_cert_level = 0;                  reg_prefs.gpg.ask_cert_level = 0;
265              }              }
266    
# Line 215  gpgprefs_dlg_proc (HWND dlg, UINT msg, W Line 276  gpgprefs_dlg_proc (HWND dlg, UINT msg, W
276              else              else
277                  delete_option( opt, "encrypt-to" );                  delete_option( opt, "encrypt-to" );
278    
279              commit_gpg_options( p, opt );              commit_gpg_options (p, opt);
280              release_gpg_options (opt);              release_gpg_options (opt);
281              EndDialog( dlg, TRUE );              EndDialog (dlg, TRUE);
282              return TRUE;              return TRUE;
283                            
284          case IDC_GPGPREFS_HOMEDLG:          case IDC_GPGPREFS_HOMEDLG:
285              const char *home;              folder = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);
286              home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);              if (folder) {
287              if (home) {                  char *name;
288                  SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home);  
289                    SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
290                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)
291                      break;                      break;
292                  char *name = make_filename (home, "gpg", "exe");                  name = make_filename (folder, "gpg", "exe");
293                  if (file_exist_check (name) == 0)                  if (file_exist_check (name) == 0)
294                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
295                  free_if_alloc (name);                  free_if_alloc (name);
296                  name = make_filename (home, "gpg", "conf");                  name = make_filename (folder, "gpg", "conf");
297                  if (file_exist_check (name) == 0)                  if (file_exist_check (name) == 0)
298                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);
299                  free_if_alloc (name);                  free_if_alloc (name);
300              }              }
301              break;              return TRUE;
302    
303            case IDC_GPGPREFS_LOCDLG:
304                folder = get_folder_dlg (dlg, _("Choose locale directory"), NULL);
305                if (folder)
306                    SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder);
307                return TRUE;
308                
309          case IDC_GPGREFS_EXEDLG:          case IDC_GPGREFS_EXEDLG:
310              file = get_fileopen_dlg (dlg, _("Choose GPG binary"),              s = get_fileopen_dlg (dlg, _("Choose GPG binary"),
311                                       _("Executable Files (*.exe)\0*.exe\0\0"),                                    _("Executable Files (*.exe)\0*.exe\0\0"),
312                                       NULL);                                    NULL);
313              if (file)              if (s)
314                  SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, file);                  SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
             return TRUE;  
               
         case IDC_GPGPREFS_OPTDLG:  
             file = get_fileopen_dlg (dlg, _("Choose GPG config file"),  
                                      NULL, NULL);  
             if (file)  
                 SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, file);  
315              return TRUE;              return TRUE;
316                
317            case IDCANCEL:
318                EndDialog (dlg, FALSE);
319                return TRUE;
320          }          }
321          break;          break;
322      }      }

Legend:
Removed from v.117  
changed lines
  Added in v.161

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26