[System] Removal of the NET_2_0 in the source code
[mono.git] / mcs / class / System / System.Configuration / ApplicationSettingsBase.cs
index b554e9903f4b1cea3bb25e78ef2dc1882ff0df41..79b3308feb693aeaeab14417707b5863bc5ae35c 100644 (file)
 // 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 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
 //
 
-#if NET_2_0
-#if CONFIGURATION_DEP
+#if CONFIGURATION_DEP && !TARGET_JVM
 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
+       public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
        {
-
-                protected ApplicationSettingsBase ()
-                {
+               protected ApplicationSettingsBase ()
+               {
                        Initialize (Context, Properties, Providers);
-                }
+               }
 
                protected ApplicationSettingsBase (IComponent owner)
                        : this (owner, String.Empty)
                {
                }
-
  
                protected ApplicationSettingsBase (string settingsKey)
                {
@@ -59,8 +61,9 @@ namespace System.Configuration {
                        if (owner == null)
                                throw new ArgumentNullException ();
 
+#if (CONFIGURATION_DEP)
                        providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
-
+#endif
                        this.settingsKey = settingsKey;
 
                        Initialize (Context, Properties, Providers);
@@ -90,12 +93,13 @@ namespace System.Configuration {
                public void Reset()
                {
 #if (CONFIGURATION_DEP)
-                       foreach (SettingsProvider provider in Providers) {
-                               IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
-                               if (iasp != null)
-                                       iasp.Reset (Context);
-                       }
-
+                       // Code bellow is identical to code in Reload().
+                       // foreach (SettingsProvider provider in Providers) {
+                       //         IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
+                       //         if (iasp != null)
+                       //              iasp.Reset (Context);
+                       // }
+                                                
                        Reload ();
 #endif
                }
@@ -103,6 +107,7 @@ namespace System.Configuration {
                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 ();
@@ -115,6 +120,7 @@ namespace System.Configuration {
                                if (cache.Count > 0)
                                        provider.SetPropertyValues (Context, cache);
                        }
+                       Context.CurrentSettings = null;
 #endif
                }
 
@@ -153,10 +159,23 @@ namespace System.Configuration {
                [Browsable (false)]
                public override SettingsContext Context {
                        get {
-                               if (context == null)
-                                       context = new SettingsContext ();
+                               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;
+                                       return context;
+                               } finally {
+                                       if (IsSynchronized)
+                                               Monitor.Exit (this);
+                               }
                        }
                }
 
@@ -200,6 +219,12 @@ namespace System.Configuration {
                [MonoTODO]
                public override object this [ string propertyName ] {
                        get {
+                               if (IsSynchronized) {
+                                       lock (this) {
+                                               return GetPropertyValue (propertyName);
+                                       }
+                               }
+
                                return GetPropertyValue (propertyName);
                        }
                        set {
@@ -241,104 +266,155 @@ namespace System.Configuration {
                [Browsable (false)]
                public override SettingsPropertyCollection Properties {
                        get {
-                               if (properties == null) {
-                                       LocalFileSettingsProvider local_provider = null;
-
-                                       properties = new SettingsPropertyCollection ();
-
-                                       foreach (PropertyInfo prop in GetType().GetProperties (/* only public properties? */)) {
-                                               SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
-                                               if (setting_attrs != null && setting_attrs.Length > 0) {
-                                                       SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
-                                                       SettingsProvider provider = null;
-                                                       object defaultValue = null;
-                                                       SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
-
-                                                       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; /* XXX this is a string.. do we convert? */
-                                                                       if (prop.PropertyType != typeof(string)) {
-                                                                               defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue);
-                                                                       }
-                                                               }
-                                                               else if (a is SettingsSerializeAsAttribute) {
-                                                                       serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
-                                                               }
-                                                               else if (a is ApplicationScopedSettingAttribute ||
-                                                                        a is UserScopedSettingAttribute) {
-                                                                       dict.Add (a.GetType(), a);
-                                                               }
-                                                               else {
-                                                                       dict.Add (a.GetType(), a);
-                                                               }
+                               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;
                                                        }
+                                               }
 
-                                                       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 ();
-                                                                       local_provider.Initialize (null, null);
-                                                               }
-                                                               setting.Provider = local_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);
+                                               }
+                                       }
 
-                                                       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;
-                                                       }
+                                       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);
+                               }
+                       }
 
-                                                       properties.Add (setting);
+                       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;
+                       }
 
-                                                       if (setting.Provider != null && Providers [setting.Provider.Name] == null)
-                                                               Providers.Add (setting.Provider);
-                                               }
-                                       }
+                       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;
+                       }
 
-                               return properties;
+                       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 (propertyValues == null) {
-                                       propertyValues = new SettingsPropertyValueCollection ();
-                               }
+                               if (IsSynchronized)
+                                       Monitor.Enter (this);
+
+                               try {
+                                       if (propertyValues == null) {
+                                               propertyValues = new SettingsPropertyValueCollection ();
+                                       }
 
-                               return propertyValues;
+                                       return propertyValues;
+                               } finally {
+                                       if (IsSynchronized)
+                                               Monitor.Exit (this);
+                               }
                        }
                }
 
                [Browsable (false)]
                public override SettingsProviderCollection Providers {
                        get {
-                               if (providers == null)
-                                       providers = new SettingsProviderCollection ();
+                               if (IsSynchronized)
+                                       Monitor.Enter (this);
+
+                               try {
+                                       if (providers == null)
+                                               providers = new SettingsProviderCollection ();
 
-                               return providers;
+                                       return providers;
+                               } finally {
+                                       if (IsSynchronized)
+                                               Monitor.Exit (this);
+                               }
                        }
                }
 
@@ -354,12 +430,13 @@ namespace System.Configuration {
 
                string settingsKey;
                SettingsContext context;
+#if (CONFIGURATION_DEP)                
                SettingsPropertyCollection properties;
+               ISettingsProviderService providerService;
+#endif
                SettingsPropertyValueCollection propertyValues;
                SettingsProviderCollection providers;
-               ISettingsProviderService providerService;
         }
 
 }
-#endif