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.

1574 lines
46 KiB

  1. //-------------------------------------------------------------------------
  2. // File: WMIFilterMgrDlg.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 "ColumnMgrDlg.h"
  16. #include "WMIFilterManager.h"
  17. #include "WMIFilterMgrDlg.h"
  18. #include "EditPropertyDlgs.h"
  19. #include "HtmlHelp.h"
  20. CWMIFilterManagerDlg * g_pFilterManagerDlg = NULL;
  21. extern CColumnManagerDlg * g_pColumnManagerDlg;
  22. //-------------------------------------------------------------------------
  23. INT_PTR CALLBACK WMIFilterManagerDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  24. {
  25. if(g_pFilterManagerDlg)
  26. {
  27. return g_pFilterManagerDlg->WMIFilterManagerDlgProc(hDlg, iMessage, wParam, lParam);
  28. }
  29. return FALSE;
  30. }
  31. //-------------------------------------------------------------------------
  32. CWMIFilterManagerDlg::CWMIFilterManagerDlg(CWMIFilterManager * pWMIFilterManager, bool bBrowsing, BSTR bstrDomain)
  33. {
  34. _ASSERT(pWMIFilterManager);
  35. m_pIWbemClassObject = NULL;
  36. m_hWnd = NULL;
  37. m_hwndFilterListView = NULL;
  38. m_hwndQueryListView = NULL;
  39. m_pWMIFilterManager = pWMIFilterManager;
  40. m_bExpanded = true;
  41. m_lExpandedHeight = 0;
  42. m_pCurCWMIFilterContainer = NULL;
  43. m_bBrowsing = bBrowsing;
  44. m_bstrDomain = bstrDomain;
  45. m_dwColumnWidth[0] = NAME_WIDTH;
  46. m_dwColumnWidth[1] = DESCRIPTION_WIDTH;
  47. m_dwColumnWidth[2] = AUTHOR_WIDTH;
  48. m_dwColumnWidth[3] = CHANGE_DATE_WIDTH;
  49. m_dwColumnWidth[4] = CREATION_DATE_wIDTH;
  50. }
  51. //-------------------------------------------------------------------------
  52. CWMIFilterManagerDlg::~CWMIFilterManagerDlg()
  53. {
  54. long i;
  55. HRESULT hr;
  56. NTDM_BEGIN_METHOD()
  57. for(i=0; i<m_ArrayColumns.GetSize(); i++)
  58. {
  59. NTDM_DELETE_OBJECT(m_ArrayColumns[i]);
  60. }
  61. NTDM_END_METHOD()
  62. // cleanup
  63. }
  64. //-------------------------------------------------------------------------
  65. INT_PTR CALLBACK CWMIFilterManagerDlg::WMIFilterManagerDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  66. {
  67. LPPSHNOTIFY lppsn = NULL;
  68. HRESULT hr;
  69. switch(iMessage)
  70. {
  71. case WM_INITDIALOG:
  72. {
  73. m_hWnd = hDlg;
  74. InitializeDialog();
  75. PopulateFilterList();
  76. break;
  77. }
  78. case WM_KEYDOWN:
  79. {
  80. if(wParam == VK_F1)
  81. OnHelp();
  82. break;
  83. }
  84. case WM_DESTROY:
  85. {
  86. DestroyDialog();
  87. break;
  88. }
  89. case WM_CLOSE:
  90. {
  91. if(SUCCEEDED(hr=CheckDirtyFlags()))
  92. {
  93. EndDialog(m_hWnd, 0);
  94. }
  95. return 0;
  96. }
  97. case WM_COMMAND:
  98. {
  99. switch(LOWORD(wParam))
  100. {
  101. case IDCANCEL:
  102. if(SUCCEEDED(hr=CheckDirtyFlags()))
  103. {
  104. EndDialog(m_hWnd, 0);
  105. }
  106. return 0;
  107. case IDOK:
  108. OnOK();
  109. return TRUE;
  110. break;
  111. }
  112. if(BN_CLICKED == HIWORD(wParam) && IDC_NEW == LOWORD(wParam))
  113. {
  114. OnNew();
  115. return TRUE;
  116. }
  117. if(BN_CLICKED == HIWORD(wParam) && IDC_DELETE == LOWORD(wParam))
  118. {
  119. OnDelete();
  120. return TRUE;
  121. }
  122. if(BN_CLICKED == HIWORD(wParam) && IDC_DUPLICATE == LOWORD(wParam))
  123. {
  124. OnDuplicate();
  125. return TRUE;
  126. }
  127. if(BN_CLICKED == HIWORD(wParam) && IDC_ADVANCED == LOWORD(wParam))
  128. {
  129. ToggleExpandedMode();
  130. return TRUE;
  131. }
  132. if(BN_CLICKED == HIWORD(wParam) && IDC_HELP2 == LOWORD(wParam))
  133. {
  134. OnHelp();
  135. return TRUE;
  136. }
  137. if(BN_CLICKED == HIWORD(wParam) && IDC_SAVE == LOWORD(wParam))
  138. {
  139. SetFocus(GetDlgItem(hDlg, IDC_SAVE));
  140. OnSave();
  141. return TRUE;
  142. }
  143. if(BN_CLICKED == HIWORD(wParam) && IDC_IMPORT == LOWORD(wParam))
  144. {
  145. OnImport();
  146. return TRUE;
  147. }
  148. if(BN_CLICKED == HIWORD(wParam) && IDC_EXPORT == LOWORD(wParam))
  149. {
  150. OnExport();
  151. return TRUE;
  152. }
  153. if(BN_CLICKED == HIWORD(wParam) && IDC_COLUMNS == LOWORD(wParam))
  154. {
  155. OnManageColumns();
  156. return TRUE;
  157. }
  158. if(EN_KILLFOCUS == HIWORD(wParam) && (
  159. IDC_NAME == LOWORD(wParam) ||
  160. IDC_DESCRIPTION == LOWORD(wParam) ||
  161. IDC_QUERIES == LOWORD(wParam)
  162. ))
  163. {
  164. SaveToMemory();
  165. return TRUE;
  166. }
  167. if(EN_CHANGE == HIWORD(wParam) && (
  168. IDC_NAME == LOWORD(wParam) ||
  169. IDC_DESCRIPTION == LOWORD(wParam) ||
  170. IDC_QUERIES == LOWORD(wParam)
  171. ))
  172. {
  173. if(m_pCurCWMIFilterContainer)
  174. {
  175. m_pCurCWMIFilterContainer->SetDirtyFlag(true);
  176. m_pCurCWMIFilterContainer->SetMemoryDirtyFlag(true);
  177. }
  178. return TRUE;
  179. }
  180. break;
  181. }
  182. case WM_NOTIFY:
  183. {
  184. LPNMHDR lpnm = (LPNMHDR) lParam;
  185. if(IDC_WMI_FILTER_LIST == lpnm->idFrom)
  186. {
  187. switch (lpnm->code)
  188. {
  189. case LVN_ITEMCHANGED:
  190. {
  191. LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam;
  192. if(pnmv->uChanged & LVIF_STATE)
  193. {
  194. if(LVIS_SELECTED & pnmv->uNewState && !(LVIS_SELECTED & pnmv->uOldState))
  195. {
  196. SaveToMemory();
  197. if FAILED(SelectFilterItem(pnmv->iItem))
  198. return TRUE;
  199. else
  200. return FALSE;
  201. }
  202. }
  203. break;
  204. }
  205. default :
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. return FALSE;
  212. }
  213. //---------------------------------------------------------------------------
  214. STDMETHODIMP CWMIFilterManagerDlg::InitializeDialog()
  215. {
  216. HRESULT hr;
  217. LVCOLUMN lvColumn;
  218. CComBSTR bstrName;
  219. CComBSTR bstrQuery;
  220. CComBSTR bstrTemp;
  221. RECT rect;
  222. CColumnItem * pNewColumnItem;
  223. NTDM_BEGIN_METHOD()
  224. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_DESCRIPTION);
  225. pNewColumnItem = new CColumnItem(bstrTemp, _T("Description"), true);
  226. m_ArrayColumns.Add(pNewColumnItem);
  227. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_AUTHOR);
  228. pNewColumnItem = new CColumnItem(bstrTemp, _T("Author"), true);
  229. m_ArrayColumns.Add(pNewColumnItem);
  230. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_CHANGE_DATE);
  231. pNewColumnItem = new CColumnItem(bstrTemp, _T("ChangeDate"), true);
  232. m_ArrayColumns.Add(pNewColumnItem);
  233. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_CREATION_DATE);
  234. pNewColumnItem = new CColumnItem(bstrTemp, _T("CreationDate"), true);
  235. m_ArrayColumns.Add(pNewColumnItem);
  236. bstrName.LoadString(_Module.GetResourceInstance(), IDS_NAME);
  237. bstrQuery.LoadString(_Module.GetResourceInstance(), IDS_QUERY);
  238. GetWindowRect(m_hWnd, &rect);
  239. m_lExpandedHeight = rect.bottom - rect.top;
  240. ToggleExpandedMode();
  241. //Initialize the ListView Control
  242. m_hwndFilterListView = GetDlgItem(m_hWnd, IDC_WMI_FILTER_LIST);
  243. NTDM_ERR_IF_NULL(m_hwndFilterListView);
  244. ListView_SetExtendedListViewStyle(m_hwndFilterListView, LVS_EX_FULLROWSELECT);
  245. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  246. lvColumn.fmt = LVCFMT_LEFT;
  247. lvColumn.pszText = bstrName;
  248. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndFilterListView, 0, &lvColumn));
  249. NTDM_ERR_IF_FAIL(SetupColumns());
  250. NTDM_END_METHOD()
  251. // cleanup
  252. return hr;
  253. }
  254. //---------------------------------------------------------------------------
  255. STDMETHODIMP CWMIFilterManagerDlg::DestroyDialog()
  256. {
  257. HRESULT hr;
  258. NTDM_BEGIN_METHOD()
  259. NTDM_ERR_IF_FAIL(ClearFilterList());
  260. NTDM_END_METHOD()
  261. // cleanup
  262. return hr;
  263. }
  264. //---------------------------------------------------------------------------
  265. STDMETHODIMP CWMIFilterManagerDlg::ClearFilterList()
  266. {
  267. HRESULT hr;
  268. LVITEM lvItem;
  269. long lCount;
  270. NTDM_BEGIN_METHOD()
  271. //Release each item in the ListView Control
  272. lvItem.mask = LVIF_PARAM;
  273. lvItem.iSubItem = 0;
  274. lCount = ListView_GetItemCount(m_hwndFilterListView);
  275. while(lCount > 0)
  276. {
  277. lCount--;
  278. lvItem.iItem = lCount;
  279. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndFilterListView, &lvItem));
  280. if(lvItem.lParam)
  281. {
  282. CWMIFilterContainer * pCWMIFilterContainer = (CWMIFilterContainer *)lvItem.lParam;
  283. NTDM_DELETE_OBJECT(pCWMIFilterContainer);
  284. }
  285. }
  286. ListView_DeleteAllItems(m_hwndFilterListView);
  287. NTDM_END_METHOD()
  288. // cleanup
  289. return hr;
  290. }
  291. //---------------------------------------------------------------------------
  292. STDMETHODIMP CWMIFilterManagerDlg::PopulateFilterList()
  293. {
  294. HRESULT hr;
  295. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  296. CComBSTR bstrQueryLanguage(_T("WQL"));
  297. CComBSTR bstrQuery;
  298. ULONG uReturned;
  299. long i = 0;
  300. long lCount = 0;
  301. NTDM_BEGIN_METHOD()
  302. bstrQuery = _T("select * from MSFT_SomFilter where domain=\"");
  303. bstrQuery += m_bstrDomain;
  304. bstrQuery += _T("\"");
  305. if(!m_pWMIFilterManager->m_pIWbemServices)
  306. NTDM_EXIT(E_FAIL);
  307. NTDM_ERR_IF_FAIL(ClearFilterList());
  308. // Get the Enumeration
  309. // CComBSTR bstrClass(_T("MSFT_SomFilter"));
  310. // NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->CreateInstanceEnum(bstrClass, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  311. // Query for filters in provided domain
  312. NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->ExecQuery(bstrQueryLanguage, bstrQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  313. // Loop through each item in the enumeration and add it to the list
  314. while(pEnumWbemClassObject)
  315. {
  316. IWbemClassObject *pIWbemClassObject = NULL;
  317. NTDM_ERR_MSG_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, &pIWbemClassObject, &uReturned));
  318. if(!uReturned)
  319. break;
  320. // Add current Item to the list
  321. AddFilterItemToList(pIWbemClassObject);
  322. pIWbemClassObject->Release();
  323. lCount++;
  324. }
  325. // disable OK if 0 items
  326. //Also disable the name, description and query edit controls
  327. if(0 == lCount)
  328. {
  329. EnableWindow(GetDlgItem(m_hWnd, IDOK), FALSE);
  330. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), FALSE);
  331. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), FALSE);
  332. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), FALSE);
  333. }
  334. else
  335. {
  336. EnableWindow(GetDlgItem(m_hWnd, IDOK), TRUE);
  337. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), TRUE);
  338. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), TRUE);
  339. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), TRUE);
  340. }
  341. // auto size columns
  342. i = 0;
  343. while (true)
  344. {
  345. if (!ListView_SetColumnWidth(m_hwndFilterListView, i++, m_dwColumnWidth[i]))
  346. break;
  347. }
  348. ListView_SetItemState(m_hwndFilterListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  349. NTDM_END_METHOD()
  350. // cleanup
  351. return hr;
  352. }
  353. //---------------------------------------------------------------------------
  354. STDMETHODIMP CWMIFilterManagerDlg::AddFilterItemToList(IWbemClassObject * pIWbemClassObject, long lIndex, bool bSelect)
  355. {
  356. HRESULT hr;
  357. CComVariant vValue;
  358. CIMTYPE vType;
  359. LVITEM lvItem;
  360. CWMIFilterContainer * pFilterContainer = NULL;
  361. NTDM_BEGIN_METHOD()
  362. pFilterContainer = new CWMIFilterContainer();
  363. pFilterContainer->SetIWbemClassObject(pIWbemClassObject);
  364. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("Name"), 0, &vValue, &vType, NULL));
  365. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  366. lvItem.iItem = lIndex;
  367. lvItem.iSubItem = 0;
  368. lvItem.pszText = vValue.bstrVal;
  369. lvItem.lParam = (LPARAM)pFilterContainer;
  370. lIndex = ListView_InsertItem(m_hwndFilterListView, &lvItem);
  371. NTDM_ERR_IF_MINUSONE(lIndex);
  372. UpdateFilterItem(lIndex);
  373. if(bSelect)
  374. {
  375. ListView_SetItemState(m_hwndFilterListView, lIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  376. ListView_SetSelectionMark(m_hwndFilterListView, lIndex);
  377. }
  378. pIWbemClassObject->AddRef();
  379. NTDM_END_METHOD()
  380. // cleanup
  381. if FAILED(hr)
  382. NTDM_DELETE_OBJECT(pFilterContainer);
  383. return hr;
  384. }
  385. //---------------------------------------------------------------------------
  386. STDMETHODIMP CWMIFilterManagerDlg::UpdateFilterItem(long lIndex)
  387. {
  388. HRESULT hr;
  389. LVITEM lvItem;
  390. CComVariant vValue;
  391. CIMTYPE cimType;
  392. long i;
  393. long lCount = 0;
  394. NTDM_BEGIN_METHOD()
  395. //Release each item in the ListView Control
  396. lvItem.mask = LVIF_PARAM;
  397. lvItem.iItem = lIndex;
  398. lvItem.iSubItem = 0;
  399. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndFilterListView, &lvItem));
  400. if(lvItem.lParam)
  401. {
  402. CComPtr<IWbemClassObject>pIWbemClassObject;
  403. CWMIFilterContainer * pCWMIFilterContainer = (CWMIFilterContainer *)lvItem.lParam;
  404. NTDM_ERR_IF_FAIL(pCWMIFilterContainer->GetIWbemClassObject(&pIWbemClassObject));
  405. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("Name"), 0, &vValue, &cimType, NULL));
  406. ListView_SetItemText(m_hwndFilterListView, lIndex, 0, V_BSTR(&vValue));
  407. // for each selected item in the columns array add the property
  408. for(i=0; i<m_ArrayColumns.GetSize(); i++)
  409. {
  410. if(m_ArrayColumns[i]->IsSelected())
  411. {
  412. lCount++;
  413. vValue.Clear();
  414. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(m_ArrayColumns[i]->GetPropertyName(), 0, &vValue, &cimType, NULL));
  415. if(vValue.vt != VT_BSTR)
  416. continue;
  417. if(cimType != CIM_DATETIME)
  418. {
  419. ListView_SetItemText(m_hwndFilterListView, lIndex, lCount, V_BSTR(&vValue));
  420. }
  421. else
  422. {
  423. //convert to readable date
  424. CComBSTR bstrTemp;
  425. TCHAR *pszCur = V_BSTR(&vValue);
  426. TCHAR pszYear[5];
  427. TCHAR pszMonth[3];
  428. TCHAR pszDay[3];
  429. _tcsncpy(pszYear, pszCur, 4);
  430. pszYear[4] = 0;
  431. pszCur += 4;
  432. _tcsncpy(pszMonth, pszCur, 2);
  433. pszMonth[2] = 0;
  434. pszCur += 2;
  435. _tcsncpy(pszDay, pszCur, 2);
  436. pszDay[2] = 0;
  437. bstrTemp = pszMonth;
  438. bstrTemp += _T("\\");
  439. bstrTemp += pszDay;
  440. bstrTemp += _T("\\");
  441. bstrTemp += pszYear;
  442. vValue = bstrTemp;
  443. ListView_SetItemText(m_hwndFilterListView, lIndex, lCount, V_BSTR(&vValue));
  444. }
  445. }
  446. }
  447. }
  448. NTDM_END_METHOD()
  449. // cleanup
  450. return hr;
  451. }
  452. //---------------------------------------------------------------------------
  453. STDMETHODIMP CWMIFilterManagerDlg::OnDelete()
  454. {
  455. HRESULT hr;
  456. CComVariant vValue;
  457. CIMTYPE cimType;
  458. long lSelectionMark;
  459. LVITEM lvItem;
  460. long lCount;
  461. NTDM_BEGIN_METHOD()
  462. if(!m_pCurIWbemClassObj)
  463. {
  464. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  465. NTDM_EXIT(E_FAIL);
  466. }
  467. if(IDNO == CNTDMUtils::DisplayMessage(m_hWnd, IDS_CONFIRM_DELETE_FILTER, IDS_DELETE_FILTER, MB_YESNO|MB_ICONWARNING))
  468. {
  469. NTDM_EXIT(E_FAIL);
  470. }
  471. NTDM_ERR_MSG_IF_FAIL(m_pCurIWbemClassObj->Get(_T("__PATH"), 0, &vValue, &cimType, NULL));
  472. if(NOTEMPTY_BSTR_VARIANT(&vValue))
  473. {
  474. NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->DeleteInstance(V_BSTR(&vValue), 0, NULL, NULL));
  475. }
  476. else
  477. {
  478. // This item was a new item that was never saved so no need to remove it from the CIM
  479. }
  480. lSelectionMark = ListView_GetSelectionMark(m_hwndFilterListView);
  481. lvItem.iItem = lSelectionMark;
  482. lvItem.mask = LVIF_PARAM;
  483. lvItem.iSubItem = 0;
  484. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndFilterListView, &lvItem));
  485. if(lvItem.lParam)
  486. {
  487. CWMIFilterContainer * pCWMIFilterContainer = (CWMIFilterContainer *)lvItem.lParam;
  488. NTDM_DELETE_OBJECT(pCWMIFilterContainer);
  489. }
  490. ListView_DeleteItem(m_hwndFilterListView, lSelectionMark);
  491. m_pCurCWMIFilterContainer = NULL;
  492. m_pCurIWbemClassObj = NULL;
  493. ListView_SetItemState(m_hwndFilterListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  494. // disable OK if last item
  495. lCount = ListView_GetItemCount(m_hwndFilterListView);
  496. if(0 == lCount)
  497. {
  498. EnableWindow(GetDlgItem(m_hWnd, IDOK), FALSE);
  499. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), FALSE);
  500. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), FALSE);
  501. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), FALSE);
  502. }
  503. else
  504. {
  505. EnableWindow(GetDlgItem(m_hWnd, IDOK), TRUE);
  506. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), TRUE);
  507. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), TRUE);
  508. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), TRUE);
  509. }
  510. NTDM_END_METHOD()
  511. // cleanup
  512. return hr;
  513. }
  514. //---------------------------------------------------------------------------
  515. STDMETHODIMP CWMIFilterManagerDlg::OnNew()
  516. {
  517. HRESULT hr;
  518. CComPtr<IWbemClassObject>pIWbemClassObject;
  519. CComPtr<IWbemClassObject>pIWbemNewInstance;
  520. CComBSTR bstrTemp;
  521. CComVariant vValue;
  522. TCHAR pszTemp[100];
  523. GUID guid;
  524. DWORD nSize = 100;
  525. SYSTEMTIME systemTime;
  526. long lCount = 0;
  527. NTDM_BEGIN_METHOD()
  528. bstrTemp = _T("MSFT_SomFilter");
  529. NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->GetObject(bstrTemp, 0, NULL, &pIWbemClassObject, NULL));
  530. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->SpawnInstance(0, &pIWbemNewInstance));
  531. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_NEW_FILTER_NAME);
  532. vValue = bstrTemp;
  533. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Name"), 0, &vValue, CIM_STRING));
  534. NTDM_ERR_MSG_IF_FAIL(CoCreateGuid(&guid));
  535. StringFromGUID2(guid, pszTemp, 100);
  536. vValue = pszTemp;
  537. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("ID"), 0, &vValue, CIM_STRING));
  538. vValue = m_bstrDomain;
  539. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("domain"), 0, &vValue, CIM_STRING));
  540. // Set the user name
  541. pszTemp[0] = 0;
  542. if(GetUserName(pszTemp, &nSize) && _tcslen(pszTemp))
  543. {
  544. vValue = pszTemp;
  545. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Author"), 0, &vValue, CIM_STRING));
  546. }
  547. // Set the create and modified dates
  548. GetLocalTime(&systemTime);
  549. NTDM_ERR_GETLASTERROR_IF_NULL(GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systemTime, _T("yyyyMMdd000000.000000-000"), pszTemp, 100));
  550. if(_tcslen(pszTemp))
  551. {
  552. // Set the create date
  553. vValue = pszTemp;
  554. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("CreationDate"), 0, &vValue, CIM_DATETIME));
  555. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("ChangeDate"), 0, &vValue, CIM_DATETIME));
  556. }
  557. // Add current Item to the list
  558. AddFilterItemToList(pIWbemNewInstance, MAX_LIST_ITEMS, true);
  559. // set focus on the name edit box
  560. SetFocus(GetDlgItem(m_hWnd, IDC_NAME));
  561. NTDM_END_METHOD()
  562. // disable OK if last item
  563. lCount = ListView_GetItemCount(m_hwndFilterListView);
  564. if(0 == lCount)
  565. {
  566. EnableWindow(GetDlgItem(m_hWnd, IDOK), FALSE);
  567. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), FALSE);
  568. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), FALSE);
  569. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), FALSE);
  570. }
  571. else
  572. {
  573. EnableWindow(GetDlgItem(m_hWnd, IDOK), TRUE);
  574. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), TRUE);
  575. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), TRUE);
  576. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), TRUE);
  577. }
  578. // cleanup
  579. return hr;
  580. }
  581. //---------------------------------------------------------------------------
  582. STDMETHODIMP CWMIFilterManagerDlg::OnDuplicate()
  583. {
  584. HRESULT hr;
  585. CComPtr<IWbemClassObject>pIWbemNewInstance;
  586. CComVariant vValue;
  587. TCHAR pszTemp[100];
  588. GUID guid;
  589. CComBSTR bstrTemp;
  590. DWORD nSize = 100;
  591. SYSTEMTIME systemTime;
  592. NTDM_BEGIN_METHOD()
  593. if(!m_pCurIWbemClassObj)
  594. {
  595. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  596. NTDM_EXIT(E_FAIL);
  597. }
  598. NTDM_ERR_MSG_IF_FAIL(m_pCurIWbemClassObj->Clone(&pIWbemNewInstance));
  599. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_NEW_FILTER_NAME);
  600. vValue = bstrTemp;
  601. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Name"), 0, &vValue, CIM_STRING));
  602. NTDM_ERR_MSG_IF_FAIL(CoCreateGuid(&guid));
  603. StringFromGUID2(guid, pszTemp, 100);
  604. vValue = pszTemp;
  605. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("ID"), 0, &vValue, CIM_STRING));
  606. vValue = m_bstrDomain;
  607. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("domain"), 0, &vValue, CIM_STRING));
  608. // set the name
  609. pszTemp[0] = 0;
  610. if(GetUserName(pszTemp, &nSize) && _tcslen(pszTemp))
  611. {
  612. vValue = pszTemp;
  613. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Author"), 0, &vValue, CIM_STRING));
  614. }
  615. // Set the create and modified dates
  616. GetLocalTime(&systemTime);
  617. NTDM_ERR_GETLASTERROR_IF_NULL(GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systemTime, _T("yyyyMMdd000000.000000-000"), pszTemp, 100));
  618. if(_tcslen(pszTemp))
  619. {
  620. // Set the create date
  621. vValue = pszTemp;
  622. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("CreationDate"), 0, &vValue, CIM_DATETIME));
  623. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("ChangeDate"), 0, &vValue, CIM_DATETIME));
  624. }
  625. // Add current Item to the list
  626. AddFilterItemToList(pIWbemNewInstance, MAX_LIST_ITEMS, true);
  627. NTDM_END_METHOD()
  628. // cleanup
  629. return hr;
  630. }
  631. //---------------------------------------------------------------------------
  632. BOOL CWMIFilterManagerDlg::OnOK()
  633. {
  634. HRESULT hr;
  635. NTDM_BEGIN_METHOD()
  636. NTDM_ERR_IF_FAIL(CheckDirtyFlags());
  637. m_pCurCWMIFilterContainer = NULL;
  638. NTDM_ERR_IF_FAIL(GetSelectedFilter(&m_pCurCWMIFilterContainer));
  639. if (m_pCurCWMIFilterContainer->IsDirty())
  640. {
  641. m_pIWbemClassObject = NULL;
  642. }
  643. else
  644. {
  645. NTDM_ERR_IF_FAIL(m_pCurCWMIFilterContainer->GetIWbemClassObject(&m_pIWbemClassObject));
  646. }
  647. EndDialog(m_hWnd, IDOK);
  648. NTDM_END_METHOD()
  649. // cleanup
  650. return TRUE;
  651. }
  652. //---------------------------------------------------------------------------
  653. STDMETHODIMP CWMIFilterManagerDlg::CheckDirtyFlags()
  654. {
  655. HRESULT hr;
  656. LVITEM lvItem;
  657. bool bPrompt = true;
  658. long lCount;
  659. long lResult;
  660. NTDM_BEGIN_METHOD()
  661. //Release each item in the ListView Control
  662. lvItem.mask = LVIF_PARAM;
  663. lvItem.iSubItem = 0;
  664. lCount = ListView_GetItemCount(m_hwndFilterListView);
  665. while(lCount > 0)
  666. {
  667. lCount--;
  668. lvItem.iItem = lCount;
  669. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndFilterListView, &lvItem));
  670. if(lvItem.lParam)
  671. {
  672. CWMIFilterContainer * pCWMIFilterContainer = (CWMIFilterContainer *)lvItem.lParam;
  673. if(pCWMIFilterContainer->IsDirty())
  674. {
  675. if(bPrompt)
  676. {
  677. bPrompt = false;
  678. lResult = CNTDMUtils::DisplayMessage(m_hWnd, IDS_PROMPT_FOR_SAVE, IDS_WMI_FILTER_MANAGER, MB_YESNOCANCEL|MB_ICONQUESTION);
  679. if(IDNO == lResult)
  680. {
  681. NTDM_EXIT(NOERROR);
  682. }
  683. else if(IDCANCEL == lResult)
  684. {
  685. NTDM_EXIT(E_FAIL);
  686. }
  687. }
  688. // save the current piwbemobject
  689. NTDM_ERR_IF_FAIL(pCWMIFilterContainer->GetIWbemClassObject(&m_pCurIWbemClassObj));
  690. if FAILED(hr = m_pWMIFilterManager->m_pIWbemServices->PutInstance(m_pCurIWbemClassObj, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL))
  691. {
  692. ListView_SetItemState(m_hwndFilterListView, lCount, LVIS_SELECTED, LVIS_SELECTED);
  693. NTDM_ERR_MSG_IF_FAIL(hr);
  694. }
  695. pCWMIFilterContainer->SetDirtyFlag(false);
  696. }
  697. }
  698. }
  699. NTDM_END_METHOD()
  700. // cleanup
  701. return hr;
  702. }
  703. //---------------------------------------------------------------------------
  704. STDMETHODIMP CWMIFilterManagerDlg::OnManageColumns()
  705. {
  706. HRESULT hr;
  707. long lCount;
  708. NTDM_BEGIN_METHOD()
  709. g_pColumnManagerDlg = new CColumnManagerDlg(&m_ArrayColumns);
  710. if(IDOK == DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_COLUMN_MANAGER), (HWND)m_hWnd, ColumnManagerDlgProc))
  711. {
  712. SetupColumns();
  713. lCount = ListView_GetItemCount(m_hwndFilterListView);
  714. while(lCount > 0)
  715. {
  716. lCount--;
  717. UpdateFilterItem(lCount);
  718. }
  719. }
  720. NTDM_END_METHOD()
  721. // cleanup
  722. return hr;
  723. }
  724. //---------------------------------------------------------------------------
  725. STDMETHODIMP CWMIFilterManagerDlg::SetupColumns()
  726. {
  727. HRESULT hr;
  728. HWND hwndHeader = NULL;
  729. long i = 0;
  730. long lCount = 0;
  731. int iColumn;
  732. LVCOLUMN lvColumn;
  733. DWORD dwListWidth[MAX_WMIFILTER_COLUMNS];
  734. NTDM_BEGIN_METHOD()
  735. iColumn = 1;
  736. for (i = 0; i < m_ArrayColumns.GetSize(); i++)
  737. {
  738. CColumnItem *pCColumnItem = m_ArrayColumns[i];
  739. if ( pCColumnItem->IsPrevSelected() )
  740. {
  741. //
  742. // The first element of m_dwColumnWidth width of "Name"
  743. // column, and the array m_ArrayColumn contains columns after
  744. // "Name" column only
  745. //
  746. m_dwColumnWidth[i+1] = ListView_GetColumnWidth( m_hwndFilterListView,iColumn++);
  747. }
  748. }
  749. // Delete all the columns but the 1st one.
  750. while(ListView_DeleteColumn(m_hwndFilterListView, 1));
  751. for (i=0; i<m_ArrayColumns.GetSize(); i++)
  752. {
  753. CColumnItem * pCColumnItem = m_ArrayColumns[i];
  754. pCColumnItem->SetPrevSelected(pCColumnItem->IsSelected() );
  755. if(!pCColumnItem->IsSelected())
  756. continue;
  757. lCount++;
  758. dwListWidth[lCount] = m_dwColumnWidth[i+1];
  759. // Add all the selected columns
  760. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  761. lvColumn.fmt = LVCFMT_LEFT;
  762. lvColumn.pszText = (LPTSTR)pCColumnItem->GetName();
  763. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndFilterListView, lCount, &lvColumn));
  764. }
  765. for (i=1; i<=lCount; i++)
  766. {
  767. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndFilterListView, i, dwListWidth[i]));
  768. }
  769. NTDM_END_METHOD()
  770. // cleanup
  771. return hr;
  772. }
  773. //---------------------------------------------------------------------------
  774. STDMETHODIMP CWMIFilterManagerDlg::GetSelectedFilter(CWMIFilterContainer ** ppCWMIFilterContainer, long lIndex)
  775. {
  776. HRESULT hr;
  777. long lSelectionMark;
  778. NTDM_BEGIN_METHOD()
  779. if(-1 == lIndex)
  780. {
  781. lSelectionMark = ListView_GetSelectionMark(m_hwndFilterListView);
  782. }
  783. else
  784. {
  785. lSelectionMark = lIndex;
  786. }
  787. if(-1 == lSelectionMark)
  788. {
  789. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  790. NTDM_EXIT(E_FAIL);
  791. }
  792. else
  793. {
  794. // get a pointer to the IWbemClassObject
  795. LVITEM lvItem;
  796. lvItem.mask = LVIF_PARAM;
  797. lvItem.iSubItem = 0;
  798. lvItem.iItem = lSelectionMark;
  799. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndFilterListView, &lvItem));
  800. if(lvItem.lParam)
  801. {
  802. CComPtr<IWbemClassObject>pIWbemClassObject;
  803. CWMIFilterContainer * pCWMIFilterContainer;
  804. *ppCWMIFilterContainer = (CWMIFilterContainer *)lvItem.lParam;
  805. }
  806. else
  807. {
  808. NTDM_EXIT(E_FAIL);
  809. }
  810. }
  811. NTDM_END_METHOD()
  812. // cleanup
  813. return hr;
  814. }
  815. //---------------------------------------------------------------------------
  816. STDMETHODIMP CWMIFilterManagerDlg::OnExport()
  817. {
  818. HRESULT hr;
  819. CComBSTR bstrTemp;
  820. CComBSTR bstrFilter;
  821. BYTE byteUnicodeHeader1 = 0xFF;
  822. BYTE byteUnicodeHeader2 = 0xFE;
  823. CComBSTR bstrNamespace = _T("#pragma namespace(\"\\\\\\\\.\\\\root\\\\policy\")");
  824. TCHAR pszFile[MAX_PATH];
  825. CComBSTR bstrObjectText;
  826. HANDLE hFile = NULL;
  827. DWORD dwWritten=0;
  828. pszFile[0] = 0;
  829. NTDM_BEGIN_METHOD()
  830. if(!m_pCurIWbemClassObj)
  831. NTDM_EXIT(E_FAIL);
  832. BOOL bLoadedResources = FALSE;
  833. bLoadedResources = ( 0 != bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ALL_FILES_FILTER) );
  834. if ( bLoadedResources )
  835. {
  836. bLoadedResources = ( 0 != bstrFilter.LoadString(_Module.GetResourceInstance(), IDS_MOF_FILES_FILTER) );
  837. }
  838. if ( ! bLoadedResources )
  839. {
  840. NTDM_EXIT(HRESULT_FROM_WIN32(GetLastError()));
  841. }
  842. bstrFilter += bstrTemp;
  843. CNTDMUtils::ReplaceCharacter(bstrFilter, L'@', L'\0');
  844. if(CNTDMUtils::SaveFileNameDlg(bstrFilter, _T("*.mof"), m_hWnd, pszFile))
  845. {
  846. if(_tcslen(pszFile))
  847. {
  848. // check if the file already exists
  849. WIN32_FIND_DATA FindFileData;
  850. HANDLE hFind;
  851. hFind = FindFirstFile(pszFile, &FindFileData);
  852. if (hFind != INVALID_HANDLE_VALUE)
  853. {
  854. FindClose(hFind);
  855. if(IDYES != CNTDMUtils::DisplayMessage(m_hWnd, IDS_WARN_OVERWRITE, IDS_WMI_FILTER_MANAGER, MB_YESNO|MB_ICONQUESTION))
  856. {
  857. NTDM_EXIT(S_FALSE);
  858. }
  859. }
  860. NTDM_ERR_MSG_IF_FAIL(m_pCurIWbemClassObj->GetObjectText(0, &bstrObjectText));
  861. // save to pszFile
  862. hFile = CreateFile(pszFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  863. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  864. if(hFile == INVALID_HANDLE_VALUE)
  865. {
  866. NTDM_ERR_GETLASTERROR_IF_NULL(NULL);
  867. goto error;
  868. }
  869. if(hFile)
  870. {
  871. NTDM_ERR_GETLASTERROR_IF_NULL(WriteFile(hFile, &byteUnicodeHeader1, sizeof(BYTE), &dwWritten, NULL));
  872. NTDM_ERR_GETLASTERROR_IF_NULL(WriteFile(hFile, &byteUnicodeHeader2, sizeof(BYTE), &dwWritten, NULL));
  873. NTDM_ERR_GETLASTERROR_IF_NULL(WriteFile(hFile, bstrNamespace, _tcslen(bstrNamespace) * sizeof(TCHAR), &dwWritten, NULL));
  874. NTDM_ERR_GETLASTERROR_IF_NULL(WriteFile(hFile, bstrObjectText, _tcslen(bstrObjectText) * sizeof(TCHAR), &dwWritten, NULL));
  875. NTDM_ERR_GETLASTERROR_IF_NULL(CloseHandle(hFile));
  876. hFile = NULL;
  877. }
  878. }
  879. }
  880. NTDM_END_METHOD()
  881. // cleanup
  882. if(hFile)
  883. {
  884. CloseHandle(hFile);
  885. hFile = NULL;
  886. }
  887. return hr;
  888. }
  889. //---------------------------------------------------------------------------
  890. STDMETHODIMP CWMIFilterManagerDlg::OnImport()
  891. {
  892. HRESULT hr;
  893. CComPtr<IMofCompiler>pIMofCompiler;
  894. CComBSTR bstrTemp;
  895. CComBSTR bstrFilter;
  896. TCHAR pszFile[MAX_PATH];
  897. pszFile[0] = 0;
  898. WBEM_COMPILE_STATUS_INFO pInfo;
  899. NTDM_BEGIN_METHOD()
  900. NTDM_ERR_IF_FAIL(CheckDirtyFlags());
  901. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ALL_FILES_FILTER);
  902. bstrFilter.LoadString(_Module.GetResourceInstance(), IDS_MOF_FILES_FILTER);
  903. bstrFilter += bstrTemp;
  904. CNTDMUtils::ReplaceCharacter(bstrFilter, L'@', L'\0');
  905. if(CNTDMUtils::OpenFileNameDlg(bstrFilter, _T("*.mof"), m_hWnd, pszFile))
  906. {
  907. if(_tcslen(pszFile))
  908. {
  909. NTDM_ERR_MSG_IF_FAIL(CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (void **)&pIMofCompiler));
  910. NTDM_ERR_MSG_IF_FAIL(pIMofCompiler->CompileFile(pszFile, NULL, NULL, NULL, NULL, 0, 0, 0, &pInfo));
  911. // check if pInfo is ok
  912. if(pInfo.lPhaseError != 0)
  913. {
  914. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_STORING_DATA);
  915. }
  916. NTDM_ERR_IF_FAIL(PopulateFilterList());
  917. }
  918. }
  919. NTDM_END_METHOD()
  920. // cleanup
  921. return hr;
  922. }
  923. //---------------------------------------------------------------------------
  924. STDMETHODIMP CWMIFilterManagerDlg::ToggleExpandedMode()
  925. {
  926. HRESULT hr;
  927. RECT rect;
  928. CComBSTR bstrTemp;
  929. long lCount = 0;
  930. NTDM_BEGIN_METHOD()
  931. if(m_bExpanded)
  932. {
  933. GetWindowRect(m_hWnd, &rect);
  934. SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, rect.right-rect.left, 240, SWP_NOZORDER|SWP_NOREPOSITION|SWP_NOMOVE);
  935. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_DETAILS2);
  936. SetDlgItemText(m_hWnd, IDC_ADVANCED, bstrTemp);
  937. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_NEW, false);
  938. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DELETE, false);
  939. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DUPLICATE, false);
  940. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_IMPORT, false);
  941. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_EXPORT, false);
  942. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_NAME, false);
  943. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DESCRIPTION, false);
  944. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_QUERIES, false);
  945. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_SAVE, false);
  946. m_bExpanded = false;
  947. }
  948. else
  949. {
  950. GetWindowRect(m_hWnd, &rect);
  951. SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, rect.right-rect.left, m_lExpandedHeight, SWP_NOZORDER|SWP_NOREPOSITION|SWP_NOMOVE);
  952. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_DETAILS1);
  953. SetDlgItemText(m_hWnd, IDC_ADVANCED, bstrTemp);
  954. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_NEW, true);
  955. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DELETE, true);
  956. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DUPLICATE, true);
  957. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_IMPORT, true);
  958. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_EXPORT, true);
  959. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_NAME, true);
  960. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_DESCRIPTION, true);
  961. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_QUERIES, true);
  962. CNTDMUtils::DisplayDlgItem(m_hWnd, IDC_SAVE, true);
  963. lCount = ListView_GetItemCount(m_hwndFilterListView);
  964. //If there are no items, make sure the name, description and query controls are disabled.
  965. if (0 == lCount)
  966. {
  967. EnableWindow(GetDlgItem(m_hWnd, IDC_NAME), FALSE);
  968. EnableWindow(GetDlgItem(m_hWnd, IDC_DESCRIPTION), FALSE);
  969. EnableWindow(GetDlgItem(m_hWnd, IDC_QUERIES), FALSE);
  970. }
  971. m_bExpanded = true;
  972. }
  973. NTDM_END_METHOD()
  974. // cleanup
  975. return hr;
  976. }
  977. //---------------------------------------------------------------------------
  978. STDMETHODIMP CWMIFilterManagerDlg::SelectFilterItem(long lIndex)
  979. {
  980. HRESULT hr;
  981. NTDM_BEGIN_METHOD()
  982. bool bDirtyFlag = false, bMemoryDirtyFlag = false;
  983. m_pCurIWbemClassObj = NULL;
  984. m_pCurCWMIFilterContainer = NULL;
  985. NTDM_ERR_IF_FAIL(GetSelectedFilter(&m_pCurCWMIFilterContainer, lIndex));
  986. NTDM_ERR_IF_FAIL(m_pCurCWMIFilterContainer->GetIWbemClassObject(&m_pCurIWbemClassObj));
  987. bDirtyFlag = m_pCurCWMIFilterContainer->IsDirty();
  988. bMemoryDirtyFlag = m_pCurCWMIFilterContainer->IsMemoryDirty();
  989. NTDM_ERR_IF_FAIL(CNTDMUtils::GetStringProperty(m_pCurIWbemClassObj, _T("Name"), m_hWnd, IDC_NAME));
  990. NTDM_ERR_IF_FAIL(CNTDMUtils::GetStringProperty(m_pCurIWbemClassObj, _T("Description"), m_hWnd, IDC_DESCRIPTION));
  991. NTDM_ERR_IF_FAIL(PopulateQueryEdit());
  992. m_pCurCWMIFilterContainer->SetDirtyFlag(bDirtyFlag);
  993. m_pCurCWMIFilterContainer->SetMemoryDirtyFlag(bMemoryDirtyFlag);
  994. NTDM_END_METHOD()
  995. // cleanup
  996. return hr;
  997. }
  998. //---------------------------------------------------------------------------
  999. STDMETHODIMP CWMIFilterManagerDlg::PopulateQueryEdit()
  1000. {
  1001. HRESULT hr;
  1002. CComVariant vValue;
  1003. SAFEARRAY *psaRules = NULL;
  1004. long lLower, lUpper, i;
  1005. CIMTYPE cimType;
  1006. CComBSTR bstrQueries;
  1007. NTDM_BEGIN_METHOD()
  1008. NTDM_ERR_MSG_IF_FAIL(m_pCurIWbemClassObj->Get(_T("Rules"), 0, &vValue, &cimType, NULL));
  1009. if(VT_NULL != V_VT(&vValue))
  1010. {
  1011. // Set the Rules property
  1012. psaRules = V_ARRAY(&vValue);
  1013. NTDM_ERR_MSG_IF_FAIL(SafeArrayGetUBound(psaRules, 1, &lUpper));
  1014. NTDM_ERR_MSG_IF_FAIL(SafeArrayGetLBound(psaRules, 1, &lLower));
  1015. for(i=lLower; i<=lUpper; i++)
  1016. {
  1017. if(V_VT(&vValue) & VT_UNKNOWN)
  1018. {
  1019. // Rules or UNKNOWNS (i.e. IWbemClassObjects)
  1020. CComPtr<IUnknown>pUnk;
  1021. CComPtr<IWbemClassObject> pIWbemClassObject;
  1022. NTDM_ERR_MSG_IF_FAIL(SafeArrayGetElement(psaRules, &i, (void *)&pUnk));
  1023. NTDM_ERR_MSG_IF_FAIL(pUnk->QueryInterface(IID_IWbemClassObject, (void **)&pIWbemClassObject));
  1024. // Show Properties of this object
  1025. if(i!= lLower)
  1026. {
  1027. bstrQueries += _T("\r\n\r\n");
  1028. }
  1029. NTDM_ERR_IF_FAIL(AddQueryItemToString(pIWbemClassObject, bstrQueries));
  1030. }
  1031. }
  1032. }
  1033. SendDlgItemMessage(m_hWnd, IDC_QUERIES, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)bstrQueries);
  1034. NTDM_END_METHOD()
  1035. // cleanup
  1036. return hr;
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. STDMETHODIMP CWMIFilterManagerDlg::AddQueryItemToString(IWbemClassObject * pIWbemClassObject, CComBSTR& bstrQueries)
  1040. {
  1041. HRESULT hr;
  1042. CComVariant vValue;
  1043. CIMTYPE cimType;
  1044. NTDM_BEGIN_METHOD()
  1045. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("TargetNameSpace"), 0, &vValue, &cimType, NULL));
  1046. if(vValue.bstrVal && _tcscmp(_T("root\\cimv2"), vValue.bstrVal) != 0)
  1047. {
  1048. bstrQueries += vValue.bstrVal;
  1049. bstrQueries +=";";
  1050. }
  1051. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(_T("Query"), 0, &vValue, &cimType, NULL));
  1052. bstrQueries += vValue.bstrVal;
  1053. NTDM_END_METHOD()
  1054. // cleanup
  1055. return hr;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. STDMETHODIMP CWMIFilterManagerDlg::SaveQueryEdit()
  1059. {
  1060. HRESULT hr;
  1061. VARIANT vValue;
  1062. SAFEARRAY *psaRules = NULL;
  1063. SAFEARRAYBOUND rgsaBound[1];
  1064. long rgIndices[1];
  1065. long i;
  1066. long lCount = 0;
  1067. CSimpleArray<BSTR>bstrArray;
  1068. CComBSTR bstrTemp;
  1069. NTDM_BEGIN_METHOD()
  1070. VariantInit(&vValue);
  1071. //Split out the string into an array of query strings
  1072. NTDM_ERR_IF_FAIL(CNTDMUtils::GetDlgItemString(m_hWnd, IDC_QUERIES, bstrTemp));
  1073. NTDM_ERR_IF_FAIL(CNTDMUtils::GetValuesInList(bstrTemp, bstrArray, _T("\r\n\r\n")));
  1074. // Get the size of the array
  1075. lCount = bstrArray.GetSize();
  1076. if(lCount)
  1077. {
  1078. rgsaBound[0].lLbound = 0;
  1079. rgsaBound[0].cElements = lCount;
  1080. psaRules = SafeArrayCreate(VT_UNKNOWN, 1, rgsaBound);
  1081. for(i=0; i<lCount; i++)
  1082. {
  1083. CComPtr<IUnknown>pUnk;
  1084. CSimpleArray<BSTR>bstrQueryDetails;
  1085. NTDM_ERR_IF_FAIL(CNTDMUtils::GetValuesInList(bstrArray[i], bstrQueryDetails, _T(";")));
  1086. if(bstrQueryDetails.GetSize() > 1)
  1087. {
  1088. NTDM_ERR_IF_FAIL(AddEditQueryString(bstrQueryDetails[1], (void**)&pUnk, bstrQueryDetails[0]));
  1089. }
  1090. else
  1091. {
  1092. NTDM_ERR_IF_FAIL(AddEditQueryString(bstrQueryDetails[0], (void**)&pUnk));
  1093. }
  1094. rgIndices[0] = i;
  1095. NTDM_ERR_MSG_IF_FAIL(SafeArrayPutElement(psaRules, rgIndices, pUnk));
  1096. }
  1097. VariantClear(&vValue);
  1098. V_VT(&vValue) = VT_ARRAY|VT_UNKNOWN;
  1099. V_ARRAY(&vValue) = psaRules;
  1100. }
  1101. else
  1102. {
  1103. VariantClear(&vValue);
  1104. V_VT(&vValue) = VT_NULL;
  1105. }
  1106. NTDM_ERR_MSG_IF_FAIL(m_pCurIWbemClassObj->Put(_T("Rules"), 0, &vValue, CIM_FLAG_ARRAY|CIM_OBJECT));
  1107. hr = S_OK;
  1108. NTDM_END_METHOD()
  1109. // cleanup
  1110. VariantClear(&vValue);
  1111. return hr;
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. STDMETHODIMP CWMIFilterManagerDlg::AddEditQueryString(BSTR bstrQuery, void **ppUnk, BSTR bstrNamespace)
  1115. {
  1116. HRESULT hr;
  1117. CComPtr<IWbemClassObject>pIWbemClassObject;
  1118. CComPtr<IWbemClassObject>pIWbemNewInstance;
  1119. CComBSTR bstrTemp;
  1120. CComVariant vValueTemp;
  1121. NTDM_BEGIN_METHOD()
  1122. bstrTemp = _T("MSFT_Rule");
  1123. NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->GetObject(bstrTemp, 0, NULL, &pIWbemClassObject, NULL));
  1124. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->SpawnInstance(0, &pIWbemNewInstance));
  1125. vValueTemp = _T("WQL");
  1126. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("QueryLanguage"), 0, &vValueTemp, CIM_STRING));
  1127. if(!bstrNamespace)
  1128. vValueTemp = _T("root\\cimv2");
  1129. else
  1130. vValueTemp = bstrNamespace;
  1131. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("TargetNameSpace"), 0, &vValueTemp, CIM_STRING));
  1132. vValueTemp = bstrQuery;
  1133. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->Put(_T("Query"), 0, &vValueTemp, CIM_STRING));
  1134. NTDM_ERR_MSG_IF_FAIL(pIWbemNewInstance->QueryInterface(IID_IUnknown, ppUnk));
  1135. NTDM_END_METHOD()
  1136. // cleanup
  1137. return hr;
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. STDMETHODIMP CWMIFilterManagerDlg::OnSave()
  1141. {
  1142. HRESULT hr;
  1143. NTDM_BEGIN_METHOD()
  1144. if(!m_pCurIWbemClassObj)
  1145. NTDM_EXIT(E_FAIL);
  1146. // save the current piwbemobject
  1147. NTDM_ERR_MSG_IF_FAIL(m_pWMIFilterManager->m_pIWbemServices->PutInstance(m_pCurIWbemClassObj, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL));
  1148. m_pCurCWMIFilterContainer->SetDirtyFlag(false);
  1149. NTDM_END_METHOD()
  1150. // cleanup
  1151. return hr;
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. STDMETHODIMP CWMIFilterManagerDlg::OnHelp()
  1155. {
  1156. HRESULT hr;
  1157. NTDM_BEGIN_METHOD()
  1158. HWND hwnd = HtmlHelp(NULL, _T("wmifltr.chm"), 0, 0);
  1159. if(!hwnd)
  1160. {
  1161. CNTDMUtils::DisplayMessage(m_hWnd, IDS_HELP_ERR);
  1162. }
  1163. NTDM_END_METHOD()
  1164. // cleanup
  1165. return hr;
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. STDMETHODIMP CWMIFilterManagerDlg::SaveToMemory()
  1169. {
  1170. HRESULT hr;
  1171. CComBSTR bstrTemp;
  1172. long lSelectionMark;
  1173. CComVariant vValue;
  1174. NTDM_BEGIN_METHOD()
  1175. if(m_pCurCWMIFilterContainer && m_pCurCWMIFilterContainer->IsMemoryDirty())
  1176. {
  1177. NTDM_ERR_IF_FAIL(CNTDMUtils::SetStringProperty(m_pCurIWbemClassObj, _T("Name"), m_hWnd, IDC_NAME));
  1178. NTDM_ERR_IF_FAIL(CNTDMUtils::SetStringProperty(m_pCurIWbemClassObj, _T("Description"), m_hWnd, IDC_DESCRIPTION));
  1179. NTDM_ERR_IF_FAIL(SaveQueryEdit());
  1180. lSelectionMark = ListView_GetSelectionMark(m_hwndFilterListView);
  1181. UpdateFilterItem(lSelectionMark);
  1182. m_pCurCWMIFilterContainer->SetMemoryDirtyFlag(false);
  1183. }
  1184. NTDM_END_METHOD()
  1185. // cleanup
  1186. return hr;
  1187. }