PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/util/Services/RecentFilesService.cs

#
C# | 176 lines | 130 code | 35 blank | 11 comment | 22 complexity | fc027696163a8970f7f6472eecdd13dc MD5 | raw file
  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
  5// ****************************************************************
  6
  7using System;
  8
  9namespace NUnit.Util
 10{
 11	/// <summary>
 12	/// Summary description for RecentFilesService.
 13	/// </summary>
 14	public class RecentFilesService : RecentFiles, NUnit.Core.IService
 15	{
 16		private RecentFilesCollection fileEntries = new RecentFilesCollection();
 17
 18		private ISettings settings;
 19
 20		public static readonly int MinSize = 0;
 21
 22		public static readonly int MaxSize = 24;
 23
 24		public static readonly int DefaultSize = 5;
 25
 26		#region Constructor
 27		public RecentFilesService()
 28			: this( Services.UserSettings ) { }
 29
 30		public RecentFilesService( ISettings settings ) 
 31		{
 32			this.settings = settings;
 33		}
 34		#endregion
 35
 36		#region Properties
 37		public int Count
 38		{
 39			get { return fileEntries.Count; }
 40		}
 41
 42		public int MaxFiles
 43		{
 44			get 
 45			{ 
 46				int size = settings.GetSetting("Gui.RecentProjects.MaxFiles", -1 );
 47                if (size < 0)
 48                {
 49                    size = settings.GetSetting("RecentProjects.MaxFiles", DefaultSize);
 50                    if (size != DefaultSize)
 51                        settings.SaveSetting("Gui.RecentProjects.MaxFiles", size);
 52                    settings.RemoveSetting("RecentProjects.MaxFiles");
 53                }
 54				
 55				if ( size < MinSize ) size = MinSize;
 56				if ( size > MaxSize ) size = MaxSize;
 57				
 58				return size;
 59			}
 60			set 
 61			{ 
 62				int oldSize = MaxFiles;
 63				int newSize = value;
 64				
 65				if ( newSize < MinSize ) newSize = MinSize;
 66				if ( newSize > MaxSize ) newSize = MaxSize;
 67
 68				settings.SaveSetting( "Gui.RecentProjects.MaxFiles", newSize );
 69				if ( newSize < oldSize ) SaveEntriesToSettings( this. settings );
 70			}
 71		}
 72		#endregion
 73
 74		#region Public Methods
 75		public RecentFilesCollection Entries
 76		{
 77			get
 78			{
 79				return fileEntries;
 80			}
 81		}
 82		
 83		public void Remove( string fileName )
 84		{
 85			fileEntries.Remove( fileName );
 86		}
 87
 88		public void SetMostRecent( string fileName )
 89		{
 90			SetMostRecent( new RecentFileEntry( fileName ) );
 91		}
 92
 93		public void SetMostRecent( RecentFileEntry entry )
 94		{
 95			int index = fileEntries.IndexOf(entry.Path);
 96
 97			if(index != -1)
 98				fileEntries.RemoveAt(index);
 99
100			fileEntries.Insert( 0, entry );
101			if( fileEntries.Count > MaxFiles )
102				fileEntries.RemoveAt( MaxFiles );
103		}
104		#endregion
105
106		#region Helper Methods for saving and restoring the settings
107		private void LoadEntriesFromSettings( ISettings settings )
108		{
109			fileEntries.Clear();
110
111            AddEntriesForPrefix("Gui.RecentProjects");
112
113            // Try legacy entries if nothing was found
114            if (fileEntries.Count == 0)
115            {
116                AddEntriesForPrefix("RecentProjects.V2");
117                AddEntriesForPrefix("RecentProjects.V1");
118            }
119
120            // Try even older legacy format
121            if (fileEntries.Count == 0)
122                AddEntriesForPrefix("RecentProjects");
123		}
124
125        private void AddEntriesForPrefix(string prefix)
126        {
127            for (int index = 1; index < MaxFiles; index++)
128            {
129                if (fileEntries.Count >= MaxFiles) break;
130
131                string fileSpec = settings.GetSetting(GetRecentFileKey(prefix, index)) as string;
132                if (fileSpec != null) fileEntries.Add(RecentFileEntry.Parse(fileSpec));
133            }
134        }
135
136		private void SaveEntriesToSettings( ISettings settings )
137		{
138			string prefix = "Gui.RecentProjects";
139
140			while( fileEntries.Count > MaxFiles )
141				fileEntries.RemoveAt( fileEntries.Count - 1 );
142
143			for( int index = 0; index < MaxSize; index++ ) 
144			{
145				string keyName = GetRecentFileKey( prefix, index + 1 );
146				if ( index < fileEntries.Count )
147					settings.SaveSetting( keyName, fileEntries[index].Path );
148				else
149					settings.RemoveSetting( keyName );
150			}
151
152            // Remove legacy entries here
153            settings.RemoveGroup("RecentProjects");
154		}
155
156		private string GetRecentFileKey( string prefix, int index )
157		{
158			return string.Format( "{0}.File{1}", prefix, index );
159		}
160		#endregion
161
162		#region IService Members
163
164		public void UnloadService()
165		{
166			SaveEntriesToSettings( this.settings );
167		}
168
169		public void InitializeService()
170		{
171			LoadEntriesFromSettings( this.settings );
172		}
173
174		#endregion
175	}
176}