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.

424 lines
10 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. if (insertionAllowed & CCM_INSERTIONALLOWED_TOP)
  85. {
  86. ULONG flags = getToolbarFlags(view);
  87. CONTEXTMENUITEM cmi;
  88. memset(&cmi, 0, sizeof(cmi));
  89. cmi.lInsertionPointID = CCM_INSERTIONPOINTID_PRIMARY_TOP;
  90. cmi.strName = moveUp;
  91. cmi.lCommandID = 0;
  92. cmi.fFlags = (flags & MOVE_UP_ALLOWED) ? MF_ENABLED : MF_GRAYED;
  93. callback->AddItem(&cmi);
  94. cmi.strName = moveDown;
  95. cmi.lCommandID = 1;
  96. cmi.fFlags = (flags & MOVE_DN_ALLOWED) ? MF_ENABLED : MF_GRAYED;
  97. callback->AddItem(&cmi);
  98. }
  99. return S_OK;
  100. }
  101. int ProxyPolicy::compare(
  102. SnapInDataItem& item,
  103. int column
  104. ) throw ()
  105. {
  106. if (column == 0)
  107. {
  108. return wcscmp(name, static_cast<ProxyPolicy&>(item).name);
  109. }
  110. else
  111. {
  112. LONG merit2 = static_cast<ProxyPolicy&>(item).merit;
  113. if (merit < merit2) { return -1; }
  114. if (merit > merit2) { return +1; }
  115. return 0;
  116. }
  117. }
  118. HRESULT ProxyPolicy::createPropertyPages(
  119. SnapInView& view,
  120. LPPROPERTYSHEETCALLBACK provider,
  121. LONG_PTR handle
  122. )
  123. {
  124. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  125. ProxyPolicyPage* page = new ProxyPolicyPage(
  126. handle,
  127. (LPARAM)this,
  128. self,
  129. getProfile(),
  130. getParent().getCxn()
  131. );
  132. page->addToMMCSheet(provider);
  133. return S_OK;
  134. }
  135. HRESULT ProxyPolicy::onDelete(
  136. SnapInView& view
  137. )
  138. {
  139. HRESULT hr = SdoResultItem::onDelete(view);
  140. if (hr == S_OK)
  141. {
  142. getParent().getCxn().getProxyProfiles().remove(getProfile());
  143. // We have to renumber the policies to refresh the view.
  144. CheckError(view.getConsole()->UpdateAllViews(&parent, 0, 0));
  145. }
  146. return hr;
  147. }
  148. HRESULT ProxyPolicy::onMenuCommand(
  149. SnapInView& view,
  150. long commandId
  151. )
  152. {
  153. return getParent().movePolicy(view, *this, commandId);
  154. }
  155. HRESULT ProxyPolicy::onRename(
  156. SnapInView& view,
  157. LPCOLESTR newName
  158. )
  159. {
  160. // Make sure we have the profile before we rename otherwise, we won't be
  161. // able to find it.
  162. getProfile();
  163. HRESULT hr = SdoResultItem::onRename(view, newName);
  164. if (hr == S_OK)
  165. {
  166. self.setValue(PROPERTY_POLICY_PROFILE_NAME, name);
  167. self.apply();
  168. profile.setName(name);
  169. profile.apply();
  170. }
  171. return hr;
  172. }
  173. HRESULT ProxyPolicy::onToolbarButtonClick(
  174. SnapInView& view,
  175. int buttonId
  176. )
  177. {
  178. return getParent().movePolicy(view, *this, buttonId);
  179. }
  180. HRESULT ProxyPolicy::onToolbarSelect(
  181. SnapInView& view,
  182. BOOL scopeItem,
  183. BOOL selected
  184. )
  185. {
  186. if (selected)
  187. {
  188. // Attach the toolbar ...
  189. IToolbar* toolbar = view.attachToolbar(TOOLBAR_POLICY);
  190. // ... and set button state according to the toolbar flags.
  191. ULONG flags = getToolbarFlags(view);
  192. toolbar->SetButtonState(
  193. 0,
  194. ENABLED,
  195. ((flags & MOVE_UP_ALLOWED) ? TRUE : FALSE)
  196. );
  197. toolbar->SetButtonState(
  198. 1,
  199. ENABLED,
  200. ((flags & MOVE_DN_ALLOWED) ? TRUE : FALSE)
  201. );
  202. }
  203. else
  204. {
  205. // We're going away so detach.
  206. view.detachToolbar(TOOLBAR_POLICY);
  207. }
  208. return S_OK;
  209. }
  210. HRESULT ProxyPolicy::onContextHelp(SnapInView& view) throw ()
  211. {
  212. return view.displayHelp(L"ias_ops.chm::/sag_ias_crp_policies.htm");
  213. }
  214. int __cdecl ProxyPolicy::SortByMerit(
  215. const SdoResultItem* const* t1,
  216. const SdoResultItem* const* t2
  217. ) throw ()
  218. {
  219. return ((const ProxyPolicy*)*t1)->merit - ((const ProxyPolicy*)*t2)->merit;
  220. }
  221. UINT ProxyPolicy::mapResourceId(ResourceId id) const throw ()
  222. {
  223. static UINT resourceIds[] =
  224. {
  225. IMAGE_PROXY_POLICY,
  226. IDS_POLICY_DELETE_CAPTION,
  227. IDS_POLICY_DELETE_LOCAL,
  228. IDS_POLICY_DELETE_REMOTE,
  229. IDS_POLICY_DELETE_LAST_LOCAL,
  230. IDS_POLICY_DELETE_LAST_REMOTE,
  231. IDS_POLICY_E_CAPTION,
  232. IDS_POLICY_E_RENAME,
  233. IDS_POLICY_E_NAME_EMPTY
  234. };
  235. return resourceIds[id];
  236. }
  237. ProxyPolicies::ProxyPolicies(SdoConnection& connection)
  238. : SdoScopeItem(
  239. connection,
  240. IDS_POLICY_NODE,
  241. IDS_POLICY_E_CAPTION,
  242. IDS_POLICY_MENU_TOP,
  243. IDS_POLICY_MENU_NEW
  244. ),
  245. nameColumn(IDS_POLICY_COLUMN_NAME),
  246. orderColumn(IDS_POLICY_COLUMN_ORDER)
  247. {
  248. }
  249. ProxyPolicies::~ProxyPolicies() throw ()
  250. { }
  251. HRESULT ProxyPolicies::movePolicy(
  252. SnapInView& view,
  253. ProxyPolicy& policy,
  254. LONG commandId
  255. )
  256. {
  257. // Get the current toolbar flags.
  258. ULONG flags = policy.getToolbarFlags(view);
  259. // Use the current merit as the starting point ...
  260. LONG newMerit = policy.getMerit();
  261. // ... and adjust depending on the the button clicked.
  262. switch (commandId)
  263. {
  264. case 0:
  265. {
  266. if (!(flags & ProxyPolicy::MOVE_UP_ALLOWED)) { return S_FALSE; }
  267. (flags & ProxyPolicy::ORDER_REVERSED) ? ++newMerit : --newMerit;
  268. break;
  269. }
  270. case 1:
  271. {
  272. if (!(flags & ProxyPolicy::MOVE_DN_ALLOWED)) { return S_FALSE; }
  273. (flags & ProxyPolicy::ORDER_REVERSED) ? --newMerit : ++newMerit;
  274. break;
  275. }
  276. default:
  277. return S_FALSE;
  278. }
  279. // Swap their merits.
  280. ProxyPolicy& policy2 = getPolicyByMerit(newMerit);
  281. policy2.setMerit(policy.getMerit());
  282. policy.setMerit(newMerit);
  283. // Re-sort our vector.
  284. items.sort(ProxyPolicy::SortByMerit);
  285. // If the view isn't sorted, ...
  286. if (view.getSortOption() & RSI_NOSORTICON)
  287. {
  288. // ... we'll sort by merit anyway.
  289. view.getResultData()->Sort(1, RSI_NOSORTICON, 0);
  290. }
  291. else if (view.getSortColumn() == 1)
  292. {
  293. // Otherwise only sort if the merit column is selected.
  294. view.reSort();
  295. }
  296. // Update the toolbar buttons based on the new state.
  297. policy.onToolbarSelect(view, FALSE, TRUE);
  298. // The configuration has changed, so tell IAS to reload.
  299. cxn.resetService();
  300. return S_OK;
  301. }
  302. HRESULT ProxyPolicies::onContextHelp(SnapInView& view) throw ()
  303. {
  304. return view.displayHelp(L"ias_ops.chm::/sag_ias_crp_policies.htm");
  305. }
  306. SdoCollection ProxyPolicies::getSelf()
  307. {
  308. return cxn.getProxyPolicies();
  309. }
  310. void ProxyPolicies::getResultItems(SdoEnum& src, ResultItems& dst)
  311. {
  312. // Convert the SDOs to ProxyPolicy objects.
  313. Sdo itemSdo;
  314. while (src.next(itemSdo))
  315. {
  316. CComPtr<ProxyPolicy> newItem(new (AfxThrow) ProxyPolicy(
  317. *this,
  318. itemSdo
  319. ));
  320. dst.push_back(newItem);
  321. }
  322. // Sort by merit.
  323. dst.sort(ProxyPolicy::SortByMerit);
  324. // Normalize.
  325. LONG merit = 0;
  326. for (ResultIterator i = dst.begin(); i != dst.end(); ++i)
  327. {
  328. ((ProxyPolicy*)*i)->setMerit(++merit);
  329. }
  330. }
  331. void ProxyPolicies::insertColumns(IHeaderCtrl2* headerCtrl)
  332. {
  333. CheckError(headerCtrl->InsertColumn(0, nameColumn, LVCFMT_LEFT, 235));
  334. CheckError(headerCtrl->InsertColumn(1, orderColumn, LVCFMT_LEFT, 100));
  335. }
  336. HRESULT ProxyPolicies::onMenuCommand(
  337. SnapInView& view,
  338. long commandId
  339. )
  340. {
  341. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  342. // Fire up the wizard.
  343. NewPolicyWizard wizard(cxn, &view);
  344. if (wizard.DoModal() != IDCANCEL)
  345. {
  346. // We've changed every policy, so refresh the view.
  347. CheckError(view.getConsole()->UpdateAllViews(this, 0, 0));
  348. // Tell the service to reload
  349. cxn.resetService();
  350. }
  351. return S_OK;
  352. }
  353. void ProxyPolicies::propertyChanged(SnapInView& view, IASPROPERTIES id)
  354. {
  355. if (id == PROPERTY_IAS_PROXYPOLICIES_COLLECTION)
  356. {
  357. CheckError(view.getConsole()->UpdateAllViews(this, 0, 0));
  358. }
  359. }