Zzz – Diff between revs 4 and 5
?pathlinks?
Rev 4 | Rev 5 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.ComponentModel; |
3 | using System.ComponentModel; |
|
4 | using System.Configuration; |
4 | using System.Configuration; |
|
5 | using System.Diagnostics; |
5 | using System.Diagnostics; |
|
6 | using System.Drawing; |
6 | using System.Drawing; |
|
7 | using System.IO; |
7 | using System.IO; |
|
8 | using System.Runtime; |
8 | using System.Runtime; |
|
9 | using System.Threading; |
9 | using System.Threading; |
|
10 | using System.Threading.Tasks; |
10 | using System.Threading.Tasks; |
|
11 | using System.Windows.Forms; |
11 | using System.Windows.Forms; |
|
12 | using Configuration; |
12 | using Configuration; |
|
13 | using MQTTnet.Client.Subscribing; |
- | ||
14 | using NetSparkleUpdater.Enums; |
13 | using NetSparkleUpdater.Enums; |
|
15 | using NetSparkleUpdater.SignatureVerifiers; |
14 | using NetSparkleUpdater.SignatureVerifiers; |
|
16 | using NetSparkleUpdater.UI.WinForms; |
15 | using NetSparkleUpdater.UI.WinForms; |
|
17 | using NetSparkleUpdater; |
16 | using NetSparkleUpdater; |
|
18 | using Serilog; |
17 | using Serilog; |
|
19 | using Zzz.Action; |
18 | using Zzz.Action; |
|
20 | using Zzz.Clients; |
19 | using Zzz.Clients; |
|
21 | using Zzz.Idle; |
20 | using Zzz.Idle; |
|
22 | using Zzz.Properties; |
21 | using Zzz.Properties; |
|
23 | using Zzz.Utilities; |
22 | using Zzz.Utilities; |
|
24 | using Zzz.Utilities.Serialization; |
23 | using Zzz.Utilities.Serialization; |
|
25 | using System.Reflection; |
24 | using System.Reflection; |
|
26 | using System.Net; |
25 | using System.Net; |
|
- | 26 | using MQTTnet.Client; |
||
- | 27 | using MqttClient = Zzz.Clients.MqttClient; |
||
27 | |
28 | |
|
28 | namespace Zzz |
29 | namespace Zzz |
|
29 | { |
30 | { |
|
30 | public partial class MainForm : Form |
31 | public partial class MainForm : Form |
|
31 | { |
32 | { |
|
32 | #region Public Enums, Properties and Fields |
33 | #region Public Enums, Properties and Fields |
|
33 | |
34 | |
|
34 | public bool MemorySinkEnabled { get; set; } |
35 | public bool MemorySinkEnabled { get; set; } |
|
35 | |
36 | |
|
36 | public Configuration.Configuration Configuration { get; set; } |
37 | public Configuration.Configuration Configuration { get; set; } |
|
37 | |
38 | |
|
38 | public ScheduledContinuation ChangedConfigurationContinuation { get; set; } |
39 | public ScheduledContinuation ChangedConfigurationContinuation { get; set; } |
|
39 | |
40 | |
|
40 | #endregion |
41 | #endregion |
|
41 | |
42 | |
|
42 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
43 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
43 | |
44 | |
|
44 | private Idler _defaultIdler; |
45 | private Idler _defaultIdler; |
|
45 | |
46 | |
|
46 | private MqttClient _mqttClient; |
47 | private MqttClient _mqttClient; |
|
47 | |
48 | |
|
48 | private readonly ActionTrigger _trigger; |
49 | private readonly ActionTrigger _trigger; |
|
49 | |
50 | |
|
50 | private AboutForm _aboutForm; |
51 | private AboutForm _aboutForm; |
|
51 | |
52 | |
|
52 | private SettingsForm _settingsForm; |
53 | private SettingsForm _settingsForm; |
|
53 | |
54 | |
|
54 | private Idler _hibernateIdler; |
55 | private Idler _hibernateIdler; |
|
55 | |
56 | |
|
56 | private LogMemorySink _memorySink; |
57 | private LogMemorySink _memorySink; |
|
57 | |
58 | |
|
58 | private readonly object _memorySinkLock; |
59 | private readonly object _memorySinkLock; |
|
59 | |
60 | |
|
60 | private LogViewForm _logViewForm; |
61 | private LogViewForm _logViewForm; |
|
61 | |
62 | |
|
62 | private SparkleUpdater _sparkle; |
63 | private SparkleUpdater _sparkle; |
|
63 | |
64 | |
|
64 | private readonly CancellationToken _cancellationToken; |
65 | private readonly CancellationToken _cancellationToken; |
|
65 | |
66 | |
|
66 | private readonly CancellationTokenSource _cancellationTokenSource; |
67 | private readonly CancellationTokenSource _cancellationTokenSource; |
|
67 | |
68 | |
|
68 | #endregion |
69 | #endregion |
|
69 | |
70 | |
|
70 | #region Constructors, Destructors and Finalizers |
71 | #region Constructors, Destructors and Finalizers |
|
71 | |
72 | |
|
72 | public MainForm(Mutex mutex) : this() |
73 | public MainForm(Mutex mutex) : this() |
|
73 | { |
74 | { |
|
74 | InitializeComponent(); |
75 | InitializeComponent(); |
|
75 | _memorySink = new LogMemorySink(); |
76 | _memorySink = new LogMemorySink(); |
|
76 | _memorySinkLock = new object(); |
77 | _memorySinkLock = new object(); |
|
77 | |
78 | |
|
78 | lock (_memorySinkLock) |
79 | lock (_memorySinkLock) |
|
79 | { |
80 | { |
|
80 | Log.Logger = new LoggerConfiguration() |
81 | Log.Logger = new LoggerConfiguration() |
|
81 | .MinimumLevel.Debug() |
82 | .MinimumLevel.Debug() |
|
82 | .WriteTo.Conditional(condition => MemorySinkEnabled, |
83 | .WriteTo.Conditional(condition => MemorySinkEnabled, |
|
83 | configureSink => configureSink.Sink(_memorySink)) |
84 | configureSink => configureSink.Sink(_memorySink)) |
|
84 | .WriteTo.File( |
85 | .WriteTo.File( |
|
85 | Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
86 | Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
|
86 | rollingInterval: RollingInterval.Day) |
87 | rollingInterval: RollingInterval.Day) |
|
87 | .CreateLogger(); |
88 | .CreateLogger(); |
|
88 | } |
89 | } |
|
89 | |
90 | |
|
90 | // Initialize the sleeper. |
91 | // Initialize the sleeper. |
|
91 | _trigger = new ActionTrigger(Handle); |
92 | _trigger = new ActionTrigger(Handle); |
|
92 | _trigger.Action += Trigger_Action; |
93 | _trigger.Action += Trigger_Action; |
|
93 | |
94 | |
|
94 | // Start application update. |
95 | // Start application update. |
|
95 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
96 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
|
96 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
97 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
|
97 | |
98 | |
|
98 | _sparkle = new SparkleUpdater("https://zzz.grimore.org/update/appcast.xml", |
99 | _sparkle = new SparkleUpdater("https://zzz.grimore.org/update/appcast.xml", |
|
99 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
100 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
|
100 | { |
101 | { |
|
101 | UIFactory = new UIFactory(icon), |
102 | UIFactory = new UIFactory(icon), |
|
102 | RelaunchAfterUpdate = true, |
103 | RelaunchAfterUpdate = true, |
|
103 | SecurityProtocolType = SecurityProtocolType.Tls12 |
104 | SecurityProtocolType = SecurityProtocolType.Tls12 |
|
104 | }; |
105 | }; |
|
105 | _sparkle.StartLoop(true, true); |
106 | _sparkle.StartLoop(true, true); |
|
106 | } |
107 | } |
|
107 | |
108 | |
|
108 | private MainForm() |
109 | private MainForm() |
|
109 | { |
110 | { |
|
110 | _cancellationTokenSource = new CancellationTokenSource(); |
111 | _cancellationTokenSource = new CancellationTokenSource(); |
|
111 | _cancellationToken = _cancellationTokenSource.Token; |
112 | _cancellationToken = _cancellationTokenSource.Token; |
|
112 | |
113 | |
|
113 | ChangedConfigurationContinuation = new ScheduledContinuation(); |
114 | ChangedConfigurationContinuation = new ScheduledContinuation(); |
|
114 | } |
115 | } |
|
115 | |
116 | |
|
116 | /// <summary> |
117 | /// <summary> |
|
117 | /// Clean up any resources being used. |
118 | /// Clean up any resources being used. |
|
118 | /// </summary> |
119 | /// </summary> |
|
119 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
120 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
120 | protected override void Dispose(bool disposing) |
121 | protected override void Dispose(bool disposing) |
|
121 | { |
122 | { |
|
122 | if (disposing) |
123 | if (disposing) |
|
123 | { |
124 | { |
|
124 | components?.Dispose(); |
125 | components?.Dispose(); |
|
125 | } |
126 | } |
|
126 | |
127 | |
|
127 | base.Dispose(disposing); |
128 | base.Dispose(disposing); |
|
128 | } |
129 | } |
|
129 | |
130 | |
|
130 | #endregion |
131 | #endregion |
|
131 | |
132 | |
|
132 | #region Event Handlers |
133 | #region Event Handlers |
|
133 | |
134 | |
|
134 | private async void MainForm_Load(object sender, EventArgs e) |
135 | private async void MainForm_Load(object sender, EventArgs e) |
|
135 | { |
136 | { |
|
136 | Configuration = await LoadConfiguration(); |
137 | Configuration = await LoadConfiguration(); |
|
137 | |
138 | |
|
138 | // Initialize the idle timer. |
139 | // Initialize the idle timer. |
|
139 | _defaultIdler = new Idler(Configuration, Resources.Default); |
140 | _defaultIdler = new Idler(Configuration, Resources.Default); |
|
140 | if (Configuration.Enabled) |
141 | if (Configuration.Enabled) |
|
141 | { |
142 | { |
|
142 | _defaultIdler.Start(TimeSpan.FromMinutes((int)Configuration.Timeout)); |
143 | _defaultIdler.Start(TimeSpan.FromMinutes((int)Configuration.Timeout)); |
|
143 | } |
144 | } |
|
144 | // Bind to the idle notification. |
145 | // Bind to the idle notification. |
|
145 | _defaultIdler.Idle += DefaultIdler_Idle; |
146 | _defaultIdler.Idle += DefaultIdler_Idle; |
|
146 | _defaultIdler.IdleImminent += DefaultIdler_IdleImminent; |
147 | _defaultIdler.IdleImminent += DefaultIdler_IdleImminent; |
|
147 | |
148 | |
|
148 | _hibernateIdler = new Idler(Configuration, Resources.Hibernate); |
149 | _hibernateIdler = new Idler(Configuration, Resources.Hibernate); |
|
149 | if (Configuration.HibernateEnabled) |
150 | if (Configuration.HibernateEnabled) |
|
150 | { |
151 | { |
|
151 | _hibernateIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
152 | _hibernateIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
|
152 | } |
153 | } |
|
153 | _hibernateIdler.Idle += HibernateIdler_Idle; |
154 | _hibernateIdler.Idle += HibernateIdler_Idle; |
|
154 | _hibernateIdler.IdleImminent += HibernateIdler_IdleImminent; |
155 | _hibernateIdler.IdleImminent += HibernateIdler_IdleImminent; |
|
155 | |
156 | |
|
156 | toolStripEnabledMenuItem.Checked = Configuration.Enabled; |
157 | toolStripEnabledMenuItem.Checked = Configuration.Enabled; |
|
157 | hibernateToolStripMenuItem.Checked = Configuration.HibernateEnabled; |
158 | hibernateToolStripMenuItem.Checked = Configuration.HibernateEnabled; |
|
158 | |
159 | |
|
159 | // Initialize MQTT client. |
160 | // Initialize MQTT client. |
|
160 | _mqttClient = new MqttClient(Configuration); |
161 | _mqttClient = new MqttClient(Configuration); |
|
161 | _mqttClient.MqttSubscribeSucceeded += MqttClient_MqttSubscribeSucceeded; |
162 | _mqttClient.MqttSubscribeSucceeded += MqttClient_MqttSubscribeSucceeded; |
|
162 | _mqttClient.MqttSubscribeFailed += MqttClient_MqttSubscribeFailed; |
163 | _mqttClient.MqttSubscribeFailed += MqttClient_MqttSubscribeFailed; |
|
163 | _mqttClient.MqttStateReceived += MqttClient_MqttStateReceived; |
164 | _mqttClient.MqttStateReceived += MqttClient_MqttStateReceived; |
|
164 | _mqttClient.MqttActionReceived += MqttClient_MqttActionReceived; |
165 | _mqttClient.MqttActionReceived += MqttClient_MqttActionReceived; |
|
165 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
166 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
|
166 | _mqttClient.Start(); |
167 | _mqttClient.Start(); |
|
167 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
168 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
|
168 | |
169 | |
|
169 | } |
170 | } |
|
170 | |
171 | |
|
171 | private void LogViewToolStripMenuItem_Click(object sender, EventArgs e) |
172 | private void LogViewToolStripMenuItem_Click(object sender, EventArgs e) |
|
172 | { |
173 | { |
|
173 | if (_logViewForm != null) |
174 | if (_logViewForm != null) |
|
174 | { |
175 | { |
|
175 | return; |
176 | return; |
|
176 | } |
177 | } |
|
177 | |
178 | |
|
178 | lock (_memorySinkLock) |
179 | lock (_memorySinkLock) |
|
179 | { |
180 | { |
|
180 | _logViewForm = new LogViewForm(this, _memorySink, _memorySinkLock); |
181 | _logViewForm = new LogViewForm(this, _memorySink, _memorySinkLock); |
|
181 | _logViewForm.Closing += LogViewFormClosing; |
182 | _logViewForm.Closing += LogViewFormClosing; |
|
182 | _logViewForm.Show(); |
183 | _logViewForm.Show(); |
|
183 | } |
184 | } |
|
184 | } |
185 | } |
|
185 | |
186 | |
|
186 | private void LogViewFormClosing(object sender, CancelEventArgs e) |
187 | private void LogViewFormClosing(object sender, CancelEventArgs e) |
|
187 | { |
188 | { |
|
188 | if (_logViewForm == null) |
189 | if (_logViewForm == null) |
|
189 | { |
190 | { |
|
190 | return; |
191 | return; |
|
191 | } |
192 | } |
|
192 | |
193 | |
|
193 | _logViewForm.Closing -= LogViewFormClosing; |
194 | _logViewForm.Closing -= LogViewFormClosing; |
|
194 | _logViewForm.Close(); |
195 | _logViewForm.Close(); |
|
195 | _logViewForm = null; |
196 | _logViewForm = null; |
|
196 | } |
197 | } |
|
197 | |
198 | |
|
198 | private async void Trigger_Action(object sender, ActionEventArgs e) |
199 | private async void Trigger_Action(object sender, ActionEventArgs e) |
|
199 | { |
200 | { |
|
200 | if (Configuration.MqttEnable) |
201 | if (Configuration.MqttEnable) |
|
201 | { |
202 | { |
|
202 | await _mqttClient.Publish(e.Action); |
203 | await _mqttClient.Publish(e.Action); |
|
203 | } |
204 | } |
|
204 | } |
205 | } |
|
205 | |
206 | |
|
206 | private void MqttClient_MqttActionReceived(object sender, MqttActionReceivedEventArgs e) |
207 | private void MqttClient_MqttActionReceived(object sender, MqttActionReceivedEventArgs e) |
|
207 | { |
208 | { |
|
208 | Log.Information($"MQTT action {e.Action} received."); |
209 | Log.Information($"MQTT action {e.Action} received."); |
|
209 | |
210 | |
|
210 | _trigger.Execute(e.Action); |
211 | _trigger.Execute(e.Action); |
|
211 | } |
212 | } |
|
212 | |
213 | |
|
213 | private void MqttClient_MqttStateReceived(object sender, MqttStateReceivedEventArgs e) |
214 | private void MqttClient_MqttStateReceived(object sender, MqttStateReceivedEventArgs e) |
|
214 | { |
215 | { |
|
215 | Log.Information($"MQTT state {e.ZzzState.State} received."); |
216 | Log.Information($"MQTT state {e.ZzzState.State} received."); |
|
216 | |
217 | |
|
217 | switch (e.ZzzState.State) |
218 | switch (e.ZzzState.State) |
|
218 | { |
219 | { |
|
219 | case State.State.Enabled: |
220 | case State.State.Enabled: |
|
220 | Configuration.Enabled = true; |
221 | Configuration.Enabled = true; |
|
221 | |
222 | |
|
222 | this.InvokeIfRequired(form => |
223 | this.InvokeIfRequired(form => |
|
223 | { |
224 | { |
|
224 | form.toolStripEnabledMenuItem.Checked = true; |
225 | form.toolStripEnabledMenuItem.Checked = true; |
|
225 | form.toolStripEnabledMenuItem.CheckState = CheckState.Checked; |
226 | form.toolStripEnabledMenuItem.CheckState = CheckState.Checked; |
|
226 | |
227 | |
|
227 | form.notifyIcon1.BalloonTipText = |
228 | form.notifyIcon1.BalloonTipText = |
|
228 | Resources.Zzz_enabled; |
229 | Resources.Zzz_enabled; |
|
229 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; |
230 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; |
|
230 | form.notifyIcon1.BalloonTipTitle = Resources.Enabled; |
231 | form.notifyIcon1.BalloonTipTitle = Resources.Enabled; |
|
231 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
232 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
|
232 | }); |
233 | }); |
|
233 | break; |
234 | break; |
|
234 | case State.State.Disabled: |
235 | case State.State.Disabled: |
|
235 | Configuration.Enabled = false; |
236 | Configuration.Enabled = false; |
|
236 | |
237 | |
|
237 | this.InvokeIfRequired(form => |
238 | this.InvokeIfRequired(form => |
|
238 | { |
239 | { |
|
239 | form.toolStripEnabledMenuItem.Checked = false; |
240 | form.toolStripEnabledMenuItem.Checked = false; |
|
240 | form.toolStripEnabledMenuItem.CheckState = CheckState.Unchecked; |
241 | form.toolStripEnabledMenuItem.CheckState = CheckState.Unchecked; |
|
241 | |
242 | |
|
242 | form.notifyIcon1.BalloonTipText = |
243 | form.notifyIcon1.BalloonTipText = |
|
243 | Resources.Zzz_disabled; |
244 | Resources.Zzz_disabled; |
|
244 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; |
245 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; |
|
245 | form.notifyIcon1.BalloonTipTitle = Resources.Disabled; |
246 | form.notifyIcon1.BalloonTipTitle = Resources.Disabled; |
|
246 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
247 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
|
247 | }); |
248 | }); |
|
248 | break; |
249 | break; |
|
249 | } |
250 | } |
|
250 | } |
251 | } |
|
251 | |
252 | |
|
252 | private void MqttClient_MqttSubscribeFailed(object sender, MqttClientSubscribeResultCode e) |
253 | private void MqttClient_MqttSubscribeFailed(object sender, MqttClientSubscribeResultCode e) |
|
253 | { |
254 | { |
|
254 | Log.Warning(Resources.Unable_to_subscribe_to_MQTT_topic); |
255 | Log.Warning(Resources.Unable_to_subscribe_to_MQTT_topic); |
|
255 | } |
256 | } |
|
256 | |
257 | |
|
257 | private void MqttClient_MqttSubscribeSucceeded(object sender, MqttClientSubscribeResultCode e) |
258 | private void MqttClient_MqttSubscribeSucceeded(object sender, MqttClientSubscribeResultCode e) |
|
258 | { |
259 | { |
|
259 | Log.Information(Resources.Subscribed_to_MQTT_topic_and_waiting_for_messages); |
260 | Log.Information(Resources.Subscribed_to_MQTT_topic_and_waiting_for_messages); |
|
260 | } |
261 | } |
|
261 | |
262 | |
|
262 | private void DefaultIdler_IdleImminent(object sender, IdleImminentEventArgs e) |
263 | private void DefaultIdler_IdleImminent(object sender, IdleImminentEventArgs e) |
|
263 | { |
264 | { |
|
264 | this.InvokeIfRequired(form => |
265 | this.InvokeIfRequired(form => |
|
265 | { |
266 | { |
|
266 | form.notifyIcon1.BalloonTipText = |
267 | form.notifyIcon1.BalloonTipText = |
|
267 | Resources.Your_computer_is_becoming_idle_and_will_go_to_sleep_in_a_minute; |
268 | Resources.Your_computer_is_becoming_idle_and_will_go_to_sleep_in_a_minute; |
|
268 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning; |
269 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning; |
|
269 | form.notifyIcon1.BalloonTipTitle = Resources.Sleeping_soon; |
270 | form.notifyIcon1.BalloonTipTitle = Resources.Sleeping_soon; |
|
270 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
271 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
|
271 | }); |
272 | }); |
|
272 | } |
273 | } |
|
273 | |
274 | |
|
274 | private void DefaultIdler_Idle(object sender, IdleEventArgs e) |
275 | private void DefaultIdler_Idle(object sender, IdleEventArgs e) |
|
275 | { |
276 | { |
|
276 | Log.Information(Resources.Sleeping); |
277 | Log.Information(Resources.Sleeping); |
|
277 | |
278 | |
|
278 | // Sleep! |
279 | // Sleep! |
|
279 | _trigger.Execute(new ZzzAction(Configuration.Action)); |
280 | _trigger.Execute(new ZzzAction(Configuration.Action)); |
|
280 | } |
281 | } |
|
281 | |
282 | |
|
282 | private void HibernateIdler_IdleImminent(object sender, IdleImminentEventArgs e) |
283 | private void HibernateIdler_IdleImminent(object sender, IdleImminentEventArgs e) |
|
283 | { |
284 | { |
|
284 | this.InvokeIfRequired(form => |
285 | this.InvokeIfRequired(form => |
|
285 | { |
286 | { |
|
286 | form.notifyIcon1.BalloonTipText = |
287 | form.notifyIcon1.BalloonTipText = |
|
287 | Resources.Your_computer_will_enter_hiberation; |
288 | Resources.Your_computer_will_enter_hiberation; |
|
288 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning; |
289 | form.notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning; |
|
289 | form.notifyIcon1.BalloonTipTitle = Resources.Hibernating_soon; |
290 | form.notifyIcon1.BalloonTipTitle = Resources.Hibernating_soon; |
|
290 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
291 | form.notifyIcon1.ShowBalloonTip(TimeSpan.MaxValue.Milliseconds); |
|
291 | }); |
292 | }); |
|
292 | } |
293 | } |
|
293 | |
294 | |
|
294 | private void HibernateIdler_Idle(object sender, IdleEventArgs e) |
295 | private void HibernateIdler_Idle(object sender, IdleEventArgs e) |
|
295 | { |
296 | { |
|
296 | Log.Information(Resources.Hibernating); |
297 | Log.Information(Resources.Hibernating); |
|
297 | |
298 | |
|
298 | _trigger.Execute(new ZzzAction(Action.Action.Hibernate)); |
299 | _trigger.Execute(new ZzzAction(Action.Action.Hibernate)); |
|
299 | } |
300 | } |
|
300 | |
301 | |
|
301 | private void OnContextMenuQuitClick(object sender, EventArgs e) |
302 | private void OnContextMenuQuitClick(object sender, EventArgs e) |
|
302 | { |
303 | { |
|
303 | Environment.Exit(0); |
304 | Environment.Exit(0); |
|
304 | } |
305 | } |
|
305 | |
306 | |
|
306 | private void OnContextMenuAboutClick(object sender, EventArgs e) |
307 | private void OnContextMenuAboutClick(object sender, EventArgs e) |
|
307 | { |
308 | { |
|
308 | if (_aboutForm != null) |
309 | if (_aboutForm != null) |
|
309 | { |
310 | { |
|
310 | return; |
311 | return; |
|
311 | } |
312 | } |
|
312 | |
313 | |
|
313 | // Show the about form. |
314 | // Show the about form. |
|
314 | _aboutForm = new AboutForm(); |
315 | _aboutForm = new AboutForm(); |
|
315 | _aboutForm.Closing += AboutForm_Closing; |
316 | _aboutForm.Closing += AboutForm_Closing; |
|
316 | _aboutForm.Show(); |
317 | _aboutForm.Show(); |
|
317 | } |
318 | } |
|
318 | |
319 | |
|
319 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
320 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
|
320 | { |
321 | { |
|
321 | if (_aboutForm == null) |
322 | if (_aboutForm == null) |
|
322 | { |
323 | { |
|
323 | return; |
324 | return; |
|
324 | } |
325 | } |
|
325 | |
326 | |
|
326 | _aboutForm.Closing -= AboutForm_Closing; |
327 | _aboutForm.Closing -= AboutForm_Closing; |
|
327 | _aboutForm.Dispose(); |
328 | _aboutForm.Dispose(); |
|
328 | _aboutForm = null; |
329 | _aboutForm = null; |
|
329 | } |
330 | } |
|
330 | |
331 | |
|
331 | private void ToolStripMenuEnabledMenuItem_CheckedChanged(object sender, EventArgs e) |
332 | private void ToolStripMenuEnabledMenuItem_CheckedChanged(object sender, EventArgs e) |
|
332 | { |
333 | { |
|
333 | Configuration.Enabled = ((ToolStripMenuItem) sender).Checked; |
334 | Configuration.Enabled = ((ToolStripMenuItem) sender).Checked; |
|
334 | |
335 | |
|
335 | switch(Configuration.Enabled) |
336 | switch(Configuration.Enabled) |
|
336 | { |
337 | { |
|
337 | case true: |
338 | case true: |
|
338 | if(!_defaultIdler.IsRunning) |
339 | if(!_defaultIdler.IsRunning) |
|
339 | { |
340 | { |
|
340 | _defaultIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
341 | _defaultIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
|
341 | } |
342 | } |
|
342 | break; |
343 | break; |
|
343 | default: |
344 | default: |
|
344 | _defaultIdler.Stop(); |
345 | _defaultIdler.Stop(); |
|
345 | break; |
346 | break; |
|
346 | } |
347 | } |
|
347 | } |
348 | } |
|
348 | |
349 | |
|
349 | private void HibernateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
350 | private void HibernateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
|
350 | { |
351 | { |
|
351 | Configuration.HibernateEnabled = ((ToolStripMenuItem)sender).Checked; |
352 | Configuration.HibernateEnabled = ((ToolStripMenuItem)sender).Checked; |
|
352 | |
353 | |
|
353 | switch (Configuration.HibernateEnabled) |
354 | switch (Configuration.HibernateEnabled) |
|
354 | { |
355 | { |
|
355 | case true: |
356 | case true: |
|
356 | if (!_hibernateIdler.IsRunning) |
357 | if (!_hibernateIdler.IsRunning) |
|
357 | { |
358 | { |
|
358 | _hibernateIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
359 | _hibernateIdler.Start(TimeSpan.FromMinutes((int)Configuration.HibernateTimeout)); |
|
359 | } |
360 | } |
|
360 | break; |
361 | break; |
|
361 | default: |
362 | default: |
|
362 | _hibernateIdler.Stop(); |
363 | _hibernateIdler.Stop(); |
|
363 | break; |
364 | break; |
|
364 | } |
365 | } |
|
365 | } |
366 | } |
|
366 | |
367 | |
|
367 | private void OnNotifyIconMouseDoubleClick(object sender, MouseEventArgs e) |
368 | private void OnNotifyIconMouseDoubleClick(object sender, MouseEventArgs e) |
|
368 | { |
369 | { |
|
369 | if (e.Button == MouseButtons.Right) |
370 | if (e.Button == MouseButtons.Right) |
|
370 | { |
371 | { |
|
371 | return; |
372 | return; |
|
372 | } |
373 | } |
|
373 | |
374 | |
|
374 | Task.Delay((int)Configuration.ClickActionDelay).ContinueWith(task => |
375 | Task.Delay((int)Configuration.ClickActionDelay).ContinueWith(task => |
|
375 | { |
376 | { |
|
376 | _trigger.Execute(new ZzzAction(Configuration.ActionDoubleClick)); |
377 | _trigger.Execute(new ZzzAction(Configuration.ActionDoubleClick)); |
|
377 | }); |
378 | }); |
|
378 | } |
379 | } |
|
379 | |
380 | |
|
380 | private void OnNotifyIconMouseClick(object sender, MouseEventArgs e) |
381 | private void OnNotifyIconMouseClick(object sender, MouseEventArgs e) |
|
381 | { |
382 | { |
|
382 | if (e.Button == MouseButtons.Right) |
383 | if (e.Button == MouseButtons.Right) |
|
383 | { |
384 | { |
|
384 | return; |
385 | return; |
|
385 | } |
386 | } |
|
386 | |
387 | |
|
387 | Task.Delay((int)Configuration.ClickActionDelay).ContinueWith(task => |
388 | Task.Delay((int)Configuration.ClickActionDelay).ContinueWith(task => |
|
388 | { |
389 | { |
|
389 | _trigger.Execute(new ZzzAction(Configuration.ActionClick)); |
390 | _trigger.Execute(new ZzzAction(Configuration.ActionClick)); |
|
390 | }); |
391 | }); |
|
391 | } |
392 | } |
|
392 | |
393 | |
|
393 | private async void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
394 | private async void UpdateToolStripMenuItem_Click(object sender, EventArgs e) |
|
394 | { |
395 | { |
|
395 | // Manually check for updates, this will not show a ui |
396 | // Manually check for updates, this will not show a ui |
|
396 | var result = await _sparkle.CheckForUpdatesQuietly(); |
397 | var result = await _sparkle.CheckForUpdatesQuietly(); |
|
397 | if (result.Status == UpdateStatus.UpdateAvailable) |
398 | if (result.Status == UpdateStatus.UpdateAvailable) |
|
398 | { |
399 | { |
|
399 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
400 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
|
400 | _sparkle.ShowUpdateNeededUI(); |
401 | _sparkle.ShowUpdateNeededUI(); |
|
401 | return; |
402 | return; |
|
402 | } |
403 | } |
|
403 | |
404 | |
|
404 | MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.Zzz, MessageBoxButtons.OK, |
405 | MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.Zzz, MessageBoxButtons.OK, |
|
405 | MessageBoxIcon.Asterisk, |
406 | MessageBoxIcon.Asterisk, |
|
406 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
407 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
|
407 | } |
408 | } |
|
408 | |
409 | |
|
409 | private void ToolStripMenuItem4_Click(object sender, EventArgs e) |
410 | private void ToolStripMenuItem4_Click(object sender, EventArgs e) |
|
410 | { |
411 | { |
|
411 | if (_settingsForm != null) |
412 | if (_settingsForm != null) |
|
412 | { |
413 | { |
|
413 | return; |
414 | return; |
|
414 | } |
415 | } |
|
415 | |
416 | |
|
416 | _settingsForm = new SettingsForm(_mqttClient, Configuration); |
417 | _settingsForm = new SettingsForm(_mqttClient, Configuration); |
|
417 | _settingsForm.Closing += SettingsForm_Closing; |
418 | _settingsForm.Closing += SettingsForm_Closing; |
|
418 | _settingsForm.Show(); |
419 | _settingsForm.Show(); |
|
419 | } |
420 | } |
|
420 | |
421 | |
|
421 | private async void SettingsForm_Closing(object sender, CancelEventArgs e) |
422 | private async void SettingsForm_Closing(object sender, CancelEventArgs e) |
|
422 | { |
423 | { |
|
423 | if (_settingsForm == null) |
424 | if (_settingsForm == null) |
|
424 | { |
425 | { |
|
425 | return; |
426 | return; |
|
426 | } |
427 | } |
|
427 | |
428 | |
|
428 | _settingsForm.Closing -= SettingsForm_Closing; |
429 | _settingsForm.Closing -= SettingsForm_Closing; |
|
429 | _settingsForm.Dispose(); |
430 | _settingsForm.Dispose(); |
|
430 | _settingsForm = null; |
431 | _settingsForm = null; |
|
431 | |
432 | |
|
432 | // Commit the configuration. |
433 | // Commit the configuration. |
|
433 | ChangedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1), |
434 | ChangedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1), |
|
434 | async () => { await SaveConfiguration(); }, _cancellationToken); |
435 | async () => { await SaveConfiguration(); }, _cancellationToken); |
|
435 | |
436 | |
|
436 | Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot); |
437 | Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot); |
|
437 | |
438 | |
|
438 | // Update idle timer parameters. |
439 | // Update idle timer parameters. |
|
439 | _defaultIdler.IdleTimeout = TimeSpan.FromMinutes((int)Configuration.Timeout); |
440 | _defaultIdler.IdleTimeout = TimeSpan.FromMinutes((int)Configuration.Timeout); |
|
440 | _hibernateIdler.IdleTimeout = TimeSpan.FromMinutes((int)Configuration.HibernateTimeout); |
441 | _hibernateIdler.IdleTimeout = TimeSpan.FromMinutes((int)Configuration.HibernateTimeout); |
|
441 | |
442 | |
|
442 | // Restart MQTT client. |
443 | // Restart MQTT client. |
|
443 | try |
444 | try |
|
444 | { |
445 | { |
|
445 | await _mqttClient.Restart(); |
446 | await _mqttClient.Restart(); |
|
446 | } |
447 | } |
|
447 | catch (Exception ex) |
448 | catch (Exception ex) |
|
448 | { |
449 | { |
|
449 | Log.Warning(ex, "Unable to restart MQTT client."); |
450 | Log.Warning(ex, "Unable to restart MQTT client."); |
|
450 | } |
451 | } |
|
451 | } |
452 | } |
|
452 | |
453 | |
|
453 | #endregion |
454 | #endregion |
|
454 | |
455 | |
|
455 | #region Public Methods |
456 | #region Public Methods |
|
456 | public async Task SaveConfiguration() |
457 | public async Task SaveConfiguration() |
|
457 | { |
458 | { |
|
458 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
459 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
459 | { |
460 | { |
|
460 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
461 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
461 | } |
462 | } |
|
462 | |
463 | |
|
463 | switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration", |
464 | switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration", |
|
464 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
465 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
|
465 | CancellationToken.None)) |
466 | CancellationToken.None)) |
|
466 | { |
467 | { |
|
467 | case SerializationSuccess<Configuration.Configuration> _: |
468 | case SerializationSuccess<Configuration.Configuration> _: |
|
468 | Log.Information("Serialized configuration."); |
469 | Log.Information("Serialized configuration."); |
|
469 | break; |
470 | break; |
|
470 | case SerializationFailure serializationFailure: |
471 | case SerializationFailure serializationFailure: |
|
471 | Log.Warning(serializationFailure.Exception.Message, "Failed to serialize configuration."); |
472 | Log.Warning(serializationFailure.Exception.Message, "Failed to serialize configuration."); |
|
472 | break; |
473 | break; |
|
473 | } |
474 | } |
|
474 | } |
475 | } |
|
475 | |
476 | |
|
476 | public static async Task<Configuration.Configuration> LoadConfiguration() |
477 | public static async Task<Configuration.Configuration> LoadConfiguration() |
|
477 | { |
478 | { |
|
478 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
479 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
|
479 | { |
480 | { |
|
480 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
481 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
|
481 | } |
482 | } |
|
482 | |
483 | |
|
483 | var deserializationResult = |
484 | var deserializationResult = |
|
484 | await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile, |
485 | await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile, |
|
485 | Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None); |
486 | Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None); |
|
486 | |
487 | |
|
487 | switch (deserializationResult) |
488 | switch (deserializationResult) |
|
488 | { |
489 | { |
|
489 | case SerializationSuccess<Configuration.Configuration> serializationSuccess: |
490 | case SerializationSuccess<Configuration.Configuration> serializationSuccess: |
|
490 | return serializationSuccess.Result; |
491 | return serializationSuccess.Result; |
|
491 | case SerializationFailure serializationFailure: |
492 | case SerializationFailure serializationFailure: |
|
492 | Log.Warning(serializationFailure.Exception, "Failed to load configuration."); |
493 | Log.Warning(serializationFailure.Exception, "Failed to load configuration."); |
|
493 | return new Configuration.Configuration(); |
494 | return new Configuration.Configuration(); |
|
494 | default: |
495 | default: |
|
495 | return new Configuration.Configuration(); |
496 | return new Configuration.Configuration(); |
|
496 | } |
497 | } |
|
497 | } |
498 | } |
|
498 | |
499 | |
|
499 | #endregion |
500 | #endregion |
|
500 | } |
501 | } |
|
501 | } |
502 | } |
|
502 | |
503 | |
|
503 |
|
504 |
|
|
504 | |
505 | |
|
505 | |
506 | |
|
506 | |
507 | |