PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/util/Services/SettingsService.cs

#
C# | 150 lines | 121 code | 20 blank | 9 comment | 23 complexity | a5fe6565f753ff317a1c23f49d07ff09 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. using System.IO;
  8. using Microsoft.Win32;
  9. using NUnit.Core;
  10. namespace NUnit.Util
  11. {
  12. /// <summary>
  13. /// Summary description for UserSettingsService.
  14. /// </summary>
  15. public class SettingsService : SettingsGroup, NUnit.Core.IService
  16. {
  17. static readonly string settingsFileName = "NUnitSettings.xml";
  18. private bool writeable;
  19. public SettingsService() : this(true) { }
  20. public SettingsService(bool writeable)
  21. {
  22. this.writeable = writeable;
  23. #if NET_2_0
  24. string settingsFile = System.Configuration.ConfigurationManager.AppSettings["settingsFile"];
  25. #else
  26. string settingsFile = System.Configuration.ConfigurationSettings.AppSettings["settingsFile"];
  27. #endif
  28. if ( settingsFile == null )
  29. settingsFile = Path.Combine( NUnitConfiguration.ApplicationDirectory, settingsFileName );
  30. this.storage = new XmlSettingsStorage( settingsFile, writeable );
  31. if ( File.Exists( settingsFile ) )
  32. storage.LoadSettings();
  33. else if (writeable)
  34. ConvertLegacySettings();
  35. }
  36. #region IService Implementation
  37. public void InitializeService()
  38. {
  39. }
  40. public void UnloadService()
  41. {
  42. if ( writeable )
  43. storage.SaveSettings();
  44. this.Dispose();
  45. }
  46. #endregion
  47. #region ConvertLegacySettings
  48. void ConvertLegacySettings()
  49. {
  50. RegistryKey key = Registry.CurrentUser.OpenSubKey( NUnitRegistry.KEY );
  51. if ( key == null )
  52. key = Registry.CurrentUser.OpenSubKey( NUnitRegistry.LEGACY_KEY );
  53. if ( key != null )
  54. {
  55. using( ISettingsStorage legacyStorage = new RegistrySettingsStorage( key ) )
  56. {
  57. new LegacySettingsConverter( legacyStorage, storage ).Convert();
  58. }
  59. storage.SaveSettings();
  60. }
  61. }
  62. private class LegacySettingsConverter : SettingsGroup
  63. {
  64. private ISettingsStorage legacy;
  65. public LegacySettingsConverter( ISettingsStorage legacy, ISettingsStorage current )
  66. : base( current )
  67. {
  68. this.legacy = legacy;
  69. }
  70. public void Convert()
  71. {
  72. Convert( "Form.x-location", "Gui.MainForm.Left" );
  73. Convert( "Form.x-location", "Gui.MiniForm.Left" );
  74. Convert( "Form.y-location", "Gui.MainForm.Top" );
  75. Convert( "Form.y-location", "Gui.MiniForm.Top" );
  76. Convert( "Form.width", "Gui.MainForm.Width" );
  77. Convert( "Form.width", "Gui.MiniForm.Width" );
  78. Convert( "Form.height", "Gui.MainForm.Height" );
  79. Convert( "Form.height", "Gui.MiniForm.Height" );
  80. Convert( "Form.maximized", "Gui.MainForm.Maximized", "False", "True" );
  81. Convert( "Form.maximized", "Gui.MiniForm.Maximized", "False", "True" );
  82. Convert( "Form.font", "Gui.MainForm.Font" );
  83. Convert( "Form.font", "Gui.MiniForm.Font" );
  84. Convert( "Form.tree-splitter-position", "Gui.MainForm.SplitPosition");
  85. Convert( "Form.tab-splitter-position", "Gui.ResultTabs.ErrorsTabSplitterPosition");
  86. Convert( "Options.TestLabels", "Gui.ResultTabs.DisplayTestLabels", "False", "True" );
  87. Convert( "Options.FailureToolTips", "Gui.ResultTabs.ErrorTab.ToolTipsEnabled", "False", "True" );
  88. Convert( "Options.EnableWordWrapForFailures", "Gui.ResultTabs.ErrorTab.WordWrapEnabled", "False", "True" );
  89. Convert( "Options.InitialTreeDisplay", "Gui.TestTree.InitialTreeDisplay", "Auto", "Expand", "Collapse", "HideTests" );
  90. Convert( "Options.ShowCheckBoxes", "Gui.TestTree.ShowCheckBoxes", "False", "True" );
  91. Convert( "Options.LoadLastProject", "Options.LoadLastProject", "False", "True" );
  92. Convert( "Options.ClearResults", "Options.TestLoader.ClearResultsOnReload", "False", "True" );
  93. Convert( "Options.ReloadOnChange", "Options.TestLoader.ReloadOnChange", "False", "True" );
  94. Convert( "Options.RerunOnChange", "Options.TestLoader.RerunOnChange", "False", "True" );
  95. Convert( "Options.ReloadOnRun", "Options.TestLoader.ReloadOnRun", "False", "True" );
  96. Convert( "Options.MergeAssemblies", "Options.TestLoader.MergeAssemblies", "False", "True" );
  97. //Convert( "Options.MultiDomain", "Options.TestLoader.MultiDomain", "False", "True" );
  98. Convert( "Options.AutoNamespaceSuites", "Options.TestLoader.AutoNamespaceSuites", "False", "True" );
  99. Convert( "Options.VisualStudioSupport", "Options.TestLoader.VisualStudioSupport", "False", "True" );
  100. Convert( "Recent-Projects.MaxFiles", "RecentProjects.MaxFiles" );
  101. object val = legacy.GetSetting("Options.MultiDomain");
  102. if (val != null && (bool)val)
  103. this.SaveSetting("Options.TestLoader.DomainUsage", NUnit.Core.DomainUsage.Multiple);
  104. int maxFiles = this.GetSetting( "RecentProjects.MaxFiles", 5 );
  105. for( int i = 1; i <= maxFiles; i++ )
  106. {
  107. string fileKey = string.Format( "File{0}", i );
  108. object fileEntry = legacy.GetSetting( "Recent-Projects." + fileKey );
  109. if ( fileEntry != null )
  110. this.SaveSetting( "RecentProjects." + fileKey, fileEntry );
  111. }
  112. }
  113. private void Convert( string legacyName, string currentName, params string[]values )
  114. {
  115. object val = legacy.GetSetting( legacyName );
  116. if ( val != null )
  117. {
  118. if ( val is int && values != null )
  119. {
  120. int ival = (int)val;
  121. if ( ival >= 0 && ival < values.Length )
  122. val = values[(int)val];
  123. }
  124. this.SaveSetting( currentName, val );
  125. }
  126. }
  127. }
  128. #endregion
  129. }
  130. }