192 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Win32;
namespace Intel.Management
{
class LibSettingsProvider : SettingsProvider
{
string _appName;
static object _lock = new object();
public LibSettingsProvider()
{
}
public override string ApplicationName
{
get
{
return _appName;
}
set
{
_appName = value;
}
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
if (string.IsNullOrEmpty(name))
name="IntelvProModule";
if (config == null)
{
config = new System.Collections.Specialized.NameValueCollection();
config.Add("AutoProxyEnabled", "false");
config.Add("HttpProxyUri", string.Empty);
config.Add("HttpProxyUser", string.Empty);
config.Add("HttpProxyPassword", string.Empty);
config.Add("SocksUri", string.Empty);
config.Add("SocksUser", string.Empty);
config.Add("SocksPassword", string.Empty);
config.Add("Hosts", string.Empty);
config.Add("ValidUnitl", new DateTime(1970,1,1).ToString());
config.Add("DiscoveryUri", string.Empty);
}
base.Initialize(name, config);
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
RegistryKey rootKey = Registry.CurrentUser;
RegistryKey softwareKey=null;
RegistryKey intelKey = null;
RegistryKey libKey=null;
RegistryKey hostsKey = null;
RegistryKey mpsKey = rootKey.OpenSubKey("Software\\Intel\\WsmanLib\\MPS", true);
foreach (SettingsPropertyValue propValue in collection)
{
if (mpsKey == null)
{
softwareKey = rootKey.OpenSubKey("Software", true);
if (softwareKey == null)
softwareKey = rootKey.CreateSubKey("Software");
intelKey = softwareKey.OpenSubKey("Intel", true);
if (intelKey == null)
intelKey = softwareKey.CreateSubKey("Intel");
libKey = intelKey.OpenSubKey("WsmanLib");
if (libKey == null)
libKey = intelKey.CreateSubKey("WsmanLib");
mpsKey = libKey.OpenSubKey("MPS");
if (mpsKey == null)
mpsKey = libKey.CreateSubKey("MPS");
}
if (propValue.Name.Equals("Hosts"))
{
hostsKey = mpsKey.OpenSubKey("Hosts",true);
if (hostsKey == null)
hostsKey = mpsKey.CreateSubKey("Hosts");
foreach (string host in hostsKey.GetValueNames())
hostsKey.DeleteValue(host);
foreach (string host in (System.Collections.Specialized.StringCollection)propValue.PropertyValue)
{
hostsKey.SetValue(host, 1);
}
}
else if (propValue.Name.Equals("AutoProxyEnabled"))
{
if (((bool)propValue.PropertyValue))
mpsKey.SetValue("Enabled", 1);
else
mpsKey.SetValue("Enabled", 0);
}
else if (propValue.Name.Equals("ValidUntil"))
{
mpsKey.SetValue(propValue.Name, propValue.PropertyValue.ToString());
}
else
{
mpsKey.SetValue(propValue.Name, propValue.PropertyValue.ToString());
}
}
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
{
SettingsPropertyValueCollection result = new SettingsPropertyValueCollection();
RegistryKey rootKey = Registry.CurrentUser;
RegistryKey mpsKey = rootKey.OpenSubKey("Software\\Intel\\WsmanLib\\MPS", false);
foreach (SettingsProperty prop in collection)
{
SettingsPropertyValue propValue = new SettingsPropertyValue(prop);
if (mpsKey != null)
{
//handle non-string values
if (prop.Name.Equals("AutoProxyEnabled"))
{
object regValue = mpsKey.GetValue("Enabled");
if (regValue != null)
propValue.PropertyValue = regValue.Equals(1);
}
else if (prop.Name.Equals("Hosts"))
{
RegistryKey hostsKey = mpsKey.OpenSubKey("Hosts");
if (hostsKey != null)
{
System.Collections.Specialized.StringCollection hosts = new System.Collections.Specialized.StringCollection();
foreach (string host in hostsKey.GetValueNames())
{
hosts.Add(host);
}
propValue.PropertyValue = hosts;
}
}
else if (prop.Name.Equals("ValidUntil"))
{
object regValue = mpsKey.GetValue(prop.Name);
if (regValue != null)
propValue.PropertyValue = DateTime.Parse(regValue.ToString());
}
else
{
object regValue = mpsKey.GetValue(prop.Name);
if (regValue != null)
propValue.PropertyValue =regValue.ToString();
}
}
result.Add(propValue);
}
return result;
}
}
}