Skip to content

Commit

Permalink
Feature: Export button (#2626)
Browse files Browse the repository at this point in the history
* Feature: Export button

* Feature: Export button

* Feature: Export button

* Feature: Export button

* Feature: ExportFileType settings

* Feature: Export dialog added

* Feature: BitCalculator export

* Feature: DiscoveryProtocol export data

* Feature: IP Geolocation export added

* Feature: Network Interface export data

* Docs: #2626
  • Loading branch information
BornToBeRoot authored Mar 24, 2024
1 parent 906b3d3 commit 5661b46
Show file tree
Hide file tree
Showing 28 changed files with 1,241 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Windows;
using System.Windows.Data;
using NETworkManager.Models.Network;

namespace NETworkManager.Converters;

Expand All @@ -14,20 +15,10 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
return "-/-";

if (value is not IPAddress[] ipAddresses)
if (value is not IPAddress[] ipAddresses)
return "-/-";

var result = string.Empty;

foreach (var ipAddr in ipAddresses)
{
if (!string.IsNullOrEmpty(result))
result += Environment.NewLine;

result += ipAddr.ToString();
}

return result;

return IPv4Address.ConvertIPAddressListToString(ipAddresses);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (value is not Tuple<IPAddress, IPAddress>[] ipAddresses)
return "-/-";

var result = string.Empty;

foreach (var ipAddr in ipAddresses)
{
if (!string.IsNullOrEmpty(result))
result += Environment.NewLine;

result += ipAddr.Item1 + "/" + Subnetmask.ConvertSubnetmaskToCidr(ipAddr.Item2);
}

return result;
return IPv4Address.ConvertIPAddressWithSubnetmaskListToString(ipAddresses);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down
115 changes: 115 additions & 0 deletions Source/NETworkManager.Models/Export/ExportManager.BitCaluclatorInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NETworkManager.Models.Network;
using Newtonsoft.Json;

namespace NETworkManager.Models.Export;

public static partial class ExportManager
{
/// <summary>
/// Method to export objects from type <see cref="BitCaluclatorInfo" /> to a file.
/// </summary>
/// <param name="filePath">Path to the export file.</param>
/// <param name="fileType">Allowed <see cref="ExportFileType" /> are CSV, XML or JSON.</param>
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
public static void Export(string filePath, ExportFileType fileType, IReadOnlyList<BitCaluclatorInfo> collection)
{
switch (fileType)
{
case ExportFileType.Csv:
CreateCsv(collection, filePath);
break;
case ExportFileType.Xml:
CreateXml(collection, filePath);
break;
case ExportFileType.Json:
CreateJson(collection, filePath);
break;
case ExportFileType.Txt:
default:
throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
}
}

/// <summary>
/// Creates a CSV file from the given <see cref="BitCaluclatorInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateCsv(IEnumerable<BitCaluclatorInfo> collection, string filePath)
{
var stringBuilder = new StringBuilder();

stringBuilder.AppendLine(
$"{nameof(BitCaluclatorInfo.Bits)},{nameof(BitCaluclatorInfo.Bytes)},{nameof(BitCaluclatorInfo.Kilobits)},{nameof(BitCaluclatorInfo.Kilobytes)},{nameof(BitCaluclatorInfo.Megabits)},{nameof(BitCaluclatorInfo.Megabytes)},{nameof(BitCaluclatorInfo.Gigabits)},{nameof(BitCaluclatorInfo.Gigabytes)},{nameof(BitCaluclatorInfo.Terabits)},{nameof(BitCaluclatorInfo.Terabytes)},{nameof(BitCaluclatorInfo.Petabits)},{nameof(BitCaluclatorInfo.Petabytes)}");

foreach (var info in collection)
stringBuilder.AppendLine(
$"{info.Bits},{info.Bytes},{info.Kilobits},{info.Kilobytes},{info.Megabits},{info.Megabytes},{info.Gigabits},{info.Gigabytes},{info.Terabits},{info.Terabytes},{info.Petabits},{info.Petabytes}");

File.WriteAllText(filePath, stringBuilder.ToString());
}

/// <summary>
/// Creates a XML file from the given <see cref="BitCaluclatorInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateXml(IEnumerable<BitCaluclatorInfo> collection, string filePath)
{
var document = new XDocument(DefaultXDeclaration,
new XElement(ApplicationName.BitCalculator.ToString(),
new XElement(nameof(BitCaluclatorInfo) + "s",
from info in collection
select
new XElement(nameof(BitCaluclatorInfo),
new XElement(nameof(BitCaluclatorInfo.Bits), info.Bits),
new XElement(nameof(BitCaluclatorInfo.Bytes), info.Bytes),
new XElement(nameof(BitCaluclatorInfo.Kilobits), info.Kilobits),
new XElement(nameof(BitCaluclatorInfo.Kilobytes), info.Kilobytes),
new XElement(nameof(BitCaluclatorInfo.Megabits), info.Megabits),
new XElement(nameof(BitCaluclatorInfo.Megabytes), info.Megabytes),
new XElement(nameof(BitCaluclatorInfo.Gigabits), info.Gigabits),
new XElement(nameof(BitCaluclatorInfo.Gigabytes), info.Gigabytes),
new XElement(nameof(BitCaluclatorInfo.Terabits), info.Terabits),
new XElement(nameof(BitCaluclatorInfo.Terabytes), info.Terabytes),
new XElement(nameof(BitCaluclatorInfo.Petabits), info.Petabits),
new XElement(nameof(BitCaluclatorInfo.Petabytes), info.Petabytes)))));

document.Save(filePath);
}

/// <summary>
/// Creates a JSON file from the given <see cref="BitCaluclatorInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateJson(IReadOnlyList<BitCaluclatorInfo> collection, string filePath)
{
var jsonData = new object[collection.Count];

for (var i = 0; i < collection.Count; i++)
jsonData[i] = new
{
collection[i].Bits,
collection[i].Bytes,
collection[i].Kilobits,
collection[i].Kilobytes,
collection[i].Megabits,
collection[i].Megabytes,
collection[i].Gigabits,
collection[i].Gigabytes,
collection[i].Terabits,
collection[i].Terabytes,
collection[i].Petabits,
collection[i].Petabytes
};

File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NETworkManager.Models.Network;
using Newtonsoft.Json;

namespace NETworkManager.Models.Export;

public static partial class ExportManager
{
/// <summary>
/// Method to export objects from type <see cref="DiscoveryProtocolPackageInfo" /> to a file.
/// </summary>
/// <param name="filePath">Path to the export file.</param>
/// <param name="fileType">Allowed <see cref="ExportFileType" /> are CSV, XML or JSON.</param>
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
public static void Export(string filePath, ExportFileType fileType,
IReadOnlyList<DiscoveryProtocolPackageInfo> collection)
{
switch (fileType)
{
case ExportFileType.Csv:
CreateCsv(collection, filePath);
break;
case ExportFileType.Xml:
CreateXml(collection, filePath);
break;
case ExportFileType.Json:
CreateJson(collection, filePath);
break;
case ExportFileType.Txt:
default:
throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
}
}

/// <summary>
/// Creates a CSV file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateCsv(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath)
{
var stringBuilder = new StringBuilder();


stringBuilder.AppendLine(
$"{nameof(DiscoveryProtocolPackageInfo.Device)},{nameof(DiscoveryProtocolPackageInfo.DeviceDescription)},{nameof(DiscoveryProtocolPackageInfo.Port)},{nameof(DiscoveryProtocolPackageInfo.PortDescription)},{nameof(DiscoveryProtocolPackageInfo.Model)},{nameof(DiscoveryProtocolPackageInfo.VLAN)},{nameof(DiscoveryProtocolPackageInfo.IPAddress)},{nameof(DiscoveryProtocolPackageInfo.Protocol)},{nameof(DiscoveryProtocolPackageInfo.TimeToLive)}");

foreach (var info in collection)
stringBuilder.AppendLine(
$"{info.Device},{info.DeviceDescription},{info.Port},{info.PortDescription},{info.Model},{info.VLAN},{info.IPAddress},{info.Protocol},{info.TimeToLive}");

File.WriteAllText(filePath, stringBuilder.ToString());
}

/// <summary>
/// Creates a XML file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateXml(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath)
{
var document = new XDocument(DefaultXDeclaration,
new XElement(ApplicationName.DiscoveryProtocol.ToString(),
new XElement(nameof(DiscoveryProtocolPackageInfo) + "s",
from info in collection
select
new XElement(nameof(DiscoveryProtocolPackageInfo),
new XElement(nameof(DiscoveryProtocolPackageInfo.Device), info.Device),
new XElement(nameof(DiscoveryProtocolPackageInfo.DeviceDescription),
info.DeviceDescription),
new XElement(nameof(DiscoveryProtocolPackageInfo.Port), info.Port),
new XElement(nameof(DiscoveryProtocolPackageInfo.PortDescription), info.PortDescription),
new XElement(nameof(DiscoveryProtocolPackageInfo.Model), info.Model),
new XElement(nameof(DiscoveryProtocolPackageInfo.VLAN), info.VLAN),
new XElement(nameof(DiscoveryProtocolPackageInfo.IPAddress), info.IPAddress),
new XElement(nameof(DiscoveryProtocolPackageInfo.Protocol), info.Protocol),
new XElement(nameof(DiscoveryProtocolPackageInfo.TimeToLive), info.TimeToLive)))));

document.Save(filePath);
}

/// <summary>
/// Creates a JSON file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateJson(IReadOnlyList<DiscoveryProtocolPackageInfo> collection, string filePath)
{
var jsonData = new object[collection.Count];

for (var i = 0; i < collection.Count; i++)
jsonData[i] = new
{
collection[i].Device,
collection[i].DeviceDescription,
collection[i].Port,
collection[i].PortDescription,
collection[i].Model,
collection[i].VLAN,
collection[i].IPAddress,
collection[i].Protocol,
collection[i].TimeToLive
};

File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented));
}
}
Loading

0 comments on commit 5661b46

Please sign in to comment.