1 // ****************************************************************
2 // Copyright 2007, Charlie Poole
3 // This is free software licensed under the NUnit license. You may
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
5 // ****************************************************************
12 /// Summary description for RecentFilesService.
14 public class RecentFilesService
: RecentFiles
, NUnit
.Core
.IService
16 private RecentFilesCollection fileEntries
= new RecentFilesCollection();
18 private ISettings settings
;
20 public static readonly int MinSize
= 1;
22 public static readonly int MaxSize
= 24;
24 public static readonly int DefaultSize
= 5;
27 public RecentFilesService()
28 : this( Services
.UserSettings
) { }
30 public RecentFilesService( ISettings settings
)
32 this.settings
= settings
;
39 get { return fileEntries.Count; }
46 int size
= settings
.GetSetting( "RecentProjects.MaxFiles", DefaultSize
);
48 if ( size
< MinSize
) size
= MinSize
;
49 if ( size
> MaxSize
) size
= MaxSize
;
55 int oldSize
= MaxFiles
;
58 if ( newSize
< MinSize
) newSize
= MinSize
;
59 if ( newSize
> MaxSize
) newSize
= MaxSize
;
61 settings
.SaveSetting( "RecentProjects.MaxFiles", newSize
);
62 if ( newSize
< oldSize
) SaveEntriesToSettings( this. settings
);
67 #region Public Methods
68 public RecentFilesCollection Entries
76 public void Remove( string fileName
)
78 fileEntries
.Remove( fileName
);
81 public void SetMostRecent( string fileName
)
83 SetMostRecent( new RecentFileEntry( fileName
) );
86 public void SetMostRecent( RecentFileEntry entry
)
88 int index
= fileEntries
.IndexOf(entry
.Path
);
91 fileEntries
.RemoveAt(index
);
93 fileEntries
.Insert( 0, entry
);
94 if( fileEntries
.Count
> MaxFiles
)
95 fileEntries
.RemoveAt( MaxFiles
);
99 #region Helper Methods for saving and restoring the settings
100 private void LoadEntriesFromSettings( ISettings settings
)
104 string prefix
= Environment
.Version
.Major
>= 2
105 ? "RecentProjects.V2"
106 : "RecentProjects.V1";
108 for ( int index
= 1; index
<= MaxFiles
; index
++ )
110 string fileSpec
= settings
.GetSetting( GetRecentFileKey( prefix
, index
) ) as string;
111 if ( fileSpec
!= null ) fileEntries
.Add( new RecentFileEntry( fileSpec
) );
114 // Try legacy entries if nothing was found
115 if ( fileEntries
.Count
== 0 )
117 for ( int index
= 1; index
<= MaxFiles
; index
++ )
119 string fileSpec
= settings
.GetSetting( GetRecentFileKey( "RecentProjects", index
) ) as string;
120 if ( fileSpec
!= null )
122 RecentFileEntry entry
= RecentFileEntry
.Parse( fileSpec
);
123 if ( entry
.CLRVersion
.Major
<= Environment
.Version
.Major
)
124 fileEntries
.Add( entry
);
130 private void SaveEntriesToSettings( ISettings settings
)
132 string prefix
= Environment
.Version
.Major
>= 2
133 ? "RecentProjects.V2"
134 : "RecentProjects.V1";
136 while( fileEntries
.Count
> MaxFiles
)
137 fileEntries
.RemoveAt( fileEntries
.Count
- 1 );
139 for( int index
= 0; index
< MaxSize
; index
++ )
141 string keyName
= GetRecentFileKey( prefix
, index
+ 1 );
142 if ( index
< fileEntries
.Count
)
143 settings
.SaveSetting( keyName
, fileEntries
[index
].Path
);
145 settings
.RemoveSetting( keyName
);
149 private string GetRecentFileKey( string prefix
, int index
)
151 return string.Format( "{0}.File{1}", prefix
, index
);
155 #region IService Members
157 public void UnloadService()
159 SaveEntriesToSettings( this.settings
);
162 public void InitializeService()
164 LoadEntriesFromSettings( this.settings
);