System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Configuration / SettingsPropertyValue.cs
1 //
2 // System.Web.UI.WebControls.SettingsPropertyValue.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30 using System;
31 using System.Globalization;
32 using System.IO;
33 using System.ComponentModel;
34 using System.Runtime.Serialization.Formatters.Binary;
35 #if (XML_DEP)
36 using System.Xml;
37 using System.Xml.Serialization;
38 #endif
39
40 namespace System.Configuration
41 {
42
43         public class SettingsPropertyValue
44         {
45                 public SettingsPropertyValue (SettingsProperty property)
46                 {
47                         this.property = property;
48                         needPropertyValue = true;
49                 }
50
51                 public bool Deserialized {
52                         get {
53                                 return deserialized;
54                         }
55                         set {
56                                 deserialized = value;
57                         }
58                 }
59
60                 public bool IsDirty {
61                         get {
62                                 return dirty;
63                         }
64                         set {
65                                 dirty = value;
66                         }
67                 }
68
69                 public string Name {
70                         get {
71                                 return property.Name;
72                         }
73                 }
74
75                 public SettingsProperty Property {
76                         get {
77                                 return property;
78                         }
79                 }
80
81                 public object PropertyValue {
82                         get {
83                                 if (needPropertyValue) {
84                                         propertyValue = GetDeserializedValue (serializedValue);
85                                         if (propertyValue == null) {
86                                                 propertyValue = GetDeserializedDefaultValue ();
87                                                 defaulted = true;
88                                         }
89                                         needPropertyValue = false;
90                                 }
91
92                                 if (propertyValue != null &&
93                                         !(propertyValue is string) &&
94                                         !(propertyValue is DateTime) &&
95                                         !property.PropertyType.IsPrimitive)
96                                         dirty = true;
97
98                                 return propertyValue;
99                         }
100                         set {
101                                 propertyValue = value;
102                                 dirty = true;
103                                 needPropertyValue = false;
104                                 needSerializedValue = true;
105                                 defaulted = false;
106                         }
107                 }
108
109                 public object SerializedValue {
110                         get {
111                                 if (needSerializedValue) {
112                                         needSerializedValue = false;
113
114                                         switch (property.SerializeAs)
115                                         {
116                                         case SettingsSerializeAs.String:
117                                                 serializedValue = TypeDescriptor.GetConverter (property.PropertyType).ConvertToInvariantString (propertyValue);
118                                                 break;
119 #if (XML_DEP)
120                                         case SettingsSerializeAs.Xml:
121                                                 if (propertyValue != null) {
122                                                         XmlSerializer serializer = new XmlSerializer (propertyValue.GetType ());
123                                                         StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
124         
125                                                         serializer.Serialize (w, propertyValue);
126                                                         serializedValue = w.ToString();
127                                                 }
128                                                 else
129                                                         serializedValue = null;
130                                                 break;
131 #endif
132                                         case SettingsSerializeAs.Binary:
133                                                 if (propertyValue != null) {
134                                                         BinaryFormatter bf = new BinaryFormatter ();
135                                                         MemoryStream ms = new MemoryStream ();
136                                                         bf.Serialize (ms, propertyValue);
137                                                         serializedValue = ms.ToArray();
138                                                 }
139                                                 else
140                                                         serializedValue = null;
141                                                 break;
142                                         default:
143                                                 serializedValue = null;
144                                                 break;
145                                         }
146
147                                 }
148
149                                 return serializedValue;
150                         }
151                         set {
152                                 serializedValue = value;
153                                 needPropertyValue = true;
154                         }
155                 }
156
157                 public bool UsingDefaultValue {
158                         get {
159                                 return defaulted;
160                         }
161                 }
162
163                 internal object Reset ()
164                 {
165                         propertyValue = GetDeserializedDefaultValue ();
166                         dirty = true;
167                         defaulted = true;
168                         needPropertyValue = true;
169                         return propertyValue;
170                 }
171
172                 private object GetDeserializedDefaultValue ()
173                 {
174                         if (property.DefaultValue == null)
175                                 if (property.PropertyType.IsValueType)
176                                         return Activator.CreateInstance (property.PropertyType);
177                                 else
178                                         return null;
179
180                         if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
181                                 if (property.PropertyType != typeof (string))
182                                         return Activator.CreateInstance (property.PropertyType);
183                                 else
184                                         return string.Empty;
185
186                         if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
187                                 return GetDeserializedValue (property.DefaultValue);
188
189                         if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
190                                 TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
191                                 return converter.ConvertFrom (null, CultureInfo.InvariantCulture, property.DefaultValue);
192                         }
193                         return property.DefaultValue;
194                 }
195
196                 private object GetDeserializedValue (object serializedValue)
197                 {
198                         if (serializedValue == null)
199                                 return null;
200
201                         object deserializedObject = null;
202
203                         try {
204                                 switch (property.SerializeAs) {
205                                         case SettingsSerializeAs.String:
206                                                 if (serializedValue is string)
207                                                         deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromInvariantString ((string) serializedValue);
208                                                 break;
209 #if (XML_DEP)
210                                         case SettingsSerializeAs.Xml:
211                                                 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
212                                                 StringReader str = new StringReader ((string) serializedValue);
213                                                 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
214                                                 break;
215 #endif
216                                         case SettingsSerializeAs.Binary:
217                                                 BinaryFormatter bf = new BinaryFormatter ();
218                                                 MemoryStream ms;
219                                                 if (serializedValue is string)
220                                                         ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
221                                                 else
222                                                         ms = new MemoryStream ((byte []) serializedValue);
223                                                 deserializedObject = bf.Deserialize (ms);
224                                                 break;
225                                 }
226                         }
227                         catch (Exception e) {
228                                 if (property.ThrowOnErrorDeserializing)
229                                         throw e;
230                         }
231
232                         return deserializedObject;
233                 }
234
235                 readonly SettingsProperty property;
236                 object propertyValue;
237                 object serializedValue;
238                 bool needSerializedValue;
239                 bool needPropertyValue;
240                 bool dirty;
241                 bool defaulted = false;
242                 bool deserialized;
243         }
244
245 }
246
247 #endif