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