Refactored code for exporting connections in visionapp remote desktop 2008 csv format

This commit is contained in:
David Sparer
2016-08-24 09:56:53 -06:00
parent 269366264d
commit 6280fdf973
3 changed files with 68 additions and 57 deletions

View File

@@ -74,13 +74,13 @@ namespace mRemoteNG.Config.Connections
SaveToSQL();
break;
case Format.mRCSV:
SaveToCsv();
SaveToMremotengFormattedCsv();
break;
case Format.vRDvRE:
SaveToVRE();
break;
case Format.vRDCSV:
SaveTovRDCSV();
SaveToRemoteDesktop2008FormattedCsv();
break;
default:
SaveToXml();
@@ -573,7 +573,7 @@ namespace mRemoteNG.Config.Connections
}
}
private void SaveToCsv()
private void SaveToMremotengFormattedCsv()
{
var csvConnectionsSerializer = new CsvConnectionsSerializerMremotengFormat { SaveSecurity = SaveSecurity };
var dataProvider = new FileDataProvider(ConnectionFileName);
@@ -582,60 +582,13 @@ namespace mRemoteNG.Config.Connections
}
#region vRD CSV
private StreamWriter csvWr;
private void SaveTovRDCSV()
{
if (Runtime.IsConnectionsFileLoaded == false)
{
return;
}
var tN = (TreeNode)RootTreeNode.Clone();
var tNC = tN.Nodes;
csvWr = new StreamWriter(ConnectionFileName);
SaveNodevRDCSV(tNC);
csvWr.Close();
}
private void SaveNodevRDCSV(TreeNodeCollection tNC)
{
foreach (TreeNode node in tNC)
{
if (ConnectionTreeNode.GetNodeType(node) == TreeNodeType.Connection)
{
ConnectionInfo curConI = (ConnectionInfo)node.Tag;
if (curConI.Protocol == ProtocolType.RDP)
{
WritevRDCSVLine(curConI);
}
}
else if (ConnectionTreeNode.GetNodeType(node) == TreeNodeType.Container)
{
SaveNodevRDCSV(node.Nodes);
}
}
}
private void WritevRDCSVLine(ConnectionInfo con)
{
string nodePath = con.TreeNode.FullPath;
int firstSlash = nodePath.IndexOf("\\", StringComparison.Ordinal);
nodePath = nodePath.Remove(0, firstSlash + 1);
int lastSlash = nodePath.LastIndexOf("\\", StringComparison.Ordinal);
if (lastSlash > 0)
{
nodePath = nodePath.Remove(lastSlash);
}
else
{
nodePath = "";
}
csvWr.WriteLine(con.Name + ";" + con.Hostname + ";" + con.MacAddress + ";;" + Convert.ToString(con.Port) + ";" + Convert.ToString(con.UseConsoleSession) + ";" + nodePath);
}
private void SaveToRemoteDesktop2008FormattedCsv()
{
var csvSerializer = new CsvConnectionsSerializerRemoteDesktop2008Format();
var dataProvider = new FileDataProvider(ConnectionFileName);
var csvContent = csvSerializer.Serialize(ConnectionTreeModel);
dataProvider.Save(csvContent);
}
#endregion
#region vRD VRE

View File

@@ -0,0 +1,57 @@
using System;
using System.Linq;
using mRemoteNG.App;
using mRemoteNG.Connection;
using mRemoteNG.Connection.Protocol;
using mRemoteNG.Container;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
namespace mRemoteNG.Config.Connections
{
public class CsvConnectionsSerializerRemoteDesktop2008Format : ISerializer
{
private string _csv = "";
public string Serialize(ConnectionTreeModel connectionTreeModel)
{
var rootNode = (RootNodeInfo)connectionTreeModel.RootNodes.First(node => node is RootNodeInfo);
return SerializeToCsv(rootNode);
}
private string SerializeToCsv(RootNodeInfo rootNodeInfo)
{
if (Runtime.IsConnectionsFileLoaded == false)
return "";
_csv = "";
SerializeNodesRecursive(rootNodeInfo);
return _csv;
}
private void SerializeNodesRecursive(ContainerInfo containerInfo)
{
foreach (var child in containerInfo.Children)
{
if (child is ContainerInfo)
SerializeNodesRecursive((ContainerInfo)child);
else if (child.Protocol == ProtocolType.RDP)
SerializeConnectionInfo(child);
}
}
private void SerializeConnectionInfo(ConnectionInfo con)
{
var nodePath = con.TreeNode.FullPath;
var firstSlash = nodePath.IndexOf("\\", StringComparison.Ordinal);
nodePath = nodePath.Remove(0, firstSlash + 1);
var lastSlash = nodePath.LastIndexOf("\\", StringComparison.Ordinal);
nodePath = lastSlash > 0 ? nodePath.Remove(lastSlash) : "";
_csv += con.Name + ";" + con.Hostname + ";" + con.MacAddress + ";;" + con.Port + ";" + con.UseConsoleSession + ";" + nodePath + Environment.NewLine;
}
}
}

View File

@@ -128,6 +128,7 @@
<Compile Include="App\Update\UpdateInfo.cs" />
<Compile Include="App\Windows.cs" />
<Compile Include="Config\Connections\CsvConnectionsSerializerMremotengFormat.cs" />
<Compile Include="Config\Connections\CsvConnectionsSerializerRemoteDesktop2008Format.cs" />
<Compile Include="Config\DataProviders\FileDataProviderWithBackup.cs" />
<Compile Include="Config\Connections\ConnectionsDecryptor.cs" />
<Compile Include="Config\Connections\ConnectionsLoader.cs" />