Skip to content

Commit f550c81

Browse files
BornToBeRootCopilotCopilot
authored
Feature: Daily settings backup (BornToBeRoot#3283)
* Feature: Create a daily settings backup * Chore: Cleanup * Docs: BornToBeRoot#3283 * Chore: Cleanup * Fix: Fixes based on copilot feedback * Use filename timestamp for backup ordering and move to Utilities (BornToBeRoot#3284) * Initial plan * Extract date from filename for backup ordering Co-authored-by: BornToBeRoot <[email protected]> * Use InvariantCulture and specific exception handling Co-authored-by: BornToBeRoot <[email protected]> * Move ExtractTimestampFromFilename to TimestampHelper in Utilities Co-authored-by: BornToBeRoot <[email protected]> * Update comment to match specific exception handling Co-authored-by: BornToBeRoot <[email protected]> * Feature: Improve check * Update Source/NETworkManager.Utilities/TimestampHelper.cs Co-authored-by: Copilot <[email protected]> * Update Source/NETworkManager.Utilities/TimestampHelper.cs Co-authored-by: Copilot <[email protected]> * Update Source/NETworkManager.Utilities/TimestampHelper.cs Co-authored-by: Copilot <[email protected]> * Update TimestampHelper.cs * Update TimestampHelper.cs --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BornToBeRoot <[email protected]> Co-authored-by: Copilot <[email protected]> * Feature: Some logging / docs --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent a4c7122 commit f550c81

File tree

9 files changed

+230
-63
lines changed

9 files changed

+230
-63
lines changed

Source/NETworkManager.Profiles/ProfileManager.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using NETworkManager.Settings;
1+
using log4net;
2+
using NETworkManager.Models.Network;
3+
using NETworkManager.Settings;
24
using NETworkManager.Utilities;
35
using System;
46
using System.Collections.Generic;
@@ -13,19 +15,8 @@ namespace NETworkManager.Profiles;
1315

1416
public static class ProfileManager
1517
{
16-
#region Constructor
17-
18-
/// <summary>
19-
/// Static constructor. Load all profile files on startup.
20-
/// </summary>
21-
static ProfileManager()
22-
{
23-
LoadProfileFiles();
24-
}
25-
26-
#endregion
27-
2818
#region Variables
19+
private static readonly ILog Log = LogManager.GetLogger(typeof(ProfileManager));
2920

3021
/// <summary>
3122
/// Profiles directory name.
@@ -84,6 +75,18 @@ private set
8475

8576
#endregion
8677

78+
#region Constructor
79+
80+
/// <summary>
81+
/// Static constructor. Load all profile files on startup.
82+
/// </summary>
83+
static ProfileManager()
84+
{
85+
LoadProfileFiles();
86+
}
87+
88+
#endregion
89+
8790
#region Events
8891

8992
/// <summary>
@@ -202,7 +205,7 @@ public static void CreateEmptyProfileFile(string profileName)
202205

203206
Directory.CreateDirectory(GetProfilesFolderLocation());
204207

205-
SerializeToFile(profileFileInfo.Path, new List<GroupInfo>());
208+
SerializeToFile(profileFileInfo.Path, []);
206209

207210
ProfileFiles.Add(profileFileInfo);
208211
}
@@ -219,7 +222,6 @@ public static void RenameProfileFile(ProfileFileInfo profileFileInfo, string new
219222
if (LoadedProfileFile != null && LoadedProfileFile.Equals(profileFileInfo))
220223
{
221224
Save();
222-
223225
switchProfile = true;
224226
}
225227

@@ -472,7 +474,12 @@ private static void Load(ProfileFileInfo profileFileInfo)
472474
public static void Save()
473475
{
474476
if (LoadedProfileFile == null)
477+
{
478+
Log.Warn("Cannot save profiles because no profile file is loaded. The profile file may be encrypted and not yet unlocked.");
479+
475480
return;
481+
}
482+
476483

477484
Directory.CreateDirectory(GetProfilesFolderLocation());
478485

Source/NETworkManager.Settings/GlobalStaticConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public static class GlobalStaticConfiguration
4646
public static string ZipFileExtensionFilter => "ZIP Archive (*.zip)|*.zip";
4747
public static string XmlFileExtensionFilter => "XML-File (*.xml)|*.xml";
4848

49+
// Backup settings
50+
public static int Backup_MaximumNumberOfBackups => 10;
51+
4952
#endregion
5053

5154
#region Default settings

Source/NETworkManager.Settings/SettingsInfo.cs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ public string Version
109109
}
110110
}
111111

112+
/// <summary>
113+
/// Private field for the <see cref="LastBackup" /> property.
114+
/// </summary>
115+
private DateTime _lastBackup = DateTime.MinValue;
116+
117+
/// <summary>
118+
/// Stores the date of the last backup of the settings file.
119+
/// </summary>
120+
public DateTime LastBackup
121+
{
122+
get => _lastBackup;
123+
set
124+
{
125+
if (value == _lastBackup)
126+
return;
127+
128+
_lastBackup = value;
129+
OnPropertyChanged();
130+
}
131+
}
132+
112133
#region General
113134

114135
// General
@@ -1216,7 +1237,7 @@ public ExportFileType IPScanner_ExportFileType
12161237

12171238
#region Port Scanner
12181239

1219-
private ObservableCollection<string> _portScanner_HostHistory = new();
1240+
private ObservableCollection<string> _portScanner_HostHistory = [];
12201241

12211242
public ObservableCollection<string> PortScanner_HostHistory
12221243
{
@@ -1231,7 +1252,7 @@ public ObservableCollection<string> PortScanner_HostHistory
12311252
}
12321253
}
12331254

