Skip to content

Commit a8853bc

Browse files
CopilotBornToBeRoot
andcommitted
Add system-wide config feature to disable update checks
Co-authored-by: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com>
1 parent 702f681 commit a8853bc

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace NETworkManager.Settings;
4+
5+
/// <summary>
6+
/// Class that represents system-wide configuration that overrides user settings.
7+
/// This configuration is loaded from a config.json file in the application directory.
8+
/// </summary>
9+
public class ConfigInfo
10+
{
11+
/// <summary>
12+
/// Disable update check for all users. When set to true, the application will not check for updates at startup.
13+
/// This overrides the user's "Update_CheckForUpdatesAtStartup" setting.
14+
/// </summary>
15+
[JsonPropertyName("Update_DisableUpdateCheck")]
16+
public bool? Update_DisableUpdateCheck { get; set; }
17+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using log4net;
2+
using System;
3+
using System.IO;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace NETworkManager.Settings;
8+
9+
/// <summary>
10+
/// Manager for system-wide configuration that is loaded from a config.json file
11+
/// in the application directory. This configuration overrides user settings.
12+
/// </summary>
13+
public static class ConfigManager
14+
{
15+
#region Variables
16+
17+
/// <summary>
18+
/// Logger for logging.
19+
/// </summary>
20+
private static readonly ILog Log = LogManager.GetLogger(typeof(ConfigManager));
21+
22+
/// <summary>
23+
/// Config file name.
24+
/// </summary>
25+
private static string ConfigFileName => "config.json";
26+
27+
/// <summary>
28+
/// System-wide configuration that is currently loaded.
29+
/// </summary>
30+
public static ConfigInfo Current { get; private set; }
31+
32+
/// <summary>
33+
/// JSON serializer options for consistent serialization/deserialization.
34+
/// </summary>
35+
private static readonly JsonSerializerOptions JsonOptions = new()
36+
{
37+
WriteIndented = true,
38+
PropertyNameCaseInsensitive = true,
39+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
40+
Converters = { new JsonStringEnumConverter() }
41+
};
42+
43+
#endregion
44+
45+
#region Methods
46+
47+
/// <summary>
48+
/// Method to get the config file path in the application directory.
49+
/// </summary>
50+
/// <returns>Config file path.</returns>
51+
private static string GetConfigFilePath()
52+
{
53+
return Path.Combine(AssemblyManager.Current.Location, ConfigFileName);
54+
}
55+
56+
/// <summary>
57+
/// Method to load the system-wide configuration from config.json file in the application directory.
58+
/// </summary>
59+
public static void Load()
60+
{
61+
var filePath = GetConfigFilePath();
62+
63+
// Check if config file exists
64+
if (File.Exists(filePath))
65+
{
66+
try
67+
{
68+
Log.Info($"Loading system-wide configuration from: {filePath}");
69+
70+
var jsonString = File.ReadAllText(filePath);
71+
Current = JsonSerializer.Deserialize<ConfigInfo>(jsonString, JsonOptions);
72+
73+
Log.Info("System-wide configuration loaded successfully.");
74+
75+
// Log enabled settings
76+
if (Current.Update_DisableUpdateCheck.HasValue)
77+
{
78+
Log.Info($"System-wide setting - Update_DisableUpdateCheck: {Current.Update_DisableUpdateCheck.Value}");
79+
}
80+
}
81+
catch (Exception ex)
82+
{
83+
Log.Error($"Failed to load system-wide configuration from: {filePath}", ex);
84+
Current = new ConfigInfo();
85+
}
86+
}
87+
else
88+
{
89+
Log.Debug($"No system-wide configuration file found at: {filePath}");
90+
Current = new ConfigInfo();
91+
}
92+
}
93+
94+
#endregion
95+
}

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ public static class SettingsManager
5656
/// </summary>
5757
public static bool HotKeysChanged { get; set; }
5858

59+
/// <summary>
60+
/// Gets whether update check should be performed at startup.
61+
/// This respects the system-wide configuration (config.json) which takes precedence over user settings.
62+
/// </summary>
63+
public static bool ShouldCheckForUpdatesAtStartup
64+
{
65+
get
66+
{
67+
// System-wide config takes precedence - if it explicitly disables updates, honor it
68+
if (ConfigManager.Current?.Update_DisableUpdateCheck == true)
69+
{
70+
return false;
71+
}
72+
73+
// Otherwise, use the user's setting
74+
return Current.Update_CheckForUpdatesAtStartup;
75+
}
76+
}
77+
5978
/// <summary>
6079
/// JSON serializer options for consistent serialization/deserialization.
6180
/// </summary>
@@ -152,6 +171,9 @@ public static void Initialize()
152171
/// </summary>
153172
public static void Load()
154173
{
174+
// Load system-wide configuration first (from app directory)
175+
ConfigManager.Load();
176+
155177
var filePath = GetSettingsFilePath();
156178
var legacyFilePath = GetLegacySettingsFilePath();
157179

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private void Load()
561561
NetworkChange.NetworkAddressChanged += (_, _) => OnNetworkHasChanged();
562562

563563
// Search for updates...
564-
if (SettingsManager.Current.Update_CheckForUpdatesAtStartup)
564+
if (SettingsManager.ShouldCheckForUpdatesAtStartup)
565565
CheckForUpdates();
566566
}
567567

0 commit comments

Comments
 (0)