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.

337 lines
9.7 KiB

  1. // NKDN2.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "keyring.h"
  5. #include "NKChseCA.h"
  6. #include "NKDN.h"
  7. #include "NKDN2.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define SZ_CCODES_FILE _T("ccodes.txt")
  14. #define SZ_CCODES_SECTION _T("CountryCodes")
  15. #define REGKEY_STP _T("SOFTWARE\\Microsoft\\INetStp")
  16. #define REGKEY_INSTALLKEY _T("InstallPath")
  17. enum {
  18. WM_INTERNAL_SETCOUNTRYCODE = WM_USER
  19. };
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CNKDistinguisedName2 dialog
  22. CNKDistinguisedName2::CNKDistinguisedName2(CWnd* pParent /*=NULL*/)
  23. : CNKPages(CNKDistinguisedName2::IDD)
  24. {
  25. //{{AFX_DATA_INIT(CNKDistinguisedName2)
  26. m_nkdn2_sz_L = _T("");
  27. m_nkdn2_sz_S = _T("");
  28. m_nkdn2_sz_C = _T("");
  29. //}}AFX_DATA_INIT
  30. m_hotlink_codessite.m_fBrowse = TRUE;
  31. }
  32. void CNKDistinguisedName2::DoDataExchange(CDataExchange* pDX)
  33. {
  34. CDialog::DoDataExchange(pDX);
  35. //{{AFX_DATA_MAP(CNKDistinguisedName2)
  36. DDX_Control(pDX, IDC_HOTLINK_CCODES, m_hotlink_codessite);
  37. DDX_Control(pDX, IDC_NEWKEY_COUNTRY, m_control_C);
  38. DDX_Control(pDX, IDC_NEWKEY_STATE, m_control_S);
  39. DDX_Control(pDX, IDC_NEWKEY_LOCALITY, m_control_L);
  40. DDX_Text(pDX, IDC_NEWKEY_LOCALITY, m_nkdn2_sz_L);
  41. DDV_MaxChars(pDX, m_nkdn2_sz_L, 128);
  42. DDX_Text(pDX, IDC_NEWKEY_STATE, m_nkdn2_sz_S);
  43. DDV_MaxChars(pDX, m_nkdn2_sz_S, 128);
  44. DDX_CBString(pDX, IDC_NEWKEY_COUNTRY, m_nkdn2_sz_C);
  45. DDV_MaxChars(pDX, m_nkdn2_sz_C, 2);
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CNKDistinguisedName2, CDialog)
  49. //{{AFX_MSG_MAP(CNKDistinguisedName2)
  50. ON_EN_CHANGE(IDC_NEWKEY_COUNTRY, OnChangeNewkeyCountry)
  51. ON_EN_CHANGE(IDC_NEWKEY_LOCALITY, OnChangeNewkeyLocality)
  52. ON_EN_CHANGE(IDC_NEWKEY_STATE, OnChangeNewkeyState)
  53. ON_CBN_CLOSEUP(IDC_NEWKEY_COUNTRY, OnCloseupNewkeyCountry)
  54. ON_CBN_SELCHANGE(IDC_NEWKEY_COUNTRY, OnSelchangeNewkeyCountry)
  55. //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57. #define SZ_DN_C "DN_COUNTRY"
  58. #define SZ_DN_L "DN_LOCALITY"
  59. #define SZ_DN_S "DN_STATE"
  60. //----------------------------------------------------------------
  61. void CNKDistinguisedName2::OnFinish()
  62. {
  63. m_nkdn2_sz_C.MakeUpper();
  64. // store the user entries
  65. SetStoredString( m_nkdn2_sz_C, SZ_DN_C );
  66. SetStoredString( m_nkdn2_sz_L, SZ_DN_L );
  67. SetStoredString( m_nkdn2_sz_S, SZ_DN_S );
  68. }
  69. //----------------------------------------------------------------
  70. BOOL CNKDistinguisedName2::OnInitDialog()
  71. {
  72. // if the entries from last time are available, use them
  73. try
  74. {
  75. FGetStoredString( m_nkdn2_sz_L, SZ_DN_L );
  76. FGetStoredString( m_nkdn2_sz_S, SZ_DN_S );
  77. }
  78. catch( CException e )
  79. {
  80. }
  81. // initialize the edit field part with the ISO code returned by GetLocalInfo
  82. GetLocaleInfo(
  83. LOCALE_SYSTEM_DEFAULT, // locale identifier
  84. LOCALE_SABBREVCTRYNAME, // type of information
  85. m_nkdn2_sz_C.GetBuffer(4), // address of buffer for information
  86. 2 // size of buffer
  87. );
  88. m_nkdn2_sz_C.ReleaseBuffer(2);
  89. // call superclass
  90. CPropertyPage::OnInitDialog();
  91. // fill in the country code drop-down list
  92. InitCountryCodeDropDown();
  93. // return 0 to say we set the default item
  94. // return 1 to just select the default default item
  95. return 1;
  96. }
  97. //----------------------------------------------------------------
  98. BOOL CNKDistinguisedName2::OnSetActive()
  99. {
  100. ActivateButtons();
  101. return CPropertyPage::OnSetActive();
  102. }
  103. //----------------------------------------------------------------
  104. void CNKDistinguisedName2::ActivateButtons()
  105. {
  106. DWORD flags = PSWIZB_BACK;
  107. BOOL fFinish = FALSE;
  108. BOOL fCanGoOn = TRUE;
  109. // first, see if this is the end of the road by checing the chooseca page
  110. if ( m_pChooseCAPage->m_nkca_radio == 1 )
  111. fFinish = TRUE;
  112. UpdateData(TRUE);
  113. //now make sure there is something in each of the required fields
  114. fCanGoOn &= !m_nkdn2_sz_C.IsEmpty();
  115. fCanGoOn &= !m_nkdn2_sz_S.IsEmpty();
  116. fCanGoOn &= !m_nkdn2_sz_L.IsEmpty();
  117. // if we can go on, hilite the button
  118. if ( fCanGoOn )
  119. {
  120. if ( fFinish )
  121. flags |= PSWIZB_FINISH;
  122. else
  123. flags |= PSWIZB_NEXT;
  124. }
  125. else
  126. // cannot go on
  127. {
  128. if ( fFinish )
  129. flags |= PSWIZB_DISABLEDFINISH;
  130. }
  131. // update the property sheet buttons
  132. m_pPropSheet->SetWizardButtons( flags );
  133. }
  134. //----------------------------------------------------------------
  135. void CNKDistinguisedName2::GetCCodePath( CString &sz )
  136. {
  137. HKEY hKey;
  138. TCHAR chPath[MAX_PATH+1];
  139. DWORD cbPath;
  140. DWORD err, type;
  141. // get the server install path from the registry
  142. // open the registry key, if it exists
  143. err = RegOpenKeyEx(
  144. HKEY_LOCAL_MACHINE, // handle of open key
  145. REGKEY_STP, // address of name of subkey to open
  146. 0, // reserved
  147. KEY_READ, // security access mask
  148. &hKey // address of handle of open key
  149. );
  150. // if we did not open the key for any reason (say... it doesn't exist)
  151. // then leave right away
  152. if ( err != ERROR_SUCCESS )
  153. return;
  154. cbPath = sizeof(chPath);
  155. type = REG_SZ;
  156. err = RegQueryValueEx(
  157. hKey, // handle of key to query
  158. REGKEY_INSTALLKEY, // address of name of value to query
  159. NULL, // reserved
  160. &type, // address of buffer for value type
  161. (PUCHAR)chPath, // address of data buffer
  162. &cbPath // address of data buffer size
  163. );
  164. // close the key
  165. RegCloseKey( hKey );
  166. // if we did get the key for any reason (say... it doesn't exist)
  167. // then leave right away
  168. if ( err != ERROR_SUCCESS )
  169. return;
  170. // put it all together
  171. sz = chPath;
  172. sz += _T('\\');
  173. sz += SZ_CCODES_FILE;
  174. }
  175. //----------------------------------------------------------------
  176. void CNKDistinguisedName2::InitCountryCodeDropDown()
  177. {
  178. WIN32_FILE_ATTRIBUTE_DATA dataFile;
  179. CString szCCodesFile;
  180. PTCHAR pszCode;
  181. // get the location of the codes file
  182. GetCCodePath( szCCodesFile );
  183. // make sure the file exists and get info on it at the same time
  184. if ( !GetFileAttributesEx(szCCodesFile, GetFileExInfoStandard, &dataFile) )
  185. return;
  186. // Allocate a buffer to recieve the data based on the size of the file
  187. PTCHAR pBuff = (PTCHAR)GlobalAlloc( GPTR, dataFile.nFileSizeLow * 2 );
  188. DWORD cch = GetPrivateProfileSection(
  189. SZ_CCODES_SECTION, // address of section name
  190. pBuff, // address of return buffer
  191. dataFile.nFileSizeLow, // size of return buffer
  192. szCCodesFile // address of initialization filename
  193. );
  194. pszCode = pBuff;
  195. // loop through the items, adding each
  196. while ( *pszCode != 0 )
  197. {
  198. // add the country code
  199. InitOneCountryCode( pszCode );
  200. // increment the list
  201. pszCode = _tcsninc( pszCode, _tcslen(pszCode)+1 );
  202. }
  203. GlobalFree( pBuff );
  204. }
  205. //----------------------------------------------------------------
  206. void CNKDistinguisedName2::InitOneCountryCode( LPCTSTR pszCode )
  207. {
  208. CString szData = pszCode;
  209. CString szCountry;
  210. INT iCode;
  211. // get the location of the equals character - it MUST be the third character
  212. if ( szData.Find(_T('=')) != 2 )
  213. return;
  214. szCountry = szData.Right( szData.GetLength() - 3 );
  215. // add the code to the cstring list and save its index position
  216. iCode = m_rgbszCodes.Add( szData.Left(2) );
  217. // add the item to the combo box
  218. int iPos = m_control_C.AddString( szCountry );
  219. // attach the index into the string array
  220. m_control_C.SetItemData( iPos, iCode );
  221. }
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CNKDistinguisedName2 message handlers
  224. //----------------------------------------------------------------
  225. void CNKDistinguisedName2::OnChangeNewkeyCountry()
  226. {
  227. ActivateButtons();
  228. }
  229. //----------------------------------------------------------------
  230. void CNKDistinguisedName2::OnChangeNewkeyLocality()
  231. {
  232. ActivateButtons();
  233. }
  234. //----------------------------------------------------------------
  235. void CNKDistinguisedName2::OnChangeNewkeyState()
  236. {
  237. ActivateButtons();
  238. }
  239. //----------------------------------------------------------------
  240. void CNKDistinguisedName2::OnCloseupNewkeyCountry()
  241. {
  242. // if there is no current selected item, do nothing
  243. if ( m_control_C.GetCurSel() == -1 )
  244. return;
  245. // get the selection's hidden dword
  246. ULONG_PTR iCode = m_control_C.GetItemData( m_control_C.GetCurSel() );
  247. m_control_C.SetCurSel(-1);
  248. // for some reason, attempting to set the string directly here isn't working.
  249. PostMessage( WM_INTERNAL_SETCOUNTRYCODE, 0, iCode );
  250. }
  251. //----------------------------------------------------------------
  252. void CNKDistinguisedName2::OnSelchangeNewkeyCountry()
  253. {
  254. // if there is no current selected item, do nothing
  255. if ( m_control_C.GetCurSel() == -1 )
  256. return;
  257. // get the selection's hidden dword
  258. ULONG_PTR iCode = m_control_C.GetItemData( m_control_C.GetCurSel() );
  259. m_control_C.SetCurSel(-1);
  260. // for some reason, attempting to set the string directly here isn't working.
  261. PostMessage( WM_INTERNAL_SETCOUNTRYCODE, 0, iCode );
  262. }
  263. //----------------------------------------------------------------
  264. LRESULT CNKDistinguisedName2::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  265. {
  266. if ( message == WM_INTERNAL_SETCOUNTRYCODE )
  267. {
  268. INT iCode = (INT)lParam;
  269. // set the text of the box
  270. m_control_C.SetWindowText( m_rgbszCodes.GetAt(iCode) );
  271. }
  272. return CDialog::WindowProc(message, wParam, lParam);
  273. }