/[cits3200i]/branches/gps-running-clean-r116/HighScorePlugin/Source/Settings.cs


UCC Code Repository

Contents of /branches/gps-running-clean-r116/HighScorePlugin/Source/Settings.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations) (download)
Sun Sep 5 15:48:34 2010 UTC (11 years, 3 months ago) by rvvs89
File size: 25783 byte(s)
Branching a local copy of the HEAD revision of the gps-running project on Google Code.
We will probably have to use this project as a template for our own plugins if we want to support SportTracks 2.1.
1 /*
2 Copyright (C) 2007, 2008 Kristian Bisgaard Lassen
3 Copyright (C) 2010 Kristian Helkjaer Lassen
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22 using System.Xml;
23 using System.IO;
24 using System.Drawing;
25 using System.Drawing.Imaging;
26 using System.Globalization;
27 using ZoneFiveSoftware.Common.Data.Measurement;
28 using GpsRunningPlugin.Properties;
29
30 namespace GpsRunningPlugin.Source
31 {
32 class Settings
33 {
34 static Settings()
35 {
36 distances = new SortedList<double, bool>();
37 times = new SortedList<int, TimeSpan>();
38 elevations = new SortedList<double, bool>();
39 pulseZones = new SortedList<double, SortedList<double, bool>>();
40 speedZones = new SortedList<double, SortedList<double, bool>>();
41 defaults();
42 }
43
44 public readonly static SortedList<double, bool> distances;
45 public readonly static SortedList<int, TimeSpan> times;
46 public readonly static SortedList<double, bool> elevations;
47 public readonly static SortedList<double, SortedList<double, bool>> pulseZones;
48 public readonly static SortedList<double, SortedList<double, bool>> speedZones;
49
50 private static bool showTable;
51 public static bool ShowTable
52 {
53 set { showTable = value; }
54 get { return showTable; }
55 }
56
57 private static bool ignoreManualData;
58 public static bool IgnoreManualData
59 {
60 set { ignoreManualData = value; }
61 get { return ignoreManualData; }
62 }
63
64 private static GoalParameter domain;
65 public static GoalParameter Domain
66 {
67 set { domain = value; }
68 get { return domain; }
69 }
70
71 private static GoalParameter image;
72 public static GoalParameter Image
73 {
74 set { image = value; }
75 get { return image; }
76 }
77
78 private static bool upperBound;
79 public static bool UpperBound
80 {
81 set { upperBound = value; }
82 get { return upperBound; }
83 }
84
85 private static Size windowSize;
86 public static Size WindowSize
87 {
88 set { windowSize = value; }
89 get { return windowSize; }
90 }
91
92 public static void resetDistances()
93 {
94 distances.Clear();
95 distances.Add(100, true);
96 distances.Add(200, true);
97 distances.Add(400, true);
98 distances.Add(500, true);
99 distances.Add(800, true);
100 distances.Add(1000, true);
101 distances.Add(1500, true);
102 distances.Add(convertTo(1, Length.Units.Mile), true);
103 distances.Add(2000, true);
104 distances.Add(3000, true);
105 distances.Add(convertTo(2, Length.Units.Mile), true);
106 distances.Add(4000, true);
107 distances.Add(convertTo(3, Length.Units.Mile), true);
108 distances.Add(convertTo(5, Length.Units.Kilometer), true);
109 distances.Add(convertTo(4, Length.Units.Mile), true);
110 distances.Add(convertTo(8, Length.Units.Kilometer), true);
111 distances.Add(convertTo(5, Length.Units.Mile), true);
112 distances.Add(convertTo(10, Length.Units.Kilometer), true);
113 distances.Add(convertTo(15, Length.Units.Kilometer), true);
114 distances.Add(convertTo(10, Length.Units.Mile), true);
115 distances.Add(convertTo(20, Length.Units.Kilometer), true);
116 distances.Add(convertTo(21.0975, Length.Units.Kilometer), true);
117 distances.Add(convertTo(15, Length.Units.Mile), true);
118 distances.Add(convertTo(25, Length.Units.Kilometer), true);
119 distances.Add(convertTo(30, Length.Units.Kilometer), true);
120 distances.Add(convertTo(20, Length.Units.Mile), true);
121 distances.Add(convertTo(25, Length.Units.Mile), true);
122 distances.Add(convertTo(42.195, Length.Units.Kilometer), true);
123 }
124
125 public static void resetTimes()
126 {
127 times.Clear();
128 times.Add(tt(0, 0, 10), new TimeSpan(0, 0, 10));
129 times.Add(tt(0, 0, 30), new TimeSpan(0, 0, 30));
130 times.Add(tt(0, 1, 0), new TimeSpan(0, 1, 0));
131 times.Add(tt(0, 2, 0), new TimeSpan(0, 2, 0));
132 times.Add(tt(0, 3, 0), new TimeSpan(0, 3, 0));
133 times.Add(tt(0, 5, 0), new TimeSpan(0, 5, 0));
134 times.Add(tt(0, 10, 0), new TimeSpan(0, 10, 0));
135 times.Add(tt(0, 15, 0), new TimeSpan(0, 15, 0));
136 times.Add(tt(0, 20, 0), new TimeSpan(0, 20, 0));
137 times.Add(tt(0, 25, 0), new TimeSpan(0, 25, 0));
138 times.Add(tt(0, 30, 0), new TimeSpan(0, 30, 0));
139 times.Add(tt(0, 40, 0), new TimeSpan(0, 40, 0));
140 times.Add(tt(0, 50, 0), new TimeSpan(0, 50, 0));
141 times.Add(tt(1, 0, 0), new TimeSpan(1, 0, 0));
142 times.Add(tt(1, 30, 0), new TimeSpan(1, 30, 0));
143 times.Add(tt(2, 0, 0), new TimeSpan(2, 0, 0));
144 times.Add(tt(2, 30, 0), new TimeSpan(2, 30, 0));
145 times.Add(tt(3, 0, 0), new TimeSpan(3, 0, 0));
146 times.Add(tt(3, 30, 0), new TimeSpan(3, 30, 0));
147 times.Add(tt(4, 0, 0), new TimeSpan(4, 0, 0));
148 }
149
150 public static void resetElevations()
151 {
152 elevations.Clear();
153 elevations.Add(10, true);
154 elevations.Add(20, true);
155 elevations.Add(30, true);
156 elevations.Add(50, true);
157 elevations.Add(75, true);
158 elevations.Add(100, true);
159 elevations.Add(150, true);
160 elevations.Add(200, true);
161 elevations.Add(300, true);
162 elevations.Add(400, true);
163 elevations.Add(500, true);
164 elevations.Add(600, true);
165 elevations.Add(700, true);
166 elevations.Add(800, true);
167 elevations.Add(1000, true);
168 elevations.Add(1200, true);
169 elevations.Add(1400, true);
170 elevations.Add(1600, true);
171 elevations.Add(1800, true);
172 elevations.Add(2000, true);
173 }
174
175 public static void resetPulseZones()
176 {
177 pulseZones.Clear();
178 addPulse(0, 100);
179 addPulse(100, 120);
180 addPulse(120, 140);
181 addPulse(140, 160);
182 addPulse(160, 180);
183 addPulse(180, 200);
184 addPulse(200, 220);
185 }
186
187 public static void resetSpeedZones()
188 {
189 speedZones.Clear();
190 addSpeed(2.77777777777778, 3.33333333333333);
191 addSpeed(3.03030303030303, 3.33333333333333);
192 addSpeed(3.03030303030303, 3.7037037037037);
193 addSpeed(3.33333333333333, 3.7037037037037);
194 addSpeed(3.50877192982456, 3.92156862745098);
195 addSpeed(3.7037037037037, 4.16666666666667);
196 addSpeed(3.7037037037037, 4.44444444444444);
197 addSpeed(3.92156862745098, 4.44444444444444);
198 addSpeed(4.16666666666667, 4.76190476190476);
199 addSpeed(4.76190476190476, 5.55555555555556);
200 }
201
202 private static int resetLists()
203 {
204 resetDistances();
205 resetTimes();
206 resetElevations();
207 resetPulseZones();
208 resetSpeedZones();
209
210 return 0;
211 }
212
213 public static void defaults()
214 {
215 resetLists();
216 domain = GoalParameter.Time;
217 image = GoalParameter.Distance;
218 upperBound = false;
219 showTable = true;
220 ignoreManualData = true;
221 windowSize = new Size(800, 600);
222 }
223
224 public static void ReadOptions(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, XmlElement pluginNode)
225 {
226 String attr, attr2;
227
228 attr = pluginNode.GetAttribute(xmlTags.settingsVersion);
229 if (attr.Length > 0) { settingsVersion = (Int16)XmlConvert.ToInt16(attr); }
230 if (0 == settingsVersion)
231 {
232 // No settings in Preferences.System found, try read old files
233 load();
234 }
235
236 attr = pluginNode.GetAttribute(xmlTags.distances);
237 if (attr.Length > 0)
238 {
239 distances.Clear();
240 String[] values = attr.Split(';');
241 foreach (String distance in values)
242 {
243 distances.Add(parseDouble(distance), true);
244 }
245 }
246 attr = pluginNode.GetAttribute(xmlTags.times);
247 if (attr.Length > 0)
248 {
249 times.Clear();
250 String[] values = attr.Split(';');
251 foreach (String value in values)
252 {
253 int seconds = int.Parse(value);
254 if (!times.ContainsKey(seconds))
255 {
256 times.Add(seconds, new TimeSpan(0, 0, seconds));
257 }
258 }
259 }
260 attr = pluginNode.GetAttribute(xmlTags.elevations);
261 if (attr.Length > 0)
262 {
263 elevations.Clear();
264 String[] values = attr.Split(';');
265 foreach (String elevation in values)
266 {
267 elevations.Add(parseDouble(elevation), true);
268 }
269 }
270 attr = pluginNode.GetAttribute(xmlTags.pulseZones);
271 if (attr.Length > 0)
272 {
273 pulseZones.Clear();
274 String[] values = attr.Split(';');
275 foreach (String value in values)
276 {
277 String[] pair = value.Split(' ');
278 double min = parseDouble(pair[0]);
279 double max = parseDouble(pair[1]);
280 if (pulseZones.ContainsKey(min))
281 {
282 if (!pulseZones[min].ContainsKey(max))
283 {
284 pulseZones[min].Add(max, true);
285 }
286 }
287 else
288 {
289 SortedList<double, bool> list = new SortedList<double, bool>();
290 list.Add(max, true);
291 pulseZones.Add(min, list);
292 }
293 }
294 }
295 attr = pluginNode.GetAttribute(xmlTags.speedZones);
296 if (attr.Length > 0)
297 {
298 speedZones.Clear();
299 String[] values = attr.Split(';');
300 foreach (String value in values)
301 {
302 String[] pair = value.Split(' ');
303 double min = parseDouble(pair[0]);
304 double max = parseDouble(pair[1]);
305 if (speedZones.ContainsKey(min))
306 {
307 if (!speedZones[min].ContainsKey(max))
308 {
309 speedZones[min].Add(max, true);
310 }
311 }
312 else
313 {
314 SortedList<double, bool> list = new SortedList<double, bool>();
315 list.Add(max, true);
316 speedZones.Add(min, list);
317 }
318 }
319 }
320
321 attr = pluginNode.GetAttribute(xmlTags.domain);
322 if (attr.Length > 0) { domain = (GoalParameter)Enum.Parse(typeof(GoalParameter), attr, true); }
323 attr = pluginNode.GetAttribute(xmlTags.image);
324 if (attr.Length > 0) { image = (GoalParameter)Enum.Parse(typeof(GoalParameter), attr, true); }
325 attr = pluginNode.GetAttribute(xmlTags.upperBound);
326 if (attr.Length > 0) { upperBound = XmlConvert.ToBoolean(attr); }
327 attr = pluginNode.GetAttribute(xmlTags.showTable);
328 if (attr.Length > 0) { showTable = XmlConvert.ToBoolean(attr); }
329 attr = pluginNode.GetAttribute(xmlTags.ignoreManualData);
330 if (attr.Length > 0) { ignoreManualData = XmlConvert.ToBoolean(attr); }
331
332 attr = pluginNode.GetAttribute(xmlTags.viewWidth);
333 attr2 = pluginNode.GetAttribute(xmlTags.viewHeight);
334 if (attr.Length > 0 && attr2.Length > 0)
335 {
336 windowSize = new Size(XmlConvert.ToInt16(attr), XmlConvert.ToInt16(attr2));
337 }
338 }
339
340 public static void WriteOptions(XmlDocument xmlDoc, XmlElement pluginNode)
341 {
342 pluginNode.SetAttribute(xmlTags.settingsVersion, XmlConvert.ToString(settingsVersionCurrent));
343
344 String distanceText = null;
345 foreach (double distance in distances.Keys)
346 {
347 if (distanceText == null) { distanceText = distance.ToString(NumberFormatInfo.InvariantInfo); }
348 else { distanceText += ";" + distance.ToString(NumberFormatInfo.InvariantInfo); }
349 }
350 pluginNode.SetAttribute(xmlTags.distances, distanceText);
351
352 String timeText = null;
353 foreach (int time in times.Keys)
354 {
355 if (timeText == null) { timeText = time.ToString(); }
356 else { timeText += ";" + time.ToString(); }
357 }
358 pluginNode.SetAttribute(xmlTags.times, timeText);
359
360 String elevationText = null;
361 foreach (double elevation in elevations.Keys)
362 {
363 if (elevationText == null) { elevationText = elevation.ToString(NumberFormatInfo.InvariantInfo); }
364 else { elevationText += ";" + elevation; }
365 }
366 pluginNode.SetAttribute(xmlTags.elevations, elevationText);
367
368 String pulseText = null;
369 foreach (double min in pulseZones.Keys)
370 {
371 foreach (double max in pulseZones[min].Keys)
372 {
373 if (pulseText == null) { pulseText = min + " " + max; }
374 else { pulseText += ";" + min + " " + max; }
375 }
376 }
377 pluginNode.SetAttribute(xmlTags.pulseZones, pulseText);
378
379 String speedText = null;
380 foreach (double min in speedZones.Keys)
381 {
382 foreach (double max in speedZones[min].Keys)
383 {
384 String minS = min.ToString(NumberFormatInfo.InvariantInfo);
385 String maxS = max.ToString(NumberFormatInfo.InvariantInfo);
386 if (speedText == null) { speedText = minS + " " + maxS; }
387 else { speedText += ";" + minS + " " + maxS; }
388 }
389 }
390 pluginNode.SetAttribute(xmlTags.speedZones, speedText);
391
392 pluginNode.SetAttribute(xmlTags.domain, domain.ToString());
393 pluginNode.SetAttribute(xmlTags.image, image.ToString());
394 pluginNode.SetAttribute(xmlTags.upperBound, XmlConvert.ToString(upperBound));
395 pluginNode.SetAttribute(xmlTags.showTable, XmlConvert.ToString(showTable));
396 pluginNode.SetAttribute(xmlTags.ignoreManualData, XmlConvert.ToString(ignoreManualData));
397
398 pluginNode.SetAttribute(xmlTags.viewWidth, XmlConvert.ToString(windowSize.Width));
399 pluginNode.SetAttribute(xmlTags.viewHeight, XmlConvert.ToString(windowSize.Height));
400 }
401
402 private static int settingsVersion = 0; //default when not existing
403 private const int settingsVersionCurrent = 1;
404
405 private class xmlTags
406 {
407 public const string settingsVersion = "settingsVersion";
408
409 public const string distances = "distances";
410 public const string times = "times";
411 public const string elevations = "elevations";
412 public const string pulseZones = "pulseZones";
413 public const string speedZones = "speedZones";
414
415 public const string domain = "domain";
416 public const string image = "image";
417 public const string upperBound = "upperBound";
418 public const string showTable = "showTable";
419 public const string ignoreManualData = "ignoreManualData";
420
421 public const string viewWidth = "viewWidth";
422 public const string viewHeight = "viewHeight";
423 }
424
425 private static bool load()
426 {
427 //Backwards compatibility, read old preferences file
428 String prefsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "HighScorePlugin" + Path.DirectorySeparatorChar + "preferences.xml";
429
430 if (!File.Exists(prefsPath)) return false;
431 XmlDocument document = new XmlDocument();
432 XmlReader reader = new XmlTextReader(prefsPath);
433 document.Load(reader);
434 XmlNode root = document.FirstChild;
435 if (!root.Name.Equals("highscore")) return false;
436 try
437 {
438 foreach (XmlNode elm in root.ChildNodes)
439 {
440 if (elm.Name.Equals("distances"))
441 {
442 if (elm.Attributes["values"].Value.Length > 0)
443 {
444 String[] values = elm.Attributes["values"].Value.Split(';');
445 foreach (String distance in values)
446 {
447 distances.Add(parseDouble(distance), true);
448 }
449 }
450 }
451 else if (elm.Name.Equals("times"))
452 {
453 if (elm.Attributes["values"].Value.Length > 0)
454 {
455 String[] values = elm.Attributes["values"].Value.Split(';');
456 foreach (String value in values)
457 {
458 int seconds = int.Parse(value);
459 if (!times.ContainsKey(seconds))
460 times.Add(seconds, new TimeSpan(0, 0, seconds));
461 }
462 }
463 }
464 else if (elm.Name.Equals("elevations"))
465 {
466 if (elm.Attributes["values"].Value.Length > 0)
467 {
468 String[] values = elm.Attributes["values"].Value.Split(';');
469 foreach (String elevation in values)
470 {
471 elevations.Add(parseDouble(elevation), true);
472 }
473 }
474 }
475 else if (elm.Name.Equals("pulseZones"))
476 {
477 if (elm.Attributes["values"].Value.Length > 0)
478 {
479 String[] values = elm.Attributes["values"].Value.Split(';');
480 foreach (String value in values)
481 {
482 String[] pair = value.Split(' ');
483 double min = parseDouble(pair[0]);
484 double max = parseDouble(pair[1]);
485 if (pulseZones.ContainsKey(min))
486 {
487 if (!pulseZones[min].ContainsKey(max))
488 {
489 pulseZones[min].Add(max, true);
490 }
491 }
492 else
493 {
494 SortedList<double, bool> list = new SortedList<double, bool>();
495 list.Add(max, true);
496 pulseZones.Add(min, list);
497 }
498 }
499 }
500 }
501 else if (elm.Name.Equals("speedZones"))
502 {
503 if (elm.Attributes["values"].Value.Length > 0)
504 {
505 String[] values = elm.Attributes["values"].Value.Split(';');
506 foreach (String value in values)
507 {
508 String[] pair = value.Split(' ');
509 double min = parseDouble(pair[0]);
510 double max = parseDouble(pair[1]);
511 if (speedZones.ContainsKey(min))
512 {
513 if (!speedZones[min].ContainsKey(max))
514 {
515 speedZones[min].Add(max, true);
516 }
517 }
518 else
519 {
520 SortedList<double, bool> list = new SortedList<double, bool>();
521 list.Add(max, true);
522 speedZones.Add(min, list);
523 }
524 }
525 }
526 }
527 else if (elm.Name.Equals("view"))
528 {
529 domain = (GoalParameter) Enum.Parse(typeof(GoalParameter),elm.Attributes["domain"].Value,true);
530 image = (GoalParameter)Enum.Parse(typeof(GoalParameter), elm.Attributes["image"].Value, true);
531 upperBound = bool.Parse(elm.Attributes["upperBound"].Value);
532 windowSize = new Size(int.Parse(elm.Attributes["viewWidth"].Value),
533 int.Parse(elm.Attributes["viewHeight"].Value));
534 if (elm.Attributes["showTable"] == null)
535 showTable = true;
536 else
537 showTable = bool.Parse(elm.Attributes["showTable"].Value);
538 if (elm.Attributes["ignoreManualData"] == null)
539 ignoreManualData = true;
540 else
541 ignoreManualData = bool.Parse(elm.Attributes["ignoreManualData"].Value);
542 }
543 }
544 }
545 catch (Exception)
546 {
547 reader.Close();
548 return false;
549 }
550 reader.Close();
551 return true;
552 }
553
554 private static double parseDouble(string p)
555 {
556 //if (!p.Contains(".")) p += ".0";
557 double d = double.Parse(p,NumberFormatInfo.InvariantInfo);
558 return d;
559 }
560
561 public static bool addSpeed(double min, double max)
562 {
563 SortedList<double, bool> list = null;
564 if (speedZones.ContainsKey(min))
565 list = speedZones[min];
566 if (list == null)
567 {
568 list = new SortedList<double, bool>();
569 list.Add(max, true);
570 speedZones.Add(min, list);
571 return true;
572 }
573 else
574 {
575 if (!list.ContainsKey(max))
576 {
577 list.Add(max, true);
578 }
579 return false;
580 }
581 }
582
583 public static bool addPulse(double min, double max)
584 {
585 SortedList<double, bool> list = null;
586 if (pulseZones.ContainsKey(min))
587 list = pulseZones[min];
588 if (list == null)
589 {
590 list = new SortedList<double, bool>();
591 list.Add(max, true);
592 pulseZones.Add(min, list);
593 return true;
594 }
595 else
596 {
597 if (!list.ContainsKey(max))
598 {
599 list.Add(max, true);
600 }
601 return false;
602 }
603 }
604
605 private static double convertTo(double p, Length.Units metric)
606 {
607 return Length.Convert(p, metric, Length.Units.Meter);
608 }
609 private static int tt(int hours, int minutes, int seconds)
610 {
611 return 3600 * hours + 60 * minutes + seconds;
612 }
613 }
614 }
615
616

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26