From a49919717eeff851a9be90ad5f3b2c04453c66e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Thu, 6 May 2010 19:20:38 +0000 Subject: [PATCH] Added an Identifier class for IHardware, ISensor and IParameter Identifier properties. --- GUI/SensorNotifyIcon.cs | 6 ++- GUI/SensorSystemTray.cs | 11 ++-- Hardware/ATI/ATIGPU.cs | 7 +-- Hardware/CPU/AMD0FCPU.cs | 4 +- Hardware/CPU/AMD10CPU.cs | 4 +- Hardware/CPU/IntelCPU.cs | 4 +- Hardware/HDD/HDD.cs | 4 +- Hardware/IHardware.cs | 2 +- Hardware/IParameter.cs | 2 +- Hardware/ISensor.cs | 5 +- Hardware/Identifier.cs | 96 +++++++++++++++++++++++++++++++++ Hardware/LPC/LPCHardware.cs | 4 +- Hardware/Mainboard/Mainboard.cs | 4 +- Hardware/Nvidia/NvidiaGPU.cs | 6 +-- Hardware/Parameter.cs | 15 +++--- Hardware/Sensor.cs | 41 ++++++++------ Hardware/TBalancer/TBalancer.cs | 6 +-- OpenHardwareMonitor.csproj | 2 + Utilities/ListSet.cs | 70 ++++++++++++++++++++++++ 19 files changed, 241 insertions(+), 52 deletions(-) create mode 100644 Hardware/Identifier.cs create mode 100644 Utilities/ListSet.cs diff --git a/GUI/SensorNotifyIcon.cs b/GUI/SensorNotifyIcon.cs index 0c7c5e2..580c204 100644 --- a/GUI/SensorNotifyIcon.cs +++ b/GUI/SensorNotifyIcon.cs @@ -71,7 +71,8 @@ namespace OpenHardwareMonitor.GUI { if (sensor.SensorType == SensorType.Load) { defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1); } - Color = Config.Get(sensor.Identifier + "/traycolor", defaultColor); + Color = Config.Get(new Identifier(sensor.Identifier, + "traycolor").ToString(), defaultColor); this.pen = new Pen(Color.FromArgb(96, Color.Black)); this.font = new Font(SystemFonts.MessageBoxFont.FontFamily, 9); @@ -88,7 +89,8 @@ namespace OpenHardwareMonitor.GUI { dialog.Color = Color; if (dialog.ShowDialog() == DialogResult.OK) { Color = dialog.Color; - Config.Set(sensor.Identifier + "/traycolor", Color); + Config.Set(new Identifier(sensor.Identifier, + "traycolor").ToString(), Color); } }; contextMenuStrip.Items.Add(colorItem); diff --git a/GUI/SensorSystemTray.cs b/GUI/SensorSystemTray.cs index a4594b3..59260cf 100644 --- a/GUI/SensorSystemTray.cs +++ b/GUI/SensorSystemTray.cs @@ -73,7 +73,8 @@ namespace OpenHardwareMonitor.GUI { } private void SensorAdded(ISensor sensor) { - if (Config.Get(sensor.Identifier + "/tray", false)) + if (Config.Get(new Identifier(sensor.Identifier, + "tray").ToString(), false)) Add(sensor, false); } @@ -104,7 +105,7 @@ namespace OpenHardwareMonitor.GUI { return; } else { list.Add(new SensorNotifyIcon(this, sensor, balloonTip)); - Config.Set(sensor.Identifier + "/tray", true); + Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true); } } @@ -114,8 +115,10 @@ namespace OpenHardwareMonitor.GUI { private void Remove(ISensor sensor, bool deleteConfig) { if (deleteConfig) { - Config.Remove(sensor.Identifier + "/tray"); - Config.Remove(sensor.Identifier + "/traycolor"); + Config.Remove( + new Identifier(sensor.Identifier, "tray").ToString()); + Config.Remove( + new Identifier(sensor.Identifier, "traycolor").ToString()); } SensorNotifyIcon instance = null; foreach (SensorNotifyIcon icon in list) diff --git a/Hardware/ATI/ATIGPU.cs b/Hardware/ATI/ATIGPU.cs index 1144058..ed59a77 100644 --- a/Hardware/ATI/ATIGPU.cs +++ b/Hardware/ATI/ATIGPU.cs @@ -69,7 +69,8 @@ namespace OpenHardwareMonitor.Hardware.ATI { this.temperature = new Sensor("GPU Core", 0, SensorType.Temperature, this); - this.fan = new Sensor("GPU", 0, speedInfo.MaxRPM, SensorType.Fan, this); + this.fan = new Sensor("GPU", 0, speedInfo.MaxRPM, SensorType.Fan, this, + null); this.coreClock = new Sensor("GPU Core", 0, SensorType.Clock, this); this.memoryClock = new Sensor("GPU Memory", 1, SensorType.Clock, this); this.coreVoltage = new Sensor("GPU Core", 0, SensorType.Voltage, this); @@ -85,8 +86,8 @@ namespace OpenHardwareMonitor.Hardware.ATI { get { return name; } } - public string Identifier { - get { return "/atigpu/" + adapterIndex; } + public Identifier Identifier { + get { return new Identifier("atigpu", adapterIndex.ToString()); } } public Image Icon { diff --git a/Hardware/CPU/AMD0FCPU.cs b/Hardware/CPU/AMD0FCPU.cs index 480722a..0f66d8e 100644 --- a/Hardware/CPU/AMD0FCPU.cs +++ b/Hardware/CPU/AMD0FCPU.cs @@ -121,8 +121,8 @@ namespace OpenHardwareMonitor.Hardware.CPU { get { return name; } } - public string Identifier { - get { return "/amdcpu/" + processorIndex; } + public Identifier Identifier { + get { return new Identifier("amdcpu", processorIndex.ToString()); } } public Image Icon { diff --git a/Hardware/CPU/AMD10CPU.cs b/Hardware/CPU/AMD10CPU.cs index 0f181fc..6d34119 100644 --- a/Hardware/CPU/AMD10CPU.cs +++ b/Hardware/CPU/AMD10CPU.cs @@ -103,8 +103,8 @@ namespace OpenHardwareMonitor.Hardware.CPU { get { return name; } } - public string Identifier { - get { return "/amdcpu/" + processorIndex; } + public Identifier Identifier { + get { return new Identifier("amdcpu", processorIndex.ToString()); } } public Image Icon { diff --git a/Hardware/CPU/IntelCPU.cs b/Hardware/CPU/IntelCPU.cs index 4239eff..36362a7 100644 --- a/Hardware/CPU/IntelCPU.cs +++ b/Hardware/CPU/IntelCPU.cs @@ -237,8 +237,8 @@ namespace OpenHardwareMonitor.Hardware.CPU { get { return name; } } - public string Identifier { - get { return "/intelcpu/" + processorIndex; } + public Identifier Identifier { + get { return new Identifier("intelcpu", processorIndex.ToString()); } } public Image Icon { diff --git a/Hardware/HDD/HDD.cs b/Hardware/HDD/HDD.cs index e5f6167..7cc51f2 100644 --- a/Hardware/HDD/HDD.cs +++ b/Hardware/HDD/HDD.cs @@ -69,8 +69,8 @@ namespace OpenHardwareMonitor.Hardware.HDD { get { return name; } } - public string Identifier { - get { return "/hdd/" + drive; } + public Identifier Identifier { + get { return new Identifier("hdd", drive.ToString()); } } public Image Icon { diff --git a/Hardware/IHardware.cs b/Hardware/IHardware.cs index 61c3221..009c72f 100644 --- a/Hardware/IHardware.cs +++ b/Hardware/IHardware.cs @@ -46,7 +46,7 @@ namespace OpenHardwareMonitor.Hardware { public interface IHardware { string Name { get; } - string Identifier { get; } + Identifier Identifier { get; } Image Icon { get; } diff --git a/Hardware/IParameter.cs b/Hardware/IParameter.cs index 8b84f65..6710e22 100644 --- a/Hardware/IParameter.cs +++ b/Hardware/IParameter.cs @@ -43,7 +43,7 @@ namespace OpenHardwareMonitor.Hardware { public interface IParameter { ISensor Sensor { get; } - string Identifier { get; } + Identifier Identifier { get; } string Name { get; } string Description { get; } diff --git a/Hardware/ISensor.cs b/Hardware/ISensor.cs index 62d3bed..d665285 100644 --- a/Hardware/ISensor.cs +++ b/Hardware/ISensor.cs @@ -60,10 +60,13 @@ namespace OpenHardwareMonitor.Hardware { IHardware Hardware { get; } SensorType SensorType { get; } - string Identifier { get; } + Identifier Identifier { get; } + string Name { get; set; } int Index { get; } + bool IsDefaultHidden { get; } + IReadOnlyArray Parameters { get; } float? Value { get; } diff --git a/Hardware/Identifier.cs b/Hardware/Identifier.cs new file mode 100644 index 0000000..e6b7513 --- /dev/null +++ b/Hardware/Identifier.cs @@ -0,0 +1,96 @@ +/* + + Version: MPL 1.1/GPL 2.0/LGPL 2.1 + + The contents of this file are subject to the Mozilla Public License Version + 1.1 (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" basis, + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + for the specific language governing rights and limitations under the License. + + The Original Code is the Open Hardware Monitor code. + + The Initial Developer of the Original Code is + Michael Möller . + Portions created by the Initial Developer are Copyright (C) 2009-2010 + the Initial Developer. All Rights Reserved. + + Contributor(s): + + Alternatively, the contents of this file may be used under the terms of + either the GNU General Public License Version 2 or later (the "GPL"), or + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + in which case the provisions of the GPL or the LGPL are applicable instead + of those above. If you wish to allow use of your version of this file only + under the terms of either the GPL or the LGPL, and not to allow others to + use your version of this file under the terms of the MPL, indicate your + decision by deleting the provisions above and replace them with the notice + and other provisions required by the GPL or the LGPL. If you do not delete + the provisions above, a recipient may use your version of this file under + the terms of any one of the MPL, the GPL or the LGPL. + +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenHardwareMonitor.Hardware { + public class Identifier { + private string identifier; + + private static char SEPARATOR = '/'; + + private void CheckIdentifiers(string[] identifiers) { + foreach (string s in identifiers) + if (s.Contains(" ") || s.Contains(SEPARATOR.ToString())) + throw new ArgumentException("Invalid identifier"); + } + + public Identifier(params string[] identifiers) { + CheckIdentifiers(identifiers); + + StringBuilder s = new StringBuilder(); + for (int i = 0; i < identifiers.Length; i++) { + s.Append(SEPARATOR); + s.Append(identifiers[i]); + } + this.identifier = s.ToString(); + } + + public Identifier(Identifier identifier, params string[] extensions) { + CheckIdentifiers(extensions); + + StringBuilder s = new StringBuilder(); + s.Append(identifier.ToString()); + for (int i = 0; i < extensions.Length; i++) { + s.Append(SEPARATOR); + s.Append(extensions[i]); + } + this.identifier = s.ToString(); + } + + public override string ToString() { + return identifier; + } + + public override bool Equals(System.Object obj) { + if (obj == null) + return false; + + Identifier id = obj as Identifier; + if (id == null) + return false; + + return (identifier == id.identifier); + } + + public override int GetHashCode() { + return identifier.GetHashCode(); + } + } +} diff --git a/Hardware/LPC/LPCHardware.cs b/Hardware/LPC/LPCHardware.cs index d93f3e2..a057196 100644 --- a/Hardware/LPC/LPCHardware.cs +++ b/Hardware/LPC/LPCHardware.cs @@ -73,8 +73,8 @@ namespace OpenHardwareMonitor.Hardware.LPC { } } - public string Identifier { - get { return "/lpc/" + chip.ToString().ToLower(); } + public Identifier Identifier { + get { return new Identifier("lpc", chip.ToString().ToLower()); } } public Image Icon { diff --git a/Hardware/Mainboard/Mainboard.cs b/Hardware/Mainboard/Mainboard.cs index 4e13379..c8ad650 100644 --- a/Hardware/Mainboard/Mainboard.cs +++ b/Hardware/Mainboard/Mainboard.cs @@ -75,8 +75,8 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { get { return name; } } - public string Identifier { - get { return "/mainboard"; } + public Identifier Identifier { + get { return new Identifier("mainboard"); } } public Image Icon { diff --git a/Hardware/Nvidia/NvidiaGPU.cs b/Hardware/Nvidia/NvidiaGPU.cs index cf49f14..6334c70 100644 --- a/Hardware/Nvidia/NvidiaGPU.cs +++ b/Hardware/Nvidia/NvidiaGPU.cs @@ -75,7 +75,7 @@ namespace OpenHardwareMonitor.Hardware.Nvidia { default: name = "GPU"; break; } temperatures[i] = new Sensor(name, i, sensor.DefaultMaxTemp, - SensorType.Temperature, this); + SensorType.Temperature, this, new ParameterDescription[0]); ActivateSensor(temperatures[i]); } @@ -93,8 +93,8 @@ namespace OpenHardwareMonitor.Hardware.Nvidia { get { return name; } } - public string Identifier { - get { return "/nvidiagpu/" + adapterIndex; } + public Identifier Identifier { + get { return new Identifier("nvidiagpu", adapterIndex.ToString()); } } public Image Icon { diff --git a/Hardware/Parameter.cs b/Hardware/Parameter.cs index 4902e77..784f56c 100644 --- a/Hardware/Parameter.cs +++ b/Hardware/Parameter.cs @@ -68,8 +68,9 @@ namespace OpenHardwareMonitor.Hardware { public Parameter(ParameterDescription description, ISensor sensor) { this.sensor = sensor; this.description = description; - this.value = Utilities.Config.Get(Identifier, description.DefaultValue); - this.isDefault = !Utilities.Config.Contains(Identifier); + this.value = Utilities.Config.Get(Identifier.ToString(), + description.DefaultValue); + this.isDefault = !Utilities.Config.Contains(Identifier.ToString()); } public ISensor Sensor { @@ -78,10 +79,10 @@ namespace OpenHardwareMonitor.Hardware { } } - public string Identifier { + public Identifier Identifier { get { - return sensor.Identifier + "/parameter/" + - Name.Replace(" ", "").ToLower(); + return new Identifier(sensor.Identifier, "parameter", + Name.Replace(" ", "").ToLower()); } } @@ -96,7 +97,7 @@ namespace OpenHardwareMonitor.Hardware { set { this.isDefault = false; this.value = value; - Utilities.Config.Set(Identifier, value); + Utilities.Config.Set(Identifier.ToString(), value); } } @@ -110,7 +111,7 @@ namespace OpenHardwareMonitor.Hardware { this.isDefault = value; if (value) { this.value = description.DefaultValue; - Utilities.Config.Remove(Identifier); + Utilities.Config.Remove(Identifier.ToString()); } } } diff --git a/Hardware/Sensor.cs b/Hardware/Sensor.cs index 4a577b2..f2d8fb0 100644 --- a/Hardware/Sensor.cs +++ b/Hardware/Sensor.cs @@ -46,6 +46,7 @@ namespace OpenHardwareMonitor.Hardware { private string defaultName; private string name; private int index; + private bool defaultHidden; private SensorType sensorType; private IHardware hardware; private ReadOnlyArray parameters; @@ -64,31 +65,37 @@ namespace OpenHardwareMonitor.Hardware { public Sensor(string name, int index, SensorType sensorType, IHardware hardware) : this(name, index, null, sensorType, hardware, - new ParameterDescription[0]) { } + null) { } - public Sensor(string name, int index, float? limit, - SensorType sensorType, IHardware hardware) : this(name, index, limit, - sensorType, hardware, new ParameterDescription[0]) { } + public Sensor(string name, int index, float? limit, SensorType sensorType, + IHardware hardware, ParameterDescription[] parameterDescriptions) : + this(name, index, false, limit, sensorType, hardware, + parameterDescriptions) { } - public Sensor(string name, int index, float? limit, SensorType sensorType, - IHardware hardware, ParameterDescription[] parameterDescriptions) + public Sensor(string name, int index, bool defaultHidden, + float? limit, SensorType sensorType, IHardware hardware, + ParameterDescription[] parameterDescriptions) { this.defaultName = name; this.index = index; + this.defaultHidden = defaultHidden; this.defaultLimit = limit; this.sensorType = sensorType; this.hardware = hardware; - Parameter[] parameters = new Parameter[parameterDescriptions.Length]; + Parameter[] parameters = new Parameter[parameterDescriptions == null ? + 0 : parameterDescriptions.Length]; for (int i = 0; i < parameters.Length; i++ ) parameters[i] = new Parameter(parameterDescriptions[i], this); this.parameters = parameters; - string configName = Config.Settings[Identifier + "/name"]; + string configName = Config.Settings[ + new Identifier(Identifier, "name").ToString()]; if (configName != null) this.name = configName; else this.name = name; - string configLimit = Config.Settings[Identifier + "/limit"]; + string configLimit = Config.Settings[ + new Identifier(Identifier, "limit").ToString()]; if (configLimit != null && configLimit != "") this.limit = float.Parse(configLimit); else @@ -103,10 +110,10 @@ namespace OpenHardwareMonitor.Hardware { get { return sensorType; } } - public string Identifier { + public Identifier Identifier { get { - return hardware.Identifier + "/" + sensorType.ToString().ToLower() + - "/" + index; + return new Identifier(hardware.Identifier, + sensorType.ToString().ToLower(), index.ToString()); } } @@ -119,7 +126,7 @@ namespace OpenHardwareMonitor.Hardware { name = value; else name = defaultName; - Config.Settings[Identifier + "/name"] = name; + Config.Settings[new Identifier(Identifier, "name").ToString()] = name; } } @@ -127,6 +134,10 @@ namespace OpenHardwareMonitor.Hardware { get { return index; } } + public bool IsDefaultHidden { + get { return defaultHidden; } + } + public IReadOnlyArray Parameters { get { return parameters; } } @@ -169,11 +180,11 @@ namespace OpenHardwareMonitor.Hardware { set { if (value.HasValue) { limit = value; - Config.Settings[Identifier + "/limit"] = + Config.Settings[new Identifier(Identifier, "limit").ToString()] = limit.ToString(); } else { limit = defaultLimit; - Config.Settings[Identifier + "/limit"] = ""; + Config.Settings[new Identifier(Identifier, "limit").ToString()] = ""; } } } diff --git a/Hardware/TBalancer/TBalancer.cs b/Hardware/TBalancer/TBalancer.cs index fa0cf54..c0d2665 100644 --- a/Hardware/TBalancer/TBalancer.cs +++ b/Hardware/TBalancer/TBalancer.cs @@ -153,7 +153,7 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { if (miniNGFans[number * 2 + i] == null) miniNGFans[number * 2 + i] = new Sensor("miniNG #" + (number + 1) + " Fan Channel " + (i + 1), - 4 + number * 2 + i, maxRPM, SensorType.Fan, this); + 4 + number * 2 + i, maxRPM, SensorType.Fan, this, null); Sensor sensor = miniNGFans[number * 2 + i]; @@ -251,8 +251,8 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { get { return "T-Balancer bigNG"; } } - public string Identifier { - get { return "/bigng/" + this.portIndex; } + public Identifier Identifier { + get { return new Identifier("bigng", this.portIndex.ToString()); } } public IHardware[] SubHardware { diff --git a/OpenHardwareMonitor.csproj b/OpenHardwareMonitor.csproj index 188e918..5c7bba0 100644 --- a/OpenHardwareMonitor.csproj +++ b/OpenHardwareMonitor.csproj @@ -85,6 +85,7 @@ + @@ -127,6 +128,7 @@ + UserControl diff --git a/Utilities/ListSet.cs b/Utilities/ListSet.cs new file mode 100644 index 0000000..484ea97 --- /dev/null +++ b/Utilities/ListSet.cs @@ -0,0 +1,70 @@ +/* + + Version: MPL 1.1/GPL 2.0/LGPL 2.1 + + The contents of this file are subject to the Mozilla Public License Version + 1.1 (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" basis, + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + for the specific language governing rights and limitations under the License. + + The Original Code is the Open Hardware Monitor code. + + The Initial Developer of the Original Code is + Michael Möller . + Portions created by the Initial Developer are Copyright (C) 2009-2010 + the Initial Developer. All Rights Reserved. + + Contributor(s): + + Alternatively, the contents of this file may be used under the terms of + either the GNU General Public License Version 2 or later (the "GPL"), or + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + in which case the provisions of the GPL or the LGPL are applicable instead + of those above. If you wish to allow use of your version of this file only + under the terms of either the GPL or the LGPL, and not to allow others to + use your version of this file under the terms of the MPL, indicate your + decision by deleting the provisions above and replace them with the notice + and other provisions required by the GPL or the LGPL. If you do not delete + the provisions above, a recipient may use your version of this file under + the terms of any one of the MPL, the GPL or the LGPL. + +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenHardwareMonitor.Utilities { + public class ListSet { + + private List list = new List(); + + public ListSet() { } + + public bool Add(T item) { + if (list.Contains(item)) + return false; + + list.Add(item); + return true; + } + + public bool Remove(T item) { + if (!list.Contains(item)) + return false; + + list.Remove(item); + return true; + } + + public bool Contains(T item) { + return list.Contains(item); + } + + } +}