Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.8 KiB

  1. // ImprtDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "keyring.h"
  5. #include "ImprtDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CImportDialog dialog
  13. CImportDialog::CImportDialog(CWnd* pParent /*=NULL*/)
  14. : CDialog(CImportDialog::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CImportDialog)
  17. m_cstring_CertFile = _T("");
  18. m_cstring_PrivateFile = _T("");
  19. //}}AFX_DATA_INIT
  20. }
  21. void CImportDialog::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CImportDialog)
  25. DDX_Control(pDX, IDC_PRIVATE_FILE, m_cedit_Private);
  26. DDX_Control(pDX, IDC_CERT_FILE, m_cedit_Cert);
  27. DDX_Text(pDX, IDC_CERT_FILE, m_cstring_CertFile);
  28. DDX_Text(pDX, IDC_PRIVATE_FILE, m_cstring_PrivateFile);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CImportDialog, CDialog)
  32. //{{AFX_MSG_MAP(CImportDialog)
  33. ON_BN_CLICKED(IDC_BROWSE_CERT, OnBrowseCert)
  34. ON_BN_CLICKED(IDC_BROWSE_PRIVATE, OnBrowsePrivate)
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CImportDialog message handlers
  39. //------------------------------------------------------------------------------
  40. void CImportDialog::OnBrowseCert()
  41. {
  42. UpdateData(TRUE);
  43. if ( FBrowseForAFile( m_cstring_CertFile, TRUE ) )
  44. UpdateData(FALSE);
  45. }
  46. //------------------------------------------------------------------------------
  47. void CImportDialog::OnBrowsePrivate()
  48. {
  49. UpdateData(TRUE);
  50. if ( FBrowseForAFile( m_cstring_PrivateFile, FALSE ) )
  51. UpdateData(FALSE);
  52. }
  53. // go browsing for a file
  54. //------------------------------------------------------------------------------
  55. BOOL CImportDialog::FBrowseForAFile( CString &szFile, BOOL fBrowseForCertificate )
  56. {
  57. CString szFilter;
  58. CString szTitle;
  59. CString szFileStart = szFile;
  60. WORD i = 0;
  61. LPSTR lpszBuffer;
  62. BOOL fAnswer = FALSE;
  63. // set up to look for either certs or private keys
  64. if ( fBrowseForCertificate )
  65. {
  66. szFilter.LoadString( IDS_CERTIFICATE_FILTER );
  67. szTitle.LoadString( IDS_OPEN_PUBLIC_KEY );
  68. }
  69. else
  70. {
  71. szFilter.LoadString( IDS_PRIVATE_FILE_TYPE );
  72. szTitle.LoadString( IDS_OPEN_PRIVATE_KEY );
  73. }
  74. // prep the dialog
  75. CFileDialog cfdlg(TRUE );
  76. // replace the "!" characters with nulls
  77. lpszBuffer = szFilter.GetBuffer(MAX_PATH+1);
  78. while( lpszBuffer[i] )
  79. {
  80. if ( lpszBuffer[i] == _T('!') )
  81. lpszBuffer[i] = _T('\0'); // yes, set \0 on purpose
  82. i++;
  83. }
  84. cfdlg.m_ofn.lpstrFilter = lpszBuffer;
  85. // finish prepping the title
  86. cfdlg.m_ofn.lpstrTitle = szTitle.GetBuffer(MAX_PATH+1);
  87. // finish prepping the starting location
  88. cfdlg.m_ofn.lpstrFile = szFileStart.GetBuffer(MAX_PATH+1);
  89. // run the dialog
  90. if ( cfdlg.DoModal() == IDOK )
  91. {
  92. fAnswer = TRUE;
  93. szFile = cfdlg.GetPathName();
  94. }
  95. // release the buffer in the filter string
  96. szFilter.ReleaseBuffer();
  97. szTitle.ReleaseBuffer();
  98. szFileStart.ReleaseBuffer();
  99. // return the answer
  100. return fAnswer;
  101. }
  102. //------------------------------------------------------------------------------
  103. void CImportDialog::OnOK()
  104. {
  105. UpdateData(TRUE);
  106. // make sure the user has chosen two valid files
  107. CFile cfile;
  108. // test the private key file
  109. if ( !cfile.Open( m_cstring_PrivateFile, CFile::modeRead|CFile::shareDenyNone ) )
  110. {
  111. // beep and select the bad field
  112. MessageBeep(0);
  113. m_cedit_Private.SetFocus();
  114. m_cedit_Private.SetSel(0xFFFF0000);
  115. return;
  116. }
  117. cfile.Close();
  118. // test the certificate file
  119. if ( !cfile.Open( m_cstring_CertFile, CFile::modeRead|CFile::shareDenyNone ) )
  120. {
  121. // beep and select the bad field
  122. MessageBeep(0);
  123. m_cedit_Cert.SetFocus();
  124. m_cedit_Cert.SetSel(0xFFFF0000);
  125. return;
  126. }
  127. cfile.Close();
  128. // all is ok. do the normal ok
  129. CDialog::OnOK();
  130. }