2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / util / RegistrySettingsStorage.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 using System;
31 using System.Diagnostics;
32 using Microsoft.Win32;
33
34 namespace NUnit.Util
35 {
36         /// <summary>
37         /// Implementation of SettingsStorage for NUnit user settings,
38         /// based on storage of settings in the registry.
39         /// </summary>
40         public class RegistrySettingsStorage : SettingsStorage, IDisposable
41         {
42                 #region Instance Variables
43
44                 /// <summary>
45                 /// If not null, the registry key for this storage
46                 /// </summary>
47                 private RegistryKey storageKey;
48
49                 #endregion
50
51                 #region Construction and Disposal
52
53                 /// <summary>
54                 /// Construct a storage as a child of another storage
55                 /// </summary>
56                 /// <param name="storageName">The name to give the storage</param>
57                 /// <param name="parentStorage">The parent in which the storage is to be created</param>
58                 public RegistrySettingsStorage( string storageName, RegistrySettingsStorage parentStorage ) 
59                         : base( storageName, parentStorage ) 
60                 { 
61                         this.storageKey = parentStorage.StorageKey.CreateSubKey( storageName );
62                 }
63
64                 /// <summary>
65                 /// Construct a storage using a registry key. This constructor is
66                 /// intended for use at the top level of the hierarchy.
67                 /// </summary>
68                 /// <param name="storageName">The name to give the storage</param>
69                 /// <param name="parentKey">The registry Key under which the storage will be created</param>
70                 public RegistrySettingsStorage( string storageName, RegistryKey parentKey ) 
71                         : base ( storageName, null )
72                 {
73                         this.storageKey = parentKey.CreateSubKey( storageName );
74                 }
75
76                 /// <summary>
77                 /// Construct a storage on top of a given key, using the key's name
78                 /// </summary>
79                 /// <param name="storageKey"></param>
80                 public RegistrySettingsStorage( RegistryKey storageKey )
81                         : base( storageKey.Name, null )
82                 {
83                         this.storageKey = storageKey;
84                 }
85
86                 /// <summary>
87                 /// Dispose of this object by closing the storage key, if any
88                 /// </summary>
89                 public override void Dispose()
90                 {
91                         if ( storageKey != null )
92                                 storageKey.Close();
93                 }
94
95                 #endregion
96
97                 #region Properties
98
99                 /// <summary>
100                 /// The registry key used to hold this storage
101                 /// </summary>
102                 public RegistryKey StorageKey
103                 {
104                         get { return storageKey; }
105                 }
106
107                 /// <summary>
108                 /// The count of settings in this storage
109                 /// </summary>
110                 public override int SettingsCount
111                 {
112                         get { return storageKey.ValueCount; }
113                 }
114
115                 #endregion
116
117                 #region Methods
118
119                 /// <summary>
120                 /// Find out if a child storage exists
121                 /// </summary>
122                 /// <param name="storageName">Name of the child storage</param>
123                 /// <returns>True if the child storage exists</returns>
124                 public override bool ChildStorageExists( string storageName )
125                 {
126                         using (RegistryKey key = storageKey.OpenSubKey( storageName ) )
127                         {
128                                 return key != null;
129                         }
130                 }
131
132                 /// <summary>
133                 /// Make a new child storage under this one
134                 /// </summary>
135                 /// <param name="storageName">Name of the child storage to make</param>
136                 /// <returns>New storage</returns>
137                 public override SettingsStorage MakeChildStorage( string storageName )
138                 {
139                         return new RegistrySettingsStorage( storageName, this );
140                 }
141
142                 /// <summary>
143                 /// Load a setting from this storage
144                 /// </summary>
145                 /// <param name="settingName">Name of the setting to load</param>
146                 /// <returns>Value of the setting</returns>
147                 public override object LoadSetting( string settingName )
148                 {
149                         return storageKey.GetValue( settingName );
150                 }
151
152                 /// <summary>
153                 /// Load an int setting from this storage. Since int is a
154                 /// value type, we can't return null so zero is used to
155                 /// indicate that nothing was found - or the found value
156                 /// was zero. If you need to distinguish, use your own 
157                 /// default value or call LoadSetting and check for null.
158                 /// </summary>
159                 /// <param name="settingName">Name of the setting to load</param>
160                 /// <returns>Value of the setting or zero if missing</returns>
161                 public override int LoadIntSetting( string settingName )
162                 {
163                         return LoadIntSetting( settingName, 0 );
164                 }
165
166                 /// <summary>
167                 /// Load a string setting from this storage
168                 /// </summary>
169                 /// <param name="settingName">Name of the setting to load</param>
170                 /// <returns>Value of the setting</returns>
171                 public override string LoadStringSetting( string settingName )
172                 {
173                         object resultValue = storageKey.GetValue( settingName );
174                         if ( resultValue == null || resultValue is string )
175                                 return (string) resultValue;
176
177                         return resultValue.ToString();
178                 }
179
180                 /// <summary>
181                 /// Load a setting from this storage or return a default value
182                 /// </summary>
183                 /// <param name="settingName">Name of setting to load</param>
184                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
185                 /// <returns>Value of the setting or the default</returns>
186                 public override object LoadSetting( string settingName, object defaultValue )
187                 {
188                         return storageKey.GetValue( settingName, defaultValue );
189                 }
190
191                 /// <summary>
192                 /// Load an integer setting from this storage or return a default value
193                 /// </summary>
194                 /// <param name="settingName">Name of setting to load</param>
195                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
196                 /// <returns>Value of the setting or the default</returns>
197                 public override int LoadIntSetting( string settingName, int defaultValue )
198                 {
199                         object resultValue = storageKey.GetValue( settingName, defaultValue );
200                         if ( resultValue is int )
201                                 return (int)resultValue;
202                         
203                         return int.Parse( (string)resultValue );
204                 }
205
206                 /// <summary>
207                 /// Load a string setting from this storage or return a default value
208                 /// </summary>
209                 /// <param name="settingName">Name of setting to load</param>
210                 /// <param name="defaultValue">Value to return if the seeting is not present</param>
211                 /// <returns>Value of the setting or the default</returns>
212                 public override string LoadStringSetting( string settingName, string defaultValue )
213                 {
214                         object resultValue = storageKey.GetValue( settingName, defaultValue );
215                         if ( resultValue is string )
216                                 return (string) resultValue;
217
218                         return resultValue.ToString();
219                 }
220
221                 /// <summary>
222                 /// Remove a setting from the storage
223                 /// </summary>
224                 /// <param name="settingName">Name of the setting to remove</param>
225                 public override void RemoveSetting( string settingName )
226                 {
227                         storageKey.DeleteValue( settingName, false );
228                 }
229
230                 /// <summary>
231                 /// Save a setting in this storage
232                 /// </summary>
233                 /// <param name="settingName">Name of the setting to save</param>
234                 /// <param name="settingValue">Value to be saved</param>
235                 public override void SaveSetting( string settingName, object settingValue )
236                 {
237                         storageKey.SetValue( settingName, settingValue );
238                 }
239
240                 /// <summary>
241                 /// Static function that clears out the contents of a key
242                 /// </summary>
243                 /// <param name="key">Key to be cleared</param>
244                 public static void ClearKey( RegistryKey key )
245                 {
246                         foreach( string name in key.GetValueNames() )
247                                 key.DeleteValue( name );
248
249                         foreach( string name in key.GetSubKeyNames() )
250                                 key.DeleteSubKeyTree( name );
251                 }
252
253                 /// <summary>
254                 /// Clear all settings from the storage - empty storage remains
255                 /// </summary>
256                 public override void Clear()
257                 {
258                         ClearKey( storageKey );
259                 }
260
261                 #endregion
262         }
263 }