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.

353 lines
11 KiB

  1. /////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: TemplateV2SupercedesPropertyPage.cpp
  7. //
  8. // Contents: Implementation of CTemplateV2SupercedesPropertyPage
  9. //
  10. //----------------------------------------------------------------------------
  11. // TemplateV2SupercedesPropertyPage.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "certtmpl.h"
  15. #include "TemplateV2SupercedesPropertyPage.h"
  16. #include "CompData.h"
  17. #include "SelectTemplateDlg.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CTemplateV2SupercedesPropertyPage property page
  25. CTemplateV2SupercedesPropertyPage::CTemplateV2SupercedesPropertyPage(
  26. CCertTemplate& rCertTemplate,
  27. bool& rbIsDirty,
  28. const CCertTmplComponentData* pCompData)
  29. : CHelpPropertyPage(CTemplateV2SupercedesPropertyPage::IDD),
  30. m_rCertTemplate (rCertTemplate),
  31. m_pGlobalTemplateNameList (0),
  32. m_bGlobalListCreatedByDialog (false),
  33. m_rbIsDirty (rbIsDirty),
  34. m_pCompData (pCompData)
  35. {
  36. //{{AFX_DATA_INIT(CTemplateV2SupercedesPropertyPage)
  37. //}}AFX_DATA_INIT
  38. m_rCertTemplate.AddRef ();
  39. if ( m_pCompData )
  40. m_pGlobalTemplateNameList = &(m_pCompData->m_globalTemplateNameList);
  41. if ( !m_pGlobalTemplateNameList )
  42. {
  43. m_bGlobalListCreatedByDialog = true;
  44. m_pGlobalTemplateNameList = new CStringList;
  45. if ( m_pGlobalTemplateNameList )
  46. {
  47. }
  48. }
  49. }
  50. CTemplateV2SupercedesPropertyPage::~CTemplateV2SupercedesPropertyPage()
  51. {
  52. m_rCertTemplate.Release ();
  53. if ( m_bGlobalListCreatedByDialog )
  54. delete m_pGlobalTemplateNameList;
  55. }
  56. void CTemplateV2SupercedesPropertyPage::DoDataExchange(CDataExchange* pDX)
  57. {
  58. CHelpPropertyPage::DoDataExchange(pDX);
  59. //{{AFX_DATA_MAP(CTemplateV2SupercedesPropertyPage)
  60. DDX_Control(pDX, IDC_SUPERCEDED_TEMPLATES_LIST, m_templateList);
  61. //}}AFX_DATA_MAP
  62. }
  63. BEGIN_MESSAGE_MAP(CTemplateV2SupercedesPropertyPage, CHelpPropertyPage)
  64. //{{AFX_MSG_MAP(CTemplateV2SupercedesPropertyPage)
  65. ON_BN_CLICKED(IDC_ADD_SUPERCEDED_TEMPLATE, OnAddSupercededTemplate)
  66. ON_BN_CLICKED(IDC_REMOVE_SUPERCEDED_TEMPLATE, OnRemoveSupercededTemplate)
  67. ON_NOTIFY(LVN_DELETEITEM, IDC_SUPERCEDED_TEMPLATES_LIST, OnDeleteitemSupercededTemplatesList)
  68. ON_NOTIFY(LVN_ITEMCHANGED, IDC_SUPERCEDED_TEMPLATES_LIST, OnItemchangedSupercededTemplatesList)
  69. ON_WM_DESTROY()
  70. //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CTemplateV2SupercedesPropertyPage message handlers
  74. void CTemplateV2SupercedesPropertyPage::OnAddSupercededTemplate()
  75. {
  76. CStringList supercededTemplateNames;
  77. // Add all the currently superceded templates. These will not be displayed
  78. // in the popup dialog
  79. int nCnt = m_templateList.GetItemCount ();
  80. for (int nIndex = 0; nIndex < nCnt; nIndex++)
  81. {
  82. CString* pszTemplateName = (CString*) m_templateList.GetItemData (nIndex);
  83. if ( pszTemplateName )
  84. supercededTemplateNames.AddTail (*pszTemplateName);
  85. }
  86. // Also add this template name. Templates can't supercede themselves.
  87. supercededTemplateNames.AddTail (m_rCertTemplate.GetTemplateName ());
  88. if ( m_pGlobalTemplateNameList )
  89. {
  90. CSelectTemplateDlg dlg (this, m_pCompData,
  91. supercededTemplateNames);
  92. CThemeContextActivator activator;
  93. if ( IDOK == dlg.DoModal () )
  94. {
  95. POSITION pos = dlg.m_returnedTemplates.GetHeadPosition ();
  96. CString szTemplateName;
  97. for (; pos; )
  98. {
  99. szTemplateName = dlg.m_returnedTemplates.GetNext (pos);
  100. HRESULT hr = m_rCertTemplate.ModifySupercededTemplateList (
  101. szTemplateName, true);
  102. if ( SUCCEEDED (hr) )
  103. {
  104. hr = AddItem (szTemplateName);
  105. SetModified ();
  106. m_rbIsDirty = true;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. void CTemplateV2SupercedesPropertyPage::OnRemoveSupercededTemplate()
  113. {
  114. int nCnt = m_templateList.GetItemCount ();
  115. ASSERT (nCnt >= 1);
  116. UINT flag = 0;
  117. while (--nCnt >= 0)
  118. {
  119. flag = ListView_GetItemState (m_templateList.m_hWnd, nCnt, LVIS_SELECTED);
  120. if ( flag & LVNI_SELECTED )
  121. {
  122. CString* pszTemplateName = (CString*) m_templateList.GetItemData (nCnt);
  123. if ( pszTemplateName )
  124. {
  125. HRESULT hr = m_rCertTemplate.ModifySupercededTemplateList (
  126. *pszTemplateName, false);
  127. if ( SUCCEEDED (hr) )
  128. {
  129. m_templateList.DeleteItem (nCnt);
  130. SetModified ();
  131. m_rbIsDirty = true;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. void CTemplateV2SupercedesPropertyPage::EnableControls()
  138. {
  139. if ( m_rCertTemplate.ReadOnly () )
  140. {
  141. GetDlgItem (IDC_SUPERCEDED_TEMPLATES_LIST)->EnableWindow (FALSE);
  142. GetDlgItem (IDC_REMOVE_SUPERCEDED_TEMPLATE)->EnableWindow (FALSE);
  143. GetDlgItem (IDC_ADD_SUPERCEDED_TEMPLATE)->EnableWindow (FALSE);
  144. }
  145. else
  146. {
  147. BOOL bEnable = (m_templateList.GetSelectedCount () > 0);
  148. GetDlgItem(IDC_REMOVE_SUPERCEDED_TEMPLATE)->EnableWindow (bEnable);
  149. }
  150. }
  151. BOOL CTemplateV2SupercedesPropertyPage::OnInitDialog()
  152. {
  153. CHelpPropertyPage::OnInitDialog ();
  154. // Set up list controls
  155. COLORREF cr = RGB (255, 0, 255);
  156. CThemeContextActivator activator;
  157. VERIFY (m_imageListNormal.Create (IDB_TEMPLATES, 32, 0, cr));
  158. VERIFY (m_imageListSmall.Create (IDB_TEMPLATES, 16, 0, cr));
  159. m_templateList.SetImageList (CImageList::FromHandle (m_imageListSmall), LVSIL_SMALL);
  160. m_templateList.SetImageList (CImageList::FromHandle (m_imageListNormal), LVSIL_NORMAL);
  161. // Set to full-row select
  162. DWORD dwExstyle = m_templateList.GetExtendedStyle ();
  163. m_templateList.SetExtendedStyle (dwExstyle | LVS_EX_FULLROWSELECT);
  164. int colWidths[NUM_COLS] = {200, 200};
  165. // Add "Certificate Extension" column
  166. CString szText;
  167. VERIFY (szText.LoadString (IDS_CERTIFICATE_TEMPLATES));
  168. VERIFY (m_templateList.InsertColumn (COL_CERT_TEMPLATE, (LPCWSTR) szText,
  169. LVCFMT_LEFT, colWidths[COL_CERT_TEMPLATE], COL_CERT_TEMPLATE) != -1);
  170. VERIFY (szText.LoadString (IDS_COLUMN_SUPPORTED_CAS));
  171. VERIFY (m_templateList.InsertColumn (COL_CERT_VERSION, (LPCWSTR) szText,
  172. LVCFMT_LEFT, colWidths[COL_CERT_VERSION], COL_CERT_VERSION) != -1);
  173. m_templateList.SetColumnWidth (COL_CERT_VERSION, LVSCW_AUTOSIZE_USEHEADER);
  174. // Initialize superceded list
  175. int nTemplateIndex = 0;
  176. CString szTemplateName;
  177. while ( SUCCEEDED ( m_rCertTemplate.GetSupercededTemplate (nTemplateIndex,
  178. szTemplateName)) )
  179. {
  180. VERIFY (SUCCEEDED (AddItem (szTemplateName)));
  181. nTemplateIndex++;
  182. }
  183. EnableControls ();
  184. return TRUE;
  185. }
  186. void CTemplateV2SupercedesPropertyPage::OnDeleteitemSupercededTemplatesList(NMHDR* pNMHDR, LRESULT* pResult)
  187. {
  188. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  189. CString* pszTemplateName = (CString*) m_templateList.GetItemData (pNMListView->iItem);
  190. if ( pszTemplateName )
  191. delete pszTemplateName;
  192. *pResult = 0;
  193. }
  194. HRESULT CTemplateV2SupercedesPropertyPage::AddItem(const CString &szTemplateName)
  195. {
  196. HCERTTYPE hCertType = 0;
  197. HRESULT hr = CAFindCertTypeByName (szTemplateName,
  198. NULL,
  199. CT_ENUM_MACHINE_TYPES | CT_ENUM_USER_TYPES | CT_FLAG_NO_CACHE_LOOKUP,
  200. &hCertType);
  201. _ASSERT (SUCCEEDED (hr));
  202. if ( SUCCEEDED (hr) )
  203. {
  204. PWSTR* rgwszProp = 0;
  205. hr = CAGetCertTypePropertyEx (hCertType,
  206. CERTTYPE_PROP_FRIENDLY_NAME, &rgwszProp);
  207. if ( SUCCEEDED (hr) )
  208. {
  209. DWORD dwVersion = 0;
  210. hr = CAGetCertTypePropertyEx (hCertType,
  211. CERTTYPE_PROP_SCHEMA_VERSION,
  212. &dwVersion);
  213. if ( SUCCEEDED (hr) )
  214. {
  215. LV_ITEM lvItem;
  216. int iItem = m_templateList.GetItemCount ();
  217. int iResult = 0;
  218. ::ZeroMemory (&lvItem, sizeof (lvItem));
  219. lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  220. lvItem.iItem = iItem;
  221. lvItem.iSubItem = COL_CERT_TEMPLATE;
  222. lvItem.pszText = rgwszProp[0];
  223. if ( 1 == dwVersion )
  224. lvItem.iImage = 0; // version is 1
  225. else
  226. lvItem.iImage = 1; // version is 2
  227. lvItem.lParam = (LPARAM) new CString (szTemplateName);
  228. iItem = m_templateList.InsertItem (&lvItem);
  229. ASSERT (-1 != iItem);
  230. if ( -1 != iItem )
  231. {
  232. ::ZeroMemory (&lvItem, sizeof (lvItem));
  233. lvItem.mask = LVIF_TEXT;
  234. lvItem.iItem = iItem;
  235. lvItem.iSubItem = COL_CERT_VERSION;
  236. CString text;
  237. if ( 1 == dwVersion )
  238. VERIFY (text.LoadString (IDS_WINDOWS_2000_AND_LATER));
  239. else
  240. VERIFY (text.LoadString (IDS_WINDOWS_2002_AND_LATER));
  241. lvItem.pszText = (PWSTR)(PCWSTR) text;
  242. iResult = m_templateList.SetItem (&lvItem);
  243. ASSERT (-1 != iResult);
  244. if ( -1 == iResult )
  245. hr = E_FAIL;
  246. }
  247. else
  248. hr = E_FAIL;
  249. }
  250. else
  251. {
  252. _TRACE (0, L"CAGetCertTypePropertyEx (CERTTYPE_PROP_SCHEMA_VERSION) failed: 0x%x\n", hr);
  253. }
  254. CAFreeCertTypeProperty (hCertType, rgwszProp);
  255. }
  256. else
  257. {
  258. _TRACE (0, L"CAGetCertTypePropertyEx (CERTTYPE_PROP_FRIENDLY_NAME) failed: 0x%x\n", hr);
  259. }
  260. hr = CACloseCertType (hCertType);
  261. _ASSERT (SUCCEEDED (hr));
  262. if ( !SUCCEEDED (hr) )
  263. {
  264. _TRACE (0, L"CACloseCertType (%s) failed: 0x%x\n", hr);
  265. }
  266. }
  267. else
  268. {
  269. _TRACE (0, L"CAFindCertTypeByName (%s) failed: 0x%x\n",
  270. (PCWSTR) szTemplateName, hr);
  271. }
  272. return hr;
  273. }
  274. void CTemplateV2SupercedesPropertyPage::OnItemchangedSupercededTemplatesList(NMHDR* /*pNMHDR*/, LRESULT* pResult)
  275. {
  276. EnableControls ();
  277. *pResult = 0;
  278. }
  279. void CTemplateV2SupercedesPropertyPage::DoContextHelp (HWND hWndControl)
  280. {
  281. _TRACE(1, L"Entering CTemplateV2SupercedesPropertyPage::DoContextHelp\n");
  282. switch (::GetDlgCtrlID (hWndControl))
  283. {
  284. case IDC_SUPERCEDES_LABEL:
  285. case IDC_STATIC:
  286. break;
  287. default:
  288. // Display context help for a control
  289. if ( !::WinHelp (
  290. hWndControl,
  291. GetContextHelpFile (),
  292. HELP_WM_HELP,
  293. (DWORD_PTR) g_aHelpIDs_IDD_TEMPLATE_V2_SUPERCEDES) )
  294. {
  295. _TRACE(0, L"WinHelp () failed: 0x%x\n", GetLastError ());
  296. }
  297. break;
  298. }
  299. _TRACE(-1, L"Leaving CTemplateV2SupercedesPropertyPage::DoContextHelp\n");
  300. }
  301. void CTemplateV2SupercedesPropertyPage::OnDestroy()
  302. {
  303. CHelpPropertyPage::OnDestroy();
  304. m_imageListNormal.Destroy ();
  305. m_imageListSmall.Destroy ();
  306. }