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.

701 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. catch (...)
  118. {
  119. sc = E_FAIL;
  120. }
  121. return sc.ToHr();
  122. }
  123. //+-------------------------------------------------------------------
  124. //
  125. // Member: CNodeInitObject::DeleteItem
  126. //
  127. // Synopsis: Delete the given item.
  128. //
  129. // Arguments: [hItem] - ItemID of item to be deleted.
  130. // [fDeleteThis] - If true Delete this item & its children
  131. // else just the children.
  132. //
  133. // Returns: HRESULT
  134. //
  135. //--------------------------------------------------------------------
  136. STDMETHODIMP CNodeInitObject::DeleteItem(HSCOPEITEM hItem, long fDeleteThis)
  137. {
  138. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::DeleteItem"));
  139. COMPONENTID nID;
  140. GetComponentID(&nID);
  141. if (nID == -1)
  142. {
  143. sc = E_UNEXPECTED;
  144. return sc.ToHr();
  145. }
  146. CMTNode *pMTNode = CMTNode::FromScopeItem (hItem);
  147. if (pMTNode == NULL)
  148. {
  149. sc = E_POINTER;
  150. TraceSnapinError(_T("HSCOPEITEM is not valid"), sc);
  151. return sc.ToHr();
  152. }
  153. CScopeTree* pScopeTree = CScopeTree::GetScopeTree();
  154. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  155. if (sc)
  156. return sc.ToHr();
  157. // Delete from scope tree.
  158. sc = pScopeTree->ScDelete(pMTNode, fDeleteThis ? TRUE : FALSE, nID);
  159. return sc.ToHr();
  160. }
  161. //+-------------------------------------------------------------------
  162. //
  163. // Member: CNodeInitObject::SetItem
  164. //
  165. // Synopsis: Change the attributes of an item.
  166. //
  167. // Arguments: [pSDI] -
  168. //
  169. // Returns: HRESULT
  170. //
  171. //--------------------------------------------------------------------
  172. STDMETHODIMP CNodeInitObject::SetItem(LPSCOPEDATAITEM pSDI)
  173. {
  174. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::SetItem"));
  175. if (!pSDI)
  176. {
  177. sc = E_INVALIDARG;
  178. TraceSnapinError(_T("NULL LPSCOPEDATAITEM ptr"), sc);
  179. return sc.ToHr();
  180. }
  181. if (IsBadReadPtr(pSDI, sizeof(SCOPEDATAITEM)) != 0)
  182. {
  183. sc = E_POINTER;
  184. TraceSnapinError(_T("LPSCOPEDATAITEM is bad read ptr"), sc);
  185. return sc.ToHr();
  186. }
  187. COMPONENTID nID;
  188. GetComponentID(&nID);
  189. if (nID == -1)
  190. {
  191. sc = E_UNEXPECTED;
  192. return sc.ToHr();
  193. }
  194. CMTNode* pMTNode = CMTNode::FromScopeItem (pSDI->ID);
  195. if (pMTNode == NULL)
  196. {
  197. sc = E_INVALIDARG;
  198. TraceSnapinError(_T("Invalid ID in LPSCOPEDATAITEM"), sc);
  199. return sc.ToHr();
  200. }
  201. // Access pMTNode inside try-catch
  202. try
  203. {
  204. if (pMTNode->GetOwnerID() != nID &&
  205. pMTNode->GetOwnerID() != TVOWNED_MAGICWORD)
  206. {
  207. sc = E_INVALIDARG;
  208. return sc.ToHr();
  209. }
  210. if (pSDI->mask & SDI_PARAM)
  211. pMTNode->SetUserParam(pSDI->lParam);
  212. if (pSDI->mask & SDI_STATE)
  213. pMTNode->SetState(pSDI->nState);
  214. if (pSDI->mask & SDI_STR)
  215. {
  216. // Only static node's string can be modified, other node's use MMC_TEXTCALLBACK.
  217. if ((pSDI->displayname != MMC_TEXTCALLBACK) && (!pMTNode->IsStaticNode()) )
  218. {
  219. /*
  220. * We should be tracing and returning E_INVALIDARG. But this code is in existence
  221. * for more than 3 years and has high impact. So we just trace and proceed as if
  222. * no error occurred.
  223. */
  224. //sc = E_INVALIDARG;
  225. TraceSnapinError(_T("Display name must be MMC_TEXTCALLBACK"), sc);
  226. //return sc.ToHr();
  227. sc = S_OK;
  228. }
  229. else
  230. {
  231. USES_CONVERSION;
  232. LPCTSTR lpstrDisplayName = NULL;
  233. if ( pSDI->displayname != MMC_TEXTCALLBACK )
  234. lpstrDisplayName = W2T(pSDI->displayname);
  235. else
  236. lpstrDisplayName = reinterpret_cast<LPCTSTR>(MMC_TEXTCALLBACK);
  237. pMTNode->SetDisplayName( lpstrDisplayName );
  238. }
  239. }
  240. int nTemp;
  241. if (pSDI->mask & SDI_IMAGE)
  242. {
  243. // Change the image.
  244. nTemp = pSDI->nImage;
  245. if (pSDI->nImage == MMC_IMAGECALLBACK)
  246. {
  247. CComponentData* pCCD = pMTNode->GetPrimaryComponentData();
  248. if (pCCD)
  249. {
  250. SCOPEDATAITEM ScopeDataItem;
  251. ZeroMemory(&ScopeDataItem, sizeof(ScopeDataItem));
  252. ScopeDataItem.mask = SDI_IMAGE;
  253. ScopeDataItem.lParam = pMTNode->GetUserParam();
  254. ScopeDataItem.nImage = 0;
  255. sc = pCCD->GetDisplayInfo(&ScopeDataItem);
  256. if (sc)
  257. return sc.ToHr();
  258. pSDI->nImage = ScopeDataItem.nImage;
  259. }
  260. }
  261. sc = m_pImageListPriv->MapRsltImage (nID, pSDI->nImage, &nTemp);
  262. if (sc)
  263. {
  264. TraceSnapinError(_T("The snapin specified image was never added initially"), sc);
  265. sc.Clear();
  266. }
  267. pMTNode->SetImage (nTemp);
  268. CMTSnapInNode* pMTSnapInNode = dynamic_cast<CMTSnapInNode*>(pMTNode);
  269. if (pMTSnapInNode)
  270. pMTSnapInNode->SetResultImage (MMC_IMAGECALLBACK);
  271. }
  272. if (pSDI->mask & SDI_OPENIMAGE)
  273. {
  274. nTemp = pSDI->nOpenImage;
  275. if (pSDI->nOpenImage == MMC_IMAGECALLBACK)
  276. {
  277. CComponentData* pCCD = pMTNode->GetPrimaryComponentData();
  278. if (pCCD)
  279. {
  280. SCOPEDATAITEM ScopeDataItem;
  281. ZeroMemory(&ScopeDataItem, sizeof(ScopeDataItem));
  282. ScopeDataItem.mask = SDI_OPENIMAGE;
  283. ScopeDataItem.lParam = pMTNode->GetUserParam();
  284. ScopeDataItem.nOpenImage = 1;
  285. sc = pCCD->GetDisplayInfo(&ScopeDataItem);
  286. if (sc)
  287. return sc.ToHr();
  288. pSDI->nOpenImage = ScopeDataItem.nOpenImage;
  289. }
  290. }
  291. sc = m_pImageListPriv->MapRsltImage (nID, pSDI->nOpenImage, &nTemp);
  292. if (sc)
  293. {
  294. TraceSnapinError(_T("The snapin specified image was never added initially"), sc);
  295. sc.Clear();
  296. }
  297. pMTNode->SetOpenImage (nTemp);
  298. }
  299. if (pSDI->mask & SDI_CHILDREN)
  300. {
  301. pMTNode->SetNoPrimaryChildren(pSDI->cChildren == 0);
  302. }
  303. // Now inform the views to modify as needed.
  304. SViewUpdateInfo vui;
  305. // Snapin nodes result pane will be handled by the snapins
  306. vui.flag = VUI_REFRESH_NODE;
  307. pMTNode->CreatePathList(vui.path);
  308. CScopeTree* pScopeTree =
  309. dynamic_cast<CScopeTree*>((IScopeTree*)m_spScopeTree);
  310. pScopeTree->UpdateAllViews(VIEW_UPDATE_MODIFY,
  311. reinterpret_cast<LPARAM>(&vui));
  312. }
  313. catch (...)
  314. {
  315. sc = E_FAIL;
  316. }
  317. return sc.ToHr();
  318. }
  319. //+-------------------------------------------------------------------
  320. //
  321. // Member: CNodeInitObject::GetItem
  322. //
  323. // Synopsis: Get the attributes of an item given ItemID.
  324. //
  325. // Arguments: [pSDI]
  326. //
  327. // Returns: HRESULT
  328. //
  329. //--------------------------------------------------------------------
  330. STDMETHODIMP CNodeInitObject::GetItem(LPSCOPEDATAITEM pSDI)
  331. {
  332. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetItem"));
  333. if (!pSDI)
  334. {
  335. sc = E_INVALIDARG;
  336. TraceSnapinError(_T("NULL LPSCOPEDATAITEM ptr"), sc);
  337. return sc.ToHr();
  338. }
  339. if (IsBadWritePtr(pSDI, sizeof(SCOPEDATAITEM)) != 0)
  340. {
  341. sc = E_POINTER;
  342. TraceSnapinError(_T("BadWrite Ptr LPSCOPEDATAITEM"), sc);
  343. return sc.ToHr();
  344. }
  345. COMPONENTID nID;
  346. GetComponentID(&nID);
  347. if (nID == -1)
  348. {
  349. sc = E_UNEXPECTED;
  350. return sc.ToHr();
  351. }
  352. CMTNode* pMTNode = CMTNode::FromScopeItem (pSDI->ID);
  353. sc = ScCheckPointers(pMTNode);
  354. if (sc)
  355. return sc.ToHr();
  356. try
  357. {
  358. if (pMTNode->GetOwnerID() != nID &&
  359. pMTNode->GetOwnerID() != TVOWNED_MAGICWORD)
  360. {
  361. sc = E_INVALIDARG;
  362. return sc.ToHr();
  363. }
  364. if (pSDI->mask & SDI_IMAGE)
  365. {
  366. sc = m_pImageListPriv->UnmapRsltImage (nID, pMTNode->GetImage(), &pSDI->nImage);
  367. if (sc)
  368. return (sc.ToHr());
  369. }
  370. if (pSDI->mask & SDI_OPENIMAGE)
  371. {
  372. sc = m_pImageListPriv->UnmapRsltImage (nID, pMTNode->GetOpenImage(), &pSDI->nOpenImage);
  373. if (sc)
  374. return (sc.ToHr());
  375. }
  376. if (pSDI->mask & SDI_STATE)
  377. pSDI->nState = pMTNode->GetState();
  378. if (pSDI->mask & SDI_PARAM)
  379. pSDI->lParam = pMTNode->GetUserParam();
  380. }
  381. catch (...)
  382. {
  383. sc = E_FAIL;
  384. }
  385. return sc.ToHr();
  386. }
  387. //+-------------------------------------------------------------------
  388. //
  389. // Member: CNodeInitObject::GetRelativeItem
  390. //
  391. // Synopsis: Helper function, Get Parent/Child/Sibling item.
  392. //
  393. // Arguments:
  394. //
  395. // Returns: HRESULT
  396. //
  397. //--------------------------------------------------------------------
  398. HRESULT CNodeInitObject::GetRelativeItem(EGetItem egi, HSCOPEITEM item,
  399. HSCOPEITEM* pItem, MMC_COOKIE* pCookie)
  400. {
  401. DECLARE_SC(sc, _T("CNodeInitObject::GetRelativeItem"));
  402. if (item == 0)
  403. {
  404. sc = E_INVALIDARG;
  405. TraceSnapinError(_T("The HSCOPEITEM is NULL"), sc);
  406. return sc.ToHr();
  407. }
  408. if (pItem == NULL)
  409. {
  410. sc = E_INVALIDARG;
  411. TraceSnapinError(_T("NULL HSCOPEITEM ptr"), sc);
  412. return sc.ToHr();
  413. }
  414. if (pCookie == NULL)
  415. {
  416. sc = E_INVALIDARG;
  417. TraceSnapinError(_T("NULL MMC_COOKIE ptr"), sc);
  418. return sc.ToHr();
  419. }
  420. COMPONENTID nID;
  421. GetComponentID(&nID);
  422. if (nID == -1)
  423. {
  424. sc = E_UNEXPECTED;
  425. return sc.ToHr();
  426. }
  427. // init
  428. *pItem = 0;
  429. *pCookie = 0;
  430. if (item != 0)
  431. {
  432. CMTNode* pMTNode = CMTNode::FromScopeItem (item);
  433. if (pMTNode == NULL)
  434. {
  435. sc = E_INVALIDARG;
  436. TraceSnapinError(_T("Invalid HSCOPEITEM"), sc);
  437. return sc.ToHr();
  438. }
  439. CMTNode* pMTNodeTemp = pMTNode;
  440. try
  441. {
  442. switch (egi)
  443. {
  444. case egiParent:
  445. if (pMTNodeTemp->GetPrimaryComponentID() != nID &&
  446. pMTNodeTemp->GetPrimaryComponentID() != TVOWNED_MAGICWORD)
  447. {
  448. sc = E_FAIL;
  449. return sc.ToHr();
  450. }
  451. if (pMTNodeTemp->IsStaticNode() == TRUE)
  452. {
  453. sc = E_FAIL;
  454. return sc.ToHr();
  455. }
  456. pMTNodeTemp = pMTNodeTemp->Parent();
  457. break;
  458. case egiChild:
  459. pMTNodeTemp = pMTNodeTemp->Child();
  460. while (pMTNodeTemp != NULL)
  461. {
  462. if (pMTNodeTemp->GetPrimaryComponentID() == nID)
  463. break;
  464. pMTNodeTemp = pMTNodeTemp->Next();
  465. }
  466. break;
  467. case egiNext:
  468. if (pMTNodeTemp->GetPrimaryComponentID() != nID &&
  469. pMTNodeTemp->GetPrimaryComponentID() != TVOWNED_MAGICWORD)
  470. {
  471. sc = E_FAIL;
  472. return sc.ToHr();
  473. }
  474. while (1)
  475. {
  476. pMTNodeTemp = pMTNodeTemp->Next();
  477. if (pMTNodeTemp == NULL)
  478. break;
  479. if (pMTNodeTemp->GetPrimaryComponentID() == nID)
  480. break;
  481. }
  482. break;
  483. default:
  484. sc = E_UNEXPECTED;
  485. break;
  486. }
  487. if (pMTNodeTemp != NULL)
  488. {
  489. *pItem = CMTNode::ToScopeItem(pMTNodeTemp);
  490. *pCookie = pMTNodeTemp->GetUserParam();
  491. }
  492. else
  493. {
  494. sc = S_FALSE;
  495. }
  496. }
  497. catch (...)
  498. {
  499. sc = E_INVALIDARG;
  500. }
  501. }
  502. return sc.ToHr();
  503. }
  504. //+-------------------------------------------------------------------
  505. //
  506. // Member: CNodeInitObject::GetChildItem
  507. //
  508. // Synopsis: Get the child item
  509. //
  510. // Arguments:
  511. //
  512. // Returns: HRESULT
  513. //
  514. //--------------------------------------------------------------------
  515. STDMETHODIMP CNodeInitObject::GetChildItem(HSCOPEITEM item,
  516. HSCOPEITEM* pItemChild, MMC_COOKIE* pCookie)
  517. {
  518. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetChildItem"));
  519. sc = GetRelativeItem(egiChild, item, pItemChild, pCookie);
  520. return sc.ToHr();
  521. }
  522. //+-------------------------------------------------------------------
  523. //
  524. // Member: CNodeInitObject::GetNextItem
  525. //
  526. // Synopsis: Get the next (sibling) item.
  527. //
  528. // Arguments:
  529. //
  530. // Returns: HRESULT
  531. //
  532. //--------------------------------------------------------------------
  533. STDMETHODIMP CNodeInitObject::GetNextItem(HSCOPEITEM item,
  534. HSCOPEITEM* pItemNext, MMC_COOKIE* pCookie)
  535. {
  536. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetNextItem"));
  537. sc = GetRelativeItem(egiNext, item, pItemNext, pCookie);
  538. return sc.ToHr();
  539. }
  540. //+-------------------------------------------------------------------
  541. //
  542. // Member: CNodeInitObject::GetParentItem
  543. //
  544. // Synopsis: Get the parent item.
  545. //
  546. // Arguments:
  547. //
  548. // Returns: HRESULT
  549. //
  550. //--------------------------------------------------------------------
  551. STDMETHODIMP CNodeInitObject::GetParentItem(HSCOPEITEM item,
  552. HSCOPEITEM* pItemParent, MMC_COOKIE* pCookie)
  553. {
  554. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::GetParentItem"));
  555. sc = GetRelativeItem(egiParent, item, pItemParent, pCookie);
  556. return sc.ToHr();
  557. }
  558. //+-------------------------------------------------------------------
  559. //
  560. // Member: CNodeInitObject::Expand
  561. //
  562. // Synopsis: Expand the given item (not visually, this will send
  563. // MMCN_EXPAND to snapin if the item is not already
  564. // expanded.)
  565. //
  566. // Arguments: [hItem] - Item to be expanded.
  567. //
  568. // Returns: HRESULT
  569. //
  570. //--------------------------------------------------------------------
  571. STDMETHODIMP CNodeInitObject::Expand(HSCOPEITEM hItem)
  572. {
  573. DECLARE_SC_FOR_PUBLIC_INTERFACE(sc, _T("IConsoleNameSpace2::Expand"));
  574. CMTNode* pMTNode = CMTNode::FromScopeItem (hItem);
  575. if (pMTNode == NULL)
  576. {
  577. sc = E_INVALIDARG;
  578. TraceSnapinError(_T("Invalid HSCOPEITEM"), sc);
  579. return sc.ToHr();
  580. }
  581. if (pMTNode->WasExpandedAtLeastOnce() == TRUE)
  582. {
  583. // Item is already expanded.
  584. sc = S_FALSE;
  585. return sc.ToHr();
  586. }
  587. sc = pMTNode->Expand();
  588. return sc.ToHr();
  589. }