Skip to content

Commit 2b23233

Browse files
committed
Feature: Migrate xml to json
1 parent be5d62b commit 2b23233

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

Source/NETworkManager.Profiles/ProfileManager.cs

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using log4net;
2-
using NETworkManager.Models.Network;
32
using NETworkManager.Settings;
43
using NETworkManager.Utilities;
54
using System;
@@ -135,6 +134,36 @@ private static void LoadedProfileFileChanged(ProfileFileInfo profileFileInfo, bo
135134
OnLoadedProfileFileChangedEvent?.Invoke(null, new ProfileFileInfoArgs(profileFileInfo, profileFileUpdating));
136135
}
137136

137+
/// <summary>
138+
/// Occurs when the profile migration process begins.
139+
/// </summary>
140+
[Obsolete("Will be removed after some time, as profile migration from legacy XML files is a one-time process.")]
141+
public static event EventHandler OnProfileMigrationStarted;
142+
143+
/// <summary>
144+
/// Raises the event indicating that the profile migration process from legacy XML files has started.
145+
/// </summary>
146+
[Obsolete("Will be removed after some time, as profile migration from legacy XML files is a one-time process.")]
147+
private static void ProfileMigrationStarted()
148+
{
149+
OnProfileMigrationStarted?.Invoke(null, EventArgs.Empty);
150+
}
151+
152+
/// <summary>
153+
/// Occurs when the profile migration from legacy XML files has completed.
154+
/// </summary>
155+
[Obsolete("Will be removed after some time, as profile migration from legacy XML files is a one-time process.")]
156+
public static event EventHandler OnProfileMigrationCompleted;
157+
158+
/// <summary>
159+
/// Raises the event indicating that the profile migration from legacy XML files has completed.
160+
/// </summary>
161+
[Obsolete("Will be removed after some time, as profile migration from legacy XML files is a one-time process.")]
162+
private static void ProfileMigrationCompleted()
163+
{
164+
OnProfileMigrationCompleted?.Invoke(null, EventArgs.Empty);
165+
}
166+
138167
/// <summary>
139168
/// Event is fired if the profiles have changed.
140169
/// </summary>
@@ -507,7 +536,30 @@ private static void Load(ProfileFileInfo profileFileInfo)
507536

