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.

305 lines
8.9 KiB

  1. //=============================================================================
  2. // The CDataSource class encapsulates a data source, which might be live data
  3. // from WMI, data saved in an NFO file, or data from XML.
  4. //=============================================================================
  5. #pragma once
  6. #include "category.h"
  7. #include "refreshthread.h"
  8. #include "xmlobject.h"
  9. extern CMSInfoHistoryCategory catHistorySystemSummary;
  10. //-----------------------------------------------------------------------------
  11. // CDataSource is a base class for the actual data sources.
  12. //-----------------------------------------------------------------------------
  13. class CDataSource
  14. {
  15. public:
  16. CDataSource() : m_pRoot(NULL), m_fStaticTree(FALSE) {};
  17. virtual ~CDataSource()
  18. {
  19. if (m_pRoot)
  20. {
  21. RemoveTree(m_pRoot);
  22. m_pRoot = NULL;
  23. }
  24. };
  25. virtual CMSInfoCategory * GetRootCategory()
  26. {
  27. return m_pRoot;
  28. };
  29. virtual HTREEITEM GetNodeByName(LPCTSTR szName, CMSInfoCategory * pLookFrom = NULL)
  30. {
  31. CMSInfoCategory * pRoot = pLookFrom;
  32. if (pRoot == NULL)
  33. pRoot = GetRootCategory();
  34. if (pRoot)
  35. {
  36. CString strName;
  37. pRoot->GetNames(NULL, &strName);
  38. if (strName.CompareNoCase(CString(szName)) == 0)
  39. return pRoot->GetHTREEITEM();
  40. }
  41. for (CMSInfoCategory * pChild = pRoot->GetFirstChild(); pChild;)
  42. {
  43. HTREEITEM hti = GetNodeByName(szName, pChild);
  44. if (hti)
  45. return hti;
  46. pChild = pChild->GetNextSibling();
  47. }
  48. return NULL;
  49. };
  50. protected:
  51. void RemoveTree(CMSInfoCategory * pCategory)
  52. {
  53. if (pCategory)
  54. {
  55. for (CMSInfoCategory * pChild = pCategory->GetFirstChild(); pChild;)
  56. {
  57. CMSInfoCategory * pNext = pChild->GetNextSibling();
  58. RemoveTree(pChild);
  59. pChild = pNext;
  60. }
  61. // If the tree is static, then don't actually delete, just reset
  62. // some state variables (possibly).
  63. if (m_fStaticTree)
  64. pCategory->ResetCategory();
  65. else
  66. delete pCategory;
  67. }
  68. }
  69. CMSInfoCategory * m_pRoot; // root of the category tree
  70. BOOL m_fStaticTree; // the tree shouldn't be deleted
  71. };
  72. //-----------------------------------------------------------------------------
  73. // CLiveDataSource provides current system information from WMI.
  74. //-----------------------------------------------------------------------------
  75. class CLiveDataSource : public CDataSource
  76. {
  77. public:
  78. CLiveDataSource();
  79. virtual ~CLiveDataSource();
  80. virtual HRESULT Create(LPCTSTR szMachine, HWND hwnd, LPCTSTR szFilter = NULL);
  81. virtual void LockData() { if (m_pThread) m_pThread->EnterCriticalSection(); };
  82. virtual void UnlockData() { if (m_pThread) m_pThread->LeaveCriticalSection(); };
  83. virtual void WaitForRefresh() { if (m_pThread) m_pThread->WaitForRefresh(); };
  84. virtual BOOL InRefresh() { if (m_pThread) return m_pThread->IsRefreshing(); return FALSE; };
  85. void SetMachineForCategories(CMSInfoLiveCategory * pCategory);
  86. virtual CMSInfoCategory * GetRootCategory() { return ((m_iDeltaIndex == -1) ? m_pRoot : m_pHistoryRoot); };
  87. virtual BOOL GetDeltaList(CStringList * pstrlist);
  88. virtual BOOL ShowDeltas(int iDeltaIndex);
  89. virtual HRESULT ValidDataSource();
  90. private:
  91. void AddExtensions();
  92. void GetExtensionSet(CStringList & strlistExtensions);
  93. void ConvertVersion5Categories(CMapWordToPtr & mapVersion5Categories, DWORD dwRootID, CMSInfoLiveCategory * m_pRoot);
  94. CMSInfoLiveCategory * GetNodeByName(const CString & strSearch, CMSInfoLiveCategory * pRoot);
  95. CMSInfoLiveCategory * MakeVersion6Category(INTERNAL_CATEGORY * pCategory5);
  96. void ApplyCategoryFilter(LPCTSTR szFilter);
  97. public:
  98. CComPtr<IStream> m_pHistoryStream;
  99. //protected:
  100. CComPtr<IXMLDOMDocument> m_pXMLDoc;
  101. CComPtr<IXMLDOMDocument> m_pXMLFileDoc;
  102. CComPtr<IXMLDOMDocument> m_pXMLLiveDoc;
  103. public:
  104. //-------------------------------------------------------------------------
  105. // Get the XML document (this will be requested by the nodes displaying
  106. // history). In a test build, this may be loaded from a file; in release
  107. // builds this will be created from the history stream
  108. // if szpathName is not null (it's default value is null), use it to create BSTR argument for m_pXMLDoc->load
  109. // (to open XML file); otherwise open from DCO stream
  110. //-------------------------------------------------------------------------
  111. CComPtr<IXMLDOMDocument> GetXMLDoc()
  112. {
  113. return m_pXMLDoc;
  114. }
  115. HRESULT LoadXMLDoc(LPCTSTR szpathName)
  116. {
  117. m_pXMLFileDoc = CreateXMLDoc(szpathName);
  118. if (m_pXMLFileDoc)
  119. {
  120. m_pXMLDoc = m_pXMLFileDoc;
  121. return S_OK;
  122. }
  123. else
  124. {
  125. return E_FAIL;
  126. }
  127. }
  128. CComPtr<IXMLDOMDocument> CreateXMLDoc(LPCTSTR szpathName = NULL)
  129. {
  130. CComPtr<IXMLDOMDocument> pXMLDoc;
  131. if ((m_pHistoryStream || szpathName))
  132. {
  133. HRESULT hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXMLDoc);
  134. if (SUCCEEDED(hr))
  135. {
  136. if (szpathName != NULL)
  137. {
  138. VARIANT_BOOL varBSuccess;
  139. COleVariant bstrPath = szpathName;
  140. if (FAILED(pXMLDoc->load(bstrPath, &varBSuccess)) || varBSuccess == FALSE)
  141. {
  142. pXMLDoc.Release();
  143. pXMLDoc = NULL;
  144. }
  145. }
  146. else
  147. {
  148. VARIANT_BOOL varBSuccess;
  149. CComVariant varStream(m_pHistoryStream);
  150. if (FAILED(pXMLDoc->load(varStream, &varBSuccess)) || !varBSuccess)
  151. {
  152. ASSERT(0 && "Failed to load xml doc from stream");
  153. pXMLDoc.Release();
  154. pXMLDoc = NULL;
  155. }
  156. }
  157. }
  158. }
  159. return pXMLDoc;
  160. }
  161. //-------------------------------------------------------------------------
  162. //switch from XML doc loaded from file to doc created from DCO stream
  163. //-------------------------------------------------------------------------
  164. void RevertToLiveXML()
  165. {
  166. m_pXMLDoc = m_pXMLLiveDoc;
  167. }
  168. //-------------------------------------------------------------------------
  169. // Sets the history stream (which is generated by the DCO and is used to
  170. // show deltas).
  171. //-------------------------------------------------------------------------
  172. void SetHistoryStream(CComPtr<IStream> pHistoryStream)
  173. {
  174. m_pHistoryStream = pHistoryStream;
  175. try
  176. {
  177. m_pXMLLiveDoc = this->CreateXMLDoc();
  178. }
  179. catch(COleException * pException)
  180. {
  181. pException->ReportError();
  182. pException->Delete();
  183. }
  184. catch(...)
  185. {
  186. }
  187. m_pXMLDoc = m_pXMLLiveDoc;
  188. }
  189. public:
  190. CRefreshThread * m_pThread;
  191. int m_iDeltaIndex;
  192. protected:
  193. CMSInfoHistoryCategory * m_pHistoryRoot;
  194. private:
  195. CString m_strMachine;
  196. public:
  197. HWND m_hwnd;
  198. };
  199. //-----------------------------------------------------------------------------
  200. // CXMLDataSource provides system information from an XML snapshot. It derives
  201. // from CLiveDataSource because much of the functionality is the same (the
  202. // same categories, refresh functions, delta display, etc.).
  203. //-----------------------------------------------------------------------------
  204. class CXMLSnapshotCategory;
  205. class CXMLDataSource : public CLiveDataSource
  206. {
  207. private:
  208. // CComPtr<IXMLDOMDocument> m_pXMLDoc; CLiveDataSource has m_pXMLDoc member
  209. //CComPtr<IXMLDOMNode> m_pSnapshotNode;
  210. public:
  211. CXMLDataSource() {};
  212. ~CXMLDataSource() {};
  213. HRESULT Create(LPCTSTR szMachine) { return S_OK; };
  214. HRESULT Create(CString strFileName, CMSInfoLiveCategory* pRootLiveCat, HWND hwnd);
  215. HRESULT Refresh(CXMLSnapshotCategory* pCat);
  216. };
  217. //-----------------------------------------------------------------------------
  218. // CNFO6DataSource provides information from a 5.0/6.0 NFO file.
  219. //-----------------------------------------------------------------------------
  220. class CNFO6DataSource : public CDataSource
  221. {
  222. public:
  223. CNFO6DataSource();
  224. ~CNFO6DataSource();
  225. HRESULT Create(HANDLE h, LPCTSTR szFilename = NULL);
  226. };
  227. //-----------------------------------------------------------------------------
  228. // CNFO7DataSource provides information from a 7.0 NFO file.
  229. //-----------------------------------------------------------------------------
  230. class CNFO7DataSource : public CDataSource
  231. {
  232. public:
  233. CNFO7DataSource();
  234. ~CNFO7DataSource();
  235. HRESULT Create(LPCTSTR szFilename = NULL);
  236. };
  237. class CMSIControl;
  238. class CMSInfo4Category;
  239. class CNFO4DataSource : public CDataSource
  240. {
  241. public:
  242. CNFO4DataSource();
  243. ~CNFO4DataSource();
  244. // HRESULT RecurseLoad410Tree(CMSInfo4Category** ppRoot, CComPtr<IStream> pStream,CComPtr<IStorage> pStorage,CMapStringToString& mapStreams);
  245. //HRESULT ReadMSI4NFO(CString strFileName/*HANDLE hFile*/,CMSInfo4Category** ppRootCat);
  246. HRESULT Create(CString strFileName);
  247. void AddControlMapping(CString strCLSID, CMSIControl* pControl)
  248. {
  249. m_mapCLSIDToControl.SetAt(strCLSID, pControl);
  250. }
  251. BOOL GetControlFromCLSID(CString strCLSID,CMSIControl*& pControl)
  252. {
  253. return m_mapCLSIDToControl.Lookup(strCLSID,(void*&)pControl);
  254. }
  255. void UpdateCurrentControl(CMSIControl* pControl);
  256. protected:
  257. CMapStringToPtr m_mapCLSIDToControl;
  258. CMSIControl* m_pCurrentControl;
  259. };