File: ApplicationSettingsBase.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (441 lines) | stat: -rw-r--r-- 12,553 bytes parent folder | download
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
//

#if CONFIGURATION_DEP
extern alias PrebuiltSystem;
using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
#endif
#if CONFIGURATION_DEP
using System.IO;
using System.Xml.Serialization;
#endif

using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Collections.Specialized;

namespace System.Configuration {

	public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
	{
		protected ApplicationSettingsBase ()
		{
			Initialize (Context, Properties, Providers);
		}

		protected ApplicationSettingsBase (IComponent owner)
			: this (owner, String.Empty)
		{
		}
 
		protected ApplicationSettingsBase (string settingsKey)
		{
			this.settingsKey = settingsKey;

			Initialize (Context, Properties, Providers);
		}

		protected ApplicationSettingsBase (IComponent owner, 
						   string settingsKey)
		{
			if (owner == null)
				throw new ArgumentNullException ();

#if (CONFIGURATION_DEP)
			providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
#endif
			this.settingsKey = settingsKey;

			Initialize (Context, Properties, Providers);
		}

		public event PropertyChangedEventHandler PropertyChanged;
		public event SettingChangingEventHandler SettingChanging;
		public event SettingsLoadedEventHandler SettingsLoaded;
		public event SettingsSavingEventHandler SettingsSaving;

		public object GetPreviousVersion (string propertyName)
		{
			throw new NotImplementedException ();
		}

		public void Reload ()
		{
#if (CONFIGURATION_DEP)
			foreach (SettingsProvider provider in Providers) {
//				IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
				CacheValuesByProvider(provider);
			}
#endif
		}

		public void Reset()
		{
#if (CONFIGURATION_DEP)
			Reload ();
			foreach (SettingsPropertyValue pv in PropertyValues)
				pv.PropertyValue = pv.Reset();
#endif
		}

		public override void Save()
		{
#if (CONFIGURATION_DEP)
			Context.CurrentSettings = this;
			/* ew.. this needs to be more efficient */
			foreach (SettingsProvider provider in Providers) {
				SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();

				foreach (SettingsPropertyValue val in PropertyValues) {
					if (val.Property.Provider == provider)
						cache.Add (val);
				}

				if (cache.Count > 0)
					provider.SetPropertyValues (Context, cache);
			}
			Context.CurrentSettings = null;
#endif
		}

		public virtual void Upgrade()
		{
		}

		protected virtual void OnPropertyChanged (object sender, 
							  PropertyChangedEventArgs e)
		{
			if (PropertyChanged != null)
				PropertyChanged (sender, e);
		}

		protected virtual void OnSettingChanging (object sender, 
							  SettingChangingEventArgs e)
		{
			if (SettingChanging != null)
				SettingChanging (sender, e);
		}

		protected virtual void OnSettingsLoaded (object sender, 
							 SettingsLoadedEventArgs e)
		{
			if (SettingsLoaded != null)
				SettingsLoaded (sender, e);
		}

		protected virtual void OnSettingsSaving (object sender, 
							 CancelEventArgs e)
		{
			if (SettingsSaving != null)
				SettingsSaving (sender, e);
		}

		[Browsable (false)]
		public override SettingsContext Context {
			get {
				if (IsSynchronized)
					Monitor.Enter (this);

				try {
					if (context == null) {
						context = new SettingsContext ();
						context ["SettingsKey"] = "";
						Type type = GetType ();
						context ["GroupName"] = type.FullName;
						context ["SettingsClassType"] = type;
					}

					return context;
				} finally {
					if (IsSynchronized)
						Monitor.Exit (this);
				}
			}
		}

		void CacheValuesByProvider (SettingsProvider provider)
		{
			SettingsPropertyCollection col = new SettingsPropertyCollection ();

			foreach (SettingsProperty p in Properties) {
				if (p.Provider == provider)
					col.Add (p);
			}

			if (col.Count > 0) {
				SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
				foreach (SettingsPropertyValue prop in vals) {
					if (PropertyValues [prop.Name] != null)
						PropertyValues [prop.Name].PropertyValue = prop.PropertyValue;
					else
						PropertyValues.Add (prop);
				}
			}

			OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider));
		}

		void InitializeSettings (SettingsPropertyCollection settings)
		{
		}

		object GetPropertyValue (string propertyName)
		{
			SettingsProperty prop = Properties [ propertyName ];

			if (prop == null)
				throw new SettingsPropertyNotFoundException (propertyName);

			if (propertyValues == null)
				InitializeSettings (Properties);

			if (PropertyValues [ propertyName ] == null)
				CacheValuesByProvider (prop.Provider);

			return PropertyValues [ propertyName ].PropertyValue;
		}

		[MonoTODO]
		public override object this [ string propertyName ] {
			get {
				if (IsSynchronized) {
					lock (this) {
						return GetPropertyValue (propertyName);
					}
				}

				return GetPropertyValue (propertyName);
			}
			set {
				SettingsProperty prop = Properties [ propertyName ];

				if (prop == null)
					throw new SettingsPropertyNotFoundException (propertyName);

				if (prop.IsReadOnly)
					throw new SettingsPropertyIsReadOnlyException (propertyName);

				/* XXX check the type of the property vs the type of @value */
				if (value != null &&
				    !prop.PropertyType.IsAssignableFrom (value.GetType()))
					throw new SettingsPropertyWrongTypeException (propertyName);

				if (PropertyValues [ propertyName ] == null)
					CacheValuesByProvider (prop.Provider);

				SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
												       GetType().FullName,
												       settingsKey,
												       value,
												       false);

				OnSettingChanging (this, changing_args);

				if (changing_args.Cancel == false) {
					/* actually set the value */
					PropertyValues [ propertyName ].PropertyValue = value;

					/* then emit PropertyChanged */
					OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
				}
			}
		}