1234-
private ObservableCollection<string> _portScanner_PortHistory = new();
1255+
private ObservableCollection<string> _portScanner_PortHistory = [];
12351256

12361257
public ObservableCollection<string> PortScanner_PortHistory
12371258
{
@@ -1246,7 +1267,7 @@ public ObservableCollection<string> PortScanner_PortHistory
12461267
}
12471268
}
12481269

1249-
private ObservableCollection<PortProfileInfo> _portScanner_PortProfiles = new();
1270+
private ObservableCollection<PortProfileInfo> _portScanner_PortProfiles = [];
12501271

12511272
public ObservableCollection<PortProfileInfo> PortScanner_PortProfiles
12521273
{
@@ -1400,7 +1421,7 @@ public ExportFileType PortScanner_ExportFileType
14001421

14011422
#region Ping Monitor
14021423

1403-
private ObservableCollection<string> _pingMonitor_HostHistory = new();
1424+
private ObservableCollection<string> _pingMonitor_HostHistory = [];
14041425

14051426
public ObservableCollection<string> PingMonitor_HostHistory
14061427
{
@@ -1569,7 +1590,7 @@ public double PingMonitor_ProfileWidth
15691590

15701591
#region Traceroute
15711592

1572-
private ObservableCollection<string> _traceroute_HostHistory = new();
1593+
private ObservableCollection<string> _traceroute_HostHistory = [];
15731594

15741595
public ObservableCollection<string> Traceroute_HostHistory
15751596
{
@@ -1999,7 +2020,7 @@ public ExportFileType DNSLookup_ExportFileType
19992020

20002021
#region Remote Desktop
20012022

2002-
private ObservableCollection<string> _remoteDesktop_HostHistory = new();
2023+
private ObservableCollection<string> _remoteDesktop_HostHistory = [];
20032024

20042025
public ObservableCollection<string> RemoteDesktop_HostHistory
20052026
{
@@ -2579,7 +2600,7 @@ public double RemoteDesktop_ProfileWidth
25792600

25802601
#region PowerShell
25812602

2582-
private ObservableCollection<string> _powerShell_HostHistory = new();
2603+
private ObservableCollection<string> _powerShell_HostHistory = [];
25832604

25842605
public ObservableCollection<string> PowerShell_HostHistory
25852606
{
@@ -2689,7 +2710,7 @@ public double PowerShell_ProfileWidth
26892710

26902711
#region PuTTY
26912712

2692-
private ObservableCollection<string> _puTTY_HostHistory = new();
2713+
private ObservableCollection<string> _puTTY_HostHistory = [];
26932714

26942715
public ObservableCollection<string> PuTTY_HostHistory
26952716
{
@@ -2839,7 +2860,7 @@ public string PuTTY_AdditionalCommandLine
28392860
}
28402861
}
28412862

2842-
private ObservableCollection<string> _puTTY_SerialLineHistory = new();
2863+
private ObservableCollection<string> _puTTY_SerialLineHistory = [];
28432864

28442865
public ObservableCollection<string> PuTTY_SerialLineHistory
28452866
{
@@ -2854,7 +2875,7 @@ public ObservableCollection<string> PuTTY_SerialLineHistory
28542875
}
28552876
}
28562877

2857-
private ObservableCollection<string> _puTTY_PortHistory = new();
2878+
private ObservableCollection<string> _puTTY_PortHistory = [];
28582879

28592880
public ObservableCollection<string> PuTTY_PortHistory
28602881
{
@@ -2869,7 +2890,7 @@ public ObservableCollection<string> PuTTY_PortHistory
28692890
}
28702891
}
28712892

2872-
private ObservableCollection<string> _puTTY_BaudHistory = new();
2893+
private ObservableCollection<string> _puTTY_BaudHistory = [];
28732894

28742895
public ObservableCollection<string> PuTTY_BaudHistory
28752896
{
@@ -2884,7 +2905,7 @@ public ObservableCollection<string> PuTTY_BaudHistory
28842905
}
28852906
}
28862907

2887-
private ObservableCollection<string> _puTTY_UsernameHistory = new();
2908+
private ObservableCollection<string> _puTTY_UsernameHistory = [];
28882909

28892910
public ObservableCollection<string> PuTTY_UsernameHistory
28902911
{
@@ -2899,7 +2920,7 @@ public ObservableCollection<string> PuTTY_UsernameHistory
28992920
}
29002921
}
29012922

