Skip to content

Commit 5661b46

Browse files
authored
Feature: Export button (BornToBeRoot#2626)
* 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: BornToBeRoot#2626
1 parent 906b3d3 commit 5661b46

28 files changed

Lines changed: 1241 additions & 210 deletions

Source/NETworkManager.Converters/IPAddressArrayToStringConverter.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Windows;
66
using System.Windows.Data;
7+
using NETworkManager.Models.Network;
78

89
namespace NETworkManager.Converters;
910

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

17-
if (value is not IPAddress[] ipAddresses)
18+
if (value is not IPAddress[] ipAddresses)
1819
return "-/-";
19-
20-
var result = string.Empty;
21-
22-
foreach (var ipAddr in ipAddresses)
23-
{
24-
if (!string.IsNullOrEmpty(result))
25-
result += Environment.NewLine;
26-
27-
result += ipAddr.ToString();
28-
}
29-
30-
return result;
20+
21+
return IPv4Address.ConvertIPAddressListToString(ipAddresses);
3122
}
3223

3324
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

Source/NETworkManager.Converters/IPAddressSubnetmaskTupleArrayToStringConverter.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
1818
if (value is not Tuple<IPAddress, IPAddress>[] ipAddresses)
1919
return "-/-";
2020

21-
var result = string.Empty;
22-
23-
foreach (var ipAddr in ipAddresses)
24-
{
25-
if (!string.IsNullOrEmpty(result))
26-
result += Environment.NewLine;
27-
28-
result += ipAddr.Item1 + "/" + Subnetmask.ConvertSubnetmaskToCidr(ipAddr.Item2);
29-
}
30-
31-
return result;
21+
return IPv4Address.ConvertIPAddressWithSubnetmaskListToString(ipAddresses);
3222
}
3323

