/[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 139 by twoaday, Wed Jan 11 12:19:41 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.
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,              EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_LOCDLG), FALSE);
65                         _("GnuPG config file (default: use gpg.conf)") );          }
66          SetDlgItemText( dlg, IDC_GPGPREFS_EXEINF,          free_if_alloc (p);
67                         _("GnuPG exe file location (full path with added gpg.exe)") );      }
68          SetDlgItemText( dlg, IDC_GPGPREFS_LOCALINF,  
69                         _("Locale directory (to access the translation files)") );      p = multi_gnupg_path (1);
70  #endif      if (p) {
71          for( n=0; (s = opts[n].name); n++ ) {          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
72              p = get_reg_entry_gpg( s );          EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDIR), FALSE);
73              if( p ) {          EnableWindow (GetDlgItem (dlg, IDC_GPGPREFS_HOMEDLG), FALSE);
74                  SetDlgItemText( dlg, opts[n].id, p );          free_if_alloc (p);
75                  free_if_alloc( p );      }
76              }  
77          }      return true;
78            }
79          if( (p = get_reg_entry_mo( )) ) {  
80              SetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, p );  
81              free_if_alloc( p );  /* Load the GPG values direct from the registry. */
82          }  static void
83          p = get_gnupg_cfgfile( );  load_registry_values (HWND dlg)
84          if( p ) {  {
85              parse_gpg_options( p, &opt );      char *p;
86              free_if_alloc( p );  
87              if( opt && find_option( opt, "force-v3-sigs" ) )      p = get_reg_entry_gpg ("HomeDir");
88                  CheckDlgButton( dlg, IDC_GPGPREFS_V3SIGS, BST_CHECKED );      if (p) {
89              if( opt && (e=find_option( opt, "comment" )) )          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
90                  SetDlgItemText( dlg, IDC_GPGPREFS_COMMENT, e->val );          free_if_alloc (p);      
91              if( opt && (e=find_option( opt, "encrypt-to" )) )      }
92                  SetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, e->val );                p = get_reg_entry_gpg ("gpgProgram");
93          }      if (p) {
94          center_window( dlg );          SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, p);
95          SetForegroundWindow( dlg );          free_if_alloc (p);      
96          return TRUE;      }
97                p = get_reg_entry_mo ();
98      case WM_SYSCOMMAND:      if (p) {
99          if( LOWORD( wparam ) == SC_CLOSE )          SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, p);
100              EndDialog( dlg, TRUE );          free_if_alloc (p);        
101          return FALSE;      }
102                if (!item_get_text_length (dlg, IDC_GPGPREFS_HOMEDIR)) {
103      case WM_COMMAND:          p = multi_gnupg_path (0);
104          switch ( LOWORD( wparam ) ) {          if (p && dir_exist_check (p) == 0)
105          case IDC_GPGPREFS_SAVE:              SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, p);
106              if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) {          free_if_alloc (p);
107                  msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR );      }
108                  return FALSE;  }
109              }  
110              if( dir_exist_check( homedir ) ) {  
111                  _snprintf( t, sizeof t - 1, "%s: %s", homedir, winpt_strerror(WPTERR_DIR_OPEN) );  /* Dialog box procedure for the GPG preferences. */
112                  msg_box( dlg, t, _("Preferences"), MB_ERR );  BOOL CALLBACK
113                  return FALSE;  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
114              }  {
115                    static int gpg4win = false;
116              if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) {      char exedir[512];
117                  if ( file_exist_check( optfile ) ) {      char homedir[512];
118                      n = msg_box(dlg, _("Could not find GPG config file.\n"      char locale_dir[512];
119                                         "Do you want to create a config file?"),      char *p = NULL, t[256];
120                                  _("Preferences"), MB_INFO|MB_YESNO );      const char *s;
121                      if( n == IDNO ) {      const char *folder;
122                          SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" );      gpg_optfile_t opt = NULL;
123                          return FALSE;      gpg_option_t e;
124                      }      UINT n;
125                      else if( n == IDYES ) {      
126                          FILE * fp = fopen( optfile, "wb" );      switch (msg) {
127                          if( fp )      case WM_INITDIALOG:
128                              fclose( fp );          SetWindowText (dlg, _("GnuPG Preferences"));
129                          if( file_exist_check( optfile ) ) {          SetDlgItemText (dlg, IDC_GPGPREFS_HOMEINF,
130                              log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) );                         _("GnuPG home directory  (where both keyrings are located)"));
131                              return FALSE;          SetDlgItemText ( dlg, IDC_GPGPREFS_EXEINF,
132                          }                          _("GnuPG exe file location (full path with added gpg.exe)"));
133                          have_optfile = 1;          SetDlgItemText ( dlg, IDC_GPGPREFS_LOCALINF,
134                      }                          _("Locale directory (to access the translation files)"));
135                  }          SetDlgItemText (dlg, IDC_GPGPREFS_ASKLEVEL, _("Ask for the signature class during key sign"));
136                  else          SetDlgItemText (dlg, IDC_GPGPREFS_CMTINF, _("Comment in armored files"));
137                      have_optfile = 1;          SetDlgItemText (dlg, IDC_GPGPREFS_ENCINF, _("Encrypt to this key"));
138                  if( have_optfile ) {          SetDlgItemText (dlg, IDC_GPGPREFS_ALLOPTINF, _("General GPG options"));
139                      if( set_reg_entry_gpg( "OptFile", optfile ) ) {  
140                          msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR );          gpg4win = load_gpg4win_values (dlg);
141                          return FALSE;          if (!gpg4win)
142                      }              load_registry_values (dlg);
143                  }  
144              }          p = get_gnupg_cfgfile ();
145              else {          if (p) {
146                  char * t = get_gnupg_cfgfile ();              parse_gpg_options (p, &opt);
147                  if (t && !file_exist_check (t))              free_if_alloc (p);
148                      set_reg_entry_gpg( "OptFile", t );              if (opt) {
149                  free_if_alloc (t);                  if (find_option (opt, "ask-cert-level"))
150              }                      CheckDlgButton (dlg, IDC_GPGPREFS_ASKLEVEL, BST_CHECKED);
151              if ( set_reg_entry_gpg( "HomeDir", homedir ) ) {                                      if ((e=find_option (opt, "comment")))
152                  msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR );                  SetDlgItemText (dlg, IDC_GPGPREFS_COMMENT, e->val);
153                  return FALSE;                  if ((e=find_option (opt, "encrypt-to")))
154              }                      SetDlgItemText (dlg, IDC_GPGPREFS_ENCTO, e->val);
155              if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) {                  release_gpg_options (opt);
156                  msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR );              }
157                  return FALSE;          }
158              }  
159              if( file_exist_check( exedir ) ) {          center_window (dlg, NULL);
160                  msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR );          SetForegroundWindow (dlg);
161                  return FALSE;          return TRUE;
162              }  
163              if( set_reg_entry_gpg("gpgProgram", exedir ) ) {      case WM_DESTROY:
164                  msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR );          gpg4win = false;
165                  return FALSE;          break;
166              }  
167              if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) {      case WM_SYSCOMMAND:
168                  if (dir_exist_check (locale_dir)) {          if (LOWORD (wparam) == SC_CLOSE)
169                      log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN));              EndDialog (dlg, TRUE);
170                      return FALSE;          return FALSE;
171                  }          
172                  set_reg_entry_mo (locale_dir);      case WM_COMMAND:
173                  set_gettext_file ("winpt", locale_dir);          switch (LOWORD (wparam)) {
174              }          case IDC_GPGPREFS_SAVE:
175              else              if (!GetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR,
176                  set_reg_entry_mo ("");                                   homedir, sizeof (homedir) -1)) {
177                    msg_box (dlg, _("Please enter the GnuPG home directory."),
178              p = get_gnupg_cfgfile ();                           _("Preferences"), MB_ERR);
179              if( !p ) {                  return FALSE;
180                  msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR );              }
181                  EndDialog( dlg, FALSE );              if (dir_exist_check (homedir)) {
182              }                  _snprintf (t, sizeof (t) - 1, "%s: %s", homedir,
183              parse_gpg_options( p, &opt );                             winpt_strerror (WPTERR_DIR_OPEN));
184                    msg_box (dlg, t, _("Preferences"), MB_ERR);
185              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_V3SIGS ) )                  return FALSE;
186                  modify_entry( opt, ENTRY_SINGLE, "force-v3-sigs", NULL );              }
187              else              
188                  delete_option( opt, "force-v3-sigs" );              if (!gpg4win && set_reg_entry_gpg ("HomeDir", homedir)) {
189                    msg_box (dlg, _("Could not save 'HomeDir' in the registry."),
190              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))                           _("Preferences"), MB_ERR);
191                  modify_entry( opt, ENTRY_MULTI, "comment", t );                  return FALSE;
192              else if( n == 0 )              }
193                  modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );              if( !GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR,
194              else                                   exedir, sizeof (exedir) -1)) {
195                  delete_option( opt, "comment" );                  msg_box (dlg, _("Please enter where GPG.exe is located."),
196                             _("Preferences"), MB_ERR);
197              if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )                  return FALSE;
198                  modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );              }
199              else              if (file_exist_check (exedir)) {
200                  delete_option( opt, "encrypt-to" );                  msg_box (dlg, _("Could not find the GPG program in this directory."),
201                             _("Preferences"), MB_ERR);
202              commit_gpg_options( p, opt );                  return FALSE;
203              release_gpg_options( opt );              }
204              EndDialog( dlg, TRUE );              if (!gpg4win && set_reg_entry_gpg ("gpgProgram", exedir)) {
205              return TRUE;                  msg_box (dlg, _("Could not save 'gpgProgram' in the registry"),
206                                         _("Preferences"), MB_ERR);
207          case IDC_GPGPREFS_HOMEDLG:                  return FALSE;
208              const char * home;              }
209              home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);              if (GetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1)) {
210              if (home) {                  if (dir_exist_check (locale_dir)) {
211                  SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home);                      log_box ( _("Preferences"), MB_ERR, "%s: %s", locale_dir,
212                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)                               winpt_strerror (WPTERR_DIR_OPEN));
213                      break;                      return FALSE;
214                  char *name = make_filename (home, "gpg", "exe");                  }
215                  if (file_exist_check (name) == 0)                  if (!gpg4win)
216                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);                      set_reg_entry_mo (locale_dir);
217                  free_if_alloc (name);                  set_gettext_file ("winpt", locale_dir);
218                  name = make_filename (home, "gpg", "conf");              }
219                  if (file_exist_check (name) == 0)              else if (!gpg4win)
220                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);                  set_reg_entry_mo ("");
221                  free_if_alloc (name);  
222              }              p = get_gnupg_cfgfile ();
223              break;              if (!p) {
224                    msg_box (dlg, _("Could not get GPG config file"),
225          case IDC_GPGREFS_EXEDLG:                           _("Preferences"), MB_ERR);
226              const char * bin;                  EndDialog (dlg, FALSE);
227              bin = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG binary"), _("Executable Files (*.exe)\0*.exe\0\0"), NULL );              }
228              if( bin )              parse_gpg_options (p, &opt);
229                  SetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, bin );  
230              return TRUE;              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_ASKLEVEL ) ) {
231                                modify_entry( opt, ENTRY_SINGLE, "ask-cert-level", NULL );
232          case IDC_GPGPREFS_OPTDLG:                  reg_prefs.gpg.ask_cert_level = 1;
233              const char * opt;              }
234              opt = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG config file"), NULL, NULL );              else {
235              if( opt )                  delete_option (opt, "ask-cert-level");
236                  SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, opt );                  reg_prefs.gpg.ask_cert_level = 0;
237              return TRUE;              }
238          }  
239          break;              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))
240      }                  modify_entry( opt, ENTRY_MULTI, "comment", t );
241                    else if( n == 0 )
242      return FALSE;                  modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );
243  } /* gpgprefs_dlg_proc */              else
244                    delete_option( opt, "comment" );
245    
246                if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )
247                    modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );
248                else
249                    delete_option( opt, "encrypt-to" );
250    
251                commit_gpg_options (p, opt);
252                release_gpg_options (opt);
253                EndDialog (dlg, TRUE);
254                return TRUE;
255                
256            case IDC_GPGPREFS_HOMEDLG:
257                folder = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);
258                if (folder) {
259                    char *name;
260    
261                    SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, folder);
262                    if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)
263                        break;
264                    name = make_filename (folder, "gpg", "exe");
265                    if (file_exist_check (name) == 0)
266                        SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
267                    free_if_alloc (name);
268                    name = make_filename (folder, "gpg", "conf");
269                    if (file_exist_check (name) == 0)
270                        SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);
271                    free_if_alloc (name);
272                }
273                return TRUE;
274    
275            case IDC_GPGPREFS_LOCDLG:
276                folder = get_folder_dlg (dlg, _("Choose locale directory"), NULL);
277                if (folder)
278                    SetDlgItemText (dlg, IDC_GPGPREFS_LOCALE, folder);
279                return TRUE;
280                
281            case IDC_GPGREFS_EXEDLG:
282                s = get_fileopen_dlg (dlg, _("Choose GPG binary"),
283                                      _("Executable Files (*.exe)\0*.exe\0\0"),
284                                      NULL);
285                if (s)
286                    SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, s);
287                return TRUE;
288                
289            case IDCANCEL:
290                EndDialog (dlg, FALSE);
291                return TRUE;
292            }
293            break;
294        }
295        
296        return FALSE;
297    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26