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.

304 lines
8.6 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 e)
  180. {
  181. e.ReportError();
  182. }
  183. catch(...)
  184. {
  185. }
  186. m_pXMLDoc = m_pXMLLiveDoc;
  187. }
  188. public:
  189. CRefreshThread * m_pThread;
  190. int m_iDeltaIndex;
  191. protected:
  192. CMSInfoHistoryCategory * m_pHistoryRoot;
  193. private:
  194. CString m_strMachine;
  195. public:
  196. HWND m_hwnd;
  197. };
  198. //-----------------------------------------------------------------------------
  199. // CXMLDataSource provides system information from an XML snapshot. It derives
  200. // from CLiveDataSource because much of the functionality is the same (the
  201. // same categories, refresh functions, delta display, etc.).
  202. //-----------------------------------------------------------------------------
  203. class CXMLSnapshotCategory;
  204. class CXMLDataSource : public CLiveDataSource
  205. {
  206. private:
  207. // CComPtr<IXMLDOMDocument> m_pXMLDoc; CLiveDataSource has m_pXMLDoc member
  208. //CComPtr<IXMLDOMNode> m_pSnapshotNode;
  209. public:
  210. CXMLDataSource() {};
  211. ~CXMLDataSource() {};
  212. HRESULT Create(LPCTSTR szMachine) { return S_OK; };
  213. HRESULT Create(CString strFileName, CMSInfoLiveCategory* pRootLiveCat, HWND hwnd);
  214. HRESULT Refresh(CXMLSnapshotCategory* pCat);
  215. };
  216. //-----------------------------------------------------------------------------
  217. // CNFO6DataSource provides information from a 5.0/6.0 NFO file.
  218. //-----------------------------------------------------------------------------
  219. class CNFO6DataSource : public CDataSource
  220. {
  221. public:
  222. CNFO6DataSource();
  223. ~CNFO6DataSource();
  224. HRESULT Create(HANDLE h, LPCTSTR szFilename = NULL);
  225. };
  226. //-----------------------------------------------------------------------------
  227. // CNFO7DataSource provides information from a 7.0 NFO file.
  228. //-----------------------------------------------------------------------------
  229. class CNFO7DataSource : public CDataSource
  230. {
  231. public:
  232. CNFO7DataSource();
  233. ~CNFO7DataSource();
  234. HRESULT Create(LPCTSTR szFilename = NULL);
  235. };
  236. class CMSIControl;
  237. class CMSInfo4Category;
  238. class CNFO4DataSource : public CDataSource
  239. {
  240. public:
  241. CNFO4DataSource();
  242. ~CNFO4DataSource();
  243. // HRESULT RecurseLoad410Tree(CMSInfo4Category** ppRoot, CComPtr<IStream> pStream,CComPtr<IStorage> pStorage,CMapStringToString& mapStreams);
  244. //HRESULT ReadMSI4NFO(CString strFileName/*HANDLE hFile*/,CMSInfo4Category** ppRootCat);
  245. HRESULT Create(CString strFileName);
  246. void AddControlMapping(CString strCLSID, CMSIControl* pControl)
  247. {
  248. m_mapCLSIDToControl.SetAt(strCLSID, pControl);
  249. }
  250. BOOL GetControlFromCLSID(CString strCLSID,CMSIControl*& pControl)
  251. {
  252. return m_mapCLSIDToControl.Lookup(strCLSID,(void*&)pControl);
  253. }
  254. void UpdateCurrentControl(CMSIControl* pControl);
  255. protected:
  256. CMapStringToPtr m_mapCLSIDToControl;
  257. CMSIControl* m_pCurrentControl;
  258. };