2902-
private ObservableCollection<string> _puTTY_PrivateKeyFileHistory = new();
2923+
private ObservableCollection<string> _puTTY_PrivateKeyFileHistory = [];
29032924

29042925
public ObservableCollection<string> PuTTY_PrivateKeyFileHistory
29052926
{
@@ -2914,7 +2935,7 @@ public ObservableCollection<string> PuTTY_PrivateKeyFileHistory
29142935
}
29152936
}
29162937

2917-
private ObservableCollection<string> _puTTY_ProfileHistory = new();
2938+
private ObservableCollection<string> _puTTY_ProfileHistory = [];
29182939

29192940
public ObservableCollection<string> PuTTY_ProfileHistory
29202941
{
@@ -3069,7 +3090,7 @@ public int PuTTY_RawPort
30693090

30703091
#region TigerVNC
30713092

3072-
private ObservableCollection<string> _tigerVNC_HostHistory = new();
3093+
private ObservableCollection<string> _tigerVNC_HostHistory = [];
30733094

30743095
public ObservableCollection<string> TigerVNC_HostHistory
30753096
{
@@ -3084,7 +3105,7 @@ public ObservableCollection<string> TigerVNC_HostHistory
30843105
}
30853106
}
30863107

3087-
private ObservableCollection<int> _tigerVNC_PortHistory = new();
3108+
private ObservableCollection<int> _tigerVNC_PortHistory = [];
30883109

30893110
public ObservableCollection<int> TigerVNC_PortHistory
30903111
{
@@ -3163,7 +3184,7 @@ public int TigerVNC_Port
31633184

31643185
#region Web Console
31653186

3166-
private ObservableCollection<string> _webConsole_UrlHistory = new();
3187+
private ObservableCollection<string> _webConsole_UrlHistory = [];
31673188

31683189
public ObservableCollection<string> WebConsole_UrlHistory
31693190
{
@@ -3257,7 +3278,7 @@ public bool WebConsole_IsPasswordSaveEnabled
32573278

32583279
#region SNMP
32593280

3260-
private ObservableCollection<string> _snmp_HostHistory = new();
3281+
private ObservableCollection<string> _snmp_HostHistory = [];
32613282

32623283
public ObservableCollection<string> SNMP_HostHistory
32633284
{
@@ -3272,7 +3293,7 @@ public ObservableCollection<string> SNMP_HostHistory
32723293
}
32733294
}
32743295

3275-
private ObservableCollection<string> _snmp_OidHistory = new();
3296+
private ObservableCollection<string> _snmp_OidHistory = [];
32763297

32773298
public ObservableCollection<string> SNMP_OidHistory
32783299
{
@@ -3287,7 +3308,7 @@ public ObservableCollection<string> SNMP_OidHistory
32873308
}
32883309
}
32893310

3290-
private ObservableCollection<SNMPOIDProfileInfo> _snmp_OidProfiles = new();
3311+
private ObservableCollection<SNMPOIDProfileInfo> _snmp_OidProfiles = [];
32913312

32923313
public ObservableCollection<SNMPOIDProfileInfo> SNMP_OidProfiles
32933314
{
@@ -3681,7 +3702,7 @@ public int WakeOnLAN_Port
36813702
}
36823703
}
36833704

