ApplicationSettingsBase.cs 9.8 KB

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