Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit f164be5bc0b0c947fb2e28a7c968c791338bed54 @crdx committed Oct 22, 2012
Showing with 222 additions and 0 deletions.
  1. +6 −0 .gitattributes
  2. +4 −0 .gitignore
  3. +212 −0 src/PortableSettingsProvider.cs
6 .gitattributes
@@ -0,0 +1,6 @@
+*.cs diff=csharp
+*.sln merge=union
+*.csproj merge=union
+*.vbproj merge=union
+*.fsproj merge=union
+*.dbproj merge=union
4 .gitignore
@@ -0,0 +1,4 @@
+bin/
+obj/
+*.suo
+*.user
212 src/PortableSettingsProvider.cs
@@ -0,0 +1,212 @@
+using System.Linq;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+using System.Configuration;
+using System.Windows.Forms;
+using System.Collections.Specialized;
+using System.Xml;
+using System.IO;
+
+namespace crdx.Settings
+{
+ public sealed class PortableSettingsProvider : SettingsProvider, IApplicationSettingsProvider
+ {
+ private const string _rootNodeName = "Settings";
+ private const string _localSettingsNodeName = "LocalSettings";
+ private const string _globalSettingsNodeName = "GlobalSettings";
+ private const string _className = "PortableSettingsProvider";
+ private XmlDocument _xmlDocument;
+
+ private string _filePath
+ {
+ get
+ {
+ return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),
+ string.Format("{0}.settings", ApplicationName));
+ }
+ }
+
+ private XmlNode _localSettingsNode
+ {
+ get
+ {
+ XmlNode settingsNode = GetSettingsNode(_localSettingsNodeName);
+ XmlNode machineNode = settingsNode.SelectSingleNode(Environment.MachineName);
+
+ if (machineNode == null)
+ {
+ machineNode = _rootDocument.CreateElement(Environment.MachineName);
+ settingsNode.AppendChild(machineNode);
+ }
+
+ return machineNode;
+ }
+ }
+
+ private XmlNode _globalSettingsNode
+ {
+ get { return GetSettingsNode(_globalSettingsNodeName); }
+ }
+
+ private XmlNode _rootNode
+ {
+ get { return _rootDocument.SelectSingleNode(_rootNodeName); }
+ }
+
+ private XmlDocument _rootDocument
+ {
+ get
+ {
+ if (_xmlDocument == null)
+ {
+ try
+ {
+ _xmlDocument = new XmlDocument();
+ _xmlDocument.Load(_filePath);
+ }
+ catch (Exception)
+ {
+ if (_xmlDocument.SelectSingleNode(_rootNodeName) != null)
+ return _xmlDocument;
+
+ _xmlDocument = new XmlDocument();
+ _xmlDocument.AppendChild(_xmlDocument.CreateXmlDeclaration("1.0", "utf-8", string.Empty));
+ _xmlDocument.AppendChild(_xmlDocument.CreateElement(_rootNodeName));
+ }
+ }
+ return _xmlDocument;
+ }
+ }
+
+ public override string ApplicationName
+ {
+ get { return Path.GetFileNameWithoutExtension(Application.ExecutablePath); }
+ set { }
+ }
+
+ public override string Name
+ {
+ get { return _className; }
+ }
+
+ public override void Initialize(string name, NameValueCollection config)
+ {
+ base.Initialize(Name, config);
+ }
+
+ public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
+ {
+ foreach (SettingsPropertyValue propertyValue in collection)
+ SetValue(propertyValue);
+
+ try
+ {
+ _rootDocument.Save(_filePath);
+ }
+ catch (Exception)
+ {
+ /*
+ * If this is a portable application and the device has been
+ * removed then this will fail, so don't do anything. It's
+ * probably better for the application to stop saving settings
+ * rather than just crashing outright. Probably.
+ */
+ }
+ }
+
+ public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
+ {
+ SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
+
+ foreach (SettingsProperty property in collection)
+ {
+ values.Add(new SettingsPropertyValue(property)
+ {
+ SerializedValue = GetValue(property)
+ });
+ }
+
+ return values;
+ }
+
+ private void SetValue(SettingsPropertyValue propertyValue)
+ {
+ XmlNode targetNode = IsGlobal(propertyValue.Property)
+ ? _globalSettingsNode
+ : _localSettingsNode;
+
+ XmlNode settingNode = targetNode.SelectSingleNode(propertyValue.Name);
+
+ if (settingNode != null)
+ settingNode.InnerText = propertyValue.SerializedValue.ToString();
+ else
+ {
+ settingNode = CreateTextElement(propertyValue.Name, propertyValue.SerializedValue.ToString());
+ targetNode.AppendChild(settingNode);
+ }
+ }
+
+ private string GetValue(SettingsProperty property)
+ {
+ XmlNode targetNode = IsGlobal(property) ? _globalSettingsNode : _localSettingsNode;
+ XmlNode settingNode = targetNode.SelectSingleNode(property.Name);
+
+ if (settingNode == null)
+ return property.DefaultValue != null ? property.DefaultValue.ToString() : string.Empty;
+
+ return settingNode.InnerText;
+ }
+
+ private bool IsGlobal(SettingsProperty property)
+ {
+ foreach (DictionaryEntry attribute in property.Attributes)
+ {
+ if ((Attribute)attribute.Value is SettingsManageabilityAttribute)
+ return true;
+ }
+
+ return false;
+ }
+
+ private XmlNode CreateTextElement(string name, string innerText)
+ {
+ XmlNode xmlElement = _rootDocument.CreateElement(name);
+ xmlElement.InnerText = innerText;
+
+ return xmlElement;
+ }
+
+ private XmlNode GetSettingsNode(string name)
+ {
+ XmlNode settingsNode = _rootNode.SelectSingleNode(name);
+
+ if (settingsNode == null)
+ {
+ settingsNode = _rootDocument.CreateElement(name);
+ _rootNode.AppendChild(settingsNode);
+ }
+
+ return settingsNode;
+ }
+
+ public void Reset(SettingsContext context)
+ {
+ _localSettingsNode.RemoveAll();
+ _globalSettingsNode.RemoveAll();
+
+ _xmlDocument.Save(_filePath);
+ }
+
+ public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property)
+ {
+ // do nothing
+ return new SettingsPropertyValue(property);
+ }
+
+ public void Upgrade(SettingsContext context, SettingsPropertyCollection properties)
+ {
+ }
+ }
+}

0 comments on commit f164be5

Please sign in to comment.
Something went wrong with that request. Please try again.