Skip to content

Commit

Permalink
p3x-robot
Browse files Browse the repository at this point in the history
  • Loading branch information
p3x-robot committed Jun 9, 2017
1 parent cd8fcba commit 391a17e
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
"PrivateKeyFile" = "8:"
"TimeStampServer" = "8:"
"InstallerBootstrapper" = "3:2"
"BootstrapperCfg:{63ACBE69-63AA-4F98-B2B6-99F9E24495F2}"
{
"Enabled" = "11:TRUE"
"PromptEnabled" = "11:TRUE"
"PrerequisitesLocation" = "2:1"
"Url" = "8:"
"ComponentsUrl" = "8:"
}
}
"Release"
{
Expand All @@ -65,6 +73,14 @@
"PrivateKeyFile" = "8:"
"TimeStampServer" = "8:"
"InstallerBootstrapper" = "3:2"
"BootstrapperCfg:{63ACBE69-63AA-4F98-B2B6-99F9E24495F2}"
{
"Enabled" = "11:TRUE"
"PromptEnabled" = "11:TRUE"
"PrerequisitesLocation" = "2:1"
"Url" = "8:"
"ComponentsUrl" = "8:"
}
}
}
"Deployable"
Expand Down Expand Up @@ -181,15 +197,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:Service Manager Tray"
"ProductCode" = "8:{A059E5F3-9E5D-4B6F-A044-A5012F25BD07}"
"PackageCode" = "8:{8B482697-6A11-467D-A92A-8E1C12A0BD6F}"
"ProductCode" = "8:{8A6796A9-65AC-4B5D-9D09-9F6A32649FA3}"
"PackageCode" = "8:{E06489B5-FA77-40DA-AC05-8282E6C770AB}"
"UpgradeCode" = "8:{9AEE7188-033D-4C05-8D66-20E147F1CA00}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:1.0.0"
"ProductVersion" = "8:1.0.1"
"Manufacturer" = "8:P3X"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down
221 changes: 221 additions & 0 deletions ServiceManagerTray/PortableSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using System.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Xml;
using System.IO;


namespace chc.servicemanagertray
{
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.ToLowerInvariant());

if (machineNode == null)
{
machineNode = _rootDocument.CreateElement(Environment.MachineName.ToLowerInvariant());
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 = GetBlankXmlDocument();
}

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(string.Format("setting[@name='{0}']", propertyValue.Name));

if (settingNode != null)
settingNode.InnerText = propertyValue.SerializedValue.ToString();
else
{
settingNode = _rootDocument.CreateElement("setting");

XmlAttribute nameAttribute = _rootDocument.CreateAttribute("name");
nameAttribute.Value = propertyValue.Name;

settingNode.Attributes.Append(nameAttribute);
settingNode.InnerText = propertyValue.SerializedValue.ToString();

targetNode.AppendChild(settingNode);
}
}

private string GetValue(SettingsProperty property)
{
XmlNode targetNode = IsGlobal(property) ? _globalSettingsNode : _localSettingsNode;
XmlNode settingNode = targetNode.SelectSingleNode(string.Format("setting[@name='{0}']", 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 GetSettingsNode(string name)
{
XmlNode settingsNode = _rootNode.SelectSingleNode(name);

if (settingsNode == null)
{
settingsNode = _rootDocument.CreateElement(name);
_rootNode.AppendChild(settingsNode);
}

return settingsNode;
}

public XmlDocument GetBlankXmlDocument()
{
XmlDocument blankXmlDocument = new XmlDocument();
blankXmlDocument.AppendChild(blankXmlDocument.CreateXmlDeclaration("1.0", "utf-8", string.Empty));
blankXmlDocument.AppendChild(blankXmlDocument.CreateElement(_rootNodeName));

return blankXmlDocument;
}

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)
{
}
}
}
10 changes: 10 additions & 0 deletions ServiceManagerTray/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions ServiceManagerTray/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="chc.servicemanagertray.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="TrayBalloonShown" Type="System.Boolean" Scope="User">
<Setting Name="TrayBalloonShown" Provider="chc.servicemanagertray.PortableSettingsProvider" Roaming="true" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="Favorites" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Setting Name="Favorites" Provider="chc.servicemanagertray.PortableSettingsProvider" Roaming="true" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;</Value>
</Setting>
<Setting Name="SortColumnName" Type="System.String" Scope="User">
<Setting Name="SortColumnName" Provider="chc.servicemanagertray.PortableSettingsProvider" Roaming="true" Type="System.String" Scope="User">
<Value Profile="(Default)">DisplayName</Value>
</Setting>
<Setting Name="SortOrder" Type="System.Windows.Forms.SortOrder" Scope="User">
<Setting Name="SortOrder" Provider="chc.servicemanagertray.PortableSettingsProvider" Roaming="true" Type="System.Windows.Forms.SortOrder" Scope="User">
<Value Profile="(Default)">Ascending</Value>
</Setting>
<Setting Name="FormWindowState" Type="System.Windows.Forms.FormWindowState" Scope="User">
<Setting Name="FormWindowState" Provider="chc.servicemanagertray.PortableSettingsProvider" Roaming="true" Type="System.Windows.Forms.FormWindowState" Scope="User">
<Value Profile="(Default)">Normal</Value>
</Setting>
</Settings>
Expand Down
1 change: 1 addition & 0 deletions ServiceManagerTray/Service Manager Tray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ColorHelper.cs" />
<Compile Include="PortableSettingsProvider.cs" />
<Compile Include="PropertyComparer.cs" />
<Compile Include="ServiceDataSource.cs" />
<Compile Include="ServiceManagerForm.cs">
Expand Down
Loading

0 comments on commit 391a17e

Please sign in to comment.