Skip to content

Commit 5a8fe3b

Browse files
committed
save settings after each change
1 parent 5b20c55 commit 5a8fe3b

4 files changed

Lines changed: 53 additions & 57 deletions

File tree

GUI/MainForm.cs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public partial class MainForm : Form {
4848
private readonly UserOption logSensors;
4949
private readonly UserRadioGroup loggingInterval;
5050
private readonly Logger logger;
51-
private readonly string configFilePath;
5251

5352
private bool selectionDragging = false;
5453
//private readonly Timer checkUpdatesTimer;
@@ -61,8 +60,7 @@ public MainForm() {
6160

6261
settings = new PersistentSettings();
6362

64-
configFilePath = Path.ChangeExtension(Application.ExecutablePath, ".config");
65-
settings.Load(configFilePath);
63+
settings.Load();
6664

6765
unitManager = new UnitManager(settings);
6866

@@ -283,7 +281,6 @@ public MainForm() {
283281
// Make sure the settings are saved when the user logs off
284282
Microsoft.Win32.SystemEvents.SessionEnded += delegate {
285283
computer.Close();
286-
SaveConfiguration();
287284
if (runWebServer.Value)
288285
server.Quit();
289286
};
@@ -311,8 +308,7 @@ private void InsertSorted(Collection<Node> nodes, HardwareNode node) {
311308
}
312309

313310
private void SubHardwareAdded(IHardware hardware, Node node) {
314-
HardwareNode hardwareNode =
315-
new HardwareNode(hardware, settings, unitManager);
311+
var hardwareNode = new HardwareNode(hardware, settings, unitManager);
316312

317313
InsertSorted(node.Nodes, hardwareNode);
318314

@@ -378,27 +374,6 @@ private void timer_Tick(object sender, EventArgs e) {
378374
delayCount++;
379375
}
380376

381-
private void SaveConfiguration() {
382-
if (settings == null)
383-
return;
384-
385-
if (server != null) {
386-
settings.SetValue("listenerPort", server.ListenerPort);
387-
}
388-
389-
try {
390-
settings.Save(configFilePath);
391-
} catch (UnauthorizedAccessException) {
392-
MessageBox.Show("Access to the path '" + configFilePath + "' is denied. " +
393-
"The current settings could not be saved.",
394-
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
395-
} catch (IOException) {
396-
MessageBox.Show("The path '" + configFilePath + "' is not writeable. " +
397-
"The current settings could not be saved.",
398-
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
399-
}
400-
}
401-
402377
private void RestoreBoundsFromSettings() {
403378
Rectangle newBounds = new Rectangle {
404379
X = settings.GetValue("mainForm.Location.X", Location.X),
@@ -434,7 +409,6 @@ private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
434409
systemTray.IsMainIconEnabled = false;
435410
timer.Enabled = false;
436411
computer.Close();
437-
SaveConfiguration();
438412
if (runWebServer.Value)
439413
server.Quit();
440414
systemTray.Dispose();
@@ -693,7 +667,9 @@ private void treeView_MouseUp(object sender, MouseEventArgs e) {
693667
}
694668

695669
private void serverPortMenuItem_Click(object sender, EventArgs e) {
696-
new PortForm(this).ShowDialog();
670+
if (new PortForm(this.server).ShowDialog() == DialogResult.OK) {
671+
settings.SetValue("listenerPort", server.ListenerPort);
672+
}
697673
}
698674

699675
public HttpServer Server {

GUI/PortForm.Designer.cs

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/PortForm.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33
using System.Net;
44
using System.Net.Sockets;
55
using System.Diagnostics;
6+
using OpenHardwareMonitor.Utilities;
67

78
namespace OpenHardwareMonitor.GUI {
89
public partial class PortForm : Form {
9-
private MainForm parent;
10-
private string localIP;
11-
public PortForm(MainForm m) {
12-
InitializeComponent();
13-
parent = m;
10+
private readonly HttpServer server;
11+
private readonly string localIP;
1412

13+
public PortForm(HttpServer server) {
14+
InitializeComponent();
15+
this.server = server;
1516
localIP = getLocalIP();
1617
}
1718

18-
private void portTextBox_TextChanged(object sender, EventArgs e) {
19-
20-
}
21-
2219
private string getLocalIP() {
2320
IPHostEntry host;
24-
string localIP = "?";
21+
var localIP = "?";
2522
host = Dns.GetHostEntry(Dns.GetHostName());
26-
foreach (IPAddress ip in host.AddressList) {
23+
foreach (var ip in host.AddressList) {
2724
if (ip.AddressFamily == AddressFamily.InterNetwork) {
2825
localIP = ip.ToString();
2926
}
@@ -32,23 +29,18 @@ private string getLocalIP() {
3229
}
3330

3431
private void portNumericUpDn_ValueChanged(object sender, EventArgs e) {
35-
string url = "http://" + localIP + ":" + portNumericUpDn.Value + "/";
32+
var url = "http://" + localIP + ":" + portNumericUpDn.Value + "/";
3633
webServerLinkLabel.Text = url;
3734
webServerLinkLabel.Links.Remove(webServerLinkLabel.Links[0]);
3835
webServerLinkLabel.Links.Add(0, webServerLinkLabel.Text.Length, url);
3936
}
4037

4138
private void portOKButton_Click(object sender, EventArgs e) {
42-
parent.Server.ListenerPort = (int)portNumericUpDn.Value;
43-
this.Close();
44-
}
45-
46-
private void portCancelButton_Click(object sender, EventArgs e) {
47-
this.Close();
39+
server.ListenerPort = (int)portNumericUpDn.Value;
4840
}
4941

5042
private void PortForm_Load(object sender, EventArgs e) {
51-
portNumericUpDn.Value = parent.Server.ListenerPort;
43+
portNumericUpDn.Value = server.ListenerPort;
5244
portNumericUpDn_ValueChanged(null, null);
5345
}
5446

Utilities/PersistentSettings.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
using System.Drawing;
44
using System.Globalization;
55
using System.IO;
6+
using System.Windows.Forms;
67
using Microsoft.Win32;
78
using OpenHardwareMonitor.Hardware;
89

910
namespace OpenHardwareMonitor {
1011

1112
public class PersistentSettings : ISettings {
1213

14+
private readonly string configFilePath;
15+
16+
public PersistentSettings() {
17+
configFilePath = Path.ChangeExtension(Application.ExecutablePath, ".config");
18+
}
19+
1320
private IDictionary<string, string> settings = new Dictionary<string, string>();
1421

15-
public void Load(string fileName) {
22+
public void Load() {
1623

1724
//old versions configs compatibility
18-
if (File.Exists(fileName)) {
25+
if (File.Exists(configFilePath)) {
1926
try {
20-
var json = File.ReadAllText(fileName);
27+
var json = File.ReadAllText(configFilePath);
2128
settings = json.FromJson<IDictionary<string, string>>();
2229
return;
2330
}
@@ -35,7 +42,7 @@ public void Load(string fileName) {
3542
}
3643
}
3744

38-
public void Save(string fileName) {
45+
public void Save() {
3946

4047
try {
4148
//remove prev settings
@@ -48,16 +55,30 @@ public void Save(string fileName) {
4855
}
4956
}
5057

51-
if (File.Exists(fileName)) {
58+
if (File.Exists(configFilePath)) {
5259
try {
53-
File.Delete(fileName);
60+
File.Delete(configFilePath);
5461
} catch (Exception) {
5562
//ignore
5663
}
5764
}
5865
} catch (Exception) {
5966
//save old-style config
60-
settings.ToJsonFile(fileName);
67+
SaveToFile();
68+
}
69+
}
70+
71+
public void SaveToFile() {
72+
try {
73+
settings.ToJsonFile(configFilePath);
74+
} catch (UnauthorizedAccessException) {
75+
MessageBox.Show("Access to the path '" + configFilePath + "' is denied. " +
76+
"The current settings could not be saved.",
77+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
78+
} catch (IOException) {
79+
MessageBox.Show("The path '" + configFilePath + "' is not writeable. " +
80+
"The current settings could not be saved.",
81+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
6182
}
6283
}
6384

@@ -67,6 +88,7 @@ public bool Contains(string name) {
6788

6889
public void SetValue(string name, string value) {
6990
settings[name] = value;
91+
Save();
7092
}
7193

7294
public string GetValue(string name, string defaultValue) {
@@ -75,10 +97,12 @@ public string GetValue(string name, string defaultValue) {
7597

7698
public void Remove(string name) {
7799
settings.Remove(name);
100+
Save();
78101
}
79102

80103
public void SetValue(string name, int value) {
81104
settings[name] = value.ToString();
105+
Save();
82106
}
83107

84108
public int GetValue(string name, int defaultValue) {
@@ -89,6 +113,7 @@ public int GetValue(string name, int defaultValue) {
89113

90114
public void SetValue(string name, float value) {
91115
settings[name] = value.ToString(CultureInfo.InvariantCulture);
116+
Save();
92117
}
93118

94119
public float GetValue(string name, float defaultValue) {
@@ -100,6 +125,7 @@ public float GetValue(string name, float defaultValue) {
100125

101126
public void SetValue(string name, bool value) {
102127
settings[name] = value ? "true" : "false";
128+
Save();
103129
}
104130

105131
public bool GetValue(string name, bool defaultValue) {
@@ -110,6 +136,7 @@ public bool GetValue(string name, bool defaultValue) {
110136

111137
public void SetValue(string name, Color color) {
112138
settings[name] = color.ToArgb().ToString("X8");
139+
Save();
113140
}
114141

115142
public Color GetValue(string name, Color defaultValue) {

0 commit comments

Comments
 (0)