3424
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Xml.Linq;
7+
using NETworkManager.Models.Network;
8+
using Newtonsoft.Json;
9+
10+
namespace NETworkManager.Models.Export;
11+
12+
public static partial class ExportManager
13+
{
14+
/// <summary>
15+
/// Method to export objects from type <see cref="BitCaluclatorInfo" /> to a file.
16+
/// </summary>
17+
/// <param name="filePath">Path to the export file.</param>
18+
/// <param name="fileType">Allowed <see cref="ExportFileType" /> are CSV, XML or JSON.</param>
19+
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
20+
public static void Export(string filePath, ExportFileType fileType, IReadOnlyList<BitCaluclatorInfo> collection)
21+
{
22+
switch (fileType)
23+
{
24+
case ExportFileType.Csv:
25+
CreateCsv(collection, filePath);
26+
break;
27+
case ExportFileType.Xml:
28+
CreateXml(collection, filePath);
29+
break;
30+
case ExportFileType.Json:
31+
CreateJson(collection, filePath);
32+
break;
33+
case ExportFileType.Txt:
34+
default:
35+
throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Creates a CSV file from the given <see cref="BitCaluclatorInfo" /> collection.
41+
/// </summary>
42+
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
43+
/// <param name="filePath">Path to the export file.</param>
44+
private static void CreateCsv(IEnumerable<BitCaluclatorInfo> collection, string filePath)
45+
{
46+
var stringBuilder = new StringBuilder();
47+
48+
stringBuilder.AppendLine(
49+
$"{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)}");
50+
51+
foreach (var info in collection)
52+
stringBuilder.AppendLine(
53+
$"{info.Bits},{info.Bytes},{info.Kilobits},{info.Kilobytes},{info.Megabits},{info.Megabytes},{info.Gigabits},{info.Gigabytes},{info.Terabits},{info.Terabytes},{info.Petabits},{info.Petabytes}");
54+
55+
File.WriteAllText(filePath, stringBuilder.ToString());
56+
}
57+
58+
/// <summary>
59+
/// Creates a XML file from the given <see cref="BitCaluclatorInfo" /> collection.
60+
/// </summary>
61+
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
62+
/// <param name="filePath">Path to the export file.</param>
63+
private static void CreateXml(IEnumerable<BitCaluclatorInfo> collection, string filePath)
64+
{
65+
var document = new XDocument(DefaultXDeclaration,
66+
new XElement(ApplicationName.BitCalculator.ToString(),
67+
new XElement(nameof(BitCaluclatorInfo) + "s",
68+
from info in collection
69+
select
70+
new XElement(nameof(BitCaluclatorInfo),
71+
new XElement(nameof(BitCaluclatorInfo.Bits), info.Bits),
72+
new XElement(nameof(BitCaluclatorInfo.Bytes), info.Bytes),
73+
new XElement(nameof(BitCaluclatorInfo.Kilobits), info.Kilobits),
74+
new XElement(nameof(BitCaluclatorInfo.Kilobytes), info.Kilobytes),
75+
new XElement(nameof(BitCaluclatorInfo.Megabits), info.Megabits),
76+
new XElement(nameof(BitCaluclatorInfo.Megabytes), info.Megabytes),
77+
new XElement(nameof(BitCaluclatorInfo.Gigabits), info.Gigabits),
78+
new XElement(nameof(BitCaluclatorInfo.Gigabytes), info.Gigabytes),
79+
new XElement(nameof(BitCaluclatorInfo.Terabits), info.Terabits),
80+
new XElement(nameof(BitCaluclatorInfo.Terabytes), info.Terabytes),
81+
new XElement(nameof(BitCaluclatorInfo.Petabits), info.Petabits),
82+
new XElement(nameof(BitCaluclatorInfo.Petabytes), info.Petabytes)))));
83+
84+
document.Save(filePath);
85+
}
86+
87+
/// <summary>
88+
/// Creates a JSON file from the given <see cref="BitCaluclatorInfo" /> collection.
89+
/// </summary>
90+
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param>
91+
/// <param name="filePath">Path to the export file.</param>
92+
private static void CreateJson(IReadOnlyList<BitCaluclatorInfo> collection, string filePath)
93+
{
94+
var jsonData = new object[collection.Count];
95+
96+
for (var i = 0; i < collection.Count; i++)
97+
jsonData[i] = new
98+
{
99+
collection[i].Bits,
100+
collection[i].Bytes,
101+
collection[i].Kilobits,
102+
collection[i].Kilobytes,
103+
collection[i].Megabits,
104+
collection[i].Megabytes,
105+
collection[i].Gigabits,
106+
collection[i].Gigabytes,
107+
collection[i].Terabits,
108+
collection[i].Terabytes,
109+
collection[i].Petabits,
110+
collection[i].Petabytes
111+
};
112+
113+
File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented));
114+
}
115+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Xml.Linq;
7+
using NETworkManager.Models.Network;
8+
using Newtonsoft.Json;
9+
10+
namespace NETworkManager.Models.Export;
11+
12+
public static partial class ExportManager
13+
{
14+
/// <summary>
15+
/// Method to export objects from type <see cref="DiscoveryProtocolPackageInfo" /> to a file.
16+
/// </summary>
17+
/// <param name="filePath">Path to the export file.</param>
18+
/// <param name="fileType">Allowed <see cref="ExportFileType" /> are CSV, XML or JSON.</param>
19+
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
20+
public static void Export(string filePath, ExportFileType fileType,
21+
IReadOnlyList<DiscoveryProtocolPackageInfo> collection)
22+
{
23+
switch (fileType)
24+
{
25+
case ExportFileType.Csv:
26+
CreateCsv(collection, filePath);
27+
break;
28+
case ExportFileType.Xml:
29+
CreateXml(collection, filePath);
30+
break;
31+
case ExportFileType.Json:
32+
CreateJson(collection, filePath);
33+
break;
34+
case ExportFileType.Txt:
35+
default:
36+
throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Creates a CSV file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
42+
/// </summary>
43+
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
44+
/// <param name="filePath">Path to the export file.</param>
45+
private static void CreateCsv(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath)
46+
{
47+
var stringBuilder = new StringBuilder();
48+
49+
50+
stringBuilder.AppendLine(
51+
$"{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)}");
52+
53+
foreach (var info in collection)
54+
stringBuilder.AppendLine(
55+
$"{info.Device},{info.DeviceDescription},{info.Port},{info.PortDescription},{info.Model},{info.VLAN},{info.IPAddress},{info.Protocol},{info.TimeToLive}");
56+
57+
File.WriteAllText(filePath, stringBuilder.ToString());
58+
}
59+
60+
/// <summary>
61+
/// Creates a XML file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
62+
/// </summary>
63+
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
64+
/// <param name="filePath">Path to the export file.</param>
65+
private static void CreateXml(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath)
66+
{
67+
var document = new XDocument(DefaultXDeclaration,
68+
new XElement(ApplicationName.DiscoveryProtocol.ToString(),
69+
new XElement(nameof(DiscoveryProtocolPackageInfo) + "s",
70+
from info in collection
71+
select
72+
new XElement(nameof(DiscoveryProtocolPackageInfo),
73+
new XElement(nameof(DiscoveryProtocolPackageInfo.Device), info.Device),
74+
new XElement(nameof(DiscoveryProtocolPackageInfo.DeviceDescription),
75+
info.DeviceDescription),
76+
new XElement(nameof(DiscoveryProtocolPackageInfo.Port), info.Port),
77+
new XElement(nameof(DiscoveryProtocolPackageInfo.PortDescription), info.PortDescription),
78+
new XElement(nameof(DiscoveryProtocolPackageInfo.Model), info.Model),
79+
new XElement(nameof(DiscoveryProtocolPackageInfo.VLAN), info.VLAN),
80+
new XElement(nameof(DiscoveryProtocolPackageInfo.IPAddress), info.IPAddress),
81+
new XElement(nameof(DiscoveryProtocolPackageInfo.Protocol), info.Protocol),
82+
new XElement(nameof(DiscoveryProtocolPackageInfo.TimeToLive), info.TimeToLive)))));
83+
84+
document.Save(filePath);
85+
}
86+
87+
/// <summary>
88+
/// Creates a JSON file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection.
89+
/// </summary>
90+
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param>
91+
/// <param name="filePath">Path to the export file.</param>
92+
private static void CreateJson(IReadOnlyList<DiscoveryProtocolPackageInfo> collection, string filePath)
93+
{
94+
var jsonData = new object[collection.Count];
95+
96+
for (var i = 0; i < collection.Count; i++)
97+
jsonData[i] = new
98+
{
99+
collection[i].Device,
100+
collection[i].DeviceDescription,
101+
collection[i].Port,
102+
collection[i].PortDescription,
103+
collection[i].Model,
104+
collection[i].VLAN,
105+
collection[i].IPAddress,
106+
collection[i].Protocol,
107+
collection[i].TimeToLive
108+
};
109+
110+
File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented));
111+
}
112+
}

0 commit comments

Comments
 (0)