Merge pull request #853 from echampet/onclick
[mono.git] / mcs / class / System.Configuration / System.Configuration / AppSettingsSection.cs
1 //
2 // System.Configuration.AppSettingsSection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Chris Toshok (toshok@ximian.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 using System;
31 using System.ComponentModel;
32 using System.Collections.Specialized;
33 using System.Xml;
34 using System.IO;
35
36 namespace System.Configuration {
37
38         public sealed class AppSettingsSection : ConfigurationSection
39         {
40                 private static ConfigurationPropertyCollection _properties;
41                 private static readonly ConfigurationProperty _propFile;
42                 private static readonly ConfigurationProperty _propSettings;
43
44                 static AppSettingsSection ()
45                 {
46                         _propFile = new ConfigurationProperty ("file", typeof(string), "",
47                                                                new StringConverter(), null, ConfigurationPropertyOptions.None);
48                         _propSettings = new ConfigurationProperty ("", typeof(KeyValueConfigurationCollection), null, 
49                                                                    null, null, ConfigurationPropertyOptions.IsDefaultCollection);
50
51                         _properties     = new ConfigurationPropertyCollection ();
52
53                         _properties.Add (_propFile);
54                         _properties.Add (_propSettings);
55                 }
56
57                 public AppSettingsSection ()
58                 {
59                 }
60
61                 protected internal override  bool IsModified ()
62                 {
63                         return Settings.IsModified ();
64                 }
65
66                 [MonoInternalNote ("file path?  do we use a System.Configuration api for opening it?  do we keep it open?  do we open it writable?")]
67                 protected internal override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
68                 {
69                         /* need to do this so we pick up the File attribute */
70                         base.DeserializeElement (reader, serializeCollectionKey);
71
72                         if (File != "") {
73                                 try {
74                                         string filePath = File;
75                                         if (!Path.IsPathRooted (filePath))
76                                                 filePath = Path.Combine (Path.GetDirectoryName (Configuration.FilePath), filePath);
77
78                                         Stream s = System.IO.File.OpenRead (filePath);
79                                         XmlReader subreader = new ConfigXmlTextReader (s, filePath);
80                                         base.DeserializeElement (subreader, serializeCollectionKey);
81                                         s.Close ();
82                                 }
83                                 catch {
84                                         // nada, we just ignore a missing/unreadble file
85                                 }
86                         }
87                 }
88
89                 protected internal override void Reset (ConfigurationElement parentSection)
90                 {
91                         AppSettingsSection psec = parentSection as AppSettingsSection;
92                         if (psec != null)
93                                 Settings.Reset (psec.Settings);
94                 }
95
96                 [MonoTODO]
97                 protected internal override string SerializeSection (
98                         ConfigurationElement parent, string name, ConfigurationSaveMode mode)
99                 {
100                         if (File == "") {
101                                 return base.SerializeSection (parent, name, mode);
102                         }
103                         else {
104                                 throw new NotImplementedException ();
105                         }
106                 }
107
108                 [ConfigurationProperty ("file", DefaultValue = "")]
109                 public string File {
110                         get { return (string)base [_propFile]; }
111                         set { base [_propFile] = value; }
112                 }
113
114                 [ConfigurationProperty ("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
115                 public KeyValueConfigurationCollection Settings {
116                         get { return (KeyValueConfigurationCollection) base [_propSettings]; }
117                 }
118
119                 protected internal override ConfigurationPropertyCollection Properties {
120                         get {
121                                 return _properties;
122                         }
123                 }
124
125                 protected internal override object GetRuntimeObject ()
126                 {
127                         KeyValueInternalCollection col = new KeyValueInternalCollection ();
128                                 
129                         foreach (string key in Settings.AllKeys) {
130                                 KeyValueConfigurationElement ele = Settings[key];
131                                 col.Add (ele.Key, ele.Value);
132                         }
133                                 
134                         if (!ConfigurationManager.ConfigurationSystem.SupportsUserConfig)
135                                 col.SetReadOnly ();
136
137                         return col;
138                 }
139         }
140 }