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.

598 lines
16 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Windows NT Directory Service Administration SnapIn
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1999
  7. //
  8. // File: toolbar.cpp
  9. //
  10. // Contents: DS App
  11. //
  12. // History: 30-apr-98 jimharr Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "stdafx.h"
  16. #include "resource.h"
  17. #include "dsutil.h"
  18. #include "DSEvent.h"
  19. #include "DSdirect.h"
  20. #include "dsfilter.h"
  21. #include "dssnap.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. MMCBUTTON g_DSAdmin_SnapinButtons[] =
  28. {
  29. { 2, dsNewUser, !TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  30. { 3, dsNewGroup, !TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  31. { 4, dsNewOU, !TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  32. { 1, dsFilter, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  33. { 0, dsFind, !TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  34. { 5, dsAddMember, !TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0 },
  35. };
  36. class CButtonStringsHolder
  37. {
  38. public:
  39. CButtonStringsHolder()
  40. {
  41. m_astr = NULL;
  42. }
  43. ~CButtonStringsHolder()
  44. {
  45. if (m_astr != NULL)
  46. delete[] m_astr;
  47. }
  48. CString* m_astr; // dynamic array of CStrings
  49. };
  50. CButtonStringsHolder g_astrButtonStrings;
  51. CONST INT cButtons = sizeof(g_DSAdmin_SnapinButtons)/sizeof(MMCBUTTON);
  52. /////////////////////////////////////////////////////////////////////////////////////////
  53. // IExtendControlbar
  54. HRESULT CDSEvent::SetControlbar (LPCONTROLBAR pControlbar)
  55. {
  56. HRESULT hr = S_OK;
  57. //
  58. // we are shutting down (get passed NULL)
  59. //
  60. if (pControlbar == NULL)
  61. {
  62. if (m_pControlbar != NULL)
  63. {
  64. //
  65. // assign to a variable on the stack to avoid
  66. // reentrancy problem: the Release() call might
  67. // cause another call in this function with null argument
  68. //
  69. LPCONTROLBAR pControlbarTemp = m_pControlbar;
  70. m_pControlbar = NULL;
  71. pControlbarTemp->Release();
  72. }
  73. return hr;
  74. }
  75. CBitmap bm;
  76. if (m_pComponentData->QuerySnapinType() == SNAPINTYPE_DS)
  77. {
  78. //
  79. // Store the control bar interface pointer
  80. //
  81. if (m_pControlbar == NULL)
  82. {
  83. m_pControlbar = pControlbar;
  84. TRACE(L"CDSEvent::SetControlbar() m_pControlbar->AddRef()\n",m_pControlbar);
  85. m_pControlbar->AddRef();
  86. }
  87. //
  88. // Create the toolbar if necessary
  89. //
  90. if (m_pToolbar == NULL)
  91. {
  92. hr = m_pControlbar->Create (TOOLBAR,
  93. this,
  94. (IUnknown **) &m_pToolbar);
  95. if (SUCCEEDED(hr))
  96. {
  97. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  98. bm.LoadBitmap (MAKEINTRESOURCE (IDB_BUTTONS));
  99. LoadToolbarStrings (g_DSAdmin_SnapinButtons);
  100. hr = m_pToolbar->AddBitmap (cButtons, (HBITMAP)bm, 16, 16, RGB(255,0,255));
  101. hr = m_pToolbar->AddButtons (cButtons, g_DSAdmin_SnapinButtons);
  102. }
  103. }
  104. else
  105. {
  106. hr = m_pControlbar->Attach (TOOLBAR, (IUnknown *) m_pToolbar);
  107. }
  108. m_UseSelectionParent = FALSE;
  109. }
  110. return hr;
  111. }
  112. HRESULT CDSEvent::LoadToolbarStrings (MMCBUTTON * Buttons)
  113. {
  114. if (g_astrButtonStrings.m_astr == NULL )
  115. {
  116. // load strings
  117. g_astrButtonStrings.m_astr = new CString[2*cButtons];
  118. for (UINT i = 0; i < cButtons; i++)
  119. {
  120. UINT iButtonTextId = 0, iTooltipTextId = 0;
  121. switch (Buttons[i].idCommand)
  122. {
  123. case dsNewUser:
  124. iButtonTextId = IDS_BUTTON_NEW_USER;
  125. iTooltipTextId = IDS_TOOLTIP_NEW_USER;
  126. break;
  127. case dsNewGroup:
  128. iButtonTextId = IDS_BUTTON_NEW_GROUP;
  129. iTooltipTextId = IDS_TOOLTIP_NEW_GROUP;
  130. break;
  131. case dsNewOU:
  132. iButtonTextId = IDS_BUTTON_NEW_OU;
  133. iTooltipTextId = IDS_TOOLTIP_NEW_OU;
  134. break;
  135. case dsFilter:
  136. iButtonTextId = IDS_BUTTON_FILTER;
  137. iTooltipTextId = IDS_TOOLTIP_FILTER;
  138. break;
  139. case dsFind:
  140. iButtonTextId = IDS_BUTTON_FIND;
  141. iTooltipTextId = IDS_TOOLTIP_FIND;
  142. break;
  143. case dsAddMember:
  144. iButtonTextId = IDS_BUTTON_ADD_MEMBER;
  145. iTooltipTextId = IDS_TOOLTIP_ADD_MEMBER;
  146. break;
  147. default:
  148. ASSERT(FALSE);
  149. break;
  150. }
  151. g_astrButtonStrings.m_astr[i*2].LoadString(iButtonTextId);
  152. Buttons[i].lpButtonText =
  153. const_cast<BSTR>((LPCTSTR)(g_astrButtonStrings.m_astr[i*2]));
  154. g_astrButtonStrings.m_astr[(i*2)+1].LoadString(iTooltipTextId);
  155. Buttons[i].lpTooltipText =
  156. const_cast<BSTR>((LPCTSTR)(g_astrButtonStrings.m_astr[(i*2)+1]));
  157. }
  158. }
  159. return S_OK;
  160. }
  161. HRESULT CDSEvent::ControlbarNotify (MMC_NOTIFY_TYPE event,
  162. LPARAM arg,
  163. LPARAM param)
  164. {
  165. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  166. CWaitCursor cwait;
  167. HRESULT hr = S_OK;
  168. if (m_pControlbar == NULL)
  169. {
  170. return hr;
  171. }
  172. BOOL bSelect;
  173. BOOL bScope;
  174. LPDATAOBJECT pDO = NULL;
  175. CDSCookie* pSelectedCookie = NULL;
  176. CDSCookie* pContainerCookie = NULL;
  177. CInternalFormatCracker dobjCracker;
  178. CUINode* pUINode = NULL, *pNode = NULL;
  179. switch (event)
  180. {
  181. case MMCN_SELECT:
  182. m_pControlbar->Attach (TOOLBAR,
  183. (IUnknown *) m_pToolbar);
  184. bSelect = HIWORD(arg);
  185. bScope = LOWORD(arg);
  186. if (bSelect)
  187. {
  188. pDO = (LPDATAOBJECT)param;
  189. dobjCracker.Extract(pDO);
  190. pUINode = dobjCracker.GetCookie();
  191. if (pUINode->IsSnapinRoot())
  192. {
  193. m_pToolbar->SetButtonState(dsNewUser,
  194. ENABLED,
  195. FALSE);
  196. m_pToolbar->SetButtonState(dsNewGroup,
  197. ENABLED,
  198. FALSE);
  199. m_pToolbar->SetButtonState(dsNewOU,
  200. ENABLED,
  201. FALSE);
  202. m_pToolbar->SetButtonState(dsFind,
  203. ENABLED,
  204. FALSE);
  205. m_pToolbar->SetButtonState(dsFilter,
  206. ENABLED,
  207. TRUE);
  208. m_pToolbar->SetButtonState(dsAddMember,
  209. ENABLED,
  210. FALSE);
  211. return hr;
  212. }
  213. if (IS_CLASS(*pUINode, CDSUINode))
  214. {
  215. pSelectedCookie = GetDSCookieFromUINode(pUINode);
  216. pContainerCookie = pSelectedCookie;
  217. pNode = pUINode;
  218. }
  219. else
  220. {
  221. //
  222. // Disable all buttons for non-DS nodes
  223. //
  224. m_pToolbar->SetButtonState (dsNewUser,
  225. ENABLED,
  226. FALSE);
  227. m_pToolbar->SetButtonState (dsNewGroup,
  228. ENABLED,
  229. FALSE);
  230. m_pToolbar->SetButtonState (dsNewOU,
  231. ENABLED,
  232. FALSE);
  233. m_pToolbar->SetButtonState (dsFind,
  234. ENABLED,
  235. FALSE);
  236. m_pToolbar->SetButtonState (dsFilter,
  237. ENABLED,
  238. FALSE);
  239. m_pToolbar->SetButtonState (dsAddMember,
  240. ENABLED,
  241. FALSE);
  242. return S_OK;
  243. }
  244. if (bScope)
  245. {
  246. pContainerCookie = pSelectedCookie;
  247. m_UseSelectionParent = FALSE;
  248. }
  249. else
  250. {
  251. pNode = pUINode->GetParent();
  252. if (IS_CLASS(*pNode, CDSUINode))
  253. {
  254. pContainerCookie = GetDSCookieFromUINode(pNode);
  255. }
  256. m_UseSelectionParent = TRUE;
  257. }
  258. if (pContainerCookie != NULL)
  259. {
  260. if (pNode->IsContainer())
  261. {
  262. int resultUser = -2, resultGroup = -2, resultOU = -2;
  263. resultUser = IsCreateAllowed (L"user", pContainerCookie);
  264. if ( resultUser != -2)
  265. {
  266. resultGroup = IsCreateAllowed(L"group", pContainerCookie);
  267. if (resultGroup != -2)
  268. {
  269. resultOU = IsCreateAllowed (L"organizationalUnit", pContainerCookie);
  270. }
  271. }
  272. m_pToolbar->SetButtonState (dsNewUser,
  273. ENABLED,
  274. resultUser >= 0);
  275. m_pToolbar->SetButtonState (dsNewGroup,
  276. ENABLED,
  277. resultGroup >= 0);
  278. m_pToolbar->SetButtonState (dsNewOU,
  279. ENABLED,
  280. resultOU >= 0);
  281. m_pToolbar->SetButtonState (dsFind,
  282. ENABLED,
  283. TRUE);
  284. m_pToolbar->SetButtonState (dsFilter,
  285. ENABLED,
  286. TRUE);
  287. }
  288. else
  289. {
  290. m_pToolbar->SetButtonState (dsNewUser,
  291. ENABLED,
  292. FALSE);
  293. m_pToolbar->SetButtonState (dsNewGroup,
  294. ENABLED,
  295. FALSE);
  296. m_pToolbar->SetButtonState (dsNewOU,
  297. ENABLED,
  298. FALSE);
  299. m_pToolbar->SetButtonState (dsFind,
  300. ENABLED,
  301. FALSE);
  302. }
  303. if ((wcscmp(pSelectedCookie->GetClass(), L"contact")==0) ||
  304. (wcscmp(pSelectedCookie->GetClass(), L"user")==0))
  305. {
  306. m_pToolbar->SetButtonState (dsAddMember,
  307. ENABLED,
  308. TRUE);
  309. }
  310. else
  311. {
  312. m_pToolbar->SetButtonState (dsAddMember,
  313. ENABLED,
  314. FALSE);
  315. }
  316. }
  317. else
  318. {
  319. m_UseSelectionParent = FALSE;
  320. }
  321. }
  322. break;
  323. case MMCN_BTN_CLICK:
  324. TRACE(_T("Button clicked. param is %d. pDataObj is %lx.\n"),
  325. param, arg);
  326. switch (param)
  327. {
  328. case dsNewUser:
  329. ToolbarCreateObject (CString (L"user"),
  330. (LPDATAOBJECT) arg);
  331. break;
  332. case dsNewGroup:
  333. ToolbarCreateObject (CString (L"group"),
  334. (LPDATAOBJECT) arg);
  335. break;
  336. case dsNewOU:
  337. ToolbarCreateObject (CString (L"organizationalUnit"),
  338. (LPDATAOBJECT) arg);
  339. break;
  340. case dsFilter:
  341. ToolbarFilter();
  342. break;
  343. case dsFind:
  344. ToolbarFind ((LPDATAOBJECT) arg);
  345. break;
  346. case dsAddMember:
  347. ToolbarAddMember((LPDATAOBJECT)arg);
  348. break;
  349. }
  350. break;
  351. }
  352. return hr;
  353. }
  354. HRESULT CDSEvent::ToolbarCreateObject (CString csClass,
  355. LPDATAOBJECT lpDataObj)
  356. {
  357. HRESULT hr = S_OK;
  358. CUINode* pSelectedUINode = NULL;
  359. CUINode* pUINode = NULL;
  360. CDSUINode* pDSUINode = NULL;
  361. CDSCookie * pCookie = NULL;
  362. int objIndex = 0;
  363. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  364. if (lpDataObj)
  365. {
  366. CInternalFormatCracker dobjCracker;
  367. VERIFY(SUCCEEDED(dobjCracker.Extract(lpDataObj)));
  368. pUINode = dobjCracker.GetCookie();
  369. }
  370. else
  371. {
  372. pUINode = m_pSelectedFolderNode;
  373. }
  374. //
  375. // Hold on to this so we can unselect it when we
  376. // create the new object
  377. //
  378. pSelectedUINode = pUINode;
  379. bool bUsingParent = false;
  380. if (!pUINode->IsContainer() &&
  381. m_UseSelectionParent)
  382. {
  383. pUINode = pUINode->GetParent();
  384. bUsingParent = true;
  385. }
  386. if (IS_CLASS(*pUINode, CDSUINode))
  387. {
  388. pCookie = GetDSCookieFromUINode(pUINode);
  389. pDSUINode = dynamic_cast<CDSUINode*>(pUINode);
  390. }
  391. ASSERT(pCookie != NULL && pDSUINode != NULL);
  392. if (pCookie == NULL || pDSUINode == NULL)
  393. {
  394. return E_INVALIDARG;
  395. }
  396. objIndex = IsCreateAllowed(csClass, pCookie);
  397. if (objIndex >= 0)
  398. {
  399. CDSUINode * pNewNode= NULL;
  400. hr = m_pComponentData->_CreateDSObject (pDSUINode,
  401. pCookie->GetChildListEntry(objIndex),
  402. NULL,
  403. &pNewNode);
  404. if (SUCCEEDED(hr) && (hr != S_FALSE) && (pNewNode != NULL))
  405. {
  406. m_pFrame->UpdateAllViews(lpDataObj,
  407. (LPARAM)pNewNode,
  408. (bUsingParent)
  409. ? DS_CREATE_OCCURRED_RESULT_PANE :
  410. DS_CREATE_OCCURRED);
  411. m_pFrame->UpdateAllViews(lpDataObj, (LPARAM)pSelectedUINode, DS_UNSELECT_OBJECT);
  412. }
  413. m_pFrame->UpdateAllViews(NULL, NULL, DS_UPDATE_OBJECT_COUNT);
  414. }
  415. return hr;
  416. }
  417. HRESULT CDSEvent::ToolbarAddMember(LPDATAOBJECT pDataObj)
  418. {
  419. HRESULT hr = S_OK;
  420. CObjectNamesFormatCracker objectNamesFormat;
  421. CInternalFormatCracker internalFormat;
  422. LPDATAOBJECT pDO = NULL;
  423. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  424. if (pDataObj)
  425. {
  426. pDO = internalFormat.ExtractMultiSelect(pDataObj);
  427. if (pDO == NULL)
  428. {
  429. pDO = pDataObj;
  430. }
  431. hr = objectNamesFormat.Extract(pDO);
  432. if (FAILED(hr))
  433. {
  434. return hr;
  435. }
  436. //
  437. // we need at least one object in the selection
  438. //
  439. ASSERT(objectNamesFormat.HasData());
  440. if (objectNamesFormat.GetCount() == 0)
  441. {
  442. TRACE (_T("DSToolbar::AddMember: can't find path\n"));
  443. return E_INVALIDARG;
  444. }
  445. }
  446. hr = AddDataObjListToGroup (&objectNamesFormat, m_hwnd, m_pComponentData);
  447. TRACE (_T("AddDataObjListToGroup returned hr = %lx\n"), hr);
  448. return hr;
  449. }
  450. HRESULT CDSEvent::ToolbarFilter()
  451. {
  452. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  453. if (m_pComponentData->CanRefreshAll())
  454. {
  455. if (m_pComponentData->m_pQueryFilter->EditFilteringOptions())
  456. {
  457. m_pComponentData->m_bDirty = TRUE;
  458. m_pComponentData->RefreshAll();
  459. }
  460. }
  461. return S_OK;
  462. }
  463. HRESULT CDSEvent::ToolbarFind(LPDATAOBJECT lpDataObj)
  464. {
  465. HRESULT hr = S_OK;
  466. CUINode* pUINode = NULL;
  467. CDSCookie * pCookie = NULL;
  468. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  469. if (lpDataObj)
  470. {
  471. CInternalFormatCracker dobjCracker;
  472. VERIFY(SUCCEEDED(dobjCracker.Extract(lpDataObj)));
  473. pUINode = dobjCracker.GetCookie();
  474. }
  475. else
  476. {
  477. pUINode = m_pSelectedFolderNode;
  478. }
  479. if (m_UseSelectionParent)
  480. {
  481. pUINode = pUINode->GetParent();
  482. }
  483. if (IS_CLASS(*pUINode, CDSUINode))
  484. {
  485. pCookie = GetDSCookieFromUINode(pUINode);
  486. }
  487. ASSERT(pCookie != NULL);
  488. if (pCookie == NULL)
  489. {
  490. return E_INVALIDARG;
  491. }
  492. m_pComponentData->m_ActiveDS->DSFind(m_pComponentData->GetHWnd(), pCookie->GetPath());
  493. return hr;
  494. }
  495. INT
  496. CDSEvent::IsCreateAllowed(CString csClass,
  497. CDSCookie * pContainer)
  498. {
  499. WCHAR ** ppChildren = NULL;
  500. HRESULT hr = S_OK;
  501. ppChildren = pContainer->GetChildList();
  502. if (ppChildren == NULL)
  503. {
  504. hr = m_pComponentData->FillInChildList(pContainer);
  505. if (hr == ERROR_DS_SERVER_DOWN)
  506. {
  507. return -2;
  508. }
  509. ppChildren = pContainer->GetChildList();
  510. }
  511. INT cChildClasses = pContainer->GetChildCount();
  512. INT i = 0;
  513. //
  514. // needs finishing
  515. //
  516. while (i < cChildClasses)
  517. {
  518. if (csClass == CString(ppChildren[i]))
  519. {
  520. return i;
  521. }
  522. else
  523. {
  524. i++;
  525. }
  526. }
  527. if (i == cChildClasses)
  528. {
  529. return -1;
  530. }
  531. return i;
  532. }