From 96946f3a1e40037f5c225ccb3ee21d53707a3ec7 Mon Sep 17 00:00:00 2001 From: Sean Kaim Date: Fri, 27 Jul 2018 14:15:23 -0400 Subject: [PATCH] porting fix for #971 to 1.76 branch --- CHANGELOG.TXT | 1 + .../Settings/Providers/ChooseProvider.cs | 4 +- .../Providers/PortableSettingsProvider.cs | 158 +++++++----------- 3 files changed, 65 insertions(+), 98 deletions(-) diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 73b0a529..6e90e8e4 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -14,6 +14,7 @@ Fixes: #999: Some hotkeys stop working if File menu was called when PuTTy Saved Sessions was selected #998: Can sometimes add connection under PuTTY Sessions node #991: Error when deleting host in filtered view +#971: Portable Settings now apply to any machine they are used on #961: Connections file overwritten if correct decryption password not provided diff --git a/mRemoteV1/Config/Settings/Providers/ChooseProvider.cs b/mRemoteV1/Config/Settings/Providers/ChooseProvider.cs index 5edbde03..10a1a342 100644 --- a/mRemoteV1/Config/Settings/Providers/ChooseProvider.cs +++ b/mRemoteV1/Config/Settings/Providers/ChooseProvider.cs @@ -1,4 +1,6 @@ -using System.Configuration; +#if !PORTABLE +using System.Configuration; +#endif namespace mRemoteNG.Config.Settings.Providers { diff --git a/mRemoteV1/Config/Settings/Providers/PortableSettingsProvider.cs b/mRemoteV1/Config/Settings/Providers/PortableSettingsProvider.cs index 3e42acfe..51349b26 100644 --- a/mRemoteV1/Config/Settings/Providers/PortableSettingsProvider.cs +++ b/mRemoteV1/Config/Settings/Providers/PortableSettingsProvider.cs @@ -1,37 +1,37 @@ -/// The MIT License (MIT) -/// -/// Copyright(c) crdx -/// -/// Permission is hereby granted, free of charge, to any person obtaining -/// a copy of this software and associated documentation files (the -/// "Software"), to deal in the Software without restriction, including -/// without limitation the rights to use, copy, modify, merge, publish, -/// distribute, sublicense, and/or sell copies of the Software, and to -/// permit persons to whom the Software is furnished to do so, subject to -/// the following conditions: -/// -/// The above copyright notice and this permission notice shall be -/// included in all copies or substantial portions of the Software. -/// -/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -/// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -/// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -/// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -/// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -/// -/// https://raw.githubusercontent.com/crdx/PortableSettingsProvider -/// -using System.Linq; +// The MIT License (MIT) +// +// Copyright(c) crdx +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// https://raw.githubusercontent.com/crdx/PortableSettingsProvider +// + 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; +//using mRemoteNG.App; namespace mRemoteNG.Config.Settings.Providers { @@ -43,63 +43,34 @@ namespace mRemoteNG.Config.Settings.Providers 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 string _filePath => Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) ?? throw new InvalidOperationException(), $"{ApplicationName}.settings"); - private XmlNode _localSettingsNode - { - get - { - XmlNode settingsNode = GetSettingsNode(_localSettingsNodeName); - XmlNode machineNode = settingsNode.SelectSingleNode(Environment.MachineName.ToLowerInvariant()); + private XmlNode _localSettingsNode => GetSettingsNode(_localSettingsNodeName); - if (machineNode == null) - { - machineNode = _rootDocument.CreateElement(Environment.MachineName.ToLowerInvariant()); - settingsNode.AppendChild(machineNode); - } + private XmlNode _globalSettingsNode => GetSettingsNode(_globalSettingsNodeName); - return machineNode; - } - } - - private XmlNode _globalSettingsNode - { - get { return GetSettingsNode(_globalSettingsNodeName); } - } - - private XmlNode _rootNode - { - get { return _rootDocument.SelectSingleNode(_rootNodeName); } - } + private XmlNode _rootNode => _rootDocument.SelectSingleNode(_rootNodeName); private XmlDocument _rootDocument { get { - if (_xmlDocument == null) + if (_xmlDocument != null) return _xmlDocument; + try { - try - { - _xmlDocument = new XmlDocument(); - _xmlDocument.Load(_filePath); - } - catch (Exception) - { - - } - - if (_xmlDocument.SelectSingleNode(_rootNodeName) != null) - return _xmlDocument; - - _xmlDocument = GetBlankXmlDocument(); + _xmlDocument = new XmlDocument(); + _xmlDocument.Load(_filePath); } + catch (Exception /*ex*/) + { + // This casues hundreds of unit tests to fail for some reason... + //Runtime.MessageCollector.AddExceptionStackTrace("PortableSettingsProvider: Error getting XML", ex); + } + + if (_xmlDocument?.SelectSingleNode(_rootNodeName) != null) + return _xmlDocument; + + _xmlDocument = GetBlankXmlDocument(); return _xmlDocument; } @@ -107,14 +78,11 @@ namespace mRemoteNG.Config.Settings.Providers public override string ApplicationName { - get { return Path.GetFileNameWithoutExtension(Application.ExecutablePath); } + get => Path.GetFileNameWithoutExtension(Application.ExecutablePath); set { } } - public override string Name - { - get { return _className; } - } + public override string Name => _className; public override void Initialize(string name, NameValueCollection config) { @@ -143,7 +111,7 @@ namespace mRemoteNG.Config.Settings.Providers public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection) { - SettingsPropertyValueCollection values = new SettingsPropertyValueCollection(); + var values = new SettingsPropertyValueCollection(); foreach (SettingsProperty property in collection) { @@ -158,11 +126,9 @@ namespace mRemoteNG.Config.Settings.Providers private void SetValue(SettingsPropertyValue propertyValue) { - XmlNode targetNode = IsGlobal(propertyValue.Property) - ? _globalSettingsNode - : _localSettingsNode; + var targetNode = IsGlobal(propertyValue.Property) ? _globalSettingsNode : _localSettingsNode; - XmlNode settingNode = targetNode.SelectSingleNode(string.Format("setting[@name='{0}']", propertyValue.Name)); + var settingNode = targetNode.SelectSingleNode($"setting[@name='{propertyValue.Name}']"); if (settingNode != null) settingNode.InnerText = propertyValue.SerializedValue.ToString(); @@ -170,10 +136,10 @@ namespace mRemoteNG.Config.Settings.Providers { settingNode = _rootDocument.CreateElement("setting"); - XmlAttribute nameAttribute = _rootDocument.CreateAttribute("name"); + var nameAttribute = _rootDocument.CreateAttribute("name"); nameAttribute.Value = propertyValue.Name; - settingNode.Attributes.Append(nameAttribute); + settingNode.Attributes?.Append(nameAttribute); settingNode.InnerText = propertyValue.SerializedValue.ToString(); targetNode.AppendChild(settingNode); @@ -182,8 +148,8 @@ namespace mRemoteNG.Config.Settings.Providers private string GetValue(SettingsProperty property) { - XmlNode targetNode = IsGlobal(property) ? _globalSettingsNode : _localSettingsNode; - XmlNode settingNode = targetNode.SelectSingleNode(string.Format("setting[@name='{0}']", property.Name)); + var targetNode = IsGlobal(property) ? _globalSettingsNode : _localSettingsNode; + var settingNode = targetNode.SelectSingleNode($"setting[@name='{property.Name}']"); if (settingNode == null) return property.DefaultValue != null ? property.DefaultValue.ToString() : string.Empty; @@ -191,7 +157,7 @@ namespace mRemoteNG.Config.Settings.Providers return settingNode.InnerText; } - private bool IsGlobal(SettingsProperty property) + private static bool IsGlobal(SettingsProperty property) { foreach (DictionaryEntry attribute in property.Attributes) { @@ -204,20 +170,18 @@ namespace mRemoteNG.Config.Settings.Providers private XmlNode GetSettingsNode(string name) { - XmlNode settingsNode = _rootNode.SelectSingleNode(name); + var settingsNode = _rootNode.SelectSingleNode(name); - if (settingsNode == null) - { - settingsNode = _rootDocument.CreateElement(name); - _rootNode.AppendChild(settingsNode); - } + if (settingsNode != null) return settingsNode; + settingsNode = _rootDocument.CreateElement(name); + _rootNode.AppendChild(settingsNode); return settingsNode; } - public XmlDocument GetBlankXmlDocument() + private static XmlDocument GetBlankXmlDocument() { - XmlDocument blankXmlDocument = new XmlDocument(); + var blankXmlDocument = new XmlDocument(); blankXmlDocument.AppendChild(blankXmlDocument.CreateXmlDeclaration("1.0", "utf-8", string.Empty)); blankXmlDocument.AppendChild(blankXmlDocument.CreateElement(_rootNodeName));