Mercurial > repos > IBBoard
annotate Preferences.cs @ 120:780169621672
* Remove rogue print statement
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Nov 2012 20:51:49 +0000 |
parents | 0f88d32b22cc |
children |
rev | line source |
---|---|
16 | 1 // This file (Preferences.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
37 | 5 using System; |
6 using System.Collections; | |
7 using System.IO; | |
8 using System.Xml; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using System.Reflection; |
37 | 10 using IBBoard.IO; |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
37 | 12 //TODO: Add import/export |
13 namespace IBBoard | |
14 { | |
15 /// <summary> | |
16 /// Summary description for Preferences. | |
17 /// </summary> | |
18 public class Preferences | |
19 { | |
20 private static Type stringType = typeof(string); | |
21 private static Type hashtableType = typeof(Hashtable); | |
22 private Hashtable htGlobal; | |
23 private Hashtable htLocal; | |
24 | |
25 private string app; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 private bool modified = false; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 public Preferences(string appName) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 app = appName; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 LoadPreferences(); |
37 | 33 } |
34 | |
35 private void LoadPreferences() | |
36 { | |
37 htGlobal = new Hashtable(); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 htLocal = new Hashtable(); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
40 string globalPath = Path.Combine(Constants.ExecutablePath, app + "Pref.xml"); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 if (File.Exists(globalPath)) |
37 | 43 { |
44 XmlDocument xmld = new XmlDocument(); | |
45 xmld.Load(globalPath); | |
46 XmlNodeList nl = xmld.LastChild.ChildNodes; | |
47 XmlNodeList nlHash; | |
48 MethodInfo m; | |
49 Type t; | |
50 Hashtable htTemp; | |
51 | |
52 if (nl == null || nl.Count==0) | |
53 { | |
54 throw new InvalidFileException("Preference file "+globalPath+" did not contain any preferences"); | |
55 } | |
56 | |
57 for (int i = 0; i<nl.Count; i++) | |
58 { | |
59 t = Type.GetType(nl[i].Attributes["type"].Value, true); | |
60 | |
61 if (t!=stringType) | |
62 { | |
63 if (t==hashtableType) | |
64 { | |
65 htTemp = new Hashtable(); | |
66 nlHash = nl[i].ChildNodes; | |
67 | |
68 for (int j = 0; j<nlHash.Count; j++) | |
69 { | |
70 if (nlHash[j].NodeType.GetType()==typeof(XmlElement)) | |
71 { | |
72 t = Type.GetType(nlHash[j].Attributes["type"].Value, true); | |
73 m = t.GetMethod("Parse", new Type[]{stringType}); | |
74 htTemp[nlHash[j].Attributes["key"].Value] = m.Invoke(null, new object[]{nlHash[j].InnerText}); | |
75 } | |
76 } | |
77 | |
78 htGlobal[nl[i].Attributes["id"].Value] = htTemp; | |
79 } | |
80 else if (t.IsEnum) | |
81 { | |
82 htGlobal[nl[i].Attributes["id"].Value] = Enum.Parse(t, nl[i].InnerText, true); | |
83 } | |
84 else | |
85 { | |
86 m = t.GetMethod("Parse", new Type[]{stringType}); | |
87 htGlobal[nl[i].Attributes["id"].Value] = m.Invoke(null, new object[]{nl[i].InnerText}); | |
88 } | |
89 } | |
90 else | |
91 { | |
92 htGlobal[nl[i].Attributes["id"].Value] = nl[i].InnerText; | |
93 } | |
94 } | |
95 | |
96 LoadLocalPreferences(); | |
97 } | |
98 else | |
99 { | |
100 throw new FileNotFoundException("Could not find default preferences at "+globalPath); | |
101 } | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 } |
37 | 103 |
104 private void LoadLocalPreferences() | |
105 { | |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
106 string path = Path.Combine(Constants.UserDataPath, "preferences.xml"); |
37 | 107 |
108 if (File.Exists(path)) | |
109 { | |
110 XmlDocument xmld = new XmlDocument(); | |
111 xmld.Load(path); | |
112 XmlNodeList nl = xmld.LastChild.ChildNodes; | |
113 XmlNodeList nlHash; | |
114 MethodInfo m; | |
115 Type t; | |
116 Hashtable htTemp = new Hashtable(); | |
117 | |
118 nl = xmld.LastChild.ChildNodes; | |
119 | |
120 for (int i = 0; i<nl.Count; i++) | |
121 { | |
122 if (!htGlobal.ContainsKey(nl[i].Attributes["id"].Value)) | |
123 { | |
124 throw new InvalidFileException("User preferences file contains a value for key \""+nl[i].Attributes["id"].Value+"\" which is not contained in the main preferences"); | |
125 } | |
126 | |
127 t = Type.GetType(nl[i].Attributes["type"].Value, true); | |
128 if (t!=stringType) | |
129 { | |
130 if (t==hashtableType) | |
131 { | |
132 htTemp = new Hashtable(); | |
133 nlHash = nl[i].ChildNodes; | |
134 Hashtable htTempInner = new Hashtable(); | |
135 | |
136 for (int j = 0; j<nlHash.Count; j++) | |
137 { | |
138 if (nlHash[j].NodeType==XmlNodeType.Element) | |
139 { | |
140 t = Type.GetType(nlHash[j].Attributes["type"].Value, true); | |
141 m = t.GetMethod("Parse", new Type[]{stringType}); | |
142 htTempInner[nlHash[j].Attributes["key"].Value] = m.Invoke(null, new object[]{nlHash[j].InnerText}); | |
143 } | |
144 } | |
145 | |
146 htTemp[nl[i].Attributes["id"].Value] = htTempInner; | |
147 } | |
148 else if (t.IsEnum) | |
149 { | |
150 htTemp[nl[i].Attributes["id"].Value] = Enum.Parse(t, nl[i].InnerText, true); | |
151 } | |
152 else | |
153 { | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
154 m = t.GetMethod("Parse", new Type[]{stringType}); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
155 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
156 if (m!=null) |
37 | 157 { |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
158 htTemp[nl[i].Attributes["id"].Value] = m.Invoke(null, new object[]{nl[i].InnerText}); |
37 | 159 } |
160 } | |
161 } | |
162 else | |
163 { | |
164 htTemp[nl[i].Attributes["id"].Value] = nl[i].InnerText; | |
165 } | |
166 } | |
167 | |
168 htLocal = htTemp; | |
169 } | |
170 } | |
171 | |
172 public void ReloadPreferences() | |
173 { | |
174 htLocal.Clear(); | |
175 LoadLocalPreferences(); | |
176 } | |
177 | |
178 public object this[string key] | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
179 { |
37 | 180 get { return this[key, true]; } |
181 | |
182 set | |
183 { | |
184 if (!htGlobal.ContainsKey(key)) | |
185 { | |
186 throw new InvalidOperationException("Preference must already exist in the Global Preferences"); | |
187 } | |
188 | |
189 if (htGlobal[key].GetType()!=value.GetType()) | |
190 { | |
191 throw new InvalidOperationException("Preferences must be set with an object of the same type as the existing preference"); | |
192 } | |
193 | |
194 if (value is Hashtable) | |
195 { | |
196 throw new InvalidOperationException("Hashtables in Preferences cannot be set, they can only be added to or removed from."); | |
197 } | |
198 | |
199 if (htGlobal[key].Equals(value)) | |
200 { | |
201 if (htLocal.ContainsKey(key)) | |
202 { | |
203 htLocal.Remove(key); | |
204 modified = true; | |
205 } | |
206 } | |
207 else if (!((htLocal[key]==null && value==null) || value.Equals(htLocal[key]))) | |
208 { | |
209 htLocal[key] = value; | |
210 modified = true; | |
211 } | |
212 //else nothing actually needs modifying | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
213 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
214 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
215 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
216 public object this[string key, bool errorOnNoVal] |
37 | 217 { |
218 get | |
219 { | |
220 if (htLocal.ContainsKey(key)) | |
221 { | |
222 return htLocal[key]; | |
223 } | |
224 else if (htGlobal.ContainsKey(key)) | |
225 { | |
226 if (htGlobal[key] is Hashtable) | |
227 { | |
228 htLocal[key] = ((Hashtable)htGlobal[key]).Clone(); | |
229 return htLocal[key]; | |
230 } | |
231 else | |
232 { | |
233 return htGlobal[key]; | |
234 } | |
235 } | |
236 else if (errorOnNoVal) | |
237 { | |
238 throw new InvalidOperationException("Key \""+key+"\" was not associated with a preference value"); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
239 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
240 |
37 | 241 return null; |
242 } | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
243 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
244 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
245 public bool GetBooleanProperty(string key) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
246 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
247 object obj = this[key, false]; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
248 bool val = false; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
249 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
250 if (obj is bool) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
251 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
252 obj = (bool)obj; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
253 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
254 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
255 return val; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
256 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
257 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
258 public string GetStringProperty(string key) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
259 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
260 object obj = this[key, false]; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
261 string str = null; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
262 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
263 if (obj is String) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
264 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
265 str = (String)obj; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
266 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
267 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
268 return str; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
269 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
270 |
37 | 271 //public String |
272 | |
273 public bool IsModified() | |
274 { | |
275 return modified; | |
276 } | |
277 | |
278 public void Save() | |
279 { | |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
280 string prefPath = Path.Combine(Constants.UserDataPath, "preferences.xml"); |
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
281 |
37 | 282 if (htLocal.Count>0) |
283 { | |
284 XmlDocument xmld = new XmlDocument(); | |
285 xmld.LoadXml("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+ | |
286 "<!DOCTYPE prefs["+ | |
287 "<!ELEMENT preferences (preferece*)> "+ | |
288 "<!ELEMENT preference (CDATA|prefsub)> "+ | |
289 "<!ELEMENT prefsub (CDATA)> "+ | |
290 "<!ATTLIST preference id ID #REQUIRED>"+ | |
291 "<!ATTLIST preference type CDATA #REQUIRED>"+ | |
292 "<!ATTLIST prefsub key CDATA #REQUIRED>"+ | |
293 "<!ATTLIST prefsub type CDATA #REQUIRED>"+ | |
294 "]>"+"<preferences></preferences>"); | |
295 XmlNode xmln = xmld.LastChild; | |
296 XmlNode pref; | |
297 XmlAttribute attr; | |
298 XmlNode prefSub; | |
299 XmlAttribute attrSub; | |
300 object o; | |
301 Hashtable htTemp; | |
302 Hashtable htGlobalTemp; | |
303 | |
304 foreach (string key in htLocal.Keys) | |
305 { | |
306 pref = xmld.CreateNode(XmlNodeType.Element, "preference",""); | |
307 attr = xmld.CreateAttribute("id"); | |
308 attr.Value = key; | |
309 pref.Attributes.Append(attr); | |
310 attr = xmld.CreateAttribute("type"); | |
311 o = htLocal[key]; | |
312 | |
313 attr.Value = o.GetType().AssemblyQualifiedName; | |
314 | |
315 if (o.GetType().ToString() == "System.Collections.Hashtable") | |
316 { | |
317 htTemp = (Hashtable)o; | |
318 htGlobalTemp = (Hashtable)htGlobal[key]; | |
319 | |
320 foreach(object subkey in htTemp.Keys) | |
321 { | |
322 if (!htGlobalTemp.ContainsKey(subkey) || !htGlobalTemp[subkey].Equals(htTemp[subkey])) | |
323 { | |
324 prefSub = xmld.CreateNode(XmlNodeType.Element, "prefsub", ""); | |
325 attrSub = xmld.CreateAttribute("key"); | |
326 attrSub.Value = subkey.ToString(); | |
327 prefSub.Attributes.Append(attrSub); | |
328 attrSub = xmld.CreateAttribute("type"); | |
329 attrSub.Value = htTemp[subkey].GetType().AssemblyQualifiedName; | |
330 prefSub.Attributes.Append(attrSub); | |
331 prefSub.InnerText = htTemp[subkey].ToString(); | |
332 pref.AppendChild(prefSub); | |
333 } | |
334 } | |
335 } | |
336 else | |
337 { | |
338 pref.InnerText = o.ToString(); | |
339 } | |
340 | |
341 pref.Attributes.Append(attr); | |
342 xmln.AppendChild(pref); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
343 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
344 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
345 if (!Directory.Exists(Constants.UserDataPath)) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
346 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
347 Directory.CreateDirectory(Constants.UserDataPath); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
348 } |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
349 |
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
350 xmld.Save(prefPath); |
37 | 351 } |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
352 else if (File.Exists(prefPath)) |
37 | 353 { |
107
0f88d32b22cc
* Swap to built-in .Net methods (listed on http://www.ironshay.com/post/Use-NET-Built-in-Methods-to-Save-Time-and-Headaches.aspx)
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
354 File.Delete(prefPath); |
37 | 355 } |
356 | |
357 modified = false; | |
358 } | |
359 } | |
360 } |