-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppSettings.cs
196 lines (159 loc) · 5.88 KB
/
AppSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.Win32;
using Newtonsoft.Json;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using MQTTnet.Protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Controls;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace PC2MQTT
{
public class AppSettings
{
#region Private Fields
// Lock object for thread-safe initialization
private static readonly object _lock = new object();
private static readonly string _settingsFilePath;
// Static variable for the singleton instance
private static AppSettings _instance;
private string _mqttPassword; // Store the encrypted version internally
private string _teamsToken; // Store the encrypted version internally
#endregion Private Fields
#region Public Constructors
// Static constructor to set up file path
static AppSettings()
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var appDataFolder = Path.Combine(localAppData, "PC2MQTT");
Directory.CreateDirectory(appDataFolder);
_settingsFilePath = Path.Combine(appDataFolder, "settings.json");
}
#endregion Public Constructors
#region Private Constructors
// Private constructor to prevent direct instantiation
private AppSettings()
{
LoadSettingsFromFile();
}
#endregion Private Constructors
#region Public Properties
public bool HasShownOneTimeNotice { get; set; } = false;
// Public property to access the singleton instance
public static AppSettings Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new AppSettings();
}
return _instance;
}
}
}
public static string ExecutablePath => System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// Properties
public string EncryptedMqttPassword
{
get => _mqttPassword;
set => _mqttPassword = value; // Only for deserialization
}
public string EncryptedTeamsToken
{
get => _teamsToken;
set => _teamsToken = value; // Only for deserialization
}
public bool IgnoreCertificateErrors { get; set; }
public string MqttAddress { get; set; }
[JsonIgnore]
public string MqttPassword
{
get => CryptoHelper.DecryptString(_mqttPassword);
set => _mqttPassword = CryptoHelper.EncryptString(value);
}
public string MqttPort { get; set; }
public string MqttUsername { get; set; }
public bool UseSafeCommands { get; set; }
public bool RunAtWindowsBoot { get; set; }
public bool RunMinimized { get; set; }
public string SensorPrefix { get; set; }
public string Theme { get; set; }
public bool UseTLS { get; set; }
public bool UseWebsockets { get; set; }
#endregion Public Properties
#region Public Methods
// Save settings to file
public void SaveSettingsToFile()
{
// Encrypt sensitive data
if (!String.IsNullOrEmpty(this.MqttPassword))
{
this.EncryptedMqttPassword = CryptoHelper.EncryptString(this.MqttPassword);
}
else
{
this.EncryptedMqttPassword = "";
}
if (string.IsNullOrEmpty(this.SensorPrefix))
{
this.SensorPrefix = System.Environment.MachineName;
}
// newcode
const string appName = "PC2MQTT"; // Your application's name
string exePath = ExecutablePath;
// Open the registry key for the current user's startup programs
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true))
{
if (this.RunAtWindowsBoot)
{
// Set the application to start with Windows startup by adding a registry value
key.SetValue(appName, exePath);
}
else
{
// Remove the registry value to prevent the application from starting with
// Windows startup
key.DeleteValue(appName, false);
}
}
Log.Debug("SetStartupAsync: Startup options set");
// Serialize and save
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(_settingsFilePath, json);
}
#endregion Public Methods
#region Private Methods
// Load settings from file
private void LoadSettingsFromFile()
{
if (File.Exists(_settingsFilePath))
{
string json = File.ReadAllText(_settingsFilePath);
JsonConvert.PopulateObject(json, this);
// Decrypt sensitive data
if (!String.IsNullOrEmpty(this.EncryptedMqttPassword))
{
this.MqttPassword = CryptoHelper.DecryptString(this.EncryptedMqttPassword);
}
if (string.IsNullOrEmpty(this.MqttPort))
{
this.MqttPort = "1883"; // Default MQTT port
}
}
else
{
this.MqttPort = "1883"; // Default MQTT port
}
}
#endregion Private Methods
}
}