Merge pull request #642 from Ventero/CleanCopyLocal
[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                                         Stream s = System.IO.File.OpenRead (File);
75                                         XmlReader subreader = new ConfigXmlTextReader (s, File);
76                                         base.DeserializeElement (subreader, serializeCollectionKey);
77                                         s.Close ();
78                                 }
79                                 catch {
80                                         // nada, we just ignore a missing/unreadble file
81                                 }
82                         }
83                 }
84
85                 protected internal override void Reset (ConfigurationElement parentSection)
86                 {
87                         AppSettingsSection psec = parentSection as AppSettingsSection;
88                         if (psec != null)
89                                 Settings.Reset (psec.Settings);
90                 }
91
92                 [MonoTODO]
93                 protected internal override string SerializeSection (
94                         ConfigurationElement parent, string name, ConfigurationSaveMode mode)
95                 {
96                         if (File == "") {
97                                 return base.SerializeSection (parent, name, mode);
98                         }
99                         else {
100                                 throw new NotImplementedException ();
101                         }
102                 }
103
104                 [ConfigurationProperty ("file", DefaultValue = "")]
105                 public string File {
106                         get { return (string)base [_propFile]; }
107                         set { base [_propFile] = value; }
108                 }
109
110                 [ConfigurationProperty ("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
111                 public KeyValueConfigurationCollection Settings {
112                         get { return (KeyValueConfigurationCollection) base [_propSettings]; }
113                 }
114
115                 protected internal override ConfigurationPropertyCollection Properties {
116                         get {
117                                 return _properties;
118                         }
119                 }
120
121                 protected internal override object GetRuntimeObject ()
122                 {
123                         KeyValueInternalCollection col = new KeyValueInternalCollection ();
124                                 
125                         foreach (string key in Settings.AllKeys) {
126                                 KeyValueConfigurationElement ele = Settings[key];
127                                 col.Add (ele.Key, ele.Value);
128                         }
129                                 
130                         if (!ConfigurationManager.ConfigurationSystem.SupportsUserConfig)
131                                 col.SetReadOnly ();
132
133                         return col;
134                 }
135         }
136 }