1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
21
| Author: Mike Lasky (mlasky@novell.com)
22
|***************************************************************************/
24
namespace Novell.iFolderWeb.Admin
27
using System.Collections;
30
using System.Resources;
33
using System.Web.UI.WebControls;
34
using System.Web.UI.HtmlControls;
37
/// Summary description for FileTypeFilter.
39
public class FileTypeFilter : System.Web.UI.UserControl
46
private ResourceManager rm;
50
/// File type filter control.
52
protected Label Title;
56
/// Non system file type policy controls.
58
protected DataGrid FileTypeList;
61
/// Table footer control.
63
protected ListFooter FileTypeListFooter;
69
protected Button DeleteButton;
74
protected Button AddButton;
79
protected Button AllowButton;
84
protected Button DenyButton;
88
/// Control that allows a new file type to be added.
90
protected TextBox NewFileTypeName;
94
/// Controls that selects file type entries.
96
protected CheckBox AllFilesCheckBox;
100
/// Event that notifies consumer that the filter list has changed.
102
public event EventHandler ListChanged = null;
109
/// Gets or sets the current file offset.
111
private int CurrentFileOffset
113
get { return ( int )ViewState[ "CurrentFileOffset" ]; }
114
set { ViewState[ "CurrentFileOffset" ] = value; }
118
/// Gets or sets the file type data source value.
120
private Hashtable FileTypeSource
122
get { return ViewState[ "FileTypeSource" ] as Hashtable; }
123
set { ViewState[ "FileTypeSource" ] = value; }
127
/// Returns true if any FileType source entries are checked.
129
private bool HasCheckedEntries
133
bool isChecked = false;
134
foreach( FileTypeInfo fti in FileTypeSource.Values )
148
/// Returns true if any FileType source entries are disallowed.
150
private bool HasDisallowedEntries
154
bool isDisallowed = false;
155
foreach( FileTypeInfo fti in FileTypeSource.Values )
157
if ( fti.IsChecked && !fti.IsAllowed )
169
/// Returns true if any FileType source entries are allowed.
171
private bool HasAllowedEntries
175
bool isAllowed = false;
176
foreach( FileTypeInfo fti in FileTypeSource.Values )
178
if ( fti.IsChecked && fti.IsAllowed )
190
/// Gets or sets the total number of files contained in
191
/// the exclude list.
193
private int TotalFiles
195
get { return ( int )ViewState[ "TotalFiles" ]; }
196
set { ViewState[ "TotalFiles" ] = value; }
201
#region Private Methods
204
/// Creates the file type list view for the web control.
206
private DataView CreateFileTypeListView()
208
DataTable dt = new DataTable();
211
dt.Columns.Add( new DataColumn( "VisibleField", typeof( bool ) ) );
212
dt.Columns.Add( new DataColumn( "FileRegExField", typeof( string ) ) );
213
dt.Columns.Add( new DataColumn( "AllowedField", typeof( string ) ) );
214
dt.Columns.Add( new DataColumn( "FileNameField", typeof( string ) ) );
215
dt.Columns.Add( new DataColumn( "EnabledField", typeof( bool ) ) );
217
// Fill the data table from the saved selected member list.
218
Hashtable ht = FileTypeSource;
219
FileTypeInfo[] ftInfoList = new FileTypeInfo[ ht.Count ];
220
TotalFiles = ht.Count;
222
// Copy the Values to the array so that they can be sorted.
223
ht.Values.CopyTo( ftInfoList, 0 );
224
Array.Sort( ftInfoList );
226
for ( int i = 0; i < ftInfoList.Length; ++i )
228
// Don't add until at the right display offset.
229
if ( i >= CurrentFileOffset )
231
// Don't add more than one page worth of data.
232
if ( i < ( CurrentFileOffset + FileTypeList.PageSize ) )
236
dr[ 1 ] = ftInfoList[ i ].RegExFileName;
237
dr[ 2 ] = GetString( ftInfoList[ i ].IsAllowed ? "ALLOW" : "DENY" );
238
dr[ 3 ] = ftInfoList[ i ].FriendlyFileName;
239
dr[ 4 ] = ftInfoList[ i ].IsEnabled;
250
// If the page size is not full, finish it with empty entries.
251
for ( int i = dt.Rows.Count; i < FileTypeList.PageSize; ++i )
255
dr[ 1 ] = String.Empty;
256
dr[ 2 ] = String.Empty;
257
dr[ 3 ] = String.Empty;
263
// Build the data view from the table.
264
return new DataView( dt );
268
/// Creates a stateful list of file type filters.
270
/// <param name="policy">User policy object</param>
271
/// <returns>A hashtable containing the file type filters.</returns>
272
private Hashtable CreateFileTypeSource( UserPolicy policy )
274
// Keep the state in a hashtable.
275
Hashtable ht = new Hashtable();
276
foreach( string s in policy.FileTypesExcludesEffective )
278
ht[ s ] = new FileTypeInfo(
280
Utils.ConvertFromRegEx( s ),
281
IsAllowed( policy.FileTypesIncludes, s ),
284
foreach( string s in policy.FileTypesIncludes )
286
ht[ s ] = new FileTypeInfo(
288
Utils.ConvertFromRegEx( s ),
297
/// Creates a stateful list of file type filters.
299
/// <param name="policy">iFolder policy object</param>
300
/// <returns>A hashtable containing the file type filters.</returns>
301
private Hashtable CreateFileTypeSource( iFolderPolicy policy )
303
// Keep the state in a hashtable.
304
Hashtable ht = new Hashtable();
305
foreach( string s in policy.FileTypesExcludesEffective )
307
ht[ s ] = new FileTypeInfo(
309
Utils.ConvertFromRegEx( s ),
310
IsAllowed( policy.FileTypesIncludesEffective, s ),
314
foreach( string s in policy.FileTypesExcludes )
316
ht[ s ] = new FileTypeInfo(
318
Utils.ConvertFromRegEx( s ),
327
/// Creates a stateful list of file type filters.
329
/// <param name="policy">System policy object</param>
330
/// <returns>A hashtable containing the file type filters.</returns>
331
private Hashtable CreateFileTypeSource( SystemPolicy policy )
333
// Keep the state in a hashtable.
334
Hashtable ht = new Hashtable();
335
foreach( string s in policy.FileTypesExcludes )
337
ht[ s ] = new FileTypeInfo( s, Utils.ConvertFromRegEx( s ), false, true );
344
/// Checks if the specified file name is already in the list.
346
/// <param name="fileName"></param>
347
/// <returns>True if the specified file name is already in the list.</returns>
348
private bool FileTypeExists( string fileName )
352
Hashtable ht = FileTypeSource;
353
foreach( FileTypeInfo fti in ht.Values )
355
if ( String.Compare( fti.FriendlyFileName, fileName, true ) == 0 )
366
/// Gets whether the specified file type is allowed.
368
/// <param name="effectivePolicy">Effective user policy</param>
369
/// <param name="fileType">Name of file type.</param>
370
/// <returns></returns>
371
private bool IsAllowed( string[] effectivePolicy, string fileType )
373
return ( Array.IndexOf( effectivePolicy, fileType ) != -1 ) ? true : false;
377
/// Event handler for when this page is loaded.
379
/// <param name="sender"></param>
380
/// <param name="e"></param>
381
private void Page_Load(object sender, System.EventArgs e)
384
rm = Application[ "RM" ] as ResourceManager;
388
// Initialize the localized fields.
389
DeleteButton.Text = GetString( "DELETE" );
390
AddButton.Text = GetString( "ADD" );
391
DenyButton.Text = GetString( "DENY" );
392
AllowButton.Text = GetString( "ALLOW" );
394
Title.Text = GetString( "EXCLUDEDFILES" );
396
// Initialize the state variables.
397
CurrentFileOffset = 0;
400
// Set the javascript function that will handle key presses.
401
NewFileTypeName.Attributes[ "OnKeyPress" ] = "return SubmitKeyDown(event, '" + AddButton.ClientID + "');";
406
/// Sets the page button state of the file type list.
408
private void SetPageButtonState()
410
FileTypeListFooter.SetPageButtonState(
414
GetString( "FILES" ),
415
GetString( "FILE" ) );
419
/// Displays an error message on the parent page.
421
/// <param name="errMsg"></param>
422
private void ShowError( string errMsg )
424
TopNavigation nav = Page.FindControl( "TopNav" ) as TopNavigation;
427
nav.ShowError( errMsg );
433
#region Protected Methods
436
/// Get a Localized String
438
/// <param name="key">Key to the localized string.</param>
439
/// <returns>Localized string.</returns>
440
protected string GetString( string key )
442
return rm.GetString( key );
446
/// Returns whether the entry has been checked.
448
/// <param name="entry"></param>
449
/// <returns></returns>
450
protected bool IsEntryChecked( Object entry )
452
FileTypeInfo fti = FileTypeSource[ entry ] as FileTypeInfo;
453
return ( fti != null ) ? fti.IsChecked : false;
457
/// Event handler that gets called when the all files checkbox is checked.
459
/// <param name="sender"></param>
460
/// <param name="e"></param>
461
protected void OnAllFilesChecked( Object sender, EventArgs e )
463
CheckBox allCheckBox = sender as CheckBox;
465
foreach( DataGridItem item in FileTypeList.Items )
467
string fileName = item.Cells[ 0 ].Text;
468
if ( fileName != " " )
470
FileTypeInfo fti = FileTypeSource[ fileName ] as FileTypeInfo;
473
CheckBox checkBox = item.Cells[ 1 ].FindControl( "FileTypeCheckBox" ) as CheckBox;
474
if ( ( checkBox != null ) && checkBox.Enabled )
476
fti.IsChecked = checkBox.Checked = allCheckBox.Checked;
482
// See if there are any checked members.
483
bool hasEntries = allCheckBox.Checked ? true : HasCheckedEntries;
484
if ( DeleteButton.Visible )
486
DeleteButton.Enabled = hasEntries;
490
AllowButton.Enabled = HasDisallowedEntries && hasEntries;
491
DenyButton.Enabled = HasAllowedEntries && hasEntries;
496
/// Event handler that gets called when the delete button is clicked.
498
/// <param name="sender"></param>
499
/// <param name="e"></param>
500
protected void OnDeleteFileType( Object sender, EventArgs e )
502
// Get all of the values from the hashtable.
503
Hashtable ht = FileTypeSource;
504
FileTypeInfo[] ftInfoList = new FileTypeInfo[ ht.Count ];
505
ht.Values.CopyTo( ftInfoList, 0 );
507
foreach( FileTypeInfo fti in ftInfoList )
511
ht.Remove( fti.RegExFileName );
515
// Reset the all files check box.
516
AllFilesCheckBox.Checked = false;
517
DeleteButton.Enabled = false;
519
// If there are no entries in the current view, set the current page back one page.
520
if ( CurrentFileOffset >= ht.Count )
522
CurrentFileOffset -= FileTypeList.PageSize;
523
if ( CurrentFileOffset < 0 )
525
CurrentFileOffset = 0;
529
// Refresh the policy view.
530
FileTypeList.DataSource = CreateFileTypeListView();
531
FileTypeList.DataBind();
532
SetPageButtonState();
534
// Indicate an event that the list has changed.
535
if ( ListChanged != null )
537
ListChanged( this, e );
542
/// Event handler that gets called when the allow file type button is clicked.
544
/// <param name="sender"></param>
545
/// <param name="e"></param>
546
protected void OnAllowFileType( Object sender, EventArgs e )
548
// Get all of the values from the hashtable.
549
foreach( FileTypeInfo fti in FileTypeSource.Values )
553
fti.IsAllowed = true;
554
fti.IsChecked = false;
558
// Reset the all files check box.
559
AllFilesCheckBox.Checked = false;
560
AllowButton.Enabled = false;
562
// Refresh the policy view.
563
FileTypeList.DataSource = CreateFileTypeListView();
564
FileTypeList.DataBind();
566
// Indicate an event that the list has changed.
567
if ( ListChanged != null )
569
ListChanged( this, e );
574
/// Event handler that gets called when the deny file type button is clicked.
576
/// <param name="sender"></param>
577
/// <param name="e"></param>
578
protected void OnDenyFileType( Object sender, EventArgs e )
580
// Get all of the values from the hashtable.
581
foreach( FileTypeInfo fti in FileTypeSource.Values )
585
fti.IsAllowed = fti.IsChecked = false;
589
// Reset the all files check box.
590
AllFilesCheckBox.Checked = false;
591
DenyButton.Enabled = false;
593
// Refresh the policy view.
594
FileTypeList.DataSource = CreateFileTypeListView();
595
FileTypeList.DataBind();
597
// Indicate an event that the list has changed.
598
if ( ListChanged != null )
600
ListChanged( this, e );
605
/// Event handler that gets called when the add button is clicked.
607
/// <param name="sender"></param>
608
/// <param name="e"></param>
609
protected void OnFileTypeAddClick( Object sender, EventArgs e )
611
string fileName = NewFileTypeName.Text;
612
if ( ( fileName != null ) && ( fileName != String.Empty ) )
614
// Make sure that this entry is not already in the list.
615
if ( !FileTypeExists( fileName ) )
617
Hashtable ht = FileTypeSource;
618
ht[ fileName ] = new FileTypeInfo( Utils.ConvertToRegEx( fileName ), fileName, false, true );
620
// A new file was added to the list. Update the page buttons.
623
// Clear out the old entry.
624
NewFileTypeName.Text = String.Empty;
626
// Indicate an event that the list has changed.
627
if ( ListChanged != null )
629
ListChanged( this, e );
632
// Refresh the policy view.
633
FileTypeList.DataSource = CreateFileTypeListView();
634
FileTypeList.DataBind();
635
SetPageButtonState();
639
ShowError( GetString( "FILETYPEALREADYEXISTS" ) );
644
AddButton.Enabled = false;
649
/// Event handler that gets called when the file type checkbox is changed.
651
/// <param name="sender"></param>
652
/// <param name="e"></param>
653
protected void OnFileTypeCheckChanged( Object sender, EventArgs e )
655
CheckBox checkBox = sender as CheckBox;
656
DataGridItem item = checkBox.Parent.Parent as DataGridItem;
657
string fileName = item.Cells[ 0 ].Text;
658
if ( fileName != " " )
660
FileTypeInfo fti = FileTypeSource[ fileName ] as FileTypeInfo;
663
fti.IsChecked = checkBox.Checked;
665
bool hasEntries = checkBox.Checked ? true : HasCheckedEntries;
666
if ( DeleteButton.Visible )
668
DeleteButton.Enabled = hasEntries;
672
AllowButton.Enabled = HasDisallowedEntries && hasEntries;
673
DenyButton.Enabled = HasAllowedEntries && hasEntries;
680
/// Event handler for the PageFirstButton.
682
/// <param name="source"></param>
683
/// <param name="e"></param>
684
protected void PageFirstButton_Click( object source, ImageClickEventArgs e )
686
// Set to get the first files.
687
CurrentFileOffset = 0;
689
// Rebind the data source with the new data.
690
FileTypeList.DataSource = CreateFileTypeListView();
691
FileTypeList.DataBind();
693
// Set the button state.
694
SetPageButtonState();
696
// Reset the all files checkbox.
697
AllFilesCheckBox.Checked = false;
701
/// Event that first when the PageNextButton is clicked.
703
/// <param name="source"></param>
704
/// <param name="e"></param>
705
protected void PageNextButton_Click( object source, ImageClickEventArgs e)
707
CurrentFileOffset += FileTypeList.PageSize;
709
// Rebind the data source with the new data.
710
FileTypeList.DataSource = CreateFileTypeListView();
711
FileTypeList.DataBind();
713
// Set the button state.
714
SetPageButtonState();
716
// Reset the all files checkbox.
717
AllFilesCheckBox.Checked = false;
721
/// Event that first when the PageLastButton is clicked.
723
/// <param name="source"></param>
724
/// <param name="e"></param>
725
protected void PageLastButton_Click( object source, ImageClickEventArgs e)
727
CurrentFileOffset = ( ( TotalFiles - 1 ) / FileTypeList.PageSize ) * FileTypeList.PageSize;
729
// Rebind the data source with the new data.
730
FileTypeList.DataSource = CreateFileTypeListView();
731
FileTypeList.DataBind();
733
// Set the button state.
734
SetPageButtonState();
736
// Reset the all files checkbox.
737
AllFilesCheckBox.Checked = false;
741
/// Event that first when the PagePreviousButton is clicked.
743
/// <param name="source"></param>
744
/// <param name="e"></param>
745
protected void PagePreviousButton_Click( object source, ImageClickEventArgs e)
747
CurrentFileOffset -= FileTypeList.PageSize;
748
if ( CurrentFileOffset < 0 )
750
CurrentFileOffset = 0;
753
// Rebind the data source with the new data.
754
FileTypeList.DataSource = CreateFileTypeListView();
755
FileTypeList.DataBind();
757
// Set the button state.
758
SetPageButtonState();
760
// Reset the all files checkbox.
761
AllFilesCheckBox.Checked = false;
766
#region Public Methods
769
/// Gets the file type policy for the current user.
771
/// <param name="policy">User policy.</param>
772
public void GetFileTypePolicy( UserPolicy policy )
774
// Show the proper control buttons.
775
AllowButton.Visible = DenyButton.Visible = true;
777
NewFileTypeName.Visible = AddButton.Visible = true;
778
// Create a list from the file type policy.
779
FileTypeSource = CreateFileTypeSource( policy );
781
// Build the data view from the table.
782
FileTypeList.DataSource = CreateFileTypeListView();
783
FileTypeList.DataBind();
784
SetPageButtonState();
788
/// Gets the file type policy for the current ifolder.
790
/// <param name="policy">iFolder policy.</param>
791
public void GetFileTypePolicy( iFolderPolicy policy )
793
// Enable the add/delete controls.
794
NewFileTypeName.Visible = AddButton.Visible = DeleteButton.Visible = true;
796
// Create a list from the file type policy.
797
FileTypeSource = CreateFileTypeSource( policy );
799
// Build the data view from the table.
800
FileTypeList.DataSource = CreateFileTypeListView();
801
FileTypeList.DataBind();
802
SetPageButtonState();
806
/// Gets the file type policy for the system.
808
/// <param name="policy">System policy.</param>
809
public void GetFileTypePolicy( SystemPolicy policy )
811
// Enable the add/delete controls.
812
NewFileTypeName.Visible = AddButton.Visible = DeleteButton.Visible = true;
814
// Create a list from the file type policy.
815
FileTypeSource = CreateFileTypeSource( policy );
817
// Build the data view from the table.
818
FileTypeList.DataSource = CreateFileTypeListView();
819
FileTypeList.DataBind();
820
SetPageButtonState();
824
/// Sets the file type policy for this user.
826
/// <param name="policy">User policy that the new file type filter will be set.</param>
827
public void SetFileTypePolicy( UserPolicy policy )
829
// Build a list of checked file types.
830
ArrayList filterListAllow = new ArrayList();
831
ArrayList filterListDeny = new ArrayList();
832
foreach( FileTypeInfo fti in FileTypeSource.Values )
836
filterListAllow.Add( fti.RegExFileName );
840
filterListDeny.Add( fti.RegExFileName );
844
// Set the user current user policy.
845
policy.FileTypesIncludes = filterListAllow.ToArray( typeof( string ) ) as string[];
846
policy.FileTypesExcludes = filterListDeny.ToArray( typeof( string ) ) as string[];
850
/// Sets the file type policy for this ifolder.
852
/// <param name="policy">iFolder policy that the new file type filter will be set.</param>
853
public void SetFileTypePolicy( iFolderPolicy policy )
855
// Build a list of checked file types.
856
ArrayList filterList = new ArrayList();
857
foreach( FileTypeInfo fti in FileTypeSource.Values )
859
if ( fti.IsEnabled && !fti.IsAllowed )
861
filterList.Add( fti.RegExFileName );
865
// Set the current ifolder policy.
866
policy.FileTypesExcludes = filterList.ToArray( typeof( string ) ) as string[];
870
/// Sets the file type policy for the system.
872
/// <param name="policy">System policy that the new file type filter will be set.</param>
873
public void SetFileTypePolicy( SystemPolicy policy )
875
// Build a list of checked file types.
876
ArrayList filterList = new ArrayList();
877
foreach( FileTypeInfo fti in FileTypeSource.Values )
879
filterList.Add( fti.RegExFileName );
882
// Set the user current system policy.
883
policy.FileTypesExcludes = filterList.ToArray( typeof( string ) ) as string[];
888
#region Web Form Designer generated code
893
/// <param name="e"></param>
894
override protected void OnInit(EventArgs e)
897
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
899
InitializeComponent();
904
/// Required method for Designer support - do not modify
905
/// the contents of this method with the code editor.
907
private void InitializeComponent()
909
FileTypeListFooter.PageFirstClick += new ImageClickEventHandler( PageFirstButton_Click );
910
FileTypeListFooter.PagePreviousClick += new ImageClickEventHandler( PagePreviousButton_Click );
911
FileTypeListFooter.PageNextClick += new ImageClickEventHandler( PageNextButton_Click );
912
FileTypeListFooter.PageLastClick += new ImageClickEventHandler( PageLastButton_Click );
914
this.Load += new System.EventHandler(this.Page_Load);
918
#region FileTypeInfo Class
921
/// Class used to hold File Type filter information.
924
private class FileTypeInfo : IComparable
926
#region Class Members
929
/// The regular expression version of the file name.
931
public string RegExFileName;
934
/// The friendly version of the file name.
936
public string FriendlyFileName;
939
/// If the file name is enabled as a filter.
941
public bool IsAllowed;
944
/// True if entry is a system level policy that can be
945
/// enabled/disabled or deleted.
947
public bool IsEnabled;
950
/// True if entry has been checked in the list.
952
public bool IsChecked = false;
961
/// <param name="regExFileName"></param>
962
/// <param name="friendlyFileName"></param>
963
/// <param name="allowed"></param>
964
/// <param name="enabled"></param>
966
string regExFileName,
967
string friendlyFileName,
971
RegExFileName = regExFileName;
972
FriendlyFileName = friendlyFileName;
979
#region IComparable Members
982
/// Compares the current instance with another object of the same type.
984
/// <param name="obj"></param>
985
/// <returns></returns>
986
public int CompareTo( object obj )
988
return String.Compare( FriendlyFileName, ( obj as FileTypeInfo ).FriendlyFileName, false );