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.

595 lines
13 KiB

  1. //-------------------------------------------------------------------------
  2. // File: PolicyTemplateMgrDlg.cpp
  3. //
  4. // Author : Kishnan Nedungadi
  5. //
  6. // created : 3/27/2000
  7. //-------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include <wbemidl.h>
  10. #include <commctrl.h>
  11. #include "resource.h"
  12. #include "defines.h"
  13. #include "ntdmutils.h"
  14. #include "SchemaManager.h"
  15. #include "PolicyTemplateManager.h"
  16. #include "PolicyTemplateMgrDlg.h"
  17. #include "PolicyTemplateEditPropertiesDlg.h"
  18. CPolicyTemplateManagerDlg * g_pPolicyTemplateManagerDlg = NULL;
  19. extern CEditPolicyTemplatePropertiesPageDlg * g_pEditPolicyTemplatePropertiesPage;
  20. //-------------------------------------------------------------------------
  21. INT_PTR CALLBACK PolicyTemplateManagerDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  22. {
  23. if(g_pPolicyTemplateManagerDlg)
  24. {
  25. return g_pPolicyTemplateManagerDlg->PolicyTemplateManagerDlgProc(hDlg, iMessage, wParam, lParam);
  26. }
  27. return FALSE;
  28. }
  29. //-------------------------------------------------------------------------
  30. CPolicyTemplateManagerDlg::CPolicyTemplateManagerDlg(CPolicyTemplateManager * pPolicyTemplateManager)
  31. {
  32. _ASSERT(pPolicyTemplateManager);
  33. m_hWnd = NULL;
  34. m_hwndListView = NULL;
  35. m_pPolicyTemplateManager = pPolicyTemplateManager;
  36. }
  37. //-------------------------------------------------------------------------
  38. CPolicyTemplateManagerDlg::~CPolicyTemplateManagerDlg()
  39. {
  40. }
  41. //-------------------------------------------------------------------------
  42. INT_PTR CALLBACK CPolicyTemplateManagerDlg::PolicyTemplateManagerDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  43. {
  44. LPPSHNOTIFY lppsn = NULL;
  45. switch(iMessage)
  46. {
  47. case WM_INITDIALOG:
  48. {
  49. m_hWnd = hDlg;
  50. InitializeDialog();
  51. PopulatePolicyTypeList();
  52. OnPolicyTypeChange();
  53. break;
  54. }
  55. case WM_DESTROY:
  56. {
  57. DestroyDialog();
  58. break;
  59. }
  60. case WM_COMMAND:
  61. {
  62. switch(LOWORD(wParam))
  63. {
  64. case IDOK:
  65. case IDCANCEL:
  66. EndDialog(m_hWnd, 0);
  67. return TRUE;
  68. }
  69. if(BN_CLICKED == HIWORD(wParam) && IDC_NEW == LOWORD(wParam))
  70. {
  71. OnNew();
  72. return TRUE;
  73. }
  74. if(BN_CLICKED == HIWORD(wParam) && IDC_EDIT == LOWORD(wParam))
  75. {
  76. OnEdit();
  77. return TRUE;
  78. }
  79. if(BN_CLICKED == HIWORD(wParam) && IDC_DELETE == LOWORD(wParam))
  80. {
  81. OnDelete();
  82. return TRUE;
  83. }
  84. if(CBN_SELCHANGE == HIWORD(wParam) && IDC_POLICY_TYPE == LOWORD(wParam))
  85. {
  86. OnPolicyTypeChange();
  87. return TRUE;
  88. }
  89. break;
  90. }
  91. case WM_NOTIFY:
  92. {
  93. LPNMHDR lpnm = (LPNMHDR) lParam;
  94. switch (lpnm->code)
  95. {
  96. case NM_DBLCLK :
  97. {
  98. if(lpnm->idFrom == IDC_SOM_FILTER_LIST)
  99. {
  100. OnEdit();
  101. return TRUE;
  102. }
  103. break;
  104. }
  105. default :
  106. break;
  107. }
  108. }
  109. }
  110. return FALSE;
  111. }
  112. //---------------------------------------------------------------------------
  113. STDMETHODIMP CPolicyTemplateManagerDlg::InitializeDialog()
  114. {
  115. HRESULT hr;
  116. LVCOLUMN lvColumn;
  117. CComBSTR bstrName;
  118. NTDM_BEGIN_METHOD()
  119. //Initialize the ListView Control
  120. m_hwndListView = GetDlgItem(m_hWnd, IDC_SOM_FILTER_LIST);
  121. NTDM_ERR_IF_NULL(m_hwndListView);
  122. ListView_SetExtendedListViewStyle(m_hwndListView, LVS_EX_FULLROWSELECT);
  123. bstrName.LoadString(_Module.GetResourceInstance(), IDS_NAME);
  124. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  125. lvColumn.fmt = LVCFMT_LEFT;
  126. lvColumn.pszText = bstrName;
  127. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndListView, 0, &lvColumn));
  128. bstrName.LoadString(_Module.GetResourceInstance(), IDS_POLICY_TYPE);
  129. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  130. lvColumn.fmt = LVCFMT_LEFT;
  131. lvColumn.pszText = bstrName;
  132. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndListView, 1, &lvColumn));
  133. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndListView, 0, LVSCW_AUTOSIZE_USEHEADER));
  134. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndListView, 1, LVSCW_AUTOSIZE_USEHEADER));
  135. NTDM_END_METHOD()
  136. // cleanup
  137. return hr;
  138. }
  139. //---------------------------------------------------------------------------
  140. STDMETHODIMP CPolicyTemplateManagerDlg::DestroyDialog()
  141. {
  142. HRESULT hr;
  143. NTDM_BEGIN_METHOD()
  144. NTDM_ERR_IF_FAIL(ClearPolicyTemplateList());
  145. NTDM_ERR_IF_FAIL(ClearPolicyTypeList());
  146. NTDM_END_METHOD()
  147. // cleanup
  148. return hr;
  149. }
  150. //---------------------------------------------------------------------------
  151. STDMETHODIMP CPolicyTemplateManagerDlg::ClearPolicyTemplateList()
  152. {
  153. HRESULT hr;
  154. LVITEM lvItem;
  155. long lCount;
  156. NTDM_BEGIN_METHOD()
  157. //Release each item in the ListView Control
  158. lvItem.mask = LVIF_PARAM;
  159. lvItem.iSubItem = 0;
  160. lCount = ListView_GetItemCount(m_hwndListView);
  161. while(lCount > 0)
  162. {
  163. lCount--;
  164. lvItem.iItem = lCount;
  165. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  166. if(lvItem.lParam)
  167. {
  168. ((IWbemClassObject *)lvItem.lParam)->Release();
  169. }
  170. }
  171. ListView_DeleteAllItems(m_hwndListView);
  172. NTDM_END_METHOD()
  173. // cleanup
  174. return hr;
  175. }
  176. //---------------------------------------------------------------------------
  177. STDMETHODIMP CPolicyTemplateManagerDlg::PopulatePolicyTemplatesList()
  178. {
  179. HRESULT hr;
  180. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  181. CComPtr<IWbemClassObject>pIWbemClassDefinitionObject;
  182. ULONG uReturned;
  183. CComVariant vValueClassName;
  184. CComBSTR bstrWQLStmt;
  185. CComBSTR bstrWQL;
  186. CComVariant vValue;
  187. CIMTYPE cimType;
  188. NTDM_BEGIN_METHOD()
  189. if(!m_pPolicyTemplateManager->m_pIWbemServices)
  190. NTDM_EXIT(E_FAIL);
  191. NTDM_ERR_IF_FAIL(ClearPolicyTemplateList());
  192. // Get the Policy Template Class
  193. NTDM_ERR_MSG_IF_FAIL(m_pIPolicyTypeClassObject->Get(_T("ClassDefinition"), 0, &vValue, &cimType, NULL));
  194. // Make sure we got a Class Object back
  195. if(V_VT(&vValue) != VT_UNKNOWN)
  196. {
  197. NTDM_EXIT(E_FAIL);
  198. }
  199. NTDM_ERR_MSG_IF_FAIL(V_UNKNOWN(&vValue)->QueryInterface(IID_IWbemClassObject, (void**)&pIWbemClassDefinitionObject));
  200. NTDM_ERR_MSG_IF_FAIL(pIWbemClassDefinitionObject->Get(_T("__CLASS"), 0, &vValueClassName, &cimType, NULL));
  201. //vValueClassName = _T("ified");
  202. // Get all the policy templates that have the TargetClass property = the class we got above.
  203. bstrWQLStmt = _T("SELECT * FROM MSFT_MergeablePolicyTemplate WHERE TargetClass='");
  204. bstrWQLStmt += V_BSTR(&vValueClassName);
  205. bstrWQLStmt += _T("'");
  206. bstrWQL = _T("WQL");
  207. NTDM_ERR_MSG_IF_FAIL(m_pPolicyTemplateManager->m_pIWbemServices->ExecQuery(bstrWQL, bstrWQLStmt, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  208. // Loop through each item in the enumeration and add it to the list
  209. while(true)
  210. {
  211. IWbemClassObject *pIWbemClassObject = NULL;
  212. NTDM_ERR_MSG_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, &pIWbemClassObject, &uReturned));
  213. if(!uReturned)
  214. break;
  215. // Add current Item to the list
  216. AddItemToList(pIWbemClassObject);
  217. pIWbemClassObject->Release();
  218. }
  219. ListView_SetItemState(m_hwndListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  220. NTDM_END_METHOD()
  221. // cleanup
  222. return hr;
  223. }
  224. //---------------------------------------------------------------------------
  225. STDMETHODIMP CPolicyTemplateManagerDlg::AddItemToList(IWbemClassObject * pIWbemClassObject, long lIndex)
  226. {
  227. HRESULT hr;
  228. CComVariant vValue;
  229. CIMTYPE vType;
  230. LVITEM lvItem;
  231. NTDM_BEGIN_METHOD()
  232. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("Name"), 0, &vValue, &vType, NULL));
  233. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  234. lvItem.iItem = lIndex;
  235. lvItem.iSubItem = 0;
  236. lvItem.pszText = vValue.bstrVal;
  237. lvItem.lParam = (LPARAM)pIWbemClassObject;
  238. NTDM_ERR_IF_MINUSONE(ListView_InsertItem(m_hwndListView, &lvItem));
  239. pIWbemClassObject->AddRef();
  240. NTDM_END_METHOD()
  241. // cleanup
  242. return hr;
  243. }
  244. //---------------------------------------------------------------------------
  245. STDMETHODIMP CPolicyTemplateManagerDlg::OnEdit()
  246. {
  247. HRESULT hr;
  248. long lSelectionMark;
  249. NTDM_BEGIN_METHOD()
  250. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  251. if(-1 == lSelectionMark)
  252. {
  253. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  254. NTDM_EXIT(E_FAIL);
  255. }
  256. else
  257. {
  258. // get a pointer to the IWbemClassObject
  259. LVITEM lvItem;
  260. lvItem.mask = LVIF_PARAM;
  261. lvItem.iSubItem = 0;
  262. lvItem.iItem = lSelectionMark;
  263. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  264. if(lvItem.lParam)
  265. {
  266. g_pEditPolicyTemplatePropertiesPage = new CEditPolicyTemplatePropertiesPageDlg((IWbemClassObject *)lvItem.lParam, m_pPolicyTemplateManager->m_pIWbemServices);
  267. if(IDOK == DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_POLICY_TEMPLATE_PROPERTIES), m_hWnd, EditPolicyTemplatePropertiesPageDlgProc))
  268. {
  269. // Refresh the SOM filters
  270. NTDM_ERR_IF_FAIL(PopulatePolicyTemplatesList());
  271. }
  272. }
  273. }
  274. NTDM_END_METHOD()
  275. // cleanup
  276. return hr;
  277. }
  278. //---------------------------------------------------------------------------
  279. STDMETHODIMP CPolicyTemplateManagerDlg::OnDelete()
  280. {
  281. HRESULT hr;
  282. long lSelectionMark;
  283. NTDM_BEGIN_METHOD()
  284. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  285. if(-1 == lSelectionMark)
  286. {
  287. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  288. NTDM_EXIT(E_FAIL);
  289. }
  290. else
  291. {
  292. // get a pointer to the IWbemClassObject
  293. LVITEM lvItem;
  294. lvItem.mask = LVIF_PARAM;
  295. lvItem.iSubItem = 0;
  296. lvItem.iItem = lSelectionMark;
  297. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  298. if(lvItem.lParam)
  299. {
  300. CComVariant vValue;
  301. CIMTYPE cimType;
  302. IWbemClassObject * pIWbemClassObject = (IWbemClassObject *)lvItem.lParam;
  303. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("__PATH"), 0, &vValue, &cimType, NULL));
  304. NTDM_ERR_MSG_IF_FAIL(m_pPolicyTemplateManager->m_pIWbemServices->DeleteInstance(V_BSTR(&vValue), 0, NULL, NULL));
  305. // Refresh the SOM filters
  306. NTDM_ERR_IF_FAIL(PopulatePolicyTemplatesList());
  307. }
  308. }
  309. NTDM_END_METHOD()
  310. // cleanup
  311. return hr;
  312. }
  313. //---------------------------------------------------------------------------
  314. STDMETHODIMP CPolicyTemplateManagerDlg::OnNew()
  315. {
  316. HRESULT hr;
  317. CComPtr<IWbemClassObject>pIWbemClassObject;
  318. CComPtr<IWbemClassObject>pIWbemNewInstance;
  319. CComBSTR bstrTemp;
  320. NTDM_BEGIN_METHOD()
  321. bstrTemp = _T("MSFT_PolicyTemplate");
  322. NTDM_ERR_MSG_IF_FAIL(m_pPolicyTemplateManager->m_pIWbemServices->GetObject(bstrTemp, 0, NULL, &pIWbemClassObject, NULL));
  323. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->SpawnInstance(0, &pIWbemNewInstance));
  324. g_pEditPolicyTemplatePropertiesPage = new CEditPolicyTemplatePropertiesPageDlg(pIWbemNewInstance, m_pPolicyTemplateManager->m_pIWbemServices);
  325. if(IDOK == DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_POLICY_TEMPLATE_PROPERTIES), m_hWnd, EditPolicyTemplatePropertiesPageDlgProc))
  326. {
  327. // Refresh the SOM filters
  328. NTDM_ERR_IF_FAIL(PopulatePolicyTemplatesList());
  329. }
  330. NTDM_END_METHOD()
  331. // cleanup
  332. return hr;
  333. }
  334. //---------------------------------------------------------------------------
  335. STDMETHODIMP CPolicyTemplateManagerDlg::PopulatePolicyTypeList()
  336. {
  337. HRESULT hr;
  338. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  339. CComBSTR bstrClass(_T("MSFT_PolicyType"));
  340. ULONG uReturned;
  341. NTDM_BEGIN_METHOD()
  342. if(!m_pPolicyTemplateManager->m_pIWbemServices)
  343. NTDM_EXIT(E_FAIL);
  344. NTDM_ERR_IF_FAIL(ClearPolicyTypeList());
  345. // Get the Enumeration
  346. NTDM_ERR_MSG_IF_FAIL(m_pPolicyTemplateManager->m_pIWbemServices->CreateInstanceEnum(bstrClass, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  347. // Loop through each item in the enumeration and add it to the list
  348. while(true)
  349. {
  350. IWbemClassObject *pIWbemClassObject = NULL;
  351. NTDM_ERR_MSG_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, &pIWbemClassObject, &uReturned));
  352. if(!uReturned)
  353. break;
  354. // Add current Item to the list
  355. AddPolicyTypeToList(pIWbemClassObject);
  356. pIWbemClassObject->Release();
  357. }
  358. SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_SETCURSEL, 0, 0);
  359. NTDM_END_METHOD()
  360. // cleanup
  361. return hr;
  362. }
  363. //---------------------------------------------------------------------------
  364. STDMETHODIMP CPolicyTemplateManagerDlg::AddPolicyTypeToList(IWbemClassObject * pIWbemClassObject)
  365. {
  366. HRESULT hr;
  367. CComVariant vValue;
  368. CIMTYPE vType;
  369. long lIndex;
  370. NTDM_BEGIN_METHOD()
  371. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("ID"), 0, &vValue, &vType, NULL));
  372. lIndex = SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_ADDSTRING, 0, (LPARAM)vValue.bstrVal);
  373. NTDM_CHECK_CB_ERR(lIndex);
  374. NTDM_CHECK_CB_ERR(SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_SETITEMDATA, lIndex, (LPARAM)pIWbemClassObject));
  375. pIWbemClassObject->AddRef();
  376. NTDM_END_METHOD()
  377. // cleanup
  378. return hr;
  379. }
  380. //---------------------------------------------------------------------------
  381. STDMETHODIMP CPolicyTemplateManagerDlg::ClearPolicyTypeList()
  382. {
  383. HRESULT hr;
  384. long lCount;
  385. IWbemClassObject * pIWbemClassObject = NULL;
  386. NTDM_BEGIN_METHOD()
  387. // Release all the objects.
  388. lCount = SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_GETCOUNT, 0, 0);
  389. NTDM_CHECK_CB_ERR(lCount);
  390. while(lCount > 0)
  391. {
  392. lCount--;
  393. pIWbemClassObject = (IWbemClassObject *)SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_GETITEMDATA, lCount, 0);
  394. if(pIWbemClassObject)
  395. {
  396. pIWbemClassObject->Release();
  397. }
  398. }
  399. NTDM_END_METHOD()
  400. // cleanup
  401. return hr;
  402. }
  403. //---------------------------------------------------------------------------
  404. STDMETHODIMP CPolicyTemplateManagerDlg::OnPolicyTypeChange()
  405. {
  406. HRESULT hr;
  407. long lIndex;
  408. IWbemClassObject * pIWbemClassObject = NULL;
  409. NTDM_BEGIN_METHOD()
  410. // Release all the objects.
  411. lIndex = SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_GETCURSEL, 0, 0);
  412. NTDM_CHECK_CB_ERR(lIndex);
  413. pIWbemClassObject = (IWbemClassObject *)SendDlgItemMessage(m_hWnd, IDC_POLICY_TYPE, CB_GETITEMDATA, lIndex, 0);
  414. if(pIWbemClassObject)
  415. m_pIPolicyTypeClassObject = pIWbemClassObject;
  416. NTDM_ERR_IF_FAIL(PopulatePolicyTemplatesList());
  417. NTDM_END_METHOD()
  418. // cleanup
  419. return hr;
  420. }