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.

596 lines
15 KiB

  1. //-------------------------------------------------------------------------
  2. // File: SomFilterEditPropertiesDlg.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 "SomFilterEditPropertiesDlg.h"
  17. #include "EditPropertyDlgs.h"
  18. extern USHORT g_CIMTypes[];
  19. extern CSimpleArray<BSTR> g_bstrCIMTypes;
  20. CEditSomFilterPropertiesPageDlg * g_pEditSomFilterPropertiesPage = NULL;
  21. //-------------------------------------------------------------------------
  22. INT_PTR CALLBACK EditSomFilterPropertiesPageDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  23. {
  24. if(g_pEditSomFilterPropertiesPage)
  25. {
  26. return g_pEditSomFilterPropertiesPage->EditSomFilterPropertiesPageDlgProc(hDlg, iMessage, wParam, lParam);
  27. }
  28. return FALSE;
  29. }
  30. //-------------------------------------------------------------------------
  31. CEditSomFilterPropertiesPageDlg::CEditSomFilterPropertiesPageDlg(IWbemClassObject * pISomFilterClassObject, IWbemServices * pIWbemServices)
  32. {
  33. _ASSERT(pISomFilterClassObject);
  34. _ASSERT(pIWbemServices);
  35. m_hWnd = NULL;
  36. m_pISomFilterClassObject = pISomFilterClassObject;
  37. m_pIWbemServices = pIWbemServices;
  38. }
  39. //-------------------------------------------------------------------------
  40. CEditSomFilterPropertiesPageDlg::~CEditSomFilterPropertiesPageDlg()
  41. {
  42. }
  43. //-------------------------------------------------------------------------
  44. INT_PTR CALLBACK CEditSomFilterPropertiesPageDlg::EditSomFilterPropertiesPageDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  45. {
  46. LPPSHNOTIFY lppsn = NULL;
  47. switch(iMessage)
  48. {
  49. case WM_INITDIALOG:
  50. {
  51. m_hWnd = hDlg;
  52. InitializeDialog();
  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. {
  66. OnOK();
  67. return TRUE;
  68. break;
  69. }
  70. case IDCANCEL:
  71. {
  72. EndDialog(m_hWnd, IDCANCEL);
  73. return TRUE;
  74. break;
  75. }
  76. }
  77. if(BN_CLICKED == HIWORD(wParam) && IDC_EDIT == LOWORD(wParam))
  78. {
  79. OnEdit();
  80. return TRUE;
  81. }
  82. if(BN_CLICKED == HIWORD(wParam) && IDC_IMPORT == LOWORD(wParam))
  83. {
  84. OnImport();
  85. return TRUE;
  86. }
  87. if(BN_CLICKED == HIWORD(wParam) && IDC_EXPORT == LOWORD(wParam))
  88. {
  89. OnExport();
  90. return TRUE;
  91. }
  92. break;
  93. }
  94. case WM_NOTIFY:
  95. {
  96. LPNMHDR lpnm = (LPNMHDR) lParam;
  97. switch (lpnm->code)
  98. {
  99. case NM_DBLCLK :
  100. {
  101. if(lpnm->idFrom == IDC_FILTER_PROPERTIES_LIST)
  102. {
  103. OnEdit();
  104. return TRUE;
  105. }
  106. break;
  107. }
  108. default :
  109. break;
  110. }
  111. }
  112. }
  113. return FALSE;
  114. }
  115. //---------------------------------------------------------------------------
  116. STDMETHODIMP CEditSomFilterPropertiesPageDlg::InitializeDialog()
  117. {
  118. HRESULT hr;
  119. LVCOLUMN lvColumn;
  120. CComBSTR bstrTemp;
  121. NTDM_BEGIN_METHOD()
  122. //Initialize the Property List Control
  123. m_hwndPropertiesListView = GetDlgItem(m_hWnd, IDC_FILTER_PROPERTIES_LIST);
  124. NTDM_ERR_IF_NULL(m_hwndPropertiesListView);
  125. ListView_SetExtendedListViewStyle(m_hwndPropertiesListView, LVS_EX_FULLROWSELECT);
  126. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  127. lvColumn.fmt = LVCFMT_LEFT;
  128. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_NAME);
  129. lvColumn.pszText = bstrTemp;
  130. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndPropertiesListView, 0, &lvColumn));
  131. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_TYPE);
  132. lvColumn.pszText = bstrTemp;
  133. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndPropertiesListView, 1, &lvColumn));
  134. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_VALUE);
  135. lvColumn.pszText = bstrTemp;
  136. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndPropertiesListView, 2, &lvColumn));
  137. PopulateSomFilterPropertiesList();
  138. NTDM_END_METHOD()
  139. // cleanup
  140. return hr;
  141. }
  142. //---------------------------------------------------------------------------
  143. STDMETHODIMP CEditSomFilterPropertiesPageDlg::DestroyDialog()
  144. {
  145. HRESULT hr;
  146. NTDM_BEGIN_METHOD()
  147. NTDM_ERR_IF_FAIL(ClearSomFilterPropertiesList());
  148. NTDM_END_METHOD()
  149. // cleanup
  150. return hr;
  151. }
  152. //---------------------------------------------------------------------------
  153. STDMETHODIMP CEditSomFilterPropertiesPageDlg::ClearSomFilterPropertiesList()
  154. {
  155. HRESULT hr;
  156. NTDM_BEGIN_METHOD()
  157. ListView_DeleteAllItems(m_hwndPropertiesListView);
  158. NTDM_END_METHOD()
  159. // cleanup
  160. return hr;
  161. }
  162. //---------------------------------------------------------------------------
  163. STDMETHODIMP CEditSomFilterPropertiesPageDlg::PopulateSomFilterPropertiesList()
  164. {
  165. HRESULT hr;
  166. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  167. CComBSTR bstrName;
  168. CComVariant vValue;
  169. CIMTYPE cimType;
  170. NTDM_BEGIN_METHOD()
  171. NTDM_ERR_IF_FAIL(ClearSomFilterPropertiesList());
  172. if(!m_pIWbemServices)
  173. NTDM_EXIT(E_FAIL);
  174. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->BeginEnumeration(WBEM_FLAG_NONSYSTEM_ONLY));
  175. while(true)
  176. {
  177. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->Next(0, &bstrName, &vValue, &cimType, NULL));
  178. if(WBEM_S_NO_MORE_DATA == hr)
  179. break;
  180. NTDM_ERR_IF_FAIL(AddItemToPropertyList(bstrName, &vValue, cimType));
  181. }
  182. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->EndEnumeration());
  183. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndPropertiesListView, 0, LVSCW_AUTOSIZE_USEHEADER));
  184. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndPropertiesListView, 1, LVSCW_AUTOSIZE_USEHEADER));
  185. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndPropertiesListView, 2, LVSCW_AUTOSIZE_USEHEADER));
  186. ListView_SetItemState(m_hwndPropertiesListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  187. NTDM_END_METHOD()
  188. // cleanup
  189. return hr;
  190. }
  191. //---------------------------------------------------------------------------
  192. STDMETHODIMP CEditSomFilterPropertiesPageDlg::AddItemToPropertyList(LPCTSTR pcszName, VARIANT * pvValue, CIMTYPE cimType, long lIndex)
  193. {
  194. HRESULT hr;
  195. LVITEM lvItem;
  196. CComVariant vValue;
  197. long i;
  198. NTDM_BEGIN_METHOD()
  199. vValue = *pvValue;
  200. // Name
  201. lvItem.mask = LVIF_TEXT;
  202. lvItem.iItem = lIndex;
  203. lvItem.iSubItem = 0;
  204. lvItem.pszText = (LPTSTR)pcszName;
  205. lvItem.iItem = ListView_InsertItem(m_hwndPropertiesListView, &lvItem);
  206. NTDM_ERR_IF_MINUSONE(lvItem.iItem);
  207. // Type
  208. lvItem.mask = LVIF_TEXT;
  209. lvItem.iSubItem = 1;
  210. lvItem.pszText = _T("");
  211. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  212. {
  213. // if Array and the type contains CIM_FLAG_ARRAY then show array
  214. if((g_CIMTypes[i] == cimType)||((g_CIMTypes[i] == CIM_FLAG_ARRAY) && (CIM_FLAG_ARRAY & cimType)))
  215. {
  216. lvItem.pszText = (LPTSTR)g_bstrCIMTypes[i];
  217. break;
  218. }
  219. }
  220. NTDM_ERR_IF_MINUSONE(ListView_SetItem(m_hwndPropertiesListView, &lvItem));
  221. // Value
  222. if FAILED(hr = VariantChangeType(&vValue, &vValue, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR))
  223. {
  224. if(V_VT(&vValue) == VT_NULL)
  225. {
  226. vValue = _T("");
  227. }
  228. else
  229. {
  230. CComBSTR bstrTemp;
  231. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_UNABLE_TO_DISPLAY);
  232. vValue = bstrTemp;
  233. }
  234. hr = NOERROR;
  235. }
  236. lvItem.mask = LVIF_TEXT;
  237. lvItem.iSubItem = 2;
  238. lvItem.pszText = vValue.bstrVal;
  239. NTDM_ERR_IF_MINUSONE(ListView_SetItem(m_hwndPropertiesListView, &lvItem));
  240. NTDM_END_METHOD()
  241. // cleanup
  242. return hr;
  243. }
  244. //---------------------------------------------------------------------------
  245. STDMETHODIMP CEditSomFilterPropertiesPageDlg::OnEdit()
  246. {
  247. HRESULT hr;
  248. long lSelectionMark;
  249. NTDM_BEGIN_METHOD()
  250. lSelectionMark = ListView_GetSelectionMark(m_hwndPropertiesListView);
  251. if(-1 == lSelectionMark)
  252. {
  253. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_PROPERTY_SELECTED);
  254. NTDM_EXIT(E_FAIL);
  255. }
  256. else
  257. {
  258. TCHAR pszBuffer[SZ_MAX_SIZE];
  259. VARIANT vValue;
  260. CComBSTR bstrName;
  261. CComBSTR bstrType;
  262. CIMTYPE cimType;
  263. LVITEM lvItem;
  264. long i;
  265. VariantInit(&vValue);
  266. // get the property info
  267. lvItem.mask = LVIF_TEXT;
  268. lvItem.iItem = lSelectionMark;
  269. lvItem.iSubItem = 0;
  270. lvItem.pszText = pszBuffer;
  271. lvItem.cchTextMax = SZ_MAX_SIZE;
  272. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndPropertiesListView, &lvItem));
  273. if(lvItem.pszText)
  274. {
  275. bstrName = lvItem.pszText;
  276. long lSpecialCaseProperty = 0;
  277. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->Get(bstrName, 0, &vValue, &cimType, NULL));
  278. if(_tcscmp(_T("RangeSettings"), bstrName) == 0)
  279. {
  280. lSpecialCaseProperty = CEditProperty::psc_rules;
  281. }
  282. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  283. {
  284. // if Array and the type contains CIM_FLAG_ARRAY then show array
  285. if((g_CIMTypes[i] == cimType)||((g_CIMTypes[i] == CIM_FLAG_ARRAY) && (CIM_FLAG_ARRAY & cimType)))
  286. {
  287. bstrType = (LPTSTR)g_bstrCIMTypes[i];
  288. break;
  289. }
  290. }
  291. CEditProperty dlgProps(m_hWnd, lvItem.pszText, bstrType, &vValue, m_pIWbemServices, lSpecialCaseProperty);
  292. if(IDOK == dlgProps.Run())
  293. {
  294. // delete the selected entry
  295. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->Put(bstrName, 0, &vValue, cimType));
  296. NTDM_ERR_IF_FALSE(ListView_DeleteItem(m_hwndPropertiesListView, lSelectionMark));
  297. NTDM_ERR_IF_FAIL(AddItemToPropertyList(bstrName, &vValue, cimType, lSelectionMark));
  298. }
  299. }
  300. }
  301. NTDM_END_METHOD()
  302. // cleanup
  303. return hr;
  304. }
  305. //---------------------------------------------------------------------------
  306. STDMETHODIMP CEditSomFilterPropertiesPageDlg::OnOK()
  307. {
  308. HRESULT hr;
  309. NTDM_BEGIN_METHOD()
  310. NTDM_ERR_MSG_IF_FAIL(m_pIWbemServices->PutInstance(m_pISomFilterClassObject, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL));
  311. hr = S_OK;
  312. NTDM_END_METHOD()
  313. // cleanup
  314. if(SUCCEEDED(hr))
  315. {
  316. EndDialog(m_hWnd, IDOK);
  317. }
  318. return hr;
  319. }
  320. //---------------------------------------------------------------------------
  321. STDMETHODIMP CEditSomFilterPropertiesPageDlg::OnImport()
  322. {
  323. HRESULT hr;
  324. CComBSTR bstrFilter;
  325. TCHAR pszFile[MAX_PATH];
  326. CComBSTR bstrTemp;
  327. pszFile[0] = 0;
  328. NTDM_BEGIN_METHOD()
  329. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ALL_FILES_FILTER);
  330. bstrFilter.LoadString(_Module.GetResourceInstance(), IDS_MOF_FILES_FILTER);
  331. bstrFilter += bstrTemp;
  332. CNTDMUtils::ReplaceCharacter(bstrFilter, L'@', L'\0');
  333. if(CNTDMUtils::OpenFileNameDlg(bstrFilter, _T("*.*"), m_hWnd, pszFile) && pszFile)
  334. {
  335. CComPtr<IMofCompiler>pIMofCompiler;
  336. CComPtr<IWbemClassObject>pINamespaceClass;
  337. CComPtr<IWbemClassObject>pIWbemNewInstance;
  338. CComPtr<IWbemClassObject>pIWbemClassObject;
  339. CComVariant vValue = _T("DeleteThisNamespace");
  340. WBEM_COMPILE_STATUS_INFO pInfo;
  341. // Generate a temporary namespace
  342. bstrTemp = _T("__Namespace");
  343. NTDM_ERR_MSG_IF_FAIL(m_pIWbemServices->GetObject(bstrTemp, WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &pINamespaceClass, NULL));
  344. NTDM_ERR_MSG_IF_FAIL(pINamespaceClass->SpawnInstance(0, &pIWbemNewInstance));
  345. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Name"), 0, &vValue, CIM_STRING));
  346. NTDM_ERR_MSG_IF_FAIL(m_pIWbemServices->PutInstance(pIWbemNewInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL));
  347. // mofcomp the file into that namespace
  348. NTDM_ERR_MSG_IF_FAIL(CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER,IID_IMofCompiler, (void**)&pIMofCompiler));
  349. NTDM_ERR_IF_FAIL(pIMofCompiler->CompileFile(pszFile, _T("\\\\.\\root\\policy\\DeleteThisNamespace"), NULL, NULL, NULL, WBEM_FLAG_DONT_ADD_TO_LIST, WBEM_FLAG_CREATE_ONLY, WBEM_FLAG_CREATE_ONLY, &pInfo));
  350. if(hr != WBEM_S_NO_ERROR)
  351. {
  352. CNTDMUtils::DisplayMessage(m_hWnd, IDS_FAILED_COMPILING_MOF_FILE);
  353. NTDM_EXIT(E_FAIL);
  354. }
  355. // get the 1st instance of MSFT_SomFilter from the newly created namespace
  356. NTDM_ERR_IF_FAIL(GetInstanceOfClass(bstrTemp, _T("MSFT_SomFilter"), &pIWbemClassObject));
  357. // copy the properties
  358. m_pISomFilterClassObject = pIWbemClassObject;
  359. PopulateSomFilterPropertiesList();
  360. }
  361. NTDM_END_METHOD()
  362. // finally delete the namespace that we created above
  363. {
  364. CComVariant vValue = "\\\\.\\root\\policy:__Namespace.Name=\"DeleteThisNamespace\"";
  365. m_pIWbemServices->DeleteInstance(V_BSTR(&vValue), 0, NULL, NULL);
  366. }
  367. return hr;
  368. }
  369. //---------------------------------------------------------------------------
  370. STDMETHODIMP CEditSomFilterPropertiesPageDlg::GetInstanceOfClass(BSTR pszNamespace, LPCTSTR pszClass, IWbemClassObject ** ppWbemClassObject)
  371. {
  372. HRESULT hr;
  373. CComPtr<IWbemLocator>pIWbemLocator;
  374. CComPtr<IWbemServices>pIWbemServices;
  375. CComPtr<IEnumWbemClassObject>pEnumWbemClassObject;
  376. CComBSTR bstrClass = _T("MSFT_SomFilter");
  377. ULONG uReturned;
  378. NTDM_BEGIN_METHOD()
  379. // create the webm locator
  380. NTDM_ERR_MSG_IF_FAIL(CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
  381. IID_IWbemLocator, (LPVOID *) &pIWbemLocator));
  382. NTDM_ERR_MSG_IF_FAIL(pIWbemLocator->ConnectServer( pszNamespace,
  383. NULL,
  384. NULL,
  385. NULL,
  386. 0,
  387. NULL,
  388. NULL,
  389. &pIWbemServices));
  390. NTDM_ERR_MSG_IF_FAIL(CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  391. RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE ,
  392. NULL, EOAC_NONE));
  393. NTDM_ERR_MSG_IF_FAIL(pIWbemServices->CreateInstanceEnum(bstrClass, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  394. NTDM_ERR_MSG_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, ppWbemClassObject, &uReturned));
  395. if(!uReturned)
  396. NTDM_EXIT(E_FAIL);
  397. NTDM_END_METHOD()
  398. // cleanup
  399. return hr;
  400. }
  401. //---------------------------------------------------------------------------
  402. STDMETHODIMP CEditSomFilterPropertiesPageDlg::OnExport()
  403. {
  404. HRESULT hr;
  405. CComBSTR bstrTemp;
  406. CComBSTR bstrFilter;
  407. TCHAR pszFile[MAX_PATH];
  408. CComBSTR bstrObjectText;
  409. HANDLE hFile = NULL;
  410. DWORD dwWritten;
  411. pszFile[0] = 0;
  412. NTDM_BEGIN_METHOD()
  413. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ALL_FILES_FILTER);
  414. bstrFilter.LoadString(_Module.GetResourceInstance(), IDS_TEXT_FILES_FILTER);
  415. bstrFilter += bstrTemp;
  416. CNTDMUtils::ReplaceCharacter(bstrFilter, L'@', L'\0');
  417. if(CNTDMUtils::SaveFileNameDlg(bstrFilter, _T("*.txt"), m_hWnd, pszFile))
  418. {
  419. if(_tcslen(pszFile))
  420. {
  421. NTDM_ERR_MSG_IF_FAIL(m_pISomFilterClassObject->GetObjectText(0, &bstrObjectText));
  422. // save to pszFile
  423. hFile = CreateFile(pszFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  424. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  425. if(hFile == INVALID_HANDLE_VALUE)
  426. {
  427. NTDM_ERR_GETLASTERROR_IF_NULL(NULL);
  428. goto error;
  429. }
  430. if(hFile)
  431. {
  432. NTDM_ERR_GETLASTERROR_IF_NULL(WriteFile(hFile, bstrObjectText, _tcslen(bstrObjectText) * sizeof(TCHAR), &dwWritten, NULL));
  433. NTDM_ERR_GETLASTERROR_IF_NULL(CloseHandle(hFile));
  434. hFile = NULL;
  435. }
  436. }
  437. }
  438. NTDM_END_METHOD()
  439. // cleanup
  440. if(hFile)
  441. {
  442. CloseHandle(hFile);
  443. hFile = NULL;
  444. }
  445. return hr;
  446. }