File: RegistryConfigSource.cs

package info (click to toggle)
nini 1.1.0%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,776 kB
  • ctags: 882
  • sloc: cs: 7,649; xml: 2,945; makefile: 62; ansic: 7
file content (280 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#region Copyright
//
// Nini Configuration Project.
// Copyright (C) 2006 Brent R. Matzelle.  All rights reserved.
//
// This software is published under the terms of the MIT X11 license, a copy of 
// which has been included with this distribution in the LICENSE.txt file.
// 
#endregion

using System;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using Nini.Ini;

namespace Nini.Config
{
	#region RegistryRecurse enumeration
	/// <include file='RegistryConfigSource.xml' path='//Enum[@name="RegistryRecurse"]/docs/*' />
	public enum RegistryRecurse
	{
		/// <include file='RegistryConfigSource.xml' path='//Enum[@name="RegistryRecurse"]/Value[@name="None"]/docs/*' />
		None,
		/// <include file='RegistryConfigSource.xml' path='//Enum[@name="RegistryRecurse"]/Value[@name="Flattened"]/docs/*' />
		Flattened,
		/// <include file='RegistryConfigSource.xml' path='//Enum[@name="RegistryRecurse"]/Value[@name="Namespacing"]/docs/*' />
		Namespacing
	}
	#endregion

	/// <include file='RegistryConfigSource.xml' path='//Class[@name="RegistryConfigSource"]/docs/*' />
	public class RegistryConfigSource : ConfigSourceBase
	{
		#region Private variables
		RegistryKey defaultKey = null;
		#endregion
		
		#region Public properties
		/// <include file='RegistryConfigSource.xml' path='//Property[@name="DefaultKey"]/docs/*' />
		public RegistryKey DefaultKey
		{
			get { return defaultKey; }
			set { defaultKey = value; }
		}
		#endregion

		#region Constructors
		#endregion
		
		#region Public methods
		/// <include file='RegistryConfigSource.xml' path='//Method[@name="AddConfig"]/docs/*' />
		public override IConfig AddConfig (string name)
		{
			if (this.DefaultKey == null) {
				throw new ApplicationException ("You must set DefaultKey");
			}

			return AddConfig (name, this.DefaultKey);
		}

		/// <include file='RegistryConfigSource.xml' path='//Method[@name="AddConfigKey"]/docs/*' />
		public IConfig AddConfig (string name, RegistryKey key)
		{
			RegistryConfig result = new RegistryConfig (name, this);
			result.Key = key;
			result.ParentKey = true;

			this.Configs.Add (result);

			return result;
		}

		/// <include file='RegistryConfigSource.xml' path='//Method[@name="AddMapping"]/docs/*' />
		public void AddMapping (RegistryKey registryKey, string path)
		{
			RegistryKey key = registryKey.OpenSubKey (path, true);
			
			if (key == null) {
				throw new ArgumentException ("The specified key does not exist");
			}
			
			LoadKeyValues (key, ShortKeyName (key));
		}
		
		/// <include file='RegistryConfigSource.xml' path='//Method[@name="AddMappingRecurse"]/docs/*' />
		public void AddMapping (RegistryKey registryKey, 
								string path, 
								RegistryRecurse recurse)
		{
			RegistryKey key = registryKey.OpenSubKey (path, true);
			
			if (key == null) {
				throw new ArgumentException ("The specified key does not exist");
			}
			
			if (recurse == RegistryRecurse.Namespacing) {
				LoadKeyValues (key, path);
			} else {
				LoadKeyValues (key, ShortKeyName (key));
			}
			
			string[] subKeys = key.GetSubKeyNames ();
			for (int i = 0; i < subKeys.Length; i++)
			{
				switch (recurse)
				{
				case RegistryRecurse.None:
					// no recursion
					break;
				case RegistryRecurse.Namespacing:
					AddMapping (registryKey, path + "\\" + subKeys[i], recurse);
					break;
				case RegistryRecurse.Flattened:
					AddMapping (key, subKeys[i], recurse);
					break;
				}
			}
		}
		
		/// <include file='IConfigSource.xml' path='//Method[@name="Save"]/docs/*' />
		public override void Save ()
		{
			MergeConfigsIntoDocument ();

			for (int i = 0; i < this.Configs.Count; i++)
			{
				// New merged configs are not RegistryConfigs
				if (this.Configs[i] is RegistryConfig) {
					RegistryConfig config = (RegistryConfig)this.Configs[i];
					string[] keys = config.GetKeys ();
					
					for (int j = 0; j < keys.Length; j++)
					{
						 config.Key.SetValue (keys[j], config.Get (keys[j]));
					}
				}
			}
		}

		/// <include file='IConfigSource.xml' path='//Method[@name="Reload"]/docs/*' />
		public override void Reload ()
		{
			ReloadKeys ();
		}
		#endregion
		
		#region Private methods
		/// <summary>
		/// Loads all values from the registry key.
		/// </summary>
		private void LoadKeyValues (RegistryKey key, string keyName)
		{
			RegistryConfig config = new RegistryConfig (keyName, this);
			config.Key = key;

			string[] values = key.GetValueNames ();
			foreach (string value in values)
			{
				config.Add (value, key.GetValue (value).ToString ());
			}
			this.Configs.Add (config);
		}

		/// <summary>
		/// Merges all of the configs from the config collection into the 
		/// registry.
		/// </summary>
		private void MergeConfigsIntoDocument ()
		{
			foreach (IConfig config in this.Configs)
			{
				if (config is RegistryConfig) {
					RegistryConfig registryConfig = (RegistryConfig)config;

					if (registryConfig.ParentKey) {
						registryConfig.Key = 
							registryConfig.Key.CreateSubKey (registryConfig.Name);
					}
					RemoveKeys (registryConfig);

					string[] keys = config.GetKeys ();
					for (int i = 0; i < keys.Length; i++)
					{
						registryConfig.Key.SetValue (keys[i], config.Get (keys[i]));
					}
					registryConfig.Key.Flush ();
				}
			}
		}

		/// <summary>
		/// Reloads all keys.
		/// </summary>
		private void ReloadKeys ()
		{
			RegistryKey[] keys = new RegistryKey[this.Configs.Count];

			for (int i = 0; i < keys.Length; i++)
			{
				keys[i] = ((RegistryConfig)this.Configs[i]).Key;
			}

			this.Configs.Clear ();
			for (int i = 0; i < keys.Length; i++)
			{
				LoadKeyValues (keys[i], ShortKeyName (keys[i]));
			}
		}

		/// <summary>
		/// Removes all keys not present in the current config.  
		/// </summary>
		private void RemoveKeys (RegistryConfig config)
		{
			foreach (string valueName in config.Key.GetValueNames ())
			{
				if (!config.Contains (valueName)) {
					config.Key.DeleteValue (valueName);
				}
			}
		}
		
		/// <summary>
		/// Returns the key name without the fully qualified path.
		/// e.g. no HKEY_LOCAL_MACHINE\\MyKey, just MyKey
		/// </summary>
		private string ShortKeyName (RegistryKey key)
		{
			int index = key.Name.LastIndexOf ("\\");

			return (index == -1) ? key.Name : key.Name.Substring (index + 1);
		}
		
		#region RegistryConfig class
		/// <summary>
		/// Registry Config class.
		/// </summary>
		private class RegistryConfig : ConfigBase
		{
			#region Private variables
			RegistryKey key = null;
			bool parentKey = false;
			#endregion

			#region Constructor
			/// <summary>
			/// Constructor.
			/// </summary>
			public RegistryConfig (string name, IConfigSource source)
				: base (name, source)
			{
			}
			#endregion

			#region Public properties
			/// <summary>
			/// Gets or sets whether the key is a parent key. 
			/// </summary>
			public bool ParentKey
			{
				get { return parentKey; }
				set { parentKey = value; }
			}

			/// <summary>
			/// Registry key for the Config.
			/// </summary>
			public RegistryKey Key
			{
				get { return key; }
				set { key = value; }
			}
			#endregion
		}
		#endregion

		#endregion
	}
}