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.

551 lines
16 KiB

  1. // DPFileInfoPage.cpp : implementation file
  2. //
  3. // 03/05/00 v-marfin bug 59643 : Make this the default starting page.
  4. // 03/23/00 v-marfin bug 61680 : escape and unescape special chars.
  5. // 03/28/00 v-marfin 61030 Change Browse for file dialog to fix default extension.
  6. // 03/29/00 v-marfin 62585 Set new Data collector's ENABLED to TRUE if user presses OK.
  7. // 03/30/00 v-marfin bug 59237 : If user does not change the default name of the data
  8. // collector when they first create it, change it for
  9. // them to a more meaningful name based on the data
  10. // they select in the property pages.
  11. #include "stdafx.h"
  12. #include "snapin.h"
  13. #include "DPFileInfoPage.h"
  14. #include "HMObject.h"
  15. #include "WmiBrowseDlg.h"
  16. #include "WmiPropertyBrowseDlg.h"
  17. #include "DataElement.h"
  18. #include "DataGroupScopeItem.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDPFileInfoPage property page
  26. IMPLEMENT_DYNCREATE(CDPFileInfoPage, CHMPropertyPage)
  27. CDPFileInfoPage::CDPFileInfoPage() : CHMPropertyPage(CDPFileInfoPage::IDD)
  28. {
  29. //{{AFX_DATA_INIT(CDPFileInfoPage)
  30. m_bRequireReset = FALSE;
  31. m_sFile = _T("");
  32. m_sFolder = _T("");
  33. m_iType = 0;
  34. //}}AFX_DATA_INIT
  35. m_sHelpTopic = _T("HMon21.chm::/dDEfile.htm");
  36. }
  37. CDPFileInfoPage::~CDPFileInfoPage()
  38. {
  39. }
  40. void CDPFileInfoPage::UpdateProperties(CListCtrl& Properties, const CString& sNamespace, const CString& sClass)
  41. {
  42. Properties.DeleteAllItems();
  43. if( sClass.IsEmpty() )
  44. {
  45. return;
  46. }
  47. CWbemClassObject classobject;
  48. classobject.SetNamespace(_T("\\\\") + GetObjectPtr()->GetSystemName() + _T("\\") + sNamespace);
  49. HRESULT hr = classobject.GetObject(sClass);
  50. if( hr != S_OK )
  51. {
  52. return;
  53. }
  54. CStringArray saNames;
  55. classobject.GetPropertyNames(saNames);
  56. classobject.Destroy();
  57. for( int i = 0; i < saNames.GetSize(); i++ )
  58. {
  59. Properties.InsertItem(0,saNames[i]);
  60. }
  61. Properties.SetColumnWidth(0,LVSCW_AUTOSIZE);
  62. }
  63. void CDPFileInfoPage::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CHMPropertyPage::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CDPFileInfoPage)
  67. DDX_Control(pDX, IDC_LIST_FOLDER_PROPERTIES, m_FolderProperties);
  68. DDX_Control(pDX, IDC_LIST_FILE_PROPERTIES, m_FileProperties);
  69. DDX_Check(pDX, IDC_CHECK_REQUIRE_RESET, m_bRequireReset);
  70. DDX_Text(pDX, IDC_EDIT_FILE, m_sFile);
  71. DDX_Text(pDX, IDC_EDIT_FOLDER, m_sFolder);
  72. DDX_Radio(pDX, IDC_RADIO1, m_iType);
  73. //}}AFX_DATA_MAP
  74. if( m_iType == 0 )
  75. {
  76. GetDlgItem(IDC_EDIT_FOLDER)->EnableWindow();
  77. GetDlgItem(IDC_BUTTON_BROWSE_FOLDER)->EnableWindow();
  78. GetDlgItem(IDC_LIST_FOLDER_PROPERTIES)->EnableWindow();
  79. GetDlgItem(IDC_BUTTON_BROWSE_FILE)->EnableWindow(FALSE);
  80. GetDlgItem(IDC_EDIT_FILE)->EnableWindow(FALSE);
  81. GetDlgItem(IDC_LIST_FILE_PROPERTIES)->EnableWindow(FALSE);
  82. }
  83. else if( m_iType == 1 )
  84. {
  85. GetDlgItem(IDC_EDIT_FOLDER)->EnableWindow(FALSE);
  86. GetDlgItem(IDC_BUTTON_BROWSE_FOLDER)->EnableWindow(FALSE);
  87. GetDlgItem(IDC_LIST_FOLDER_PROPERTIES)->EnableWindow(FALSE);
  88. GetDlgItem(IDC_BUTTON_BROWSE_FILE)->EnableWindow();
  89. GetDlgItem(IDC_EDIT_FILE)->EnableWindow();
  90. GetDlgItem(IDC_LIST_FILE_PROPERTIES)->EnableWindow();
  91. }
  92. }
  93. BEGIN_MESSAGE_MAP(CDPFileInfoPage, CHMPropertyPage)
  94. //{{AFX_MSG_MAP(CDPFileInfoPage)
  95. ON_BN_CLICKED(IDC_BUTTON_BROWSE_FILE, OnButtonBrowseFile)
  96. ON_BN_CLICKED(IDC_BUTTON_BROWSE_FOLDER, OnButtonBrowseFolder)
  97. ON_BN_CLICKED(IDC_CHECK_REQUIRE_RESET, OnCheckRequireReset)
  98. ON_EN_CHANGE(IDC_EDIT_FILE, OnChangeEditFile)
  99. ON_EN_CHANGE(IDC_EDIT_FOLDER, OnChangeEditFolder)
  100. ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
  101. ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
  102. ON_NOTIFY(NM_CLICK, IDC_LIST_FILE_PROPERTIES, OnClickListFileProperties)
  103. ON_NOTIFY(NM_CLICK, IDC_LIST_FOLDER_PROPERTIES, OnClickListFolderProperties)
  104. ON_WM_DESTROY()
  105. //}}AFX_MSG_MAP
  106. END_MESSAGE_MAP()
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CDPFileInfoPage message handlers
  109. BOOL CDPFileInfoPage::OnInitDialog()
  110. {
  111. // v-marfin : bug 59643 : This will be the default starting page for the property
  112. // sheet so call CnxPropertyPageCreate() to unmarshal the
  113. // connection for this thread. This function must be called
  114. // by the first page of the property sheet. It used to
  115. // be called by the "General" page and its call still remains
  116. // there as well in case the general page is loaded by a
  117. // different code path that does not also load this page.
  118. // The CnxPropertyPageCreate function has been safeguarded
  119. // to simply return if the required call has already been made.
  120. // CnxPropertyPageDestory() must be called from this page's
  121. // OnDestroy function.
  122. // unmarshal connmgr
  123. CnxPropertyPageCreate();
  124. CHMPropertyPage::OnInitDialog();
  125. // initialize the list view
  126. m_FileProperties.SetExtendedStyle(LVS_EX_CHECKBOXES);
  127. m_FolderProperties.SetExtendedStyle(LVS_EX_CHECKBOXES);
  128. CString sColumnTitle;
  129. sColumnTitle.LoadString(IDS_STRING_NAME);
  130. m_FileProperties.InsertColumn(0,sColumnTitle);
  131. m_FolderProperties.InsertColumn(0,sColumnTitle);
  132. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  133. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  134. {
  135. return TRUE;
  136. }
  137. UpdateProperties(m_FileProperties,_T("root\\cimv2"),_T("CIM_DATAFILE"));
  138. UpdateProperties(m_FolderProperties,_T("root\\cimv2"),_T("WIN32_DIRECTORY"));
  139. //-------------------------------------------------------------------------
  140. // v-marfin 59237 : Store original name in case this data collector is
  141. // just being created. When they save, we will modify the
  142. // name if they haven't.
  143. pClassObject->GetProperty(IDS_STRING_MOF_NAME,m_sOriginalName);
  144. //-------------------------------------------------------------------------
  145. CString sQuery;
  146. pClassObject->GetProperty(IDS_STRING_MOF_QUERY,sQuery);
  147. sQuery.MakeUpper();
  148. int iIndex = -1;
  149. CSnapInApp* pApp = (CSnapInApp*) AfxGetApp();
  150. if( (iIndex = sQuery.Find(_T("WIN32_DIRECTORY"))) != -1 )
  151. {
  152. CString sDrive;
  153. CString sPath;
  154. CWbemClassObject::GetPropertyValueFromString(sQuery,_T("NAME"),m_sFolder);
  155. // v-marfin 61680 : unescape special chars
  156. m_sFolder = pApp->UnEscapeSpecialChars(m_sFolder);
  157. m_iType = 0;
  158. }
  159. else if( (iIndex = sQuery.Find(_T("CIM_DATAFILE"))) != -1 )
  160. {
  161. CWbemClassObject::GetPropertyValueFromString(sQuery,_T("NAME"),m_sFile);
  162. // v-marfin 61680 : unescape special chars
  163. m_sFile = pApp->UnEscapeSpecialChars(m_sFile);
  164. m_iType = 1;
  165. }
  166. CStringArray saProperties;
  167. CString sProperties;
  168. pClassObject->GetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saProperties);
  169. if( m_iType == 0 )
  170. {
  171. for( int i=0; i < saProperties.GetSize(); i++ )
  172. {
  173. LVFINDINFO lvfi;
  174. ZeroMemory(&lvfi,sizeof(LVFINDINFO));
  175. lvfi.flags = LVFI_WRAP|LVFI_STRING;
  176. lvfi.psz = saProperties[i];
  177. int iListIndex = m_FolderProperties.FindItem(&lvfi);
  178. if( iListIndex >= 0 )
  179. {
  180. m_FolderProperties.SetCheck(iListIndex);
  181. }
  182. }
  183. }
  184. else if( m_iType == 1 )
  185. {
  186. for( int i=0; i < saProperties.GetSize(); i++ )
  187. {
  188. LVFINDINFO lvfi;
  189. ZeroMemory(&lvfi,sizeof(LVFINDINFO));
  190. lvfi.flags = LVFI_WRAP|LVFI_STRING;
  191. lvfi.psz = saProperties[i];
  192. int iListIndex = m_FileProperties.FindItem(&lvfi);
  193. if( iListIndex >= 0 )
  194. {
  195. m_FileProperties.SetCheck(iListIndex);
  196. }
  197. }
  198. }
  199. bool bReset;
  200. pClassObject->GetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  201. m_bRequireReset = bReset;
  202. delete pClassObject;
  203. UpdateData(FALSE);
  204. return TRUE; // return TRUE unless you set the focus to a control
  205. // EXCEPTION: OCX Property Pages should return FALSE
  206. }
  207. BOOL CDPFileInfoPage::OnApply()
  208. {
  209. if( ! CHMPropertyPage::OnApply() )
  210. {
  211. return FALSE;
  212. }
  213. // v-marfin 62585 : So we can set the collector's state to enabled when OK pressed.
  214. m_bOnApplyUsed=TRUE;
  215. UpdateData();
  216. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  217. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  218. {
  219. return FALSE;
  220. }
  221. CStringArray saCounters;
  222. if( m_iType == 0 )
  223. {
  224. if( m_FolderProperties.GetItemCount() <= 0 )
  225. {
  226. return FALSE;
  227. }
  228. for( int i = 0; i < m_FolderProperties.GetItemCount(); i++ )
  229. {
  230. if( m_FolderProperties.GetCheck(i) )
  231. {
  232. saCounters.Add(m_FolderProperties.GetItemText(i,0));
  233. }
  234. }
  235. }
  236. else if( m_iType == 1 )
  237. {
  238. if( m_FileProperties.GetItemCount() <= 0 )
  239. {
  240. return FALSE;
  241. }
  242. for( int i = 0; i < m_FileProperties.GetItemCount(); i++ )
  243. {
  244. if( m_FileProperties.GetCheck(i) )
  245. {
  246. saCounters.Add(m_FileProperties.GetItemText(i,0));
  247. }
  248. }
  249. }
  250. // if( ! saCounters.GetSize() )
  251. // {
  252. // return FALSE;
  253. // }
  254. CString sNamespace = _T("root\\cimv2");
  255. pClassObject->SetProperty(IDS_STRING_MOF_TARGETNAMESPACE,sNamespace);
  256. CSnapInApp* pApp = (CSnapInApp*) AfxGetApp();
  257. CString sQuery;
  258. if( m_iType == 0 )
  259. {
  260. // v-marfin: 61680 escape backslashes
  261. CString sFolder = pApp->EscapeSpecialChars(m_sFolder);
  262. sQuery.Format(_T("select * from Win32_Directory where Name=\"%s\""),sFolder);
  263. pClassObject->SetProperty(IDS_STRING_MOF_QUERY,sQuery);
  264. }
  265. else if( m_iType == 1 )
  266. {
  267. // v-marfin: 61680 escape backslashes
  268. CString sFile = pApp->EscapeSpecialChars(m_sFile);
  269. sQuery.Format(_T("select * from CIM_DataFile where Name=\"%s\""),sFile);
  270. pClassObject->SetProperty(IDS_STRING_MOF_QUERY,sQuery);
  271. }
  272. pClassObject->SetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saCounters);
  273. bool bReset = m_bRequireReset ? true : false;
  274. pClassObject->SetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  275. pClassObject->SaveAllProperties();
  276. delete pClassObject;
  277. SetModified(FALSE);
  278. return TRUE;
  279. }
  280. void CDPFileInfoPage::OnButtonBrowseFile()
  281. {
  282. CString sFilter;
  283. CString sTitle;
  284. sFilter.LoadString(IDS_STRING_FILTER);
  285. sTitle.LoadString(IDS_STRING_BROWSE_FILE);
  286. // v-marfin 61030 Change Browse for file dialog to fix default extension
  287. // CFileDialog fdlg(TRUE,_T("*.*"),NULL,OFN_FILEMUSTEXIST|OFN_SHAREAWARE|OFN_HIDEREADONLY,sFilter);
  288. CFileDialog fdlg(TRUE, // Is FILEOPEN dialog?
  289. NULL, // default extension if no extension provided
  290. NULL, // initial filename
  291. OFN_FILEMUSTEXIST|OFN_SHAREAWARE|OFN_HIDEREADONLY, // flags
  292. sFilter); // filter
  293. fdlg.m_ofn.lpstrTitle = sTitle;
  294. if( fdlg.DoModal() == IDOK )
  295. {
  296. m_sFile = fdlg.GetPathName();
  297. UpdateData(FALSE);
  298. SetModified();
  299. }
  300. }
  301. void CDPFileInfoPage::OnButtonBrowseFolder()
  302. {
  303. LPMALLOC pMalloc;
  304. if( ::SHGetMalloc(&pMalloc) == NOERROR )
  305. {
  306. BROWSEINFO bi;
  307. TCHAR szBuffer[MAX_PATH];
  308. LPITEMIDLIST pidlDesktop;
  309. LPITEMIDLIST pidl;
  310. if( ::SHGetSpecialFolderLocation(GetSafeHwnd(),CSIDL_DESKTOP,&pidlDesktop) != NOERROR )
  311. return;
  312. CString sResString;
  313. sResString.LoadString(IDS_STRING_BROWSE_FOLDER);
  314. bi.hwndOwner = GetSafeHwnd();
  315. bi.pidlRoot = pidlDesktop;
  316. bi.pszDisplayName = szBuffer;
  317. bi.lpszTitle = LPCTSTR(sResString);
  318. bi.ulFlags = BIF_RETURNONLYFSDIRS;
  319. bi.lpfn = NULL;
  320. bi.lParam = 0;
  321. if( (pidl = ::SHBrowseForFolder(&bi)) != NULL )
  322. {
  323. if (SUCCEEDED(::SHGetPathFromIDList(pidl, szBuffer)))
  324. {
  325. m_sFolder = szBuffer;
  326. UpdateData(FALSE);
  327. SetModified();
  328. }
  329. pMalloc->Free(pidl);
  330. }
  331. pMalloc->Free(pidlDesktop);
  332. pMalloc->Release();
  333. }
  334. }
  335. void CDPFileInfoPage::OnCheckRequireReset()
  336. {
  337. SetModified();
  338. }
  339. void CDPFileInfoPage::OnChangeEditFile()
  340. {
  341. // TODO: If this is a RICHEDIT control, the control will not
  342. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  343. // function and call CRichEditCtrl().SetEventMask()
  344. // with the ENM_CHANGE flag ORed into the mask.
  345. SetModified();
  346. }
  347. void CDPFileInfoPage::OnChangeEditFolder()
  348. {
  349. // TODO: If this is a RICHEDIT control, the control will not
  350. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  351. // function and call CRichEditCtrl().SetEventMask()
  352. // with the ENM_CHANGE flag ORed into the mask.
  353. SetModified();
  354. }
  355. void CDPFileInfoPage::OnRadio1()
  356. {
  357. UpdateData();
  358. }
  359. void CDPFileInfoPage::OnRadio2()
  360. {
  361. UpdateData();
  362. }
  363. void CDPFileInfoPage::OnClickListFileProperties(NMHDR* pNMHDR, LRESULT* pResult)
  364. {
  365. SetModified();
  366. *pResult = 0;
  367. }
  368. void CDPFileInfoPage::OnClickListFolderProperties(NMHDR* pNMHDR, LRESULT* pResult)
  369. {
  370. SetModified();
  371. *pResult = 0;
  372. }
  373. void CDPFileInfoPage::OnDestroy()
  374. {
  375. // v-marfin 62585 : For this new data collector, set its Enabled property to TRUE, but
  376. // only if the user is not cancelling these property pages.
  377. if (m_bOnApplyUsed)
  378. {
  379. ClearStatistics(); // 62548
  380. CDataElement* pElement = (CDataElement*)GetObjectPtr();
  381. if (pElement && pElement->IsStateSetToEnabledOnOK())
  382. {
  383. TRACE(_T("CDPFileInfoPage::OnDestroy - New Perfmon Collector: Setting to Enabled\n"));
  384. pElement->SetStateToEnabledOnOK(FALSE); // don't do this again
  385. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  386. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  387. {
  388. TRACE(_T("ERROR: CDPPerfMonPage::OnOK - Failed to GetClassObject()\n"));
  389. return;
  390. }
  391. // Set the new collector to enabled.
  392. BOOL bEnabled=TRUE;
  393. HRESULT hr = pClassObject->GetProperty(IDS_STRING_MOF_ENABLE,bEnabled);
  394. hr = pClassObject->SetProperty(IDS_STRING_MOF_ENABLE,TRUE);
  395. if (!CHECKHRESULT(hr))
  396. {
  397. TRACE(_T("ERROR: CDPFileInfoPage::OnDestroy - Failed to set ENABLED property on new collector\n"));
  398. }
  399. //-------------------------------------------------------------------
  400. // v-marfin 59237 : If the user has not changed the original default
  401. // name, do so for them. Compare against original
  402. // name we fetched during OnInitDialog.
  403. CString sName;
  404. pClassObject->GetProperty(IDS_STRING_MOF_NAME,sName);
  405. // Did the user change the default name?
  406. if (m_sOriginalName.CompareNoCase(sName)==0)
  407. {
  408. CString sObject = m_sFile.IsEmpty() ? m_sFolder : m_sFile;
  409. // No, so set the new name
  410. if (!sObject.IsEmpty())
  411. {
  412. // Use parent to ensure name is unique
  413. //CDataGroup* pParent = (CDataGroup*) pElement->GetCollectorsParentClassObject();
  414. if(pElement->GetScopeItemCount())
  415. {
  416. CDataElementScopeItem* pItem = (CDataElementScopeItem*)pElement->GetScopeItem(0);
  417. if( pItem )
  418. {
  419. CDataGroupScopeItem* pDGItem = (CDataGroupScopeItem*)pItem->GetParent();
  420. sName = pDGItem->GetUniqueDisplayName(sObject);
  421. }
  422. }
  423. // Set the local element's object data
  424. pElement->SetName(sName);
  425. // Set its WMI property
  426. pClassObject->SetProperty(IDS_STRING_MOF_NAME,sName);
  427. // Refresh to show the new name in the IU
  428. //pElement->Refresh(); // 63005
  429. pElement->UpdateStatus(); // 63005
  430. }
  431. }
  432. //-------------------------------------------------------------------
  433. pClassObject->SaveAllProperties();
  434. delete pClassObject;
  435. } // if (pElement && pElement->IsStateSetToEnabledOnOK())
  436. } // if (m_bOnApplyUsed)
  437. CHMPropertyPage::OnDestroy();
  438. // v-marfin : bug 59643 : CnxPropertyPageDestory() must be called from this page's
  439. // OnDestroy function.
  440. CnxPropertyPageDestroy();
  441. }