Skip to content

Commit f01e6c6

Browse files
settings helper
1 parent 071a1f8 commit f01e6c6

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Helper/Settings.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Xml;
5+
using System.Xml.Linq;
6+
using Vaiona.Utils.Cfg;
7+
8+
namespace BExIS.Modules.EMM.UI.Helper
9+
{
10+
static class Settings
11+
{
12+
13+
private static String filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("EMM"), "Emm.settings.xml");
14+
private static Dictionary<string, object> settings = new Dictionary<string, object>();
15+
16+
/// <summary>
17+
/// setup settings model
18+
/// * load settings from settings.xml
19+
/// </summary>
20+
static Settings()
21+
{
22+
// intial loading of settings
23+
reloadSettings();
24+
25+
// set up file watcher to listen for changes
26+
using (FileSystemWatcher fw = new FileSystemWatcher())
27+
{
28+
fw.Path = Path.GetDirectoryName(filePath);
29+
fw.Filter = Path.GetFileName(filePath);
30+
fw.Changed += new FileSystemEventHandler(fw_Changed);
31+
fw.EnableRaisingEvents = true;
32+
}
33+
34+
}
35+
36+
/// <summary>
37+
/// retrieve a value from the settings file
38+
/// </summary>
39+
/// <param name="key">the key for the parameter</param>
40+
/// <returns>the respective value</returns>
41+
public static object get(String key)
42+
{
43+
if (settings.ContainsKey(key))
44+
{
45+
return settings[key];
46+
}
47+
else
48+
{
49+
return null;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// add or change an entry in the settings
55+
/// TODO persist changes in workflow file
56+
/// </summary>
57+
/// <param name="key"></param>
58+
/// <param name="value"></param>
59+
public static void set(String key, object value)
60+
{
61+
if (settings.ContainsKey(key))
62+
{
63+
settings[key] = value;
64+
}
65+
else
66+
{
67+
settings.Add(key, value);
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Handler to listen for changes in settings file
73+
/// </summary>
74+
/// <param name="sender"></param>
75+
/// <param name="e"></param>
76+
private static void fw_Changed(object sender, FileSystemEventArgs e)
77+
{
78+
reloadSettings();
79+
}
80+
81+
/// <summary>
82+
/// load settings anew from settings.xml
83+
/// </summary>
84+
private static void reloadSettings()
85+
{
86+
// get XML data
87+
XDocument xDoc = XDocument.Load(filePath);
88+
XmlDocument xmlDoc = new XmlDocument();
89+
xmlDoc.Load(xDoc.CreateReader());
90+
91+
// empty old settings list
92+
settings.Clear();
93+
94+
// parse values
95+
foreach (XmlNode node in xmlDoc.SelectNodes("//settings/entry"))
96+
{
97+
// shortcuts
98+
var key = node.Attributes["key"] != null ? node.Attributes["key"].Value : null;
99+
var value = node.Attributes["value"] != null ? node.Attributes["value"].Value : null;
100+
var type = node.Attributes["type"] != null ? node.Attributes["type"].Value : null;
101+
102+
// only parse valid entries
103+
if ((null == key) || (null == value))
104+
{
105+
continue;
106+
}
107+
108+
// convert types
109+
switch (type)
110+
{
111+
case "int":
112+
int intVal;
113+
if (Int32.TryParse(value, out intVal))
114+
{
115+
settings.Add(key, intVal);
116+
}
117+
else
118+
{
119+
settings.Add(key, value);
120+
}
121+
break;
122+
123+
// default is string
124+
default:
125+
settings.Add(key, value);
126+
break;
127+
}
128+
}
129+
}
130+
131+
}
132+
}

0 commit comments

Comments
 (0)