|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Xml; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 using System.Drawing; |
|
22 using System.ComponentModel; |
|
23 using System.Collections.Generic; |
|
24 using System.Diagnostics; |
|
25 using System.Collections; |
|
26 using System.Windows.Forms; |
|
27 using Microsoft.Win32; |
|
28 |
|
29 namespace SymbianUtils.Settings |
|
30 { |
|
31 public class XmlSettingsValue |
|
32 { |
|
33 #region Constructors |
|
34 internal XmlSettingsValue( object aValue ) |
|
35 { |
|
36 Value = aValue; |
|
37 } |
|
38 |
|
39 internal XmlSettingsValue( XmlReader aReader ) |
|
40 { |
|
41 try |
|
42 { |
|
43 Read( aReader ); |
|
44 } |
|
45 catch ( Exception ) |
|
46 { |
|
47 } |
|
48 } |
|
49 #endregion |
|
50 |
|
51 #region Store & Restore API |
|
52 internal void Write( XmlWriter aWriter ) |
|
53 { |
|
54 string typeString = Value.GetType().ToString(); |
|
55 aWriter.WriteAttributeString( "type", typeString ); |
|
56 // |
|
57 string valueString = Value.ToString(); |
|
58 aWriter.WriteAttributeString( "value", valueString ); |
|
59 } |
|
60 |
|
61 internal void Read( XmlReader aReader ) |
|
62 { |
|
63 string typeString = XmlUtils.ReadAttributeValue( aReader, "type" ); |
|
64 string valueString = XmlUtils.ReadAttributeValue( aReader, "value" ); |
|
65 // |
|
66 Type type = Type.GetType( typeString ); |
|
67 TypeConverter converter = TypeDescriptor.GetConverter( type ); |
|
68 // |
|
69 bool canConvertFromString = converter.CanConvertFrom( valueString.GetType() ); |
|
70 if ( canConvertFromString ) |
|
71 { |
|
72 object value = converter.ConvertFromString( valueString ); |
|
73 Value = value; |
|
74 } |
|
75 else |
|
76 { |
|
77 Value = valueString; |
|
78 } |
|
79 } |
|
80 #endregion |
|
81 |
|
82 #region Operators |
|
83 public static implicit operator XmlSettingsValue( int aValue ) |
|
84 { |
|
85 XmlSettingsValue ret = new XmlSettingsValue( aValue ); |
|
86 return ret; |
|
87 } |
|
88 |
|
89 public static implicit operator XmlSettingsValue( string aValue ) |
|
90 { |
|
91 XmlSettingsValue ret = new XmlSettingsValue( aValue ); |
|
92 return ret; |
|
93 } |
|
94 |
|
95 public static implicit operator XmlSettingsValue( bool aValue ) |
|
96 { |
|
97 XmlSettingsValue ret = new XmlSettingsValue( aValue ); |
|
98 return ret; |
|
99 } |
|
100 |
|
101 public static implicit operator XmlSettingsValue( uint aValue ) |
|
102 { |
|
103 XmlSettingsValue ret = new XmlSettingsValue( aValue ); |
|
104 return ret; |
|
105 } |
|
106 |
|
107 public static implicit operator XmlSettingsValue( long aValue ) |
|
108 { |
|
109 XmlSettingsValue ret = new XmlSettingsValue( aValue ); |
|
110 return ret; |
|
111 } |
|
112 |
|
113 public static implicit operator int( XmlSettingsValue aValue ) |
|
114 { |
|
115 int ret = aValue.ToInt(); |
|
116 return ret; |
|
117 } |
|
118 |
|
119 public static implicit operator string( XmlSettingsValue aValue ) |
|
120 { |
|
121 string ret = aValue.ToString(); |
|
122 return ret; |
|
123 } |
|
124 |
|
125 public static implicit operator bool( XmlSettingsValue aValue ) |
|
126 { |
|
127 bool ret = aValue.ToBool(); |
|
128 return ret; |
|
129 } |
|
130 |
|
131 public static implicit operator uint( XmlSettingsValue aValue ) |
|
132 { |
|
133 uint ret = aValue.ToUint(); |
|
134 return ret; |
|
135 } |
|
136 |
|
137 public static implicit operator long( XmlSettingsValue aValue ) |
|
138 { |
|
139 long ret = aValue.ToLong(); |
|
140 return ret; |
|
141 } |
|
142 |
|
143 public static implicit operator Size( XmlSettingsValue aValue ) |
|
144 { |
|
145 Size ret = aValue.ToSize(); |
|
146 return ret; |
|
147 } |
|
148 |
|
149 public static implicit operator Point( XmlSettingsValue aValue ) |
|
150 { |
|
151 Point ret = aValue.ToPoint(); |
|
152 return ret; |
|
153 } |
|
154 #endregion |
|
155 |
|
156 #region Conversion methods |
|
157 public int ToInt() |
|
158 { |
|
159 return ToInt( 0 ); |
|
160 } |
|
161 |
|
162 public int ToInt( int aDefault ) |
|
163 { |
|
164 int ret = XmlSettingsValueConverter<int>.Convert( this, aDefault ); |
|
165 return ret; |
|
166 } |
|
167 |
|
168 public uint ToUint() |
|
169 { |
|
170 return ToUint( 0 ); |
|
171 } |
|
172 |
|
173 public uint ToUint( uint aDefault ) |
|
174 { |
|
175 uint ret = XmlSettingsValueConverter<uint>.Convert( this, aDefault ); |
|
176 return ret; |
|
177 } |
|
178 |
|
179 public long ToLong() |
|
180 { |
|
181 return ToLong( 0 ); |
|
182 } |
|
183 |
|
184 public long ToLong( long aDefault ) |
|
185 { |
|
186 long ret = XmlSettingsValueConverter<long>.Convert( this, aDefault ); |
|
187 return ret; |
|
188 } |
|
189 |
|
190 public bool ToBool() |
|
191 { |
|
192 return ToBool( false ); |
|
193 } |
|
194 |
|
195 public bool ToBool( bool aDefault ) |
|
196 { |
|
197 bool defaultApplied = false; |
|
198 bool ret = XmlSettingsValueConverter<bool>.Convert( this, aDefault, out defaultApplied ); |
|
199 // |
|
200 if ( defaultApplied ) |
|
201 { |
|
202 string valueAsString = ToString().ToLower().Trim(); |
|
203 // |
|
204 switch ( valueAsString ) |
|
205 { |
|
206 case "true": |
|
207 case "yes": |
|
208 case "1": |
|
209 ret = true; |
|
210 break; |
|
211 case "false": |
|
212 case "no": |
|
213 case "0": |
|
214 ret = false; |
|
215 break; |
|
216 default: |
|
217 ret = aDefault; |
|
218 break; |
|
219 } |
|
220 } |
|
221 // |
|
222 return ret; |
|
223 } |
|
224 |
|
225 public string ToString( string aDefault ) |
|
226 { |
|
227 string ret = XmlSettingsValueConverter<string>.Convert( this, aDefault ); |
|
228 return ret; |
|
229 } |
|
230 |
|
231 public Point ToPoint() |
|
232 { |
|
233 return ToPoint( new Point( 0, 0 ) ); |
|
234 } |
|
235 |
|
236 public Point ToPoint( Point aDefault ) |
|
237 { |
|
238 Point ret = XmlSettingsValueConverter<Point>.Convert( this, aDefault ); |
|
239 return ret; |
|
240 } |
|
241 |
|
242 public Size ToSize() |
|
243 { |
|
244 return ToSize( new Size( 0, 0 ) ); |
|
245 } |
|
246 |
|
247 public Size ToSize( Size aDefault ) |
|
248 { |
|
249 Size ret = XmlSettingsValueConverter<Size>.Convert( this, aDefault ); |
|
250 return ret; |
|
251 } |
|
252 #endregion |
|
253 |
|
254 #region Properties |
|
255 internal object Value |
|
256 { |
|
257 get { return iValue; } |
|
258 set |
|
259 { |
|
260 if ( value == null ) |
|
261 { |
|
262 throw new Exception( "Value cannot be NULL" ); |
|
263 } |
|
264 else if ( value.GetType() == typeof( XmlSettingsValue ) ) |
|
265 { |
|
266 SymbianUtils.SymDebug.SymDebugger.Break(); |
|
267 } |
|
268 |
|
269 iValue = value; |
|
270 } |
|
271 } |
|
272 #endregion |
|
273 |
|
274 #region From System.Object |
|
275 public override string ToString() |
|
276 { |
|
277 return Value.ToString(); |
|
278 } |
|
279 #endregion |
|
280 |
|
281 #region Internal methods |
|
282 #endregion |
|
283 |
|
284 #region Data members |
|
285 private object iValue = string.Empty; |
|
286 #endregion |
|
287 } |
|
288 } |
|
289 |
|
290 |