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 Novell, Inc (http://www.novell.com)
25 extern alias PrebuiltSystem;
26 using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
29 using System.ComponentModel;
30 using System.Reflection;
31 using System.Collections.Specialized;
33 namespace System.Configuration {
35 public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
38 protected ApplicationSettingsBase ()
40 Initialize (Context, Properties, Providers);
43 protected ApplicationSettingsBase (IComponent owner)
44 : this (owner, String.Empty)
49 protected ApplicationSettingsBase (string settingsKey)
51 this.settingsKey = settingsKey;
53 Initialize (Context, Properties, Providers);
56 protected ApplicationSettingsBase (IComponent owner,
60 throw new ArgumentNullException ();
62 providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
64 this.settingsKey = settingsKey;
66 Initialize (Context, Properties, Providers);
69 public event PropertyChangedEventHandler PropertyChanged;
70 public event SettingChangingEventHandler SettingChanging;
71 public event SettingsLoadedEventHandler SettingsLoaded;
72 public event SettingsSavingEventHandler SettingsSaving;
74 public object GetPreviousVersion (string propertyName)
76 throw new NotImplementedException ();
81 #if (CONFIGURATION_DEP)
82 foreach (SettingsProvider provider in Providers) {
83 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
92 #if (CONFIGURATION_DEP)
93 foreach (SettingsProvider provider in Providers) {
94 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
103 public override void Save()
105 #if (CONFIGURATION_DEP)
106 /* ew.. this needs to be more efficient */
107 foreach (SettingsProvider provider in Providers) {
108 SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
110 foreach (SettingsPropertyValue val in PropertyValues) {
111 if (val.Property.Provider == provider)
116 provider.SetPropertyValues (Context, cache);
121 public virtual void Upgrade()
125 protected virtual void OnPropertyChanged (object sender,
126 PropertyChangedEventArgs e)
128 if (PropertyChanged != null)
129 PropertyChanged (sender, e);
132 protected virtual void OnSettingChanging (object sender,
133 SettingChangingEventArgs e)
135 if (SettingChanging != null)
136 SettingChanging (sender, e);
139 protected virtual void OnSettingsLoaded (object sender,
140 SettingsLoadedEventArgs e)
142 if (SettingsLoaded != null)
143 SettingsLoaded (sender, e);
146 protected virtual void OnSettingsSaving (object sender,
149 if (SettingsSaving != null)
150 SettingsSaving (sender, e);
154 public override SettingsContext Context {
157 context = new SettingsContext ();
163 void CacheValuesByProvider (SettingsProvider provider)
165 SettingsPropertyCollection col = new SettingsPropertyCollection ();
167 foreach (SettingsProperty p in Properties) {
168 if (p.Provider == provider)
173 SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
174 PropertyValues.Add (vals);
177 OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider));
180 void InitializeSettings (SettingsPropertyCollection settings)
184 object GetPropertyValue (string propertyName)
186 SettingsProperty prop = Properties [ propertyName ];
189 throw new SettingsPropertyNotFoundException (propertyName);
191 if (propertyValues == null)
192 InitializeSettings (Properties);
194 if (PropertyValues [ propertyName ] == null)
195 CacheValuesByProvider (prop.Provider);
197 return PropertyValues [ propertyName ].PropertyValue;
201 public override object this [ string propertyName ] {
203 return GetPropertyValue (propertyName);
206 SettingsProperty prop = Properties [ propertyName ];
209 throw new SettingsPropertyNotFoundException (propertyName);
212 throw new SettingsPropertyIsReadOnlyException (propertyName);
214 /* XXX check the type of the property vs the type of @value */
216 !prop.PropertyType.IsAssignableFrom (value.GetType()))
217 throw new SettingsPropertyWrongTypeException (propertyName);
219 if (PropertyValues [ propertyName ] == null)
220 CacheValuesByProvider (prop.Provider);
222 SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
228 OnSettingChanging (this, changing_args);
230 if (changing_args.Cancel == false) {
231 /* actually set the value */
232 PropertyValues [ propertyName ].PropertyValue = value;
234 /* then emit PropertyChanged */
235 OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
240 #if (CONFIGURATION_DEP)
242 public override SettingsPropertyCollection Properties {
244 if (properties == null) {
245 LocalFileSettingsProvider local_provider = null;
247 properties = new SettingsPropertyCollection ();
249 foreach (PropertyInfo prop in GetType().GetProperties (/* only public properties? */)) {
250 SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
251 if (setting_attrs != null && setting_attrs.Length > 0) {
252 SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
253 SettingsProvider provider = null;
254 object defaultValue = null;
255 SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
257 foreach (Attribute a in prop.GetCustomAttributes (false)) {
258 /* the attributes we handle natively here */
259 if (a is SettingsProviderAttribute) {
260 Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName);
261 provider = (SettingsProvider) Activator.CreateInstance (provider_type);
262 provider.Initialize (null, null);
264 else if (a is DefaultSettingValueAttribute) {
265 defaultValue = ((DefaultSettingValueAttribute)a).Value; /* XXX this is a string.. do we convert? */
266 if (prop.PropertyType != typeof(string)) {
267 defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue);
270 else if (a is SettingsSerializeAsAttribute) {
271 serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
273 else if (a is ApplicationScopedSettingAttribute ||
274 a is UserScopedSettingAttribute) {
275 dict.Add (a.GetType(), a);
278 dict.Add (a.GetType(), a);
282 SettingsProperty setting = new SettingsProperty (prop.Name,
286 defaultValue /* XXX always a string? */,
292 if (providerService != null)
293 setting.Provider = providerService.GetSettingsProvider (setting);
295 if (provider == null) {
296 if (local_provider == null) {
297 local_provider = new LocalFileSettingsProvider ();
298 local_provider.Initialize (null, null);
300 setting.Provider = local_provider;
303 if (provider != null) {
304 /* make sure we're using the same instance of a
305 given provider across multiple properties */
306 SettingsProvider p = Providers[provider.Name];
308 setting.Provider = p;
311 properties.Add (setting);
313 if (setting.Provider != null && Providers [setting.Provider.Name] == null)
314 Providers.Add (setting.Provider);
325 public override SettingsPropertyValueCollection PropertyValues {
327 if (propertyValues == null) {
328 propertyValues = new SettingsPropertyValueCollection ();
331 return propertyValues;
336 public override SettingsProviderCollection Providers {
338 if (providers == null)
339 providers = new SettingsProviderCollection ();
346 public string SettingsKey {
356 SettingsContext context;
357 SettingsPropertyCollection properties;
358 SettingsPropertyValueCollection propertyValues;
359 SettingsProviderCollection providers;
360 ISettingsProviderService providerService;