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.

428 lines
11 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // proxypolicies.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the classes ProxyPolicy and ProxyPolicies.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/10/2000 Original version.
  16. // 04/19/2000 SdoScopeItem::getSelf returns by value, not reference.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <proxypch.h>
  20. #include <proxypolicies.h>
  21. #include <policypage.h>
  22. #include <policywiz.h>
  23. #include <proxynode.h>
  24. ProxyPolicy::ProxyPolicy(
  25. SdoScopeItem& owner,
  26. ISdo* sdo
  27. )
  28. : SdoResultItem(owner, sdo)
  29. {
  30. // Cache an integer ...
  31. self.getValue(PROPERTY_POLICY_MERIT, merit);
  32. // ... and string version of our merit.
  33. _ltow(merit, szMerit, 10);
  34. }
  35. Sdo& ProxyPolicy::getProfile()
  36. {
  37. if (!profile)
  38. {
  39. profile = getParent().getCxn().getProxyProfiles().find(name);
  40. }
  41. return profile;
  42. }
  43. ULONG ProxyPolicy::getToolbarFlags(const SnapInView& view) throw ()
  44. {
  45. // Is the order reversed ?
  46. BOOL reversed = (view.getSortColumn() == 1) &&
  47. (view.getSortOption() & RSI_DESCENDING);
  48. ULONG flags = reversed ? ORDER_REVERSED : 0;
  49. // Are we the highest priority policy?
  50. if (merit != 1)
  51. {
  52. flags |= reversed ? MOVE_DN_ALLOWED : MOVE_UP_ALLOWED;
  53. }
  54. // Are we the lowest priority policy?
  55. if (merit != parent.getNumItems())
  56. {
  57. flags |= reversed ? MOVE_UP_ALLOWED : MOVE_DN_ALLOWED;
  58. }
  59. return flags;
  60. }
  61. void ProxyPolicy::setMerit(LONG newMerit)
  62. {
  63. // Check if it's dirty to save excessive writes to the SDOs.
  64. if (newMerit != merit)
  65. {
  66. merit = newMerit;
  67. _ltow(merit, szMerit, 10);
  68. self.setValue(PROPERTY_POLICY_MERIT, merit);
  69. self.apply();
  70. }
  71. }
  72. PCWSTR ProxyPolicy::getDisplayName(int column) const throw ()
  73. {
  74. return column ? szMerit : name;
  75. }
  76. HRESULT ProxyPolicy::addMenuItems(
  77. SnapInView& view,
  78. LPCONTEXTMENUCALLBACK callback,
  79. long insertionAllowed
  80. )
  81. {
  82. static ResourceString moveUp(IDS_POLICY_MOVE_UP);
  83. static ResourceString moveDown(IDS_POLICY_MOVE_DOWN);
  84. static ResourceString moveUpStatusBar(IDS_POLICY_MOVE_UP_STATUS_BAR);
  85. static ResourceString moveDownStatusBar(IDS_POLICY_MOVE_DOWN_STATUS_BAR);
  86. if (insertionAllowed & CCM_INSERTIONALLOWED_TOP)
  87. {
  88. ULONG flags = getToolbarFlags(view);
  89. CONTEXTMENUITEM cmi;
  90. memset(&cmi, 0, sizeof(cmi));
  91. cmi.lInsertionPointID = CCM_INSERTIONPOINTID_PRIMARY_TOP;
  92. cmi.strName = moveUp;
  93. cmi.strStatusBarText = moveUpStatusBar;
  94. cmi.lCommandID = 0;
  95. cmi.fFlags = (flags & MOVE_UP_ALLOWED) ? MF_ENABLED : MF_GRAYED;
  96. callback->AddItem(&cmi);
  97. cmi.strName = moveDown;
  98. cmi.strStatusBarText = moveDownStatusBar;
  99. cmi.lCommandID = 1;
  100. cmi.fFlags = (flags & MOVE_DN_ALLOWED) ? MF_ENABLED : MF_GRAYED;
  101. callback->AddItem(&cmi);
  102. }
  103. return S_OK;
  104. }
  105. int ProxyPolicy::compare(
  106. SnapInDataItem& item,
  107. int column
  108. ) throw ()
  109. {
  110. if (column == 0)
  111. {
  112. return wcscmp(name, static_cast<ProxyPolicy&>(item).name);
  113. }
  114. else
  115. {
  116. LONG merit2 = static_cast<ProxyPolicy&>(item).merit;
  117. if (merit < merit2) { return -1; }
  118. if (merit > merit2) { return +1; }
  119. return 0;
  120. }
  121. }
  122. HRESULT ProxyPolicy::createPropertyPages(
  123. SnapInView& view,
  124. LPPROPERTYSHEETCALLBACK provider,
  125. LONG_PTR handle
  126. )
  127. {
  128. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  129. ProxyPolicyPage* page = new ProxyPolicyPage(
  130. handle,
  131. (LPARAM)this,
  132. self,
  133. getProfile(),
  134. getParent().getCxn()
  135. );
  136. page->addToMMCSheet(provider);
  137. return S_OK;
  138. }
  139. HRESULT ProxyPolicy::onDelete(
  140. SnapInView& view
  141. )
  142. {
  143. HRESULT hr = SdoResultItem::onDelete(view);
  144. if (hr == S_OK)
  145. {
  146. getParent().getCxn().getProxyProfiles().remove(getProfile());
  147. // We have to renumber the policies to refresh the view.
  148. view.updateAllViews(parent);
  149. }
  150. return hr;
  151. }
  152. HRESULT ProxyPolicy::onMenuCommand(
  153. SnapInView& view,
  154. long commandId
  155. )
  156. {
  157. return getParent().movePolicy(view, *this, commandId);
  158. }
  159. HRESULT ProxyPolicy::onRename(
  160. SnapInView& view,
  161. LPCOLESTR newName
  162. )
  163. {
  164. // Make sure we have the profile before we rename otherwise, we won't be
  165. // able to find it.
  166. getProfile();
  167. HRESULT hr = SdoResultItem::onRename(view, newName);
  168. if (hr == S_OK)
  169. {
  170. self.setValue(PROPERTY_POLICY_PROFILE_NAME, name);
  171. self.apply();
  172. profile.setName(name);
  173. profile.apply();
  174. }
  175. return hr;
  176. }
  177. HRESULT ProxyPolicy::onToolbarButtonClick(
  178. SnapInView& view,
  179. int buttonId
  180. )
  181. {
  182. return getParent().movePolicy(view, *this, buttonId);
  183. }
  184. HRESULT ProxyPolicy::onToolbarSelect(
  185. SnapInView& view,
  186. BOOL scopeItem,
  187. BOOL selected
  188. )
  189. {
  190. if (selected)
  191. {
  192. // Attach the toolbar ...
  193. IToolbar* toolbar = view.attachToolbar(TOOLBAR_POLICY);
  194. // ... and set button state according to the toolbar flags.
  195. ULONG flags = getToolbarFlags(view);
  196. toolbar->SetButtonState(
  197. 0,
  198. ENABLED,
  199. ((flags & MOVE_UP_ALLOWED) ? TRUE : FALSE)
  200. );
  201. toolbar->SetButtonState(
  202. 1,
  203. ENABLED,
  204. ((flags & MOVE_DN_ALLOWED) ? TRUE : FALSE)
  205. );
  206. }
  207. else
  208. {
  209. // We're going away so detach.
  210. view.detachToolbar(TOOLBAR_POLICY);
  211. }
  212. return S_OK;
  213. }
  214. HRESULT ProxyPolicy::onContextHelp(SnapInView& view) throw ()
  215. {
  216. return view.displayHelp(L"ias_ops.chm::/sag_ias_crp_policies.htm");
  217. }
  218. int __cdecl ProxyPolicy::SortByMerit(
  219. const SdoResultItem* const* t1,
  220. const SdoResultItem* const* t2
  221. ) throw ()
  222. {
  223. return ((const ProxyPolicy*)*t1)->merit - ((const ProxyPolicy*)*t2)->merit;
  224. }
  225. UINT ProxyPolicy::mapResourceId(ResourceId id) const throw ()
  226. {
  227. static UINT resourceIds[] =
  228. {
  229. IMAGE_PROXY_POLICY,
  230. IDS_POLICY_DELETE_CAPTION,
  231. IDS_POLICY_DELETE_LOCAL,
  232. IDS_POLICY_DELETE_REMOTE,
  233. IDS_POLICY_DELETE_LAST_LOCAL,
  234. IDS_POLICY_DELETE_LAST_REMOTE,
  235. IDS_POLICY_E_CAPTION,
  236. IDS_POLICY_E_RENAME,
  237. IDS_POLICY_E_NAME_EMPTY
  238. };
  239. return resourceIds[id];
  240. }
  241. ProxyPolicies::ProxyPolicies(SdoConnection& connection)
  242. : SdoScopeItem(
  243. connection,
  244. IDS_POLICY_NODE,
  245. IDS_POLICY_E_CAPTION,
  246. IDS_POLICY_MENU_TOP,
  247. IDS_POLICY_MENU_NEW,
  248. IDS_POLICY_MENU_STATUS_BAR
  249. ),
  250. nameColumn(IDS_POLICY_COLUMN_NAME),
  251. orderColumn(IDS_POLICY_COLUMN_ORDER)
  252. {
  253. }
  254. ProxyPolicies::~ProxyPolicies() throw ()
  255. { }
  256. HRESULT ProxyPolicies::movePolicy(
  257. SnapInView& view,
  258. ProxyPolicy& policy,
  259. LONG commandId
  260. )
  261. {
  262. // Get the current toolbar flags.
  263. ULONG flags = policy.getToolbarFlags(view);
  264. // Use the current merit as the starting point ...
  265. LONG newMerit = policy.getMerit();
  266. // ... and adjust depending on the the button clicked.
  267. switch (commandId)
  268. {
  269. case 0:
  270. {
  271. if (!(flags & ProxyPolicy::MOVE_UP_ALLOWED)) { return S_FALSE; }
  272. (flags & ProxyPolicy::ORDER_REVERSED) ? ++newMerit : --newMerit;
  273. break;
  274. }
  275. case 1:
  276. {
  277. if (!(flags & ProxyPolicy::MOVE_DN_ALLOWED)) { return S_FALSE; }
  278. (flags & ProxyPolicy::ORDER_REVERSED) ? --newMerit : ++newMerit;
  279. break;
  280. }
  281. default:
  282. return S_FALSE;
  283. }
  284. // Swap their merits.
  285. ProxyPolicy& policy2 = getPolicyByMerit(newMerit);
  286. policy2.setMerit(policy.getMerit());
  287. policy.setMerit(newMerit);
  288. // Re-sort our vector.
  289. items.sort(ProxyPolicy::SortByMerit);
  290. // If the view isn't sorted, ...
  291. if (view.getSortOption() & RSI_NOSORTICON)
  292. {
  293. // ... we'll sort by merit anyway.
  294. view.getResultData()->Sort(1, RSI_NOSORTICON, 0);
  295. }
  296. else
  297. {
  298. view.reSort();
  299. }
  300. // Update the toolbar buttons based on the new state.
  301. policy.onToolbarSelect(view, FALSE, TRUE);
  302. // The configuration has changed, so tell IAS to reload.
  303. cxn.resetService();
  304. return S_OK;
  305. }
  306. HRESULT ProxyPolicies::onContextHelp(SnapInView& view) throw ()
  307. {
  308. return view.displayHelp(L"ias_ops.chm::/sag_ias_crp_policies.htm");
  309. }
  310. SdoCollection ProxyPolicies::getSelf()
  311. {
  312. return cxn.getProxyPolicies();
  313. }
  314. void ProxyPolicies::getResultItems(SdoEnum& src, ResultItems& dst)
  315. {
  316. // Convert the SDOs to ProxyPolicy objects.
  317. Sdo itemSdo;
  318. while (src.next(itemSdo))
  319. {
  320. CComPtr<ProxyPolicy> newItem(new (AfxThrow) ProxyPolicy(
  321. *this,
  322. itemSdo
  323. ));
  324. dst.push_back(newItem);
  325. }
  326. // Sort by merit.
  327. dst.sort(ProxyPolicy::SortByMerit);
  328. // Normalize.
  329. LONG merit = 0;
  330. for (ResultIterator i = dst.begin(); i != dst.end(); ++i)
  331. {
  332. ((ProxyPolicy*)*i)->setMerit(++merit);
  333. }
  334. }
  335. void ProxyPolicies::insertColumns(IHeaderCtrl2* headerCtrl)
  336. {
  337. CheckError(headerCtrl->InsertColumn(0, nameColumn, LVCFMT_LEFT, 235));
  338. CheckError(headerCtrl->InsertColumn(1, orderColumn, LVCFMT_LEFT, 100));
  339. }
  340. HRESULT ProxyPolicies::onMenuCommand(
  341. SnapInView& view,
  342. long commandId
  343. )
  344. {
  345. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  346. // Fire up the wizard.
  347. NewPolicyWizard wizard(cxn, &view);
  348. if (wizard.DoModal() != IDCANCEL)
  349. {
  350. // We've changed every policy, so refresh the view.
  351. view.updateAllViews(*this);
  352. // Tell the service to reload
  353. cxn.resetService();
  354. }
  355. return S_OK;
  356. }
  357. void ProxyPolicies::propertyChanged(SnapInView& view, IASPROPERTIES id)
  358. {
  359. if (id == PROPERTY_IAS_PROXYPOLICIES_COLLECTION)
  360. {
  361. view.updateAllViews(*this);
  362. }
  363. }