1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
24 #if CONFIGURATION_DEP && !TARGET_JVM
25 extern alias PrebuiltSystem;
26 using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
30 using System.Xml.Serialization;
33 using System.ComponentModel;
34 using System.Reflection;
35 using System.Collections.Specialized;
37 namespace System.Configuration {
39 public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
42 protected ApplicationSettingsBase ()
44 Initialize (Context, Properties, Providers);
47 protected ApplicationSettingsBase (IComponent owner)
48 : this (owner, String.Empty)
53 protected ApplicationSettingsBase (string settingsKey)
55 this.settingsKey = settingsKey;
57 Initialize (Context, Properties, Providers);
60 protected ApplicationSettingsBase (IComponent owner,
64 throw new ArgumentNullException ();
66 providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
68 this.settingsKey = settingsKey;
70 Initialize (Context, Properties, Providers);
73 public event PropertyChangedEventHandler PropertyChanged;
74 public event SettingChangingEventHandler SettingChanging;
75 public event SettingsLoadedEventHandler SettingsLoaded;
76 public event SettingsSavingEventHandler SettingsSaving;
78 public object GetPreviousVersion (string propertyName)
80 throw new NotImplementedException ();
85 #if (CONFIGURATION_DEP)
86 foreach (SettingsProvider provider in Providers) {
87 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
96 #if (CONFIGURATION_DEP)
97 foreach (SettingsProvider provider in Providers) {
98 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
100 iasp.Reset (Context);
107 public override void Save()
109 #if (CONFIGURATION_DEP)
110 /* ew.. this needs to be more efficient */
111 foreach (SettingsProvider provider in Providers) {
112 SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
114 foreach (SettingsPropertyValue val in PropertyValues) {
115 if (val.Property.Provider == provider)
120 provider.SetPropertyValues (Context, cache);
125 public virtual void Upgrade()
129 protected virtual void OnPropertyChanged (object sender,
130 PropertyChangedEventArgs e)
132 if (PropertyChanged != null)
133 PropertyChanged (sender, e);
136 protected virtual void OnSettingChanging (object sender,
137 SettingChangingEventArgs e)
139 if (SettingChanging != null)
140 SettingChanging (sender, e);
143 protected virtual void OnSettingsLoaded (object sender,
144 SettingsLoadedEventArgs e)
146 if (SettingsLoaded != null)
147 SettingsLoaded (sender, e);
150 protected virtual void OnSettingsSaving (object sender,
153 if (SettingsSaving != null)
154 SettingsSaving (sender, e);
158 public override SettingsContext Context {
161 context = new SettingsContext ();
167 void CacheValuesByProvider (SettingsProvider provider)
169 SettingsPropertyCollection col = new SettingsPropertyCollection ();
171 foreach (SettingsProperty p in Properties) {
172 if (p.Provider == provider)
177 SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
178 PropertyValues.Add (vals);
181 OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider));
184 void InitializeSettings (SettingsPropertyCollection settings)
188 object GetPropertyValue (string propertyName)
190 SettingsProperty prop = Properties [ propertyName ];
193 throw new SettingsPropertyNotFoundException (propertyName);
195 if (propertyValues == null)
196 InitializeSettings (Properties);
198 if (PropertyValues [ propertyName ] == null)
199 CacheValuesByProvider (prop.Provider);
201 return PropertyValues [ propertyName ].PropertyValue;
205 public override object this [ string propertyName ] {
207 return GetPropertyValue (propertyName);
210 SettingsProperty prop = Properties [ propertyName ];
213 throw new SettingsPropertyNotFoundException (propertyName);
216 throw new SettingsPropertyIsReadOnlyException (propertyName);
218 /* XXX check the type of the property vs the type of @value */
220 !prop.PropertyType.IsAssignableFrom (value.GetType()))
221 throw new SettingsPropertyWrongTypeException (propertyName);
223 if (PropertyValues [ propertyName ] == null)
224 CacheValuesByProvider (prop.Provider);
226 SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
232 OnSettingChanging (this, changing_args);
234 if (changing_args.Cancel == false) {
235 /* actually set the value */
236 PropertyValues [ propertyName ].PropertyValue = value;
238 /* then emit PropertyChanged */
239 OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
244 #if (CONFIGURATION_DEP)
246 public override SettingsPropertyCollection Properties {
248 if (properties == null) {
249 LocalFileSettingsProvider local_provider = null;
251 properties = new SettingsPropertyCollection ();
253 foreach (PropertyInfo prop in GetType().GetProperties (/* only public properties? */)) {
254 SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
255 if (setting_attrs != null && setting_attrs.Length > 0) {
256 SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
257 SettingsProvider provider = null;
258 object defaultValue = null;
259 SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
261 foreach (Attribute a in prop.GetCustomAttributes (false)) {
262 /* the attributes we handle natively here */
263 if (a is SettingsProviderAttribute) {
264 Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName);
265 provider = (SettingsProvider) Activator.CreateInstance (provider_type);
266 provider.Initialize (null, null);
268 else if (a is DefaultSettingValueAttribute) {
269 defaultValue = ((DefaultSettingValueAttribute)a).Value; /* XXX this is a string.. do we convert? */
270 // note: for StringCollection, TypeDescriptor.GetConverter(prop.PropertyType) returns
271 // CollectionConverter, however this class cannot handle the XML serialized strings
272 if (prop.PropertyType == typeof(StringCollection)) {
273 XmlSerializer xs = new XmlSerializer (typeof (string[]));
274 string[] values = (string[]) xs.Deserialize (new StringReader ((string)defaultValue));
275 StringCollection sc = new StringCollection ();
276 sc.AddRange (values);
278 } else if (prop.PropertyType != typeof(string)) {
279 defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue);
282 else if (a is SettingsSerializeAsAttribute) {
283 serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
285 else if (a is ApplicationScopedSettingAttribute ||
286 a is UserScopedSettingAttribute) {
287 dict.Add (a.GetType(), a);
290 dict.Add (a.GetType(), a);
294 SettingsProperty setting = new SettingsProperty (prop.Name,
298 defaultValue /* XXX always a string? */,
304 if (providerService != null)
305 setting.Provider = providerService.GetSettingsProvider (setting);
307 if (provider == null) {
308 if (local_provider == null) {
309 local_provider = new LocalFileSettingsProvider ();
310 local_provider.Initialize (null, null);
312 setting.Provider = local_provider;
315 if (provider != null) {
316 /* make sure we're using the same instance of a
317 given provider across multiple properties */
318 SettingsProvider p = Providers[provider.Name];
320 setting.Provider = p;
323 properties.Add (setting);
325 if (setting.Provider != null && Providers [setting.Provider.Name] == null)
326 Providers.Add (setting.Provider);
337 public override SettingsPropertyValueCollection PropertyValues {
339 if (propertyValues == null) {
340 propertyValues = new SettingsPropertyValueCollection ();
343 return propertyValues;
348 public override SettingsProviderCollection Providers {
350 if (providers == null)
351 providers = new SettingsProviderCollection ();
358 public string SettingsKey {
368 SettingsContext context;
369 SettingsPropertyCollection properties;
370 SettingsPropertyValueCollection propertyValues;
371 SettingsProviderCollection providers;
372 ISettingsProviderService providerService;