1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright 2000-2002 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright 2000-2002 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
31 using System
.Collections
;
36 /// Base class for settings that hold lists of recent files
38 public abstract class RecentFileSettings
: SettingsGroup
40 // TODO: This class does more loading and
41 // storing than it should but this is the
42 // current simplest solution to having
43 // multiple recentfiles objects around.
44 // We can fix this by using a singleton.
45 private IList fileEntries
;
47 public static readonly int MinSize
= 1;
49 public static readonly int MaxSize
= 24;
51 public static readonly int DefaultSize
= 5;
53 public RecentFileSettings( string name
) : base ( name
, UserSettings
.GetStorageImpl( name
) )
58 public RecentFileSettings( string name
, SettingsStorage storage
) : base( name
, storage
)
63 public RecentFileSettings( string name
, SettingsGroup parent
) : base( name
, parent
)
72 int size
= LoadIntSetting( "MaxFiles", DefaultSize
);
74 if ( size
< MinSize
) size
= MinSize
;
75 if ( size
> MaxSize
) size
= MaxSize
;
81 int oldSize
= MaxFiles
;
84 if ( newSize
< MinSize
) newSize
= MinSize
;
85 if ( newSize
> MaxSize
) newSize
= MaxSize
;
87 SaveIntSetting( "MaxFiles", newSize
);
88 if ( newSize
< oldSize
) SaveSettings();
92 protected void LoadFiles()
94 fileEntries
= new ArrayList();
95 for ( int index
= 1; index
<= MaxFiles
; index
++ )
97 string fileName
= LoadStringSetting( ValueName( index
) );
98 if ( fileName
!= null )
99 fileEntries
.Add( fileName
);
103 public override void Clear()
106 fileEntries
= new ArrayList();
109 public IList
GetFiles()
115 public string RecentFile
120 if( fileEntries
.Count
> 0 )
121 return (string)fileEntries
[0];
129 int index
= fileEntries
.IndexOf(value);
131 if(index
== 0) return;
135 fileEntries
.RemoveAt(index
);
138 fileEntries
.Insert( 0, value );
139 if( fileEntries
.Count
> MaxFiles
)
140 fileEntries
.RemoveAt( MaxFiles
);
146 public void Remove( string fileName
)
149 fileEntries
.Remove( fileName
);
153 private void SaveSettings()
155 while( fileEntries
.Count
> MaxFiles
)
156 fileEntries
.RemoveAt( fileEntries
.Count
- 1 );
158 for( int index
= 0; index
< MaxSize
; index
++ )
160 string valueName
= ValueName( index
+ 1 );
161 if ( index
< fileEntries
.Count
)
162 SaveSetting( valueName
, fileEntries
[index
] );
164 RemoveSetting( valueName
);
168 private string ValueName( int index
)
170 return string.Format( "File{0}", index
);