/[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 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
1  /* wptGPGPrefsDlg.cpp - WinPT GnuPG Preferences  /* wptGPGPrefsDlg.cpp - WinPT 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"  #include <windows.h>
27  #include "wptGPG.h"  
28  #include "wptRegistry.h"  #include "../resource.h"
29  #include "wptCommonCtl.h"  #include "wptNLS.h"
30  #include "wptContext.h" /* for passphrase_s */  #include "wptTypes.h"
31  #include "wptDlgs.h"  #include "wptErrors.h"
32  #include "wptW32API.h"  #include "wptGPG.h"
33  #include "wptVersion.h"  #include "wptRegistry.h"
34    #include "wptCommonCtl.h"
35  static struct {  #include "wptContext.h" /* for passphrase_s */
36      const char * name;  #include "wptDlgs.h"
37      int id;  #include "wptW32API.h"
38  } opts[] = {  #include "wptVersion.h"
39      {"HomeDir",    IDC_GPGPREFS_HOMEDIR},  
40      {"gpgProgram", IDC_GPGPREFS_EXEDIR},  static struct {
41      {"OptFile",    IDC_GPGPREFS_OPTFILE},      const char * name;
42      {0}      int id;
43  };  } opts[] = {
44        {"HomeDir",    IDC_GPGPREFS_HOMEDIR},
45        {"gpgProgram", IDC_GPGPREFS_EXEDIR},
46  BOOL CALLBACK      {"OptFile",    IDC_GPGPREFS_OPTFILE},
47  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      {0}
48  {  };
49      char exedir[1024], homedir[1024], optfile[1024];  
50      char locale_dir[1024];  
51      char * p = NULL, t[512];  BOOL CALLBACK
52      const char * s;  gpgprefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
53      int have_optfile = 0;  {
54      gpg_optfile_t opt = NULL;      char exedir[1024], homedir[1024], optfile[1024];
55      gpg_option_t e;      char locale_dir[1024];
56      UINT n;      char * p = NULL, t[512];
57            const char * s;
58      switch ( msg ) {      int have_optfile = 0;
59      case WM_INITDIALOG:      gpg_optfile_t opt = NULL;
60  #ifndef LANG_DE      gpg_option_t e;
61          SetWindowText( dlg, _("GnuPG Preferences") );      UINT n;
62          SetDlgItemText( dlg, IDC_GPGPREFS_HOMEINF,      
63                         _("GnuPG home directory  (where both keyrings are located)") );      switch ( msg ) {
64          SetDlgItemText( dlg, IDC_GPGPREFS_OPTINF,      case WM_INITDIALOG:
65                         _("GnuPG config file (default: use gpg.conf)") );  #ifndef LANG_DE
66          SetDlgItemText( dlg, IDC_GPGPREFS_EXEINF,          SetWindowText( dlg, _("GnuPG Preferences") );
67                         _("GnuPG exe file location (full path with added gpg.exe)") );          SetDlgItemText( dlg, IDC_GPGPREFS_HOMEINF,
68          SetDlgItemText( dlg, IDC_GPGPREFS_LOCALINF,                         _("GnuPG home directory  (where both keyrings are located)") );
69                         _("Locale directory (to access the translation files)") );          SetDlgItemText( dlg, IDC_GPGPREFS_OPTINF,
70  #endif                         _("GnuPG config file (default: use gpg.conf)") );
71          for( n=0; (s = opts[n].name); n++ ) {          SetDlgItemText( dlg, IDC_GPGPREFS_EXEINF,
72              p = get_reg_entry_gpg( s );                         _("GnuPG exe file location (full path with added gpg.exe)") );
73              if( p ) {          SetDlgItemText( dlg, IDC_GPGPREFS_LOCALINF,
74                  SetDlgItemText( dlg, opts[n].id, p );                         _("Locale directory (to access the translation files)") );
75                  free_if_alloc( p );  #endif
76              }          for( n=0; (s = opts[n].name); n++ ) {
77          }              p = get_reg_entry_gpg( s );
78                        if( p ) {
79          if( (p = get_reg_entry_mo( )) ) {                  SetDlgItemText( dlg, opts[n].id, p );
80              SetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, p );                  free_if_alloc( p );
81              free_if_alloc( p );              }
82          }          }
83          p = get_gnupg_cfgfile( );          
84          if( p ) {          if( (p = get_reg_entry_mo( )) ) {
85              parse_gpg_options( p, &opt );              SetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, p );
86              free_if_alloc( p );              free_if_alloc( p );
87              if( opt && find_option( opt, "force-v3-sigs" ) )          }
88                  CheckDlgButton( dlg, IDC_GPGPREFS_V3SIGS, BST_CHECKED );          p = get_gnupg_cfgfile( );
89              if( opt && (e=find_option( opt, "comment" )) )          if( p ) {
90                  SetDlgItemText( dlg, IDC_GPGPREFS_COMMENT, e->val );              parse_gpg_options( p, &opt );
91              if( opt && (e=find_option( opt, "encrypt-to" )) )              free_if_alloc( p );
92                  SetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, e->val );                        if( opt && find_option( opt, "force-v3-sigs" ) )
93          }                  CheckDlgButton( dlg, IDC_GPGPREFS_V3SIGS, BST_CHECKED );
94          center_window( dlg, NULL );              if( opt && (e=find_option( opt, "comment" )) )
95          SetForegroundWindow( dlg );                  SetDlgItemText( dlg, IDC_GPGPREFS_COMMENT, e->val );
96          return TRUE;              if( opt && (e=find_option( opt, "encrypt-to" )) )
97                            SetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, e->val );          
98      case WM_SYSCOMMAND:          }
99          if( LOWORD( wparam ) == SC_CLOSE )          center_window( dlg, NULL );
100              EndDialog( dlg, TRUE );          SetForegroundWindow( dlg );
101          return FALSE;          return TRUE;
102                    
103      case WM_COMMAND:      case WM_SYSCOMMAND:
104          switch ( LOWORD( wparam ) ) {          if( LOWORD( wparam ) == SC_CLOSE )
105          case IDC_GPGPREFS_SAVE:              EndDialog( dlg, TRUE );
106              if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) {          return FALSE;
107                  msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR );          
108                  return FALSE;      case WM_COMMAND:
109              }          switch ( LOWORD( wparam ) ) {
110              if( dir_exist_check( homedir ) ) {          case IDC_GPGPREFS_SAVE:
111                  _snprintf( t, sizeof t - 1, "%s: %s", homedir, winpt_strerror(WPTERR_DIR_OPEN) );              if( !GetDlgItemText( dlg, IDC_GPGPREFS_HOMEDIR, homedir, sizeof homedir -1 ) ) {
112                  msg_box( dlg, t, _("Preferences"), MB_ERR );                  msg_box( dlg, _("Please enter the GnuPG home directory."), _("Preferences"), MB_ERR );
113                  return FALSE;                  return FALSE;
114              }              }
115                            if( dir_exist_check( homedir ) ) {
116              if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) {                  _snprintf( t, sizeof t - 1, "%s: %s", homedir, winpt_strerror(WPTERR_DIR_OPEN) );
117                  if ( file_exist_check( optfile ) ) {                  msg_box( dlg, t, _("Preferences"), MB_ERR );
118                      n = msg_box(dlg, _("Could not find GPG config file.\n"                  return FALSE;
119                                         "Do you want to create a config file?"),              }
120                                  _("Preferences"), MB_INFO|MB_YESNO );              
121                      if( n == IDNO ) {              if( GetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, optfile, sizeof optfile - 1 ) ) {
122                          SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" );                  if ( file_exist_check( optfile ) ) {
123                          return FALSE;                      n = msg_box(dlg, _("Could not find GPG config file.\n"
124                      }                                         "Do you want to create a config file?"),
125                      else if( n == IDYES ) {                                  _("Preferences"), MB_INFO|MB_YESNO );
126                          FILE * fp = fopen( optfile, "wb" );                      if( n == IDNO ) {
127                          if( fp )                          SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, "" );
128                              fclose( fp );                          return FALSE;
129                          if( file_exist_check( optfile ) ) {                      }
130                              log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) );                      else if( n == IDYES ) {
131                              return FALSE;                          FILE * fp = fopen( optfile, "wb" );
132                          }                          if( fp )
133                          have_optfile = 1;                              fclose( fp );
134                      }                          if( file_exist_check( optfile ) ) {
135                  }                              log_box( _("Preferences"), MB_ERR, "%s: %s", optfile, winpt_strerror( WPTERR_FILE_CREAT ) );
136                  else                              return FALSE;
137                      have_optfile = 1;                          }
138                  if( have_optfile ) {                          have_optfile = 1;
139                      if( set_reg_entry_gpg( "OptFile", optfile ) ) {                      }
140                          msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR );                  }
141                          return FALSE;                  else
142                      }                      have_optfile = 1;
143                  }                  if( have_optfile ) {
144              }                      if( set_reg_entry_gpg( "OptFile", optfile ) ) {
145              else {                          msg_box( dlg, _("Could not save 'OptFile' in the registry."), _("Preferences"), MB_ERR );
146                  char * t = get_gnupg_cfgfile ();                          return FALSE;
147                  if (t && !file_exist_check (t))                      }
148                      set_reg_entry_gpg( "OptFile", t );                  }
149                  free_if_alloc (t);              }
150              }              else {
151              if ( set_reg_entry_gpg( "HomeDir", homedir ) ) {                                      char * t = get_gnupg_cfgfile ();
152                  msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR );                  if (t && !file_exist_check (t))
153                  return FALSE;                      set_reg_entry_gpg( "OptFile", t );
154              }                  free_if_alloc (t);
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 );              if ( set_reg_entry_gpg( "HomeDir", homedir ) ) {                    
157                  return FALSE;                  msg_box( dlg, _("Could not save 'HomeDir' in the registry."), _("Preferences"), MB_ERR );
158              }                  return FALSE;
159              if( file_exist_check( exedir ) ) {              }
160                  msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR );              if( !GetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, exedir, sizeof exedir -1 ) ) {
161                  return FALSE;                  msg_box( dlg, _("Please enter where GPG.exe is located."), _("Preferences"), MB_ERR );
162              }                  return FALSE;
163              if( set_reg_entry_gpg("gpgProgram", exedir ) ) {              }
164                  msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR );              if( file_exist_check( exedir ) ) {
165                  return FALSE;                  msg_box( dlg, _("Could not find the GPG program in this directory."), _("Preferences"), MB_ERR );
166              }                  return FALSE;
167              if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) {              }
168                  if (dir_exist_check (locale_dir)) {              if( set_reg_entry_gpg("gpgProgram", exedir ) ) {
169                      log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN));                  msg_box( dlg, _("Could not save 'gpgProgram' in the registry"), _("Preferences"), MB_ERR );
170                      return FALSE;                  return FALSE;
171                  }              }
172                  set_reg_entry_mo (locale_dir);              if( GetDlgItemText( dlg, IDC_GPGPREFS_LOCALE, locale_dir, sizeof (locale_dir) -1 ) ) {
173                  set_gettext_file ("winpt", locale_dir);                  if (dir_exist_check (locale_dir)) {
174              }                      log_box( _("Preferences"), MB_ERR, "%s: %s", locale_dir, winpt_strerror (WPTERR_DIR_OPEN));
175              else                      return FALSE;
176                  set_reg_entry_mo ("");                  }
177                    set_reg_entry_mo (locale_dir);
178              p = get_gnupg_cfgfile ();                  set_gettext_file ("winpt", locale_dir);
179              if( !p ) {              }
180                  msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR );              else
181                  EndDialog( dlg, FALSE );                  set_reg_entry_mo ("");
182              }  
183              parse_gpg_options( p, &opt );              p = get_gnupg_cfgfile ();
184                if( !p ) {
185              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_V3SIGS ) )                  msg_box( dlg, _("Could not get GPG config file"), _("Preferences"), MB_ERR );
186                  modify_entry( opt, ENTRY_SINGLE, "force-v3-sigs", NULL );                  EndDialog( dlg, FALSE );
187              else              }
188                  delete_option( opt, "force-v3-sigs" );              parse_gpg_options( p, &opt );
189    
190              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))              if( IsDlgButtonChecked( dlg, IDC_GPGPREFS_V3SIGS ) )
191                  modify_entry( opt, ENTRY_MULTI, "comment", t );                  modify_entry( opt, ENTRY_SINGLE, "force-v3-sigs", NULL );
192              else if( n == 0 )              else
193                  modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );                  delete_option( opt, "force-v3-sigs" );
194              else  
195                  delete_option( opt, "comment" );              if( (n = GetDlgItemText(dlg, IDC_GPGPREFS_COMMENT, t, sizeof t - 1 ) ))
196                    modify_entry( opt, ENTRY_MULTI, "comment", t );
197              if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )              else if( n == 0 )
198                  modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );                  modify_entry( opt, ENTRY_MULTI, "comment", "\"\"" );
199              else              else
200                  delete_option( opt, "encrypt-to" );                  delete_option( opt, "comment" );
201    
202              commit_gpg_options( p, opt );              if( (n=GetDlgItemText( dlg, IDC_GPGPREFS_ENCTO, t, sizeof t -1 ) ) )
203              release_gpg_options( opt );                  modify_entry( opt, ENTRY_MULTI, "encrypt-to", t );
204              EndDialog( dlg, TRUE );              else
205              return TRUE;                  delete_option( opt, "encrypt-to" );
206                
207          case IDC_GPGPREFS_HOMEDLG:              commit_gpg_options( p, opt );
208              const char * home;              release_gpg_options( opt );
209              home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);              EndDialog( dlg, TRUE );
210              if (home) {              return TRUE;
211                  SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home);              
212                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)          case IDC_GPGPREFS_HOMEDLG:
213                      break;              const char * home;
214                  char *name = make_filename (home, "gpg", "exe");              home = get_folder_dlg (dlg, _("Choose GPG home directory"), NULL);
215                  if (file_exist_check (name) == 0)              if (home) {
216                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);                  SetDlgItemText (dlg, IDC_GPGPREFS_HOMEDIR, home);
217                  free_if_alloc (name);                  if (GetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, exedir, DIM (exedir)-1) > 0)
218                  name = make_filename (home, "gpg", "conf");                      break;
219                  if (file_exist_check (name) == 0)                  char *name = make_filename (home, "gpg", "exe");
220                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);                  if (file_exist_check (name) == 0)
221                  free_if_alloc (name);                      SetDlgItemText (dlg, IDC_GPGPREFS_EXEDIR, name);
222              }                  free_if_alloc (name);
223              break;                  name = make_filename (home, "gpg", "conf");
224                    if (file_exist_check (name) == 0)
225          case IDC_GPGREFS_EXEDLG:                      SetDlgItemText (dlg, IDC_GPGPREFS_OPTFILE, name);
226              const char * bin;                  free_if_alloc (name);
227              bin = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG binary"), _("Executable Files (*.exe)\0*.exe\0\0"), NULL );              }
228              if( bin )              break;
229                  SetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, bin );  
230              return TRUE;          case IDC_GPGREFS_EXEDLG:
231                            const char * bin;
232          case IDC_GPGPREFS_OPTDLG:              bin = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG binary"), _("Executable Files (*.exe)\0*.exe\0\0"), NULL );
233              const char * opt;              if( bin )
234              opt = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG config file"), NULL, NULL );                  SetDlgItemText( dlg, IDC_GPGPREFS_EXEDIR, bin );
235              if( opt )              return TRUE;
236                  SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, opt );              
237              return TRUE;          case IDC_GPGPREFS_OPTDLG:
238          }              const char * opt;
239          break;              opt = get_filename_dlg( dlg, FILE_OPEN, _("Choose GPG config file"), NULL, NULL );
240      }              if( opt )
241                        SetDlgItemText( dlg, IDC_GPGPREFS_OPTFILE, opt );
242      return FALSE;              return TRUE;
243  } /* gpgprefs_dlg_proc */          }
244            break;
245        }
246        
247        return FALSE;
248    } /* gpgprefs_dlg_proc */

Legend:
Removed from v.23  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26