3684-
private ObservableCollection<string> _wakeOnLan_MACAddressHistory = new();
3705+
private ObservableCollection<string> _wakeOnLan_MACAddressHistory = [];
36853706

36863707
public ObservableCollection<string> WakeOnLan_MACAddressHistory
36873708
{
@@ -3696,7 +3717,7 @@ public ObservableCollection<string> WakeOnLan_MACAddressHistory
36963717
}
36973718
}
36983719

3699-
private ObservableCollection<string> _wakeOnLan_BroadcastHistory = new();
3720+
private ObservableCollection<string> _wakeOnLan_BroadcastHistory = [];
37003721

37013722
public ObservableCollection<string> WakeOnLan_BroadcastHistory
37023723
{
@@ -3745,7 +3766,7 @@ public double WakeOnLAN_ProfileWidth
37453766

37463767
#region Whois
37473768

3748-
private ObservableCollection<string> _whois_DomainHistory = new();
3769+
private ObservableCollection<string> _whois_DomainHistory = [];
37493770

37503771
public ObservableCollection<string> Whois_DomainHistory
37513772
{
@@ -3824,7 +3845,7 @@ public ExportFileType Whois_ExportFileType
38243845

38253846
#region IP Geolocation
38263847

3827-
private ObservableCollection<string> _ipGeolocation_HostHistory = new();
3848+
private ObservableCollection<string> _ipGeolocation_HostHistory = [];
38283849

38293850
public ObservableCollection<string> IPGeolocation_HostHistory
38303851
{
@@ -3905,7 +3926,7 @@ public ExportFileType IPGeolocation_ExportFileType
39053926

39063927
#region Calculator
39073928

3908-
private ObservableCollection<string> _subnetCalculator_Calculator_SubnetHistory = new();
3929+
private ObservableCollection<string> _subnetCalculator_Calculator_SubnetHistory = [];
39093930

39103931
public ObservableCollection<string> SubnetCalculator_Calculator_SubnetHistory
39113932
{
@@ -3924,7 +3945,7 @@ public ObservableCollection<string> SubnetCalculator_Calculator_SubnetHistory
39243945

39253946
#region Subnetting
39263947

3927-
private ObservableCollection<string> _subnetCalculator_Subnetting_SubnetHistory = new();
3948+
private ObservableCollection<string> _subnetCalculator_Subnetting_SubnetHistory = [];
39283949

39293950
public ObservableCollection<string> SubnetCalculator_Subnetting_SubnetHistory
39303951
{
@@ -3939,7 +3960,7 @@ public ObservableCollection<string> SubnetCalculator_Subnetting_SubnetHistory
39393960
}
39403961
}
39413962

3942-
private ObservableCollection<string> _subnetCalculator_Subnetting_NewSubnetmaskHistory = new();
3963+
private ObservableCollection<string> _subnetCalculator_Subnetting_NewSubnetmaskHistory = [];
39433964

39443965
public ObservableCollection<string> SubnetCalculator_Subnetting_NewSubnetmaskHistory
39453966
{
@@ -3989,7 +4010,7 @@ public ExportFileType SubnetCalculator_Subnetting_ExportFileType
39894010

39904011
#region WideSubnet
39914012

3992-
private ObservableCollection<string> _subnetCalculator_WideSubnet_Subnet1 = new();
4013+
private ObservableCollection<string> _subnetCalculator_WideSubnet_Subnet1 = [];
39934014

39944015
public ObservableCollection<string> SubnetCalculator_WideSubnet_Subnet1
39954016
{
@@ -4004,7 +4025,7 @@ public ObservableCollection<string> SubnetCalculator_WideSubnet_Subnet1
40044025
}
40054026
}
40064027

4007-
private ObservableCollection<string> _subnetCalculator_WideSubnet_Subnet2 = new();
4028+
private ObservableCollection<string> _subnetCalculator_WideSubnet_Subnet2 = [];
40084029

40094030
public ObservableCollection<string> SubnetCalculator_WideSubnet_Subnet2
40104031
{
@@ -4025,7 +4046,7 @@ public ObservableCollection<string> SubnetCalculator_WideSubnet_Subnet2
40254046

40264047
#region Bit Calculator
40274048

4028-
private ObservableCollection<string> _bitCalculator_InputHistory = new();
4049+
private ObservableCollection<string> _bitCalculator_InputHistory = [];
40294050

40304051
public ObservableCollection<string> BitCalculator_InputHistory
40314052
{

0 commit comments

Comments
 (0)