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.

681 lines
18 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: ScopData.cpp
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History:
  15. //____________________________________________________________________________
  16. //
  17. #include "stdafx.h"
  18. #include "ScopImag.h"
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. class CDoc;
  24. //+-------------------------------------------------------------------
  25. //
  26. // Member: CNodeInitObject::InsertItem
  27. //
  28. // Synopsis: Insert an item in TreeView (IConsoleNameSpace method).
  29. //
  30. // Arguments: [pSDI] - LPSCOPEDATEITEM
  31. //
  32. // Returns: HRESULT
  33. //
  34. //--------------------------------------------------------------------
  35. STDMETHODIMP CNodeInitObject::InsertItem(LPSCOPEDATAITEM pSDI)
  36. {
  37. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::InsertItem"));
  38. if (!pSDI)
  39. {
  40. sc = E_INVALIDARG;
  41. TraceSnapinError(_T("NULL LPSCOPEDATAITEM ptr"), sc);
  42. return sc.ToHr();
  43. }
  44. if (IsBadWritePtr(pSDI, sizeof(SCOPEDATAITEM)) != 0)
  45. {
  46. sc = E_POINTER;
  47. TraceSnapinError(_T("BadWrite Ptr LPSCOPEDATAITEM"), sc);
  48. return sc.ToHr();
  49. }
  50. COMPONENTID nID;
  51. GetComponentID(&nID);
  52. if (nID == TVOWNED_MAGICWORD)
  53. {
  54. sc = E_UNEXPECTED;
  55. return sc.ToHr();
  56. }
  57. if (nID == -1)
  58. {
  59. sc = E_UNEXPECTED;
  60. return sc.ToHr();
  61. }
  62. if (pSDI->relativeID == NULL)
  63. {
  64. sc = E_INVALIDARG;
  65. TraceSnapinError(_T("RelativeID is NULL"), sc);
  66. return sc.ToHr();
  67. }
  68. if ( 0 == (pSDI->mask & SDI_STR))
  69. {
  70. sc = E_INVALIDARG;
  71. TraceSnapinError(_T("SDI_STR mask is not set"), sc);
  72. return sc.ToHr();
  73. }
  74. if (0 == (pSDI->mask & SDI_PARAM))
  75. {
  76. sc = E_INVALIDARG;
  77. TraceSnapinError(_T("SDI_PARAM mask is not set"), sc);
  78. return sc.ToHr();
  79. }
  80. if (pSDI->displayname != MMC_TEXTCALLBACK)
  81. {
  82. sc = E_INVALIDARG;
  83. TraceSnapinError(_T("Display name must be MMC_TEXTCALLBACK"), sc);
  84. return sc.ToHr();
  85. }
  86. SCOPEDATAITEM sdiTemp;
  87. CopyMemory(&sdiTemp, pSDI, sizeof(sdiTemp));
  88. sdiTemp.nImage = sdiTemp.nOpenImage = 0;
  89. if (pSDI->mask & SDI_IMAGE)
  90. {
  91. m_pImageListPriv->MapRsltImage(nID, pSDI->nImage, &sdiTemp.nImage);
  92. sdiTemp.nOpenImage = sdiTemp.nImage;
  93. }
  94. if (pSDI->mask & SDI_OPENIMAGE)
  95. m_pImageListPriv->MapRsltImage(nID, pSDI->nOpenImage, &sdiTemp.nOpenImage);
  96. try
  97. {
  98. CScopeTree* pScopeTree =
  99. dynamic_cast<CScopeTree*>((IScopeTree*)m_spScopeTree);
  100. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  101. if (sc)
  102. return sc.ToHr();
  103. CMTNode* pMTNodeNew = NULL;
  104. sc = pScopeTree->ScInsert(&sdiTemp, nID, &pMTNodeNew);
  105. if(sc)
  106. return sc.ToHr();
  107. sc = ScCheckPointers(pMTNodeNew, E_UNEXPECTED);
  108. if (sc)
  109. return sc.ToHr();
  110. pSDI->ID = sdiTemp.ID;
  111. ASSERT (CMTNode::FromScopeItem(pSDI->ID) == pMTNodeNew);
  112. }
  113. catch( std::bad_alloc )
  114. {
  115. sc = E_OUTOFMEMORY;
  116. }
  117. return sc.ToHr();
  118. }
  119. //+-------------------------------------------------------------------
  120. //
  121. // Member: CNodeInitObject::DeleteItem
  122. //
  123. // Synopsis: Delete the given item.
  124. //
  125. // Arguments: [hItem] - ItemID of item to be deleted.
  126. // [fDeleteThis] - If true Delete this item & its children
  127. // else just the children.
  128. //
  129. // Returns: HRESULT
  130. //
  131. //--------------------------------------------------------------------
  132. STDMETHODIMP CNodeInitObject::DeleteItem(HSCOPEITEM hItem, long fDeleteThis)
  133. {
  134. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::DeleteItem"));
  135. COMPONENTID nID;
  136. GetComponentID(&nID);
  137. if (nID == -1)
  138. {
  139. sc = E_UNEXPECTED;
  140. return sc.ToHr();
  141. }
  142. CMTNode *pMTNode = CMTNode::FromScopeItem (hItem);
  143. if (pMTNode == NULL)
  144. {
  145. sc = E_POINTER;
  146. TraceSnapinError(_T("HSCOPEITEM is not valid"), sc);
  147. return sc.ToHr();
  148. }
  149. CScopeTree* pScopeTree = CScopeTree::GetScopeTree();
  150. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  151. if (sc)
  152. return sc.ToHr();
  153. // Delete from scope tree.
  154. sc = pScopeTree->ScDelete(pMTNode, fDeleteThis ? TRUE : FALSE, nID);
  155. return sc.ToHr();
  156. }
  157. //+-------------------------------------------------------------------
  158. //
  159. // Member: CNodeInitObject::SetItem
  160. //
  161. // Synopsis: Change the attributes of an item.
  162. //
  163. // Arguments: [pSDI] -
  164. //
  165. // Returns: HRESULT
  166. //
  167. //--------------------------------------------------------------------
  168. STDMETHODIMP CNodeInitObject::SetItem(LPSCOPEDATAITEM pSDI)
  169. {
  170. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::SetItem"));
  171. if (!pSDI)
  172. {
  173. sc = E_INVALIDARG;
  174. TraceSnapinError(_T("NULL LPSCOPEDATAITEM ptr"), sc);
  175. return sc.ToHr();
  176. }
  177. if (IsBadReadPtr(pSDI, sizeof(SCOPEDATAITEM)) != 0)
  178. {
  179. sc = E_POINTER;
  180. TraceSnapinError(_T("LPSCOPEDATAITEM is bad read ptr"), sc);
  181. return sc.ToHr();
  182. }
  183. COMPONENTID nID;
  184. GetComponentID(&nID);
  185. if (nID == -1)
  186. {
  187. sc = E_UNEXPECTED;
  188. return sc.ToHr();
  189. }
  190. CMTNode* pMTNode = CMTNode::FromScopeItem (pSDI->ID);
  191. if (pMTNode == NULL)
  192. {
  193. sc = E_INVALIDARG;
  194. TraceSnapinError(_T("Invalid ID in LPSCOPEDATAITEM"), sc);
  195. return sc.ToHr();
  196. }
  197. if (pMTNode->GetOwnerID() != nID &&
  198. pMTNode->GetOwnerID() != TVOWNED_MAGICWORD)
  199. {
  200. sc = E_INVALIDARG;
  201. return sc.ToHr();
  202. }
  203. if (pSDI->mask & SDI_PARAM)
  204. pMTNode->SetUserParam(pSDI->lParam);
  205. if (pSDI->mask & SDI_STATE)
  206. pMTNode->SetState(pSDI->nState);
  207. if (pSDI->mask & SDI_STR)
  208. {
  209. // Only static node's string can be modified, other node's use MMC_TEXTCALLBACK.
  210. if ((pSDI->displayname != MMC_TEXTCALLBACK) && (!pMTNode->IsStaticNode()) )
  211. {
  212. /*
  213. * We should be tracing and returning E_INVALIDARG. But this code is in existence
  214. * for more than 3 years and has high impact. So we just trace and proceed as if
  215. * no error occurred.
  216. */
  217. //sc = E_INVALIDARG;
  218. TraceSnapinError(_T("Display name must be MMC_TEXTCALLBACK"), sc);
  219. //return sc.ToHr();
  220. sc = S_OK;
  221. }
  222. else
  223. {
  224. USES_CONVERSION;
  225. LPCTSTR lpstrDisplayName = NULL;
  226. if ( pSDI->displayname != MMC_TEXTCALLBACK )
  227. lpstrDisplayName = W2T(pSDI->displayname);
  228. else
  229. lpstrDisplayName = reinterpret_cast<LPCTSTR>(MMC_TEXTCALLBACK);
  230. pMTNode->SetDisplayName( lpstrDisplayName );
  231. }
  232. }
  233. int nTemp;
  234. if (pSDI->mask & SDI_IMAGE)
  235. {
  236. // Change the image.
  237. nTemp = pSDI->nImage;
  238. if (pSDI->nImage == MMC_IMAGECALLBACK)
  239. {
  240. CComponentData* pCCD = pMTNode->GetPrimaryComponentData();
  241. if (pCCD)
  242. {
  243. SCOPEDATAITEM ScopeDataItem;
  244. ZeroMemory(&ScopeDataItem, sizeof(ScopeDataItem));
  245. ScopeDataItem.mask = SDI_IMAGE;
  246. ScopeDataItem.lParam = pMTNode->GetUserParam();
  247. ScopeDataItem.nImage = 0;
  248. sc = pCCD->GetDisplayInfo(&ScopeDataItem);
  249. if (sc)
  250. return sc.ToHr();
  251. pSDI->nImage = ScopeDataItem.nImage;
  252. }
  253. }
  254. sc = m_pImageListPriv->MapRsltImage (nID, pSDI->nImage, &nTemp);
  255. if (sc)
  256. {
  257. TraceSnapinError(_T("The snapin specified image was never added initially"), sc);
  258. sc.Clear();
  259. }
  260. pMTNode->SetImage (nTemp);
  261. CMTSnapInNode* pMTSnapInNode = dynamic_cast<CMTSnapInNode*>(pMTNode);
  262. if (pMTSnapInNode)
  263. pMTSnapInNode->SetResultImage (MMC_IMAGECALLBACK);
  264. }
  265. if (pSDI->mask & SDI_OPENIMAGE)
  266. {
  267. nTemp = pSDI->nOpenImage;
  268. if (pSDI->nOpenImage == MMC_IMAGECALLBACK)
  269. {
  270. CComponentData* pCCD = pMTNode->GetPrimaryComponentData();
  271. if (pCCD)
  272. {
  273. SCOPEDATAITEM ScopeDataItem;
  274. ZeroMemory(&ScopeDataItem, sizeof(ScopeDataItem));
  275. ScopeDataItem.mask = SDI_OPENIMAGE;
  276. ScopeDataItem.lParam = pMTNode->GetUserParam();
  277. ScopeDataItem.nOpenImage = 1;
  278. sc = pCCD->GetDisplayInfo(&ScopeDataItem);
  279. if (sc)
  280. return sc.ToHr();
  281. pSDI->nOpenImage = ScopeDataItem.nOpenImage;
  282. }
  283. }
  284. sc = m_pImageListPriv->MapRsltImage (nID, pSDI->nOpenImage, &nTemp);
  285. if (sc)
  286. {
  287. TraceSnapinError(_T("The snapin specified image was never added initially"), sc);
  288. sc.Clear();
  289. }
  290. pMTNode->SetOpenImage (nTemp);
  291. }
  292. if (pSDI->mask & SDI_CHILDREN)
  293. {
  294. pMTNode->SetNoPrimaryChildren(pSDI->cChildren == 0);
  295. }
  296. // Now inform the views to modify as needed.
  297. SViewUpdateInfo vui;
  298. // Snapin nodes result pane will be handled by the snapins
  299. vui.flag = VUI_REFRESH_NODE;
  300. pMTNode->CreatePathList(vui.path);
  301. CScopeTree* pScopeTree =
  302. dynamic_cast<CScopeTree*>((IScopeTree*)m_spScopeTree);
  303. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  304. if(sc)
  305. return sc.ToHr();
  306. pScopeTree->UpdateAllViews(VIEW_UPDATE_MODIFY,
  307. reinterpret_cast<LPARAM>(&vui));
  308. return sc.ToHr();
  309. }
  310. //+-------------------------------------------------------------------
  311. //
  312. // Member: CNodeInitObject::GetItem
  313. //
  314. // Synopsis: Get the attributes of an item given ItemID.
  315. //
  316. // Arguments: [pSDI]
  317. //
  318. // Returns: HRESULT
  319. //
  320. //--------------------------------------------------------------------
  321. STDMETHODIMP CNodeInitObject::GetItem(LPSCOPEDATAITEM pSDI)
  322. {
  323. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetItem"));
  324. if (!pSDI)
  325. {
  326. sc = E_INVALIDARG;
  327. TraceSnapinError(_T("NULL LPSCOPEDATAITEM ptr"), sc);
  328. return sc.ToHr();
  329. }
  330. if (IsBadWritePtr(pSDI, sizeof(SCOPEDATAITEM)) != 0)
  331. {
  332. sc = E_POINTER;
  333. TraceSnapinError(_T("BadWrite Ptr LPSCOPEDATAITEM"), sc);
  334. return sc.ToHr();
  335. }
  336. COMPONENTID nID;
  337. GetComponentID(&nID);
  338. if (nID == -1)
  339. {
  340. sc = E_UNEXPECTED;
  341. return sc.ToHr();
  342. }
  343. CMTNode* pMTNode = CMTNode::FromScopeItem (pSDI->ID);
  344. sc = ScCheckPointers(pMTNode);
  345. if (sc)
  346. return sc.ToHr();
  347. if (pMTNode->GetOwnerID() != nID &&
  348. pMTNode->GetOwnerID() != TVOWNED_MAGICWORD)
  349. {
  350. sc = E_INVALIDARG;
  351. return sc.ToHr();
  352. }
  353. if (pSDI->mask & SDI_IMAGE)
  354. {
  355. sc = m_pImageListPriv->UnmapRsltImage (nID, pMTNode->GetImage(), &pSDI->nImage);
  356. if (sc)
  357. return (sc.ToHr());
  358. }
  359. if (pSDI->mask & SDI_OPENIMAGE)
  360. {
  361. sc = m_pImageListPriv->UnmapRsltImage (nID, pMTNode->GetOpenImage(), &pSDI->nOpenImage);
  362. if (sc)
  363. return (sc.ToHr());
  364. }
  365. if (pSDI->mask & SDI_STATE)
  366. pSDI->nState = pMTNode->GetState();
  367. if (pSDI->mask & SDI_PARAM)
  368. pSDI->lParam = pMTNode->GetUserParam();
  369. return sc.ToHr();
  370. }
  371. //+-------------------------------------------------------------------
  372. //
  373. // Member: CNodeInitObject::GetRelativeItem
  374. //
  375. // Synopsis: Helper function, Get Parent/Child/Sibling item.
  376. //
  377. // Arguments:
  378. //
  379. // Returns: HRESULT
  380. //
  381. //--------------------------------------------------------------------
  382. HRESULT CNodeInitObject::GetRelativeItem(EGetItem egi, HSCOPEITEM item,
  383. HSCOPEITEM* pItem, MMC_COOKIE* pCookie)
  384. {
  385. DECLARE_SC(sc, _T("CNodeInitObject::GetRelativeItem"));
  386. if (item == 0)
  387. {
  388. sc = E_INVALIDARG;
  389. TraceSnapinError(_T("The HSCOPEITEM is NULL"), sc);
  390. return sc.ToHr();
  391. }
  392. if (pItem == NULL)
  393. {
  394. sc = E_INVALIDARG;
  395. TraceSnapinError(_T("NULL HSCOPEITEM ptr"), sc);
  396. return sc.ToHr();
  397. }
  398. if (pCookie == NULL)
  399. {
  400. sc = E_INVALIDARG;
  401. TraceSnapinError(_T("NULL MMC_COOKIE ptr"), sc);
  402. return sc.ToHr();
  403. }
  404. COMPONENTID nID;
  405. GetComponentID(&nID);
  406. if (nID == -1)
  407. {
  408. sc = E_UNEXPECTED;
  409. return sc.ToHr();
  410. }
  411. // init
  412. *pItem = 0;
  413. *pCookie = 0;
  414. if (item != 0)
  415. {
  416. CMTNode* pMTNode = CMTNode::FromScopeItem (item);
  417. if (pMTNode == NULL)
  418. {
  419. sc = E_INVALIDARG;
  420. TraceSnapinError(_T("Invalid HSCOPEITEM"), sc);
  421. return sc.ToHr();
  422. }
  423. CMTNode* pMTNodeTemp = pMTNode;
  424. switch (egi)
  425. {
  426. case egiParent:
  427. if (pMTNodeTemp->GetPrimaryComponentID() != nID &&
  428. pMTNodeTemp->GetPrimaryComponentID() != TVOWNED_MAGICWORD)
  429. {
  430. sc = E_FAIL;
  431. return sc.ToHr();
  432. }
  433. if (pMTNodeTemp->IsStaticNode() == TRUE)
  434. {
  435. sc = E_FAIL;
  436. return sc.ToHr();
  437. }
  438. pMTNodeTemp = pMTNodeTemp->Parent();
  439. break;
  440. case egiChild:
  441. pMTNodeTemp = pMTNodeTemp->Child();
  442. while (pMTNodeTemp != NULL)
  443. {
  444. // Backed out change made for bug# 525959 because of
  445. // regression (bug# 713914)
  446. if (pMTNodeTemp->GetPrimaryComponentID() == nID)
  447. break;
  448. pMTNodeTemp = pMTNodeTemp->Next();
  449. }
  450. break;
  451. case egiNext:
  452. if (pMTNodeTemp->GetPrimaryComponentID() != nID &&
  453. pMTNodeTemp->GetPrimaryComponentID() != TVOWNED_MAGICWORD)
  454. {
  455. sc = E_FAIL;
  456. return sc.ToHr();
  457. }
  458. while (1)
  459. {
  460. pMTNodeTemp = pMTNodeTemp->Next();
  461. if (pMTNodeTemp == NULL)
  462. break;
  463. if (pMTNodeTemp->GetPrimaryComponentID() == nID)
  464. break;
  465. }
  466. break;
  467. default:
  468. sc = E_UNEXPECTED;
  469. break;
  470. }
  471. if (pMTNodeTemp != NULL)
  472. {
  473. *pItem = CMTNode::ToScopeItem(pMTNodeTemp);
  474. *pCookie = pMTNodeTemp->GetUserParam();
  475. }
  476. else
  477. {
  478. sc = S_FALSE;
  479. }
  480. }
  481. return sc.ToHr();
  482. }
  483. //+-------------------------------------------------------------------
  484. //
  485. // Member: CNodeInitObject::GetChildItem
  486. //
  487. // Synopsis: Get the child item
  488. //
  489. // Arguments:
  490. //
  491. // Returns: HRESULT
  492. //
  493. //--------------------------------------------------------------------
  494. STDMETHODIMP CNodeInitObject::GetChildItem(HSCOPEITEM item,
  495. HSCOPEITEM* pItemChild, MMC_COOKIE* pCookie)
  496. {
  497. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetChildItem"));
  498. sc = GetRelativeItem(egiChild, item, pItemChild, pCookie);
  499. return sc.ToHr();
  500. }
  501. //+-------------------------------------------------------------------
  502. //
  503. // Member: CNodeInitObject::GetNextItem
  504. //
  505. // Synopsis: Get the next (sibling) item.
  506. //
  507. // Arguments:
  508. //
  509. // Returns: HRESULT
  510. //
  511. //--------------------------------------------------------------------
  512. STDMETHODIMP CNodeInitObject::GetNextItem(HSCOPEITEM item,
  513. HSCOPEITEM* pItemNext, MMC_COOKIE* pCookie)
  514. {
  515. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetNextItem"));
  516. sc = GetRelativeItem(egiNext, item, pItemNext, pCookie);
  517. return sc.ToHr();
  518. }
  519. //+-------------------------------------------------------------------
  520. //
  521. // Member: CNodeInitObject::GetParentItem
  522. //
  523. // Synopsis: Get the parent item.
  524. //
  525. // Arguments:
  526. //
  527. // Returns: HRESULT
  528. //
  529. //--------------------------------------------------------------------
  530. STDMETHODIMP CNodeInitObject::GetParentItem(HSCOPEITEM item,
  531. HSCOPEITEM* pItemParent, MMC_COOKIE* pCookie)
  532. {
  533. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetParentItem"));
  534. sc = GetRelativeItem(egiParent, item, pItemParent, pCookie);
  535. return sc.ToHr();
  536. }
  537. //+-------------------------------------------------------------------
  538. //
  539. // Member: CNodeInitObject::Expand
  540. //
  541. // Synopsis: Expand the given item (not visually, this will send
  542. // MMCN_EXPAND to snapin if the item is not already
  543. // expanded.)
  544. //
  545. // Arguments: [hItem] - Item to be expanded.
  546. //
  547. // Returns: HRESULT
  548. //
  549. //--------------------------------------------------------------------
  550. STDMETHODIMP CNodeInitObject::Expand(HSCOPEITEM hItem)
  551. {
  552. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::Expand"));
  553. CMTNode* pMTNode = CMTNode::FromScopeItem (hItem);
  554. if (pMTNode == NULL)
  555. {
  556. sc = E_INVALIDARG;
  557. TraceSnapinError(_T("Invalid HSCOPEITEM"), sc);
  558. return sc.ToHr();
  559. }
  560. if (pMTNode->WasExpandedAtLeastOnce() == TRUE)
  561. {
  562. // Item is already expanded.
  563. sc = S_FALSE;
  564. return sc.ToHr();
  565. }
  566. sc = pMTNode->Expand();
  567. return sc.ToHr();
  568. }