ApplicationSettingsBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 CONFIGURATION_DEP
  23. using System.IO;
  24. using System.Xml.Serialization;
  25. #endif
  26. using System.ComponentModel;
  27. using System.Reflection;
  28. using System.Threading;
  29. using System.Collections.Specialized;
  30. namespace System.Configuration {
  31. public abstract class ApplicationSettingsBase : SettingsBase, INotifyPropertyChanged
  32. {
  33. protected ApplicationSettingsBase ()
  34. {
  35. Initialize (Context, Properties, Providers);
  36. }
  37. protected ApplicationSettingsBase (IComponent owner)
  38. : this (owner, String.Empty)
  39. {
  40. }
  41. protected ApplicationSettingsBase (string settingsKey)
  42. {
  43. this.settingsKey = settingsKey;
  44. Initialize (Context, Properties, Providers);
  45. }
  46. protected ApplicationSettingsBase (IComponent owner,
  47. string settingsKey)
  48. {
  49. if (owner == null)
  50. throw new ArgumentNullException ();
  51. #if (CONFIGURATION_DEP)
  52. providerService = (ISettingsProviderService)owner.Site.GetService(typeof (ISettingsProviderService));
  53. #endif
  54. this.settingsKey = settingsKey;
  55. Initialize (Context, Properties, Providers);
  56. }
  57. public event PropertyChangedEventHandler PropertyChanged;
  58. public event SettingChangingEventHandler SettingChanging;
  59. public event SettingsLoadedEventHandler SettingsLoaded;
  60. public event SettingsSavingEventHandler SettingsSaving;
  61. public object GetPreviousVersion (string propertyName)
  62. {
  63. throw new NotImplementedException ();
  64. }
  65. public void Reload ()
  66. {
  67. #if (CONFIGURATION_DEP)
  68. foreach (SettingsProvider provider in Providers) {
  69. // IApplicationSettingsProvider iasp = provider as IApplicationSettingsProvider;
  70. CacheValuesByProvider(provider);
  71. }
  72. #endif
  73. }
  74. public void Reset()
  75. {
  76. #if (CONFIGURATION_DEP)
  77. Reload ();
  78. foreach (SettingsPropertyValue pv in PropertyValues)
  79. pv.PropertyValue = pv.Reset();
  80. #endif
  81. }
  82. public override void Save()
  83. {
  84. #if (CONFIGURATION_DEP)
  85. Context.CurrentSettings = this;
  86. /* ew.. this needs to be more efficient */
  87. foreach (SettingsProvider provider in Providers) {
  88. SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
  89. foreach (SettingsPropertyValue val in PropertyValues) {
  90. if (val.Property.Provider == provider)
  91. cache.Add (val);
  92. }
  93. if (cache.Count > 0)
  94. provider.SetPropertyValues (Context, cache);
  95. }
  96. Context.CurrentSettings = null;
  97. #endif
  98. }
  99. public virtual void Upgrade()
  100. {
  101. }
  102. protected virtual void OnPropertyChanged (object sender,
  103. PropertyChangedEventArgs e)
  104. {
  105. if (PropertyChanged != null)
  106. PropertyChanged (sender, e);
  107. }
  108. protected virtual void OnSettingChanging (object sender,
  109. SettingChangingEventArgs e)
  110. {
  111. if (SettingChanging != null)
  112. SettingChanging (sender, e);
  113. }
  114. protected virtual void OnSettingsLoaded (object sender,
  115. SettingsLoadedEventArgs e)
  116. {
  117. if (SettingsLoaded != null)
  118. SettingsLoaded (sender, e);
  119. }
  120. protected virtual void OnSettingsSaving (object sender,
  121. CancelEventArgs e)
  122. {
  123. if (SettingsSaving != null)
  124. SettingsSaving (sender, e);
  125. }
  126. [Browsable (false)]
  127. public override SettingsContext Context {
  128. get {
  129. if (IsSynchronized)
  130. Monitor.Enter (this);
  131. try {
  132. if (context == null) {
  133. context = new SettingsContext ();
  134. context ["SettingsKey"] = "";
  135. Type type = GetType ();
  136. context ["GroupName"] = type.FullName;
  137. context ["SettingsClassType"] = type;
  138. }
  139. return context;
  140. } finally {
  141. if (IsSynchronized)
  142. Monitor.Exit (this);
  143. }
  144. }
  145. }
  146. void CacheValuesByProvider (SettingsProvider provider)
  147. {
  148. SettingsPropertyCollection col = new SettingsPropertyCollection ();
  149. foreach (SettingsProperty p in Properties) {
  150. if (p.Provider == provider)
  151. col.Add (p);
  152. }
  153. if (col.Count > 0) {
  154. SettingsPropertyValueCollection vals = provider.GetPropertyValues (Context, col);
  155. foreach (SettingsPropertyValue prop in vals) {
  156. if (PropertyValues [prop.Name] != null)
  157. PropertyValues [prop.Name].PropertyValue = prop.PropertyValue;
  158. else
  159. PropertyValues.Add (prop);
  160. }
  161. }
  162. OnSettingsLoaded (this, new SettingsLoadedEventArgs (provider));
  163. }
  164. void InitializeSettings (SettingsPropertyCollection settings)
  165. {
  166. }
  167. object GetPropertyValue (string propertyName)
  168. {
  169. SettingsProperty prop = Properties [ propertyName ];
  170. if (prop == null)
  171. throw new SettingsPropertyNotFoundException (propertyName);
  172. if (propertyValues == null)
  173. InitializeSettings (Properties);
  174. if (PropertyValues [ propertyName ] == null)
  175. CacheValuesByProvider (prop.Provider);
  176. return PropertyValues [ propertyName ].PropertyValue;
  177. }
  178. [MonoTODO]
  179. public override object this [ string propertyName ] {
  180. get {
  181. if (IsSynchronized) {
  182. lock (this) {
  183. return GetPropertyValue (propertyName);
  184. }
  185. }
  186. return GetPropertyValue (propertyName);
  187. }
  188. set {
  189. SettingsProperty prop = Properties [ propertyName ];
  190. if (prop == null)
  191. throw new SettingsPropertyNotFoundException (propertyName);
  192. if (prop.IsReadOnly)
  193. throw new SettingsPropertyIsReadOnlyException (propertyName);
  194. /* XXX check the type of the property vs the type of @value */
  195. if (value != null &&
  196. !prop.PropertyType.IsAssignableFrom (value.GetType()))
  197. throw new SettingsPropertyWrongTypeException (propertyName);
  198. if (PropertyValues [ propertyName ] == null)
  199. CacheValuesByProvider (prop.Provider);
  200. SettingChangingEventArgs changing_args = new SettingChangingEventArgs (propertyName,
  201. GetType().FullName,
  202. settingsKey,
  203. value,
  204. false);
  205. OnSettingChanging (this, changing_args);
  206. if (changing_args.Cancel == false) {
  207. /* actually set the value */
  208. PropertyValues [ propertyName ].PropertyValue = value;
  209. /* then emit PropertyChanged */
  210. OnPropertyChanged (this, new PropertyChangedEventArgs (propertyName));
  211. }
  212. }
  213. }
  214. #if (CONFIGURATION_DEP)
  215. [Browsable (false)]
  216. public override SettingsPropertyCollection Properties {
  217. get {
  218. if (IsSynchronized)
  219. Monitor.Enter (this);
  220. try {
  221. if (properties == null) {
  222. SettingsProvider local_provider = null;
  223. properties = new SettingsPropertyCollection ();
  224. Type this_type = GetType();
  225. SettingsProviderAttribute[] provider_attrs = (SettingsProviderAttribute[])this_type.GetCustomAttributes (typeof (SettingsProviderAttribute), false);;
  226. if (provider_attrs != null && provider_attrs.Length != 0) {
  227. Type provider_type = Type.GetType (provider_attrs[0].ProviderTypeName);
  228. SettingsProvider provider = (SettingsProvider) Activator.CreateInstance (provider_type);
  229. provider.Initialize (null, null);
  230. if (provider != null && Providers [provider.Name] == null) {
  231. Providers.Add (provider);
  232. local_provider = provider;
  233. }
  234. }
  235. PropertyInfo[] type_props = this_type.GetProperties ();
  236. foreach (PropertyInfo prop in type_props) { // only public properties
  237. SettingAttribute[] setting_attrs = (SettingAttribute[])prop.GetCustomAttributes (typeof (SettingAttribute), false);
  238. if (setting_attrs == null || setting_attrs.Length == 0)
  239. continue;
  240. CreateSettingsProperty (prop, properties, ref local_provider);
  241. }
  242. }
  243. return properties;
  244. } finally {
  245. if (IsSynchronized)
  246. Monitor.Exit (this);
  247. }
  248. }
  249. }
  250. void CreateSettingsProperty (PropertyInfo prop, SettingsPropertyCollection properties, ref SettingsProvider local_provider)
  251. {
  252. SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
  253. SettingsProvider provider = null;
  254. object defaultValue = null;
  255. SettingsSerializeAs serializeAs = SettingsSerializeAs.String;
  256. bool explicitSerializeAs = false;
  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);
  263. }
  264. else if (a is DefaultSettingValueAttribute) {
  265. defaultValue = ((DefaultSettingValueAttribute)a).Value;
  266. }
  267. else if (a is SettingsSerializeAsAttribute) {
  268. serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs;
  269. explicitSerializeAs = true;
  270. }
  271. else if (a is ApplicationScopedSettingAttribute ||
  272. a is UserScopedSettingAttribute) {
  273. dict.Add (a.GetType(), a);
  274. }
  275. else {
  276. dict.Add (a.GetType(), a);
  277. }
  278. }
  279. if (!explicitSerializeAs) {
  280. // DefaultValue is a string and if we can't convert from string to the
  281. // property type then the only other option left is for the string to
  282. // be XML.
  283. //
  284. TypeConverter converter = TypeDescriptor.GetConverter (prop.PropertyType);
  285. if (converter != null &&
  286. (!converter.CanConvertFrom (typeof (string)) ||
  287. !converter.CanConvertTo (typeof (string))))
  288. serializeAs = SettingsSerializeAs.Xml;
  289. }
  290. SettingsProperty setting =
  291. new SettingsProperty (prop.Name, prop.PropertyType, provider, false /* XXX */,
  292. defaultValue /* XXX always a string? */, serializeAs, dict,
  293. false, false);
  294. if (providerService != null)
  295. setting.Provider = providerService.GetSettingsProvider (setting);
  296. if (provider == null) {
  297. if (local_provider == null) {
  298. local_provider = new LocalFileSettingsProvider () as SettingsProvider;
  299. local_provider.Initialize (null, null);
  300. }
  301. setting.Provider = local_provider;
  302. // .NET ends up to set this to providers.
  303. provider = local_provider;
  304. }
  305. if (provider != null) {
  306. /* make sure we're using the same instance of a
  307. given provider across multiple properties */
  308. SettingsProvider p = Providers[provider.Name];
  309. if (p != null)
  310. setting.Provider = p;
  311. }
  312. properties.Add (setting);
  313. if (setting.Provider != null && Providers [setting.Provider.Name] == null)
  314. Providers.Add (setting.Provider);
  315. }
  316. #endif
  317. [Browsable (false)]
  318. public override SettingsPropertyValueCollection PropertyValues {
  319. get {
  320. if (IsSynchronized)
  321. Monitor.Enter (this);
  322. try {
  323. if (propertyValues == null) {
  324. propertyValues = new SettingsPropertyValueCollection ();
  325. }
  326. return propertyValues;
  327. } finally {
  328. if (IsSynchronized)
  329. Monitor.Exit (this);
  330. }
  331. }
  332. }
  333. [Browsable (false)]
  334. public override SettingsProviderCollection Providers {
  335. get {
  336. if (IsSynchronized)
  337. Monitor.Enter (this);
  338. try {
  339. if (providers == null)
  340. providers = new SettingsProviderCollection ();
  341. return providers;
  342. } finally {
  343. if (IsSynchronized)
  344. Monitor.Exit (this);
  345. }
  346. }
  347. }
  348. [Browsable (false)]
  349. public string SettingsKey {
  350. get {
  351. return settingsKey;
  352. }
  353. set {
  354. settingsKey = value;
  355. }
  356. }
  357. string settingsKey;
  358. SettingsContext context;
  359. #if (CONFIGURATION_DEP)
  360. SettingsPropertyCollection properties;
  361. ISettingsProviderService providerService;
  362. #endif
  363. SettingsPropertyValueCollection propertyValues;
  364. SettingsProviderCollection providers;
  365. }
  366. }