508537
if (IsXmlContent(decryptedBytes))
509538
{
510-
groups = new List<GroupInfo>(); // ToDo
539+
//
540+
// MIGRATION FROM LEGACY XML PROFILE FILE
541+
//
542+
543+
Log.Info($"Legacy XML profile file detected inside encrypted profile: {profileFileInfo.Path}. Migration in progress...");
544+
545+
// Load from legacy XML byte array
546+
groups = DeserializeFromXmlByteArray(decryptedBytes);
547+
548+
// Create a backup of the legacy XML file
549+
Backup(profileFileInfo.Path,
550+
GetSettingsBackupFolderLocation(),
551+
TimestampHelper.GetTimestampFilename(Path.GetFileName(profileFileInfo.Path)));
552+
553+
// Save encrypted profile file with new JSON format
554+
var newDecryptedBytes = SerializeToByteArray([.. groups]);
555+
var newEncryptedBytes = CryptoHelper.Encrypt(newDecryptedBytes,
556+
SecureStringHelper.ConvertToString(profileFileInfo.Password),
557+
GlobalStaticConfiguration.Profile_EncryptionKeySize,
558+
GlobalStaticConfiguration.Profile_EncryptionIterations);
559+
560+
File.WriteAllBytes(profileFileInfo.Path, newEncryptedBytes);
561+
562+
Log.Info($"Legacy XML profile file migration completed inside encrypted profile: {profileFileInfo.Path}.");
511563
}
512564
else
513565
{
@@ -528,6 +580,9 @@ private static void Load(ProfileFileInfo profileFileInfo)
528580

529581
if (Path.GetExtension(profileFileInfo.Path) == LegacyProfileFileExtension)
530582
{
583+
//
584+
// MIGRATION FROM LEGACY XML PROFILE FILE
585+
//
531586
Log.Info($"Legacy XML profile file detected: {profileFileInfo.Path}. Migration in progress...");
532587

533588
// Load from legacy XML file
@@ -537,22 +592,23 @@ private static void Load(ProfileFileInfo profileFileInfo)
537592

538593
LoadedProfileFile = profileFileInfo;
539594

595+
// Create a backup of the legacy XML file and delete the original
596+
Backup(profileFileInfo.Path,
597+
GetSettingsBackupFolderLocation(),
598+
TimestampHelper.GetTimestampFilename(Path.GetFileName(profileFileInfo.Path)));
599+
540600
// Create new profile file info with JSON extension
541601
var newProfileFileInfo = new ProfileFileInfo(profileFileInfo.Name,
542602
Path.ChangeExtension(profileFileInfo.Path, ProfileFileExtension));
543603

544604
// Save new JSON file
545605
SerializeToFile(newProfileFileInfo.Path, groups);
546606

547-
// Create a backup of the legacy XML file and delete the original
548-
Backup(profileFileInfo.Path,
549-
GetSettingsBackupFolderLocation(),
550-
TimestampHelper.GetTimestampFilename(Path.GetFileName(profileFileInfo.Path)));
551-
607+
// Notify migration started
608+
ProfileMigrationStarted();
609+
552610
// Add the new profile
553-
Log.Info("Adding migrated profile file to the profile files list.");
554611
ProfileFiles.Add(newProfileFileInfo);
555-
Log.Info("Migrated profile file added to the profile files list.");
556612

557613
// Switch profile
558614
Log.Info($"Switching to migrated profile file: {newProfileFileInfo.Path}.");
@@ -563,6 +619,9 @@ private static void Load(ProfileFileInfo profileFileInfo)
563619
File.Delete(profileFileInfo.Path);
564620
ProfileFiles.Remove(profileFileInfo);
565621

622+
// Notify migration completed
623+
ProfileMigrationCompleted();
624+
566625
Log.Info($"Legacy XML profile file migration completed: {profileFileInfo.Path}.");
567626
return;
568627
}

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
using System.Windows.Interop;
3434
using System.Windows.Markup;
3535
using System.Windows.Threading;
36-
using static System.Runtime.InteropServices.JavaScript.JSType;
3736
using Application = System.Windows.Application;
3837
using ContextMenu = System.Windows.Controls.ContextMenu;
3938
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
@@ -1376,6 +1375,8 @@ private void LoadProfiles()
13761375
_isProfileFilesLoading = false;
13771376

13781377
ProfileManager.OnLoadedProfileFileChangedEvent += ProfileManager_OnLoadedProfileFileChangedEvent;
1378+
ProfileManager.OnProfileMigrationStarted += ProfileManager_OnProfileMigrationStarted;
1379+
ProfileManager.OnProfileMigrationCompleted += ProfileManager_OnProfileMigrationCompleted;
13791380

13801381
SelectedProfileFile = ProfileFiles.SourceCollection.Cast<ProfileFileInfo>()
13811382
.FirstOrDefault(x => x.Name == SettingsManager.Current.Profiles_LastSelected);
@@ -1472,7 +1473,7 @@ private void OnProfilesLoaded(ApplicationName name)
14721473
/// <param name="sender"></param>
14731474
/// <param name="e"></param>
14741475
private void ProfileManager_OnLoadedProfileFileChangedEvent(object sender, ProfileFileInfoArgs e)
1475-
{
1476+
{
14761477
_isProfileFileUpdating = e.ProfileFileUpdating;
14771478

14781479
SelectedProfileFile = ProfileFiles.SourceCollection.Cast<ProfileFileInfo>()
@@ -1481,6 +1482,16 @@ private void ProfileManager_OnLoadedProfileFileChangedEvent(object sender, Profi
14811482
_isProfileFileUpdating = false;
14821483
}
14831484

1485+
private void ProfileManager_OnProfileMigrationCompleted(object sender, EventArgs e)
1486+
{
1487+
_isProfileFileUpdating = false;
1488+
}
1489+
1490+
private void ProfileManager_OnProfileMigrationStarted(object sender, EventArgs e)
1491+
{
1492+
_isProfileFileUpdating = true;
1493+
}
1494+
14841495
#endregion
14851496

14861497
#region Update check

0 commit comments

Comments
 (0)