/[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 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 128 by twoaday, Mon Dec 19 13:05:59 2005 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.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * 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   * 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   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
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  #include <windows.h>  #ifdef HAVE_CONFIG_H
22    #include <config.h>
23  #include "../resource.h"  #endif
24  #include "wptNLS.h"  
25  #include "wptTypes.h"  #include <windows.h>
26  #include "wptErrors.h"  
27  #include "wptGPG.h"  #include "resource.h"
28  #include "wptRegistry.h"  #include "wptNLS.h"
29  #include "wptCommonCtl.h"  #include "wptTypes.h"
30  #include "wptContext.h" /* for passphrase_s */  #include "wptErrors.h"
31  #include "wptDlgs.h"  #include "wptGPG.h"
32  #include "wptW32API.h"  #include "wptRegistry.h"
33  #include "wptVersion.h"  #include "wptCommonCtl.h"
34    #include "wptW32API.h"
35  static struct {  
36      const char * name;  
37      int id;  /* Load the GPG4WIN default values and disabled the
38  } opts[] = {     dialog items to indicate these are fixed values.
39      {"HomeDir",    IDC_GPGPREFS_HOMEDIR},     Return value: true if GPG4win was found. */
40      {"gpgProgram", IDC_GPGPREFS_EXEDIR},  static bool
41      {"OptFile",    IDC_GPGPREFS_OPTFILE},  load_gpg4win_values (HWND dlg)
42      {0}  {
43  };      char *path;
44        char *p;
45    
46  BOOL CALLBACK      path = get_reg_entry_gpg4win (NULL);
47  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      if (!path)
48  {          return false;
49      char exedir[1024], homedir[1024], optfile[1024];      p = make_filename (path, "gpg", "exe");
50      char locale_dir[1024];      if (p) {
51      char * p = NULL, t[512];          if (file_exist_check (p) == 0) {
52      const char * s;              SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p);
53      int have_optfile = 0;              EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_EXEDIR), FALSE);
54      gpg_optfile_t opt = NULL;              EnableWindow (GetDlgItem (dlg, IDC_GPGREFS_EXEDLG), FALSE);
55      gpg_option_t e;          }
56      UINT n;          free_if_alloc (p);
57            }
58      switch ( msg ) {  
59      case WM_INITDIALOG:      p = get_reg_entry_mo ();
60  #ifndef LANG_DE      if (p) {
61          SetWindowText( dlg, _("GnuPG Preferences") );          if (dir_exist_check (p) == 0) {
62          SetDlgItemText( dlg, IDC_GPGPREFS_HOMEINF,              SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p);
63                         _("GnuPG home directory  (where both keyrings are located)") );              EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCALE), FALSE);
64          SetDlgItemText( dlg, IDC_GPGPREFS_OPTINF,          }
65                         _("GnuPG config file (default: use gpg.conf)") );          free_if_alloc (p);
66          SetDlgItemText( dlg, IDC_GPGPREFS_EXEINF,      }
67                         _("GnuPG exe file location (full path with added gpg.exe)") );  
68          SetDlgItemText( dlg, IDC_GPGPREFS_LOCALINF,      p = multi_gnupg_path (1);
69                         _("Locale directory (to access the translation files)") );      if (p) {
70  #endif          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
71          for( n=0; (s = opts[n].name); n++ ) {          EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE);
72              p = get_reg_entry_gpg( s );          EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE);
73              if( p ) {          free_if_alloc (p);
74                  SetDlgItemText( dlg, opts[n].id, p );      }
75                  free_if_alloc( p );  
76              }      return true;
77          }  }
78            
79          if( (p = get_reg_entry_mo( )) ) {  
80              SetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, p );  /* Load the GPG values direct from the registry. */
81              free_if_alloc( p );  static void
82          }  load_registry_values (HWND dlg)
83          p = get_gnupg_cfgfile( );  {
84          if( p ) {      char *p;
85              parse_gpg_options( p, &opt );  
86              free_if_alloc( p );      p = get_reg_entry_gpg ("HomeDir");
87              if( opt && find_option( opt, "force-v3-sigs" ) )      if (p) {
88                  CheckDlgButton( dlg, IDC_GPGPREFS_V3SIGS, BST_CHECKED );          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
89              if( opt && (e=find_option( opt, "comment" )) )          free_if_alloc (p);      
90                  SetDlgItemText( dlg, IDC_GPGPREFS_COMMENT, e->val );      }
91              if( opt && (e=find_option( opt, "encrypt-to" )) )      p = get_reg_entry_gpg ("gpgProgram");
92                  SetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, e->val );                if (p) {
93          }          SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p);
94          center_window( dlg );          free_if_alloc (p);      
95          SetForegroundWindow( dlg );      }
96          return TRUE;      p = get_reg_entry_mo ();
97                if (p) {
98      case WM_SYSCOMMAND:          SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p);
99          if( LOWORD( wparam ) == SC_CLOSE )          free_if_alloc (p);        
100              EndDialog( dlg, TRUE );      }
101          return FALSE;      if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) {
102                    p = multi_gnupg_path (0);
103      case WM_COMMAND:          if (p && dir_exist_check (p) == 0)
104          switch ( LOWORD( wparam ) ) {              SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
105          case IDC_GPGPREFS_SAVE:          free_if_alloc (p);
106              if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) {      }
107                  msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR );  }
108                  return FALSE;  
109              }  
110              if( dir_exist_check( homedir ) ) {  /* Dialog box procedure for the GPG preferences. */
111                  _snprintf( t, sizeof t - 1, "%s: %s", homedir, winpt_strerror(WPTERR_DIR_OPEN) );  BOOL CALLBACK
112                  msg_box( dlg, t, _("Preferences"), MB_ERR );  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
113                  return FALSE;  {
114              }      static int gpg4win = false;
115                    char exedir[512];
116              if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) {      char homedir[512];
117                  if ( file_exist_check( optfile ) ) {      char locale_dir[512];
118                      n = msg_box(dlg, _("Could not find GPG config file.\n"      char *p = NULL, t[256];
119                                         "Do you want to create a config file?"),      const char *s;
120                                  _("Preferences"), MB_INFO|MB_YESNO );      const char *folder;
121                      if( n == IDNO ) {      gpg_optfile_t opt = NULL;
122                          SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" );      gpg_option_t e;
123                          return FALSE;      UINT n;
124                      }      
125                      else if( n == IDYES ) {      switch (msg) {
126                          FILE * fp = fopen( optfile, "wb" );      case WM_INITDIALOG:
127                          if( fp )          SetWindowText (dlg, _("GnuPG Preferences"));
128                              fclose( fp );          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
129                          if( file_exist_check( optfile ) ) {                         _("GnuPG home directory  (where both keyrings are located)"));
130                              log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) );          SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
131                              return FALSE;                          _("GnuPG exe file location (full path with added gpg.exe)"));
132                          }          SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,
133                          have_optfile = 1;                          _("Locale directory (to access the translation files)"));
134                      }          SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign"));
135                  }          SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
136                  else          SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
137                      have_optfile = 1;          SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
138                  if( have_optfile ) {  
139                      if( set_reg_entry_gpg( "OptFile", optfile ) ) {          gpg4win = load_gpg4win_values (dlg);
140                          msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR );          if (!gpg4win)
141                          return FALSE;              load_registry_values (dlg);
142                      }  
143                  }          p = get_gnupg_cfgfile ();
144              }          if (p) {
145              else {              parse_gpg_options (p, &opt);
146                  char * t = get_gnupg_cfgfile ();              free_if_alloc (p);
147                  if (t && !file_exist_check (t))              if (opt) {
148                      set_reg_entry_gpg( "OptFile", t );                  if (find_option (opt, "ask-cert-level"))
149                  free_if_alloc (t);                      CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
150              }                  if ((e=find_option (opt, "comment")))
151              if ( set_reg_entry_gpg( "HomeDir", homedir ) ) {                                      SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
152                  msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR );                  if ((e=find_option (opt, "encrypt-to")))
153                  return FALSE;                      SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
154              }                  release_gpg_options (opt);
155              if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) {              }
156                  msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR );          }
157                  return FALSE;  
158              }          center_window (dlg, NULL);
159              if( file_exist_check( exedir ) ) {          SetForegroundWindow (dlg);
160                  msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR );          return TRUE;
161                  return FALSE;  
162              }      case WM_DESTROY:
163              if( set_reg_entry_gpg("gpgProgram", exedir ) ) {          gpg4win = false;
164                  msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR );          break;
165                  return FALSE;  
166              }      case WM_SYSCOMMAND:
167              if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) {          if (LOWORD (wparam) == SC_CLOSE)
168                  if (dir_exist_check (locale_dir)) {              EndDialog (dlg, TRUE);
169                      log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN));          return FALSE;
170                      return FALSE;          
171                  }      case WM_COMMAND:
172                  set_reg_entry_mo (locale_dir);          switch (LOWORD (wparam)) {
173                  set_gettext_file ("winpt", locale_dir);          case IDC_GPGPREFS_SAVE:
174              }              if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
175              else                                   homedir, sizeof (homedir) -1)) {
176                  set_reg_entry_mo ("");                  msg_box (dlg, _("Please enter the GnuPG home directory."),
177                             _("Preferences"), MB_ERR);
178              p = get_gnupg_cfgfile ();                  return FALSE;
179              if( !p ) {              }
180                  msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR );              if (dir_exist_check (homedir)) {
181                  EndDialog( dlg, FALSE );                  _snprintf (t, sizeof (t) - 1, "%s: %s", homedir,
182              }                             winpt_strerror (WPTERR_DIR_OPEN));
183              parse_gpg_options( p, &opt );                  msg_box (dlg, t, _("Preferences"), MB_ERR);
184                    return FALSE;
185              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_V3SIGS ) )              }
186                  modify_entry( opt, ENTRY_SINGLE, "force-v3-sigs", NULL );              
187              else              if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
188                  delete_option( opt, "force-v3-sigs" );                  msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
189                             _("Preferences"), MB_ERR);
190              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))                  return FALSE;
191                  modify_entry( opt, ENTRY_MULTI, "comment", t );              }
192              else if( n == 0 )              if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
193                  modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );                                   exedir, sizeof (exedir) -1)) {
194              else                  msg_box (dlg, _("Please enter where GPG.exe is located."),
195                  delete_option( opt, "comment" );                           _("Preferences"), MB_ERR);
196                    return FALSE;
197              if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )              }
198                  modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );              if (file_exist_check (exedir)) {
199              else                  msg_box (dlg, _("Could not find the GPG program in this directory."),
200                  delete_option( opt, "encrypt-to" );                           _("Preferences"), MB_ERR);
201                    return FALSE;
202              commit_gpg_options( p, opt );              }
203              release_gpg_options( opt );              if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
204              EndDialog( dlg, TRUE );                  msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
205              return TRUE;                           _("Preferences"), MB_ERR);
206                                return FALSE;
207          case IDC_GPGPREFS_HOMEDLG:              }
208              const char * home;              if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1)) {
209              home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);                  if (dir_exist_check (locale_dir)) {
210              if (home) {                      log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir,
211                  SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home);                               winpt_strerror (WPTERR_DIR_OPEN));
212                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)                      return FALSE;
213                      break;                  }
214                  char *name = make_filename (home, "gpg", "exe");                  if (!gpg4win)
215                  if (file_exist_check (name) == 0)                      set_reg_entry_mo (locale_dir);
216                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);                  set_gettext_file ("winpt", locale_dir);
217                  free_if_alloc (name);              }
218                  name = make_filename (home, "gpg", "conf");              else if (!gpg4win)
219                  if (file_exist_check (name) == 0)                  set_reg_entry_mo ("");
220                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);  
221                  free_if_alloc (name);              p = get_gnupg_cfgfile ();
222              }              if (!p) {
223              break;                  msg_box (dlg, _("Could not get GPG config file"),
224                             _("Preferences"), MB_ERR);
225          case IDC_GPGREFS_EXEDLG:                  EndDialog (dlg, FALSE);
226              const char * bin;              }
227              bin = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG binary"), _("Executable Files (*.exe)\0*.exe\0\0"), NULL );              parse_gpg_options (p, &opt);
228              if( bin )  
229                  SetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, bin );              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) {
230              return TRUE;                  modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL );
231                                reg_prefs.gpg.ask_cert_level = 1;
232          case IDC_GPGPREFS_OPTDLG:              }
233              const char * opt;              else {
234              opt = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG config file"), NULL, NULL );                  delete_option (opt, "ask-cert-level");
235              if( opt )                  reg_prefs.gpg.ask_cert_level = 0;
236                  SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, opt );              }
237              return TRUE;  
238          }              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))
239          break;                  modify_entry( opt, ENTRY_MULTI, "comment", t );
240      }              else if( n == 0 )
241                        modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );
242      return FALSE;              else
243  } /* gpgprefs_dlg_proc */                  delete_option( opt, "comment" );
244    
245                if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )
246                    modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );
247                else
248                    delete_option( opt, "encrypt-to" );
249    
250                commit_gpg_options (p, opt);
251                release_gpg_options (opt);
252                EndDialog (dlg, TRUE);
253                return TRUE;
254                
255            case IDC_GPGPREFS_HOMEDLG:
256                folder = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);
257                if (folder) {
258                    char *name;
259    
260                    SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
261                    if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)
262                        break;
263                    name = make_filename (folder, "gpg", "exe");
264                    if (file_exist_check (name) == 0)
265                        SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
266                    free_if_alloc (name);
267                    name = make_filename (folder, "gpg", "conf");
268                    if (file_exist_check (name) == 0)
269                        SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);
270                    free_if_alloc (name);
271                }
272                return TRUE;
273    
274            case IDC_GPGPREFS_LOCDLG:
275                folder = get_folder_dlg (dlg, _("Choose locale directory"), NULL);
276                if (folder)
277                    SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder);
278                return TRUE;
279                
280            case IDC_GPGREFS_EXEDLG:
281                s = get_fileopen_dlg (dlg, _("Choose GPG binary"),
282                                      _("Executable Files (*.exe)\0*.exe\0\0"),
283                                      NULL);
284                if (s)
285                    SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
286                return TRUE;
287            }
288            break;
289        }
290        
291        return FALSE;
292    }

Legend:
Removed from v.22  
changed lines
  Added in v.128

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26