#if (CONFIGURATION_DEP)
		[Browsable (false)]
		public override SettingsPropertyCollection Properties {
			get {
				if (IsSynchronized)
					Monitor.Enter (this);

				try {
					if (properties == null) {
						SettingsProvider local_provider = null;

						properties = new SettingsPropertyCollection ();

						Type this_type = GetType();
						SettingsProviderAttribute[] provider_attrs = (SettingsProviderAttribute[])this_type.GetCustomAttributes (typeof (SettingsProviderAttribute), false);;
						if (provider_attrs != null && provider_attrs.Length != 0) {
							Type provider_type = Type.GetType (provider_attrs[0].ProviderTypeName);
							SettingsProvider provider = (SettingsProvider) Activator.CreateInstance (provider_type);
							provider.Initialize (null, null);
							if (provider != null && Providers [provider.Name] == null) {
								Providers.Add (provider);
								local_provider = provider;
							}
						}

						PropertyInfo[] type_props = this_type.GetProperties ();
						foreach (PropertyInfo prop in type_props) { // only public properties
							SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
							if (setting_attrs == null || setting_attrs.Length == 0)
								continue;
							CreateSettingsProperty (prop, properties, ref local_provider);
						}
					}

					return properties;
				} finally {
					if (IsSynchronized)
						Monitor.Exit (this);
				}
			}
		}

		void CreateSettingsProperty (PropertyInfo prop, SettingsPropertyCollection properties, ref SettingsProvider local_provider)
		{
			SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
			SettingsProvider provider = null;
			object defaultValue = null;
			SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
			bool explicitSerializeAs = false;

			foreach (Attribute a in prop.GetCustomAttributes (false)) {
				/* the attributes we handle natively here */
				if (a is SettingsProviderAttribute) {
					Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName);
					provider = (SettingsProvider) Activator.CreateInstance (provider_type);
					provider.Initialize (null, null);
				}
				else if (a is DefaultSettingValueAttribute) {
					defaultValue = ((DefaultSettingValueAttribute)a).Value;
				}
				else if (a is SettingsSerializeAsAttribute) {
					serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
					explicitSerializeAs = true;
				}
				else if (a is ApplicationScopedSettingAttribute ||
					 a is UserScopedSettingAttribute) {
					dict.Add (a.GetType(), a);
				}
				else {
					dict.Add (a.GetType(), a);
				}
			}

			if (!explicitSerializeAs) {
				// DefaultValue is a string and if we can't convert from string to the 
				// property type then the only other option left is for the string to 
				// be XML.
				//
				TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType);
				if (converter != null && 
				    (!converter.CanConvertFrom (typeof (string)) || 
				     !converter.CanConvertTo (typeof (string))))
					serializeAs = SettingsSerializeAs.Xml;
			}

			SettingsProperty setting =
				new SettingsProperty (prop.Name, prop.PropertyType, provider, false /* XXX */,
						      defaultValue /* XXX always a string? */, serializeAs, dict,
						      false, false);


			if (providerService != null)
				setting.Provider = providerService.GetSettingsProvider (setting);

			if (provider == null) {
				if (local_provider == null) {
					local_provider = new LocalFileSettingsProvider () as SettingsProvider;
					local_provider.Initialize (null, null);
				}
				setting.Provider = local_provider;
				// .NET ends up to set this to providers.
				provider = local_provider;
			}

			if (provider != null) {
				/* make sure we're using the same instance of a
				   given provider across multiple properties */
				SettingsProvider p = Providers[provider.Name];
				if (p != null)
					setting.Provider = p;
			}

			properties.Add (setting);

			if (setting.Provider != null && Providers [setting.Provider.Name] == null)
				Providers.Add (setting.Provider);
		}
#endif

		[Browsable (false)]
		public override SettingsPropertyValueCollection PropertyValues {
			get {
				if (IsSynchronized)
					Monitor.Enter (this);

				try {
					if (propertyValues == null) {
						propertyValues = new SettingsPropertyValueCollection ();
					}

					return propertyValues;
				} finally {
					if (IsSynchronized)
						Monitor.Exit (this);
				}
			}
		}

		[Browsable (false)]
		public override SettingsProviderCollection Providers {
			get {
				if (IsSynchronized)
					Monitor.Enter (this);

				try {
					if (providers == null)
						providers = new SettingsProviderCollection ();

					return providers;
				} finally {
					if (IsSynchronized)
						Monitor.Exit (this);
				}
			}
		}

		[Browsable (false)]
		public string SettingsKey {
			get {
				return settingsKey;
			}
			set {
				settingsKey = value;
			}
		}

		string settingsKey;
		SettingsContext context;
#if (CONFIGURATION_DEP)		
		SettingsPropertyCollection properties;
		ISettingsProviderService providerService;
#endif
		SettingsPropertyValueCollection propertyValues;
		SettingsProviderCollection providers;
        }

}