PageRenderTime 42ms CodeModel.GetById 34ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

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

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