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)
24 using System.ComponentModel;
25 using System.Reflection;
26 using System.Collections.Specialized;
28 namespace System.Configuration {
30 public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
33 protected ApplicationSettingsBase ()
35 Initialize (Context, Properties, Providers);
38 protected ApplicationSettingsBase (IComponent owner)
39 : this (owner, String.Empty)
44 protected ApplicationSettingsBase (string settingsKey)
46 this.settingsKey = settingsKey;
48 Initialize (Context, Properties, Providers);
51 protected ApplicationSettingsBase (IComponent owner,
55 throw new ArgumentNullException ();
57 providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
59 this.settingsKey = settingsKey;
61 Initialize (Context, Properties, Providers);
64 public event PropertyChangedEventHandler PropertyChanged;
65 public event SettingChangingEventHandler SettingChanging;
66 public event SettingsLoadedEventHandler SettingsLoaded;
67 public event SettingsSavingEventHandler SettingsSaving;
69 public object GetPreviousVersion (string propertyName)
71 throw new NotImplementedException ();
76 #if (CONFIGURATION_DEP)
77 foreach (SettingsProvider provider in Providers) {
78 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
87 #if (CONFIGURATION_DEP)
88 foreach (SettingsProvider provider in Providers) {
89 IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
98 public override void Save()
100 #if (CONFIGURATION_DEP)
101 /* ew.. this needs to be more efficient */
102 foreach (SettingsProvider provider in Providers) {
103 SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
105 foreach (SettingsPropertyValue val in PropertyValues) {
106 if (val.Property.Provider == provider)
111 provider.SetPropertyValues (Context, cache);
116 public virtual void Upgrade()
120 protected virtual void OnPropertyChanged (object sender,
121 PropertyChangedEventArgs e)
123 if (PropertyChanged != null)
124 PropertyChanged (sender, e);
127 protected virtual void OnSettingChanging (object sender,
128 SettingChangingEventArgs e)
130 if (SettingChanging != null)
131 SettingChanging (sender, e);
134 protected virtual void OnSettingsLoaded (object sender,
135 SettingsLoadedEventArgs e)
137 if (SettingsLoaded != null)
138 SettingsLoaded (sender, e);
141 protected virtual void OnSettingsSaving (object sender,
144 if (SettingsSaving != null)
145 SettingsSaving (sender, e);
149 public override SettingsContext Context {
152 context = new SettingsContext ();
158 void CacheValuesByProvider (SettingsProvider provider)
160 SettingsPropertyCollection col = new SettingsPropertyCollection ();
162 foreach (SettingsProperty p in Properties) {
163 if (p.Provider == provider)
168 SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
169 PropertyValues.Add (vals);
173 void InitializeSettings (SettingsPropertyCollection settings)
177 object GetPropertyValue (string propertyName)
179 SettingsProperty prop = Properties [ propertyName ];
182 throw new SettingsPropertyNotFoundException (propertyName);
184 if (propertyValues == null)
185 InitializeSettings (Properties);
187 if (PropertyValues [ propertyName ] == null)
188 CacheValuesByProvider (prop.Provider);
190 return PropertyValues [ propertyName ].PropertyValue;
194 public override object this [ string propertyName ] {
196 return GetPropertyValue (propertyName);
199 SettingsProperty prop = Properties [ propertyName ];
202 throw new SettingsPropertyNotFoundException (propertyName);
205 throw new SettingsPropertyIsReadOnlyException (propertyName);
207 /* XXX check the type of the property vs the type of @value */
209 !prop.PropertyType.IsAssignableFrom (value.GetType()))
210 throw new SettingsPropertyWrongTypeException (propertyName);
212 if (PropertyValues [ propertyName ] == null)
213 CacheValuesByProvider (prop.Provider);
215 SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
216 "" /* XXX settingClass? */,
221 OnSettingChanging (this, changing_args);
223 if (changing_args.Cancel == false) {
224 /* actually set the value */
225 PropertyValues [ propertyName ].PropertyValue = value;
227 /* then emit PropertyChanged */
228 OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
233 #if (CONFIGURATION_DEP)
235 public override SettingsPropertyCollection Properties {
237 if (properties == null) {
238 LocalFileSettingsProvider local_provider = null;
240 properties = new SettingsPropertyCollection ();
242 foreach (PropertyInfo prop in GetType().GetProperties (/* only public properties? */)) {
243 SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
244 if (setting_attrs != null && setting_attrs.Length > 0 &&
245 (setting_attrs[0] is UserScopedSettingAttribute
246 || setting_attrs[0] is ApplicationScopedSettingAttribute)) {
248 SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
249 SettingsProvider provider = null;
250 object defaultValue = null;
251 SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
253 foreach (Attribute a in prop.GetCustomAttributes (false)) {
254 if (a is UserScopedSettingAttribute
255 || a is ApplicationScopedSettingAttribute)
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);
263 else if (a is DefaultSettingValueAttribute) {
264 defaultValue = ((DefaultSettingValueAttribute)a).Value; /* XXX this is a string.. do we convert? */
266 else if (a is SettingsSerializeAsAttribute) {
267 serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
270 dict.Add (a.GetType().ToString() /* XXX ?*/, a);
274 SettingsProperty setting = new SettingsProperty (prop.Name,
278 defaultValue /* XXX always a string? */,
284 if (providerService != null)
285 setting.Provider = providerService.GetSettingsProvider (setting);
287 if (provider == null) {
288 if (local_provider == null)
289 local_provider = new LocalFileSettingsProvider ();
290 setting.Provider = local_provider;
293 if (provider != null) {
294 /* make sure we're using the same instance of a
295 given provider across multiple properties */
296 SettingsProvider p = Providers[provider.Name];
298 setting.Provider = p;
301 properties.Add (setting);
303 if (setting.Provider != null && Providers [setting.Provider.Name] == null)
304 Providers.Add (setting.Provider);
315 public override SettingsPropertyValueCollection PropertyValues {
317 if (propertyValues == null) {
318 propertyValues = new SettingsPropertyValueCollection ();
321 return propertyValues;
326 public override SettingsProviderCollection Providers {
328 if (providers == null)
329 providers = new SettingsProviderCollection ();
336 public string SettingsKey {
346 SettingsContext context;
347 SettingsPropertyCollection properties;
348 SettingsPropertyValueCollection propertyValues;
349 SettingsProviderCollection providers;
350 ISettingsProviderService providerService;