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.

1177 lines
36 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. PrMrIe.cpp
  5. Abstract:
  6. Inclusion / Exclusion property Page.
  7. Author:
  8. Art Bragg [abragg] 08-Aug-1997
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "PrMrIe.h"
  13. #include "Rule.h"
  14. #include "joblib.h"
  15. #include "manvol.h"
  16. #include "IeList.h"
  17. static DWORD pHelpIds[] =
  18. {
  19. IDC_LIST_IE_LABEL, idh_rule_list,
  20. IDC_LIST_IE, idh_rule_list,
  21. IDC_BTN_ADD, idh_new_rule_button,
  22. IDC_BTN_REMOVE, idh_rule_delete_button,
  23. IDC_BTN_EDIT, idh_rule_edit_button,
  24. IDC_BTN_UP, idh_rule_up_button,
  25. IDC_BTN_DOWN, idh_rule_down_button,
  26. 0, 0
  27. };
  28. // Columns for listview control
  29. #define IE_COLUMN_ACTION 0
  30. #define IE_COLUMN_FILE_TYPE 1
  31. #define IE_COLUMN_PATH 2
  32. #define IE_COLUMN_ATTRS 3
  33. int CALLBACK CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort );
  34. int PathCollate( CString PathA, CString PathB );
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CPrMrIe property page
  37. CPrMrIe::CPrMrIe( ) : CSakVolPropPage( CPrMrIe::IDD )
  38. {
  39. //{{AFX_DATA_INIT( CPrMrIe )
  40. // NOTE: the ClassWizard will add member initialization here
  41. //}}AFX_DATA_INIT
  42. m_hConsoleHandle = NULL;
  43. m_LineCount = 0;
  44. m_pHelpIds = pHelpIds;
  45. }
  46. CPrMrIe::~CPrMrIe( )
  47. {
  48. int i;
  49. // Clean up old lines
  50. for( i = 0; i < m_LineCount; i++ ) {
  51. if( m_LineList[ i ] ) {
  52. delete m_LineList[ i ];
  53. }
  54. }
  55. m_LineCount = 0;
  56. }
  57. void CPrMrIe::DoDataExchange( CDataExchange* pDX )
  58. {
  59. CSakVolPropPage::DoDataExchange( pDX );
  60. //{{AFX_DATA_MAP( CPrMrIe )
  61. DDX_Control( pDX, IDC_BTN_UP, m_BtnUp );
  62. DDX_Control( pDX, IDC_BTN_REMOVE, m_BtnRemove );
  63. DDX_Control( pDX, IDC_BTN_EDIT, m_BtnEdit );
  64. DDX_Control( pDX, IDC_BTN_DOWN, m_BtnDown );
  65. DDX_Control( pDX, IDC_BTN_ADD, m_BtnAdd );
  66. DDX_Control( pDX, IDC_LIST_IE, m_listIncExc );
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP( CPrMrIe, CSakVolPropPage )
  70. //{{AFX_MSG_MAP( CPrMrIe )
  71. ON_BN_CLICKED( IDC_BTN_ADD, OnBtnAdd )
  72. ON_BN_CLICKED( IDC_BTN_DOWN, OnBtnDown )
  73. ON_BN_CLICKED( IDC_BTN_REMOVE, OnBtnRemove )
  74. ON_BN_CLICKED( IDC_BTN_UP, OnBtnUp )
  75. ON_BN_CLICKED( IDC_BTN_EDIT, OnBtnEdit )
  76. ON_WM_DESTROY( )
  77. ON_NOTIFY( NM_DBLCLK, IDC_LIST_IE, OnDblclkListIe )
  78. ON_NOTIFY( NM_CLICK, IDC_LIST_IE, OnClickListIe )
  79. ON_NOTIFY( LVN_ITEMCHANGED, IDC_LIST_IE, OnItemchangedListIe )
  80. ON_WM_VSCROLL( )
  81. ON_WM_DRAWITEM( )
  82. ON_WM_MEASUREITEM( )
  83. //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP( )
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CPrMrIe message handlers
  87. BOOL CPrMrIe::OnApply( )
  88. {
  89. ULONG count = 0;
  90. int i;
  91. CComPtr<IHsmRule> pLocalRule;
  92. CString path;
  93. CString name;
  94. BOOL bInclude;
  95. BOOL bSubdirs;
  96. BOOL bUserDefined;
  97. HRESULT hr;
  98. CComPtr<IHsmRule> pRemoteRule;
  99. CComPtr<IWsbCreateLocalObject> pLocalObject;
  100. CComPtr<IHsmCriteria> pCriteria;
  101. CComPtr<IWsbCollection> pCriteriaCollection;
  102. CComPtr <IUnknown> pUnkRule;
  103. CComPtr <IWsbCollection> pRulesCollection;
  104. try {
  105. // Empty the collection of rules
  106. WsbAffirmPointer( m_pRulesIndexedCollection );
  107. WsbAffirmHr( m_pRulesIndexedCollection->QueryInterface( IID_IWsbCollection,( void ** ) &pRulesCollection ) );
  108. pRulesCollection->RemoveAllAndRelease( );
  109. //
  110. // Get a CreateLocalobject interface with which to create the
  111. // new rule( s ).
  112. //
  113. WsbAffirmPointer( m_pFsaServer );
  114. WsbAffirmHr( m_pFsaServer->QueryInterface( IID_IWsbCreateLocalObject,( void ** ) &pLocalObject ) );
  115. //
  116. // Now go through the list box and add the rules in the list box to the
  117. // collection. Must do this backwards to be considered correctly by
  118. // job mechanism
  119. //
  120. int listCount = m_listIncExc.GetItemCount( );
  121. int insertIndex = 0;
  122. for( i = listCount - 1; i >= 0; i-- ) {
  123. //
  124. // Get the pointer to the rule from the list box
  125. //
  126. pLocalRule.Release( );
  127. pLocalRule = (IHsmRule *) m_listIncExc.GetItemData( i );
  128. if( pLocalRule ) {
  129. //
  130. // Get rule data from the local object
  131. //
  132. WsbAffirmHr( GetRuleFromObject( pLocalRule, path,
  133. name, &bInclude, &bSubdirs, &bUserDefined ) );
  134. //
  135. // Create a new remote rule object in the Fsa
  136. //
  137. pRemoteRule.Release( );
  138. WsbAffirmHr( pLocalObject->CreateInstance( CLSID_CHsmRule, IID_IHsmRule,( void** ) &pRemoteRule ) );
  139. //
  140. // Set the data in the remote rule object
  141. //
  142. WsbAffirmHr( SetRuleInObject( pRemoteRule, path, name, bInclude, bSubdirs, bUserDefined ) );
  143. //
  144. // Add the rule pointer to the collection of rules
  145. //
  146. pUnkRule.Release( );
  147. WsbAffirmHr( pRemoteRule->QueryInterface( IID_IUnknown, (void **) &pUnkRule ) );
  148. WsbAffirmHr( m_pRulesIndexedCollection->AddAt( pUnkRule, insertIndex++ ) );
  149. //
  150. // Get the criteria collection pointer
  151. //
  152. pCriteriaCollection.Release( );
  153. WsbAffirmHr( pRemoteRule->Criteria( &pCriteriaCollection ) );
  154. //
  155. // Add the appropriate criterion to the rule
  156. //
  157. pCriteria.Release( );
  158. switch( bInclude ) {
  159. case TRUE:
  160. //
  161. // Include
  162. //
  163. WsbAffirmHr( pLocalObject->CreateInstance( CLSID_CHsmCritManageable, IID_IHsmCriteria,( void** ) &pCriteria ) );
  164. WsbAffirmHr( pCriteria->SetIsNegated( FALSE ) );
  165. break;
  166. case FALSE:
  167. //
  168. // Exclude
  169. //
  170. WsbAffirmHr( pLocalObject->CreateInstance( CLSID_CHsmCritAlways, IID_IHsmCriteria,( void** ) &pCriteria ) );
  171. WsbAffirmHr( pCriteria->SetIsNegated( FALSE ) );
  172. break;
  173. }
  174. WsbAffirmHr( pCriteriaCollection->Add( pCriteria ) );
  175. }
  176. }
  177. //
  178. // Tell the FSA to save itself.
  179. //
  180. WsbAffirmHr( RsServerSaveAll( m_pFsaServer ) );
  181. } WsbCatch( hr );
  182. return CSakVolPropPage::OnApply( );
  183. }
  184. HRESULT CPrMrIe::SetRuleInObject(
  185. IHsmRule *pHsmRule,
  186. CString Path,
  187. CString Name,
  188. BOOL bInclude,
  189. BOOL bSubdirs,
  190. BOOL bUserDefined )
  191. {
  192. HRESULT hr = S_OK;
  193. try {
  194. CWsbStringPtr wsbPath = Path;
  195. CWsbStringPtr wsbName = Name;
  196. WsbAffirmHr( pHsmRule->SetPath( ( OLECHAR * )wsbPath ) );
  197. WsbAffirmHr( pHsmRule->SetName( ( OLECHAR * )wsbName ) );
  198. WsbAffirmHr( pHsmRule->SetIsInclude( bInclude ) );
  199. WsbAffirmHr( pHsmRule->SetIsUsedInSubDirs( bSubdirs ) );
  200. WsbAffirmHr( pHsmRule->SetIsUserDefined( bUserDefined ) );
  201. } WsbCatch( hr );
  202. return( hr );
  203. }
  204. BOOL CPrMrIe::OnInitDialog( )
  205. {
  206. CSakVolPropPage::OnInitDialog( );
  207. AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  208. ULONG count = 0;
  209. int index = 0;
  210. HRESULT hr = S_OK;
  211. CString path;
  212. CString name;
  213. BOOL bInclude;
  214. BOOL bSubdirs;
  215. BOOL bUserDefined;
  216. RECT rect;
  217. CString *columnWidths[20];
  218. int cColumns;
  219. int columnWidth[20];
  220. CString *columnTitles[20];
  221. CSize size;
  222. try {
  223. //
  224. // Set the icons into the buttons
  225. //
  226. HRESULT hrAlternateIcon = RsIsWhiteOnBlack( );
  227. HICON downIcon, upIcon;
  228. downIcon = (HICON)LoadImage( _Module.m_hInstResource,
  229. S_OK == hrAlternateIcon ? MAKEINTRESOURCE( IDI_MOVEDOWN2 ) : MAKEINTRESOURCE( IDI_MOVEDOWN ),
  230. IMAGE_ICON, 16, 16, 0 );
  231. upIcon = (HICON)LoadImage( _Module.m_hInstResource,
  232. S_OK == hrAlternateIcon ? MAKEINTRESOURCE( IDI_MOVEUP2 ) : MAKEINTRESOURCE( IDI_MOVEUP ),
  233. IMAGE_ICON, 16, 16, 0 );
  234. m_BtnDown.SetIcon( downIcon );
  235. m_BtnUp.SetIcon( upIcon );
  236. //
  237. // Setup up rules listview
  238. //
  239. CString sTitle;
  240. m_listIncExc.GetClientRect( &rect );
  241. ULONG totalWidth = rect.right;
  242. RsGetInitialLVColumnProps(
  243. IDS_LISTVIEW_WIDTHS_IE,
  244. IDS_LISTVIEW_TITLES_IE,
  245. columnWidths,
  246. columnTitles,
  247. &cColumns
  248. );
  249. //
  250. // NOTE: We shouldn't throw any errors until the DC is released
  251. //
  252. for( int col = 0; col < cColumns; col++ ) {
  253. size = m_listIncExc.GetStringWidth( *columnWidths[col] );
  254. columnWidth[col] = size.cx + 12;
  255. m_listIncExc.InsertColumn( col, *columnTitles[col], LVCFMT_LEFT, columnWidth[col] );
  256. // Free the CStrings
  257. delete columnTitles[col];
  258. delete columnWidths[col];
  259. }
  260. m_listIncExc.Initialize( cColumns, IE_COLUMN_PATH );
  261. // Set the Path column to fit
  262. int leftOver = totalWidth - columnWidth[IE_COLUMN_ACTION] -
  263. columnWidth[IE_COLUMN_FILE_TYPE] - columnWidth[IE_COLUMN_ATTRS];
  264. m_listIncExc.SetColumnWidth( IE_COLUMN_PATH, leftOver );
  265. // Note: this page is only implemented for single select
  266. WsbAffirm( ( m_pParent->IsMultiSelect( ) != S_OK ), E_FAIL );
  267. // Get the FsaServer interface - Apply will need it
  268. WsbAffirmHr( m_pParent->GetFsaServer( &m_pFsaServer ) );
  269. // Get the resource pointer from the sheet object
  270. WsbAffirmHr( m_pVolParent->GetFsaResource( &m_pFsaResource ) );
  271. // Get the rules collection from the resource
  272. CComPtr <IWsbCollection> pRulesCollection;
  273. WsbAffirmHr( m_pFsaResource->GetDefaultRules( &pRulesCollection ) );
  274. WsbAffirmHr( pRulesCollection->QueryInterface( IID_IWsbIndexedCollection, (void **) &m_pRulesIndexedCollection ) );
  275. CString resourceName;
  276. WsbAffirmHr( RsGetVolumeDisplayName( m_pFsaResource, resourceName ) );
  277. m_pResourceName = resourceName;
  278. // Itterate through the indexed collection
  279. WsbAffirmHr( m_pRulesIndexedCollection->GetEntries( &count ) );
  280. CComPtr <IHsmRule> pLocalRule;
  281. CComPtr <IHsmRule> pHsmRule;
  282. // Put the rules in the collection in reverse order
  283. for( INT i =( int ) count - 1; i >= 0; i-- ) {
  284. pHsmRule.Release( );
  285. pLocalRule.Release( );
  286. WsbAffirmHr( m_pRulesIndexedCollection->At( i, IID_IHsmRule,( void** )&pHsmRule ) );
  287. //
  288. // Create a local rule object and copy the remote object to it
  289. //
  290. WsbAffirmHr( pLocalRule.CoCreateInstance( CLSID_CHsmRule ) );
  291. WsbAffirmHr( GetRuleFromObject( pHsmRule, path, name, &bInclude, &bSubdirs, &bUserDefined ) );
  292. WsbAffirmHr( SetRuleInObject( pLocalRule, path, name, bInclude, bSubdirs, bUserDefined ) );
  293. //
  294. // Insert rule in list box
  295. //
  296. index = m_listIncExc.InsertItem( count - 1 - i, TEXT( "" ) );
  297. //
  298. // Set the item data to the local object
  299. //
  300. m_listIncExc.SetItemData( index, (UINT_PTR) pLocalRule.Detach( ) );
  301. //
  302. // Show the rule in the list box
  303. //
  304. WsbAffirmHr( DisplayUserRuleText( &m_listIncExc, index ) );
  305. } // for
  306. SortList( );
  307. } WsbCatch( hr );
  308. SetBtnState( );
  309. return TRUE; // return TRUE unless you set the focus to a control
  310. // EXCEPTION: OCX Property Pages should return FALSE
  311. }
  312. //HRESULT CPrMrIe::CreateImageList( )
  313. //{
  314. // HICON hIcon;
  315. // int nImage;
  316. // HRESULT hr;
  317. //
  318. // AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  319. //
  320. // try {
  321. //
  322. // CWinApp* pApp = AfxGetApp( );
  323. //
  324. // WsbAffirm( m_ImageList.Create( ::GetSystemMetrics( SM_CXSMICON ),
  325. // ::GetSystemMetrics( SM_CYSMICON ),
  326. // ILC_COLOR | ILC_MASK, 2,5 ), E_FAIL );
  327. //
  328. // hIcon = pApp->LoadIcon( IDI_LOCKED );
  329. // WsbAffirm( hIcon, E_FAIL );
  330. // nImage = m_ImageList.Add( hIcon );
  331. // ::DeleteObject( hIcon );
  332. //
  333. // hIcon = pApp->LoadIcon( IDI_UNLOCKED );
  334. // WsbAffirm( hIcon, E_FAIL );
  335. // nImage = m_ImageList.Add( hIcon );
  336. // ::DeleteObject( hIcon );
  337. //
  338. // m_listIncExc.SetImageList( &m_ImageList, LVSIL_SMALL );
  339. // } WsbCatch( hr );
  340. // return( hr );
  341. //}
  342. /////////////////////////////////////////////////////////////////////////////////////////////
  343. //
  344. // Display the rule contained in the supplied object in the supplied list at the indicated
  345. // index. The itemdata must be set to the object with correct data in it.
  346. //
  347. HRESULT CPrMrIe::DisplayUserRuleText(
  348. CListCtrl *pListControl,
  349. int index )
  350. {
  351. AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  352. HRESULT hr = S_OK;
  353. try {
  354. CString textString, tempString;
  355. CString path, name;
  356. BOOL bInclude;
  357. BOOL bSubdirs;
  358. BOOL bUserDefined;
  359. CComPtr<IHsmRule> pHsmRule;
  360. //
  361. // Get the rule from the object
  362. //
  363. pHsmRule = (IHsmRule *) m_listIncExc.GetItemData( index );
  364. WsbAssertPointer( pHsmRule );
  365. WsbAffirmHr( GetRuleFromObject( pHsmRule, path, name, &bInclude, &bSubdirs, &bUserDefined ) );
  366. //
  367. // Show the values in the list box
  368. //
  369. // ACTION
  370. textString.LoadString( bInclude ? IDS_INCLUDE : IDS_EXCLUDE );
  371. WsbAffirm( pListControl->SetItemText( index, IE_COLUMN_ACTION, textString ), E_FAIL );
  372. // FILE TYPE
  373. WsbAffirm( pListControl->SetItemText( index, IE_COLUMN_FILE_TYPE, name ), E_FAIL );
  374. // PATH
  375. WsbAffirm( pListControl->SetItemText( index, IE_COLUMN_PATH, path ), E_FAIL );
  376. // ATTRS
  377. textString.LoadString( bSubdirs ? IDS_RULE_SUBDIRS_USE : IDS_RULE_SUBDIRS_IGNORE );
  378. tempString.LoadString( bUserDefined ? IDS_RULE_TYPE_USER : IDS_RULE_TYPE_SYSTEM );
  379. textString.TrimLeft( );
  380. tempString.TrimLeft( );
  381. textString += tempString;
  382. WsbAffirm( pListControl->SetItemText( index, IE_COLUMN_ATTRS, textString ), E_FAIL );
  383. } WsbCatch( hr );
  384. return( hr );
  385. }
  386. HRESULT CPrMrIe::GetRuleFromObject(
  387. IHsmRule *pHsmRule,
  388. CString& Path,
  389. CString& Name,
  390. BOOL *bInclude,
  391. BOOL *bSubdirs,
  392. BOOL *bUserDefined )
  393. {
  394. CWsbStringPtr wsbName;
  395. CWsbStringPtr wsbPath;
  396. HRESULT hr = S_OK;
  397. try {
  398. // Get the values from the object
  399. WsbAffirmHr( pHsmRule->GetName( &wsbName, 0 ) );
  400. Name = wsbName;
  401. WsbAffirmHr ( pHsmRule->GetPath( &wsbPath, 0 ) );
  402. Path = wsbPath;
  403. *bInclude =( pHsmRule->IsInclude( ) == S_OK ) ? TRUE : FALSE;
  404. *bSubdirs =( pHsmRule->IsUsedInSubDirs( ) == S_OK ) ? TRUE : FALSE;
  405. *bUserDefined =( pHsmRule->IsUserDefined( ) == S_OK ) ? TRUE : FALSE;
  406. } WsbCatch( hr );
  407. return( hr );
  408. }
  409. void CPrMrIe::OnBtnAdd( )
  410. {
  411. LRESULT nRet;
  412. int index;
  413. BOOL fDone = FALSE;
  414. HRESULT hr;
  415. AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  416. CRule ruleDlg;
  417. ruleDlg.m_subDirs = FALSE;
  418. ruleDlg.m_includeExclude = 0; // Exclude
  419. ruleDlg.m_path = TEXT( "" );
  420. ruleDlg.m_fileSpec = TEXT( "" );
  421. ruleDlg.m_pResourceName = m_pResourceName;
  422. try {
  423. while( !fDone )
  424. {
  425. nRet = ruleDlg.DoModal( );
  426. if( nRet == IDOK ) {
  427. //
  428. // OK was pressed
  429. // Check for dupes( in entire list )
  430. //
  431. if( !IsRuleInList( ruleDlg.m_path, ruleDlg.m_fileSpec, -1 ) ) {
  432. fDone = TRUE;
  433. //
  434. // Create a new local rule object
  435. //
  436. CComPtr <IHsmRule> pLocalRule;
  437. WsbAffirmHr( pLocalRule.CoCreateInstance( CLSID_CHsmRule ) );
  438. //
  439. // Set the data in the local object
  440. //
  441. WsbAffirmHr( SetRuleInObject( pLocalRule, ruleDlg.m_path, ruleDlg.m_fileSpec,
  442. ruleDlg.m_includeExclude, ruleDlg.m_subDirs, TRUE ) );
  443. //
  444. // Insert the rule and put the pointer in the list.
  445. // We will sort the list later
  446. //
  447. index = m_listIncExc.InsertItem( 0, TEXT( "" ) );
  448. //
  449. // Set the item data to the local object
  450. //
  451. m_listIncExc.SetItemData( index, (UINT_PTR) pLocalRule.Detach( ) );
  452. //
  453. // Show the rule in the list box
  454. //
  455. WsbAffirmHr( DisplayUserRuleText( &m_listIncExc, index ) );
  456. //
  457. // Sort the list
  458. //
  459. SortList( );
  460. SetSelectedItem( (ULONG_PTR)(void *) pLocalRule );
  461. SetModified( );
  462. } else {
  463. //
  464. // Rule is a duplicate
  465. //
  466. CString sText;
  467. AfxFormatString2( sText, IDS_ERR_RULE_DUPLICATE, ruleDlg.m_path, ruleDlg.m_fileSpec );
  468. AfxMessageBox( sText, RS_MB_ERROR );
  469. }
  470. } else {
  471. //
  472. // Cancel was pressed
  473. //
  474. fDone = TRUE;
  475. }
  476. } // while
  477. } WsbCatch( hr )
  478. SetBtnState( );
  479. }
  480. // Select the item corresponding to the supplied item data
  481. void CPrMrIe::SetSelectedItem( ULONG_PTR itemData )
  482. {
  483. int listCount = m_listIncExc.GetItemCount( );
  484. for( int i = 0; i < listCount; i++ ) {
  485. // Get the pointer to the rule from the list box
  486. if( itemData ==( m_listIncExc.GetItemData( i ) ) ) {
  487. // Mark the item as selected
  488. m_listIncExc.SetItemState( i, LVIS_SELECTED, LVIS_SELECTED );
  489. m_listIncExc.EnsureVisible( i, FALSE );
  490. break;
  491. }
  492. }
  493. }
  494. void CPrMrIe::OnBtnDown( )
  495. {
  496. MoveSelectedListItem( &m_listIncExc, + 1 );
  497. SetBtnState( );
  498. }
  499. void CPrMrIe::SortList( )
  500. {
  501. m_listIncExc.SortItems( CompareFunc, NULL );
  502. }
  503. int CALLBACK CompareFunc( LPARAM lParam1, LPARAM lParam2,
  504. LPARAM /*lParamSort*/ )
  505. {
  506. CComPtr<IHsmRule> pHsmRule;
  507. CWsbStringPtr wsbPathA;
  508. CWsbStringPtr wsbPathB;
  509. // Get data for RuleA
  510. pHsmRule = (IHsmRule *) lParam1;
  511. WsbAffirmHr( pHsmRule->GetPath( &wsbPathA, 0 ) );
  512. CString pathA = wsbPathA;
  513. // Get data for RuleB
  514. pHsmRule = (IHsmRule *) lParam2;
  515. WsbAffirmHr ( pHsmRule->GetPath( &wsbPathB, 0 ) );
  516. CString pathB = wsbPathB;
  517. // Upper case the paths
  518. pathA.MakeUpper( );
  519. pathB.MakeUpper( );
  520. // Compare the two paths
  521. int rVal = PathCollate( pathA, pathB );
  522. // int rVal = pathA.Collate( pathB );
  523. // int rVal = pathB.Compare( pathA );
  524. return rVal;
  525. }
  526. int PathCollate( CString PathA, CString PathB )
  527. {
  528. CString charA;
  529. CString charB;
  530. int compareLen;
  531. int rVal;
  532. int lenA = PathA.GetLength( );
  533. int lenB = PathB.GetLength( );
  534. compareLen = min( lenA, lenB );
  535. for( int i = 0; i < compareLen; i++ )
  536. {
  537. charA = PathA.GetAt( i );
  538. charB = PathB.GetAt( i );
  539. // If either is a \, we bypass Collate
  540. if( ( charA == L"\\" ) &( charB != L"\\" ) ) {
  541. // A is \ and B is not - A is less than B
  542. return -1;
  543. }
  544. if( ( charA != L"\\" ) &( charB == L"\\" ) ) {
  545. // A is not \ and B is - B is less than A
  546. return 1;
  547. }
  548. // NOTE: If both are \, the CString Collate result is correct
  549. rVal = charA.Collate( charB );
  550. if( rVal != 0 ) {
  551. return rVal;
  552. }
  553. }
  554. // If we get here, the strings are equal as far as the shorter string.
  555. rVal = ( lenA < lenB ) ? -1 : ( lenB < lenA ) ? 1 : 0;
  556. return rVal;
  557. }
  558. void CPrMrIe::MoveSelectedListItem( CListCtrl *pList, int moveAmount )
  559. {
  560. int curIndex = -1;
  561. int itemCount = 0;
  562. CString pathA;
  563. CString pathB;
  564. CString path;
  565. CString name;
  566. BOOL bInclude;
  567. BOOL bSubdirs;
  568. BOOL bUserDefined;
  569. CComPtr<IHsmRule> pLocalRule;
  570. // Get the current index
  571. curIndex = pList->GetNextItem( -1, LVNI_SELECTED );
  572. // Is an item selected?
  573. if( curIndex != -1 ) {
  574. // Is this a user-defined rule?
  575. pLocalRule =( IHsmRule * ) m_listIncExc.GetItemData( curIndex );
  576. GetRuleFromObject( pLocalRule, path, name, &bInclude, &bSubdirs, &bUserDefined );
  577. if( bUserDefined )
  578. {
  579. // Get the item count
  580. itemCount = pList->GetItemCount( );
  581. // Is there more than one item in the list?
  582. if( itemCount > 1 )
  583. {
  584. // Make sure where we're moving to is in range
  585. if( ( ( curIndex + moveAmount ) < itemCount ) &&
  586. ( ( curIndex + moveAmount ) >= 0 ) ) {
  587. // Does the rule we're moving to have the same path?
  588. pathA = pList->GetItemText( curIndex, IE_COLUMN_PATH );
  589. pathB = pList->GetItemText( curIndex + moveAmount, IE_COLUMN_PATH );
  590. if( pathA.CompareNoCase( pathB ) == 0 ) {
  591. // Swap the lines
  592. SwapLines( pList, curIndex, curIndex + moveAmount );
  593. // Select the orignal line in it's new position and make
  594. // sure it's shown.
  595. pList->SetItemState( curIndex + moveAmount, LVIS_SELECTED, LVIS_SELECTED );
  596. pList->EnsureVisible( curIndex + moveAmount, FALSE );
  597. SetModified( );
  598. }
  599. else {
  600. MessageBeep( MB_OK );
  601. }
  602. } else {
  603. MessageBeep( MB_OK );
  604. }
  605. }
  606. else {
  607. MessageBeep( MB_OK );
  608. }
  609. } else {
  610. MessageBeep( MB_OK );
  611. }
  612. } else {
  613. MessageBeep( MB_OK );
  614. }
  615. }
  616. void CPrMrIe::SwapLines( CListCtrl *pListControl, int indexA, int indexB )
  617. {
  618. CComPtr<IHsmRule> pHsmRuleA;
  619. CComPtr<IHsmRule> pHsmRuleB;
  620. //-------------------- Get data from list ----------------------------------
  621. // LIST A
  622. // Get the item data
  623. pHsmRuleA = (IHsmRule *) pListControl->GetItemData( indexA );
  624. // LIST B
  625. // Get the item data
  626. pHsmRuleB = (IHsmRule *) pListControl->GetItemData( indexB );
  627. //--------------------- Show data in list ------------------------------------
  628. // Set the item data
  629. m_listIncExc.SetItemData( indexA,( DWORD_PTR )( void * ) pHsmRuleB );
  630. // Show the rule
  631. DisplayUserRuleText( pListControl, indexA );
  632. // Set the item data
  633. m_listIncExc.SetItemData( indexB,( DWORD_PTR )( void * ) pHsmRuleA );
  634. // Show the rule
  635. DisplayUserRuleText( pListControl, indexB );
  636. }
  637. void CPrMrIe::SetBtnState( )
  638. {
  639. CString path;
  640. CString name;
  641. CWsbStringPtr pathAbove;
  642. CWsbStringPtr pathBelow;
  643. BOOL bInclude;
  644. BOOL bSubdirs;
  645. BOOL bUserDefined;
  646. int curIndex;
  647. CComPtr<IHsmRule> pLocalRule;
  648. CComPtr<IHsmRule> pLocalRuleAbove;
  649. CComPtr<IHsmRule> pLocalRuleBelow;
  650. curIndex = m_listIncExc.GetNextItem( -1, LVNI_SELECTED );
  651. if( curIndex != -1 ) {
  652. // An item is selected. Is it User-Defined?
  653. pLocalRule =( IHsmRule * ) m_listIncExc.GetItemData( curIndex );
  654. if( !pLocalRule ) {
  655. // Seperator
  656. m_BtnRemove.EnableWindow( FALSE );
  657. m_BtnEdit.EnableWindow( FALSE );
  658. m_BtnAdd.EnableWindow( TRUE );
  659. m_BtnUp.EnableWindow( FALSE );
  660. m_BtnDown.EnableWindow( FALSE );
  661. } else {
  662. GetRuleFromObject( pLocalRule, path, name, &bInclude, &bSubdirs, &bUserDefined );
  663. if( bUserDefined ) {
  664. // User-Defined Rule is editable
  665. m_BtnRemove.EnableWindow( TRUE );
  666. m_BtnEdit.EnableWindow( TRUE );
  667. m_BtnAdd.EnableWindow( TRUE );
  668. // Are we at the top?
  669. if( curIndex == 0 ) {
  670. m_BtnUp.EnableWindow( FALSE );
  671. } else {
  672. // Does the rule above have the same path? or is separator
  673. pLocalRuleAbove =( IHsmRule * ) m_listIncExc.GetItemData( curIndex - 1 );
  674. if( pLocalRuleAbove ) {
  675. pLocalRuleAbove->GetPath( &pathAbove, 0 );
  676. if( path.CompareNoCase( pathAbove ) == 0 ) {
  677. m_BtnUp.EnableWindow( TRUE );
  678. } else {
  679. m_BtnUp.EnableWindow( FALSE );
  680. }
  681. } else {
  682. m_BtnUp.EnableWindow( FALSE );
  683. }
  684. }
  685. // Are we at the bottom?
  686. if( curIndex ==( m_listIncExc.GetItemCount( ) - 1 ) ) {
  687. m_BtnDown.EnableWindow( FALSE );
  688. } else {
  689. // Does the rule below have the same path?
  690. pLocalRuleBelow =( IHsmRule * ) m_listIncExc.GetItemData( curIndex + 1 );
  691. if( pLocalRuleBelow ) {
  692. pLocalRuleBelow->GetPath( &pathBelow, 0 );
  693. if( path.CompareNoCase( pathBelow ) == 0 ) {
  694. m_BtnDown.EnableWindow( TRUE );
  695. } else {
  696. m_BtnDown.EnableWindow( FALSE );
  697. }
  698. } else {
  699. m_BtnDown.EnableWindow( FALSE );
  700. }
  701. }
  702. }
  703. else {
  704. // System rule. Cannot be moved or modified.
  705. m_BtnUp.EnableWindow( FALSE );
  706. m_BtnRemove.EnableWindow( FALSE );
  707. m_BtnEdit.EnableWindow( FALSE );
  708. m_BtnDown.EnableWindow( FALSE );
  709. m_BtnAdd.EnableWindow( TRUE );
  710. }
  711. }
  712. }
  713. else {
  714. // No items selected
  715. m_BtnUp.EnableWindow( FALSE );
  716. m_BtnRemove.EnableWindow( FALSE );
  717. m_BtnEdit.EnableWindow( FALSE );
  718. m_BtnDown.EnableWindow( FALSE );
  719. m_BtnAdd.EnableWindow( TRUE );
  720. }
  721. }
  722. void CPrMrIe::OnBtnRemove( )
  723. {
  724. int curIndex;
  725. CString path;
  726. CString name;
  727. BOOL bInclude;
  728. BOOL bSubdirs;
  729. BOOL bUserDefined;
  730. IHsmRule *pHsmRule; // OK to not use smart pointer
  731. HRESULT hr;
  732. AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  733. try {
  734. // Is there an item selected?
  735. curIndex = m_listIncExc.GetNextItem( -1, LVNI_SELECTED );
  736. if( curIndex != -1 )
  737. {
  738. // Is the rule User-Defined?
  739. pHsmRule =( IHsmRule * ) m_listIncExc.GetItemData( curIndex );
  740. GetRuleFromObject( pHsmRule, path, name, &bInclude, &bSubdirs, &bUserDefined );
  741. if( bUserDefined )
  742. {
  743. // Confirm with user
  744. CString sMessage;
  745. AfxFormatString2( sMessage, IDS_CONFIRM_DELETE_RULE, path, name );
  746. if( AfxMessageBox( sMessage, MB_ICONQUESTION | MB_DEFBUTTON2 | MB_YESNO ) == IDYES )
  747. {
  748. // Get and release the local object pointer
  749. WsbAffirmPointer( pHsmRule );
  750. pHsmRule->Release( );
  751. // Remove from the list control
  752. m_listIncExc.DeleteItem( curIndex );
  753. int setIndex;
  754. if( curIndex >= m_listIncExc.GetItemCount( ) ) {
  755. setIndex = m_listIncExc.GetItemCount( ) - 1;
  756. } else {
  757. setIndex = curIndex;
  758. }
  759. // Select the item above the removed item
  760. m_listIncExc.SetItemState( setIndex, LVIS_SELECTED, LVIS_SELECTED );
  761. m_listIncExc.EnsureVisible( setIndex, FALSE );
  762. SortList( );
  763. SetModified( );
  764. }
  765. }
  766. else {
  767. MessageBeep( MB_OK );
  768. }
  769. }
  770. else {
  771. // No item selected
  772. AfxMessageBox( IDS_ERR_NO_ITEM_SELECTED, RS_MB_ERROR );
  773. }
  774. } WsbCatch( hr );
  775. SetBtnState( );
  776. }
  777. void CPrMrIe::OnBtnUp( )
  778. {
  779. MoveSelectedListItem( &m_listIncExc, - 1 );
  780. SetBtnState( );
  781. }
  782. void CPrMrIe::OnBtnEdit( )
  783. {
  784. BOOL fDone = FALSE;
  785. LRESULT nRet;
  786. int curIndex;
  787. CString path;
  788. CString name;
  789. BOOL bInclude;
  790. BOOL bSubdirs;
  791. BOOL bUserDefined;
  792. HRESULT hr;
  793. AFX_MANAGE_STATE( AfxGetStaticModuleState( ) );
  794. try {
  795. // Get the currently selected item
  796. curIndex = m_listIncExc.GetNextItem( -1, LVNI_SELECTED );
  797. if( curIndex == -1 ) {
  798. // No item selected
  799. AfxMessageBox( IDS_ERR_NO_ITEM_SELECTED, RS_MB_ERROR );
  800. } else {
  801. // Create the rule dialog
  802. CRule ruleDlg;
  803. // Get the local object from the list itemdata
  804. CComPtr<IHsmRule> pLocalRule;
  805. pLocalRule = (IHsmRule *) m_listIncExc.GetItemData( curIndex );
  806. WsbAffirmPointer( pLocalRule );
  807. // Get the rule from the local object
  808. WsbAffirmHr( GetRuleFromObject( pLocalRule, path, name, &bInclude, &bSubdirs, &bUserDefined ) );
  809. // Is this a user-defined rule?
  810. if( bUserDefined ) {
  811. // Set the rule info in the rule dialog
  812. ruleDlg.m_subDirs = bSubdirs;
  813. ruleDlg.m_includeExclude = bInclude;
  814. ruleDlg.m_path = path;
  815. ruleDlg.m_fileSpec = name;
  816. ruleDlg.m_pResourceName = m_pResourceName;
  817. while( !fDone ) {
  818. // Show the dialog
  819. nRet = ruleDlg.DoModal( );
  820. if( nRet == IDOK ) {
  821. // OK was pressed
  822. // Check for duplicates - but don't check against the rule we edited
  823. // in case the path and fileSpec are stil the same
  824. if( !IsRuleInList( ruleDlg.m_path, ruleDlg.m_fileSpec, curIndex ) ) {
  825. fDone = TRUE;
  826. SetModified( );
  827. // Set the data in the local object
  828. WsbAffirmHr( SetRuleInObject( pLocalRule, ruleDlg.m_path, ruleDlg.m_fileSpec,
  829. ruleDlg.m_includeExclude, ruleDlg.m_subDirs, TRUE ) );
  830. // Show the edited rule in the list box
  831. WsbAffirmHr( DisplayUserRuleText( &m_listIncExc, curIndex ) );
  832. // Resort the list
  833. SortList( );
  834. SetSelectedItem( (ULONG_PTR)(IHsmRule*) pLocalRule );
  835. } else {
  836. CString sText;
  837. AfxFormatString2( sText, IDS_ERR_RULE_DUPLICATE, ruleDlg.m_path, ruleDlg.m_fileSpec );
  838. AfxMessageBox( sText, RS_MB_ERROR );
  839. }
  840. } else {
  841. fDone = TRUE;
  842. }
  843. } // while
  844. } else { // Not user defined
  845. MessageBeep( MB_OK );
  846. }
  847. }
  848. } WsbCatch( hr );
  849. SetBtnState( );
  850. }
  851. BOOL CPrMrIe::IsRuleInList( CString Path, CString Name, int ignoreIndex )
  852. {
  853. int i;
  854. int count;
  855. short result;
  856. HRESULT hr;
  857. BOOL fDuplicate = FALSE;
  858. CWsbStringPtr wsbPath;
  859. CWsbStringPtr wsbName;
  860. count = m_listIncExc.GetItemCount( );
  861. for( i = 0; i < count; i++ ) {
  862. // Make sure we're not comparing the rule to itself
  863. if( i != ignoreIndex ) {
  864. // Get the pointer to the rule from the list box
  865. CComPtr<IHsmRule> pHsmRule;
  866. pHsmRule = (IHsmRule *) m_listIncExc.GetItemData( i );
  867. if( !pHsmRule ) continue;
  868. // Convert name and path to wsb strings
  869. wsbPath = Path;
  870. wsbName = Name;
  871. hr = pHsmRule->CompareToPathAndName( wsbPath, wsbName, &result );
  872. if( result == 0 ) {
  873. // The rules are the same
  874. fDuplicate = TRUE;
  875. break;
  876. }
  877. }
  878. } // for
  879. return fDuplicate;
  880. }
  881. void CPrMrIe::OnDestroy( )
  882. {
  883. HRESULT hr;
  884. CSakVolPropPage::OnDestroy( );
  885. IHsmRule *pHsmRule; //OK not to be smart pointer
  886. ULONG lRefCount;
  887. try {
  888. // Release all local object pointers in the list box
  889. int listCount = m_listIncExc.GetItemCount( );
  890. for( int i = 0; i < listCount; i++ )
  891. {
  892. // Get the pointer to the rule from the list box
  893. pHsmRule = (IHsmRule *) m_listIncExc.GetItemData( i );
  894. if( pHsmRule ) {
  895. lRefCount = pHsmRule->Release( );
  896. }
  897. }
  898. } WsbCatch( hr );
  899. }
  900. void CPrMrIe::OnDblclkListIe( NMHDR* /*pNMHDR*/, LRESULT* pResult )
  901. {
  902. OnBtnEdit( );
  903. *pResult = 0;
  904. }
  905. void CPrMrIe::OnClickListIe( NMHDR* /*pNMHDR*/, LRESULT* pResult )
  906. {
  907. SetBtnState( );
  908. *pResult = 0;
  909. }
  910. void CPrMrIe::OnItemchangedListIe( NMHDR* pNMHDR, LRESULT* pResult )
  911. {
  912. NM_LISTVIEW* pNMListView =( NM_LISTVIEW* )pNMHDR;
  913. SetBtnState( );
  914. *pResult = 0;
  915. }
  916. void CPrMrIe::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
  917. {
  918. // TODO: Add your message handler code here and/or call default
  919. CSakVolPropPage::OnVScroll( nSBCode, nPos, pScrollBar );
  920. }
  921. void CPrMrIe::OnDrawItem( int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct )
  922. {
  923. // TODO: Add your message handler code here and/or call default
  924. CSakVolPropPage::OnDrawItem( nIDCtl, lpDrawItemStruct );
  925. }
  926. void CPrMrIe::OnMeasureItem( int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct )
  927. {
  928. CFont *pFont;
  929. LOGFONT logFont;
  930. pFont = GetFont( );
  931. pFont->GetLogFont( &logFont );
  932. LONG fontHeight = abs( logFont.lfHeight );
  933. // Ask the list how high to make each row. It needs to know the font
  934. // height at this point because it's window is not yet created.
  935. lpMeasureItemStruct->itemHeight = m_listIncExc.GetItemHeight( fontHeight );
  936. CSakVolPropPage::OnMeasureItem( nIDCtl, lpMeasureItemStruct );
  937. }