Leaked source code of windows server 2003
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.

472 lines
10 KiB

  1. //-------------------------------------------------------------------------
  2. // File: CDlgSomFilterManager.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 "SomFilterManager.h"
  16. #include "SomFilterMgrDlg.h"
  17. #include "SomFilterEditPropertiesDlg.h"
  18. CSomFilterManagerDlg * g_pFilterManagerDlg = NULL;
  19. extern CEditSomFilterPropertiesPageDlg * g_pEditSomFilterPropertiesPage;
  20. //-------------------------------------------------------------------------
  21. INT_PTR CALLBACK SomFilterManagerDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  22. {
  23. if(g_pFilterManagerDlg)
  24. {
  25. return g_pFilterManagerDlg->SomFilterManagerDlgProc(hDlg, iMessage, wParam, lParam);
  26. }
  27. return FALSE;
  28. }
  29. //-------------------------------------------------------------------------
  30. CSomFilterManagerDlg::CSomFilterManagerDlg(CSomFilterManager * pSomFilterManager)
  31. {
  32. _ASSERT(pSomFilterManager);
  33. m_hWnd = NULL;
  34. m_hwndListView = NULL;
  35. m_pSomFilterManager = pSomFilterManager;
  36. }
  37. //-------------------------------------------------------------------------
  38. CSomFilterManagerDlg::~CSomFilterManagerDlg()
  39. {
  40. }
  41. //-------------------------------------------------------------------------
  42. INT_PTR CALLBACK CSomFilterManagerDlg::SomFilterManagerDlgProc(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. PopulateFilterList();
  52. break;
  53. }
  54. case WM_DESTROY:
  55. {
  56. DestroyDialog();
  57. break;
  58. }
  59. case WM_COMMAND:
  60. {
  61. switch(LOWORD(wParam))
  62. {
  63. case IDCANCEL:
  64. EndDialog(m_hWnd, 0);
  65. return TRUE;
  66. case IDOK:
  67. OnOK();
  68. return TRUE;
  69. break;
  70. }
  71. if(BN_CLICKED == HIWORD(wParam) && IDC_NEW == LOWORD(wParam))
  72. {
  73. OnNew();
  74. return TRUE;
  75. }
  76. if(BN_CLICKED == HIWORD(wParam) && IDC_EDIT == LOWORD(wParam))
  77. {
  78. OnEdit();
  79. return TRUE;
  80. }
  81. if(BN_CLICKED == HIWORD(wParam) && IDC_DELETE == LOWORD(wParam))
  82. {
  83. OnDelete();
  84. return TRUE;
  85. }
  86. break;
  87. }
  88. case WM_NOTIFY:
  89. {
  90. LPNMHDR lpnm = (LPNMHDR) lParam;
  91. switch (lpnm->code)
  92. {
  93. case NM_DBLCLK :
  94. {
  95. if(lpnm->idFrom == IDC_SOM_FILTER_LIST)
  96. {
  97. OnEdit();
  98. return TRUE;
  99. }
  100. break;
  101. }
  102. default :
  103. break;
  104. }
  105. }
  106. }
  107. return FALSE;
  108. }
  109. //---------------------------------------------------------------------------
  110. STDMETHODIMP CSomFilterManagerDlg::InitializeDialog()
  111. {
  112. HRESULT hr;
  113. LVCOLUMN lvColumn;
  114. CComBSTR bstrName;
  115. NTDM_BEGIN_METHOD()
  116. bstrName.LoadString(_Module.GetResourceInstance(), IDS_NAME);
  117. //Initialize the ListView Control
  118. m_hwndListView = GetDlgItem(m_hWnd, IDC_SOM_FILTER_LIST);
  119. NTDM_ERR_IF_NULL(m_hwndListView);
  120. ListView_SetExtendedListViewStyle(m_hwndListView, LVS_EX_FULLROWSELECT);
  121. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  122. lvColumn.fmt = LVCFMT_LEFT;
  123. lvColumn.pszText = bstrName;
  124. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndListView, 0, &lvColumn));
  125. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndListView, 0, LVSCW_AUTOSIZE_USEHEADER));
  126. NTDM_END_METHOD()
  127. // cleanup
  128. return hr;
  129. }
  130. //---------------------------------------------------------------------------
  131. STDMETHODIMP CSomFilterManagerDlg::DestroyDialog()
  132. {
  133. HRESULT hr;
  134. NTDM_BEGIN_METHOD()
  135. NTDM_ERR_IF_FAIL(ClearFilterList());
  136. NTDM_END_METHOD()
  137. // cleanup
  138. return hr;
  139. }
  140. //---------------------------------------------------------------------------
  141. STDMETHODIMP CSomFilterManagerDlg::ClearFilterList()
  142. {
  143. HRESULT hr;
  144. LVITEM lvItem;
  145. long lCount;
  146. NTDM_BEGIN_METHOD()
  147. //Release each item in the ListView Control
  148. lvItem.mask = LVIF_PARAM;
  149. lvItem.iSubItem = 0;
  150. lCount = ListView_GetItemCount(m_hwndListView);
  151. while(lCount > 0)
  152. {
  153. lCount--;
  154. lvItem.iItem = lCount;
  155. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  156. if(lvItem.lParam)
  157. {
  158. ((IWbemClassObject *)lvItem.lParam)->Release();
  159. }
  160. }
  161. ListView_DeleteAllItems(m_hwndListView);
  162. NTDM_END_METHOD()
  163. // cleanup
  164. return hr;
  165. }
  166. //---------------------------------------------------------------------------
  167. STDMETHODIMP CSomFilterManagerDlg::PopulateFilterList()
  168. {
  169. HRESULT hr;
  170. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  171. CComBSTR bstrClass(_T("MSFT_SomFilter"));
  172. ULONG uReturned;
  173. NTDM_BEGIN_METHOD()
  174. if(!m_pSomFilterManager->m_pIWbemServices)
  175. NTDM_EXIT(E_FAIL);
  176. NTDM_ERR_IF_FAIL(ClearFilterList());
  177. // Get the Enumeration
  178. NTDM_ERR_MSG_IF_FAIL(m_pSomFilterManager->m_pIWbemServices->CreateInstanceEnum(bstrClass, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  179. // Loop through each item in the enumeration and add it to the list
  180. while(true)
  181. {
  182. IWbemClassObject *pIWbemClassObject = NULL;
  183. NTDM_ERR_MSG_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, &pIWbemClassObject, &uReturned));
  184. if(!uReturned)
  185. break;
  186. // Add current Item to the list
  187. AddItemToList(pIWbemClassObject);
  188. pIWbemClassObject->Release();
  189. }
  190. ListView_SetItemState(m_hwndListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  191. NTDM_END_METHOD()
  192. // cleanup
  193. return hr;
  194. }
  195. //---------------------------------------------------------------------------
  196. STDMETHODIMP CSomFilterManagerDlg::AddItemToList(IWbemClassObject * pIWbemClassObject, long lIndex)
  197. {
  198. HRESULT hr;
  199. CComVariant vValue;
  200. CIMTYPE vType;
  201. LVITEM lvItem;
  202. NTDM_BEGIN_METHOD()
  203. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("Name"), 0, &vValue, &vType, NULL));
  204. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  205. lvItem.iItem = lIndex;
  206. lvItem.iSubItem = 0;
  207. lvItem.pszText = vValue.bstrVal;
  208. lvItem.lParam = (LPARAM)pIWbemClassObject;
  209. NTDM_ERR_IF_MINUSONE(ListView_InsertItem(m_hwndListView, &lvItem));
  210. pIWbemClassObject->AddRef();
  211. NTDM_END_METHOD()
  212. // cleanup
  213. return hr;
  214. }
  215. //---------------------------------------------------------------------------
  216. STDMETHODIMP CSomFilterManagerDlg::OnEdit()
  217. {
  218. HRESULT hr;
  219. long lSelectionMark;
  220. NTDM_BEGIN_METHOD()
  221. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  222. if(-1 == lSelectionMark)
  223. {
  224. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  225. NTDM_EXIT(E_FAIL);
  226. }
  227. else
  228. {
  229. // get a pointer to the IWbemClassObject
  230. LVITEM lvItem;
  231. lvItem.mask = LVIF_PARAM;
  232. lvItem.iSubItem = 0;
  233. lvItem.iItem = lSelectionMark;
  234. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  235. if(lvItem.lParam)
  236. {
  237. g_pEditSomFilterPropertiesPage = new CEditSomFilterPropertiesPageDlg((IWbemClassObject *)lvItem.lParam, m_pSomFilterManager->m_pIWbemServices);
  238. if(IDOK == DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_SOM_FILTER_PROPERTIES), m_hWnd, EditSomFilterPropertiesPageDlgProc))
  239. {
  240. // Refresh the SOM filters
  241. NTDM_ERR_IF_FAIL(PopulateFilterList());
  242. }
  243. }
  244. }
  245. NTDM_END_METHOD()
  246. // cleanup
  247. return hr;
  248. }
  249. //---------------------------------------------------------------------------
  250. STDMETHODIMP CSomFilterManagerDlg::OnDelete()
  251. {
  252. HRESULT hr;
  253. long lSelectionMark;
  254. NTDM_BEGIN_METHOD()
  255. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  256. if(-1 == lSelectionMark)
  257. {
  258. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  259. NTDM_EXIT(E_FAIL);
  260. }
  261. else
  262. {
  263. // get a pointer to the IWbemClassObject
  264. LVITEM lvItem;
  265. lvItem.mask = LVIF_PARAM;
  266. lvItem.iSubItem = 0;
  267. lvItem.iItem = lSelectionMark;
  268. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  269. if(lvItem.lParam)
  270. {
  271. CComVariant vValue;
  272. CIMTYPE cimType;
  273. IWbemClassObject * pIWbemClassObject = (IWbemClassObject *)lvItem.lParam;
  274. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("__PATH"), 0, &vValue, &cimType, NULL));
  275. NTDM_ERR_MSG_IF_FAIL(m_pSomFilterManager->m_pIWbemServices->DeleteInstance(V_BSTR(&vValue), 0, NULL, NULL));
  276. // Refresh the SOM filters
  277. NTDM_ERR_IF_FAIL(PopulateFilterList());
  278. }
  279. }
  280. NTDM_END_METHOD()
  281. // cleanup
  282. return hr;
  283. }
  284. //---------------------------------------------------------------------------
  285. STDMETHODIMP CSomFilterManagerDlg::OnNew()
  286. {
  287. HRESULT hr;
  288. CComPtr<IWbemClassObject>pIWbemClassObject;
  289. CComPtr<IWbemClassObject>pIWbemNewInstance;
  290. CComBSTR bstrTemp;
  291. NTDM_BEGIN_METHOD()
  292. bstrTemp = _T("MSFT_SomFilter");
  293. NTDM_ERR_MSG_IF_FAIL(m_pSomFilterManager->m_pIWbemServices->GetObject(bstrTemp, 0, NULL, &pIWbemClassObject, NULL));
  294. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->SpawnInstance(0, &pIWbemNewInstance));
  295. g_pEditSomFilterPropertiesPage = new CEditSomFilterPropertiesPageDlg(pIWbemNewInstance, m_pSomFilterManager->m_pIWbemServices);
  296. if(IDOK == DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_SOM_FILTER_PROPERTIES), m_hWnd, EditSomFilterPropertiesPageDlgProc))
  297. {
  298. // Refresh the SOM filters
  299. NTDM_ERR_IF_FAIL(PopulateFilterList());
  300. }
  301. NTDM_END_METHOD()
  302. // cleanup
  303. return hr;
  304. }
  305. //---------------------------------------------------------------------------
  306. BOOL CSomFilterManagerDlg::OnOK()
  307. {
  308. HRESULT hr;
  309. long lSelectionMark;
  310. NTDM_BEGIN_METHOD()
  311. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  312. if(-1 == lSelectionMark)
  313. {
  314. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  315. goto error;
  316. }
  317. else
  318. {
  319. // get a pointer to the IWbemClassObject
  320. LVITEM lvItem;
  321. lvItem.mask = LVIF_PARAM;
  322. lvItem.iSubItem = 0;
  323. lvItem.iItem = lSelectionMark;
  324. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  325. if(lvItem.lParam)
  326. {
  327. m_pIWbemClassObject = (IWbemClassObject *)lvItem.lParam;
  328. m_pIWbemClassObject->AddRef();
  329. }
  330. }
  331. EndDialog(m_hWnd, IDOK);
  332. NTDM_END_METHOD()
  333. // cleanup
  334. return TRUE;
  335. }