ApplicationSettingsBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
  21. //
  22. #if NET_2_0
  23. #if CONFIGURATION_DEP && !TARGET_JVM
  24. extern alias PrebuiltSystem;
  25. using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
  26. #endif
  27. #if CONFIGURATION_DEP
  28. using System.IO;
  29. using System.Xml.Serialization;
  30. #endif
  31. using System.ComponentModel;
  32. using System.Reflection;
  33. using System.Collections.Specialized;
  34. namespace System.Configuration {
  35. public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
  36. {
  37. protected ApplicationSettingsBase ()
  38. {
  39. Initialize (Context, Properties, Providers);
  40. }
  41. protected ApplicationSettingsBase (IComponent owner)
  42. : this (owner, String.Empty)
  43. {
  44. }
  45. protected ApplicationSettingsBase (string settingsKey)
  46. {
  47. this.settingsKey = settingsKey;
  48. Initialize (Context, Properties, Providers);
  49. }
  50. protected ApplicationSettingsBase (IComponent owner,
  51. string settingsKey)
  52. {
  53. if (owner == null)
  54. throw new ArgumentNullException ();
  55. providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
  56. this.settingsKey = settingsKey;
  57. Initialize (Context, Properties, Providers);
  58. }
  59. public event PropertyChangedEventHandler PropertyChanged;
  60. public event SettingChangingEventHandler SettingChanging;
  61. public event SettingsLoadedEventHandler SettingsLoaded;
  62. public event SettingsSavingEventHandler SettingsSaving;
  63. public object GetPreviousVersion (string propertyName)
  64. {
  65. throw new NotImplementedException ();
  66. }
  67. public void Reload ()
  68. {
  69. #if (CONFIGURATION_DEP)
  70. foreach (SettingsProvider provider in Providers) {
  71. IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
  72. if (iasp != null)
  73. iasp.Reset (Context);
  74. }
  75. #endif
  76. }
  77. public void Reset()
  78. {
  79. #if (CONFIGURATION_DEP)
  80. foreach (SettingsProvider provider in Providers) {
  81. IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
  82. if (iasp != null)
  83. iasp.Reset (Context);
  84. }
  85. Reload ();
  86. #endif
  87. }
  88. public override void Save()
  89. {
  90. #if (CONFIGURATION_DEP)
  91. /* ew.. this needs to be more efficient */
  92. foreach (SettingsProvider provider in Providers) {
  93. SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
  94. foreach (SettingsPropertyValue val in PropertyValues) {
  95. if (val.Property.Provider == provider)
  96. cache.Add (val);
  97. }
  98. if (cache.Count > 0)
  99. provider.SetPropertyValues (Context, cache);
  100. }
  101. #endif
  102. }
  103. public virtual void Upgrade()
  104. {
  105. }
  106. protected virtual void OnPropertyChanged (object sender,
  107. PropertyChangedEventArgs e)
  108. {
  109. if (PropertyChanged != null)
  110. PropertyChanged (sender, e);
  111. }
  112. protected virtual void OnSettingChanging (object sender,
  113. SettingChangingEventArgs e)
  114. {
  115. if (SettingChanging != null)
  116. SettingChanging (sender, e);
  117. }
  118. protected virtual void OnSettingsLoaded (object sender,
  119. SettingsLoadedEventArgs e)
  120. {
  121. if (SettingsLoaded != null)
  122. SettingsLoaded (sender, e);
  123. }
  124. protected virtual void OnSettingsSaving (object sender,
  125. CancelEventArgs e)
  126. {
  127. if (SettingsSaving != null)
  128. SettingsSaving (sender, e);
  129. }
  130. [Browsable (false)]
  131. public override SettingsContext Context {
  132. get {
  133. if (context == null)
  134. context = new SettingsContext ();
  135. return context;
  136. }
  137. }
  138. void CacheValuesByProvider (SettingsProvider provider)
  139. {
  140. SettingsPropertyCollection col = new SettingsPropertyCollection ();
  141. foreach (SettingsProperty p in Properties) {
  142. if (p.Provider == provider)
  143. col.Add (p);
  144. }
  145. if (col.Count > 0) {
  146. SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
  147. PropertyValues.Add (vals);
  148. }
  149. OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider));
  150. }
  151. void InitializeSettings (SettingsPropertyCollection settings)
  152. {
  153. }
  154. object GetPropertyValue (string propertyName)
  155. {
  156. SettingsProperty prop = Properties [ propertyName ];
  157. if (prop == null)
  158. throw new SettingsPropertyNotFoundException (propertyName);
  159. if (propertyValues == null)
  160. InitializeSettings (Properties);
  161. if (PropertyValues [ propertyName ] == null)
  162. CacheValuesByProvider (prop.Provider);
  163. return PropertyValues [ propertyName ].PropertyValue;
  164. }
  165. [MonoTODO]
  166. public override object this [ string propertyName ] {
  167. get {
  168. return GetPropertyValue (propertyName);
  169. }
  170. set {
  171. SettingsProperty prop = Properties [ propertyName ];
  172. if (prop == null)
  173. throw new SettingsPropertyNotFoundException (propertyName);
  174. if (prop.IsReadOnly)
  175. throw new SettingsPropertyIsReadOnlyException (propertyName);
  176. /* XXX check the type of the property vs the type of @value */
  177. if (value != null &&
  178. !prop.PropertyType.IsAssignableFrom (value.GetType()))
  179. throw new SettingsPropertyWrongTypeException (propertyName);
  180. if (PropertyValues [ propertyName ] == null)
  181. CacheValuesByProvider (prop.Provider);
  182. SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
  183. GetType().FullName,
  184. settingsKey,
  185. value,
  186. false);
  187. OnSettingChanging (this, changing_args);
  188. if (changing_args.Cancel == false) {
  189. /* actually set the value */
  190. PropertyValues [ propertyName ].PropertyValue = value;
  191. /* then emit PropertyChanged */
  192. OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
  193. }
  194. }
  195. }
  196. #if (CONFIGURATION_DEP)
  197. [Browsable (false)]
  198. public override SettingsPropertyCollection Properties {
  199. get {
  200. if (properties == null) {
  201. LocalFileSettingsProvider local_provider = null;
  202. properties = new SettingsPropertyCollection ();
  203. foreach (PropertyInfo prop in GetType ().GetProperties ()) { // only public properties
  204. SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
  205. if (setting_attrs == null || setting_attrs.Length == 0)
  206. continue;
  207. CreateSettingsProperty (prop, properties, ref local_provider);
  208. }
  209. }
  210. return properties;
  211. }
  212. }
  213. void CreateSettingsProperty (PropertyInfo prop, SettingsPropertyCollection properties, ref LocalFileSettingsProvider local_provider)
  214. {
  215. SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
  216. SettingsProvider provider = null;
  217. object defaultValue = null;
  218. SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
  219. foreach (Attribute a in prop.GetCustomAttributes (false)) {
  220. /* the attributes we handle natively here */
  221. if (a is SettingsProviderAttribute) {
  222. Type provider_type = Type.GetType (((SettingsProviderAttribute)a).ProviderTypeName);
  223. provider = (SettingsProvider) Activator.CreateInstance (provider_type);
  224. provider.Initialize (null, null);
  225. }
  226. else if (a is DefaultSettingValueAttribute) {
  227. defaultValue = ((DefaultSettingValueAttribute)a).Value; /* XXX this is a string.. do we convert? */
  228. // note: for StringCollection, TypeDescriptor.GetConverter(prop.PropertyType) returns
  229. // CollectionConverter, however this class cannot handle the XML serialized strings
  230. if (prop.PropertyType == typeof(StringCollection)) {
  231. XmlSerializer xs = new XmlSerializer (typeof (string[]));
  232. string[] values = (string[]) xs.Deserialize (new StringReader ((string)defaultValue));
  233. StringCollection sc = new StringCollection ();
  234. sc.AddRange (values);
  235. defaultValue = sc;
  236. } else if (prop.PropertyType != typeof(string)) {
  237. defaultValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString((string)defaultValue);
  238. }
  239. }
  240. else if (a is SettingsSerializeAsAttribute) {
  241. serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
  242. }
  243. else if (a is ApplicationScopedSettingAttribute ||
  244. a is UserScopedSettingAttribute) {
  245. dict.Add (a.GetType(), a);
  246. }
  247. else {
  248. dict.Add (a.GetType(), a);
  249. }
  250. }
  251. SettingsProperty setting =
  252. new SettingsProperty (prop.Name, prop.PropertyType, provider, false /* XXX */,
  253. defaultValue /* XXX always a string? */, serializeAs, dict,
  254. false, false);
  255. if (providerService != null)
  256. setting.Provider = providerService.GetSettingsProvider (setting);
  257. if (provider == null) {
  258. if (local_provider == null) {
  259. local_provider = new LocalFileSettingsProvider ();
  260. local_provider.Initialize (null, null);
  261. }
  262. setting.Provider = local_provider;
  263. // .NET ends up to set this to providers.
  264. provider = local_provider;
  265. }
  266. if (provider != null) {
  267. /* make sure we're using the same instance of a
  268. given provider across multiple properties */
  269. SettingsProvider p = Providers[provider.Name];
  270. if (p != null)
  271. setting.Provider = p;
  272. }
  273. properties.Add (setting);
  274. if (setting.Provider != null && Providers [setting.Provider.Name] == null)
  275. Providers.Add (setting.Provider);
  276. if (Providers.Count == 0) throw new Exception (setting.Provider.Name + ((object) Providers [setting.Provider.Name] ?? "(null)"));
  277. }
  278. #endif
  279. [Browsable (false)]
  280. public override SettingsPropertyValueCollection PropertyValues {
  281. get {
  282. if (propertyValues == null) {
  283. propertyValues = new SettingsPropertyValueCollection ();
  284. }
  285. return propertyValues;
  286. }
  287. }
  288. [Browsable (false)]
  289. public override SettingsProviderCollection Providers {
  290. get {
  291. if (providers == null)
  292. providers = new SettingsProviderCollection ();
  293. return providers;
  294. }
  295. }
  296. [Browsable (false)]
  297. public string SettingsKey {
  298. get {
  299. return settingsKey;
  300. }
  301. set {
  302. settingsKey = value;
  303. }
  304. }
  305. string settingsKey;
  306. SettingsContext context;
  307. SettingsPropertyCollection properties;
  308. SettingsPropertyValueCollection propertyValues;
  309. SettingsProviderCollection providers;
  310. ISettingsProviderService providerService;
  311. }
  312. }
  313. #endif