Skip to content

Commit 6cdf525

Browse files
committed
Chore: Handle user settings location
1 parent 712a05d commit 6cdf525

File tree

6 files changed

+298
-73
lines changed

6 files changed

+298
-73
lines changed

Source/NETworkManager.Profiles/ProfileManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ private static void Load(ProfileFileInfo profileFileInfo)
616616
{
617617
var loadedProfileUpdated = false;
618618

619-
Log.Info($"Load profile file: {profileFileInfo.Path}");
620-
621619
if (File.Exists(profileFileInfo.Path))
622620
{
621+
Log.Info($"Loading profile file from: {profileFileInfo.Path}");
622+
623623
// Encrypted profile file
624624
if (profileFileInfo.IsEncrypted)
625625
{
@@ -734,6 +734,8 @@ private static void Load(ProfileFileInfo profileFileInfo)
734734

735735
// Notify subscribers that profiles have been loaded/updated
736736
ProfilesUpdated(false);
737+
738+
Log.Info("Profile file loaded successfully.");
737739
}
738740

739741
/// <summary>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
3+
using System.Text.Json.Serialization;
4+
5+
namespace NETworkManager.Settings;
6+
7+
/// <summary>
8+
/// Class contains local settings that are stored outside the main settings file.
9+
/// These settings control where the main settings file is located.
10+
/// </summary>
11+
public class LocalSettingsInfo
12+
{
13+
/// <summary>
14+
/// Occurs when a property value changes.
15+
/// </summary>
16+
/// <remarks>This event is typically used to notify subscribers that a property value has been updated. It
17+
/// is commonly implemented in classes that support data binding or need to signal changes to property
18+
/// values.</remarks>
19+
public event PropertyChangedEventHandler PropertyChanged;
20+
21+
/// <summary>
22+
/// Helper method to raise the <see cref="PropertyChanged" /> event.
23+
/// </summary>
24+
/// <param name="propertyName">Name of the property that changed.</param>
25+
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
26+
{
27+
SettingsChanged = true;
28+
29+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
30+
}
31+
32+
#region Variables
33+
34+
[JsonIgnore] public bool SettingsChanged { get; set; }
35+
36+
/// <summary>
37+
/// Private field for the <see cref="SettingsFolderLocation" /> property."
38+
/// </summary>
39+
private string _settingsFolderLocation;
40+
41+
/// <summary>
42+
/// Location of the folder where the local settings file is stored.
43+
/// This can be changed by the user to move the settings file to a different location.
44+
/// </summary>
45+
public string SettingsFolderLocation
46+
{
47+
get => _settingsFolderLocation;
48+
set
49+
{
50+
if (_settingsFolderLocation == value)
51+
return;
52+
53+
_settingsFolderLocation = value;
54+
OnPropertyChanged();
55+
}
56+
}
57+
#endregion
58+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using log4net;
2+
using Microsoft.Xaml.Behaviors.Media;
3+
using System;
4+
using System.IO;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace NETworkManager.Settings;
9+
10+
/// <summary>
11+
/// Manages local application settings that are stored outside the main settings file.
12+
/// This is used for settings that control where the main settings file is located.
13+
/// </summary>
14+
public static class LocalSettingsManager
15+
{
16+
#region Variables
17+
18+
/// <summary>
19+
/// Logger for logging.
20+
/// </summary>
21+
private static readonly ILog Log = LogManager.GetLogger(typeof(LocalSettingsManager));
22+
23+
/// <summary>
24+
/// Settings file name.
25+
/// </summary>
26+
private static string SettingsFileName => "Settings.json";
27+
28+
/// <summary>
29+
/// Settings that are currently loaded.
30+
/// </summary>
31+
public static LocalSettingsInfo Current { get; private set; }
32+
33+
/// <summary>
34+
/// JSON serializer options for consistent serialization/deserialization.
35+
/// </summary>
36+
private static readonly JsonSerializerOptions JsonOptions = new()
37+
{
38+
WriteIndented = true,
39+
PropertyNameCaseInsensitive = true,
40+
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
41+
Converters = { new JsonStringEnumConverter() }
42+
};
43+
#endregion
44+
45+
#region Methods
46+
47+
/// <summary>
48+
/// Method to get the path of the settings folder.
49+
/// </summary>
50+
/// <returns>Path to the settings folder.</returns>
51+
private static string GetSettingsFolderLocation()
52+
{
53+
return Path.Combine(
54+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
55+
AssemblyManager.Current.Name);
56+
}
57+
58+
/// <summary>
59+
/// Method to get the settings file path
60+
/// </summary>
61+
/// <returns>Settings file path.</returns>
62+
private static string GetSettingsFilePath()
63+
{
64+
return Path.Combine(
65+
GetSettingsFolderLocation(),
66+
SettingsFileName);
67+
}
68+
69+
/// <summary>
70+
/// Initialize new settings (<see cref="SettingsInfo" />) and save them (to a file).
71+
/// </summary>
72+
private static void Initialize()
73+
{
74+
Log.Info("Initializing new local settings.");
75+
76+
Current = new LocalSettingsInfo();
77+
78+
Save();
79+
}
80+
81+
/// <summary>
82+
/// Method to load the settings from a file.
83+
/// </summary>
84+
public static void Load()
85+
{
86+
var filePath = GetSettingsFilePath();
87+
88+
if (File.Exists(filePath))
89+
{
90+
Log.Info($"Loading local settings from: {filePath}");
91+
92+
var jsonString = File.ReadAllText(filePath);
93+
Current = JsonSerializer.Deserialize<LocalSettingsInfo>(jsonString, JsonOptions);
94+
95+
Log.Info("Local settings loaded successfully.");
96+
97+
return;
98+
}
99+
100+
Initialize();
101+
}
102+
103+
/// <summary>
104+
/// Method to save the current settings to a file.
105+
/// </summary>
106+
public static void Save()
107+
{
108+
// Create the directory if it does not exist
109+
Directory.CreateDirectory(GetSettingsFolderLocation());
110+
111+
// Serialize to file
112+
var filePath = GetSettingsFilePath();
113+
114+
var jsonString = JsonSerializer.Serialize(Current, JsonOptions);
115+
File.WriteAllText(filePath, jsonString);
116+
117+
Log.Info($"Local settings saved to {filePath}");
118+
119+
// Reset change tracking
120+
Current.SettingsChanged = false;
121+
}
122+
#endregion
123+
}

Source/NETworkManager.Settings/SettingsInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919

2020
namespace NETworkManager.Settings;
2121

22+
/// <summary>
23+
/// Represents the application settings, user preferences, and configuration data for all supported features and
24+
/// modules. Supports property change notification for data binding and persistence scenarios.
25+
/// </summary>
26+
/// <remarks>The <see cref="SettingsInfo" /> class provides a centralized container for storing and managing user-configurable
27+
/// options, operational parameters, and history collections for various application modules, such as network tools,
28+
/// remote access, and calculators. It implements the INotifyPropertyChanged interface to enable data binding and
29+
/// automatic UI updates when settings change. Most properties raise the PropertyChanged event when modified, allowing
30+
/// consumers to track changes and persist settings as needed. This class is typically used as the main settings model
31+
/// in applications that require user customization and state management across sessions.</remarks>
2232
public class SettingsInfo : INotifyPropertyChanged
2333
{
2434
/// <summary>

0 commit comments

Comments
 (0)