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.

475 lines
14 KiB

  1. // DPWmiPolledQueryPage.cpp : implementation file
  2. //
  3. // 03/05/00 v-marfin bug 59643 : Make this the default starting page.
  4. // 03/29/00 v-marfin bug 62585 : Set new Data collector's ENABLED to TRUE if user presses OK.
  5. // 03/30/00 v-marfin bug 59237 : If user does not change the default name of the data
  6. // collector when they first create it, change it for
  7. // them to a more meaningful name based on the data
  8. // they select in the property pages.
  9. #include "stdafx.h"
  10. #include "snapin.h"
  11. #include "DPWmiPolledQueryPage.h"
  12. #include "HMObject.h"
  13. #include "WmiBrowseDlg.h"
  14. #include "WmiPropertyBrowseDlg.h"
  15. #include "DataElement.h"
  16. #include "DataGroupScopeItem.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CDPWmiPolledQueryPage property page
  24. IMPLEMENT_DYNCREATE(CDPWmiPolledQueryPage, CHMPropertyPage)
  25. CDPWmiPolledQueryPage::CDPWmiPolledQueryPage() : CHMPropertyPage(CDPWmiPolledQueryPage::IDD)
  26. {
  27. //{{AFX_DATA_INIT(CDPWmiPolledQueryPage)
  28. m_sClass = _T("");
  29. m_sNamespace = _T("");
  30. m_sQuery = _T("");
  31. m_bRequireReset = FALSE;
  32. //}}AFX_DATA_INIT
  33. m_sHelpTopic = _T("HMon21.chm::/dDEquery.htm");
  34. }
  35. CDPWmiPolledQueryPage::~CDPWmiPolledQueryPage()
  36. {
  37. }
  38. inline void CDPWmiPolledQueryPage::ConstructQuery()
  39. {
  40. m_sQuery.Format(_T("SELECT * FROM %s"), m_sClass.IsEmpty() ? _T("<<your_class_name_here>>") : m_sClass);
  41. GetDlgItem(IDC_EDIT_QUERY)->SetWindowText(m_sQuery);
  42. }
  43. void CDPWmiPolledQueryPage::UpdateProperties()
  44. {
  45. m_Properties.DeleteAllItems();
  46. if( m_sClass.IsEmpty() )
  47. {
  48. return;
  49. }
  50. CWbemClassObject classobject;
  51. classobject.SetNamespace(_T("\\\\") + GetObjectPtr()->GetSystemName() + _T("\\") + m_sNamespace);
  52. HRESULT hr = classobject.GetObject(m_sClass);
  53. if( hr != S_OK )
  54. {
  55. return;
  56. }
  57. CStringArray saNames;
  58. classobject.GetPropertyNames(saNames);
  59. classobject.Destroy();
  60. for( int i = 0; i < saNames.GetSize(); i++ )
  61. {
  62. m_Properties.InsertItem(0,saNames[i]);
  63. }
  64. m_Properties.SetColumnWidth(0,LVSCW_AUTOSIZE);
  65. }
  66. void CDPWmiPolledQueryPage::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CHMPropertyPage::DoDataExchange(pDX);
  69. //{{AFX_DATA_MAP(CDPWmiPolledQueryPage)
  70. DDX_Control(pDX, IDC_LIST_PROPERTIES, m_Properties);
  71. DDX_Text(pDX, IDC_EDIT_CLASS, m_sClass);
  72. DDX_Text(pDX, IDC_EDIT_NAMESPACE, m_sNamespace);
  73. DDX_Text(pDX, IDC_EDIT_QUERY, m_sQuery);
  74. DDX_Check(pDX, IDC_CHECK_REQUIRE_RESET, m_bRequireReset);
  75. //}}AFX_DATA_MAP
  76. }
  77. BEGIN_MESSAGE_MAP(CDPWmiPolledQueryPage, CHMPropertyPage)
  78. //{{AFX_MSG_MAP(CDPWmiPolledQueryPage)
  79. ON_WM_DESTROY()
  80. ON_BN_CLICKED(IDC_BUTTON_BROWSE_NAMESPACE, OnButtonBrowseNamespace)
  81. ON_BN_CLICKED(IDC_BUTTON_BROWSE_CLASS, OnButtonBrowseClass)
  82. ON_EN_CHANGE(IDC_EDIT_CLASS, OnChangeEditClass)
  83. ON_EN_CHANGE(IDC_EDIT_NAMESPACE, OnChangeEditNamespace)
  84. ON_EN_CHANGE(IDC_EDIT_QUERY, OnChangeEditQuery)
  85. ON_BN_CLICKED(IDC_CHECK_REQUIRE_RESET, OnCheckRequireReset)
  86. ON_NOTIFY(NM_CLICK, IDC_LIST_PROPERTIES, OnClickListProperties)
  87. //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CDPWmiPolledQueryPage message handlers
  91. BOOL CDPWmiPolledQueryPage::OnInitDialog()
  92. {
  93. // v-marfin : bug 59643 : This will be the default starting page for the property
  94. // sheet so call CnxPropertyPageCreate() to unmarshal the
  95. // connection for this thread. This function must be called
  96. // by the first page of the property sheet. It used to
  97. // be called by the "General" page and its call still remains
  98. // there as well in case the general page is loaded by a
  99. // different code path that does not also load this page.
  100. // The CnxPropertyPageCreate function has been safeguarded
  101. // to simply return if the required call has already been made.
  102. // CnxPropertyPageDestory() must be called from this page's
  103. // OnDestroy function.
  104. // unmarshal connmgr
  105. CnxPropertyPageCreate();
  106. CHMPropertyPage::OnInitDialog();
  107. // initialize the list view
  108. m_Properties.SetExtendedStyle(LVS_EX_CHECKBOXES);
  109. CString sColumnTitle;
  110. sColumnTitle.LoadString(IDS_STRING_NAME);
  111. m_Properties.InsertColumn(0,sColumnTitle);
  112. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  113. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  114. {
  115. return TRUE;
  116. }
  117. //-------------------------------------------------------------------------
  118. // v-marfin 59237 : Store original name in case this data collector is
  119. // just being created. When they save, we will modify the
  120. // name if they haven't.
  121. pClassObject->GetProperty(IDS_STRING_MOF_NAME,m_sOriginalName);
  122. //-------------------------------------------------------------------------
  123. pClassObject->GetProperty(IDS_STRING_MOF_TARGETNAMESPACE,m_sNamespace);
  124. bool bReset;
  125. pClassObject->GetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  126. m_bRequireReset = bReset;
  127. pClassObject->GetProperty(IDS_STRING_MOF_QUERY,m_sQuery);
  128. m_sQuery.MakeUpper();
  129. int iIndex = m_sQuery.Find(_T("SELECT * FROM "));
  130. if( iIndex != -1 )
  131. {
  132. m_sClass = m_sQuery.Right(m_sQuery.GetLength()-iIndex-14);
  133. iIndex = m_sClass.Find(_T(" "));
  134. if( iIndex != -1 )
  135. {
  136. m_sClass = m_sClass.Left(iIndex);
  137. }
  138. }
  139. UpdateProperties();
  140. CStringArray saProperties;
  141. pClassObject->GetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saProperties);
  142. for( int i=0; i < saProperties.GetSize(); i++ )
  143. {
  144. LVFINDINFO lvfi;
  145. ZeroMemory(&lvfi,sizeof(LVFINDINFO));
  146. lvfi.flags = LVFI_WRAP|LVFI_STRING;
  147. lvfi.psz = saProperties[i];
  148. int iListIndex = m_Properties.FindItem(&lvfi);
  149. if( iListIndex >= 0 )
  150. {
  151. m_Properties.SetCheck(iListIndex);
  152. }
  153. }
  154. delete pClassObject;
  155. UpdateData(FALSE);
  156. return TRUE; // return TRUE unless you set the focus to a control
  157. // EXCEPTION: OCX Property Pages should return FALSE
  158. }
  159. void CDPWmiPolledQueryPage::OnDestroy()
  160. {
  161. // v-marfin 62585 : For this new data collector, set its Enabled property to TRUE, but
  162. // only if the user is not cancelling these property pages.
  163. if (m_bOnApplyUsed)
  164. {
  165. ClearStatistics(); // 62548
  166. CDataElement* pElement = (CDataElement*)GetObjectPtr();
  167. if (pElement && pElement->IsStateSetToEnabledOnOK())
  168. {
  169. TRACE(_T("CDPWmiPolledQueryPage::OnDestroy - New Perfmon Collector: Setting to Enabled\n"));
  170. pElement->SetStateToEnabledOnOK(FALSE); // don't do this again
  171. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  172. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  173. {
  174. TRACE(_T("ERROR: CDPWmiPolledQueryPage::OnDestroy - Failed to GetClassObject()\n"));
  175. return;
  176. }
  177. // Set the new collector to enabled.
  178. BOOL bEnabled=TRUE;
  179. HRESULT hr = pClassObject->GetProperty(IDS_STRING_MOF_ENABLE,bEnabled);
  180. hr = pClassObject->SetProperty(IDS_STRING_MOF_ENABLE,TRUE);
  181. if (!CHECKHRESULT(hr))
  182. {
  183. TRACE(_T("ERROR: CDPWmiPolledQueryPage::OnDestroy - Failed to set ENABLED property on new collector\n"));
  184. }
  185. //-------------------------------------------------------------------
  186. // v-marfin 59237 : If the user has not changed the original default
  187. // name, do so for them. Compare against original
  188. // name we fetched during OnInitDialog.
  189. CString sName;
  190. pClassObject->GetProperty(IDS_STRING_MOF_NAME,sName);
  191. // Did the user change the default name?
  192. if (m_sOriginalName.CompareNoCase(sName)==0)
  193. {
  194. // No, so set the new name
  195. if (!m_sQuery.IsEmpty())
  196. {
  197. // Use parent to ensure name is unique
  198. //CDataGroup* pParent = (CDataGroup*) pElement->GetCollectorsParentClassObject();
  199. if(pElement->GetScopeItemCount())
  200. {
  201. CDataElementScopeItem* pItem = (CDataElementScopeItem*)pElement->GetScopeItem(0);
  202. if( pItem )
  203. {
  204. CDataGroupScopeItem* pDGItem = (CDataGroupScopeItem*)pItem->GetParent();
  205. sName = pDGItem->GetUniqueDisplayName(m_sQuery);
  206. }
  207. }
  208. // Set the local element's object data
  209. pElement->SetName(sName);
  210. // Set its WMI property
  211. pClassObject->SetProperty(IDS_STRING_MOF_NAME,sName);
  212. // Refresh to show the new name in the IU
  213. //pElement->Refresh(); // 63005
  214. pElement->UpdateStatus(); // 63005
  215. }
  216. }
  217. //-------------------------------------------------------------------
  218. pClassObject->SaveAllProperties();
  219. delete pClassObject;
  220. } // if (pElement && pElement->IsStateSetToEnabledOnOK())
  221. } // if (m_bOnApplyUsed)
  222. CHMPropertyPage::OnDestroy();
  223. // v-marfin : bug 59643 : CnxPropertyPageDestory() must be called from this page's
  224. // OnDestroy function.
  225. CnxPropertyPageDestroy();
  226. }
  227. void CDPWmiPolledQueryPage::OnOK()
  228. {
  229. CHMPropertyPage::OnOK();
  230. }
  231. void CDPWmiPolledQueryPage::OnButtonBrowseNamespace()
  232. {
  233. CWmiNamespaceBrowseDlg dlg;
  234. // set the dialog window title
  235. CString sItem;
  236. sItem.LoadString(IDS_STRING_NAMESPACES);
  237. dlg.m_sDlgTitle.Format(IDS_STRING_WMI_BROWSE_TITLE,sItem,GetObjectPtr()->GetSystemName());
  238. // create the namespace enumerator
  239. if( ! CHECKHRESULT(dlg.m_ClassObject.Create(GetObjectPtr()->GetSystemName())) )
  240. {
  241. return;
  242. }
  243. if( m_sNamespace.IsEmpty() )
  244. {
  245. m_sNamespace.Format(IDS_STRING_MOF_NAMESPACE_FORMAT,GetObjectPtr()->GetSystemName());
  246. }
  247. dlg.m_sTitle.Format(_T("\\\\%s\\%s"),GetObjectPtr()->GetSystemName(),m_sNamespace);
  248. dlg.m_ClassObject.SetNamespace(dlg.m_sTitle);
  249. // set the listbox title
  250. dlg.m_sTitle = m_sNamespace;
  251. // display dialog
  252. if( dlg.DoModal() == IDOK )
  253. {
  254. m_sNamespace = dlg.m_sSelectedItem;
  255. m_sClass.Empty();
  256. UpdateData(FALSE);
  257. UpdateProperties();
  258. SetModified();
  259. ConstructQuery();
  260. }
  261. }
  262. void CDPWmiPolledQueryPage::OnButtonBrowseClass()
  263. {
  264. CWmiClassBrowseDlg dlg;
  265. // set the dialog window title
  266. CString sItem;
  267. sItem.LoadString(IDS_STRING_CLASSES);
  268. dlg.m_sDlgTitle.Format(IDS_STRING_WMI_BROWSE_TITLE,sItem,GetObjectPtr()->GetSystemName());
  269. // create the class enumerator
  270. if( ! CHECKHRESULT(dlg.m_ClassObject.Create(GetObjectPtr()->GetSystemName())) )
  271. {
  272. return;
  273. }
  274. CString sTemp;
  275. sTemp.Format(_T("\\\\%s\\%s"),GetObjectPtr()->GetSystemName(),m_sNamespace);
  276. dlg.m_ClassObject.SetNamespace(sTemp);
  277. if( ! CHECKHRESULT(dlg.m_ClassObject.CreateClassEnumerator(NULL)) )
  278. {
  279. return;
  280. }
  281. // set the listbox title
  282. dlg.m_sTitle = m_sNamespace;
  283. // display the dialog
  284. if( dlg.DoModal() == IDOK )
  285. {
  286. m_sClass = dlg.m_sSelectedItem;
  287. UpdateData(FALSE);
  288. UpdateProperties();
  289. SetModified();
  290. ConstructQuery();
  291. }
  292. }
  293. void CDPWmiPolledQueryPage::OnChangeEditClass()
  294. {
  295. // TODO: If this is a RICHEDIT control, the control will not
  296. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  297. // function and call CRichEditCtrl().SetEventMask()
  298. // with the ENM_CHANGE flag ORed into the mask.
  299. UpdateData();
  300. UpdateProperties();
  301. SetModified();
  302. ConstructQuery();
  303. }
  304. void CDPWmiPolledQueryPage::OnChangeEditNamespace()
  305. {
  306. // TODO: If this is a RICHEDIT control, the control will not
  307. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  308. // function and call CRichEditCtrl().SetEventMask()
  309. // with the ENM_CHANGE flag ORed into the mask.
  310. if( m_sNamespace.Find(_T("\\\\")) != -1 )
  311. {
  312. AfxMessageBox(IDS_STRING_NO_REMOTE_NAMESPACES);
  313. GetDlgItem(IDC_EDIT_NAMESPACE)->SetWindowText(_T(""));
  314. }
  315. GetDlgItem(IDC_EDIT_CLASS)->SetWindowText(_T(""));
  316. UpdateData();
  317. UpdateProperties();
  318. SetModified();
  319. ConstructQuery();
  320. }
  321. void CDPWmiPolledQueryPage::OnChangeEditQuery()
  322. {
  323. // TODO: If this is a RICHEDIT control, the control will not
  324. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  325. // function and call CRichEditCtrl().SetEventMask()
  326. // with the ENM_CHANGE flag ORed into the mask.
  327. UpdateData();
  328. SetModified();
  329. }
  330. BOOL CDPWmiPolledQueryPage::OnApply()
  331. {
  332. // v-marfin 62585 : So we can set the collector's state to enabled when OK pressed.
  333. m_bOnApplyUsed=TRUE;
  334. if( ! CHMPropertyPage::OnApply() )
  335. {
  336. return FALSE;
  337. }
  338. UpdateData();
  339. CStringArray saProperties;
  340. for( int i = 0; i < m_Properties.GetItemCount(); i++ )
  341. {
  342. if( m_Properties.GetCheck(i) )
  343. {
  344. saProperties.Add(m_Properties.GetItemText(i,0));
  345. }
  346. }
  347. // if( ! saProperties.GetSize() )
  348. // {
  349. // return FALSE;
  350. // }
  351. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  352. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  353. {
  354. return FALSE;
  355. }
  356. pClassObject->SetProperty(IDS_STRING_MOF_TARGETNAMESPACE,m_sNamespace);
  357. bool bReset = m_bRequireReset ? true : false;
  358. pClassObject->SetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  359. pClassObject->SetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saProperties);
  360. pClassObject->SetProperty(IDS_STRING_MOF_QUERY,m_sQuery);
  361. pClassObject->SaveAllProperties();
  362. delete pClassObject;
  363. SetModified(FALSE);
  364. return TRUE;
  365. }
  366. void CDPWmiPolledQueryPage::OnCheckRequireReset()
  367. {
  368. UpdateData();
  369. SetModified();
  370. }
  371. void CDPWmiPolledQueryPage::OnClickListProperties(NMHDR* pNMHDR, LRESULT* pResult)
  372. {
  373. SetModified();
  374. *pResult = 0;
  375. }