PageRenderTime 132ms CodeModel.GetById 43ms RepoModel.GetById 8ms app.codeStats 0ms

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

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